The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Generic3D.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/IHistogram3D.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 <typename INTERFACE, typename IMPLEMENTATION>
45  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase {
46  public:
49  Generic3D() = default;
50 
51  protected:
53  Generic3D( IMPLEMENTATION* p ) : m_rep( p ) {}
54 
55  public:
57  TObject* representation() const override { return m_rep.get(); }
59  void adoptRepresentation( TObject* rep ) override;
60 
62  int dimension() const override { return 3; }
64  std::string title() const override { return m_annotation.value( "Title" ); }
66  bool setTitle( const std::string& title ) override;
68  std::string name() const { return m_annotation.value( "Name" ); }
70  bool setName( const std::string& newName );
72  AIDA::IAnnotation& annotation() override { return m_annotation; }
74  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
75 
77  int entries() const override;
79  int allEntries() const override;
81  double sumBinHeights() const override;
83  double sumAllBinHeights() const override;
85  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
87  double minBinHeight() const override;
89  double maxBinHeight() const override;
90 
91  int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
92  int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
93  int rIndexZ( int index ) const { return m_zAxis.rIndex( index ); }
94 
96  double binMeanX( int indexX, int, int ) const override {
97  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
98  }
100  double binMeanY( int, int indexY, int ) const override {
101  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
102  }
104  double binMeanZ( int, int, int indexZ ) const override {
105  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexZ ) );
106  }
108  int binEntries( int indexX, int indexY, int indexZ ) const override {
109  if ( binHeight( indexX, indexY, indexZ ) <= 0 ) return 0;
110  double xx = binHeight( indexX, indexY, indexZ ) / binError( indexX, indexY, indexZ );
111  return int( xx * xx + 0.5 );
112  }
114  int binEntriesX( int index ) const override {
115  int n = 0;
116  for ( int i = -2; i < yAxis().bins(); ++i )
117  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( index, i, j );
118  return n;
119  }
121  int binEntriesY( int index ) const override {
122  int n = 0;
123  for ( int i = -2; i < xAxis().bins(); ++i )
124  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( i, index, j );
125  return n;
126  }
127 
129  int binEntriesZ( int index ) const override {
130  int n = 0;
131  for ( int i = -2; i < xAxis().bins(); ++i )
132  for ( int j = -2; j < yAxis().bins(); ++j ) n += binEntries( i, j, index );
133  return n;
134  }
135 
137  double binHeight( int indexX, int indexY, int indexZ ) const {
138  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
139  }
140 
142  double binHeightX( int index ) const override {
143  double s = 0;
144  for ( int i = -2; i < yAxis().bins(); ++i )
145  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( index, i, j );
146  return s;
147  }
149  double binHeightY( int index ) const override {
150  double s = 0;
151  for ( int i = -2; i < xAxis().bins(); ++i )
152  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( i, index, j );
153  return s;
154  }
156  double binHeightZ( int index ) const override {
157  double s = 0;
158  for ( int i = -2; i < xAxis().bins(); ++i )
159  for ( int j = -2; j < yAxis().bins(); ++j ) s += binHeight( i, j, index );
160  return s;
161  }
163  double binError( int indexX, int indexY, int indexZ ) const override {
164  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
165  }
167  double meanX() const override { return m_rep->GetMean( 1 ); }
168 
170  double meanY() const override { return m_rep->GetMean( 2 ); }
172  double meanZ() const override { return m_rep->GetMean( 3 ); }
174  double rmsX() const override { return m_rep->GetRMS( 1 ); }
176  double rmsY() const override { return m_rep->GetRMS( 2 ); }
178  double rmsZ() const override { return m_rep->GetRMS( 3 ); }
180  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
182  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
184  const AIDA::IAxis& zAxis() const override { return m_zAxis; }
186  int coordToIndexX( double coord ) const override { return xAxis().coordToIndex( coord ); }
188  int coordToIndexY( double coord ) const override { return yAxis().coordToIndex( coord ); }
190  int coordToIndexZ( double coord ) const override { return zAxis().coordToIndex( coord ); }
191 
193  double equivalentBinEntries() const override;
196  bool scale( double scaleFactor ) override;
198  bool add( const INTERFACE& hist ) override {
199  const Base* p = dynamic_cast<const Base*>( &hist );
200  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
201  m_rep->Add( p->m_rep.get() );
202  return true;
203  }
204 
205  // overwrite extraentries
206  int extraEntries() const override {
207  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
208  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
209  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
210  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
211  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
212  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
213  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
214  }
216  std::ostream& print( std::ostream& s ) const override;
218  std::ostream& write( std::ostream& s ) const override;
220  int write( const char* file_name ) const override;
221 
222  protected:
229  std::unique_ptr<IMPLEMENTATION> m_rep;
230  // class type
231  std::string m_classType;
232  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
233  int m_sumEntries = 0;
234  }; // end class IHistogram3D
235 
236  template <class INTERFACE, class IMPLEMENTATION>
237  bool Generic3D<INTERFACE, IMPLEMENTATION>::setTitle( const std::string& title ) {
238  m_rep->SetTitle( title.c_str() );
239  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
240  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
241  return true;
242  }
243 
244  template <class INTERFACE, class IMPLEMENTATION>
245  bool Generic3D<INTERFACE, IMPLEMENTATION>::setName( const std::string& newName ) {
246  m_rep->SetName( newName.c_str() );
247  m_annotation.setValue( "Name", newName );
248  return true;
249  }
250  template <class INTERFACE, class IMPLEMENTATION>
252  return m_rep->GetEntries();
253  }
254 
255  template <class INTERFACE, class IMPLEMENTATION>
257  return int( m_rep->GetEntries() );
258  }
259 
260  template <class INTERFACE, class IMPLEMENTATION>
262  return m_rep->GetMinimum();
263  }
264 
265  template <class INTERFACE, class IMPLEMENTATION>
267  return m_rep->GetMaximum();
268  }
269 
270  template <class INTERFACE, class IMPLEMENTATION>
272  return m_rep->GetSumOfWeights();
273  }
274 
275  template <class INTERFACE, class IMPLEMENTATION>
277  return m_rep->GetSum();
278  }
279 
280  template <class INTERFACE, class IMPLEMENTATION>
282  if ( sumBinHeights() <= 0 ) return 0;
283  Stat_t stats[11]; // cover up to 3D...
284  m_rep->GetStats( stats );
285  return stats[0] * stats[0] / stats[1];
286  }
287 
288  template <class INTERFACE, class IMPLEMENTATION>
289  bool Generic3D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
290  m_rep->Scale( scaleFactor );
291  return true;
292  }
293 
294  template <class INTERFACE, class IMPLEMENTATION>
295  std::ostream& Generic3D<INTERFACE, IMPLEMENTATION>::print( std::ostream& s ) const {
297  m_rep->Print( "all" );
298  return s;
299  }
300 
302  template <class INTERFACE, class IMPLEMENTATION>
303  std::ostream& Generic3D<INTERFACE, IMPLEMENTATION>::write( std::ostream& s ) const {
304  s << "\n3D Histogram Table: " << std::endl;
305  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
306  for ( int i = 0; i < xAxis().bins(); ++i )
307  for ( int j = 0; j < yAxis().bins(); ++j )
308  for ( int k = 0; k < zAxis().bins(); ++k )
309  s << binMeanX( i, j, k ) << ", " << binMeanY( i, j, k ) << ", " << binMeanZ( i, j, k ) << ", "
310  << binHeight( i, j, k ) << ", " << binError( i, j, k ) << std::endl;
311  s << std::endl;
312  return s;
313  }
314 
316  template <class INTERFACE, class IMPLEMENTATION>
317  int Generic3D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
318  TFile* f = TFile::Open( file_name, "RECREATE" );
319  Int_t nbytes = m_rep->Write();
320  f->Close();
321  return nbytes;
322  }
323 } // namespace Gaudi
324 
325 #ifdef __clang__
326 # pragma clang diagnostic pop
327 #elif defined( __GNUC__ )
328 # pragma GCC diagnostic pop
329 #endif
Annotation.h
Gaudi::Generic3D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:266
Gaudi::Generic3D::meanX
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:167
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:188
Gaudi::Generic3D::rmsY
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:176
Gaudi::Generic3D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:72
Gaudi::Generic3D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic3D.h:62
Gaudi::Generic3D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:229
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:289
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:137
Gaudi::Generic3D::m_zAxis
Gaudi::Axis m_zAxis
Definition: Generic3D.h:225
Gaudi::Generic3D::sumAllBinHeights
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:276
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Generic3D::m_xAxis
Gaudi::Axis m_xAxis
Definition: Generic3D.h:223
Gaudi::Generic3D::equivalentBinEntries
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:281
Gaudi::Generic3D::m_yAxis
Gaudi::Axis m_yAxis
Definition: Generic3D.h:224
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:25
Gaudi::Generic3D::zAxis
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:184
Gaudi::Generic3D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic3D.h:317
Gaudi::Generic3D::meanZ
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:172
Gaudi::Generic3D::Base
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:47
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:156
Gaudi::Generic3D::Generic3D
Generic3D()=default
Default constructor.
Gaudi::HistogramBase
Definition: HistogramBase.h:31
Gaudi::Generic3D::rIndexX
int rIndexX(int index) const
Definition: Generic3D.h:91
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:142
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::Generic3D::xAxis
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:180
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:186
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:149
Gaudi::Generic3D::yAxis
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:182
Axis.h
Gaudi::Generic3D::Generic3D
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:53
Gaudi::Generic3D::rIndexY
int rIndexY(int index) const
Definition: Generic3D.h:92
HistogramBase.h
Gaudi::Generic3D::rmsX
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:174
Gaudi::Generic3D::meanY
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:170
Gaudi::Generic3D::representation
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:57
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:114
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:121
Gaudi::Generic3D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:271
Gaudi::Generic3D::name
std::string name() const
object name
Definition: Generic3D.h:68
Gaudi::Generic3D::rmsZ
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:178
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:256
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:245
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Generic3D::extraEntries
int extraEntries() const override
Definition: Generic3D.h:206
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:303
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:108
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:28
Gaudi::Generic3D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:261
Gaudi::Generic3D::print
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:295
Gaudi::Generic3D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:251
Gaudi::Generic3D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:85
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:190
Gaudi::Generic3D::rIndexZ
int rIndexZ(int index) const
Definition: Generic3D.h:93
Gaudi::Generic3D::annotation
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:74
Gaudi::Generic3D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:237
Gaudi::Generic3D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic3D.h:227
Gaudi::Generic3D::add
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:198
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:96
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:100
Gaudi::Generic3D
Definition: Generic3D.h:45
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:129
Gaudi::Generic3D::m_classType
std::string m_classType
Definition: Generic3D.h:231
Gaudi::Generic3D::binError
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:163
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Generic3D::title
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:64
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:104