BCVariable.cxx
1 /*
2  * Copyright (C) 2007-2013, 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 // ---------------------------------------------------------
10 #include "BCVariable.h"
11 #include "BCLog.h"
12 #include "BCAux.h"
13 
14 #include <TString.h>
15 #include <TH1D.h>
16 #include <TH2D.h>
17 #include <TH3D.h>
18 #include <TRandom.h>
19 
20 #include <limits>
21 
22 // ---------------------------------------------------------
24  fPrefix("Variable"),
25  fLowerLimit(-std::numeric_limits<double>::infinity()),
26  fUpperLimit(+std::numeric_limits<double>::infinity()),
27  fPrecision(4),
28  fFillH1(true),
29  fFillH2(true),
30  fNbins(100)
31 {
32  SetName("parameter");
33 }
34 
35 // ---------------------------------------------------------
36 BCVariable::BCVariable(const std::string& name, double lowerlimit, double upperlimit, const std::string& latexname, const std::string& unitstring) :
37  fPrefix("Variable"),
38  fLowerLimit(-std::numeric_limits<double>::infinity()),
39  fUpperLimit(+std::numeric_limits<double>::infinity()),
40  fPrecision(3),
41  fLatexName(latexname),
42  fUnitString(unitstring),
43  fFillH1(true),
44  fFillH2(true),
45  fNbins(100)
46 {
47  SetName(name);
48  SetLimits(lowerlimit, upperlimit);
49 }
50 
51 // ---------------------------------------------------------
52 void BCVariable::SetName(const std::string& name)
53 {
54  fName = name;
55  fSafeName = BCAux::SafeName(name);
56 }
57 
58 // ---------------------------------------------------------
59 void BCVariable::SetLimits(double lowerlimit, double upperlimit)
60 {
61  fLowerLimit = lowerlimit;
62  fUpperLimit = upperlimit;
64  BCLog::OutError(Form("BCVariable:SetLimits : lower limit (%f) is greater than upper limit (%f) for variable %s", fLowerLimit, fUpperLimit, fName.data()));
67 }
68 
69 // ---------------------------------------------------------
71 {
72  // need some extra digits to see where things become insignificant
73  unsigned new_precision = 4 + ceil(-log10(fabs(fUpperLimit - fLowerLimit) / std::max(fabs(fUpperLimit), fabs(fLowerLimit))));
74  if (force || new_precision > GetPrecision())
75  SetPrecision(new_precision);
76 }
77 // ---------------------------------------------------------
78 bool BCVariable::IsAtLimit(double value) const
79 {
80  if ((value - fLowerLimit) * (value - fLowerLimit) / fLowerLimit / fLowerLimit <= 1e-10
81  ||
82  (value - fUpperLimit) * (value - fUpperLimit) / fUpperLimit / fUpperLimit <= 1e-10)
83  return true;
84 
85  return false;
86 }
87 
88 // ---------------------------------------------------------
90 {
91  BCLog::OutSummary(fPrefix + " summary:");
92  BCLog::OutSummary(Form("%11s : %s", fPrefix.c_str(), fName.c_str()));
93  BCLog::OutSummary(Form("Lower limit : % .*f", fPrecision, fLowerLimit));
94  BCLog::OutSummary(Form("Upper limit : % .*f", fPrecision, fUpperLimit));
95 }
96 
97 // ---------------------------------------------------------
98 std::string BCVariable::OneLineSummary(bool print_prefix, int name_length) const
99 {
100  if (name_length < 0)
101  name_length = fName.size();
102  if (print_prefix)
103  return std::string(Form("%s \"%*s\" : [%.*g, %.*g]", fPrefix.data(), name_length, fName.data(), fPrecision, fLowerLimit, fPrecision, fUpperLimit));
104  else
105  return std::string(Form("%-*s : [%.*g, %.*g]", name_length, fName.data(), fPrecision, fLowerLimit, fPrecision, fUpperLimit));
106 }
107 
108 // ---------------------------------------------------------
109 std::string BCVariable::H1Title() const
110 {
111  return std::string(";") + GetLatexNameWithUnits() + ";P(" + GetLatexName() + " | Data)";
112 }
113 
114 // ---------------------------------------------------------
115 std::string BCVariable::H2Title(const BCVariable& ordinate) const
116 {
117  return std::string(";") + GetLatexNameWithUnits() +
118  ";" + ordinate.GetLatexNameWithUnits() +
119  ";P(" + GetLatexName() + ", " + ordinate.GetLatexName() + " | Data)";
120 }
121 
122 // ---------------------------------------------------------
123 std::string BCVariable::H3Title(const BCVariable& ordinate_y, const BCVariable& ordinate_z) const
124 {
125  return std::string(";") + GetLatexNameWithUnits() +
126  ";" + ordinate_y.GetLatexNameWithUnits() +
127  ";" + ordinate_z.GetLatexNameWithUnits() +
128  ";P(" + GetLatexName() + ", " + ordinate_y.GetLatexName() + ", " + ordinate_z.GetLatexName() + " | Data)";
129 }
130 
131 // ---------------------------------------------------------
132 TH1* BCVariable::CreateH1(const std::string& name) const
133 {
135  return new TH1D(name.data(), H1Title().data(),
137 }
138 
139 // ---------------------------------------------------------
140 TH2* BCVariable::CreateH2(const std::string& name, const BCVariable& ordinate) const
141 {
143  return new TH2D(name.data(), H2Title(ordinate).data(),
145  ordinate.GetNbins(), ordinate.GetLowerLimit(), ordinate.GetUpperLimit());
146 }
147 
148 // ---------------------------------------------------------
149 TH3* BCVariable::CreateH3(const std::string& name, const BCVariable& ordinate_y, const BCVariable& ordinate_z) const
150 {
152  return new TH3D(name.data(), H3Title(ordinate_y, ordinate_z).data(),
154  ordinate_y.GetNbins(), ordinate_y.GetLowerLimit(), ordinate_y.GetUpperLimit(),
155  ordinate_z.GetNbins(), ordinate_z.GetLowerLimit(), ordinate_z.GetUpperLimit());
156 }
157 
158 // ---------------------------------------------------------
159 double BCVariable::GetUniformRandomValue(TRandom* const R) const
160 {
161  if (!R)
162  return std::numeric_limits<double>::quiet_NaN();
163  return ValueFromPositionInRange(R->Rndm());
164 }
std::string fSafeName
Safe name of the variable for use in ROOT object naming.
Definition: BCVariable.h:323
virtual TH1 * CreateH1(const std::string &name) const
Creates a 1D Histogram for this variable.
Definition: BCVariable.cxx:132
std::string fUnitString
Unit string for variable.
Definition: BCVariable.h:338
virtual std::string OneLineSummary(bool print_prefix=true, int name_length=-1) const
Definition: BCVariable.cxx:98
unsigned fPrecision
Necessary precision for output.
Definition: BCVariable.h:332
virtual std::string H2Title(const BCVariable &ordinate) const
Definition: BCVariable.cxx:115
A guard object to prevent ROOT from taking over ownership of TNamed objects.
Definition: BCAux.h:171
double fUpperLimit
The upper limit of the variable value.
Definition: BCVariable.h:329
virtual std::string H1Title() const
Definition: BCVariable.cxx:109
A class representing a variable of a model.
Definition: BCVariable.h:35
BCAux::BCRange RangeType(double xmin, double xmax)
Return type of range as a BCAux::BCRange enum.
Definition: BCAux.cxx:85
unsigned fNbins
The number of equal-size bins used in histograms involving this variable.
Definition: BCVariable.h:347
virtual double GetLowerLimit() const
Definition: BCVariable.h:97
BCVariable()
The default constructor.
Definition: BCVariable.cxx:23
virtual std::string H3Title(const BCVariable &ordinate_y, const BCVariable &ordinate_z) const
Definition: BCVariable.cxx:123
std::string SafeName(const std::string &name)
Convert a name into a safe name for use in ROOT object naming.
Definition: BCAux.cxx:111
double fLowerLimit
The lower limit of the variable value.
Definition: BCVariable.h:326
virtual void SetName(const std::string &name)
Definition: BCVariable.cxx:52
virtual void CalculatePrecision(bool force=false)
Calculate the necessary precision for outputting this parameter and replace current precision is smal...
Definition: BCVariable.cxx:70
virtual const std::string & GetLatexName() const
Definition: BCVariable.h:82
virtual double GetUpperLimit() const
Definition: BCVariable.h:102
bool fFillH1
Flag to store MCMC samples in 1D histogram.
Definition: BCVariable.h:341
virtual bool IsAtLimit(double value) const
Definition: BCVariable.cxx:78
virtual void SetLimits(double lowerlimit=0, double upperlimit=1)
Set the limits of the variable values.
Definition: BCVariable.cxx:59
std::string fLatexName
The latex name of the variable.
Definition: BCVariable.h:335
virtual std::string GetLatexNameWithUnits() const
Definition: BCVariable.h:92
std::string fName
The name of the variable.
Definition: BCVariable.h:320
virtual unsigned GetNbins() const
Definition: BCVariable.h:134
std::string fPrefix
prefix for output
Definition: BCVariable.h:317
lower < upper, lower and upper limits finite
Definition: BCAux.h:89
virtual void SetPrecision(unsigned precision)
Set the precision of the output of variable.
Definition: BCVariable.h:177
virtual double ValueFromPositionInRange(double p) const
Translate from unit interval to value in variable range.
Definition: BCVariable.h:235
virtual double GetUniformRandomValue(TRandom *const R) const
Get random value uniformly distributed in range.
Definition: BCVariable.cxx:159
virtual void PrintSummary() const
Prints a variable summary on the screen.
Definition: BCVariable.cxx:89
bool fFillH2
Flag to store MCMC samples in 2D histograms.
Definition: BCVariable.h:344
virtual unsigned GetPrecision() const
Definition: BCVariable.h:119