The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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
32namespace 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>
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>
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
#define GAUDI_API
Definition Kernel.h:49
Implementation of the AIDA IAnnotation interface class.
Definition Annotation.h:25
An IAxis represents a binned histogram axis.
Definition Axis.h:28
double minBinHeight() const override
Get the minimum height of the in-range bins.
Definition Generic1D.h:98
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition Generic1D.h:73
virtual double binRms(int index) const
Definition Generic1D.h:163
AIDA::Annotation m_annotation
Object annotations.
Definition Generic1D.h:138
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
double binMean(int index) const override
The weighted mean of a bin.
Definition Generic1D.h:168
int write(const char *file_name) const override
Write (ASCII) the histogram table into a file.
Definition Generic1D.h:237
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
int binEntries(int index) const override
Number of entries in the corresponding bin (ie the number of times fill was called for this bin).
std::string m_classType
Definition Generic1D.h:142
std::string name() const
object name
Definition Generic1D.h:69
int extraEntries() const override
Get the number of entries in the underflow and overflow bins.
Definition Generic1D.h:183
Axis & axis()
Access to axis object.
Definition Generic1D.h:77
Generic1D(IMPLEMENTATION *p)
constructor
Definition Generic1D.h:53
std::string title() const override
Get the title of the object.
Definition Generic1D.h:65
virtual const std::string & userLevelClassType() const
The AIDA user-level unterface leaf class type.
Definition Generic1D.h:57
bool add(const INTERFACE &profile) override
Modifies this IProfile1D by adding the contents of profile to it.
Definition Generic1D.h:208
std::ostream & write(std::ostream &s) const override
Write (ASCII) the histogram table into the output stream.
Definition Generic1D.h:226
int dimension() const override
Get the Histogram's dimension.
Definition Generic1D.h:126
bool reset() override
Reset the Histogram; as if just created.
Definition Generic1D.h:187
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
Generic1D()=default
Default constructor.
const AIDA::IAnnotation & annotation() const override
Access annotation object (cons)
Definition Generic1D.h:75
const Axis & axis() const override
Get the x axis of the IHistogram1D.
Definition Generic1D.h:79
Axis m_axis
Axis member.
Definition Generic1D.h:136
int coordToIndex(double coord) const override
Get the bin number corresponding to a given coordinate along the x axis.
Definition Generic1D.h:124
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition Generic1D.h:194
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
void * cast(const std::string &cl) const override
Manual cast by class name.
TObject * representation() const override
ROOT object implementation.
Definition Generic1D.h:61
bool setTitle(const std::string &title) override
Set the title of the object.
Definition Generic1D.h:148
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition Generic1D.h:92
Generic1D< INTERFACE, IMPLEMENTATION > Base
Definition Generic1D.h:47
double rms() const override
The RMS of the whole IHistogram1D.
Definition Generic1D.h:122
double binError(int index) const override
The error of a given bin.
Definition Generic1D.h:178
double sumAllBinHeights() const override
Get the sum of all the bins heights (including underflow and overflow bin).
Definition Generic1D.h:94
virtual int rIndex(int index) const
operator methods
Definition Generic1D.h:112
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition Generic1D.h:140
double maxBinHeight() const override
Get the maximum height of the in-range bins.
Definition Generic1D.h:100
int entries() const override
Get the number or all the entries.
Definition Generic1D.h:82
double mean() const override
The mean of the whole IHistogram1D.
Definition Generic1D.h:120
double sumExtraBinHeights() const override
Get the sum of the underflow and overflow bin height.
Definition Generic1D.h:96
std::ostream & print(std::ostream &s) const override
Print (ASCII) the histogram into the output stream.
Definition Generic1D.h:218
bool setName(const std::string &newName)
Set the name of the object.
Definition Generic1D.h:156
Common base class for all histograms Use is solely functional to minimize dynamic_casts inside Histog...
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1