ATLAS Offline Software
Loading...
Searching...
No Matches
LArSamples::PersistentAccessor Class Reference

#include <PersistentAccessor.h>

Inheritance diagram for LArSamples::PersistentAccessor:
Collaboration diagram for LArSamples::PersistentAccessor:

Public Member Functions

 PersistentAccessor (TTree &cellTree, TTree &SCTree, TTree &eventTree, TTree *runTree, TFile *file)
 Constructor.
 PersistentAccessor (const TString &fileName)
 PersistentAccessor (const PersistentAccessor &)
PersistentAccessoroperator= (const PersistentAccessor &)
virtual ~PersistentAccessor ()
unsigned int historySize (unsigned int i) const
unsigned int historySizeSC (unsigned int i) const
bool save () const
const TTree & cellTree () const
const TTree & SCTree () const
const TTree & eventTree () const
const TTree & runTree () const
TFile * file () const
TString fileName () const
const HistoryContainerhistoryContainer (unsigned int i) const
const HistoryContainerhistoryContainerSC (unsigned int i) const
unsigned int nEvents () const
const EventDataeventData (unsigned int i) const
unsigned int nRuns () const
const RunDatarunData (unsigned int i) const
void add (HistoryContainer *cont)
void addSC (HistoryContainer *cont)
void addEvent (EventData *eventData)
void addRun (RunData *runData)
HistoryContainercurrentContainer () const
HistoryContainercurrentContainerSC () const
int getCellEntry (unsigned int i) const
int getSCEntry (unsigned int i) const

Static Public Member Functions

static PersistentAccessoropen (const TString &fileName)
static PersistentAccessormerge (const std::vector< const PersistentAccessor * > &accessors, const TString &fileName)
static PersistentAccessormerge (const std::vector< TString > &inputFiles, const TString &fileName)

Private Attributes

TTree * m_cellTree
TTree * m_SCTree
TTree * m_eventTree
TTree * m_runTree
TFile * m_file
HistoryContainerm_historyCont
HistoryContainerm_historyContSC
EventDatam_eventData
RunDatam_runData
std::map< unsigned int, const RunData * > m_runCache

Detailed Description

Definition at line 24 of file PersistentAccessor.h.

Constructor & Destructor Documentation

◆ PersistentAccessor() [1/3]

PersistentAccessor::PersistentAccessor ( TTree & cellTree,
TTree & SCTree,
TTree & eventTree,
TTree * runTree,
TFile * file )

Constructor.

Definition at line 26 of file PersistentAccessor.cxx.

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}

◆ PersistentAccessor() [2/3]

PersistentAccessor::PersistentAccessor ( const TString & fileName)

Definition at line 46 of file PersistentAccessor.cxx.

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}

◆ PersistentAccessor() [3/3]

LArSamples::PersistentAccessor::PersistentAccessor ( const PersistentAccessor & )

◆ ~PersistentAccessor()

PersistentAccessor::~PersistentAccessor ( )
virtual

Definition at line 85 of file PersistentAccessor.cxx.

86{
87 ClassCounts::decrementInstanceCount("PersistentAccessor");
88 for (std::map<unsigned int, const RunData*>::iterator run = m_runCache.begin();
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;
101 if (m_eventData) delete m_eventData;
102 if (m_runData) delete m_runData;
103}
std::map< unsigned int, const RunData * > m_runCache

Member Function Documentation

◆ add()

void PersistentAccessor::add ( HistoryContainer * cont)

Definition at line 160 of file PersistentAccessor.cxx.

161{
162 m_historyCont = cont;
163 m_cellTree->Fill();
164 m_historyCont = nullptr;
165}

◆ addEvent()

void PersistentAccessor::addEvent ( EventData * eventData)

Definition at line 175 of file PersistentAccessor.cxx.

176{
178 m_eventTree->Fill();
179 m_eventData = nullptr;
180}
const EventData * eventData(unsigned int i) const

◆ addRun()

void PersistentAccessor::addRun ( RunData * runData)

Definition at line 183 of file PersistentAccessor.cxx.

184{
186 m_runTree->Fill();
187 m_runData = nullptr;
188}
const RunData * runData(unsigned int i) const

◆ addSC()

void PersistentAccessor::addSC ( HistoryContainer * cont)

Definition at line 167 of file PersistentAccessor.cxx.

168{
169 m_historyContSC = cont;
170 m_SCTree->Fill();
171 m_historyContSC = nullptr;
172}

◆ cellTree()

const TTree & LArSamples::PersistentAccessor::cellTree ( ) const
inline

Definition at line 44 of file PersistentAccessor.h.

44{ return *m_cellTree; }

◆ currentContainer()

HistoryContainer * LArSamples::PersistentAccessor::currentContainer ( ) const
inline

Definition at line 66 of file PersistentAccessor.h.

66{ return m_historyCont; }

◆ currentContainerSC()

HistoryContainer * LArSamples::PersistentAccessor::currentContainerSC ( ) const
inline

Definition at line 67 of file PersistentAccessor.h.

67{ return m_historyContSC; }

◆ eventData()

const EventData * PersistentAccessor::eventData ( unsigned int i) const

Definition at line 132 of file PersistentAccessor.cxx.

133{
134 if (i >= nEvents()) return nullptr;
135 m_eventTree->GetEntry(i);
136 m_eventData->setRunData(runData(m_eventData->runIndex()));
137 return m_eventData;
138}

◆ eventTree()

const TTree & LArSamples::PersistentAccessor::eventTree ( ) const
inline

Definition at line 46 of file PersistentAccessor.h.

46{ return *m_eventTree; }

◆ file()

TFile * LArSamples::PersistentAccessor::file ( ) const
inline

Definition at line 49 of file PersistentAccessor.h.

49{ return m_file; }

◆ fileName()

TString PersistentAccessor::fileName ( ) const

Definition at line 154 of file PersistentAccessor.cxx.

155{
156 return (m_file ? m_file->GetName() : "");
157}

◆ getCellEntry()

int LArSamples::PersistentAccessor::getCellEntry ( unsigned int i) const
inline

Definition at line 72 of file PersistentAccessor.h.

72{ return m_cellTree->GetEntry(i); }

◆ getSCEntry()

int LArSamples::PersistentAccessor::getSCEntry ( unsigned int i) const
inline

Definition at line 73 of file PersistentAccessor.h.

73{ return m_SCTree->GetEntry(i); }

◆ historyContainer()

const HistoryContainer * PersistentAccessor::historyContainer ( unsigned int i) const

Definition at line 106 of file PersistentAccessor.cxx.

107{
108 m_cellTree->GetEntry(i);
109 return m_historyCont;
110}

◆ historyContainerSC()

const HistoryContainer * PersistentAccessor::historyContainerSC ( unsigned int i) const

Definition at line 112 of file PersistentAccessor.cxx.

113{
114 m_SCTree->GetEntry(i);
115 return m_historyContSC;
116}

◆ historySize()

unsigned int PersistentAccessor::historySize ( unsigned int i) const

Definition at line 119 of file PersistentAccessor.cxx.

120{
121 const HistoryContainer* cont = historyContainer(i);
122 return (cont ? cont->nDataContainers() : 0);
123}
unsigned int nDataContainers() const
const HistoryContainer * historyContainer(unsigned int i) const

◆ historySizeSC()

unsigned int PersistentAccessor::historySizeSC ( unsigned int i) const

Definition at line 125 of file PersistentAccessor.cxx.

126{
127 const HistoryContainer* cont = historyContainerSC(i);
128 return (cont ? cont->nDataContainers() : 0);
129}
const HistoryContainer * historyContainerSC(unsigned int i) const

◆ merge() [1/2]

PersistentAccessor * PersistentAccessor::merge ( const std::vector< const PersistentAccessor * > & accessors,
const TString & fileName )
static

Definition at line 209 of file PersistentAccessor.cxx.

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 cout << "Merging cells" << endl;
267 for (unsigned int i = 0; i < Definitions::nChannels; i++) {
268 if (i % 10000 == 0) {
269 cout << "Merging channel " << i << "/" << Definitions::nChannels << " (current size = " << size << ")" << endl;
270 //ClassCounts::printCountsTable();
271 }
272 HistoryContainer* newHistory = nullptr;
273 for (const PersistentAccessor* accessor : accessors) {
274 const HistoryContainer* history = accessor->historyContainer(i);
275 if (!history || !history->isValid()) continue;
276 if (!newHistory) {
277 info = new CellInfo(*history->cellInfo());
278 newHistory = new HistoryContainer(info);
279 }
280 for (unsigned int j = 0; j < history->nDataContainers(); j++) {
281 DataContainer* newDC = new DataContainer(*history->dataContainer(j));
282 std::map<std::pair<const PersistentAccessor*, int>, int>::const_iterator newIndex
283 = evtAccMap.find(std::make_pair(accessor, history->dataContainer(j)->eventIndex()));
284 if (newIndex == evtAccMap.end()) cout << "Event not found for cell " << i << ", data " << j << "." << endl;
285 newDC->setEventIndex(newIndex != evtAccMap.end() ? newIndex->second : -1);
286 newHistory->add(newDC);
287 if (!info->shape(history->dataContainer(j)->gain())) {
288 const ShapeInfo* shape = history->cellInfo()->shape(history->dataContainer(j)->gain());
289 //if (!shape)
290 // cout << "Shape not filled for hash = " << i << ", index = " << j << ", gain = " << history->dataContainer(j)->gain() << endl;
291 info->setShape(history->dataContainer(j)->gain(), (shape ? new ShapeInfo(*shape) : nullptr));
292 }
293 }
294 }
295 if (newHistory) size += newHistory->nDataContainers();
296 newAcc->add(newHistory);
297 delete newHistory;
298 }
299
300
301 cout << "Merging SC" << endl;
302 size=0;
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
337 cout << "Merging done, final size = " << size << endl;
338 newAcc->save();
339 return newAcc;
340}
const ShapeInfo * shape(CaloGain::CaloGain gain) const
Definition CellInfo.cxx:78
CaloGain::CaloGain gain() const
void setEventIndex(int index)
void add(const DataContainer *data)
append data (takes ownership)
const CellInfo * cellInfo() const
const DataContainer * dataContainer(unsigned int i) const
void addEvent(EventData *eventData)
void add(HistoryContainer *cont)
PersistentAccessor(TTree &cellTree, TTree &SCTree, TTree &eventTree, TTree *runTree, TFile *file)
Constructor.
void addSC(HistoryContainer *cont)
const AccessorWrapper< T > * accessor(xAOD::JetAttribute::AttributeID id)
Returns an attribute accessor corresponding to an AttributeID.

◆ merge() [2/2]

PersistentAccessor * PersistentAccessor::merge ( const std::vector< TString > & inputFiles,
const TString & fileName )
static

Definition at line 343 of file PersistentAccessor.cxx.

344{
345 std::vector<const PersistentAccessor*> accessors;
346 for (const TString& inputFile : inputFiles) {
347 PersistentAccessor* accessor = open(inputFile);
348 if (!accessor) {
349 cout << "ERROR : could not open file " << inputFile << endl;
350 return nullptr;
351 }
352 accessors.push_back(accessor);
353 }
355 for (const PersistentAccessor* accessor : accessors) {
356 delete accessor;
357 }
358 return result;
359}
static PersistentAccessor * open(const TString &fileName)
static PersistentAccessor * merge(const std::vector< const PersistentAccessor * > &accessors, const TString &fileName)

◆ nEvents()

unsigned int LArSamples::PersistentAccessor::nEvents ( ) const
inline

Definition at line 55 of file PersistentAccessor.h.

55{ return m_eventTree->GetEntries(); }

◆ nRuns()

unsigned int LArSamples::PersistentAccessor::nRuns ( ) const
inline

Definition at line 58 of file PersistentAccessor.h.

58{ return m_runTree->GetEntries(); }

◆ open()

PersistentAccessor * PersistentAccessor::open ( const TString & fileName)
static

Definition at line 68 of file PersistentAccessor.cxx.

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}

◆ operator=()

PersistentAccessor & LArSamples::PersistentAccessor::operator= ( const PersistentAccessor & )

◆ runData()

const RunData * PersistentAccessor::runData ( unsigned int i) const

Definition at line 141 of file PersistentAccessor.cxx.

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}

◆ runTree()

const TTree & LArSamples::PersistentAccessor::runTree ( ) const
inline

Definition at line 47 of file PersistentAccessor.h.

47{ return *m_runTree; }

◆ save()

bool PersistentAccessor::save ( ) const

Definition at line 191 of file PersistentAccessor.cxx.

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}

◆ SCTree()

const TTree & LArSamples::PersistentAccessor::SCTree ( ) const
inline

Definition at line 45 of file PersistentAccessor.h.

45{ return *m_SCTree; }

Member Data Documentation

◆ m_cellTree

TTree* LArSamples::PersistentAccessor::m_cellTree
private

Definition at line 77 of file PersistentAccessor.h.

◆ m_eventData

EventData* LArSamples::PersistentAccessor::m_eventData
mutableprivate

Definition at line 81 of file PersistentAccessor.h.

◆ m_eventTree

TTree * LArSamples::PersistentAccessor::m_eventTree
private

Definition at line 77 of file PersistentAccessor.h.

◆ m_file

TFile* LArSamples::PersistentAccessor::m_file
mutableprivate

Definition at line 78 of file PersistentAccessor.h.

◆ m_historyCont

HistoryContainer* LArSamples::PersistentAccessor::m_historyCont
mutableprivate

Definition at line 79 of file PersistentAccessor.h.

◆ m_historyContSC

HistoryContainer* LArSamples::PersistentAccessor::m_historyContSC
mutableprivate

Definition at line 80 of file PersistentAccessor.h.

◆ m_runCache

std::map<unsigned int, const RunData*> LArSamples::PersistentAccessor::m_runCache
mutableprivate

Definition at line 83 of file PersistentAccessor.h.

◆ m_runData

RunData* LArSamples::PersistentAccessor::m_runData
mutableprivate

Definition at line 82 of file PersistentAccessor.h.

◆ m_runTree

TTree * LArSamples::PersistentAccessor::m_runTree
private

Definition at line 77 of file PersistentAccessor.h.

◆ m_SCTree

TTree * LArSamples::PersistentAccessor::m_SCTree
private

Definition at line 77 of file PersistentAccessor.h.


The documentation for this class was generated from the following files: