StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Example_read_xdf_makehist.C
1 // $Id: Example_read_xdf_makehist.C,v 1.9 2006/08/15 21:42:58 jeromel Exp $
2 // $Log: Example_read_xdf_makehist.C,v $
3 // Revision 1.9 2006/08/15 21:42:58 jeromel
4 // Fix rhic -> rhic.bnl.gov
5 //
6 // Revision 1.8 2000/06/05 16:35:35 kathy
7 // remove use of member function GetHeader since it is no longer available - now use memb functions of TTable
8 //
9 // Revision 1.7 2000/04/18 20:37:25 kathy
10 // St_DataSet,St_DataSetIter,St_Table classes are nowchanged to TDataSet,TDataSetIter,TTable
11 //
12 // Revision 1.6 2000/04/13 21:46:21 kathy
13 // remove loading of libtpc_Tables since l3Track table is now dst_track type from global
14 //
15 // Revision 1.5 2000/04/12 16:13:40 kathy
16 // have changed so that macro loads only table libraries needed instead of all table libraries
17 //
18 // Revision 1.4 2000/01/19 21:00:40 kathy
19 // update macros to use standard default xdf files in /afs/rhic.bnl.gov/star/data/samples
20 //
21 // Revision 1.3 2000/01/06 19:35:48 kathy
22 // change to use available xdf file as input
23 //
24 // Revision 1.2 1999/11/03 19:03:06 kathy
25 // changes to default input files and output file names - needed by perl script for testing
26 //
27 // Revision 1.1 1999/10/11 17:17:59 kathy
28 // changed names of some macros to make them more standard; changed default input file to MakeHists since previous no longer existed; combined some macros so that the one example will show all functionality
29 //
30 //
31 //=======================================================================
32 // owner: Kathy Turner
33 // what it does:
34 // Reads an xdf file, finds a table (dst/vertex) and fills 2
35 // histograms with variable "z" from the table. Draws hist. to
36 // canvas and sends to output ps file at the end.
37 //
38 //=======================================================================
39 
40 
41 void Example_read_xdf_makehist(
42  const Char_t *InputXdfFile=
43  "/afs/rhic.bnl.gov/star/data/samples/gstar.dst.xdf")
44 {
45  // load libraries
46  gSystem.Load("St_base");
47  gSystem.Load("xdf2root");
48  gSystem->Load("libglobal_Tables");
49  gSystem->Load("libgen_Tables");
50  gSystem->Load("libsim_Tables");
51 
52  // create instance of St_XDFFile called f1
53  // use method OpenXDF of St_XDFFile with instance f1 to open input file
54  St_XDFFile f1;
55  f1.OpenXDF(InputXdfFile);
56  // create a pointer to an TDataSet called record
57  TDataSet *record;
58  // point record to an event from the XDFFile previously opened to f1
59  // using method NextEventGet from St_XDFFile - get first record (run header)
60  record = f1.NextEventGet();
61 
62  // put record in browser
63  TBrowser b;
64  b.Add(record);
65 
66  // open output file:
67  TFile *hist_outfile = 0;
68  const Char_t *root_file = "Example_read_xdf_makehist.root";
69  hist_outfile = new TFile(root_file,"RECREATE");
70 
71  // book the 1d histogram
72  // I am allocating memory on the heap - saved, not deleted (by using new)
73  // Then am calling the constructor for TH1F
74  // To allocate memory on stack - deleted - and then call constructor do:
75  // TH1F h1("h1","my first hist",100,-10.,10.);
76  TH1F *h1 = new TH1F("h1","z position of prim vtx",100,-100.,100.);
77  // set x & y titles
78  h1->SetXTitle("vertex z position ");
79  h1->SetYTitle("num events");
80  TH1F *h2 = new TH1F("h2","z position of all vtx in event",100,-100.,100.);
81  h2->SetXTitle("vertex z position ");
82  h2->SetYTitle("num events");
83 
84 
85 // now passed header - want to loop over events
86  // create a pointer to an TDataSet called recorde
87  TDataSet *recorde=0;
88  Int_t ijk=0;
89 
90  while (recorde=f1.NextEventGet())
91  {
92 
93  // cout << " recorde = " << recorde << endl;
94 
95  ijk++;
96  cout << " ==> event # " << ijk << endl;
97  // create instance of DataSetIter - uses the event record defined previously
98  TDataSetIter roote(recorde);
99  // now want to go down to a table so create another dataset
100  TDataSet *sete=0;
101  // now cd to table globtrk
102  sete = roote.Cd("/dst/vertex");
103  cout << " find vertex table pointer = " << sete << endl;
104 // if pointer is zero, go back to top and read in next event
105 // last event is really end-run record, so it doesn't have the
106 // data on it. After this record is passed, the while loop ends
107  if (!sete) continue;
108  // now create pointer of type dst_vertex (vertex is of type dst_vertex)
109  St_dst_vertex *pdt=0;
110  // Set the pointer pdt = to sete which is already a pointer to vertex
111  // Must "cast" sete as a type St_dst_vertex for this to work !
112  // since sete is a pointer to an TDataSet and St_dst_vertex inherits
113  // from TDataSet, you can do this - then can use St_dst_vertex functions
114  pdt = (St_dst_vertex *)sete;
115 
116 
117 // get the table header data using member functions of TTable
118  cout << " table header info: name = " << pdt->GetName() << endl;
119  cout << " table header info: type = " << pdt->GetType() << endl;
120  cout << " table header info: #rows used = " << pdt->GetNRows() << endl;
121  cout << " table header info: #rows allocated = " << pdt->GetTableSize() << endl;
122  cout << " table header info: row size (bytes) = " << pdt->GetRowSize() << endl;
123  cout << " table header info: #columns = " << pdt->GetNumberOfColumns() << endl;
124 
125  pdt->ls();
126  // now get actual values in table
127  dst_vertex_st *tdt_v = pdt->GetTable();
128  // print out values:
129  cout << " prim vtx z : " << tdt_v->z << endl;
130  h1->Fill(tdt_v->z);
131  // Now setup loop over all rows in table and fill histogram
132  Int_t ij = 0;
133  for (ij=0; ij< pdt->GetNRows(); ij++)
134  {
135  // point to correct row & fill histogram
136  // dst_vertex_st *p = tdt_v[ij]; or = tdt_v++
137  h2->Fill(tdt_v[ij]->z);
138  }
139 }
140 
141  cout << " ==> finished loop" << endl;
142 
143  // create canvas to show results - this is how it is displayed
144  TCanvas *c1 = new TCanvas("c1"," from table dst/vertex",200,10,600,880);
145  // to set grid
146  c1->SetGrid();
147  // to do zone(1,2)
148  c1->Divide(1,2);
149  // can also do: c1->SetLogz();
150 
151 // open output ps file
152  TPostScript ps("Example_read_xdf_makehist.ps",111);
153 
154  // update histogram in canvas - can do this every event or at end of loop!
155  // Draw histogram - this should be done ONCE to link hist with canvas
156  h1->Draw();
157  c1->Update();
158  h2->Draw();
159  c1->Update();
160 
161  // Send to output file:
162  hist_outfile->Write();
163  hist_outfile->ls();
164 
165  // close ps file
166  ps.Close();
167 
168 }
169 
170 
171 
172 
virtual void ls(Option_t *option="") const
Definition: TDataSet.cxx:495