StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StJetReader.cxx
1 //M.L. Miller
2 //MIT Software
3 //6/04
4 
5 //std
6 #include <map>
7 #include <string>
8 #include <algorithm>
9 #include <iostream>
10 
11 
12 //StEmc
13 #include "StEmcClusterCollection.h"
14 #include "StEmcPoint.h"
15 #include "StEmcUtil/geometry/StEmcGeom.h"
16 #include "StEmcUtil/others/emcDetectorName.h"
17 #include "StEmcADCtoEMaker/StBemcData.h"
18 #include "StEmcADCtoEMaker/StEmcADCtoEMaker.h"
19 
20 //root
21 #include "TTree.h"
22 #include "TFriendElement.h"
23 #include "TFile.h"
24 
25 //StMuDst
26 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
27 #include "StMuDSTMaker/COMMON/StMuDst.h"
28 #include "StMuDSTMaker/COMMON/StMuEvent.h"
29 
30 //StJetMaker
31 #include "StSpinPool/StJets/StJet.h"
32 #include "StSpinPool/StJets/StJets.h"
33 #include "StJetMaker/StJetReader.h"
34 #include "StSpinPool/StJetSkimEvent/StJetSkimEvent.h"
35 
36 ClassImp(StJetReader);
37 
38 StJetReader::StJetReader(const char* name)
39  : StMaker(name)
40  , mFile(0)
41  , mTree(0)
42  , mSkimFile(0)
43  , mSkimTree(0)
44  , mValid(false)
45  , mSkimEvent(0)
46  , mOfstream(0)
47 {
48  LOG_INFO << "StJetReader::StJetReader()" << endm;
49 }
50 
51 StJetReader::~StJetReader()
52 {
53  LOG_INFO << "StJetReader::~StJetReader()" << endm;
54 }
55 
56 Int_t StJetReader::Init()
57 {
58  return kStOk;
59 }
60 
61 void StJetReader::InitFile(const char* file)
62 {
63  LOG_INFO << "StJetReader::InitFile()" << endm;
64 
65  LOG_INFO << "open file:\t" << file << "\tfor reading" << endm;
66  mFile = TFile::Open(file);
67  assert(mFile);
68 
69  LOG_INFO << "recover tree" << endm;
70  mTree = dynamic_cast<TTree*>(mFile->Get("jet"));
71  assert(mTree);
72 
73  // Build index table to access jets by run number and event number
74  int nentries = mTree->BuildIndex("mRunNumber","mEventNumber");
75  if (nentries < 0) {
76  LOG_WARN << "Could not build jet tree index: nentries=" << nentries << endm;
77  }
78  else {
79  LOG_INFO << "Built jet tree index: nentries=" << nentries << endm;
80  }
81 
82  LOG_INFO << "\tset tree pointer" << endm;
83  LOG_INFO << "Number of entries in tree:\t" << mTree->GetEntriesFast() << endm;
84 
85  LOG_INFO << "\tGet Branches" << endm;
86  TObjArray* branches = mTree->GetListOfBranches();
87  if (!branches) {
88  LOG_ERROR << "StJetReader::InitFile(). Null branches" << endm;
89  abort();
90  }
91 
92  LOG_INFO << "\tLoop on branches" << endm;
93 
94  for (int i=0; i<branches->GetEntriesFast(); ++i) {
95  TBranch* branch = dynamic_cast<TBranch*>(branches->At(i));
96  if (!branch) {
97  LOG_ERROR << "StJetReader::InitFile(). Null branch" << endm;
98  abort();
99  }
100 
101  LOG_INFO << "\t--- Found branch:\t" << branch->GetName() << endm;
102  }
103 
104  if (0) {
105  string jetCheck(file);
106  jetCheck += ".read.txt";
107  mOfstream = new ofstream(jetCheck.c_str());
108  }
109 
110  LOG_INFO << "\tfinished!" <<endm;
111 
112  return;
113 }
114 
115 void StJetReader::InitJetSkimFile(const char* file)
116 {
117  LOG_INFO << "StJetReader::InitJetSkimFile(const char* file)" << endm;
118  LOG_INFO << "open file:\t" << file << "\tfor reading" << endm;
119 
120  mSkimFile = TFile::Open(file);
121  assert(mSkimFile);
122 
123  mSkimTree = dynamic_cast<TTree*>(mSkimFile->Get("jetSkimTree"));
124  assert(mSkimTree);
125 
126  // Build index table to access skim events by run number and event number
127  int nentries = mSkimTree->BuildIndex("mRunId","mEventId");
128  if (nentries < 0) {
129  LOG_WARN << "Could not build skim tree index: nentries=" << nentries << endm;
130  }
131  else {
132  LOG_INFO << "Built skim tree index: nentries=" << nentries << endm;
133  }
134 
135  mSkimEvent = new StJetSkimEvent;
136 
137  //set branch status
138  mSkimTree->SetBranchStatus("skimEventBranch", 1);
139  mSkimTree->SetBranchAddress("skimEventBranch", &mSkimEvent);
140 
141  LOG_INFO << "successfully recovered tree from file." << endm;
142 }
143 
144 void StJetReader::InitTree(TTree* tree)
145 {
146  LOG_INFO << "StJetReader::InitTree()" << endm;
147 
148  LOG_INFO << "\tset tree pointer" << endm;
149  LOG_INFO << "Number of entries in tree:\t" << tree->GetEntriesFast() << endm;
150 
151  TList* friendList = tree->GetListOfFriends();
152  TTree* t=0;
153  for (int j=0; j<friendList->GetSize(); ++j) {
154  TFriendElement* fr = static_cast<TFriendElement*>( friendList->At(j) );
155  string tree_name( fr->GetTreeName() );
156  if (tree_name == "jet") {
157  t = fr->GetTree();
158  break;
159  }
160  }
161  assert(t);
162 
163  LOG_INFO << "\tGet Branches" << endm;
164  TObjArray* branches = t->GetListOfBranches();
165  if (!branches) {
166  LOG_ERROR << "StJetReader::InitFile(). Null branches" << endm;
167  abort();
168  }
169 
170  LOG_INFO << "\tLoop on branches" << endm;
171 
172  for (int i=0; i<branches->GetEntriesFast(); ++i) {
173  TBranch* branch = dynamic_cast<TBranch*>(branches->At(i));
174  if (!branch) {
175  LOG_INFO << "StJetReader::InitFile(). Null branch" << endm;
176  abort();
177  }
178 
179  LOG_INFO << "\t--- Found branch:\t" << branch->GetName() << endm;
180  }
181 
182  LOG_INFO << "\tfinished!" << endm;
183 
184  return;
185 }
186 
188 {
189  LOG_INFO << "StJetReader::preparedForDualRead()" << endm;
190  int jetStatus = mTree != 0;
191  int skimStatus = mSkimTree != 0;
192  LOG_INFO << "jet tree status:\t" << jetStatus << endm;
193  LOG_INFO << "skim tree status:\t" << skimStatus << endm;
194  assert(mTree);
195  assert(mSkimTree);
196 
197  int nJetEvents = mTree->GetEntriesFast();
198  int nSkimEvents = mSkimTree->GetEntriesFast();
199  LOG_INFO << "nJetEvents:\t" << nJetEvents << endm;
200  LOG_INFO << "nSkimEvents:\t" << nSkimEvents << endm;
201  assert(nJetEvents == nSkimEvents);
202  assert(mSkimEvent);
203 
204  LOG_INFO << "Congratulations, you are ready to process events!" << endm;
205  mValid = true;
206  return 1;
207 }
208 
210 {
211  if (mTree && mTree->GetEntryWithIndex(GetRunNumber(),GetEventNumber()) < 0) {
212  LOG_WARN << Form("Could not find jet tree entry for run=%d event=%d",GetRunNumber(),GetEventNumber()) << endm;
213  return kStWarn;
214  }
215 
216  if (mSkimTree && mSkimTree->GetEntryWithIndex(GetRunNumber(),GetEventNumber()) < 0) {
217  LOG_WARN << Form("Could not find skim tree entry for run=%d event=%d",GetRunNumber(),GetEventNumber()) << endm;
218  return kStWarn;
219  }
220 
221  return kStOk;
222 }
223 
225 {
226  if (mOfstream) {
227  delete mOfstream;
228  mOfstream=0;
229  }
230 
231  return kStOk;
232 }
233 
234 void dumpProtojetToStream(int event, ostream& os, StJets* stjets);
235 
236 string idString(TrackToJetIndex* t)
237 {
238  string idstring;
239  StDetectorId mDetId = t->detectorId();
240  if (mDetId==kTpcId) {
241  idstring = "kTpcId";
242  }
243  else if (mDetId==kBarrelEmcTowerId) {
244  idstring = "kBarrelEmcTowerId";
245  }
246  else if (mDetId==kEndcapEmcTowerId) {
247  idstring = "kEndcapEmcTowerId";
248  }
249  else {
250  idstring = "kUnknown";
251  }
252  return idstring;
253 }
254 
255 //nice check to verify that the jet 4-mom is equal to the _vector_ sum of it's part
256 bool verifyJet(StJets* stjets, int ijet)
257 {
258  TClonesArray& jets = *(stjets->jets());
259  StJet* pj = static_cast<StJet*>(jets[ijet]);
260 
261 
262  StThreeVectorD j3(pj->Px(), pj->Py(), pj->Pz());
263  StLorentzVectorD j4(pj->E(),j3 );
264 
265  //LOG_INFO <<"\tjet:\t"<<ijet<<"\t4mom:\t"<<j4<<endm;
266 
267  StLorentzVectorD jetMom(0., 0., 0., 0.);
268 
269  typedef vector<TLorentzVector*> FourpList;
270  FourpList particles = stjets->particles(ijet);
271 
272  for (FourpList::iterator it=particles.begin(); it!=particles.end(); ++it) {
273  TLorentzVector* v = *it;
274  StThreeVectorD v3(v->Px(), v->Py(), v->Pz() );
275  StLorentzVectorD v4(v->E(), v3 );
276  jetMom += v4;
277  //LOG_INFO <<"\t\t4p:\t"<<v4<<endm;
278  }
279  //LOG_INFO <<"\t\t\tcheck:\t"<<jetMom<<endm;
280  StLorentzVectorD diff = j4-jetMom;
281  if (abs(diff)>1.e-6) { //they have to be the same to 1 eV
282  LOG_INFO << "verifyJet. assert will fail for jet:\t" << ijet << "\t4p:\t" << j4 <<"\tcompared to sum_particles:\t" << jetMom << endm;
283  return false;
284  }
285  else {
286  return true;
287  }
288 }
289 
290 
292 {
293  LOG_INFO << "StJetReader::exampleEventAna()" << endm;
294 
295  StJetSkimEvent* skEv = skimEvent();
296 
297  //basic information:
298  LOG_INFO << "fill/run/event:\t" << skEv->fill() << "\t" << skEv->runId() << "\t" << skEv->eventId() << endm;
299  LOG_INFO << "fileName:\t" << skEv->mudstFileName().GetString() << endm;
300 
301  //some spin info:
302  LOG_INFO << "bx48:\t" << skEv->bx48() << "\tspinBits:\t" << skEv->spinBits() << endm;
303 
304  //some event info:
305  LOG_INFO << "Ebbc:\t" << skEv->eBbc() << "\tWbbc:\t" << skEv->wBbc() << "\tbbcTimeBin:\t" << skEv->bbcTimeBin() << endm;
306 
307  //best vertex info:
308  const float* bestPos = skEv->bestVert()->position();
309  LOG_INFO << "best Vertex (x,y,z):\t" << bestPos[0] << "\t" << bestPos[1] << "\t" << bestPos[2] << endm;
310 
311  //trigger info:
312  const TClonesArray* trigs = skEv->triggers();
313  assert(trigs);
314  int nTrigs = trigs->GetEntriesFast();
315  for (int i=0; i<nTrigs; ++i) {
316  StJetSkimTrig* trig = static_cast<StJetSkimTrig*>( trigs->At(i) );
317  assert(trig);
318  if (trig->didFire() != 0) {
319  //LOG_INFO << "\tTriggerd:\t" << trig->trigId() << "\twith prescale:\t" << trig->prescale() << endm;
320  }
321  if (trig->shouldFireL2() == 1) {
322  LOG_INFO << "\tshTrigger L2: " << trig->trigId() << endm;
323  const int* l2temp = trig->L2ResultEmulated();
324  for (int ii = 0; ii < 9; ++ii) {
325  LOG_INFO << "SimL2--- " << ii << "\t" << l2temp[ii] << endm;
326  }
327  }
328  }
329 
330  //L2 Info:
331  LOG_INFO << "\n--- Non-zero L2 Results:" << endm;
332  const int* l2temp = skEv->L2Result();
333  for (int ii=0; ii<9; ii++) {
334  if (l2temp[ii]!=0) LOG_INFO << "DatL2--- " << ii << "\t" << l2temp[ii] << endm;
335  }
336 
337  //vertex info:
338  const TClonesArray* verts = skEv->vertices();
339  assert(verts);
340  int nVerts = verts->GetEntriesFast();
341  for (int i=0; i<nVerts; ++i) {
342  StJetSkimVert* vert = static_cast<StJetSkimVert*>( verts->At(i) );
343  assert(vert);
344  const float* vertPos = vert->position();
345  LOG_INFO << "vert:\t" << i << "\t at z:\t" << vertPos[2] << "\tranking:\t"
346  << vert->ranking() << "\tnTracks:\t" << vert->nTracksUsed() << endm;
347  }
348 
349  //And then access to jets from different algorithms...
350  LOG_INFO << "\nLoop on jet branches" << endm;
351  for (int i = 0; i < numberOfBranches(); ++i) {
352  StJets* stjets = getStJets(i);
353  int nJets = stjets->nJets();
354  LOG_INFO << "Found\t" << nJets << "\tjets from:\t" << branchName(i) << endm;
355 
356  //first make sure that we have the same run/event:
357  assert(stjets->runId()==skEv->runId());
358  assert(stjets->eventId()==skEv->eventId());
359 
360  TClonesArray* jets = stjets->jets();
361 
362  for(int ijet=0; ijet<nJets; ++ijet){
363 
364  //loop on jets
365  StJet* j = dynamic_cast<StJet*>( jets->At(ijet) );
366  assert(j);
367  //assert(verifyJet(stjets, ijet));
368 
369  LOG_INFO <<"jet:\t"<<ijet<<"\tEjet:\t"<<j->E()<<"\tEta:\t"<<j->Eta()<<"\tPhi:\t"<<j->Phi()<<"\tdetEta:\t"<<j->detEta()<<endm;
370 
371  //look at 4-momenta in the jet:
372  typedef vector<TLorentzVector*> TrackToJetVec;
373  TrackToJetVec particles = stjets->particles(ijet);
374 
375  for (TrackToJetVec::iterator partIt=particles.begin(); partIt!=particles.end(); ++partIt) {
376  const TLorentzVector* theParticle = *partIt;
377  LOG_INFO <<"\tparticle \t pt:\t"<<theParticle->Pt()<<"\teta:\t"<<theParticle->Eta()<<"\tphi:\t"<<theParticle->Phi()<<endm;
378  }
379  }
380  }
381 }
382 
384 {
385  LOG_INFO <<"StJetReader::exampleEventAna()"<<endm;
386 
387  LOG_INFO <<"nPrimary:\t"<<StMuDst::numberOfPrimaryTracks() << endm;
388 
389  for (int i = 0; i < numberOfBranches(); ++i) {
390  StJets* stjets = getStJets(i);
391  int nJets = stjets->nJets();
392  LOG_INFO <<"Found\t"<<nJets<<"\tjets from:\t"<<branchName(i)<<endm;
393 
394  TClonesArray* jets = stjets->jets();
395 
396  //Dylan, here's a nice check...
397  if (0) {
398  if (stjets->nJets()>0) {
399  dumpProtojetToStream(StMuDst::event()->eventId(), *mOfstream, stjets);
400  }
401  }
402 
403  for(int ijet=0; ijet<nJets; ++ijet){
404 
405  //loop on jets
406  StJet* j = dynamic_cast<StJet*>( jets->At(ijet) );
407  assert(j);
408  assert(verifyJet(stjets, ijet));
409 
410  LOG_INFO <<"jet:\t"<<ijet<<"\tEjet:\t"<<j->E()<<"\tEta:\t"<<j->Eta()<<"\tPhi:\t"<<j->Phi()<<endm;
411 
412  //look at 4-momenta in the jet:
413  typedef vector<TLorentzVector*> TrackToJetVec;
414  TrackToJetVec particles = stjets->particles(ijet);
415  }
416  }
417 }
418 
420 {
421  return mTree->GetNbranches();
422 }
423 
424 const char* StJetReader::branchName(int i) const
425 {
426  TBranch* branch = (TBranch*)mTree->GetListOfBranches()->At(i);
427  return branch ? branch->GetName() : 0;
428 }
429 
430 StJets* StJetReader::getStJets(int i) const
431 {
432  TBranch* branch = (TBranch*)mTree->GetListOfBranches()->At(i);
433  return branch ? *(StJets**)branch->GetAddress() : 0;
434 }
435 
436 StJets* StJetReader::getStJets(const char* bname) const
437 {
438  TBranch* branch = mTree->GetBranch(bname);
439  return branch ? *(StJets**)branch->GetAddress() : 0;
440 }
virtual Int_t Make()
virtual void InitJetSkimFile(const char *file)
Recover the &quot;fast&quot; tree of StJetSkimEvent.
int preparedForDualRead()
Check if we are all ready to read the Skim and StjJet trees together.
void exampleFastAna()
An example analysis method to read StJetSkimEvent and StJets trees together.
virtual void InitFile(const char *file)
Recover the TTree from file and prepare for reading.
Definition: StJetReader.cxx:61
StJetReader(const char *name="JetReader")
The constructor requires a valid instance of StMuDstMaker.
Definition: StJetReader.cxx:38
void exampleEventAna()
An example analysis method, look here for a demonstration of jet/track histogramming.
TClonesArray * jets()
Access to the jets in this event.
Definition: StJets.h:42
StJetSkimEvent * skimEvent() const
Acces to the StJetSkimEvent.
Definition: StJetReader.h:71
Definition: StJets.h:24
int nJets() const
The number of jets found in this event.
Definition: StJets.h:39
int eventId() const
access to event numbers, used to synchronize with StMuDstMaker for simultaneous reading ...
Definition: StJets.h:59
virtual Int_t Finish()
virtual void InitTree(TTree *tree)
int numberOfBranches() const
Access to jet branches.
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
Definition: Stypes.h:42
virtual Int_t GetRunNumber() const
Returns the current RunNumber.
Definition: StMaker.cxx:1054
Definition: StJet.h:91
Definition: Stypes.h:41