All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
H3D.cpp
Go to the documentation of this file.
1 #ifdef __ICC
2 // disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
3 // TODO: To be removed, since it comes from ROOT TMathBase.h
4 #pragma warning(disable:2259)
5 #endif
6 #ifdef WIN32
7 // Disable warning
8 // warning C4996: 'sprintf': This function or variable may be unsafe.
9 // coming from TString.h
10 #pragma warning(disable:4996)
11 #endif
12 
13 #include <GaudiKernel/DataObject.h>
17 #include <GaudiCommonSvc/H3D.h>
18 
19 #include "GaudiPI.h"
20 #include "TH3D.h"
21 
22 namespace Gaudi {
23  template <>
25  TH3D* imp = dynamic_cast<TH3D*>(rep);
26  if ( !imp ) throw std::runtime_error("Cannot adopt native histogram representation.");
27  m_rep.reset( imp );
28  m_xAxis.initialize(m_rep->GetXaxis(),true);
29  m_yAxis.initialize(m_rep->GetYaxis(),true);
30  m_zAxis.initialize(m_rep->GetZaxis(),true);
31  const TArrayD* a = m_rep->GetSumw2();
32  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
33  setTitle(m_rep->GetTitle());
34  }
35 }
36 
39 Gaudi::createH3D(const std::string& title,int nBinsX,double xlow,double xup,int nBinsY,double ylow,double yup,int nBinsZ,double zlow,double zup) {
40  auto p = new Histogram3D(new TH3D(title.c_str(),title.c_str(),nBinsX,xlow,xup,nBinsY,ylow,yup,nBinsZ,zlow,zup));
41  return {p,p};
42 }
43 
46 Gaudi::createH3D(const std::string& title, const Edges& eX,const Edges& eY,const Edges& eZ) {
47  auto p = new Histogram3D(new TH3D(title.c_str(),title.c_str(),eX.size()-1,&eX.front(), eY.size()-1,&eY.front(), eZ.size()-1,&eZ.front()));
48  return {p,p};
49 }
50 
51 std::pair<DataObject*,AIDA::IHistogram3D*> Gaudi::createH3D(const AIDA::IHistogram3D& hist) {
52  TH3D *h = getRepresentation<AIDA::IHistogram3D,TH3D>(hist);
53  Histogram3D *n = h ? new Histogram3D(new TH3D(*h)) : nullptr;
54  return {n,n};
55 }
56 
58 : Base( new TH3D() )
59 {
60  setTitle("");
61  m_rep->Sumw2();
62  m_sumwx = 0;
63  m_sumwy = 0;
64  m_sumwz = 0;
65  m_rep->SetDirectory(nullptr);
66 }
67 
70  m_sumwx = 0;
71  m_sumwy = 0;
72  m_sumwz = 0;
73  m_rep->SetDirectory(nullptr);
74 }
75 
76 // set bin content (entries and centre are not used )
77 bool Gaudi::Histogram3D::setBinContents(int i,int j,int k,int entries,double height,double error,double centreX, double centreY, double centreZ ) {
78  m_rep->SetBinContent(rIndexX(i), rIndexY(j), rIndexZ(k), height);
79  m_rep->SetBinError(rIndexX(i), rIndexY(j), rIndexZ(k), error);
80  // accumulate sum bin centers
81  if (i >=0 && j >= 0 && k >= 0) {
82  m_sumwx += centreX*height;
83  m_sumwy += centreY*height;
84  m_sumwz += centreZ*height;
85  }
87  return true;
88 }
89 
91  m_sumwx = 0;
92  m_sumwy = 0;
93  m_sumwz = 0;
94  m_sumEntries = 0;
95  m_rep->Reset ( );
96  return true;
97 }
98 
99 bool Gaudi::Histogram3D::fill ( double x, double y, double z, double weight) {
100  m_rep->Fill ( x , y, z, weight );
101  return true;
102 }
103 
104 void* Gaudi::Histogram3D::cast(const std::string & className) const {
105  if (className == "AIDA::IHistogram3D") {
106  return (AIDA::IHistogram3D*)this;
107  }
108  else if (className == "AIDA::IHistogram") {
109  return (AIDA::IHistogram*)this;
110  }
111  return nullptr;
112 }
113 
114 #ifdef __ICC
115 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
116 // The comparison are meant
117 #pragma warning(push)
118 #pragma warning(disable:1572)
119 #endif
120 bool Gaudi::Histogram3D::setRms(double rmsX, double rmsY, double rmsZ ) {
121  m_rep->SetEntries(m_sumEntries);
122  std::vector<double> stat(11);
123  // sum weights
124  stat[0] = sumBinHeights();
125  stat[1] = 0;
126  if (equivalentBinEntries() != 0)
127  stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
128  stat[2] = m_sumwx;
129  double meanX = 0;
130  if ( sumBinHeights() != 0 ) meanX = m_sumwx/ sumBinHeights();
131  stat[3] = ( meanX*meanX + rmsX*rmsX )* sumBinHeights();
132  stat[4] = m_sumwy;
133  double meanY = 0;
134  if ( sumBinHeights() != 0 ) meanY = m_sumwy/ sumBinHeights();
135  stat[5] = ( meanY*meanY + rmsY*rmsY )* sumBinHeights();
136  stat[6] = 0;
137  stat[7] = m_sumwz;
138  double meanZ = 0;
139  if ( sumBinHeights() != 0 ) meanZ = m_sumwz/ sumBinHeights();
140  stat[8] = ( meanZ*meanZ + rmsZ*rmsZ )* sumBinHeights();
141  // do not need to use sumwxy sumwxz and sumwyz
142 
143  m_rep->PutStats(&stat.front());
144  return true;
145 }
146 
147 void Gaudi::Histogram3D::copyFromAida(const AIDA::IHistogram3D & h) {
148  // implement here the copy
149  const char* tit = h.title().c_str();
150  if (h.xAxis().isFixedBinning() && h.yAxis().isFixedBinning() && h.zAxis().isFixedBinning() ) {
151  m_rep.reset( new TH3D(tit,tit,
152  h.xAxis().bins(), h.xAxis().lowerEdge(), h.xAxis().upperEdge(),
153  h.yAxis().bins(), h.yAxis().lowerEdge(), h.yAxis().upperEdge(),
154  h.zAxis().bins(), h.zAxis().lowerEdge(), h.zAxis().upperEdge() ) );
155  }
156  else {
157  Edges eX, eY, eZ;
158  for (int i =0; i < h.xAxis().bins(); ++i)
159  eX.push_back(h.xAxis().binLowerEdge(i));
160  // add also upperedges at the end
161  eX.push_back(h.xAxis().upperEdge() );
162  for (int i =0; i < h.yAxis().bins(); ++i)
163  eY.push_back(h.yAxis().binLowerEdge(i));
164  // add also upperedges at the end
165  eY.push_back(h.yAxis().upperEdge() );
166  for (int i =0; i < h.zAxis().bins(); ++i)
167  eZ.push_back(h.zAxis().binLowerEdge(i));
168  // add also upperedges at the end
169  eZ.push_back(h.zAxis().upperEdge() );
170  m_rep.reset( new TH3D(tit,tit,eX.size()-1,&eX.front(),eY.size()-1,&eY.front(),eZ.size()-1,&eZ.front()) );
171  }
172  m_xAxis.initialize(m_rep->GetXaxis(),true);
173  m_yAxis.initialize(m_rep->GetYaxis(),true);
174  m_zAxis.initialize(m_rep->GetZaxis(),true);
175  const TArrayD* a = m_rep->GetSumw2();
176  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
177  m_sumEntries = 0;
178  m_sumwx = 0;
179  m_sumwy = 0;
180  m_sumwz = 0;
181 
182  // statistics
183  double sumw = h.sumBinHeights();
184  double sumw2 = 0;
185  if (h.equivalentBinEntries() != 0)
186  sumw2 = ( sumw * sumw ) /h.equivalentBinEntries();
187  double sumwx = h.meanX()*h.sumBinHeights();
188  double sumwx2 = (h.meanX()*h.meanX() + h.rmsX()*h.rmsX() )*h.sumBinHeights();
189  double sumwy = h.meanY()*h.sumBinHeights();
190  double sumwy2 = (h.meanY()*h.meanY() + h.rmsY()*h.rmsY() )*h.sumBinHeights();
191  double sumwz = h.meanZ()*h.sumBinHeights();
192  double sumwz2 = (h.meanZ()*h.meanZ() + h.rmsZ()*h.rmsZ() )*h.sumBinHeights();
193  double sumwxy = 0;
194  double sumwxz = 0;
195  double sumwyz = 0;
196 
197  // copy the contents in (AIDA underflow/overflow are -2,-1)
198  for (int i=-2; i < xAxis().bins(); ++i) {
199  for (int j=-2; j < yAxis().bins(); ++j) {
200  for (int k=-2; k < zAxis().bins(); ++k) {
201  m_rep->SetBinContent(rIndexX(i), rIndexY(j), rIndexZ(k), h.binHeight(i,j,k) );
202  m_rep->SetBinError(rIndexX(i), rIndexY(j), rIndexZ(k), h.binError(i,j,k) );
203  // calculate statistics
204  if ( i >= 0 && j >= 0 && k >= 0) {
205  sumwxy += h.binHeight(i,j,k)*h.binMeanX(i,j,k)*h.binMeanY(i,j,k);
206  sumwxz += h.binHeight(i,j,k)*h.binMeanX(i,j,k)*h.binMeanZ(i,j,k);
207  sumwyz += h.binHeight(i,j,k)*h.binMeanY(i,j,k)*h.binMeanZ(i,j,k);
208  }
209  }
210  }
211  }
212  // need to do set entries after setting contents otherwise root will recalulate them
213  // taking into account how many time SetBinContents() has been called
214  m_rep->SetEntries(h.allEntries());
215 
216  // fill stat vector
217  std::vector<double> stat(11);
218  stat[0] = sumw;
219  stat[1] = sumw2;
220  stat[2] = sumwx;
221  stat[3] = sumwx2;
222  stat[4] = sumwy;
223  stat[5] = sumwy2;
224  stat[6] = sumwxy;
225  stat[7] = sumwz;
226  stat[8] = sumwz2;
227  stat[9] = sumwxz;
228  stat[10] = sumwyz;
229  m_rep->PutStats(&stat.front());
230 }
231 
232 #ifdef __ICC
233 // re-enable icc remark #1572
234 #pragma warning(pop)
235 #endif
int rIndexZ(int index) const
Definition: Generic3D.h:83
int rIndexX(int index) const
Definition: Generic3D.h:81
Gaudi::Axis m_xAxis
Definition: Generic3D.h:215
double meanZ() const override
The mean of the IHistogram3D along the z axis.
Definition: Generic3D.h:164
Gaudi::Axis m_yAxis
Definition: Generic3D.h:216
int entries() const override
Get the number or all the entries.
Definition: Generic3D.h:245
T front(T...args)
const AIDA::IAxis & yAxis() const override
Get the y axis of the IHistogram3D.
Definition: Generic3D.h:174
std::vector< double > Edges
Definition: GaudiPI.h:17
const AIDA::IAxis & zAxis() const override
Get the z axis of the IHistogram3D.
Definition: Generic3D.h:176
double m_sumwx
Definition: H3D.h:42
double rmsZ() const override
The RMS of the IHistogram3D along the z axis.
Definition: Generic3D.h:170
STL class.
double m_sumwy
Definition: H3D.h:43
T push_back(T...args)
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:77
Histogram3D()
Standard Constructor.
Definition: H3D.cpp:57
AIDA implementation for 3 D histograms using ROOT THD2.
Definition: H3D.h:17
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic3D.h:265
T reset(T...args)
Gaudi::Axis m_zAxis
Definition: Generic3D.h:217
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic3D.h:229
bool fill(double x, double y, double z, double weight) override
Fill bin content.
Definition: H3D.cpp:99
int rIndexY(int index) const
Definition: Generic3D.h:82
void copyFromAida(const AIDA::IHistogram3D &h)
Create new histogram from any AIDA based histogram.
Definition: H3D.cpp:147
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic3D.h:221
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:66
std::pair< DataObject *, AIDA::IHistogram3D * > createH3D(const AIDA::IHistogram3D &hist)
Copy constructor.
T c_str(T...args)
const AIDA::IAxis & xAxis() const override
Get the x axis of the IHistogram3D.
Definition: Generic3D.h:172
virtual bool setRms(double rmsX, double rmsY, double rmsZ)
Sets the rms of the histogram.
Definition: H3D.cpp:120
double rmsY() const override
The RMS of the IHistogram3D along the y axis.
Definition: Generic3D.h:168
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
double rmsX() const override
The RMS of the IHistogram3D along the x axis.
Definition: Generic3D.h:166
double meanY() const override
The mean of the IHistogram3D along the y axis.
Definition: Generic3D.h:162
void * cast(const std::string &className) const override
Introspection method.
Definition: H3D.cpp:104
double equivalentBinEntries() const override
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic3D.h:275
bool reset() override
Definition: H3D.cpp:90
Helper functions to set/get the application return code.
Definition: __init__.py:1
double meanX() const override
The mean of the IHistogram3D along the x axis.
Definition: Generic3D.h:159
double m_sumwz
Definition: H3D.h:44
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic3D.h:35