Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Generic2D.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include "Annotation.h"
14 #include "Axis.h"
15 #include <AIDA/IProfile2D.h>
17 #include <TFile.h>
18 #include <memory>
19 #include <stdexcept>
20 
21 // Hide warning message:
22 // warning: 'XYZ' overrides a member function but is not marked 'override'
23 #ifdef __clang__
24 # pragma clang diagnostic push
25 # pragma clang diagnostic ignored "-Wsuggest-override"
26 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
27 #elif defined( __GNUC__ )
28 # pragma GCC diagnostic push
29 # pragma GCC diagnostic ignored "-Wsuggest-override"
30 #endif
31 
32 namespace Gaudi {
33 
44  template <class INTERFACE, class IMPLEMENTATION>
45  class GAUDI_API Generic2D : virtual public INTERFACE, virtual public HistogramBase {
46  public:
48 
49  Generic2D() = default;
50 
51  protected:
53  Generic2D( IMPLEMENTATION* p ) : m_rep( p ) {}
54 
55  public:
57  TObject* representation() const override { return m_rep.get(); }
59  void adoptRepresentation( TObject* rep ) override;
61  std::string title() const override { return m_annotation.value( "Title" ); }
63  bool setTitle( const std::string& title ) override;
65  std::string name() const { return m_annotation.value( "Name" ); }
67  bool setName( const std::string& newName );
69  AIDA::IAnnotation& annotation() override { return m_annotation; }
71  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
72 
74  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
76  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
78  virtual int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
80  virtual int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
81 
83  int entries() const override;
85  int allEntries() const override;
87  int extraEntries() const override;
89  double sumBinHeights() const override;
91  double sumAllBinHeights() const override;
93  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
95  double minBinHeight() const override;
97  double maxBinHeight() const override;
98 
100  double binMeanX( int indexX, int indexY ) const override;
102  double binMeanY( int indexX, int indexY ) const override;
104  int binEntries( int indexX, int indexY ) const override;
106  int binEntriesX( int indexX ) const override;
108  int binEntriesY( int indexY ) const override;
110  double binHeight( int indexX, int indexY ) const override;
112  double binHeightX( int indexX ) const override;
114  double binHeightY( int indexY ) const override;
116  double binError( int indexX, int indexY ) const override;
118  virtual double binRms( int indexX, int indexY ) const;
120  double meanX() const override;
122  double meanY() const override;
124  double rmsX() const override;
126  double rmsY() const override;
128  int coordToIndexX( double coordX ) const override;
130  int coordToIndexY( double coordY ) const override;
132  virtual double equivalentBinEntries() const;
135  virtual bool scale( double scaleFactor );
137  bool add( const INTERFACE& h ) override;
138  // overwrite reset
139  bool reset() override;
141  void* cast( const std::string& className ) const override;
143  const std::string& userLevelClassType() const { return m_classType; }
145  int dimension() const override { return 2; }
147  std::ostream& print( std::ostream& s ) const override;
149  std::ostream& write( std::ostream& s ) const override;
151  int write( const char* file_name ) const override;
152 
153  protected:
161  std::unique_ptr<IMPLEMENTATION> m_rep;
163  std::string m_classType;
165  int m_sumEntries = 0;
166  };
167 
168  template <class INTERFACE, class IMPLEMENTATION>
169  bool Generic2D<INTERFACE, IMPLEMENTATION>::setTitle( const std::string& title ) {
170  m_rep->SetTitle( title.c_str() );
171  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
172  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
173  return true;
174  }
175 
176  template <class INTERFACE, class IMPLEMENTATION>
177  bool Generic2D<INTERFACE, IMPLEMENTATION>::setName( const std::string& newName ) {
178  m_rep->SetName( newName.c_str() );
179  m_annotation.setValue( "Name", newName );
180  return true;
181  }
182 
183  template <class INTERFACE, class IMPLEMENTATION>
185  return m_rep->GetEntries();
186  }
187 
188  template <class INTERFACE, class IMPLEMENTATION>
190  return m_rep->GetEntries();
191  }
192 
193  template <class INTERFACE, class IMPLEMENTATION>
195  return m_rep->GetMinimum();
196  }
197 
198  template <class INTERFACE, class IMPLEMENTATION>
200  return m_rep->GetMaximum();
201  }
202 
203  template <class INTERFACE, class IMPLEMENTATION>
205  return m_rep->GetSumOfWeights();
206  }
207 
208  template <class INTERFACE, class IMPLEMENTATION>
210  return m_rep->GetSum();
211  }
212 
213  template <class INTERFACE, class IMPLEMENTATION>
214  double Generic2D<INTERFACE, IMPLEMENTATION>::binRms( int indexX, int indexY ) const {
215  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
216  }
217 
218  template <class INTERFACE, class IMPLEMENTATION>
219  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanX( int indexX, int ) const {
220  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
221  }
222 
223  template <class INTERFACE, class IMPLEMENTATION>
224  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanY( int, int indexY ) const {
225  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
226  }
227 
228  template <class INTERFACE, class IMPLEMENTATION>
230  int n = 0;
231  for ( int iY = -2; iY < yAxis().bins(); ++iY ) n += binEntries( index, iY );
232  return n;
233  }
234 
235  template <class INTERFACE, class IMPLEMENTATION>
237  int n = 0;
238  for ( int iX = -2; iX < xAxis().bins(); ++iX ) n += binEntries( iX, index );
239  return n;
240  }
241 
242  template <class INTERFACE, class IMPLEMENTATION>
243  double Generic2D<INTERFACE, IMPLEMENTATION>::binHeight( int indexX, int indexY ) const {
244  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ) );
245  }
246 
247  template <class INTERFACE, class IMPLEMENTATION>
249  double s = 0;
250  for ( int iY = -2; iY < yAxis().bins(); ++iY ) { s += binHeight( index, iY ); }
251  return s;
252  }
253 
254  template <class INTERFACE, class IMPLEMENTATION>
256  double s = 0;
257  for ( int iX = -2; iX < xAxis().bins(); ++iX ) s += binHeight( iX, index );
258  return s;
259  }
260 
261  template <class INTERFACE, class IMPLEMENTATION>
262  double Generic2D<INTERFACE, IMPLEMENTATION>::binError( int indexX, int indexY ) const {
263  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
264  }
265 
266  template <class INTERFACE, class IMPLEMENTATION>
268  return m_rep->GetMean( 1 );
269  }
270 
271  template <class INTERFACE, class IMPLEMENTATION>
273  return m_rep->GetMean( 2 );
274  }
275 
276  template <class INTERFACE, class IMPLEMENTATION>
278  return m_rep->GetRMS( 1 );
279  }
280 
281  template <class INTERFACE, class IMPLEMENTATION>
283  return m_rep->GetRMS( 2 );
284  }
285 
286  template <class INTERFACE, class IMPLEMENTATION>
288  return xAxis().coordToIndex( coord );
289  }
290 
291  template <class INTERFACE, class IMPLEMENTATION>
293  return yAxis().coordToIndex( coord );
294  }
295 
296  template <class INTERFACE, class IMPLEMENTATION>
297  bool Generic2D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& hist ) {
298  const Base* p = dynamic_cast<const Base*>( &hist );
299  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
300  m_rep->Add( p->m_rep.get() );
301  return true;
302  }
303 
304  template <class INTERFACE, class IMPLEMENTATION>
306  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
307  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
308  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
309  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
310  }
311 
312  template <class INTERFACE, class IMPLEMENTATION>
314  if ( sumBinHeights() <= 0 ) return 0;
315  Stat_t stats[11]; // cover up to 3D...
316  m_rep->GetStats( stats );
317  return stats[0] * stats[0] / stats[1];
318  }
319 
320  template <class INTERFACE, class IMPLEMENTATION>
321  bool Generic2D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
322  m_rep->Scale( scaleFactor );
323  return true;
324  }
325 
326  template <class INTERFACE, class IMPLEMENTATION>
328  m_sumEntries = 0;
329  m_rep->Reset();
330  return true;
331  }
332 
333  template <class INTERFACE, class IMPLEMENTATION>
334  std::ostream& Generic2D<INTERFACE, IMPLEMENTATION>::print( std::ostream& s ) const {
336  m_rep->Print( "all" );
337  return s;
338  }
339 
341  template <class INTERFACE, class IMPLEMENTATION>
342  std::ostream& Generic2D<INTERFACE, IMPLEMENTATION>::write( std::ostream& s ) const {
343  s << std::endl << "2D Histogram Table: " << std::endl;
344  s << "BinX, BinY, Height, Error " << std::endl;
345  for ( int i = 0; i < xAxis().bins(); ++i ) {
346  for ( int j = 0; j < yAxis().bins(); ++j ) {
347  s << binMeanX( i, j ) << ", " << binMeanY( i, j ) << ", " << binHeight( i, j ) << ", " << binError( i, j )
348  << std::endl;
349  }
350  }
351  s << std::endl;
352  return s;
353  }
354 
356  template <class INTERFACE, class IMPLEMENTATION>
357  int Generic2D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
358  TFile* f = TFile::Open( file_name, "RECREATE" );
359  Int_t nbytes = m_rep->Write();
360  f->Close();
361  return nbytes;
362  }
363 } // namespace Gaudi
364 
365 #ifdef __clang__
366 # pragma clang diagnostic pop
367 #elif defined( __GNUC__ )
368 # pragma GCC diagnostic pop
369 #endif
Annotation.h
Gaudi::Generic2D::Base
Generic2D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic2D.h:47
Gaudi::Generic2D::extraEntries
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic2D.h:305
Gaudi::Generic2D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic2D.h:194
Gaudi::Generic2D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic2D.h:145
Gaudi::Generic2D::binMeanX
double binMeanX(int indexX, int indexY) const override
The weighted mean along x of a given bin.
Definition: Generic2D.h:219
Gaudi::Generic2D::setName
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic2D.h:177
Gaudi::Generic2D::representation
TObject * representation() const override
ROOT object implementation.
Definition: Generic2D.h:57
Gaudi::Generic2D::xAxis
const AIDA::IAxis & xAxis() const override
Return the X axis.
Definition: Generic2D.h:74
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Generic2D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic2D.h:159
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:25
Gaudi::Generic2D::coordToIndexY
int coordToIndexY(double coordY) const override
Convenience method, equivalent to yAxis().coordToIndex(coord).
Definition: Generic2D.h:292
Gaudi::Generic2D::binHeightY
double binHeightY(int indexY) const override
Equivalent to projectionY().binHeight(indexY).
Definition: Generic2D.h:255
Gaudi::Generic2D::binEntriesX
int binEntriesX(int indexX) const override
Equivalent to projectionX().binEntries(indexX).
Definition: Generic2D.h:229
Gaudi::Generic2D::rIndexY
virtual int rIndexY(int index) const
operator methods
Definition: Generic2D.h:80
Gaudi::Generic2D::coordToIndexX
int coordToIndexX(double coordX) const override
Convenience method, equivalent to xAxis().coordToIndex(coord).
Definition: Generic2D.h:287
Gaudi::Generic2D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic2D.h:357
Gaudi::Generic2D::rIndexX
virtual int rIndexX(int index) const
operator methods
Definition: Generic2D.h:78
Gaudi::Generic2D::binEntriesY
int binEntriesY(int indexY) const override
Equivalent to projectionY().binEntries(indexY).
Definition: Generic2D.h:236
Gaudi::Generic2D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:169
Gaudi::Generic2D::rmsY
double rmsY() const override
Returns the rms of the profile as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:282
Gaudi::Generic2D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic2D.h:199
Gaudi::Generic2D::cast
void * cast(const std::string &className) const override
Introspection method.
Gaudi::HistogramBase
Definition: HistogramBase.h:31
Gaudi::Generic2D::binMeanY
double binMeanY(int indexX, int indexY) const override
The weighted mean along y of a given bin.
Definition: Generic2D.h:224
Gaudi::Generic2D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic2D.h:69
Gaudi::Generic2D::binEntries
int binEntries(int indexX, int indexY) const override
The number of entries (ie the number of times fill was called for this bin).
Gaudi::Generic2D::Generic2D
Generic2D(IMPLEMENTATION *p)
constructor
Definition: Generic2D.h:53
Gaudi::Generic2D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Gaudi::Generic2D::annotation
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic2D.h:71
Gaudi::Generic2D::scale
virtual bool scale(double scaleFactor)
Scale the weights and the errors of all the IHistogram's bins (in-range and out-of-range ones) by a g...
Definition: Generic2D.h:321
Gaudi::Generic2D::allEntries
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile.
Definition: Generic2D.h:189
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::Generic2D::m_yAxis
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:157
AlgSequencer.h
h
Definition: AlgSequencer.py:31
Gaudi::Generic2D::m_classType
std::string m_classType
class type
Definition: Generic2D.h:163
Axis.h
HistogramBase.h
Gaudi::Generic2D::reset
bool reset() override
Definition: Generic2D.h:327
Gaudi::Generic2D::add
bool add(const INTERFACE &h) override
Modifies this profile by adding the contents of profile to it.
Definition: Generic2D.h:297
Gaudi::Generic2D::binHeightX
double binHeightX(int indexX) const override
Equivalent to projectionX().binHeight(indexX).
Definition: Generic2D.h:248
Gaudi::Generic2D::userLevelClassType
const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic2D.h:143
Gaudi::Generic2D::sumAllBinHeights
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic2D.h:209
Gaudi::Generic2D::yAxis
const AIDA::IAxis & yAxis() const override
Return the Y axis.
Definition: Generic2D.h:76
Gaudi::Generic2D::m_xAxis
Axis m_xAxis
X axis member.
Definition: Generic2D.h:155
Gaudi::Generic2D::meanX
double meanX() const override
Returns the mean of the profile, as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:267
Gaudi::Generic2D::title
std::string title() const override
Get the title of the object.
Definition: Generic2D.h:61
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Generic2D::binRms
virtual double binRms(int indexX, int indexY) const
The spread (RMS) of this bin.
Definition: Generic2D.h:214
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:28
Gaudi::Generic2D::rmsX
double rmsX() const override
Returns the rms of the profile as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:277
Gaudi::Generic2D::name
std::string name() const
object name
Definition: Generic2D.h:65
Gaudi::Generic2D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:161
Gaudi::Generic2D::binHeight
double binHeight(int indexX, int indexY) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic2D.h:243
Gaudi::Generic2D::Generic2D
Generic2D()=default
Gaudi::Generic2D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic2D.h:184
Gaudi::Generic2D
Definition: Generic2D.h:45
Gaudi::Generic2D::binError
double binError(int indexX, int indexY) const override
The error on this bin.
Definition: Generic2D.h:262
Gaudi::Generic2D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic2D.h:93
Gaudi::Generic2D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:204
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Generic2D::write
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic2D.h:342
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39
Gaudi::Generic2D::print
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic2D.h:334
Gaudi::Generic2D::meanY
double meanY() const override
Returns the mean of the profile, as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:272
Gaudi::Generic2D::equivalentBinEntries
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:313