BCTH1Prior.h
1 #ifndef __BCTH1PRIOR__H
2 #define __BCTH1PRIOR__H
3 
14 /*
15  * Copyright (C) 2007-2018, the BAT core developer team
16  * All rights reserved.
17  *
18  * For the licensing terms see doc/COPYING.
19  * For documentation see http://mpp.mpg.de/bat
20  */
21 
22 // ---------------------------------------------------------
23 
24 #include "BCPrior.h"
25 #include <TH1.h>
26 #include <limits>
27 
28 // ---------------------------------------------------------
29 
30 class BCTH1Prior : public BCPrior
31 {
32 public:
40  BCTH1Prior(TH1& h, bool interpolate = false);
41 
46  BCTH1Prior(TH1* h, bool interpolate = false);
47 
49  BCTH1Prior(const BCTH1Prior& other);
50 
52  virtual ~BCTH1Prior();
53 
61 
63  friend void swap(BCTH1Prior& A, BCTH1Prior& B);
64 
71  virtual BCPrior* Clone() const
72  { return new BCTH1Prior(*this); }
73 
76  virtual bool IsValid() const;
77 
83  virtual double GetPrior(double x, bool normalize = false)
84  { return ((fInterpolate) ? fPriorHistogram->Interpolate(x) : fPriorHistogram->GetBinContent(fPriorHistogram->FindFixBin(x))) * ((normalize) ? exp(-fLogIntegral) : 1); }
85 
90  virtual double GetLogPrior(double x);
91 
97  virtual double GetMode(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
98 
105  virtual double GetRawMoment(unsigned n, double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
106 
112  virtual double GetIntegral(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
113 
120  virtual double GetStandardizedMoment(unsigned n, double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
121 
127  virtual double GetVariance(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
128  { double s = GetStandardDeviation(xmin, xmax); return s * s; }
129 
135  virtual double GetStandardDeviation(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
136  { (void)xmin; (void)xmax; return fPriorHistogram->GetRMS(); }
137 
143  virtual BCH1D GetBCH1D(TH1* bins, const std::string& name = "prior");
144 
150  virtual void SetInterpolate(bool interpolate)
151  { fInterpolate = interpolate; }
152 
158  virtual double GetRandomValue(double xmin, double xmax, TRandom* const R = NULL)
159  {
160  (void) xmin;
161  (void) xmax;
162  (void) R;
163  return fPriorHistogram->GetRandom();
164  }
165 
171  virtual TH1& GetHistogram()
172  { return *fPriorHistogram; }
173 
174  virtual const TH1& GetHistogram() const
175  { return *fPriorHistogram; }
176 
177  virtual bool GetInterpolate()
178  { return fInterpolate; }
179 
186  void NormalizeHistogram();
187 
190 protected:
191 
192  // We don't accept nullptr and used a reference up to bat 1.0-rc1
193  // but unfortunately, TH1& operator=(const TH1&) is declared private
194  // at least up root 5.34/30 so we cannot change it in the swap function, hence we need to use a pointer
196 
198 };
199 
200 #endif
TH1 * fPriorHistogram
Definition: BCTH1Prior.h:195
A class to represent the prior of a parameter.
Definition: BCPrior.h:49
virtual double GetVariance(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get variance of prior.
Definition: BCTH1Prior.h:127
virtual double GetPrior(double x, bool normalize=false)
Get prior.
Definition: BCTH1Prior.h:83
virtual double GetLogPrior(double x)
Get log of prior.
Definition: BCTH1Prior.cxx:92
virtual double GetStandardizedMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get standardised moment of prior distrubion.
Definition: BCTH1Prior.cxx:121
virtual BCPrior * Clone() const
Clone function.
Definition: BCTH1Prior.h:71
virtual double GetIntegral(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get integral of prior.
Definition: BCTH1Prior.cxx:135
BCTH1Prior(TH1 &h, bool interpolate=false)
Constructor.
Definition: BCTH1Prior.cxx:15
virtual BCH1D GetBCH1D(TH1 *bins, const std::string &name="prior")
Get BCH1D object for prior.
Definition: BCTH1Prior.cxx:154
void NormalizeHistogram()
Normalize the histogram holding the prior.
Definition: BCTH1Prior.cxx:79
virtual double GetMode(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Return mode of prior (in range).
Definition: BCTH1Prior.cxx:103
BCTH1Prior & operator=(BCTH1Prior rhs)
assignment operator
Definition: BCTH1Prior.cxx:48
virtual ~BCTH1Prior()
Destructor.
Definition: BCTH1Prior.cxx:42
virtual bool IsValid() const
Definition: BCTH1Prior.cxx:63
friend void swap(BCTH1Prior &A, BCTH1Prior &B)
swap
Definition: BCTH1Prior.cxx:55
A class to represent the prior of a parameter by a TH1.
Definition: BCTH1Prior.h:30
A class for handling 1D distributions.
Definition: BCH1D.h:34
bool fInterpolate
whether to interpolate values of hist for prior function
Definition: BCTH1Prior.h:197
virtual double GetRawMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get raw moment of prior distrubion.
Definition: BCTH1Prior.cxx:109
double fLogIntegral
Log of integral of unnormalized pdf over the range.
Definition: BCPrior.h:293
virtual double GetRandomValue(double xmin, double xmax, TRandom *const R=NULL)
Definition: BCTH1Prior.h:158
virtual double GetStandardDeviation(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get standard deviation of prior.
Definition: BCTH1Prior.h:135