ATLAS Offline Software
InDetPlotBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
10 #include "InDetPlotBase.h"
11 // bring Athena/Gaudi utilities in scope
12 #include "GaudiKernel/Bootstrap.h"
13 #include "GaudiKernel/ISvcLocator.h"
14 #include "GaudiKernel/Service.h"
15 #include "GaudiKernel/IToolSvc.h"
16 // #include <iostream>
17 #include "TEfficiency.h"
18 // to retrieve HistogramDefinitionSvc
20 #include <cmath>
21 
22 namespace {
23  bool
24  validArguments(const float arg) {
25  return not (std::isnan(arg));
26  }
27 
28  bool
29  validArguments(const float arg, const float arg2) {
30  return not (std::isnan(arg) or std::isnan(arg2));
31  }
32 
33  bool
34  validArguments(const float arg, const float arg2, const float arg3) {
35  return not (std::isnan(arg) or std::isnan(arg2) or std::isnan(arg3));
36  }
37 }
38 
39 
40 InDetPlotBase::InDetPlotBase(InDetPlotBase* pParent, const std::string& dirName) :
41  PlotBase(pParent, dirName), AthMessaging("InDetPlotBase"), m_histoDefSvc(nullptr) {
42  // nop
43 }
44 
45 void
47  if (hd.isValid()) {
48  pHisto = Book1D(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, false);
49  }
50  }
51 void
53  if (hd.isValid()) {
54  pHisto = BookTProfile(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, hd.yAxis.first,
55  hd.yAxis.second, false);
56  }
57  }
58 void
60  if (hd.isValid()) {
61  pHisto = BookTProfile2D(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, hd.nBinsY, hd.yAxis.first,
62  hd.yAxis.second, false);
63  }
64  }
65 void
67  if (hd.isValid()) {
68  pHisto = Book2D(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, hd.nBinsY, hd.yAxis.first,
69  hd.yAxis.second, false);
70  }
71  }
72 
73 void
74 InDetPlotBase::book(TEfficiency*& pHisto, const SingleHistogramDefinition& hd) {
75  if (hd.isValid()) {
76  if(hd.nBinsY==0) {
77  pHisto = BookTEfficiency(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, false);
78  } else {
79  pHisto = BookTEfficiency(hd.name, hd.allTitles, hd.nBinsX, hd.xAxis.first, hd.xAxis.second, hd.nBinsY, hd.yAxis.first, hd.yAxis.second, false);
80  }
81  }
82  }
83 
84 void
85 InDetPlotBase::fillHisto(TProfile* pTprofile, const float bin, const float weight, const float weight2) {
86  if (pTprofile and validArguments(bin, weight)) {
87  pTprofile->Fill(bin, weight,weight2);
88  }
89 }
90 
91 void
92 InDetPlotBase::fillHisto(TProfile2D* pTprofile, const float xval, const float yval, const float weight, const float weight2) {
93  if (pTprofile and validArguments(xval,yval, weight) and validArguments(weight2)) {
94  pTprofile->Fill(xval,yval, weight,weight2);
95  }
96 }
97 
98 //
99 void
100 InDetPlotBase::fillHisto(TH1* pTh1, const float value) {
101  if (pTh1 and validArguments(value)) {
102  pTh1->Fill(value);
103  }
104 }
105 
106 void
107 InDetPlotBase::fillHisto(TH1* pTh1, const float value, const float weight) {
108  if (pTh1 and validArguments(value)) {
109  pTh1->Fill(value, weight);
110  }
111 }
112 
113 //
114 void
115 InDetPlotBase::fillHisto(TH2* pTh2, const float xval, const float yval) {
116  if (pTh2 and validArguments(xval, yval)) {
117  pTh2->Fill(xval, yval);
118  }
119 }
120 
121 //
122 void
123 InDetPlotBase::fillHisto(TH2* pTh2, const float xval, const float yval, const float weight) {
124  if (pTh2 and validArguments(xval, yval)) {
125  pTh2->Fill(xval, yval, weight);
126  }
127 }
128 
129 void
130 InDetPlotBase::fillHisto(TH3* pTh3, const float xval, const float yval, const float zval) {
131  if (pTh3 and validArguments(xval, yval, zval)) {
132  pTh3->Fill(xval, yval, zval);
133  }
134 }
135 
136 void
137 InDetPlotBase::fillHisto(TEfficiency* pTeff, const float value, const bool accepted, float weight) {
138  if (pTeff and validArguments(value)) {
139  if(weight==1.) pTeff->Fill(accepted, value); // To get proper error estimate when possible
140  else pTeff->FillWeighted(accepted, weight, value);
141  }
142 }
143 
144 void
145 InDetPlotBase::fillHisto(TEfficiency* eff2d, const float xvalue, const float yvalue, const bool accepted, const float weight) {
146  if (eff2d and validArguments(xvalue, yvalue)) {
147  if(weight==1.) eff2d->Fill(accepted, xvalue, yvalue);
148  else eff2d->FillWeighted(accepted, weight, xvalue, yvalue);
149  }
150 }
151 
152 
154 InDetPlotBase::retrieveDefinition(const std::string& histoIdentifier, const std::string& folder, const std::string & nameOverride) {
155  SingleHistogramDefinition s; // invalid result
156 
157  if (not m_histoDefSvc) {
158  ISvcLocator* svcLoc = Gaudi::svcLocator();
159  StatusCode sc = svcLoc->service("HistogramDefinitionSvc", m_histoDefSvc);
160  if (sc.isFailure()) {
161  ATH_MSG_FATAL("failed to retrieve HistogramDefinitionSvc in " << __FILE__);
162  throw std::runtime_error("Could initialise the HistogramDefinitionSvc");
163  return s;
164  }
165  }
166  bool folderDefault = (folder.empty() or folder == "default");
167  s = m_histoDefSvc->definition(histoIdentifier, folder);
168  // "default" and empty string should be equivalent
169  if (folderDefault and s.empty()) {
170  const std::string otherDefault = (folder.empty()) ? ("default") : "";
171  s = m_histoDefSvc->definition(histoIdentifier, otherDefault);
172  }
173  if (s.empty()) {
174  ATH_MSG_WARNING("Histogram definition is empty for identifier " << histoIdentifier);
175  }
176  if (!nameOverride.empty()){
177  s.name = nameOverride;
178  }
179  return s;
180 }
SingleHistogramDefinition::nBinsX
unsigned int nBinsX
Definition: SingleHistogramDefinition.h:47
TH2::Fill
int Fill(double, double)
Definition: rootspy.cxx:382
PlotBase::Book1D
TH1F * Book1D(const std::string &name, const std::string &labels, int nBins, float start, float end, bool prependDir=true)
Book a TH1D histogram.
Definition: PlotBase.cxx:88
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
PlotBase
Definition: PlotBase.h:33
InDetPlotBase::book
void book(Htype *&pHisto, const std::string &histoIdentifier, const std::string &nameOverride="", const std::string &folder="default")
Helper method to book histograms using an identifier string.
HistogramDefinitionSvc.h
PlotBase::BookTEfficiency
TEfficiency * BookTEfficiency(const std::string &name, const std::string &labels, const int nBinsX, const float xlo, const float xhi, const bool prependDir=true)
Book a (1-D) TEfficiency histogram.
Definition: PlotBase.cxx:251
TProfile2D
Definition: rootspy.cxx:531
bin
Definition: BinsDiffFromStripMedian.h:43
PlotBase::Book2D
TH2F * Book2D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY, float startY, float endY, bool prependDir=true)
Book a TH2D histogram.
Definition: PlotBase.cxx:117
athena.value
value
Definition: athena.py:122
PlotBase::BookTProfile2D
TProfile2D * BookTProfile2D(const std::string &name, const std::string &labels, const int nBinsX, const double xlo, const double xhi, const int nBinsY, const double ylo, const double yhi, bool prependDir=true, bool useRMS=false)
Book a TProfile 2D histogram with variable binning in x-axis and limits in y-values.
Definition: PlotBase.cxx:225
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
InDetPlotBase
Mixin class to give extra capabilities to plots such as ATH_MSG and an easier booking interface,...
Definition: InDetPlotBase.h:31
SingleHistogramDefinition::xAxis
IHistogramDefinitionSvc::axesLimits_t xAxis
Definition: SingleHistogramDefinition.h:50
InDetPlotBase.h
TH3::Fill
int Fill(double, double, double)
Definition: rootspy.cxx:453
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TH3
Definition: rootspy.cxx:440
plotting.yearwise_efficiency.yval
float yval
Definition: yearwise_efficiency.py:43
TProfile2D::Fill
int Fill(double, double, double)
Definition: rootspy.cxx:541
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
IHistogramDefinitionSvc::definition
virtual SingleHistogramDefinition definition(const std::string &name, const std::string &dirName="") const =0
Return a histogram definition, retrieved by histogram identifier (and directory name,...
TH1::Fill
int Fill(double)
Definition: rootspy.cxx:285
plotting.yearwise_efficiency.xval
float xval
Definition: yearwise_efficiency.py:42
TH2
Definition: rootspy.cxx:373
InDetPlotBase::m_histoDefSvc
IHistogramDefinitionSvc * m_histoDefSvc
Definition: InDetPlotBase.h:86
SingleHistogramDefinition
Almost-a-struct for holding the single histogram definition.
Definition: SingleHistogramDefinition.h:17
create_dcsc_inputs_sqlite.arg
list arg
Definition: create_dcsc_inputs_sqlite.py:48
TProfile
Definition: rootspy.cxx:515
InDetPlotBase::InDetPlotBase
InDetPlotBase(InDetPlotBase *pParent, const std::string &dirName)
Constructor taking parent node and directory name for plots.
Definition: InDetPlotBase.cxx:40
TProfile::Fill
int Fill(double, double)
Definition: rootspy.cxx:523
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TH1
Definition: rootspy.cxx:268
PlotBase::BookTProfile
TProfile * BookTProfile(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, float startY=-1, float endY=-1, bool prependDir=true, bool useRMS=false)
Book a TProfile histogram.
Definition: PlotBase.cxx:180
SingleHistogramDefinition::name
std::string name
Definition: SingleHistogramDefinition.h:44
CaloCondBlobAlgs_fillNoiseFromASCII.folder
folder
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:56
SingleHistogramDefinition::yAxis
IHistogramDefinitionSvc::axesLimits_t yAxis
Definition: SingleHistogramDefinition.h:51
SingleHistogramDefinition::isValid
bool isValid() const
Is the histogram definition valid.
Definition: SingleHistogramDefinition.cxx:104
SingleHistogramDefinition::allTitles
std::string allTitles
Definition: SingleHistogramDefinition.h:56
InDetPlotBase::fillHisto
static void fillHisto(TProfile *pTprofile, const float bin, const float weight, const float weight2=1.0)
Definition: InDetPlotBase.cxx:85
InDetPlotBase::retrieveDefinition
SingleHistogramDefinition retrieveDefinition(const std::string &histoIdentifier, const std::string &folder="default", const std::string &nameOverride="")
Retrieve a single histogram definition, given the unique string identifier.
Definition: InDetPlotBase.cxx:154
SingleHistogramDefinition::nBinsY
unsigned int nBinsY
Definition: SingleHistogramDefinition.h:48