ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaMonitoringKernel
src
HistogramFiller
HistogramFillerUtils.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#ifndef AthenaMonitoringKernel_HistogramFillerUtils_h
6
#define AthenaMonitoringKernel_HistogramFillerUtils_h
7
8
#include <algorithm>
9
#include <utility>
10
11
#include "
AthenaMonitoringKernel/IMonitoredVariable.h
"
12
#include "
AthenaMonitoringKernel/OHLockedHist.h
"
13
#include "
HistogramFactory.h
"
14
15
#include "TH1.h"
16
#include "TProfile.h"
17
#include "THashList.h"
18
#include "TProfile2D.h"
19
20
namespace
Monitored
{
21
23
enum
Axis
{
X
= 0,
Y
,
Z
};
24
25
namespace
detail
{
26
27
auto
noWeight
= [](size_t){
return
1.0; };
28
auto
noCut
= [](size_t){
return
true
; };
29
31
constexpr
std::array
axis_name
{
"X"
,
"Y"
,
"Z"
};
32
constexpr
std::array
axis_bit
{TH1::kXaxis, TH1::kYaxis, TH1::kZaxis};
33
38
template
<Axis AXIS,
typename
H>
39
constexpr
auto
getAxis
(
H
* hist) {
40
if
constexpr
(AXIS==
Axis::X
)
return
hist->GetXaxis();
41
else
if
constexpr
(AXIS==
Axis::Y
)
return
hist->GetYaxis();
42
else
return
hist->GetZaxis();
43
}
44
54
template
<Axis AXIS,
typename
H>
55
double
getFillValue
(
const
H
* hist,
const
IMonitoredVariable
* var,
size_t
i) {
56
if
( var->hasStringRepresentation() ) {
57
const
TAxis* axis =
getAxis<AXIS>
(hist);
58
const
int
binNumber = axis->FindFixBin( var->getString(i).c_str() );
59
return
axis->GetBinCenter(binNumber);
60
}
else
{
61
return
var->get(i);
62
}
63
}
64
69
bool
shouldRebinHistogram
(
const
TAxis* axis,
const
double
value) {
70
return
axis ? axis->GetXmax() <= value :
false
;
71
}
72
87
template
<Axis AXIS,
typename
H>
88
void
rebinHistogram
(
H
* hist,
const
double
value) {
89
hist->SetCanExtend(
axis_bit
[AXIS]);
90
TAxis*
a
=
getAxis<AXIS>
(hist);
91
92
// Rebinning requires to take OH lock in online (no-op offline)
93
oh_scoped_lock_histogram
lock
;
94
// Rebinning requires a lock on the global ROOT directory state
95
std::scoped_lock<std::mutex> dirLock(
HistogramFactory::globalROOTMutex
());
96
do
{
97
// need to unset cleanup bit of parent histogram during this operation
98
// since it gets copied to a hidden temporary (probably unintentionally)
99
bool
curcleanup = hist->TestBit(TObject::kMustCleanup);
100
hist->ResetBit(TObject::kMustCleanup);
101
hist->LabelsInflate(
axis_name
[AXIS]);
102
hist->SetBit(TObject::kMustCleanup, curcleanup);
103
}
while
(
shouldRebinHistogram
(
a
, value));
104
}
105
111
template
<
typename
T>
112
bool
fillWillRebinHistogram
(
const
TAxis* axis, T value) {
113
if
(not axis)
return
false
;
114
const
int
bin
= axis->FindFixBin(value);
115
// if under/overflow
116
if
(
bin
==0 or
bin
==axis->GetNbins()+1 ) {
117
return
true
;
118
}
119
return
false
;
120
}
121
128
template
<>
129
bool
fillWillRebinHistogram
(
const
TAxis* axis,
const
char
* value) {
130
// valid bin found
131
if
( not axis or axis->FindFixBin(value)>0 )
return
false
;
132
133
// If there are no labels yet at least one unlabeled bin is available
134
const
THashList* labels = axis->GetLabels();
135
if
( not labels )
return
false
;
136
137
return
true
;
138
}
139
147
template
<
typename
H
,
typename
T, T...
a
,
typename
...Vs>
148
bool
fillWillRebinHistogram
(
H
* hist, std::integer_sequence<T, a...>,
const
Vs&... v) {
149
// First check if axis is extensible, then if value would be outside of range
150
return
(... || (
getAxis
<
static_cast<
Axis
>
(
a
)>(hist)->CanExtend() and
151
detail::fillWillRebinHistogram
(
getAxis
<
static_cast<
Axis
>
(
a
)>(hist), v)));
152
}
153
162
template
<
typename
H
,
typename
W,
typename
M,
typename
...Ms>
163
void
doFill
(
H
* hist, W weight,
size_t
i,
const
M& m1,
const
Ms&... m) {
164
165
// Template magic: Recursively convert all M to double or string
166
if
constexpr
(std::is_same_v<M, Monitored::IMonitoredVariable>) {
167
// For >=2D: If one variable has a single entry, do repeated fills with that value
168
const
size_t
j = m1.size()==1 ? 0 : i;
169
if
(not m1.hasStringRepresentation())
170
doFill
(hist, weight, i, m..., m1.get(j));
171
else
172
doFill
(hist, weight, i, m..., m1.getString(j).c_str());
173
}
else
{
174
// In case re-binning occurs need to take the OH lock for online (no-op offline)
175
if
(
fillWillRebinHistogram
(hist, std::index_sequence_for<M, Ms...>{},
176
m1, m...)) [[
unlikely
]] {
177
oh_scoped_lock_histogram
lock
;
178
// Rebinning requires a lock on the global ROOT directory state
179
std::scoped_lock<std::mutex> dirLock(
HistogramFactory::globalROOTMutex
());
180
hist->Fill(m1, m..., weight(i));
181
}
182
else
hist->Fill(m1, m..., weight(i));
183
}
184
}
185
186
// TProfile does not support string as y-value.
187
// (this terminates the above pack expansion)
188
template
<
typename
W>
189
void
doFill
(TProfile*, W,
size_t
,
const
double
&,
const
char
*
const
&) {}
190
template
<
typename
W>
191
void
doFill
(TProfile*, W,
size_t
,
const
char
*
const
&,
const
char
*
const
&) {}
192
template
<
typename
W>
193
void
doFill
(TProfile2D*, W,
size_t
,
const
double
&,
const
double
&,
const
char
*
const
&) {}
194
template
<
typename
W>
195
void
doFill
(TProfile2D*, W,
size_t
,
const
char
*
const
&,
const
char
*
const
&,
const
char
*
const
&) {}
196
template
<
typename
W>
197
void
doFill
(TProfile2D*, W,
size_t
,
const
char
*
const
&,
const
double
&,
const
char
*
const
&) {}
198
template
<
typename
W>
199
void
doFill
(TProfile2D*, W,
size_t
,
const
double
&,
const
char
*
const
&,
const
char
*
const
&) {}
200
}
201
}
202
203
#endif
HistogramFactory.h
lock
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
IMonitoredVariable.h
a
static Double_t a
Definition
LArPhysWaveHECTool.cxx:38
H
#define H(x, y, z)
Definition
MD5.cxx:114
OHLockedHist.h
OH histogram lock header file.
Monitored::HistogramFactory::globalROOTMutex
static std::mutex & globalROOTMutex()
Definition
HistogramFactory.h:68
Monitored::IMonitoredVariable
Definition
IMonitoredVariable.h:14
bin
Definition
BinsDiffFromStripMedian.h:43
oh_scoped_lock_histogram
Scoped lock to be used for threaded histogram operations.
Definition
OHLockedHist.h:108
Monitored::detail::fillWillRebinHistogram
bool fillWillRebinHistogram(const TAxis *axis, T value)
Check if Fill would result in rebinning.
Definition
HistogramFillerUtils.h:112
Monitored::detail::getAxis
constexpr auto getAxis(H *hist)
Helper to get corresponding TAxis selected by Monitored::Axis.
Definition
HistogramFillerUtils.h:39
Monitored::detail::noWeight
auto noWeight
no weight for filling
Definition
HistogramFillerUtils.h:27
Monitored::detail::doFill
void doFill(H *hist, W weight, size_t i, const M &m1, const Ms &... m)
Perform (arbitrary dimension) histogram fill with weight.
Definition
HistogramFillerUtils.h:163
Monitored::detail::axis_bit
constexpr std::array axis_bit
Definition
HistogramFillerUtils.h:32
Monitored::detail::axis_name
constexpr std::array axis_name
Convert axis to ROOT-compatible character.
Definition
HistogramFillerUtils.h:31
Monitored::detail::shouldRebinHistogram
bool shouldRebinHistogram(const TAxis *axis, const double value)
Method checks if histogram should be rebinned.
Definition
HistogramFillerUtils.h:69
Monitored::detail::noCut
auto noCut
no cut for filling
Definition
HistogramFillerUtils.h:28
Monitored::detail::getFillValue
double getFillValue(const H *hist, const IMonitoredVariable *var, size_t i)
Return value for filling i'th entry of var into AXIS for hist.
Definition
HistogramFillerUtils.h:55
Monitored::detail::rebinHistogram
void rebinHistogram(H *hist, const double value)
Method that rebins a histogram.
Definition
HistogramFillerUtils.h:88
Monitored
Generic monitoring tool for athena components.
Definition
GenericMonitoringTool.h:28
Monitored::Axis
Axis
Helper type for histogram axis selection.
Definition
HistogramFillerUtils.h:23
Monitored::X
@ X
Definition
HistogramFillerUtils.h:23
Monitored::Y
@ Y
Definition
HistogramFillerUtils.h:23
Monitored::Z
@ Z
Definition
HistogramFillerUtils.h:23
detail
Definition
extract_histogram_tag.cxx:14
unlikely
#define unlikely(x)
Definition
pythonic_coracool.cxx:9
Generated on
for ATLAS Offline Software by
1.17.0