laura is hosted by Hepforge, IPPP Durham
Laura++  v2r2p1
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 
19 #include "TMath.h"
20 #include "TRandom.h"
21 
22 #include "LauMagPhaseCPCoeffSet.hh"
23 #include "LauComplex.hh"
24 #include "LauConstants.hh"
25 #include "LauParameter.hh"
26 #include "LauPrint.hh"
27 #include "LauRandom.hh"
28 
30 
31 
32 LauMagPhaseCPCoeffSet::LauMagPhaseCPCoeffSet(const TString& compName, Double_t mag, Double_t phase, Double_t magBar, Double_t phaseBar,
33  Bool_t magFixed, Bool_t phaseFixed,Bool_t magBarFixed, Bool_t phaseBarFixed) :
34  LauAbsCoeffSet(compName),
35  mag_(new LauParameter("Mag", mag, minMagnitude_, maxMagnitude_, magFixed)),
36  phase_(new LauParameter("Phase", phase, minPhase_, maxPhase_, phaseFixed)),
37  magBar_(new LauParameter("MagBar", magBar, minMagnitude_, maxMagnitude_, magBarFixed)),
38  phaseBar_(new LauParameter("PhaseBar", phaseBar, minPhase_, maxPhase_, phaseBarFixed)),
39  particleCoeff_( mag*TMath::Cos(phase), mag*TMath::Sin(phase) ),
40  antiparticleCoeff_( magBar*TMath::Cos(phaseBar), magBar*TMath::Sin(phaseBar) ),
41  acp_("ACP", (magBar*magBar - mag*mag)/(magBar*magBar + mag*mag), -1.0, 1.0)
42 {
43 }
44 
46  mag_(0),
47  phase_(0),
48  magBar_(0),
49  phaseBar_(0),
50  particleCoeff_( rhs.particleCoeff_ ),
51  antiparticleCoeff_( rhs.antiparticleCoeff_ ),
52  acp_( rhs.acp_ )
53 {
54  if ( cloneOption == All || cloneOption == TieMagnitude ) {
55  mag_ = rhs.mag_->createClone(constFactor);
56  magBar_ = rhs.magBar_->createClone(constFactor);
57  } else {
58  mag_ = new LauParameter("Mag", rhs.mag_->value(), minMagnitude_, maxMagnitude_, rhs.mag_->fixed());
59  magBar_ = new LauParameter("MagBar", rhs.magBar_->value(), minMagnitude_, maxMagnitude_, rhs.magBar_->fixed());
60  }
61 
62  if ( cloneOption == All || cloneOption == TiePhase ) {
63  phase_ = rhs.phase_->createClone(constFactor);
64  phaseBar_ = rhs.phaseBar_->createClone(constFactor);
65  } else {
66  phase_ = new LauParameter("Phase", rhs.phase_->value(), minPhase_, maxPhase_, rhs.phase_->fixed());
67  phaseBar_ = new LauParameter("PhaseBar", rhs.phaseBar_->value(), minPhase_, maxPhase_, rhs.phaseBar_->fixed());
68  }
69 }
70 
71 std::vector<LauParameter*> LauMagPhaseCPCoeffSet::getParameters()
72 {
73  std::vector<LauParameter*> pars;
74  pars.push_back(mag_);
75  pars.push_back(phase_);
76  pars.push_back(magBar_);
77  pars.push_back(phaseBar_);
78  return pars;
79 }
80 
82 {
83  std::cout<<"INFO in LauMagPhaseCPCoeffSet::printParValues : Component \""<<this->name()<<"\" has ";
84  std::cout<<"mag = "<<mag_->value()<<",\t";
85  std::cout<<"phase = "<<phase_->value()<<",\t";
86  std::cout<<"magBar = "<<magBar_->value()<<",\t";
87  std::cout<<"phaseBar = "<<phaseBar_->value()<<"."<<std::endl;
88 }
89 
90 void LauMagPhaseCPCoeffSet::printTableHeading(std::ostream& stream) const
91 {
92  stream<<"\\begin{tabular}{|l|c|c|c|c|}"<<std::endl;
93  stream<<"\\hline"<<std::endl;
94  stream<<"Component & Magnitude & Phase & Magnitude_bar & Phase_bar \\\\"<<std::endl;
95  stream<<"\\hline"<<std::endl;
96 }
97 
98 void LauMagPhaseCPCoeffSet::printTableRow(std::ostream& stream) const
99 {
100  LauPrint print;
101  TString resName = this->name();
102  resName = resName.ReplaceAll("_", "\\_");
103  stream<<resName<<" & $";
104  print.printFormat(stream, mag_->value());
105  stream<<" \\pm ";
106  print.printFormat(stream, mag_->error());
107  stream<<"$ & $";
108  print.printFormat(stream, phase_->value());
109  stream<<" \\pm ";
110  print.printFormat(stream, phase_->error());
111  stream<<"$ & $";
112  print.printFormat(stream, magBar_->value());
113  stream<<" \\pm ";
114  print.printFormat(stream, magBar_->error());
115  stream<<"$ & $";
116  print.printFormat(stream, phaseBar_->value());
117  stream<<" \\pm ";
118  print.printFormat(stream, phaseBar_->error());
119  stream<<"$ \\\\"<<std::endl;
120 }
121 
123 {
124  if (mag_->fixed() == kFALSE) {
125  // Choose a value for "magnitude" between 0.0 and 2.0
126  Double_t value = LauRandom::zeroSeedRandom()->Rndm()*2.0;
127  mag_->initValue(value); mag_->value(value);
128  }
129  if (phase_->fixed() == kFALSE) {
130  // Choose a phase between +- pi
132  phase_->initValue(value); phase_->value(value);
133  }
134  if (magBar_->fixed() == kFALSE) {
135  // Choose a value for "magnitude" between 0.0 and 2.0
136  Double_t value = LauRandom::zeroSeedRandom()->Rndm()*2.0;
137  magBar_->initValue(value); magBar_->value(value);
138  }
139  if (phaseBar_->fixed() == kFALSE) {
140  // Choose a phase between +- pi
142  phaseBar_->initValue(value); phaseBar_->value(value);
143  }
144 }
145 
147 {
148  // retrieve the current values from the parameters
149  Double_t mVal= mag_->value();
150  Double_t pVal= phase_->value();
151  Double_t mBarVal= magBar_->value();
152  Double_t pBarVal= phaseBar_->value();
153  Double_t genPhase = phase_->genValue();
154  Double_t genPhaseBar = phaseBar_->genValue();
155 
156  // Check whether we have a negative magnitude.
157  // If so make it positive and add pi to the phase.
158  if (mVal < 0.0) {
159  mVal *= -1.0;
160  pVal += LauConstants::pi;
161  }
162  if (mBarVal < 0.0) {
163  mBarVal *= -1.0;
164  pBarVal += LauConstants::pi;
165  }
166 
167  // Check now whether the phases lies in the right range (-pi to pi).
168  Bool_t pWithinRange(kFALSE);
169  Bool_t pBarWithinRange(kFALSE);
170  while (pWithinRange == kFALSE && pBarWithinRange == kFALSE) {
171  if (pVal > -LauConstants::pi && pVal < LauConstants::pi) {
172  pWithinRange = kTRUE;
173  } else {
174  // Not within the specified range
175  if (pVal > LauConstants::pi) {
176  pVal -= LauConstants::twoPi;
177  } else if (pVal < -LauConstants::pi) {
178  pVal += LauConstants::twoPi;
179  }
180  }
181 
182  if (pBarVal > -LauConstants::pi && pBarVal < LauConstants::pi) {
183  pBarWithinRange = kTRUE;
184  } else {
185  // Not within the specified range
186  if (pBarVal > LauConstants::pi) {
187  pBarVal -= LauConstants::twoPi;
188  } else if (pBarVal < -LauConstants::pi) {
189  pBarVal += LauConstants::twoPi;
190  }
191  }
192  }
193 
194  // A further problem can occur when the generated phase is close to -pi or pi.
195  // The phase can wrap over to the other end of the scale -
196  // this leads to artificially large pulls so we wrap it back.
197  Double_t diff = pVal - genPhase;
198  if (diff > LauConstants::pi) {
199  pVal -= LauConstants::twoPi;
200  } else if (diff < -LauConstants::pi) {
201  pVal += LauConstants::twoPi;
202  }
203 
204  diff = pBarVal - genPhaseBar;
205  if (diff > LauConstants::pi) {
206  pBarVal -= LauConstants::twoPi;
207  } else if (diff < -LauConstants::pi) {
208  pBarVal += LauConstants::twoPi;
209  }
210 
211  // finally store the new values in the parameters
212  // and update the pulls
213  mag_->value(mVal); mag_->updatePull();
214  phase_->value(pVal); phase_->updatePull();
215  magBar_->value(mBarVal); magBar_->updatePull();
216  phaseBar_->value(pBarVal); phaseBar_->updatePull();
217 
218 
219 }
220 
222 {
223  particleCoeff_.setRealImagPart( mag_->value()*TMath::Cos(phase_->value()), mag_->value()*TMath::Sin(phase_->value()) );
224  return particleCoeff_;
225 }
226 
228 {
229  antiparticleCoeff_.setRealImagPart( magBar_->value()*TMath::Cos(phaseBar_->value()), magBar_->value()*TMath::Sin(phaseBar_->value()) );
230  return antiparticleCoeff_;
231 }
232 
233 void LauMagPhaseCPCoeffSet::setCoeffValues( const LauComplex& coeff, const LauComplex& coeffBar, Bool_t init )
234 {
235  Double_t magVal( coeff.abs() );
236  Double_t phaseVal( coeff.arg() );
237  Double_t magBarVal( coeffBar.abs() );
238  Double_t phaseBarVal( coeffBar.arg() );
239 
240  mag_->value( magVal );
241  phase_->value( phaseVal );
242  magBar_->value( magBarVal );
243  phaseBar_->value( phaseBarVal );
244 
245  if ( init ) {
246  mag_->genValue( magVal );
247  phase_->genValue( phaseVal );
248  magBar_->genValue( magBarVal );
249  phaseBar_->genValue( phaseBarVal );
250 
251  mag_->initValue( magVal );
252  phase_->initValue( phaseVal );
253  magBar_->initValue( magBarVal );
254  phaseBar_->initValue( phaseBarVal );
255  }
256 }
257 
259 {
260  // set the name
261  TString parName(this->baseName()); parName += "_ACP";
262  acp_.name(parName);
263 
264  // work out the ACP value
265 
266  Double_t value(-99.0);
267  value = (magBar_->value()*magBar_->value() - mag_->value()*mag_->value())/(magBar_->value()*magBar_->value() + mag_->value()*mag_->value());
268 
269  // is it fixed?
270  Bool_t fixed = magBar_->fixed() && mag_->fixed();
271  acp_.fixed(fixed);
272 
273  // we can't work out the error without the covariance matrix
274  Double_t error(0.0);
275 
276  // set the value and error
277  acp_.valueAndErrors(value,error);
278 
279  return acp_;
280 }
281 
282 LauAbsCoeffSet* LauMagPhaseCPCoeffSet::createClone(const TString& newName, CloneOption cloneOption, Double_t constFactor)
283 {
284  LauAbsCoeffSet* clone(0);
285  if ( cloneOption == All || cloneOption == TiePhase || cloneOption == TieMagnitude ) {
286  clone = new LauMagPhaseCPCoeffSet( *this, cloneOption, constFactor );
287  clone->name( newName );
288  } else {
289  std::cerr << "ERROR in LauMagPhaseCPCoeffSet::createClone : Invalid clone option" << std::endl;
290  }
291  return clone;
292 }
293 
static Double_t maxPhase_
Maximum allowed value of phase parameters.
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 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.
ClassImp(LauAbsCoeffSet)
LauParameter * phase_
The phase for particles.
LauParameter()
Default constructor.
Definition: LauParameter.cc:30
const TString & name() const
The parameter name.
virtual void printTableRow(std::ostream &stream) const
Print the parameters of the complex coefficient as a row in the results table.
File containing declaration of LauPrint class.
virtual LauAbsCoeffSet * createClone(const TString &newName, CloneOption cloneOption=All, Double_t constFactor=1.0)
Create a clone of the coefficient set.
virtual std::vector< LauParameter * > getParameters()
Retrieve the parameters of the coefficient, e.g. so that they can be loaded into a fit...
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...
virtual void printParValues() const
Print the current values of the parameters.
CloneOption
Options for cloning operation.
Bool_t clone() const
Check whether is a clone or not.
static Double_t maxMagnitude_
Maximum allowed value of magnitude parameters.
File containing declaration of LauParameter class.
virtual const LauComplex & particleCoeff()
Retrieve the complex coefficient for a particle.
File containing declaration of LauComplex class.
virtual void setCoeffValues(const LauComplex &coeff, const LauComplex &coeffBar, Bool_t init)
Set the parameters based on the complex coefficients for particles and antiparticles.
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:33
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.
virtual const LauComplex & antiparticleCoeff()
Retrieve the complex coefficient for an antiparticle.
void setRealImagPart(Double_t realpart, Double_t imagpart)
Set both real and imaginary part.
Definition: LauComplex.hh:311
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.
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.
static Double_t minPhase_
Minimum allowed value of phase parameters.
Double_t value() const
The value of the parameter.
LauComplex particleCoeff_
The particle complex coefficient.
static Double_t minMagnitude_
Minimum allowed value of magnitude parameters.
Double_t abs() const
Obtain the absolute value of the complex number.
Definition: LauComplex.hh:220
virtual const TString & baseName() const
Retrieve the base name of the coefficient set.
LauParameter * phaseBar_
The phase for antiparticles.
LauComplex antiparticleCoeff_
The antiparticle complex coefficient.
virtual void printTableHeading(std::ostream &stream) const
Print the column headings for a results table.
Double_t genValue() const
The value generated for the parameter.