3 <title>Histograms</title>
4 <link rel="stylesheet" type="text/css" href="pythia.css"/>
5 <link rel="shortcut icon" href="pythia32.gif"/>
9 <script language=javascript type=text/javascript>
10 function stopRKey(evt) {
11 var evt = (evt) ? evt : ((event) ? event : null);
12 var node = (evt.target) ? evt.target :((evt.srcElement) ? evt.srcElement : null);
13 if ((evt.keyCode == 13) && (node.type=="text"))
17 document.onkeypress = stopRKey;
20 if($_POST['saved'] == 1) {
21 if($_POST['filepath'] != "files/") {
22 echo "<font color='red'>SETTINGS SAVED TO FILE</font><br/><br/>"; }
24 echo "<font color='red'>NO FILE SELECTED YET.. PLEASE DO SO </font><a href='SaveSettings.php'>HERE</a><br/><br/>"; }
28 <form method='post' action='Histograms.php'>
32 The <code>Hist</code> class gives a simple implementation of
33 one-dimensional histograms, useful for quick-and-dirty testing,
34 without the need to link to more sophisticated packages.
35 For this reason it is used in many of the
36 <?php $filepath = $_GET["filepath"];
37 echo "<a href='SampleMainPrograms.php?filepath=".$filepath."' target='page'>";?>sample main programs</a>
38 found in the <code>examples</code> subdirectory.
40 <h3>Basic principles</h3>
42 We here provide a simple overview of what is involved.
43 As a first step you need to declare a histogram, with name,
44 title, number of bins and <i>x</i> range (from, to).
46 Hist ZpT( "Z0 pT spectrum", 100, 0., 100.);
48 Alternatively you can first declare it and later define it:
51 ZpT.book( "Z0 pT spectrum", 100, 0., 100.);
54 Once declared, its contents can be added by repeated calls to
59 where the first argument is the <i>x</i> value and the second the
60 weight. Since the weight defaults to 1 the last argument could have
61 been omitted in this case.
64 A set of overloaded operators have been defined, so that histograms
65 can be added, subtracted, divided or multiplied by each other. Then the
66 contents are modified accordingly bin by bin. Thus the relative
67 deviation between two histograms <code>data</code> and
68 <code>theory</code> can be found as
70 diff = (data - theory) / (data + theory);
72 assuming that <code>diff</code>, <code>data</code> and <code>theory</code>
73 have been booked with the same number of bins and <i>x</i> range. That
74 responsibility rests on the user; some checks are made for compatibility,
75 but not enough to catch all possible mistakes.
78 Also overloaded operations with double real numbers are available.
79 Again these four operations are defined bin by bin, i.e. the
80 corresponding amount is added to, subtracted from, multiplied by or
81 divided by each bin. The double number can come before or after the
82 histograms, with obvious results. Thus the inverse of a histogram
83 <code>result</code> is given by <code>1. / result</code>.
84 The two kind of operations can be combined, e.g.
86 allpT = ZpT + 2. * WpT
88 Finally, also the <code>+=, -+, *=, /=</code> are overloaded, with
89 the right-hand side being either a histogram or a real number.
91 <h3>Output format</h3>
94 A histogram can be printed by making use of the overloaded <<
99 The printout format is inspired by the old HBOOK one. To understand
100 how to read this format, consider the simplified example
120 The key feature is that the <code>Contents</code> and
121 <code>Low edge</code> have to be read vertically. For instance,
122 the first bin has the contents
123 <code>3 * 10^2 + 4 * 10^1 + 7 * 10^0 = 347</code>. Correspondingly,
124 the other bins have contents 179, 123, 207 and 283. The first bin
125 stretches from <code>-(1 * 10^1 + 0 * 10^0) = -10</code> to the
126 beginning of the second bin, at <code>-(0 * 10^1 + 5 * 10^0) = -5</code>.
129 The visual representation above the contents give a simple impression
130 of the shape. An <code>X</code> means that the contents are filled up
131 to this level, a digit in the topmost row the fraction to which the
132 last level is filled. So the 9 of the first column indicates this bin
133 is filled 9/10 of the way from <code>3.00*10^2 = 300</code> to
134 <code>3.50*10^2 = 350</code>, i.e. somewhere close to 345,
135 or more precisely in the range 342.5 to 347.5.
138 The printout also provides some other information, such as the
139 number of entries, i.e. how many times the histogram has been filled,
140 the total weight inside the histogram, the total weight in underflow
141 and overflow, and the mean value and root-mean-square width (disregarding
142 underflow and overflow). The mean and width assumes that all the
143 contents is in the middle of the respective bin. This is especially
144 relevant when you plot a integer quantity, such as a multiplicity.
145 Then it makes sense to book with limits that are half-integers, e.g.
147 Hist multMPI( "number of multiparton interactions", 20, -0.5, 19.5);
149 so that the bins are centered at 0, 1, 2, ..., respectively. This also
150 avoids ambiguities which bin gets to be filled if entries are
151 exactly at the border between two bins. Also note that the
152 <code>fill( xValue)</code> method automatically performs a cast
153 to double precision where necessary, i.e. <code>xValue</code>
158 We here collect a more complete and formal overview of the methods.
160 <a name="method1"></a>
161 <p/><strong>Hist::Hist() </strong> <br/>
162 declare a histogram, but does not define it.
165 <a name="method2"></a>
166 <p/><strong>Hist::Hist(string title, int numberOfBins, double xMin, double xMax) </strong> <br/>
167 declare and define a histogram, where
168 <br/><code>argument</code><strong> title </strong> :
169 is a string with the title of the histogram at output,
171 <br/><code>argument</code><strong> numberOfBins </strong> :
172 is the number of bin the <i>x</i> range will be subdivided into,
173 limited to be at most 1000,
175 <br/><code>argument</code><strong> xMin </strong> :
176 is the lower edge of the histogram,
178 <br/><code>argument</code><strong> xMax </strong> :
179 is the upper edge of the histogram.
183 <a name="method3"></a>
184 <p/><strong>Hist::Hist(const Hist& h) </strong> <br/>
185 creates an identical copy of the histogram in the argument,
186 including bin contents.
189 <a name="method4"></a>
190 <p/><strong>Hist::Hist(string title, const Hist& h) </strong> <br/>
191 creates an identical copy of the histogram in the argument,
192 including bin contents, except that a new title is provided
196 <a name="method5"></a>
197 <p/><strong>Hist& Hist::operator=(const Hist& h) </strong> <br/>
198 copies all properties of the histogram in the argument,
199 except that the original histogram title is retained.
202 <a name="method6"></a>
203 <p/><strong>void Hist::book(string title, int numberOfBins, double xMin, double xMax) </strong> <br/>
204 define a histogram that previously was only declared;
205 see above for the meaning of the arguments.
208 <a name="method7"></a>
209 <p/><strong>void Hist::name(string title) </strong> <br/>
210 change the title of a histogram, but keep other properties unchanged.
213 <a name="method8"></a>
214 <p/><strong>void Hist::null() </strong> <br/>
215 reset bin contents, but keep other histogram properties unchanged.
218 <a name="method9"></a>
219 <p/><strong>void Hist::fill(double xValue, double weight) </strong> <br/>
220 fill the histogram, where
221 <br/><code>argument</code><strong> xValue </strong> :
222 is the <i>x</i> position where the filling should occur, and
224 <br/><code>argument</code><strong> weight </strong> (<code>default = <strong>1.</strong></code>) :
225 is the amount of weight to be added at this <i>x</i> value.
229 <a name="method10"></a>
230 <p/><strong>friend ostream& operator<<(ostream& os, const Hist& h) </strong> <br/>
231 appends a simple histogram printout (see above for format) to the
232 <code>ostream</code>, while leaving the histogram object itself
233 unchanged. At most 100 columns are allowed to be displayed.
234 If the number of bins is larger than 100 then the contents of
235 adjacent bins are added to give the value in each column. (Two by two
236 up to 200 bins, three by three up to 300, and so on, with the very
237 last column possibly summing fewer rows than the others.)
240 <a name="method11"></a>
241 <p/><strong>void Hist::table(ostream& os = cout) </strong> <br/>
243 <strong>void Hist::table(string fileName) </strong> <br/>
244 print a two-column table, where the first column gives the center of
245 each bin and the second one the corresponding bin contents. The desired
246 output stream or file name can be provided as argument. The former
247 is more flexible (e.g., it allows easy append to an existing file),
248 whereas the latter is simpler for the case that each histogram should
249 be a file of its own. The table may be useful for plotting e.g. with
253 <a name="method12"></a>
254 <p/><strong>friend void table(const Hist& h1, const Hist& h2, ostream& os = cout) </strong> <br/>
256 <strong>friend void table(const Hist& h1, const Hist& h2, string fileName) </strong> <br/>
257 print a three-column table, where the first column gives the center of
258 each bin and the second and third ones the corresponding bin contents
259 of the two histograms. Only works if the two histograms have the same
260 x axis (within a tiny tolerance), else nothing will be done.
263 <a name="method13"></a>
264 <p/><strong>double Hist::getBinContent(int iBin) </strong> <br/>
265 return the value in bin <code>iBin</code>, ranging from 1 through
266 <code>numberOfBins</code>, with <code>0</code> for underflow and
267 <code>numberOfBins + 1</code> for overflow.
270 <a name="method14"></a>
271 <p/><strong>int Hist::getEntries() </strong> <br/>
272 return the number of entries, i.e. the number of time that
273 <code>fill(...)</code> has been called.
276 <a name="method15"></a>
277 <p/><strong>bool Hist::sameSize(const Hist& h) </strong> <br/>
278 checks that the number of bins and upper and lower limits are the
279 same as in the histogram in the argument.
282 <a name="method16"></a>
283 <p/><strong>void Hist::takeLog(bool tenLog = true) </strong> <br/>
284 by default take 10-logarithm of current contents bin by bin. With
285 optional argument <code>false</code> instead take <i>e</i>-logarithm
286 of contents bin by bin. If to be used, then right before the
290 <a name="method17"></a>
291 <p/><strong>void Hist::takeSqrt() </strong> <br/>
292 take square root of current contents bin by bin, with negative contents
296 <a name="method18"></a>
297 <p/><strong>Hist& Hist::operator+=(const Hist& h) </strong> <br/>
299 <strong>Hist& Hist::operator-=(const Hist& h) </strong> <br/>
300 adds or subtracts the current histogram by the contents of the
301 histogram in the argument if <code>sameSize(...)</code> is true,
305 <a name="method19"></a>
306 <p/><strong>Hist& Hist::operator*=(const Hist& h) </strong> <br/>
308 <strong>Hist& Hist::operator/=(const Hist& h) </strong> <br/>
309 multiplies or divides the current histogram by the contents of the
310 histogram in the argument if <code>sameSize(...)</code> is true,
314 <a name="method20"></a>
315 <p/><strong>Hist& Hist::operator+=(double f) </strong> <br/>
317 <strong>Hist& Hist::operator-=(double f) </strong> <br/>
318 adds or subtracts each bin content by the common offset <i>f</i>.
321 <a name="method21"></a>
322 <p/><strong>Hist& Hist::operator*=(double f) </strong> <br/>
324 <strong>Hist& Hist::operator*=(double f) </strong> <br/>
325 multiplies or divides each bin content by the common factor <i>f</i>.
328 <a name="method22"></a>
329 <p/><strong>friend Hist operator+(double f, const Hist& h1) </strong> <br/>
331 <strong>friend Hist operator+(const Hist& h1, double f) </strong> <br/>
333 <strong>friend Hist operator+(const Hist& h1, const Hist h2) </strong> <br/>
334 add a constant to a histogram or two histograms to each other, bin by bin.
337 <a name="method23"></a>
338 <p/><strong>friend Hist operator-(double f, const Hist& h1) </strong> <br/>
340 <strong>friend Hist operator-(const Hist& h1, double f) </strong> <br/>
342 <strong>friend Hist operator-(const Hist& h1, const Hist h2) </strong> <br/>
343 subtract a histogram from a constant, a constant from a histogram,
344 or two histograms from each other, bin by bin.
347 <a name="method24"></a>
348 <p/><strong>friend Hist operator*(double f, const Hist& h1) </strong> <br/>
350 <strong>friend Hist operator*(const Hist& h1, double f) </strong> <br/>
352 <strong>friend Hist operator*(const Hist& h1, const Hist h2) </strong> <br/>
353 multiply a constant by a histogram or two histograms by each other,
357 <a name="method25"></a>
358 <p/><strong>friend Hist operator/(double f, const Hist& h1) </strong> <br/>
360 <strong>friend Hist operator/(const Hist& h1, double f) </strong> <br/>
362 <strong>friend Hist operator/(const Hist& h1, const Hist h2) </strong> <br/>
363 divide a constant by a histogram, a histogram by a constant,
364 or two histograms by each other, bin by bin.
370 <!-- Copyright (C) 2013 Torbjorn Sjostrand -->