CbmRoot
CbmMCDataArray.cxx
Go to the documentation of this file.
1 
4 #include "CbmMCDataArray.h"
5 
6 #include <FairLogger.h> // for Logger, LOG
7 #include <FairRootManager.h> // for FairRootManager
8 
9 #include <TChain.h> // for TChain
10 #include <TClonesArray.h> // for TClonesArray
11 #include <TString.h> // for TString, operator<<
12 
13 #include <utility> // for pair
14 
15 using namespace std;
16 
17 
18 // --- Standard constructor
19 CbmMCDataArray::CbmMCDataArray(const char* branchname,
20  const std::vector<std::list<TString>>& fileList)
21  : fLegacy(0)
22  , fLegacyArray(nullptr)
23  , fBranchName(branchname)
24  , fSize(-1111)
25  , fChains()
26  , fTArr()
27  , fN()
28  , fArrays() {
29  list<TString>::const_iterator p;
30  Int_t i;
31  Int_t s = fileList.size();
32 
33  fSize = s;
34  fChains.resize(s);
35  fTArr.resize(s);
36  fN.resize(s);
37 
38  for (i = 0; i < s; i++) {
39  fN[i] = 0;
40  fTArr[i] = nullptr;
41  fChains[i] = nullptr;
42  if (fileList[i].size() == 0) continue;
43  fChains[i] = new TChain("cbmsim");
44  for (p = fileList[i].begin(); p != fileList[i].end(); ++p)
45  fChains[i]->AddFile(*p);
46  fChains[i]->SetBranchAddress(branchname, &(fTArr[i]));
47  fN[i] = fChains[i]->GetEntries();
48  }
49 
50  fArrays.resize(s);
51  for (i = 0; i < s; i++)
52  fArrays[i].clear();
53 }
54 
55 // --- Make TChain number chainNum2 friend of TChain number chainNum2
56 void CbmMCDataArray::AddFriend(Int_t chainNum1, Int_t chainNum2) {
57  if (fLegacy == 1) {
58  LOG(error) << "AddFriend method should not be called in legacy mode";
59  return;
60  }
61  if (chainNum1 < 0 || chainNum1 >= static_cast<Int_t>(fChains.size())
62  || fChains[chainNum1] == nullptr) {
63  LOG(error) << "chainNum1=" << chainNum1
64  << " is not a correct chain number.";
65  return;
66  }
67  if (chainNum2 < 0 || chainNum2 >= static_cast<Int_t>(fChains.size())
68  || fChains[chainNum2] == nullptr) {
69  LOG(error) << "chainNum2=" << chainNum2
70  << " is not a correct chain number.";
71  return;
72  }
73  fChains[chainNum1]->AddFriend(fChains[chainNum2]);
74 }
75 
76 // --- Legacy constructor
77 CbmMCDataArray::CbmMCDataArray(const char* branchname)
78  : fLegacy(1)
79  , fLegacyArray(nullptr)
80  , fBranchName(branchname)
81  , fSize(-1111)
82  , fChains()
83  , fTArr()
84  , fN()
85  , fArrays() {
86  FairRootManager* fManager = FairRootManager::Instance();
87  if (!fManager) {
88  LOG(fatal) << "CbmMCDataArray(): Can't find a Root Manager.";
89  return;
90  }
91  fLegacyArray = (TClonesArray*) fManager->GetObject(branchname);
92  if (!fLegacyArray) {
93  LOG(fatal) << "CbmMCDataArray(): Can't find " << fBranchName
94  << " in the system.";
95  return;
96  }
97 }
98 
99 // --- Legacy Get
100 TObject*
101 CbmMCDataArray::LegacyGet(Int_t fileNumber, Int_t eventNumber, Int_t index) {
102  if (fileNumber >= 0 || eventNumber >= 0)
103  LOG(debug1) << "LegacyGet: Trying to get object with fileNum="
104  << fileNumber << ", entryNum=" << eventNumber
105  << " in legacy mode.";
106  if (index < 0) return 0;
107  return fLegacyArray->At(index);
108 }
109 
110 
111 // --- Get an object
112 TObject* CbmMCDataArray::Get(Int_t fileNumber, Int_t eventNumber, Int_t index) {
113  if (fLegacy == 1) return LegacyGet(fileNumber, eventNumber, index);
114  if (fileNumber < 0 || fileNumber >= fSize) return nullptr;
115  if (eventNumber < 0 || eventNumber >= fN[fileNumber]) return nullptr;
116  if (index < 0) return nullptr;
117 
118  // --- Cached arrays
119  map<Int_t, TClonesArray*>& arr = fArrays[fileNumber];
120 
121  // --- If the array for this event is already in the cache, use it.
122  if (arr.find(eventNumber) != arr.end()) return arr[eventNumber]->At(index);
123 
124  // --- If not, copy the array from the chain into the cache
125  TChain* ch = fChains[fileNumber];
126  ch->GetEntry(eventNumber);
127  arr[eventNumber] = (TClonesArray*) (fTArr[fileNumber]->Clone());
128 
129  return arr[eventNumber]->At(index);
130 }
131 
132 // --- Get a size of TClonesArray . Slow if TClonesArray not in cache
133 Int_t CbmMCDataArray::Size(Int_t fileNumber, Int_t eventNumber) {
134  if (fLegacy == 1) return fLegacyArray->GetEntriesFast();
135  if (fileNumber < 0 || fileNumber >= fSize) return -1111;
136  if (eventNumber < 0 || eventNumber >= fN[fileNumber]) return -1111;
137 
138  // --- Cached arrays
139  map<Int_t, TClonesArray*>& arr = fArrays[fileNumber];
140 
141  // --- If the array for this event is already in the cache, use it.
142  if (arr.find(eventNumber) != arr.end())
143  return arr[eventNumber]->GetEntriesFast();
144 
145  // --- If not, get the array from the chain (slow)
146  TChain* ch = fChains[fileNumber];
147  ch->GetEntry(eventNumber);
148 
149  return fTArr[fileNumber]->GetEntriesFast();
150 }
151 
152 
153 // --- At end of one event: clear the cache to free the memory
155  if (fLegacy == 1) return;
156 
157  Int_t i;
158  map<Int_t, TClonesArray*>::const_iterator p;
159 
160  for (i = 0; i < fSize; i++) {
161  for (p = fArrays[i].begin(); p != fArrays[i].end(); ++p)
162  delete (p->second);
163  fArrays[i].clear();
164  }
165 }
166 
167 
168 // --- Clean up
170  if (fLegacy == 1) return;
171  Int_t i;
172 
173  FinishEvent();
174  for (i = 0; i < fSize; i++)
175  delete fChains[i];
176 }
177 
CbmMCDataArray::fArrays
std::vector< std::map< Int_t, TClonesArray * > > fArrays
Number of entries in chains.
Definition: CbmMCDataArray.h:155
CbmMCDataArray::Done
void Done()
Definition: CbmMCDataArray.cxx:169
CbmMCDataArray::LegacyGet
TObject * LegacyGet(Int_t fileNumber, Int_t eventNumber, Int_t index)
Definition: CbmMCDataArray.cxx:101
CbmMCDataArray::Size
Int_t Size(Int_t fileNumber, Int_t eventNumber)
Definition: CbmMCDataArray.cxx:133
CbmMCDataArray::fLegacyArray
TClonesArray * fLegacyArray
If true, run in legacy mode.
Definition: CbmMCDataArray.h:144
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmMCDataArray::AddFriend
void AddFriend(Int_t chainNum1, Int_t chainNum2)
Definition: CbmMCDataArray.cxx:56
CbmMCDataArray.h
CbmMCDataArray
Access to a MC data branch for time-based analysis.
Definition: CbmMCDataArray.h:35
CbmMCDataArray::FinishEvent
void FinishEvent()
Definition: CbmMCDataArray.cxx:154
CbmMCDataArray::fLegacy
Int_t fLegacy
Definition: CbmMCDataArray.h:143
CbmMCDataArray::fTArr
std::vector< TClonesArray * > fTArr
Arrays of chains (one per input source)
Definition: CbmMCDataArray.h:149
CbmMCDataArray::fSize
Int_t fSize
Name of the data branch.
Definition: CbmMCDataArray.h:146
CbmMCDataArray::Get
TObject * Get(const CbmLink *lnk)
Definition: CbmMCDataArray.h:47
CbmMCDataArray::fBranchName
TString fBranchName
Pointer to TClonesArray for legacy mode.
Definition: CbmMCDataArray.h:145
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMCDataArray::CbmMCDataArray
CbmMCDataArray()
Definition: CbmMCDataArray.h:92
eventNumber
Int_t eventNumber
Definition: riplet/Lx.cxx:78
CbmMCDataArray::fChains
std::vector< TChain * > fChains
Number of input file lists (one per source)
Definition: CbmMCDataArray.h:147
CbmMCDataArray::fN
std::vector< Long64_t > fN
Data arrays from chains (one per input source)
Definition: CbmMCDataArray.h:150