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