Generic3D.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_GENERIC3D_H
2 #define GAUDISVC_GENERIC3D_H 1
3 #include "AIDA_visibility_hack.h"
5 
6 #include "Axis.h"
7 #include "TFile.h"
8 #include "Annotation.h"
9 #include "GaudiKernel/HistogramBase.h"
10 #include "AIDA/IHistogram3D.h"
11 #include <stdexcept>
12 #include <memory>
13 
14 /*
15  * Gaudi namespace
16  */
17 namespace Gaudi {
18 
29  template<typename INTERFACE, typename IMPLEMENTATION>
30  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase {
31  public:
34  Generic3D() = default;
35  protected:
37  Generic3D(IMPLEMENTATION* p) : m_rep(p) { }
38  public:
40  ~Generic3D() override = default;
42  TObject* representation() const override { return m_rep.get(); }
44  void adoptRepresentation(TObject* rep) override;
45 
47  int dimension() const override { return 3; }
49  std::string title() const override { return m_annotation.value("Title");}
51  bool setTitle(const std::string & title) override;
53  std::string name() const { return m_annotation.value("Name"); }
55  bool setName( const std::string& newName);
57  AIDA::IAnnotation & annotation() override { return m_annotation; }
59  const AIDA::IAnnotation & annotation() const override { return m_annotation; }
60 
62  int entries() const override;
64  int allEntries() const override;
66  double sumBinHeights() const override;
68  double sumAllBinHeights() const override;
70  double sumExtraBinHeights ( ) const override { return sumAllBinHeights()-sumBinHeights(); }
72  double minBinHeight() const override;
74  double maxBinHeight() const override;
75 
76  int rIndexX(int index) const { return m_xAxis.rIndex(index);}
77  int rIndexY(int index) const { return m_yAxis.rIndex(index);}
78  int rIndexZ(int index) const { return m_zAxis.rIndex(index);}
79 
81  double binMeanX(int indexX,int ,int ) const
82  { return m_rep->GetXaxis()->GetBinCenter( rIndexX(indexX) ); }
84  double binMeanY(int,int indexY,int ) const
85  { return m_rep->GetYaxis()->GetBinCenter( rIndexY(indexY) ); }
87  double binMeanZ(int ,int ,int indexZ) const
88  { return m_rep->GetYaxis()->GetBinCenter( rIndexY(indexZ) ); }
90  int binEntries(int indexX,int indexY,int indexZ) const {
91  if (binHeight(indexX, indexY, indexZ)<=0) return 0;
92  double xx = binHeight(indexX, indexY, indexZ)/binError(indexX, indexY, indexZ);
93  return int(xx*xx+0.5);
94  }
96  int binEntriesX(int index) const override {
97  int n = 0;
98  for (int i = -2; i < yAxis().bins(); ++i)
99  for (int j = -2; j < zAxis().bins(); ++j)
100  n += binEntries(index,i,j);
101  return n;
102 
103  }
105  int binEntriesY(int index) const override {
106  int n = 0;
107  for (int i = -2; i < xAxis().bins(); ++i)
108  for (int j = -2; j < zAxis().bins(); ++j)
109  n += binEntries(i,index,j);
110  return n;
111  }
112 
114  int binEntriesZ(int index) const override {
115  int n = 0;
116  for (int i = -2; i < xAxis().bins(); ++i)
117  for (int j = -2; j < yAxis().bins(); ++j)
118  n += binEntries(i,j,index);
119  return n;
120  }
121 
123  double binHeight ( int indexX,int indexY,int indexZ ) const
124  { return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY), rIndexZ(indexZ) ); }
125 
127  double binHeightX(int index) const override {
128  double s = 0;
129  for (int i = -2; i < yAxis().bins(); ++i)
130  for (int j = -2; j < zAxis().bins(); ++j)
131  s += binHeight(index,i,j);
132  return s;
133  }
135  double binHeightY(int index) const override {
136  double s = 0;
137  for (int i = -2; i < xAxis().bins(); ++i)
138  for (int j = -2; j < zAxis().bins(); ++j)
139  s += binHeight(i,index,j);
140  return s;
141  }
143  double binHeightZ(int index) const override {
144  double s = 0;
145  for (int i = -2; i < xAxis().bins(); ++i)
146  for (int j = -2; j < yAxis().bins(); ++j)
147  s += binHeight(i,j,index);
148  return s;
149  }
151  double binError ( int indexX,int indexY,int indexZ ) const override
152  { return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ), rIndexZ(indexZ ) ); }
154  double meanX ( ) const override { return m_rep->GetMean ( 1); }
155 
157  double meanY ( ) const override { return m_rep->GetMean ( 2 ); }
159  double meanZ ( ) const override { return m_rep->GetMean ( 3 ); }
161  double rmsX ( ) const override { return m_rep->GetRMS( 1 ); }
163  double rmsY ( ) const override { return m_rep->GetRMS( 2 ); }
165  double rmsZ ( ) const override { return m_rep->GetRMS( 3 ); }
167  const AIDA::IAxis & xAxis ( ) const override { return m_xAxis; }
169  const AIDA::IAxis & yAxis ( ) const override { return m_yAxis; }
171  const AIDA::IAxis & zAxis ( ) const override { return m_zAxis; }
173  int coordToIndexX ( double coord ) const override { return xAxis().coordToIndex(coord);}
175  int coordToIndexY ( double coord ) const override { return yAxis().coordToIndex(coord);}
177  int coordToIndexZ ( double coord ) const override { return zAxis().coordToIndex(coord);}
178 
180  double equivalentBinEntries ( ) const override;
182  bool scale( double scaleFactor ) override;
184  bool add ( const INTERFACE & hist ) override {
185  const Base* p = dynamic_cast<const Base*>(&hist);
186  if ( !p ) throw std::runtime_error("Cannot add profile histograms of different implementations.");
187  m_rep->Add(p->m_rep.get());
188  return true;
189  }
190 
191  // overwrite extraentries
192  int extraEntries() const {
193  return
194  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
195  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
196  binEntries(AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
197  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
198  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
199  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
200  binEntries(AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
201  }
203  std::ostream& print( std::ostream& s ) const override;
205  std::ostream& write( std::ostream& s ) const override;
207  int write( const char* file_name ) const override;
208 
209  protected:
216  std::unique_ptr<IMPLEMENTATION> m_rep;
217  // class type
218  std::string m_classType;
219  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
220  int m_sumEntries = 0;
221  }; // end class IHistogram3D
222 
223  template <class INTERFACE, class IMPLEMENTATION>
224  bool Generic3D<INTERFACE,IMPLEMENTATION>::setTitle(const std::string & title) {
225  m_rep->SetTitle(title.c_str());
226  if ( !annotation().addItem( "Title", title ) )
227  m_annotation.setValue( "Title" , title );
228  if ( !annotation().addItem( "title", title ) )
229  annotation().setValue( "title", title );
230  return true;
231  }
232 
233  template <class INTERFACE, class IMPLEMENTATION>
234  bool Generic3D<INTERFACE,IMPLEMENTATION>::setName( const std::string& newName ) {
235  m_rep->SetName(newName.c_str());
236  m_annotation.setValue( "Name", newName );
237  return true;
238  }
239  template <class INTERFACE, class IMPLEMENTATION>
241  return m_rep->GetEntries();
242  }
243 
244  template <class INTERFACE, class IMPLEMENTATION>
246  return int(m_rep->GetEntries());
247  }
248 
249  template <class INTERFACE, class IMPLEMENTATION>
251  return m_rep->GetMinimum();
252  }
253 
254  template <class INTERFACE, class IMPLEMENTATION>
256  return m_rep->GetMaximum();
257  }
258 
259  template <class INTERFACE, class IMPLEMENTATION>
261  return m_rep->GetSumOfWeights();
262  }
263 
264  template <class INTERFACE, class IMPLEMENTATION>
266  return m_rep->GetSum();
267  }
268 
269  template <class INTERFACE, class IMPLEMENTATION>
271  if (sumBinHeights() <= 0) return 0;
272  Stat_t stats[11]; // cover up to 3D...
273  m_rep->GetStats(stats);
274  return stats[0]*stats[0]/stats[1];
275  }
276 
277  template <class INTERFACE, class IMPLEMENTATION>
279  m_rep->Scale ( scaleFactor );
280  return true;
281  }
282 
283  template <class INTERFACE, class IMPLEMENTATION>
284  std::ostream& Generic3D<INTERFACE,IMPLEMENTATION>::print( std::ostream& s ) const
285  {
287  m_rep->Print("all");
288  return s;
289  }
290 
291 
293  template <class INTERFACE, class IMPLEMENTATION>
294  std::ostream& Generic3D<INTERFACE,IMPLEMENTATION>::write( std::ostream& s ) const
295  {
296  s << "\n3D Histogram Table: " << std::endl;
297  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
298  for ( int i = 0; i < xAxis().bins(); ++i )
299  for ( int j = 0; j < yAxis().bins(); ++j )
300  for ( int k = 0; k < zAxis().bins(); ++k )
301  s << binMeanX( i, j, k ) << ", "
302  << binMeanY( i, j, k ) << ", "
303  << binMeanZ( i, j, k ) << ", "
304  << binHeight( i, j, k ) << ", "
305  << binError ( i, j, k ) << std::endl;
306  s << std::endl;
307  return s;
308  }
309 
311  template <class INTERFACE, class IMPLEMENTATION>
312  int Generic3D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
313  {
314  TFile *f = TFile::Open(file_name,"RECREATE");
315  Int_t nbytes = m_rep->Write();
316  f->Close();
317  return nbytes;
318  }
319 } // end namespace AIDA
320 #endif // GAUDIPI_GENERIC3D_H
int rIndexZ(int index) const
Definition: Generic3D.h:78
int rIndexX(int index) const
Definition: Generic3D.h:76
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:57
Gaudi::Axis m_xAxis
Definition: Generic3D.h:210
int binEntries(int indexX, int indexY, int indexZ) const
Number of entries in the corresponding bin (ie the number of times fill was calle d for this bin)...
Definition: Generic3D.h:90
#define GAUDI_API
Definition: Kernel.h:107
Gaudi::Axis m_yAxis
Definition: Generic3D.h:211
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:265
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:175
double binMeanX(int indexX, int, int) const
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:81
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:167
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:114
double binMeanZ(int, int, int indexZ) const
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:87
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:70
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:42
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:260
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:49
std::string name() const
object name
Definition: Generic3D.h:53
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:184
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:240
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:171
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:255
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:154
int extraEntries() const
Definition: Generic3D.h:192
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:105
double binMeanY(int, int indexY, int) const
The weighted mean along the y axis of a given bin.
Definition: Generic3D.h:84
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
bool scale(double scaleFactor) override
Scale the weights and the errors of all the IHistogram's bins (in-range and out-of-range ones) by a g...
Definition: Generic3D.h:278
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:234
Gaudi::Axis m_zAxis
Definition: Generic3D.h:212
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:224
int rIndexY(int index) const
Definition: Generic3D.h:77
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:32
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:216
double binHeight(int indexX, int indexY, int indexZ) const
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic3D.h:123
int dimension() const override
Get the Histogram's dimension.
Definition: Generic3D.h:47
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic3D.h:245
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:143
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:163
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:135
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
Definition: HistogramBase.h:22
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:173
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic3D.h:214
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:59
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:294
std::string m_classType
Definition: Generic3D.h:218
string s
Definition: gaudirun.py:246
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:250
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:177
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:96
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:284
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:165
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:151
list i
Definition: ana.py:128
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:157
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:127
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:37
Helper functions to set/get the application return code.
Definition: __init__.py:1
An IAxis represents a binned histogram axis.
Definition: Axis.h:31
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:161
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:159
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:270
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic3D.h:30
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:169