ATLAS Offline Software
MultiTreeAccessor.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 #include "LArSamplesMon/Data.h"
9 #include "LArCafJobs/EventData.h"
10 #include "TSystem.h"
11 #include "TChain.h"
12 #include <iostream>
13 #include <iomanip>
14 #include <fstream>
15 #include <string>
16 
17 using std::string;
18 using std::cout;
19 using std::endl;
20 using namespace std;
21 
22 using namespace LArSamples;
23 
24 
25 MultiTreeAccessor* MultiTreeAccessor::open(const std::vector<TString>& files)
26 {
27  std::vector<const TreeAccessor*> accessors;
28  for (const TString& fileName : files) {
30  if (!accessor) {
31  cout << "Skipping invalid file " << fileName << endl;
32  continue;
33  }
34  accessors.push_back(accessor);
35  }
36 
37  if (accessors.empty()) return nullptr;
38  return new MultiTreeAccessor(accessors);
39 }
40 
41 
42 MultiTreeAccessor* MultiTreeAccessor::openList(const TString& fileList)
43 {
44  std::ifstream f(fileList);
45  if (!f) {
46  cout << "file " << fileList << " not accessible" << endl;
47  return nullptr;
48  }
49 
50  std::string fileName;
51  unsigned int i = 0;
52 
53  std::vector<const TreeAccessor*> accessors;
54 
55  while (f >> fileName) {
57  if (!accessor) {
58  cout << "Skipping invalid file " << fileName << endl;
59  continue;
60  }
61  cout << std::setw(2) << ++i << " - " << fileName << endl;
62  accessors.push_back(accessor);
63  }
64 
65  if (accessors.empty()) return nullptr;
66  return new MultiTreeAccessor(accessors);
67 }
68 
69 
70 MultiTreeAccessor* MultiTreeAccessor::openWild(const TString& wcName)
71 {
72  // Piggyback on TChain wildcarding feature...
73  TChain chain("");
74  chain.Add(wcName);
75 
76  std::vector<const TreeAccessor*> accessors;
77 
78  for (int i = 0; i < chain.GetListOfFiles()->GetEntries(); i++) {
79  std::string fileName = chain.GetListOfFiles()->At(i)->GetTitle();
81  if (!accessor) {
82  cout << "Skipping invalid file " << fileName << endl;
83  continue;
84  }
85  cout << std::setw(2) << i+1 << " - " << fileName << " , nEvents = " << accessor->nEvents() << ", nRuns = " << accessor->nRuns() << endl;
86  accessors.push_back(accessor);
87  }
88 
89  if (accessors.empty()) return nullptr;
90  return new MultiTreeAccessor(accessors);
91 }
92 
93 
94 MultiTreeAccessor::~MultiTreeAccessor()
95 {
96  for (const TreeAccessor* accessor : m_accessors)
97  delete accessor;
98 }
99 
100 
101 const EventData* MultiTreeAccessor::eventData(unsigned int i) const
102 {
103  unsigned int nEventsSoFar = 0;
104  for (const TreeAccessor* accessor : m_accessors) {
105  unsigned int n = accessor->nEvents();
106  if (i < nEventsSoFar + n) return accessor->eventData(i - nEventsSoFar);
107  nEventsSoFar += n;
108  }
109  return nullptr;
110 }
111 
112 
113 const RunData* MultiTreeAccessor::runData(unsigned int i) const
114 {
115  unsigned int nRunsSoFar = 0;
116  for (const TreeAccessor* accessor : m_accessors) {
117  unsigned int n = accessor->nRuns();
118  if (i < nRunsSoFar + n) return accessor->runData(i - nRunsSoFar);
119  nRunsSoFar += n;
120  }
121  return nullptr;
122 }
123 
124 
125 unsigned int MultiTreeAccessor::nEvents() const
126 {
127  unsigned int n = 0;
128  for (const TreeAccessor* accessor : m_accessors)
129  n += accessor->nEvents();
130  return n;
131 }
132 
133 
134 unsigned int MultiTreeAccessor::nRuns() const
135 {
136  unsigned int n = 0;
137  for (const TreeAccessor* accessor : m_accessors)
138  n += accessor->nRuns();
139  return n;
140 }
141 
142 
143 unsigned int MultiTreeAccessor::historySize(unsigned int i) const
144 {
145  resetCache();
146  unsigned int size = 0;
147  for (const TreeAccessor* accessor : m_accessors) {
148  const HistoryContainer* cont = accessor->historyContainer(i);
149  if (cont) size += cont->nDataContainers();
150  }
151  return size;
152 }
153 
154 
155 const History* MultiTreeAccessor::getCellHistory(unsigned int i) const
156 {
157  CellInfo* cellInfo = nullptr;
158  std::vector<const Data*> allData;
159  std::vector<const EventData*> allEventData;
160  for (const TreeAccessor* accessor : m_accessors) {
161  const History* thisHistory = accessor->getCellHistory(i);
162  if (!thisHistory) continue;
163  if (!cellInfo) {
164  cellInfo = new CellInfo(*thisHistory->cellInfo());
165  }
166  const std::vector<const EventData*>& thisEventData = thisHistory->eventData();
167  std::map<const EventData*, const EventData*> eventMap;
168  for (const EventData* event : thisEventData) {
169  if (eventMap.find(event) != eventMap.end()) continue;
170  EventData* newED = new EventData(*event);
171  eventMap[event] = newED;
172  allEventData.push_back(newED);
173  }
174 
175  for (unsigned int i = 0; i < thisHistory->nData(); i++) {
176  allData.push_back(new Data(*thisHistory->data(i), eventMap[thisHistory->data(i)->eventData()], nullptr, -1));
177  if (!cellInfo->shape(thisHistory->data(i)->gain())) {
178  const ShapeInfo* thisShape = thisHistory->cellInfo()->shape(thisHistory->data(i)->gain());
179  cellInfo->setShape(thisHistory->data(i)->gain(), thisShape ? new ShapeInfo(*thisShape) : nullptr);
180  }
181  }
182  delete thisHistory;
183  }
184  //data are copied from cellInfo into History member variable
185  auto * h = cellInfo ? new History(allData, *cellInfo, allEventData, i): nullptr;
186  delete cellInfo;
187  return h;
188 }
189 
190 
191 const CellInfo* MultiTreeAccessor::getCellInfo(unsigned int i) const
192 {
193  resetCache();
194  for (const TreeAccessor* accessor : m_accessors) {
195  const HistoryContainer* cont = accessor->historyContainer(i);
196  if (cont && cont->cellInfo()) return new CellInfo(*cont->cellInfo());
197  }
198  return nullptr;
199 }
200 
201 
202 bool MultiTreeAccessor::writeToFile(const TString& fileName) const
203 {
204  std::vector<const Accessor*> accessors;
205  for (unsigned int i = 0; i < m_accessors.size(); i++)
206  accessors.push_back(m_accessors[i]);
207  cout << "Merging data..." << endl;
208  TreeAccessor* singleContainer = TreeAccessor::merge(accessors, fileName);
209  if (!singleContainer) return false;
210  delete singleContainer;
211  return true;
212 }
213 
214 
215 std::vector<MultiTreeAccessor*> MultiTreeAccessor::filterComponents(const FilterList& filterList, const DataTweaker& tweaker) const
216 {
217  std::vector< std::vector<const TreeAccessor*> > filteredAccessors(filterList.size());
218 
219  for (unsigned int i = 0; i < nAccessors(); i++) {
220  const TreeAccessor* treeAcc = dynamic_cast<const TreeAccessor*>(&accessor(i));
221  cout << "Processing data " << i << " of " << nAccessors();
222  if (treeAcc) cout << " (fileName = " << treeAcc->fileName() << ")";
223  cout << endl;
224  FilterList thisFilterList;
225  //
226  for (unsigned int f = 0; f < filterList.size(); f++) {
227  std::string pathname = (string)filterList.fileName(f);
228  if( pathname.find("eos/atlas/") < pathname.length() ){
229  int nslashes = 0, slpos = 0;
230  for( int k1 = 0; k1 < (int)pathname.length(); k1++ ){
231  if( nslashes > 2 )break;
232  if( pathname[k1] != '/' )continue;
233  nslashes++;
234  slpos = k1;
235  }
236  pathname.resize( slpos );
237  }
238  TString thisFN = Form("%s_filter%d.root", pathname.c_str(), i );//filterList.fileName(f).Data(), i);
239  thisFilterList.add(filterList.filterParams(f), thisFN);
240  }
241  std::vector<TreeAccessor*> filteredTreeAccs = TreeAccessor::filter(accessor(i), thisFilterList, tweaker);
242  if (filteredTreeAccs.size() != filterList.size()) {
243  cout << "Filtering failed, exiting" << endl;
244  return std::vector<MultiTreeAccessor*>();
245  }
246  for (unsigned int f = 0; f < filteredTreeAccs.size(); f++) filteredAccessors[f].push_back(filteredTreeAccs[f]);
247  }
248 
249  std::vector<MultiTreeAccessor*> result;
250  for (unsigned int f = 0; f < filteredAccessors.size(); f++) result.push_back(new MultiTreeAccessor(filteredAccessors[f]));
251  return result;
252 }
MultiTreeAccessor.h
get_generator_info.result
result
Definition: get_generator_info.py:21
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArSamples::CellInfo::setShape
bool setShape(CaloGain::CaloGain gain, ShapeInfo *shape)
Definition: CellInfo.cxx:90
LArSamples::History::cellInfo
const CellInfo * cellInfo() const
Definition: History.h:61
Data
@ Data
Definition: BaseObject.h:11
LArSamples::History
Definition: History.h:40
LArSamples::FilterList::filterParams
const FilterParams & filterParams(unsigned int i) const
Definition: FilterList.h:30
CscCalibQuery.fileList
fileList
Definition: CscCalibQuery.py:330
LArSamples
Definition: AbsShape.h:24
TreeAccessor.h
GetEntries
TGraphErrors * GetEntries(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:4019
LArSamples::ShapeInfo
Definition: ShapeInfo.h:24
LArSamples::FilterList::add
void add(const FilterParams &params, const TString &fileName)
Definition: FilterList.h:27
covarianceTool.filter
filter
Definition: covarianceTool.py:514
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
LArSamples::RunData
Definition: RunData.h:21
LArSamples::History::data
const Data * data(unsigned int i) const
Definition: History.cxx:89
systematicsTool.writeToFile
def writeToFile(histDict, fOut)
Definition: systematicsTool.py:1035
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
lumiFormat.i
int i
Definition: lumiFormat.py:85
h
beamspotman.n
n
Definition: beamspotman.py:731
extractSporadic.h
list h
Definition: extractSporadic.py:97
LArSamples::CellInfo::shape
const ShapeInfo * shape(CaloGain::CaloGain gain) const
Definition: CellInfo.cxx:78
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
nEvents
int nEvents
Definition: fbtTestBasics.cxx:79
hist_file_dump.f
f
Definition: hist_file_dump.py:135
ITk::EventData
InDet::SiSpacePointsSeedMakerEventData EventData
Definition: ITkSiSpacePointsSeedMaker.h:63
LArSamples::DataTweaker
Definition: DataTweaker.h:23
LArSamples::History::nData
unsigned int nData() const
Definition: History.h:56
EventData.h
LArSamples::FilterList
Definition: FilterList.h:21
LArSamples::MultiTreeAccessor
Definition: MultiTreeAccessor.h:27
LArSamples::FilterList::fileName
const TString & fileName(unsigned int i) const
Definition: FilterList.h:31
LArSamples::Data::gain
CaloGain::CaloGain gain() const
Definition: Data.h:90
LArSamples::CellInfo
Definition: CellInfo.h:31
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
Trk::open
@ open
Definition: BinningType.h:40
LArSamples::HistoryContainer
Definition: HistoryContainer.h:29
python.utility.LHE.merge
def merge(input_file_pattern, output_file)
Merge many input LHE files into a single output file.
Definition: LHE.py:29
Data.h
xAODRootTest.accessors
dictionary accessors
Definition: xAODRootTest.py:73
LArSamples::Data::eventData
const EventData * eventData() const
Definition: Data.h:100
LArSamples::History::eventData
const std::vector< const EventData * > & eventData() const
Definition: History.h:63
LArSamples::TreeAccessor
Definition: TreeAccessor.h:32
LArSamples::FilterList::size
unsigned int size() const
Definition: FilterList.h:29
LArSamples::EventData
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/EventData.h:29
LArSamples::PersistentAccessor::fileName
TString fileName() const
Definition: PersistentAccessor.cxx:135
python.ComponentAccumulator.filterComponents
def filterComponents(comps, onlyComponents=[])
Definition: ComponentAccumulator.py:79
LArSamples::HistoryContainer::nDataContainers
unsigned int nDataContainers() const
Definition: HistoryContainer.h:40