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