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 <stdexcept>
5 #include <memory>
6 #include "AIDA/IProfile2D.h"
8 #include "Annotation.h"
9 #include "Axis.h"
10 #include "TFile.h"
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  protected:
42  Generic2D(IMPLEMENTATION* p) : m_rep(p) { }
43  public:
45  ~Generic2D() override = default;
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;
124  virtual bool scale( double scaleFactor );
126  bool add(const INTERFACE & h) override;
127  // overwrite reset
128  bool reset ( ) override;
130  void * cast(const std::string & className) const override;
132  const std::string& userLevelClassType() const { return m_classType; }
134  int dimension() const override { return 2; }
136  std::ostream& print( std::ostream& s ) const override;
138  std::ostream& write( std::ostream& s ) const override;
140  int write( const char* file_name ) const override;
141 
142  protected:
154  int m_sumEntries = 0;
155  };
156 
157  template <class INTERFACE, class IMPLEMENTATION>
159  m_rep->SetTitle(title.c_str());
160  if ( !annotation().addItem( "Title", title ) )
161  m_annotation.setValue( "Title" , title );
162  if ( !annotation().addItem( "title", title ) )
163  annotation().setValue( "title", title );
164  return true;
165  }
166 
167  template <class INTERFACE, class IMPLEMENTATION>
169  m_rep->SetName(newName.c_str());
170  m_annotation.setValue( "Name", newName );
171  return true;
172  }
173 
174  template <class INTERFACE, class IMPLEMENTATION>
176  return m_rep->GetEntries();
177  }
178 
179  template <class INTERFACE, class IMPLEMENTATION>
181  return m_rep->GetEntries();
182  }
183 
184  template <class INTERFACE, class IMPLEMENTATION>
186  return m_rep->GetMinimum();
187  }
188 
189  template <class INTERFACE, class IMPLEMENTATION>
191  return m_rep->GetMaximum();
192  }
193 
194  template <class INTERFACE, class IMPLEMENTATION>
196  return m_rep->GetSumOfWeights();
197  }
198 
199  template <class INTERFACE, class IMPLEMENTATION>
201  return m_rep->GetSum();
202  }
203 
204  template <class INTERFACE, class IMPLEMENTATION>
205  double Generic2D<INTERFACE,IMPLEMENTATION>::binRms(int indexX,int indexY) const {
206  return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY) );
207  }
208 
209  template <class INTERFACE, class IMPLEMENTATION>
210  double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanX(int indexX,int ) const {
211  return m_rep->GetXaxis()->GetBinCenter( rIndexX(indexX) );
212  }
213 
214  template <class INTERFACE, class IMPLEMENTATION>
215  double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanY(int,int indexY) const {
216  return m_rep->GetYaxis()->GetBinCenter( rIndexY(indexY) );
217  }
218 
219  template <class INTERFACE, class IMPLEMENTATION>
221  int n = 0;
222  for (int iY = -2; iY < yAxis().bins(); ++iY)
223  n += binEntries(index,iY);
224  return n;
225  }
226 
227  template <class INTERFACE, class IMPLEMENTATION>
229  int n = 0;
230  for (int iX = -2; iX < xAxis().bins(); ++iX)
231  n += binEntries(iX,index);
232  return n;
233  }
234 
235  template <class INTERFACE, class IMPLEMENTATION>
236  double Generic2D<INTERFACE,IMPLEMENTATION>::binHeight ( int indexX,int indexY ) const {
237  return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY) );
238  }
239 
240  template <class INTERFACE, class IMPLEMENTATION>
242  double s = 0;
243  for (int iY = -2; iY < yAxis().bins(); ++iY) {
244  s += binHeight(index,iY);
245  }
246  return s;
247  }
248 
249  template <class INTERFACE, class IMPLEMENTATION>
251  double s = 0;
252  for (int iX = -2; iX < xAxis().bins(); ++iX)
253  s += binHeight(iX,index);
254  return s;
255  }
256 
257  template <class INTERFACE, class IMPLEMENTATION>
258  double Generic2D<INTERFACE,IMPLEMENTATION>::binError(int indexX,int indexY) const {
259  return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ) );
260  }
261 
262  template <class INTERFACE, class IMPLEMENTATION>
264  return m_rep->GetMean(1);
265  }
266 
267  template <class INTERFACE, class IMPLEMENTATION>
269  return m_rep->GetMean(2);
270  }
271 
272  template <class INTERFACE, class IMPLEMENTATION>
274  return m_rep->GetRMS(1);
275  }
276 
277  template <class INTERFACE, class IMPLEMENTATION>
279  return m_rep->GetRMS(2);
280  }
281 
282  template <class INTERFACE, class IMPLEMENTATION>
284  return xAxis().coordToIndex(coord);
285  }
286 
287  template <class INTERFACE, class IMPLEMENTATION>
289  return yAxis().coordToIndex(coord);
290  }
291 
292  template <class INTERFACE, class IMPLEMENTATION>
293  bool Generic2D<INTERFACE,IMPLEMENTATION>::add ( const INTERFACE & hist ) {
294  const Base* p = dynamic_cast<const Base*>(&hist);
295  if ( !p ) throw std::runtime_error("Cannot add profile histograms of different implementations.");
296  m_rep->Add(p->m_rep.get());
297  return true;
298  }
299 
300  template <class INTERFACE, class IMPLEMENTATION>
302  return
303  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
304  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
305  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
306  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
307  }
308 
309  template <class INTERFACE, class IMPLEMENTATION>
311  if (sumBinHeights() <= 0) return 0;
312  Stat_t stats[11]; // cover up to 3D...
313  m_rep->GetStats(stats);
314  return stats[0]*stats[0]/stats[1];
315  }
316 
317  template <class INTERFACE, class IMPLEMENTATION>
319  m_rep->Scale ( scaleFactor );
320  return true;
321  }
322 
323  template <class INTERFACE, class IMPLEMENTATION>
325  m_sumEntries = 0;
326  m_rep->Reset ( );
327  return true;
328  }
329 
330  template <class INTERFACE, class IMPLEMENTATION>
332  {
334  m_rep->Print("all");
335  return s;
336  }
337 
339  template <class INTERFACE, class IMPLEMENTATION>
341  {
342  s << std::endl << "2D Histogram Table: " << std::endl;
343  s << "BinX, BinY, Height, Error " << std::endl;
344  for ( int i = 0; i < xAxis().bins(); ++i ) {
345  for ( int j = 0; j < yAxis().bins(); ++j ) {
346  s << binMeanX( i, j ) << ", "
347  << binMeanY( i, j ) << ", "
348  << binHeight( i, j ) << ", "
349  << binError ( i, j ) << std::endl;
350  }
351  }
352  s << std::endl;
353  return s;
354  }
355 
357  template <class INTERFACE, class IMPLEMENTATION>
358  int Generic2D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
359  {
360  TFile *f = TFile::Open(file_name,"RECREATE");
361  Int_t nbytes = m_rep->Write();
362  f->Close();
363  return nbytes;
364  }
365 }
366 
367 #ifdef __clang__
368 #pragma clang diagnostic pop
369 #elif defined(__GNUC__) && __GNUC__ >= 5
370 #pragma GCC diagnostic pop
371 #endif
372 
373 #endif // GAUDIPI_GENERIC2D_H
int coordToIndexX(double coordX) const override
Convenience method, equivalent to xAxis().coordToIndex(coord).
Definition: Generic2D.h:283
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:310
Generic2D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic2D.h:37
double rmsX() const override
Returns the rms of the profile as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:273
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic2D.h:301
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic2D.h:134
T endl(T...args)
Generic2D(IMPLEMENTATION *p)
constructor
Definition: Generic2D.h:42
bool reset() override
Definition: Generic2D.h:324
double binMeanY(int indexX, int indexY) const override
The weighted mean along y of a given bin.
Definition: Generic2D.h:215
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:318
double rmsY() const override
Returns the rms of the profile as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:278
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic2D.h:190
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:168
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:220
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:195
double binHeightY(int indexY) const override
Equivalent to projectionY().binHeight(indexY).
Definition: Generic2D.h:250
double binHeightX(int indexX) const override
Equivalent to projectionX().binHeight(indexX).
Definition: Generic2D.h:241
std::string m_classType
class type
Definition: Generic2D.h:152
Axis m_xAxis
X axis member.
Definition: Generic2D.h:144
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic2D.h:180
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:288
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:331
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:132
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:258
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:148
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:158
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:263
void print(string text)
Definition: mergesort.cpp:33
T c_str(T...args)
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:146
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic2D.h:340
string s
Definition: gaudirun.py:245
double binMeanX(int indexX, int indexY) const override
The weighted mean along x of a given bin.
Definition: Generic2D.h:210
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic2D.h:200
virtual double binRms(int indexX, int indexY) const
The spread (RMS) of this bin.
Definition: Generic2D.h:205
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:150
int binEntriesY(int indexY) const override
Equivalent to projectionY().binEntries(indexY).
Definition: Generic2D.h:228
int entries() const override
Get the number or all the entries.
Definition: Generic2D.h:175
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:268
#define GAUDI_API
Definition: Kernel.h:107
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:293
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:185
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:236
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic2D.h:35