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