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