The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
P1D.cpp
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#ifdef __ICC
12// disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
13// TODO: To be removed, since it comes from ROOT TMathBase.h
14# pragma warning( disable : 2259 )
15#endif
16
17#include "GaudiPI.h"
18#include <Gaudi/MonitoringHub.h>
20#include <GaudiCommonSvc/P1D.h>
22
23#include <cmath>
24
25std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( ISvcLocator* svcLocator, const std::string& path,
26 const std::string& title, int nBins, double xlow,
27 double xup, double ylow, double yup,
28 const std::string& opt ) {
29 auto _p = new TProfile( title.c_str(), title.c_str(), nBins, xlow, xup, ylow, yup, opt.c_str() );
30 auto p = new Profile1D( _p );
31 svcLocator->monitoringHub().registerEntity( "", path, "histogram:ProfileHistogram:double", *p );
32 return { p, p };
33}
34
35std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( ISvcLocator* svcLocator, const std::string& path,
36 const std::string& title, const Edges& e, double ylow,
37 double yup, const std::string& opt ) {
38 auto p =
39 new Profile1D( new TProfile( title.c_str(), title.c_str(), e.size() - 1, &e.front(), ylow, yup, opt.c_str() ) );
40 svcLocator->monitoringHub().registerEntity( "", path, "histogram:ProfileHistogram:double", *p );
41 return { p, p };
42}
43
44std::pair<DataObject*, AIDA::IProfile1D*> Gaudi::createProf1D( ISvcLocator* svcLocator, const std::string& path,
45 const AIDA::IProfile1D& hist ) {
47 auto n = ( h ? new Profile1D( new TProfile( *h ) ) : nullptr );
48 if ( n ) { svcLocator->monitoringHub().registerEntity( "", path, "histogram:ProfileHistogram:double", *n ); }
49 return { n, n };
50}
51
52namespace Gaudi {
53 template <>
55 return int( m_rep->GetBinEntries( rIndex( index ) ) + 0.5 );
56 }
57
58 template <>
59 void* Generic1D<AIDA::IProfile1D, TProfile>::cast( const std::string& className ) const {
60 return className == "AIDA::IProfile1D"
61 ? const_cast<AIDA::IProfile1D*>( static_cast<const AIDA::IProfile1D*>( this ) )
62 : className == "AIDA::IProfile" ? const_cast<AIDA::IProfile*>( static_cast<const AIDA::IProfile*>( this ) )
63 : className == "AIDA::IBaseHistogram"
64 ? const_cast<AIDA::IBaseHistogram*>( static_cast<const AIDA::IBaseHistogram*>( this ) )
65 : nullptr;
66 }
67
68 template <>
70 TProfile* imp = dynamic_cast<TProfile*>( rep );
71 if ( !imp ) throw std::runtime_error( "Cannot adopt native histogram representation." );
72 m_rep.reset( imp );
73 m_axis.initialize( m_rep->GetXaxis(), true );
74 const TArrayD* a = m_rep->GetSumw2();
75 if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
76 setTitle( m_rep->GetTitle() );
77 }
78} // namespace Gaudi
79
80Gaudi::Profile1D::Profile1D() : Base( new TProfile() ) { init( "", false ); }
81
82Gaudi::Profile1D::Profile1D( TProfile* rep ) : Base( rep ) { init( m_rep->GetTitle() ); }
83
84void Gaudi::Profile1D::init( const std::string& title, bool initialize_axis ) {
85 m_classType = "IProfile1D";
86 setTitle( title );
87 setName( title );
88 if ( initialize_axis ) { axis().initialize( m_rep->GetXaxis(), false ); }
89 // m_rep->SetErrorOption("s");
90 m_rep->SetDirectory( nullptr );
91 m_sumEntries = 0;
92}
93
94bool Gaudi::Profile1D::setBinContents( int i, int entries, double height, double /*error*/, double spread,
95 double /* centre */ ) {
96 m_rep->SetBinEntries( rIndex( i ), entries );
97 // set content takes in root height * entries
98 m_rep->SetBinContent( rIndex( i ), height * entries );
99 // set error takes sqrt of bin sum(w*y**2)
100 double sumwy2Bin = ( spread * spread + height * height ) * entries;
101 m_rep->SetBinError( rIndex( i ), sqrt( sumwy2Bin ) );
103 // not very efficient (but do evey bin since root cannot figure out by himself)
104 m_rep->SetEntries( m_sumEntries );
105 return true;
106}
107
108bool Gaudi::Profile1D::fill( double x, double y, double weight ) {
109 // avoid race conditions when filling the profile
110 auto guard = std::scoped_lock{ m_fillSerialization };
111 m_rep->Fill( x, y, weight );
112 return true;
113}
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 title() const override
Definition Generic1D.h:65
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
void * cast(const std::string &cl) const override
Manual cast by class name.
bool setTitle(const std::string &title) override
Definition Generic1D.h:148
Generic1D< AIDA::IProfile1D, TProfile > Base
Definition Generic1D.h:47
virtual int rIndex(int index) const
Definition Generic1D.h:112
bool setName(const std::string &newName)
Definition Generic1D.h:156
AIDA implementation for 1 D profiles using ROOT TProfile.
Definition P1D.h:32
Profile1D()
Default Constructor.
Definition P1D.cpp:80
bool fill(double x, double y, double weight=1.) override
Fill the Profile1D with a value and the corresponding weight.
Definition P1D.cpp:108
std::mutex m_fillSerialization
Definition P1D.h:55
virtual bool setBinContents(int i, int entries, double height, double error, double spread, double centre)
Definition P1D.cpp:94
void init(const std::string &title, bool initialize_axis=true)
Definition P1D.cpp:84
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
GAUDI_API ISvcLocator * svcLocator()
std::pair< DataObject *, AIDA::IProfile1D * > createProf1D(ISvcLocator *svcLocator, const std::string &path, const AIDA::IProfile1D &hist)
Copy constructor.
T * getRepresentation(const Q &hist)