H2D.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 #include <array>
15 #include <GaudiCommonSvc/H2D.h>
16 #include <GaudiCommonSvc/H1D.h>
17 #include <GaudiCommonSvc/P1D.h>
18 #include <TH2D.h>
19 #include <TProfile.h>
20 #include "GaudiPI.h"
21 
22 namespace {
23  using AIDA::IProfile1D;
24  using AIDA::IHistogram1D;
25  using AIDA::IHistogram2D;
26 }
27 
28 std::pair<DataObject*,IHistogram2D*> Gaudi::createH2D(const std::string & title,int binsX,double iminX,double imaxX,int binsY,double iminY,double imaxY) {
29  auto p = new Histogram2D(new TH2D(title.c_str(),title.c_str(),binsX, iminX, imaxX, binsY, iminY, imaxY));
30  return { p, p };
31 }
32 
34  auto p = new Histogram2D(new TH2D(title.c_str(),title.c_str(),eX.size()-1,&eX.front(),eY.size()-1,&eY.front()));
35  return { p, p };
36 }
37 
39  auto p = new Histogram2D(rep);
40  return { p, p };
41 }
42 
44  TH2D *h = getRepresentation<AIDA::IHistogram2D,TH2D>(hist);
45  Histogram2D *n = h ? new Histogram2D(new TH2D(*h)) : nullptr;
46  return { n, n };
47 }
48 
50 Gaudi::slice1DX(const std::string& nam,const IHistogram2D& hist,int first, int last) {
51  TH2 *r = getRepresentation<IHistogram2D,TH2>(hist);
52  TH1D *t = r ? r->ProjectionX("_px",first,last,"e") : nullptr;
53  if ( t ) t->SetName(nam.c_str());
54  Histogram1D* p = ( t ? new Histogram1D(t) : nullptr );
55  return { p, p };
56 }
57 
59 Gaudi::slice1DY(const std::string& nam,const IHistogram2D& hist,int first, int last) {
60  TH2 *r = getRepresentation<IHistogram2D,TH2>(hist);
61  TH1D *t = r ? r->ProjectionY("_py",first,last,"e") : nullptr;
62  if ( t ) t->SetName(nam.c_str());
63  Histogram1D* p = ( t ? new Histogram1D(t) : nullptr );
64  return { p, p };
65 }
66 
68 Gaudi::project1DY(const std::string& nam,const IHistogram2D& hist,int first,int last) {
69  TH2 *r = getRepresentation<IHistogram2D,TH2>(hist);
70  TH1D *t = r ? r->ProjectionY("_px",first,last,"e") : nullptr;
71  if ( t ) t->SetName(nam.c_str());
72  Histogram1D* p = ( t ? new Histogram1D(t) : nullptr );
73  return { p, p };
74 }
75 
77 Gaudi::profile1DX(const std::string& nam,const IHistogram2D& hist,int first,int last) {
78  TH2 *r = Gaudi::getRepresentation<IHistogram2D,TH2>(hist);
79  TProfile *t = r ? r->ProfileX("_pfx",first,last,"e") : nullptr;
80  if ( t ) t->SetName(nam.c_str());
81  Profile1D* p = ( t ? new Profile1D(t) : nullptr );
82  return { p, p };
83 }
84 
86 Gaudi::profile1DY(const std::string& nam, const IHistogram2D& hist,int first, int last) {
87  TH2 *r = getRepresentation<IHistogram2D,TH2>(hist);
88  TProfile *t = r ? r->ProfileY("_pfx",first,last,"e") : nullptr;
89  if ( t ) t->SetName(nam.c_str());
90  Profile1D* p = ( t ? new Profile1D(t) : nullptr );
91  return { p, p };
92 }
93 
94 
95 namespace Gaudi {
96  template <>
97  void * Generic2D<IHistogram2D,TH2D>::cast(const std::string & className) const {
98  if (className == "AIDA::IHistogram2D")
99  return (IHistogram2D*)this;
100  else if (className == "AIDA::IHistogram")
101  return (IHistogram*)this;
102  return nullptr;
103  }
104 
105  template <>
106  int Generic2D<IHistogram2D,TH2D>::binEntries(int indexX,int indexY) const {
107  if (binHeight(indexX, indexY)<=0) return 0;
108  double xx = binHeight(indexX, indexY)/binError(indexX, indexY);
109  return int(xx*xx+0.5);
110  }
111 
112  template <>
114  TH2D* imp = dynamic_cast<TH2D*>(rep);
115  if ( !imp ) throw std::runtime_error("Cannot adopt native histogram representation.");
116  m_rep.reset( imp );
117  m_xAxis.initialize(m_rep->GetXaxis(),true);
118  m_yAxis.initialize(m_rep->GetYaxis(),true);
119  const TArrayD* a = m_rep->GetSumw2();
120  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
121  setTitle(m_rep->GetTitle());
122  }
123 }
124 
126  : Base( new TH2D() )
127 {
128  m_rep->Sumw2();
129  m_sumwx = m_sumwy = 0;
130  setTitle("");
131  m_rep->SetDirectory(nullptr);
132 }
133 
135 {
136  adoptRepresentation(rep);
137  m_sumwx = m_sumwy = 0;
138  m_rep->SetDirectory(nullptr);
139 }
140 
141 bool Gaudi::Histogram2D::setBinContents( int i,int j,int entries,double height,double error,double centreX,double centreY) {
142  m_rep->SetBinContent(rIndexX(i), rIndexY(j), height);
143  m_rep->SetBinError(rIndexX(i), rIndexY(j), error);
144  // accumulate sumwx for in range bins
145  if (i >=0 && j >= 0) {
146  m_sumwx += centreX*height;
147  m_sumwy += centreY*height;
148  }
150  return true;
151 }
152 
154  m_sumwx = 0;
155  m_sumwy = 0;
156  return Base::reset();
157 }
158 
159 #ifdef __ICC
160 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
161 // The comparison are meant
162 #pragma warning(push)
163 #pragma warning(disable:1572)
164 #endif
165 bool Gaudi::Histogram2D::fill ( double x,double y,double weight) {
166  (weight==1.) ? m_rep->Fill(x,y) : m_rep->Fill(x,y,weight );
167  return true;
168 }
169 
170 bool Gaudi::Histogram2D::setRms(double rmsX,double rmsY) {
171  m_rep->SetEntries(m_sumEntries);
172  std::vector<double> stat(11);
173  stat[0] = sumBinHeights();
174  stat[1] = 0;
175  if(equivalentBinEntries() != 0)
176  stat[1] = (sumBinHeights() * sumBinHeights()) / equivalentBinEntries();
177  stat[2] = m_sumwx;
178  double meanX = 0;
179  if(sumBinHeights() != 0) meanX = m_sumwx/ sumBinHeights();
180  stat[3] = (meanX*meanX + rmsX*rmsX) * sumBinHeights();
181  stat[4] = m_sumwy;
182  double meanY = 0;
183  if(sumBinHeights() != 0) meanY = m_sumwy/ sumBinHeights();
184  stat[5] = (meanY*meanY + rmsY*rmsY) * sumBinHeights();
185  stat[6] = 0;
186  m_rep->PutStats(&stat.front());
187  return true;
188 }
189 
190 void Gaudi::Histogram2D::copyFromAida(const IHistogram2D& h) {
191  // implement here the copy
192  const char* tit = h.title().c_str();
193  if (h.xAxis().isFixedBinning() && h.yAxis().isFixedBinning() )
194  m_rep.reset( new TH2D(tit,tit,
195  h.xAxis().bins(),h.xAxis().lowerEdge(),h.xAxis().upperEdge(),
196  h.yAxis().bins(),h.yAxis().lowerEdge(),h.yAxis().upperEdge() ) );
197  else {
198  Edges eX, eY;
199  for (int i =0; i < h.xAxis().bins(); ++i)
200  eX.push_back(h.xAxis().binLowerEdge(i));
201  // add also upperedges at the end
202  eX.push_back(h.xAxis().upperEdge() );
203  for (int i =0; i < h.yAxis().bins(); ++i)
204  eY.push_back(h.yAxis().binLowerEdge(i));
205  // add also upperedges at the end
206  eY.push_back(h.yAxis().upperEdge() );
207  m_rep.reset( new TH2D(tit,tit,eX.size()-1,&eX.front(),eY.size()-1,&eY.front()) );
208  }
209  m_xAxis.initialize(m_rep->GetXaxis(),true);
210  m_yAxis.initialize(m_rep->GetYaxis(),true);
211  m_rep->Sumw2();
212  m_sumEntries = 0;
213  m_sumwx = 0;
214  m_sumwy = 0;
215  // statistics
216  double sumw = h.sumBinHeights();
217  double sumw2 = 0;
218  if (h.equivalentBinEntries() != 0)
219  sumw2 = ( sumw * sumw ) /h.equivalentBinEntries();
220  double sumwx = h.meanX()*h.sumBinHeights();
221  double sumwx2 = (h.meanX()*h.meanX() + h.rmsX()*h.rmsX() )*h.sumBinHeights();
222  double sumwy = h.meanY()*h.sumBinHeights();
223  double sumwy2 = (h.meanY()*h.meanY() + h.rmsY()*h.rmsY() )*h.sumBinHeights();
224  double sumwxy = 0;
225 
226  // copy the contents in (AIDA underflow/overflow are -2,-1)
227  for (int i=-2; i < xAxis().bins(); ++i) {
228  for (int j=-2; j < yAxis().bins(); ++j) {
229  // root binning starts from one !
230  m_rep->SetBinContent(rIndexX(i), rIndexY(j), h.binHeight(i,j) );
231  m_rep->SetBinError(rIndexX(i), rIndexY(j), h.binError(i,j) );
232  // calculate statistics
233  if ( i >= 0 && j >= 0) {
234  sumwxy += h.binHeight(i,j)*h.binMeanX(i,j)*h.binMeanY(i,j);
235  }
236  }
237  }
238  // need to do set entries after setting contents otherwise root will recalculate them
239  // taking into account how many time SetBinContents() has been called
240  m_rep->SetEntries(h.allEntries());
241  // fill stat vector
242  std::array<double,11> stat = {{ sumw, sumw2,
243  sumwx, sumwx2,
244  sumwy, sumwy2,
245  sumwxy }};
246  m_rep->PutStats(stat.data());
247 }
248 
249 #ifdef __ICC
250 // re-enable icc remark #1572
251 #pragma warning(pop)
252 #endif
int m_sumEntries
cache sumEntries (allEntries) when setting contents since Root can&#39;t compute by himself ...
Definition: Generic2D.h:154
std::pair< DataObject *, AIDA::IHistogram1D * > slice1DY(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D slice from 2D histogram.
bool setRms(double rmsX, double rmsY)
Sets the rms of the histogram.
Definition: H2D.cpp:170
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic2D.h:310
double rmsX() const override
Returns the rms of the profile as calculated on filling-time projected on the X axis.
Definition: Generic2D.h:273
T front(T...args)
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.
bool fill(double x, double y, double weight=1.) override
Fill the Histogram2D with a value and the.
Definition: H2D.cpp:165
bool reset() override
Definition: H2D.cpp:153
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:141
std::vector< double > Edges
Definition: GaudiPI.h:17
double rmsY() const override
Returns the rms of the profile as calculated on filling-time projected on the Y axis.
Definition: Generic2D.h:278
STL class.
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
virtual int rIndexX(int index) const
operator methods
Definition: Generic2D.h:68
Histogram2D()
Standard Constructor.
Definition: H2D.cpp:125
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic2D.h:195
T push_back(T...args)
T data(T...args)
Axis m_xAxis
X axis member.
Definition: Generic2D.h:144
int binEntries(int indexX, int indexY) const override
The number of entries (ie the number of times fill was called for this bin).
std::pair< DataObject *, AIDA::IHistogram2D * > createH2D(const AIDA::IHistogram2D &hist)
Copy constructor.
std::pair< DataObject *, AIDA::IHistogram1D * > slice1DX(const std::string &name, const AIDA::IHistogram2D &h, int firstbin, int lastbin)
Create 1D slice from 2D histogram.
T reset(T...args)
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.
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.
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:66
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic2D.h:158
double meanX() const override
Returns the mean of the profile, as calculated on filling-time projected on the X axis...
Definition: Generic2D.h:263
T c_str(T...args)
Axis m_yAxis
Y axis member.
Definition: Generic2D.h:146
void * cast(const std::string &className) const override
Introspection method.
STL class.
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic2D.h:150
int entries() const override
Get the number or all the entries.
Definition: Generic2D.h:175
void copyFromAida(const AIDA::IHistogram2D &h)
Create new histogram from any AIDA based histogram.
Definition: H2D.cpp:190
virtual int rIndexY(int index) const
operator methods
Definition: Generic2D.h:70
double m_sumwy
Definition: H2D.h:43
double m_sumwx
Definition: H2D.h:42
double meanY() const override
Returns the mean of the profile, as calculated on filling-time projected on the Y axis...
Definition: Generic2D.h:268
Helper functions to set/get the application return code.
Definition: __init__.py:1
const AIDA::IAxis & yAxis() const override
Return the Y axis.
Definition: Generic2D.h:66
const AIDA::IAxis & xAxis() const override
Return the X axis.
Definition: Generic2D.h:64
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic2D.h:35