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