ATLAS Offline Software
HistogramDefinitionSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //
6 // HistogramDefinitionSvc.cpp
7 // HDef
8 //
9 // Created by sroe on 07/07/2015.
10 //
11 
12 #include <memory>
13 
15 #include "ReadFromXmlDom.h"
16 #include "IReadHistoDef.h"
17 namespace {
18  const std::pair<float, float> invalidLimits = std::make_pair(
19  std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN());
20  const std::string defaultFormat("text/plain");
21  const std::string xmlFormat("text/xml");
22 }
23 
24 
25 HistogramDefinitionSvc::HistogramDefinitionSvc(const std::string& name, ISvcLocator* pSvcLocator) :
26  base_class(name, pSvcLocator), m_format{UNKNOWN}, m_reader{} {
27  declareProperty("DefinitionSource", m_source);
28  declareProperty("DefinitionFormat", m_formatString = "text/plain");
29 }
30 
32 
35  if (m_formatString.value() == defaultFormat) {
36  m_format = TEXT_PLAIN;
37  }
38  if (m_formatString.value() == xmlFormat) {
39  m_format = TEXT_XML;
40  }
41  if (m_format == UNKNOWN) {
42  ATH_MSG_ERROR("Unknown format for the input to the hdef service");
43  return StatusCode::FAILURE;
44  }
45  ATH_MSG_INFO("hdef source:" << m_source.value());
46  ATH_MSG_INFO("Set format:" << m_formatString.value());
47  ATH_MSG_INFO("format " << m_format);
48  if (m_format == TEXT_PLAIN) {
49  ATH_MSG_FATAL("Text format histogram definition files are deprecated.");
50  return StatusCode::FAILURE;
51  }
52  if (m_format == TEXT_XML) {
53  m_reader = std::make_unique<ReadFromXmlDom>(m_source.value());
54  }
55  ATH_MSG_INFO("Reader initialising from " << m_reader->source() << " in " << m_reader->format() << " format.");
56  bool ok = m_reader->initialize();
57  if (not ok) {
58  ATH_MSG_WARNING("Reader did not initialise");
59  }
60  ok = m_reader->histoDefinitionMap(m_histoDefMap);
61  bool allDefsOk(true);
62  for (auto& h:m_histoDefMap) {
63  if (not h.second.isValid()) {
64  ATH_MSG_WARNING("Invalid histogram definition: " << h.second.str());
65  allDefsOk = false;
66  }
67  }
68  if (ok and(not allDefsOk)) {
69  ATH_MSG_WARNING("Some histogram definitions were bad.");
70  return StatusCode::RECOVERABLE;
71  }
72  if (not ok) {
73  ATH_MSG_ERROR("The definition reader failed to read the histogram definitions.");
74  return StatusCode::FAILURE;
75  }
76  return StatusCode::SUCCESS;
77 }
78 
81  ATH_MSG_INFO("Reader initialised from " << m_reader->source() << " in " << m_reader->format() << " format.");
82  return StatusCode::SUCCESS;
83 }
84 
86 HistogramDefinitionSvc::definition(const std::string& name, const std::string& dirName) const {
88  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
89 
90  if (pthisHistoPair != m_histoDefMap.end()) {
91  result = pthisHistoPair->second;
92  }
93  return result;
94 }
95 
96 std::string
97 HistogramDefinitionSvc::histoType(const std::string& name, const std::string& dirName) const {
98  std::string result {};
99  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
100 
101  if (pthisHistoPair != m_histoDefMap.end()) {
102  result = pthisHistoPair->second.histoType;
103  }
104  return result;
105 }
106 
107 std::string
108 HistogramDefinitionSvc::title(const std::string& name, const std::string& dirName) const {
109  std::string result {};
110  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
111 
112  if (pthisHistoPair != m_histoDefMap.end()) {
113  result = pthisHistoPair->second.title;
114  }
115  return result;
116 }
117 
118 unsigned int
119 HistogramDefinitionSvc::nBinsX(const std::string& name, const std::string& dirName) const {
120  unsigned int nbins(0);
121  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
122 
123  if (pthisHistoPair != m_histoDefMap.end()) {
124  nbins = pthisHistoPair->second.nBinsX;
125  }
126  return nbins;
127 }
128 
129 unsigned int
130 HistogramDefinitionSvc::nBinsY(const std::string& name, const std::string& dirName) const {
131  unsigned int nbins(0);
132  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
133 
134  if (pthisHistoPair != m_histoDefMap.end()) {
135  nbins = pthisHistoPair->second.nBinsY;
136  }
137  return nbins;
138 }
139 
140 unsigned int
141 HistogramDefinitionSvc::nBinsZ(const std::string& name, const std::string& dirName) const {
142  unsigned int nbins(0);
143  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
144 
145  if (pthisHistoPair != m_histoDefMap.end()) {
146  nbins = pthisHistoPair->second.nBinsZ;
147  }
148  return nbins;
149 }
150 
152 HistogramDefinitionSvc::xLimits(const std::string& name, const std::string& dirName) const {
153  axesLimits_t result(invalidLimits);
154  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
155 
156  if (pthisHistoPair != m_histoDefMap.end()) {
157  result = pthisHistoPair->second.xAxis;
158  }
159  return result;
160 }
161 
163 HistogramDefinitionSvc::yLimits(const std::string& name, const std::string& dirName) const {
164  axesLimits_t result(invalidLimits);
165  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
166 
167  if (pthisHistoPair != m_histoDefMap.end()) {
168  result = pthisHistoPair->second.yAxis;
169  }
170  return result;
171 }
172 
174 HistogramDefinitionSvc::zLimits(const std::string& name, const std::string& dirName) const {
175  axesLimits_t result(invalidLimits);
176  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
177 
178  if (pthisHistoPair != m_histoDefMap.end()) {
179  result = pthisHistoPair->second.zAxis;
180  }
181  return result;
182 }
183 
184 std::string
185 HistogramDefinitionSvc::xTitle(const std::string& name, const std::string& dirName) const {
186  std::string result {};
187  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
188 
189  if (pthisHistoPair != m_histoDefMap.end()) {
190  result = pthisHistoPair->second.xTitle;
191  }
192  return result;
193 }
194 
195 std::string
196 HistogramDefinitionSvc::yTitle(const std::string& name, const std::string& dirName) const {
197  std::string result {};
198  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
199  if (pthisHistoPair != m_histoDefMap.end()) {
200  result = pthisHistoPair->second.yTitle;
201  }
202  return result;
203 }
204 
205 std::string
206 HistogramDefinitionSvc::zTitle(const std::string& name, const std::string& dirName) const {
207  std::string result {};
208  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
209  if (pthisHistoPair != m_histoDefMap.end()) {
210  result = pthisHistoPair->second.zTitle;
211  }
212  return result;
213 }
214 
215 bool
217  return m_reader->sourceExists();
218 }
219 
220 bool
222  return (m_format < NFORMATS)and(m_format >= 0);
223 }
HistogramDefinitionSvc::title
std::string title(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:108
SingleHistogramDefinition::stringIndex
std::string stringIndex() const
add the folder path to produce the full histogram name e.g. myFolder/myHisto
Definition: SingleHistogramDefinition.cxx:126
python.StoreID.UNKNOWN
int UNKNOWN
Definition: StoreID.py:16
IHistogramDefinitionSvc::axesLimits_t
std::pair< float, float > axesLimits_t
typedef for axes limits, (lower bound, upper bound)
Definition: IHistogramDefinitionSvc.h:29
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
HistogramDefinitionSvc::xLimits
IHistogramDefinitionSvc::axesLimits_t xLimits(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:152
get_generator_info.result
result
Definition: get_generator_info.py:21
HistogramDefinitionSvc::formatOk
bool formatOk()
Definition: HistogramDefinitionSvc.cxx:221
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HistogramDefinitionSvc.h
HistogramDefinitionSvc::zTitle
std::string zTitle(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:206
HistogramDefinitionSvc::finalize
virtual StatusCode finalize()
Definition: HistogramDefinitionSvc.cxx:80
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
HistogramDefinitionSvc::yTitle
std::string yTitle(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:196
HistogramDefinitionSvc::m_histoDefMap
std::map< std::string, SingleHistogramDefinition > m_histoDefMap
Definition: HistogramDefinitionSvc.h:52
HistogramDefinitionSvc::HistogramDefinitionSvc
HistogramDefinitionSvc(const std::string &name, ISvcLocator *svc)
Definition: HistogramDefinitionSvc.cxx:25
HistogramDefinitionSvc::initialize
virtual StatusCode initialize()
Definition: HistogramDefinitionSvc.cxx:34
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HistogramDefinitionSvc::nBinsY
unsigned int nBinsY(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:130
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HistogramDefinitionSvc::zLimits
IHistogramDefinitionSvc::axesLimits_t zLimits(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:174
HistogramDefinitionSvc::m_format
IHistogramDefinitionSvc::Formats m_format
Definition: HistogramDefinitionSvc.h:51
IReadHistoDef.h
SingleHistogramDefinition
Almost-a-struct for holding the single histogram definition.
Definition: SingleHistogramDefinition.h:17
HistogramDefinitionSvc::nBinsZ
unsigned int nBinsZ(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:141
HistogramDefinitionSvc::nBinsX
unsigned int nBinsX(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:119
HistogramDefinitionSvc::~HistogramDefinitionSvc
virtual ~HistogramDefinitionSvc()
ReadFromXmlDom.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
HistogramDefinitionSvc::m_formatString
StringProperty m_formatString
Definition: HistogramDefinitionSvc.h:50
HistogramDefinitionSvc::sourceExists
bool sourceExists()
Definition: HistogramDefinitionSvc.cxx:216
h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HistogramDefinitionSvc::histoType
std::string histoType(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:97
HistogramDefinitionSvc::definition
SingleHistogramDefinition definition(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:86
HistogramDefinitionSvc::m_source
StringProperty m_source
Definition: HistogramDefinitionSvc.h:49
HistogramDefinitionSvc::xTitle
std::string xTitle(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:185
HistogramDefinitionSvc::yLimits
IHistogramDefinitionSvc::axesLimits_t yLimits(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:163
HistogramDefinitionSvc::m_reader
std::unique_ptr< IReadHistoDef > m_reader
Definition: HistogramDefinitionSvc.h:53