Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:24:28

0001 // -*- C++ -*-
0002 //
0003 // Histogram.h is a part of Herwig - A multi-purpose Monte Carlo event generator
0004 // Copyright (C) 2002-2019 The Herwig Collaboration
0005 //
0006 // Herwig is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef HERWIG_Histogram_H
0010 #define HERWIG_Histogram_H
0011 //
0012 // This is the declaration of the Histogram class.
0013 //
0014 #include "Histogram.fh"
0015 #include "ThePEG/Interface/Interfaced.h"
0016 #include "Statistic.h"
0017 #include <string>
0018 
0019 namespace Herwig {
0020 
0021 using namespace ThePEG;
0022 
0023   /**
0024    * Options for histogram output. 
0025    * They can be combined using the '|' operator, e.g. 'Frame | Ylog'
0026    */
0027   namespace HistogramOptions {
0028     const unsigned int None      = 0;      /**< No options */
0029     const unsigned int Frame     = 1;      /**< Plot on new frame */
0030     const unsigned int Errorbars = 1 << 1; /**< Plot error bars */
0031     const unsigned int Xlog      = 1 << 2; /**< log scale for x-axis */
0032     const unsigned int Ylog      = 1 << 3; /**< log scale for y-axis */
0033     const unsigned int Smooth    = 1 << 4; /**< smooth the line */
0034     const unsigned int Rawcount  = 1 << 5; /**< don't normalize to unit area */
0035   }
0036 
0037 /**
0038  * The Histogram class is a simple histogram for the Analysis handlers.
0039  *
0040  * @see \ref HistogramInterfaces "The interfaces"
0041  * defined for Histogram.
0042  */
0043 class Histogram: public Interfaced {
0044 
0045 public:
0046 
0047   /** @name Standard constructors and destructors. */
0048   //@{
0049   /**
0050    * The default constructor.
0051    * @param lower The lower limit of the histogram
0052    * @param upper The upper limit of the histogram
0053    * @param nbin  Number of bins
0054    */
0055   Histogram(double lower=0., double upper=0., unsigned int nbin=0)
0056   : _globalStats(), _havedata(false), _bins(nbin+2),_prefactor(1.),_total(0.) {
0057     if (upper<lower) swap(upper,lower);
0058     _bins[0].limit=-1.e100;
0059     double limit(lower);
0060     double width((upper-lower)/nbin);
0061     for(unsigned int ix=1; ix <= nbin; ++ix) {
0062       _bins[ix].limit=limit;
0063       limit += width;
0064     }
0065     _bins.back().limit=limit;
0066   }
0067 
0068   /**
0069    * Constructor for variable width bins
0070    * @param limits The lower limits for the bins followed by the upper limit of the last bin
0071    */
0072   Histogram(vector<double> limits) 
0073     : _globalStats(), _havedata(false), _bins(limits.size()+1), _prefactor(1.),_total(0.) {
0074     _bins[0].limit=-1.e100;
0075     for (size_t i=1; i<=limits.size(); ++i)
0076       _bins[i].limit=limits[i-1];
0077   }
0078 
0079   /**
0080    * Constructor with data included
0081    * @param limits The lower limits for the bins followed by the upper limit of the last bin
0082    * @param data The data
0083    * @param dataerror The errors on the data
0084    */
0085   Histogram(vector<double> limits, vector<double> data, vector<double> dataerror)
0086     : _globalStats(), _havedata(true), _bins(limits.size()+1), _prefactor(1.),_total(0.) {
0087     _bins[0].limit=-1.e100;
0088     for (size_t i=1; i<=limits.size(); ++i)
0089       _bins[i].limit=limits[i-1];
0090     
0091     // no data goes into _bins[0] or _bins.back()!
0092     for (size_t i=1; i<=min(limits.size()-1,data.size()); ++i)
0093       _bins[i].data=data[i-1];
0094     
0095     for (size_t i=1; i<=min(limits.size()-1,dataerror.size()); ++i)
0096       _bins[i].dataerror=dataerror[i-1];
0097   }
0098   
0099   //@}
0100 
0101 public:
0102 
0103   /**
0104    *  Operator to add a point to the histogrma with unit weight
0105    */
0106   void operator += (double input) {
0107     addWeighted(input,1.0);
0108   }
0109 
0110   /**
0111    *  Function to add a weighted point to the histogram
0112    */
0113   void addWeighted(double input, double weight) {
0114     if(std::isnan(input)) return;
0115     unsigned int ibin;
0116     for(ibin=1; ibin<_bins.size(); ++ibin) {
0117       if(input<_bins[ibin].limit)
0118     break;
0119     }
0120     _bins[ibin-1].contents   += weight;
0121     _bins[ibin-1].contentsSq += sqr(weight);
0122     _globalStats += weight * input;
0123     _total += weight;
0124   }
0125 
0126   /**
0127    *  Number of bins (not counting the overflow)
0128    */
0129   unsigned int numberOfBins() const { 
0130     return _bins.size()-2;
0131   }
0132 
0133 
0134   /**
0135    *  Get the prefactor
0136    */
0137   double prefactor() const {
0138     return _prefactor;
0139   }
0140 
0141   /**
0142    *  Set the prefactor
0143    */
0144   void   prefactor(double in ) {
0145     _prefactor=in;
0146   }
0147 
0148   /**
0149    *  Access to the statistics on the total entry of the histogram
0150    */
0151   const Statistic & globalStatistics() const {
0152     return _globalStats;
0153   }
0154 
0155   /**
0156    *  Normalise the distributions to the data
0157    */
0158   void normaliseToData();
0159 
0160   /**
0161    *  Normalise the distributions to the total cross section
0162    */
0163   void normaliseToCrossSection();
0164 
0165   /**
0166    *  Return the chi squared
0167    * @param chisq The chi squared
0168    * @param ndegrees The number of points
0169    * @param minfrac The minimum fractional error on the data point
0170    */
0171   void chiSquared(double & chisq, 
0172           unsigned int & ndegrees, double minfrac=0.) const;
0173 
0174   /**
0175    * @brief Output as file ready for usage with flat2aida and other Rivet tools
0176    * @param out           The output stream
0177    * @param histogramname The histogram name identifying the histogram. Required
0178    *                      for comparisons (e.g. with rivet-mkhtml or with
0179    *                      compare-histos)
0180    * @param analysisname  The analysis name
0181    * @param title         The title for the top of the plot in LaTeX format
0182    * @param xlabel        The x label in LaTeX format
0183    * @param ylabel        The y label in LaTeX format
0184    * @param rawcount      Don't normalise to unit area.
0185    * @param multiplicator Factor the histogram is multiplied with.
0186    * N.B.  Experimental data is not output.
0187    */
0188   void rivetOutput(ostream & out,
0189                    string histogramname = string("default"),
0190                    string analysisname = string("default"),
0191                    string title  = string(),
0192                    string xlabel = string(),
0193                    string ylabel = string(),
0194                    bool rawcount = false,
0195                    double multiplicator = 1.0) const;
0196 
0197   /**
0198    *  Output as a topdrawer file. The histogram is normalised to unit area
0199    * @param out The output stream
0200    * @param flags A bitmask of flags from HistogramOptions, e.g. Frame|Ylog
0201    * @param colour The colour for the line
0202    * @param title  The title for the top of the plot
0203    * @param titlecase topdraw format for the title
0204    * @param left   Left axis lable
0205    * @param leftcase topdraw format for left axis label
0206    * @param bottom  Bottom axis lable
0207    * @param bottomcase Bottom axis lable ofr topdraw
0208    * N.B. in td smoothing only works for histograms with uniform binning.
0209    */
0210   void topdrawOutput(ostream & out,
0211              unsigned int flags = 0,
0212              string colour = string("BLACK"),
0213              string title = string(),
0214              string titlecase = string(),
0215              string left = string(),
0216              string leftcase = string(),
0217              string bottom = string(),
0218              string bottomcase = string()
0219              ) const;
0220 
0221   void topdrawMCatNLO(ostream & out,
0222               unsigned int flags =0 ,
0223               string colour = string("BLACK"),
0224               string title = string()
0225               ) const;
0226 
0227   /**
0228    *  Output as a topdrawer file. A bin by bin average is taken.
0229    * @param out The output stream
0230    * @param frame output on a new graph
0231    * @param errorbars output data points with error bars
0232    * @param xlog  log scale on x axis
0233    * @param ylog  log scale on y axis
0234    * @param colour The colour for the line
0235    * @param title  The title for the top of the plot
0236    * @param titlecase topdraw format for the title
0237    * @param left   Left axis lable
0238    * @param leftcase topdraw format for left axis label
0239    * @param bottom  Bottom axis lable
0240    * @param bottomcase Bottom axis lable ofr topdraw
0241    */
0242   void topdrawOutputAverage(ostream & out,
0243                 bool frame,
0244                 bool errorbars,
0245                 bool xlog, bool ylog,
0246                 string colour=string("BLACK"),
0247                 string title=string(),
0248                 string titlecase =string(),
0249                 string left=string(),
0250                 string leftcase =string(),
0251                 string bottom=string(),
0252                 string bottomcase =string()) const;
0253 
0254   /**
0255    * get the number of visible entries (all entries without those in the
0256    * under- and overflow bins) in the histogram.  This assumes integer
0257    * entries, ie it gives wrong results for weighted histograms.
0258    */
0259   unsigned int visibleEntries() const;
0260 
0261   /**
0262    * Compute the normalisation of the data. 
0263    */
0264   double dataNorm() const;
0265 
0266   /**
0267    * Output into a simple ascii file, easily readable by gnuplot.
0268    */
0269   void simpleOutput(ostream & out, bool errorbars, bool normdata=false);
0270 
0271   /**
0272    * Dump bin data into a vector
0273    */
0274   vector<double> dumpBins() const;
0275 
0276   /**
0277    * Returns a new histogram containing bin-by-bin ratios of two histograms
0278    */
0279   Histogram ratioWith(const Histogram & h2) const;
0280 
0281 
0282   /**
0283    * @brief Returns limits for bins with exponentially increasing widths.
0284    *        For usage with the variable-bin-width Histogram constructor.
0285    * @param xmin  Lower limit of the first bin, needs to be > 0
0286    * @param nbins Number of bins
0287    * @param base  The base, needs to be > 1
0288    */
0289   static
0290   vector<double> LogBins(double xmin, unsigned nbins, double base = 10.0);
0291 
0292 
0293 public:
0294 
0295   /**
0296    * The standard Init function used to initialize the interfaces.
0297    * Called exactly once for each class by the class description system
0298    * before the main function starts or
0299    * when this class is dynamically loaded.
0300    */
0301   static void Init();
0302 
0303 protected:
0304 
0305   /** @name Clone Methods. */
0306   //@{
0307   /**
0308    * Make a simple clone of this object.
0309    * @return a pointer to the new object.
0310    */
0311   virtual IBPtr clone() const;
0312 
0313   /** Make a clone of this object, possibly modifying the cloned object
0314    * to make it sane.
0315    * @return a pointer to the new object.
0316    */
0317   virtual IBPtr fullclone() const;
0318   //@}
0319 
0320 private:
0321 
0322   /**
0323    * The assignment operator is private and must never be called.
0324    * In fact, it should not even be implemented.
0325    */
0326   Histogram & operator=(const Histogram &) = delete;
0327 
0328 private:
0329 
0330  /**
0331    *  Global statistics of all data that went into the histogram.
0332    */
0333   Statistic _globalStats;
0334 
0335  /**
0336    * Set to true if there is experimental data available
0337    */
0338   bool _havedata;
0339 
0340  /**
0341    *  One bin of the histogram. limit is the _lower_ bound of the bin.
0342    */
0343   struct Bin {
0344     /**
0345      *  Default constructor
0346      */
0347     Bin() : contents(0.0), contentsSq(0.0), 
0348         limit(0.0), data(0.0), dataerror(0.0), points(0) {}
0349     /**
0350      *  Contents of the bin
0351      */
0352     double contents;
0353 
0354     /**
0355      *  Contents squared for the error
0356      */
0357     double contentsSq;
0358 
0359     /**
0360      * The limit for the bin
0361      */
0362     double limit;
0363 
0364     /**
0365      *  The experimental value for the bin
0366      */
0367     double data;
0368 
0369     /**
0370      *  The error on the experimental value for the bin
0371      */
0372     double dataerror;
0373 
0374     /**
0375      *  The number of points in the bin
0376      */
0377     long points;
0378   };
0379 
0380   /**
0381    *  The histogram bins. _bins[0] is the underflow, _bins.back() the overflow
0382    */
0383   vector<Bin> _bins;
0384 
0385   /**
0386    *  Prefactors to multiply the output by
0387    */
0388   double _prefactor;
0389 
0390   /**
0391    *  Total entry
0392    */
0393   double _total;
0394 
0395 
0396 public:
0397 
0398   /**
0399    * The vector of bins
0400    */
0401   vector<Bin> bins() const { return _bins; }
0402 
0403 };
0404 
0405 }
0406 
0407 #endif /* HERWIG_Histogram_H */