StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Example_read_dst_write_table_ntup.C
1 // $Id: Example_read_dst_write_table_ntup.C,v 1.6 2006/08/15 21:42:56 jeromel Exp $
2 // $Log: Example_read_dst_write_table_ntup.C,v $
3 // Revision 1.6 2006/08/15 21:42:56 jeromel
4 // Fix rhic -> rhic.bnl.gov
5 //
6 // Revision 1.5 2000/05/09 20:15:43 kathy
7 // transfer obsolete macros to /macros/obsolete; update other macros so that they use standard default inputs plus only few events by default so they'll be easy to run in autoQA macro testing
8 //
9 // Revision 1.4 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.3 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.2 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.1 2000/01/21 18:20:45 kathy
19 // now have macro that writes a table-based ntuple and another that reads the ntuple back in and draws from it
20 //
21 //
22 //=======================================================================
23 // owner: Kathy Turner
24 // what it does:
25 // - Reads an .dst.root produced from bfc
26 // - finds a table (dst/globtrk)
27 // - fills an ntuple with this table.
28 // - Draws some plots from the ntuple.
29 // - Writes out ntuple at end.
30 //=======================================================================
31 
32 
33 void Example_read_dst_write_table_ntup(
34  Int_t nevents=2,
35  const Char_t *MainFile=
36  "/afs/rhic.bnl.gov/star/data/samples/gstar.dst.root",
37  const Char_t *NtupFile="globtrk_ntup.root")
38 {
39 
40  cout << " nevents to process = " << nevents << endl;
41  cout << " Input file = " << MainFile << endl;
42  cout << " Output ntup file = " << NtupFile << endl;
43 
44  gSystem->Load("St_base");
45  gSystem->Load("StChain");
46 
47  gSystem->Load("libglobal_Tables");
48  gSystem->Load("libgen_Tables");
49  gSystem->Load("libsim_Tables");
50 
51  gSystem->Load("StIOMaker");
52  gSystem->Load("StarClassLibrary");
53  gSystem->Load("StUtilities");
54  gSystem->Load("StAnalysisUtilities");
55 
56 // Setup top part of chain
57  chain = new StChain("bfc");
58  chain->SetDebug();
59 
60 // setup chain with IOMaker - can read in .dst.root, .dst.xdf files
61  StIOMaker *IOMk = new StIOMaker("IO","r",MainFile,"bfcTree");
62  IOMk->SetDebug();
63  IOMk->SetIOMode("r");
64  IOMk->SetBranch("*",0,"0"); //deactivate all branches
65 // IOMk->SetBranch("tpc_tracks",0,"r"); //activate tpc_tracks Branch
66 // IOMk->SetBranch("geantBranch",0,"r"); //activate geant Branch
67  IOMk->SetBranch("dstBranch",0,"r"); //activate dst Branch
68 
69 
70 // construct output ntuple file
71  TFile *ntupfile = 0;
72 
73 // construct ntuple
74  St_TableNtuple *myNtuple=0;
75 
76 // --- now execute chain member functions
77  chain->Init();
78 
79 
80 // loop over events
81  for (int iev=0;iev<nevents; iev++)
82  {
83  chain->Clear();
84  int iret = chain->Make();
85  if (iret) break;
86 
87  cout << " !!!!! Now read event # " << iev << endl;
88 
89  TDataSet *ds=chain->GetDataSet("dst/globtrk");
90  TDataSetIter dsiter(ds);
91  St_dst_track *glob = (St_dst_track *) dsiter.Find("globtrk");
92 
93  // cout << " globtrk table pointer = " << glob << endl;
94 
95 // if pointer is zero, go back to top and read in next event
96 // last event is really end-run record, so it doesn't have the
97 // data on it. After this record is passed, the while loop ends
98 
99  if (!glob) continue;
100 
101 // Create ntuple & file in first event!
102 // Ntuple gets named "globtrk" here!
103 // Must have variable list setup already in order to define an ntuple
104 // We don't have this done until after we get to the table in the
105 // first event - so am defining the ntuple below.
106 // See Manuel's StMCAnalysisMaker class to see how to setup and fill
107 // a general (not necessarily table) ntuple!
108 // I *think* that this ntuple gets written to the last TFile opened!
109 //
110 
111  if (iev==0){
112  cout << " CREATE NTUPLE! " << endl;
113  ntupfile = new TFile(NtupFile,"RECREATE","My Ntuple");
114  myNtuple = new St_TableNtuple((St_Table&)*glob);
115  }
116 
117 
118 // fill ntuple
119  myNtuple->Fill((TTable&)*glob);
120 }
121 
122  cout << " ==> finished loop, now write ntuple to output file" << endl;
123 // write ntuple in memory to the Tfile:
124  myNtuple->Write();
125 
126 // draw some things from ntuple which is in memory
127  gStyle->SetOptStat(111111);
128  myNtuple->Draw("n_point");
129  myNtuple->Draw("r0:z0","n_point<50 && n_fit_point<40");
130  myNtuple->Draw("x_first0:x_first1","n_point<50 && n_fit_point<40");
131 
132  cout << " ==> close output file" << endl;
133 // close ntuple in TFile
134  ntupfile->Close();
135 
136 }
137 
138 
139 
140 
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
Definition: TTable.h:48