ATLAS Offline Software
Loading...
Searching...
No Matches
Variable Class Reference

Wrapper around a histogram which allows for some additional filling patterns and data manipulation. More...

#include <Variable.h>

Collaboration diagram for Variable:

Public Member Functions

 Variable ()=delete
 Forbid default constructor.
 Variable (const std::string &name, TH1 *cacheHistoPtr, VariableType type=kPerCall)
 Construct Variable.
 ~Variable ()=default
 Default destructor.
Variableoperator= (const Variable &)=delete
 Forbid copy.
 Variable (const Variable &)=delete
 Forbid copy.
const std::string & getName () const
 Getter for Variable's name.
size_t getCalls () const
 Getter for how many times fill() has already been called on this Variable in this event.
float getAccumulator () const
 Getter for accumulated value of a kPerEvent Variable.
StatusCode fill (float value, float weight=1.0)
 Fill histogram (per-Call Variable), or add value to internal accumulator (per-Event Variable) to be filled at the end of the event.
StatusCode fill (float xvalue, float yvalue, float weight)
 Fill histogram (per-Call Variable), or add value to internal accumulator (per-Event Variable) to be filled at the end of the event.
StatusCode fill (const std::string &label, float weight=1.0)
 Fill histogram's bin (per-Call Variable) with given label.
StatusCode increment (float weight=1.0)
 Convenience function.
StatusCode setBinLabel (int bin, const std::string &label)
 Set label on given bin in cached histogram.
StatusCode setYBinLabel (int bin, const std::string &label)
 Set label on given bin in cached histogram on y axis.
void setDenominator (float value)
 Sets, until the end of the event, a denominator which will be used to normalise every Fill.
StatusCode endEvent ()
 Called by the framework.

Private Attributes

const std::string m_name
const VariableType m_variableType
TH1 * m_cacheHistoPtr
size_t m_calls
float m_xaccumulator
float m_yaccumulator
float m_weight
 Cache of the event weight.
float m_oneOverDenominator
 Cache of the reciprocal of the denominator used to normalise when filling the histogram.

Detailed Description

Wrapper around a histogram which allows for some additional filling patterns and data manipulation.

Behaviour based on type parameter. per-Call directly fills underlying histogram for each call of fill. per-Event fills the underlying histogram exactly once in events where fill was called one or more times. The filled value is accumulated from all calls to fill during the event in per-Event mode.

Definition at line 39 of file Trigger/TrigCost/TrigCostAnalysis/src/Variable.h.

Constructor & Destructor Documentation

◆ Variable() [1/3]

Variable::Variable ( )
delete

Forbid default constructor.

◆ Variable() [2/3]

Variable::Variable ( const std::string & name,
TH1 * cacheHistoPtr,
VariableType type = kPerCall )

Construct Variable.

Parameters
[in]nameVariable's name
[in]cacheHistoPtrCached non-owning pointer to the histogram which this Variable wraps.
[in]typeIf the Variable should operate in per-Call mode (direct fill) or per-Event mode (accumulate and fill at the end of the event).

Definition at line 12 of file Variable.cxx.

12 :
13 m_name(name),
14 m_variableType(type),
15 m_cacheHistoPtr(cacheHistoPtr),
16 m_calls(0),
19 m_weight(0.),
21}
float m_weight
Cache of the event weight.
float m_oneOverDenominator
Cache of the reciprocal of the denominator used to normalise when filling the histogram.

◆ ~Variable()

Variable::~Variable ( )
default

Default destructor.

◆ Variable() [3/3]

Variable::Variable ( const Variable & )
delete

Forbid copy.

Member Function Documentation

◆ endEvent()

StatusCode Variable::endEvent ( )

Called by the framework.

Triggers actual histogram fill for a per-Event variable. Resets internals.

Definition at line 120 of file Variable.cxx.

120 {
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.
@ kPerEvent
Variable should buffer fill calls in an accumulator and fill the underlying histogram once at the end...
TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)

◆ fill() [1/3]

StatusCode Variable::fill ( const std::string & label,
float weight = 1.0 )

Fill histogram's bin (per-Call Variable) with given label.

Parameters
[in]labelLabel of bin to fill
[in]weightGlobal event weight

Definition at line 96 of file Variable.cxx.

96 {
97 m_cacheHistoPtr->Fill(label.c_str(), weight);
98 return StatusCode::SUCCESS;
99}
std::string label(const std::string &format, int i)
Definition label.h:19

◆ fill() [2/3]

StatusCode Variable::fill ( float value,
float weight = 1.0 )

Fill histogram (per-Call Variable), or add value to internal accumulator (per-Event Variable) to be filled at the end of the event.

Parameters
[in]valueThe payload.
[in]weightGlobal event weight

Definition at line 49 of file Variable.cxx.

49 {
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;
59 break;
60 default:
61 return StatusCode::FAILURE;
62 break;
63 }
64 return StatusCode::SUCCESS;
65}
@ kPerCall
Variable should fill underlying histogram on each fill.

◆ fill() [3/3]

StatusCode Variable::fill ( float xvalue,
float yvalue,
float weight )

Fill histogram (per-Call Variable), or add value to internal accumulator (per-Event Variable) to be filled at the end of the event.

Parameters
[in]xvalueThe x-axis payload.
[in]yvalueThe y-axis payload.
[in]weightGlobal event weight

Definition at line 68 of file Variable.cxx.

68 {
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;
87 break;
88 default:
89 return StatusCode::FAILURE;
90 break;
91 }
92 return StatusCode::SUCCESS;
93}

◆ getAccumulator()

float Variable::getAccumulator ( ) const

Getter for accumulated value of a kPerEvent Variable.

Returns
Total accumulated payload

Definition at line 34 of file Variable.cxx.

34 {
35 return m_xaccumulator;
36}

◆ getCalls()

size_t Variable::getCalls ( ) const

Getter for how many times fill() has already been called on this Variable in this event.

Returns
Number of fill calls this event.

Definition at line 29 of file Variable.cxx.

29 {
30 return m_calls;
31}

◆ getName()

const std::string & Variable::getName ( ) const

Getter for Variable's name.

Returns
Variable's name const string reference.

Definition at line 24 of file Variable.cxx.

24 {
25 return m_name;
26}

◆ increment()

StatusCode Variable::increment ( float weight = 1.0)

Convenience function.

Equivalent of fill(1.0, weight). For use with per-Event counting type variables.

Parameters
[in]weightGlobal event weight

Definition at line 102 of file Variable.cxx.

102 {
103 ATH_CHECK(fill(1.0, weight));
104 return StatusCode::SUCCESS;
105}
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

◆ operator=()

Variable & Variable::operator= ( const Variable & )
delete

Forbid copy.

◆ setBinLabel()

StatusCode Variable::setBinLabel ( int bin,
const std::string & label )

Set label on given bin in cached histogram.

Parameters
[in]binBin number
[in]labelLabel to set

Definition at line 108 of file Variable.cxx.

108 {
109 m_cacheHistoPtr->GetXaxis()->SetBinLabel(bin, label.c_str());
110 return StatusCode::SUCCESS;
111}

◆ setDenominator()

void Variable::setDenominator ( float value)

Sets, until the end of the event, a denominator which will be used to normalise every Fill.

@pram[in] value The denominator to normalise Fill operations.

Definition at line 39 of file Variable.cxx.

39 {
40 // Stored as reciprocal so as to be able to check for /0 here
41 if (std::abs(value) < 1e-10) {
43 } else {
45 }
46}

◆ setYBinLabel()

StatusCode Variable::setYBinLabel ( int bin,
const std::string & label )

Set label on given bin in cached histogram on y axis.

Parameters
[in]binBin number
[in]labelLabel to set

Definition at line 114 of file Variable.cxx.

114 {
115 m_cacheHistoPtr->GetYaxis()->SetBinLabel(bin, label.c_str());
116 return StatusCode::SUCCESS;
117}

Member Data Documentation

◆ m_cacheHistoPtr

TH1* Variable::m_cacheHistoPtr
private

◆ m_calls

size_t Variable::m_calls
private

◆ m_name

const std::string Variable::m_name
private

◆ m_oneOverDenominator

float Variable::m_oneOverDenominator
private

Cache of the reciprocal of the denominator used to normalise when filling the histogram.

Definition at line 149 of file Trigger/TrigCost/TrigCostAnalysis/src/Variable.h.

◆ m_variableType

const VariableType Variable::m_variableType
private

◆ m_weight

float Variable::m_weight
private

Cache of the event weight.

Assumed to be the same for every call to fill with per-Event monitoring!

Definition at line 148 of file Trigger/TrigCost/TrigCostAnalysis/src/Variable.h.

◆ m_xaccumulator

float Variable::m_xaccumulator
private

◆ m_yaccumulator

float Variable::m_yaccumulator
private

The documentation for this class was generated from the following files: