laura is hosted by Hepforge, IPPP Durham
Laura++  v1r0
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauMagPhaseCPCoeffSet.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2011 - 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 <fstream>
17 #include <vector>
18 using std::cout;
19 using std::cerr;
20 using std::endl;
21 
22 #include "TMath.h"
23 #include "TRandom.h"
24 
25 #include "LauMagPhaseCPCoeffSet.hh"
26 #include "LauComplex.hh"
27 #include "LauConstants.hh"
28 #include "LauParameter.hh"
29 #include "LauPrint.hh"
30 #include "LauRandom.hh"
31 
32 ClassImp(LauMagPhaseCPCoeffSet)
33 
34 
35 LauMagPhaseCPCoeffSet::LauMagPhaseCPCoeffSet(const TString& compName, Double_t mag, Double_t phase, Double_t magBar, Double_t phaseBar,
36  Bool_t magFixed, Bool_t phaseFixed,Bool_t magBarFixed, Bool_t phaseBarFixed) :
37  LauAbsCoeffSet(compName),
38  minMag_(-40.0),
39  maxMag_(+40.0),
40  minPhase_(-LauConstants::threePi),
41  maxPhase_(+LauConstants::threePi),
42  mag_(new LauParameter("Mag", mag, minMag_, maxMag_, magFixed)),
43  phase_(new LauParameter("Phase", phase, minPhase_, maxPhase_, phaseFixed)),
44  magBar_(new LauParameter("MagBar", magBar, minMag_, maxMag_, magBarFixed)),
45  phaseBar_(new LauParameter("PhaseBar", phaseBar, minPhase_, maxPhase_, phaseBarFixed)),
46  acp_("ACP", (magBar*magBar - mag*mag)/(magBar*magBar + mag*mag), -1.0, 1.0)
47 {
48  // Print message
49  cout<<"Set component \""<<this->name()<<"\" to have mag = "<<mag_->value()<<",\tphase = "<<phase_->value()<<",\t";
50  cout<<"magBar = "<<magBar_->value()<<",\tphaseBar = "<<phaseBar_->value()<<"."<<endl;
51 
52 }
53 
55 {
56  minMag_ = rhs.minMag_;
57  maxMag_ = rhs.maxMag_;
58  minPhase_ = rhs.minPhase_;
59  maxPhase_ = rhs.maxPhase_;
60  mag_ = rhs.mag_->createClone(constFactor);
61  phase_ = rhs.phase_->createClone(constFactor);
62  magBar_ = rhs.magBar_->createClone(constFactor);
63  phaseBar_ = rhs.phaseBar_->createClone(constFactor);
64  acp_ = rhs.acp_;
65 }
66 
68 {
69  if (&rhs == this) {
70  return *this;
71  }
72  this->name(rhs.name());
73  minMag_ = rhs.minMag_;
74  maxMag_ = rhs.maxMag_;
75  minPhase_ = rhs.minPhase_;
76  maxPhase_ = rhs.maxPhase_;
77  mag_ = rhs.mag_->createClone();
78  phase_ = rhs.phase_->createClone();
79  magBar_ = rhs.magBar_->createClone();
81  acp_ = rhs.acp_;
82  return *this;
83 }
84 
85 std::vector<LauParameter*> LauMagPhaseCPCoeffSet::getParameters()
86 {
87  std::vector<LauParameter*> pars;
88  pars.push_back(mag_);
89  pars.push_back(phase_);
90  pars.push_back(magBar_);
91  pars.push_back(phaseBar_);
92  return pars;
93 }
94 
95 void LauMagPhaseCPCoeffSet::printTableHeading(std::ostream& stream)
96 {
97  stream<<"\\begin{tabular}{|l|c|c|c|c|}"<<endl;
98  stream<<"\\hline"<<endl;
99  stream<<"Component & Magnitude & Phase & Magnitude_bar & Phase_bar \\\\"<<endl;
100  stream<<"\\hline"<<endl;
101 }
102 
103 void LauMagPhaseCPCoeffSet::printTableRow(std::ostream& stream)
104 {
105  LauPrint print;
106  TString resName = this->name();
107  resName = resName.ReplaceAll("_", "\\_");
108  stream<<resName<<" & $";
109  print.printFormat(stream, mag_->value());
110  stream<<" \\pm ";
111  print.printFormat(stream, mag_->error());
112  stream<<"$ & $";
113  print.printFormat(stream, phase_->value());
114  stream<<" \\pm ";
115  print.printFormat(stream, phase_->error());
116  stream<<"$ & $";
117  print.printFormat(stream, magBar_->value());
118  stream<<" \\pm ";
119  print.printFormat(stream, magBar_->error());
120  stream<<"$ & $";
121  print.printFormat(stream, phaseBar_->value());
122  stream<<" \\pm ";
123  print.printFormat(stream, phaseBar_->error());
124  stream<<"$ \\\\"<<endl;
125 }
126 
128 {
129  if (mag_->fixed() == kFALSE) {
130  // Choose a value for "magnitude" between 0.0 and 2.0
131  Double_t value = LauRandom::zeroSeedRandom()->Rndm()*2.0;
132  mag_->initValue(value); mag_->value(value);
133  }
134  if (phase_->fixed() == kFALSE) {
135  // Choose a phase between +- pi
137  phase_->initValue(value); phase_->value(value);
138  }
139  if (magBar_->fixed() == kFALSE) {
140  // Choose a value for "magnitude" between 0.0 and 2.0
141  Double_t value = LauRandom::zeroSeedRandom()->Rndm()*2.0;
142  magBar_->initValue(value); magBar_->value(value);
143  }
144  if (phaseBar_->fixed() == kFALSE) {
145  // Choose a phase between +- pi
147  phaseBar_->initValue(value); phaseBar_->value(value);
148  }
149 }
150 
152 {
153  // retrieve the current values from the parameters
154  Double_t mVal= mag_->value();
155  Double_t pVal= phase_->value();
156  Double_t mBarVal= magBar_->value();
157  Double_t pBarVal= phaseBar_->value();
158  Double_t genPhase = phase_->genValue();
159  Double_t genPhaseBar = phaseBar_->genValue();
160 
161  // Check whether we have a negative magnitude.
162  // If so make it positive and add pi to the phase.
163  if (mVal < 0.0) {
164  mVal *= -1.0;
165  pVal += LauConstants::pi;
166  }
167  if (mBarVal < 0.0) {
168  mBarVal *= -1.0;
169  pBarVal += LauConstants::pi;
170  }
171 
172  // Check now whether the phases lies in the right range (-pi to pi).
173  Bool_t pWithinRange(kFALSE);
174  Bool_t pBarWithinRange(kFALSE);
175  while (pWithinRange == kFALSE && pBarWithinRange == kFALSE) {
176  if (pVal > -LauConstants::pi && pVal < LauConstants::pi) {
177  pWithinRange = kTRUE;
178  } else {
179  // Not within the specified range
180  if (pVal > LauConstants::pi) {
181  pVal -= LauConstants::twoPi;
182  } else if (pVal < -LauConstants::pi) {
183  pVal += LauConstants::twoPi;
184  }
185  }
186 
187  if (pBarVal > -LauConstants::pi && pBarVal < LauConstants::pi) {
188  pBarWithinRange = kTRUE;
189  } else {
190  // Not within the specified range
191  if (pBarVal > LauConstants::pi) {
192  pBarVal -= LauConstants::twoPi;
193  } else if (pBarVal < -LauConstants::pi) {
194  pBarVal += LauConstants::twoPi;
195  }
196  }
197  }
198 
199  // A further problem can occur when the generated phase is close to -pi or pi.
200  // The phase can wrap over to the other end of the scale -
201  // this leads to artificially large pulls so we wrap it back.
202  Double_t diff = pVal - genPhase;
203  if (diff > LauConstants::pi) {
204  pVal -= LauConstants::twoPi;
205  } else if (diff < -LauConstants::pi) {
206  pVal += LauConstants::twoPi;
207  }
208 
209  diff = pBarVal - genPhaseBar;
210  if (diff > LauConstants::pi) {
211  pBarVal -= LauConstants::twoPi;
212  } else if (diff < -LauConstants::pi) {
213  pBarVal += LauConstants::twoPi;
214  }
215 
216  // finally store the new values in the parameters
217  // and update the pulls
218  mag_->value(mVal); mag_->updatePull();
219  phase_->value(pVal); phase_->updatePull();
220  magBar_->value(mBarVal); magBar_->updatePull();
221  phaseBar_->value(pBarVal); phaseBar_->updatePull();
222 
223 
224 }
225 
227 {
228  return LauComplex( mag_->value()*TMath::Cos(phase_->value()), mag_->value()*TMath::Sin(phase_->value()) );
229 }
230 
232 {
233  return LauComplex( magBar_->value()*TMath::Cos(phaseBar_->value()), magBar_->value()*TMath::Sin(phaseBar_->value()) );
234 }
235 
236 void LauMagPhaseCPCoeffSet::setCoeffValues( const LauComplex& coeff, const LauComplex& coeffBar )
237 {
238  mag_->value( coeff.abs() );
239  phase_->value( coeff.arg() );
240 
241  magBar_->value( coeffBar.abs() );
242  phaseBar_->value( coeffBar.arg() );
243 }
244 
246 {
247  // set the name
248  TString parName(this->baseName()); parName += "_ACP";
249  acp_.name(parName);
250 
251  // work out the ACP value
252 
253  Double_t value(-99.0);
254  value = (magBar_->value()*magBar_->value() - mag_->value()*mag_->value())/(magBar_->value()*magBar_->value() + mag_->value()*mag_->value());
255 
256  // is it fixed?
257  Bool_t fixed = magBar_->fixed() && mag_->fixed();
258  acp_.fixed(fixed);
259 
260  // we can't work out the error without the covariance matrix
261  Double_t error(0.0);
262 
263  // set the value and error
264  acp_.valueAndErrors(value,error);
265 
266  return acp_;
267 }
268 
269 LauAbsCoeffSet* LauMagPhaseCPCoeffSet::createClone(const TString& newName, Double_t constFactor)
270 {
271  LauAbsCoeffSet* clone = new LauMagPhaseCPCoeffSet( *this, constFactor );
272  clone->name( newName );
273  return clone;
274 }
275 
Bool_t fixed() const
Check whether the parameter is fixed or floated.
TRandom * zeroSeedRandom()
Access the singleton random number generator with seed set from machine clock time (within +-1 sec)...
Definition: LauRandom.cc:30
virtual void finaliseValues()
Make sure values are in &quot;standard&quot; ranges, e.g. phases should be between -pi and pi.
LauParameter acp_
The CP asymmetry.
virtual LauComplex particleCoeff()
Retrieve the complex coefficient for a particle.
virtual void randomiseInitValues()
Randomise the starting values of the parameters for a fit.
const Double_t twoPi
Two times Pi.
Definition: LauConstants.hh:93
File containing declaration of LauMagPhaseCPCoeffSet class.
virtual LauAbsCoeffSet * createClone(const TString &newName, Double_t constFactor=1.0)
Create a clone of the coefficient set.
virtual TString baseName() const
Retrieve the base name of the coefficient set.
LauParameter * phase_
The phase for particles.
const TString & name() const
The parameter name.
virtual LauComplex antiparticleCoeff()
Retrieve the complex coefficient for an antiparticle.
Double_t minMag_
The minimum allowed value for magnitudes.
File containing declaration of LauPrint class.
virtual std::vector< LauParameter * > getParameters()
Retrieve the parameters of the coefficient, e.g. so that they can be loaded into a fit...
Double_t maxPhase_
The maximum allowed value for phases.
LauMagPhaseCPCoeffSet & operator=(const LauMagPhaseCPCoeffSet &rhs)
Copy assignment operator.
Class to define various output print commands.
Definition: LauPrint.hh:29
Class for defining a complex coefficient using seperate magnitudes and phases for particles and antip...
Double_t maxMag_
The maximum allowed value for magnitudes.
Bool_t clone() const
Check whether is a clone or not.
const Double_t threePi
Three times Pi.
Definition: LauConstants.hh:95
virtual void printTableRow(std::ostream &stream)
Print the parameters of the complex coefficient as a row in the results table.
File containing declaration of LauParameter class.
virtual void setCoeffValues(const LauComplex &coeff, const LauComplex &coeffBar)
Set the parameters based on the complex coefficients for particles and antiparticles.
File containing declaration of LauComplex class.
Double_t error() const
The error on the parameter.
const Double_t pi
Pi.
Definition: LauConstants.hh:89
Class for defining the abstract interface for complex coefficient classes.
Class for defining the fit parameter objects.
Definition: LauParameter.hh:31
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.
virtual LauParameter acp()
Calculate the CP asymmetry.
LauParameter * magBar_
The magnitude for antiparticles.
File containing LauRandom namespace.
Double_t initValue() const
The initial value of the parameter.
File containing LauConstants namespace.
void printFormat(std::ostream &stream, Double_t value) const
Method to choose the printing format to a specified level of precision.
Definition: LauPrint.cc:32
Class for defining a complex number.
Definition: LauComplex.hh:47
void updatePull()
Call to update the bias and pull values.
Double_t minPhase_
The minimum allowed value for phases.
LauParameter * mag_
The magnitude for particles.
Double_t arg() const
Obtain the phase angle of the complex number.
Definition: LauComplex.hh:238
LauParameter * createClone(Double_t constFactor=1.0)
Method to create a clone from the parent parameter using the copy constructor.
virtual TString name() const
Retrieve the name of the coefficient set.
LauMagPhaseCPCoeffSet(const TString &compName, Double_t mag, Double_t phase, Double_t magBar, Double_t phaseBar, Bool_t magFixed, Bool_t phaseFixed, Bool_t magBarFixed, Bool_t phaseBarFixed)
Constructor.
virtual void printTableHeading(std::ostream &stream)
Print the column headings for a results table.
Double_t value() const
The value of the parameter.
Double_t abs() const
Obtain the absolute value of the complex number.
Definition: LauComplex.hh:220
LauParameter * phaseBar_
The phase for antiparticles.
Double_t genValue() const
The value generated for the parameter.