The Gaudi Framework  master (37c0b60a)
Generic3D.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_GENERIC3D_H
12 #define GAUDISVC_GENERIC3D_H 1
13 
14 #include "Annotation.h"
15 #include "Axis.h"
16 #include <AIDA/IHistogram3D.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 <typename INTERFACE, typename IMPLEMENTATION>
46  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase {
47  public:
50  Generic3D() = default;
51 
52  protected:
54  Generic3D( IMPLEMENTATION* p ) : m_rep( p ) {}
55 
56  public:
58  TObject* representation() const override { return m_rep.get(); }
60  void adoptRepresentation( TObject* rep ) override;
61 
63  int dimension() const override { return 3; }
65  std::string title() const override { return m_annotation.value( "Title" ); }
67  bool setTitle( const std::string& title ) override;
69  std::string name() const { return m_annotation.value( "Name" ); }
71  bool setName( const std::string& newName );
73  AIDA::IAnnotation& annotation() override { return m_annotation; }
75  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
76 
78  int entries() const override;
80  int allEntries() const override;
82  double sumBinHeights() const override;
84  double sumAllBinHeights() const override;
86  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
88  double minBinHeight() const override;
90  double maxBinHeight() const override;
91 
92  int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
93  int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
94  int rIndexZ( int index ) const { return m_zAxis.rIndex( index ); }
95 
97  double binMeanX( int indexX, int, int ) const override {
98  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
99  }
101  double binMeanY( int, int indexY, int ) const override {
102  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
103  }
105  double binMeanZ( int, int, int indexZ ) const override {
106  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexZ ) );
107  }
109  int binEntries( int indexX, int indexY, int indexZ ) const override {
110  if ( binHeight( indexX, indexY, indexZ ) <= 0 ) return 0;
111  double xx = binHeight( indexX, indexY, indexZ ) / binError( indexX, indexY, indexZ );
112  return int( xx * xx + 0.5 );
113  }
115  int binEntriesX( int index ) const override {
116  int n = 0;
117  for ( int i = -2; i < yAxis().bins(); ++i )
118  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( index, i, j );
119  return n;
120  }
122  int binEntriesY( int index ) const override {
123  int n = 0;
124  for ( int i = -2; i < xAxis().bins(); ++i )
125  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( i, index, j );
126  return n;
127  }
128 
130  int binEntriesZ( int index ) const override {
131  int n = 0;
132  for ( int i = -2; i < xAxis().bins(); ++i )
133  for ( int j = -2; j < yAxis().bins(); ++j ) n += binEntries( i, j, index );
134  return n;
135  }
136 
138  double binHeight( int indexX, int indexY, int indexZ ) const {
139  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
140  }
141 
143  double binHeightX( int index ) const override {
144  double s = 0;
145  for ( int i = -2; i < yAxis().bins(); ++i )
146  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( index, i, j );
147  return s;
148  }
150  double binHeightY( int index ) const override {
151  double s = 0;
152  for ( int i = -2; i < xAxis().bins(); ++i )
153  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( i, index, j );
154  return s;
155  }
157  double binHeightZ( int index ) const override {
158  double s = 0;
159  for ( int i = -2; i < xAxis().bins(); ++i )
160  for ( int j = -2; j < yAxis().bins(); ++j ) s += binHeight( i, j, index );
161  return s;
162  }
164  double binError( int indexX, int indexY, int indexZ ) const override {
165  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
166  }
168  double meanX() const override { return m_rep->GetMean( 1 ); }
169 
171  double meanY() const override { return m_rep->GetMean( 2 ); }
173  double meanZ() const override { return m_rep->GetMean( 3 ); }
175  double rmsX() const override { return m_rep->GetRMS( 1 ); }
177  double rmsY() const override { return m_rep->GetRMS( 2 ); }
179  double rmsZ() const override { return m_rep->GetRMS( 3 ); }
181  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
183  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
185  const AIDA::IAxis& zAxis() const override { return m_zAxis; }
187  int coordToIndexX( double coord ) const override { return xAxis().coordToIndex( coord ); }
189  int coordToIndexY( double coord ) const override { return yAxis().coordToIndex( coord ); }
191  int coordToIndexZ( double coord ) const override { return zAxis().coordToIndex( coord ); }
192 
194  double equivalentBinEntries() const override;
197  bool scale( double scaleFactor ) override;
199  bool add( const INTERFACE& hist ) override {
200  const Base* p = dynamic_cast<const Base*>( &hist );
201  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
202  m_rep->Add( p->m_rep.get() );
203  return true;
204  }
205 
206  // overwrite extraentries
207  int extraEntries() const override {
208  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
209  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
210  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
211  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
212  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
213  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
214  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
215  }
217  std::ostream& print( std::ostream& s ) const override;
219  std::ostream& write( std::ostream& s ) const override;
221  int write( const char* file_name ) const override;
222 
223  protected:
231  // class type
233  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
234  int m_sumEntries = 0;
235  }; // end class IHistogram3D
236 
237  template <class INTERFACE, class IMPLEMENTATION>
239  m_rep->SetTitle( title.c_str() );
240  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
241  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
242  return true;
243  }
244 
245  template <class INTERFACE, class IMPLEMENTATION>
247  m_rep->SetName( newName.c_str() );
248  m_annotation.setValue( "Name", newName );
249  return true;
250  }
251  template <class INTERFACE, class IMPLEMENTATION>
253  return m_rep->GetEntries();
254  }
255 
256  template <class INTERFACE, class IMPLEMENTATION>
258  return int( m_rep->GetEntries() );
259  }
260 
261  template <class INTERFACE, class IMPLEMENTATION>
263  return m_rep->GetMinimum();
264  }
265 
266  template <class INTERFACE, class IMPLEMENTATION>
268  return m_rep->GetMaximum();
269  }
270 
271  template <class INTERFACE, class IMPLEMENTATION>
273  return m_rep->GetSumOfWeights();
274  }
275 
276  template <class INTERFACE, class IMPLEMENTATION>
278  return m_rep->GetSum();
279  }
280 
281  template <class INTERFACE, class IMPLEMENTATION>
283  if ( sumBinHeights() <= 0 ) return 0;
284  Stat_t stats[11]; // cover up to 3D...
285  m_rep->GetStats( stats );
286  return stats[0] * stats[0] / stats[1];
287  }
288 
289  template <class INTERFACE, class IMPLEMENTATION>
290  bool Generic3D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
291  m_rep->Scale( scaleFactor );
292  return true;
293  }
294 
295  template <class INTERFACE, class IMPLEMENTATION>
298  m_rep->Print( "all" );
299  return s;
300  }
301 
303  template <class INTERFACE, class IMPLEMENTATION>
305  s << "\n3D Histogram Table: " << std::endl;
306  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
307  for ( int i = 0; i < xAxis().bins(); ++i )
308  for ( int j = 0; j < yAxis().bins(); ++j )
309  for ( int k = 0; k < zAxis().bins(); ++k )
310  s << binMeanX( i, j, k ) << ", " << binMeanY( i, j, k ) << ", " << binMeanZ( i, j, k ) << ", "
311  << binHeight( i, j, k ) << ", " << binError( i, j, k ) << std::endl;
312  s << std::endl;
313  return s;
314  }
315 
317  template <class INTERFACE, class IMPLEMENTATION>
318  int Generic3D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
319  TFile* f = TFile::Open( file_name, "RECREATE" );
320  Int_t nbytes = m_rep->Write();
321  f->Close();
322  return nbytes;
323  }
324 } // namespace Gaudi
325 
326 #ifdef __clang__
327 # pragma clang diagnostic pop
328 #elif defined( __GNUC__ )
329 # pragma GCC diagnostic pop
330 #endif
331 
332 #endif // GAUDIPI_GENERIC3D_H
Annotation.h
Gaudi::Generic3D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:267
Gaudi::Generic3D::meanX
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:168
std::string
STL class.
Gaudi::Generic3D::coordToIndexY
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:189
Gaudi::Generic3D::rmsY
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:177
Gaudi::Generic3D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:73
Gaudi::Generic3D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic3D.h:63
Gaudi::Generic3D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:230
Gaudi::Generic3D::scale
bool scale(double scaleFactor) override
Scale the weights and the errors of all the IHistogram's bins (in-range and out-of-range ones) by a g...
Definition: Generic3D.h:290
Gaudi::Generic3D::binHeight
double binHeight(int indexX, int indexY, int indexZ) const
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic3D.h:138
Gaudi::Generic3D::m_zAxis
Gaudi::Axis m_zAxis
Definition: Generic3D.h:226
Gaudi::Generic3D::sumAllBinHeights
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:277
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Generic3D::m_xAxis
Gaudi::Axis m_xAxis
Definition: Generic3D.h:224
Gaudi::Generic3D::equivalentBinEntries
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:282
std::unique_ptr::get
T get(T... args)
Gaudi::Generic3D::m_yAxis
Gaudi::Axis m_yAxis
Definition: Generic3D.h:225
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:26
Gaudi::Generic3D::zAxis
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:185
Gaudi::Generic3D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic3D.h:318
Gaudi::Generic3D::meanZ
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:173
Gaudi::Generic3D::Base
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:48
Gaudi::Generic3D::binHeightZ
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:157
Gaudi::Generic3D::Generic3D
Generic3D()=default
Default constructor.
Gaudi::HistogramBase
Definition: HistogramBase.h:32
Gaudi::Generic3D::rIndexX
int rIndexX(int index) const
Definition: Generic3D.h:92
Gaudi::Generic3D::binHeightX
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:143
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ostream
STL class.
Gaudi::Generic3D::xAxis
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:181
std::string::c_str
T c_str(T... args)
Gaudi::Generic3D::coordToIndexX
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:187
Gaudi::Generic3D::binHeightY
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:150
Gaudi::Generic3D::yAxis
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:183
Axis.h
Gaudi::Generic3D::Generic3D
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:54
Gaudi::Generic3D::rIndexY
int rIndexY(int index) const
Definition: Generic3D.h:93
HistogramBase.h
Gaudi::Generic3D::rmsX
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:175
Gaudi::Generic3D::meanY
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:171
std::runtime_error
STL class.
Gaudi::Generic3D::representation
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:58
Gaudi::Generic3D::binEntriesX
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:115
Gaudi::Generic3D::binEntriesY
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:122
Gaudi::Generic3D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:272
Gaudi::Generic3D::name
std::string name() const
object name
Definition: Generic3D.h:69
Gaudi::Generic3D::rmsZ
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:179
Gaudi::Generic3D::allEntries
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile.
Definition: Generic3D.h:257
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Generic3D::setName
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:246
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Generic3D::extraEntries
int extraEntries() const override
Definition: Generic3D.h:207
Gaudi::Generic3D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Gaudi::Generic3D::write
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:304
Gaudi::Generic3D::binEntries
int binEntries(int indexX, int indexY, int indexZ) const override
Number of entries in the corresponding bin (ie the number of times fill was calle d for this bin).
Definition: Generic3D.h:109
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:29
Gaudi::Generic3D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:262
Gaudi::Generic3D::print
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:296
Gaudi::Generic3D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:252
Gaudi::Generic3D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:86
std::endl
T endl(T... args)
Gaudi::Generic3D::coordToIndexZ
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:191
Gaudi::Generic3D::rIndexZ
int rIndexZ(int index) const
Definition: Generic3D.h:94
Gaudi::Generic3D::annotation
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:75
Gaudi::Generic3D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:238
Gaudi::Generic3D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic3D.h:228
Gaudi::Generic3D::add
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:199
Gaudi::Generic3D::binMeanX
double binMeanX(int indexX, int, int) const override
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:97
Gaudi::Generic3D::binMeanY
double binMeanY(int, int indexY, int) const override
The weighted mean along the y axis of a given bin.
Definition: Generic3D.h:101
Gaudi::Generic3D
Definition: Generic3D.h:46
Gaudi::Generic3D::binEntriesZ
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:130
Gaudi::Generic3D::m_classType
std::string m_classType
Definition: Generic3D.h:232
std::unique_ptr< IMPLEMENTATION >
Gaudi::Generic3D::binError
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:164
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Generic3D::title
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:65
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::Generic3D::binMeanZ
double binMeanZ(int, int, int indexZ) const override
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:105