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