The Gaudi Framework  v36r9p1 (5c15b2bb)
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 <GaudiCommonSvc/H1D.h>
24 #include <GaudiCommonSvc/H2D.h>
26 #include <GaudiCommonSvc/P1D.h>
28 #include <TH2D.h>
29 #include <TProfile.h>
30 #include <array>
31 
32 namespace {
33  using AIDA::IHistogram1D;
34  using AIDA::IHistogram2D;
35  using AIDA::IProfile1D;
36 } // namespace
37 
38 std::pair<DataObject*, IHistogram2D*> Gaudi::createH2D( const std::string& title, int binsX, double iminX, double imaxX,
39  int binsY, double iminY, double imaxY ) {
40  auto p = new Histogram2D( new TH2D( title.c_str(), title.c_str(), binsX, iminX, imaxX, binsY, iminY, imaxY ) );
41  return { p, p };
42 }
43 
44 std::pair<DataObject*, IHistogram2D*> Gaudi::createH2D( const std::string& title, const Edges& eX, const Edges& eY ) {
45  auto p = new Histogram2D(
46  new TH2D( title.c_str(), title.c_str(), eX.size() - 1, &eX.front(), eY.size() - 1, &eY.front() ) );
47  return { p, p };
48 }
49 
51  auto p = new Histogram2D( rep );
52  return { p, p };
53 }
54 
55 std::pair<DataObject*, IHistogram2D*> Gaudi::createH2D( const IHistogram2D& hist ) {
56  TH2D* h = getRepresentation<AIDA::IHistogram2D, TH2D>( hist );
57  Histogram2D* n = h ? new Histogram2D( new TH2D( *h ) ) : nullptr;
58  return { n, n };
59 }
60 
61 std::pair<DataObject*, IHistogram1D*> Gaudi::slice1DX( const std::string& nam, const IHistogram2D& hist, int first,
62  int last ) {
63  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
64  TH1D* t = r ? r->ProjectionX( "_px", first, last, "e" ) : nullptr;
65  if ( t ) t->SetName( nam.c_str() );
66  Histogram1D* p = ( t ? new Histogram1D( t ) : nullptr );
67  return { p, p };
68 }
69 
70 std::pair<DataObject*, IHistogram1D*> Gaudi::slice1DY( const std::string& nam, const IHistogram2D& hist, int first,
71  int last ) {
72  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
73  TH1D* t = r ? r->ProjectionY( "_py", 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::project1DY( 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( "_px", 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*, IProfile1D*> Gaudi::profile1DX( const std::string& nam, const IHistogram2D& hist, int first,
89  int last ) {
90  TH2* r = Gaudi::getRepresentation<IHistogram2D, TH2>( hist );
91  TProfile* t = r ? r->ProfileX( "_pfx", first, last, "e" ) : nullptr;
92  if ( t ) t->SetName( nam.c_str() );
93  Profile1D* p = ( t ? new Profile1D( t ) : nullptr );
94  return { p, p };
95 }
96 
97 std::pair<DataObject*, IProfile1D*> Gaudi::profile1DY( const std::string& nam, const IHistogram2D& hist, int first,
98  int last ) {
99  TH2* r = getRepresentation<IHistogram2D, TH2>( hist );
100  TProfile* t = r ? r->ProfileY( "_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 namespace Gaudi {
107  template <>
108  void* Generic2D<IHistogram2D, TH2D>::cast( const std::string& className ) const {
109  if ( className == "AIDA::IHistogram2D" )
110  return (IHistogram2D*)this;
111  else if ( className == "AIDA::IHistogram" )
112  return (IHistogram*)this;
113  return nullptr;
114  }
115 
116  template <>
117  int Generic2D<IHistogram2D, TH2D>::binEntries( int indexX, int indexY ) const {
118  if ( binHeight( indexX, indexY ) <= 0 ) return 0;
119  double xx = binHeight( indexX, indexY ) / binError( indexX, indexY );
120  return int( xx * xx + 0.5 );
121  }
122 
123  template <>
125  TH2D* imp = dynamic_cast<TH2D*>( rep );
126  if ( !imp ) throw std::runtime_error( "Cannot adopt native histogram representation." );
127  m_rep.reset( imp );
128  m_xAxis.initialize( m_rep->GetXaxis(), true );
129  m_yAxis.initialize( m_rep->GetYaxis(), true );
130  const TArrayD* a = m_rep->GetSumw2();
131  if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
132  setTitle( m_rep->GetTitle() );
133  }
134 } // namespace Gaudi
135 
137  m_rep->Sumw2();
138  m_sumwx = m_sumwy = 0;
139  setTitle( "" );
140  m_rep->SetDirectory( nullptr );
141 }
142 
144  adoptRepresentation( rep );
145  m_sumwx = m_sumwy = 0;
146  m_rep->SetDirectory( nullptr );
147 }
148 
149 bool Gaudi::Histogram2D::setBinContents( int i, int j, int entries, double height, double error, double centreX,
150  double centreY ) {
151  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), height );
152  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), error );
153  // accumulate sumwx for in range bins
154  if ( i >= 0 && j >= 0 ) {
155  m_sumwx += centreX * height;
156  m_sumwy += centreY * height;
157  }
158  m_sumEntries += entries;
159  return true;
160 }
161 
163  m_sumwx = 0;
164  m_sumwy = 0;
165  return Base::reset();
166 }
167 
168 #ifdef __ICC
169 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
170 // The comparison are meant
171 # pragma warning( push )
172 # pragma warning( disable : 1572 )
173 #endif
174 bool Gaudi::Histogram2D::fill( double x, double y, double weight ) {
175  // avoid race conditiosn when filling the histogram
176  auto guard = std::scoped_lock{ m_fillSerialization };
177  ( weight == 1. ) ? m_rep->Fill( x, y ) : m_rep->Fill( x, y, weight );
178  return true;
179 }
180 
181 bool Gaudi::Histogram2D::setRms( double rmsX, double rmsY ) {
182  m_rep->SetEntries( m_sumEntries );
183  std::vector<double> stat( 11 );
184  stat[0] = sumBinHeights();
185  stat[1] = 0;
186  if ( equivalentBinEntries() != 0 ) stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
187  stat[2] = m_sumwx;
188  double meanX = 0;
189  if ( sumBinHeights() != 0 ) meanX = m_sumwx / sumBinHeights();
190  stat[3] = ( meanX * meanX + rmsX * rmsX ) * sumBinHeights();
191  stat[4] = m_sumwy;
192  double meanY = 0;
193  if ( sumBinHeights() != 0 ) meanY = m_sumwy / sumBinHeights();
194  stat[5] = ( meanY * meanY + rmsY * rmsY ) * sumBinHeights();
195  stat[6] = 0;
196  m_rep->PutStats( &stat.front() );
197  return true;
198 }
199 
200 void Gaudi::Histogram2D::copyFromAida( const IHistogram2D& h ) {
201  // implement here the copy
202  std::string titlestr = h.title();
203  const char* title = titlestr.c_str();
204  if ( h.xAxis().isFixedBinning() && h.yAxis().isFixedBinning() )
205  m_rep.reset( new TH2D( title, title, h.xAxis().bins(), h.xAxis().lowerEdge(), h.xAxis().upperEdge(),
206  h.yAxis().bins(), h.yAxis().lowerEdge(), h.yAxis().upperEdge() ) );
207  else {
208  Edges eX, eY;
209  for ( int i = 0; i < h.xAxis().bins(); ++i ) eX.push_back( h.xAxis().binLowerEdge( i ) );
210  // add also upperedges at the end
211  eX.push_back( h.xAxis().upperEdge() );
212  for ( int i = 0; i < h.yAxis().bins(); ++i ) eY.push_back( h.yAxis().binLowerEdge( i ) );
213  // add also upperedges at the end
214  eY.push_back( h.yAxis().upperEdge() );
215  m_rep.reset( new TH2D( title, title, eX.size() - 1, &eX.front(), eY.size() - 1, &eY.front() ) );
216  }
217  m_xAxis.initialize( m_rep->GetXaxis(), true );
218  m_yAxis.initialize( m_rep->GetYaxis(), true );
219  m_rep->Sumw2();
220  m_sumEntries = 0;
221  m_sumwx = 0;
222  m_sumwy = 0;
223  // statistics
224  double sumw = h.sumBinHeights();
225  double sumw2 = 0;
226  if ( h.equivalentBinEntries() != 0 ) sumw2 = ( sumw * sumw ) / h.equivalentBinEntries();
227  double sumwx = h.meanX() * h.sumBinHeights();
228  double sumwx2 = ( h.meanX() * h.meanX() + h.rmsX() * h.rmsX() ) * h.sumBinHeights();
229  double sumwy = h.meanY() * h.sumBinHeights();
230  double sumwy2 = ( h.meanY() * h.meanY() + h.rmsY() * h.rmsY() ) * h.sumBinHeights();
231  double sumwxy = 0;
232 
233  // copy the contents in (AIDA underflow/overflow are -2,-1)
234  for ( int i = -2; i < xAxis().bins(); ++i ) {
235  for ( int j = -2; j < yAxis().bins(); ++j ) {
236  // root binning starts from one !
237  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), h.binHeight( i, j ) );
238  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), h.binError( i, j ) );
239  // calculate statistics
240  if ( i >= 0 && j >= 0 ) { sumwxy += h.binHeight( i, j ) * h.binMeanX( i, j ) * h.binMeanY( i, j ); }
241  }
242  }
243  // need to do set entries after setting contents otherwise root will recalculate them
244  // taking into account how many time SetBinContents() has been called
245  m_rep->SetEntries( h.allEntries() );
246  // fill stat vector
247  std::array<double, 11> stat = { { sumw, sumw2, sumwx, sumwx2, sumwy, sumwy2, sumwxy } };
248  m_rep->PutStats( stat.data() );
249 }
250 
251 #ifdef __ICC
252 // re-enable icc remark #1572
253 # pragma warning( pop )
254 #endif
Gaudi::Histogram2D::reset
bool reset() override
Definition: H2D.cpp:162
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:52
std::string
STL class.
std::pair
Gaudi::Histogram2D::m_sumwy
double m_sumwy
Definition: H2D.h:53
std::vector< double >
Gaudi::createH2D
std::pair< DataObject *, AIDA::IHistogram2D * > createH2D(const AIDA::IHistogram2D &hist)
Copy constructor.
P1D.h
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:30
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:149
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:32
std::array
STL class.
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:93
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:200
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
H2D.h
Gaudi::Histogram2D::Histogram2D
Histogram2D()
Standard Constructor.
Definition: H2D.cpp:136
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:27
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:181
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:174