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