StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Example_read_dst_makehist.C
1 // $Id: Example_read_dst_makehist.C,v 1.10 2006/08/15 21:42:54 jeromel Exp $
2 // $Log: Example_read_dst_makehist.C,v $
3 // Revision 1.10 2006/08/15 21:42:54 jeromel
4 // Fix rhic -> rhic.bnl.gov
5 //
6 // Revision 1.9 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.8 2000/04/18 20:37:24 kathy
10 // St_DataSet,St_DataSetIter,St_Table classes are nowchanged to TDataSet,TDataSetIter,TTable
11 //
12 // Revision 1.7 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.6 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.5 2000/01/19 15:46:04 kathy
19 // change default input files to point to ones in /afs/rhic.bnl.gov/star/data/samples
20 //
21 // Revision 1.4 1999/11/30 20:04:17 kathy
22 // fix Example macros so that they work from .dst.root files or .dst.xdf files & update documentation; also had to change which values printed in *read_dst_print_tables* macro since the names have changed in dst tables
23 //
24 // Revision 1.3 1999/11/03 19:03:05 kathy
25 // changes to default input files and output file names - needed by perl script for testing
26 //
27 // Revision 1.2 1999/11/03 16:56:59 kathy
28 // fix macros to use StIOMaker instead of StTreeMaker
29 //
30 // Revision 1.1 1999/10/11 17:17:56 kathy
31 // 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
32 //
33 // Revision 1.1 1999/09/28 21:59:08 kathy
34 // add new macro to show how to read .dst.root file, find a value in a table and make a histogram of it and print out
35 //
36 //=======================================================================
37 // owner: Kathy Turner
38 // what it does:
39 // Reads an .dst.root or .dst.xdf file, finds a table (dst/vertex) and fills
40 // histogram with variable "z" from the table. Draws hist. to
41 // canvas and sends to output ps file at the end.
42 //
43 //=======================================================================
44 
45 
46 void Example_read_dst_makehist(
47  Int_t nevents=3,
48  const char *MainFile=
49 "/afs/rhic.bnl.gov/star/data/samples/gstar.dst.root")
50 {
51 
52  gSystem->Load("St_base");
53  gSystem->Load("StChain");
54 
55  gSystem->Load("libglobal_Tables");
56  gSystem->Load("libgen_Tables");
57  gSystem->Load("libsim_Tables");
58 
59  gSystem->Load("StIOMaker");
60  gSystem->Load("StarClassLibrary");
61 
62 
63 // Setup top part of chain
64  chain = new StChain("bfc");
65  chain->SetDebug();
66 
67 // setup chain with IOMaker - can read in .dst.root, .dst.xdf files
68  StIOMaker *IOMk = new StIOMaker("IO","r",MainFile,"bfcTree");
69  IOMk->SetDebug();
70  IOMk->SetIOMode("r");
71  IOMk->SetBranch("*",0,"0"); //deactivate all branches
72 // IOMk->SetBranch("tpc_tracks",0,"r"); //activate tpc_tracks Branch
73 // IOMk->SetBranch("geantBranch",0,"r"); //activate geant Branch
74  IOMk->SetBranch("dstBranch",0,"r"); //activate dst Branch
75 
76 
77 // --- now execute chain member functions
78  chain->Init();
79 
80 // open output hist file
81  TFile *hist_outfile = 0;
82  const Char_t *root_file = "Example_read_dst_makehist.root";
83  hist_outfile = new TFile(root_file,"RECREATE");
84 
85 // book histogram
86  TH1F *h1 = new TH1F("h1","z position of primary vertex",100,-100.,100.);
87  h1->SetXTitle("vertex z position ");
88  h1->SetYTitle("num events");
89  TH1F *h2 = new TH1F("h2","z position of all vtx in event",100,-100.,100.);
90  h2->SetXTitle("vertex z position ");
91  h2->SetYTitle("num events");
92 
93  for (int iev=0;iev<nevents; iev++)
94  {
95  chain->Clear();
96  int iret = chain->Make();
97  if (iret) break;
98 
99  cout << " !!!!! Now read event # " << iev << endl;
100 
101  TDataSet *ds=chain->GetDataSet("dst/vertex");
102  TDataSetIter dsiter(ds);
103  St_dst_vertex *vert = (St_dst_vertex *) dsiter.Find("vertex");
104 
105  cout << " vertex table pointer = " << vert << endl;
106 
107 // if pointer is zero, go back to top and read in next event
108 // last event is really end-run record, so it doesn't have the
109 // data on it. After this record is passed, the while loop ends
110  if (!vert) continue;
111 
112 
113 // get the table header data using member functions of TTable
114  cout << " table header info: name = " << vert->GetName() << endl;
115  cout << " table header info: type = " << vert->GetType() << endl;
116  cout << " table header info: #rows used = " << vert->GetNRows() << endl;
117  cout << " table header info: #rows allocated = " << vert->GetTableSize() << endl;
118  cout << " table header info: row size (bytes) = " << vert->GetRowSize() << endl;
119  cout << " table header info: #columns = " << vert->GetNumberOfColumns() << endl;
120 
121 
122  vert->ls();
123 
124 // get actual values in table
125 
126  dst_vertex_st *sth = vert->GetTable();
127  cout << " prim vtx z : " << sth->z << endl;
128 
129 // fill hist h1
130  h1->Fill(sth->z);
131 
132 // Now setup loop over all rows in table and fill histogram h2
133  Int_t ij = 0;
134  for (ij=0; ij< vert->GetNRows(); ij++)
135  {
136  h2->Fill(sth[ij]->z);
137  }
138 
139 }
140 
141  cout << " ==> finished loop" << endl;
142 
143  TCanvas *c1 = new TCanvas("c1"," from table dst/vertex",200,10,600,880);
144 
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  TPostScript ps("Example_read_dst_makehist.ps",111);
152 
153  // update histogram in canvas - can do this every event or at end of loop!
154  // Draw histogram - this should be done ONCE to link hist with canvas
155  h1->Draw();
156  c1->Update();
157  h2->Draw();
158  c1->Update();
159 
160  hist_outfile->Write();
161  hist_outfile->ls();
162 
163  ps.Close();
164 
165 }
166 
167 
168 
169 
virtual void SetIOMode(Option_t *iomode="w")
number of transactions
Definition: StIOInterFace.h:35
virtual void Clear(Option_t *option="")
User defined functions.
Definition: StChain.cxx:77
virtual Int_t Make()
Definition: StChain.cxx:110