Skip to content

Commit

Permalink
Add experimental support for histograms as counters (!1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikacic committed Dec 13, 2019
2 parents 3560174 + f678045 commit 6475930
Show file tree
Hide file tree
Showing 14 changed files with 984 additions and 316 deletions.
2 changes: 2 additions & 0 deletions GaudiExamples/options/Histograms.opts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ MessageSvc.OutputLevel = 3;

// Example of Histogramming using GaudiHistoAlg base class from GaudiAlg
ApplicationMgr.TopAlg += { "GaudiHistoAlgorithm/SimpleHistos" };
ApplicationMgr.TopAlg += { "Gaudi::Examples::R7::GaudiHistoAlgorithm/SimpleR7Histos" };
ApplicationMgr.TopAlg += { "Gaudi::Examples::Boost::GaudiHistoAlgorithm/SimpleBoostHistos" };
// Exampel of Histograming using "histogram proeprties"
ApplicationMgr.TopAlg += { "Gaudi::Examples::HistoProps/Histos2" };
SimpleHistos.HistoPrint = true;
Expand Down
10 changes: 8 additions & 2 deletions GaudiExamples/options/Histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

#from GaudiExamples.GaudiExamplesConf import GaudiHistoAlgorithm
from Configurables import GaudiHistoAlgorithm
from Configurables import Gaudi__Examples__R7__GaudiHistoAlgorithm as R7HistoAlg
from Configurables import Gaudi__Examples__Boost__GaudiHistoAlgorithm as BoostHistoAlg

alg = GaudiHistoAlgorithm('SimpleHistos', HistoPrint=True, OutputLevel=DEBUG)
algs = [
GaudiHistoAlgorithm('SimpleHistos', HistoPrint=True, OutputLevel=DEBUG),
R7HistoAlg('SimpleR7Histos', OutputLevel=DEBUG),
BoostHistoAlg('SimpleBoostHistos', OutputLevel=DEBUG),
]

ApplicationMgr(
EvtMax=5000, EvtSel='NONE', HistogramPersistency='ROOT', TopAlg=[alg])
EvtMax=50000, EvtSel='NONE', HistogramPersistency='ROOT', TopAlg=algs)
13 changes: 9 additions & 4 deletions GaudiExamples/options/HistogramsTiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

#HistogramSvc('HistogramDataSvc').Input = [ "InFile DATAFILE='../data/input.hbook' TYP='HBOOK'" ]

from Configurables import HistoTimingAlg
from Configurables import (
HistoTimingAlg, Gaudi__Examples__R7__HistoTimingAlg as R7HistoTiming,
Gaudi__Examples__Boost__HistoTimingAlg as BoostHistoTiming)

seq = GaudiSequencer("TimingSeq", MeasureTime=True)
seq.Members = [HistoTimingAlg("DirectFill"), HistoTimingAlg("LookupFill")]
HistoTimingAlg("DirectFill").UseLookup = False
HistoTimingAlg("LookupFill").UseLookup = True
seq.Members = [
HistoTimingAlg("DirectFill", UseLookup=False),
HistoTimingAlg("LookupFill", UseLookup=True),
R7HistoTiming("R7Timing"),
BoostHistoTiming("BoostTiming"),
]
for alg in seq.Members:
alg.NumTracks = 20
alg.NumHistos = 30
Expand Down
64 changes: 0 additions & 64 deletions GaudiExamples/options/Histograms_c2.py

This file was deleted.

10 changes: 6 additions & 4 deletions GaudiExamples/options/Histograms_with_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# algorithms
algorithms = [
C.GaudiHistoAlgorithm('SimpleHistos', HistoPrint=True, OutputLevel=3),
C.Gaudi.Examples.HistoProps('Histos2', Histo2=(-5, 5, 200)),
C.Gaudi.Examples.R7.GaudiHistoAlgorithm('SimpleR7Histos', OutputLevel=2),
C.Gaudi.Examples.Boost.GaudiHistoAlgorithm(
'SimpleBoostHistos', OutputLevel=2),
]

app = C.ApplicationMgr(
'ApplicationMgr',
TopAlg=['SimpleHistos', 'Histos2'],
TopAlg=['SimpleHistos', 'SimpleR7Histos', 'SimpleBoostHistos'],
EvtMax=50000,
EvtSel='NONE',
)
Expand All @@ -37,5 +39,5 @@

C.HistogramSvc(
'HistogramDataSvc',
Predefined1DHistos={"/stat/Histos2/2": ("TEST2", -100, 200)},
OutputLevel=2)
OutputLevel=2,
Input=["InFile DATAFILE='../data/input.hbook' TYP='HBOOK'"])
9 changes: 6 additions & 3 deletions GaudiExamples/python/GaudiExamples/TestConfig2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
def setUpAlgorithms():
algorithms = [
C.GaudiHistoAlgorithm('SimpleHistos', HistoPrint=True, OutputLevel=3),
C.Gaudi.Examples.HistoProps('Histos2', Histo2=(-5, 5, 200)),
C.Gaudi.Examples.R7.GaudiHistoAlgorithm(
'SimpleR7Histos', OutputLevel=2),
C.Gaudi.Examples.Boost.GaudiHistoAlgorithm(
'SimpleBoostHistos', OutputLevel=2),
]
app = C.ApplicationMgr(TopAlg=algorithms)
return algorithms + [app]
Expand All @@ -24,8 +27,8 @@ def histogramWriting(filename):
C.RootHistCnv.PersSvc('RootHistSvc', OutputFile=filename),
C.HistogramSvc(
'HistogramDataSvc',
Predefined1DHistos={"/stat/Histos2/2": ("TEST2", -100, 200)},
OutputLevel=2)
OutputLevel=2,
Input=["InFile DATAFILE='../data/input.hbook' TYP='HBOOK'"])
]


Expand Down
132 changes: 132 additions & 0 deletions GaudiExamples/src/Histograms/Boost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <Gaudi/Accumulators/Histogram.h>
#include <Gaudi/Algorithm.h>
#include <GaudiKernel/RndmGenerators.h>
#include <boost/histogram.hpp>
#include <vector>

// helper type aliases
using BH1D = decltype( boost::histogram::make_histogram( boost::histogram::axis::regular<>{10, 0., 10.} ) );
using BH2D = decltype( boost::histogram::make_histogram( boost::histogram::axis::regular<>{10, 0., 10.},
boost::histogram::axis::regular<>{10, 0., 10.} ) );

namespace Gaudi {
namespace Examples {
namespace Boost {
/// Simple timing of Boost.Histograms filling.
class HistoTimingAlg : public Gaudi::Algorithm {
public:
using Gaudi::Algorithm::Algorithm;

StatusCode initialize() override {
// must be called first
const StatusCode sc = Algorithm::initialize();
if ( sc.isFailure() ) return sc;

// random number generator
auto randSvc = service<IRndmGenSvc>( "RndmGenSvc", true );
if ( !randSvc || !m_rand.initialize( randSvc, Rndm::Flat( 0., 1. ) ) ) {
error() << "Unable to create Random generator" << endmsg;
return StatusCode::FAILURE;
}

m_histos.reserve( m_nHistos );
for ( unsigned int iH = 0; iH < m_nHistos; ++iH ) {
m_histos.emplace_back( boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, 0., 1.} ) );
}

return sc;
}

StatusCode execute( const EventContext& ) const override {
for ( unsigned int iT = 0; iT < m_nTracks; ++iT ) {
for ( auto& h : m_histos ) { h( m_rand() ); }
}
return StatusCode::SUCCESS;
}

private:
mutable Rndm::Numbers m_rand;

mutable std::vector<BH1D> m_histos;

Gaudi::Property<unsigned int> m_nHistos{this, "NumHistos", 20, ""};
Gaudi::Property<unsigned int> m_nTracks{this, "NumTracks", 30, ""};
};
DECLARE_COMPONENT( HistoTimingAlg )

/// Example of algorithm using Boost.Histograms accumulators.
class GaudiHistoAlgorithm : public Gaudi::Algorithm {
public:
using Gaudi::Algorithm::Algorithm;

StatusCode execute( const EventContext& ) const override {
// some random number generators, just to provide numbers
static Rndm::Numbers Gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
static Rndm::Numbers Flat( randSvc(), Rndm::Flat( -10.0, 10.0 ) );
static Rndm::Numbers Expo( randSvc(), Rndm::Exponential( 1.0 ) );
static Rndm::Numbers Breit( randSvc(), Rndm::BreitWigner( 0.0, 1.0 ) );
static Rndm::Numbers Poisson( randSvc(), Rndm::Poisson( 2.0 ) );
static Rndm::Numbers Binom( randSvc(), Rndm::Binomial( 8, 0.25 ) );

// cache some numbers
const double gauss( Gauss() );
const double flat( Flat() );
const double expo( Expo() );
const double breit( Breit() );
const double poisson( Poisson() );
const double binom( Binom() );

// =============== Histogramming Examples =================================

m_h_gauss( gauss );
m_h_expo( expo );
m_h_breit( breit );
m_h_poisson( poisson );
m_h_binom( binom );

m_h_gaussVflat( flat, gauss );
m_h_expoVflat( flat, expo );
m_h_binVpois( poisson, binom );
m_h_expoVpois( poisson, expo );

if ( m_nCalls.nEntries() == 0 ) always() << "Filling Histograms...... Please be patient !" << endmsg;
++m_nCalls;
return StatusCode::SUCCESS;
}

private:
mutable Gaudi::Accumulators::Counter<> m_nCalls{this, "calls"};

mutable Gaudi::Accumulators::Histogram<BH1D> m_h_gauss{
this, "Gaussian mean=0, sigma=1",
boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, -5, 5} )};
mutable Gaudi::Accumulators::Histogram<BH1D> m_h_expo{
this, "Exponential", boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, 0, 5} )};
mutable Gaudi::Accumulators::Histogram<BH1D> m_h_breit{
this, "Breit", boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, -5, 5} )};
mutable Gaudi::Accumulators::Histogram<BH1D> m_h_poisson{
this, "Poisson", boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, -5, 5} )};
mutable Gaudi::Accumulators::Histogram<BH1D> m_h_binom{
this, "Binominal", boost::histogram::make_histogram( boost::histogram::axis::regular<>{100, -5, 5} )};

mutable Gaudi::Accumulators::Histogram<BH2D> m_h_gaussVflat{
this, "Gaussian V Flat",
boost::histogram::make_histogram( boost::histogram::axis::regular<>{50, -5, 5},
boost::histogram::axis::regular<>{50, -5, 5} )};
mutable Gaudi::Accumulators::Histogram<BH2D> m_h_expoVflat{
this, "Exponential V Flat",
boost::histogram::make_histogram( boost::histogram::axis::regular<>{50, -5, 5},
boost::histogram::axis::regular<>{50, 0, 5} )};
mutable Gaudi::Accumulators::Histogram<BH2D> m_h_binVpois{
this, "Binom V poisson",
boost::histogram::make_histogram( boost::histogram::axis::regular<>{50, -5, 5},
boost::histogram::axis::regular<>{50, -5, 5} )};
mutable Gaudi::Accumulators::Histogram<BH2D> m_h_expoVpois{
this, "Expo V poisson",
boost::histogram::make_histogram( boost::histogram::axis::regular<>{50, -5, 5},
boost::histogram::axis::regular<>{50, -5, 5} )};
};
DECLARE_COMPONENT( GaudiHistoAlgorithm )
} // namespace Boost
} // namespace Examples
} // namespace Gaudi
Loading

0 comments on commit 6475930

Please sign in to comment.