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