MINT2
AmpPair.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/AmpPair.h"
4 #include "Mint/Amplitude.h"
5 #include "Mint/IDalitzEvent.h"
6 #include "Mint/NamedParameter.h"
7 
8 #include <iostream>
9 #include <iomanip>
10 #include <fstream>
11 
12 #include <complex>
13 #include <algorithm>
14 
15 //#include <boost/filesystem>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 
19 //#include "boost/filesystem.hpp"
20 
21 using namespace std;
22 using namespace MINT;
23 
25  : _a1(a1)
26  , _a2(a2)
27  , _sum(0)
28  , _sumName("AmpPair._sum")
29  , _sumsq(0)
30  , _sumSqName("AmpPair._sumSq")
31  , _Nevents(0)
32  , _NName("AmpPair._N")
33  , _weightSum(0)
34  , _weightSumName("AmpPair._weightSum")
35  , _hsRe()
36  , _hsIm()
37  , _name("")
38  , _dirName("")
39  , _lastEntry(0)
40 {
41  if(0 == _a1 || 0 == _a2){
42  cout << "ERROR in AmpPair::AmpPair !"
43  << "\n\t One of the amplitude pointers is zero:"
44  << "\n\t _a1 = " << _a1 << ", _a2 = " << _a2
45  << "\n\t ... will crash now" << endl;
46  throw "zero amp pointer in AmpPair constructor";
47  }
48 
49  std::string name_1 = _a1->name();
50  std::string name_2 = _a2->name();
51  if(name_1 > name_2) std::swap(_a1, _a2);
52  // this ensures that the amplitudes are always stored in
53  // a well-defined order. Determines sign of imagninary part.
54  // Since we want Re(A1 A2*), this doesn't matter, however
55  // we have to be consistent when we save and retrive and then add to this.
56  // Also ensures consistent file and directory naming etc.
57  makeName();
58  makeDirName();
59 
62 }
64  : FitParDependent(other)
65  , _a1(other._a1)
66  , _a2(other._a2)
67  , _sum(other._sum)
68  , _sumName(other._sumName)
69  , _sumsq(other._sumsq)
70  , _sumSqName(other._sumSqName)
71  , _Nevents(other._Nevents)
72  , _NName(other._NName)
73  , _weightSum(other._weightSum)
74  , _weightSumName(other._weightSumName)
75  , _hsRe(other._hsRe)
76  , _hsIm(other._hsIm)
77  , _name(other._name)
78  , _dirName(other._dirName)
79  , _lastEntry(other._lastEntry)
80 {
81 
85 }
86 
87 bool AmpPair::isCompatibleWith(const AmpPair& other) const{
88  return (amp1()->name() == other.amp1()->name() &&
89  amp2()->name() == other.amp2()->name());
90 }
91 bool AmpPair::add(const AmpPair& other){
92  if(! isCompatibleWith(other)){
93  cout << "ERROR in AmpPair::add "
94  << "trying to add two incompatible AmpPairs: "
95  << "\n " << this->name() << " + " << other.name()
96  << endl;
97  return false;
98  }
99 
100  _sum += other._sum;
101  _sumsq += other._sumsq;
102  _Nevents += other._Nevents;
103  _weightSum += other._weightSum;
104 
105  _hsRe += other._hsRe;
106  _hsIm += other._hsIm;
107 
108  return true;
109 }
111  , const std::complex<double>& c){
112  _hsRe.addEvent(*evtPtr, c.real());
113  _hsIm.addEvent(*evtPtr, c.imag());
114 }
115 
116 const std::string& AmpPair::makeName(){
117  if(0 == amp1() || 0 == amp2()){
118  cout << "ERROR in AmpPair::makeName():"
119  << " At least one of the amplitude pointers is 0:"
120  << " _a1 = " << _a1 << ", _a2 = " << _a2 << endl;
121  cout << "I'll crash. " << endl;
122  throw "zero amplitude pointers";
123  }
124  _name = "A(" + amp1()->name() + ")_x_A(" + amp2()->name() + ")*";
125  return _name;
126 }
127 
128 const std::string& AmpPair::makeDirName(){
129  _dirName="";
130  if("" == _name) makeName();
131 
132  std::string::const_iterator preEnd = _name.end();
133  preEnd--;
134 
135  for(std::string::const_iterator it = _name.begin();
136  it != _name.end(); it++){
137  if(it != preEnd){
138  std::string::const_iterator next = it; next++;
139  if('-' == *it && '>' == *next){
140  _dirName += "_to_";
141  it++;
142  continue;
143  }
144  }
145  if('*' == *it) _dirName += "star";
146  else if(' ' == *it) _dirName += "_";
147  // else if('(' == *it) _dirName +="_bra_";
148  // else if(')'==*it) _dirName +="_ket_";
149  else if('['==*it) _dirName += "_sqbra_";
150  else if(']'==*it) _dirName += "_sqket_";
151  // else if(','==*it) _dirName += "_comma_";
152  // else if('-' == *it) _dirName += "min";
153  // else if('+' == *it) _dirName += "pls";
154  else _dirName += *it;
155  }
156  return _dirName;
157 }
158 
159 bool AmpPair::makeDirectory(const std::string& asSubdirOf)const{
160  /*
161  A mode is created from or'd permission bit masks defined
162  in <sys/stat.h>:
163  #define S_IRWXU 0000700 RWX mask for owner
164  #define S_IRUSR 0000400 R for owner
165  #define S_IWUSR 0000200 W for owner
166  #define S_IXUSR 0000100 X for owner
167 
168  #define S_IRWXG 0000070 RWX mask for group
169  #define S_IRGRP 0000040 R for group
170  #define S_IWGRP 0000020 W for group
171  #define S_IXGRP 0000010 X for group
172 
173  #define S_IRWXO 0000007 RWX mask for other
174  #define S_IROTH 0000004 R for other
175  #define S_IWOTH 0000002 W for other
176  #define S_IXOTH 0000001 X for other
177 
178  #define S_ISUID 0004000 set user id on execution
179  #define S_ISGID 0002000 set group id on execution
180  #define S_ISVTX 0001000 save swapped text even after use
181  */
182 
183  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO
184  | S_ISUID | S_ISGID;
185  // see above for meaning. I want everybody to be allowed to read/write/exec.
186  // Not sure about the last two bits.
187 
188  int zeroForSuccess = 0;
189  zeroForSuccess |= mkdir( (asSubdirOf ).c_str(), mode );
190  zeroForSuccess |= mkdir( (asSubdirOf + "/" + dirName() ).c_str(), mode );
191  return (0 == zeroForSuccess);
192 }
193 
194 bool AmpPair::save(const std::string& asSubdirOf) const{
195  bool success=true;
196  makeDirectory(asSubdirOf); // ignore error codes from mkdir, they could
197  // be because directory already exists - if
198  // not, we'll pick it up when we try to save
199  // the files.
200 
201  success &= saveValues(asSubdirOf);
202  success &= saveHistos(asSubdirOf);
203 
204  return success;
205 }
206 bool AmpPair::retrieve(const std::string& asSubdirOf){
207  bool dbThis=false;
208  bool success=true;
209 
210  success &= retrieveValues(asSubdirOf);
211  success &= retrieveHistos(asSubdirOf);
212 
213  if(dbThis){
214  cout << "after AmpPair::retrieve: pat = "
215  << histosRe().begin()->second.pattern() << ", "
216  << histosIm().begin()->second.pattern() << endl;
217  }
218  return success;
219 }
220 
221 
222 std::string AmpPair::valueFileName(const std::string& asSubdirOf)const{
223  return asSubdirOf + "/" + dirName() + "/value.txt";
224 }
225 std::string AmpPair::histoReFileName(const std::string& asSubdirOf)const{
226  return asSubdirOf + "/" + dirName() + "/histoRe";
227 }
228 std::string AmpPair::histoImFileName(const std::string& asSubdirOf)const{
229  return asSubdirOf + "/" + dirName() + "/histoIm";
230 }
231 
232 bool AmpPair::saveHistos(const std::string& asSubdirOf) const{
233  bool sc = true;
234  sc |= histosRe().saveAsDir(histoReFileName(asSubdirOf));
235  sc |= histosIm().saveAsDir(histoImFileName(asSubdirOf));
236  return sc;
237 }
238 bool AmpPair::retrieveHistos(const std::string& asSubdirOf){
239  bool sc = true;
240  sc |= histosRe().retrieveFromDir(histoReFileName(asSubdirOf));
241  sc |= histosIm().retrieveFromDir(histoImFileName(asSubdirOf));
242  return sc;
243 }
244 
245 bool AmpPair::saveValues(const std::string& asSubdirOf) const{
246 
247  NamedParameter<double> n_sumRe(_sumName + ".real()"
248  , NamedParameterBase::QUIET);
249  NamedParameter<double> n_sumIm(_sumName + ".imag()"
250  ,NamedParameterBase::QUIET);
251  NamedParameter<double> n_sumSqRe(_sumSqName + ".real()"
252  , NamedParameterBase::QUIET);
253  NamedParameter<double> n_sumSqIm(_sumSqName + ".imag()"
254  , NamedParameterBase::QUIET);
256  , NamedParameterBase::QUIET);
258  , NamedParameterBase::QUIET);
259 
260  n_sumRe = _sum.real();
261  n_sumIm = _sum.imag();
262  n_sumSqRe = _sumsq.real();
263  n_sumSqIm = _sumsq.imag();
264  n_Nevents = _Nevents;
265  n_weightSum = _weightSum;
266 
267  std::string fname = valueFileName(asSubdirOf);
268  ofstream os(fname.c_str());
269  if(os.bad()){
270  cout << "ERROR in AmpPair::saveValues of \n\t" << name()
271  << "\n\t unable to create file: "
272  << "\n\t" << fname << endl;
273  return false;
274  }
275  os << setprecision(20)
276  << name()
277  << '\n' << n_sumRe
278  << '\n' << n_sumIm
279  << '\n' << n_sumSqRe
280  << '\n' << n_sumSqIm
281  << '\n' << n_Nevents
282  << '\n' << n_weightSum
283  << endl;
284  os.close();
285  return true;
286 }
287 bool AmpPair::retrieveValues(const std::string& fromDirectory){
288  bool dbThis=false;
289  std::string fname = valueFileName(fromDirectory);
290  if(dbThis)cout << "trying to retreive values from: " << fname << endl;
291  if(dbThis)cout << "current sum " << _sum << endl;
292 
293  NamedParameter<double> n_sumRe(_sumName + ".real()", fname.c_str()
294  );
295  n_sumRe.reloadFile(fname.c_str());
296  NamedParameter<double> n_sumIm(_sumName + ".imag()", fname.c_str()
297  );//,NamedParameterBase::QUIET);
298  NamedParameter<double> n_sumSqRe(_sumSqName + ".real()", fname.c_str()
299  );//, NamedParameterBase::QUIET);
300  NamedParameter<double> n_sumSqIm(_sumSqName + ".imag()", fname.c_str()
301  );//, NamedParameterBase::QUIET);
302  NamedParameter<long int> n_Nevents(_NName, fname.c_str()
303  );// , NamedParameterBase::QUIET);
304  NamedParameter<double> n_weightSum(_weightSumName, fname.c_str()
305  );//, NamedParameterBase::QUIET);
306 
307  complex<double> n_sum(n_sumRe, n_sumIm);
308 
309  if(dbThis) cout << "n_sumRe " << n_sumRe << "n_sum " << n_sum << endl;
310  _sum = n_sum;
311  if(dbThis) cout << "_sum = " << _sum << endl;
312  complex<double> n_sumsq(n_sumSqRe, n_sumSqIm);
313  _sumsq = n_sumsq;
314  _Nevents = n_Nevents;
315  _weightSum = n_weightSum;
316 
317  return true;
318 }
319 
320 //bool AmpPair::saveHistograms(const std::string& asSubdirOf) const;
321 //}
322 
323 std::complex<double> AmpPair::add(IDalitzEvent* evtPtr
324  , double weight
325  , double efficiency
326  ){
327  bool dbThis=false;
328 
329  _Nevents++;
330  if(0 == evtPtr) return 0;
331 
332  double ps = evtPtr->phaseSpace();
333  if(ps <= 0.0000){
334  if(!(_Nevents%100000)){
335  cout << "WARNING in AmpPair::addToHistograms"
336  << " event with phase space = " << ps << endl;
337  }
338  return 0; // should not happen.
339  }
340 
341  double w = evtPtr->getWeight()
343  w *= weight;
344 
345  _weightSum += w;// / ps;
346 
347  if(dbThis){
348  cout << " AmpPair::add, for pair "
349  << _a1->name() << " / " << _a2->name()
350  << endl;
351  }
352 
353  complex<double> c=ampValue(evtPtr) * efficiency * w;
354  _lastEntry = c;
355  _sum += c;
356  this->addToHistograms(evtPtr, c);
357 
358  if(dbThis){
359  cout << "\t c = " << c
360  << " _sum " << _sum
361  << endl;
362  }
363  complex<double> csq(c.real()*c.real(), c.imag()*c.imag());
364  _sumsq += csq;
365 
366  return c;
367 }
368 
369 std::complex<double> AmpPair::add(const counted_ptr<IDalitzEvent>& evtPtr
370  , double weight
371  , double efficiency
372  ){
373  return add(evtPtr.get(), weight, efficiency);
374 }
375 complex<double> AmpPair::lastEntry() const{
376  return _lastEntry;
377 }
378 complex<double> AmpPair::ampValue(IDalitzEvent* evtPtr){
379  if(0 == _a1 || 0 == _a2 || 0 == evtPtr) return 0;
380 
381 
382  complex<double> c1 = _a1->getVal(*evtPtr);
383  complex<double> c2 = _a2->getVal(*evtPtr);
384  complex<double> c2star = conj(c2);
385 
386  complex<double> val = (c1 * c2star);
387 
388  return val;
389 }
390 
391 int AmpPair::oneOrTwo()const{
392  if(isSingleAmp()) return 1;
393  else return 2;
394 }
395 
396 std::complex<double> AmpPair::integral() const{
397  bool dbThis=false;
398  if(dbThis){
399  cout << " AmpPair::sumWithoutFitPars for pair "
400  << _a1->name() << " / " << _a2->name()
401  << endl;
402  }
403  double dN = (double) _Nevents;
404  std::complex<double> total = ((std::complex<double>)oneOrTwo()) * _sum;
405  std::complex<double> returnVal;
406 
407  if(_weightSum > 0){
408  returnVal = total/_weightSum;
409  }else{
410  returnVal = total/dN;
411  }
412 
413  if(dbThis){
414  cout << "\t returning " << returnVal << endl;
415  cout << "\t = Real( " << oneOrTwo()
416  << " * " << _sum
417  << " / " << _Nevents
418  << " )"
419  << endl;
420  }
421  return returnVal;
422 }
423 
424 double AmpPair::weightSum() const{
425  if(_weightSum > 0) return _weightSum;
426  else return (double) _Nevents;
427 }
428 
429 long int AmpPair::N() const{
430  return _Nevents;
431 }
432 
433 std::complex<double> AmpPair::variance() const{
434  if(_Nevents <=0) return 0;
435  double dN = (double) _Nevents;
436  complex<double> mean = _sum/dN;
437  complex<double> meansq = _sumsq/dN;
438 
439  complex<double> var(meansq.real() - mean.real()*mean.real()
440  , meansq.imag() - mean.imag()*mean.imag());
441 
442  var /= dN;
443 
444  if(_weightSum > 0) var *= dN*dN/(_weightSum*_weightSum);
445 
446  return ((double) oneOrTwo()) * var;
447 }
448 
449 bool AmpPair::isSingleAmp() const{
450  return _a1 == _a2;
451 }
452 
454  if(_a1->theBareDecay().getVal() == _a2->theBareDecay().getVal() )return true;
455  return false;
456 }
457 
458 const std::string& AmpPair::name(){
459  if("" == _name) makeName();
460  return _name;
461  // if(isSingleAmp()) return "| " + _a1->name() + " |^2";
462  // else return "(" + _a1->name() +") * (" + _a2->name() + ")*";
463 }
464 const std::string& AmpPair::name() const{
465  return _name;
466 }
467 
468 const std::string& AmpPair::dirName(){
469  if("" == _dirName) makeDirName();
470  return _dirName;
471 }
472 const std::string& AmpPair::dirName() const{
473  return _dirName;
474 }
475 
476 /*
477 // the following routines are needed to
478 // calculate the variance
479 double AmpPair::ReSquared() const{
480  return pow(oneOrTwo() * fitParValue().real(), 2)*_sumsq.real();
481 }
482 double AmpPair::ImSquared() const{
483  return pow(oneOrTwo() * fitParValue().imag(), 2)*_sumsq.imag();
484 }
485 double AmpPair::ImRe() const{
486  return oneOrTwo() * oneOrTwo()
487  * fitParValue().real()*_sum.real()
488  * fitParValue().imag()*_sum.imag();
489 
490 }
491 */
492 
493 void AmpPair::print(std::ostream& os) const{
494  os << "AmpPair " << name()
495  << ": N=" << N() << ", integral=" << integral();
496 }
498  , const AmpPair& b) const{
499  return abs(a.integral()) < abs(b.integral());
500 }
501 
503 
504  if(0 == a && 0==b) return false;
505  if(0 == a && 0!=b) return true;
506  if(0 !=a && 0==b) return false;
507  return abs(a->integral()) < abs(b->integral());
508 }
509 bool lessByAmpPairIntegral_ptr_int_pair::operator()(const std::pair<AmpPair*, int>& apair
510  , const std::pair<AmpPair*, int>& bpair) const{
511  const AmpPair* a = apair.first;
512  const AmpPair* b = bpair.first;
513 
514  if(0 == a && 0==b) return false;
515  if(0 == a && 0!=b) return true;
516  if(0 !=a && 0==b) return false;
517  return abs(a->integral()) < abs(b->integral());
518 }
519 
520 
522  this->add(other);
523  return *this;
524 }
525 AmpPair AmpPair::operator+(const AmpPair& other) const{
526  AmpPair returnVal(*this);
527  returnVal += other;
528  return returnVal;
529 }
530 
531 std::ostream& operator<<(std::ostream& os, const AmpPair& fap){
532  fap.print(os);
533  return os;
534 }
535 //
std::complex< double > lastEntry() const
Definition: AmpPair.cpp:375
bool hasMatchingPattern() const
Definition: AmpPair.cpp:453
bool retrieve(const std::string &asSubdirOf=".")
Definition: AmpPair.cpp:206
DalitzHistoSet _hsIm
Definition: AmpPair.h:38
long int _Nevents
Definition: AmpPair.h:31
Amplitude * amp1()
Definition: AmpPair.h:102
bool operator()(const AmpPair *a, const AmpPair *b) const
Definition: AmpPair.cpp:502
bool retrieveHistos(const std::string &asSubdirOf=".")
Definition: AmpPair.cpp:238
std::string name() const
Definition: Amplitude.cpp:446
const std::string & makeName()
Definition: AmpPair.cpp:116
virtual double getWeight() const =0
bool add(const AmpPair &other)
Definition: AmpPair.cpp:91
std::string _sumSqName
Definition: AmpPair.h:29
virtual double phaseSpace() const =0
bool save(const std::string &asSubdirOf=".") const
Definition: AmpPair.cpp:194
DalitzHistoSet _hsRe
Definition: AmpPair.h:37
Amplitude * amp2()
Definition: AmpPair.h:103
bool saveHistos(const std::string &asSubdirOf=".") const
Definition: AmpPair.cpp:232
std::complex< double > _lastEntry
Definition: AmpPair.h:43
std::complex< double > _sum
Definition: AmpPair.h:25
std::string valueFileName(const std::string &asSubdirOf) const
Definition: AmpPair.cpp:222
std::string histoReFileName(const std::string &asSubdirOf) const
Definition: AmpPair.cpp:225
long int N() const
Definition: AmpPair.cpp:429
AmpPair & operator+=(const AmpPair &other)
Definition: AmpPair.cpp:521
bool makeDirectory(const std::string &asSubdirOf=".") const
Definition: AmpPair.cpp:159
const DalitzHistoSet & histosIm() const
Definition: AmpPair.h:49
const ValueType & getVal() const
Definition: DDTree.h:102
const std::string & dirName() const
Definition: AmpPair.cpp:472
bool isCompatibleWith(const AmpPair &other) const
Definition: AmpPair.cpp:87
AmpPair(Amplitude *a1=0, Amplitude *a2=0)
Definition: AmpPair.cpp:24
Amplitude * _a1
Definition: AmpPair.h:22
std::complex< double > integral() const
Definition: AmpPair.cpp:396
virtual std::complex< double > getVal(IDalitzEvent &evt)
Definition: Amplitude.h:122
std::ostream & operator<<(std::ostream &os, const AmpPair &fap)
Definition: AmpPair.cpp:531
bool retrieveFromDir(const std::string &asSubDirOf=".")
virtual bool registerFitParDependence(const IFitParDependent &fpd)
const std::string & name() const
Definition: AmpPair.cpp:464
bool operator()(const AmpPair &a, const AmpPair &b) const
Definition: AmpPair.cpp:497
bool saveValues(const std::string &asSubdirOf=".") const
Definition: AmpPair.cpp:245
double _weightSum
Definition: AmpPair.h:34
Amplitude * _a2
Definition: AmpPair.h:23
std::string _NName
Definition: AmpPair.h:32
void addToHistograms(IDalitzEvent *evtPtr, const std::complex< double > &c)
Definition: AmpPair.cpp:110
virtual double getGeneratorPdfRelativeToPhaseSpace() const =0
AmpPair operator+(const AmpPair &other) const
Definition: AmpPair.cpp:525
bool isSingleAmp() const
Definition: AmpPair.cpp:449
void addEvent(const IDalitzEvent &evt, double weight=1)
std::string _name
Definition: AmpPair.h:40
bool reloadFile(const std::string &id)
int oneOrTwo() const
Definition: AmpPair.cpp:391
std::map< Key, Val >::iterator begin()
Definition: PolymorphMap.h:26
std::complex< double > _sumsq
Definition: AmpPair.h:28
const std::string & makeDirName()
Definition: AmpPair.cpp:128
std::complex< double > variance() const
Definition: AmpPair.cpp:433
bool saveAsDir(const std::string &asSubdirOf=".") const
std::string _dirName
Definition: AmpPair.h:41
DecayTree theBareDecay() const
Definition: Amplitude.h:157
const DalitzHistoSet & histosRe() const
Definition: AmpPair.h:48
std::complex< double > ampValue(IDalitzEvent *evtPtr)
Definition: AmpPair.cpp:378
virtual void print(std::ostream &os=std::cout) const
Definition: AmpPair.cpp:493
bool retrieveValues(const std::string &fromDirectory=".")
Definition: AmpPair.cpp:287
X * get() const
Definition: counted_ptr.h:123
std::string _sumName
Definition: AmpPair.h:26
std::string histoImFileName(const std::string &asSubdirOf) const
Definition: AmpPair.cpp:228
std::string _weightSumName
Definition: AmpPair.h:35
bool operator()(const std::pair< AmpPair *, int > &a, const std::pair< AmpPair *, int > &b) const
Definition: AmpPair.cpp:509
double weightSum() const
Definition: AmpPair.cpp:424