BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes
HistogramAccumulator Class Reference

Class to accumulate and merge histograms in different ways. More...

#include <HistogramAccumulator.hh>

Inheritance diagram for HistogramAccumulator:
Inheritance graph
Collaboration diagram for HistogramAccumulator:
Collaboration graph

Public Member Functions

 HistogramAccumulator ()
 Default constructor only for ROOT reflexivity - not intended for use.
 
 HistogramAccumulator (TH1 *baseHistogram, const std::string &resultHistNameIn, const std::string &resultHistTitleIn)
 
 HistogramAccumulator (TH1 *baseHistogram, int nDimensionsIn, const std::string &resultHistName, const std::string &resultHistTitle)
 
virtual ~HistogramAccumulator ()
 
virtual void Accumulate (TH1 *newValue)
 
virtual TH1 * Terminate ()
 
TH1 * Result () const
 Accessor.
 
void AddNEmptyEntries (unsigned long i)
 
unsigned long N () const
 Access currently accumulated number of entries.
 

Protected Member Functions

virtual void AccumulateSingleValue (double oldMean, double oldVari, double x, double xVari, unsigned long nEntriesAccumulated, unsigned long nEntriesToAccumulate, double &newMean, double &newVari) const
 
 ClassDef (HistogramAccumulator, 1)
 

Protected Attributes

int nDimensions
 Number of dimensions.
 
unsigned long n
 Counter.
 
bool terminated
 Whether this instance has been finished.
 
const std::string resultHistName
 Name for resultant histogram.
 
const std::string resultHistTitle
 Title for resultant histogram.
 
TH1 * mean
 
TH1 * variance
 
TH1 * result
 

Detailed Description

Class to accumulate and merge histograms in different ways.

This acts as a base class to accumulate a single histogram from many. The default implementation is to calculate the mean and the standard error on the mean, however, the AccumulateSingleValue() function is virtual and may be overridden to provide different functionality.

TH1 is the base class of all ROOT histograms so even 2 and 3D histograms can be supplied through a TH1 pointer. These are statically cast to TH1D, TH2D and TH3D based on the number of dimensions (1,2 or 3 only).

A result histogram name can be specified. Note ROOT uses this to create objects in the currently open file (lots of static stuff) and therefore this should be unique from the supplied base histogram name!

Each instance is single use. Once terminated, the accumulation should not be used.

The algorithm used to calculate the mean and variance is one that supports online calculation and is numerically stable.

Normally, at least 2 entries should be accumulated to calculate the variance and to avoid nans from 1/(n-1), however, in this special case, the bin error is set to 0.

Author
Laurie Nevay

Definition at line 57 of file HistogramAccumulator.hh.

Constructor & Destructor Documentation

◆ HistogramAccumulator() [1/2]

HistogramAccumulator::HistogramAccumulator ( TH1 *  baseHistogram,
const std::string &  resultHistNameIn,
const std::string &  resultHistTitleIn 
)

Construct Accumulator based on another histogram of 1,2, or 3 dimensions. Inspects the TH1 (ie base class) instance to get the number of dimensions then uses the next constructor.

Definition at line 43 of file HistogramAccumulator.cc.

◆ HistogramAccumulator() [2/2]

HistogramAccumulator::HistogramAccumulator ( TH1 *  baseHistogram,
int  nDimensionsIn,
const std::string &  resultHistName,
const std::string &  resultHistTitle 
)

Construct accumulator based on another histogram of 1,2 or 3 dimensions. Note, the result name must be different from that of baseHistogram if baseHistogram exists in the currently open file.

Definition at line 49 of file HistogramAccumulator.cc.

References nDimensions, resultHistName, and resultHistTitle.

◆ ~HistogramAccumulator()

HistogramAccumulator::~HistogramAccumulator ( )
virtual

Destructor deletes mean and variance temporary histograms but leaks result as deleting it would remove it from the open file in ROOT.

Definition at line 128 of file HistogramAccumulator.cc.

Member Function Documentation

◆ Accumulate()

void HistogramAccumulator::Accumulate ( TH1 *  newValue)
virtual

Loop over the bins in a histogram and accumulate that bin from a new histogram ("newValue"). newValue is assumed to be the exact same as the baseHistogram the instance of this class was constructed with.

Reimplemented in HistogramAccumulatorMerge, and HistogramAccumulatorSum.

Definition at line 136 of file HistogramAccumulator.cc.

References AccumulateSingleValue(), n, and nDimensions.

Referenced by PerEntryHistogram::AccumulateCurrentEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AccumulateSingleValue()

void HistogramAccumulator::AccumulateSingleValue ( double  oldMean,
double  oldVari,
double  x,
double  xVari,
unsigned long  nEntriesAccumulated,
unsigned long  nEntriesToAccumulate,
double &  newMean,
double &  newVari 
) const
protectedvirtual

Accumulate a single value into the online mean and variance histograms. This by default accumulates the mean and variance with a new value x. The xVari argument is not used in the implementation provided with this class but is used in derived classes with different AccumulateSingleValue implementations.

Reimplemented in HistogramAccumulatorMerge.

Definition at line 343 of file HistogramAccumulator.cc.

Referenced by Accumulate().

Here is the caller graph for this function:

◆ AddNEmptyEntries()

void HistogramAccumulator::AddNEmptyEntries ( unsigned long  i)
inline

Permit the number of recorded entries to be incremented with zero values, ie just increment n.

Definition at line 96 of file HistogramAccumulator.hh.

References n.

Referenced by PerEntryHistogram::AddNEmptyEntries().

Here is the caller graph for this function:

◆ N()

unsigned long HistogramAccumulator::N ( ) const
inline

Access currently accumulated number of entries.

Definition at line 99 of file HistogramAccumulator.hh.

References n.

◆ Result()

TH1 * HistogramAccumulator::Result ( ) const
inline

Accessor.

Definition at line 92 of file HistogramAccumulator.hh.

◆ Terminate()

TH1 * HistogramAccumulator::Terminate ( )
virtual

Write the result to the result histogram. Calculate the standard error on the mean from the variance for the error in each bin.

Reimplemented in HistogramAccumulatorSum.

Definition at line 242 of file HistogramAccumulator.cc.

References n, and nDimensions.

Referenced by PerEntryHistogram::Terminate().

Here is the caller graph for this function:

Field Documentation

◆ mean

TH1* HistogramAccumulator::mean
protected

Definition at line 121 of file HistogramAccumulator.hh.

◆ n

unsigned long HistogramAccumulator::n
protected

◆ nDimensions

int HistogramAccumulator::nDimensions
protected

Number of dimensions.

Definition at line 116 of file HistogramAccumulator.hh.

Referenced by Accumulate(), HistogramAccumulatorMerge::Accumulate(), HistogramAccumulator(), and Terminate().

◆ result

TH1* HistogramAccumulator::result
protected

Definition at line 123 of file HistogramAccumulator.hh.

◆ resultHistName

const std::string HistogramAccumulator::resultHistName
protected

Name for resultant histogram.

Definition at line 119 of file HistogramAccumulator.hh.

Referenced by HistogramAccumulator().

◆ resultHistTitle

const std::string HistogramAccumulator::resultHistTitle
protected

Title for resultant histogram.

Definition at line 120 of file HistogramAccumulator.hh.

Referenced by HistogramAccumulator().

◆ terminated

bool HistogramAccumulator::terminated
protected

Whether this instance has been finished.

Definition at line 118 of file HistogramAccumulator.hh.

◆ variance

TH1* HistogramAccumulator::variance
protected

Definition at line 122 of file HistogramAccumulator.hh.


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