All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "AIDA/IProfile2D.h"
9 #include "Annotation.h"
10 #include "Axis.h"
11 #include "TFile.h"
12 
13 
14 /*
15  * Gaudi namespace
16  */
17 namespace Gaudi {
18 
29  template <class INTERFACE, class IMPLEMENTATION>
30  class GAUDI_API Generic2D : virtual public INTERFACE, virtual public HistogramBase {
31  public:
33 
34  Generic2D() : m_rep(0) {}
35 
37  virtual ~Generic2D() { delete m_rep; }
38 
40  TObject* representation() const { return m_rep; }
42  virtual void adoptRepresentation(TObject* rep);
44  virtual std::string title() const { return m_annotation.value( "Title" ); }
46  virtual bool setTitle(const std::string & title);
48  std::string name() const { return m_annotation.value("Name"); }
50  bool setName( const std::string& newName );
52  virtual AIDA::IAnnotation & annotation() { return m_annotation; }
54  virtual const AIDA::IAnnotation & annotation() const { return m_annotation; }
55 
57  virtual const AIDA::IAxis & xAxis() const { return m_xAxis; }
59  virtual const AIDA::IAxis & yAxis() const { return m_yAxis; }
61  virtual int rIndexX(int index) const { return m_xAxis.rIndex(index); }
63  virtual int rIndexY(int index) const { return m_yAxis.rIndex(index); }
64 
66  virtual int entries() const;
68  virtual int allEntries() const;
70  virtual int extraEntries() const;
72  virtual double sumBinHeights() const;
74  virtual double sumAllBinHeights() const;
76  virtual double sumExtraBinHeights ( ) const { return sumAllBinHeights()-sumBinHeights(); }
78  virtual double minBinHeight() const;
80  virtual double maxBinHeight() const;
81 
83  virtual double binMeanX(int indexX,int indexY) const;
85  virtual double binMeanY(int indexX,int indexY) const;
87  virtual int binEntries ( int indexX,int indexY ) const;
89  virtual int binEntriesX(int indexX) const;
91  virtual int binEntriesY(int indexY) const;
93  virtual double binHeight(int indexX,int indexY) const;
95  virtual double binHeightX(int indexX) const;
97  virtual double binHeightY(int indexY) const;
99  virtual double binError(int indexX,int indexY) const;
101  virtual double binRms(int indexX,int indexY) const;
103  virtual double meanX() const;
105  virtual double meanY() const;
107  virtual double rmsX() const;
109  virtual double rmsY() const;
111  virtual int coordToIndexX(double coordX) const;
113  virtual int coordToIndexY(double coordY) const;
115  virtual double equivalentBinEntries ( ) const;
117  virtual bool scale( double scaleFactor );
119  virtual bool add(const INTERFACE & h);
120  // overwrite reset
121  bool reset ( );
123  void * cast(const std::string & className) const;
125  const std::string& userLevelClassType() const { return m_classType; }
127  virtual int dimension() const { return 2; }
129  virtual std::ostream& print( std::ostream& s ) const;
131  virtual std::ostream& write( std::ostream& s ) const;
133  virtual int write( const char* file_name ) const;
134 
135  protected:
143  IMPLEMENTATION* m_rep;
145  std::string m_classType;
148  };
149 
150  template <class INTERFACE, class IMPLEMENTATION>
151  bool Generic2D<INTERFACE,IMPLEMENTATION>::setTitle(const std::string & title) {
152  m_rep->SetTitle(title.c_str());
153  if ( !annotation().addItem( "Title", title ) )
154  m_annotation.setValue( "Title" , title );
155  if ( !annotation().addItem( "title", title ) )
156  annotation().setValue( "title", title );
157  return true;
158  }
159 
160  template <class INTERFACE, class IMPLEMENTATION>
161  bool Generic2D<INTERFACE,IMPLEMENTATION>::setName( const std::string& newName ) {
162  m_rep->SetName(newName.c_str());
163  m_annotation.setValue( "Name", newName );
164  return true;
165  }
166 
167  template <class INTERFACE, class IMPLEMENTATION>
169  return (int)m_rep->GetEntries();
170  }
171 
172  template <class INTERFACE, class IMPLEMENTATION>
174  return int(m_rep->GetEntries());
175  }
176 
177  template <class INTERFACE, class IMPLEMENTATION>
179  return m_rep->GetMinimum();
180  }
181 
182  template <class INTERFACE, class IMPLEMENTATION>
184  return m_rep->GetMaximum();
185  }
186 
187  template <class INTERFACE, class IMPLEMENTATION>
189  return m_rep->GetSumOfWeights();
190  }
191 
192  template <class INTERFACE, class IMPLEMENTATION>
194  return m_rep->GetSum();
195  }
196 
197  template <class INTERFACE, class IMPLEMENTATION>
198  double Generic2D<INTERFACE,IMPLEMENTATION>::binRms(int indexX,int indexY) const {
199  return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY) );
200  }
201 
202  template <class INTERFACE, class IMPLEMENTATION>
203  double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanX(int indexX,int ) const {
204  return (m_rep->GetXaxis())->GetBinCenter( rIndexX(indexX) );
205  }
206 
207  template <class INTERFACE, class IMPLEMENTATION>
208  double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanY(int,int indexY) const {
209  return (m_rep->GetYaxis())->GetBinCenter( rIndexY(indexY) );
210  }
211 
212  template <class INTERFACE, class IMPLEMENTATION>
214  int n = 0;
215  for (int iY = -2; iY < yAxis().bins(); ++iY)
216  n += binEntries(index,iY);
217  return n;
218  }
219 
220  template <class INTERFACE, class IMPLEMENTATION>
222  int n = 0;
223  for (int iX = -2; iX < xAxis().bins(); ++iX)
224  n += binEntries(iX,index);
225  return n;
226  }
227 
228  template <class INTERFACE, class IMPLEMENTATION>
229  double Generic2D<INTERFACE,IMPLEMENTATION>::binHeight ( int indexX,int indexY ) const {
230  return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY) );
231  }
232 
233  template <class INTERFACE, class IMPLEMENTATION>
235  double s = 0;
236  for (int iY = -2; iY < yAxis().bins(); ++iY) {
237  s += binHeight(index,iY);
238  }
239  return s;
240  }
241 
242  template <class INTERFACE, class IMPLEMENTATION>
244  double s = 0;
245  for (int iX = -2; iX < xAxis().bins(); ++iX)
246  s += binHeight(iX,index);
247  return s;
248  }
249 
250  template <class INTERFACE, class IMPLEMENTATION>
251  double Generic2D<INTERFACE,IMPLEMENTATION>::binError(int indexX,int indexY) const {
252  return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ) );
253  }
254 
255  template <class INTERFACE, class IMPLEMENTATION>
257  return m_rep->GetMean(1);
258  }
259 
260  template <class INTERFACE, class IMPLEMENTATION>
262  return m_rep->GetMean(2);
263  }
264 
265  template <class INTERFACE, class IMPLEMENTATION>
267  return m_rep->GetRMS(1);
268  }
269 
270  template <class INTERFACE, class IMPLEMENTATION>
272  return m_rep->GetRMS(2);
273  }
274 
275  template <class INTERFACE, class IMPLEMENTATION>
277  return xAxis().coordToIndex(coord);
278  }
279 
280  template <class INTERFACE, class IMPLEMENTATION>
282  return yAxis().coordToIndex(coord);
283  }
284 
285  template <class INTERFACE, class IMPLEMENTATION>
286  bool Generic2D<INTERFACE,IMPLEMENTATION>::add ( const INTERFACE & hist ) {
287  const Base* p = dynamic_cast<const Base*>(&hist);
288  if ( p ) {
289  m_rep->Add(p->m_rep);
290  return true;
291  }
292  throw std::runtime_error("Cannot add profile histograms of different implementations.");
293  }
294 
295  template <class INTERFACE, class IMPLEMENTATION>
297  return
298  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
299  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
300  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
301  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
302  }
303 
304  template <class INTERFACE, class IMPLEMENTATION>
306  if (sumBinHeights() <= 0) return 0;
307  Stat_t stats[11]; // cover up to 3D...
308  m_rep->GetStats(stats);
309  return stats[0]*stats[0]/stats[1];
310  }
311 
312  template <class INTERFACE, class IMPLEMENTATION>
314  m_rep->Scale ( scaleFactor );
315  return true;
316  }
317 
318  template <class INTERFACE, class IMPLEMENTATION>
320  m_sumEntries = 0;
321  m_rep->Reset ( );
322  return true;
323  }
324 
325  template <class INTERFACE, class IMPLEMENTATION>
326  std::ostream& Generic2D<INTERFACE,IMPLEMENTATION>::print( std::ostream& s ) const
327  {
329  m_rep->Print("all");
330  return s;
331  }
332 
334  template <class INTERFACE, class IMPLEMENTATION>
335  std::ostream& Generic2D<INTERFACE,IMPLEMENTATION>::write( std::ostream& s ) const
336  {
337  s << std::endl << "2D Histogram Table: " << std::endl;
338  s << "BinX, BinY, Height, Error " << std::endl;
339  for ( int i = 0; i < xAxis().bins(); ++i ) {
340  for ( int j = 0; j < yAxis().bins(); ++j ) {
341  s << binMeanX( i, j ) << ", "
342  << binMeanY( i, j ) << ", "
343  << binHeight( i, j ) << ", "
344  << binError ( i, j ) << std::endl;
345  }
346  }
347  s << std::endl;
348  return s;
349  }
350 
352  template <class INTERFACE, class IMPLEMENTATION>
353  int Generic2D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
354  {
355  TFile *f = TFile::Open(file_name,"RECREATE");
356  Int_t nbytes = m_rep->Write();
357  f->Close();
358  return nbytes;
359  }
360 } // end namespace AIDA
361 #endif // GAUDIPI_GENERIC2D_H
int m_sumEntries
cache sumEntries (allEntries) when setting contents since Root can't compute by himself ...
Definition: Generic2D.h:147
virtual double binHeightY(int indexY) const
Equivalent to projectionY().binHeight(indexY).
Definition: Generic2D.h:243
virtual bool setTitle(const std::string &title)
Set the title of the object.
Definition: Generic2D.h:151
virtual AIDA::IAnnotation & annotation()
Access annotation object.
Definition: Generic2D.h:52
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:305
virtual double rmsX() const
Returns the rms of the profile as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:266
virtual double minBinHeight() const
Get the minimum height of the in-range bins.
Definition: Generic2D.h:178
virtual double binMeanX(int indexX, int indexY) const
The weighted mean along x of a given bin.
Definition: Generic2D.h:203
Generic2D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic2D.h:32
virtual int binEntriesY(int indexY) const
Equivalent to projectionY().binEntries(indexY).
Definition: Generic2D.h:221
virtual ~Generic2D()
Destructor.
Definition: Generic2D.h:37
TObject * representation() const
ROOT object implementation.
Definition: Generic2D.h:40
virtual double sumBinHeights() const
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:188
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:313
std::string name() const
object name
Definition: Generic2D.h:48
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic2D.h:161
virtual int rIndexX(int index) const
operator methods
Definition: Generic2D.h:61
virtual int extraEntries() const
Get the number of entries in the underflow and overflow bins.
Definition: Generic2D.h:296
virtual double maxBinHeight() const
Get the maximum height of the in-range bins.
Definition: Generic2D.h:183
virtual int dimension() const
Get the Histogram's dimension.
Definition: Generic2D.h:127
virtual double binError(int indexX, int indexY) const
The error on this bin.
Definition: Generic2D.h:251
std::string m_classType
class type
Definition: Generic2D.h:145
Axis m_xAxis
X axis member.
Definition: Generic2D.h:137
virtual std::string title() const
Get the title of the object.
Definition: Generic2D.h:44
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
virtual std::ostream & write(std::ostream &s) const
Write (ASCII) the histogram table into the output stream.
Definition: Generic2D.h:335
virtual bool add(const INTERFACE &h)
Modifies this profile by adding the contents of profile to it.
Definition: Generic2D.h:286
virtual double meanY() const
Returns the mean of the profile, as calculated on filling-time projected on the Y axis...
Definition: Generic2D.h:261
virtual int allEntries() const
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic2D.h:173
const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic2D.h:125
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:141
virtual int coordToIndexX(double coordX) const
Convenience method, equivalent to xAxis().coordToIndex(coord).
Definition: Generic2D.h:276
virtual double sumAllBinHeights() const
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic2D.h:193
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:229
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:139
virtual double sumExtraBinHeights() const
Get the sum of the underflow and overflow bin height.
Definition: Generic2D.h:76
virtual double binHeightX(int indexX) const
Equivalent to projectionX().binHeight(indexX).
Definition: Generic2D.h:234
virtual int entries() const
Get the number or all the entries.
Definition: Generic2D.h:168
string s
Definition: gaudirun.py:210
virtual const AIDA::IAxis & yAxis() const
Return the Y axis.
Definition: Generic2D.h:59
virtual const AIDA::IAnnotation & annotation() const
Access annotation object (cons)
Definition: Generic2D.h:54
virtual double binMeanY(int indexX, int indexY) const
The weighted mean along y of a given bin.
Definition: Generic2D.h:208
virtual double binRms(int indexX, int indexY) const
The spread (RMS) of this bin.
Definition: Generic2D.h:198
virtual std::ostream & print(std::ostream &s) const
Print (ASCII) the histogram into the output stream.
Definition: Generic2D.h:326
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:15
virtual int coordToIndexY(double coordY) const
Convenience method, equivalent to yAxis().coordToIndex(coord).
Definition: Generic2D.h:281
virtual int rIndexY(int index) const
operator methods
Definition: Generic2D.h:63
#define GAUDI_API
Definition: Kernel.h:108
list i
Definition: ana.py:128
virtual double rmsY() const
Returns the rms of the profile as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:271
virtual int binEntriesX(int indexX) const
Equivalent to projectionX().binEntries(indexX).
Definition: Generic2D.h:213
An IAxis represents a binned histogram axis.
Definition: Axis.h:31
virtual const AIDA::IAxis & xAxis() const
Return the X axis.
Definition: Generic2D.h:57
IMPLEMENTATION * m_rep
Reference to underlying implementation.
Definition: Generic2D.h:143
virtual double meanX() const
Returns the mean of the profile, as calculated on filling-time projected on the X axis...
Definition: Generic2D.h:256
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic2D.h:30