The Gaudi Framework  v37r1 (a7f61348)
H3D.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 <Gaudi/MonitoringHub.h>
25 #include <GaudiCommonSvc/H3D.h>
27 #include <GaudiKernel/DataObject.h>
29 
30 #include "GaudiPI.h"
31 #include "TH3D.h"
32 
33 namespace Gaudi {
34  template <>
36  TH3D* imp = dynamic_cast<TH3D*>( rep );
37  if ( !imp ) throw std::runtime_error( "Cannot adopt native histogram representation." );
38  m_rep.reset( imp );
39  m_xAxis.initialize( m_rep->GetXaxis(), true );
40  m_yAxis.initialize( m_rep->GetYaxis(), true );
41  m_zAxis.initialize( m_rep->GetZaxis(), true );
42  const TArrayD* a = m_rep->GetSumw2();
43  if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
44  setTitle( m_rep->GetTitle() );
45  }
46 } // namespace Gaudi
47 
50  const std::string& title, int nBinsX, double xlow,
51  double xup, int nBinsY, double ylow, double yup,
52  int nBinsZ, double zlow, double zup ) {
53  auto p = new Histogram3D(
54  new TH3D( title.c_str(), title.c_str(), nBinsX, xlow, xup, nBinsY, ylow, yup, nBinsZ, zlow, zup ) );
55  svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *p );
56  return { p, p };
57 }
58 
61  const std::string& title, const Edges& eX,
62  const Edges& eY, const Edges& eZ ) {
63  auto p = new Histogram3D( new TH3D( title.c_str(), title.c_str(), eX.size() - 1, &eX.front(), eY.size() - 1,
64  &eY.front(), eZ.size() - 1, &eZ.front() ) );
65  svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *p );
66  return { p, p };
67 }
68 
70  const AIDA::IHistogram3D& hist ) {
71  TH3D* h = getRepresentation<AIDA::IHistogram3D, TH3D>( hist );
72  Histogram3D* n = h ? new Histogram3D( new TH3D( *h ) ) : nullptr;
73  if ( n ) { svcLocator->monitoringHub().registerEntity( "", path, "histogram:Histogram:double", *n ); }
74  return { n, n };
75 }
76 
78  setTitle( "" );
79  m_rep->Sumw2();
80  m_sumwx = 0;
81  m_sumwy = 0;
82  m_sumwz = 0;
83  m_rep->SetDirectory( nullptr );
84 }
85 
87  adoptRepresentation( rep );
88  m_sumwx = 0;
89  m_sumwy = 0;
90  m_sumwz = 0;
91  m_rep->SetDirectory( nullptr );
92 }
93 
94 // set bin content (entries and centre are not used )
95 bool Gaudi::Histogram3D::setBinContents( int i, int j, int k, int entries, double height, double error, double centreX,
96  double centreY, double centreZ ) {
97  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), rIndexZ( k ), height );
98  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), rIndexZ( k ), error );
99  // accumulate sum bin centers
100  if ( i >= 0 && j >= 0 && k >= 0 ) {
101  m_sumwx += centreX * height;
102  m_sumwy += centreY * height;
103  m_sumwz += centreZ * height;
104  }
105  m_sumEntries += entries;
106  return true;
107 }
108 
110  m_sumwx = 0;
111  m_sumwy = 0;
112  m_sumwz = 0;
113  m_sumEntries = 0;
114  m_rep->Reset();
115  return true;
116 }
117 
118 bool Gaudi::Histogram3D::fill( double x, double y, double z, double weight ) {
119  // avoid race conditiosn when filling the histogram
120  auto guard = std::scoped_lock{ m_fillSerialization };
121  m_rep->Fill( x, y, z, weight );
122  return true;
123 }
124 
125 void* Gaudi::Histogram3D::cast( const std::string& className ) const {
126  if ( className == "AIDA::IHistogram3D" ) {
127  return (AIDA::IHistogram3D*)this;
128  } else if ( className == "AIDA::IHistogram" ) {
129  return (AIDA::IHistogram*)this;
130  }
131  return nullptr;
132 }
133 
134 bool Gaudi::Histogram3D::setRms( double rmsX, double rmsY, double rmsZ ) {
135  m_rep->SetEntries( m_sumEntries );
136  std::vector<double> stat( 11 );
137  // sum weights
138  stat[0] = sumBinHeights();
139  stat[1] = 0;
140  if ( abs( equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
141  stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
142  stat[2] = m_sumwx;
143  stat[4] = m_sumwy;
144  stat[6] = 0;
145  stat[7] = m_sumwz;
146  double meanX = 0;
147  double meanY = 0;
148  double meanZ = 0;
149  if ( abs( sumBinHeights() ) > std::numeric_limits<double>::epsilon() ) {
150  meanX = m_sumwx / sumBinHeights();
151  meanY = m_sumwy / sumBinHeights();
152  meanZ = m_sumwz / sumBinHeights();
153  }
154  stat[3] = ( meanX * meanX + rmsX * rmsX ) * sumBinHeights();
155  stat[5] = ( meanY * meanY + rmsY * rmsY ) * sumBinHeights();
156  stat[8] = ( meanZ * meanZ + rmsZ * rmsZ ) * sumBinHeights();
157  // do not need to use sumwxy sumwxz and sumwyz
158  m_rep->PutStats( &stat.front() );
159  return true;
160 }
161 
162 void Gaudi::Histogram3D::copyFromAida( const AIDA::IHistogram3D& h ) {
163  // implement here the copy
164  std::string titlestr = h.title();
165  const char* title = titlestr.c_str();
166  if ( h.xAxis().isFixedBinning() && h.yAxis().isFixedBinning() && h.zAxis().isFixedBinning() ) {
167  m_rep = std::make_unique<TH3D>( title, title, h.xAxis().bins(), h.xAxis().lowerEdge(), h.xAxis().upperEdge(),
168  h.yAxis().bins(), h.yAxis().lowerEdge(), h.yAxis().upperEdge(), h.zAxis().bins(),
169  h.zAxis().lowerEdge(), h.zAxis().upperEdge() );
170  } else {
171  Edges eX, eY, eZ;
172  for ( int i = 0; i < h.xAxis().bins(); ++i ) eX.push_back( h.xAxis().binLowerEdge( i ) );
173  // add also upperedges at the end
174  eX.push_back( h.xAxis().upperEdge() );
175  for ( int i = 0; i < h.yAxis().bins(); ++i ) eY.push_back( h.yAxis().binLowerEdge( i ) );
176  // add also upperedges at the end
177  eY.push_back( h.yAxis().upperEdge() );
178  for ( int i = 0; i < h.zAxis().bins(); ++i ) eZ.push_back( h.zAxis().binLowerEdge( i ) );
179  // add also upperedges at the end
180  eZ.push_back( h.zAxis().upperEdge() );
181  m_rep.reset(
182  new TH3D( title, title, eX.size() - 1, &eX.front(), eY.size() - 1, &eY.front(), eZ.size() - 1, &eZ.front() ) );
183  }
184  m_xAxis.initialize( m_rep->GetXaxis(), true );
185  m_yAxis.initialize( m_rep->GetYaxis(), true );
186  m_zAxis.initialize( m_rep->GetZaxis(), true );
187  const TArrayD* a = m_rep->GetSumw2();
188  if ( !a || ( a && a->GetSize() == 0 ) ) m_rep->Sumw2();
189  m_sumEntries = 0;
190  m_sumwx = 0;
191  m_sumwy = 0;
192  m_sumwz = 0;
193 
194  // statistics
195  double sumw = h.sumBinHeights();
196  double sumw2 = 0;
197  if ( abs( h.equivalentBinEntries() ) > std::numeric_limits<double>::epsilon() )
198  sumw2 = ( sumw * sumw ) / h.equivalentBinEntries();
199  double sumwx = h.meanX() * h.sumBinHeights();
200  double sumwx2 = ( h.meanX() * h.meanX() + h.rmsX() * h.rmsX() ) * h.sumBinHeights();
201  double sumwy = h.meanY() * h.sumBinHeights();
202  double sumwy2 = ( h.meanY() * h.meanY() + h.rmsY() * h.rmsY() ) * h.sumBinHeights();
203  double sumwz = h.meanZ() * h.sumBinHeights();
204  double sumwz2 = ( h.meanZ() * h.meanZ() + h.rmsZ() * h.rmsZ() ) * h.sumBinHeights();
205  double sumwxy = 0;
206  double sumwxz = 0;
207  double sumwyz = 0;
208 
209  // copy the contents in (AIDA underflow/overflow are -2,-1)
210  for ( int i = -2; i < xAxis().bins(); ++i ) {
211  for ( int j = -2; j < yAxis().bins(); ++j ) {
212  for ( int k = -2; k < zAxis().bins(); ++k ) {
213  m_rep->SetBinContent( rIndexX( i ), rIndexY( j ), rIndexZ( k ), h.binHeight( i, j, k ) );
214  m_rep->SetBinError( rIndexX( i ), rIndexY( j ), rIndexZ( k ), h.binError( i, j, k ) );
215  // calculate statistics
216  if ( i >= 0 && j >= 0 && k >= 0 ) {
217  sumwxy += h.binHeight( i, j, k ) * h.binMeanX( i, j, k ) * h.binMeanY( i, j, k );
218  sumwxz += h.binHeight( i, j, k ) * h.binMeanX( i, j, k ) * h.binMeanZ( i, j, k );
219  sumwyz += h.binHeight( i, j, k ) * h.binMeanY( i, j, k ) * h.binMeanZ( i, j, k );
220  }
221  }
222  }
223  }
224  // need to do set entries after setting contents otherwise root will recalulate them
225  // taking into account how many time SetBinContents() has been called
226  m_rep->SetEntries( h.allEntries() );
227 
228  // fill stat vector
229  std::vector<double> stat( 11 );
230  stat[0] = sumw;
231  stat[1] = sumw2;
232  stat[2] = sumwx;
233  stat[3] = sumwx2;
234  stat[4] = sumwy;
235  stat[5] = sumwy2;
236  stat[6] = sumwxy;
237  stat[7] = sumwz;
238  stat[8] = sumwz2;
239  stat[9] = sumwxz;
240  stat[10] = sumwyz;
241  m_rep->PutStats( &stat.front() );
242 }
Gaudi::Histogram3D::setRms
virtual bool setRms(double rmsX, double rmsY, double rmsZ)
Sets the rms of the histogram.
Definition: H3D.cpp:134
ISvcLocator::monitoringHub
Gaudi::Monitoring::Hub & monitoringHub()
Definition: ISvcLocator.cpp:26
std::string
STL class.
Gaudi::Histogram3D::m_sumwx
double m_sumwx
Definition: H3D.h:59
H3D.h
GaudiAlg.HistoUtils.path
path
Definition: HistoUtils.py:961
Gaudi::Generic3D< AIDA::IHistogram3D, TH3D >::m_rep
std::unique_ptr< TH3D > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:230
MonitoringHub.h
Gaudi::Histogram3D::cast
void * cast(const std::string &className) const override
Introspection method.
Definition: H3D.cpp:125
std::pair
std::vector< double >
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Histogram3D::setBinContents
virtual bool setBinContents(int i, int j, int k, int entries, double height, double error, double centreX, double centreY, double centreZ)
Fast filling method for a given bin. It can be also the over/underflow bin.
Definition: H3D.cpp:95
Gaudi::Histogram3D::Histogram3D
Histogram3D()
Standard Constructor.
Definition: H3D.cpp:77
Gaudi::Histogram3D::m_sumwz
double m_sumwz
Definition: H3D.h:61
ObjectFactory.h
std::vector::front
T front(T... args)
Gaudi::Histogram3D::fill
bool fill(double x, double y, double z, double weight) override
Fill bin content.
Definition: H3D.cpp:118
HistogramUtility.h
std::vector::push_back
T push_back(T... args)
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
Gaudi::Histogram3D::copyFromAida
void copyFromAida(const AIDA::IHistogram3D &h)
Create new histogram from any AIDA based histogram.
Definition: H3D.cpp:162
ProduceConsume.j
j
Definition: ProduceConsume.py:101
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:32
Gaudi::createH3D
std::pair< DataObject *, AIDA::IHistogram3D * > createH3D(ISvcLocator *svcLocator, const std::string &path, const AIDA::IHistogram3D &hist)
Copy constructor.
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:88
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:174
std::runtime_error
STL class.
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
std::numeric_limits::epsilon
T epsilon(T... args)
Gaudi::Generic3D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Gaudi::Histogram3D::m_sumwy
double m_sumwy
Definition: H3D.h:60
DataObject.h
GaudiPI.h
Gaudi::Edges
std::vector< double > Edges
Definition: GaudiPI.h:28
Gaudi::Histogram3D::reset
bool reset() override
Definition: H3D.cpp:109
Gaudi::Generic3D< AIDA::IHistogram3D, TH3D >::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:238
Generic3D.h
Gaudi::Generic3D< AIDA::IHistogram3D, TH3D >
std::numeric_limits