The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
Gaudi::Histogram1D Class Reference

AIDA implementation for 1 D histograms using ROOT THD1. More...

#include </builds/gaudi/Gaudi/GaudiCommonSvc/include/GaudiCommonSvc/H1D.h>

Inheritance diagram for Gaudi::Histogram1D:
Collaboration diagram for Gaudi::Histogram1D:

Public Member Functions

 Histogram1D ()
 Standard constructor.
 
 Histogram1D (TH1D *rep)
 Standard constructor with initialization. The histogram representation will be adopted.
 
void adoptRepresentation (TObject *rep) override
 Adopt ROOT histogram representation.
 
virtual bool setBinContents (int i, int entries, double height, double error, double centre)
 set bin content (entries and centre are not used )
 
bool reset () override
 
virtual bool setStatistics (int allEntries, double eqBinEntries, double mean, double rms)
 set histogram statistics
 
bool fill (double x, double weight) override
 Fill the Profile1D with a value and the corresponding weight.
 
bool setRms (double rms)
 Update histogram RMS.
 
void copyFromAida (const AIDA::IHistogram1D &h)
 Create new histogram from any AIDA based histogram.
 
const CLIDclID () const override
 Retrieve reference to class defininition identifier.
 
StreamBufferserialize (StreamBuffer &s)
 Serialization mechanism, Serialize the object for reading.
 
StreamBufferserialize (StreamBuffer &s) const
 Serialization mechanism, Serialize the object for writing.
 
- Public Member Functions inherited from DataObject
 DataObject ()
 Standard Constructor.
 
 DataObject (const DataObject &rhs)
 Copy Constructor.
 
DataObjectoperator= (const DataObject &rhs)
 Assignment Operator.
 
 DataObject (DataObject &&rhs)
 Move Constructor.
 
DataObjectoperator= (DataObject &&rhs)
 Move Assignment Operator.
 
virtual ~DataObject ()
 Standard Destructor.
 
virtual unsigned long addRef ()
 Add reference to object.
 
virtual unsigned long release ()
 release reference to object
 
const std::string & name () const
 Retreive DataObject name. It is the name when registered in the store.
 
virtual StatusCode update ()
 Provide empty placeholder for internal object reconfiguration callback.
 
void setRegistry (IRegistry *pRegistry)
 Set pointer to Registry.
 
IRegistryregistry () const
 Get pointer to Registry.
 
LinkManagerlinkMgr ()
 Retrieve Link manager.
 
const LinkManagerlinkMgr () const
 
unsigned char version () const
 Retrieve version number of this object representation.
 
void setVersion (unsigned char vsn)
 Set version number of this object representation.
 
unsigned long refCount () const
 Return the refcount.
 
virtual std::ostream & fillStream (std::ostream &s) const
 Fill the output stream (ASCII)
 
- Public Member Functions inherited from Gaudi::Generic1D< AIDA::IHistogram1D, TH1D >
void * cast (const std::string &className) const
 
int binEntries (int index) const
 
int binEntries (int index) const
 
void * cast (const std::string &className) const
 
 Generic1D ()=default
 Default constructor.
 
virtual const std::string & userLevelClassType () const
 The AIDA user-level unterface leaf class type.
 
void * cast (const std::string &cl) const override
 Manual cast by class name.
 
void * cast (const std::string &className) const
 
void * cast (const std::string &className) const
 
TObject * representation () const override
 ROOT object implementation.
 
std::string title () const override
 Get the title of the object.
 
bool setTitle (const std::string &title) override
 Set the title of the object.
 
std::string name () const
 object name
 
bool setName (const std::string &newName)
 Set the name of the object.
 
AIDA::IAnnotation & annotation () override
 Access annotation object.
 
const AIDA::IAnnotation & annotation () const override
 Access annotation object (cons)
 
Axisaxis ()
 Access to axis object.
 
const Axisaxis () const override
 Get the x axis of the IHistogram1D.
 
int entries () const override
 Get the number or all the entries.
 
int allEntries () const override
 Get the number or all the entries, both in range and underflow/overflow bins of the IProfile.
 
int extraEntries () const override
 Get the number of entries in the underflow and overflow bins.
 
int binEntries (int index) const override
 Number of entries in the corresponding bin (ie the number of times fill was called for this bin).
 
int binEntries (int index) const
 
int binEntries (int index) const
 
virtual double binRms (int index) const
 
double sumBinHeights () const override
 Get the sum of in range bin heights in the IProfile.
 
double sumAllBinHeights () const override
 Get the sum of all the bins heights (including underflow and overflow bin).
 
double sumExtraBinHeights () const override
 Get the sum of the underflow and overflow bin height.
 
double minBinHeight () const override
 Get the minimum height of the in-range bins.
 
double maxBinHeight () const override
 Get the maximum height of the in-range bins.
 
virtual double equivalentBinEntries () const
 Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
 
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 given scale factor.
 
bool reset () override
 Reset the Histogram; as if just created.
 
bool add (const AIDA::IHistogram1D &profile) override
 Modifies this IProfile1D by adding the contents of profile to it.
 
virtual int rIndex (int index) const
 operator methods
 
double binMean (int index) const override
 The weighted mean of a bin.
 
double binHeight (int index) const override
 Total height of the corresponding bin (ie the sum of the weights in this bin).
 
double binError (int index) const override
 The error of a given bin.
 
double mean () const override
 The mean of the whole IHistogram1D.
 
double rms () const override
 The RMS of the whole IHistogram1D.
 
int coordToIndex (double coord) const override
 Get the bin number corresponding to a given coordinate along the x axis.
 
int dimension () const override
 Get the Histogram's dimension.
 
std::ostream & print (std::ostream &s) const override
 Print (ASCII) the histogram into the output stream.
 
std::ostream & write (std::ostream &s) const override
 Write (ASCII) the histogram table into the output stream.
 
int write (const char *file_name) const override
 Write (ASCII) the histogram table into a file.
 
- Public Member Functions inherited from Gaudi::HistogramBase
virtual ~HistogramBase ()=default
 

Static Public Member Functions

static const CLIDclassID ()
 
- Static Public Member Functions inherited from DataObject
static const CLIDclassID ()
 Retrieve reference to class definition structure (static access)
 

Protected Attributes

double m_sumwx = 0
 cache sumwx when setting contents since I don't have bin mean
 
- Protected Attributes inherited from Gaudi::Generic1D< AIDA::IHistogram1D, TH1D >
Axis m_axis
 Axis member.
 
AIDA::Annotation m_annotation
 Object annotations.
 
std::unique_ptr< TH1D > m_rep
 Reference to underlying implementation.
 
std::string m_classType
 
int m_sumEntries
 

Private Member Functions

void init (const std::string &title, bool initialize_axis=true)
 
void initSums ()
 

Private Attributes

std::mutex m_fillSerialization
 

Friends

void reset (Histogram1D &h)
 
void to_json (nlohmann::json &j, Gaudi::Histogram1D const &h)
 conversion to json via nlohmann library
 

Additional Inherited Members

- Public Types inherited from Gaudi::Generic1D< AIDA::IHistogram1D, TH1D >
typedef Generic1D< AIDA::IHistogram1D, TH1D > Base
 
- Protected Member Functions inherited from Gaudi::Generic1D< AIDA::IHistogram1D, TH1D >
 Generic1D (TH1D *p)
 constructor
 

Detailed Description

AIDA implementation for 1 D histograms using ROOT THD1.

Author
M.Frank

Definition at line 31 of file H1D.h.

Constructor & Destructor Documentation

◆ Histogram1D() [1/2]

Gaudi::Histogram1D::Histogram1D ( )

Standard constructor.

Definition at line 73 of file H1D.cpp.

73: Base( new TH1D() ) { init( "", false ); }
Generic1D< AIDA::IHistogram1D, TH1D > Base
Definition Generic1D.h:47
void init(const std::string &title, bool initialize_axis=true)
Definition H1D.cpp:81

◆ Histogram1D() [2/2]

Gaudi::Histogram1D::Histogram1D ( TH1D * rep)

Standard constructor with initialization. The histogram representation will be adopted.

Definition at line 75 of file H1D.cpp.

75 {
76 m_rep.reset( rep );
77 init( m_rep->GetTitle() );
78 initSums();
79}
void initSums()
Definition H1D.cpp:92

Member Function Documentation

◆ adoptRepresentation()

void Gaudi::Histogram1D::adoptRepresentation ( TObject * rep)
overridevirtual

Adopt ROOT histogram representation.

Reimplemented from Gaudi::Generic1D< AIDA::IHistogram1D, TH1D >.

Definition at line 108 of file H1D.cpp.

108 {
110 if ( m_rep ) {
111 init( m_rep->GetTitle() );
112 initSums();
113 }
114}
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.

◆ classID()

static const CLID & Gaudi::Histogram1D::classID ( )
inlinestatic

Definition at line 66 of file H1D.h.

66{ return CLID_H1D; }

◆ clID()

const CLID & Gaudi::Histogram1D::clID ( ) const
inlineoverridevirtual

Retrieve reference to class defininition identifier.

Reimplemented from DataObject.

Definition at line 65 of file H1D.h.

65{ return classID(); }
static const CLID & classID()
Definition H1D.h:66

◆ copyFromAida()

void Gaudi::Histogram1D::copyFromAida ( const AIDA::IHistogram1D & h)

Create new histogram from any AIDA based histogram.

Definition at line 167 of file H1D.cpp.

167 {
168 // implement here the copy
169 std::string title = h.title() + "Copy";
170 if ( h.axis().isFixedBinning() ) {
171 m_rep.reset(
172 new TH1D( title.c_str(), title.c_str(), h.axis().bins(), h.axis().lowerEdge(), h.axis().upperEdge() ) );
173 } else {
174 Edges e;
175 for ( int i = 0; i < h.axis().bins(); ++i ) { e.push_back( h.axis().binLowerEdge( i ) ); }
176 // add also upperedges at the end
177 e.push_back( h.axis().upperEdge() );
178 m_rep.reset( new TH1D( title.c_str(), title.c_str(), e.size() - 1, &e.front() ) );
179 }
180 m_axis.initialize( m_rep->GetXaxis(), false );
181 m_rep->Sumw2();
182 m_sumEntries = 0;
183 m_sumwx = 0;
184 // sumw
185 double sumw = h.sumBinHeights();
186 // sumw2
187 double sumw2 = 0;
188 if ( std::abs( h.equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
189 sumw2 = ( sumw * sumw ) / h.equivalentBinEntries();
190
191 double sumwx = h.mean() * h.sumBinHeights();
192 double sumwx2 = ( h.mean() * h.mean() + h.rms() * h.rms() ) * h.sumBinHeights();
193
194 // copy the contents in
195 for ( int i = -2; i < axis().bins(); ++i ) {
196 // root binning starts from one !
197 m_rep->SetBinContent( rIndex( i ), h.binHeight( i ) );
198 m_rep->SetBinError( rIndex( i ), h.binError( i ) );
199 }
200 // need to do set entries after setting contents otherwise root will recalulate them
201 // taking into account how many time SetBinContents() has been called
202 m_rep->SetEntries( h.allEntries() );
203 // stat vector
204 std::vector<double> stat( 11 );
205 stat[0] = sumw;
206 stat[1] = sumw2;
207 stat[2] = sumwx;
208 stat[3] = sumwx2;
209 m_rep->PutStats( &stat.front() );
210}
int bins() const override
The number of bins (excluding underflow and overflow) on the IAxis.
Definition Axis.h:90
std::string title() const override
Definition Generic1D.h:65
virtual int rIndex(int index) const
Definition Generic1D.h:112
double m_sumwx
cache sumwx when setting contents since I don't have bin mean
Definition H1D.h:38
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition ParticleID.h:191

◆ fill()

bool Gaudi::Histogram1D::fill ( double x,
double weight )
override

Fill the Profile1D with a value and the corresponding weight.

Definition at line 160 of file H1D.cpp.

160 {
161 // avoid race conditions when filling the histogram
162 auto guard = std::scoped_lock{ m_fillSerialization };
163 m_rep->Fill( x, weight );
164 return true;
165}
std::mutex m_fillSerialization
Definition H1D.h:80

◆ init()

void Gaudi::Histogram1D::init ( const std::string & title,
bool initialize_axis = true )
private

Definition at line 81 of file H1D.cpp.

81 {
82 m_classType = "IHistogram1D";
83 if ( initialize_axis ) { m_axis.initialize( m_rep->GetXaxis(), false ); }
84 const TArrayD* a = m_rep->GetSumw2();
85 if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
86 setTitle( title );
87 m_rep->SetDirectory( nullptr );
88 m_sumEntries = 0;
89 m_sumwx = 0;
90}
bool setTitle(const std::string &title) override
Definition Generic1D.h:148

◆ initSums()

void Gaudi::Histogram1D::initSums ( )
private

Definition at line 92 of file H1D.cpp.

92 {
93 m_sumwx = 0;
94 m_sumEntries = 0;
95 for ( int i = 1, n = m_rep->GetNbinsX(); i <= n; ++i ) {
96 m_sumwx += m_rep->GetBinContent( i ) * m_rep->GetBinCenter( i );
97 m_sumEntries += m_rep->GetBinContent( i );
98 }
99}

◆ reset()

bool Gaudi::Histogram1D::reset ( )
override

Definition at line 101 of file H1D.cpp.

101 {
102 m_sumwx = 0;
103 m_sumEntries = 0;
104 return Base::reset();
105}

◆ serialize() [1/2]

StreamBuffer & Gaudi::Histogram1D::serialize ( StreamBuffer & s)

Serialization mechanism, Serialize the object for reading.

Parameters
sthe StreamBuffer containing the data to be read
Returns
the resulting StreamBuffer, after reading

Definition at line 212 of file H1D.cpp.

212 {
213 // DataObject::serialize(s);
214 std::string title;
215 int size;
216 s >> size;
217 for ( int j = 0; j < size; j++ ) {
218 std::string key, value;
219 s >> key >> value;
220 if ( !annotation().addItem( key, value ) ) { annotation().setValue( key, value ); };
221 if ( "Title" == key ) { title = value; }
222 }
223 double lowerEdge, upperEdge, binHeight, binError;
224 int isFixedBinning, bins;
225 s >> isFixedBinning >> bins;
226
227 if ( isFixedBinning ) {
228 s >> lowerEdge >> upperEdge;
229 m_rep.reset( new TH1D( title.c_str(), title.c_str(), bins, lowerEdge, upperEdge ) );
230 } else {
231 Edges edges;
232 edges.resize( bins );
233 for ( int i = 0; i <= bins; ++i ) s >> edges[i];
234 m_rep.reset( new TH1D( title.c_str(), title.c_str(), edges.size() - 1, &edges.front() ) );
235 }
236 m_axis.initialize( m_rep->GetXaxis(), true );
237 m_rep->Sumw2();
238 m_sumEntries = 0;
239 m_sumwx = 0;
240
241 for ( int i = 0; i <= bins + 1; ++i ) {
242 s >> binHeight >> binError;
243 m_rep->SetBinContent( i, binHeight );
244 m_rep->SetBinError( i, binError );
245 }
246 Stat_t allEntries;
247 s >> allEntries;
248 m_rep->SetEntries( allEntries );
249 Stat_t stats[4]; // stats array
250 s >> stats[0] >> stats[1] >> stats[2] >> stats[3];
251 m_rep->PutStats( stats );
252 return s;
253}
AIDA::IAnnotation & annotation() override
Definition Generic1D.h:73
double binHeight(int index) const override
Definition Generic1D.h:173
double binError(int index) const override
Definition Generic1D.h:178
constexpr auto size(const T &, Args &&...) noexcept

◆ serialize() [2/2]

StreamBuffer & Gaudi::Histogram1D::serialize ( StreamBuffer & s) const

Serialization mechanism, Serialize the object for writing.

Parameters
sthe StreamBuffer where to write the data
Returns
the resulting StreamBuffer, after wrinting

Definition at line 255 of file H1D.cpp.

255 {
256 // DataObject::serialize(s);
257 s << static_cast<int>( annotation().size() );
258 for ( int i = 0; i < annotation().size(); i++ ) {
259 s << annotation().key( i );
260 s << annotation().value( i );
261 }
262 const AIDA::IAxis& axis( this->axis() );
263 const int isFixedBinning = axis.isFixedBinning();
264 const int bins = axis.bins();
265 s << isFixedBinning << bins;
266 if ( isFixedBinning ) {
267 s << axis.lowerEdge();
268 } else {
269 for ( int i = 0; i < bins; ++i ) s << axis.binLowerEdge( i );
270 }
271 s << axis.upperEdge();
272 for ( int i = 0; i <= bins + 1; ++i ) s << m_rep->GetBinContent( i ) << m_rep->GetBinError( i );
273
274 s << m_rep->GetEntries();
275 Stat_t stats[4]; // stats array
276 m_rep->GetStats( stats );
277 s << stats[0] << stats[1] << stats[2] << stats[3];
278 return s;
279}

◆ setBinContents()

bool Gaudi::Histogram1D::setBinContents ( int i,
int entries,
double height,
double error,
double centre )
virtual

set bin content (entries and centre are not used )

Definition at line 116 of file H1D.cpp.

116 {
117 m_rep->SetBinContent( rIndex( i ), height );
118 m_rep->SetBinError( rIndex( i ), error );
119 // accumulate sumwx for in range bins
120 if ( i != AIDA::IAxis::UNDERFLOW_BIN && i != AIDA::IAxis::OVERFLOW_BIN ) m_sumwx += centre * height;
122 return true;
123}

◆ setRms()

bool Gaudi::Histogram1D::setRms ( double rms)

Update histogram RMS.

Definition at line 125 of file H1D.cpp.

125 {
126 m_rep->SetEntries( m_sumEntries );
127 std::vector<double> stat( 11 );
128 // sum weights
129 stat[0] = sumBinHeights();
130 stat[1] = 0;
131 if ( std::abs( equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
132 stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
133 stat[2] = m_sumwx;
134 double mean = 0;
135 if ( std::abs( sumBinHeights() ) > std::numeric_limits<double>::epsilon() ) mean = m_sumwx / sumBinHeights();
136 stat[3] = ( mean * mean + rms * rms ) * sumBinHeights();
137 m_rep->PutStats( &stat.front() );
138 return true;
139}
virtual double equivalentBinEntries() const
Definition Generic1D.h:194

◆ setStatistics()

bool Gaudi::Histogram1D::setStatistics ( int allEntries,
double eqBinEntries,
double mean,
double rms )
virtual

set histogram statistics

Definition at line 142 of file H1D.cpp.

142 {
143 m_rep->SetEntries( allEntries );
144 // fill statistcal vector for Root
145 std::vector<double> stat( 11 );
146 // sum weights
147 stat[0] = sumBinHeights();
148 // sum weights **2
149 stat[1] = 0;
150 if ( std::abs( eqBinEntries ) > std::numeric_limits<double>::epsilon() )
151 stat[1] = ( sumBinHeights() * sumBinHeights() ) / eqBinEntries;
152 // sum weights * x
153 stat[2] = mean * sumBinHeights();
154 // sum weight * x **2
155 stat[3] = ( mean * mean + rms * rms ) * sumBinHeights();
156 m_rep->PutStats( &stat.front() );
157 return true;
158}

Friends And Related Symbol Documentation

◆ reset

void reset ( Histogram1D & h)
friend

Definition at line 53 of file H1D.h.

53{ h.reset(); }

◆ to_json

void to_json ( nlohmann::json & j,
Gaudi::Histogram1D const & h )
friend

conversion to json via nlohmann library

Definition at line 55 of file H1D.h.

55{ j = *h.m_rep.get(); }

Member Data Documentation

◆ m_fillSerialization

std::mutex Gaudi::Histogram1D::m_fillSerialization
private

Definition at line 80 of file H1D.h.

◆ m_sumwx

double Gaudi::Histogram1D::m_sumwx = 0
protected

cache sumwx when setting contents since I don't have bin mean

Definition at line 38 of file H1D.h.


The documentation for this class was generated from the following files: