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