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