The Gaudi Framework  v30r4 (9b837755)
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>
17 
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 
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 <>
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 <>
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 <>
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 
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 
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 
99 {
100  m_sumwx = 0;
101  m_sumEntries = 0;
102  return Base::reset();
103 }
104 
107 {
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;
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
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 title = h.title() + "Copy";
176  if ( h.axis().isFixedBinning() ) {
177  m_rep.reset(
178  new TH1D( title.c_str(), title.c_str(), h.axis().bins(), h.axis().lowerEdge(), h.axis().upperEdge() ) );
179  } else {
180  Edges e;
181  for ( int i = 0; i < h.axis().bins(); ++i ) {
182  e.push_back( h.axis().binLowerEdge( i ) );
183  }
184  // add also upperedges at the end
185  e.push_back( h.axis().upperEdge() );
186  m_rep.reset( new TH1D( title.c_str(), title.c_str(), e.size() - 1, &e.front() ) );
187  }
188  m_axis.initialize( m_rep->GetXaxis(), false );
189  m_rep->Sumw2();
190  m_sumEntries = 0;
191  m_sumwx = 0;
192  // sumw
193  double sumw = h.sumBinHeights();
194  // sumw2
195  double sumw2 = 0;
196  if ( h.equivalentBinEntries() != 0 ) sumw2 = ( sumw * sumw ) / h.equivalentBinEntries();
197 
198  double sumwx = h.mean() * h.sumBinHeights();
199  double sumwx2 = ( h.mean() * h.mean() + h.rms() * h.rms() ) * h.sumBinHeights();
200 
201  // copy the contents in
202  for ( int i = -2; i < axis().bins(); ++i ) {
203  // root binning starts from one !
204  m_rep->SetBinContent( rIndex( i ), h.binHeight( i ) );
205  m_rep->SetBinError( rIndex( i ), h.binError( i ) );
206  }
207  // need to do set entries after setting contents otherwise root will recalulate them
208  // taking into account how many time SetBinContents() has been called
209  m_rep->SetEntries( h.allEntries() );
210  // stat vector
211  std::vector<double> stat( 11 );
212  stat[0] = sumw;
213  stat[1] = sumw2;
214  stat[2] = sumwx;
215  stat[3] = sumwx2;
216  m_rep->PutStats( &stat.front() );
217 }
218 
219 #ifdef __ICC
220 // re-enable icc remark #1572
221 #pragma warning( pop )
222 #endif
223 
225 {
226  // DataObject::serialize(s);
228  int size;
229  s >> size;
230  for ( int j = 0; j < size; j++ ) {
231  std::string key, value;
232  s >> key >> value;
233  annotation().addItem( key, value );
234  if ( "Title" == key ) {
235  title = value;
236  }
237  }
238  double lowerEdge, upperEdge, binHeight, binError;
239  int isFixedBinning, bins;
240  s >> isFixedBinning >> bins;
241 
242  if ( isFixedBinning ) {
243  s >> lowerEdge >> upperEdge;
244  m_rep.reset( new TH1D( title.c_str(), title.c_str(), bins, lowerEdge, upperEdge ) );
245  } else {
246  Edges edges;
247  edges.resize( bins );
248  for ( int i = 0; i <= bins; ++i ) s >> *(double*)&edges[i];
249  m_rep.reset( new TH1D( title.c_str(), title.c_str(), edges.size() - 1, &edges.front() ) );
250  }
251  m_axis.initialize( m_rep->GetXaxis(), true );
252  m_rep->Sumw2();
253  m_sumEntries = 0;
254  m_sumwx = 0;
255 
256  for ( int i = 0; i <= bins + 1; ++i ) {
257  s >> binHeight >> binError;
258  m_rep->SetBinContent( i, binHeight );
259  m_rep->SetBinError( i, binError );
260  }
261  Stat_t allEntries;
262  s >> allEntries;
263  m_rep->SetEntries( allEntries );
264  Stat_t stats[4]; // stats array
265  s >> stats[0] >> stats[1] >> stats[2] >> stats[3];
266  m_rep->PutStats( stats );
267  return s;
268 }
269 
271 {
272  // DataObject::serialize(s);
273  s << static_cast<int>( annotation().size() );
274  for ( int i = 0; i < annotation().size(); i++ ) {
275  s << annotation().key( i );
276  s << annotation().value( i );
277  }
278  const AIDA::IAxis& axis( this->axis() );
279  const int isFixedBinning = axis.isFixedBinning();
280  const int bins = axis.bins();
281  s << isFixedBinning << bins;
282  if ( isFixedBinning ) {
283  s << axis.lowerEdge();
284  } else {
285  for ( int i = 0; i < bins; ++i ) s << axis.binLowerEdge( i );
286  }
287  s << axis.upperEdge();
288  for ( int i = 0; i <= bins + 1; ++i ) s << m_rep->GetBinContent( i ) << m_rep->GetBinError( i );
289 
290  s << m_rep->GetEntries();
291  Stat_t stats[4]; // stats array
292  m_rep->GetStats( stats );
293  s << stats[0] << stats[1] << stats[2] << stats[3];
294  return s;
295 }
bool fill(double x, double weight) override
Fill the Profile1D with a value and the corresponding weight.
Definition: H1D.cpp:166
void initSums()
Definition: H1D.cpp:88
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:106
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:148
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:224
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:134
bool reset() override
need to overwrite reset to reset the sums
Definition: H1D.cpp:98
double binHeight(int index) const override
Total height of the corresponding bin (ie the sum of the weights in this bin).
Definition: Generic1D.h:169
std::vector< double > Edges
Definition: GaudiPI.h:18
void init(const std::string &title, bool initialize_axis=true)
Definition: H1D.cpp:74
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
T resize(T...args)
double binError(int index) const override
The error of a given bin.
Definition: Generic1D.h:175
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:132
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:140
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:128
int bins() const override
The number of bins (excluding underflow and overflow) on the IAxis.
Definition: Axis.h:95
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:114
void copyFromAida(const AIDA::IHistogram1D &h)
Create new histogram from any AIDA based histogram.
Definition: H1D.cpp:172
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:67
bool setRms(double rms)
Update histogram RMS.
Definition: H1D.cpp:131
T c_str(T...args)
string s
Definition: gaudirun.py:253
virtual double equivalentBinEntries() const
Number of equivalent entries, i.e. SUM[ weight ] ^ 2 / SUM[ weight^2 ]
Definition: Generic1D.h:194
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
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:104
AIDA::IAnnotation & annotation() override
Access annotation object.
Definition: Generic1D.h:65
Histogram1D()
Standard constructor.
Definition: H1D.cpp:65
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::pair< DataObject *, AIDA::IHistogram1D * > createH1D(const AIDA::IHistogram1D &hist)
Copy constructor.
double mean() const override
The mean of the whole IHistogram1D.
Definition: Generic1D.h:112
double m_sumwx
cache sumwx when setting contents since I don&#39;t have bin mean
Definition: H1D.h:26
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:36