herwig is hosted by Hepforge, IPPP Durham
Herwig 7.3.0
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
19namespace Herwig {
20
21using 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
43class Histogram: public Interfaced {
44
45public:
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
101public:
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
159
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
293public:
294
301 static void Init();
302
303protected:
304
311 virtual IBPtr clone() const;
312
317 virtual IBPtr fullclone() const;
319
320private:
321
326 Histogram & operator=(const Histogram &) = delete;
327
328private:
329
334
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
358
362 double limit;
363
367 double data;
368
372 double dataerror;
373
377 long points;
378 };
379
383 vector<Bin> _bins;
384
389
393 double _total;
394
395
396public:
397
401 vector<Bin> bins() const { return _bins; }
402
403};
404
405}
406
407#endif /* HERWIG_Histogram_H */
The Histogram class is a simple histogram for the Analysis handlers.
Definition: Histogram.h:43
const Statistic & globalStatistics() const
Access to the statistics on the total entry of the histogram.
Definition: Histogram.h:151
Histogram & operator=(const Histogram &)=delete
The assignment operator is private and must never be called.
vector< Bin > bins() const
The vector of bins.
Definition: Histogram.h:401
unsigned int numberOfBins() const
Number of bins (not counting the overflow)
Definition: Histogram.h:129
void simpleOutput(ostream &out, bool errorbars, bool normdata=false)
Output into a simple ascii file, easily readable by gnuplot.
unsigned int visibleEntries() const
get the number of visible entries (all entries without those in the under- and overflow bins) in the ...
bool _havedata
Set to true if there is experimental data available.
Definition: Histogram.h:338
void prefactor(double in)
Set the prefactor.
Definition: Histogram.h:144
virtual IBPtr clone() const
Make a simple clone of this object.
double dataNorm() const
Compute the normalisation of the data.
double _total
Total entry.
Definition: Histogram.h:393
void normaliseToCrossSection()
Normalise the distributions to the total cross section.
vector< double > dumpBins() const
Dump bin data into a vector.
double _prefactor
Prefactors to multiply the output by.
Definition: Histogram.h:388
Histogram(vector< double > limits, vector< double > data, vector< double > dataerror)
Constructor with data included.
Definition: Histogram.h:85
void chiSquared(double &chisq, unsigned int &ndegrees, double minfrac=0.) const
Return the chi squared.
Histogram(vector< double > limits)
Constructor for variable width bins.
Definition: Histogram.h:72
void rivetOutput(ostream &out, string histogramname=string("default"), string analysisname=string("default"), string title=string(), string xlabel=string(), string ylabel=string(), bool rawcount=false, double multiplicator=1.0) const
Output as file ready for usage with flat2aida and other Rivet tools.
double prefactor() const
Get the prefactor.
Definition: Histogram.h:137
virtual IBPtr fullclone() const
Make a clone of this object, possibly modifying the cloned object to make it sane.
static vector< double > LogBins(double xmin, unsigned nbins, double base=10.0)
Returns limits for bins with exponentially increasing widths.
Histogram ratioWith(const Histogram &h2) const
Returns a new histogram containing bin-by-bin ratios of two histograms.
Statistic _globalStats
Global statistics of all data that went into the histogram.
Definition: Histogram.h:333
static void Init()
The standard Init function used to initialize the interfaces.
void topdrawOutput(ostream &out, unsigned int flags=0, string colour=string("BLACK"), string title=string(), string titlecase=string(), string left=string(), string leftcase=string(), string bottom=string(), string bottomcase=string()) const
Output as a topdrawer file.
vector< Bin > _bins
The histogram bins.
Definition: Histogram.h:383
void operator+=(double input)
Operator to add a point to the histogrma with unit weight.
Definition: Histogram.h:106
void normaliseToData()
Normalise the distributions to the data.
void topdrawOutputAverage(ostream &out, bool frame, bool errorbars, bool xlog, bool ylog, string colour=string("BLACK"), string title=string(), string titlecase=string(), string left=string(), string leftcase=string(), string bottom=string(), string bottomcase=string()) const
Output as a topdrawer file.
Histogram(double lower=0., double upper=0., unsigned int nbin=0)
The default constructor.
Definition: Histogram.h:55
void addWeighted(double input, double weight)
Function to add a weighted point to the histogram.
Definition: Histogram.h:113
The Statistic class is a simple class designed to store a variable for statistical analysis.
Definition: Statistic.h:23
const unsigned int Xlog
log scale for x-axis
Definition: Histogram.h:31
const unsigned int Smooth
smooth the line
Definition: Histogram.h:33
const unsigned int Errorbars
Plot error bars.
Definition: Histogram.h:30
const unsigned int Rawcount
don't normalize to unit area
Definition: Histogram.h:34
const unsigned int Frame
Plot on new frame.
Definition: Histogram.h:29
const unsigned int None
No options.
Definition: Histogram.h:28
const unsigned int Ylog
log scale for y-axis
Definition: Histogram.h:32
-*- C++ -*-
ThePEG::Ptr< InterfacedBase >::pointer IBPtr
constexpr auto sqr(const T &x) -> decltype(x *x)
ostream & left(ostream &os)
One bin of the histogram.
Definition: Histogram.h:343
double limit
The limit for the bin.
Definition: Histogram.h:362
double contentsSq
Contents squared for the error.
Definition: Histogram.h:357
Bin()
Default constructor.
Definition: Histogram.h:347
long points
The number of points in the bin.
Definition: Histogram.h:377
double data
The experimental value for the bin.
Definition: Histogram.h:367
double dataerror
The error on the experimental value for the bin.
Definition: Histogram.h:372
double contents
Contents of the bin.
Definition: Histogram.h:352