laura is hosted by Hepforge, IPPP Durham
Laura++  v1r0
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauParameter.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2006 - 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Authors:
7 // Thomas Latham
8 // John Back
9 // Paul Harrison
10 
15 #include <iostream>
16 #include <map>
17 using std::cout;
18 using std::cerr;
19 using std::endl;
20 using std::map;
21 
22 #include "TRandom.h"
23 
24 #include "LauParameter.hh"
25 #include "LauRandom.hh"
26 
27 ClassImp(LauParameter)
28 
29 
31  name_(""),
32  value_(0.0),
33  error_(0.0),
34  negError_(0.0),
35  posError_(0.0),
36  genValue_(0.0),
37  initValue_(0.0),
38  minValue_(0.0),
39  maxValue_(0.0),
40  fixed_(kTRUE),
41  firstStage_(kFALSE),
42  secondStage_(kFALSE),
43  bias_(0.0),
44  pull_(0.0),
45  clone_(kFALSE),
46  parent_(0)
47 {
48 }
49 
50 LauParameter::LauParameter(const TString& parName) :
51  name_(parName),
52  value_(0.0),
53  error_(0.0),
54  negError_(0.0),
55  posError_(0.0),
56  genValue_(0.0),
57  initValue_(0.0),
58  minValue_(0.0),
59  maxValue_(0.0),
60  fixed_(kTRUE),
61  firstStage_(kFALSE),
62  secondStage_(kFALSE),
63  bias_(0.0),
64  pull_(0.0),
65  clone_(kFALSE),
66  parent_(0)
67 {
68 }
69 
70 LauParameter::LauParameter(Double_t parValue) :
71  name_(""),
72  value_(parValue),
73  error_(0.0),
74  negError_(0.0),
75  posError_(0.0),
76  genValue_(parValue),
77  initValue_(parValue),
78  minValue_(parValue-1e-6),
79  maxValue_(parValue+1e-6),
80  fixed_(kTRUE),
81  firstStage_(kFALSE),
82  secondStage_(kFALSE),
83  bias_(0.0),
84  pull_(0.0),
85  clone_(kFALSE),
86  parent_(0)
87 {
88 }
89 
90 LauParameter::LauParameter(const TString& parName, Double_t parValue) :
91  name_(parName),
92  value_(parValue),
93  error_(0.0),
94  negError_(0.0),
95  posError_(0.0),
96  genValue_(parValue),
97  initValue_(parValue),
98  minValue_(parValue-1e-6),
99  maxValue_(parValue+1e-6),
100  fixed_(kTRUE),
101  firstStage_(kFALSE),
102  secondStage_(kFALSE),
103  bias_(0.0),
104  pull_(0.0),
105  clone_(kFALSE),
106  parent_(0)
107 {
108 }
109 
110 LauParameter::LauParameter(Double_t parValue, Double_t min, Double_t max) :
111  name_(""),
112  value_(parValue),
113  error_(0.0),
114  negError_(0.0),
115  posError_(0.0),
116  genValue_(parValue),
117  initValue_(parValue),
118  minValue_(min),
119  maxValue_(max),
120  fixed_(kTRUE),
121  firstStage_(kFALSE),
122  secondStage_(kFALSE),
123  bias_(0.0),
124  pull_(0.0),
125  clone_(kFALSE),
126  parent_(0)
127 {
128  this->checkRange();
129 }
130 
131 LauParameter::LauParameter(Double_t parValue, Double_t parError, Double_t min, Double_t max) :
132  name_(""),
133  value_(parValue),
134  error_(parError),
135  negError_(0.0),
136  posError_(0.0),
137  genValue_(parValue),
138  initValue_(parValue),
139  minValue_(min),
140  maxValue_(max),
141  fixed_(kTRUE),
142  firstStage_(kFALSE),
143  secondStage_(kFALSE),
144  bias_(0.0),
145  pull_(0.0),
146  clone_(kFALSE),
147  parent_(0)
148 {
149  this->checkRange();
150 }
151 
152 LauParameter::LauParameter(Double_t parValue, Double_t min, Double_t max, Bool_t parFixed) :
153  name_(""),
154  value_(parValue),
155  error_(0.0),
156  negError_(0.0),
157  posError_(0.0),
158  genValue_(parValue),
159  initValue_(parValue),
160  minValue_(min),
161  maxValue_(max),
162  fixed_(parFixed),
163  firstStage_(kFALSE),
164  secondStage_(kFALSE),
165  bias_(0.0),
166  pull_(0.0),
167  clone_(kFALSE),
168  parent_(0)
169 {
170  this->checkRange();
171 }
172 
173 LauParameter::LauParameter(const TString& parName, Double_t parValue, Double_t min, Double_t max) :
174  name_(parName),
175  value_(parValue),
176  error_(0.0),
177  negError_(0.0),
178  posError_(0.0),
179  genValue_(parValue),
180  initValue_(parValue),
181  minValue_(min),
182  maxValue_(max),
183  fixed_(kTRUE),
184  firstStage_(kFALSE),
185  secondStage_(kFALSE),
186  bias_(0.0),
187  pull_(0.0),
188  clone_(kFALSE),
189  parent_(0)
190 {
191  this->checkRange();
192 }
193 
194 LauParameter::LauParameter(const TString& parName, Double_t parValue, Double_t min, Double_t max, Bool_t parFixed) :
195  name_(parName),
196  value_(parValue),
197  error_(0.0),
198  negError_(0.0),
199  posError_(0.0),
200  genValue_(parValue),
201  initValue_(parValue),
202  minValue_(min),
203  maxValue_(max),
204  fixed_(parFixed),
205  firstStage_(kFALSE),
206  secondStage_(kFALSE),
207  bias_(0.0),
208  pull_(0.0),
209  clone_(kFALSE),
210  parent_(0)
211 {
212  this->checkRange();
213 }
214 
215 LauParameter::LauParameter(const TString& parName, Double_t parValue, Double_t parError, Double_t min, Double_t max) :
216  name_(parName),
217  value_(parValue),
218  error_(parError),
219  negError_(0.0),
220  posError_(0.0),
221  genValue_(parValue),
222  initValue_(parValue),
223  minValue_(min),
224  maxValue_(max),
225  fixed_(kTRUE),
226  firstStage_(kFALSE),
227  secondStage_(kFALSE),
228  bias_(0.0),
229  pull_(0.0),
230  clone_(kFALSE),
231  parent_(0)
232 {
233  this->checkRange();
234 }
235 
237 {
238  this->name(rhs.name());
239  this->valueAndRange(rhs.value(), rhs.minValue(), rhs.maxValue());
240  this->genValue(rhs.genValue());
241  this->initValue(rhs.initValue());
242  this->fixed(rhs.fixed());
243  this->firstStage(rhs.firstStage());
244  this->secondStage(rhs.secondStage());
245  this->errors(rhs.error(), rhs.negError(), rhs.posError());
246  this->clone(rhs.parent());
247  clones_ = rhs.clones_;
248 }
249 
251 {
252  if (&rhs == this) {
253  return *this;
254  }
255  this->name(rhs.name());
256  this->valueAndRange(rhs.value(), rhs.minValue(), rhs.maxValue());
257  this->genValue(rhs.genValue());
258  this->initValue(rhs.initValue());
259  this->fixed(rhs.fixed());
260  this->firstStage(rhs.firstStage());
261  this->secondStage(rhs.secondStage());
262  this->errors(rhs.error(), rhs.negError(), rhs.posError());
263  this->clone(rhs.parent());
264  clones_ = rhs.clones_;
265  return *this;
266 }
267 
268 void LauParameter::value(Double_t newValue)
269 {
270  this->checkRange(newValue,this->minValue(),this->maxValue());
271  if (!this->clone()) {
272  this->updateClones(kTRUE);
273  }
274 }
275 
276 void LauParameter::error(Double_t newError)
277 {
278  error_ = TMath::Abs(newError);
279  if (!this->clone()) {
280  this->updateClones();
281  }
282 }
283 
284 void LauParameter::negError(Double_t newNegError)
285 {
286  negError_ = TMath::Abs(newNegError);
287  if (!this->clone()) {
288  this->updateClones();
289  }
290 }
291 
292 void LauParameter::posError(Double_t newPosError)
293 {
294  posError_ = TMath::Abs(newPosError);
295  if (!this->clone()) {
296  this->updateClones();
297  }
298 }
299 
300 void LauParameter::errors(Double_t newError, Double_t newNegError, Double_t newPosError)
301 {
302  error_ = TMath::Abs(newError);
303  negError_ = TMath::Abs(newNegError);
304  posError_ = TMath::Abs(newPosError);
305  if (!this->clone()) {
306  this->updateClones();
307  }
308 }
309 
310 void LauParameter::valueAndErrors(Double_t newValue, Double_t newError, Double_t newNegError, Double_t newPosError)
311 {
312  this->checkRange(newValue,this->minValue(),this->maxValue());
313  error_ = TMath::Abs(newError);
314  negError_ = TMath::Abs(newNegError);
315  posError_ = TMath::Abs(newPosError);
316  if (!this->clone()) {
317  this->updateClones();
318  }
319 }
320 
321 void LauParameter::genValue(Double_t newGenValue)
322 {
323  genValue_ = newGenValue;
324  if (!this->clone()) {
325  this->updateClones();
326  }
327 }
328 
329 void LauParameter::initValue(Double_t newInitValue)
330 {
331  initValue_ = newInitValue;
332  if (!this->clone()) {
333  this->updateClones();
334  }
335 }
336 
337 void LauParameter::minValue(Double_t newMinValue)
338 {
339  this->checkRange(this->value(),newMinValue,this->maxValue());
340  if (!this->clone()) {
341  this->updateClones();
342  }
343 }
344 
345 void LauParameter::maxValue(Double_t newMaxValue)
346 {
347  this->checkRange(this->value(),this->minValue(),newMaxValue);
348  if (!this->clone()) {
349  this->updateClones();
350  }
351 }
352 
353 void LauParameter::range(Double_t newMinValue, Double_t newMaxValue)
354 {
355  this->checkRange(this->value(),newMinValue,newMaxValue);
356  if (!this->clone()) {
357  this->updateClones();
358  }
359 }
360 
361 void LauParameter::valueAndRange(Double_t newValue, Double_t newMinValue, Double_t newMaxValue)
362 {
363  this->checkRange(newValue,newMinValue,newMaxValue);
364  if (!this->clone()) {
365  this->updateClones();
366  }
367 }
368 
369 void LauParameter::name(const TString& newName)
370 {
371  // no need to update clones here
372  // clones are allowed to have different names
373  name_ = newName;
374 }
375 
376 void LauParameter::fixed(Bool_t parFixed)
377 {
378  fixed_ = parFixed;
379  if (!this->clone()) {
380  this->updateClones();
381  }
382 }
383 
384 void LauParameter::firstStage(Bool_t firstStagePar)
385 {
386  firstStage_ = firstStagePar;
387  if (!this->clone()) {
388  this->updateClones();
389  }
390 }
391 
392 void LauParameter::secondStage(Bool_t secondStagePar)
393 {
394  secondStage_ = secondStagePar;
395  if (!this->clone()) {
396  this->updateClones();
397  }
398 }
399 
401 {
402  this->checkRange(val,this->minValue(),this->maxValue());
403  if (!this->clone()) {
404  this->updateClones(kTRUE);
405  }
406  return *this;
407 }
408 
410 {
411  this->checkRange(this->value()+val,this->minValue(),this->maxValue());
412  if (!this->clone()) {
413  this->updateClones(kTRUE);
414  }
415  return *this;
416 }
417 
419 {
420  this->checkRange(this->value()-val,this->minValue(),this->maxValue());
421  if (!this->clone()) {
422  this->updateClones(kTRUE);
423  }
424  return *this;
425 }
426 
428 {
429  this->checkRange(this->value()*val,this->minValue(),this->maxValue());
430  if (!this->clone()) {
431  this->updateClones(kTRUE);
432  }
433  return *this;
434 }
435 
437 {
438  this->checkRange(this->value()/val,this->minValue(),this->maxValue());
439  if (!this->clone()) {
440  this->updateClones(kTRUE);
441  }
442  return *this;
443 }
444 
446 {
447  // calculate the bias
448  bias_ = value_ - genValue_;
449 
450  // if we have errors calculated then calculate
451  // the pull using the best error available
452  if ((bias_ > 0.0) && (negError_ > 1e-10)) {
453  pull_ = bias_ / negError_;
454  } else if ((bias_ < 0.0) && (posError_ > 1e-10)) {
455  pull_ = bias_ / posError_;
456  } else if (error_ > 1e-10) {
457  pull_ = bias_ / error_;
458  } else {
459  pull_ = 0.0;
460  }
461 }
462 
463 void LauParameter::checkRange(Double_t val, Double_t minVal, Double_t maxVal)
464 {
465  // first check that min is less than max (or they are the same - this is allowed)
466  if (minVal > maxVal) {
467  cerr<<"ERROR in LauParameter::checkRange : minValue: "<<minVal<<" greater than maxValue: "<<maxVal<<endl;
468  if (minValue_ > maxValue_) {
470  cerr<<" : Setting both to "<<maxValue_<<"."<<endl;
471  } else {
472  cerr<<" : Not changing anything."<<endl;
473  }
474  return;
475  }
476 
477  minValue_ = minVal;
478  maxValue_ = maxVal;
479 
480  // now check that the value is still within the range
481  if ((val < minVal) || (val > maxVal)) {
482  if (name_ != "") {
483  cerr<<"ERROR in LauParameter::checkRange : value: "<<val<<" not in allowed range ["<<minVal<<" , "<<maxVal<<"] for parameter \""<<name_<<"\"."<<endl;
484  } else {
485  cerr<<"ERROR in LauParameter::checkRange : value: "<<val<<" not in allowed range ["<<minVal<<" , "<<maxVal<<"]."<<endl;
486  }
487  if (val < minVal) {
488  cerr<<" : Setting value to minValue: "<<minVal<<endl;
489  val = minVal;
490  } else {
491  cerr<<" : Setting value to maxValue: "<<maxVal<<endl;
492  val = maxVal;
493  }
494  }
495  value_ = val;
496 }
497 
499 {
500  // if we're a clone we mustn't be cloned ourselves
501  // but instead return another clone of our parent
502  // (this is so that the parent knows of all its clones)
503  if (this->clone()) {
504  LauParameter* clonePar = parent_->createClone(constFactor);
505  clonePar->name(this->name());
506  return clonePar;
507  }
508 
509  // clone ourselves using the copy-constructor
510  LauParameter* clonePar = new LauParameter(*this);
511  (*clonePar) *= constFactor;
512  clonePar->wipeClones();
513  clonePar->clone(this);
514  clones_.insert( std::make_pair( clonePar, constFactor ) );
515  return clonePar;
516 }
517 
518 LauParameter* LauParameter::createClone(const TString& newName, Double_t constFactor)
519 {
520  // self message to create the clone
521  LauParameter* clonePar = this->createClone(constFactor);
522  // set the new name
523  clonePar->name(newName);
524  // and return
525  return clonePar;
526 }
527 
528 void LauParameter::updateClones(Bool_t justValue)
529 {
530  if (justValue) {
531  for (map<LauParameter*,Double_t>::iterator iter = clones_.begin(); iter != clones_.end(); ++iter) {
532  LauParameter* clonePar = iter->first;
533  Double_t constFactor = iter->second;
534  clonePar->value(constFactor*this->value());
535  }
536  } else {
537  for (map<LauParameter*,Double_t>::iterator iter = clones_.begin(); iter != clones_.end(); ++iter) {
538  LauParameter* clonePar = iter->first;
539  Double_t constFactor = iter->second;
540  clonePar->valueAndRange(constFactor*this->value(),constFactor*this->minValue(),constFactor*this->maxValue());
541  clonePar->errors(constFactor*this->error(),constFactor*this->negError(),constFactor*this->posError());
542  clonePar->genValue(constFactor*this->genValue());
543  clonePar->initValue(constFactor*this->initValue());
544  clonePar->fixed(this->fixed());
545  clonePar->firstStage(this->firstStage());
546  clonePar->secondStage(this->secondStage());
547  }
548  }
549 }
550 
552 {
553  this->randomiseValue(this->minValue(), this->maxValue());
554 }
555 
556 void LauParameter::randomiseValue(Double_t minVal, Double_t maxVal)
557 {
558  // if we're fixed then do nothing
559  if (this->fixed()) {
560  return;
561  }
562  // check supplied values are sensible
563  if (maxVal < minVal) {
564  cerr<<"ERROR in LauParameter::randomiseValue : Supplied maximum value smaller than minimum value."<<endl;
565  return;
566  }
567  if (maxVal > this->maxValue()) {
568  maxVal = this->maxValue();
569  }
570  if (minVal < this->minValue()) {
571  minVal = this->minValue();
572  }
573 
574  Double_t randNo = LauRandom::zeroSeedRandom()->Rndm();
575  Double_t val = randNo*(maxVal - minVal) + minVal;
576  this->initValue(val);
577 }
578 
579 // various mathematical operators (non-member functions)
580 Double_t operator + (const LauParameter& lhs, Double_t rhs)
581 {
582  return (lhs.value() + rhs);
583 }
584 Double_t operator + (Double_t lhs, const LauParameter& rhs)
585 {
586  return (lhs + rhs.value());
587 }
588 Double_t operator + (const LauParameter& lhs, const LauParameter& rhs)
589 {
590  return (lhs.value() + rhs.value());
591 }
592 Double_t operator - (const LauParameter& lhs, Double_t rhs)
593 {
594  return (lhs.value() - rhs);
595 }
596 Double_t operator - (Double_t lhs, const LauParameter& rhs)
597 {
598  return (lhs - rhs.value());
599 }
600 Double_t operator - (const LauParameter& lhs, const LauParameter& rhs)
601 {
602  return (lhs.value() - rhs.value());
603 }
604 Double_t operator * (const LauParameter& lhs, Double_t rhs)
605 {
606  return (lhs.value() * rhs);
607 }
608 Double_t operator * (Double_t lhs, const LauParameter& rhs)
609 {
610  return (lhs * rhs.value());
611 }
612 Double_t operator * (const LauParameter& lhs, const LauParameter& rhs)
613 {
614  return (lhs.value() * rhs.value());
615 }
616 Double_t operator / (const LauParameter& lhs, Double_t rhs)
617 {
618  return (lhs.value() / rhs);
619 }
620 Double_t operator / (Double_t lhs, const LauParameter& rhs)
621 {
622  return (lhs / rhs.value());
623 }
624 Double_t operator / (const LauParameter& lhs, const LauParameter& rhs)
625 {
626  return (lhs.value() / rhs.value());
627 }
628 Double_t operator += (Double_t& lhs, const LauParameter& rhs)
629 {
630  lhs += rhs.value();
631  return lhs;
632 }
633 Double_t operator -= (Double_t& lhs, const LauParameter& rhs)
634 {
635  lhs -= rhs.value();
636  return lhs;
637 }
638 Double_t operator *= (Double_t& lhs, const LauParameter& rhs)
639 {
640  lhs *= rhs.value();
641  return lhs;
642 }
643 Double_t operator /= (Double_t& lhs, const LauParameter& rhs)
644 {
645  lhs /= rhs.value();
646  return lhs;
647 }
648 
649 // ostream operator
650 ostream& operator << (ostream& stream, const LauParameter& par)
651 {
652  stream << par.value();
653  return stream;
654 }
655 
Double_t range() const
The range allowed for the parameter.
Double_t genValue_
Toy generation value.
Bool_t fixed() const
Check whether the parameter is fixed or floated.
Double_t operator+=(Double_t &lhs, const LauParameter &rhs)
std::map< LauParameter *, Double_t > clones_
The clones of this parameter.
Double_t operator*=(Double_t &lhs, const LauParameter &rhs)
TRandom * zeroSeedRandom()
Access the singleton random number generator with seed set from machine clock time (within +-1 sec)...
Definition: LauRandom.cc:30
Double_t maxValue() const
The maximum value allowed for the parameter.
LauParameter()
Default constructor.
Definition: LauParameter.cc:30
const TString & name() const
The parameter name.
Double_t operator/(const LauParameter &lhs, Double_t rhs)
LauParameter * parent_
The parent parameter.
Double_t negError() const
The lower error on the parameter.
Double_t minValue() const
The minimum value allowed for the parameter.
Double_t maxValue_
Maximum value for the parameter.
LauParameter & operator*=(Double_t val)
Multiplication assignment operator.
Bool_t firstStage_
Flag whether it is floated only in the first stage of the fit.
Double_t operator-=(Double_t &lhs, const LauParameter &rhs)
void valueAndRange(Double_t newValue, Double_t newMinValue, Double_t newMaxValue)
Set the value and range for the parameter.
void updateClones(Bool_t justValue=kFALSE)
Method to update clone values.
Double_t operator/=(Double_t &lhs, const LauParameter &rhs)
Double_t bias_
Parameter bias.
Double_t operator*(const LauParameter &lhs, Double_t rhs)
Double_t posError() const
The upper error on the parameter.
Double_t error_
The error on the parameter.
Bool_t clone() const
Check whether is a clone or not.
void checkRange()
Method to check whether value provided is whithin the range and that the minimum and maximum limits m...
Bool_t firstStage() const
Check whether the parameter should be floated only in the first stage of a two stage fit...
std::ostream & operator<<(std::ostream &os, const LauComplex &z)
Definition: LauComplex.cc:43
Double_t minValue_
Minimum value for the parameter.
File containing declaration of LauParameter class.
Double_t pull_
Parameter pull.
Double_t negError_
The lower error on the parameter.
Bool_t secondStage() const
Check whether the parameter should be floated only in the second stage of a two stage fit...
Double_t error() const
The error on the parameter.
LauParameter & operator/=(Double_t val)
Division assignment operator.
Class for defining the fit parameter objects.
Definition: LauParameter.hh:31
void randomiseValue()
Randomise the value of the parameter (if it is floating).
void valueAndErrors(Double_t newValue, Double_t newError, Double_t newNegError=0.0, Double_t newPosError=0.0)
Set the value and errors on the parameter.
Double_t posError_
The upper error on the parameter.
Double_t operator+(const LauParameter &lhs, Double_t rhs)
LauParameter * parent() const
The parent parameter.
File containing LauRandom namespace.
TString name_
The parameter name.
LauParameter & operator-=(Double_t val)
Subtraction assignment operator.
Double_t initValue() const
The initial value of the parameter.
Bool_t secondStage_
Flag whether it is floated only in the second stage of the fit.
Double_t value_
The parameter value.
Bool_t clone_
Flag whether the parameter is a clone.
void updatePull()
Call to update the bias and pull values.
LauParameter * createClone(Double_t constFactor=1.0)
Method to create a clone from the parent parameter using the copy constructor.
Bool_t fixed_
Fix/float option for parameter.
Double_t value() const
The value of the parameter.
void wipeClones()
Method to clear the clone parameters.
Double_t operator-(const LauParameter &lhs, Double_t rhs)
Double_t genValue() const
The value generated for the parameter.
void errors(Double_t newError, Double_t newNegError, Double_t newPosError)
Set the error values on the parameter.
Double_t initValue_
Initial fit value.
LauParameter & operator=(const LauParameter &rhs)
Copy assignment operator.
LauParameter & operator+=(Double_t val)
Addition assignment operator.