Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
34  template <typename INTERFACE, typename IMPLEMENTATION>
35  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase {
36  public:
39  Generic3D() = default;
40 
41  protected:
43  Generic3D( IMPLEMENTATION* p ) : m_rep( p ) {}
44 
45  public:
47  TObject* representation() const override { return m_rep.get(); }
49  void adoptRepresentation( TObject* rep ) override;
50 
52  int dimension() const override { return 3; }
54  std::string title() const override { return m_annotation.value( "Title" ); }
56  bool setTitle( const std::string& title ) override;
58  std::string name() const { return m_annotation.value( "Name" ); }
60  bool setName( const std::string& newName );
62  AIDA::IAnnotation& annotation() override { return m_annotation; }
64  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
65 
67  int entries() const override;
69  int allEntries() const override;
71  double sumBinHeights() const override;
73  double sumAllBinHeights() const override;
75  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
77  double minBinHeight() const override;
79  double maxBinHeight() const override;
80 
81  int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
82  int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
83  int rIndexZ( int index ) const { return m_zAxis.rIndex( index ); }
84 
86  double binMeanX( int indexX, int, int ) const override {
87  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
88  }
90  double binMeanY( int, int indexY, int ) const override {
91  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
92  }
94  double binMeanZ( int, int, int indexZ ) const override {
95  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexZ ) );
96  }
98  int binEntries( int indexX, int indexY, int indexZ ) const override {
99  if ( binHeight( indexX, indexY, indexZ ) <= 0 ) return 0;
100  double xx = binHeight( indexX, indexY, indexZ ) / binError( indexX, indexY, indexZ );
101  return int( xx * xx + 0.5 );
102  }
104  int binEntriesX( int index ) const override {
105  int n = 0;
106  for ( int i = -2; i < yAxis().bins(); ++i )
107  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( index, i, j );
108  return n;
109  }
111  int binEntriesY( int index ) const override {
112  int n = 0;
113  for ( int i = -2; i < xAxis().bins(); ++i )
114  for ( int j = -2; j < zAxis().bins(); ++j ) n += binEntries( i, index, j );
115  return n;
116  }
117 
119  int binEntriesZ( int index ) const override {
120  int n = 0;
121  for ( int i = -2; i < xAxis().bins(); ++i )
122  for ( int j = -2; j < yAxis().bins(); ++j ) n += binEntries( i, j, index );
123  return n;
124  }
125 
127  double binHeight( int indexX, int indexY, int indexZ ) const {
128  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
129  }
130 
132  double binHeightX( int index ) const override {
133  double s = 0;
134  for ( int i = -2; i < yAxis().bins(); ++i )
135  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( index, i, j );
136  return s;
137  }
139  double binHeightY( int index ) const override {
140  double s = 0;
141  for ( int i = -2; i < xAxis().bins(); ++i )
142  for ( int j = -2; j < zAxis().bins(); ++j ) s += binHeight( i, index, j );
143  return s;
144  }
146  double binHeightZ( int index ) const override {
147  double s = 0;
148  for ( int i = -2; i < xAxis().bins(); ++i )
149  for ( int j = -2; j < yAxis().bins(); ++j ) s += binHeight( i, j, index );
150  return s;
151  }
153  double binError( int indexX, int indexY, int indexZ ) const override {
154  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ), rIndexZ( indexZ ) );
155  }
157  double meanX() const override { return m_rep->GetMean( 1 ); }
158 
160  double meanY() const override { return m_rep->GetMean( 2 ); }
162  double meanZ() const override { return m_rep->GetMean( 3 ); }
164  double rmsX() const override { return m_rep->GetRMS( 1 ); }
166  double rmsY() const override { return m_rep->GetRMS( 2 ); }
168  double rmsZ() const override { return m_rep->GetRMS( 3 ); }
170  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
172  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
174  const AIDA::IAxis& zAxis() const override { return m_zAxis; }
176  int coordToIndexX( double coord ) const override { return xAxis().coordToIndex( coord ); }
178  int coordToIndexY( double coord ) const override { return yAxis().coordToIndex( coord ); }
180  int coordToIndexZ( double coord ) const override { return zAxis().coordToIndex( coord ); }
181 
183  double equivalentBinEntries() const override;
186  bool scale( double scaleFactor ) override;
188  bool add( const INTERFACE& hist ) override {
189  const Base* p = dynamic_cast<const Base*>( &hist );
190  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
191  m_rep->Add( p->m_rep.get() );
192  return true;
193  }
194 
195  // overwrite extraentries
196  int extraEntries() const override {
197  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
198  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
199  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
200  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
201  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
202  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
203  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
204  }
206  std::ostream& print( std::ostream& s ) const override;
208  std::ostream& write( std::ostream& s ) const override;
210  int write( const char* file_name ) const override;
211 
212  protected:
220  // class type
222  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
223  int m_sumEntries = 0;
224  }; // end class IHistogram3D
225 
226  template <class INTERFACE, class IMPLEMENTATION>
228  m_rep->SetTitle( title.c_str() );
229  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
230  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
231  return true;
232  }
233 
234  template <class INTERFACE, class IMPLEMENTATION>
236  m_rep->SetName( newName.c_str() );
237  m_annotation.setValue( "Name", newName );
238  return true;
239  }
240  template <class INTERFACE, class IMPLEMENTATION>
242  return m_rep->GetEntries();
243  }
244 
245  template <class INTERFACE, class IMPLEMENTATION>
247  return int( m_rep->GetEntries() );
248  }
249 
250  template <class INTERFACE, class IMPLEMENTATION>
252  return m_rep->GetMinimum();
253  }
254 
255  template <class INTERFACE, class IMPLEMENTATION>
257  return m_rep->GetMaximum();
258  }
259 
260  template <class INTERFACE, class IMPLEMENTATION>
262  return m_rep->GetSumOfWeights();
263  }
264 
265  template <class INTERFACE, class IMPLEMENTATION>
267  return m_rep->GetSum();
268  }
269 
270  template <class INTERFACE, class IMPLEMENTATION>
272  if ( sumBinHeights() <= 0 ) return 0;
273  Stat_t stats[11]; // cover up to 3D...
274  m_rep->GetStats( stats );
275  return stats[0] * stats[0] / stats[1];
276  }
277 
278  template <class INTERFACE, class IMPLEMENTATION>
279  bool Generic3D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
280  m_rep->Scale( scaleFactor );
281  return true;
282  }
283 
284  template <class INTERFACE, class IMPLEMENTATION>
287  m_rep->Print( "all" );
288  return s;
289  }
290 
292  template <class INTERFACE, class IMPLEMENTATION>
294  s << "\n3D Histogram Table: " << std::endl;
295  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
296  for ( int i = 0; i < xAxis().bins(); ++i )
297  for ( int j = 0; j < yAxis().bins(); ++j )
298  for ( int k = 0; k < zAxis().bins(); ++k )
299  s << binMeanX( i, j, k ) << ", " << binMeanY( i, j, k ) << ", " << binMeanZ( i, j, k ) << ", "
300  << binHeight( i, j, k ) << ", " << binError( i, j, k ) << std::endl;
301  s << std::endl;
302  return s;
303  }
304 
306  template <class INTERFACE, class IMPLEMENTATION>
307  int Generic3D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
308  TFile* f = TFile::Open( file_name, "RECREATE" );
309  Int_t nbytes = m_rep->Write();
310  f->Close();
311  return nbytes;
312  }
313 } // namespace Gaudi
314 
315 #ifdef __clang__
316 # pragma clang diagnostic pop
317 #elif defined( __GNUC__ ) && __GNUC__ >= 5
318 # pragma GCC diagnostic pop
319 #endif
320 
321 #endif // GAUDIPI_GENERIC3D_H
int rIndexZ(int index) const
Definition: Generic3D.h:83
int rIndexX(int index) const
Definition: Generic3D.h:81
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:62
Gaudi::Axis m_xAxis
Definition: Generic3D.h:213
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:162
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:64
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:111
Gaudi::Axis m_yAxis
Definition: Generic3D.h:214
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:256
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:241
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:285
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:90
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:172
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:174
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:168
std::string name() const
object name
Definition: Generic3D.h:58
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:188
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:266
STL class.
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:37
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:98
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:261
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:139
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:146
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:16
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:279
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:235
Gaudi::Axis m_zAxis
Definition: Generic3D.h:215
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:227
int rIndexY(int index) const
Definition: Generic3D.h:82
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:47
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:219
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:127
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:54
T get(T...args)
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:251
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:104
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
Definition: HistogramBase.h:22
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic3D.h:217
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:178
T c_str(T...args)
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:170
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:166
std::string m_classType
Definition: Generic3D.h:221
string s
Definition: gaudirun.py:312
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:164
double binMeanZ(int, int, int indexZ) const override
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:94
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:180
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic3D.h:52
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:160
double binMeanX(int indexX, int, int) const override
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:86
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:176
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:293
#define GAUDI_API
Definition: Kernel.h:71
int extraEntries() const override
Definition: Generic3D.h:196
STL class.
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:271
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic3D.h:246
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:132
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:153
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:43
Helper functions to set/get the application return code.
Definition: __init__.py:1
An IAxis represents a binned histogram axis.
Definition: Axis.h:29
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:157
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:119
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic3D.h:35
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:75