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