ATLAS Offline Software
Loading...
Searching...
No Matches
LiveHistogramProvider.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
16namespace 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(std::move(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
57 if (lumiBlock > m_currentLumiBlock) {
58 // Update the variable keeping track of the highest LB.
59 m_currentLumiBlock = std::max((float) lumiBlock, m_histDef->xmax);
60
61 if (!m_currentHistogram) {
62 // The histogram does not exist yet and must be created.
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 = static_cast<TEfficiency*>(m_currentHistogram)->GetCopyTotalHisto();
70 TH1* passedClone = static_cast<TEfficiency*>(m_currentHistogram)->GetCopyPassedHisto();
71 m_factory->remove(*m_histDef);
73 TEfficiency* eNew = static_cast<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 = static_cast<TProfile*> (m_currentHistogram->Clone());
88 m_factory->remove(*m_histDef);
89 // Update the bin ranges and register the new histogram.
91 TProfile* hNew = static_cast<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.
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
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
155 void copyDataToNewHistogram(TProfile* hOld, TProfile* hNew) {
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 */
Interface of the source of ROOT objects for HistogramFillers.
std::shared_ptr< HistogramDef > m_histDef
void copyDataToNewHistogram(TH1 *hOld, TH1 *hNew)
Copies bin contents from an old to a new histogram.
GenericMonitoringTool *const m_gmTool
void copyDataToNewHistogram(TProfile *hOld, TProfile *hNew)
void updateHistDef()
Updates HistogramDef xmin, xmax and xbins members.
std::shared_ptr< HistogramFactory > m_factory
LiveHistogramProvider(GenericMonitoringTool *const gmTool, std::shared_ptr< HistogramFactory > factory, const HistogramDef &histDef)
Constructor.
TNamed * histogram() override
Getter of ROOT histogram.
Generic monitoring tool for athena components.
STL namespace.
the internal class used to keep parsed Filler properties