ATLAS Offline Software
Loading...
Searching...
No Matches
PlotMgr.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
11#include "PlotMgr.h"
13
15#include "GaudiKernel/ISvcLocator.h"
16#include "GaudiKernel/Service.h"
17
19#include <cmath> // for std::isnan
20
21
26 const std::string& dirName,
27 const std::string& anaTag,
28 PlotMgr* pParent ) :
29 PlotBase( pParent, dirName ),
30 AthMessaging( "PlotMgr"+anaTag ),
31 m_anaTag( anaTag ) { }
32
33
38{
41 return StatusCode::SUCCESS;
42}
43
44
49 const std::string& identifier,
50 const std::string& folderOverride,
51 const std::string& nameOverride ) const
52{
54 ISvcLocator* svcLoc = Gaudi::svcLocator();
55 SmartIF<IPlotsDefinitionSvc> plotsDefSvc(svcLoc->service( "PlotsDefSvc"+m_anaTag ));
56 ATH_CHECK( plotsDefSvc.isValid(), {} );
57
59 SinglePlotDefinition sDef = plotsDefSvc->definition( identifier );
60
62 if( sDef.isEmpty() or not sDef.isValid() ) return sDef;
63
65 if( not folderOverride.empty() ) sDef.folder( folderOverride );
66
68 if( not nameOverride.empty() ) sDef.name( nameOverride );
69
70 return sDef;
71}
72
73
79 TH1*& pHisto, const IDTPM::SinglePlotDefinition& def )
80{
81 if( not def.isValid() ) {
82 ATH_MSG_ERROR( "Non-valid TH1 plot : " << def.identifier() );
83 return StatusCode::FAILURE;
84 }
85
86 pHisto = Book1D( def.name(), def.titleDigest(),
87 def.nBinsX(), def.xLow(), def.xHigh(),
88 false );
89
90 if( def.doLogLinBinsX() ) {
91 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
92 }
93
94 if( def.doVarBinsX() ) {
95 ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
96 }
97
98 if( not def.xBinLabelsVec().empty() ) {
99 ATH_CHECK( setBinLabels( pHisto, def.xBinLabelsVec(), 'X' ) );
100 }
101
102 return StatusCode::SUCCESS;
103}
104
105
108 TH2*& pHisto, const IDTPM::SinglePlotDefinition& def )
109{
110 if( not def.isValid() ) {
111 ATH_MSG_ERROR( "Non-valid TH2 plot : " << def.identifier() );
112 return StatusCode::FAILURE;
113 }
114
115 pHisto = Book2D( def.name(), def.titleDigest(),
116 def.nBinsX(), def.xLow(), def.xHigh(),
117 def.nBinsY(), def.yLow(), def.yHigh(),
118 false );
119
120 if( def.doLogLinBinsX() ) {
121 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
122 }
123
124 if( def.doLogLinBinsY() ) {
125 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
126 }
127
128 if( def.doVarBinsX() ) {
129 ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
130 }
131
132 if( def.doVarBinsY() ) {
133 ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
134 }
135
136 if( not def.xBinLabelsVec().empty() ) {
137 ATH_CHECK( setBinLabels( pHisto, def.xBinLabelsVec(), 'X' ) );
138 }
139
140 if( not def.yBinLabelsVec().empty() ) {
141 ATH_CHECK( setBinLabels( pHisto, def.yBinLabelsVec(), 'Y' ) );
142 }
143
144 return StatusCode::SUCCESS;
145}
146
147
150 TH3*& pHisto, const IDTPM::SinglePlotDefinition& def )
151{
152 if( not def.isValid() ) {
153 ATH_MSG_ERROR( "Non-valid TH3 plot : " << def.identifier() );
154 return StatusCode::FAILURE;
155 }
156
157 pHisto = Book3D( def.name(), def.titleDigest(),
158 def.nBinsX(), def.xLow(), def.xHigh(),
159 def.nBinsY(), def.yLow(), def.yHigh(),
160 def.nBinsZ(), def.zLow(), def.zHigh(),
161 false );
162
163 if( def.doLogLinBinsX() ) {
164 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
165 }
166
167 if( def.doLogLinBinsY() ) {
168 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
169 }
170
171 if( def.doLogLinBinsZ() ) {
172 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsZ(), def.zLow(), def.zHigh(), 'Z' ) );
173 }
174
175 if( def.doVarBinsX() ) {
176 ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
177 }
178
179 if( def.doVarBinsY() ) {
180 ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
181 }
182
183 if( def.doVarBinsZ() ) {
184 ATH_CHECK( setVariableBins( pHisto, def.zBinsVec(), 'Z' ) );
185 }
186
187 if( not def.xBinLabelsVec().empty() ) {
188 ATH_CHECK( setBinLabels( pHisto, def.xBinLabelsVec(), 'X' ) );
189 }
190
191 if( not def.yBinLabelsVec().empty() ) {
192 ATH_CHECK( setBinLabels( pHisto, def.yBinLabelsVec(), 'Y' ) );
193 }
194
195 if( not def.zBinLabelsVec().empty() ) {
196 ATH_CHECK( setBinLabels( pHisto, def.zBinLabelsVec(), 'Z' ) );
197 }
198
199 return StatusCode::SUCCESS;
200}
201
202
205 TProfile*& pHisto, const IDTPM::SinglePlotDefinition& def )
206{
207 if( not def.isValid() ) {
208 ATH_MSG_ERROR( "Non-valid TProfile plot : " << def.identifier() );
209 return StatusCode::FAILURE;
210 }
211
212 pHisto = BookTProfile( def.name(), def.titleDigest(),
213 def.nBinsX(), def.xLow(), def.xHigh(),
214 def.yLow(), def.yHigh(),
215 false );
216
217 if( def.doLogLinBinsX() ) {
218 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
219 }
220
221 if( def.doVarBinsX() ) {
222 ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
223 }
224
225 if( not def.xBinLabelsVec().empty() ) {
226 ATH_CHECK( setBinLabels( pHisto, def.xBinLabelsVec(), 'X' ) );
227 }
228
229 return StatusCode::SUCCESS;
230}
231
232
235 TProfile2D*& pHisto, const IDTPM::SinglePlotDefinition& def )
236{
237 if( not def.isValid() ) {
238 ATH_MSG_ERROR( "Non-valid TProfile2D plot : " << def.identifier() );
239 return StatusCode::FAILURE;
240 }
241
242 pHisto = BookTProfile2D( def.name(), def.titleDigest(),
243 def.nBinsX(), def.xLow(), def.xHigh(),
244 def.nBinsY(), def.yLow(), def.yHigh(),
245 false );
246
247 if( def.doLogLinBinsX() ) {
248 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
249 }
250
251 if( def.doLogLinBinsY() ) {
252 ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
253 }
254
255 if( def.doVarBinsX() ) {
256 ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
257 }
258
259 if( def.doVarBinsY() ) {
260 ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
261 }
262
263 if( not def.xBinLabelsVec().empty() ) {
264 ATH_CHECK( setBinLabels( pHisto, def.xBinLabelsVec(), 'X' ) );
265 }
266
267 if( not def.yBinLabelsVec().empty() ) {
268 ATH_CHECK( setBinLabels( pHisto, def.yBinLabelsVec(), 'Y' ) );
269 }
270
271 return StatusCode::SUCCESS;
272}
273
274
277 TEfficiency*& pHisto, const IDTPM::SinglePlotDefinition& def )
278{
279 if( not def.isValid() ) {
280 ATH_MSG_ERROR( "Non-valid TEfficiency plot : " << def.identifier() );
281 return StatusCode::FAILURE;
282 }
283
284 pHisto = ( def.nBinsY() == 0 ) ?
285 BookTEfficiency( def.name(), def.titleDigest(),
286 def.nBinsX(), def.xLow(), def.xHigh(),
287 false ) :
288 BookTEfficiency( def.name(), def.titleDigest(),
289 def.nBinsX(), def.xLow(), def.xHigh(),
290 def.nBinsY(), def.yLow(), def.yHigh(),
291 false );
292
293 if( def.doLogLinBinsX() ) {
294 ATH_CHECK( setLogLinearBinsEff( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
295 }
296
297 if( def.doLogLinBinsY() and def.nBinsY() != 0 ) {
298 ATH_CHECK( setLogLinearBinsEff( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
299 }
300
301 if( def.doVarBinsX() ) {
302 ATH_CHECK( setVariableBinsEff( pHisto, def.xBinsVec(), 'X' ) );
303 }
304
305 if( def.doVarBinsY() and def.nBinsY() != 0 ) {
306 ATH_CHECK( setVariableBinsEff( pHisto, def.yBinsVec(), 'Y' ) );
307 }
308
309 return StatusCode::SUCCESS;
310}
311
312
318 TH1* pTh1, float value, float weight ) const
319{
320 if( not pTh1 ) {
321 ATH_MSG_ERROR( "Trying to fill non-definded TH1" );
322 return StatusCode::FAILURE;
323 }
324
325 if( std::isnan( value ) or std::isnan( weight ) ) {
326 ATH_MSG_ERROR( "Non-valid fill arguments for TH1:" << pTh1->GetName() );
327 return StatusCode::FAILURE;
328 }
329
331 pTh1->Fill( value, weight );
332 return StatusCode::SUCCESS;
333}
334
335
338 TH2* pTh2, float xval, float yval, float weight ) const
339{
340 if( not pTh2 ) {
341 ATH_MSG_ERROR( "Trying to fill non-definded TH2" );
342 return StatusCode::FAILURE;
343 }
344
345 if( std::isnan( xval ) or std::isnan( yval ) or std::isnan( weight ) ) {
346 ATH_MSG_ERROR( "Non-valid fill arguments for TH2:" << pTh2->GetName() );
347 return StatusCode::FAILURE;
348 }
349
351 pTh2->Fill( xval, yval, weight );
352 return StatusCode::SUCCESS;
353}
354
355
358 TH3* pTh3, float xval, float yval, float zval, float weight ) const
359{
360 if( not pTh3 ) {
361 ATH_MSG_ERROR( "Trying to fill non-definded TH3" );
362 return StatusCode::FAILURE;
363 }
364
365 if( std::isnan( xval ) or std::isnan( yval ) or
366 std::isnan( zval ) or std::isnan( weight ) ) {
367 ATH_MSG_ERROR( "Non-valid fill arguments for TH3:" << pTh3->GetName() );
368 return StatusCode::FAILURE;
369 }
370
371
373 pTh3->Fill( xval, yval, zval, weight );
374 return StatusCode::SUCCESS;
375}
376
377
380 TProfile* pTprofile, float xval, float yval, float weight ) const
381{
382 if( not pTprofile ) {
383 ATH_MSG_ERROR( "Trying to fill non-definded TProfile" );
384 return StatusCode::FAILURE;
385 }
386
387 if( std::isnan( xval ) or std::isnan( yval ) or std::isnan( weight ) ) {
388 ATH_MSG_ERROR( "Non-valid fill arguments for TProfile:" << pTprofile->GetName() );
389 return StatusCode::FAILURE;
390 }
391
393 pTprofile->Fill( xval, yval, weight );
394 return StatusCode::SUCCESS;
395}
396
397
400 TProfile2D* pTprofile, float xval, float yval, float zval, float weight ) const
401{
402 if( not pTprofile ) {
403 ATH_MSG_ERROR( "Trying to fill non-definded TProfile2D" );
404 return StatusCode::FAILURE;
405 }
406
407 if( std::isnan( xval ) or std::isnan( yval ) or
408 std::isnan( zval ) or std::isnan( weight ) ) {
409 ATH_MSG_ERROR( "Non-valid fill arguments for TProfile2D:" << pTprofile->GetName() );
410 return StatusCode::FAILURE;
411 }
412
414 pTprofile->Fill( xval, yval, zval, weight );
415 return StatusCode::SUCCESS;
416}
417
418
421 TEfficiency* pTeff, float value, bool accepted, float weight ) const
422{
423 if( not pTeff ) {
424 ATH_MSG_ERROR( "Trying to fill non-definded 1D TEfficiency" );
425 return StatusCode::FAILURE;
426 }
427
428 if( std::isnan( value ) or std::isnan( weight ) ) {
429 ATH_MSG_ERROR( "Non-valid fill arguments for 1D TEfficiency:" << pTeff->GetName() );
430 return StatusCode::FAILURE;
431 }
432
435 if( weight==1.) pTeff->Fill( accepted, value );
436 else pTeff->FillWeighted( accepted, weight, value );
437 return StatusCode::SUCCESS;
438}
439
440
443 TEfficiency* pTeff2d, float xvalue, float yvalue, bool accepted, float weight ) const
444{
445 if( not pTeff2d ) {
446 ATH_MSG_ERROR( "Trying to fill non-definded 2D TEfficiency" );
447 return StatusCode::FAILURE;
448 }
449
450 if( std::isnan( xvalue ) or std::isnan( yvalue ) or std::isnan( weight ) ) {
451 ATH_MSG_ERROR( "Non-valid fill arguments for 2D TEfficiency:" << pTeff2d->GetName() );
452 return StatusCode::FAILURE;
453 }
454
457 if( weight==1.) pTeff2d->Fill( accepted, xvalue, yvalue );
458 else pTeff2d->FillWeighted( accepted, weight, xvalue, yvalue );
459 return StatusCode::SUCCESS;
460}
461
462
465 unsigned int nBins, float absMin, float absMax, bool symmetriseAroundZero )
466{
467 std::vector<float> emptyVec;
469 if( absMin<=0 or absMax<=0 ) {
470 ATH_MSG_WARNING( "absMin or absMax argument to getLogLinearBins is out of range" );
471 return emptyVec;
472 } else if( nBins==0 ) {
473 ATH_MSG_WARNING( "nBins argument to getLogLinearBins is zero" );
474 return emptyVec;
475 }
477 unsigned int asymVecSize = nBins + 1;
478 std::vector<float> theBinning( asymVecSize, 0.);
480 float logStart = std::log( absMin );
481 float logDist = std::log( absMax ) - logStart;
482 float logStep = logDist / (float) nBins;
484 float thisLog{ logStart };
485 for( float& thisBin : theBinning ) {
486 thisBin = std::exp( thisLog );
487 thisLog += logStep;
488 }
489 if( symmetriseAroundZero ) {
491 return emptyVec;
492 }
493 return theBinning;
494}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
Derived class to give extra capabilities to TrkValHistUtils/PlotBase.h such as ATH_MSG and an easier ...
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
StatusCode setLogLinearBins(P *&pHisto, unsigned int nBins, float absMin, float absMax, char axis)
Set Log-Linear axis.
Definition PlotMgr.h:170
StatusCode setVariableBins(P *&pHisto, const std::vector< float > &binning, char axis)
SetVariableBins.
Definition PlotMgr.h:135
StatusCode setBinLabels(P *&pHisto, const std::vector< std::string > &binLabels, char axis)
SetBinLabels (for TH* and TProfile* only)
Definition PlotMgr.h:196
StatusCode setLogLinearBinsEff(P *&pHisto, unsigned int nBins, float absMin, float absMax, char axis)
Set Log-Linear axis (for Efficiencies)
Definition PlotMgr.h:180
std::string m_anaTag
Definition PlotMgr.h:232
StatusCode book(TH1 *&pHisto, const SinglePlotDefinition &def)
Book a TH1 histogram.
Definition PlotMgr.cxx:78
StatusCode setVariableBinsEff(P *&pHisto, const std::vector< float > &binning, char axis)
SetVariableBins (for Efficiencies)
Definition PlotMgr.h:150
std::vector< float > getLogLinearBins(unsigned int nBins, float absMin, float absMax, bool symmetriseAroundZero=false)
Get Log-Linear binning vector inherited from InDetPhysValMonitoring/src/logLinearBinning....
Definition PlotMgr.cxx:464
StatusCode initialize()
initialize
Definition PlotMgr.cxx:37
StatusCode fill(TH1 *pTh1, float value, float weight=1.) const
Definition PlotMgr.cxx:317
PlotMgr(const std::string &dirName, const std::string &anaTag, PlotMgr *pParent=nullptr)
Constructor taking parent node and directory name for plots pParent = nullptr by default to book plot...
Definition PlotMgr.cxx:25
SinglePlotDefinition retrieveDefinition(const std::string &identifier, const std::string &folderOverride="", const std::string &nameOverride="") const
Retrieve a single histogram definition, given the unique string identifier.
Definition PlotMgr.cxx:48
const std::vector< float > & xBinsVec() const
const std::vector< std::string > & yBinLabelsVec() const
const std::string & folder() const
const std::string & titleDigest() const
const std::vector< float > & yBinsVec() const
const std::vector< float > & zBinsVec() const
const std::string & identifier() const
const std::string & name() const
const std::vector< std::string > & zBinLabelsVec() const
const std::vector< std::string > & xBinLabelsVec() const
TH1D * 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:94
void initialize()
Definition PlotBase.cxx:39
PlotBase(PlotBase *parent, const std::string &sDir)
Definition PlotBase.cxx:29
TH3F * Book3D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY, float startY, float endY, int nBinsZ, float startZ, float endZ, bool prependDir=true)
Book a TH3F histogram.
Definition PlotBase.cxx:157
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:186
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 TH2F histogram.
Definition PlotBase.cxx:123
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:257
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:231