ATLAS Offline Software
TreeShapeErrorGetter.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 
7 
9 #include "LArCafJobs/Geometry.h"
11 
12 #include "TFile.h"
13 #include "TH2D.h"
14 #include "TChain.h"
15 
16 #include <iomanip>
17 #include <fstream>
18 #include <iostream>
19 using std::cout;
20 using std::endl;
21 
22 using namespace LArSamples;
23 
24 
26  : m_file(nullptr), m_cellTrees(3), m_ringTrees(3), m_cellCalc(nullptr), m_ringCalc(nullptr)
27 {
28  m_file = TFile::Open(fileName, (recreate ? "RECREATE" : "READ"));
29  if (!m_file || !m_file->IsOpen()) {
30  cout << "File " << fileName << " is not accessible" << endl;
31  return;
32  }
33  if (recreate) {
34  m_cellTrees[0] = new TTree("cells_HIGH", "");
35  m_cellTrees[1] = new TTree("cells_MED", "");
36  m_cellTrees[2] = new TTree("cells_LOW", "");
37  m_ringTrees[0] = new TTree("rings_HIGH", "");
38  m_ringTrees[1] = new TTree("rings_MED", "");
39  m_ringTrees[2] = new TTree("rings_LOW", "");
40  for (unsigned int i = 0; i < 3; i++ ) {
41  m_cellTrees[i]->Branch("calc", &m_cellCalc, 32000, 0);
42  m_ringTrees[i]->Branch("calc", &m_ringCalc, 32000, 0);
43  }
44  }
45  else {
46  m_cellTrees[0] = (TTree*)m_file->Get("cells_HIGH");
47  m_cellTrees[1] = (TTree*)m_file->Get("cells_MED");
48  m_cellTrees[2] = (TTree*)m_file->Get("cells_LOW");
49  if (!m_cellTrees[0] || !m_cellTrees[1] || !m_cellTrees[2]) {
50  cout << "Trees with cell shape error data not found in " << fileName << endl;
51  return;
52  }
53  m_ringTrees[0] = (TTree*)m_file->Get("rings_HIGH");
54  m_ringTrees[1] = (TTree*)m_file->Get("rings_MED");
55  m_ringTrees[2] = (TTree*)m_file->Get("rings_LOW");
56  if (!m_ringTrees[0] || !m_ringTrees[1] || !m_ringTrees[2]) {
57  cout << "Trees with phi symmetric shape error data not found in " << fileName << endl;
58  return;
59  }
60  for (unsigned int i = 0; i < 3; i++ ) {
61  m_cellTrees[i]->SetBranchAddress("calc", &m_cellCalc);
62  m_ringTrees[i]->SetBranchAddress("calc", &m_ringCalc);
63  }
64  }
65 }
66 
67 
69 {
70  if (m_file->GetOption() == TString("CREATE")) {
71  m_file->cd();
72  for (unsigned int g = 0; g < 3; g++ ) {
73  if (!m_cellTrees[g] || !m_ringTrees[g]) {
74  cout << "Missing trees, error writing data to TreeShapeErrorGetter" << endl;
75  return;
76  }
77  m_cellTrees[g]->Write();
78  m_ringTrees[g]->Write();
79  }
80  }
81  delete m_file;
82 }
83 
84 
86 {
87  switch (gain) {
88  case CaloGain::LARHIGHGAIN : return m_cellTrees[0];
89  case CaloGain::LARMEDIUMGAIN : return m_cellTrees[1];
90  case CaloGain::LARLOWGAIN : return m_cellTrees[2];
91  default : return nullptr;
92  }
93  return nullptr;
94 }
95 
96 
98 {
99  switch (gain) {
100  case CaloGain::LARHIGHGAIN : return m_ringTrees[0];
101  case CaloGain::LARMEDIUMGAIN : return m_ringTrees[1];
102  case CaloGain::LARLOWGAIN : return m_ringTrees[2];
103  default : return nullptr;
104  }
105  return nullptr;
106 }
107 
108 
110 {
111  if (!cellTree(gain) || hash >= cellTree(gain)->GetEntries()) return nullptr;
112  cellTree(gain)->GetEntry(hash);
113 
114  if (toExclude) m_cellCalc->remove(*toExclude);
115  if (m_cellCalc->size() < 2) return nullptr;
116  return m_cellCalc->shapeErrorData();
117 }
118 
119 
121 {
122  if (!ringTree(gain) || ring >= ringTree(gain)->GetEntries()) return nullptr;
123  ringTree(gain)->GetEntry(ring);
124 
125  if (toExclude) m_ringCalc->remove(*toExclude);
126  if (m_ringCalc->size() < 2) return nullptr;
127  return m_ringCalc->shapeErrorData();
128 }
129 
130 
132 {
133  if (!cellTree(gain)) return -1;
134  if (m_cellCalc) delete m_cellCalc;
136  cellTree(gain)->Fill();
137  return cellTree(gain)->GetEntries();
138 }
139 
140 
142 {
143  if (!ringTree(gain)) return 0;
144  if (m_ringCalc) delete m_ringCalc;
146  ringTree(gain)->Fill();
147  return ringTree(gain)->GetEntries();
148 }
149 
150 
152 {
153  if (!cellTree(gain)) return;
154  for (long long i = 0; i < cellTree(gain)->GetEntries(); i++) {
156  if (!sed) continue;
157  cout << "-> " << i << endl;
158  sed->xi().Print();
159  sed->xip().Print();
160  delete sed;
161  }
162 }
163 
165  unsigned int nBins, double xMin, double xMax) const
166 {
167  TH2D* h = new TH2D(Form("%s_%d", xip ? "xip" : "xi", sample), "", nBins, xMin, xMax, nBins, xMin, xMax);
168  for (long long i = 0; i < Definitions::nChannels; i++) {
170  if (!data1) continue;
171  ShapeErrorData* data2 = other.shapeErrorData(i, gain);
172  if (!data2) { delete data1; continue; }
173  cout << i << endl;
174  unsigned int sample1 = sample + data1->xip().GetLwb(); // sample indices may not match, but we assume the range covered for the reference shape is the same
175  unsigned int sample2 = sample + data2->xip().GetLwb();
176  if (data1->isInRange(sample1) && data2->isInRange(sample2))
177  h->Fill(xip ? data1->xip()(sample1) : data1->xi()(sample1),
178  xip ? data2->xip()(sample2) : data2->xi()(sample2));
179  delete data1;
180  delete data2;
181  }
182  return h;
183 }
184 
185 
186 bool TreeShapeErrorGetter::merge(const TString& listFile, const TString& outputFile)
187 {
188  std::ifstream f(listFile);
189  if (!f) {
190  cout << "file " << listFile << " not accessible" << endl;
191  return 0;
192  }
193 
194  std::string fileName;
195  unsigned int i = 0;
196  std::vector<const TreeShapeErrorGetter*> getters;
197 
198  while (f >> fileName) {
199  //gSystem->Exec("free");
200  const TreeShapeErrorGetter* getter = new TreeShapeErrorGetter(fileName.c_str());
201  if (!getter) {
202  cout << "Skipping invalid file " << fileName << endl;
203  continue;
204  }
205  cout << std::setw(2) << ++i << " - " << fileName << endl;
206  getters.push_back(getter);
207  }
208  bool result = merge(getters, outputFile);
209 
210  for (const TreeShapeErrorGetter* getter : getters)
211  delete getter;
212 
213  return result;
214 }
215 
216 
217 bool TreeShapeErrorGetter::merge(const std::vector<const TreeShapeErrorGetter*>& getters, const TString& outputFile)
218 {
219  auto output = std::make_unique<TreeShapeErrorGetter>(outputFile, true);
220  for (unsigned int i = 0; i < Definitions::nChannels; i++) {
221  for (unsigned int g = 0; g < 3; g++) {
222  bool gotResult = false;
223  for (const TreeShapeErrorGetter* getter : getters) {
224  if (getter->shapeErrorData(i, (CaloGain::CaloGain)g)) {
225  if (gotResult) {
226  cout << "TreeShapeErrorGetter::merge : input getters have non-zero overlap for cell " << i << " -- not supported, exiting." << endl;
227  return false;
228  }
229  gotResult = true;
230  cout << "Adding " << i << " " << g << " " << getter->cellCalc()->size() << endl;
231  output->addCell(*getter->cellCalc(), (CaloGain::CaloGain)g);
232  }
233  }
234  if (!gotResult) output->addCell(ResidualCalculator(), (CaloGain::CaloGain)g);
235  }
236  }
237 
238  for (int i = 0; i < Geo::nPhiRings(); i++) {
239  for (unsigned int g = 0; g < 3; g++) {
240  bool gotResult = false;
241  for (const TreeShapeErrorGetter* getter : getters) {
242  if (getter->phiSymShapeErrorData(i, (CaloGain::CaloGain)g)) {
243  if (gotResult) {
244  cout << "TreeShapeErrorGetter::merge : input getters have non-zero overlap for ring " << i << " -- not supported, exiting." << endl;
245  return false;
246  }
247  gotResult = true;
248  cout << "Adding ring " << i << " " << g << endl;
249  output->addRing(*getter->ringCalc(), (CaloGain::CaloGain)g);
250  }
251  }
252  if (!gotResult) output->addRing(ResidualCalculator(), (CaloGain::CaloGain)g);
253  }
254  }
255  return true;
256 }
257 
258 
259 bool TreeShapeErrorGetter::compare(const TreeShapeErrorGetter& other, const TString& fileName, const Interface* tmpl) const
260 {
261  std::unique_ptr<TFile> f(TFile::Open(fileName, "RECREATE"));
262  auto tree = std::make_unique<TTree>("tree", "");
263 
264  int hash, gain, lwb1, lwb2, nSamples;
265  double xi1[99], xi2[99], xip1[99], xip2[99];
266  int calo;
267  unsigned int layer, ft, slot, channel;
268  double eta, phi;
269 
270  tree->Branch("hash", &hash);
271 
272  if (tmpl) {
273  tree->Branch("calo", &calo);
274  tree->Branch("layer", &layer);
275  tree->Branch("ft", &ft);
276  tree->Branch("slot", &slot);
277  tree->Branch("channel", &channel);
278  tree->Branch("eta", &eta);
279  tree->Branch("phi", &phi);
280  }
281  tree->Branch("gain", &gain);
282  tree->Branch("lwb1", &lwb1);
283  tree->Branch("lwb2", &lwb2);
284  tree->Branch("nSamples", &nSamples);
285  tree->Branch("xi1", xi1, "xi1[nSamples]/D");
286  tree->Branch("xi2", xi2, "xi2[nSamples]/D");
287  tree->Branch("xip1", xip1, "xip1[nSamples]/D");
288  tree->Branch("xip2", xip2, "xip2[nSamples]/D");
289 
290  for (long long k = 0; k < Definitions::nChannels; k++) {
291  if (k % 10000 == 0) cout << "Processing entry " << k << endl;
292  hash = k;
293  if (tmpl) {
294  const CellInfo* cellInfo = tmpl->cellInfo(k);
295  calo = (cellInfo ? cellInfo->calo() : -999 );
296  layer = (cellInfo ? cellInfo->layer() : -999 );
297  ft = (cellInfo ? cellInfo->feedThrough() : -999 );
298  slot = (cellInfo ? cellInfo->slot() : -999 );
299  channel = (cellInfo ? cellInfo->channel() : -999 );
300  eta = (cellInfo ? cellInfo->eta() : -999 );
301  phi = (cellInfo ? cellInfo->phi() : -999 );
302  }
303  for (unsigned int g = 0; g < 3; g++) {
304  gain = g;
305  std::unique_ptr<ShapeErrorData> data1(shapeErrorData(k, (CaloGain::CaloGain)g));
306  std::unique_ptr<ShapeErrorData> data2(other.shapeErrorData(k, (CaloGain::CaloGain)g));
307 
308  lwb1 = (data1 ? data1->lwb() : -1);
309  lwb2 = (data2 ? data2->lwb() : -1);
310  nSamples = (data1 ? data1->nSamples() : data2 ? data2->nSamples() : 0);
311  int lwb = (lwb1 >= 0 ? lwb1 : lwb2);
312  if (lwb<0) throw std::runtime_error("TreeShapeErrorGetter::compare() attempt to access arrays with negative index");
313  for (int j = lwb; j < lwb + nSamples; j++) {
314  xi1[j] = (data1 ? data1->xi()(j) : -999);
315  xip1[j] = (data1 ? data1->xip()(j) : -999);
316  xi2[j] = (data2 && data2->isInRange(j) ? data2->xi()(j) : -999);
317  xip2[j] = (data2 && data2->isInRange(j) ? data2->xip()(j) : -999);
318  }
319  tree->Fill();
320  }
321  }
322  f->cd();
323  tree->Write();
324  return true;
325 }
LArSamples::TreeShapeErrorGetter::m_ringTrees
std::vector< TTree * > m_ringTrees
Definition: TreeShapeErrorGetter.h:64
TreeShapeErrorGetter.h
LArSamples::TreeShapeErrorGetter::TreeShapeErrorGetter
TreeShapeErrorGetter(const TString &fileName, bool recreate=false)
Definition: TreeShapeErrorGetter.cxx:25
LArSamples::TreeShapeErrorGetter::phiSymShapeErrorData
ShapeErrorData * phiSymShapeErrorData(short ring, CaloGain::CaloGain gain, const Residual *toExclude=0) const
Definition: TreeShapeErrorGetter.cxx:120
PlotCalibFromCool.ft
ft
Definition: PlotCalibFromCool.py:329
LArSamples::CellInfo::slot
short slot() const
Definition: CellInfo.h:68
get_generator_info.result
result
Definition: get_generator_info.py:21
LArSamples::CellInfo::calo
CaloId calo() const
Definition: CellInfo.h:50
LArSamples::TreeShapeErrorGetter::addCell
int addCell(const ResidualCalculator &calc, CaloGain::CaloGain gain)
Definition: TreeShapeErrorGetter.cxx:131
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:24
Residual.h
tree
TChain * tree
Definition: tile_monitor.h:30
CaloCondBlobAlgs_fillNoiseFromASCII.gain
gain
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:110
LArSamples::TreeShapeErrorGetter::m_file
TFile * m_file
Definition: TreeShapeErrorGetter.h:62
LArSamples::CellInfo::feedThrough
short feedThrough() const
Definition: CellInfo.h:65
Geometry.h
LArSamples::TreeShapeErrorGetter::m_cellCalc
ResidualCalculator * m_cellCalc
Definition: TreeShapeErrorGetter.h:65
LArSamples::AbsLArCells::cellInfo
virtual const CellInfo * cellInfo(unsigned int i) const
Definition: AbsLArCells.cxx:71
LArSamples
Definition: AbsShape.h:24
GetEntries
TGraphErrors * GetEntries(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:4019
LArSamples::CellInfo::phi
double phi() const
Definition: CellInfo.h:94
LArSamples::ResidualCalculator::remove
bool remove(const Residual &residual)
Definition: LArCalorimeter/LArSamplesMon/src/Residual.cxx:221
LArSamples::ResidualCalculator::size
unsigned int size() const
Definition: LArCalorimeter/LArSamplesMon/LArSamplesMon/Residual.h:117
LArSamples::ResidualCalculator
Definition: LArCalorimeter/LArSamplesMon/LArSamplesMon/Residual.h:101
LArSamples::TreeShapeErrorGetter::correlate
TH2D * correlate(const TreeShapeErrorGetter &other, CaloGain::CaloGain gain, unsigned short sample, bool xip, unsigned int nBins, double xMin, double xMax) const
Definition: TreeShapeErrorGetter.cxx:164
LArSamples::TreeShapeErrorGetter::shapeErrorData
ShapeErrorData * shapeErrorData(unsigned int hash, CaloGain::CaloGain gain, const Residual *toExclude=0) const
Definition: TreeShapeErrorGetter.cxx:109
LArSamples::TreeShapeErrorGetter::addRing
int addRing(const ResidualCalculator &calc, CaloGain::CaloGain gain)
Definition: TreeShapeErrorGetter.cxx:141
LArSamples::TreeShapeErrorGetter::ringTree
TTree * ringTree(CaloGain::CaloGain gain) const
Definition: TreeShapeErrorGetter.cxx:97
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::ShapeErrorData::xip
const TVectorD & xip() const
Definition: ShapeErrorData.h:41
compareGeometries.outputFile
string outputFile
Definition: compareGeometries.py:25
RTTAlgmain.data2
data2
Definition: RTTAlgmain.py:54
LArSamples::TreeShapeErrorGetter::compare
bool compare(const TreeShapeErrorGetter &other, const TString &fileName, const Interface *tmpl=0) const
Definition: TreeShapeErrorGetter.cxx:259
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
LArSamples::Definitions::nChannels
static const unsigned int nChannels
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:14
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:113
lumiFormat.i
int i
Definition: lumiFormat.py:85
h
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
extractSporadic.h
list h
Definition: extractSporadic.py:97
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
LArSamples::TreeShapeErrorGetter::merge
static bool merge(const TString &listFile, const TString &outputFile)
Definition: TreeShapeErrorGetter.cxx:186
LArSamples::TreeShapeErrorGetter::m_ringCalc
ResidualCalculator * m_ringCalc
Definition: TreeShapeErrorGetter.h:65
LArSamples::TreeShapeErrorGetter::~TreeShapeErrorGetter
virtual ~TreeShapeErrorGetter()
Definition: TreeShapeErrorGetter.cxx:68
LArSamples::TreeShapeErrorGetter::cellTree
TTree * cellTree(CaloGain::CaloGain gain) const
Definition: TreeShapeErrorGetter.cxx:85
hist_file_dump.f
f
Definition: hist_file_dump.py:135
LArSamples::TreeShapeErrorGetter::dump
void dump(CaloGain::CaloGain gain) const
Definition: TreeShapeErrorGetter.cxx:151
LArSamples::ResidualCalculator::shapeErrorData
ShapeErrorData * shapeErrorData() const
Definition: LArCalorimeter/LArSamplesMon/src/Residual.cxx:236
LArSamples::Residual
storage of a pulse shape residual set
Definition: LArCalorimeter/LArSamplesMon/LArSamplesMon/Residual.h:29
merge.output
output
Definition: merge.py:17
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
LArSamples::TreeShapeErrorGetter
Definition: TreeShapeErrorGetter.h:30
CaloGain::LARHIGHGAIN
@ LARHIGHGAIN
Definition: CaloGain.h:18
LArSamples::CellInfo
Definition: CellInfo.h:31
LArSamples::TreeShapeErrorGetter::m_cellTrees
std::vector< TTree * > m_cellTrees
Definition: TreeShapeErrorGetter.h:63
CaloGain::CaloGain
CaloGain
Definition: CaloGain.h:11
RTTAlgmain.data1
data1
Definition: RTTAlgmain.py:54
LArSamples::CellInfo::channel
short channel() const
Definition: CellInfo.h:76
CaloGain::LARMEDIUMGAIN
@ LARMEDIUMGAIN
Definition: CaloGain.h:18
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
LArSamples::Interface
Definition: Interface.h:36
LArDigits2NtupleDumper.nSamples
nSamples
Definition: LArDigits2NtupleDumper.py:70
LArSamples::CellInfo::eta
double eta() const
Definition: CellInfo.h:93
LArSamples::ShapeErrorData
Definition: ShapeErrorData.h:19
python.CaloScaleNoiseConfig.default
default
Definition: CaloScaleNoiseConfig.py:79
CaloGain::LARLOWGAIN
@ LARLOWGAIN
Definition: CaloGain.h:18
LArSamples::ShapeErrorData::xi
const TVectorD & xi() const
Definition: ShapeErrorData.h:40
LArSamples::FitterData::sed
const ScaledErrorData * sed
Definition: ShapeFitter.cxx:26
LArSamples::Geo::nPhiRings
static short nPhiRings()
Definition: Geometry.cxx:84
beamspotnt.calc
calc
Definition: bin/beamspotnt.py:1252
Interface.h
LArSamples::CellInfo::layer
short layer() const
Definition: CellInfo.h:53
fitman.k
k
Definition: fitman.py:528