MINT2
FitAmpSum.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/FitAmpSum.h"
4 #include "Mint/NamedParameter.h"
5 #include "Mint/FitAmplitude.h"
8 #include "Mint/FitAmplitude.h"
9 #include "Mint/IntegCalculator.h"
10 
11 #include <iostream>
12 #include <cmath>
13 
14 using namespace std;
15 using namespace MINT;
16 
17 // note - this inherits now from FitAmpList, many
18 // methods formerly defined here are now defined there.
19 
21  , const char* fname
22  , MinuitParameterSet* pset
23  , const std::string& prefix
24  , const std::string& lineshapePrefix
25  , const std::string& opt
26  )
27  : FitAmpList(pat, fname, pset, prefix, lineshapePrefix, opt)
28  , _useAnalyticGradient("useAnalyticGradient",0)
29 {
30  /*
31  //Important! Ensures everything is initialised
32  DalitzEventList eventTest;
33  eventTest.generatePhaseSpaceEvents(1,pat);
34  this->getVal(eventTest[0]);
35  */
36 }
37 
39  , MinuitParameterSet* pset
40  , const std::string& prefix
41  , const std::string& lineshapePrefix
42  , const std::string& opt
43  )
44  : FitAmpList(pat, pset, prefix, lineshapePrefix, opt)
45  , _useAnalyticGradient("useAnalyticGradient",0)
46 {
47 
48  // cout << "pset pointer in FitAmpSum::FitAmpSum " << pset << " = " << getMPS() << endl;
49 
50  /*
51  //Important! Ensures everything is initialised
52  DalitzEventList eventTest;
53  eventTest.generatePhaseSpaceEvents(1,pat);
54  this->getVal(eventTest[0]);
55  */
56 }
58  , const std::string& prefix
59  , const std::string& lineshapePrefix
60  , const std::string& opt
61  )
62  : FitAmpList(pat, prefix, lineshapePrefix, opt)
63  , _useAnalyticGradient("useAnalyticGradient",0)
64 {
65  /*
66  //Important! Ensures everything is initialised
67  DalitzEventList eventTest;
68  eventTest.generatePhaseSpaceEvents(1,pat);
69  this->getVal(eventTest[0]);
70  */
71 }
72 
78  , FitAmpList(other)
79  , _useAnalyticGradient("useAnalyticGradient",0)
80 {
81  /*
82  //Important! Ensures everything is initialised
83  DalitzEventList eventTest;
84  eventTest.generatePhaseSpaceEvents(1,_pat);
85  this->getVal(eventTest[0]);
86  */
87 }
88 
94  , FitAmpList(other)
95  , _useAnalyticGradient("useAnalyticGradient",0)
96 {
97  /*
98  //Important! Ensures everything is initialised
99  DalitzEventList eventTest;
100  eventTest.generatePhaseSpaceEvents(1,_pat);
101  this->getVal(eventTest[0]);
102  */
103 }
104 
106  if(&other == this) return *this;
107  (FitAmpList)(*this) = (FitAmpList) (other);
108  return *this;
109 }
111  if(&other == this) return *this;
112  (FitAmpSum)(*this) = other;
113  return *this;
114 }
115 
117 // need to reform these one day...
118 // ... it all relies on the copy-constructur/=operator in FitAmpitude
119 // not copying the fit parameters, but just their pointers
120 // which will need to be reviewed.
121 //
122  bool dbThis=false;
123  if(dbThis) cout << "FitAmpSum::GetCloneSameFitParameters()" << endl;
124  /*
125  There'll be 'physical' copies of all Amplitudes, but the
126  FitParameters remain the same (pointers to the same
127  FitParameter Object). This is useful for the CP-con coding
128  as it is now, but perhaps a bit counter-intuitive. needs to
129  be reviewed at some point. This behaviour is defined in the
130  copy constructor of the FitAmplitude class.
131  */
132 
133  /*
134  counted_ptr<FitAmpSum>
135  newList(new FitAmpSum((IDalitzEventList*) this->getEventRecord()
136  , _paraFName.c_str(), _minuitParaSet));
137  */
138  counted_ptr<FitAmpSum> newList(new FitAmpSum(*this));
139  newList->deleteAll();
140 
141  if(dbThis) cout << "FitAmpSum::GetCloneSameFitParameters() made ptr" << endl;
142  newList->add(*this);
143  if(dbThis) cout << "cloning FitAmpSum " << newList->size() << endl;
144  return newList;
145 }
146 
148  // need to reform these one day...
149  // ... it all relies on the copy-constructur/=operator in FitAmpitude
150  // not copying the fit parameters, but just their pointers
151  // which will need to be reviewed.
152  //
153  bool dbThis=true;
154  if(dbThis) cout << "FitAmpSum::GetCloneOfSubsetSameFitParameters()" << endl;
155  /*
156  There'll be 'physical' copies of all Amplitudes, but the
157  FitParameters remain the same (pointers to the same
158  FitParameter Object). This is useful for the CP-con coding
159  as it is now, but perhaps a bit counter-intuitive. needs to
160  be reviewed at some point. This behaviour is defined in the
161  copy constructor of the FitAmplitude class.
162  */
163 
164  /*
165  counted_ptr<FitAmpSum>
166  newList(new FitAmpSum((IDalitzEventList*) this->getEventRecord()
167  , _paraFName.c_str(), _minuitParaSet));
168  */
169  counted_ptr<FitAmpSum> newList(new FitAmpSum(*this));
170  newList->deleteAll();
171 
172  if(dbThis) cout << "FitAmpSum::GetCloneOfSubsetSameFitParameters() made ptr" << endl;
173  newList->addCopyOfSubsetWithSameFitParameters(*this,(string) name);
174  if(dbThis) cout << "cloning subset of FitAmpSum " << newList->size() << endl;
175  return newList;
176 }
177 
178 std::complex<double> FitAmpSum::getVal(IDalitzEvent* evt){
179  return getVal(*evt);
180 }
181 
182 std::complex<double> FitAmpSum::getVal(IDalitzEvent& evt){
183  //double dbThis=false;
184 
185  std::complex<double> sum(0);
186  //if(0 == size()) createAllAmps(evt.eventPattern());
187  for(unsigned int i=0; i< this->size(); i++){
188  //if(dbThis) cout << "FitAmpSum::getVal: " << i << endl;
189  sum = (! this->getAmpPtr(i)->isZero()) ? sum + this->getAmpPtr(i)->getVal(evt) : sum;
190  //cout << sum << "," << endl;
191  }
192 // if(dbThis){
193 // cout << "sum= " << sum << endl;
194 // cout << "efficiency(evt) " << efficiency(evt) << endl;
195 // }
196  std::complex<double> result(sqrt(fabs(efficiency(evt)))*sum);
197 // if(dbThis) cout << "result " << result << endl;
198  /*
199  double resultSq = norm(result);
200  bool invalid = (! isfinite(resultSq)) || std::isnan(resultSq);
201  if(dbThis || invalid){
202  cout << "\n---------------------------" << endl;
203  cout << "sqrt(efficiency())*sum = "
204  << "sqrt(" << efficiency(evt) << ")*" << sum
205  << endl;
206  printValues(evt);
207  cout << "\n---------------------------" << endl;
208  }
209  */
210  return result;
211 }
212 
213 
214 double FitAmpSum::getAmpSqr(IDalitzEvent& evt, std::vector<string> ampNames, bool CC){
215  double dbThis=false;
216 
217  std::complex<double> sum(0);
218  if(0 == size()) createAllAmps(evt.eventPattern());
219  for(unsigned int i=0; i< this->size(); i++){
220  for (unsigned int n=0; n <ampNames.size(); n++) {
221  if(A_is_in_B(ampNames[n], this->getAmpPtr(i)->name())){
222  if(A_is_in_B("Cconj_FS", this->getAmpPtr(i)->name()) && !CC) continue;
223  if(dbThis) cout << "found amp " << this->getAmpPtr(i)->name() << endl;
224  sum += this->getAmpPtr(i)->getVal(evt);
225  }
226  }
227  }
228  std::complex<double> result(sqrt(fabs(efficiency(evt)))*sum);
229 
230  return norm(result);
231 }
232 
233 void FitAmpSum::Gradient(IDalitzEvent& evt,vector<double>& grad,MinuitParameterSet* mps){
234 
235  std::complex<double> val = getVal(evt);
236  std::complex<double> valConj= conj(val);
237 
238  for (unsigned int i=0; i<mps->size(); i++) {
239  if(mps->getParPtr(i)->hidden())continue;
240 
241  string name_i= mps->getParPtr(i)->name();
242  if(name_i.find("Inco")!=std::string::npos)continue;
243 
244  if(name_i.find("_Re")!=std::string::npos){
245  if(mps->getParPtr(i)->iFixInit() && mps->getParPtr(i+1)->iFixInit()){
246  i++;
247  continue;
248  }
249  name_i.replace(name_i.find("_Re"),3,"");
250  for(unsigned int j=0; j< this->size(); j++){
251  if(A_is_in_B(name_i, this->getAmpPtr(j)->name())){
252  if(i+1 >= grad.size()){
253  cout << "WARNING in FitAmpSum::Gradient"
254  << " have to increase size of grad to avoid memory issues" << endl;
255  grad.resize(i+2);
256  }
257 
258  std::complex<double> tmp = 2.*valConj* getAmpPtr(j)->getValWithoutFitParameters(evt);
259  grad[i]= tmp.real();
260  grad[i+1]= -tmp.imag();
261  i++;
262  break;
263  }
264  }
265  }
266  // Doesn't work. Don't use!
267  /*
268  else if(name_i.find("_Amp")!=std::string::npos){
269  name_i.replace(name_i.find("_Amp"),4,"");
270 
271  for(unsigned int j=0; j<_fitAmps.size(); j++){
272  if(A_is_in_B(name_i, _fitAmps[j]->name())){
273  std::complex<double> tmp = (_fitAmps[j])->getValWithoutFitParameters(evt);
274  grad[i]= 2.*(tmp*valConj).real()/std::abs((_fitAmps[j])->AmpPhase());
275  grad[i+1]= -2.*std::arg((_fitAmps[j])->AmpPhase())*(tmp*(valConj-conj((_fitAmps[j])->getVal(evt)))).imag();
276  i++;
277  break;
278  }
279  }
280  }
281  */
282  else if(mps->getParPtr(i)->iFixInit())continue;
283  else {
284  std::cout << "FitAmpSum::Gradient() called. Sorry, I don't know how to calculate the derivative with respect to the fit parameter " << mps->getParPtr(i)->name() << " ! Please implement me or set useAnalytic Gradient to 0 in your options file. I'll crash now. " << std::endl;
285  throw "crash";
286  }
287 
288  }
289 
290 }
291 
292 void FitAmpSum::print(std::ostream& os) const{
293  bool dbThis=false;
294  os << "FitAmpSum::print " << this->size() << " amplitude components \n"
295  << "======================================================" << endl;
296 
297  for(unsigned int i=0; i< this->size(); i++){
298  if(dbThis){
299  cout << "ampPtr( " << i << " ): " << endl;
300  cout << getAmpPtr(i) << endl;
301  }
302  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
303  << endl;
304  }
305 }
306 void FitAmpSum::printNonZero(std::ostream& os) const{
307  os << "FitAmpSum::print\n====================";
308 
309  for(unsigned int i=0; i< this->size(); i++){
310  if(getAmpPtr(i)->isZero()) continue;
311  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
312  << endl;
313  }
314 
315 }
316 void FitAmpSum::printValues(IDalitzEvent& evt, std::ostream& os){
317  os << "FitAmpSum::print\n====================";
318 
319  for(unsigned int i=0; i< this->size(); i++){
320  complex<double> val = getAmpPtr(i)->getVal(evt);
321  os << "\n\t" << getAmpPtr(i)->theBareDecay().oneLiner()
322  << val;
323  if(norm(val) > 1.e10) os << " HUGE!!!" << endl;
324  os << endl;
325  }
326 }
327 
330 }
331 
332 // duplication betweem making IntegCalculator and FitAmpPairList should be removed one day...
335  for(unsigned int i=0; i < _fitAmps.size(); i++){
336  if(_fitAmps[i]->canBeIgnored()) continue;
337  for(unsigned int j=i; j < _fitAmps.size(); j++){
338  if(_fitAmps[j]->canBeIgnored()) continue;
339  // all terms within the list
340  list->addAmps( (_fitAmps[i]), (_fitAmps[j]));
341  }
342  // cross terms between list, and list-of-list
343  for(unsigned int k=0; k < _fitAmpLists.size(); k++){
344  for(unsigned int l=0; l < _fitAmpLists[k]->size(); l++){
345  if(_fitAmpLists[k]->getAmpPtr(l)->canBeIgnored()) continue;
346  list->addAmps(_fitAmps[i], _fitAmpLists[k]->getAmpPtr(l));
347  }
348  }
349  }
350 
351 
352  // Now it gets complicated. I want to leave all the
353  // terms within a list to whatever that list
354  // wants to do. But we don't want to forget cross terms
355  // between lists.
356 
357  // cross terms between list, and list-of-list
358  for(unsigned int i=1; i < _fitAmpLists.size(); i++){
359  for(unsigned int j=0; j < i; j++){
360  for(unsigned int ki = 0; ki < _fitAmpLists[i]->size(); ki++){
361  if(_fitAmpLists[i]->getAmpPtr(ki)->canBeIgnored()) continue;
362  for(unsigned int kj = 0; kj < _fitAmpLists[j]->size(); kj++){
363  if(_fitAmpLists[j]->getAmpPtr(kj)->canBeIgnored()) continue;
364  list->addAmps(_fitAmpLists[i]->getAmpPtr(ki), _fitAmpLists[j]->getAmpPtr(kj));
365  }
366  }
367  }
368  }
369 
370 
371  // finally, the terms within each list
372  // which is up to the list itself.
373  // (this is where we save a lot of space and time
374  // when it comes to the model-independent stuff
375  // where there are no internal cross terms)
376  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
378  }
379 
380  cout << "FitAmpSum: setting efficiency POINTER "
381  << " in integCalculator to "
382  << _efficiency.get();
383  if(0 == _efficiency.get()){
384  cout << " (0 means no pointer, 100% efficiency).";
385  }
386  cout << endl;
387 
388  list->setEfficiency(_efficiency);
389 
390 
391  cout << "this->size()" << this->size() << endl;
392  cout << "_fitAmpLists.size()" << _fitAmpLists.size() << endl;
393  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
394  cout << "printing list number " << i << endl;
395  _fitAmpLists[i]->print();
396  cout << endl;
397  }
398  cout << " the pair list: " << endl;
399  list->print();
400 
401  return list;
402 }
403 
406  for(unsigned int i=0; i < _fitAmps.size(); i++){
407  if(_fitAmps[i]->canBeIgnored()) continue;
408  for(unsigned int j=i; j < _fitAmps.size(); j++){
409  if(_fitAmps[j]->canBeIgnored()) continue;
410  // all terms within the list
411  list->addAmps( (_fitAmps[i]), (_fitAmps[j]));
412  }
413  // cross terms between list, and list-of-list
414  for(unsigned int k=0; k < _fitAmpLists.size(); k++){
415  for(unsigned int l=0; l < _fitAmpLists[k]->size(); l++){
416  if(_fitAmpLists[k]->getAmpPtr(l)->canBeIgnored()) continue;
417  list->addAmps(_fitAmps[i], _fitAmpLists[k]->getAmpPtr(l));
418  }
419  }
420  }
421 
422 
423  // Now it gets complicated. I want to leave all the
424  // terms within a list to whatever that list
425  // wants to do. But we don't want to forget cross terms
426  // between lists.
427 
428  // cross terms between list, and list-of-list
429  for(unsigned int i=1; i < _fitAmpLists.size(); i++){
430  for(unsigned int j=0; j < i; j++){
431  for(unsigned int ki = 0; ki < _fitAmpLists[i]->size(); ki++){
432  if(_fitAmpLists[i]->getAmpPtr(ki)->canBeIgnored()) continue;
433  for(unsigned int kj = 0; kj < _fitAmpLists[j]->size(); kj++){
434  if(_fitAmpLists[j]->getAmpPtr(kj)->canBeIgnored()) continue;
435  list->addAmps(_fitAmpLists[i]->getAmpPtr(ki), _fitAmpLists[j]->getAmpPtr(kj));
436  }
437  }
438  }
439  }
440 
441 
442  // finally, the terms within each list
443  // which is up to the list itself.
444  // (this is where we save a lot of space and time
445  // when it comes to the model-independent stuff
446  // where there are no internal cross terms)
447  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
449  }
450 
451  cout << "FitAmpSum: setting efficiency POINTER "
452  << " in integCalculator to "
453  << _efficiency.get();
454  if(0 == _efficiency.get()){
455  cout << " (0 means no pointer, 100% efficiency).";
456  }
457  cout << endl;
458 
459  list->setEfficiency(_efficiency);
460 
461  /*
462  cout << "this->size()" << this->size() << endl;
463  cout << "_fitAmpLists.size()" << _fitAmpLists.size() << endl;
464  for(unsigned int i=0; i < _fitAmpLists.size(); i++){
465  cout << "printing list number " << i << endl;
466  _fitAmpLists[i]->print();
467  cout << endl;
468  }
469  cout << " the pair list: " << endl;
470  list->print();
471  */
472 
473  return list;
474 }
475 
477  bool dbThis=true;
478  if(dbThis) cout << "FitAmpSum::~FitAmpSum() " << endl;
479  deleteAll();
480  if(dbThis) cout << "FitAmpSum::~FitAmpSum() done" << endl;
481 
482 }
483 
485  add(other);
486  return *this;
487 }
489  FitAmpSum fas(*this);
490  fas.add(rhs);
491  return fas;
492 }
493 
495  multiply(r);
496  return *this;
497 }
498 FitAmpSum& FitAmpSum::operator*=(const complex<double>& z){
499  multiply(z);
500  return *this;
501 }
503  multiply(irc);
504  return *this;
505 }
507  multiply(ircfe);
508  return *this;
509 }
510 
512  FitAmpSum fas(*this);
513  fas.multiply(r);
514  return fas;
515 }
516 FitAmpSum FitAmpSum::operator*(const complex<double>& z) const{
517  FitAmpSum fas(*this);
518  fas.multiply(z);
519  return fas;
520 }
522  FitAmpSum fas(*this);
523  fas.multiply(irc);
524  return fas;
525 }
527  FitAmpSum fas(*this);
528  fas.multiply(ircfe);
529  return fas;
530 }
531 
532 FitAmpSum operator*(double r, const FitAmpSum& rhs){
533  FitAmpSum fas(rhs);
534  fas.multiply(r);
535  return fas;
536 }
537 FitAmpSum operator*(const complex<double>& z, const FitAmpSum& rhs){
538  FitAmpSum fas(rhs);
539  fas.multiply(z);
540  return fas;
541 }
543  , const FitAmpSum& rhs){
544  FitAmpSum fas(rhs);
545  fas.multiply(irc);
546  return fas;
547 }
549  , const FitAmpSum& rhs){
550  FitAmpSum fas(rhs);
551  fas.multiply(ircfe);
552  return fas;
553 }
554 
555 
556 
557 //
virtual bool append(const IntegCalculator &other)
FitAmpSum operator+(const FitAmpSum &other) const
Definition: FitAmpSum.cpp:488
virtual MINT::counted_ptr< FitAmpListBase > GetCloneOfSubsetSameFitParameters(std::string name) const
Definition: FitAmpSum.cpp:147
bool isZero() const
Definition: FitAmplitude.h:98
virtual bool append(const FitAmpPairList &otherListPtr)
virtual int add(const FitAmpListBase &other, double factor=1)
virtual FitAmplitude * getAmpPtr(unsigned int i)
virtual void print(std::ostream &os=std::cout) const
Definition: FitAmpSum.cpp:292
virtual MINT::counted_ptr< FitAmpPairList > makeFitAmpPairList()
Definition: FitAmpSum.cpp:404
std::string name() const
Definition: FitAmplitude.h:213
virtual int iFixInit() const =0
bool canBeIgnored() const
virtual std::complex< double > getVal(IDalitzEvent &evt)
virtual MINT::counted_ptr< FitAmpListBase > GetCloneSameFitParameters() const
Definition: FitAmpSum.cpp:116
virtual void addAmps(FitAmplitude *a1, FitAmplitude *a2)
virtual void Gradient(IDalitzEvent &evt, std::vector< double > &grad, MINT::MinuitParameterSet *mps)
Definition: FitAmpSum.cpp:233
IMinuitParameter * getParPtr(unsigned int i)
FitAmpSum & operator=(const FitAmpSum &other)
Definition: FitAmpSum.cpp:105
unsigned int size() const
virtual void print(std::ostream &os=std::cout) const
virtual std::complex< double > getValWithoutFitParameters(IDalitzEvent &evt)
Definition: FitAmplitude.h:196
virtual std::complex< double > getVal(IDalitzEvent &evt)
Definition: FitAmpSum.cpp:182
DecayTree theBareDecay() const
Definition: FitAmplitude.h:166
virtual const std::string & name() const =0
virtual ~FitAmpSum()
Definition: FitAmpSum.cpp:476
virtual void deleteAll()
virtual int addCopyOfSubsetWithSameFitParameters(const FitAmpListBase &other, std::string name, double factor=1)
MINT::counted_ptr< MINT::IReturnRealForEvent< IDalitzEvent > > _efficiency
FitAmpSum(const DalitzEventPattern &pat, const char *fname=0, MINT::MinuitParameterSet *pset=0, const std::string &prefix="", const std::string &lineshapePrefix="", const std::string &opt="")
Definition: FitAmpSum.cpp:20
FitAmpSum operator *(double r, const FitAmpSum &rhs)
Definition: FitAmpSum.cpp:532
bool A_is_in_B(const std::string &a, const std::string &b)
Definition: Utils.cpp:34
std::vector< FitAmplitude * > _fitAmps
void setEfficiency(MINT::counted_ptr< MINT::IReturnRealForEvent< IDalitzEvent > > eff)
double efficiency(IDalitzEvent &evt)
virtual void addAmps(FitAmplitude *a1, FitAmplitude *a2)
virtual bool hidden() const =0
FitAmpList(const DalitzEventPattern &pat, const char *fname=0, MINT::MinuitParameterSet *pset=0, const std::string &prefix="", const std::string &lineshapePrefix="", const std::string &opt="")
Definition: FitAmpList.cpp:19
virtual const DalitzEventPattern & eventPattern() const =0
std::vector< MINT::counted_ptr< FitAmpListBase > > _fitAmpLists
virtual MINT::counted_ptr< IntegCalculator > makeIntegCalculator()
Definition: FitAmpSum.cpp:333
FitAmpSum & operator+=(const FitAmpSum &other)
Definition: FitAmpSum.cpp:484
virtual MINT::counted_ptr< IIntegrationCalculator > makeIntegrationCalculator()
Definition: FitAmpSum.cpp:328
virtual void printNonZero(std::ostream &os=std::cout) const
Definition: FitAmpSum.cpp:306
virtual void printValues(IDalitzEvent &evt, std::ostream &os=std::cout)
Definition: FitAmpSum.cpp:316
double getAmpSqr(IDalitzEvent &evt, std::vector< std::string > ampNames, bool CC=false)
Definition: FitAmpSum.cpp:214
void oneLiner(std::stringstream &seam, int generation=0) const
Definition: DDTree.h:375
FitAmpSum operator *(double r) const
Definition: FitAmpSum.cpp:511
FitAmpSum & operator *=(double r)
Definition: FitAmpSum.cpp:494
virtual unsigned int size() const
void setEfficiency(MINT::counted_ptr< MINT::IReturnRealForEvent< IDalitzEvent > > eff)
virtual bool createAllAmps(const DalitzEventPattern &thePattern, const std::string &prefix="", const std::string &lineshapePrefix="")
Definition: FitAmpList.cpp:125
X * get() const
Definition: counted_ptr.h:123
virtual void multiply(double r)