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