The Gaudi Framework  master (37c0b60a)
Generic1D.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 "Annotation.h"
15 #include "Axis.h"
16 #include <AIDA/IProfile1D.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 "-Wsuggest-override"
27 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
28 #elif defined( __GNUC__ )
29 # pragma GCC diagnostic push
30 # pragma GCC diagnostic ignored "-Wsuggest-override"
31 #endif
32 
33 namespace Gaudi {
34 
45  template <class INTERFACE, class IMPLEMENTATION>
46  class GAUDI_API Generic1D : virtual public INTERFACE, virtual public HistogramBase {
47  public:
50  Generic1D() = default;
51 
52  protected:
54  Generic1D( IMPLEMENTATION* p ) : m_rep( p ) {}
55 
56  public:
58  virtual const std::string& userLevelClassType() const { return m_classType; }
60  void* cast( const std::string& cl ) const override;
62  TObject* representation() const override { return m_rep.get(); }
64  void adoptRepresentation( TObject* rep ) override;
66  std::string title() const override { return m_annotation.value( "Title" ); }
68  bool setTitle( const std::string& title ) override;
70  std::string name() const { return m_annotation.value( "Name" ); }
72  bool setName( const std::string& newName );
74  AIDA::IAnnotation& annotation() override { return m_annotation; }
76  const AIDA::IAnnotation& annotation() const override { return m_annotation; }
78  Axis& axis() { return m_axis; }
80  const Axis& axis() const override { return m_axis; }
81 
83  int entries() const override { return m_rep->GetEntries(); }
85  int allEntries() const override { return m_rep->GetEntries(); }
87  int extraEntries() const override;
89  int binEntries( int index ) const override;
90  // spread
91  virtual double binRms( int index ) const;
93  double sumBinHeights() const override { return m_rep->GetSumOfWeights(); }
95  double sumAllBinHeights() const override { return m_rep->GetSum(); }
97  double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
99  double minBinHeight() const override { return m_rep->GetMinimum(); }
101  double maxBinHeight() const override { return m_rep->GetMaximum(); }
102 
104  virtual double equivalentBinEntries() const;
107  virtual bool scale( double scaleFactor );
109  bool reset() override;
111  bool add( const INTERFACE& profile ) override;
113  virtual int rIndex( int index ) const { return m_axis.rIndex( index ); }
115  double binMean( int index ) const override;
117  double binHeight( int index ) const override;
119  double binError( int index ) const override;
121  double mean() const override { return m_rep->GetMean(); }
123  double rms() const override { return m_rep->GetRMS(); }
125  int coordToIndex( double coord ) const override { return axis().coordToIndex( coord ); }
127  int dimension() const override { return 1; }
129  std::ostream& print( std::ostream& s ) const override;
131  std::ostream& write( std::ostream& s ) const override;
133  int write( const char* file_name ) const override;
134 
135  protected:
142  // class type
144  // cache sumEntries (allEntries) when setting contents since Root can't compute by himself
145  int m_sumEntries{ 0 };
146  }; // end class Generic1D
147 
148  template <class INTERFACE, class IMPLEMENTATION>
150  m_rep->SetTitle( title.c_str() );
151  if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
152  if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
153  return true;
154  }
155 
156  template <class INTERFACE, class IMPLEMENTATION>
158  m_rep->SetName( newName.c_str() );
159  m_annotation.setValue( "Name", newName );
160  return true;
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 m_rep->GetBinCenter( rIndex( index ) );
171  }
172 
173  template <class INTERFACE, class IMPLEMENTATION>
175  return m_rep->GetBinContent( rIndex( index ) );
176  }
177 
178  template <class INTERFACE, class IMPLEMENTATION>
180  return m_rep->GetBinError( rIndex( index ) );
181  }
182 
183  template <class INTERFACE, class IMPLEMENTATION>
185  return binEntries( AIDA::IAxis::UNDERFLOW_BIN ) + binEntries( AIDA::IAxis::OVERFLOW_BIN );
186  }
187  template <class INTERFACE, class IMPLEMENTATION>
189  m_sumEntries = 0;
190  m_rep->Reset();
191  return true;
192  }
193 
194  template <class INTERFACE, class IMPLEMENTATION>
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>
203  bool Generic1D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
204  m_rep->Scale( scaleFactor );
205  return true;
206  }
207 
208  template <class INTERFACE, class IMPLEMENTATION>
209  bool Generic1D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& h ) {
211  if ( p ) {
212  m_rep->Add( p->m_rep.get() );
213  return true;
214  }
215  throw std::runtime_error( "Cannot add profile histograms of different implementations." );
216  }
217 
218  template <class INTERFACE, class IMPLEMENTATION>
221  m_rep->Print( "all" );
222  return s;
223  }
224 
226  template <class INTERFACE, class IMPLEMENTATION>
228  s << "\n1D Histogram Table: " << std::endl;
229  s << "Bin, Height, Error " << std::endl;
230  for ( int i = 0; i < axis().bins(); ++i )
231  s << binMean( i ) << ", " << binHeight( i ) << ", " << binError( i ) << std::endl;
232  s << std::endl;
233  return s;
234  }
235 
237  template <class INTERFACE, class IMPLEMENTATION>
238  int Generic1D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
239  TFile* f = TFile::Open( file_name, "RECREATE" );
240  Int_t nbytes = m_rep->Write();
241  f->Close();
242  return nbytes;
243  }
244 } // namespace Gaudi
245 
246 #ifdef __clang__
247 # pragma clang diagnostic pop
248 #elif defined( __GNUC__ )
249 # pragma GCC diagnostic pop
250 #endif
251 
252 #endif // AIDAROOT_GENERIC1D_H
Gaudi::Generic1D::coordToIndex
int coordToIndex(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition: Generic1D.h:125
Annotation.h
Gaudi::Generic1D::m_classType
std::string m_classType
Definition: Generic1D.h:143
Gaudi::Generic1D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:141
std::string
STL class.
Gaudi::Generic1D::binRms
virtual double binRms(int index) const
Definition: Generic1D.h:164
Gaudi::Generic1D::binError
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:179
Gaudi::Generic1D::setName
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic1D.h:157
Gaudi::Generic1D::Base
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic1D.h:48
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Generic1D::sumAllBinHeights
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition: Generic1D.h:95
Gaudi::Generic1D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Gaudi::Generic1D::representation
TObject * representation() const override
ROOT object implementation.
Definition: Generic1D.h:62
Gaudi::Generic1D::axis
const Axis & axis() const override
Get the x axis of the IHistogram1D.
Definition: Generic1D.h:80
Gaudi::Generic1D::annotation
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic1D.h:76
Gaudi::Generic1D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:83
std::unique_ptr::get
T get(T... args)
Gaudi::Generic1D::axis
Axis & axis()
Access to axis object.
Definition: Generic1D.h:78
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:26
Gaudi::Generic1D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic1D.h:139
Gaudi::Generic1D::title
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:66
Gaudi::Generic1D::rIndex
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:113
Gaudi::Generic1D::rms
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:123
Gaudi::HistogramBase
Definition: HistogramBase.h:32
Gaudi::Generic1D::allEntries
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile.
Definition: Generic1D.h:85
Gaudi::Generic1D::Generic1D
Generic1D(IMPLEMENTATION *p)
constructor
Definition: Generic1D.h:54
Gaudi::Generic1D::binHeight
double binHeight(int index) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic1D.h:174
std::ostream
STL class.
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:31
Gaudi::Generic1D::add
bool add(const INTERFACE &profile) override
Modifies this IProfile1D by adding the contents of profile to it.
Definition: Generic1D.h:209
Axis.h
HistogramBase.h
Gaudi::Generic1D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic1D.h:93
std::runtime_error
STL class.
Gaudi::Generic1D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic1D.h:127
Gaudi::Generic1D::write
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic1D.h:227
Gaudi::Generic1D::m_axis
Axis m_axis
Axis member.
Definition: Generic1D.h:137
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Generic1D::mean
double mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:121
Gaudi::Generic1D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic1D.h:97
Gaudi::Generic1D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic1D.h:238
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:29
Gaudi::Generic1D::userLevelClassType
virtual const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic1D.h:58
Gaudi::Generic1D::cast
void * cast(const std::string &cl) const override
Manual cast by class name.
std::endl
T endl(T... args)
Gaudi::Generic1D::extraEntries
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic1D.h:184
Gaudi::Generic1D::reset
bool reset() override
Reset the Histogram; as if just created.
Definition: Generic1D.h:188
Gaudi::Generic1D::scale
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:203
Gaudi::Generic1D::binEntries
int binEntries(int index) const override
Number of entries in the corresponding bin (ie the number of times fill was called for this bin).
Gaudi::Generic1D
Definition: Generic1D.h:46
Gaudi::Generic1D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:149
Gaudi::Generic1D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic1D.h:101
Gaudi::Generic1D::name
std::string name() const
object name
Definition: Generic1D.h:70
std::unique_ptr< IMPLEMENTATION >
Gaudi::Generic1D::print
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic1D.h:219
Gaudi::Generic1D::Generic1D
Generic1D()=default
Default constructor.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Generic1D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic1D.h:99
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39
Gaudi::Generic1D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:74
Gaudi::Generic1D::equivalentBinEntries
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:195
Gaudi::Generic1D::binMean
double binMean(int index) const override
The weighted mean of a bin.
Definition: Generic1D.h:169