Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooParamHistFunc.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * *
4  * This code was autogenerated by RooClassFactory *
5  *****************************************************************************/
6 
7 /** \class RooParamHistFunc
8  * \ingroup Roofit
9  * A histogram function that assigns scale parameters to every bin. Instead of the bare bin contents,
10  * it therefore yields:
11  * \f[
12  * \gamma_{i} * \mathrm{bin}_i
13  * \f]
14  *
15  * The \f$ \gamma_i \f$ can therefore be used to parametrise statistical uncertainties of the histogram
16  * template. In conjuction with a constraint term, this can be used to implement the Barlow-Beeston method.
17  * The constraint can be implemented using RooHistConstraint.
18  *
19  * See also the tutorial rf709_BarlowBeeston.C
20  */
21 
22 #include "Riostream.h"
23 #include "RooParamHistFunc.h"
24 #include "RooAbsReal.h"
25 #include "RooAbsCategory.h"
26 #include "RooRealVar.h"
27 #include <math.h>
28 #include "TMath.h"
29 
30 using namespace std ;
31 
32 ClassImp(RooParamHistFunc);
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Populate x with observables
36 
37 RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, RooDataHist& dh, Bool_t paramRelative) :
38  RooAbsReal(name,title),
39  _x("x","x",this),
40  _p("p","p",this),
41  _dh(dh),
42  _relParam(paramRelative)
43 {
44  _x.add(*_dh.get()) ;
45 
46  // Now populate p with parameters
47  RooArgSet allVars ;
48  for (Int_t i=0 ; i<_dh.numEntries() ; i++) {
49  _dh.get(i) ;
50 
51  const char* vname = Form("%s_gamma_bin_%i",GetName(),i) ;
52  RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;
53  var->setVal(_relParam ? 1 : _dh.weight()) ;
54  var->setError(_relParam ? 1 / sqrt(_dh.weight()) : sqrt(_dh.weight()));
55  var->setConstant(kTRUE) ;
56  allVars.add(*var) ;
57  _p.add(*var) ;
58  }
59  addOwnedComponents(allVars) ;
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Populate x with observables
64 
65 RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, const RooAbsArg& /*x*/, RooDataHist& dh, Bool_t paramRelative) :
66  RooAbsReal(name,title),
67  _x("x","x",this),
68  _p("p","p",this),
69  _dh(dh),
70  _relParam(paramRelative)
71 {
72  _x.add(*_dh.get()) ;
73 
74  // Now populate p with parameters
75  RooArgSet allVars ;
76  for (Int_t i=0 ; i<_dh.numEntries() ; i++) {
77  _dh.get(i) ;
78  const char* vname = Form("%s_gamma_bin_%i",GetName(),i) ;
79  RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;
80  var->setVal(_relParam ? 1 : _dh.weight()) ;
81  var->setError(_relParam ? 1 / sqrt(_dh.weight()) : sqrt(_dh.weight()));
82  var->setConstant(kTRUE) ;
83  allVars.add(*var) ;
84  _p.add(*var) ;
85  }
86  addOwnedComponents(allVars) ;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 
91 RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, RooDataHist& dh, const RooParamHistFunc& paramSource, Bool_t paramRelative) :
92  RooAbsReal(name,title),
93  _x("x","x",this),
94  _p("p","p",this),
95  _dh(dh),
96  _relParam(paramRelative)
97 {
98  // Populate x with observables
99  _x.add(*_dh.get()) ;
100 
101  // Now populate p with existing parameters
102  _p.add(paramSource._p) ;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 
107 RooParamHistFunc::RooParamHistFunc(const RooParamHistFunc& other, const char* name) :
108  RooAbsReal(other,name),
109  _x("x",this,other._x),
110  _p("p",this,other._p),
111  _dh(other._dh),
112  _relParam(other._relParam)
113 {
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 
118 Double_t RooParamHistFunc::evaluate() const
119 {
120  Int_t idx = ((RooDataHist&)_dh).getIndex(_x,kTRUE) ;
121  Double_t ret = ((RooAbsReal*)_p.at(idx))->getVal() ;
122  if (_relParam) {
123  Double_t nom = getNominal(idx) ;
124  ret *= nom ;
125  }
126  return ret ;
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 
131 Double_t RooParamHistFunc::getActual(Int_t ibin)
132 {
133  return ((RooAbsReal&)_p[ibin]).getVal() ;
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 
138 void RooParamHistFunc::setActual(Int_t ibin, Double_t newVal)
139 {
140  ((RooRealVar&)_p[ibin]).setVal(newVal) ;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 
145 Double_t RooParamHistFunc::getNominal(Int_t ibin) const
146 {
147  _dh.get(ibin) ;
148  return _dh.weight() ;
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 
153 Double_t RooParamHistFunc::getNominalError(Int_t ibin) const
154 {
155  _dh.get(ibin) ;
156  return _dh.weightError() ;
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Return sampling hint for making curves of (projections) of this function
161 /// as the recursive division strategy of RooCurve cannot deal efficiently
162 /// with the vertical lines that occur in a non-interpolated histogram
163 
164 list<Double_t>* RooParamHistFunc::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
165 {
166  // Check that observable is in dataset, if not no hint is generated
167  RooAbsLValue* lvarg = dynamic_cast<RooAbsLValue*>(_dh.get()->find(obs.GetName())) ;
168  if (!lvarg) {
169  return 0 ;
170  }
171 
172  // Retrieve position of all bin boundaries
173  const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
174  Double_t* boundaries = binning->array() ;
175 
176  list<Double_t>* hint = new list<Double_t> ;
177 
178  // Widen range slightly
179  xlo = xlo - 0.01*(xhi-xlo) ;
180  xhi = xhi + 0.01*(xhi-xlo) ;
181 
182  Double_t delta = (xhi-xlo)*1e-8 ;
183 
184  // Construct array with pairs of points positioned epsilon to the left and
185  // right of the bin boundaries
186  for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
187  if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
188  hint->push_back(boundaries[i]-delta) ;
189  hint->push_back(boundaries[i]+delta) ;
190  }
191  }
192 
193  return hint ;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Return sampling hint for making curves of (projections) of this function
198 /// as the recursive division strategy of RooCurve cannot deal efficiently
199 /// with the vertical lines that occur in a non-interpolated histogram
200 
201 std::list<Double_t>* RooParamHistFunc::binBoundaries(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
202 {
203  // Check that observable is in dataset, if not no hint is generated
204  RooAbsLValue* lvarg = dynamic_cast<RooAbsLValue*>(_dh.get()->find(obs.GetName())) ;
205  if (!lvarg) {
206  return 0 ;
207  }
208 
209  // Retrieve position of all bin boundaries
210  const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
211  Double_t* boundaries = binning->array() ;
212 
213  list<Double_t>* hint = new list<Double_t> ;
214 
215  // Construct array with pairs of points positioned epsilon to the left and
216  // right of the bin boundaries
217  for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
218  if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
219  hint->push_back(boundaries[i]) ;
220  }
221  }
222 
223  return hint ;
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Advertise that all integrals can be handled internally.
228 
229 Int_t RooParamHistFunc::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars,
230  const RooArgSet* /*normSet*/, const char* /*rangeName*/) const
231 {
232  // Simplest scenario, integrate over all dependents
233  RooAbsCollection *allVarsCommon = allVars.selectCommon(_x) ;
234  Bool_t intAllObs = (allVarsCommon->getSize()==_x.getSize()) ;
235  delete allVarsCommon ;
236  if (intAllObs && matchArgs(allVars,analVars,_x)) {
237  return 1 ;
238  }
239 
240  return 0 ;
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Implement analytical integrations by doing appropriate weighting from component integrals
245 /// functions to integrators of components
246 
247 Double_t RooParamHistFunc::analyticalIntegralWN(Int_t code, const RooArgSet* /*normSet2*/,const char* /*rangeName*/) const
248 {
249  R__ASSERT(code==1) ;
250 
251  Double_t ret(0) ;
252  Int_t i(0) ;
253  for (const auto param : _p) {
254  auto p = static_cast<const RooAbsReal*>(param);
255  Double_t bin = p->getVal() ;
256  if (_relParam) bin *= getNominal(i++) ;
257  ret += bin ;
258  }
259 
260  // WVE fix this!!! Assume uniform binning for now!
261  Double_t binV(1) ;
262  for (const auto obs : _x) {
263  auto xx = static_cast<const RooRealVar*>(obs);
264  binV *= (xx->getMax()-xx->getMin())/xx->numBins() ;
265  }
266 
267  return ret*binV ;
268 }