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