The Gaudi Framework  v29r0 (ff2e7097)
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  ~Generic3D() override = default;
51  TObject* representation() const override { return m_rep.get(); }
53  void adoptRepresentation( TObject* rep ) override;
54 
56  int dimension() const override { return 3; }
58  std::string title() const override { return m_annotation.value( "Title" ); }
60  bool setTitle( const std::string& title ) override;
62  std::string name() const { return m_annotation.value( "Name" ); }
64  bool setName( const std::string& newName );
66  AIDA::IAnnotation& annotation() override { return m_annotation; }
68  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
69 
71  int entries() const override;
73  int allEntries() const override;
75  double sumBinHeights() const override;
77  double sumAllBinHeights() const override;
79  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
81  double minBinHeight() const override;
83  double maxBinHeight() const override;
84 
85  int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
86  int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
87  int rIndexZ( int index ) const { return m_zAxis.rIndex( index ); }
88 
90  double binMeanX( int indexX, int, int ) const override
91  {
92  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
93  }
95  double binMeanY( int, int indexY, int ) const override
96  {
97  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
98  }
100  double binMeanZ( int, int, int indexZ ) const override
101  {
102  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexZ ) );
103  }
105  int binEntries( int indexX, int indexY, int indexZ ) const override
106  {
107  if ( binHeight( indexX, indexY, indexZ ) <= 0 ) return 0;
108  double xx = binHeight( indexX, indexY, indexZ ) / binError( indexX, indexY, indexZ );
109  return int( xx * xx + 0.5 );
110  }
112  int binEntriesX( int index ) const override
113  {
114  int n = 0;
115  for ( int i = -2; i < yAxis().bins(); ++i )
116  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( index, i, j );
117  return n;
118  }
120  int binEntriesY( int index ) const override
121  {
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  {
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  {
140  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
141  }
142 
144  double binHeightX( int index ) const override
145  {
146  double s = 0;
147  for ( int i = -2; i < yAxis().bins(); ++i )
148  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( index, i, j );
149  return s;
150  }
152  double binHeightY( int index ) const override
153  {
154  double s = 0;
155  for ( int i = -2; i < xAxis().bins(); ++i )
156  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( i, index, j );
157  return s;
158  }
160  double binHeightZ( int index ) const override
161  {
162  double s = 0;
163  for ( int i = -2; i < xAxis().bins(); ++i )
164  for ( int j = -2; j < yAxis().bins(); ++j ) s += binHeight( i, j, index );
165  return s;
166  }
168  double binError( int indexX, int indexY, int indexZ ) const override
169  {
170  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
171  }
173  double meanX() const override { return m_rep->GetMean( 1 ); }
174 
176  double meanY() const override { return m_rep->GetMean( 2 ); }
178  double meanZ() const override { return m_rep->GetMean( 3 ); }
180  double rmsX() const override { return m_rep->GetRMS( 1 ); }
182  double rmsY() const override { return m_rep->GetRMS( 2 ); }
184  double rmsZ() const override { return m_rep->GetRMS( 3 ); }
186  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
188  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
190  const AIDA::IAxis& zAxis() const override { return m_zAxis; }
192  int coordToIndexX( double coord ) const override { return xAxis().coordToIndex( coord ); }
194  int coordToIndexY( double coord ) const override { return yAxis().coordToIndex( coord ); }
196  int coordToIndexZ( double coord ) const override { return zAxis().coordToIndex( coord ); }
197 
199  double equivalentBinEntries() const override;
202  bool scale( double scaleFactor ) override;
204  bool add( const INTERFACE& hist ) override
205  {
206  const Base* p = dynamic_cast<const Base*>( &hist );
207  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
208  m_rep->Add( p->m_rep.get() );
209  return true;
210  }
211 
212  // overwrite extraentries
213  int extraEntries() const override
214  {
215  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
216  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
217  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
218  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
219  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
220  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
221  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
222  }
224  std::ostream& print( std::ostream& s ) const override;
226  std::ostream& write( std::ostream& s ) const override;
228  int write( const char* file_name ) const override;
229 
230  protected:
238  // class type
240  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
241  int m_sumEntries = 0;
242  }; // end class IHistogram3D
243 
244  template <class INTERFACE, class IMPLEMENTATION>
246  {
247  m_rep->SetTitle( title.c_str() );
248  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
249  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
250  return true;
251  }
252 
253  template <class INTERFACE, class IMPLEMENTATION>
255  {
256  m_rep->SetName( newName.c_str() );
257  m_annotation.setValue( "Name", newName );
258  return true;
259  }
260  template <class INTERFACE, class IMPLEMENTATION>
262  {
263  return m_rep->GetEntries();
264  }
265 
266  template <class INTERFACE, class IMPLEMENTATION>
268  {
269  return int( m_rep->GetEntries() );
270  }
271 
272  template <class INTERFACE, class IMPLEMENTATION>
274  {
275  return m_rep->GetMinimum();
276  }
277 
278  template <class INTERFACE, class IMPLEMENTATION>
280  {
281  return m_rep->GetMaximum();
282  }
283 
284  template <class INTERFACE, class IMPLEMENTATION>
286  {
287  return m_rep->GetSumOfWeights();
288  }
289 
290  template <class INTERFACE, class IMPLEMENTATION>
292  {
293  return m_rep->GetSum();
294  }
295 
296  template <class INTERFACE, class IMPLEMENTATION>
298  {
299  if ( sumBinHeights() <= 0 ) return 0;
300  Stat_t stats[11]; // cover up to 3D...
301  m_rep->GetStats( stats );
302  return stats[0] * stats[0] / stats[1];
303  }
304 
305  template <class INTERFACE, class IMPLEMENTATION>
307  {
308  m_rep->Scale( scaleFactor );
309  return true;
310  }
311 
312  template <class INTERFACE, class IMPLEMENTATION>
314  {
316  m_rep->Print( "all" );
317  return s;
318  }
319 
321  template <class INTERFACE, class IMPLEMENTATION>
323  {
324  s << "\n3D Histogram Table: " << std::endl;
325  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
326  for ( int i = 0; i < xAxis().bins(); ++i )
327  for ( int j = 0; j < yAxis().bins(); ++j )
328  for ( int k = 0; k < zAxis().bins(); ++k )
329  s << binMeanX( i, j, k ) << ", " << binMeanY( i, j, k ) << ", " << binMeanZ( i, j, k ) << ", "
330  << binHeight( i, j, k ) << ", " << binError( i, j, k ) << std::endl;
331  s << std::endl;
332  return s;
333  }
334 
336  template <class INTERFACE, class IMPLEMENTATION>
337  int Generic3D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const
338  {
339  TFile* f = TFile::Open( file_name, "RECREATE" );
340  Int_t nbytes = m_rep->Write();
341  f->Close();
342  return nbytes;
343  }
344 }
345 
346 #ifdef __clang__
347 #pragma clang diagnostic pop
348 #elif defined( __GNUC__ ) && __GNUC__ >= 5
349 #pragma GCC diagnostic pop
350 #endif
351 
352 #endif // GAUDIPI_GENERIC3D_H
int rIndexZ(int index) const
Definition: Generic3D.h:87
int rIndexX(int index) const
Definition: Generic3D.h:85
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:66
Gaudi::Axis m_xAxis
Definition: Generic3D.h:231
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:178
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:68
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:120
Gaudi::Axis m_yAxis
Definition: Generic3D.h:232
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:279
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:261
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:313
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:95
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:188
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:190
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:184
std::string name() const
object name
Definition: Generic3D.h:62
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:204
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:291
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:105
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:285
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:152
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:160
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:306
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:254
Gaudi::Axis m_zAxis
Definition: Generic3D.h:233
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:245
int rIndexY(int index) const
Definition: Generic3D.h:86
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:51
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:237
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
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:58
T get(T...args)
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:273
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:112
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:235
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:194
void print(string text)
Definition: mergesort.cpp:30
T c_str(T...args)
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:186
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:182
std::string m_classType
Definition: Generic3D.h:239
string s
Definition: gaudirun.py:253
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:180
double binMeanZ(int, int, int indexZ) const override
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:100
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:196
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic3D.h:56
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:176
double binMeanX(int indexX, int, int) const override
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:90
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:192
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:322
#define GAUDI_API
Definition: Kernel.h:110
int extraEntries() const override
Definition: Generic3D.h:213
STL class.
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:297
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic3D.h:267
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:144
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:168
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:173
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:129
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:79