StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StGammaTreeReader.cxx
1 #include "StGammaTreeReader.h"
2 
3 #include "TString.h"
4 #include <algorithm>
5 
6 ClassImp(StGammaTreeReader);
7 
8 struct ID {
9  int run;
10  int evt;
11 };
12 
13 
14 // ----------------------------------------------------------------------------
15 StGammaTreeReader::StGammaTreeReader(const Char_t *name,const Char_t *bname):StMaker(name)
16 {
17  mChain=new TChain(name,"gamma");
18  mBranchName=bname;
19  index=0;
20  mNumberOfFiles=0;
21  mIndexed=0;
22  mFirst=false;
23 }
24 
25 // ----------------------------------------------------------------------------
26 Int_t StGammaTreeReader::Init()
27 {
28  mEvent=new StGammaEvent();
29  mChain-> SetBranchAddress(mBranchName,&mEvent);
30  return StMaker::Init();
31 }
32 
33 // ----------------------------------------------------------------------------
35 {
36  // when run as a maker, retrieves each event in sequence
37  Int_t stat= getEvent(index++);
38  if ( !stat ) return kStEOF;
39  return kStOK;
40 }
41 
42 // ----------------------------------------------------------------------------
43 Int_t StGammaTreeReader::getEvent(Long64_t i)
44 {
45  if ( mFirst ) treeDetails();
46  Int_t stat=mChain->GetEntry(i);
47  return stat;
48 }
49 
50 Int_t StGammaTreeReader::getEvent(Int_t run, Int_t myevent)
51 {
52  if ( !mIndexed ) buildIndex();
53  Long64_t mykey = key(run,myevent);
54  Long64_t entry = mIndex[ mykey ];
55  if ( !entry ) {
56  mEvent->Clear();
57  assert(myevent);
58  return 0;
59  }
60  return getEvent( entry - 1 ); // entry is offset by +1 when we buildIndex
61 }
62 
63 void StGammaTreeReader::treeDetails()
64 {
65  mFirst=false;
66  std::cout << GetCVS() << std::endl;
67  std::cout << mChain->GetTitle() << std::endl;
68 }
69 
70 // ----------------------------------------------------------------------------
71 void StGammaTreeReader::Clear(Option_t *opts)
72 {
73 }
74 
75 // ----------------------------------------------------------------------------
76 void StGammaTreeReader::chainFile( const Char_t *file, const Char_t *matches )
77 {
78 
79  TString fname=file;
80  if ( fname.Contains(matches) ) // must be a root file
81  {
82  std::cout << Form("[%02i] add %s",mNumberOfFiles++,file ) << std::endl;
83  mChain->Add(fname);
84  }
85 
86 }
87 
88 // ----------------------------------------------------------------------------
89 void StGammaTreeReader::buildIndex()
90 {
91 
92  TStopwatch timer;
93  timer.Start();
94 
95  // loop over all entries in TTree
96  Long64_t N = getNumberOfEvents();
97  std::cout << GetName() << "::Indexing " << N << " entries" << std::endl;
98 
99  for ( Long64_t i=0;i<N;i++ )
100  {
101  if ( !getEvent(i) ) continue; // read in event
102  Int_t run = mEvent->runNumber();
103  Int_t event = mEvent->eventNumber();
104  Long64_t mykey = key(run,event); // generate key
105  mIndex[ mykey ] = i + 1; // add event to index, offset by 1 so that 0 is an error condition
106  if ( i < 10 ) {
107  std::cout << Form("[%i] RUN: %i EVENT: %i KEY: ",(Int_t)i,run,event) << mykey << " INDEX: " << i << std::endl;
108  }
109 
110  }
111  mIndexed=1;
112 
113  timer.Stop();
114 
115  std::cout << "[Time to build index for "<<N<<" events -- ";
116  std::cout << Form("real: %.2f s cpu: %.2f s]",timer.RealTime(),timer.CpuTime()) << std::endl;
117 
118 }
119 
120 // ----------------------------------------------------------------------------
122 {
123 
124  static Int_t ngood=0;
125 
126  Long64_t N = getNumberOfEvents();
127 
128  std::vector<ID> ids;
129  for ( Long64_t i=0;i<N;i++ )
130  {
131  ID myid ;
132  if ( !getEvent(i) ) continue;
133  myid.run = mEvent->runNumber();
134  myid.evt = mEvent->eventNumber();
135  ids.push_back(myid); // add this ID to list of IDs
136 
137  }
138 
139  // Shuffle events randomly
140  std::random_shuffle( ids.begin(), ids.end() );
141 
142  std::cout << Form("Verifying indexing scheme for %g events",(double)N)
143  << std::endl;
144 
145  for ( Long64_t i=0;i<N;i++ )
146  {
147 
148  if (!getEvent( ids[i].run, ids[i].evt ))continue;
149 
150 
151  Bool_t state=true;
152  if ( mEvent->runNumber() != ids[i].run ||
153  mEvent->eventNumber() != ids[i].evt ) {
154  mEvent->Print();
155  state=false;
156  }
157  else
158  ngood++;
159 
160  if ( !state )
161  std::cout << Form("run=%i event=%i state=%s",ids[i].run,ids[i].evt,"bad")
162  << std::endl;
163 
164 
165  assert( mEvent->runNumber() == ids[i].run ); // Run mismatch
166  assert( mEvent->eventNumber() == ids[i].evt ); // Event mismatch
167 
168  }
169  std::cout << Form("Verified %g of %g events were properly indexed",(double)ngood,(double)N) << std::endl;
170 
171 }
172 
173 Long64_t StGammaTreeReader::key( Int_t run, Int_t event )
174 {
175 
176  Long64_t mykey=0;
177  mykey = (Long64_t)run; // assign run number
178  //mykey = ( mykey << 32 ); // left shift it by 32 bits
179  mykey *= (Long64_t)1.0E10; // better, so the key is readable
180  mykey += event;
181 
182  //std::cout << Form("run=%i event=%i",run,event,mykey) << std::endl;
183  return mykey;
184 
185 }
Int_t runNumber() const
Return ith candidate.
Definition: StGammaEvent.h:73
void Test()
This is a &quot;self test&quot;. Do not feed it alot of data.
StGammaEvent * mEvent
Definition: Stypes.h:43
Long64_t getNumberOfEvents()
Returns the total number of entries for this chain of events.
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
Definition: Stypes.h:40
Int_t eventNumber() const
Returns run number.
Definition: StGammaEvent.h:74
void Clear(Option_t *opts="")
User defined functions.
void chainFile(const Char_t *name, const Char_t *matches=".root")
Add a file to the list of files in the chain being processed.
Int_t getEvent(Long64_t entry)
Loads the specified entry in the chain. Access using event() defined above.