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