ATLAS Offline Software
LiveHistogramProvider.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef AthenaMonitoringKernel_HistogramFiller_LiveHistogramProvider_h
6 #define AthenaMonitoringKernel_HistogramFiller_LiveHistogramProvider_h
7 
8 #include <memory>
9 
13 
14 #include "HistogramFactory.h"
15 
16 namespace Monitored {
25  public:
26 
35  GenericMonitoringTool* const gmTool,
36  std::shared_ptr<HistogramFactory> factory,
37  const HistogramDef& histDef)
39  , m_gmTool(gmTool)
40  , m_factory(factory)
41  , m_histDef(new HistogramDef(histDef))
42  {}
43 
53  TNamed* histogram() override {
54  // Get the LB for this event.
55  const int lumiBlock = m_gmTool->lumiBlock();
56 
58  // Update the variable keeping track of the highest LB.
60 
61  if (!m_currentHistogram) {
62  // The histogram does not exist yet and must be created.
63  updateHistDef();
65  } else {
66  // The histogram exists and needs to be rolled
67  if (m_histDef->type=="TEfficiency") {
68  // Roll a TEfficiency (same process as the codeblock immediately above)
69  TH1* totalClone = ((TEfficiency*) m_currentHistogram)->GetCopyTotalHisto();
70  TH1* passedClone = ((TEfficiency*) m_currentHistogram)->GetCopyPassedHisto();
71  m_factory->remove(*m_histDef);
72  updateHistDef();
73  TEfficiency* eNew = (TEfficiency*) m_factory->create(*m_histDef);
74  TH1* totalNew = eNew->GetCopyTotalHisto();
75  TH1* passedNew = eNew->GetCopyPassedHisto();
76  copyDataToNewHistogram(totalClone, totalNew);
77  copyDataToNewHistogram(passedClone, passedNew);
78  eNew->SetTotalHistogram(*totalNew, "");
79  eNew->SetPassedHistogram(*passedNew, "");
80  m_currentHistogram = eNew;
81  delete totalClone;
82  delete totalNew;
83  delete passedClone;
84  delete passedNew;
85  } else if (m_histDef->type=="TProfile") {
86  // Store the data and deregister the old histogram.
87  TProfile* hClone = (TProfile*) m_currentHistogram->Clone();
88  m_factory->remove(*m_histDef);
89  // Update the bin ranges and register the new histogram.
90  updateHistDef();
91  TProfile* hNew = (TProfile*) m_factory->create(*m_histDef);
92  // Fill it with the old histogram's data and update pointer.
93  copyDataToNewHistogram(hClone, hNew);
94  m_currentHistogram = hNew;
95  // Free the memory used by the clone
96  delete hClone;
97  } else {
98  // Store the data and deregister the old histogram.
99  TH1* hClone = (TH1*) m_currentHistogram->Clone();
100  m_factory->remove(*m_histDef);
101  // Update the bin ranges and register the new histogram.
102  updateHistDef();
103  TH1* hNew = (TH1*) m_factory->create(*m_histDef);
104  // Fill it with the old histogram's data and update pointer.
105  copyDataToNewHistogram(hClone, hNew);
106  m_currentHistogram = hNew;
107  // Free the memory used by the clone
108  delete hClone;
109  }
110  }
111  }
112  return m_currentHistogram;
113  }
114 
118  void updateHistDef() {
119  m_histDef->xmax = std::max(m_currentLumiBlock + 0.5f, m_histDef->xmax);
120  m_histDef->xmin = std::max(m_currentLumiBlock + 0.5f - m_histDef->kLive, 0.5f);
121  m_histDef->xbins = m_histDef->xmax - m_histDef->xmin;
122  }
123 
127  void copyDataToNewHistogram(TH1* hOld, TH1* hNew) {
128  int offset = hNew->GetXaxis()->GetXmax() - hOld->GetXaxis()->GetXmax();
129  int nNewEntries(0);
130  bool sumw2Filled = (hOld->GetSumw2N()>0);
131 
132  // Loop through the old histogram bins
133  for (int oldBin=0; oldBin < hOld->GetNcells(); oldBin++) {
134  // Convert global bin number into x-y-z bin number
135  int oldBinX, oldBinY, oldBinZ;
136  hOld->GetBinXYZ(oldBin, oldBinX, oldBinY, oldBinZ);
137  if ((oldBinX-offset < 1) || hOld->IsBinUnderflow(oldBin, 1) || hOld->IsBinOverflow(oldBin, 1)) {
138  // Overflow bins are ignored since their meaning has changed.
139  continue;
140  } else {
141  // Get the global bin coordinate of this (x, y, z) bin coordinates.
142  int newBin = hNew->GetBin(oldBinX-offset, oldBinY, oldBinZ);
143  if (hOld->GetBinContent(oldBin)) hNew->SetBinContent(newBin, hOld->GetBinContent(oldBin));
144  if (sumw2Filled) {
145  hNew->SetBinError(newBin, hOld->GetBinError(oldBin));
146  nNewEntries+=(*hOld->GetSumw2())[oldBin]; // works correctly only for weight=1
147  }
148  }
149  }
150  // Update the total number of entries member.
151  if (sumw2Filled) hNew->SetEntries(nNewEntries);
152  else hNew->SetEntries(hOld->GetEntries()); // a choice since there is no way to get it right.
153  }
154 
156  int offset = hNew->GetXaxis()->GetXmax() - hOld->GetXaxis()->GetXmax();
157  int nNewEntries(0);
158  bool sumw2Filled = (hOld->GetSumw2N()>0);
159 
160  // Loop through the old histogram bins
161  for (int oldBin=0; oldBin < hOld->GetNcells(); oldBin++) {
162  // Convert global bin number into x-y-z bin number
163  int oldBinX, oldBinY, oldBinZ;
164  hOld->GetBinXYZ(oldBin, oldBinX, oldBinY, oldBinZ);
165  if ((oldBinX-offset < 1) || hOld->IsBinUnderflow(oldBin, 1) || hOld->IsBinOverflow(oldBin, 1)) {
166  // Overflow bins are ignored since their meaning has changed.
167  continue;
168  } else {
169  // Get the global bin coordinate of this (x, y, z) bin coordinates.
170  int newBin = hNew->GetBin(oldBinX-offset, oldBinY, oldBinZ);
171  int oldBinEntries = hOld->GetBinEntries(oldBin);
172  if (oldBinEntries>0) {
173  nNewEntries += oldBinEntries;
174  hNew->SetBinEntries(newBin, oldBinEntries);
175  (*hNew)[newBin] = (*hOld)[oldBin];
176  (*hNew->GetSumw2())[newBin] = (*hOld->GetSumw2())[oldBin];
177  }
178  }
179  }
180  // Update the total number of entries member.
181  if (sumw2Filled) hNew->SetEntries(nNewEntries);
182  else hNew->SetEntries(hOld->GetEntries()); // a choice since there is no way to get it right.
183  }
184 
185  private:
187  std::shared_ptr<HistogramFactory> m_factory;
188  std::shared_ptr<HistogramDef> m_histDef;
189  TNamed *m_currentHistogram = nullptr;
191  };
192 }
193 
194 #endif /* AthenaMonitoringKernel_HistogramFiller_LiveHistogramProvider_h */
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
IHistogramProvider.h
max
#define max(a, b)
Definition: cfImp.cxx:41
Monitored::IHistogramProvider
Interface of the source of ROOT objects for HistogramFillers.
Definition: IHistogramProvider.h:14
HistogramDef.h
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
GenericMonitoringTool::lumiBlock
virtual uint32_t lumiBlock()
Definition: GenericMonitoringTool.cxx:286
Monitored::LiveHistogramProvider
Provides latest-N-lumiblock histograms to be filled.
Definition: LiveHistogramProvider.h:24
Monitored::HistogramDef
the internal class used to keep parsed Filler properties
Definition: HistogramDef.h:15
GenericMonitoringTool.h
Monitored::LiveHistogramProvider::copyDataToNewHistogram
void copyDataToNewHistogram(TProfile *hOld, TProfile *hNew)
Definition: LiveHistogramProvider.h:155
TH1::SetBinContent
void SetBinContent(int, double)
Definition: rootspy.cxx:301
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
Monitored::LiveHistogramProvider::m_currentHistogram
TNamed * m_currentHistogram
Definition: LiveHistogramProvider.h:189
Monitored::LiveHistogramProvider::m_factory
std::shared_ptr< HistogramFactory > m_factory
Definition: LiveHistogramProvider.h:187
Monitored::LiveHistogramProvider::histogram
TNamed * histogram() override
Getter of ROOT histogram.
Definition: LiveHistogramProvider.h:53
TH1::SetBinError
void SetBinError(int, double)
Definition: rootspy.cxx:304
Monitored::LiveHistogramProvider::m_gmTool
GenericMonitoringTool *const m_gmTool
Definition: LiveHistogramProvider.h:186
GenericMonitoringTool
Definition: GenericMonitoringTool.py:1
Monitored::LiveHistogramProvider::m_currentLumiBlock
int m_currentLumiBlock
Definition: LiveHistogramProvider.h:190
TProfile
Definition: rootspy.cxx:515
TH1::GetBinContent
double GetBinContent(int) const
Definition: rootspy.cxx:298
HistogramFactory.h
Monitored::LiveHistogramProvider::LiveHistogramProvider
LiveHistogramProvider(GenericMonitoringTool *const gmTool, std::shared_ptr< HistogramFactory > factory, const HistogramDef &histDef)
Constructor.
Definition: LiveHistogramProvider.h:34
TH1
Definition: rootspy.cxx:268
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
Monitored::LiveHistogramProvider::copyDataToNewHistogram
void copyDataToNewHistogram(TH1 *hOld, TH1 *hNew)
Copies bin contents from an old to a new histogram.
Definition: LiveHistogramProvider.h:127
Monitored::LiveHistogramProvider::updateHistDef
void updateHistDef()
Updates HistogramDef xmin, xmax and xbins members.
Definition: LiveHistogramProvider.h:118
xAOD::lumiBlock
setTeId lumiBlock
Definition: L2StandAloneMuon_v1.cxx:327
Monitored::LiveHistogramProvider::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: LiveHistogramProvider.h:188