Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Generic1D.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 #include "Annotation.h"
14 #include "Axis.h"
15 #include <AIDA/IProfile1D.h>
17 #include <TFile.h>
18 #include <memory>
19 #include <stdexcept>
20 
21 // Hide warning message:
22 // warning: 'XYZ' overrides a member function but is not marked 'override'
23 #ifdef __clang__
24 # pragma clang diagnostic push
25 # pragma clang diagnostic ignored "-Wsuggest-override"
26 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
27 #elif defined( __GNUC__ )
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:
140  std::unique_ptr<IMPLEMENTATION> m_rep;
141  // class type
142  std::string m_classType;
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>
148  bool Generic1D<INTERFACE, IMPLEMENTATION>::setTitle( const std::string& title ) {
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>
156  bool Generic1D<INTERFACE, IMPLEMENTATION>::setName( const std::string& newName ) {
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>
218  std::ostream& Generic1D<INTERFACE, IMPLEMENTATION>::print( std::ostream& s ) const {
220  m_rep->Print( "all" );
221  return s;
222  }
223 
225  template <class INTERFACE, class IMPLEMENTATION>
226  std::ostream& Generic1D<INTERFACE, IMPLEMENTATION>::write( std::ostream& s ) const {
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__ )
248 # pragma GCC diagnostic pop
249 #endif
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:124
Annotation.h
Gaudi::Generic1D::m_classType
std::string m_classType
Definition: Generic1D.h:142
Gaudi::Generic1D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:140
Gaudi::Generic1D::binRms
virtual double binRms(int index) const
Definition: Generic1D.h:163
Gaudi::Generic1D::binError
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:178
Gaudi::Generic1D::setName
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic1D.h:156
Gaudi::Generic1D::Base
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition: Generic1D.h:47
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:94
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:61
Gaudi::Generic1D::axis
const Axis & axis() const override
Get the x axis of the IHistogram1D.
Definition: Generic1D.h:79
Gaudi::Generic1D::annotation
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition: Generic1D.h:75
Gaudi::Generic1D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:82
Gaudi::Generic1D::axis
Axis & axis()
Access to axis object.
Definition: Generic1D.h:77
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:25
Gaudi::Generic1D::m_annotation
AIDA::Annotation m_annotation
Object annotations.
Definition: Generic1D.h:138
Gaudi::Generic1D::title
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:65
Gaudi::Generic1D::rIndex
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:112
Gaudi::Generic1D::rms
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:122
Gaudi::HistogramBase
Definition: HistogramBase.h:31
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:84
Gaudi::Generic1D::Generic1D
Generic1D(IMPLEMENTATION *p)
constructor
Definition: Generic1D.h:53
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:173
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:208
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:92
Gaudi::Generic1D::dimension
int dimension() const override
Get the Histogram's dimension.
Definition: Generic1D.h:126
Gaudi::Generic1D::write
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition: Generic1D.h:226
Gaudi::Generic1D::m_axis
Axis m_axis
Axis member.
Definition: Generic1D.h:136
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:120
Gaudi::Generic1D::sumExtraBinHeights
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition: Generic1D.h:96
Gaudi::Generic1D::write
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition: Generic1D.h:237
Gaudi::Axis
An IAxis represents a binned histogram axis.
Definition: Axis.h:28
Gaudi::Generic1D::userLevelClassType
virtual const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition: Generic1D.h:57
Gaudi::Generic1D::cast
void * cast(const std::string &cl) const override
Manual cast by class name.
Gaudi::Generic1D::extraEntries
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition: Generic1D.h:183
Gaudi::Generic1D::reset
bool reset() override
Reset the Histogram; as if just created.
Definition: Generic1D.h:187
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:202
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:45
Gaudi::Generic1D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:148
Gaudi::Generic1D::maxBinHeight
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition: Generic1D.h:100
Gaudi::Generic1D::name
std::string name() const
object name
Definition: Generic1D.h:69
Gaudi::Generic1D::print
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition: Generic1D.h:218
Gaudi::Generic1D::Generic1D
Generic1D()=default
Default constructor.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Generic1D::minBinHeight
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition: Generic1D.h:98
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:73
Gaudi::Generic1D::equivalentBinEntries
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:194
Gaudi::Generic1D::binMean
double binMean(int index) const override
The weighted mean of a bin.
Definition: Generic1D.h:168