| The Gaudi Framework v32r1 (f65d50dc)

P1D.cpp
Go to the documentation of this file.
1 #ifdef __ICC
2 // disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
3 // TODO: To be removed, since it comes from ROOT TMathBase.h
4 # pragma warning( disable : 2259 )
5 #endif
6 #ifdef WIN32
7 // Disable warning
8 // warning C4996: 'sprintf': This function or variable may be unsafe.
9 // coming from TString.h
10 # pragma warning( disable : 4996 )
11 #endif
12
13 #include "GaudiPI.h"
14 #include <GaudiCommonSvc/HistogramUtility.h>
15 #include <GaudiCommonSvc/P1D.h>
16 #include <GaudiKernel/ObjectFactory.h>
17 #include <cmath>
18
19 std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( const std::string& title, int nBins, double xlow,
20 double xup, double ylow, double yup,
21 const std::string& opt ) {
22 auto _p = new TProfile( title.c_str(), title.c_str(), nBins, xlow, xup, ylow, yup, opt.c_str() );
23 auto p = new Profile1D( _p );
24 return {p, p};
25 }
26
27 std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( const std::string& title, const Edges& e, double ylow,
28 double yup, const std::string& opt ) {
29 auto p =
30 new Profile1D( new TProfile( title.c_str(), title.c_str(), e.size() - 1, &e.front(), ylow, yup, opt.c_str() ) );
31 return {p, p};
32 }
33
34 std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( const AIDA::IProfile1D& hist ) {
35 TProfile* h = getRepresentation<AIDA::IProfile1D, TProfile>( hist );
36 auto n = ( h ? new Profile1D( new TProfile( *h ) ) : nullptr );
37 return {n, n};
38 }
39
40 namespace Gaudi {
41 template <>
42 int Generic1D<AIDA::IProfile1D, TProfile>::binEntries( int index ) const {
43 return int( m_rep->GetBinEntries( rIndex( index ) ) + 0.5 );
44 }
45
46 template <>
47 void* Generic1D<AIDA::IProfile1D, TProfile>::cast( const std::string& className ) const {
48 return className == "AIDA::IProfile1D"
49 ? const_cast<AIDA::IProfile1D*>( static_cast<const AIDA::IProfile1D*>( this ) )
50 : className == "AIDA::IProfile"
51 ? const_cast<AIDA::IProfile*>( static_cast<const AIDA::IProfile*>( this ) )
52 : className == "AIDA::IBaseHistogram"
53 ? const_cast<AIDA::IBaseHistogram*>( static_cast<const AIDA::IBaseHistogram*>( this ) )
54 : nullptr;
55 }
56
57 template <>
58 void Generic1D<AIDA::IProfile1D, TProfile>::adoptRepresentation( TObject* rep ) {
59 TProfile* imp = dynamic_cast<TProfile*>( rep );
60 if ( !imp ) throw std::runtime_error( "Cannot adopt native histogram representation." );
61 m_rep.reset( imp );
62 m_axis.initialize( m_rep->GetXaxis(), true );
63 const TArrayD* a = m_rep->GetSumw2();
64 if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
65 setTitle( m_rep->GetTitle() );
66 }
67 } // namespace Gaudi
68
69 Gaudi::Profile1D::Profile1D() : Base( new TProfile() ) { init( "", false ); }
70
71 Gaudi::Profile1D::Profile1D( TProfile* rep ) : Base( rep ) { init( m_rep->GetTitle() ); }
72
73 void Gaudi::Profile1D::init( const std::string& title, bool initialize_axis ) {
74 m_classType = "IProfile1D";
75 setTitle( title );
76 setName( title );
77 if ( initialize_axis ) { axis().initialize( m_rep->GetXaxis(), false ); }
78 // m_rep->SetErrorOption("s");
79 m_rep->SetDirectory( nullptr );
80 m_sumEntries = 0;
81 }
82
83 bool Gaudi::Profile1D::setBinContents( int i, int entries, double height, double /*error*/, double spread,
84 double /* centre */ ) {
85 m_rep->SetBinEntries( rIndex( i ), entries );
86 // set content takes in root height * entries
87 m_rep->SetBinContent( rIndex( i ), height * entries );
88 // set error takes sqrt of bin sum(w*y**2)
89 double sumwy2Bin = ( spread * spread + height * height ) * entries;
90 m_rep->SetBinError( rIndex( i ), sqrt( sumwy2Bin ) );
91 m_sumEntries += entries;
92 // not very efficient (but do evey bin since root cannot figure out by himself)
93 m_rep->SetEntries( m_sumEntries );
94 return true;
95 }
96
97 #ifdef __ICC
98 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
99 // The comparison is correct
100 # pragma warning( disable : 1572 )
101 #endif
102 bool Gaudi::Profile1D::fill( double x, double y, double weight ) {
103 // avoid race conditions when filling the profile
104 std::lock_guard<std::mutex> guard( m_fillSerialization );
105 ( weight == 1. ) ? m_rep->Fill( x, y ) : m_rep->Fill( x, y, weight );
106 return true;
107 }
Gaudi::Generic1D::cast
void * cast(const std::string &cl) const override
Manual cast by class name.
ObjectFactory.h
Gaudi::Profile1D::setBinContents
virtual bool setBinContents(int i, int entries, double height, double error, double spread, double centre)
Definition: P1D.cpp:83
Gaudi::Generic1D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
GaudiPI.h
P1D.h
std::string
STL class.
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
Gaudi::Generic1D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:130
std::runtime_error
STL class.
std::pair
Gaudi::Accumulators::sqrt
auto sqrt(std::chrono::duration< Rep, Period > d)
sqrt for std::chrono::duration
Definition: Counters.h:28
Gaudi::Profile1D::Profile1D
Profile1D()
Default Constructor.
Definition: P1D.cpp:69
std::lock_guard
std::string::c_str
T c_str(T... args)
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::Profile1D::fill
bool fill(double x, double y, double weight=1.) override
Fill the Profile1D with a value and the corresponding weight.
Definition: P1D.cpp:102
HistogramUtility.h
Gaudi::Profile1D::init
void init(const std::string &title, bool initialize_axis=true)
Definition: P1D.cpp:73
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::createProf1D
std::pair< DataObject *, AIDA::IProfile1D * > createProf1D(const AIDA::IProfile1D &hist)
Copy constructor.
Gaudi::Generic1D
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:35