BCTH1Prior.cxx
1 /*
2  * Copyright (C) 2007-2016, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 #include "BCTH1Prior.h"
10 #include <config.h>
11 
12 #include <cmath>
13 
14 // ---------------------------------------------------------
15 BCTH1Prior::BCTH1Prior(TH1& h, bool interpolate)
16  : BCPrior(),
17  fPriorHistogram(static_cast<TH1*>(h.Clone())),
18  fInterpolate(interpolate)
19 {
21 }
22 
23 // ---------------------------------------------------------
24 BCTH1Prior::BCTH1Prior(TH1* h, bool interpolate)
25  : BCPrior(),
26  fPriorHistogram(static_cast<TH1*>((TH1*)h->Clone())),
27  fInterpolate(interpolate)
28 {
30 }
31 
32 // ---------------------------------------------------------
34  : BCPrior(other),
35  fPriorHistogram(static_cast<TH1*>(other.fPriorHistogram->Clone())),
37 {
39 }
40 
41 // ---------------------------------------------------------
43 {
44  delete fPriorHistogram;
45 }
46 
47 // ---------------------------------------------------------
49 {
50  swap(*this, rhs);
51  return *this;
52 }
53 
54 // ---------------------------------------------------------
56 {
57  swap(static_cast<BCPrior&>(A), static_cast<BCPrior&>(B));
58  std::swap(A.fPriorHistogram, B.fPriorHistogram);
59  std::swap(A.fInterpolate, B.fInterpolate);
60 }
61 
62 // ---------------------------------------------------------
63 bool BCTH1Prior::IsValid() const
64 {
65  if (fPriorHistogram->GetDimension() != 1)
66  return false;
67  double integral = 0;
68  for (int i = 1; i <= fPriorHistogram->GetNbinsX(); ++i) {
69  if (fPriorHistogram->GetBinContent(i) < 0)
70  return false;
71  integral += fPriorHistogram->GetBinContent(i);
72  }
73  if (integral == 0)
74  return false;
75  return true;
76 }
77 
78 // ---------------------------------------------------------
80 {
81  double integral = 0;
82  if (fInterpolate)
83  integral = GetFunction().Integral(fPriorHistogram->GetXaxis()->GetXmin(), fPriorHistogram->GetXaxis()->GetXmax());
84  else
85  integral = fPriorHistogram->Integral("width");
86 
87  if (integral != 0)
88  fPriorHistogram->Scale(1. / integral);
89 }
90 
91 // ---------------------------------------------------------
92 double BCTH1Prior::GetLogPrior(double x)
93 {
94  double p = GetPrior(x, false);
95  if (p > 0)
96  return log(p);
97  if (p == 0)
98  return -std::numeric_limits<double>::infinity();
99  return std::numeric_limits<double>::quiet_NaN();
100 }
101 
102 // ---------------------------------------------------------
103 double BCTH1Prior::GetMode(double /*xmin*/, double /*xmax*/)
104 {
105  return fPriorHistogram->GetXaxis()->GetBinCenter(fPriorHistogram->GetMaximumBin());
106 }
107 
108 // ---------------------------------------------------------
109 double BCTH1Prior::GetRawMoment(unsigned n, double xmin, double xmax)
110 {
111  if (n == 0)
112  return 0;
113  if (n == 1)
114  return fPriorHistogram->GetMean();
115  if (n == 2)
116  return fPriorHistogram->GetMean() * fPriorHistogram->GetMean() + fPriorHistogram->GetRMS() * fPriorHistogram->GetRMS();
117  return BCPrior::GetRawMoment(n, xmin, xmax);
118 }
119 
120 // ---------------------------------------------------------
121 double BCTH1Prior::GetStandardizedMoment(unsigned n, double xmin, double xmax)
122 {
123  if (n == 0)
124  return 0;
125  if (n == 1)
126  return 1;
127  if (n == 2)
128  return GetSkewness(xmin, xmax);
129  if (n == 3)
130  return GetKurtosis(xmin, xmax);
131  return BCPrior::GetStandardizedMoment(n, xmin, xmax);
132 }
133 
134 // ---------------------------------------------------------
135 double BCTH1Prior::GetIntegral(double xmin, double xmax)
136 {
137  xmin = std::max(xmin, fPriorHistogram->GetXaxis()->GetXmin());
138  xmax = std::min(xmax, fPriorHistogram->GetXaxis()->GetXmax());
139 
140  // if interpolating, use built in function
141  if (fInterpolate)
142  return const_cast<TF1*>(&GetFunction())->Integral(xmin, xmax);
143 
144  // else calculate directly from histogram
145  int bmin = fPriorHistogram->FindFixBin(xmin);
146  int bmax = fPriorHistogram->FindFixBin(xmax);
147  double I = fPriorHistogram->Integral(bmin, bmax, "width");
148  I -= fPriorHistogram->GetBinContent(xmin) * (xmin - fPriorHistogram->GetXaxis()->GetBinLowEdge(bmin));
149  I -= fPriorHistogram->GetBinContent(xmax) * (fPriorHistogram->GetXaxis()->GetBinUpEdge(bmax) - xmax);
150  return I;
151 }
152 
153 // ---------------------------------------------------------
154 BCH1D BCTH1Prior::GetBCH1D(TH1* bins, const std::string& name)
155 {
156  // if not interpolating, use actual histogram binning
157  if (!fInterpolate) {
158  std::vector<double> bin_edges(fPriorHistogram->GetNbinsX() + 1, 0);
159  fPriorHistogram->GetXaxis()->GetLowEdge(&bin_edges[0]);
160  bin_edges.back() = fPriorHistogram->GetXaxis()->GetXmax();
161  bins->SetBins(bin_edges.size() - 1, &bin_edges[0]);
162  }
163  return BCPrior::GetBCH1D(bins, name);
164 }
TH1 * fPriorHistogram
Definition: BCTH1Prior.h:195
A class to represent the prior of a parameter.
Definition: BCPrior.h:49
virtual double GetKurtosis(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get kurtosis of prior.
Definition: BCPrior.h:205
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 TF1 & GetFunction()
Return back ROOT TF1 evaluating BCPrior::GetPrior.
Definition: BCPrior.h:118
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 GetSkewness(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get skewness of prior.
Definition: BCPrior.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
virtual BCH1D GetBCH1D(TH1 *bins, const std::string &name="prior")
Get BCH1D object for prior.
Definition: BCPrior.cxx:159
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: BCPrior.cxx:112
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: BCPrior.cxx:72