herwig is hosted by Hepforge, IPPP Durham
Herwig++  2.7.0
Histogram2.h
00001 // -*- C++ -*-
00002 
00003 // (C) 2007-2009 Simon Plaetzer -- sp@particle.uni-karlsruhe.de
00004 
00005 #ifndef Analysis2_Histogram2_H
00006 #define Analysis2_Histogram2_H
00007 //
00008 // This is the declaration of the Histogram2 class.
00009 //
00010 
00011 #include <cassert>
00012 
00013 #include "ThePEG/Interface/Interfaced.h"
00014 #include "Histogram2.fh"
00015 
00016 #include "ThePEG/Utilities/StringUtils.h"
00017 
00018 namespace Analysis2 {
00019 
00020 using namespace ThePEG;
00021 
00027 class HistogramChannel {
00028 
00029 public:
00030 
00033 
00037   inline HistogramChannel ();
00038 
00042   inline explicit HistogramChannel (unsigned int bins, bool counting = true);
00043 
00047   inline explicit HistogramChannel (const vector<pair<double,double> >&,
00048                                     double underflow = 0., double overflow = 0.);
00050 
00051 public:
00052 
00059   inline void book (unsigned int bin, double weight = 1.);
00060 
00064   inline void bookUnderflow (double weight = 1.);
00065 
00069   inline void bookOverflow (double weight = 1.);
00070 
00074   inline void nanEvent ();
00076 
00077 public:
00078 
00084   HistogramChannel& operator += (const HistogramChannel&);
00085 
00089   HistogramChannel& operator -= (const HistogramChannel&);
00090 
00094   HistogramChannel& operator *= (const HistogramChannel&);
00095 
00099   HistogramChannel& operator *= (double);
00100 
00104   HistogramChannel& operator += (double);
00105 
00110   HistogramChannel& operator *= (pair<double,double>);
00111 
00115   HistogramChannel& operator /= (const HistogramChannel&);
00116 
00120   inline HistogramChannel& operator /= (double);
00121 
00126   HistogramChannel& operator /= (pair<double,double>);
00128 
00129 public:
00130 
00136   inline bool isCountingChannel () const;
00137 
00141   inline vector<pair<double,double> > bins () const;
00142 
00149   inline pair<double,double> bin (unsigned int) const;
00150 
00154   inline vector<unsigned long> binEntries () const;
00155 
00160   inline unsigned long binEntries (unsigned int) const;
00161 
00166   inline void bin (unsigned int, pair<double,double>, unsigned long en = 0);
00167 
00171   inline pair<double,double> outOfRange () const;
00172 
00176   inline unsigned long visible () const;
00177 
00181   inline unsigned long total () const;
00182 
00187   inline vector<unsigned long> nanWeights () const;
00188 
00193   unsigned long nanWeightEvents () const;
00194 
00198   inline unsigned long nanEvents () const;
00200 
00201 public:
00202 
00205 
00210   inline void finish ();
00211 
00216   inline double binVariance (unsigned int) const;
00217 
00222   inline double weightMean (unsigned int) const;
00223 
00228   inline double weightVariance (unsigned int) const;
00229 
00234   void differential (const vector<pair<double,double> >&);
00235 
00239   pair<double,double> binSum () const;
00240 
00244   inline pair<double,double> binAverage () const;
00245 
00250   pair<double,double> integrate (const vector<pair<double,double> >&) const;
00251 
00255   pair<double,double> average (const vector<pair<double,double> >&) const;
00256 
00260   HistogramChannel delta (const HistogramChannel& channel) const;
00261 
00268   HistogramChannel chi2 (const HistogramChannel& channel, double minfrac = .0) const;
00269 
00277   HistogramChannel profile () const;
00278 
00280 
00281 public:
00282 
00285 
00289   void write (ostream&, const string&);
00290 
00294   string read (istream&);
00295 
00297 
00298 public:
00299 
00306   void persistentOutput(PersistentOStream & os) const;
00307 
00312   void persistentInput(PersistentIStream & is);
00314 
00315 private:
00316 
00321   bool _isCountingChannel;
00322 
00327   vector<pair<double,double> > _bins;
00328 
00333   vector<unsigned long> _binEntries;
00334 
00338   pair<double,double> _outOfRange;
00339 
00343   unsigned long _visible;
00344 
00348   unsigned long _total;
00349 
00354   unsigned long _nanEvents;
00355 
00360   vector<unsigned long> _nanWeights;
00361 
00365   bool _finished;
00366 
00367 };
00368 
00372 inline PersistentOStream& operator << (PersistentOStream& os, const HistogramChannel&);
00373 
00377 inline PersistentIStream& operator >> (PersistentIStream& is, HistogramChannel&);
00378 
00382 inline HistogramChannel operator + (const HistogramChannel& a, const HistogramChannel& b);
00383 
00387 inline HistogramChannel operator - (const HistogramChannel& a, const HistogramChannel& b);
00388 
00392 inline HistogramChannel operator * (const HistogramChannel& a, const HistogramChannel& b);
00393 
00397 inline HistogramChannel operator / (const HistogramChannel& a, const HistogramChannel& b);
00398 
00405 inline string getNextTag (istream&);
00406 
00411 template<class T>
00412 inline void fromString (const string& str, T& result) {
00413   istringstream buffer (str);
00414   buffer >> result;
00415 }
00416 
00420 namespace ChannelOutput {
00421 
00425   const unsigned int Default     = 0;
00426 
00431   const unsigned int Bincenters  = 1;
00432 
00436   const unsigned int NoErrorbars = 1 << 1;
00437 
00442   const unsigned int NanEvents   = 1 << 2;
00443 
00447   const unsigned int Statistics  = 1 << 3;
00448 
00449 }
00450 
00460 class Histogram2: public Interfaced {
00461 
00462 public:
00463 
00466 
00470   inline Histogram2 ();
00471 
00475   virtual ~Histogram2();
00477 
00478 public:
00479 
00488   explicit Histogram2 (double low, double high,
00489                        unsigned int bins,
00490                        const string& name = "mc");
00491 
00497   explicit Histogram2 (const vector<pair<double,double> >&, const string& name);
00498 
00505   explicit Histogram2 (const string& dataFile, const string& dataName);
00506 
00510   inline void setName (const string&);
00511 
00513 
00514 public:
00515 
00519   void book (const string& name, double event, double weight = 1.);
00520 
00527   inline bool haveChannel (const string&) const;
00528 
00532   inline HistogramChannel& channel (const string&);
00533 
00537   inline HistogramChannel channel (const string&) const;
00538 
00542   inline void insertChannel (const string&, const HistogramChannel&);
00543 
00547   inline void insertChannel (const string&);
00548 
00552   inline HistogramChannel removeChannel (const string&);
00554 
00555 public:
00556 
00562   inline const vector<pair<double,double> >& binning () const;
00563 
00567   inline const map<double,unsigned int>& binhash () const;
00568 
00572   inline pair<double,double> range () const;
00574 
00575 public:
00576 
00579 
00583   inline void finish (const string&);
00584 
00589   inline void differential (const string&);
00590 
00594   inline pair<double,double> integrate (const string&) const;
00595 
00599   inline void normalise (const string&);
00600 
00605   inline void normalise (const string&, const string&);
00606 
00610   inline void rescale (const string&, double);
00611 
00615   inline void rescale (double);
00616 
00621   inline void normaliseToCrossSection(const string& name = "mc");
00622 
00627   inline pair<double,double> chi2perDOF (const string&, const string&, double minfrac = .0) const;
00629 
00630 public:
00631 
00638   vector<string> channels () const;
00639 
00644   void output (ostream&, const string& name,
00645                unsigned int flags = 0,
00646                char comment = '#') const;
00648 
00649 public:
00650 
00651   /*@name Support for parallel runs */
00653 
00657   void store (const string& name);
00658 
00664   bool load (const string& name);
00665 
00671   Histogram2Ptr loadToHistogram (const string& name) const;
00672 
00689   void combine (const string& prefix, const string& name,
00690                 unsigned int numRuns, const string& dataChannel = "",
00691                 const string& mcChannel = "MC");
00692 
00696   inline CrossSection xSec () const;
00697 
00701   inline void xSec (CrossSection);
00702 
00704 
00705 public:
00706 
00712 
00718   explicit Histogram2 (vector<double> limits);
00719 
00725   explicit Histogram2 (vector<double> limits,
00726                        vector<double> data,
00727                        vector<double> dataerror);
00728 
00732   inline void operator+=(double);
00733 
00737   inline void addWeighted(double data, double weight);
00738 
00742   inline unsigned int numberOfBins() const;
00743 
00747   inline void normaliseToData();
00748 
00754   inline void chiSquared(double & chisq, 
00755                          unsigned int & ndegrees,
00756                          double minfrac=0.) const;
00757 
00762   Histogram2 ratioWith(const Histogram2& h2) const;
00763 
00765 
00766 public:
00767 
00774   void persistentOutput(PersistentOStream & os) const;
00775 
00781   void persistentInput(PersistentIStream & is, int version);
00783 
00790   static void Init();
00791 
00792 protected:
00793 
00800   inline virtual IBPtr clone() const;
00801 
00806   inline virtual IBPtr fullclone() const;
00808 
00809 // If needed, insert declarations of virtual function defined in the
00810 // InterfacedBase class here (using ThePEG-interfaced-decl in Emacs).
00811 
00812 private:
00813 
00818   vector<pair<double,double> > _binning;
00819 
00824   pair<double,double> _range;
00825 
00830   map<double, unsigned int> _binhash;
00831 
00835   map<string,HistogramChannel> _channels;
00836 
00841   CrossSection _xSec;
00842 
00847   static ClassDescription<Histogram2> initHistogram2;
00848 
00853   Histogram2 & operator=(const Histogram2 &);
00854 
00855 };
00856 
00857 }
00858 
00859 #include "ThePEG/Utilities/ClassTraits.h"
00860 
00861 namespace ThePEG {
00862 
00867 template <>
00868 struct BaseClassTrait<Analysis2::Histogram2,1> {
00870   typedef Interfaced NthBase;
00871 };
00872 
00875 template <>
00876 struct ClassTraits<Analysis2::Histogram2>
00877   : public ClassTraitsBase<Analysis2::Histogram2> {
00879   static string className() { return "Analysis2::Histogram2"; }
00887   static string library() { return "Analysis2.so"; }
00888 };
00889 
00892 }
00893 
00894 #include "Histogram2.icc"
00895 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
00896 // #include "Histogram2.tcc"
00897 #endif
00898 
00899 #endif /* Analysis2_Histogram2_H */