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