<chapter name="Histograms"> 
     
<h2>Histograms</h2> 
 
The <code>Hist</code> class gives a simple implementation of 
one-dimensional histograms, useful for quick-and-dirty testing, 
without the need to link to more sophisticated packages. 
For this reason it is used in many of the 
<aloc href="SampleMainPrograms">sample main programs</aloc> 
found in the <code>examples</code> subdirectory. 
 
<h3>Basic principles</h3> 
 
We here provide a simple overview of what is involved. 
As a first step you need to declare a histogram, with name, 
title, number of bins and <ei>x</ei> range (from, to). 
<pre> 
   Hist ZpT( "Z0 pT spectrum", 100, 0., 100.); 
</pre> 
Alternatively you can first declare it and later define it: 
<pre> 
   Hist ZpT; 
   ZpT.book( "Z0 pT spectrum", 100, 0., 100.); 
</pre> 
 
Once declared, its contents can be added by repeated calls to 
<code>fill</code>, 
<pre> 
   ZpT.fill( 22.7, 1.); 
</pre> 
where the first argument is the <ei>x</ei> value and the second the 
weight. Since the weight defaults to 1 the last argument could have 
been omitted in this case. 
 
<p/> 
A set of overloaded operators have been defined, so that histograms 
can be added, subtracted, divided or multiplied by each other. Then the 
contents are modified accordingly bin by bin. Thus the relative 
deviation between two histograms <code>data</code> and 
<code>theory</code> can be found as 
<pre> 
  diff = (data - theory) / (data + theory); 
</pre> 
assuming that <code>diff</code>, <code>data</code> and <code>theory</code> 
have been booked with the same number of bins and <ei>x</ei> range. That 
responsibility rests on the user; some checks are made for compatibility, 
but not enough to catch all possible mistakes. 
 
<p/> 
Also overloaded operations with double real numbers are available. 
Again these four operations are defined bin by bin, i.e. the 
corresponding amount is added to, subtracted from, multiplied by or 
divided by each bin. The double number can come before or after the 
histograms, with obvious results. Thus the inverse of a histogram 
<code>result</code> is given by <code>1. / result</code>. 
The two kind of operations can be combined, e.g. 
<pre> 
  allpT = ZpT + 2. * WpT 
</pre> 
Finally, also the <code>+=, -+, *=, /=</code> are overloaded, with 
the right-hand side being either a histogram or a real number. 
 
<h3>Output format</h3> 
 
<p/> 
A histogram can be printed by making use of the overloaded &lt;&lt; 
operator, e.g.: 
<pre> 
   cout &lt;&lt; ZpT; 
</pre> 
The printout format is inspired by the old HBOOK one. To understand 
how to read this format, consider the simplified example 
<pre> 
                                     
        3.50*10^ 2  9 
        3.00*10^ 2  X   7 
        2.50*10^ 2  X  1X 
        2.00*10^ 2  X6 XX 
        1.50*10^ 2  XX5XX 
        1.00*10^ 2  XXXXX 
        0.50*10^ 2  XXXXX 
 
          Contents 
            *10^ 2  31122 
            *10^ 1  47208 
            *10^ 0  79373 
 
          Low edge  -- 
            *10^ 1  10001 
            *10^ 0  05050 
</pre> 
The key feature is that the <code>Contents</code> and 
<code>Low edge</code> have to be read vertically. For instance, 
the first bin has the contents 
<code>3 * 10^2 + 4 * 10^1 + 7 * 10^0 = 347</code>. Correspondingly, 
the other bins have contents 179, 123, 207 and 283. The first bin 
stretches from <code>-(1 * 10^1 + 0 * 10^0) = -10</code> to the 
beginning of the second bin, at <code>-(0 * 10^1 + 5 * 10^0) = -5</code>. 
 
<p/> 
The visual representation above the contents give a simple impression 
of the shape. An <code>X</code> means that the contents are filled up 
to this level, a digit in the topmost row the fraction to which the 
last level is filled. So the 9 of the first column indicates this bin 
is filled 9/10 of the way from <code>3.00*10^2 = 300</code> to 
<code>3.50*10^2 = 350</code>, i.e. somewhere close to 345, 
or more precisely in the range 342.5 to 347.5. 
 
<p/> 
The printout also provides some other information, such as the 
number of entries, i.e. how many times the histogram has been filled, 
the total weight inside the histogram, the total weight in underflow 
and overflow, and the mean value and root-mean-square width (disregarding 
underflow and overflow). The mean and width assumes that all the 
contents is in the middle of the respective bin. This is especially 
relevant when you plot a integer quantity, such as a multiplicity. 
Then it makes sense to book with limits that are half-integers, e.g. 
<pre> 
   Hist multMPI( "number of multiparton interactions", 20, -0.5, 19.5); 
</pre> 
so that the bins are centered at 0, 1, 2, ..., respectively.  This also 
avoids ambiguities which bin gets to be filled if entries are 
exactly at the border between two bins. Also note that the 
<code>fill( xValue)</code> method automatically performs a cast 
to double precision where necessary, i.e. <code>xValue</code> 
can be an integer. 
 
<h3>The methods</h3> 
 
We here collect a more complete and formal overview of the methods. 
    
<method name="Hist::Hist()"> 
declare a histogram, but does not define it. 
</method> 
 
<method name="Hist::Hist(string title, int numberOfBins, double xMin, 
double xMax)"> 
declare and define a histogram, where 
<argument name="title"> 
is a string with the title of the histogram at output, 
</argument> 
<argument name="numberOfBins"> 
is the number of bin the <ei>x</ei> range will be subdivided into, 
limited to be at most 1000, 
</argument> 
<argument name="xMin"> 
is the lower edge of the histogram, 
</argument> 
<argument name="xMax"> 
is the upper edge of the histogram. 
</argument> 
</method> 
    
<method name="Hist::Hist(const Hist& h)"> 
creates an identical copy of the histogram in the argument, 
including bin contents. 
</method> 
    
<method name="Hist::Hist(string title, const Hist& h)"> 
creates an identical copy of the histogram in the argument, 
including bin contents, except that a new title is provided 
as first argument. 
</method> 
    
<method name="Hist& Hist::operator=(const Hist& h)"> 
copies all properties of the histogram in the argument, 
except that the original histogram title is retained. 
</method> 
 
<method name="void Hist::book(string title, int numberOfBins, 
double xMin, double xMax)"> 
define a histogram that previously was only declared; 
see above for the meaning of the arguments. 
</method> 
 
<method name="void Hist::name(string title)"> 
change the title of a histogram, but keep other properties unchanged. 
</method> 
 
<method name="void Hist::null()"> 
reset bin contents, but keep other histogram properties unchanged. 
</method> 
 
<method name="void Hist::fill(double xValue, double weight)"> 
fill the histogram, where 
<argument name="xValue"> 
is the <ei>x</ei> position where the filling should occur, and 
</argument> 
<argument name="weight" default="1."> 
is the amount of weight to be added at this <ei>x</ei> value. 
</argument> 
</method> 
 
<method name="friend ostream& operator<<(ostream& os, const Hist& h)"> 
appends a simple histogram printout (see above for format) to the 
<code>ostream</code>, while leaving the histogram object itself 
unchanged. At most 100 columns are allowed to be displayed. 
If the number of bins is larger than 100 then the contents of 
adjacent bins are added to give the value in each column. (Two by two 
up to 200 bins, three by three up to 300, and so on, with the very 
last column possibly summing fewer rows than the others.) 
</method> 
 
<method name="void Hist::table(ostream& os = cout, 
bool printOverUnder = false, bool xMidBin = true)"> 
</method> 
<methodmore name="void Hist::table(string fileName, 
bool printOverUnder = false, bool xMidBin = true)"> 
print a two-column table, where the first column gives the center of 
each bin and the second one the corresponding bin contents. The table 
may be useful for plotting e.g. with Gnuplot. 
<br/>The desired output stream or file name can be provided as argument. 
The former is more flexible (e.g., it allows easy append to an existing 
file), whereas the latter is simpler for the case that each histogram 
should be a file of its own. 
<br/>An optional <code>printOverUnder = true</code> argument allows also 
underflow and overflow contents to be printed. (The arbitrary <ei>x</ei> 
coordinates for these are placed as if corresponding to same-size bins 
just below or above the regular histogram bins.) 
<br/>An optional <code>xMidBin = false</code> argument will have the
<ei>x</ei> value at the beginning of each bin printed, rather than the
default midpoint value.
</methodmore> 
 
<method name="friend void table(const Hist& h1, const Hist& h2, 
ostream& os = cout, bool printOverUnder = false, bool xMidBin = true)"> 
</method> 
<methodmore name="friend void table(const Hist& h1, const Hist& h2, 
string fileName, bool printOverUnder = false, bool xMidBin = true)"> 
print a three-column table, where the first column gives the center of 
each bin and the second and third ones the corresponding bin contents 
of the two histograms. Only works if the two histograms have the same 
<ei>x</ei> axis (within a tiny tolerance), else nothing will be done. 
The optional last two arguments allows also underflow and overflow 
contents to be printed, and the <ei>x</ei> to refer to the beginning
of the bin rather than the center; see above. 
</methodmore> 
 
<method name="double Hist::getBinContent(int iBin)"> 
return the value in bin <code>iBin</code>, ranging from 1 through 
<code>numberOfBins</code>, with <code>0</code> for underflow and 
<code>numberOfBins + 1</code> for overflow. 
</method> 
 
<method name="int Hist::getEntries()"> 
return the number of entries, i.e. the number of time that 
<code>fill(...)</code> has been called. 
</method> 
 
<method name="bool Hist::sameSize(const Hist& h)"> 
checks that the number of bins and upper and lower limits are the 
same as in the histogram in the argument. 
</method> 
 
<method name="void Hist::takeLog(bool tenLog = true)"> 
by default take 10-logarithm of current contents bin by bin. With 
optional argument <code>false</code> instead take <ei>e</ei>-logarithm 
of contents bin by bin. If to be used, then right before the 
histogram is output. 
</method> 
 
<method name="void Hist::takeSqrt()"> 
take square root of current contents bin by bin, with negative contents 
set to zero. 
</method> 
 
<method name="Hist& Hist::operator+=(const Hist& h)"> 
</method> 
<methodmore name="Hist& Hist::operator-=(const Hist& h)"> 
adds or subtracts the current histogram by the contents of the 
histogram in the argument if <code>sameSize(...)</code> is true, 
else does nothing. 
</methodmore> 
 
<method name="Hist& Hist::operator*=(const Hist& h)"> 
</method> 
<methodmore name="Hist& Hist::operator/=(const Hist& h)"> 
multiplies or divides the current histogram by the contents of the 
histogram in the argument if <code>sameSize(...)</code> is true, 
else does nothing. 
</methodmore> 
 
<method name="Hist& Hist::operator+=(double f)"> 
</method> 
<methodmore name="Hist& Hist::operator-=(double f)"> 
adds or subtracts each bin content by the common offset <ei>f</ei>. 
</methodmore> 
 
<method name="Hist& Hist::operator*=(double f)"> 
</method> 
<methodmore name="Hist& Hist::operator*=(double f)"> 
multiplies or divides each bin content by the common factor <ei>f</ei>. 
</methodmore> 
 
<method name="friend Hist operator+(double f, const Hist& h1)"> 
</method> 
<methodmore name="friend Hist operator+(const Hist& h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator+(const Hist& h1, const Hist h2)"> 
add a constant to a histogram or two histograms to each other, bin by bin. 
</methodmore> 
 
<method name="friend Hist operator-(double f, const Hist& h1)"> 
</method> 
<methodmore name="friend Hist operator-(const Hist& h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator-(const Hist& h1, const Hist h2)"> 
subtract a histogram from a constant, a constant from a histogram, 
or two histograms from each other, bin by bin. 
</methodmore> 
 
<method name="friend Hist operator*(double f, const Hist& h1)"> 
</method> 
<methodmore name="friend Hist operator*(const Hist& h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator*(const Hist& h1, const Hist h2)"> 
multiply a constant by a histogram or two histograms by each other, 
bin by bin. 
</methodmore> 
 
<method name="friend Hist operator/(double f, const Hist& h1)"> 
</method> 
<methodmore name="friend Hist operator/(const Hist& h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator/(const Hist& h1, const Hist h2)"> 
divide a constant by a histogram, a histogram by a constant, 
or two histograms by each other, bin by bin. 
</methodmore> 
 
</chapter> 
 
<!-- Copyright (C) 2014 Torbjorn Sjostrand --> 
