The Gaudi Framework  v30r3 (a5ef0a68)
Generic3D.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_GENERIC3D_H
2 #define GAUDISVC_GENERIC3D_H 1
3 
4 #include "AIDA/IHistogram3D.h"
5 #include "Annotation.h"
6 #include "Axis.h"
8 #include "TFile.h"
9 #include <memory>
10 #include <stdexcept>
11 
12 // Hide warning message:
13 // warning: 'XYZ' overrides a member function but is not marked 'override'
14 #ifdef __clang__
15 #pragma clang diagnostic push
16 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
17 #elif defined( __GNUC__ ) && __GNUC__ >= 5
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wsuggest-override"
20 #endif
21 
22 namespace Gaudi
23 {
24 
35  template <typename INTERFACE, typename IMPLEMENTATION>
36  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase
37  {
38  public:
41  Generic3D() = default;
42 
43  protected:
45  Generic3D( IMPLEMENTATION* p ) : m_rep( p ) {}
46 
47  public:
49  TObject* representation() const override { return m_rep.get(); }
51  void adoptRepresentation( TObject* rep ) override;
52 
54  int dimension() const override { return 3; }
56  std::string title() const override { return m_annotation.value( "Title" ); }
58  bool setTitle( const std::string& title ) override;
60  std::string name() const { return m_annotation.value( "Name" ); }
62  bool setName( const std::string& newName );
64  AIDA::IAnnotation& annotation() override { return m_annotation; }
66  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
67 
69  int entries() const override;
71  int allEntries() const override;
73  double sumBinHeights() const override;
75  double sumAllBinHeights() const override;
77  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
79  double minBinHeight() const override;
81  double maxBinHeight() const override;
82 
83  int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
84  int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
85  int rIndexZ( int index ) const { return m_zAxis.rIndex( index ); }
86 
88  double binMeanX( int indexX, int, int ) const override
89  {
90  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
91  }
93  double binMeanY( int, int indexY, int ) const override
94  {
95  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
96  }
98  double binMeanZ( int, int, int indexZ ) const override
99  {
100  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexZ ) );
101  }
103  int binEntries( int indexX, int indexY, int indexZ ) const override
104  {
105  if ( binHeight( indexX, indexY, indexZ ) <= 0 ) return 0;
106  double xx = binHeight( indexX, indexY, indexZ ) / binError( indexX, indexY, indexZ );
107  return int( xx * xx + 0.5 );
108  }
110  int binEntriesX( int index ) const override
111  {
112  int n = 0;
113  for ( int i = -2; i < yAxis().bins(); ++i )
114  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( index, i, j );
115  return n;
116  }
118  int binEntriesY( int index ) const override
119  {
120  int n = 0;
121  for ( int i = -2; i < xAxis().bins(); ++i )
122  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( i, index, j );
123  return n;
124  }
125 
127  int binEntriesZ( int index ) const override
128  {
129  int n = 0;
130  for ( int i = -2; i < xAxis().bins(); ++i )
131  for ( int j = -2; j < yAxis().bins(); ++j ) n += binEntries( i, j, index );
132  return n;
133  }
134 
136  double binHeight( int indexX, int indexY, int indexZ ) const
137  {
138  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
139  }
140 
142  double binHeightX( int index ) const override
143  {
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  {
152  double s = 0;
153  for ( int i = -2; i < xAxis().bins(); ++i )
154  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( i, index, j );
155  return s;
156  }
158  double binHeightZ( int index ) const override
159  {
160  double s = 0;
161  for ( int i = -2; i < xAxis().bins(); ++i )
162  for ( int j = -2; j < yAxis().bins(); ++j ) s += binHeight( i, j, index );
163  return s;
164  }
166  double binError( int indexX, int indexY, int indexZ ) const override
167  {
168  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
169  }
171  double meanX() const override { return m_rep->GetMean( 1 ); }
172 
174  double meanY() const override { return m_rep->GetMean( 2 ); }
176  double meanZ() const override { return m_rep->GetMean( 3 ); }
178  double rmsX() const override { return m_rep->GetRMS( 1 ); }
180  double rmsY() const override { return m_rep->GetRMS( 2 ); }
182  double rmsZ() const override { return m_rep->GetRMS( 3 ); }
184  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
186  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
188  const AIDA::IAxis& zAxis() const override { return m_zAxis; }
190  int coordToIndexX( double coord ) const override { return xAxis().coordToIndex( coord ); }
192  int coordToIndexY( double coord ) const override { return yAxis().coordToIndex( coord ); }
194  int coordToIndexZ( double coord ) const override { return zAxis().coordToIndex( coord ); }
195 
197  double equivalentBinEntries() const override;
200  bool scale( double scaleFactor ) override;
202  bool add( const INTERFACE& hist ) override
203  {
204  const Base* p = dynamic_cast<const Base*>( &hist );
205  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
206  m_rep->Add( p->m_rep.get() );
207  return true;
208  }
209 
210  // overwrite extraentries
211  int extraEntries() const override
212  {
213  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
214  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
215  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
216  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
217  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
218  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
219  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
220  }
222  std::ostream& print( std::ostream& s ) const override;
224  std::ostream& write( std::ostream& s ) const override;
226  int write( const char* file_name ) const override;
227 
228  protected:
236  // class type
238  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
239  int m_sumEntries = 0;
240  }; // end class IHistogram3D
241 
242  template <class INTERFACE, class IMPLEMENTATION>
244  {
245  m_rep->SetTitle( title.c_str() );
246  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
247  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
248  return true;
249  }
250 
251  template <class INTERFACE, class IMPLEMENTATION>
253  {
254  m_rep->SetName( newName.c_str() );
255  m_annotation.setValue( "Name", newName );
256  return true;
257  }
258  template <class INTERFACE, class IMPLEMENTATION>
260  {
261  return m_rep->GetEntries();
262  }
263 
264  template <class INTERFACE, class IMPLEMENTATION>
266  {
267  return int( m_rep->GetEntries() );
268  }
269 
270  template <class INTERFACE, class IMPLEMENTATION>
272  {
273  return m_rep->GetMinimum();
274  }
275 
276  template <class INTERFACE, class IMPLEMENTATION>
278  {
279  return m_rep->GetMaximum();
280  }
281 
282  template <class INTERFACE, class IMPLEMENTATION>
284  {
285  return m_rep->GetSumOfWeights();
286  }
287 
288  template <class INTERFACE, class IMPLEMENTATION>
290  {
291  return m_rep->GetSum();
292  }
293 
294  template <class INTERFACE, class IMPLEMENTATION>
296  {
297  if ( sumBinHeights() <= 0 ) return 0;
298  Stat_t stats[11]; // cover up to 3D...
299  m_rep->GetStats( stats );
300  return stats[0] * stats[0] / stats[1];
301  }
302 
303  template <class INTERFACE, class IMPLEMENTATION>
305  {
306  m_rep->Scale( scaleFactor );
307  return true;
308  }
309 
310  template <class INTERFACE, class IMPLEMENTATION>
312  {
314  m_rep->Print( "all" );
315  return s;
316  }
317 
319  template <class INTERFACE, class IMPLEMENTATION>
321  {
322  s << "\n3D Histogram Table: " << std::endl;
323  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
324  for ( int i = 0; i < xAxis().bins(); ++i )
325  for ( int j = 0; j < yAxis().bins(); ++j )
326  for ( int k = 0; k < zAxis().bins(); ++k )
327  s << binMeanX( i, j, k ) << ", " << binMeanY( i, j, k ) << ", " << binMeanZ( i, j, k ) << ", "
328  << binHeight( i, j, k ) << ", " << binError( i, j, k ) << std::endl;
329  s << std::endl;
330  return s;
331  }
332 
334  template <class INTERFACE, class IMPLEMENTATION>
335  int Generic3D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const
336  {
337  TFile* f = TFile::Open( file_name, "RECREATE" );
338  Int_t nbytes = m_rep->Write();
339  f->Close();
340  return nbytes;
341  }
342 }
343 
344 #ifdef __clang__
345 #pragma clang diagnostic pop
346 #elif defined( __GNUC__ ) && __GNUC__ >= 5
347 #pragma GCC diagnostic pop
348 #endif
349 
350 #endif // GAUDIPI_GENERIC3D_H
int rIndexZ(int index) const
Definition: Generic3D.h:85
int rIndexX(int index) const
Definition: Generic3D.h:83
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:64
Gaudi::Axis m_xAxis
Definition: Generic3D.h:229
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:176
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:66
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:118
Gaudi::Axis m_yAxis
Definition: Generic3D.h:230
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:277
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:259
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:311
T endl(T...args)
double binMeanY(int, int indexY, int) const override
The weighted mean along the y axis of a given bin.
Definition: Generic3D.h:93
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:186
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:188
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:182
std::string name() const
object name
Definition: Generic3D.h:60
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:202
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:289
STL class.
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:39
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:103
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:283
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:150
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:158
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:17
bool scale(double scaleFactor) override
Scale the weights and the errors of all the IHistogram&#39;s bins (in-range and out-of-range ones) by a g...
Definition: Generic3D.h:304
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:252
Gaudi::Axis m_zAxis
Definition: Generic3D.h:231
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:243
int rIndexY(int index) const
Definition: Generic3D.h:84
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:49
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:235
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:136
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:56
T get(T...args)
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:271
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:110
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
Definition: HistogramBase.h:23
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic3D.h:233
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:192
T c_str(T...args)
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:184
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:180
std::string m_classType
Definition: Generic3D.h:237
string s
Definition: gaudirun.py:253
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:178
double binMeanZ(int, int, int indexZ) const override
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:98
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:194
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic3D.h:54
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:174
double binMeanX(int indexX, int, int) const override
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:88
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:190
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:320
#define GAUDI_API
Definition: Kernel.h:104
int extraEntries() const override
Definition: Generic3D.h:211
STL class.
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:295
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic3D.h:265
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:142
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:166
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:45
Helper functions to set/get the application return code.
Definition: __init__.py:1
An IAxis represents a binned histogram axis.
Definition: Axis.h:30
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:171
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:127
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic3D.h:36
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:77