00001 // $Id: Example_read_xdf_makehist.C,v 1.9 2006/08/15 21:42:58 jeromel Exp $ 00002 // $Log: Example_read_xdf_makehist.C,v $ 00003 // Revision 1.9 2006/08/15 21:42:58 jeromel 00004 // Fix rhic -> rhic.bnl.gov 00005 // 00006 // Revision 1.8 2000/06/05 16:35:35 kathy 00007 // remove use of member function GetHeader since it is no longer available - now use memb functions of TTable 00008 // 00009 // Revision 1.7 2000/04/18 20:37:25 kathy 00010 // St_DataSet,St_DataSetIter,St_Table classes are nowchanged to TDataSet,TDataSetIter,TTable 00011 // 00012 // Revision 1.6 2000/04/13 21:46:21 kathy 00013 // remove loading of libtpc_Tables since l3Track table is now dst_track type from global 00014 // 00015 // Revision 1.5 2000/04/12 16:13:40 kathy 00016 // have changed so that macro loads only table libraries needed instead of all table libraries 00017 // 00018 // Revision 1.4 2000/01/19 21:00:40 kathy 00019 // update macros to use standard default xdf files in /afs/rhic.bnl.gov/star/data/samples 00020 // 00021 // Revision 1.3 2000/01/06 19:35:48 kathy 00022 // change to use available xdf file as input 00023 // 00024 // Revision 1.2 1999/11/03 19:03:06 kathy 00025 // changes to default input files and output file names - needed by perl script for testing 00026 // 00027 // Revision 1.1 1999/10/11 17:17:59 kathy 00028 // 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 00029 // 00030 // 00031 //======================================================================= 00032 // owner: Kathy Turner 00033 // what it does: 00034 // Reads an xdf file, finds a table (dst/vertex) and fills 2 00035 // histograms with variable "z" from the table. Draws hist. to 00036 // canvas and sends to output ps file at the end. 00037 // 00038 //======================================================================= 00039 00040 00041 void Example_read_xdf_makehist( 00042 const Char_t *InputXdfFile= 00043 "/afs/rhic.bnl.gov/star/data/samples/gstar.dst.xdf") 00044 { 00045 // load libraries 00046 gSystem.Load("St_base"); 00047 gSystem.Load("xdf2root"); 00048 gSystem->Load("libglobal_Tables"); 00049 gSystem->Load("libgen_Tables"); 00050 gSystem->Load("libsim_Tables"); 00051 00052 // create instance of St_XDFFile called f1 00053 // use method OpenXDF of St_XDFFile with instance f1 to open input file 00054 St_XDFFile f1; 00055 f1.OpenXDF(InputXdfFile); 00056 // create a pointer to an TDataSet called record 00057 TDataSet *record; 00058 // point record to an event from the XDFFile previously opened to f1 00059 // using method NextEventGet from St_XDFFile - get first record (run header) 00060 record = f1.NextEventGet(); 00061 00062 // put record in browser 00063 TBrowser b; 00064 b.Add(record); 00065 00066 // open output file: 00067 TFile *hist_outfile = 0; 00068 const Char_t *root_file = "Example_read_xdf_makehist.root"; 00069 hist_outfile = new TFile(root_file,"RECREATE"); 00070 00071 // book the 1d histogram 00072 // I am allocating memory on the heap - saved, not deleted (by using new) 00073 // Then am calling the constructor for TH1F 00074 // To allocate memory on stack - deleted - and then call constructor do: 00075 // TH1F h1("h1","my first hist",100,-10.,10.); 00076 TH1F *h1 = new TH1F("h1","z position of prim vtx",100,-100.,100.); 00077 // set x & y titles 00078 h1->SetXTitle("vertex z position "); 00079 h1->SetYTitle("num events"); 00080 TH1F *h2 = new TH1F("h2","z position of all vtx in event",100,-100.,100.); 00081 h2->SetXTitle("vertex z position "); 00082 h2->SetYTitle("num events"); 00083 00084 00085 // now passed header - want to loop over events 00086 // create a pointer to an TDataSet called recorde 00087 TDataSet *recorde=0; 00088 Int_t ijk=0; 00089 00090 while (recorde=f1.NextEventGet()) 00091 { 00092 00093 // cout << " recorde = " << recorde << endl; 00094 00095 ijk++; 00096 cout << " ==> event # " << ijk << endl; 00097 // create instance of DataSetIter - uses the event record defined previously 00098 TDataSetIter roote(recorde); 00099 // now want to go down to a table so create another dataset 00100 TDataSet *sete=0; 00101 // now cd to table globtrk 00102 sete = roote.Cd("/dst/vertex"); 00103 cout << " find vertex table pointer = " << sete << endl; 00104 // if pointer is zero, go back to top and read in next event 00105 // last event is really end-run record, so it doesn't have the 00106 // data on it. After this record is passed, the while loop ends 00107 if (!sete) continue; 00108 // now create pointer of type dst_vertex (vertex is of type dst_vertex) 00109 St_dst_vertex *pdt=0; 00110 // Set the pointer pdt = to sete which is already a pointer to vertex 00111 // Must "cast" sete as a type St_dst_vertex for this to work ! 00112 // since sete is a pointer to an TDataSet and St_dst_vertex inherits 00113 // from TDataSet, you can do this - then can use St_dst_vertex functions 00114 pdt = (St_dst_vertex *)sete; 00115 00116 00117 // get the table header data using member functions of TTable 00118 cout << " table header info: name = " << pdt->GetName() << endl; 00119 cout << " table header info: type = " << pdt->GetType() << endl; 00120 cout << " table header info: #rows used = " << pdt->GetNRows() << endl; 00121 cout << " table header info: #rows allocated = " << pdt->GetTableSize() << endl; 00122 cout << " table header info: row size (bytes) = " << pdt->GetRowSize() << endl; 00123 cout << " table header info: #columns = " << pdt->GetNumberOfColumns() << endl; 00124 00125 pdt->ls(); 00126 // now get actual values in table 00127 dst_vertex_st *tdt_v = pdt->GetTable(); 00128 // print out values: 00129 cout << " prim vtx z : " << tdt_v->z << endl; 00130 h1->Fill(tdt_v->z); 00131 // Now setup loop over all rows in table and fill histogram 00132 Int_t ij = 0; 00133 for (ij=0; ij< pdt->GetNRows(); ij++) 00134 { 00135 // point to correct row & fill histogram 00136 // dst_vertex_st *p = tdt_v[ij]; or = tdt_v++ 00137 h2->Fill(tdt_v[ij]->z); 00138 } 00139 } 00140 00141 cout << " ==> finished loop" << endl; 00142 00143 // create canvas to show results - this is how it is displayed 00144 TCanvas *c1 = new TCanvas("c1"," from table dst/vertex",200,10,600,880); 00145 // to set grid 00146 c1->SetGrid(); 00147 // to do zone(1,2) 00148 c1->Divide(1,2); 00149 // can also do: c1->SetLogz(); 00150 00151 // open output ps file 00152 TPostScript ps("Example_read_xdf_makehist.ps",111); 00153 00154 // update histogram in canvas - can do this every event or at end of loop! 00155 // Draw histogram - this should be done ONCE to link hist with canvas 00156 h1->Draw(); 00157 c1->Update(); 00158 h2->Draw(); 00159 c1->Update(); 00160 00161 // Send to output file: 00162 hist_outfile->Write(); 00163 hist_outfile->ls(); 00164 00165 // close ps file 00166 ps.Close(); 00167 00168 } 00169 00170 00171 00172
1.5.9