EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EicRootManager.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EicRootManager.cxx
1 //
2 // AYK (ayk@bnl.gov), 2014/07/15
3 //
4 // Trivial input branch synchronization hack;
5 //
6 
7 #include <assert.h>
8 
9 #include <EicRootManager.h>
10 
12 
13 // ---------------------------------------------------------------------------------------
14 
15 EicRootManager::EicRootManager(const char *mocaInFile, const char *digiInFile,
16  const char *recoInFile, const char *assyInFile)
17 {
18  ResetVars();
19 
20  if (mInstance) {
21  printf("EicRootManager is a singleton instance, do not try to initialize twice!\n");
22  return;
23  } //if
24 
25  // Well, yes: assign right away here;
26  mInstance = this;
27 
28  // Initialize input files; do not mind to unify few lines here;
29  if (mocaInFile) mMoCaFile = new EicRootInputFile(mocaInFile);
30  if (digiInFile) mDigiFile = new EicRootInputFile(digiInFile);
31  if (recoInFile) mRecoFile = new EicRootInputFile(recoInFile);
32  // Typically EicEventAssembler task will be launched from reconstruction.C script;
33  // allow to run it separately though;
34  if (recoInFile || assyInFile)
35  mAssyFile = new EicRootInputFile(assyInFile ? assyInFile : recoInFile);
36 
37  // Initialize branches; request certain synchronization which ROOT can provide on its own;
38  if (mAssyFile) {
40  if (!mEicRcTree) return;
41 
43  } //if
44 
45  if (mMoCaFile) {
47 
49  (void **)&mGeneratorEvent);
50  } //if
51 
52  if (mRecoFile)
54  (void **)&mPndPidChargedCand);
55 } // EicRootManager::EicRootManager()
56 
57 // ---------------------------------------------------------------------------------------
58 
59 EicRootManager::EicRootManager(erhic::EventMC **generatorEventHack, TClonesArray *PndMCTracks,
60  TClonesArray *PndPidChargedCand, EicRcEvent **EicRcEventHack)
61 {
62  ResetVars();
63 
64  if (mInstance) {
65  printf("EicRootManager is a singleton instance, do not try to initialize twice!\n");
66  return;
67  } //if
68 
69  // Well, yes: assign right away here;
70  mInstance = this;
71 
72  mGeneratorEventHack = generatorEventHack;
73  mPndMCTracks = PndMCTracks;
74  mPndPidChargedCand = PndPidChargedCand;
75  mEicRcEventHack = EicRcEventHack;
76 } // EicRootManager::EicRootManager()
77 
78 // ---------------------------------------------------------------------------------------
79 
80 //
81 // FIXME: at some point figure out how to load these branches automatically;
82 //
83 
84 void EicRootManager::SynchronizeBranch(TBranch *branch)
85 {
86  // This check is sufficient for the same code to work in production mode (direct
87  // access to preloaded TCloneArray pointers) as well as user mode (brach
88  // synchronization needed);
89  if (!branch) return;
90 
91  // Yes, assume mEicRcTree governs synchronization; FIXME: what a crap!;
92  int currentEntry = mEicRcTree->GetReadEntry();
93 
94  if (mBranchStatus.at(branch) != currentEntry) {
95  branch->GetEntry(currentEntry);
96 
97  mBranchStatus[branch] = currentEntry;
98  } //if
99 } // EicRootManager::SynchronizeBranch()
100 
101 // ---------------------------------------------------------------------------------------
102 
103 TBranch *EicRootManager::SetupBranch(const EicRootInputFile *tfile, const char *treeName,
104  const char *branchName, void *addr)
105 {
106  if (!tfile) return 0;
107 
108  TBranch *branch = tfile->SetupBranch(treeName, branchName, addr);
109  if (!branch) return 0;
110 
111  mBranchStatus[branch] = -1;
112 
113  // Add friend trees right here, so that at least 'rctree->GetEntry(iq)' in analysis.C
114  // or similar purpose script works correctly;
115  {
116  TTree *ttree = tfile->GetTree(treeName);
117 
118  if (ttree != mEicRcTree) mEicRcTree->AddFriend(ttree);
119  }
120 
121  return branch;
122 } // EicRootManager::SetupBranch()
123 
124 // ---------------------------------------------------------------------------------------
125 
127 {
128  if (!GetGeneratorEventPtr()) return 0;
129 
130  // THINK: really needed here?;
132 
133  return GetGeneratorEventPtr();
134 } // EicRootManager::GetGenMcEvent()
135 
136 // ---------------------------------------------------------------------------------------
137 
139 {
140  if (!GetGeneratorEventPtr() || genMcIndex < 0) return 0;
141 
143 
144  return (genMcIndex < GetGeneratorEventPtr()->GetNTracks() ?
145  GetGeneratorEventPtr()->GetTrack(genMcIndex) : 0);
146 } // EicRootManager::GetGenMcTrack()
147 
148 // ---------------------------------------------------------------------------------------
149 // ---------------------------------------------------------------------------------------
150 
152 {
153  ResetVars();
154 
155  mFileName = TString(fileName);
156 
157  mFptr = new TFile(fileName);
158 
159  if (!mFptr->IsOpen()) {
160  printf("Failed to open input file '%s'!\n", fileName);
161  return;
162  } //if
163 } // EicRootInputFile::EicRootInputFile()
164 
165 // ---------------------------------------------------------------------------------------
166 
167 TBranch *EicRootInputFile::SetupBranch(const char *treeName, const char *branchName, void *addr) const
168 {
169  if (!mFptr) return 0;
170 
171  TTree *tree = (TTree*)mFptr->Get(treeName);
172  if (!tree) {
173  printf("Tree '%s' is missing in the input file '%s'!\n", treeName, mFileName.Data());
174  return 0;
175  } //if
176 
177  TBranch *branch = tree->GetBranch(branchName);
178 
179  if (!branch) return 0;
180 
181  tree->SetBranchAddress(branchName, addr);
182 
183  return branch;
184 } // EicRootInputFile::SetupBranch()
185 
186 // ---------------------------------------------------------------------------------------
187 
190