H1D.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
14 #include <GaudiCommonSvc/H1D.h>
16 #include "GaudiPI.h"
17 
18 std::pair<DataObject*,AIDA::IHistogram1D*> Gaudi::createH1D(const std::string& title,int nBins,double xlow, double xup) {
19  auto p = new Histogram1D(new TH1D(title.c_str(),title.c_str(),nBins,xlow,xup));
20  return { p, p };
21 }
22 
24  auto p = new Histogram1D(new TH1D(title.c_str(),title.c_str(),e.size()-1,&e.front()));
25  return { p, p };
26 }
27 
28 std::pair<DataObject*,AIDA::IHistogram1D*> Gaudi::createH1D(const AIDA::IHistogram1D& hist) {
29  TH1D *h = getRepresentation<AIDA::IHistogram1D,TH1D>(hist);
30  auto n = ( h ? new Histogram1D(new TH1D(*h)) : nullptr );
31  return { n, n };
32 }
33 namespace Gaudi {
34 
35  template<> void *Generic1D<AIDA::IHistogram1D,TH1D>::cast(const std::string & className) const {
36  if (className == "AIDA::IHistogram1D")
37  return const_cast<AIDA::IHistogram1D*>((AIDA::IHistogram1D*)this);
38  if (className == "AIDA::IHistogram")
39  return const_cast<AIDA::IHistogram*>((AIDA::IHistogram*)this);
40  return nullptr;
41  }
42 
43 
44  template<> int Generic1D<AIDA::IHistogram1D,TH1D>::binEntries (int index) const {
45  if (binHeight(index)<=0) return 0;
46  double xx = binHeight(index)/binError(index);
47  return int(xx*xx+0.5);
48  }
49 
50  template <>
52  TH1D* imp = dynamic_cast<TH1D*>(rep);
53  if ( !imp ) throw std::runtime_error("Cannot adopt native histogram representation.");
54  m_rep.reset(imp);
55  }
56 }
57 
59  : Base( new TH1D() )
60 {
61  init("",false);
62 }
63 
65  m_rep.reset( rep );
66  init(m_rep->GetTitle());
67  initSums();
68 }
69 
70 void Gaudi::Histogram1D::init(const std::string& title, bool initialize_axis) {
71  m_classType = "IHistogram1D";
72  if ( initialize_axis ) {
73  m_axis.initialize(m_rep->GetXaxis(),false);
74  }
75  const TArrayD* a = m_rep->GetSumw2();
76  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
77  setTitle(title);
78  m_rep->SetDirectory(nullptr);
79  m_sumEntries = 0;
80  m_sumwx = 0;
81 }
82 
84  m_sumwx = 0;
85  m_sumEntries = 0;
86  for(int i=1, n=m_rep->GetNbinsX(); i<=n; ++i) {
87  m_sumwx += m_rep->GetBinContent(i)*m_rep->GetBinCenter(i);
88  m_sumEntries += m_rep->GetBinContent(i);
89  }
90 }
91 
93  m_sumwx = 0;
94  m_sumEntries = 0;
95  return Base::reset();
96 }
97 
101  if ( m_rep ) {
102  init(m_rep->GetTitle());
103  initSums();
104  }
105 }
106 
107 bool Gaudi::Histogram1D::setBinContents(int i,int entries ,double height,double error,double centre) {
108  m_rep->SetBinContent(rIndex(i),height);
109  m_rep->SetBinError(rIndex(i),error);
110  // accumulate sumwx for in range bins
111  if (i != AIDA::IAxis::UNDERFLOW_BIN && i != AIDA::IAxis::OVERFLOW_BIN )
112  m_sumwx += centre*height;
114  return true;
115 }
116 
117 #ifdef __ICC
118 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
119 // The comparison are meant
120 #pragma warning(push)
121 #pragma warning(disable:1572)
122 #endif
124  m_rep->SetEntries(m_sumEntries);
125  std::vector<double> stat(11);
126  // sum weights
127  stat[0] = sumBinHeights();
128  stat[1] = 0;
129  if (equivalentBinEntries() != 0)
130  stat[1] = ( sumBinHeights() * sumBinHeights() ) / equivalentBinEntries();
131  stat[2] = m_sumwx;
132  double mean = 0;
133  if ( sumBinHeights() != 0 ) mean = m_sumwx/ sumBinHeights();
134  stat[3] = ( mean*mean + rms*rms )* sumBinHeights();
135  m_rep->PutStats(&stat.front());
136  return true;
137 }
138 
139 // set histogram statistics
140 bool Gaudi::Histogram1D::setStatistics(int allEntries,double eqBinEntries,double mean,double rms) {
141  m_rep->SetEntries(allEntries);
142  // fill statistcal vector for Root
143  std::vector<double> stat(11);
144  // sum weights
145  stat[0] = sumBinHeights();
146  // sum weights **2
147  stat[1] = 0;
148  if (eqBinEntries != 0)
149  stat[1] = ( sumBinHeights() * sumBinHeights() ) / eqBinEntries;
150  // sum weights * x
151  stat[2] = mean*sumBinHeights();
152  // sum weight * x **2
153  stat[3] = ( mean*mean + rms*rms )* sumBinHeights();
154  m_rep->PutStats(&stat.front());
155  return true;
156 }
157 
158 bool Gaudi::Histogram1D::fill ( double x,double weight ) {
159  (weight == 1.) ? m_rep->Fill(x) : m_rep->Fill(x,weight);
160  return true;
161 }
162 
163 void Gaudi::Histogram1D::copyFromAida(const AIDA::IHistogram1D & h) {
164  // implement here the copy
165  std::string tit = h.title()+"Copy";
166  if (h.axis().isFixedBinning() ) {
167  m_rep.reset( new TH1D(tit.c_str(),tit.c_str(),h.axis().bins(),h.axis().lowerEdge(),h.axis().upperEdge()) );
168  }
169  else {
170  Edges e;
171  for (int i =0; i < h.axis().bins(); ++i) {
172  e.push_back(h.axis().binLowerEdge(i));
173  }
174  // add also upperedges at the end
175  e.push_back(h.axis().upperEdge() );
176  m_rep.reset( new TH1D(tit.c_str(),tit.c_str(),e.size()-1,&e.front()) );
177  }
178  m_axis.initialize(m_rep->GetXaxis(),false);
179  m_rep->Sumw2();
180  m_sumEntries = 0;
181  m_sumwx = 0;
182  // sumw
183  double sumw = h.sumBinHeights();
184  // sumw2
185  double sumw2 = 0;
186  if (h.equivalentBinEntries() != 0)
187  sumw2 = ( sumw * sumw ) /h.equivalentBinEntries();
188 
189  double sumwx = h.mean()*h.sumBinHeights();
190  double sumwx2 = (h.mean()*h.mean() + h.rms()*h.rms() )*h.sumBinHeights();
191 
192  // copy the contents in
193  for (int i=-2; i < axis().bins(); ++i) {
194  // root binning starts from one !
195  m_rep->SetBinContent(rIndex(i),h.binHeight(i) );
196  m_rep->SetBinError(rIndex(i),h.binError(i) );
197  }
198  // need to do set entries after setting contents otherwise root will recalulate them
199  // taking into account how many time SetBinContents() has been called
200  m_rep->SetEntries(h.allEntries());
201  // stat vector
202  std::vector<double> stat(11);
203  stat[0] = sumw;
204  stat[1] = sumw2;
205  stat[2] = sumwx;
206  stat[3] = sumwx2;
207  m_rep->PutStats(&stat.front());
208 }
209 
210 #ifdef __ICC
211 // re-enable icc remark #1572
212 #pragma warning(pop)
213 #endif
214 
216  //DataObject::serialize(s);
218  int size;
219  s >> size;
220  for (int j = 0; j < size; j++) {
221  std::string key, value;
222  s >> key >> value;
223  annotation().addItem (key, value);
224  if ("Title" == key) {
225  title = value;
226  }
227  }
228  double lowerEdge, upperEdge, binHeight, binError;
229  int isFixedBinning, bins;
230  s >> isFixedBinning >> bins;
231 
232  if ( isFixedBinning ) {
233  s >> lowerEdge >> upperEdge;
234  m_rep.reset( new TH1D(title.c_str(),title.c_str(),bins,lowerEdge,upperEdge) );
235  } else {
236  Edges edges;
237  edges.resize(bins);
238  for ( int i = 0; i <= bins; ++i )
239  s >> *(double*)&edges[i];
240  m_rep.reset( new TH1D(title.c_str(),title.c_str(),edges.size()-1,&edges.front()) );
241  }
242  m_axis.initialize(m_rep->GetXaxis(),true);
243  m_rep->Sumw2();
244  m_sumEntries = 0;
245  m_sumwx = 0;
246 
247  for ( int i = 0; i <= bins + 1; ++i ) {
248  s >> binHeight >> binError;
249  m_rep->SetBinContent( i, binHeight );
250  m_rep->SetBinError( i, binError );
251  }
252  Stat_t allEntries;
253  s >> allEntries;
254  m_rep->SetEntries( allEntries );
255  Stat_t stats[4]; // stats array
256  s >> stats[0] >> stats[1] >> stats[2] >> stats[3];
257  m_rep->PutStats( stats );
258  return s;
259 }
260 
262  //DataObject::serialize(s);
263  s << static_cast<int>( annotation().size() );
264  for (int i = 0; i < annotation().size(); i++) {
265  s << annotation().key(i);
266  s << annotation().value(i);
267  }
268  const AIDA::IAxis & axis( this->axis() );
269  const int isFixedBinning = axis.isFixedBinning();
270  const int bins = axis.bins();
271  s << isFixedBinning << bins;
272  if ( isFixedBinning ) {
273  s << axis.lowerEdge();
274  }
275  else {
276  for ( int i = 0; i < bins; ++i )
277  s << axis.binLowerEdge(i);
278  }
279  s << axis.upperEdge();
280  for ( int i = 0; i <= bins + 1; ++i )
281  s << m_rep->GetBinContent(i) << m_rep->GetBinError( i );
282 
283  s << m_rep->GetEntries();
284  Stat_t stats[4]; // stats array
285  m_rep->GetStats( stats );
286  s << stats[0] << stats[1] << stats[2] << stats[3];
287  return s;
288 }
bool fill(double x, double weight) override
Fill the Profile1D with a value and the corresponding weight.
Definition: H1D.cpp:158
void initSums()
Definition: H1D.cpp:83
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:55
void * cast(const std::string &cl) const override
Manual cast by class name.
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Definition: H1D.cpp:99
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:72
virtual bool setStatistics(int allEntries, double eqBinEntries, double mean, double rms)
set histogram statistics
Definition: H1D.cpp:140
std::pair< DataObject *, AIDA::IHistogram1D * > createH1D(const AIDA::IHistogram1D &hist)
Copy constructor.
T front(T...args)
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
StreamBuffer & serialize(StreamBuffer &s)
Serialization mechanism, Serialize the object for reading.
Definition: H1D.cpp:215
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic1D.h:82
std::string m_classType
Definition: Generic1D.h:131
bool reset() override
need to overwrite reset to reset the sums
Definition: H1D.cpp:92
double binHeight(int index) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic1D.h:164
std::vector< double > Edges
Definition: GaudiPI.h:17
void init(const std::string &title, bool initialize_axis=true)
Definition: H1D.cpp:70
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
T resize(T...args)
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:169
STL class.
Axis & axis()
Access to axis object.
Definition: Generic1D.h:67
T push_back(T...args)
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:129
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:137
int allEntries() const override
Get the number or all the entries, both in range and underflow/overflow bins of the IProfile...
Definition: Generic1D.h:74
Axis m_axis
Axis member.
Definition: Generic1D.h:125
int bins() const override
The number of bins (excluding underflow and overflow) on the IAxis.
Definition: Axis.h:97
int binEntries(int index) const override
Number of entries in the corresponding bin (ie the number of times fill was called for this bin)...
T reset(T...args)
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:111
void copyFromAida(const AIDA::IHistogram1D &h)
Create new histogram from any AIDA based histogram.
Definition: H1D.cpp:163
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:66
bool setRms(double rms)
Update histogram RMS.
Definition: H1D.cpp:123
T c_str(T...args)
string s
Definition: gaudirun.py:245
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:186
virtual bool setBinContents(int i, int entries, double height, double error, double centre)
set bin content (entries and centre are not used )
Definition: H1D.cpp:107
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:101
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:63
Histogram1D()
Standard constructor.
Definition: H1D.cpp:58
Helper functions to set/get the application return code.
Definition: __init__.py:1
double mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:109
double m_sumwx
cache sumwx when setting contents since I don&#39;t have bin mean
Definition: H1D.h:23
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:35