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