ATLAS Offline Software
Loading...
Searching...
No Matches
Variable.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
6
7#include "Variable.h"
8#include "TH1F.h"
9#include "TH2F.h"
10#include "TProfile.h"
11#include <cmath>
12
13Variable::Variable(const std::string& name, TH1* cacheHistoPtr, VariableType type) :
14 m_name(name),
16 m_cacheHistoPtr(cacheHistoPtr),
17 m_calls(0),
20 m_weight(0.),
22}
23
24
25const std::string& Variable::getName() const {
26 return m_name;
27}
28
29
30size_t Variable::getCalls() const {
31 return m_calls;
32}
33
34
36 return m_xaccumulator;
37}
38
39
40void Variable::setDenominator(float value) {
41 // Stored as reciprocal so as to be able to check for /0 here
42 if (std::abs(value) < 1e-10) {
44 } else {
45 m_oneOverDenominator = 1. / value;
46 }
47}
48
49
50StatusCode Variable::fill(float value, float weight) {
51 switch(m_variableType) {
52 case kPerCall:
53 ATH_CHECK(m_cacheHistoPtr != nullptr);
54 m_cacheHistoPtr->Fill(value * m_oneOverDenominator, weight);
55 break;
56 case kPerEvent:
57 ++m_calls;
58 m_xaccumulator += value;
59 m_weight = weight;
60 break;
61 default:
62 return StatusCode::FAILURE;
63 break;
64 }
65 return StatusCode::SUCCESS;
66}
67
68
69StatusCode Variable::fill(float xvalue, float yvalue, float weight) {
70 switch(m_variableType) {
71 case kPerCall:
72 {
73 ATH_CHECK(m_cacheHistoPtr != nullptr);
74 TH2F* th2f = dynamic_cast<TH2F*>(m_cacheHistoPtr);
75 if (th2f == nullptr){
76 // Cast failed - should be TProfile
77 auto p = dynamic_cast<TProfile*>(m_cacheHistoPtr);
78 if (p){
79 p->Fill(xvalue * m_oneOverDenominator, yvalue * m_oneOverDenominator, weight);
80 } else {
81 return StatusCode::FAILURE;
82 }
83 } else {
84 th2f->Fill(xvalue * m_oneOverDenominator, yvalue * m_oneOverDenominator, weight);
85 }
86 break;
87 }
88 case kPerEvent:
89 ++m_calls;
90 m_xaccumulator += xvalue;
91 m_yaccumulator += yvalue;
92 m_weight = weight;
93 break;
94 default:
95 return StatusCode::FAILURE;
96 break;
97 }
98 return StatusCode::SUCCESS;
99}
100
101
102StatusCode Variable::fill(const std::string& label, float weight) {
103 m_cacheHistoPtr->Fill(label.c_str(), weight);
104 return StatusCode::SUCCESS;
105}
106
107
108StatusCode Variable::increment(float weight) {
109 ATH_CHECK(fill(1.0, weight));
110 return StatusCode::SUCCESS;
111}
112
113
114StatusCode Variable::setBinLabel(int bin, const std::string& label) {
115 m_cacheHistoPtr->GetXaxis()->SetBinLabel(bin, label.c_str());
116 return StatusCode::SUCCESS;
117}
118
119
120StatusCode Variable::setYBinLabel(int bin, const std::string& label) {
121 m_cacheHistoPtr->GetYaxis()->SetBinLabel(bin, label.c_str());
122 return StatusCode::SUCCESS;
123}
124
125
126StatusCode Variable::endEvent() {
127 if (m_variableType == kPerEvent && m_calls > 0) {
128 ATH_CHECK(m_cacheHistoPtr != nullptr);
129 // 2D histogram
130 if (m_yaccumulator > 0){
131 TH2F* th2f = dynamic_cast<TH2F*>(m_cacheHistoPtr);
132 if (th2f == nullptr){
133 // Cast failed - should be TProfile
134 auto p = dynamic_cast<TProfile*>(m_cacheHistoPtr);
135 if (p){
137 } else {
138 return StatusCode::FAILURE;
139 }
140 } else {
142 }
143 } else {
145 }
146
147 }
148 m_calls = 0;
149 m_xaccumulator = 0.0;
150 m_yaccumulator = 0.0;
151 m_weight = 0.0;
153 return StatusCode::SUCCESS;
154}
#define ATH_CHECK
Evaluate an expression and check for errors.
VariableType
Behaviour of Variable.
Definition Variable.h:19
@ kPerEvent
Variable should buffer fill calls in an accumulator and fill the underlying histogram once at the end...
Definition Variable.h:21
@ kPerCall
Variable should fill underlying histogram on each fill.
Definition Variable.h:20
float m_xaccumulator
Definition Variable.h:148
float m_weight
Cache of the event weight.
Definition Variable.h:150
StatusCode endEvent()
Called by the framework.
Definition Variable.cxx:126
Variable()=delete
Forbid default constructor.
StatusCode increment(float weight=1.0)
Convenience function.
Definition Variable.cxx:108
StatusCode fill(float value, float weight=1.0)
Fill histogram (per-Call Variable), or add value to internal accumulator (per-Event Variable) to be f...
Definition Variable.cxx:50
void setDenominator(float value)
Sets, until the end of the event, a denominator which will be used to normalise every Fill.
Definition Variable.cxx:40
StatusCode setYBinLabel(int bin, const std::string &label)
Set label on given bin in cached histogram on y axis.
Definition Variable.cxx:120
const std::string & getName() const
Getter for Variable's name.
Definition Variable.cxx:25
size_t getCalls() const
Getter for how many times fill() has already been called on this Variable in this event.
Definition Variable.cxx:30
const VariableType m_variableType
Definition Variable.h:145
float m_yaccumulator
Definition Variable.h:149
float getAccumulator() const
Getter for accumulated value of a kPerEvent Variable.
Definition Variable.cxx:35
StatusCode setBinLabel(int bin, const std::string &label)
Set label on given bin in cached histogram.
Definition Variable.cxx:114
size_t m_calls
Definition Variable.h:147
const std::string m_name
Definition Variable.h:144
TH1 * m_cacheHistoPtr
Definition Variable.h:146
float m_oneOverDenominator
Cache of the reciprocal of the denominator used to normalise when filling the histogram.
Definition Variable.h:151
std::string label(const std::string &format, int i)
Definition label.h:19