Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PersistentAccessor.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
10 #include "LArCafJobs/CellInfo.h"
11 #include "LArCafJobs/ShapeInfo.h"
12 #include "TFile.h"
13 #include "TTree.h"
14 #include "TSystem.h"
15 #include "TString.h"
16 #include <iostream>
17 #include <fstream>
18 #include <iomanip>
19 
20 using std::cout;
21 using std::endl;
22 
23 using namespace LArSamples;
24 
25 
26 PersistentAccessor::PersistentAccessor(TTree& cellTree, TTree& SCTree, TTree& eventTree, TTree* runTree, TFile* file)
27  : m_cellTree(&cellTree), m_SCTree(&SCTree), m_eventTree(&eventTree), m_runTree(runTree), m_file(file),
28  m_historyCont(nullptr), m_historyContSC(nullptr), m_eventData(nullptr), m_runData(nullptr)
29 {
30  ClassCounts::incrementInstanceCount("PersistentAccessor");
31  m_cellTree->SetBranchAddress("history", &m_historyCont);
32  m_SCTree->SetBranchAddress("historySC", &m_historyContSC);
33  m_eventTree->SetBranchAddress("event", &m_eventData);
34  m_eventTree->LoadBaskets(); // loads the tree to memory
35  if (m_eventTree->MemoryFull(0)) {
36  cout << "WARNING: could not load all the baskets of the event tree into memory -- processing will be very slow." << endl;
37  cout << "Please check if the file you are loading is too large or corrupted." << endl;
38  }
39  if (m_runTree) {
40  m_runTree->SetBranchAddress("run", &m_runData);
41  m_runTree->LoadBaskets();
42  }
43 }
44 
45 
47  : m_cellTree(nullptr), m_SCTree(nullptr), m_eventTree(nullptr), m_runTree(nullptr), m_file(nullptr),
48  m_historyCont(nullptr), m_historyContSC(nullptr), m_eventData(nullptr), m_runData(nullptr)
49 {
50  ClassCounts::incrementInstanceCount("PersistentAccessor");
51  if (TString(fileName) != "") m_file = new TFile(fileName, "RECREATE");
52  if (m_file && !m_file->IsOpen()) { delete m_file; m_file = nullptr; }
53  m_cellTree = new TTree("cells", "");
54  m_SCTree = new TTree("SC", "");
55  m_eventTree = new TTree("events", "");
56  m_runTree = new TTree("runs", "");
57  m_cellTree->Branch("history", &m_historyCont, 32000, 0);
58  m_SCTree->Branch("historySC", &m_historyContSC, 32000, 0);
59  m_eventTree->Branch("event", &m_eventData, 32000, 0);
60  m_runTree->Branch("run", &m_runData, 32000, 0);
61  m_eventTree->SetAutoSave(0); // keep everything in memory
62  m_eventTree->SetAutoFlush(0); // keep everything in memory
63  m_runTree->SetAutoSave(0); // keep everything in memory
64  m_runTree->SetAutoFlush(0); // keep everything in memory
65 }
66 
67 
69 {
70  TFile* file = TFile::Open(fileName);
71  if (!file) return nullptr;
72  if (!file->IsOpen()) { delete file; return nullptr; }
73  TTree* cellTree = (TTree*)file->Get("cells");
74  if (!cellTree) return nullptr;
75  TTree* SCTree = (TTree*)file->Get("SC");
76  if (!SCTree) return nullptr;
77  TTree* eventTree = (TTree*)file->Get("events");
78  if (!eventTree) return nullptr;
79  TTree* runTree = (TTree*)file->Get("runs");
81  return accessor;
82 }
83 
84 
86 {
87  ClassCounts::decrementInstanceCount("PersistentAccessor");
89  run != m_runCache.end(); ++run)
90  delete run->second;
91  if (m_file)
92  delete m_file;
93  else {
94  delete m_cellTree;
95  delete m_SCTree;
96  delete m_eventTree;
97  delete m_runTree;
98  }
99  if (m_historyCont) delete m_historyCont;
100  if (m_historyContSC) delete m_historyContSC;
101  if (m_eventData) delete m_eventData;
102  if (m_runData) delete m_runData;
103 }
104 
105 
107 {
108  m_cellTree->GetEntry(i);
109  return m_historyCont;
110 }
111 
113 {
114  m_SCTree->GetEntry(i);
115  return m_historyContSC;
116 }
117 
118 
119 unsigned int PersistentAccessor::historySize(unsigned int i) const
120 {
121  const HistoryContainer* cont = historyContainer(i);
122  return (cont ? cont->nDataContainers() : 0);
123 }
124 
125 unsigned int PersistentAccessor::historySizeSC(unsigned int i) const
126 {
127  const HistoryContainer* cont = historyContainerSC(i);
128  return (cont ? cont->nDataContainers() : 0);
129 }
130 
131 
132 const EventData* PersistentAccessor::eventData(unsigned int i) const
133 {
134  if (i >= nEvents()) return nullptr;
135  m_eventTree->GetEntry(i);
137  return m_eventData;
138 }
139 
140 
141 const RunData* PersistentAccessor::runData(unsigned int i) const
142 {
143  if (i >= nRuns()) return nullptr;
144  std::map<unsigned int, const RunData*>::const_iterator cache = m_runCache.find(i);
145  if (cache != m_runCache.end()) return cache->second;
146 
147  m_runTree->GetEntry(i);
148  RunData* newRunData = new RunData(*m_runData);
149  m_runCache[m_eventData->runIndex()] = newRunData;
150  return newRunData;
151 }
152 
153 
155 {
156  return (m_file ? m_file->GetName() : "");
157 }
158 
159 
161 {
162  m_historyCont = cont;
163  m_cellTree->Fill();
164  m_historyCont = nullptr;
165 }
166 
168 {
169  m_historyContSC = cont;
170  m_SCTree->Fill();
171  m_historyContSC = nullptr;
172 }
173 
174 
176 {
178  m_eventTree->Fill();
179  m_eventData = nullptr;
180 }
181 
182 
184 {
185  m_runData = runData;
186  m_runTree->Fill();
187  m_runData = nullptr;
188 }
189 
190 
192 {
193  if (!m_file) return 0;
194  m_file->cd();
195  cout << "Writing " << m_runTree->GetEntries() << " run(s)..." << endl;
196  m_runTree->Write();
197  cout << "Writing " << m_eventTree->GetEntries() << " event(s)..." << endl;
198  m_eventTree->Write();
199  cout << "Writing " << m_cellTree->GetEntries() << " cell(s)..." << endl;
200  m_cellTree->Write();
201  cout << "Writing " << m_SCTree->GetEntries() << " SC(s)..." << endl;
202  m_SCTree->Write();
203  m_file->Flush();
204  cout << "Writing done!" << endl;
205  return true;
206 }
207 
208 
209 PersistentAccessor* PersistentAccessor::merge(const std::vector<const PersistentAccessor*>& accessors,
210  const TString& fileName)
211 {
213  unsigned int size = 0;
214  CellInfo* info = nullptr;
215 
216  int evtIndex = 0, runIndex = 0;
217  std::map<std::pair<int, int>, int> evtMap;
218  std::map<int, int> runMap;
219  std::map< std::pair<const PersistentAccessor*, int>, int > evtAccMap;
220 
221  cout << "Merging runs" << endl;
222  for (const PersistentAccessor* accessor : accessors) {
223  if (!accessor) {
224  cout << "Cannot merge: one of the inputs is null!" << endl;
225  delete newAcc;
226  return nullptr;
227  }
228  for (unsigned int i = 0; i < accessor->nRuns(); i++) {
229  int run = accessor->runData(i)->run();
230  if (runMap.find(run) != runMap.end()) continue;
231  runMap[run] = runIndex;
232  RunData* newRun = new RunData(*accessor->runData(i));
233  newAcc->addRun(newRun);
234  delete newRun;
235  runIndex++;
236  }
237  }
238 
239  cout << "Merging events" << endl;
240  unsigned int nEventsTotal = 0, iEvt = 0;
241  for (const PersistentAccessor* accessor : accessors) {
242  nEventsTotal += accessor->nEvents();
243  }
244  for (const PersistentAccessor* accessor : accessors) {
245  for (unsigned int i = 0; i < accessor->nEvents(); i++) {
246  iEvt++;
247  if (iEvt % 100000 == 0) cout << "Merging event " << iEvt << "/" << nEventsTotal << endl;
248  std::pair<int, int> evtId(accessor->eventData(i)->run(), accessor->eventData(i)->event());
249  std::pair<const PersistentAccessor*, int> evtAccId(accessor, i);
250  if (evtMap.find(evtId) != evtMap.end()) {
251  cout << "ERROR: Skipping duplicate entry for run " << accessor->eventData(i)->run() << ", event " << accessor->eventData(i)->event() << endl;
252  continue;
253  }
254  evtAccMap[evtAccId] = evtIndex;
255  evtMap[evtId] = evtIndex;
256  std::map<int, int>::const_iterator idx = runMap.find(accessor->eventData(i)->run());
257  int newRunIndex = (idx == runMap.end() ? -999 : idx->second);
258  //cout << "Storing eventData for run " << accessor->eventData(i)->run() << " at index " << newRunIndex << " instead of " << accessor->eventData(i)->runIndex() << endl;
259  EventData* newEvent = new EventData(*accessor->eventData(i), newRunIndex);
260  newAcc->addEvent(newEvent);
261  delete newEvent;
262  evtIndex++;
263  }
264  }
265 
266 
267  cout << "Merging cells" << endl;
268  for (unsigned int i = 0; i < Definitions::nChannels; i++) {
269  if (i % 10000 == 0) {
270  cout << "Merging channel " << i << "/" << Definitions::nChannels << " (current size = " << size << ")" << endl;
271  //ClassCounts::printCountsTable();
272  }
273  HistoryContainer* newHistory = nullptr;
274  for (const PersistentAccessor* accessor : accessors) {
275  const HistoryContainer* history = accessor->historyContainer(i);
276  if (!history || !history->isValid()) continue;
277  if (!newHistory) {
278  info = new CellInfo(*history->cellInfo());
279  newHistory = new HistoryContainer(info);
280  }
281  for (unsigned int j = 0; j < history->nDataContainers(); j++) {
282  DataContainer* newDC = new DataContainer(*history->dataContainer(j));
283  std::map<std::pair<const PersistentAccessor*, int>, int>::const_iterator newIndex
284  = evtAccMap.find(std::make_pair(accessor, history->dataContainer(j)->eventIndex()));
285  if (newIndex == evtAccMap.end()) cout << "Event not found for cell " << i << ", data " << j << "." << endl;
286  newDC->setEventIndex(newIndex != evtAccMap.end() ? newIndex->second : -1);
287  newHistory->add(newDC);
288  if (!info->shape(history->dataContainer(j)->gain())) {
289  const ShapeInfo* shape = history->cellInfo()->shape(history->dataContainer(j)->gain());
290  //if (!shape)
291  // cout << "Shape not filled for hash = " << i << ", index = " << j << ", gain = " << history->dataContainer(j)->gain() << endl;
292  info->setShape(history->dataContainer(j)->gain(), (shape ? new ShapeInfo(*shape) : nullptr));
293  }
294  }
295  }
296  if (newHistory) size += newHistory->nDataContainers();
297  newAcc->add(newHistory);
298  delete newHistory;
299  }
300 
301 
302  cout << "Merging SC" << endl;
303  for (unsigned int i = 0; i < Definitions::nChannelsSC; i++) {
304  if (i % 10000 == 0) {
305  cout << "Merging channel " << i << "/" << Definitions::nChannelsSC << " (current size = " << size << ")" << endl;
306  //ClassCounts::printCountsTable();
307  }
308  HistoryContainer* newHistory = nullptr;
309  for (const PersistentAccessor* accessor : accessors) {
310  const HistoryContainer* history = accessor->historyContainerSC(i);
311  if (!history || !history->isValid()) continue;
312  if (!newHistory) {
313  info = new CellInfo(*history->cellInfo());
314  newHistory = new HistoryContainer(info);
315  }
316  for (unsigned int j = 0; j < history->nDataContainers(); j++) {
317  DataContainer* newDC = new DataContainer(*history->dataContainer(j));
318  std::map<std::pair<const PersistentAccessor*, int>, int>::const_iterator newIndex
319  = evtAccMap.find(std::make_pair(accessor, history->dataContainer(j)->eventIndex()));
320  if (newIndex == evtAccMap.end()) cout << "Event not found for cell " << i << ", data " << j << "." << endl;
321  newDC->setEventIndex(newIndex != evtAccMap.end() ? newIndex->second : -1);
322  newHistory->add(newDC);
323  if (!info->shape(history->dataContainer(j)->gain())) {
324  const ShapeInfo* shape = history->cellInfo()->shape(history->dataContainer(j)->gain());
325  //if (!shape)
326  // cout << "Shape not filled for hash = " << i << ", index = " << j << ", gain = " << history->dataContainer(j)->gain() << endl;
327  info->setShape(history->dataContainer(j)->gain(), (shape ? new ShapeInfo(*shape) : nullptr));
328  }
329  }
330  }
331  if (newHistory) size += newHistory->nDataContainers();
332  newAcc->addSC(newHistory);
333  delete newHistory;
334  }
335 
336  cout << "Merging done, final size = " << size << endl;
337  newAcc->save();
338  return newAcc;
339 }
340 
341 
342 PersistentAccessor* PersistentAccessor::merge(const std::vector<TString>& inputFiles, const TString& fileName)
343 {
344  std::vector<const PersistentAccessor*> accessors;
345  for (const TString& inputFile : inputFiles) {
347  if (!accessor) {
348  cout << "ERROR : could not open file " << inputFile << endl;
349  return nullptr;
350  }
351  accessors.push_back(accessor);
352  }
354  for (const PersistentAccessor* accessor : accessors) {
355  delete accessor;
356  }
357  return result;
358 }
grepfile.info
info
Definition: grepfile.py:38
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
LArSamples::PersistentAccessor::m_SCTree
TTree * m_SCTree
Definition: PersistentAccessor.h:77
get_generator_info.result
result
Definition: get_generator_info.py:21
LArSamples::EventData::setRunData
void setRunData(const RunData *runData)
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/EventData.h:75
LArSamples::PersistentAccessor::m_cellTree
TTree * m_cellTree
Definition: PersistentAccessor.h:77
LArSamples::DataContainer::gain
CaloGain::CaloGain gain() const
Definition: DataContainer.h:54
LArSamples::PersistentAccessor::runData
const RunData * runData(unsigned int i) const
Definition: PersistentAccessor.cxx:141
Epos_Base_Fragment.inputFiles
string inputFiles
Definition: Epos_Base_Fragment.py:18
LArSamples::HistoryContainer::dataContainer
const DataContainer * dataContainer(unsigned int i) const
Definition: HistoryContainer.h:41
LArSamples::PersistentAccessor
Definition: PersistentAccessor.h:24
run
int run(int argc, char *argv[])
Definition: ttree2hdf5.cxx:28
LArSamples
Definition: AbsShape.h:24
LArSamples::EventData::runIndex
int runIndex() const
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/EventData.h:53
LArSamples::PersistentAccessor::runTree
const TTree & runTree() const
Definition: PersistentAccessor.h:47
LArSamples::ShapeInfo
Definition: ShapeInfo.h:24
LArSamples::PersistentAccessor::eventData
const EventData * eventData(unsigned int i) const
Definition: PersistentAccessor.cxx:132
LArSamples::PersistentAccessor::~PersistentAccessor
virtual ~PersistentAccessor()
Definition: PersistentAccessor.cxx:85
LArSamples::PersistentAccessor::addEvent
void addEvent(EventData *eventData)
Definition: PersistentAccessor.cxx:175
ShapeInfo.h
LArSamples::PersistentAccessor::SCTree
const TTree & SCTree() const
Definition: PersistentAccessor.h:45
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
LArSamples::PersistentAccessor::file
TFile * file() const
Definition: PersistentAccessor.h:49
LArSamples::PersistentAccessor::add
void add(HistoryContainer *cont)
Definition: PersistentAccessor.cxx:160
PersistentAccessor.h
LArSamples::PersistentAccessor::m_historyContSC
HistoryContainer * m_historyContSC
Definition: PersistentAccessor.h:80
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
LArSamples::RunData
Definition: RunData.h:21
DataContainer.h
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
LArSamples::Definitions::nChannels
static const unsigned int nChannels
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:14
LArSamples::PersistentAccessor::nEvents
unsigned int nEvents() const
Definition: PersistentAccessor.h:55
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArSamples::CellInfo::shape
const ShapeInfo * shape(CaloGain::CaloGain gain) const
Definition: CellInfo.cxx:78
LArSamples::ClassCounts::decrementInstanceCount
void decrementInstanceCount() const
Definition: LArCafJobs/LArCafJobs/ClassCounts.h:33
LArSamples::HistoryContainer::isValid
bool isValid() const
Definition: HistoryContainer.cxx:52
LArSamples::PersistentAccessor::historySizeSC
unsigned int historySizeSC(unsigned int i) const
Definition: PersistentAccessor.cxx:125
LArSamples::PersistentAccessor::historySize
unsigned int historySize(unsigned int i) const
Definition: PersistentAccessor.cxx:119
file
TFile * file
Definition: tile_monitor.h:29
LArSamples::PersistentAccessor::m_file
TFile * m_file
Definition: PersistentAccessor.h:78
ITk::EventData
InDet::SiSpacePointsSeedMakerEventData EventData
Definition: ITkSiSpacePointsSeedMaker.h:63
run
Definition: run.py:1
LArSamples::PersistentAccessor::m_historyCont
HistoryContainer * m_historyCont
Definition: PersistentAccessor.h:79
LArSamples::PersistentAccessor::m_eventData
EventData * m_eventData
Definition: PersistentAccessor.h:81
LArSamples::PersistentAccessor::open
static PersistentAccessor * open(const TString &fileName)
Definition: PersistentAccessor.cxx:68
LArSamples::PersistentAccessor::nRuns
unsigned int nRuns() const
Definition: PersistentAccessor.h:58
LArSamples::PersistentAccessor::cellTree
const TTree & cellTree() const
Definition: PersistentAccessor.h:44
LArSamples::ClassCounts::incrementInstanceCount
void incrementInstanceCount() const
Definition: LArCafJobs/LArCafJobs/ClassCounts.h:32
LArSamples::PersistentAccessor::m_runTree
TTree * m_runTree
Definition: PersistentAccessor.h:77
LArSamples::PersistentAccessor::m_eventTree
TTree * m_eventTree
Definition: PersistentAccessor.h:77
LArSamples::CellInfo
Definition: CellInfo.h:31
LArSamples::PersistentAccessor::historyContainerSC
const HistoryContainer * historyContainerSC(unsigned int i) const
Definition: PersistentAccessor.cxx:112
xAOD::JetAttributeAccessor::accessor
const AccessorWrapper< T > * accessor(xAOD::JetAttribute::AttributeID id)
Returns an attribute accessor corresponding to an AttributeID.
Definition: JetAccessorMap.h:26
LArSamples::HistoryContainer::cellInfo
const CellInfo * cellInfo() const
Definition: HistoryContainer.h:43
LArSamples::PersistentAccessor::save
bool save() const
Definition: PersistentAccessor.cxx:191
LArSamples::DataContainer
Definition: DataContainer.h:25
LArSamples::PersistentAccessor::m_runData
RunData * m_runData
Definition: PersistentAccessor.h:82
LArSamples::HistoryContainer
Definition: HistoryContainer.h:29
LArSamples::Definitions::nChannelsSC
static const unsigned int nChannelsSC
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:15
LArSamples::DataContainer::eventIndex
int eventIndex() const
Definition: DataContainer.h:64
LArSamples::HistoryContainer::add
void add(const DataContainer *data)
append data (takes ownership)
Definition: HistoryContainer.h:46
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
LArSamples::PersistentAccessor::addSC
void addSC(HistoryContainer *cont)
Definition: PersistentAccessor.cxx:167
LArSamples::PersistentAccessor::eventTree
const TTree & eventTree() const
Definition: PersistentAccessor.h:46
Definitions.h
xAODRootTest.accessors
dictionary accessors
Definition: xAODRootTest.py:73
LArSamples::PersistentAccessor::addRun
void addRun(RunData *runData)
Definition: PersistentAccessor.cxx:183
LArSamples::PersistentAccessor::m_runCache
std::map< unsigned int, const RunData * > m_runCache
Definition: PersistentAccessor.h:83
LArSamples::PersistentAccessor::historyContainer
const HistoryContainer * historyContainer(unsigned int i) const
Definition: PersistentAccessor.cxx:106
LArSamples::PersistentAccessor::merge
static PersistentAccessor * merge(const std::vector< const PersistentAccessor * > &accessors, const TString &fileName)
Definition: PersistentAccessor.cxx:209
LArSamples::EventData
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/EventData.h:29
CellInfo.h
LArSamples::PersistentAccessor::fileName
TString fileName() const
Definition: PersistentAccessor.cxx:154
LArSamples::PersistentAccessor::PersistentAccessor
PersistentAccessor(TTree &cellTree, TTree &SCTree, TTree &eventTree, TTree *runTree, TFile *file)
Constructor
Definition: PersistentAccessor.cxx:26
LArSamples::DataContainer::setEventIndex
void setEventIndex(int index)
Definition: DataContainer.h:92
LArSamples::HistoryContainer::nDataContainers
unsigned int nDataContainers() const
Definition: HistoryContainer.h:40
ClassCounts.h