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/ShapeInfo.h"
10 #include "LArCafJobs/EventData.h"
11 #include "TSystem.h"
12 #include "TChain.h"
13 #include <iostream>
14 #include <iomanip>
15 #include <fstream>
16 #include <string>
17 
18 using std::string;
19 using std::cout;
20 using std::endl;
21 using namespace std;
22 
23 using namespace LArSamples;
24 
25 
26 MultiTreeAccessor* MultiTreeAccessor::open(const std::vector<TString>& files)
27 {
28  std::vector<const TreeAccessor*> accessors;
29  for (const TString& fileName : files) {
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 
43 MultiTreeAccessor* MultiTreeAccessor::openList(const TString& fileList)
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) {
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 
71 MultiTreeAccessor* MultiTreeAccessor::openWild(const TString& wcName)
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();
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 
95 MultiTreeAccessor::~MultiTreeAccessor()
96 {
97  for (const TreeAccessor* accessor : m_accessors)
98  delete accessor;
99 }
100 
101 
102 const 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 
114 const 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 
126 unsigned 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 
135 unsigned 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 
144 unsigned 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 
155 
156 const History* MultiTreeAccessor::getCellHistory(unsigned int i) const
157 {
158  CellInfo* cellInfo = nullptr;
159  std::vector<const Data*> allData;
160  std::vector<const EventData*> allEventData;
161  for (const TreeAccessor* accessor : m_accessors) {
162  const History* thisHistory = accessor->getCellHistory(i);
163  if (!thisHistory) continue;
164  if (!cellInfo) {
165  cellInfo = new CellInfo(*thisHistory->cellInfo());
166  }
167  const std::vector<const EventData*>& thisEventData = thisHistory->eventData();
168  std::map<const EventData*, const EventData*> eventMap;
169  for (const EventData* event : thisEventData) {
170  if (eventMap.find(event) != eventMap.end()) continue;
171  EventData* newED = new EventData(*event);
172  eventMap[event] = newED;
173  allEventData.push_back(newED);
174  }
175 
176  for (unsigned int i = 0; i < thisHistory->nData(); i++) {
177  allData.push_back(new Data(*thisHistory->data(i), eventMap[thisHistory->data(i)->eventData()], nullptr, -1));
178  if (!cellInfo->shape(thisHistory->data(i)->gain())) {
179  const ShapeInfo* thisShape = thisHistory->cellInfo()->shape(thisHistory->data(i)->gain());
180  cellInfo->setShape(thisHistory->data(i)->gain(), thisShape ? new ShapeInfo(*thisShape) : nullptr);
181  }
182  }
183  delete thisHistory;
184  }
185  //data are copied from cellInfo into History member variable
186  auto * h = cellInfo ? new History(allData, *cellInfo, allEventData, i): nullptr;
187  delete cellInfo;
188  return h;
189 }
190 
191 
192 const CellInfo* MultiTreeAccessor::getCellInfo(unsigned int i) const
193 {
194  resetCache();
195  for (const TreeAccessor* accessor : m_accessors) {
196  const HistoryContainer* cont = accessor->historyContainer(i);
197  if (cont && cont->cellInfo()) return new CellInfo(*cont->cellInfo());
198  }
199  return nullptr;
200 }
201 
202 
203 bool MultiTreeAccessor::writeToFile(const TString& fileName) const
204 {
205  std::vector<const Accessor*> accessors;
206  for (unsigned int i = 0; i < m_accessors.size(); i++)
207  accessors.push_back(m_accessors[i]);
208  cout << "Merging data..." << endl;
209  TreeAccessor* singleContainer = TreeAccessor::merge(accessors, fileName);
210  if (!singleContainer) return false;
211  delete singleContainer;
212  return true;
213 }
214 
215 
216 std::vector<MultiTreeAccessor*> MultiTreeAccessor::filterComponents(const FilterList& filterList, const DataTweaker& tweaker) const
217 {
218  std::vector< std::vector<const TreeAccessor*> > filteredAccessors(filterList.size());
219 
220  for (unsigned int i = 0; i < nAccessors(); i++) {
221  const TreeAccessor* treeAcc = dynamic_cast<const TreeAccessor*>(&accessor(i));
222  cout << "Processing data " << i << " of " << nAccessors();
223  if (treeAcc) cout << " (fileName = " << treeAcc->fileName() << ")";
224  cout << endl;
225  FilterList thisFilterList;
226  //
227  for (unsigned int f = 0; f < filterList.size(); f++) {
228  std::string pathname = (string)filterList.fileName(f);
229  if( pathname.find("eos/atlas/") < pathname.length() ){
230  int nslashes = 0, slpos = 0;
231  for( int k1 = 0; k1 < (int)pathname.length(); k1++ ){
232  if( nslashes > 2 )break;
233  if( pathname[k1] != '/' )continue;
234  nslashes++;
235  slpos = k1;
236  }
237  pathname.resize( slpos );
238  }
239  TString thisFN = Form("%s_filter%d.root", pathname.c_str(), i );//filterList.fileName(f).Data(), i);
240  thisFilterList.add(filterList.filterParams(f), thisFN);
241  }
242  std::vector<TreeAccessor*> filteredTreeAccs = TreeAccessor::filter(accessor(i), thisFilterList, tweaker);
243  if (filteredTreeAccs.size() != filterList.size()) {
244  cout << "Filtering failed, exiting" << endl;
245  return std::vector<MultiTreeAccessor*>();
246  }
247  for (unsigned int f = 0; f < filteredTreeAccs.size(); f++) filteredAccessors[f].push_back(filteredTreeAccs[f]);
248  }
249 
250  std::vector<MultiTreeAccessor*> result;
251  for (unsigned int f = 0; f < filteredAccessors.size(); f++) result.push_back(new MultiTreeAccessor(filteredAccessors[f]));
252  return result;
253 }
nEvents
const int nEvents
Definition: fbtTestBasics.cxx:77
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:56
Data
@ Data
Definition: BaseObject.h:11
LArSamples::History
Definition: History.h:35
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
ShapeInfo.h
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:91
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
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:24
LArSamples::History::nData
unsigned int nData() const
Definition: History.h:51
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:85
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:95
LArSamples::History::eventData
const std::vector< const EventData * > & eventData() const
Definition: History.h:58
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