MINT2
FitParameter.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:55 GMT
3 #include "Mint/FitParameter.h"
5 #include "TRandom3.h"
6 
7 #include <iostream>
8 using namespace std;
9 using namespace MINT;
10 
11 const char* FitParameter::_initString = " 1\t0.0\t0.0\t0.0\t0.0";
12 const char* FitParameter::getInitString(){ return _initString;}
13 
14 FitParameter::FitParameter(const std::string& name
15  , const char* fname
16  , MinuitParameterSet* setPtr
17  , FIX_OR_WHAT fow
19  )
20  : INamedParameter()
21  , NamedParameterBase(name, fname, vb)
23  // , _minPtr(0)
24  , _blinding(0)
25  , _pset(0)
26  , _psetIndex(-9999)
27  // , _pN(-1)
28  , _iFixInit((int)fow)
29  , _meanInit(0)
30  , _stepInit(0)
31  , _minInit(0)
32  , _maxInit(0)
33  , _scanParameters(name + "_Scan", fname, NamedParameterBase::QUIET)
34  , _blindingParameters(name + "_Blind", fname, NamedParameterBase::QUIET)
35 {
36  defaultInit();
38  MinuitParameterSet* ps = setPtr;
39  if(0 == ps) ps = MinuitParameterSet::getDefaultSet();
40  addToParSet(ps);
41  initToResult();
42  setupBlinding();
43  // cout << "FitParameter constructed " << this->name() << endl;
44 }
45 FitParameter::FitParameter(const std::string& name
46  , int fix
47  , double mean
48  , double step
49  , double mi
50  , double ma
51  , MinuitParameterSet* setPtr
53  , const char* fname
54  , const std::vector<double>& blindingPars
55  )
56  : INamedParameter()
57  , NamedParameterBase(name, fname, vb)
59  // , _minPtr(0)
60  , _blinding(0)
61  , _pset(0)
62  , _psetIndex(-9999)
63  // , _pN(-1)
64  , _iFixInit(fix)
65  , _meanInit(mean)
66  , _stepInit(step)
67  , _minInit(mi)
68  , _maxInit(ma)
69  , _scanParameters(name + "_Scan", 0, NamedParameterBase::QUIET)
70  , _blindingParameters(name + "_Blind", blindingPars, 0, NamedParameterBase::QUIET)
71 {
72  _gotInitialised = true;
73  setFromParsedFile();// so the above values are for initialisation but over-ruled by what's in the file
74  MinuitParameterSet* ps = setPtr;
75  if(0 == ps) ps = MinuitParameterSet::getDefaultSet();
76 
77  addToParSet(ps);
78 
79  initToResult();
80  setupBlinding();
81  //cout << "FitParameter constructed a " << this->name() << endl;
82 }
83 
84 FitParameter::FitParameter(const std::string& name
85  , MinuitParameterSet* setPtr
86  , FIX_OR_WHAT fow
88  )
89  : INamedParameter()
90  , NamedParameterBase(name, 0, vb)
92  // , _minPtr(0)
93  , _blinding(0)
94  , _pset(0)
95  , _psetIndex(-9999)
96  // , _pN(-1)
97  , _iFixInit((int) fow)
98  , _meanInit(0)
99  , _stepInit(0)
100  , _minInit(0)
101  , _maxInit(0)
102  , _scanParameters(name + "_Scan", 0, NamedParameterBase::QUIET)
103  , _blindingParameters(name + "_Blind", 0, NamedParameterBase::QUIET)
104 {
105  defaultInit();
107  MinuitParameterSet* ps = setPtr;
108  if(0 == ps) ps = MinuitParameterSet::getDefaultSet();
109  addToParSet(ps);
110  initToResult();
111  setupBlinding();
112  //cout << "FitParameter constructed b " << this->name() << endl;
113 }
114 
116  if(! _blindingParameters.gotInitialised() || _blindingParameters.size() == 0) return true;
117  int seed = (int) fabs(_blindingParameters.getVal(0));
118  double min=-1, max=1;
119 
120  if(_blindingParameters.size() > 1){
122  }
123  if(_blindingParameters.size() > 2){
125  }else{
126  max = min + 1;
127  }
128 
129  TRandom3 rnd(seed + 99);
130  _blinding = rnd.Rndm()*(max - min) + min;
131  return true;
132 
133 }
134 
135 // not so clear if I should keep copy constructor...
136 // made it protected for now.
138  : INamedParameter()
139  , NamedParameterBase(other)
140  , IMinuitParameter()
141  , _blinding(other._blinding)
142  // , _minPtr(other._minPtr)
143  , _pset(other._pset)
144  , _psetIndex(other._psetIndex)
145  // , _pN(other._pN)
146  , _iFixInit(other._iFixInit)
147  , _meanInit(other._meanInit)
148  , _stepInit(other._stepInit)
149  , _minInit(other._minInit)
150  , _maxInit(other._maxInit)
151  , _scanParameters(other._scanParameters)
152  , _blindingParameters(other._blindingParameters)
153  , _meanResult(other._meanResult)
154  , _errResult(other._errResult)
155  , _errPosResult(other._errPosResult)
156  , _errNegResult(other._errNegResult)
157 {
158  //cout << "FitParameter copy-constructed " << this->name() << endl;
159 }
160 
163 }
164 
168  _errPosResult = -9999;
169  _errNegResult = -9999;
170 }
172  // I once gave it this counter-intuitive name
173  // (the picture was: init->Result) keep it for compatibility.
174  resetToInit();
175 }
177  //cout << "FitParameter destructor destroying " << this->name() << endl;
178  if(0 != _pset) _pset->unregister(this);
179 }
180 
182  // now the same as setParSet.
183  if(0 == ps) return false;
184  setParSet(ps);
185  return true;
186 }
188  // if(0 == ps) return false;
189  if(ps == _pset) return true;
190  if(0 != _pset) _pset->unregister(this);
191  _pset = ps;
192  _psetIndex = -9999;
193  if(0 != ps) ps->add(this);
194  return true;
195 }
196 bool FitParameter::setParSetIndex(int psetIndex){
197  _psetIndex = psetIndex;
198  return true;
199 }
201  return _psetIndex;
202 }
203 
205  const std::vector<std::string>& vsl = line.parsedStrings();
206  if(vsl.size() < 3){
207  if(! _quiet) cout << "ERROR in FitParameter::setFromParsedLine"
208  << " need at least 3 parameters:\n"
209  << " iFix, initial mean"
210  << endl;
211  return false;
212  }
213  if(vsl[0] != _name){
214  if(! _quiet) std::cout << "ERROR in FitParameter::setFromParsedLine"
215  << " wrong parameter name. My name is " << _name
216  << " the line's first element is " << vsl[0]
217  << endl;
218  return false;
219  }
220  unsigned int start=0;
221  // if(vsl.size() >=7) ++start; // 1st parameter is index number
222  // above was for compatibility with Mikhail's code, not needed anymore
223 
224  if(vsl.size() > ++start) stringToAnything(vsl[start], _iFixInit);
225  if(vsl.size() > ++start) stringToAnything(vsl[start], _meanInit);
226  if(vsl.size() > ++start) stringToAnything(vsl[start], _stepInit);
227  if(vsl.size() > ++start) stringToAnything(vsl[start], _minInit);
228  if(vsl.size() > ++start) stringToAnything(vsl[start], _maxInit);
229  initToResult();
230 
231  return true;
232 }
233 
234 /*
235 const TMinuit* FitParameter::getMinuit() const{
236  return _minPtr;
237 }
238 TMinuit* FitParameter::getMinuit(){
239  return _minPtr;
240 }
241 */
242 
243 int FitParameter::iFixInit() const{ return _iFixInit;}
244 double FitParameter::meanInit() const{ return _meanInit;}
245 double FitParameter::stepInit() const{ return _stepInit;}
246 double FitParameter::minInit() const{ return _minInit;}
247 double FitParameter::maxInit() const{ return _maxInit;}
248 
249 bool FitParameter::scan() const{
251 }
252 double FitParameter::scanMin() const{
253  if(_scanParameters.size() < 2) return 0.0;
254  else return _scanParameters.getVal(0);
255 }
256 double FitParameter::scanMax() const{
257  if(_scanParameters.size() < 2) return 0.0;
258  else return _scanParameters.getVal(1);
259 }
260 
261 /*
262 int FitParameter::parNumber() const{
263  if(_pN < 0 || _pN > getMinuit()->fMaxpar){
264  cout << "ERROR IN FitParameter "
265  << name()
266  << " index out of bounds" << endl;
267  throw "index out of bounds in FitParameter";
268  }
269  return _pN;
270 }
271 */
272 
273 /*
274 void FitParameter::associate(TMinuit* tm, int parNum){
275  _minPtr = tm;
276  _pN = parNum;
277 }
278 */
279 
280 /*
281 bool FitParameter::MinuitOK() const{
282  bool OK = (0!= getMinuit());
283  if(!OK) return false;
284  OK &= (_pN >= 0);
285  if(!OK) return false;
286  OK &= (_pN < getMinuit()->fMaxpar);
287  if(!OK) return false;
288  OK &= (1 != getMinuit()->fEmpty);
289  return OK;
290 }
291 */
292 
294  return _meanResult;
295 }
296 
298  _meanResult = cfv;
299 }
300 
301 void FitParameter::setResult(double fitMean
302  , double fitErr
303  , double fitErrPos
304  , double fitErrNeg){
305  _meanResult = fitMean;
306  _errResult = fitErr;
307  _errPosResult = fitErrPos;
308  _errNegResult = fitErrNeg;
309 
310 }
311 
312 double FitParameter::mean() const{
313  return _meanResult;
314 }
315 
317  return mean() - blinding() ;
318 }
319 double FitParameter::min() const{
320  return minInit();
321 }
322 double FitParameter::max() const{
323  return maxInit();
324 }
326  return _errPosResult;
327 }
329  return _errNegResult;
330 }
331 double FitParameter::err() const{
332  return _errResult;
333 }
334 
336  // defaults to fixed - i.e. won't be varied in fit
337  // unless explicitly intitialised.
338  // Otherwise all set to zero.
340 }
341 void FitParameter::print(std::ostream& os) const{
342  // ... in a format that can be read back in
343  os << "\"" << name() << "\""
344  << "\t" << iFixInit()
345  << "\t" << blindedMean()
346  << "\t" << err()
347  << "\t" << min()
348  << "\t" << max();
349 }
350 void FitParameter::printVal(std::ostream& os) const{
351  // ... in a format that can be read back in, w/o the name
352  os << iFixInit()
353  << "\t" << blindedMean()
354  << "\t" << err()
355  << "\t" << min()
356  << "\t" << max();
357 }
358 void FitParameter::printResultVsInput(std::ostream& os) const{
359 
360  double pull = -9999;
361  if(err() != 0) pull = (blindedMean() - meanInit() )/err();
362 
363 
364  os << "\"" << name() << "\""
365  << "\t" << iFixInit()
366  << "\t" << blindedMean() << " - " << meanInit() << " / " << err()
367  << " = \t" << pull;
368 }
369 void FitParameter::printFormat(std::ostream& os, int namelength){ // static
370  std::string nameStr = "** name";
371  if(namelength > 0) nameStr.resize(namelength, ' ');
372  os << nameStr;
373  os << "\t" << "Fix?"
374  << "\t" << "init"
375  << "\t" << "step"
376  << "\t" << "min"
377  << "\t" << "max"
378  << std::endl;
379 }
380 void FitParameter::printResultFormat(std::ostream& os, int namelength){ // static
381  std::string nameStr = "** name";
382  if(namelength > 0) nameStr.resize(namelength, ' ');
383  os << nameStr;
384  os << "\t" << "Fix?"
385  << "\t" << "mean"
386  << "\t" << "err"
387  << "\t" << "min"
388  << "\t" << "max"
389  << std::endl;
390 }
391 
393  _iFixInit = 2;
394 }
396  initToResult();
397  fix();
398 }
400  _iFixInit = 1;
401 }
403  initToResult();
404  fixAndHide();
405 }
407  _iFixInit = 0;
408 }
409 
410 std::ostream& operator<<(std::ostream& os, const FitParameter& fp){
411  fp.print(os);
412  return os;
413 }
414 
415 //
virtual int parSetIndex() const
void stringToAnything(const std::string &str, T &anything)
Definition: Utils.h:56
virtual void print(std::ostream &os=std::cout) const
virtual int size() const
virtual bool setParSet(MinuitParameterSet *ps)
virtual bool setFromParsedLine(const ParsedParameterLine &line)
double scanMin() const
double meanInit() const
double maxInit() const
int iFixInit() const
NamedParameter< double > _blindingParameters
Definition: FitParameter.h:67
double blindedMean() const
static void printFormat(std::ostream &os=std::cout, int pad=0)
static MinuitParameterSet * getDefaultSet()
const std::vector< std::string > & parsedStrings() const
static void printResultFormat(std::ostream &os=std::cout, int pad=0)
double minInit() const
bool unregister(IMinuitParameter *patPtr)
virtual const std::string & name() const
Definition: FitParameter.h:144
double scanMax() const
virtual double blinding() const
Definition: FitParameter.h:80
virtual void printResultVsInput(std::ostream &os=std::cout) const
double mean() const
virtual void setCurrentFitVal(double fv)
double min() const
double max() const
bool add(IMinuitParameter *parPtr)
FitParameter(const FitParameter &other)
virtual double getCurrentFitVal() const
MinuitParameterSet * _pset
Definition: FitParameter.h:59
void setResult(double fitMean, double fitErr, double fitErrPos, double fitErrNeg)
virtual void printVal(std::ostream &os=std::cout) const
double err() const
static const char * getInitString()
bool setFromInitStringNoName(const std::string &str_in)
double stepInit() const
NamedParameter< double > _scanParameters
Definition: FitParameter.h:66
bool addToParSet(MinuitParameterSet *ps)
virtual const T & getVal(int i=0) const
virtual bool setParSetIndex(int psetIndex)
std::ostream & operator<<(std::ostream &os, const FitParameter &fp)