ATLAS Offline Software
Loading...
Searching...
No Matches
CounterBase.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "MonitoredRange.h"
6#include "MonitorBase.h"
7#include "CounterBase.h"
8#include "TH1F.h"
9#include "TH2F.h"
10#include "TProfile.h"
11#include <stdexcept>
12#include <format>
13
14CounterBase::CounterBase(const std::string& name, const MonitorBase* parent)
15 : m_name(name), m_parent(parent) {
16}
17
18
19const std::string& CounterBase::getName() const {
20 return m_name;
21}
22
23
25 return m_parent;
26}
27
28
29void CounterBase::regHistogram(const std::string& name,
30 const std::string& title,
31 const VariableType type,
32 const LogType xaxis,
33 const float min,
34 const float max,
35 const size_t bins)
36{
37 std::string hisSvcName = getParent()->getParent()->getName() + "_" + getParent()->getName() + "_" + getName() + "_" + name;
38 std::unique_ptr<TH1F> hist;
39
40 if (max <= min || bins == 0) {
41 throw std::runtime_error("CounterBase::regHistogram: Cannot have max <= min or bins == 0");
42 }
43
44 if (xaxis == kLinear) {
45 hist = std::make_unique<TH1F>(hisSvcName.c_str(), title.c_str(), bins, min, max);
46 } else if (xaxis == kLog) {
47 if (min <= 0) {
48 throw std::runtime_error("CounterBase::regHistogram: Cannot have min <= 0 with log binning");
49 }
50 std::unique_ptr<double[]> xbins = std::make_unique<double[]>(bins+1);
51 const double xlogmin = log10(min);
52 const double xlogmax = log10(max);
53 const double dlogx = (xlogmax-xlogmin)/((double)bins);
54 for (size_t i = 0; i <= bins; ++i) {
55 const double xlog = xlogmin + i*dlogx;
56 xbins[i] = exp( log(10) * xlog );
57 }
58 hist = std::make_unique<TH1F>(hisSvcName.c_str(), title.c_str(), bins, xbins.get());
59 } else {
60 throw std::runtime_error("CounterBase::regHistogram: Unknown logarithm flag");
61 }
62
63 m_variables.emplace(std::piecewise_construct,
64 std::forward_as_tuple(name),
65 std::forward_as_tuple(name, bookGetPointer(hist.release()), type)
66 );
67 // Histogram is now owned by THistSvc. A cache of the ptr is kept in the Variable
68}
69
70void CounterBase::regTProfile(const std::string& name,
71 const std::string& title,
72 const VariableType type,
73 const LogType xaxis,
74 const float min,
75 const float max,
76 const size_t bins)
77{
78 std::string hisSvcName = getParent()->getParent()->getName() + "_" + getParent()->getName() + "_" + getName() + "_" + name;
79 std::unique_ptr<TProfile> hist;
80
81 if (max <= min || bins == 0) {
82 throw std::runtime_error("CounterBase::regTProfile: Cannot have max <= min or bins == 0");
83 }
84
85 if (xaxis == kLinear) {
86 hist = std::make_unique<TProfile>(hisSvcName.c_str(), title.c_str(), bins, min, max);
87 } else if (xaxis == kLog) {
88 if (min <= 0) {
89 throw std::runtime_error("CounterBase::regTProfile: Cannot have min <= 0 with log binning");
90 }
91 std::unique_ptr<double[]> xbins = std::make_unique<double[]>(bins+1);
92 const double xlogmin = log10(min);
93 const double xlogmax = log10(max);
94 const double dlogx = (xlogmax-xlogmin)/((double)bins);
95 for (size_t i = 0; i <= bins; ++i) {
96 const double xlog = xlogmin + i*dlogx;
97 xbins[i] = exp( log(10) * xlog );
98 }
99 hist = std::make_unique<TProfile>(hisSvcName.c_str(), title.c_str(), bins, xbins.get());
100 } else {
101 throw std::runtime_error("CounterBase::regTProfile: Unknown logarithm flag");
102 }
103
104 m_variables.emplace(std::piecewise_construct,
105 std::forward_as_tuple(name),
106 std::forward_as_tuple(name, bookGetPointer(hist.release()), type)
107 );
108 // Histogram is now owned by THistSvc. A cache of the ptr is kept in the Variable
109}
110
111void CounterBase::regHistogram(const std::string& name,
112 const std::string& title,
113 const VariableType type,
114 const LogType xaxis,
115 const float xmin,
116 const float xmax,
117 const size_t xbins,
118 const LogType yaxis,
119 const float ymin,
120 const float ymax,
121 const size_t ybins)
122{
123 std::string hisSvcName = getParent()->getParent()->getName() + "_" + getParent()->getName() + "_" + getName() + "_" + name;
124 std::unique_ptr<TH2F> hist;
125
126 if (xmax <= xmin || ymax <= ymin || xbins == 0 || ybins == 0) {
127 throw std::runtime_error("CounterBase::regHistogram: Cannot have max <= min or bins == 0");
128 }
129
130 if (xaxis == kLinear && yaxis == kLinear) {
131 hist = std::make_unique<TH2F>(hisSvcName.c_str(), title.c_str(), xbins, xmin, xmax, ybins, ymin, ymax);
132 } else if (xaxis == kLog || yaxis == kLog) {
133 if ( (xaxis == kLog && xmin <= 0) || (yaxis == kLog && ymin <= 0) ) {
134 throw std::runtime_error("CounterBase::regHistogram: Cannot have min <= 0 with log binning");
135 }
136 std::unique_ptr<double[]> xlogbins = std::make_unique<double[]>(xbins+1);
137 std::unique_ptr<double[]> ylogbins = std::make_unique<double[]>(ybins+1);
138 if (xaxis == kLog){
139 const double xlogmin = log10(xmin);
140 const double xlogmax = log10(xmax);
141 const double dlogx = (xlogmax-xlogmin)/((double)xbins);
142 for (size_t i = 0; i <= xbins; ++i) {
143 const double xlog = xlogmin + i*dlogx;
144 xlogbins[i] = exp( log(10) * xlog );
145 }
146 } else {
147 const double dx = (xmax-xmin)/((double)xbins);
148 for (size_t i = 0; i <= xbins; ++i) {
149 xlogbins[i] = xmin + i*dx;
150 }
151 }
152
153 if (yaxis == kLog){
154 const double ylogmin = log10(ymin);
155 const double ylogmax = log10(ymax);
156 const double dlogy = (ylogmax-ylogmin)/((double)ybins);
157 for (size_t i = 0; i <= ybins; ++i) {
158 const double ylog = ylogmin + i*dlogy;
159 ylogbins[i] = exp( log(10) * ylog );
160 }
161 } else {
162 const double dy = (ymax-ymin)/((double)ybins);
163 for (size_t i = 0; i <= ybins; ++i) {
164 ylogbins[i] = ymin + i*dy;
165 }
166 }
167
168 hist = std::make_unique<TH2F>(hisSvcName.c_str(), title.c_str(), xbins, xlogbins.get(), ybins, ylogbins.get());
169 } else {
170 throw std::runtime_error("CounterBase::regHistogram: Unknown logarithm flag");
171 }
172
173 m_variables.emplace(std::piecewise_construct,
174 std::forward_as_tuple(name),
175 std::forward_as_tuple(name, bookGetPointer(hist.release()), type)
176 );
177 // Histogram is now owned by THistSvc. A cache of the ptr is kept in the Variable
178}
179
180bool CounterBase::variableExists(const std::string& name) const {
181 return m_variables.contains(name);
182}
183
184Variable& CounterBase::getVariable(std::string_view name) {
185 auto it = m_variables.find(name);
186 if (it == m_variables.end()) {
187 throw std::runtime_error( std::format("CounterBase::getVariable: No variable with name {}", name) );
188 }
189 return (it->second);
190}
191
192
193StatusCode CounterBase::fill(std::string_view name, float value, float weight) {
194 auto it = m_variables.find(name);
195 if (it == m_variables.end()) {
196 return StatusCode::FAILURE;
197 }
198 ATH_CHECK( it->second.fill(value, weight) );
199 return StatusCode::SUCCESS;
200}
201
202StatusCode CounterBase::fill(std::string_view name, float xvalue, float yvalue, float weight) {
203 auto it = m_variables.find(name);
204 if (it == m_variables.end()) {
205 return StatusCode::FAILURE;
206 }
207 ATH_CHECK( it->second.fill(xvalue, yvalue, weight) );
208 return StatusCode::SUCCESS;
209}
210
211
212StatusCode CounterBase::setDenominator(const std::string& name, float value) {
213 auto it = m_variables.find(name);
214 if (it == m_variables.end()) {
215 return StatusCode::FAILURE;
216 }
217 it->second.setDenominator(value);
218 return StatusCode::SUCCESS;
219}
220
221
222StatusCode CounterBase::increment(std::string_view name, float weight) {
223 return fill(name, 1.0, weight);
224}
225
226
227StatusCode CounterBase::endEvent(float) {
228 for (auto& nameVariablePair : m_variables) {
229 ATH_CHECK( nameVariablePair.second.endEvent() );
230 }
231 return StatusCode::SUCCESS;
232}
233
234
235TH1* CounterBase::bookGetPointer(TH1* hist, const std::string& tDir) const {
236 std::string dir = getName();
237 if (tDir != "") {
238 dir += "/";
239 dir += tDir;
240 }
241 return getParent()->bookGetPointer(hist, dir);
242}
243
244
245float CounterBase::timeToMilliSec(const uint64_t start, const uint64_t stop) const {
246 if (stop < start) {
247 throw std::runtime_error("Asked for a stop time " + std::to_string(stop) + " which is before the start time " + std::to_string(start));
248 }
249 const uint64_t difference = stop - start;
250 return (difference * 1e-3); // micro to ms
251}
#define ATH_CHECK
Evaluate an expression and check for errors.
static const std::vector< std::string > bins
VariableType
Behaviour of Variable.
Definition Variable.h:19
LogType
Histogram x-axis type flag.
Definition Variable.h:28
@ kLinear
Linear x-binning.
Definition Variable.h:29
@ kLog
Logarithmic x-binning.
Definition Variable.h:30
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
float timeToMilliSec(const uint64_t start, const uint64_t stop) const
Helper function.
CounterBase()=delete
Forbid default constructor.
TH1 * bookGetPointer(TH1 *hist, const std::string &tDir="") const
Appends Counter name (to histogram path) and forwards histogram book request to parent Monitor.
StatusCode increment(std::string_view name, float weight=1.0)
Convenience function.
const MonitorBase * getParent() const
Return cached non-owning const ptr to this Counter's parent Monitor.
void regTProfile(const std::string &name, const std::string &title, const VariableType type=VariableType::kPerCall, const LogType xaxis=kLog, const float min=0.1, const float max=1000000., const size_t bins=70)
Book a TProfile for this Counter, to be filled in per-event monitoring.
void regHistogram(const std::string &name, const std::string &title, const VariableType type=VariableType::kPerCall, const LogType xaxis=kLog, const float min=0.1, const float max=1000000., const size_t bins=70)
Book a histogram for this Counter, to be filled in per-event monitoring.
const std::string & getName() const
Getter for Counter's name.
virtual StatusCode endEvent(float weight=1.0)
Called by the framework.
bool variableExists(const std::string &name) const
Check if a variable of a given name exists.
const MonitorBase * m_parent
Counter's parent Monitor.
Variable & getVariable(std::string_view name)
Returns a mutable reference to a named Variable.
StatusCode fill(std::string_view name, float value, float weight=1.0)
Fill (for per-Call) or accumulate in a buffer (for per-Event) a quantity histogrammed by a named Vari...
std::unordered_map< std::string, Variable, CxxUtils::TransparentStringHash, std::equal_to<> > m_variables
Store of Counter's Variables.
const std::string m_name
Counter's name.
StatusCode setDenominator(const std::string &name, float value)
Optional for per-Event Variables.
const MonitoredRange * getParent() const
Return cached non-owning const ptr to this Monitor's parent Range.
const std::string & getName() const
Getter for Monitor's name.
TH1 * bookGetPointer(TH1 *hist, const std::string &tDir="") const
Appends Monitor name (to histogram path) and forwards histogram book request to parent Range.
const std::string & getName() const
Getter for Range's name.
Wrapper around a histogram which allows for some additional filling patterns and data manipulation.
Definition Variable.h:41
double xmax
Definition listroot.cxx:61
double ymin
Definition listroot.cxx:63
double xmin
Definition listroot.cxx:60
double ymax
Definition listroot.cxx:64