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"
11 #include "AIDA/IProfile1D.h"
12 #include "TFile.h"
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 <class INTERFACE, class IMPLEMENTATION>
37  class GAUDI_API Generic1D : virtual public INTERFACE, virtual public HistogramBase {
38  public:
41  Generic1D() = default;
42  protected:
44  Generic1D(IMPLEMENTATION* p) : m_rep(p) { }
45  public:
47  ~Generic1D() override = default ;
49  virtual const std::string& userLevelClassType() const { return m_classType; }
51  void* cast(const std::string& cl) const override;
53  TObject* representation() const override { return m_rep.get(); }
55  void adoptRepresentation(TObject*rep) override;
57  std::string title() const override { return m_annotation.value("Title"); }
59  bool setTitle(const std::string & title) override;
61  std::string name() const { return m_annotation.value("Name"); }
63  bool setName( const std::string& newName );
65  AIDA::IAnnotation & annotation() override { return m_annotation; }
67  const AIDA::IAnnotation & annotation() const override { return m_annotation; }
69  Axis & axis () { return m_axis; }
71  const Axis & axis () const override { return m_axis; }
72 
74  int entries() const override { return m_rep->GetEntries(); }
76  int allEntries() const override { return m_rep->GetEntries(); }
78  int extraEntries() const override;
80  int binEntries ( int index ) const override;
81  // spread
82  virtual double binRms(int index) const;
84  double sumBinHeights() const override { return m_rep->GetSumOfWeights(); }
86  double sumAllBinHeights() const override { return m_rep->GetSum(); }
88  double sumExtraBinHeights () const override { return sumAllBinHeights()-sumBinHeights(); }
90  double minBinHeight() const override { return m_rep->GetMinimum(); }
92  double maxBinHeight() const override { return m_rep->GetMaximum(); }
93 
95  virtual double equivalentBinEntries ( ) const;
97  virtual bool scale( double scaleFactor ) ;
99  bool reset() override;
101  bool add(const INTERFACE & profile) override;
103  virtual int rIndex(int index) const { return m_axis.rIndex(index);}
105  double binMean(int index) const override;
107  double binHeight(int index) const override;
109  double binError(int index) const override;
111  double mean() const override { return m_rep->GetMean(); }
113  double rms () const override { return m_rep->GetRMS(); }
115  int coordToIndex ( double coord ) const override{ return axis().coordToIndex(coord);}
117  int dimension ( ) const override { return 1; }
119  std::ostream& print( std::ostream& s ) const override;
121  std::ostream& write( std::ostream& s ) const override;
123  int write( const char* file_name ) const override;
124 
125  protected:
132  // class type
134  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
136  }; // end class Generic1D
137 
138  template <class INTERFACE, class IMPLEMENTATION>
140  m_rep->SetTitle(title.c_str());
141  if ( !annotation().addItem( "Title", title ) )
142  m_annotation.setValue( "Title" , title );
143  if ( !annotation().addItem( "title", title ) )
144  annotation().setValue( "title", title );
145  return true;
146  }
147 
148  template <class INTERFACE, class IMPLEMENTATION>
150  m_rep->SetName(newName.c_str());
151  m_annotation.setValue( "Name", newName );
152  return true;
153  }
154 
155  template <class INTERFACE, class IMPLEMENTATION>
157  return m_rep->GetBinError ( rIndex(index) );
158  }
159 
160  template <class INTERFACE, class IMPLEMENTATION>
161  double Generic1D<INTERFACE,IMPLEMENTATION>::binMean ( int index ) const {
162  return m_rep->GetBinCenter ( rIndex(index) );
163  }
164 
165  template <class INTERFACE, class IMPLEMENTATION>
167  return m_rep->GetBinContent ( rIndex(index) );
168  }
169 
170  template <class INTERFACE, class IMPLEMENTATION>
172  return m_rep->GetBinError ( rIndex(index) );
173  }
174 
175  template <class INTERFACE, class IMPLEMENTATION>
177  return binEntries(AIDA::IAxis::UNDERFLOW_BIN) +
178  binEntries(AIDA::IAxis::OVERFLOW_BIN);
179  }
180  template <class INTERFACE, class IMPLEMENTATION>
182  m_sumEntries = 0;
183  m_rep->Reset();
184  return true;
185  }
186 
187  template <class INTERFACE, class IMPLEMENTATION>
189  if (sumBinHeights() <= 0) return 0;
190  Stat_t stats[11]; // cover up to 3D...
191  m_rep->GetStats(stats);
192  return stats[0]*stats[0]/stats[1];
193  }
194 
195  template <class INTERFACE, class IMPLEMENTATION>
197  m_rep->Scale ( scaleFactor );
198  return true;
199  }
200 
201  template <class INTERFACE, class IMPLEMENTATION>
202  bool Generic1D<INTERFACE,IMPLEMENTATION>::add(const INTERFACE & h) {
204  dynamic_cast<const Generic1D<INTERFACE,IMPLEMENTATION>*>(&h);
205  if ( p ) {
206  m_rep->Add(p->m_rep.get());
207  return true;
208  }
209  throw std::runtime_error("Cannot add profile histograms of different implementations.");
210  }
211 
212  template <class INTERFACE, class IMPLEMENTATION>
215  m_rep->Print("all");
216  return s;
217  }
218 
220  template <class INTERFACE, class IMPLEMENTATION>
222  s << "\n1D Histogram Table: " << std::endl;
223  s << "Bin, Height, Error " << std::endl;
224  for( int i = 0; i < axis().bins(); ++i )
225  s << binMean( i ) << ", "
226  << binHeight( i ) << ", "
227  << binError ( i ) << std::endl;
228  s << std::endl;
229  return s;
230  }
231 
233  template <class INTERFACE, class IMPLEMENTATION>
234  int Generic1D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
235  {
236  TFile *f = TFile::Open(file_name,"RECREATE");
237  Int_t nbytes = m_rep->Write();
238  f->Close();
239  return nbytes;
240  }
241 }
242 
243 #ifdef __clang__
244 #pragma clang diagnostic pop
245 #elif defined(__GNUC__) && __GNUC__ >= 5
246 #pragma GCC diagnostic pop
247 #endif
248 
249 #endif // AIDAROOT_GENERIC1D_H
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:57
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic1D.h:221
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:74
bool reset() override
Reset the Histogram; as if just created.
Definition: Generic1D.h:181
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic1D.h:92
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic1D.h:84
std::string m_classType
Definition: Generic1D.h:133
T endl(T...args)
double binHeight(int index) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic1D.h:166
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic1D.h:88
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic1D.h:176
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:171
STL class.
virtual double binRms(int index) const
Definition: Generic1D.h:156
const Axis & axis() const override
Get the x axis of the IHistogram1D.
Definition: Generic1D.h:71
Axis & axis()
Access to axis object.
Definition: Generic1D.h:69
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:131
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic1D.h:129
bool add(const INTERFACE &profile) override
Modifies this IProfile1D by adding the contents of profile to it.
Definition: Generic1D.h:202
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:139
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic1D.h:67
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic1D.h:76
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic1D.h:86
Generic1D(IMPLEMENTATION *p)
constructor
Definition: Generic1D.h:44
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
Axis m_axis
Axis member.
Definition: Generic1D.h:127
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic1D.h:117
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic1D.h:90
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic1D.h:39
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:113
virtual const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic1D.h:49
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic1D.h:213
T get(T...args)
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
Definition: HistogramBase.h:22
TObject * representation() const override
ROOT object implementation.
Definition: Generic1D.h:53
double binMean(int index) const override
The weighted mean of a bin.
Definition: Generic1D.h:161
int coordToIndex(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic1D.h:115
void print(string text)
Definition: mergesort.cpp:33
T c_str(T...args)
string s
Definition: gaudirun.py:245
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:188
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:103
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:65
#define GAUDI_API
Definition: Kernel.h:107
STL class.
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 mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:111
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic1D.h:149
virtual bool scale(double scaleFactor)
Scale the weights and the errors of all the IHistogram&#39;s bins (in-range and out-of-range ones) by a g...
Definition: Generic1D.h:196
std::string name() const
object name
Definition: Generic1D.h:61
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:37