ATLAS Offline Software
Loading...
Searching...
No Matches
JsonPlotsDefReadTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
9
13
15#include <nlohmann/json.hpp>
16
17
22{
23 ATH_MSG_DEBUG( "Initializing " << name() );
25 return StatusCode::SUCCESS;
26}
27
28
32std::vector< IDTPM::SinglePlotDefinition >
34{
35 using json = nlohmann::json;
36 using cstr_t = const std::string&;
37 using strVec_t = std::vector< std::string >;
38
39 std::vector< SinglePlotDefinition > plotDefVec;
40
42 for( cstr_t plotDefStr : m_plotsDefs ) {
43
44 ATH_MSG_DEBUG( "Reading plot definition : " << plotDefStr );
45
47 const json& plotDef = json::parse( plotDefStr );
48
50 cstr_t name = plotDef.contains( "name" ) ?
51 plotDef.at( "name" ).get_ref< cstr_t >() : "";
52
54 cstr_t type = plotDef.contains( "type" ) ?
55 plotDef.at( "type" ).get_ref< cstr_t >() : "";
56
58 cstr_t folder = plotDef.contains( "folder" ) ?
59 plotDef.at( "folder" ).get_ref< cstr_t >() : "";
60
62 cstr_t title = plotDef.contains( "title" ) ?
63 plotDef.at( "title" ).get_ref< cstr_t >() : "";
64
66 unsigned int nBinsX = plotDef.contains( "xAxis_nBins" ) ?
67 getInt( plotDef.at( "xAxis_nBins" ).get_ref< cstr_t >() ) : 0;
68
70 unsigned int nBinsY = plotDef.contains( "yAxis_nBins" ) ?
71 getInt( plotDef.at( "yAxis_nBins" ).get_ref< cstr_t >() ) : 0;
72
74 unsigned int nBinsZ = plotDef.contains( "zAxis_nBins" ) ?
75 getInt( plotDef.at( "zAxis_nBins" ).get_ref< cstr_t >() ) : 0;
76
78 float xLow = plotDef.contains( "xAxis_low" ) ?
79 getFloat( plotDef.at( "xAxis_low" ).get_ref< cstr_t >() ) : 0.;
80 float xHigh = plotDef.contains( "xAxis_high" ) ?
81 getFloat( plotDef.at( "xAxis_high" ).get_ref< cstr_t >() ) : 0.;
82
84 float yLow = plotDef.contains( "yAxis_low" ) ?
85 getFloat( plotDef.at( "yAxis_low" ).get_ref< cstr_t >() ) : 0.;
86 float yHigh = plotDef.contains( "yAxis_high" ) ?
87 getFloat( plotDef.at( "yAxis_high" ).get_ref< cstr_t >() ) : 0.;
88
90 float zLow = plotDef.contains( "zAxis_low" ) ?
91 getFloat( plotDef.at( "zAxis_low" ).get_ref< cstr_t >() ) : 0.;
92 float zHigh = plotDef.contains( "zAxis_high" ) ?
93 getFloat( plotDef.at( "zAxis_high" ).get_ref< cstr_t >() ) : 0.;
94
96 bool xDoLogLinBins(false);
97 if( plotDef.contains( "xAxis_doLogLinBins" ) ) {
98 std::string xDoLogLinBinsStr = plotDef.at( "xAxis_doLogLinBins" ).get_ref< cstr_t >();
99 if( xDoLogLinBinsStr == "true" ) xDoLogLinBins = true;
100 else if( xDoLogLinBinsStr == "false" ) xDoLogLinBins = false;
101 else ATH_MSG_WARNING( "xAxis_doLogLinBins not valid" );
102 }
103
105 bool yDoLogLinBins(false);
106 if( plotDef.contains( "yAxis_doLogLinBins" ) ) {
107 std::string yDoLogLinBinsStr = plotDef.at( "yAxis_doLogLinBins" ).get_ref< cstr_t >();
108 if( yDoLogLinBinsStr == "true" ) yDoLogLinBins = true;
109 else if( yDoLogLinBinsStr == "false" ) yDoLogLinBins = false;
110 else ATH_MSG_WARNING( "yAxis_doLogLinBins not valid" );
111 }
112
114 bool zDoLogLinBins(false);
115 if( plotDef.contains( "zAxis_doLogLinBins" ) ) {
116 std::string zDoLogLinBinsStr = plotDef.at( "zAxis_doLogLinBins" ).get_ref< cstr_t >();
117 if( zDoLogLinBinsStr == "true" ) zDoLogLinBins = true;
118 else if( zDoLogLinBinsStr == "false" ) zDoLogLinBins = false;
119 else ATH_MSG_WARNING( "zAxis_doLogLinBins not valid" );
120 }
121
123 strVec_t xBinsStrVec;
124 if( plotDef.contains( "xAxis_bins" ) ) xBinsStrVec = plotDef.at( "xAxis_bins" ).get< strVec_t >();
125 std::vector< float > xBinsVec;
126 for( cstr_t thisBin : xBinsStrVec ) xBinsVec.push_back( getFloat( thisBin ) );
127 if( not xBinsVec.empty() ) {
129 xLow = xBinsVec.front(); xHigh = xBinsVec.back(); nBinsX = xBinsVec.size() - 1;
130 }
131
133 strVec_t yBinsStrVec;
134 if( plotDef.contains( "yAxis_bins" ) ) yBinsStrVec = plotDef.at( "yAxis_bins" ).get< strVec_t >();
135 std::vector< float > yBinsVec;
136 for( cstr_t thisBin : yBinsStrVec ) yBinsVec.push_back( getFloat( thisBin ) );
137 if( not yBinsVec.empty() ) {
139 yLow = yBinsVec.front(); yHigh = yBinsVec.back(); nBinsY = yBinsVec.size() - 1;
140 }
141
143 strVec_t zBinsStrVec;
144 if( plotDef.contains( "zAxis_bins" ) ) zBinsStrVec = plotDef.at( "zAxis_bins" ).get< strVec_t >();
145 std::vector< float > zBinsVec;
146 for( cstr_t thisBin : zBinsStrVec ) zBinsVec.push_back( getFloat( thisBin ) );
147 if( not zBinsVec.empty() ) {
149 zLow = zBinsVec.front(); zHigh = zBinsVec.back(); nBinsZ = zBinsVec.size() - 1;
150 }
151
153 cstr_t xTitle = plotDef.contains( "xAxis_title" ) ?
154 plotDef.at( "xAxis_title" ).get_ref< cstr_t >() : "";
155
157 cstr_t yTitle = plotDef.contains( "yAxis_title" ) ?
158 plotDef.at( "yAxis_title" ).get_ref< cstr_t >() : "";
159
161 cstr_t zTitle = plotDef.contains( "zAxis_title" ) ?
162 plotDef.at( "zAxis_title" ).get_ref< cstr_t >() : "";
163
165 strVec_t xBinLabelsVec;
166 if( plotDef.contains( "xAxis_labels" ) ) xBinLabelsVec = plotDef.at( "xAxis_labels" ).get< strVec_t >();
167 if( not xBinLabelsVec.empty() ) {
169 nBinsX = xBinLabelsVec.size(); xLow = 0; xHigh = nBinsX;
170 }
171
173 strVec_t yBinLabelsVec;
174 if( plotDef.contains( "yAxis_labels" ) ) yBinLabelsVec = plotDef.at( "yAxis_labels" ).get< strVec_t >();
175 if( not yBinLabelsVec.empty() ) {
177 nBinsY = yBinLabelsVec.size(); yLow = 0; yHigh = nBinsY;
178 }
179
181 strVec_t zBinLabelsVec;
182 if( plotDef.contains( "zAxis_labels" ) ) zBinLabelsVec = plotDef.at( "zAxis_labels" ).get< strVec_t >();
183 if( not zBinLabelsVec.empty() ) {
185 nBinsZ = zBinLabelsVec.size(); zLow = 0; zHigh = nBinsZ;
186 }
187
189 plotDefVec.emplace_back(
190 name, type, title,
191 xTitle, nBinsX, xLow, xHigh, xDoLogLinBins, xBinsVec, xBinLabelsVec,
192 yTitle, nBinsY, yLow, yHigh, yDoLogLinBins, yBinsVec, yBinLabelsVec,
193 zTitle, nBinsZ, zLow, zHigh, zDoLogLinBins, zBinsVec, zBinLabelsVec,
194 folder );
195
197 if( not plotDefVec.back().isValid() ) {
198 ATH_MSG_ERROR( "Removing invalid plot :" <<
199 "\n\t- string: " << plotDefStr <<
200 "\n\t- digest: " << plotDefVec.back().plotDigest() );
201 plotDefVec.pop_back(); // removing from vector
202 }
203
204 } // close m_plotDefs loop
205
206 return plotDefVec;
207}
208
209
214 const std::string& s, float defaultNum ) const
215{
216 try {
217 float f = std::stof(s);
218 return f;
219 } catch(...) {
220 return defaultNum;
221 }
222}
223
225 const std::string& s, unsigned int defaultNum ) const
226{
227 return ( static_cast< unsigned int >( getFloat( s, defaultNum ) ) );
228}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
nlohmann::json json
Tool to read/parse plots definitions from Json input format.
Class to store (internally) each plot definition in this package (originally based on the SingleHisto...
float getFloat(const std::string &s, float defaultNum=std::numeric_limits< float >::quiet_NaN()) const
Utility functions to perform string->number conversion.
unsigned int getInt(const std::string &s, unsigned int defaultNum=0) const
StringArrayProperty m_plotsDefs
Tool properties.
virtual StatusCode initialize() override
Initialize.
virtual std::vector< SinglePlotDefinition > getPlotsDefinitions() const override
Parse input pltos defnitions and returns vector of SinglePlotDefinition.
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition AsgTool.h:133