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"
10 #include "AIDA/IHistogram3D.h"
11 #include <stdexcept>
12 #include <memory>
13 
14 // Hide warning message:
15 // warning: 'XYZ' overrides a member function but is not marked 'override'
16 #ifdef __clang__
17 #pragma clang diagnostic push
18 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
19 #elif defined(__GNUC__) && __GNUC__ >= 5
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wsuggest-override"
22 #endif
23 
24 namespace Gaudi {
25 
36  template<typename INTERFACE, typename IMPLEMENTATION>
37  class GAUDI_API Generic3D : virtual public INTERFACE, virtual public HistogramBase {
38  public:
41  Generic3D() = default;
42  protected:
44  Generic3D(IMPLEMENTATION* p) : m_rep(p) { }
45  public:
47  ~Generic3D() override = default;
49  TObject* representation() const override { return m_rep.get(); }
51  void adoptRepresentation(TObject* rep) override;
52 
54  int dimension() const override { return 3; }
56  std::string title() const override { return m_annotation.value("Title");}
58  bool setTitle(const std::string & title) override;
60  std::string name() const { return m_annotation.value("Name"); }
62  bool setName( const std::string& newName);
64  AIDA::IAnnotation & annotation() override { return m_annotation; }
66  const AIDA::IAnnotation & annotation() const override { return m_annotation; }
67 
69  int entries() const override;
71  int allEntries() const override;
73  double sumBinHeights() const override;
75  double sumAllBinHeights() const override;
77  double sumExtraBinHeights ( ) const override { return sumAllBinHeights()-sumBinHeights(); }
79  double minBinHeight() const override;
81  double maxBinHeight() const override;
82 
83  int rIndexX(int index) const { return m_xAxis.rIndex(index);}
84  int rIndexY(int index) const { return m_yAxis.rIndex(index);}
85  int rIndexZ(int index) const { return m_zAxis.rIndex(index);}
86 
88  double binMeanX(int indexX,int ,int ) const override
89  { return m_rep->GetXaxis()->GetBinCenter( rIndexX(indexX) ); }
91  double binMeanY(int,int indexY,int ) const override
92  { return m_rep->GetYaxis()->GetBinCenter( rIndexY(indexY) ); }
94  double binMeanZ(int ,int ,int indexZ) const override
95  { return m_rep->GetYaxis()->GetBinCenter( rIndexY(indexZ) ); }
97  int binEntries(int indexX,int indexY,int indexZ) const override {
98  if (binHeight(indexX, indexY, indexZ)<=0) return 0;
99  double xx = binHeight(indexX, indexY, indexZ)/binError(indexX, indexY, indexZ);
100  return int(xx*xx+0.5);
101  }
103  int binEntriesX(int index) const override {
104  int n = 0;
105  for (int i = -2; i < yAxis().bins(); ++i)
106  for (int j = -2; j < zAxis().bins(); ++j)
107  n += binEntries(index,i,j);
108  return n;
109 
110  }
112  int binEntriesY(int index) const override {
113  int n = 0;
114  for (int i = -2; i < xAxis().bins(); ++i)
115  for (int j = -2; j < zAxis().bins(); ++j)
116  n += binEntries(i,index,j);
117  return n;
118  }
119 
121  int binEntriesZ(int index) const override {
122  int n = 0;
123  for (int i = -2; i < xAxis().bins(); ++i)
124  for (int j = -2; j < yAxis().bins(); ++j)
125  n += binEntries(i,j,index);
126  return n;
127  }
128 
130  double binHeight ( int indexX,int indexY,int indexZ ) const
131  { return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY), rIndexZ(indexZ) ); }
132 
134  double binHeightX(int index) const override {
135  double s = 0;
136  for (int i = -2; i < yAxis().bins(); ++i)
137  for (int j = -2; j < zAxis().bins(); ++j)
138  s += binHeight(index,i,j);
139  return s;
140  }
142  double binHeightY(int index) const override {
143  double s = 0;
144  for (int i = -2; i < xAxis().bins(); ++i)
145  for (int j = -2; j < zAxis().bins(); ++j)
146  s += binHeight(i,index,j);
147  return s;
148  }
150  double binHeightZ(int index) const override {
151  double s = 0;
152  for (int i = -2; i < xAxis().bins(); ++i)
153  for (int j = -2; j < yAxis().bins(); ++j)
154  s += binHeight(i,j,index);
155  return s;
156  }
158  double binError ( int indexX,int indexY,int indexZ ) const override
159  { return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ), rIndexZ(indexZ ) ); }
161  double meanX ( ) const override { return m_rep->GetMean ( 1); }
162 
164  double meanY ( ) const override { return m_rep->GetMean ( 2 ); }
166  double meanZ ( ) const override { return m_rep->GetMean ( 3 ); }
168  double rmsX ( ) const override { return m_rep->GetRMS( 1 ); }
170  double rmsY ( ) const override { return m_rep->GetRMS( 2 ); }
172  double rmsZ ( ) const override { return m_rep->GetRMS( 3 ); }
174  const AIDA::IAxis & xAxis ( ) const override { return m_xAxis; }
176  const AIDA::IAxis & yAxis ( ) const override { return m_yAxis; }
178  const AIDA::IAxis & zAxis ( ) const override { return m_zAxis; }
180  int coordToIndexX ( double coord ) const override { return xAxis().coordToIndex(coord);}
182  int coordToIndexY ( double coord ) const override { return yAxis().coordToIndex(coord);}
184  int coordToIndexZ ( double coord ) const override { return zAxis().coordToIndex(coord);}
185 
187  double equivalentBinEntries ( ) const override;
189  bool scale( double scaleFactor ) override;
191  bool add ( const INTERFACE & hist ) override {
192  const Base* p = dynamic_cast<const Base*>(&hist);
193  if ( !p ) throw std::runtime_error("Cannot add profile histograms of different implementations.");
194  m_rep->Add(p->m_rep.get());
195  return true;
196  }
197 
198  // overwrite extraentries
199  int extraEntries() const override {
200  return
201  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
202  binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
203  binEntries(AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
204  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
205  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
206  binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
207  binEntries(AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
208  }
210  std::ostream& print( std::ostream& s ) const override;
212  std::ostream& write( std::ostream& s ) const override;
214  int write( const char* file_name ) const override;
215 
216  protected:
224  // class type
226  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
227  int m_sumEntries = 0;
228  }; // end class IHistogram3D
229 
230  template <class INTERFACE, class IMPLEMENTATION>
232  m_rep->SetTitle(title.c_str());
233  if ( !annotation().addItem( "Title", title ) )
234  m_annotation.setValue( "Title" , title );
235  if ( !annotation().addItem( "title", title ) )
236  annotation().setValue( "title", title );
237  return true;
238  }
239 
240  template <class INTERFACE, class IMPLEMENTATION>
242  m_rep->SetName(newName.c_str());
243  m_annotation.setValue( "Name", newName );
244  return true;
245  }
246  template <class INTERFACE, class IMPLEMENTATION>
248  return m_rep->GetEntries();
249  }
250 
251  template <class INTERFACE, class IMPLEMENTATION>
253  return int(m_rep->GetEntries());
254  }
255 
256  template <class INTERFACE, class IMPLEMENTATION>
258  return m_rep->GetMinimum();
259  }
260 
261  template <class INTERFACE, class IMPLEMENTATION>
263  return m_rep->GetMaximum();
264  }
265 
266  template <class INTERFACE, class IMPLEMENTATION>
268  return m_rep->GetSumOfWeights();
269  }
270 
271  template <class INTERFACE, class IMPLEMENTATION>
273  return m_rep->GetSum();
274  }
275 
276  template <class INTERFACE, class IMPLEMENTATION>
278  if (sumBinHeights() <= 0) return 0;
279  Stat_t stats[11]; // cover up to 3D...
280  m_rep->GetStats(stats);
281  return stats[0]*stats[0]/stats[1];
282  }
283 
284  template <class INTERFACE, class IMPLEMENTATION>
286  m_rep->Scale ( scaleFactor );
287  return true;
288  }
289 
290  template <class INTERFACE, class IMPLEMENTATION>
292  {
294  m_rep->Print("all");
295  return s;
296  }
297 
298 
300  template <class INTERFACE, class IMPLEMENTATION>
302  {
303  s << "\n3D Histogram Table: " << std::endl;
304  s << "BinX, BinY, BinZ, Height, Error " << std::endl;
305  for ( int i = 0; i < xAxis().bins(); ++i )
306  for ( int j = 0; j < yAxis().bins(); ++j )
307  for ( int k = 0; k < zAxis().bins(); ++k )
308  s << binMeanX( i, j, k ) << ", "
309  << binMeanY( i, j, k ) << ", "
310  << binMeanZ( i, j, k ) << ", "
311  << binHeight( i, j, k ) << ", "
312  << binError ( i, j, k ) << std::endl;
313  s << std::endl;
314  return s;
315  }
316 
318  template <class INTERFACE, class IMPLEMENTATION>
319  int Generic3D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
320  {
321  TFile *f = TFile::Open(file_name,"RECREATE");
322  Int_t nbytes = m_rep->Write();
323  f->Close();
324  return nbytes;
325  }
326 }
327 
328 #ifdef __clang__
329 #pragma clang diagnostic pop
330 #elif defined(__GNUC__) && __GNUC__ >= 5
331 #pragma GCC diagnostic pop
332 #endif
333 
334 #endif // GAUDIPI_GENERIC3D_H
int rIndexZ(int index) const
Definition: Generic3D.h:85
int rIndexX(int index) const
Definition: Generic3D.h:83
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic3D.h:64
Gaudi::Axis m_xAxis
Definition: Generic3D.h:217
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:166
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic3D.h:66
int binEntriesY(int index) const override
Sum of all the entries of the bins along a given y bin.
Definition: Generic3D.h:112
Gaudi::Axis m_yAxis
Definition: Generic3D.h:218
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic3D.h:262
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:247
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic3D.h:291
T endl(T...args)
double binMeanY(int, int indexY, int) const override
The weighted mean along the y axis of a given bin.
Definition: Generic3D.h:91
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:176
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:178
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:172
std::string name() const
object name
Definition: Generic3D.h:60
bool add(const INTERFACE &hist) override
Add to this Histogram3D the contents of another IHistogram3D.
Definition: Generic3D.h:191
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic3D.h:272
STL class.
int binEntries(int indexX, int indexY, int indexZ) const override
Number of entries in the corresponding bin (ie the number of times fill was calle d for this bin)...
Definition: Generic3D.h:97
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:267
double binHeightY(int index) const override
Sum of all the heights of the bins along a given y bin.
Definition: Generic3D.h:142
double binHeightZ(int index) const override
Sum of all the heights of the bins along a given z bin.
Definition: Generic3D.h:150
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&#39;s bins (in-range and out-of-range ones) by a g...
Definition: Generic3D.h:285
bool setName(const std::string &newName)
Sets the name of the object.
Definition: Generic3D.h:241
Gaudi::Axis m_zAxis
Definition: Generic3D.h:219
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:231
int rIndexY(int index) const
Definition: Generic3D.h:84
Generic3D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic3D.h:39
TObject * representation() const override
ROOT object implementation.
Definition: Generic3D.h:49
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:223
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:130
std::string title() const override
Get the title of the object.
Definition: Generic3D.h:56
T get(T...args)
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic3D.h:257
int binEntriesX(int index) const override
Sum of all the entries of the bins along a given x bin.
Definition: Generic3D.h:103
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: Generic3D.h:221
int coordToIndexY(double coord) const override
Get the bin number corresponding to a given coordinate along the y axis.
Definition: Generic3D.h:182
void print(string text)
Definition: mergesort.cpp:33
T c_str(T...args)
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:174
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:170
std::string m_classType
Definition: Generic3D.h:225
string s
Definition: gaudirun.py:245
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:168
double binMeanZ(int, int, int indexZ) const override
The weighted mean along the z axis of a given bin.
Definition: Generic3D.h:94
int coordToIndexZ(double coord) const override
Get the bin number corresponding to a given coordinate along the z axis.
Definition: Generic3D.h:184
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic3D.h:54
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:164
double binMeanX(int indexX, int, int) const override
The weighted mean along the x axis of a given bin.
Definition: Generic3D.h:88
int coordToIndexX(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic3D.h:180
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic3D.h:301
#define GAUDI_API
Definition: Kernel.h:107
int extraEntries() const override
Definition: Generic3D.h:199
STL class.
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:277
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic3D.h:252
double binHeightX(int index) const override
Sum of all the heights of the bins along a given x bin.
Definition: Generic3D.h:134
double binError(int indexX, int indexY, int indexZ) const override
The error of a given bin.
Definition: Generic3D.h:158
Generic3D(IMPLEMENTATION *p)
constructor
Definition: Generic3D.h:44
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 meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:161
int binEntriesZ(int index) const override
Sum of all the entries of the bins along a given z bin.
Definition: Generic3D.h:121
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic3D.h:37
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic3D.h:77