3 <title>Histograms</title>
4 <link rel="stylesheet" type="text/css" href="pythia.css"/>
5 <link rel="shortcut icon" href="pythia32.gif"/>
11 The <code>Hist</code> class gives a simple implementation of
12 one-dimensional histograms, useful for quick-and-dirty testing,
13 without the need to link to more sophisticated packages.
14 For this reson it is used in many of the
15 <a href="SampleMainPrograms.html" target="page">sample main programs</a>
16 found in the <code>examples</code> subdirectory.
18 <h3>Basic principles</h3>
20 We here provide a simple overview of what is involved.
21 As a first step you need to declare a histogram, with name,
22 title, number of bins and <i>x</i> range (from, to).
24 Hist ZpT( "Z0 pT spectrum", 100, 0., 100.);
26 Alternatively you can first declare it and later define it:
29 ZpT.book( "Z0 pT spectrum", 100, 0., 100.);
32 Once declared, its contents can be added by repeated calls to
37 where the first argument is the <i>x</i> value and the second the
38 weight. Since the weight defaults to 1 the last argument could have
39 been omitted in this case.
42 A set of overloaded operators have been defined, so that histograms
43 can be added, subtracted, divided or multiplied by each other. Then the
44 contents are modified accordingly bin by bin. Thus the relative
45 deviation between two histograms <code>data</code> and
46 <code>theory</code> can be found as
48 diff = (data - theory) / (data + theory);
50 assuming that <code>diff</code>, <code>data</code> and <code>theory</code>
51 have been booked with the same number of bins and <i>x</i> range. That
52 responsibility rests on the user; some checks are made for compatibility,
53 but not enough to catch all possible mistakes.
56 Also overloaded operations with double real numbers are available.
57 Again these four operations are defined bin by bin, i.e. the
58 corresponding amount is added to, subtracted from, multiplied by or
59 divided by each bin. The double number can come before or after the
60 histograms, with obvious results. Thus the inverse of a histogram
61 <code>result</code> is given by <code>1. / result</code>.
62 The two kind of operations can be combined, e.g.
64 allpT = ZpT + 2. * WpT
66 Finally, also the <code>+=, -+, *=, /=</code> are overloaded, with
67 the right-hand side being either a histogram or a real number.
69 <h3>Output format</h3>
72 A histogram can be printed by making use of the overloaded <<
77 The printout format is inspired by the old HBOOK one. To understand
78 how to read this format, consider the simplified example
98 The key feature is that the <code>Contents</code> and
99 <code>Low edge</code> have to be read vertically. For instance,
100 the first bin has the contents
101 <code>3 * 10^2 + 4 * 10^1 + 7 * 10^0 = 347</code>. Correspondingly,
102 the other bins have contents 179, 123, 207 and 283. The first bin
103 stretches from <code>-(1 * 10^1 + 0 * 10^0) = -10</code> to the
104 beginning of the second bin, at <code>-(0 * 10^1 + 5 * 10^0) = -5</code>.
107 The visual representation above the contents give a simple impression
108 of the shape. An <code>X</code> means that the contents are filled up
109 to this level, a digit in the topmost row the fraction to which the
110 last level is filled. So the 9 of the first column indicates this bin
111 is filled 9/10 of the way from <code>3.00*10^2 = 300</code> to
112 <code>3.50*10^2 = 350</code>, i.e. somewhere close to 345,
113 or more precisely in the range 342.5 to 347.5.
116 The printout also provides some other information, such as the
117 number of entries, i.e. how many times the histogram has been filled,
118 the total weight inside the histogram, the total weight in underflow
119 and overflow, and the mean value and root-mean-square width (disregarding
120 underflow and overflow). The mean and width assumes that all the
121 contents is in the middle of the respective bin. This is especially
122 relevant when you plot a integer quantity, such as a multiplicity.
123 Then it makes sense to book with limits that are half-integers, e.g.
125 Hist multMI( "number of multiple interactions", 20, -0.5, 19.5);
127 so that the bins are centered at 0, 1, 2, ..., respectively. This also
128 avoids ambiguities which bin gets to be filled if entries are
129 exactly at the border between two bins. Also note that the
130 <code>fill( xValue)</code> method automatically performs a cast
131 to double precision where necessary, i.e. <code>xValue</code>
136 We here collect a more complete and formal overview of the methods.
138 <a name="method1"></a>
139 <p/><strong>Hist::Hist() </strong> <br/>
140 declare a histogram, but does not define it.
143 <a name="method2"></a>
144 <p/><strong>Hist::Hist(string title, int numberOfBins, double xMin, double xMax) </strong> <br/>
145 declare and define a histogram, where
146 <br/><code>argument</code><strong> title </strong> :
147 is a string with the title of the histogram at output,
149 <br/><code>argument</code><strong> numberOfBins </strong> :
150 is the number of bin the <i>x</i> range will be subdivided into,
151 limited to be at most 1000,
153 <br/><code>argument</code><strong> xMin </strong> :
154 is the lower edge of the histogram,
156 <br/><code>argument</code><strong> xMax </strong> :
157 is the upper edge of the histogram.
161 <a name="method3"></a>
162 <p/><strong>Hist::Hist(const Hist& h) </strong> <br/>
163 creates an identical copy of the histogram in the argument,
164 including bin contents.
167 <a name="method4"></a>
168 <p/><strong>Hist::Hist(string title, const Hist& h) </strong> <br/>
169 creates an identical copy of the histogram in the argument,
170 including bin contents, except that a new title is provided
174 <a name="method5"></a>
175 <p/><strong>Hist& Hist::operator=(const Hist& h) </strong> <br/>
176 copies all properties of the histogram in the argument,
177 except that the original histogram title is retained.
180 <a name="method6"></a>
181 <p/><strong>void Hist::book(string title, int numberOfBins, double xMin, double xMax) </strong> <br/>
182 define a histogram that previously was only declared;
183 see above for the meaning of the arguments.
186 <a name="method7"></a>
187 <p/><strong>void Hist::name(string title) </strong> <br/>
188 change the title of a histogram, but keep other properties unchanged.
191 <a name="method8"></a>
192 <p/><strong>void Hist::null() </strong> <br/>
193 reset bin contents, but keep other histogram properties unchanged.
196 <a name="method9"></a>
197 <p/><strong>void Hist::fill(double xValue, double weight) </strong> <br/>
198 fill the histogram, where
199 <br/><code>argument</code><strong> xValue </strong> :
200 is the <i>x</i> position where the filling should occur, and
202 <br/><code>argument</code><strong> weight </strong> (<code>default = <strong>1.</strong></code>) :
203 is the amount of weight to be added at this <i>x</i> value.
207 <a name="method10"></a>
208 <p/><strong>friend ostream& operator<<(ostream& os, const Hist& h) </strong> <br/>
209 appends a simple histogram printout (see above for format) to the
210 <code>ostream</code>, while leaving the histogram object itself
211 unchanged. At most 100 columns are allowed to be displayed.
212 If the number of bins is larger than 100 then the contents of
213 adjacent bins are added to give the value in each column. (Two by two
214 up to 200 bins, three by three up to 300, and so on, with the very
215 last column possibly summing fewer rows than the others.)
218 <a name="method11"></a>
219 <p/><strong>void Hist::table(ostream& os = cout) </strong> <br/>
221 <strong>void Hist::table(string fileName) </strong> <br/>
222 print a two-column table, where the first column gives the center of
223 each bin and the second one the corresponding bin contents. The desired
224 output stream or file name can be provided as argument. The former
225 is more flexible (e.g., it allows easy append to an existing file),
226 whereas the latter is simpler for the case that each histogram should
227 be a file of its own. The table may be useful for plotting e.g. with
231 <a name="method12"></a>
232 <p/><strong>friend void table(const Hist& h1, const Hist& h2, ostream& os = cout) </strong> <br/>
234 <strong>friend void table(const Hist& h1, const Hist& h2, string fileName) </strong> <br/>
235 print a three-column table, where the first column gives the center of
236 each bin and the second and third ones the corresponding bin contents
237 of the two histograms. Only works if the two histograms have the same
238 x axis (within a tiny tolarance), else nothing will be done.
241 <a name="method13"></a>
242 <p/><strong>double Hist::getBinContent(int iBin) </strong> <br/>
243 return the value in bin <code>iBin</code>, ranging from 1 through
244 <code>numberOfBins</code>, with <code>0</code> for underflow and
245 <code>numberOfBins + 1</code> for overflow.
248 <a name="method14"></a>
249 <p/><strong>int Hist::getEntries() </strong> <br/>
250 return the number of entries, i.e. the number of time that
251 <code>fill(...)</code> has been called.
254 <a name="method15"></a>
255 <p/><strong>bool Hist::sameSize(const Hist& h) </strong> <br/>
256 checks that the number of bins and upper and lower limits are the
257 same as in the histogram in the argument.
260 <a name="method16"></a>
261 <p/><strong>void Hist::takeLog(bool tenLog = true) </strong> <br/>
262 by default take 10-logarithm of current contents bin by bin. With
263 optional argument <code>false</code> instead take <i>e</i>-logarithm
264 of contents bin by bin. If to be used, then right before the
268 <a name="method17"></a>
269 <p/><strong>void Hist::takeSqrt() </strong> <br/>
270 take square root of current contents bin by bin, with negative contents
274 <a name="method18"></a>
275 <p/><strong>Hist& Hist::operator+=(const Hist& h) </strong> <br/>
277 <strong>Hist& Hist::operator-=(const Hist& h) </strong> <br/>
278 adds or subtracts the current histogram by the contents of the
279 histogram in the argument if <code>sameSize(...)</code> is true,
283 <a name="method19"></a>
284 <p/><strong>Hist& Hist::operator*=(const Hist& h) </strong> <br/>
286 <strong>Hist& Hist::operator/=(const Hist& h) </strong> <br/>
287 multiplies or divides the current histogram by the contents of the
288 histogram in the argument if <code>sameSize(...)</code> is true,
292 <a name="method20"></a>
293 <p/><strong>Hist& Hist::operator+=(double f) </strong> <br/>
295 <strong>Hist& Hist::operator-=(double f) </strong> <br/>
296 adds or subtracts each bin content by the common offset <i>f</i>.
299 <a name="method21"></a>
300 <p/><strong>Hist& Hist::operator*=(double f) </strong> <br/>
302 <strong>Hist& Hist::operator*=(double f) </strong> <br/>
303 multiplies or divides each bin content by the common factor <i>f</i>.
306 <a name="method22"></a>
307 <p/><strong>friend Hist operator+(double f, const Hist& h1) </strong> <br/>
309 <strong>friend Hist operator+(const Hist& h1, double f) </strong> <br/>
311 <strong>friend Hist operator+(const Hist& h1, const Hist h2) </strong> <br/>
312 add a constant to a histogram or two histograms to each other, bin by bin.
315 <a name="method23"></a>
316 <p/><strong>friend Hist operator-(double f, const Hist& h1) </strong> <br/>
318 <strong>friend Hist operator-(const Hist& h1, double f) </strong> <br/>
320 <strong>friend Hist operator-(const Hist& h1, const Hist h2) </strong> <br/>
321 subtract a histogram from a constant, a constant from a histogram,
322 or two histograms from each other, bin by bin.
325 <a name="method24"></a>
326 <p/><strong>friend Hist operator*(double f, const Hist& h1) </strong> <br/>
328 <strong>friend Hist operator*(const Hist& h1, double f) </strong> <br/>
330 <strong>friend Hist operator*(const Hist& h1, const Hist h2) </strong> <br/>
331 multiply a constant by a histogram or two histograms by each other,
335 <a name="method25"></a>
336 <p/><strong>friend Hist operator/(double f, const Hist& h1) </strong> <br/>
338 <strong>friend Hist operator/(const Hist& h1, double f) </strong> <br/>
340 <strong>friend Hist operator/(const Hist& h1, const Hist h2) </strong> <br/>
341 divide a constant by a histogram, a histogram by a constant,
342 or two histograms by each other, bin by bin.
348 <!-- Copyright (C) 2010 Torbjorn Sjostrand -->