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