EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EicRootManager.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EicRootManager.h
1 //
2 // AYK (ayk@bnl.gov), 2014/07/15
3 //
4 // For whatever reason neither TRef nor making friends work in my case,
5 // where eg in cbmsim->Draw() command like
6 //
7 // cbmsim->Draw("EicRcParticle.mc()->GetMomentum().Mag()");
8 //
9 // EicRcParticle.mc() accesses MCTrack entry internally, but respective
10 // branch does not get synchronized automatically;
11 //
12 // NB: this stuff is to be used in user ROOT macros only, like:
13 //
14 // TString mcInFile = "simulation.root";
15 // TString rcInFile = "reconstruction.root";
16 //
17 // EicRootManager *io = new EicRootManager(mcInFile, 0, rcInFile);
18 // TTree *rctree = io->GetEicRcTree();
19 //
20 // ... and now 'rctree' is the basic tree with EicRcEvent entries and
21 // all the links to other trees/branches are setup internally in a way
22 // they are accessible when needed to retrieve quantities like
23 //
24 // io->GetEicRcEvent()->GetTrack(0)->genmc()->GetPz()
25 //
26 
27 #include <map>
28 
29 #include <TClonesArray.h>
30 #include <TBranch.h>
31 #include <TTree.h>
32 #include <TFile.h>
33 
34 #include <EicRcEvent.h>
35 #include <EicEventAssembler.h>
36 #include <EicEventGenerator.h>
37 
38 #ifndef _EIC_ROOT_MANAGER_
39 #define _EIC_ROOT_MANAGER_
40 
41 #define _CBM_TREE_ "cbmsim"
42 
43 class EicRootInputFile: public TObject {
44  friend class EicRootManager;
45 
46  public:
48  EicRootInputFile(const char *fileName);
49  ~EicRootInputFile() { if (mFptr) mFptr->Close(); ResetVars(); };
50 
51  void ResetVars() {
52  mFptr = 0;
53  };
54 
55  private:
56  TTree *GetTree(const char *treeName) const { return (TTree*)mFptr->Get(treeName); };
57  // 2015/10/12: had to change 'void **addr' to 'void *addr' here and in EicRootManager::SetupBranch()
58  // in order to make this stuff work again under Jul'2015 bundled ROOT 5 version;
59  // otherwise errors in analysis.C like
60  //
61  // Error in <TTree::SetBranchAddress>: Unable to determine the type given for the address for "EicRcEvent"
62  //
63  // occured;
64  TBranch *SetupBranch(const char *treeName, const char *branchName, void *addr) const;
65 
66  TString mFileName;
67  TFile *mFptr;
68 
70 };
71 
72 class EicRootManager: public TObject {
73  public:
75  EicRootManager(const char *mocaInFile, const char *digiInFile,
76  const char *recoInFile, const char *assyInFile = 0);
77  EicRootManager(erhic::EventMC **generatorEventHack, TClonesArray *PndMCTracks,
78  TClonesArray *PndPidChargedCand, EicRcEvent **EicRcEventHack);
79  // FIXME: perform proper cleanup later;
81  if (mMoCaFile) delete mMoCaFile;
82  if (mDigiFile) delete mDigiFile;
83  if (mRecoFile) delete mRecoFile;
84  if (mAssyFile) delete mAssyFile;
85 
86  if (mPndMCTracks) {
87  mPndMCTracks->Delete();
88  delete mPndMCTracks;
89  } //if
90  if (mPndPidChargedCand) {
91  mPndPidChargedCand->Delete();
92  delete mPndPidChargedCand;
93  } //if
94 
95  ResetVars();
96 
97  mInstance = 0;
98  };
99 
100  void ResetVars() {
101  mGeneratorEvent = 0;
103  mEicRcEvent = 0;
104  mEicRcTree = 0;
106 
107  mEicRcEventHack = 0;
108 
111  };
112 
113  static EicRootManager* Instance() { return mInstance; };
114 
115  void SynchronizeBranch(TBranch *branch);
116 
117  const TClonesArray* GetPndMcTracks() const { return mPndMCTracks; };
118  const TClonesArray* GetPndPidCandidates() const { return mPndPidChargedCand; };
119 
120  TBranch* GetGenBranch() const { return mGeneratorBranch; };
121  TBranch* GetPndMcBranch() const { return mPndMcBranch; };
122  TBranch* GetPndPidBranch() const { return mPndPidBranch; };
123  TBranch* GetEicRcEventBranch() const { return mEicRcEventBranch; };
124 
125  const TTree *GetEicRcTree() const { return mEicRcTree; };
126 
127  // These calls will perform synchronization if needed;
128  const erhic::EventMC* GetGenMcEvent() const;
129  const erhic::ParticleMC* GetGenMcTrack(int genMcIndex) const;
130 
131  const EicRcEvent *GetEicRcEvent() const {
133  };
134 
135  // Just two shortcuts (bypass calling GetEicRcTree() in analysis.C); return values
136  // match ROOT GetEntries() and GetEntry() call types of course;
137  Long64_t GetEicRcTreeEntries() const { return mEicRcTree ? mEicRcTree->GetEntries() : 0; }
138  Int_t GetEicRcTreeEntry(unsigned ev) const { return mEicRcTree ? mEicRcTree->GetEntry(ev) : -1; }
139 
140  private:
142 
143  // FIXME: the easiest for now; if performance becomes an issue,
144  // optimize for efficiency;
145  std::map<const TBranch*, int> mBranchStatus;
146 
148 
152 
155  TTree *mEicRcTree;
157 
158  // Assume there are at most 4 types of input files with predefined tree/branch
159  // combinations which may require synchronization;
164 
165  TBranch *SetupBranch(const EicRootInputFile *tfile, const char *treeName,
166  const char *branchName, void *addr);
167 
168  // These calls are just to access respective pointers;
171  };
172 
174 };
175 
176 #endif