ATLAS Offline Software
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 
12 Variable::Variable(const std::string& name, TH1* cacheHistoPtr, VariableType type) :
13  m_name(name),
14  m_variableType(type),
15  m_cacheHistoPtr(cacheHistoPtr),
16  m_calls(0),
17  m_xaccumulator(0.),
18  m_yaccumulator(0.),
19  m_weight(0.),
20  m_oneOverDenominator(1.) {
21 }
22 
23 
24 const std::string& Variable::getName() const {
25  return m_name;
26 }
27 
28 
29 size_t Variable::getCalls() const {
30  return m_calls;
31 }
32 
33 
34 float Variable::getAccumulator() const {
35  return m_xaccumulator;
36 }
37 
38 
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 }
47 
48 
50  switch(m_variableType) {
51  case kPerCall:
52  ATH_CHECK(m_cacheHistoPtr != nullptr);
54  break;
55  case kPerEvent:
56  ++m_calls;
58  m_weight = weight;
59  break;
60  default:
61  return StatusCode::FAILURE;
62  break;
63  }
64  return StatusCode::SUCCESS;
65 }
66 
67 
68 StatusCode 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 
96 StatusCode Variable::fill(const std::string& label, float weight) {
97  m_cacheHistoPtr->Fill(label.c_str(), weight);
98  return StatusCode::SUCCESS;
99 }
100 
101 
103  ATH_CHECK(fill(1.0, weight));
104  return StatusCode::SUCCESS;
105 }
106 
107 
108 StatusCode Variable::setBinLabel(int bin, const std::string& label) {
109  m_cacheHistoPtr->GetXaxis()->SetBinLabel(bin, label.c_str());
110  return StatusCode::SUCCESS;
111 }
112 
113 
114 StatusCode Variable::setYBinLabel(int bin, const std::string& label) {
115  m_cacheHistoPtr->GetYaxis()->SetBinLabel(bin, label.c_str());
116  return StatusCode::SUCCESS;
117 }
118 
119 
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;
141  m_oneOverDenominator = 1.0;
142  return StatusCode::SUCCESS;
143 }
Variable::m_calls
size_t m_calls
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:145
Variable::m_weight
float m_weight
Cache of the event weight.
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:148
TH2F
Definition: rootspy.cxx:420
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
kPerEvent
@ kPerEvent
Variable should buffer fill calls in an accumulator and fill the underlying histogram once at the end...
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:19
bin
Definition: BinsDiffFromStripMedian.h:43
athena.value
value
Definition: athena.py:122
Variable::getCalls
size_t getCalls() const
Getter for how many times fill() has already been called on this Variable in this event.
Definition: Variable.cxx:29
Variable::endEvent
StatusCode endEvent()
Called by the framework.
Definition: Variable.cxx:120
Variable::m_cacheHistoPtr
TH1 * m_cacheHistoPtr
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:144
VariableType
VariableType
Behaviour of Variable.
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:17
Variable::m_yaccumulator
float m_yaccumulator
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:147
Variable::m_oneOverDenominator
float m_oneOverDenominator
Cache of the reciprocal of the denominator used to normalise when filling the histogram.
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:149
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
Variable::m_variableType
const VariableType m_variableType
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:143
Variable.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Variable::m_xaccumulator
float m_xaccumulator
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:146
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TH1::Fill
int Fill(double)
Definition: rootspy.cxx:285
Variable::increment
StatusCode increment(float weight=1.0)
Convenience function.
Definition: Variable.cxx:102
Variable::m_name
const std::string m_name
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:142
Variable::getAccumulator
float getAccumulator() const
Getter for accumulated value of a kPerEvent Variable.
Definition: Variable.cxx:34
Variable::setBinLabel
StatusCode setBinLabel(int bin, const std::string &label)
Set label on given bin in cached histogram.
Definition: Variable.cxx:108
kPerCall
@ kPerCall
Variable should fill underlying histogram on each fill.
Definition: Trigger/TrigCost/TrigCostAnalysis/src/Variable.h:18
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TProfile
Definition: rootspy.cxx:515
Variable::setYBinLabel
StatusCode setYBinLabel(int bin, const std::string &label)
Set label on given bin in cached histogram on y axis.
Definition: Variable.cxx:114
Variable::Variable
Variable()=delete
Forbid default constructor.
Variable::getName
const std::string & getName() const
Getter for Variable's name.
Definition: Variable.cxx:24
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
TH1
Definition: rootspy.cxx:268
Variable::fill
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
Variable::setDenominator
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