herwig is hosted by Hepforge, IPPP Durham
Herwig  7.2.1
Histogram.h
1 // -*- C++ -*-
2 //
3 // Histogram.h is a part of Herwig - A multi-purpose Monte Carlo event generator
4 // Copyright (C) 2002-2019 The Herwig Collaboration
5 //
6 // Herwig is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef HERWIG_Histogram_H
10 #define HERWIG_Histogram_H
11 //
12 // This is the declaration of the Histogram class.
13 //
14 #include "Histogram.fh"
15 #include "ThePEG/Interface/Interfaced.h"
16 #include "Statistic.h"
17 #include <string>
18 
19 namespace Herwig {
20 
21 using namespace ThePEG;
22 
27  namespace HistogramOptions {
28  const unsigned int None = 0;
29  const unsigned int Frame = 1;
30  const unsigned int Errorbars = 1 << 1;
31  const unsigned int Xlog = 1 << 2;
32  const unsigned int Ylog = 1 << 3;
33  const unsigned int Smooth = 1 << 4;
34  const unsigned int Rawcount = 1 << 5;
35  }
36 
43 class Histogram: public Interfaced {
44 
45 public:
46 
55  Histogram(double lower=0., double upper=0., unsigned int nbin=0)
56  : _globalStats(), _havedata(false), _bins(nbin+2),_prefactor(1.),_total(0.) {
57  if (upper<lower) swap(upper,lower);
58  _bins[0].limit=-1.e100;
59  double limit(lower);
60  double width((upper-lower)/nbin);
61  for(unsigned int ix=1; ix <= nbin; ++ix) {
62  _bins[ix].limit=limit;
63  limit += width;
64  }
65  _bins.back().limit=limit;
66  }
67 
72  Histogram(vector<double> limits)
73  : _globalStats(), _havedata(false), _bins(limits.size()+1), _prefactor(1.),_total(0.) {
74  _bins[0].limit=-1.e100;
75  for (size_t i=1; i<=limits.size(); ++i)
76  _bins[i].limit=limits[i-1];
77  }
78 
85  Histogram(vector<double> limits, vector<double> data, vector<double> dataerror)
86  : _globalStats(), _havedata(true), _bins(limits.size()+1), _prefactor(1.),_total(0.) {
87  _bins[0].limit=-1.e100;
88  for (size_t i=1; i<=limits.size(); ++i)
89  _bins[i].limit=limits[i-1];
90 
91  // no data goes into _bins[0] or _bins.back()!
92  for (size_t i=1; i<=min(limits.size()-1,data.size()); ++i)
93  _bins[i].data=data[i-1];
94 
95  for (size_t i=1; i<=min(limits.size()-1,dataerror.size()); ++i)
96  _bins[i].dataerror=dataerror[i-1];
97  }
98 
100 
101 public:
102 
106  void operator += (double input) {
107  addWeighted(input,1.0);
108  }
109 
113  void addWeighted(double input, double weight) {
114  if(std::isnan(input)) return;
115  unsigned int ibin;
116  for(ibin=1; ibin<_bins.size(); ++ibin) {
117  if(input<_bins[ibin].limit)
118  break;
119  }
120  _bins[ibin-1].contents += weight;
121  _bins[ibin-1].contentsSq += sqr(weight);
122  _globalStats += weight * input;
123  _total += weight;
124  }
125 
129  unsigned int numberOfBins() const {
130  return _bins.size()-2;
131  }
132 
133 
137  double prefactor() const {
138  return _prefactor;
139  }
140 
144  void prefactor(double in ) {
145  _prefactor=in;
146  }
147 
151  const Statistic & globalStatistics() const {
152  return _globalStats;
153  }
154 
158  void normaliseToData();
159 
163  void normaliseToCrossSection();
164 
171  void chiSquared(double & chisq,
172  unsigned int & ndegrees, double minfrac=0.) const;
173 
188  void rivetOutput(ostream & out,
189  string histogramname = string("default"),
190  string analysisname = string("default"),
191  string title = string(),
192  string xlabel = string(),
193  string ylabel = string(),
194  bool rawcount = false,
195  double multiplicator = 1.0) const;
196 
210  void topdrawOutput(ostream & out,
211  unsigned int flags = 0,
212  string colour = string("BLACK"),
213  string title = string(),
214  string titlecase = string(),
215  string left = string(),
216  string leftcase = string(),
217  string bottom = string(),
218  string bottomcase = string()
219  ) const;
220 
221  void topdrawMCatNLO(ostream & out,
222  unsigned int flags =0 ,
223  string colour = string("BLACK"),
224  string title = string()
225  ) const;
226 
242  void topdrawOutputAverage(ostream & out,
243  bool frame,
244  bool errorbars,
245  bool xlog, bool ylog,
246  string colour=string("BLACK"),
247  string title=string(),
248  string titlecase =string(),
249  string left=string(),
250  string leftcase =string(),
251  string bottom=string(),
252  string bottomcase =string()) const;
253 
259  unsigned int visibleEntries() const;
260 
264  double dataNorm() const;
265 
269  void simpleOutput(ostream & out, bool errorbars, bool normdata=false);
270 
274  vector<double> dumpBins() const;
275 
279  Histogram ratioWith(const Histogram & h2) const;
280 
281 
289  static
290  vector<double> LogBins(double xmin, unsigned nbins, double base = 10.0);
291 
292 
293 public:
294 
301  static void Init();
302 
303 protected:
304 
311  virtual IBPtr clone() const;
312 
317  virtual IBPtr fullclone() const;
319 
320 private:
321 
326  Histogram & operator=(const Histogram &) = delete;
327 
328 private:
329 
334 
338  bool _havedata;
339 
343  struct Bin {
347  Bin() : contents(0.0), contentsSq(0.0),
348  limit(0.0), data(0.0), dataerror(0.0), points(0) {}
352  double contents;
353 
357  double contentsSq;
358 
362  double limit;
363 
367  double data;
368 
372  double dataerror;
373 
377  long points;
378  };
379 
383  vector<Bin> _bins;
384 
388  double _prefactor;
389 
393  double _total;
394 
395 
396 public:
397 
401  vector<Bin> bins() const { return _bins; }
402 
403 };
404 
405 }
406 
407 #endif /* HERWIG_Histogram_H */
double dataerror
The error on the experimental value for the bin.
Definition: Histogram.h:372
Histogram(double lower=0., double upper=0., unsigned int nbin=0)
The default constructor.
Definition: Histogram.h:55
The Statistic class is a simple class designed to store a variable for statistical analysis...
Definition: Statistic.h:23
Bin()
Default constructor.
Definition: Histogram.h:347
double contents
Contents of the bin.
Definition: Histogram.h:352
bool _havedata
Set to true if there is experimental data available.
Definition: Histogram.h:338
ThePEG::Ptr< InterfacedBase >::pointer IBPtr
double prefactor() const
Get the prefactor.
Definition: Histogram.h:137
The Histogram class is a simple histogram for the Analysis handlers.
Definition: Histogram.h:43
constexpr auto sqr(const T &x) -> decltype(x *x)
ostream & left(ostream &os)
const unsigned int Smooth
smooth the line
Definition: Histogram.h:33
vector< Bin > bins() const
The vector of bins.
Definition: Histogram.h:401
double _prefactor
Prefactors to multiply the output by.
Definition: Histogram.h:388
Histogram(vector< double > limits)
Constructor for variable width bins.
Definition: Histogram.h:72
const unsigned int Xlog
log scale for x-axis
Definition: Histogram.h:31
void prefactor(double in)
Set the prefactor.
Definition: Histogram.h:144
const unsigned int Rawcount
don&#39;t normalize to unit area
Definition: Histogram.h:34
double _total
Total entry.
Definition: Histogram.h:393
double limit
The limit for the bin.
Definition: Histogram.h:362
vector< Bin > _bins
The histogram bins.
Definition: Histogram.h:383
double contentsSq
Contents squared for the error.
Definition: Histogram.h:357
double data
The experimental value for the bin.
Definition: Histogram.h:367
unsigned int numberOfBins() const
Number of bins (not counting the overflow)
Definition: Histogram.h:129
void addWeighted(double input, double weight)
Function to add a weighted point to the histogram.
Definition: Histogram.h:113
const unsigned int Errorbars
Plot error bars.
Definition: Histogram.h:30
const unsigned int Frame
Plot on new frame.
Definition: Histogram.h:29
Histogram(vector< double > limits, vector< double > data, vector< double > dataerror)
Constructor with data included.
Definition: Histogram.h:85
One bin of the histogram.
Definition: Histogram.h:343
-*- C++ -*-
Statistic _globalStats
Global statistics of all data that went into the histogram.
Definition: Histogram.h:333
const unsigned int Ylog
log scale for y-axis
Definition: Histogram.h:32
const unsigned int None
No options.
Definition: Histogram.h:28
const Statistic & globalStatistics() const
Access to the statistics on the total entry of the histogram.
Definition: Histogram.h:151
long points
The number of points in the bin.
Definition: Histogram.h:377