The Gaudi Framework  v30r3 (a5ef0a68)
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  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;
98  virtual bool scale( double scaleFactor );
100  bool reset() override;
102  bool add( const INTERFACE& profile ) override;
104  virtual int rIndex( int index ) const { return m_axis.rIndex( index ); }
106  double binMean( int index ) const override;
108  double binHeight( int index ) const override;
110  double binError( int index ) const override;
112  double mean() const override { return m_rep->GetMean(); }
114  double rms() const override { return m_rep->GetRMS(); }
116  int coordToIndex( double coord ) const override { return axis().coordToIndex( coord ); }
118  int dimension() const override { return 1; }
120  std::ostream& print( std::ostream& s ) const override;
122  std::ostream& write( std::ostream& s ) const override;
124  int write( const char* file_name ) const override;
125 
126  protected:
133  // class type
135  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
137  }; // end class Generic1D
138 
139  template <class INTERFACE, class IMPLEMENTATION>
141  {
142  m_rep->SetTitle( title.c_str() );
143  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
144  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
145  return true;
146  }
147 
148  template <class INTERFACE, class IMPLEMENTATION>
150  {
151  m_rep->SetName( newName.c_str() );
152  m_annotation.setValue( "Name", newName );
153  return true;
154  }
155 
156  template <class INTERFACE, class IMPLEMENTATION>
158  {
159  return m_rep->GetBinError( rIndex( index ) );
160  }
161 
162  template <class INTERFACE, class IMPLEMENTATION>
164  {
165  return m_rep->GetBinCenter( rIndex( index ) );
166  }
167 
168  template <class INTERFACE, class IMPLEMENTATION>
170  {
171  return m_rep->GetBinContent( rIndex( index ) );
172  }
173 
174  template <class INTERFACE, class IMPLEMENTATION>
176  {
177  return m_rep->GetBinError( rIndex( index ) );
178  }
179 
180  template <class INTERFACE, class IMPLEMENTATION>
182  {
183  return binEntries( AIDA::IAxis::UNDERFLOW_BIN ) + binEntries( AIDA::IAxis::OVERFLOW_BIN );
184  }
185  template <class INTERFACE, class IMPLEMENTATION>
187  {
188  m_sumEntries = 0;
189  m_rep->Reset();
190  return true;
191  }
192 
193  template <class INTERFACE, class IMPLEMENTATION>
195  {
196  if ( sumBinHeights() <= 0 ) return 0;
197  Stat_t stats[11]; // cover up to 3D...
198  m_rep->GetStats( stats );
199  return stats[0] * stats[0] / stats[1];
200  }
201 
202  template <class INTERFACE, class IMPLEMENTATION>
204  {
205  m_rep->Scale( scaleFactor );
206  return true;
207  }
208 
209  template <class INTERFACE, class IMPLEMENTATION>
210  bool Generic1D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& h )
211  {
213  if ( p ) {
214  m_rep->Add( p->m_rep.get() );
215  return true;
216  }
217  throw std::runtime_error( "Cannot add profile histograms of different implementations." );
218  }
219 
220  template <class INTERFACE, class IMPLEMENTATION>
222  {
224  m_rep->Print( "all" );
225  return s;
226  }
227 
229  template <class INTERFACE, class IMPLEMENTATION>
231  {
232  s << "\n1D Histogram Table: " << std::endl;
233  s << "Bin, Height, Error " << std::endl;
234  for ( int i = 0; i < axis().bins(); ++i )
235  s << binMean( i ) << ", " << binHeight( i ) << ", " << binError( i ) << std::endl;
236  s << std::endl;
237  return s;
238  }
239 
241  template <class INTERFACE, class IMPLEMENTATION>
242  int Generic1D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const
243  {
244  TFile* f = TFile::Open( file_name, "RECREATE" );
245  Int_t nbytes = m_rep->Write();
246  f->Close();
247  return nbytes;
248  }
249 }
250 
251 #ifdef __clang__
252 #pragma clang diagnostic pop
253 #elif defined( __GNUC__ ) && __GNUC__ >= 5
254 #pragma GCC diagnostic pop
255 #endif
256 
257 #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:230
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:186
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:134
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:169
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:181
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:175
STL class.
virtual double binRms(int index) const
Definition: Generic1D.h:157
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:132
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic1D.h:130
bool add(const INTERFACE &profile) override
Modifies this IProfile1D by adding the contents of profile to it.
Definition: Generic1D.h:210
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:140
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:45
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:17
Axis m_axis
Axis member.
Definition: Generic1D.h:128
int dimension() const override
Get the Histogram&#39;s dimension.
Definition: Generic1D.h:118
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic1D.h:90
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:114
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:221
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:53
double binMean(int index) const override
The weighted mean of a bin.
Definition: Generic1D.h:163
int coordToIndex(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic1D.h:116
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:194
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:104
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:65
#define GAUDI_API
Definition: Kernel.h:104
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:112
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:203
std::string name() const
object name
Definition: Generic1D.h:61
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic1D.h:39
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:36