MINT2
FitAmpListBase.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:18:03 GMT
3 #include "Mint/FitAmpListBase.h"
4 
5 #include "Mint/FitAmplitude.h"
8 #include "Mint/FitAmplitude.h"
9 #include "Mint/FitAmpSum.h"
11 #include "Mint/FitAmpPairList.h"
12 #include "TRandom3.h"
13 #include <iostream>
14 
15 using namespace std;
16 using namespace MINT;
17 
19  : _efficiency(0)
20 {
21 }
22 
24  : _efficiency(other._efficiency)
25 {
26  bool dbThis=false;
27  this->deleteAll();
28  if(dbThis)cout << "copy-ctor FitAmpListBase, done deleteAll()" << endl;
29  /*
30  There'll be 'physical' copies of all Amplitudes, but the
31  FitParameters remain the same (pointers to the same
32  FitParameter Object). This is useful for the CP-con coding
33  as it is now, but perhaps a bit counter-intuitive. Needs to
34  be reviewed at some point. This behaviour is defined in the
35  copy construcopy-ctor of the FitAmplitude class.
36  */
37 
39 }
40 
41 FitAmpListBase::FitAmpListBase(const FitAmpListBase& other, string name)
42 : _efficiency(other._efficiency)
43 {
44  bool dbThis=false;
45  this->deleteAll();
46  if(dbThis)cout << "copy-ctor FitAmpListBaseSubset, done deleteAll()" << endl;
47  /*
48  There'll be 'physical' copies of all Amplitudes, but the
49  FitParameters remain the same (pointers to the same
50  FitParameter Object). This is useful for the CP-con coding
51  as it is now, but perhaps a bit counter-intuitive. Needs to
52  be reviewed at some point. This behaviour is defined in the
53  copy construcopy-ctor of the FitAmplitude class.
54  */
55 
56  addCopyOfSubsetWithSameFitParameters(other,(string) name);
57 }
58 
60  if(&other == this) return *this;
61  _efficiency = other._efficiency;
62  deleteAll();
64  return *this;
65 }
66 int FitAmpListBase::add(const FitAmpListBase& other, double factor){
67  return addCopyWithSameFitParameters(other, factor);
68 }
69 int FitAmpListBase::addAsList(const FitAmpListBase& other, double factor){
70  if(0 == other.size()) return size();
71  // keeps them together
72  //cout << "Hello from FitAmpListBase::addAsList" << endl;
74  if(1.0 != factor) newList->multiply(factor);
75  _fitAmpLists.push_back(newList);
76  //cout << "all done in FitAmpListBase::addAsList" << endl;
77  return size();
78 }
79 
81  , double factor){
82  for(unsigned int i=0; i < other._fitAmps.size(); i++){
83  FitAmplitude* fa = other._fitAmps[i];
84  FitAmplitude* newFa = new FitAmplitude(*fa);
85  if(1.0 != factor) newFa->multiply(factor);
86  //_fitAmps.push_back(newFa);
87  this->addAmplitude(newFa);
88  }
89  for(unsigned int i=0; i < other._fitAmpLists.size(); i++){
90  this->addAsList(*(other._fitAmpLists[i]->GetCloneSameFitParameters()));
91  }
92  return this->size();
93 }
94 
95 int FitAmpListBase::addCopyOfSubsetWithSameFitParameters(const FitAmpListBase& other, string name, double factor){
96  for(unsigned int i=0; i < other._fitAmps.size(); i++){
97  if(! A_is_in_B(name,other._fitAmps[i]->name()))continue;
98  FitAmplitude* fa = other._fitAmps[i];
99  FitAmplitude* newFa = new FitAmplitude(*fa);
100  if(1.0 != factor) newFa->multiply(factor);
101  this->addAmplitude(newFa);
102  }
103  for(unsigned int i=0; i < other._fitAmpLists.size(); i++){
104  this->addAsList(*(other._fitAmpLists[i]->GetCloneOfSubsetSameFitParameters(name)));
105  }
106  return this->size();
107 }
108 
109 unsigned int FitAmpListBase::size() const{
110  unsigned int sz(_fitAmps.size());
111  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
112  sz += _fitAmpLists[i]->size();
113  }
114  return sz;
115 }
116 
118 
119  if(in < _fitAmps.size()) return _fitAmps[in];
120  unsigned int sz = _fitAmps.size();
121  for(unsigned int i = 0; i < _fitAmpLists.size(); i++){
122  int index = in - sz;
123  if(index >= 0 && static_cast<unsigned int>(index) < _fitAmpLists[i]->size()) return _fitAmpLists[i]->getAmpPtr(index);
124  sz += _fitAmpLists[i]->size();
125  }
126 
127  cout << " FitAmpListBase::getAmp index out of range"
128  << endl;
129  return 0;
130 }
131 const FitAmplitude* FitAmpListBase::getAmpPtr(unsigned int in) const{
132 
133  if(in < _fitAmps.size()) return _fitAmps[in];
134  unsigned int sz = _fitAmps.size();
135  for(unsigned int i = 0; i < _fitAmpLists.size(); i++){
136  int index = in - sz;
137  if(index >= 0 && static_cast<unsigned int>(index) < _fitAmpLists[i]->size()) return _fitAmpLists[i]->getAmpPtr(index);
138  sz += _fitAmpLists[i]->size();
139  }
140 
141  cout << " FitAmpListBase::getAmp index out of range"
142  << endl;
143  return 0;
144 }
145 
147  bool dbThis=false;
148  if(dbThis) cout << "FitAmpListBase::CPConjugateSameFitParameters()" << endl;
149 
150  bool success=true;
151  for(unsigned int i=0; i< this->size(); i++){
152  success &= getAmpPtr(i)->CPConjugateSameFitParameters();
153  }
154  return success;
155 }
156 
158  bool dbThis=false;
159  if(dbThis) cout << "FitAmpListBase::CConjugateFinalStateSameFitParameters()" << endl;
160 
161  bool success=true;
162  for(unsigned int i=0; i< this->size(); i++){
164  }
165  return success;
166 }
167 
169  bool dbThis=false;
170  if(dbThis) cout << "FitAmpListBase::CConjugateFinalStateSameFitParameters()" << endl;
171 
172  bool success=true;
173  for(unsigned int i=0; i< this->size(); i++){
175  }
176  return success;
177 }
178 
180  bool dbThis=false;
181  if(dbThis) cout << "FitAmpListBase::setLSameFitParameters()" << endl;
182 
183  bool success=true;
184  for(unsigned int i=0; i< this->size(); i++){
185  success &= getAmpPtr(i)->setLSameFitParameters(L);
186  }
187  return success;
188 }
189 
191  bool dbThis=false;
192  if(dbThis) cout << "FitAmpListBase::GetCloneSameFitParameters()" << endl;
193  /*
194  There'll be 'physical' copies of all Amplitudes, but the
195  FitParameters remain the same (pointers to the same
196  FitParameter Object). This is useful for the CP-con coding
197  as it is now, but perhaps a bit counter-intuitive. needs to
198  be reviewed at some point. This behaviour is defined in the
199  copy constructor of the FitAmplitude class.
200  */
201 
202  counted_ptr<FitAmpListBase> newList(new FitAmpListBase(*this));
203  return newList;
204 }
205 
207  bool dbThis=false;
208  if(dbThis) cout << "FitAmpListBase::GetCloneSameFitParameters()" << endl;
209  /*
210  There'll be 'physical' copies of all Amplitudes, but the
211  FitParameters remain the same (pointers to the same
212  FitParameter Object). This is useful for the CP-con coding
213  as it is now, but perhaps a bit counter-intuitive. needs to
214  be reviewed at some point. This behaviour is defined in the
215  copy constructor of the FitAmplitude class.
216  */
217 
218  counted_ptr<FitAmpListBase> newList(new FitAmpListBase(*this,name));
219  return newList;
220 }
221 
223  bool dbThis=false;
224  if(dbThis) cout << "FitAmpListBase::GetCPConjugateSameFitParameters()" << endl;
225 
227  newList->CPConjugateSameFitParameters();
228  return newList;
229 }
230 
232  bool dbThis=false;
233  if(dbThis) cout << "FitAmpListBase::GetCConjugateFinalStateSameFitParameters()" << endl;
234 
237  return newList;
238 }
239 
241  bool dbThis=false;
242  if(dbThis) cout << "FitAmpListBase::GetCConjugateFinalStateSameFitParameters()" << endl;
243 
246  return newList;
247 }
248 
250  bool dbThis=false;
251  if(dbThis) cout << "FitAmpListBase::GetDifferentLSameFitParameters()" << endl;
252 
254  newList->setLSameFitParameters(L);
255  return newList;
256 }
257 
259  bool dbThis=false;
260  if(0 == fa) return false;
261  if(dbThis) cout << "check init values: " << *fa << endl;
262  _fitAmps.push_back(fa);
263  return true;
264 }
265 void FitAmpListBase::printLargestAmp(IDalitzEvent& evt, std::ostream& os){
266  bool dbthis=false;
267  if(size()==0){
268  os << "FitAmpListBase::printLargestAmp: list is empty" << endl;
269  return;
270  }
271 
272  double largestValue = -9999;
273  std::string largestName = "none";
274 
275  for(unsigned int i=0; i< this->size(); i++){
276  if(dbthis){
277  cout << "FitAmpListBase::printLargestAmp()"
278  << "\n > for " << getAmpPtr(i)->theBareDecay().oneLiner()
279  << "\n > I get " << getAmpPtr(i)->getVal(evt)
280  << endl;
281  }
282  double val = norm(getAmpPtr(i)->getVal(evt));
283  if(val > largestValue){
284  largestValue = val;
285  largestName = getAmpPtr(i)->name();
286  }
287  }
288  os << "largest amp for event " << evt
289  << "\n is " << largestName
290  << " with value " << largestValue
291  << endl;
292 }
293 
294 
295 void FitAmpListBase::printAllAmps(IDalitzEvent& evt, std::ostream& os){
296  bool dbThis=false;
297  if(size()==0){
298  os << "FitAmpListBase::printAllAmps: list is empty" << endl;
299  return;
300  }
301 
302  std::string largestName = "none";
303  if(dbThis) cout << "Debug mode for FitAmpListBase::printAllAmps" << endl;
304 
305  os << "FitAmpListBase::printAllAmps()\n====================";
306 
307  for(unsigned int i=0; i< this->size(); i++){
308  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
309  << " \t" << getAmpPtr(i)->getVal(evt)
310  << endl;
311  }
312 }
313 void FitAmpListBase::printAllAmps(std::ostream& os)const{
314  bool dbThis=false;
315  if(size() == 0){
316  os << "FitAmpListBase::printAllAmps: list is empty" << endl;
317  return;
318  }
319 
320  std::string largestName = "none";
321  if(dbThis) cout << "Debug mode for FitAmpListBase::printAllAmps" << endl;
322 
323  os << "FitAmpListBase::printAllAmps()\n====================";
324 
325  for(unsigned int i=0; i< this->size(); i++){
326  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
327  << endl;
328  }
329 }
330 
332  bool dbThis=false;
333  if(size() == 0){
334  os << "FitAmpListBase::printNonZeroWithValue: list is empty" << endl;
335  return;
336  }
337 
338  std::string largestName = "none";
339  if(dbThis) cout << "Debug mode for FitAmpListBase::printAllAmps" << endl;
340 
341  os << "FitAmpListBase::printNonZeroWithValue\n====================\n";
342 
343  for(unsigned int i=0; i < this->size(); i++){
344  if(getAmpPtr(i)->isZero()) continue;
345  os << "\t" << getAmpPtr(i)->theBareDecay().oneLiner()
346  << " \t" << getAmpPtr(i)->getVal(evt)
347  << endl;
348  }
349 }
350 void FitAmpListBase::print(std::ostream& os) const{
351  os << "FitAmpListBase::print\n====================";
352 
353  for(unsigned int i=0; i< this->size(); i++){
354  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
355  << endl;
356  }
357 }
358 void FitAmpListBase::printNonZero(std::ostream& os) const{
359  os << "FitAmpListBase::printNonZero\n====================";
360 
361  for(unsigned int i=0; i< this->size(); i++){
362  if(getAmpPtr(i)->isZero()) continue;
363  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
364  << endl;
365  }
366 }
367 
368 void FitAmpListBase::setAllAmpsTo(std::complex<double> z){
369  for(unsigned int i=0; i< this->size(); i++){
370  getAmpPtr(i)->FitAmpPhase().set(z);
371  }
372 }
373 
376  , double nSigma){
377  DalitzBoxSet boxes;
378  DalitzBox phaseSpaceBox(pat);
379  boxes.add(phaseSpaceBox);
380 
381  for(unsigned int i=0; i< this->size(); i++){
382  boxes.add( getAmpPtr(i)->MakeBoxes(pat, nSigma) );
383  }
384  boxes.setPDF(pdf);
385  return boxes;
386 }
387 
390  , TRandom* rnd
391  ){
392  DalitzBWBoxSet boxes(pdf, rnd);
393  // DalitzBox phaseSpaceBox(pat);
394  // boxes.add(phaseSpaceBox);
395 
396  for(unsigned int i=0; i< this->size(); i++){
397  DalitzBWBoxSet oneAmpsBox(getAmpPtr(i)->MakeBWBoxes(pat, rnd));
398  boxes.add(oneAmpsBox);
399  }
400  return boxes;
401 }
402 
403 
405  for(unsigned int i=0; i< this->size(); i++){
406  if(0 != getAmpPtr(i)){
407  getAmpPtr(i)->multiply(r);
408  }
409  }
410 }
411 void FitAmpListBase::multiply(const std::complex<double>& z){
412  for(unsigned int i=0; i< this->size(); i++){
413  if(0 != getAmpPtr(i)){
414  getAmpPtr(i)->multiply(z);
415  }
416  }
417 }
419  for(unsigned int i=0; i< this->size(); i++){
420  if(0 != getAmpPtr(i)){
421  getAmpPtr(i)->multiply(irc);
422  }
423  }
424 }
426  for(unsigned int i=0; i< this->size(); i++){
427  if(0 != getAmpPtr(i)){
428  getAmpPtr(i)->multiply(irc);
429  }
430  }
431 }
432 
434  bool dbThis=false;
435  if(dbThis) cout << "Hello from FitAmpListBase::deleteAll()" << endl;
436  for(unsigned int i=0; i<_fitAmps.size(); i++){
437  if(0 != (_fitAmps[i])){
438  delete (_fitAmps[i]);
439  _fitAmps[i]=0;
440  }
441  }
442  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
443  if(0 != _fitAmpLists[i]){
444  _fitAmpLists[i]->deleteAll();
445  }
446  }
447 
448  _fitAmps.clear();
449  _fitAmpLists.clear();
450  if(dbThis) cout << "all fine in FitAmpListBase::deleteAll()" << endl;
451 }
452 
454  _efficiency=eff;
455 }
457  if(0 == _efficiency) return 1.0;
458  double eff = _efficiency->RealVal(evt);
459  if(eff < 0) return 0;
460  return eff;
461 }
462 
464 
465  for(unsigned int i=0; i< this->size(); i++){
466  if(0 == getAmpPtr(i))continue;
467  double integral=0.;
468  double weight_sum=0.;
469  for (unsigned int j=0; j<evtList.size(); j++) {
470  double weight = evtList[j].getWeight()/evtList[j].getGeneratorPdfRelativeToPhaseSpace();
471  weight_sum += weight;
472  integral += weight * std::norm(getAmpPtr(i)->getValWithoutFitParameters(evtList[j]));
473  }
474  if(weight_sum==0)weight_sum = evtList.size();
475  if(integral>0)(getAmpPtr(i))->multiply(sqrt(weight_sum/integral));
476  }
477 }
478 
480 
481  vector<double> tmp;
482  for(unsigned int i=0; i< this->size(); i++){
483  if(0 == getAmpPtr(i))continue;
484  double integral=0.;
485  double weight_sum=0.;
486  for (unsigned int j=0; j<evtList.size(); j++) {
487  double weight = evtList[j].getWeight()/evtList[j].getGeneratorPdfRelativeToPhaseSpace();
488  weight_sum += weight;
489  integral += weight * std::norm(getAmpPtr(i)->amp().getNewVal(evtList[j]));
490  }
491  if(weight_sum==0)weight_sum = evtList.size();
492  if(integral>0)tmp.push_back(sqrt(weight_sum/integral));
493  }
494  return tmp;
495 }
496 
498  TRandom3* r = new TRandom3(seed);
499 
500  for(unsigned int i=0; i< this->size(); i++){
501  //if(0 == getAmpPtr(i))continue;
502  double mag = r->Uniform(0.,1.);
503  double re,im;
504  r->Circle(re,im,mag);
505  complex<double> c(re,im);
506  //getAmpPtr(i)->multiply(c);
507  if(!(getAmpPtr(i)->FitAmpPhase().isConstant()))getAmpPtr(i)->FitAmpPhase().set(c);
508  }
509 }
510 
511 
513  TRandom3* r = new TRandom3(seed);
514 
515  for(unsigned int i=0; i< this->size(); i++){
516  //if(0 == getAmpPtr(i))continue;
517  double phase = r->Uniform(-pi,pi);
518  if(!(getAmpPtr(i)->FitAmpPhase().isConstant()))getAmpPtr(i)->FitAmpPhase().set( polar(getAmpPtr(i)->FitAmpPhase().getAmp(),phase) );
519  }
520 }
521 
523  for(unsigned int i=0; i< this->size(); i++){
524  this->getAmpPtr(i)->setTag(tag);
525  }
526 }
527 
529  deleteAll();
530 }
531 
533  add(other);
534  return *this;
535 }
537  FitAmpListBase fas(*this);
538  fas.add(rhs);
539  return fas;
540 }
541 
542 
544  multiply(r);
545  return *this;
546 }
547 FitAmpListBase& FitAmpListBase::operator*=(const complex<double>& z){
548  multiply(z);
549  return *this;
550 }
552  multiply(irc);
553  return *this;
554 }
555 
557  FitAmpListBase fas(*this);
558  fas.multiply(r);
559  return fas;
560 }
561 FitAmpListBase FitAmpListBase::operator*(const complex<double>& z) const{
562  FitAmpListBase fas(*this);
563  fas.multiply(z);
564  return fas;
565 }
567  FitAmpListBase fas(*this);
568  fas.multiply(irc);
569  return fas;
570 }
571 
572 
574  FitAmpListBase fas(rhs);
575  fas.multiply(r);
576  return fas;
577 }
578 FitAmpListBase operator*(const complex<double>& z, const FitAmpListBase& rhs){
579  FitAmpListBase fas(rhs);
580  fas.multiply(z);
581  return fas;
582 }
584  , const FitAmpListBase& rhs){
585  FitAmpListBase fas(rhs);
586  fas.multiply(irc);
587  return fas;
588 }
589 
590 std::ostream& operator<<(std::ostream& os, const FitAmpListBase& fal){
591  fal.print(os);
592  return os;
593 }
594 
595 //
virtual MINT::counted_ptr< FitAmpListBase > GetCloneOfSubsetSameFitParameters(std::string name) const
void setPDF(MINT::IReturnRealForEvent< IDalitzEvent > *amps)
bool isZero() const
Definition: FitAmplitude.h:98
virtual int add(const FitAmpListBase &other, double factor=1)
virtual bool addAmplitude(FitAmplitude *fa)
virtual FitAmplitude * getAmpPtr(unsigned int i)
virtual MINT::counted_ptr< FitAmpListBase > GetCConjugateInitialStateSameFitParameters() const
virtual int addCopyWithSameFitParameters(const FitAmpListBase &other, double factor=1)
DalitzBWBoxSet makeBWBoxes(const DalitzEventPattern &pat, MINT::IReturnRealForEvent< IDalitzEvent > *pdf, TRandom *rnd=gRandom)
virtual bool CPConjugateSameFitParameters()
virtual void printNonZero(std::ostream &os=std::cout) const
std::string name() const
Definition: FitAmplitude.h:213
DalitzBoxSet makeBoxes(const DalitzEventPattern &pat, MINT::IReturnRealForEvent< IDalitzEvent > *pdf, double nSigma=2)
static const double pi
virtual std::complex< double > getVal(IDalitzEvent &evt)
bool CPConjugateSameFitParameters()
void setTag(int tag)
void setEfficiency(const MINT::counted_ptr< MINT::IReturnRealForEvent< IDalitzEvent > > &eff)
virtual double RealVal(EVENT_TYPE &evt)=0
std::ostream & operator<<(std::ostream &os, const FitAmpListBase &fal)
FitAmpListBase operator *(double r) const
FitAmpListBase operator+(const FitAmpListBase &other) const
bool CConjugateInitialStateSameFitParameters()
DecayTree theBareDecay() const
Definition: FitAmplitude.h:166
virtual void printLargestAmp(IDalitzEvent &evt, std::ostream &os=std::cout)
FitAmpListBase operator *(double r, const FitAmpListBase &rhs)
virtual void deleteAll()
virtual int addAsList(const FitAmpListBase &other, double factor=1)
virtual MINT::counted_ptr< FitAmpListBase > GetCPConjugateSameFitParameters() const
std::vector< double > normFactors(DalitzEventList &evtList)
virtual int addCopyOfSubsetWithSameFitParameters(const FitAmpListBase &other, std::string name, double factor=1)
MINT::counted_ptr< MINT::IReturnRealForEvent< IDalitzEvent > > _efficiency
FitAmpListBase & operator *=(double r)
MINT::FitComplex & FitAmpPhase()
Definition: FitAmplitude.h:147
bool CConjugateFinalStateSameFitParameters()
bool A_is_in_B(const std::string &a, const std::string &b)
Definition: Utils.cpp:34
virtual void printNonZeroWithValue(IDalitzEvent &evt, std::ostream &os=std::cout)
std::vector< FitAmplitude * > _fitAmps
void add(const DalitzBox &box)
bool setLSameFitParameters(int L)
virtual MINT::counted_ptr< FitAmpListBase > GetCloneSameFitParameters() const
double efficiency(IDalitzEvent &evt)
void randomizeStartVals(int seed=0)
void setTag(int tag)
Definition: FitAmplitude.h:128
void normalizeAmps(DalitzEventList &evtList)
std::vector< MINT::counted_ptr< FitAmpListBase > > _fitAmpLists
virtual MINT::counted_ptr< FitAmpListBase > GetDifferentLSameFitParameters(int L) const
virtual void set(std::complex< double > z)=0
virtual bool setLSameFitParameters(int L)
virtual unsigned int size() const
Definition: EventList.h:59
virtual MINT::counted_ptr< FitAmpListBase > GetCConjugateFinalStateSameFitParameters() const
virtual bool CConjugateInitialStateSameFitParameters()
void setAllAmpsTo(const std::complex< double > z)
friend class FitAmplitude
void oneLiner(std::stringstream &seam, int generation=0) const
Definition: DDTree.h:375
void randomizePhaseStartVals(int seed=0)
virtual ~FitAmpListBase()
FitAmpListBase & operator=(const FitAmpListBase &other)
virtual bool CConjugateFinalStateSameFitParameters()
virtual unsigned int size() const
virtual void print(std::ostream &os=std::cout) const
virtual void printAllAmps(std::ostream &os=std::cout) const
void add(DalitzBWBox &box)
virtual void multiply(double r)
FitAmpListBase & operator+=(const FitAmpListBase &other)
void multiply(double r)