Generic1D.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_GENERIC1D_H
2 #define GAUDISVC_GENERIC1D_H 1
3 #include "AIDA_visibility_hack.h"
5 
6 #include <stdexcept>
7 #include <memory>
8 #include "Axis.h"
9 #include "Annotation.h"
10 #include "GaudiKernel/HistogramBase.h"
11 #include "AIDA/IProfile1D.h"
12 #include "TFile.h"
13 
14 #ifdef __clang__
15 #pragma clang diagnostic push
16 // Hide warning message:
17 // warning: 'XYZ' overrides a member function but is not marked 'override'
18 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
19 #endif
20 
21 namespace Gaudi {
22 
33  template <class INTERFACE, class IMPLEMENTATION>
34  class GAUDI_API Generic1D : virtual public INTERFACE, virtual public HistogramBase {
35  public:
38  Generic1D() = default;
39  protected:
41  Generic1D(IMPLEMENTATION* p) : m_rep(p) { }
42  public:
44  ~Generic1D() override = default ;
46  virtual const std::string& userLevelClassType() const { return m_classType; }
48  void* cast(const std::string& cl) const override;
50  TObject* representation() const override { return m_rep.get(); }
52  void adoptRepresentation(TObject*rep) override;
54  std::string title() const override { return m_annotation.value("Title"); }
56  bool setTitle(const std::string & title) override;
58  std::string name() const { return m_annotation.value("Name"); }
60  bool setName( const std::string& newName );
62  AIDA::IAnnotation & annotation() override { return m_annotation; }
64  const AIDA::IAnnotation & annotation() const override { return m_annotation; }
66  Axis & axis () { return m_axis; }
68  const Axis & axis () const override { return m_axis; }
69 
71  int entries() const override { return m_rep->GetEntries(); }
73  int allEntries() const override { return m_rep->GetEntries(); }
75  int extraEntries() const override;
77  int binEntries ( int index ) const override;
78  // spread
79  virtual double binRms(int index) const;
81  double sumBinHeights() const override { return m_rep->GetSumOfWeights(); }
83  double sumAllBinHeights() const override { return m_rep->GetSum(); }
85  double sumExtraBinHeights () const override { return sumAllBinHeights()-sumBinHeights(); }
87  double minBinHeight() const override { return m_rep->GetMinimum(); }
89  double maxBinHeight() const override { return m_rep->GetMaximum(); }
90 
92  virtual double equivalentBinEntries ( ) const;
94  virtual bool scale( double scaleFactor ) ;
96  bool reset() override;
98  bool add(const INTERFACE & profile) override;
100  virtual int rIndex(int index) const { return m_axis.rIndex(index);}
102  double binMean(int index) const override;
104  double binHeight(int index) const override;
106  double binError(int index) const override;
108  double mean() const override { return m_rep->GetMean(); }
110  double rms () const override { return m_rep->GetRMS(); }
112  int coordToIndex ( double coord ) const override{ return axis().coordToIndex(coord);}
114  int dimension ( ) const override { return 1; }
116  std::ostream& print( std::ostream& s ) const override;
118  std::ostream& write( std::ostream& s ) const override;
120  int write( const char* file_name ) const override;
121 
122  protected:
128  std::unique_ptr<IMPLEMENTATION> m_rep;
129  // class type
130  std::string m_classType;
131  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
133  }; // end class Generic1D
134 
135  template <class INTERFACE, class IMPLEMENTATION>
136  bool Generic1D<INTERFACE,IMPLEMENTATION>::setTitle(const std::string & title) {
137  m_rep->SetTitle(title.c_str());
138  if ( !annotation().addItem( "Title", title ) )
139  m_annotation.setValue( "Title" , title );
140  if ( !annotation().addItem( "title", title ) )
141  annotation().setValue( "title", title );
142  return true;
143  }
144 
145  template <class INTERFACE, class IMPLEMENTATION>
146  bool Generic1D<INTERFACE,IMPLEMENTATION>::setName( const std::string& newName ) {
147  m_rep->SetName(newName.c_str());
148  m_annotation.setValue( "Name", newName );
149  return true;
150  }
151 
152  template <class INTERFACE, class IMPLEMENTATION>
154  return m_rep->GetBinError ( rIndex(index) );
155  }
156 
157  template <class INTERFACE, class IMPLEMENTATION>
158  double Generic1D<INTERFACE,IMPLEMENTATION>::binMean ( int index ) const {
159  return m_rep->GetBinCenter ( rIndex(index) );
160  }
161 
162  template <class INTERFACE, class IMPLEMENTATION>
164  return m_rep->GetBinContent ( rIndex(index) );
165  }
166 
167  template <class INTERFACE, class IMPLEMENTATION>
169  return m_rep->GetBinError ( rIndex(index) );
170  }
171 
172  template <class INTERFACE, class IMPLEMENTATION>
174  return binEntries(AIDA::IAxis::UNDERFLOW_BIN) +
175  binEntries(AIDA::IAxis::OVERFLOW_BIN);
176  }
177  template <class INTERFACE, class IMPLEMENTATION>
179  m_sumEntries = 0;
180  m_rep->Reset();
181  return true;
182  }
183 
184  template <class INTERFACE, class IMPLEMENTATION>
186  if (sumBinHeights() <= 0) return 0;
187  Stat_t stats[11]; // cover up to 3D...
188  m_rep->GetStats(stats);
189  return stats[0]*stats[0]/stats[1];
190  }
191 
192  template <class INTERFACE, class IMPLEMENTATION>
194  m_rep->Scale ( scaleFactor );
195  return true;
196  }
197 
198  template <class INTERFACE, class IMPLEMENTATION>
199  bool Generic1D<INTERFACE,IMPLEMENTATION>::add(const INTERFACE & h) {
201  dynamic_cast<const Generic1D<INTERFACE,IMPLEMENTATION>*>(&h);
202  if ( p ) {
203  m_rep->Add(p->m_rep.get());
204  return true;
205  }
206  throw std::runtime_error("Cannot add profile histograms of different implementations.");
207  }
208 
209  template <class INTERFACE, class IMPLEMENTATION>
210  std::ostream& Generic1D<INTERFACE,IMPLEMENTATION>::print( std::ostream& s ) const {
212  m_rep->Print("all");
213  return s;
214  }
215 
217  template <class INTERFACE, class IMPLEMENTATION>
218  std::ostream& Generic1D<INTERFACE,IMPLEMENTATION>::write( std::ostream& s ) const {
219  s << "\n1D Histogram Table: " << std::endl;
220  s << "Bin, Height, Error " << std::endl;
221  for( int i = 0; i < axis().bins(); ++i )
222  s << binMean( i ) << ", "
223  << binHeight( i ) << ", "
224  << binError ( i ) << std::endl;
225  s << std::endl;
226  return s;
227  }
228 
230  template <class INTERFACE, class IMPLEMENTATION>
231  int Generic1D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
232  {
233  TFile *f = TFile::Open(file_name,"RECREATE");
234  Int_t nbytes = m_rep->Write();
235  f->Close();
236  return nbytes;
237  }
238 }
239 
240 #ifdef __clang__
241 #pragma clang diagnostic pop
242 #endif
243 
244 #endif // AIDAROOT_GENERIC1D_H
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic1D.h:89
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:110
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic1D.h:83
#define GAUDI_API
Definition: Kernel.h:107
bool reset() override
Reset the Histogram; as if just created.
Definition: Generic1D.h:178
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic1D.h:210
std::string m_classType
Definition: Generic1D.h:130
double binMean(int index) const override
The weighted mean of a bin.
Definition: Generic1D.h:158
virtual double binRms(int index) const
Definition: Generic1D.h:153
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:168
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic1D.h:73
Axis & axis()
Access to axis object.
Definition: Generic1D.h:66
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:128
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic1D.h:81
int coordToIndex(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic1D.h:112
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic1D.h:126
double mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:108
bool add(const INTERFACE &profile) override
Modifies this IProfile1D by adding the contents of profile to it.
Definition: Generic1D.h:199
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:136
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic1D.h:85
Generic1D(IMPLEMENTATION *p)
constructor
Definition: Generic1D.h:41
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
Axis m_axis
Axis member.
Definition: Generic1D.h:124
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic1D.h:36
TObject * representation() const override
ROOT object implementation.
Definition: Generic1D.h:50
virtual const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic1D.h:46
int dimension() const override
Get the Histogram's dimension.
Definition: Generic1D.h:114
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic1D.h:173
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
Definition: HistogramBase.h:22
void print(string text)
Definition: mergesort.cpp:33
const Axis & axis() const override
Get the x axis of the IHistogram1D.
Definition: Generic1D.h:68
string s
Definition: gaudirun.py:245
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic1D.h:64
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic1D.h:87
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:71
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:185
double binHeight(int index) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic1D.h:163
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic1D.h:218
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:100
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:62
list i
Definition: ana.py:128
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:54
Helper functions to set/get the application return code.
Definition: __init__.py:1
An IAxis represents a binned histogram axis.
Definition: Axis.h:31
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic1D.h:146
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: Generic1D.h:193
std::string name() const
object name
Definition: Generic1D.h:58
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:34