ATLAS Offline Software
Loading...
Searching...
No Matches
MultiTreeAccessor.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
11#include "TSystem.h"
12#include "TChain.h"
13#include <iostream>
14#include <iomanip>
15#include <fstream>
16#include <string>
17
18using std::string;
19using std::cout;
20using std::endl;
21using namespace std;
22
23using namespace LArSamples;
24
25
26MultiTreeAccessor* MultiTreeAccessor::open(const std::vector<TString>& files)
27{
28 std::vector<const TreeAccessor*> accessors;
29 for (const TString& fileName : files) {
30 const TreeAccessor* accessor = TreeAccessor::open(fileName);
31 if (!accessor) {
32 cout << "Skipping invalid file " << fileName << endl;
33 continue;
34 }
35 accessors.push_back(accessor);
36 }
37
38 if (accessors.empty()) return nullptr;
39 return new MultiTreeAccessor(accessors);
40}
41
42
44{
45 std::ifstream f(fileList);
46 if (!f) {
47 cout << "file " << fileList << " not accessible" << endl;
48 return nullptr;
49 }
50
51 std::string fileName;
52 unsigned int i = 0;
53
54 std::vector<const TreeAccessor*> accessors;
55
56 while (f >> fileName) {
57 const TreeAccessor* accessor = TreeAccessor::open(fileName.c_str());
58 if (!accessor) {
59 cout << "Skipping invalid file " << fileName << endl;
60 continue;
61 }
62 cout << std::setw(2) << ++i << " - " << fileName << endl;
63 accessors.push_back(accessor);
64 }
65
66 if (accessors.empty()) return nullptr;
67 return new MultiTreeAccessor(accessors);
68}
69
70
72{
73 // Piggyback on TChain wildcarding feature...
74 TChain chain("");
75 chain.Add(wcName);
76
77 std::vector<const TreeAccessor*> accessors;
78
79 for (int i = 0; i < chain.GetListOfFiles()->GetEntries(); i++) {
80 std::string fileName = chain.GetListOfFiles()->At(i)->GetTitle();
81 const TreeAccessor* accessor = TreeAccessor::open(fileName.c_str());
82 if (!accessor) {
83 cout << "Skipping invalid file " << fileName << endl;
84 continue;
85 }
86 cout << std::setw(2) << i+1 << " - " << fileName << " , nEvents = " << accessor->nEvents() << ", nRuns = " << accessor->nRuns() << endl;
87 accessors.push_back(accessor);
88 }
89
90 if (accessors.empty()) return nullptr;
91 return new MultiTreeAccessor(accessors);
92}
93
94
100
101
102const EventData* MultiTreeAccessor::eventData(unsigned int i) const
103{
104 unsigned int nEventsSoFar = 0;
105 for (const TreeAccessor* accessor : m_accessors) {
106 unsigned int n = accessor->nEvents();
107 if (i < nEventsSoFar + n) return accessor->eventData(i - nEventsSoFar);
108 nEventsSoFar += n;
109 }
110 return nullptr;
111}
112
113
114const RunData* MultiTreeAccessor::runData(unsigned int i) const
115{
116 unsigned int nRunsSoFar = 0;
117 for (const TreeAccessor* accessor : m_accessors) {
118 unsigned int n = accessor->nRuns();
119 if (i < nRunsSoFar + n) return accessor->runData(i - nRunsSoFar);
120 nRunsSoFar += n;
121 }
122 return nullptr;
123}
124
125
126unsigned int MultiTreeAccessor::nEvents() const
127{
128 unsigned int n = 0;
129 for (const TreeAccessor* accessor : m_accessors)
130 n += accessor->nEvents();
131 return n;
132}
133
134
135unsigned int MultiTreeAccessor::nRuns() const
136{
137 unsigned int n = 0;
138 for (const TreeAccessor* accessor : m_accessors)
139 n += accessor->nRuns();
140 return n;
141}
142
143
144unsigned int MultiTreeAccessor::historySize(unsigned int i) const
145{
146 resetCache();
147 unsigned int size = 0;
148 for (const TreeAccessor* accessor : m_accessors) {
149 const HistoryContainer* cont = accessor->historyContainer(i);
150 if (cont) size += cont->nDataContainers();
151 }
152 return size;
153}
154
155unsigned int MultiTreeAccessor::historySizeSC(unsigned int i) const
156{
157 resetCache();
158 unsigned int size = 0;
159 for (const TreeAccessor* accessor : m_accessors) {
160 const HistoryContainer* cont = accessor->historyContainerSC(i);
161 if (cont) size += cont->nDataContainers();
162 }
163 return size;
164}
165
166
167const History* MultiTreeAccessor::getCellHistory(unsigned int i) const
168{
169 CellInfo* cellInfo = nullptr;
170 std::vector<const Data*> allData;
171 std::vector<const EventData*> allEventData;
172 for (const TreeAccessor* accessor : m_accessors) {
173 const History* thisHistory = accessor->getCellHistory(i);
174 if (!thisHistory) continue;
175 if (!cellInfo) {
176 cellInfo = new CellInfo(*thisHistory->cellInfo());
177 }
178 const std::vector<const EventData*>& thisEventData = thisHistory->eventData();
179 std::map<const EventData*, const EventData*> eventMap;
180 for (const EventData* event : thisEventData) {
181 if (eventMap.find(event) != eventMap.end()) continue;
182 EventData* newED = new EventData(*event);
183 eventMap[event] = newED;
184 allEventData.push_back(newED);
185 }
186
187 for (unsigned int j = 0; j < thisHistory->nData(); j++) {
188 allData.push_back(new Data(*thisHistory->data(j), eventMap[thisHistory->data(j)->eventData()], nullptr, -1));
189 if (!cellInfo->shape(thisHistory->data(j)->gain())) {
190 const ShapeInfo* thisShape = thisHistory->cellInfo()->shape(thisHistory->data(j)->gain());
191 cellInfo->setShape(thisHistory->data(j)->gain(), thisShape ? new ShapeInfo(*thisShape) : nullptr);
192 }
193 }
194 delete thisHistory;
195 }
196 //data are copied from cellInfo into History member variable
197 auto * h = cellInfo ? new History(allData, *cellInfo, allEventData, i): nullptr;
198 delete cellInfo;
199 return h;
200}
201
202const History* MultiTreeAccessor::getSCHistory(unsigned int i) const
203{
204 std::unique_ptr<CellInfo> cellInfo{};
205 std::vector<const Data*> allData;
206 std::vector<const EventData*> allEventData;
207 for (const TreeAccessor* accessor : m_accessors) {
208 //cout << "---> Getting history for a treeAccessor..." << endl;
209 const History* thisHistory = accessor->getSCHistory(i);
210 //cout << "---> done Getting history for a treeAccessor..." << endl;
211 if (!thisHistory) continue;
212 if (!cellInfo) {
213 cellInfo = std::make_unique<CellInfo>(*thisHistory->cellInfo());
214 //cout << "---> done new cell info" << endl;
215 }
216 //cout << "---> Creating new event data N = " << thisHistory->eventData().size() << endl;
217 const std::vector<const EventData*>& thisEventData = thisHistory->eventData();
218 std::map<const EventData*, const EventData*> eventMap;
219 for (const EventData* event : thisEventData) {
220 if (eventMap.find(event) != eventMap.end()) continue;
221 EventData* newED = new EventData(*event);
222 eventMap[event] = newED;
223 allEventData.push_back(newED);
224 }
225 //cout << "---> Creating new data N = " << thisHistory->nData() << endl;
226
227 for (unsigned int ii = 0; ii < thisHistory->nData(); ii++) {
228 //cout << "------> Creating new data " << i << endl;
229 allData.push_back(new Data(*thisHistory->data(ii), eventMap[thisHistory->data(ii)->eventData()], nullptr, -1));
230 //cout << "------> done Creating new data " << i << endl;
231 if (!cellInfo->shape(thisHistory->data(ii)->gain())) {
232 const ShapeInfo* thisShape = thisHistory->cellInfo()->shape(thisHistory->data(ii)->gain());
233 cellInfo->setShape(thisHistory->data(ii)->gain(), thisShape ? new ShapeInfo(*thisShape) : nullptr);
234 }
235 //cout << "------> done shape " << i << endl;
236 }
237 //cout << "---> done Creating new data, deleting treeAcc history" << endl;
238 delete thisHistory;
239 }
240 //cout << "--->returning new history..." << endl;
241 return (cellInfo ? new History(allData, *cellInfo, allEventData, i) : nullptr);
242}
243
244
245const CellInfo* MultiTreeAccessor::getCellInfo(unsigned int i) const
246{
247 resetCache();
248 for (const TreeAccessor* accessor : m_accessors) {
249 const HistoryContainer* cont = accessor->historyContainer(i);
250 if (cont && cont->cellInfo()) return new CellInfo(*cont->cellInfo());
251 }
252 return nullptr;
253}
254
255
256bool MultiTreeAccessor::writeToFile(const TString& fileName) const
257{
258 std::vector<const Accessor*> accessors;
259 for (unsigned int i = 0; i < m_accessors.size(); i++)
260 accessors.push_back(m_accessors[i]);
261 cout << "Merging data..." << endl;
262 TreeAccessor* singleContainer = TreeAccessor::merge(accessors, fileName);
263 if (!singleContainer) return false;
264 delete singleContainer;
265 return true;
266}
267
268
269std::vector<MultiTreeAccessor*> MultiTreeAccessor::filterComponents(const FilterList& filterList, const DataTweaker& tweaker) const
270{
271 std::vector< std::vector<const TreeAccessor*> > filteredAccessors(filterList.size());
272
273 for (unsigned int i = 0; i < nAccessors(); i++) {
274 const TreeAccessor* treeAcc = dynamic_cast<const TreeAccessor*>(&accessor(i));
275 cout << "Processing data " << i << " of " << nAccessors();
276 if (treeAcc) cout << " (fileName = " << treeAcc->fileName() << ")";
277 cout << endl;
278 FilterList thisFilterList;
279 //
280 for (unsigned int f = 0; f < filterList.size(); f++) {
281 std::string pathname = (string)filterList.fileName(f);
282 if( pathname.find("eos/atlas/") < pathname.length() ){
283 int nslashes = 0, slpos = 0;
284 for( int k1 = 0; k1 < (int)pathname.length(); k1++ ){
285 if( nslashes > 2 )break;
286 if( pathname[k1] != '/' )continue;
287 nslashes++;
288 slpos = k1;
289 }
290 pathname.resize( slpos );
291 }
292 TString thisFN = Form("%s_filter%d.root", pathname.c_str(), i );//filterList.fileName(f).Data(), i);
293 thisFilterList.add(filterList.filterParams(f), thisFN);
294 }
295 std::vector<TreeAccessor*> filteredTreeAccs = TreeAccessor::filter(accessor(i), thisFilterList, tweaker);
296 if (filteredTreeAccs.size() != filterList.size()) {
297 cout << "Filtering failed, exiting" << endl;
298 return std::vector<MultiTreeAccessor*>();
299 }
300 for (unsigned int f = 0; f < filteredTreeAccs.size(); f++) filteredAccessors[f].push_back(filteredTreeAccs[f]);
301 }
302
303 std::vector<MultiTreeAccessor*> result;
304 for (unsigned int f = 0; f < filteredAccessors.size(); f++) result.push_back(new MultiTreeAccessor(filteredAccessors[f]));
305 return result;
306}
@ Data
Definition BaseObject.h:11
TGraphErrors * GetEntries(TH2F *histo)
virtual void resetCache() const
virtual const CellInfo * cellInfo(unsigned int i) const
const ShapeInfo * shape(CaloGain::CaloGain gain) const
Definition CellInfo.cxx:78
const EventData * eventData() const
Definition Data.h:95
CaloGain::CaloGain gain() const
Definition Data.h:85
void add(const FilterParams &params, const TString &fileName)
Definition FilterList.h:27
unsigned int size() const
Definition FilterList.h:29
const FilterParams & filterParams(unsigned int i) const
Definition FilterList.h:30
const TString & fileName(unsigned int i) const
Definition FilterList.h:31
const CellInfo * cellInfo() const
unsigned int nDataContainers() const
const std::vector< const EventData * > & eventData() const
Definition History.h:58
const CellInfo * cellInfo() const
Definition History.h:56
const Data * data(unsigned int i) const
Definition History.cxx:91
unsigned int nData() const
Definition History.h:51
static MultiTreeAccessor * openList(const TString &fileList)
unsigned int historySize(unsigned int i) const
const History * getSCHistory(unsigned int i) const
const EventData * eventData(unsigned int i) const
unsigned int historySizeSC(unsigned int i) const
std::vector< MultiTreeAccessor * > filterComponents(const FilterList &filterList, const DataTweaker &tweaker) const
unsigned int nAccessors() const
const History * getCellHistory(unsigned int i) const
MultiTreeAccessor(const std::vector< const TreeAccessor * > &accessors)
Constructor.
const TreeAccessor & accessor(unsigned int i) const
const RunData * runData(unsigned int i) const
std::vector< const TreeAccessor * > m_accessors
const CellInfo * getCellInfo(unsigned int i) const
bool writeToFile(const TString &fileName) const
static MultiTreeAccessor * open(const std::vector< TString > &files)
static MultiTreeAccessor * openWild(const TString &wcName)
static TreeAccessor * filter(const Accessor &accessor, const FilterParams &filterParams, const TString &fileName, const DataTweaker &tweaker)
static TreeAccessor * merge(const std::vector< const Accessor * > &accessors, const TString &fileName="")
static TreeAccessor * open(const TString &fileName)
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
STL namespace.