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 
29 
32  if (m_formatString.value() == defaultFormat) {
33  m_format = TEXT_PLAIN;
34  }
35  if (m_formatString.value() == xmlFormat) {
36  m_format = TEXT_XML;
37  }
38  if (m_format == UNKNOWN) {
39  ATH_MSG_ERROR("Unknown format for the input to the hdef service");
40  return StatusCode::FAILURE;
41  }
42  ATH_MSG_INFO("hdef source:" << m_source.value());
43  ATH_MSG_INFO("Set format:" << m_formatString.value());
44  ATH_MSG_INFO("format " << m_format);
45  if (m_format == TEXT_PLAIN) {
46  ATH_MSG_FATAL("Text format histogram definition files are deprecated.");
47  return StatusCode::FAILURE;
48  }
49  if (m_format == TEXT_XML) {
50  m_reader = std::make_unique<ReadFromXmlDom>(m_source.value());
51  }
52  ATH_MSG_INFO("Reader initialising from " << m_reader->source() << " in " << m_reader->format() << " format.");
53  bool ok = m_reader->initialize();
54  if (not ok) {
55  ATH_MSG_WARNING("Reader did not initialise");
56  }
57  ok = m_reader->histoDefinitionMap(m_histoDefMap);
58  bool allDefsOk(true);
59  for (auto& h:m_histoDefMap) {
60  if (not h.second.isValid()) {
61  ATH_MSG_WARNING("Invalid histogram definition: " << h.second.str());
62  allDefsOk = false;
63  }
64  }
65  if (ok and(not allDefsOk)) {
66  ATH_MSG_WARNING("Some histogram definitions were bad.");
67  return StatusCode::RECOVERABLE;
68  }
69  if (not ok) {
70  ATH_MSG_ERROR("The definition reader failed to read the histogram definitions.");
71  return StatusCode::FAILURE;
72  }
73  return StatusCode::SUCCESS;
74 }
75 
78  ATH_MSG_INFO("Reader initialised from " << m_reader->source() << " in " << m_reader->format() << " format.");
79  return StatusCode::SUCCESS;
80 }
81 
83 HistogramDefinitionSvc::definition(const std::string& name, const std::string& dirName) const {
85  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
86 
87  if (pthisHistoPair != m_histoDefMap.end()) {
88  result = pthisHistoPair->second;
89  }
90  return result;
91 }
92 
93 std::string
94 HistogramDefinitionSvc::histoType(const std::string& name, const std::string& dirName) const {
95  std::string result {};
96  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
97 
98  if (pthisHistoPair != m_histoDefMap.end()) {
99  result = pthisHistoPair->second.histoType;
100  }
101  return result;
102 }
103 
104 std::string
105 HistogramDefinitionSvc::title(const std::string& name, const std::string& dirName) const {
106  std::string result {};
107  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
108 
109  if (pthisHistoPair != m_histoDefMap.end()) {
110  result = pthisHistoPair->second.title;
111  }
112  return result;
113 }
114 
115 unsigned int
116 HistogramDefinitionSvc::nBinsX(const std::string& name, const std::string& dirName) const {
117  unsigned int nbins(0);
118  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
119 
120  if (pthisHistoPair != m_histoDefMap.end()) {
121  nbins = pthisHistoPair->second.nBinsX;
122  }
123  return nbins;
124 }
125 
126 unsigned int
127 HistogramDefinitionSvc::nBinsY(const std::string& name, const std::string& dirName) const {
128  unsigned int nbins(0);
129  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
130 
131  if (pthisHistoPair != m_histoDefMap.end()) {
132  nbins = pthisHistoPair->second.nBinsY;
133  }
134  return nbins;
135 }
136 
137 unsigned int
138 HistogramDefinitionSvc::nBinsZ(const std::string& name, const std::string& dirName) const {
139  unsigned int nbins(0);
140  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
141 
142  if (pthisHistoPair != m_histoDefMap.end()) {
143  nbins = pthisHistoPair->second.nBinsZ;
144  }
145  return nbins;
146 }
147 
149 HistogramDefinitionSvc::xLimits(const std::string& name, const std::string& dirName) const {
150  axesLimits_t result(invalidLimits);
151  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
152 
153  if (pthisHistoPair != m_histoDefMap.end()) {
154  result = pthisHistoPair->second.xAxis;
155  }
156  return result;
157 }
158 
160 HistogramDefinitionSvc::yLimits(const std::string& name, const std::string& dirName) const {
161  axesLimits_t result(invalidLimits);
162  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
163 
164  if (pthisHistoPair != m_histoDefMap.end()) {
165  result = pthisHistoPair->second.yAxis;
166  }
167  return result;
168 }
169 
171 HistogramDefinitionSvc::zLimits(const std::string& name, const std::string& dirName) const {
172  axesLimits_t result(invalidLimits);
173  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
174 
175  if (pthisHistoPair != m_histoDefMap.end()) {
176  result = pthisHistoPair->second.zAxis;
177  }
178  return result;
179 }
180 
181 std::string
182 HistogramDefinitionSvc::xTitle(const std::string& name, const std::string& dirName) const {
183  std::string result {};
184  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
185 
186  if (pthisHistoPair != m_histoDefMap.end()) {
187  result = pthisHistoPair->second.xTitle;
188  }
189  return result;
190 }
191 
192 std::string
193 HistogramDefinitionSvc::yTitle(const std::string& name, const std::string& dirName) const {
194  std::string result {};
195  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
196  if (pthisHistoPair != m_histoDefMap.end()) {
197  result = pthisHistoPair->second.yTitle;
198  }
199  return result;
200 }
201 
202 std::string
203 HistogramDefinitionSvc::zTitle(const std::string& name, const std::string& dirName) const {
204  std::string result {};
205  const auto pthisHistoPair(m_histoDefMap.find(SingleHistogramDefinition::stringIndex(name, dirName)));
206  if (pthisHistoPair != m_histoDefMap.end()) {
207  result = pthisHistoPair->second.zTitle;
208  }
209  return result;
210 }
211 
212 bool
214  return m_reader->sourceExists();
215 }
216 
217 bool
219  return (m_format < NFORMATS)and(m_format >= 0);
220 }
HistogramDefinitionSvc::title
std::string title(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:105
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:149
get_generator_info.result
result
Definition: get_generator_info.py:21
HistogramDefinitionSvc::formatOk
bool formatOk()
Definition: HistogramDefinitionSvc.cxx:218
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:203
HistogramDefinitionSvc::finalize
virtual StatusCode finalize()
Definition: HistogramDefinitionSvc.cxx:77
HistogramDefinitionSvc::yTitle
std::string yTitle(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:193
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:31
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:127
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:171
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:138
HistogramDefinitionSvc::nBinsX
unsigned int nBinsX(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:116
HistogramDefinitionSvc::~HistogramDefinitionSvc
virtual ~HistogramDefinitionSvc()
ReadFromXmlDom.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
HistogramDefinitionSvc::m_formatString
StringProperty m_formatString
Definition: HistogramDefinitionSvc.h:50
HistogramDefinitionSvc::sourceExists
bool sourceExists()
Definition: HistogramDefinitionSvc.cxx:213
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
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:94
HistogramDefinitionSvc::definition
SingleHistogramDefinition definition(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:83
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:182
HistogramDefinitionSvc::yLimits
IHistogramDefinitionSvc::axesLimits_t yLimits(const std::string &name, const std::string &dirName="") const final
Definition: HistogramDefinitionSvc.cxx:160
HistogramDefinitionSvc::m_reader
std::unique_ptr< IReadHistoDef > m_reader
Definition: HistogramDefinitionSvc.h:53