| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <GaudiKernel/StreamBuffer.h>
13 #include <GaudiKernel/ObjectFactory.h>
14 #include <GaudiCommonSvc/H1D.h>
15 #include <GaudiCommonSvc/HistogramUtility.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
23 std::pair<DataObject*,AIDA::IHistogram1D*> Gaudi::createH1D(const std::string& title, const Edges& e) {
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 <>
51 void Generic1D<AIDA::IHistogram1D,TH1D>::adoptRepresentation(TObject* rep) {
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
58 Gaudi::Histogram1D::Histogram1D()
59 : Base( new TH1D() )
60 {
61 init("",false);
62 }
63
64 Gaudi::Histogram1D::Histogram1D(TH1D* rep) {
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
83 void Gaudi::Histogram1D::initSums() {
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
92 bool Gaudi::Histogram1D::reset() {
93 m_sumwx = 0;
94 m_sumEntries = 0;
95 return Base::reset();
96 }
97
99 void Gaudi::Histogram1D::adoptRepresentation(TObject* rep) {
100 Gaudi::Generic1D<AIDA::IHistogram1D,TH1D>::adoptRepresentation(rep);
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;
113 m_sumEntries += entries;
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
123 bool Gaudi::Histogram1D::setRms(double rms) {
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
215 StreamBuffer& Gaudi::Histogram1D::serialize(StreamBuffer& s) {
216 //DataObject::serialize(s);
217 std::string title;
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
261 StreamBuffer& Gaudi::Histogram1D::serialize(StreamBuffer& s) const {
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 }
Gaudi::Histogram1D::fill
bool fill(double x, double weight) override
Fill the Profile1D with a value and the corresponding weight.
Definition: H1D.cpp:158
Gaudi::Histogram1D::initSums
void initSums()
Definition: H1D.cpp:83
Gaudi::Generic1D::title
std::string title() const override
Get the title of the object.
Definition: Generic1D.h:55
Gaudi::Generic1D::cast
void * cast(const std::string &cl) const override
Manual cast by class name.
Gaudi::Histogram1D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
Definition: H1D.cpp:99
Gaudi::Generic1D::entries
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:72
Gaudi::Histogram1D::setStatistics
virtual bool setStatistics(int allEntries, double eqBinEntries, double mean, double rms)
set histogram statistics
Definition: H1D.cpp:140
Gaudi::createH1D
std::pair< DataObject *, AIDA::IHistogram1D * > createH1D(const AIDA::IHistogram1D &hist)
Copy constructor.
std::vector::front
T front(T...args)
StreamBuffer
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
Gaudi::Histogram1D::serialize
StreamBuffer & serialize(StreamBuffer &s)
Serialization mechanism, Serialize the object for reading.
Definition: H1D.cpp:215
Gaudi::Generic1D::sumBinHeights
double sumBinHeights() const override
Get the sum of in range bin heights in the IProfile.
Definition: Generic1D.h:82
Gaudi::Generic1D::m_classType
std::string m_classType
Definition: Generic1D.h:131
ObjectFactory.h
Gaudi::Histogram1D::reset
bool reset() override
need to overwrite reset to reset the sums
Definition: H1D.cpp:92
Gaudi::Generic1D::binHeight
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
Gaudi::Edges
std::vector< double > Edges
Definition: GaudiPI.h:17
Gaudi::Histogram1D::init
void init(const std::string &title, bool initialize_axis=true)
Definition: H1D.cpp:70
Gaudi::Generic1D::adoptRepresentation
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
StreamBuffer.h
GaudiPI.h
std::vector::resize
T resize(T...args)
Gaudi::Generic1D::binError
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:169
std::string
STL class.
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:217
Gaudi::Generic1D::axis
Axis & axis()
Access to axis object.
Definition: Generic1D.h:67
std::vector::push_back
T push_back(T...args)
Gaudi::Generic1D::m_rep
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:129
HiveAlgSequencer.h
h
Definition: HiveAlgSequencer.py:24
Gaudi::Generic1D::setTitle
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:137
Gaudi::Generic1D::allEntries
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
std::runtime_error
STL class.
Gaudi::Generic1D::m_axis
Axis m_axis
Axis member.
Definition: Generic1D.h:125
Gaudi::Axis::bins
int bins() const override
The number of bins (excluding underflow and overflow) on the IAxis.
Definition: Axis.h:97
Gaudi::Generic1D::binEntries
int binEntries(int index) const override
Number of entries in the corresponding bin (ie the number of times fill was called for this bin)...
std::unique_ptr::reset
T reset(T...args)
Gaudi::Generic1D::rms
double rms() const override
The RMS of the whole IHistogram1D.
Definition: Generic1D.h:111
Gaudi::Histogram1D::copyFromAida
void copyFromAida(const AIDA::IHistogram1D &h)
Create new histogram from any AIDA based histogram.
Definition: H1D.cpp:163
Gaudi::Generic1D::m_sumEntries
int m_sumEntries
Definition: Generic1D.h:133
std::pair
std::vector< double >
Gaudi::Axis::initialize
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:66
Gaudi::Histogram1D::setRms
bool setRms(double rms)
Update histogram RMS.
Definition: H1D.cpp:123
H1D.h
std::string::c_str
T c_str(T...args)
gaudirun.s
string s
Definition: gaudirun.py:245
Gaudi::Generic1D::equivalentBinEntries
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:186
HistogramUtility.h
Gaudi::Histogram1D::setBinContents
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
Gaudi::Generic1D::rIndex
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:101
Gaudi::Generic1D::annotation
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:63
Gaudi::Histogram1D::Histogram1D
Histogram1D()
Standard constructor.
Definition: H1D.cpp:58
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::Generic1D::mean
double mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:109
Gaudi::Histogram1D::m_sumwx
double m_sumwx
cache sumwx when setting contents since I don&#39;t have bin mean
Definition: H1D.h:23
Gaudi::Generic1D
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:35