ATLAS Offline Software
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 
10 #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  return StatusCode::SUCCESS;
99 }
100 
101 
104  TH2*& pHisto, const IDTPM::SinglePlotDefinition& def )
105 {
106  if( not def.isValid() ) {
107  ATH_MSG_ERROR( "Non-valid TH2 plot : " << def.identifier() );
108  return StatusCode::FAILURE;
109  }
110 
111  pHisto = Book2D( def.name(), def.titleDigest(),
112  def.nBinsX(), def.xLow(), def.xHigh(),
113  def.nBinsY(), def.yLow(), def.yHigh(),
114  false );
115 
116  if( def.doLogLinBinsX() ) {
117  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
118  }
119 
120  if( def.doLogLinBinsY() ) {
121  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
122  }
123 
124  if( def.doVarBinsX() ) {
125  ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
126  }
127 
128  if( def.doVarBinsY() ) {
129  ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
130  }
131 
132  return StatusCode::SUCCESS;
133 }
134 
135 
138  TH3*& pHisto, const IDTPM::SinglePlotDefinition& def )
139 {
140  if( not def.isValid() ) {
141  ATH_MSG_ERROR( "Non-valid TH3 plot : " << def.identifier() );
142  return StatusCode::FAILURE;
143  }
144 
145  pHisto = Book3D( def.name(), def.titleDigest(),
146  def.nBinsX(), def.xLow(), def.xHigh(),
147  def.nBinsY(), def.yLow(), def.yHigh(),
148  def.nBinsZ(), def.zLow(), def.zHigh(),
149  false );
150 
151  if( def.doLogLinBinsX() ) {
152  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
153  }
154 
155  if( def.doLogLinBinsY() ) {
156  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
157  }
158 
159  if( def.doLogLinBinsZ() ) {
160  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsZ(), def.zLow(), def.zHigh(), 'Z' ) );
161  }
162 
163  if( def.doVarBinsX() ) {
164  ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
165  }
166 
167  if( def.doVarBinsY() ) {
168  ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
169  }
170 
171  if( def.doVarBinsZ() ) {
172  ATH_CHECK( setVariableBins( pHisto, def.zBinsVec(), 'Z' ) );
173  }
174 
175  return StatusCode::SUCCESS;
176 }
177 
178 
181  TProfile*& pHisto, const IDTPM::SinglePlotDefinition& def )
182 {
183  if( not def.isValid() ) {
184  ATH_MSG_ERROR( "Non-valid TProfile plot : " << def.identifier() );
185  return StatusCode::FAILURE;
186  }
187 
188  pHisto = BookTProfile( def.name(), def.titleDigest(),
189  def.nBinsX(), def.xLow(), def.xHigh(),
190  def.yLow(), def.yHigh(),
191  false );
192 
193  if( def.doLogLinBinsX() ) {
194  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
195  }
196 
197  if( def.doVarBinsX() ) {
198  ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
199  }
200 
201  return StatusCode::SUCCESS;
202 }
203 
204 
207  TProfile2D*& pHisto, const IDTPM::SinglePlotDefinition& def )
208 {
209  if( not def.isValid() ) {
210  ATH_MSG_ERROR( "Non-valid TProfile2D plot : " << def.identifier() );
211  return StatusCode::FAILURE;
212  }
213 
214  pHisto = BookTProfile2D( def.name(), def.titleDigest(),
215  def.nBinsX(), def.xLow(), def.xHigh(),
216  def.nBinsY(), def.yLow(), def.yHigh(),
217  false );
218 
219  if( def.doLogLinBinsX() ) {
220  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
221  }
222 
223  if( def.doLogLinBinsY() ) {
224  ATH_CHECK( setLogLinearBins( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
225  }
226 
227  if( def.doVarBinsX() ) {
228  ATH_CHECK( setVariableBins( pHisto, def.xBinsVec(), 'X' ) );
229  }
230 
231  if( def.doVarBinsY() ) {
232  ATH_CHECK( setVariableBins( pHisto, def.yBinsVec(), 'Y' ) );
233  }
234 
235  return StatusCode::SUCCESS;
236 }
237 
238 
241  TEfficiency*& pHisto, const IDTPM::SinglePlotDefinition& def )
242 {
243  if( not def.isValid() ) {
244  ATH_MSG_ERROR( "Non-valid TEfficiency plot : " << def.identifier() );
245  return StatusCode::FAILURE;
246  }
247 
248  pHisto = ( def.nBinsY() == 0 ) ?
249  BookTEfficiency( def.name(), def.titleDigest(),
250  def.nBinsX(), def.xLow(), def.xHigh(),
251  false ) :
252  BookTEfficiency( def.name(), def.titleDigest(),
253  def.nBinsX(), def.xLow(), def.xHigh(),
254  def.nBinsY(), def.yLow(), def.yHigh(),
255  false );
256 
257  if( def.doLogLinBinsX() ) {
258  ATH_CHECK( setLogLinearBinsEff( pHisto, def.nBinsX(), def.xLow(), def.xHigh(), 'X' ) );
259  }
260 
261  if( def.doLogLinBinsY() and def.nBinsY() != 0 ) {
262  ATH_CHECK( setLogLinearBinsEff( pHisto, def.nBinsY(), def.yLow(), def.yHigh(), 'Y' ) );
263  }
264 
265  if( def.doVarBinsX() ) {
266  ATH_CHECK( setVariableBinsEff( pHisto, def.xBinsVec(), 'X' ) );
267  }
268 
269  if( def.doVarBinsY() and def.nBinsY() != 0 ) {
270  ATH_CHECK( setVariableBinsEff( pHisto, def.yBinsVec(), 'Y' ) );
271  }
272 
273  return StatusCode::SUCCESS;
274 }
275 
276 
282  TH1* pTh1, float value, float weight ) const
283 {
284  if( not pTh1 ) {
285  ATH_MSG_ERROR( "Trying to fill non-definded TH1" );
286  return StatusCode::FAILURE;
287  }
288 
289  if( std::isnan( value ) or std::isnan( weight ) ) {
290  ATH_MSG_ERROR( "Non-valid fill arguments for TH1:" << pTh1->GetName() );
291  return StatusCode::FAILURE;
292  }
293 
295  pTh1->Fill( value, weight );
296  return StatusCode::SUCCESS;
297 }
298 
299 
302  TH2* pTh2, float xval, float yval, float weight ) const
303 {
304  if( not pTh2 ) {
305  ATH_MSG_ERROR( "Trying to fill non-definded TH2" );
306  return StatusCode::FAILURE;
307  }
308 
309  if( std::isnan( xval ) or std::isnan( yval ) or std::isnan( weight ) ) {
310  ATH_MSG_ERROR( "Non-valid fill arguments for TH2:" << pTh2->GetName() );
311  return StatusCode::FAILURE;
312  }
313 
315  pTh2->Fill( xval, yval, weight );
316  return StatusCode::SUCCESS;
317 }
318 
319 
322  TH3* pTh3, float xval, float yval, float zval, float weight ) const
323 {
324  if( not pTh3 ) {
325  ATH_MSG_ERROR( "Trying to fill non-definded TH3" );
326  return StatusCode::FAILURE;
327  }
328 
329  if( std::isnan( xval ) or std::isnan( yval ) or
330  std::isnan( zval ) or std::isnan( weight ) ) {
331  ATH_MSG_ERROR( "Non-valid fill arguments for TH3:" << pTh3->GetName() );
332  return StatusCode::FAILURE;
333  }
334 
335 
337  pTh3->Fill( xval, yval, zval, weight );
338  return StatusCode::SUCCESS;
339 }
340 
341 
344  TProfile* pTprofile, float xval, float yval, float weight ) const
345 {
346  if( not pTprofile ) {
347  ATH_MSG_ERROR( "Trying to fill non-definded TProfile" );
348  return StatusCode::FAILURE;
349  }
350 
351  if( std::isnan( xval ) or std::isnan( yval ) or std::isnan( weight ) ) {
352  ATH_MSG_ERROR( "Non-valid fill arguments for TProfile:" << pTprofile->GetName() );
353  return StatusCode::FAILURE;
354  }
355 
357  pTprofile->Fill( xval, yval, weight );
358  return StatusCode::SUCCESS;
359 }
360 
361 
364  TProfile2D* pTprofile, float xval, float yval, float zval, float weight ) const
365 {
366  if( not pTprofile ) {
367  ATH_MSG_ERROR( "Trying to fill non-definded TProfile2D" );
368  return StatusCode::FAILURE;
369  }
370 
371  if( std::isnan( xval ) or std::isnan( yval ) or
372  std::isnan( zval ) or std::isnan( weight ) ) {
373  ATH_MSG_ERROR( "Non-valid fill arguments for TProfile2D:" << pTprofile->GetName() );
374  return StatusCode::FAILURE;
375  }
376 
378  pTprofile->Fill( xval, yval, zval, weight );
379  return StatusCode::SUCCESS;
380 }
381 
382 
385  TEfficiency* pTeff, float value, bool accepted, float weight ) const
386 {
387  if( not pTeff ) {
388  ATH_MSG_ERROR( "Trying to fill non-definded 1D TEfficiency" );
389  return StatusCode::FAILURE;
390  }
391 
392  if( std::isnan( value ) or std::isnan( weight ) ) {
393  ATH_MSG_ERROR( "Non-valid fill arguments for 1D TEfficiency:" << pTeff->GetName() );
394  return StatusCode::FAILURE;
395  }
396 
399  if( weight==1.) pTeff->Fill( accepted, value );
400  else pTeff->FillWeighted( accepted, weight, value );
401  return StatusCode::SUCCESS;
402 }
403 
404 
407  TEfficiency* pTeff2d, float xvalue, float yvalue, bool accepted, float weight ) const
408 {
409  if( not pTeff2d ) {
410  ATH_MSG_ERROR( "Trying to fill non-definded 2D TEfficiency" );
411  return StatusCode::FAILURE;
412  }
413 
414  if( std::isnan( xvalue ) or std::isnan( yvalue ) or std::isnan( weight ) ) {
415  ATH_MSG_ERROR( "Non-valid fill arguments for 2D TEfficiency:" << pTeff2d->GetName() );
416  return StatusCode::FAILURE;
417  }
418 
421  if( weight==1.) pTeff2d->Fill( accepted, xvalue, yvalue );
422  else pTeff2d->FillWeighted( accepted, weight, xvalue, yvalue );
423  return StatusCode::SUCCESS;
424 }
425 
426 
429  unsigned int nBins, float absMin, float absMax, bool symmetriseAroundZero )
430 {
431  std::vector<float> emptyVec;
433  if( absMin<=0 or absMax<=0 ) {
434  ATH_MSG_WARNING( "absMin or absMax argument to getLogLinearBins is out of range" );
435  return emptyVec;
436  } else if( nBins==0 ) {
437  ATH_MSG_WARNING( "nBins argument to getLogLinearBins is zero" );
438  return emptyVec;
439  }
441  unsigned int asymVecSize = nBins + 1;
442  std::vector<float> theBinning( asymVecSize, 0.);
444  float logStart = std::log( absMin );
445  float logDist = std::log( absMax ) - logStart;
446  float logStep = logDist / (float) nBins;
448  float thisLog{ logStart };
449  for( float& thisBin : theBinning ) {
450  thisBin = std::exp( thisLog );
451  thisLog += logStep;
452  }
453  if( symmetriseAroundZero ) {
455  return emptyVec;
456  }
457  return theBinning;
458 }
IDTPM::PlotMgr::book
StatusCode book(TH1 *&pHisto, const SinglePlotDefinition &def)
Book a TH1 histogram.
Definition: PlotMgr.cxx:78
IDTPM::SinglePlotDefinition::zBinsVec
const std::vector< float > & zBinsVec() const
Definition: SinglePlotDefinition.h:75
IDTPM::SinglePlotDefinition::doVarBinsZ
bool doVarBinsZ() const
Definition: SinglePlotDefinition.h:78
IDTPM::PlotMgr::getLogLinearBins
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:428
PlotBase
Definition: PlotBase.h:34
IDTPM::SinglePlotDefinition::name
const std::string & name() const
Definition: SinglePlotDefinition.h:55
IDTPM::SinglePlotDefinition::isEmpty
bool isEmpty() const
Definition: SinglePlotDefinition.h:80
IDTPM::SinglePlotDefinition::isValid
bool isValid() const
Definition: SinglePlotDefinition.cxx:72
IPlotsDefinitionSvc.h
IDTPM::SinglePlotDefinition::doVarBinsX
bool doVarBinsX() const
Definition: SinglePlotDefinition.h:76
IDTPM::SinglePlotDefinition::yLow
float yLow() const
Definition: SinglePlotDefinition.h:66
athena.value
value
Definition: athena.py:124
IDTPM::SinglePlotDefinition
Definition: SinglePlotDefinition.h:25
IDTPM::PlotMgr::retrieveDefinition
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
xAOD::identifier
identifier
Definition: UncalibratedMeasurement_v1.cxx:15
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
IDTPM::SinglePlotDefinition::doLogLinBinsY
bool doLogLinBinsY() const
Definition: SinglePlotDefinition.h:71
IDTPM::SinglePlotDefinition::nBinsX
unsigned int nBinsX() const
Definition: SinglePlotDefinition.h:61
IDTPM::SinglePlotDefinition::identifier
const std::string & identifier() const
Definition: SinglePlotDefinition.h:81
IDTPM::SinglePlotDefinition::yBinsVec
const std::vector< float > & yBinsVec() const
Definition: SinglePlotDefinition.h:74
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
IDTPM::SinglePlotDefinition::xHigh
float xHigh() const
Definition: SinglePlotDefinition.h:65
IDTPM::SinglePlotDefinition::nBinsZ
unsigned int nBinsZ() const
Definition: SinglePlotDefinition.h:63
IDTPM::PlotMgr
Definition: PlotMgr.h:33
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
IDTPM::SinglePlotDefinition::doVarBinsY
bool doVarBinsY() const
Definition: SinglePlotDefinition.h:77
plotting.yearwise_efficiency_vs_mu.xval
float xval
Definition: yearwise_efficiency_vs_mu.py:35
python.TrigEgammaMonitorHelper.TProfile
def TProfile(*args, **kwargs)
Definition: TrigEgammaMonitorHelper.py:81
PlotBase::initialize
void initialize()
Definition: PlotBase.cxx:39
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
PlotMgr.h
Derived class to give extra capabilities to TrkValHistUtils/PlotBase.h such as ATH_MSG and an easier ...
IDTPM::SinglePlotDefinition::zHigh
float zHigh() const
Definition: SinglePlotDefinition.h:69
IDTPM::SinglePlotDefinition::nBinsY
unsigned int nBinsY() const
Definition: SinglePlotDefinition.h:62
IDTPM::SinglePlotDefinition::yHigh
float yHigh() const
Definition: SinglePlotDefinition.h:67
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
IDTPM::SinglePlotDefinition::xBinsVec
const std::vector< float > & xBinsVec() const
Definition: SinglePlotDefinition.h:73
IDTPM::SinglePlotDefinition::xLow
float xLow() const
Definition: SinglePlotDefinition.h:64
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
IDTPM::PlotMgr::PlotMgr
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
IDTPM::SinglePlotDefinition::doLogLinBinsZ
bool doLogLinBinsZ() const
Definition: SinglePlotDefinition.h:72
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
IDTPM::SinglePlotDefinition::titleDigest
const std::string & titleDigest() const
Definition: SinglePlotDefinition.h:83
plotting.yearwise_efficiency_vs_mu.yval
float yval
Definition: yearwise_efficiency_vs_mu.py:36
IDTPM::PlotMgr::fill
StatusCode fill(TH1 *pTh1, float value, float weight=1.) const
Definition: PlotMgr.cxx:281
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
IDTPM::SinglePlotDefinition::zLow
float zLow() const
Definition: SinglePlotDefinition.h:68
IDTPM::PlotMgr::initialize
StatusCode initialize()
initialize
Definition: PlotMgr.cxx:37
IDTPM::SinglePlotDefinition::folder
const std::string & folder() const
Definition: SinglePlotDefinition.h:79
IDTPM::SinglePlotDefinition::doLogLinBinsX
bool doLogLinBinsX() const
Definition: SinglePlotDefinition.h:70
readCCLHist.float
float
Definition: readCCLHist.py:83