The Gaudi Framework  master (37c0b60a)
H2D.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 #include "GaudiPI.h"
23 #include <Gaudi/MonitoringHub.h>
24 #include <GaudiCommonSvc/H1D.h>
25 #include <GaudiCommonSvc/H2D.h>
27 #include <GaudiCommonSvc/P1D.h>
29 #include <TH2D.h>
30 #include <TProfile.h>
31 #include <array>
32 #include <cmath>
33 
34 namespace {
35  using AIDA::IHistogram1D;
36  using AIDA::IHistogram2D;
37  using AIDA::IProfile1D;
38 } // namespace
39 
41  const std::string& title, int binsX, double iminX, double imaxX,
42  int binsY, double iminY, double imaxY ) {
43  auto p = new Histogram2D( new TH2D( title.c_str(), title.c_str(), binsX, iminX, imaxX, binsY, iminY, imaxY ) );
44  svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *p );
45  return { p, p };
46 }
47 
49  const std::string& title, const Edges& eX, const Edges& eY ) {
50  auto p = new Histogram2D(
51  new TH2D( title.c_str(), title.c_str(), eX.size() - 1, &eX.front(), eY.size() - 1, &eY.front() ) );
52  svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *p );
53  return { p, p };
54 }
55 
57  auto p = new Histogram2D( rep );
58  svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *p );
59  return { p, p };
60 }
61 
63  const IHistogram2D& hist ) {
64  TH2D* h = getRepresentation<AIDA::IHistogram2D, TH2D>( hist );
65  Histogram2D* n = h ? new Histogram2D( new TH2D( *h ) ) : nullptr;
66  if ( n ) { svcLocator->monitoringHub().registerEntity( path, h->GetName(), "histogram:Histogram:double", *n ); }
67  return { n, n };
68 }
69 
70 std::pair<DataObject*, IHistogram1D*> Gaudi::slice1DX( const std::string& nam, const IHistogram2D& hist, int first,
71  int last ) {
72  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
73  TH1D* t = r ? r->ProjectionX( "_px", first, last, "e" ) : nullptr;
74  if ( t ) t->SetName( nam.c_str() );
75  Histogram1D* p = ( t ? new Histogram1D( t ) : nullptr );
76  return { p, p };
77 }
78 
79 std::pair<DataObject*, IHistogram1D*> Gaudi::slice1DY( const std::string& nam, const IHistogram2D& hist, int first,
80  int last ) {
81  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
82  TH1D* t = r ? r->ProjectionY( "_py", first, last, "e" ) : nullptr;
83  if ( t ) t->SetName( nam.c_str() );
84  Histogram1D* p = ( t ? new Histogram1D( t ) : nullptr );
85  return { p, p };
86 }
87 
88 std::pair<DataObject*, IHistogram1D*> Gaudi::project1DY( const std::string& nam, const IHistogram2D& hist, int first,
89  int last ) {
90  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
91  TH1D* t = r ? r->ProjectionY( "_px", first, last, "e" ) : nullptr;
92  if ( t ) t->SetName( nam.c_str() );
93  Histogram1D* p = ( t ? new Histogram1D( t ) : nullptr );
94  return { p, p };
95 }
96 
97 std::pair<DataObject*, IProfile1D*> Gaudi::profile1DX( const std::string& nam, const IHistogram2D& hist, int first,
98  int last ) {
99  TH2* r = Gaudi::getRepresentation<IHistogram2D, TH2>( hist );
100  TProfile* t = r ? r->ProfileX( "_pfx", first, last, "e" ) : nullptr;
101  if ( t ) t->SetName( nam.c_str() );
102  Profile1D* p = ( t ? new Profile1D( t ) : nullptr );
103  return { p, p };
104 }
105 
106 std::pair<DataObject*, IProfile1D*> Gaudi::profile1DY( const std::string& nam, const IHistogram2D& hist, int first,
107  int last ) {
108  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
109  TProfile* t = r ? r->ProfileY( "_pfx", first, last, "e" ) : nullptr;
110  if ( t ) t->SetName( nam.c_str() );
111  Profile1D* p = ( t ? new Profile1D( t ) : nullptr );
112  return { p, p };
113 }
114 
115 namespace Gaudi {
116  template <>
117  void* Generic2D<IHistogram2D, TH2D>::cast( const std::string& className ) const {
118  if ( className == "AIDA::IHistogram2D" )
119  return (IHistogram2D*)this;
120  else if ( className == "AIDA::IHistogram" )
121  return (IHistogram*)this;
122  return nullptr;
123  }
124 
125  template <>
126  int Generic2D<IHistogram2D, TH2D>::binEntries( int indexX, int indexY ) const {
127  if ( binHeight( indexX, indexY ) <= 0 ) return 0;
128  double xx = binHeight( indexX, indexY ) / binError( indexX, indexY );
129  return int( xx * xx + 0.5 );
130  }
131 
132  template <>
134  TH2D* imp = dynamic_cast<TH2D*>( rep );
135  if ( !imp ) throw std::runtime_error( "Cannot adopt native histogram representation." );
136  m_rep.reset( imp );
137  m_xAxis.initialize( m_rep->GetXaxis(), true );
138  m_yAxis.initialize( m_rep->GetYaxis(), true );
139  const TArrayD* a = m_rep->GetSumw2();
140  if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
141  setTitle( m_rep->GetTitle() );
142  }
143 } // namespace Gaudi
144 
146  m_rep->Sumw2();
147  m_sumwx = m_sumwy = 0;
148  setTitle( "" );
149  m_rep->SetDirectory( nullptr );
150 }
151 
153  adoptRepresentation( rep );
154  m_sumwx = m_sumwy = 0;
155  m_rep->SetDirectory( nullptr );
156 }
157 
158 bool Gaudi::Histogram2D::setBinContents( int i, int j, int entries, double height, double error, double centreX,
159  double centreY ) {
160  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), height );
161  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), error );
162  // accumulate sumwx for in range bins
163  if ( i >= 0 && j >= 0 ) {
164  m_sumwx += centreX * height;
165  m_sumwy += centreY * height;
166  }
167  m_sumEntries += entries;
168  return true;
169 }
170 
172  m_sumwx = 0;
173  m_sumwy = 0;
174  return Base::reset();
175 }
176 
177 bool Gaudi::Histogram2D::fill( double x, double y, double weight ) {
178  // avoid race conditiosn when filling the histogram
179  auto guard = std::scoped_lock{ m_fillSerialization };
180  m_rep->Fill( x, y, weight );
181  return true;
182 }
183 
184 bool Gaudi::Histogram2D::setRms( double rmsX, double rmsY ) {
185  m_rep->SetEntries( m_sumEntries );
186  std::vector<double> stat( 11 );
187  stat[0] = sumBinHeights();
188  stat[1] = 0;
189  if ( std::abs( equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
190  stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
191  stat[2] = m_sumwx;
192  stat[4] = m_sumwy;
193  double meanX = 0;
194  double meanY = 0;
195  if ( std::abs( sumBinHeights() ) > std::numeric_limits<double>::epsilon() ) {
196  meanX = m_sumwx / sumBinHeights();
197  meanY = m_sumwy / sumBinHeights();
198  }
199  stat[3] = ( meanX * meanX + rmsX * rmsX ) * sumBinHeights();
200  stat[5] = ( meanY * meanY + rmsY * rmsY ) * sumBinHeights();
201  stat[6] = 0;
202  m_rep->PutStats( &stat.front() );
203  return true;
204 }
205 
206 void Gaudi::Histogram2D::copyFromAida( const IHistogram2D& h ) {
207  // implement here the copy
208  std::string titlestr = h.title();
209  const char* title = titlestr.c_str();
210  if ( h.xAxis().isFixedBinning() && h.yAxis().isFixedBinning() )
211  m_rep.reset( new TH2D( title, title, h.xAxis().bins(), h.xAxis().lowerEdge(), h.xAxis().upperEdge(),
212  h.yAxis().bins(), h.yAxis().lowerEdge(), h.yAxis().upperEdge() ) );
213  else {
214  Edges eX, eY;
215  for ( int i = 0; i < h.xAxis().bins(); ++i ) eX.push_back( h.xAxis().binLowerEdge( i ) );
216  // add also upperedges at the end
217  eX.push_back( h.xAxis().upperEdge() );
218  for ( int i = 0; i < h.yAxis().bins(); ++i ) eY.push_back( h.yAxis().binLowerEdge( i ) );
219  // add also upperedges at the end
220  eY.push_back( h.yAxis().upperEdge() );
221  m_rep.reset( new TH2D( title, title, eX.size() - 1, &eX.front(), eY.size() - 1, &eY.front() ) );
222  }
223  m_xAxis.initialize( m_rep->GetXaxis(), true );
224  m_yAxis.initialize( m_rep->GetYaxis(), true );
225  m_rep->Sumw2();
226  m_sumEntries = 0;
227  m_sumwx = 0;
228  m_sumwy = 0;
229  // statistics
230  double sumw = h.sumBinHeights();
231  double sumw2 = 0;
232  if ( std::abs( h.equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
233  sumw2 = ( sumw * sumw ) / h.equivalentBinEntries();
234  double sumwx = h.meanX() * h.sumBinHeights();
235  double sumwx2 = ( h.meanX() * h.meanX() + h.rmsX() * h.rmsX() ) * h.sumBinHeights();
236  double sumwy = h.meanY() * h.sumBinHeights();
237  double sumwy2 = ( h.meanY() * h.meanY() + h.rmsY() * h.rmsY() ) * h.sumBinHeights();
238  double sumwxy = 0;
239 
240  // copy the contents in (AIDA underflow/overflow are -2,-1)
241  for ( int i = -2; i < xAxis().bins(); ++i ) {
242  for ( int j = -2; j < yAxis().bins(); ++j ) {
243  // root binning starts from one !
244  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), h.binHeight( i, j ) );
245  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), h.binError( i, j ) );
246  // calculate statistics
247  if ( i >= 0 && j >= 0 ) { sumwxy += h.binHeight( i, j ) * h.binMeanX( i, j ) * h.binMeanY( i, j ); }
248  }
249  }
250  // need to do set entries after setting contents otherwise root will recalculate them
251  // taking into account how many time SetBinContents() has been called
252  m_rep->SetEntries( h.allEntries() );
253  // fill stat vector
254  std::array<double, 11> stat = { { sumw, sumw2, sumwx, sumwx2, sumwy, sumwy2, sumwxy } };
255  m_rep->PutStats( stat.data() );
256 }
Gaudi::Histogram2D::reset
bool reset() override
Definition: H2D.cpp:171
Gaudi::profile1DX
std::pair< DataObject *, AIDA::IProfile1D * > profile1DX(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D profile in X from 2D histogram.
Gaudi::profile1DY
std::pair< DataObject *, AIDA::IProfile1D * > profile1DY(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D profile in Y from 2D histogram.
Gaudi::Histogram2D::m_sumwx
double m_sumwx
Definition: H2D.h:58
ISvcLocator::monitoringHub
Gaudi::Monitoring::Hub & monitoringHub()
Definition: ISvcLocator.cpp:25
std::string
STL class.
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
MonitoringHub.h
std::pair
Gaudi::Histogram2D::m_sumwy
double m_sumwy
Definition: H2D.h:59
std::vector< double >
ISvcLocator
Definition: ISvcLocator.h:46
P1D.h
std::abs
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition: ParticleID.h:191
Gaudi::slice1DY
std::pair< DataObject *, AIDA::IHistogram1D * > slice1DY(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D slice from 2D histogram.
ObjectFactory.h
Gaudi::Generic2D< AIDA::IHistogram2D, TH2D >::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:170
std::vector::front
T front(T... args)
Gaudi::Generic2D::cast
void * cast(const std::string &className) const override
Introspection method.
HistogramUtility.h
Gaudi::Generic2D::binEntries
int binEntries(int indexX, int indexY) const override
The number of entries (ie the number of times fill was called for this bin).
std::vector::push_back
T push_back(T... args)
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
Gaudi::Generic2D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Gaudi::Histogram2D::setBinContents
virtual bool setBinContents(int binIndexX, int binIndexY, int entries, double height, double error, double centreX, double centreY)
Fast filling method for a given bin. It can be also the over/underflow bin.
Definition: H2D.cpp:158
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:31
std::array
STL class.
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:87
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:140
std::runtime_error
STL class.
H1D.h
Gaudi::Histogram2D::copyFromAida
void copyFromAida(const AIDA::IHistogram2D &h)
Create new histogram from any AIDA based histogram.
Definition: H2D.cpp:206
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
cpluginsvc.n
n
Definition: cpluginsvc.py:234
std::numeric_limits::epsilon
T epsilon(T... args)
H2D.h
Gaudi::Histogram2D::Histogram2D
Histogram2D()
Standard Constructor.
Definition: H2D.cpp:145
Gaudi::Generic2D< AIDA::IHistogram2D, TH2D >::m_rep
std::unique_ptr< TH2D > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:162
GaudiPI.h
Gaudi::Edges
std::vector< double > Edges
Definition: GaudiPI.h:28
Gaudi::slice1DX
std::pair< DataObject *, AIDA::IHistogram1D * > slice1DX(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D slice from 2D histogram.
Gaudi::project1DY
std::pair< DataObject *, AIDA::IHistogram1D * > project1DY(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D projection in Y from 2D histogram.
Gaudi::Generic2D< AIDA::IHistogram2D, TH2D >
Gaudi::Histogram2D::setRms
bool setRms(double rmsX, double rmsY)
Sets the rms of the histogram.
Definition: H2D.cpp:184
std::numeric_limits
Gaudi::createH2D
std::pair< DataObject *, AIDA::IHistogram2D * > createH2D(ISvcLocator *svcLocator, const std::string &path, const AIDA::IHistogram2D &hist)
Copy constructor.
std::array::data
T data(T... args)
Gaudi::Histogram2D::fill
bool fill(double x, double y, double weight=1.) override
Fill the Histogram2D with a value and the.
Definition: H2D.cpp:177