<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. At the end 
of the section we show an example of how to link to a more 
sophisticated package. 
 
<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 (and associated statistical errors) 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>Basic output format</h3> 
 
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 
(including both underflow and overflow), the total weight inside the 
histogram (excluding underflow and overflow), the total weight in underflow 
and overflow, and the statistical power of the contents inside the 
histogram expressed in terms of 
<ei>nEffective = ( sum ( w ) )^2 / ( sum w^2 )</ei>. For an 
unweighted histogram, the latter is equal to the number of entries 
(excluding underflow and overflow), while for one filled with 
varying weights, it is less than that. 
 
<p/> 
Also printed are the mean value, median, and root-mean-square width 
(RMS). (Further moments about the mean can be computed via dedicated 
getter methods, see below.) If <code>doStats == true</code> then 
statistical errors are also printed, summed in quadrature with an 
estimate of the error due to the finite bin granularity (calculated as 
the difference with respect to unbinned estimates of the same quantities). 
 
<p/> 
In the printout, the moments and the median all disregard underflow 
and overflow and assume that all the contents is in the middle of the 
respective bin (with the median using a linear interpolation inside the median 
bin). This is especially relevant when you plot an 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>Matplotlib output format</h3> 
 
Assuming you have Python installed on your platform, it is possible to 
generate simple <a href="https://matplotlib.org/">Matplotlib/Pyplot</a> 
Python code from the histograms generated above, which then can be run 
to produce PDF plots. This should be done near the end of a run, after 
the histograms have been filled and properly normalized, as an alternative 
or complement to the basic output format above. 
 
<p/> 
In  a first step you must then decide on the name of the Python program, 
e.g.: 
<pre> 
  HistPlot hpl( "bosonpT"); 
</pre> 
where file ending <code>.py</code> is added automatically. 
 
<p/> 
For each new frame the name should be given, which will later give 
rise to a PDF file, with ending <code>.pdf</code> added automatically. 
If you leave the name field empty the same file will be used as for the 
latest named one, i.e. producing several frames in one file. One can 
optionally also give title and <ei>x</ei> and <ei>y</ei> axis labels: 
<pre> 
  hpl.frame( "pTdist", "Boson pT distributions", "pT (GeV)", "sigma"); 
</pre> 
Next, existing <code>Hist</code> histograms can be added to the frame, 
one by one: 
<pre> 
  hpl.add( ZpT, "-"); 
  hpl.add( WpT, "--,indigo"); 
  hpl.add( ZpT + 2. * WpT , "", "pT spectrum of Z, W+ and W-"); 
</pre> 
where the second argument tells how each histogram will be plotted. 
Default is histogram style, "h", but the values can also be connected 
with full lines "-", dashed ones "--", or dash-dotted ones "-.", or 
plotted as points "." or crosses "x", to mention some of the many 
options offered by Pyplot. As alternative to "h", the argument "e" can 
be specified which provides the same style as "h" but with error bars 
included. You can also specify the colour, separated by a comma from 
the line style, to override the normal colour cycle. The most common 
colours can be given just as a single letter, such as "r", "g", "b", 
but a more extensive 
<a href="https://matplotlib.org/stable/gallery/color/named_colors.html"> 
colour palette</a> allow finetuning to nuances such as "orange", 
"gold", "darkgreen", "royalblue", "crimson", and so on. 
A third argument can set the legend of each histogram; by default 
it is taken as the title of histogram. 
<br/>Finally the plot code itself will be set up by 
<pre> 
  hpl.plot(); 
</pre> 
where optionally it is possible to demand a logarithmic <ei>y</ei> scale. 
 
<p/> 
The <code>frame - add - plot</code> steps can be repeated as needed, each 
giving rise to a separate PDF file with a plot. In case a plot is to be 
generated from a single histogram the three steps can be joined into one 
<pre> 
  hpl.plotFrame( "onlyZ", ZpT ); 
</pre> 
where only the name of the PDF file and the histogram are compulsory, while 
further arguments as discussed above are optional. 
 
<p/> 
At the end, a file <code>bosonpT.py</code> has now been generated with the 
proper plotting commands. Additionally a data file has been generated for 
each histogram to be plotted, <code>pTdist-0.dat, pTdist-1.dat</code>, etc. 
Now doing 
<pre> 
  python bosonpT.py 
</pre> 
in a terminal window will produce the plots, such as <code>pTdist.pdf</code> 
and <code>onlyZ.pdf</code>. You may of course edit the python file further 
to improve on the finer details. 
 
<p/> 
Several examples are provided, e.g. in <code>main112.cc</code> and 
<code>main113.cc</code>. The <code>main201.cc</code> example 
illustrates that the <ei>x</ei> scale can be chosen logarithmically, 
by using an optional last argument when histograms are booked. 
 
<p/> 
Alternatively, it also straight forward to produce histograms direcly 
through the <aloc href="PythonInterface">Python interface</aloc> using 
the <code>binEdges</code> and <code>binContents</code> methods of the 
<code>Hist</code> class. 
<pre> 
  import pythia8, random 
  from matplotlib import pyplot 
  hst = pythia8.Hist("log", 100, 0.1, 4, True) 
  for i in range(0, int(1e6)): hst.Fill(random.gauss(0, 1)) 
  pyplot.hist(hst.getBinEdges()[:-1], hst.getBinEdges(), 
               weights = hst.getBinContents(), 
               histtype = "step", label = hst.getTitle()) 
</pre> 
 
<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, bool logX = false, bool doStats = false)"> 
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> 
<argument name="logX"> 
gives a logarithmic <ei>x</ei> scale between <code>xMin</code> and 
<code>xMax</code> if set <code>true</code>. Note that then 
<code>xMin</code> must be above 1e-20 so as to stay with positive 
numbers. 
</argument> 
<argument name="doStats"> 
  decides whether statistics allowing unbinned higher moments to be 
  calculated are saved (up to n = 7) and also whether the histogram summary 
  printout via the <code>&lt;&lt;</code> operator includes 
  <ei>+/-</ei> uncertainty estimates on the Mean, Median, and RMS values. 
</argument> 
</method> 
 
<method name="Hist::Hist(const Hist&amp; h)"> 
creates an identical copy of the histogram in the argument, 
including bin contents. 
</method> 
 
<method name="Hist::Hist(string title, const Hist&amp; 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&amp; Hist::operator=(const Hist&amp; 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, bool logXIn = false, bool doStatsIn = false)"> 
define a histogram that previously was only declared; 
see above for the meaning of the arguments. 
</method> 
 
<method name="static Hist Hist::plotFunc(function&lt;double(double)&gt; f, 
string title, int nBin, double xMin, double xMax, bool logX = false)"> 
create a histogram out of a provided function <ei>f(x)</ei>. 
The function is evaluated in the middle of each bin. 
</method> 
 
<method name="void Hist::title(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, double wstat)"> 
fill the histogram, where 
<argument name="xValue"> 
is the <ei>x</ei> position where the filling should occur, 
</argument> 
<argument name="weight" default="1."> 
is the amount of weight to be added at this <ei>x</ei> value, and 
</argument> 
<argument name="sigma" default="-1."> 
allows to assign the weight being added a statistical uncertainty larger or 
smaller than the weight itself. For values <code>sigma &gt; 0</code>, 
the statistical uncertainty of the weight being added is interpreted 
as an absolute value; i.e., <code>weight</code> &pm; 
<code>sigma</code> is added to the histogram contents. 
For values <code>sigma &lt; 0</code>, it is interpreted relative to 
the weight itself, as &pm; <code>-sigma * weight</code>. 
Thus, to add 
<ei>100 &pm; 10</ei> to a histogram bin, one could call either 
<code>fill( x, 100., 10. )</code> 
or <code>fill( x, 100., -0.1 )</code>. 
</argument> 
</method> 
 
<method name="friend ostream& operator&lt;&lt;(ostream&amp; os, 
const Hist&amp; 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.) 
<br/>If the histogram has been booked with logarithmic <ei>x</ei> 
scale then the log10 of each lower bin border will be displayed 
rather than the border itself, to avoid undue complication. 
The Pyplot interface will display correct values, however. 
</method> 
 
<method name="void Hist::table(ostream&amp; os = cout, 
bool printOverUnder = false, bool xMidBin = true, bool printError = false)"> 
</method> 
<methodmore name="void Hist::table(string fileName, 
bool printOverUnder = false, bool xMidBin = true, bool printError = false)"> 
print a three-column table, where the first column gives the center of 
each bin, the second one the corresponding bin contents, and the bin 
errors. 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. Finally, the optional <code>printError = 
false</code> will force the values in the third column to be zero. 
</methodmore> 
 
<method name="void Hist::yodaTable(ostream&amp; os = cout, 
string path = &quot;hist&quot;, double scaledBy = 1.0, 
vector&lt;int&gt; maskedBins = {})"> 
</method> 
<methodmore name="void Hist::yodaTable(string fileName, 
string path = &quot;hist&quot;, double scaledBy = 1.0, 
vector&lt;int&gt; maskedBins = {})"> 
print YODA <code>ESTIMATE1D_V3</code> histogram. Here, the arguments 
provide additional information that can be stored in a YODA histogram: 
<code>path</code> is the plotting path for the histogram, 
<code>scaledBy</code> is the histogram scale, and <code>maskedBins</code> 
is a vector of bin indices to mask. The choice between the two methods 
is the same as above for the <code>table</code> methods. 
</methodmore> 
 
<method name="void Hist::rivetTable(ostream&amp; os = cout, 
bool printError = true)"> 
</method> 
<methodmore name="void Hist::rivetTable(string fileName, 
bool printError = true)"> 
print a five-column table, where the first two columns give the lower 
and upper borders of each bin, the third one the bin contents, and the 
fourth and fifth the error (up and down) associated with the contents. 
This format matches the one that Rivet uses for its histograms. 
The choice between the two methods is the same as above for the 
<code>table</code> methods. 
<br/>With the optional <code>printError = false</code> the errors will 
be output as zero. 
</methodmore> 
 
<method name="void Hist::pyplotTable(ostream&amp; os = cout, 
bool isHist = true, bool printError = false)"> 
</method> 
<methodmore name="void Hist:pyplotTable(string fileName, 
bool isHist = true, bool printError = false)"> 
prints either a two- or a three-column table, depending on whether 
the contents are to be plotted as a curve or as a histogram, the latter 
being default. In either case the first column gives the center of each 
bin and the second one the corresponding bin contents. For a histogram 
the third column gives the lower border of each bin. Then also a final 
line is provided, that gives no further histogram contents but provides 
the upper border of the last bin. This format is used for plotting with 
Pyplot, see below. The choice between the two methods is the same as 
above for the <code>table</code> methods. 
</methodmore> 
 
<method name="void Hist::fillTable(ostream&amp; os = cout)"> 
</method> 
<methodmore name="void Hist::fillTable(string fileName)"> 
fill the contents of a two-column table into the histogram. The first 
column should give the center of each bin and the second one the 
corresponding bin contents to be filled. This is basically the inverse 
of the <code>table</code> methods above, whereby a written histogram 
contents can be read back in. Over- and underflow will be read back in 
if provided, but the <code>xMidBin = false</code> option will not work. 
</methodmore> 
 
<method name="friend void table(const Hist&amp; h1, const Hist&amp; h2, 
ostream& os = cout, bool printOverUnder = false, bool xMidBin = true)"> 
</method> 
<methodmore name="friend void table(const Hist&amp; h1, const Hist&amp; 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="string Hist::getTitle()"> 
return the title of the histogram. 
</method> 
 
<method name="int Hist::getBinNumber()"> 
return the number of bins in the histogram. 
</method> 
 
<method name="int Hist::getNonFinite()"> 
return the number of entries that are neither infinite nor NaN. 
</method> 
 
<method name="bool Hist::getLinX()"> 
return true if the histogram has a linear <ei>x</ei> scale and 
false if a logarithmic one. 
</method> 
 
<method name="bool Hist::getXMin()"> 
</method> 
<methodmore name="bool Hist::getXMax()"> 
return the lower and upper edge of the booked <ei>x</ei> range. 
</methodmore> 
 
<method name="bool Hist::getYMin()"> 
</method> 
<methodmore name="bool Hist::getYMax()"> 
return the highest and lowest value of any bin inside the histogram, 
i.e. excluding underflow and overflow. 
</methodmore> 
 
<method name="double Hist::getYAbsMin()"> 
returns the smallest absolute value (above 1e-20, to sidestep bins with 
zero content) in any of the bins inside the histogram. 
</method> 
 
<method name="double getXMean(bool unbinned=true)"> 
</method> 
<methodmore name="double getXMeanErr(bool unbinned=true)"> 
Return mean in X and error on mean, either unbinned from saved weight 
sums (default) or explicitly from the histogram bins <code>(unbinned = 
false)</code>. 
In the latter case, the error estimate includes a measure of the error 
due to the finite bin granularity, calculated as the difference between 
the binned and unbinned values and summed in quadrature with the 
statistical error. 
</methodmore> 
 
<method name="double getXMedian(bool includeOverUnder=true)"> 
</method> 
<methodmore name="double getXMedianErr(bool unbinned=true)"> 
Return median in X and its statistical error, ignoring underflow and 
 overflow (default) or including them <code>(includeOverUnder = 
 true)</code>. By default, the reported error includes the same granularity 
 estimate as for the mean value (and also computed using the mean, not the 
 median), but this can be switched off <code>(unbinned = false)</code>. 
</methodmore> 
 
<method name="double getXPercentile(double n, bool includeOverUnder = false)"> 
Return <ei>n</ei>'th percentile in X, ignoring underflow and overflow 
(default) or including them <code>(includeOverUnder = true)</code>. 
The input value for <ei>n</ei> is assumed to be in percent. Thus, 
e.g., for the 90th percentile, use <code>n = 90.0</code>. 
</method> 
 
<method name="double getYMean()"> 
Return the average value in Y, calculated as the sum of weights inside 
the histogram range divided by the number of times it was filled. 
</method> 
 
<method name="double getXRMN(int n=2, bool unbinned=true)"> 
</method> 
<methodmore name="double getXRMNErr(int n=2, bool unbinned=true)"> 
Return the <ei>n</ei>'th root of the <ei>n</ei>'th moment about the mean, 
<ei>&lt; (x - &lt;x&gt;)^n &gt;</ei>, and its estimated statistical error. 
The default value <ei>n = 2</ei> corresponds to the standard root-mean-square 
width. Up to <ei>n = 6</ei>, both unbinned and binned moments can 
be calculated. For <ei>n >= 7</ei>, and for all error estimates, only 
binned calculations are available. Note that (unlike ROOT), the error 
estimates do not assume normal distributions. If <code>unbinned = 
false</code> an estimate of the bin granularity error is added in 
quadrature to the statistical error (up to <ei>n = 6</ei>, beyond 
which the bin granularity error is not estimated). 
</methodmore> 
 
<method name="double getXRMS(bool unbinned=true)"> 
</method> 
<methodmore name="double getXRMSErr(bool unbinned=true)"> 
Wrapper of <code>getXRMN()</code> to return the root-mean-square width 
(aka the root of the second central moment, aka the root-mean-square 
deviation about the mean), and its error. If <code>unbinned = 
false</code> an estimate of the bin granularity error is added in 
quadrature to the statistical error. 
</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="double Hist::getBinError(int iBin)"> 
return the error in bin <code>iBin</code>, ranging from 1 through 
<code>numberOfBins</code>. Note: with the value defined as the sum of 
weights in the bin, the error is defined the square root of the sum of 
squared weights. 
</method> 
 
<method name="double Hist::getBinError2(int iBin)"> 
return the error squared in bin <code>iBin</code>, ranging from 1 
through <code>numberOfBins</code>. 
</method> 
 
<method name="double Hist::getBinEdge(int iBin)"> 
return the lower edge of bin <code>iBin</code>, ranging from 1 through 
<code>numberOfBins</code>. 
</method> 
 
<method name="double Hist::getBinCenter(int iBin)"> 
return the center of bin <code>iBin</code>, ranging from 1 through 
<code>numberOfBins</code>. 
</method> 
 
<method name="double Hist::getBinWidth(int iBin)"> 
return the width of bin <code>iBin</code>, ranging from 1 through 
<code>numberOfBins</code>. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinContents()"> 
the same as <code>getBinContent</code>, but for all bins. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinErrors()"> 
the same as <code>getBinError</code>, but for all bins. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinError2s()"> 
the same as <code>getBinError2</code>, but for all bins. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinEdges()"> 
the same as <code>getBinEdges</code>, but for all bins. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinWidths()"> 
the same as <code>getBinWidths</code>, but for all bins. 
</method> 
 
<method name="vector&lt;double&gt; Hist::getBinCenters()"> 
the same as <code>getBinCenters</code>, but for all bins. 
</method> 
 
<method name="int Hist::getEntries(bool alsoNonFinite = true)"> 
return the number of entries, i.e. the number of time that 
<code>fill(...)</code> has been called. If <code>alsoNonFinite</code> 
is false, then entries that are infinity or NaN are ignored. 
</method> 
 
<method name="double Hist::getWeightSum(bool alsoOverUnder = true)"> 
  return the sum of weights with which the histogram was filled. The 
  output of this method only differs from that of 
  <code>getEntries()</code> above if non-unity weights were used when 
  the histogram was filled. If <code>alsoOverUnder</code> is true, then 
  the overflow and underflow bins are included in the weight sum, 
  otherwise not. 
</method> 
 
<method name="double Hist::getNEffective()"> 
 Return effective entries (for weighted histograms = number 
 of equivalent unweighted events for same statistical power), 
 calculated as <ei>(sum(w))^2 / sum(w^2)</ei>. 
</method> 
 
 
<method name="bool Hist::sameSize(const Hist&amp; 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::takeFunc(function&lt;double(double)&gt; func)"> 
pass an arbitrary function that takes as an argument a 
<code>double</code> and returns a <code>double</code>. This function 
is applied to the current contents bin by bin. 
</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="void Hist::normalize( double f = 1., 
bool overflow = true)"> 
Rescale the histogram to a given sum of all bin contents, where 
<argument name="sum" default="1."> 
is the intended sum of all bin contents after rescaling, and 
</argument> 
<argument name="overflow" default="on"> 
includes the underflow and overflow bins in the definition of the 
original area to be rescaled, or not. Either way, the under/overflow 
are always rescaled by the same factor as the histogram proper. 
</argument> 
</method> 
 
<method name="void Hist::normalizeIntegral( double f = 1., 
bool alsoOverflow = true)"> 
Rescale the histogram so that it integrates to the value given by 
<argument name="f" default="1."/>. This is the appropriate 
normalization if the histogram represents a probability 
distribution. The argument <argument name="overflow" default="on"/> 
specifies whether the underflow and overflow bins in the definition of 
the original area to be rescaled, or not. Either way, the 
under/overflow are always rescaled by the same factor, except the bin 
width, as the histogram proper. Bin widths are handled for both linear 
and log plots. 
</method> 
 
<method name="void Hist::normalizeSpectrum( double wtSum)"> 
Rescale the histogram by a factor <code>1/(wtSum * dx)</code> where 
<code>dx</code> is the bin width. This is the appropriate 
renormalization if the histogram represents a spectrum on the form 
<ei>(1/N_{ev}) dN/dx</ei>. <argument name="wtSum" default="1."/> is 
the total weight of the events, which if events are unweighted is just 
the total number of successfully generated events. The under/overflow 
are always rescaled by the same factor, except the bin width, as the 
histogram proper. Bin widths are handled for both linear and log 
plots. 
</method> 
 
<method name="void Hist::makeCumulative( bool updateStatistics = false, 
bool withUnderflow = false)"> 
Add the contents of the previous (lower) bin to that of the current 
one, iteratively, such that the value stored for a bin becomes the 
sum of all original bin contents up to and including the current one. 
In the limit of bin width going to zero this cumulative sum would 
become normal integration. 
<argument name="updateStatistics" default="off"> 
By default the statistical information, such as mean and width, 
is kept from the original unintegrated distribution. As an alternative 
the statistics can be recalculated, but then on the assumption that 
all previous bin contents are added to the middle of the current bin. 
</argument> 
<argument name="withUnderflow" default="off"> 
Normally underflow is ignored, but as an option it can be added to the 
first bin as a first step of the iteration over bins. 
</argument> 
</method> 
 
<method name="Hist&amp; Hist::operator+=(const Hist&amp; h)"> 
</method> 
<methodmore name="Hist&amp; Hist::operator-=(const Hist&amp; 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&amp; Hist::operator*=(const Hist&amp; h)"> 
</method> 
<methodmore name="Hist&amp; Hist::operator/=(const Hist&amp; 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&amp; Hist::operator+=(double f)"> 
</method> 
<methodmore name="Hist&amp; Hist::operator-=(double f)"> 
adds or subtracts each bin content by the common offset <ei>f</ei>. 
</methodmore> 
 
<method name="Hist&amp; Hist::operator*=(double f)"> 
</method> 
<methodmore name="Hist&amp; 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&amp; h1)"> 
</method> 
<methodmore name="friend Hist operator+(const Hist&amp; h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator+(const Hist&amp; 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&amp; h1)"> 
</method> 
<methodmore name="friend Hist operator-(const Hist&amp; h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator-(const Hist&amp; 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&amp; h1)"> 
</method> 
<methodmore name="friend Hist operator*(const Hist&amp; h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator*(const Hist&amp; 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&amp; h1)"> 
</method> 
<methodmore name="friend Hist operator/(const Hist&amp; h1, double f)"> 
</methodmore> 
<methodmore name="friend Hist operator/(const Hist&amp; 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> 
 
<method name="HistPlot::HistPlot(string pythonName, bool useLegacy = true)"> 
create a file to which successively Python commands can be written. 
<argument name="pythonName"> 
the name of the Python code file will become <code>pythonName.py</code>. 
When later executed with <code>python pythonName.py</code> the frames 
defined below will be drawn and output. 
</argument> 
<argument name="useLegacy"> 
A parameter necessary for compatibility with older versions of Matplotlib. 
For Matplotlib version 3.2 and earlier, this argument must be false; 
for version 3.5 or later, it must be true. 
</argument> 
</method> 
 
<method name="void HistPlot::frame( string frameName, string title = 
&quot;&quot;, string xLabel = &quot;&quot;, string yLabel = &quot;&quot;, 
double xSize = 8., double ySize = 6.)"> 
command to open a frame where successive histograms can be added and 
plotted. 
<argument name="frameName"> 
the name of the frame. When the Python program is run, the output PDF 
file will be <code>frameName.pdf</code>. If this is blank or it is the 
same name as the previous frame, then the frame will be appended to 
the most recent previous PDF file. It cannot be left blank for the 
first call. 
</argument> 
<argument name="title"> 
an optional title of the histogram, plotted on top of the frame. 
</argument> 
<argument name="xLabel"> 
an optional label plotted below the <ei>x</ei> axis. 
</argument> 
<argument name="yLabel"> 
an optional label plotted to the right of the <ei>y</ei> axis. 
</argument> 
<argument name="xSize, ySize"> 
specifies the <ei>x</ei> and <ei>y</ei> size, in inches, of the canvas 
(the full figure area). Default values agree with the Pyplot defaults. 
</argument> 
<note>Note:</note> the title and labels may contain special LaTeX symbols, 
e.g. simple math formulae enclosed by dollar signs $...$, that are 
interpreted by the Pyplot package. A backslash \ needs to be doubled to \\, 
however, since else it will be intepreted as an escape sequence. 
</method> 
 
<method name="void HistPlot::add(const Hist& hist, 
string style = &quot;h&quot;, string legend = &quot;void&quot;)"> 
add a histogram to the current plot frame, where 
<argument name="hist"> 
is the name of the histogram to be plotted, 
</argument> 
<argument name="style"> 
tells how the histogram data will be represented in the figure, where 
default "h" is histogram style, "-" full lines, "--" dashed ones, "-." 
dash-dotted ones, "." points, "o" circles, "+" and "x" crosses, "*" 
stars, and so on as described in the Pyplot manual; alternatively to 
"h" the argument "e" can be specified which will include error bars on 
the histogram; additionally a colour may also be specified as 
described earlier, separated by a comma from the symbol; either of 
both can be used, e.g. "h,r", ",magenta", "+,", "--" or just ""; and 
</argument> 
<argument name="legend"> 
is the text written in association with the plotting symbol, where the 
default <code>void</code> is replaced by the title provided when the 
histogram was booked, and LaTex input may be used as described in the note 
above. 
</argument> 
</method> 
 
<method name="void HistPlot::addFile(string file, 
string style = &quot;o&quot;, string legend = &quot;void&quot;, 
string xyerr = &quot;&quot;)"> 
add data in an already existing file to the current plot frame, where 
<argument name="file"> 
is the name of the file to be plotted, 
</argument> 
<argument name="style"> 
tells how the data will be represented in the figure, just like in the 
<code>add</code> method above, but by default intended for discrete 
points rather than histograms or connected lines; 
</argument> 
<argument name="legend"> 
is the text written in association with the plotting symbol, where the 
default <code>void</code> is replaced by the name of the input file, 
and LaTex input may be used as described in the note above for 
<code>frame</code>; and 
</argument> 
<argument name="xyerr"> 
can be used to provide error bars for a point, where the options are 
to give an <code>x</code> to plot a symmetric error bar in the <ei>x</ei> 
direction, an <code>X</code> for an asymmetric ditto, a <code>y</code> for 
a symmetric error bar in the <ei>y</ei> direction, and a <code>Y</code> for 
an asymmetric ditto, and where both <ei>x</ei> and <ei>y</ei> can be 
combined or omitted as desired. 
</argument> 
<note>Note:</note> this method is primarily intended to overlay data 
points over existing histograms, and not for standalone usage. The data 
file is expected to contain at least two columns, where the first two 
give the <ei>(x,y)</ei> coordinates of the points. Then up to four 
error columns can follow, first the ones pertaining to the <ei>x</ei> 
error, if any, and then to <ei>y</ei>. Symmetric errors require one 
column with length in both directions, whereas asymetric gives first the 
length downwards and then the length upwards, both as non-negative numbers. 
For example, to plot the point (1.0, 2.0) with a symmetric error +/-0.5 in the 
x-direction and an asymmetric error +0.2/-0.5 in the y-direction, the file 
should contain the row <code>1.0 2.0 0.5 0.2 -0.5</code> and the value of 
<code>xyerr</code> might be <code>"xY"</code>. 
</method> 
 
<method name="void HistPlot::plot( bool logY = false, bool logX = false)"> 
write the Pyplot commands for the current frame, as defined by the input 
from the two methods above, and additionally write the data files needed 
when the Python code is to be run. 
<argument name="logY"> 
By default a linear <ei>y</ei> scale is used, but when true the <ei>y</ei> 
scale is instead logarithmic, except that a linear scale is used the very 
last step to zero bin content. 
</argument> 
<argument name="logX"> 
By default a linear or logarithmic <ei>x</ei> scale is used depending on 
how the histograms were booked, i.e. by default linear, and this should 
not be overridden. For the case that the only input comes from 
<code>addFile</code> commands, however, this option gives the possibility 
to pick a logarithmic <ei>x</ei> scale instead of the default linear one. 
(Presumably you would then use the <code>plot</code>variant below.) 
Note the confusing order of the two arguments, which is caused by the 
first presumably being much more commonly used than the second. 
</argument> 
</method> 
 
<method name="void HistPlot::plot( double xMin, double xMax, double yMin, 
double yMax, bool logY = false, bool logX = false)"> 
a variant of the <code>plot</code> method above, where the <ei>x</ei> 
and <ei>y</ei> ranges of the plot frame can be set by hand instead of 
calculated from the histogram information. 
</method> 
 
<method name="void HistPlot::plotFrame( string frameName, const Hist& hist, 
string title = &quot;&quot;, string xLabel = &quot;&quot;, string yLabel = 
&quot;&quot;, string style = &quot;h&quot;, string legend = &quot;void&quot;, 
bool logY = false)"> 
is an omnibus version combining the three above methods when only a single 
histogram is to be plotted in a frame. The arguments and their meaning is 
the same as already explained for them, and the order in which they appear 
is almost the same, the exception being that the two compulsory arguments 
<code>frameName</code> and <code>hist</code> have been put at the beginning. 
</method> 
 
 <h3>Using YODA histograms</h3> 
 
The <code>YODA</code> library <ref>Buc23</ref> for statistics and plotting 
(found at <a href="https://yoda.hepforge.org/">the YODA webpage</a>) is a 
lightweight library for histogramming, which mainly serves as the plotting 
framework for <a href="https://rivet.hepforge.org/">RIVET</a> (see also the 
section on <aloc href="RIVETusage">RIVET usage in PYTHIA</aloc>). It can, 
however, also be used in stand-alone form, and PYTHIA comes with a 
simplistic interface to make it easy to link to <code>YODA</code> and use 
the statistics objects. 
 
<p/> 
After installing the <code>YODA</code> library to a suitable location, 
configure PYTHIA with <code>YODA</code> using <code>--with-yoda</code>, 
after which you can use the interface, located in 
<code>include/PythiaPlugins/Pythia8Yoda.h</code>. Note that programs using 
<code>YODA</code> must be compiled with at least <code>--std=c++17</code>, 
which is handled automatically by <code>examples/Makefile</code> for 
the example <code>main114.cc</code>. 
 
<p/> 
Use the interface by creating a <code>Pythia8Yoda</code> object using the 
constructor: 
<method name="Pythia8Yoda::Pythia8Yoda(const string& anaNameIn = 
&quot;/PYTHIA8/&quot;, const string& outputIn = &quot;pythia&quot;)"> 
which takes two optional arguments: one that gives histograms a prefix 
for easier sorting, and a second providing the output file name, which 
will be postfixed by <code>.yoda</code>. 
</method> 
 
<p/> 
The main method for booking new histograms or profiles is the templated: 
<method name="shared_ptr&lt;T&gt; Pythia8Yoda::book(Args&&... args)"> 
which will return a shared pointer to any <code>YODA</code> object of 
type <code>T</code>, given its normal constructor arguments. 
</method> 
<p/> 
Specializations are given for the most common types, which are also aliased 
as: <code>Histo1DPtr, Histo2DPtr, Histo3DPtr, Profile1DPtr</code>, and 
<code>Profile2DPtr</code>. They can each, in turn, be booked by the methods: 
 
<method name="Histo1DPtr Pythia8Yoda::bookHisto1D(int nBins, double xMin, 
double xMax, const string& title)"> 
which books a 1D histogram. 
</method> 
 
<method name="Histo2DPtr Pythia8Yoda::bookHisto2D(int nBinsX, double xMin, 
double xMax, int nBinsY, double yMin, double yMax, const std::string& title)"> 
which books a 2D histogram. 
</method> 
 
<method name="Histo3DPtr Pythia8Yoda::bookHisto3D(int nBinsX, double xMin, 
double xMax, int nBinsY, double yMin, double yMax, int nBinsZ, double zMin, 
double zMax, const std::string& title)"> 
which books a 3D histogram. 
</method> 
 
<method name="Profile1DPtr Pythia8Yoda::bookProfile1D(int nBins, double xMin, 
double xMax, const std::string& title)"> 
which books a 1D profile histogram. 
</method> 
<method name="Profile2DPtr Pythia8Yoda::bookProfile2D(int nBinsX, double xMin, 
double xMax, int nBinsY, double yMin, double yMax, const std::string& title)"> 
which books a 2D profile histogram. 
</method> 
<p/> 
At the end of the run (or intermittently), histograms can be written to a file 
by calling: 
<method name="void Pythia8Yoda::write()"> 
which writes to the file name given in the constructor. 
</method> 
<p/> 
Further example use cases can be studied in the example <code>main114</code>. 
After writing out a histogram file, it can be plotted with the standard 
<code>YODA</code> or <code>RIVET</code> tools (e.g. 
<code>rivet-mkhtml</code>), or by yourself using a custom Python 
script. We refer to the <a href="https://yoda.hepforge.org/">YODA 
documentation</a> for details. 
</chapter> 
 
<!-- Copyright (C) 2026 Torbjorn Sjostrand --> 
