The Gaudi Framework  v30r3 (a5ef0a68)
Generic2D.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_GENERIC2D_H
2 #define GAUDISVC_GENERIC2D_H 1
3 
4 #include "AIDA/IProfile2D.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 <class INTERFACE, class IMPLEMENTATION>
36  class GAUDI_API Generic2D : virtual public INTERFACE, virtual public HistogramBase
37  {
38  public:
40 
41  Generic2D() = default;
42 
43  protected:
45  Generic2D( IMPLEMENTATION* p ) : m_rep( p ) {}
46 
47  public:
49  TObject* representation() const override { return m_rep.get(); }
51  void adoptRepresentation( TObject* rep ) override;
53  std::string title() const override { return m_annotation.value( "Title" ); }
55  bool setTitle( const std::string& title ) override;
57  std::string name() const { return m_annotation.value( "Name" ); }
59  bool setName( const std::string& newName );
61  AIDA::IAnnotation& annotation() override { return m_annotation; }
63  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
64 
66  const AIDA::IAxis& xAxis() const override { return m_xAxis; }
68  const AIDA::IAxis& yAxis() const override { return m_yAxis; }
70  virtual int rIndexX( int index ) const { return m_xAxis.rIndex( index ); }
72  virtual int rIndexY( int index ) const { return m_yAxis.rIndex( index ); }
73 
75  int entries() const override;
77  int allEntries() const override;
79  int extraEntries() 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 
92  double binMeanX( int indexX, int indexY ) const override;
94  double binMeanY( int indexX, int indexY ) const override;
96  int binEntries( int indexX, int indexY ) const override;
98  int binEntriesX( int indexX ) const override;
100  int binEntriesY( int indexY ) const override;
102  double binHeight( int indexX, int indexY ) const override;
104  double binHeightX( int indexX ) const override;
106  double binHeightY( int indexY ) const override;
108  double binError( int indexX, int indexY ) const override;
110  virtual double binRms( int indexX, int indexY ) const;
112  double meanX() const override;
114  double meanY() const override;
116  double rmsX() const override;
118  double rmsY() const override;
120  int coordToIndexX( double coordX ) const override;
122  int coordToIndexY( double coordY ) const override;
124  virtual double equivalentBinEntries() const;
127  virtual bool scale( double scaleFactor );
129  bool add( const INTERFACE& h ) override;
130  // overwrite reset
131  bool reset() override;
133  void* cast( const std::string& className ) const override;
135  const std::string& userLevelClassType() const { return m_classType; }
137  int dimension() const override { return 2; }
139  std::ostream& print( std::ostream& s ) const override;
141  std::ostream& write( std::ostream& s ) const override;
143  int write( const char* file_name ) const override;
144 
145  protected:
157  int m_sumEntries = 0;
158  };
159 
160  template <class INTERFACE, class IMPLEMENTATION>
162  {
163  m_rep->SetTitle( title.c_str() );
164  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
165  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
166  return true;
167  }
168 
169  template <class INTERFACE, class IMPLEMENTATION>
171  {
172  m_rep->SetName( newName.c_str() );
173  m_annotation.setValue( "Name", newName );
174  return true;
175  }
176 
177  template <class INTERFACE, class IMPLEMENTATION>
179  {
180  return m_rep->GetEntries();
181  }
182 
183  template <class INTERFACE, class IMPLEMENTATION>
185  {
186  return m_rep->GetEntries();
187  }
188 
189  template <class INTERFACE, class IMPLEMENTATION>
191  {
192  return m_rep->GetMinimum();
193  }
194 
195  template <class INTERFACE, class IMPLEMENTATION>
197  {
198  return m_rep->GetMaximum();
199  }
200 
201  template <class INTERFACE, class IMPLEMENTATION>
203  {
204  return m_rep->GetSumOfWeights();
205  }
206 
207  template <class INTERFACE, class IMPLEMENTATION>
209  {
210  return m_rep->GetSum();
211  }
212 
213  template <class INTERFACE, class IMPLEMENTATION>
214  double Generic2D<INTERFACE, IMPLEMENTATION>::binRms( int indexX, int indexY ) const
215  {
216  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
217  }
218 
219  template <class INTERFACE, class IMPLEMENTATION>
220  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanX( int indexX, int ) const
221  {
222  return m_rep->GetXaxis()->GetBinCenter( rIndexX( indexX ) );
223  }
224 
225  template <class INTERFACE, class IMPLEMENTATION>
226  double Generic2D<INTERFACE, IMPLEMENTATION>::binMeanY( int, int indexY ) const
227  {
228  return m_rep->GetYaxis()->GetBinCenter( rIndexY( indexY ) );
229  }
230 
231  template <class INTERFACE, class IMPLEMENTATION>
233  {
234  int n = 0;
235  for ( int iY = -2; iY < yAxis().bins(); ++iY ) n += binEntries( index, iY );
236  return n;
237  }
238 
239  template <class INTERFACE, class IMPLEMENTATION>
241  {
242  int n = 0;
243  for ( int iX = -2; iX < xAxis().bins(); ++iX ) n += binEntries( iX, index );
244  return n;
245  }
246 
247  template <class INTERFACE, class IMPLEMENTATION>
248  double Generic2D<INTERFACE, IMPLEMENTATION>::binHeight( int indexX, int indexY ) const
249  {
250  return m_rep->GetBinContent( rIndexX( indexX ), rIndexY( indexY ) );
251  }
252 
253  template <class INTERFACE, class IMPLEMENTATION>
255  {
256  double s = 0;
257  for ( int iY = -2; iY < yAxis().bins(); ++iY ) {
258  s += binHeight( index, iY );
259  }
260  return s;
261  }
262 
263  template <class INTERFACE, class IMPLEMENTATION>
265  {
266  double s = 0;
267  for ( int iX = -2; iX < xAxis().bins(); ++iX ) s += binHeight( iX, index );
268  return s;
269  }
270 
271  template <class INTERFACE, class IMPLEMENTATION>
272  double Generic2D<INTERFACE, IMPLEMENTATION>::binError( int indexX, int indexY ) const
273  {
274  return m_rep->GetBinError( rIndexX( indexX ), rIndexY( indexY ) );
275  }
276 
277  template <class INTERFACE, class IMPLEMENTATION>
279  {
280  return m_rep->GetMean( 1 );
281  }
282 
283  template <class INTERFACE, class IMPLEMENTATION>
285  {
286  return m_rep->GetMean( 2 );
287  }
288 
289  template <class INTERFACE, class IMPLEMENTATION>
291  {
292  return m_rep->GetRMS( 1 );
293  }
294 
295  template <class INTERFACE, class IMPLEMENTATION>
297  {
298  return m_rep->GetRMS( 2 );
299  }
300 
301  template <class INTERFACE, class IMPLEMENTATION>
303  {
304  return xAxis().coordToIndex( coord );
305  }
306 
307  template <class INTERFACE, class IMPLEMENTATION>
309  {
310  return yAxis().coordToIndex( coord );
311  }
312 
313  template <class INTERFACE, class IMPLEMENTATION>
314  bool Generic2D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& hist )
315  {
316  const Base* p = dynamic_cast<const Base*>( &hist );
317  if ( !p ) throw std::runtime_error( "Cannot add profile histograms of different implementations." );
318  m_rep->Add( p->m_rep.get() );
319  return true;
320  }
321 
322  template <class INTERFACE, class IMPLEMENTATION>
324  {
325  return binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
326  binEntries( AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN ) +
327  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::UNDERFLOW_BIN ) +
328  binEntries( AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN );
329  }
330 
331  template <class INTERFACE, class IMPLEMENTATION>
333  {
334  if ( sumBinHeights() <= 0 ) return 0;
335  Stat_t stats[11]; // cover up to 3D...
336  m_rep->GetStats( stats );
337  return stats[0] * stats[0] / stats[1];
338  }
339 
340  template <class INTERFACE, class IMPLEMENTATION>
342  {
343  m_rep->Scale( scaleFactor );
344  return true;
345  }
346 
347  template <class INTERFACE, class IMPLEMENTATION>
349  {
350  m_sumEntries = 0;
351  m_rep->Reset();
352  return true;
353  }
354 
355  template <class INTERFACE, class IMPLEMENTATION>
357  {
359  m_rep->Print( "all" );
360  return s;
361  }
362 
364  template <class INTERFACE, class IMPLEMENTATION>
366  {
367  s << std::endl << "2D Histogram Table: " << std::endl;
368  s << "BinX, BinY, Height, Error " << std::endl;
369  for ( int i = 0; i < xAxis().bins(); ++i ) {
370  for ( int j = 0; j < yAxis().bins(); ++j ) {
371  s << binMeanX( i, j ) << ", " << binMeanY( i, j ) << ", " << binHeight( i, j ) << ", " << binError( i, j )
372  << std::endl;
373  }
374  }
375  s << std::endl;
376  return s;
377  }
378 
380  template <class INTERFACE, class IMPLEMENTATION>
381  int Generic2D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const
382  {
383  TFile* f = TFile::Open( file_name, "RECREATE" );
384  Int_t nbytes = m_rep->Write();
385  f->Close();
386  return nbytes;
387  }
388 }
389 
390 #ifdef __clang__
391 #pragma clang diagnostic pop
392 #elif defined( __GNUC__ ) && __GNUC__ >= 5
393 #pragma GCC diagnostic pop
394 #endif
395 
396 #endif // GAUDIPI_GENERIC2D_H
int coordToIndexX(double coordX) const override
Convenience method, equivalent to xAxis().coordToIndex(coord).
Definition: Generic2D.h:302
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:332
double rmsX() const override
Returns the rms of the profile as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:290
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic2D.h:323
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic2D.h:137
T endl(T...args)
Generic2D(IMPLEMENTATION *p)
constructor
Definition: Generic2D.h:45
bool reset() override
Definition: Generic2D.h:348
double binMeanY(int indexX, int indexY) const override
The weighted mean along y of a given bin.
Definition: Generic2D.h:226
virtual bool scale(double scaleFactor)
Scale the weights and the errors of all the IHistogram&#39;s bins (in-range and out-of-range ones) by a g...
Definition: Generic2D.h:341
double rmsY() const override
Returns the rms of the profile as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:296
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic2D.h:196
std::string name() const
object name
Definition: Generic2D.h:57
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic2D.h:170
STL class.
virtual int rIndexX(int index) const
operator methods
Definition: Generic2D.h:70
int binEntriesX(int indexX) const override
Equivalent to projectionX().binEntries(indexX).
Definition: Generic2D.h:232
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:202
double binHeightY(int indexY) const override
Equivalent to projectionY().binHeight(indexY).
Definition: Generic2D.h:264
double binHeightX(int indexX) const override
Equivalent to projectionX().binHeight(indexX).
Definition: Generic2D.h:254
std::string m_classType
class type
Definition: Generic2D.h:155
Axis m_xAxis
X axis member.
Definition: Generic2D.h:147
Generic2D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic2D.h:39
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic2D.h:184
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:17
int coordToIndexY(double coordY) const override
Convenience method, equivalent to yAxis().coordToIndex(coord).
Definition: Generic2D.h:308
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic2D.h:85
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic2D.h:356
T get(T...args)
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic2D.h:63
const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic2D.h:135
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic2D.h:61
double binError(int indexX, int indexY) const override
The error on this bin.
Definition: Generic2D.h:272
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: Generic2D.h:151
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:161
TObject * representation() const override
ROOT object implementation.
Definition: Generic2D.h:49
double meanX() const override
Returns the mean of the profile, as calculated on filling-time projected on the X axis...
Definition: Generic2D.h:278
T c_str(T...args)
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:149
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic2D.h:365
string s
Definition: gaudirun.py:253
double binMeanX(int indexX, int indexY) const override
The weighted mean along x of a given bin.
Definition: Generic2D.h:220
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic2D.h:208
virtual double binRms(int indexX, int indexY) const
The spread (RMS) of this bin.
Definition: Generic2D.h:214
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:153
int binEntriesY(int indexY) const override
Equivalent to projectionY().binEntries(indexY).
Definition: Generic2D.h:240
int entries() const override
Get the number or all the entries.
Definition: Generic2D.h:178
virtual int rIndexY(int index) const
operator methods
Definition: Generic2D.h:72
double meanY() const override
Returns the mean of the profile, as calculated on filling-time projected on the Y axis...
Definition: Generic2D.h:284
#define GAUDI_API
Definition: Kernel.h:104
std::string title() const override
Get the title of the object.
Definition: Generic2D.h:53
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1
An IAxis represents a binned histogram axis.
Definition: Axis.h:30
bool add(const INTERFACE &h) override
Modifies this profile by adding the contents of profile to it.
Definition: Generic2D.h:314
const AIDA::IAxis & yAxis() const override
Return the Y axis.
Definition: Generic2D.h:68
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic2D.h:190
const AIDA::IAxis & xAxis() const override
Return the X axis.
Definition: Generic2D.h:66
double binHeight(int indexX, int indexY) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic2D.h:248
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic2D.h:36