The Gaudi Framework  master (37c0b60a)
Generic2D.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #ifndef GAUDISVC_GENERIC2D_H
12 #define GAUDISVC_GENERIC2D_H 1
13 
14 #include "Annotation.h"
15 #include "Axis.h"
16 #include <AIDA/IProfile2D.h>
18 #include <TFile.h>
19 #include <memory>
20 #include <stdexcept>
21 
22 // Hide warning message:
23 // warning: 'XYZ' overrides a member function but is not marked 'override'
24 #ifdef __clang__
25 # pragma clang diagnostic push
26 # pragma clang diagnostic ignored "-Wsuggest-override"
27 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
28 #elif defined( __GNUC__ )
29 # pragma GCC diagnostic push
30 # pragma GCC diagnostic ignored "-Wsuggest-override"
31 #endif
32 
33 namespace Gaudi {
34 
45  template <class INTERFACE, class IMPLEMENTATION>
46  class GAUDI_API Generic2D : virtual public INTERFACE, virtual public HistogramBase {
47  public:
49 
50  Generic2D() = default;
51 
52  protected:
54  Generic2D( IMPLEMENTATION* p ) : m_rep( p ) {}
55 
56  public:
58  TObject* representation() const override { return m_rep.get(); }
60  void adoptRepresentation( TObject* rep ) override;
62  std::string title() const override { return m_annotation.value( "Title" ); }
64  bool setTitle( const std::string& title ) override;
66  std::string name() const { return m_annotation.value( "Name" ); }
68  bool setName( const std::string& newName );
70  AIDA::IAnnotation& annotation() override { return m_annotation; }
72  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
73 
75  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
77  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
79  virtual int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
81  virtual int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
82 
84  int entries() const override;
86  int allEntries() const override;
88  int extraEntries() const override;
90  double sumBinHeights() const override;
92  double sumAllBinHeights() const override;
94  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
96  double minBinHeight() const override;
98  double maxBinHeight() const override;
99 
101  double binMeanX( int indexX, int indexY ) const override;
103  double binMeanY( int indexX, int indexY ) const override;
105  int binEntries( int indexX, int indexY ) const override;
107  int binEntriesX( int indexX ) const override;
109  int binEntriesY( int indexY ) const override;
111  double binHeight( int indexX, int indexY ) const override;
113  double binHeightX( int indexX ) const override;
115  double binHeightY( int indexY ) const override;
117  double binError( int indexX, int indexY ) const override;
119  virtual double binRms( int indexX, int indexY ) const;
121  double meanX() const override;
123  double meanY() const override;
125  double rmsX() const override;
127  double rmsY() const override;
129  int coordToIndexX( double coordX ) const override;
131  int coordToIndexY( double coordY ) const override;
133  virtual double equivalentBinEntries() const;
136  virtual bool scale( double scaleFactor );
138  bool add( const INTERFACE& h ) override;
139  // overwrite reset
140  bool reset() override;
142  void* cast( const std::string& className ) const override;
144  const std::string& userLevelClassType() const { return m_classType; }
146  int dimension() const override { return 2; }
148  std::ostream& print( std::ostream& s ) const override;
150  std::ostream& write( std::ostream& s ) const override;
152  int write( const char* file_name ) const override;
153 
154  protected:
166  int m_sumEntries = 0;
167  };
168 
169  template <class INTERFACE, class IMPLEMENTATION>
171  m_rep->SetTitle( title.c_str() );
172  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
173  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
174  return true;
175  }
176 
177  template <class INTERFACE, class IMPLEMENTATION>
179  m_rep->SetName( newName.c_str() );
180  m_annotation.setValue( "Name", newName );
181  return true;
182  }
183 
184  template <class INTERFACE, class IMPLEMENTATION>
186  return m_rep->GetEntries();
187  }
188 
189  template <class INTERFACE, class IMPLEMENTATION>
191  return m_rep->GetEntries();
192  }
193 
194  template <class INTERFACE, class IMPLEMENTATION>
196  return m_rep->GetMinimum();
197  }
198 
199  template <class INTERFACE, class IMPLEMENTATION>
201  return m_rep->GetMaximum();
202  }
203 
204  template <class INTERFACE, class IMPLEMENTATION>
206  return m_rep->GetSumOfWeights();
207  }
208 
209  template <class INTERFACE, class IMPLEMENTATION>
211  return m_rep->GetSum();
212  }
213 
214  template <class INTERFACE, class IMPLEMENTATION>
215  double Generic2D<INTERFACE, IMPLEMENTATION>::binRms( int indexX, int indexY ) const {
216  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
217  }
218 
219  template <class INTERFACE, class IMPLEMENTATION>
220  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanX( int indexX, int ) const {
221  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
222  }
223 
224  template <class INTERFACE, class IMPLEMENTATION>
225  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanY( int, int indexY ) const {
226  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
227  }
228 
229  template <class INTERFACE, class IMPLEMENTATION>
231  int n = 0;
232  for ( int iY = -2; iY < yAxis().bins(); ++iY ) n += binEntries( index, iY );
233  return n;
234  }
235 
236  template <class INTERFACE, class IMPLEMENTATION>
238  int n = 0;
239  for ( int iX = -2; iX < xAxis().bins(); ++iX ) n += binEntries( iX, index );
240  return n;
241  }
242 
243  template <class INTERFACE, class IMPLEMENTATION>
244  double Generic2D<INTERFACE, IMPLEMENTATION>::binHeight( int indexX, int indexY ) const {
245  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ) );
246  }
247 
248  template <class INTERFACE, class IMPLEMENTATION>
250  double s = 0;
251  for ( int iY = -2; iY < yAxis().bins(); ++iY ) { s += binHeight( index, iY ); }
252  return s;
253  }
254 
255  template <class INTERFACE, class IMPLEMENTATION>
257  double s = 0;
258  for ( int iX = -2; iX < xAxis().bins(); ++iX ) s += binHeight( iX, index );
259  return s;
260  }
261 
262  template <class INTERFACE, class IMPLEMENTATION>
263  double Generic2D<INTERFACE, IMPLEMENTATION>::binError( int indexX, int indexY ) const {
264  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
265  }
266 
267  template <class INTERFACE, class IMPLEMENTATION>
269  return m_rep->GetMean( 1 );
270  }
271 
272  template <class INTERFACE, class IMPLEMENTATION>
274  return m_rep->GetMean( 2 );
275  }
276 
277  template <class INTERFACE, class IMPLEMENTATION>
279  return m_rep->GetRMS( 1 );
280  }
281 
282  template <class INTERFACE, class IMPLEMENTATION>
284  return m_rep->GetRMS( 2 );
285  }
286 
287  template <class INTERFACE, class IMPLEMENTATION>
289  return xAxis().coordToIndex( coord );
290  }
291 
292  template <class INTERFACE, class IMPLEMENTATION>
294  return yAxis().coordToIndex( coord );
295  }
296 
297  template <class INTERFACE, class IMPLEMENTATION>
298  bool Generic2D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& hist ) {
299  const Base* p = dynamic_cast<const Base*>( &hist );
300  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
301  m_rep->Add( p->m_rep.get() );
302  return true;
303  }
304 
305  template <class INTERFACE, class IMPLEMENTATION>
307  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
308  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
309  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
310  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
311  }
312 
313  template <class INTERFACE, class IMPLEMENTATION>
315  if ( sumBinHeights() <= 0 ) return 0;
316  Stat_t stats[11]; // cover up to 3D...
317  m_rep->GetStats( stats );
318  return stats[0] * stats[0] / stats[1];
319  }
320 
321  template <class INTERFACE, class IMPLEMENTATION>
322  bool Generic2D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
323  m_rep->Scale( scaleFactor );
324  return true;
325  }
326 
327  template <class INTERFACE, class IMPLEMENTATION>
329  m_sumEntries = 0;
330  m_rep->Reset();
331  return true;
332  }
333 
334  template <class INTERFACE, class IMPLEMENTATION>
337  m_rep->Print( "all" );
338  return s;
339  }
340 
342  template <class INTERFACE, class IMPLEMENTATION>
344  s << std::endl << "2D Histogram Table: " << std::endl;
345  s << "BinX, BinY, Height, Error " << std::endl;
346  for ( int i = 0; i < xAxis().bins(); ++i ) {
347  for ( int j = 0; j < yAxis().bins(); ++j ) {
348  s << binMeanX( i, j ) << ", " << binMeanY( i, j ) << ", " << binHeight( i, j ) << ", " << binError( i, j )
349  << std::endl;
350  }
351  }
352  s << std::endl;
353  return s;
354  }
355 
357  template <class INTERFACE, class IMPLEMENTATION>
358  int Generic2D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
359  TFile* f = TFile::Open( file_name, "RECREATE" );
360  Int_t nbytes = m_rep->Write();
361  f->Close();
362  return nbytes;
363  }
364 } // namespace Gaudi
365 
366 #ifdef __clang__
367 # pragma clang diagnostic pop
368 #elif defined( __GNUC__ )
369 # pragma GCC diagnostic pop
370 #endif
371 
372 #endif // GAUDIPI_GENERIC2D_H
Annotation.h
Gaudi::Generic2D::Base
Generic2D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic2D.h:48
Gaudi::Generic2D::extraEntries
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic2D.h:306
Gaudi::Generic2D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic2D.h:195
std::string
STL class.
Gaudi::Generic2D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic2D.h:146
Gaudi::Generic2D::binMeanX
double binMeanX(int indexX, int indexY) const override
The weighted mean along x of a given bin.
Definition: Generic2D.h:220
Gaudi::Generic2D::setName
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic2D.h:178
Gaudi::Generic2D::representation
TObject * representation() const override
ROOT object implementation.
Definition: Generic2D.h:58
Gaudi::Generic2D::xAxis
const AIDA::IAxis & xAxis() const override
Return the X axis.
Definition: Generic2D.h:75
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Generic2D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic2D.h:160
std::unique_ptr::get
T get(T... args)
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:26
Gaudi::Generic2D::coordToIndexY
int coordToIndexY(double coordY) const override
Convenience method, equivalent to yAxis().coordToIndex(coord).
Definition: Generic2D.h:293
Gaudi::Generic2D::binHeightY
double binHeightY(int indexY) const override
Equivalent to projectionY().binHeight(indexY).
Definition: Generic2D.h:256
Gaudi::Generic2D::binEntriesX
int binEntriesX(int indexX) const override
Equivalent to projectionX().binEntries(indexX).
Definition: Generic2D.h:230
Gaudi::Generic2D::rIndexY
virtual int rIndexY(int index) const
operator methods
Definition: Generic2D.h:81
Gaudi::Generic2D::coordToIndexX
int coordToIndexX(double coordX) const override
Convenience method, equivalent to xAxis().coordToIndex(coord).
Definition: Generic2D.h:288
Gaudi::Generic2D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic2D.h:358
Gaudi::Generic2D::rIndexX
virtual int rIndexX(int index) const
operator methods
Definition: Generic2D.h:79
Gaudi::Generic2D::binEntriesY
int binEntriesY(int indexY) const override
Equivalent to projectionY().binEntries(indexY).
Definition: Generic2D.h:237
Gaudi::Generic2D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:170
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:283
Gaudi::Generic2D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic2D.h:200
Gaudi::Generic2D::cast
void * cast(const std::string &className) const override
Introspection method.
Gaudi::HistogramBase
Definition: HistogramBase.h:32
Gaudi::Generic2D::binMeanY
double binMeanY(int indexX, int indexY) const override
The weighted mean along y of a given bin.
Definition: Generic2D.h:225
Gaudi::Generic2D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic2D.h:70
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:54
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:72
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:322
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:190
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ostream
STL class.
Gaudi::Generic2D::m_yAxis
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:158
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:31
Gaudi::Generic2D::m_classType
std::string m_classType
class type
Definition: Generic2D.h:164
Axis.h
HistogramBase.h
Gaudi::Generic2D::reset
bool reset() override
Definition: Generic2D.h:328
Gaudi::Generic2D::add
bool add(const INTERFACE &h) override
Modifies this profile by adding the contents of profile to it.
Definition: Generic2D.h:298
Gaudi::Generic2D::binHeightX
double binHeightX(int indexX) const override
Equivalent to projectionX().binHeight(indexX).
Definition: Generic2D.h:249
std::runtime_error
STL class.
Gaudi::Generic2D::userLevelClassType
const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic2D.h:144
Gaudi::Generic2D::sumAllBinHeights
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic2D.h:210
Gaudi::Generic2D::yAxis
const AIDA::IAxis & yAxis() const override
Return the Y axis.
Definition: Generic2D.h:77
Gaudi::Generic2D::m_xAxis
Axis m_xAxis
X axis member.
Definition: Generic2D.h:156
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:268
Gaudi::Generic2D::title
std::string title() const override
Get the title of the object.
Definition: Generic2D.h:62
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:215
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:29
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:278
Gaudi::Generic2D::name
std::string name() const
object name
Definition: Generic2D.h:66
Gaudi::Generic2D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:162
std::endl
T endl(T... args)
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:244
Gaudi::Generic2D::Generic2D
Generic2D()=default
Gaudi::Generic2D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic2D.h:185
Gaudi::Generic2D
Definition: Generic2D.h:46
Gaudi::Generic2D::binError
double binError(int indexX, int indexY) const override
The error on this bin.
Definition: Generic2D.h:263
Gaudi::Generic2D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic2D.h:94
std::unique_ptr< IMPLEMENTATION >
Gaudi::Generic2D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:205
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Generic2D::write
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic2D.h:343
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:335
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:273
Gaudi::Generic2D::equivalentBinEntries
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:314