Logo ROOT   6.10/09
Reference Guide
RooHistConstraint.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * *
4  * This code was autogenerated by RooClassFactory *
5  *****************************************************************************/
6 
7 /** \class RooHistConstraint
8  \ingroup Roofit
9 
10 **/
11 
12 #include "Riostream.h"
13 
14 #include "RooHistConstraint.h"
15 #include "RooAbsReal.h"
16 #include "RooAbsCategory.h"
17 #include "RooParamHistFunc.h"
18 #include "RooRealVar.h"
19 #include <math.h>
20 #include "TMath.h"
21 
22 using namespace std;
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 
28 RooHistConstraint::RooHistConstraint(const char *name, const char *title, const RooArgSet& phfSet, Int_t threshold) :
29  RooAbsPdf(name,title),
30  _gamma("gamma","gamma",this),
31  _nominal("nominal","nominal",this),
32  _nominalErr("nominalErr","nominalErr",this),
33  _relParam(kTRUE)
34 {
35  // Implementing constraint on sum of RooParamHists
36  //
37  // Step 1 - Create new gamma parameters for sum
38  // Step 2 - Replace entries in gamma listproxy of components with new sum components
39  // Step 3 - Implement constraints in terms of gamma sum parameters
40 
41 
42  if (phfSet.getSize()==1) {
43 
44  RooParamHistFunc* phf = dynamic_cast<RooParamHistFunc*>(phfSet.first()) ;
45 
46  if (!phf) {
47  coutE(InputArguments) << "RooHistConstraint::ctor(" << GetName()
48  << ") ERROR: input object must be a RooParamHistFunc" << endl ;
49  throw std::string("RoohistConstraint::ctor ERROR incongruent input arguments") ;
50  }
51 
52  // Now populate nominal with parameters
53  RooArgSet allVars ;
54  for (Int_t i=0 ; i<phf->_dh.numEntries() ; i++) {
55  phf->_dh.get(i) ;
56  if (phf->_dh.weight()<threshold) {
57  const char* vname = Form("%s_nominal_bin_%i",GetName(),i) ;
58  RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;
59  var->setVal(phf->_dh.weight()) ;
60  var->setConstant(kTRUE) ;
61  allVars.add(*var) ;
62  _nominal.add(*var) ;
63  RooRealVar* gam = (RooRealVar*) phf->_p.at(i) ;
64  if (var->getVal()>0) {
65  gam->setConstant(kFALSE) ;
66  }
67  _gamma.add(*gam) ;
68  }
69  }
70  addOwnedComponents(allVars) ;
71 
72  return ;
73  }
74 
75 
76 
77  RooFIter phiter = phfSet.fwdIterator() ;
78  RooAbsArg* arg ;
79  Int_t nbins(-1) ;
80  vector<RooParamHistFunc*> phvec ;
81  RooArgSet gammaSet ;
82  string bin0_name ;
83  while((arg=phiter.next())) {
84 
85  RooParamHistFunc* phfComp = dynamic_cast<RooParamHistFunc*>(arg) ;
86  if (phfComp) {
87  phvec.push_back(phfComp) ;
88  if (nbins==-1) {
89  nbins = phfComp->_p.getSize() ;
90  bin0_name = phfComp->_p.at(0)->GetName() ;
91  gammaSet.add(phfComp->_p) ;
92  } else {
93  if (phfComp->_p.getSize()!=nbins) {
94  coutE(InputArguments) << "RooHistConstraint::ctor(" << GetName()
95  << ") ERROR: incongruent input arguments: all input RooParamHistFuncs should have same #bins" << endl ;
96  throw std::string("RoohistConstraint::ctor ERROR incongruent input arguments") ;
97  }
98  if (bin0_name != phfComp->_p.at(0)->GetName()) {
99  coutE(InputArguments) << "RooHistConstraint::ctor(" << GetName()
100  << ") ERROR: incongruent input arguments: all input RooParamHistFuncs should have the same bin parameters" << endl ;
101  throw std::string("RoohistConstraint::ctor ERROR incongruent input arguments") ;
102  }
103 
104  }
105  } else {
106  coutW(InputArguments) << "RooHistConstraint::ctor(" << GetName()
107  << ") WARNING: ignoring input argument " << arg->GetName() << " which is not of type RooParamHistFunc" << endl;
108  }
109  }
110 
111  _gamma.add(gammaSet) ;
112 
113  // Now populate nominal and nominalErr with parameters
114  RooArgSet allVars ;
115  for (Int_t i=0 ; i<nbins ; i++) {
116 
117  Double_t sumVal(0) ;
118  for (vector<RooParamHistFunc*>::iterator iter = phvec.begin() ; iter != phvec.end() ; ++iter) {
119  sumVal += (*iter)->getNominal(i) ;
120  }
121 
122  if (sumVal<threshold) {
123 
124  const char* vname = Form("%s_nominal_bin_%i",GetName(),i) ;
125  RooRealVar* var = new RooRealVar(vname,vname,0,1000) ;
126 
127  Double_t sumVal2(0) ;
128  for (vector<RooParamHistFunc*>::iterator iter = phvec.begin() ; iter != phvec.end() ; ++iter) {
129  sumVal2 += (*iter)->getNominal(i) ;
130  }
131  var->setVal(sumVal2) ;
132  var->setConstant(kTRUE) ;
133 
134  vname = Form("%s_nominal_error_bin_%i",GetName(),i) ;
135  RooRealVar* vare = new RooRealVar(vname,vname,0,1000) ;
136 
137  Double_t sumErr2(0) ;
138  for (vector<RooParamHistFunc*>::iterator iter = phvec.begin() ; iter != phvec.end() ; ++iter) {
139  sumErr2 += pow((*iter)->getNominalError(i),2) ;
140  }
141  vare->setVal(sqrt(sumErr2)) ;
142  vare->setConstant(kTRUE) ;
143 
144  allVars.add(RooArgSet(*var,*vare)) ;
145  _nominal.add(*var) ;
146  _nominalErr.add(*vare) ;
147 
148  ((RooRealVar*)_gamma.at(i))->setConstant(kFALSE) ;
149 
150  }
151  }
152  addOwnedComponents(allVars) ;
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 
158  RooAbsPdf(other,name),
159  _gamma("gamma",this,other._gamma),
160  _nominal("nominal",this,other._nominal),
161  _nominalErr("nominalErr",this,other._nominalErr),
162  _relParam(other._relParam)
163  {
164  }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 
169  {
170  Double_t prod(1) ;
171  RooFIter niter = _nominal.fwdIterator() ;
172  RooFIter giter = _gamma.fwdIterator() ;
173  RooAbsReal *gam, *nom ;
174  while ((gam = (RooAbsReal*) giter.next())) {
175  nom = (RooAbsReal*) niter.next() ;
176  Double_t gamVal = gam->getVal() ;
177  if (_relParam) gamVal *= nom->getVal() ;
178  Double_t pois = TMath::Poisson(nom->getVal(),gamVal) ;
179  prod *= pois ;
180  }
181  return prod ;
182  }
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 
187 {
188  Double_t sum(0) ;
189  RooFIter niter = _nominal.fwdIterator() ;
190  RooFIter giter = _gamma.fwdIterator() ;
191  RooAbsReal *gamv, *nomv ;
192  while ((gamv = (RooAbsReal*) giter.next())) {
193  nomv = (RooAbsReal*) niter.next() ;
194  Double_t gam = gamv->getVal() ;
195  Int_t nom = (Int_t) nomv->getVal() ;
196  if (_relParam) gam *= nom ;
197  if (gam>0) {
198  Double_t logPoisson = nom*log(gam) - gam - logSum(nom) ;
199  sum += logPoisson ;
200  } else if (nom>0) {
201  cout << "ERROR gam=0 and nom>0" << endl ;
202  }
203  }
204  return sum ;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 
210 {
211  static Double_t* _lut = 0 ;
212  if (!_lut) {
213  _lut = new Double_t[5000] ;
214  for (Int_t ii=0 ; ii<5000 ; ii++) _lut[ii] = 0 ;
215 
216  for (Int_t j=1 ; j<=5000 ; j++) {
217  Double_t logj = log((Double_t)j) ;
218  for (Int_t ii=j ; ii<=5000 ; ii++) {
219  _lut[ii-1] += logj ;
220  }
221  }
222  }
223 
224  if (i<5000) {
225  return _lut[i-1] ;
226  } else {
227  Double_t ret = _lut[4999] ;
228  cout << "logSum i=" << i << endl ;
229  for (Int_t j=5000 ; j<=i ; j++) {
230  ret += log((Double_t)j) ;
231  }
232  return ret ;
233  }
234 
235 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:145
static long int sum(long int i)
Definition: Factory.cxx:2162
#define coutE(a)
Definition: RooMsgService.h:34
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:86
RooListProxy _nominal
virtual const RooArgSet * get() const
Definition: RooDataHist.h:77
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
int Int_t
Definition: RtypesCore.h:41
int nbins[3]
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:33
Double_t getLogVal(const RooArgSet *set=0) const
Return the log of the current value with given normalization An error message is printed if the argum...
double sqrt(double)
Double_t evaluate() const
double pow(double, double)
virtual Double_t weight() const
Definition: RooDataHist.h:96
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void setVal(Double_t value)
Set value of variable to &#39;value&#39;.
Definition: RooRealVar.cxx:205
double gamma(double x)
Int_t getSize() const
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
void setConstant(Bool_t value=kTRUE)
char * Form(const char *fmt,...)
Double_t logSum(Int_t i) const
Double_t Poisson(Double_t x, Double_t par)
Compute the Poisson distribution function for (x,par) The Poisson PDF is implemented by means of Eule...
Definition: TMath.cxx:571
RooAbsArg * next()
const Bool_t kFALSE
Definition: RtypesCore.h:92
#define ClassImp(name)
Definition: Rtypes.h:336
virtual Int_t numEntries() const
Return the number of bins.
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooFIter fwdIterator() const
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: RtypesCore.h:91
double log(double)