| The Gaudi Framework v30r0 (c919700c)

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