MINT2
FitAmpPairCovariance.cpp
Go to the documentation of this file.
1 
3 #include "Mint/FitAmpPairList.h"
4 #include "Mint/Utils.h"
5 
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <fstream>
12 
13 using namespace std;
14 using namespace MINT;
15 
16 unsigned int FitAmpPairCovariance::_maxSize = 1500;
17 
19  : _myList(myList)
20  , _Nevents(0)
21  , _size()
22  , _needToRecalculate(true)
23  , _sum_x(1, 1)
24  , _sum_xy(1)
25  , _cov2N(2)
26  , _corr2N(2)
27  , _cov(1)
28  , _corr(1)
29  , _fractionCov(1)
30  , _fractionSumCov(1)
31  , _integralCov(1)
32  , _dN_by_d2N(1, 2)
33  , _dFraction_by_dN(1, 1)
34  , _dFractionSum_by_dFraction(1, 1)
35  , _dIntegral_by_dN(1, 1)
36 {
37  if(0 != _myList && _myList->size() >=1) resize();
38 }
40  : _myList(other._myList)
41  , _Nevents(other._Nevents)
42  , _size(other._size)
43  , _needToRecalculate(true)
44  , _sum_x(other._sum_x)
45  , _sum_xy(other._sum_xy)
46  , _cov2N(other._cov2N)
47  , _corr2N(other._corr2N)
48  , _cov(other._cov)
49  , _corr(other._corr)
50  , _fractionCov(other._fractionCov)
51  , _fractionSumCov(other._fractionSumCov)
52  , _integralCov(other._integralCov)
53  , _dN_by_d2N(other._dN_by_d2N)
54  , _dFraction_by_dN(other._dFraction_by_dN)
55  , _dFractionSum_by_dFraction(other._dFractionSum_by_dFraction)
56  , _dIntegral_by_dN(other._dIntegral_by_dN)
57 {
58 }
60  , const FitAmpPairList* newList)
61  : _myList(newList)
62  , _Nevents(other._Nevents)
63  , _size(other._size)
64  , _needToRecalculate(true)
65  , _sum_x(other._sum_x)
66  , _sum_xy(other._sum_xy)
67  , _cov2N(other._cov2N)
68  , _corr2N(other._corr2N)
69  , _cov(other._cov)
70  , _corr(other._corr)
71  , _fractionCov(other._fractionCov)
72  , _fractionSumCov(other._fractionSumCov)
73  , _integralCov(other._integralCov)
74  , _dN_by_d2N(other._dN_by_d2N)
75  , _dFraction_by_dN(other._dFraction_by_dN)
76  , _dFractionSum_by_dFraction(other._dFractionSum_by_dFraction)
77  , _dIntegral_by_dN(other._dIntegral_by_dN)
78 {
79 }
80 
82  clearAll();
83 }
85  other) const{
86  if(0 == _myList && 0 == other._myList) return true;
87  if(0 == _myList || 0 == other._myList) return false;
88  if(_myList->size() != other._myList->size()) return false;
89 
90  if(! _myList->isCompatibleWith(*(other._myList))) return false;
91 
92  // the following check should be redundant:
93  for(unsigned int i=0; i < this->size(); i++){
94  if(realName(i) != other.realName(i)) return false;
95  if(imagName(i) != other.imagName(i)) return false;
96  }
97  return true;
98 }
100  if(0 == _myList || 0 == other._myList) {
101  cout << "WARNING in FitAmpPairCovariance::add: "
102  << "trying to add incompatible FitAmpPairCovariances"
103  << "\n\t _myListPtr = " << _myList << " other's: " << other._myList
104  << endl;
105  return false;
106  }
107  if(! isCompatibleWith(other)){
108  cout << "WARNING in FitAmpPairCovariance::add: "
109  << "trying to add incompatible FitAmpPairCovariances"
110  << endl;
111  return false;
112  }
113  _needToRecalculate = true;
114  _Nevents += other._Nevents;
115  _sum_x += other._sum_x;
116  _sum_xy += other._sum_xy;
117 
118  return true;
119 }
120 
121 unsigned int FitAmpPairCovariance::size() const{
122  return _size;
123 }
125  bool dbThis=false;
126  if(dbThis){
127  cout << "FitAmpPairCovariance::resize()"
128  << " resizing from " << size()
129  << " to " << _myList->size()
130  << " with maxSize() = " << maxSize()
131  << endl;
132  }
133 
134  _needToRecalculate=true;
135 
136  _size = _myList->size();
137 
138  if(size() > maxSize()){
139  cout << "FitAmpPairCovariance::resize size() = " << size()
140  << " that's too big! won't do it!" << endl;
141  return false;
142  }
143 
144  _sum_x. ResizeTo(size()*2, 1);
145  _sum_xy.ResizeTo(size()*2, size()*2);
146  _cov2N. ResizeTo(size()*2, size()*2);
147  _corr2N.ResizeTo(size()*2, size()*2);
148 
149  _cov. ResizeTo(size(), size());
150  _corr. ResizeTo(size(), size());
151 
152  _fractionCov. ResizeTo(size(), size());
153  _fractionSumCov. ResizeTo(1,1);
154  _integralCov. ResizeTo(1,1);
155 
156  _dN_by_d2N. ResizeTo(size(), size()*2);
157  _dFraction_by_dN. ResizeTo(size(), size());
158 
159  _dFractionSum_by_dFraction.ResizeTo(1, size());
160 
161  _dIntegral_by_dN. ResizeTo(1, size());
162 
163  if(dbThis){
164  cout << "FitAmpPairCovariance::resize() to "
165  << size() << " done." << endl;
166  }
167  return true;
168 }
169 
170 std::string FitAmpPairCovariance::realName(int listPosition) const{
171  return _myList->at(listPosition).name() + "_real";
172 }
173 std::string FitAmpPairCovariance::imagName(int listPosition) const{
174  return _myList->at(listPosition).name() + "_imag";
175 }
176 
178  bool dbThis=false;
179  if(size() != _myList->size()) {
180  if(dbThis) cout << "FitAmpPairCovariance::addLastEventFromList() size() "
181  << size() << " == " << _myList->size() << endl;
182  if(! resize()) return false;
183  }
184  _needToRecalculate = true;
185  _Nevents++;
186  if(dbThis)cout << "FitAmpPairCovariance::addLastEventFromList()"
187  << " adding event to cov matrix." << endl;
188 
189  if(! isValid()) return false;
190  for(unsigned int i=0; i < _myList->size(); i++){
191  if(dbThis) cout << " i= " << i << endl;
192  double re_i = _myList->at(i).lastEntry().real();
193  double im_i = _myList->at(i).lastEntry().imag();
194  _sum_x(indexReal(i), 0) += re_i;
195  _sum_x(indexImag(i), 0) += im_i;
196 
197  for(unsigned int j=0; j < _myList->size(); j++){
198  double re_j = _myList->at(j).lastEntry().real();
199  double im_j = _myList->at(j).lastEntry().imag();
200 
201  _sum_xy(indexReal(i), indexReal(j)) += re_i * re_j;
202  _sum_xy(indexReal(i), indexImag(j)) += re_i * im_j;
203  _sum_xy(indexImag(i), indexReal(j)) += im_i * re_j;
204  _sum_xy(indexImag(i), indexImag(j)) += im_i * im_j;
205  }
206  }
207  return true;
208 }
209 
211  bool dbThis=false;
212  if(! isValid()) return false;
213  bool sc=true;
214  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
215  << " making dN_by_d2N" << endl;
216  sc &= make_dN_by_d2N();
217 
218  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
219  << " making dFraction_by_dN" << endl;
220  sc &= make_dFraction_by_dN();
221 
222  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
223  << " making dFractionSum_by_dFraction" << endl;
225 
226  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
227  << " making dIntegral_by_dN" << endl;
228  sc &= make_dIntegral_by_dN();
229 
230  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
231  << " making 2NCovariance" << endl;
232  sc &= make2NCovariance();
233  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
234  << " making NCovariance" << endl;
235  sc &= makeNCovariance();
236  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
237  << " making FractionCovariance" << endl;
238  sc &= makeFractionCovariance();
239  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
240  << " making FractionSumCovariance" << endl;
242  if(dbThis) cout << "FitAmpPairCovariance::calculateAll"
243  << " making IntegralCovariance" << endl;
244  sc &= makeIntegralCovariance();
245 
246 
247 
248  //_needToRecalculate=false;
249  return sc;
250 }
252  bool dbThis=false;
253  if(dbThis) cout << "FitAmpPairCovariance::make_dN_by_d2N() called" << endl;
254 
255  /*
256  each FitAmpPair is evaluated as the real part of
257  the product of two complex numbers.
258  The product of fit parameters: oneOrTwo * (x_1 + iy_1)*(x_2 - iy_2)
259  The integral: a + ib
260  So we want to transfrom from (a, b) to c = xa - yb.
261 
262  Here we do the covariance due to the statistical uncertainty
263  in the MC integration, not the uncertainy in the fit parameters x,y.
264 
265  So we calculate d(c) by d(a,b) = (x, -y)
266  */
267  for(unsigned int i=0; i < _myList->size(); i++){
268  complex<double> fpv =
269  _myList->at(i).fitParValue()
270  * (double)(_myList->at(i).oneOrTwo());
271 
272  _dN_by_d2N(i, indexReal(i)) = fpv.real();
273  _dN_by_d2N(i, indexImag(i)) = - fpv.imag();
274  }
275  return true;
276 }
277 
279  bool dbThis=false;
280  if(dbThis) cout << "FitAmpPairCovariance::make_dFraction_by_dN() called"
281  << endl;
282  /*
283  We are calculating trafo matrix for the following trafo:
284 
285  f_a = a/(a + b + c...)
286 
287 
288  diagonal terms:
289 
290  d(f_a)/d(a) = 1/(a + b + c +...) - a/(a + b + c ...)^2
291 
292  off-diagonal terms:
293 
294  d(f_a)/d(b) = -a/(a + b + c ...)^2
295  */
296 
297  double integ = _myList->integral();
298  double integ2 = integ*integ;
299  for(unsigned int i=0; i < _myList->size(); i++){
300  double num = _myList->at(i).integral();
301  for(unsigned int j=0; j < _myList->size(); j++){
302  _dFraction_by_dN(i, j) = -num/integ2;
303  }
304  _dFraction_by_dN(i, i) += 1.0/integ;
305  }
306  return true;
307 }
308 
310  bool dbThis=false;
311  if(dbThis) cout << "FitAmpPairCovariance::make_dIntegral_by_dN() called"
312  << endl;
313  /*
314  We are calculating trafo matrix for the following trafo:
315 
316  sum = a + b + c...
317 
318  */
319 
320  for(unsigned int i=0; i < _myList->size(); i++){
321  _dIntegral_by_dN(0, i) = 1;
322  }
323  return true;
324 }
325 
327  bool dbThis=false;
328  if(dbThis) cout << "FitAmpPairCovariance::make_dFractionSum_by_dFraction()"
329  << " called" << endl;
330  /*
331  We are calculating trafo matrix for the trafo to the sum
332  of all terms entering the fraction calculation, this
333  excludes the terms for interference effect.
334 
335  It's just ones (those included) and zeros (for those not included)
336  */
337 
338  for(unsigned int i=0; i < _myList->size(); i++){
339  if(_myList->at(i).isSingleAmp()){
340  _dFractionSum_by_dFraction(0, i) = 1.0;
341  }else{
342  _dFractionSum_by_dFraction(0, i) = 0.0;
343  }
344  }
345  return true;
346 }
347 
348 
350  bool dbThis=false;
351  if(dbThis) cout << "making 2N covariance" << endl;
352 
353  double dN = _myList->numEvents();
354  TMatrixT<float> mean_x(_myList->size()*2, 1);
355  TMatrixTSym<float> mean_xy(_myList->size()*2);
356 
357  mean_x = _sum_x;
358  mean_x *= 1./dN;
359 
360  mean_xy = _sum_xy;
361  mean_xy *= 1./dN;
362 
363  for(unsigned int i=0; i < _myList->size()*2; i++){
364  for(unsigned int j=0; j < _myList->size()*2; j++){
365  _cov2N(i, j) = (mean_xy(i,j) - mean_x(i,0)*mean_x(j,0));
366  // above: cov of the distribution
367  // now: .. of the mean
368  _cov2N(i, j) /= dN;
369  // now taking into account other factors that I muliply the result
370  // with.
371  int il = listPositionFromIndex(i);
372  int jl = listPositionFromIndex(j);
373  double sfden = _myList->at(il).weightSum() * _myList->at(jl).weightSum();
374  if(sfden > 0){
375  _cov2N(i, j) *= dN*dN/sfden;
376  }
377  }
378  }
379  for(unsigned int i=0; i < _myList->size()*2; i++){
380  for(unsigned int j=0; j < _myList->size()*2; j++){
381  double den = _cov2N(i,i)*_cov2N(j,j);
382  if(den > 0){
383  _corr2N(i,j) = _cov2N(i,j)/sqrt(den);
384  }
385  }
386  }
387 
388  if(dbThis){
389  cout << "After " << _Nevents << "events" << endl;
390  cout << "mean matrix" << endl;
391  mean_x.Print();
392  cout << "Covariance Matrix 2N" << endl;
393  _cov2N.Print();
394  cout << "Correlation Matrix 2N" << endl;
395  _corr2N.Print();
396  }
397  return true;
398 
399 }
400 
402  bool dbThis=false;
403 
404  bool success = true;
405  // success &= make2NCovariance();
406  // success &= make_dN_by_d2N();
407 
408  TMatrixT<float> dN_by_d2N_Transpose(2*size(), size());
409  dN_by_d2N_Transpose.Transpose(_dN_by_d2N);
410  if(dbThis){
411  cout << "FitAmpPairCovariance::makeNCovariance()"
412  << " _dN_by_d2N(): "
413  << endl;
414  _dN_by_d2N.Print();
415  cout << "and its transpose:" << endl;
416  dN_by_d2N_Transpose.Print();
417  }
418 
419  _cov = makeTMatrixTSym(_dN_by_d2N * _cov2N * dN_by_d2N_Transpose);
420 
421  for(unsigned int i=0; i < _myList->size(); i++){
422  if(_cov(i, i) <= 0) continue;
423  for(unsigned int j=0; j < _myList->size(); j++){
424  double den = _cov(i,i)*_cov(j,j);
425  if(den > 0){
426  _corr(i,j) = _cov(i,j)/sqrt(den);
427  }
428  }
429  }
430 
431  if(dbThis){
432  cout << "After " << _Nevents << "events" << endl;
433  cout << "Covariance Matrix N" << endl;
434  _cov.Print();
435  cout << "Correlation Matrix N" << endl;
436  _corr.Print();
437  }
438  return success;
439 }
440 
442  bool dbThis=false;
443 
444  TMatrixT<float> dFraction_by_dN_Transpose(size(), size());
445  dFraction_by_dN_Transpose.Transpose(_dFraction_by_dN);
446 
448  dFraction_by_dN_Transpose);
449 
450  if(dbThis){
451  cout << "Correlation Matrix Fractions" << endl;
452  _fractionCov.Print();
453  }
454 
455  return true;
456 }
458  bool dbThis=false;
459 
460  TMatrixT<float> dFractionSum_by_dFraction_Transpose(size(), 1);
461  dFractionSum_by_dFraction_Transpose.Transpose(_dFractionSum_by_dFraction);
462 
464  _fractionCov *
465  dFractionSum_by_dFraction_Transpose);
466 
467  if(dbThis){
468  cout << "After " << _Nevents << "events" << endl;
469  cout << "Covariance Matrix for FractionSum" << endl;
470  _fractionSumCov.Print();
471  }
472 
473  return true;
474 }
475 
477  bool dbThis=false;
478 
479  TMatrixT<float> dIntegral_by_dN_Transpose(size(), 1);
480  dIntegral_by_dN_Transpose.Transpose(_dIntegral_by_dN);
481 
483  _cov
484  * dIntegral_by_dN_Transpose);
485 
486  if(dbThis){
487  cout << "Correlation Matrix Integral" << endl;
488  _fractionCov.Print();
489  }
490 
491  return true;
492 }
493 
494 
497  if(i < 0 || i >= _fractionCov.GetNcols()) return -9999.0;
498  if(i < 0 || i >= _fractionCov.GetNrows()) return -9999.0;
499  return _fractionCov(i, i);
500 }
502  double V = getFractionVariance(i);
503  if(V < 0){
504  cout << "ERROR in FitAmpPairCovariance::getFractionError"
505  << " variance is " << V << " which is < 0."
506  << " will return -9999."
507  << endl;
508  return -9999.0;
509  }
510  return sqrt(V);
511 }
512 
515  return _fractionSumCov(0,0);
516 }
517 
519  double V = getFractionSumVariance();
520  if(V < 0){
521  cout << "ERROR in FitAmpPairCovariance::getFractionSumError"
522  << " variance is " << V << " which is < 0."
523  << " will return 0."
524  << endl;
525  return 0;
526  }
527  return sqrt(V);
528 }
529 
531  bool dbThis=false;
532  if(dbThis) cout << "need to recalculate ? " << _needToRecalculate << endl;
534  if(_integralCov.GetNcols() < 1 || _integralCov.GetNrows() < 1) return -9999.0;
535  return _integralCov(0,0);
536 }
538  double V = getIntegralVariance();
539  if(V < 0){
540  cout << "ERROR in FitAmpPairCovariance::getFractionError"
541  << " variance is " << V << " which is < 0."
542  << " will return -9999."
543  << endl;
544  return -9999.0;
545  }
546  return sqrt(V);
547 }
548 
550  bool dbThis=false;
551  static int numWarnings=0;
552  const static int maxWarnings=10;
553  if(dbThis){
554  cout << " FitAmpPairCovariance::isValid() with: "
555  << size() << " == " << _sum_x.GetNcols() << " ? " << endl;
556  }
557  if(size() > maxSize()){
558  if(numWarnings < 10){
559  cout << "WARNING: FitAmpPairCovariance::isValid returns false"
560  << " because size (" << size() << ") is too big"
561  << endl;
562  cout << " Will print this " << maxWarnings
563  << " times (" << numWarnings << ")." << endl;
564  }
565  numWarnings++;
566  return false;
567  }
568  if(size() <= 0)return false;
569  //if((unsigned int) _sum_x.GetNcols() != 2*size()) return false;
570  return true;
571 }
572 
574  _Nevents=0;
575  _needToRecalculate=true;
576  resize();
577  if(0 != this->size()){
578  _sum_x *= 0;
579  _sum_xy *= 0;
580  }
581  return 0;
582 }
584  clearAll();
585  resize();
586  return true;
587 }
588 
589 
590 bool FitAmpPairCovariance::makeDirectory(const std::string& asSubdirOf)const{
591  /*
592  A mode is created from or'd permission bit masks defined
593  in <sys/stat.h>:
594  #define S_IRWXU 0000700 RWX mask for owner
595  #define S_IRUSR 0000400 R for owner
596  #define S_IWUSR 0000200 W for owner
597  #define S_IXUSR 0000100 X for owner
598 
599  #define S_IRWXG 0000070 RWX mask for group
600  #define S_IRGRP 0000040 R for group
601  #define S_IWGRP 0000020 W for group
602  #define S_IXGRP 0000010 X for group
603 
604  #define S_IRWXO 0000007 RWX mask for other
605  #define S_IROTH 0000004 R for other
606  #define S_IWOTH 0000002 W for other
607  #define S_IXOTH 0000001 X for other
608 
609  #define S_ISUID 0004000 set user id on execution
610  #define S_ISGID 0002000 set group id on execution
611  #define S_ISVTX 0001000 save swapped text even after use
612  */
613 
614  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO
615  | S_ISUID | S_ISGID;
616  // see above for meaning. I want everybody to be allowed to read/write/exec.
617  // Not sure about the last two bits.
618 
619  int zeroForSuccess = 0;
620  zeroForSuccess |= mkdir( (asSubdirOf ).c_str(), mode );
621  zeroForSuccess |= mkdir( (dirName(asSubdirOf) ).c_str(), mode );
622  return (0 == zeroForSuccess);
623 }
624 
625 std::string FitAmpPairCovariance::dirName(const std::string& asSubdirOf) const{
626  return asSubdirOf +"/" + "FitAmpPairCovariance";
627 }
628 std::string FitAmpPairCovariance::matrix_x_fname(const std::string& asSubdirOf) const{
629  return dirName(asSubdirOf) + "/" + "matrix_x.txt";
630 }
631 std::string FitAmpPairCovariance::matrix_xy_fname(const std::string& asSubdirOf) const{
632  return dirName(asSubdirOf) + "/" + "matrix_xy.txt";
633 }
634 
635 bool FitAmpPairCovariance::save(const std::string& inDirectory)const{
636  bool dbThis=false;
637  if(! isValid()){
638  cout << "WARNING FitAmpPairCovariance::save(" << inDirectory <<")"
639  << " I'm not valid, I won't save anything." << endl;
640  return false;
641  }
642  if(dbThis){
643  cout << "saving FitAmpPairCovariance in " << inDirectory << "." << endl;
644  }
645  makeDirectory(inDirectory);
646  bool sc=true;
647  sc &= save_sum_x(inDirectory);
648  sc &= save_sum_xy(inDirectory);
649 
650  return true;
651 }
652 
653 bool FitAmpPairCovariance::save_sum_x(const std::string & inDirectory)const{
654  bool dbThis=false;
655  if(! isValid()){
656  cout << "WARNING FitAmpPairCovariance::save_sum_x(" << inDirectory <<")"
657  << " I'm not valid, I won't save anything." << endl;
658  return false;
659  }
660  ofstream os(matrix_x_fname(inDirectory).c_str());
661  os << "N " << _Nevents << endl;
662  if(dbThis){
663  cout << "about to save: ";
664  _sum_x.Print();
665  }
666  for(unsigned int i=0; i < _myList->size(); i++){
667 
668  os << setprecision(20) << realName(i) << " "
669  << _sum_x(indexReal(i), 0) << endl;
670 
671  os << setprecision(20) << imagName(i) << " "
672  << _sum_x(indexImag(i), 0) << endl;
673  }
674  os.close();
675  return true;
676 }
677 
678 bool FitAmpPairCovariance::save_sum_xy(const std::string & inDirectory)const{
679  bool dbThis=false;
680  if(! isValid()){
681  cout << "WARNING FitAmpPairCovariance::save_sum_xy(" << inDirectory <<")"
682  << " I'm not valid, I won't save anything." << endl;
683  return false;
684  }
685  ofstream os(matrix_xy_fname(inDirectory).c_str());
686  os << "N " << _Nevents << endl;
687  if(dbThis){
688  cout << "about to save: ";
689  _sum_xy.Print();
690  }
691  for(unsigned int i=0; i < _myList->size(); i++){
692  for(unsigned int j=0; j < _myList->size(); j++){
693 
694  os << setprecision(20)<< realName(i) << " " << realName(j) << " "
695  << _sum_xy(indexReal(i), indexReal(j)) << endl;
696 
697  os << setprecision(20)<< realName(i) << " " << imagName(j) << " "
698  << _sum_xy(indexReal(i), indexImag(j)) << endl;
699 
700  os << setprecision(20)<< imagName(i) << " " << realName(j) << " "
701  << _sum_xy(indexImag(i), indexReal(j)) << endl;
702 
703  os << setprecision(20)<< imagName(i) << " " << imagName(j) << " "
704  << _sum_xy(indexImag(i), indexImag(j)) << endl;
705  }
706  }
707  os.close();
708  return true;
709 }
710 
711 bool FitAmpPairCovariance::retrieve(const std::string& inDirectory){
712  if(! resize()) return false;
713  if(! retrieve_sum_x(inDirectory)) return false;
714  if(! retrieve_sum_xy(inDirectory)) return false;
715  _needToRecalculate=true;
716  return true;
717 }
718 
719 bool FitAmpPairCovariance::retrieve_sum_x(const std::string & inDirectory){
720  bool dbThis=false;
721  if(size() > maxSize()) return false;
722  ifstream is(matrix_x_fname(inDirectory).c_str());
723  if(is.bad()) return false;
724 
725  if(dbThis) cout << "opened file: "
726  << "\n" << matrix_x_fname(inDirectory)
727  << endl;
728  std::string st1;
729  double val;
730  std::map<string, double> map_x;
731 
732  int counter=0;
733  while(is){
734  if(dbThis) cout << "reading " << counter << ". line" << endl;
735  if(0 == counter){
736  is >> st1 >> _Nevents;
737  if(dbThis){
738  cout << "just read " << st1 << " " << _Nevents << endl;
739  }
740  }else{
741  is >> st1 >> val;
742  map_x[st1] = val;
743  if(dbThis){
744  cout << "just read " << st1 << " " << val << endl;
745  }
746  }
747  counter++;
748  }
749  is.close();
750  if(dbThis)cout << "retrieve_sum_x, filling _sum_x for size "
751  << size() << endl;
752  for(unsigned int i=0; i < size(); i++){
753  _sum_x(indexReal(i), 0) = map_x[realName(i)];
754  _sum_x(indexImag(i), 0) = map_x[imagName(i)];
755  if(dbThis){
756  cout << "Tried to retrieve from map entry for " << realName(i)
757  << " \n got: " << map_x[realName(i)] << endl;
758  }
759 
760  }
761  if(dbThis){
762  cout << " did so, look at what I got for sum_x" << endl;
763  _sum_x.Print();
764  }
765  _needToRecalculate=true;
766  return true;
767 }
768 bool FitAmpPairCovariance::retrieve_sum_xy(const std::string & inDirectory){
769  bool dbThis=false;
770  if(size() > maxSize()) return false;
771  ifstream is(matrix_xy_fname(inDirectory).c_str());
772  if(is.bad()) return false;
773  if(dbThis) cout << "opened file: "
774  << "\n" << matrix_xy_fname(inDirectory)
775  << endl;
776  std::string st1, st2;
777  double val;
778  map< string, map<string, double> > map_xy;
779 
780  int counter=0;
781  while(is){
782  if(0 == counter){
783  is >> st1 >> _Nevents;
784  }else{
785  is >> st1 >> st2 >> val;
786  map_xy[st1][st2] = val;
787  if(dbThis) cout << "just read " << counter << ". line: "
788  << st1 << " " << st2 << " " << val << endl;
789  }
790  counter++;
791  }
792 
793  is.close();
794  for(unsigned int i=0; i < size(); i++){
795  for(unsigned int j=0; j < size(); j++){
796  _sum_xy(indexReal(i), indexReal(j)) = map_xy[realName(i)][realName(j)];
797  _sum_xy(indexReal(i), indexImag(j)) = map_xy[realName(i)][imagName(j)];
798  _sum_xy(indexImag(i), indexReal(j)) = map_xy[imagName(i)][realName(j)];
799  _sum_xy(indexImag(i), indexImag(j)) = map_xy[imagName(i)][imagName(j)];
800  }
801  }
802  if(dbThis){
803  cout << "got _sum_xy: " << endl;
804  _sum_xy.Print();
805  }
806  _needToRecalculate=true;
807  return true;
808 }
809 
811  this->add(other);
812  return *this;
813 }
815  FitAmpPairCovariance returnVal(*this);
816  return (returnVal += other);
817 }
818 
819 //
int indexImag(int listPosition) const
std::string realName(int listPosition) const
TMatrixT< float > _sum_x
std::complex< double > fitParValue() const
Definition: FitAmpPair.h:58
TMatrixTSym< float > _fractionCov
TMatrixTSym< float > _cov2N
double integral() const
Definition: FitAmpPair.cpp:476
std::string imagName(int listPosition) const
bool makeDirectory(const std::string &asSubdirOf=".") const
std::string dirName(const std::string &asSubdirOf=".") const
std::complex< double > lastEntry() const
Definition: FitAmpPair.cpp:443
TMatrixTSym< T > makeTMatrixTSym(const TMatrixT< T > &m)
Definition: Utils.h:45
bool retrieve_sum_xy(const std::string &inDirectory)
static unsigned int _maxSize
int listPositionFromIndex(int idx) const
int indexReal(int listPosition) const
bool retrieve_sum_x(const std::string &inDirectory)
bool isCompatibleWith(const FitAmpPairList &other) const
int oneOrTwo() const
Definition: FitAmpPair.h:65
const FitAmpPairList * _myList
TMatrixTSym< float > _fractionSumCov
double weightSum() const
Definition: FitAmpPair.cpp:480
TMatrixT< float > _dFractionSum_by_dFraction
FitAmpPairCovariance(const FitAmpPairList *myList)
T & at(unsigned int i)
virtual double integral() const
unsigned int maxSize() const
TMatrixTSym< float > _sum_xy
unsigned int size() const
FitAmpPairCovariance & operator+=(const FitAmpPairCovariance &other)
FitAmpPairCovariance operator+(const FitAmpPairCovariance &other) const
TMatrixT< float > _dFraction_by_dN
bool retrieve(const std::string &fromDirectory=".")
unsigned int size() const
bool add(const FitAmpPairCovariance &other)
std::string matrix_xy_fname(const std::string &asSubdirOf=".") const
TMatrixTSym< float > _corr2N
TMatrixT< float > _dN_by_d2N
bool isSingleAmp() const
Definition: FitAmpPair.cpp:590
TMatrixTSym< float > _integralCov
bool save_sum_xy(const std::string &inDirectory) const
virtual int numEvents() const
TMatrixT< float > _dIntegral_by_dN
bool save_sum_x(const std::string &inDirectory) const
bool isCompatibleWith(const FitAmpPairCovariance &other) const
TMatrixTSym< float > _cov
const std::string & name() const
Definition: FitAmpPair.cpp:605
bool save(const std::string &asSubdirOf=".") const
TMatrixTSym< float > _corr
std::string matrix_x_fname(const std::string &asSubdirOf=".") const