MINT2
Neg2LLMultiConstraint.h
Go to the documentation of this file.
1 #ifndef NEG_TWO_LL_MultiConstraint_HH
2 #define NEG_TWO_LL_MultiConstraint_HH
3 // author: Philippe d'Argent (p.dargent@cern.ch)
4 
5 #include "TMath.h"
6 #include "Mint/Minimisable.h"
8 #include "Mint/NamedParameter.h"
9 #include "RooMultiVarGaussian.h"
10 #include "RooRealVar.h"
11 #include "RooRealConstant.h"
12 #include "RooDataSet.h"
13 #include "TDecompChol.h"
14 #include <TROOT.h>
15 #include "TRandom3.h"
16 #include <vector>
17 
18 using namespace std;
19 using namespace RooFit ;
20 
21 namespace MINT{
22 
24  protected:
25  RooArgList* _x, *_mu;
26  RooMultiVarGaussian* _gauss_cov;
27  TMatrixTSym<double>* _cov;
29  TMatrixD* _UT;
30 
31  public:
32  Neg2LLMultiConstraint(MinuitParameterSet* mps=0, string label = "")
33  : Minimisable(mps), _mps(mps) {
34 
36  NamedParameter<string> constrain(("ConstrainMulti"+label).c_str(), (string)"" , (char*) 0);
37  string constrain_s = constrain;
38  vector<string> constrain_v = split(constrain_s, " ");
39 
41  NamedParameter<string> corr(("ConstrainMulti"+label+"_corr").c_str(),(string)"", (char*) 0);
42  string corr_s = corr;
43  vector<double> corr_v = splitValues(corr_s, " ");
44 
45  TMatrixDSym cov(constrain_v.size());
46  int index = 0;
47  for(int i=0; i < cov.GetNcols(); i++){
48  for(int j=i; j < cov.GetNcols(); j++){
49  cov(i,j) = corr_v[index];
50  cov(j,i) = corr_v[index];
51  index++;
52  }
53  }
54 
55  //double* corr_a = &corr_v[0];
56  //TMatrixDSym cov(constrain_v.size(),corr_a);
57  cov.Print();
58 
60  if(constrain_v.size() * (constrain_v.size()+1)/2 != corr_v.size()) {
61  cout << "ERROR in Neg2LLMultiConstraint::Inconsistent number of parameters and cov. matrix" << endl;
62  throw "ERROR";
63  }
64 
65  _x = new RooArgList();
66  _mu = new RooArgList();
67  vector<double> sigma_v;
68 
69  for(int j= 0; j < constrain_v.size();j++){
70 
71  cout << "Adding gauss constraint for parameter: " << constrain_v[j] << endl;
72 
73  double mean = -99999;
74  double sigma = -99999;
75 
76  for(unsigned int i=0; i < _mps->size(); i++){
77  if(0 == _mps->getParPtr(i)) continue;
78  if(constrain_v[j] == _mps->getParPtr(i)->name()){
79  mean = _mps->getParPtr(i)->mean();
80  sigma = ((FitParameter*)_mps->getParPtr(i))->stepInit();
81  }
82  }
83  if(mean == -99999) {
84  cout << "ERROR in Neg2LLMultiConstraint::Parameter not found" << endl;
85  throw "ERROR";
86  }
87  else {
88  RooRealVar* x = new RooRealVar((constrain_v[j]).c_str(), (constrain_v[j]).c_str(),mean);
89  _x->add(*x);
90  _mu->add(RooRealConstant::value(mean));
91  sigma_v.push_back(sigma);
92  }
93  }
94 
95  for(int i=0; i < cov.GetNcols(); i++){
96  for(int j=0; j < cov.GetNcols(); j++){
97  cov(i,j) = cov(i,j) * sigma_v[i] * sigma_v[j];
98  }
99  }
100  _cov = new TMatrixDSym(cov);
101 
102  _x->Print();
103  _mu->Print();
104  _cov->Print();
105  _gauss_cov = new RooMultiVarGaussian("gauss_cov","gauss_cov",*_x, *_mu, *_cov);
106 
107  TDecompChol tdc(*_cov);
108  tdc.Decompose();
109  TMatrixD U = tdc.GetU();
110  _UT = new TMatrixD(TMatrixD::kTransposed,U);
111  _UT->Print();
112 
113  };
114 
115  virtual void beginFit(){};
116  virtual void parametersChanged(){
117  for(int i=0; i < _x->getSize(); i++){
118  ((RooRealVar*)_x->at(i))->setVal(_mps->getParPtr(_x->at(i)->GetName())->mean());
119  }
120  };
121  virtual void endFit(){};
122 
123  virtual double getVal(){
124  return -2. * log(_gauss_cov->getVal(*_x));
125  }
126 
127  virtual double getNewVal(){
128  parametersChanged();
129  return getVal();
130  }
131 
133  return _cov->GetNcols();
134  }
135 
137 
138  cout << "Smearing input values " << endl;
139 
140  RooDataSet* data_cov = _gauss_cov->generate(*_x, 1);
141  RooArgList* xvec_cov= (RooArgList*)data_cov->get(0);
142 
143  xvec_cov->Print();
144 
145  for(int i=0; i < xvec_cov->getSize(); i++){
146  _mps->getParPtr(xvec_cov->at(i)->GetName())->setCurrentFitVal(((RooRealVar*)xvec_cov->at(i))->getVal());
147  ((FitParameter*)_mps->getParPtr(xvec_cov->at(i)->GetName()))->setInit(((RooRealVar*)xvec_cov->at(i))->getVal());
148  cout << "Set parameter " << xvec_cov->at(i)->GetName() << " to " << _mps->getParPtr(xvec_cov->at(i)->GetName())->mean() << endl;
149  }
150  }
151 
152  double smearInputValuesChol(int index = 0, int seed = 0, int offset = 1){
153 
154  // Use the same random seed for each chol index
155  TRandom3 r(seed + offset);
156  cout << "Smearing input values for parameter " << index << " using random seed = " << seed + offset << endl;
157 
158  double val = 0;
159  for(int i = 0 ; i < _UT->GetNcols(); i++){
160  if(i != index)continue;
161  val = _mps->getParPtr(_x->at(i)->GetName())->mean();
162  for(int j = 0 ; j < _UT->GetNcols(); j++){
163  val += r.Gaus(0.,1.) * (*_UT)(i,j);
164  }
165  _mps->getParPtr(_x->at(i)->GetName())->setCurrentFitVal(val);
166  ((FitParameter*)_mps->getParPtr(_x->at(i)->GetName()))->setInit(val);
167  cout << "Set parameter " << _x->at(i)->GetName() << " to " << val << endl;
168  }
169  return val;
170  }
171 
172  RooDataSet* generateToys(int N = 100){
173  cout << "Smearing input values " << endl;
174  RooDataSet* data_cov = _gauss_cov->generate(*_x, N);
175  return data_cov;
176  }
177 
178  TMatrixDSym* getCovMatrix(){
179  return _cov;
180  }
181 
182  vector<string> split(string s, string delimiter){
183  size_t pos = 0;
184  string token;
185  vector<string> list;
186  while ((pos = s.find(delimiter)) != string::npos) {
187  token = s.substr(0, pos);
188  list.push_back(token);
189  s.erase(0, pos + delimiter.length());
190  }
191  return list;
192  }
193 
194  vector<double> splitValues(string s, string delimiter){
195  size_t pos = 0;
196  string token;
197  vector<double> list;
198  while ((pos = s.find(delimiter)) != string::npos) {
199  token = s.substr(0, pos);
200  list.push_back(atof(token.c_str()));
201  s.erase(0, pos + delimiter.length());
202  }
203  return list;
204  }
205 
206 
208 
209 };
210 
211 }// namespace MINT
212 #endif
213 //
double smearInputValuesChol(int index=0, int seed=0, int offset=1)
vector< double > splitValues(string s, string delimiter)
static const double s
IMinuitParameter * getParPtr(unsigned int i)
unsigned int size() const
virtual const std::string & name() const =0
Neg2LLMultiConstraint(MinuitParameterSet *mps=0, string label="")
virtual double mean() const =0
RooMultiVarGaussian * _gauss_cov
TMatrixTSym< double > * _cov
RooDataSet * generateToys(int N=100)
vector< string > split(string s, string delimiter)