MINT2
FitAmpPairFitCovariance.cpp
Go to the documentation of this file.
1 
3 #include "Mint/FitAmpPairList.h"
4 #include "Mint/Utils.h"
5 
6 #include <iostream>
7 
8 using namespace std;
9 using namespace MINT;
10 
11 unsigned int FitAmpPairFitCovariance::_maxSize = 1500;
12 
14  , const TMatrixTSym<double>& fc
15  )
16  : _myList(myList)
17  , _fitCov(fc)
18  , _Nevents(0)
19  , _size(1)
20  , _needToRecalculate(true)
21  , _pairCov(1)
22  , _pairCorr(1)
23  , _fractionCov(1)
24  , _fractionSumCov(1)
25  , _interferenceFracSumCov(1)
26  , _totalFractionSumCov(1)
27  , _dFitAmpPair_by_dFitParameters(1, fc.GetNcols())
28  , _dFraction_by_dFitAmpPair(1, 1)
29  , _dFractionSum_by_dFraction(1, 1)
30  , _dInterferenceFracSum_by_dFraction(1, 1)
31  , _dTotalFractionSum_by_dFraction(1, 1)
32 {
33  bool dbThis=false;
34  if(0 != _myList && _myList->size() >=1) resize();
35 
36  if(dbThis) {
37  cout << "FitAmpPairFitCovariance constructor called with this cov matrix:"
38  << endl;
39  _fitCov.Print();
40  }
41 }
43  : _myList(other._myList)
44  , _fitCov(other._fitCov)
45  , _Nevents(other._Nevents)
46  , _size(other._size)
47  , _needToRecalculate(false)
48  , _pairCov(other._pairCov)
49  , _pairCorr(other._pairCorr)
50  , _fractionCov(other._fractionCov)
51  , _fractionSumCov(other._fractionSumCov)
52  , _interferenceFracSumCov(other._interferenceFracSumCov)
53  , _totalFractionSumCov(other._totalFractionSumCov)
54  , _dFitAmpPair_by_dFitParameters(other._dFitAmpPair_by_dFitParameters)
55  , _dFraction_by_dFitAmpPair(other._dFraction_by_dFitAmpPair)
56  , _dFractionSum_by_dFraction(other._dFractionSum_by_dFraction)
57  , _dInterferenceFracSum_by_dFraction(other._dInterferenceFracSum_by_dFraction)
58  , _dTotalFractionSum_by_dFraction(other._dTotalFractionSum_by_dFraction)
59 {
60 }
61 
62 unsigned int FitAmpPairFitCovariance::size() const{
63  return _size;
64 }
66  bool dbThis=false;
67  if(dbThis){
68  cout << "FitAmpPairFitCovariance::resize()"
69  << " resizing from " << size()
70  << " to " << _myList->size()
71  << " with maxSize() = " << maxSize() << "."
72  << endl;
73  }
74 
75  _needToRecalculate=true;
76 
77  _size = _myList->size();
78 
79  if(size() > maxSize()){
80  cout << "FitAmpPairFitCovariance::resize size() = " << size()
81  << " that's too big! won't do it!" << endl;
82  return false;
83  }
84 
85  _pairCov. ResizeTo(size(), size());
86  _pairCorr. ResizeTo(size(), size());
87 
88  _fractionCov. ResizeTo(size(), size());
89  _fractionSumCov. ResizeTo(1,1);
90  _interferenceFracSumCov. ResizeTo(1,1);
91  _totalFractionSumCov. ResizeTo(1,1);
92  _dFitAmpPair_by_dFitParameters. ResizeTo(size(), _fitCov.GetNcols());
93  _dFraction_by_dFitAmpPair. ResizeTo(size(), size());
94 
95  _dFractionSum_by_dFraction.ResizeTo(1, size());
98 
99  return true;
100 }
101 
102 std::string FitAmpPairFitCovariance::realName(int i) const{
103  return (*_myList)[i].name() + "_real";
104 }
105 std::string FitAmpPairFitCovariance::imagName(int i) const{
106  return (*_myList)[i].name() + "_imag";
107 }
108 
110  bool dbThis=false;
111  bool sc=true;
112  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
113  << " making dFitAmpPair_by_dFitParameters" << endl;
115 
116  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
117  << " making dFraction_by_dFitAmpPair" << endl;
119 
120  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
121  << " making dFractionSum_by_dFraction" << endl;
123 
124  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
125  << " making dInterferenceFrac_by_dFraction" << endl;
127 
128  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
129  << " making dTotalFractionSum_by_dFraction" << endl;
131 
132  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
133  << " making FitAmpPairCov" << endl;
134  sc &= makeFitAmpPairCov();
135  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
136  << " making FractionCovariance" << endl;
137  sc &= makeFractionCovariance();
138  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
139  << " making FractionSumCovariance" << endl;
141  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
142  << " making InterferenceFracSumCovariance" << endl;
144  if(dbThis) cout << "FitAmpPairFitCovariance::calculateAll"
145  << " making TotalFractionSumCovariance" << endl;
147 
148  _needToRecalculate=false;
149  return sc;
150 }
152  bool dbThis=false;
153  if(dbThis) cout << "FitAmpPairFitCovariance::"
154  << "make_dFitAmpPair_by_dFitParameters called" << endl;
155  /*
156  each FitAmpPair is evaluated as the real part of
157  the product of three complex numbers, times other factors (f)
158 
159  p = z1 z2* a * f, p* = z1* z2 a f*
160 
161  The paramters are:
162  o The product of fit parameters z1 z2* = fitParValue()
163  o The integral: a
164 
165  Each FitAmpPair returns real(p) = 0.5(p + p*);
166  The total integral is the sum of these.
167 
168  z1 is either given as
169  o z1 = (x1 + iy1) or as
170  o z1 = r1 exp(i phi1)
171 
172  We want to transform from the fit parameters to p. We'll need
173 
174  dp/d(x1) = z2* a f = p/z1
175  dp/d(y1) = iz2* a f = i p/z1
176  dp/d(r1) = exp(i degFac phi1) z2* a f = p/r1
177  dp/d(phi1) = i degFac r1 exp(i phi1) z2* a f = i p
178 
179  dp/d(x2) = z1 a f = p/z2*
180  dp/d(y2) = -iz1 a f = -i p/z2*
181  dp/d(r2) = exp(-i degFac phi2) z1 a f = p/r2
182  dp/d(phi2) = -i degFac r2 exp(-i degFac phi2) z1 a f = -i p
183  */
184  for(int i=0; i < _dFitAmpPair_by_dFitParameters.GetNrows(); i++){
185  for(int j=0; j < _dFitAmpPair_by_dFitParameters.GetNcols(); j++){
187  }
188  }
189  for(unsigned int i=0; i < _myList->size(); i++){
190  complex<double> p = (*_myList)[i].complexVal();
191  complex<double> z1 = (*_myList)[i].fitAmp1().FitAmpPhase();
192  complex<double> z2 = (*_myList)[i].fitAmp2().FitAmpPhase();
193  complex<double> z2star(conj(z2));
194  complex<double> number_i(0, 1);
195 
196  FitComplex::TYPE t1 = (*_myList)[i].fitAmp1().FitAmpPhase().type();
197 
198  int index_1a = (*_myList)[i].fitAmp1().FitAmpPhase().p1().parSetIndex();
199  int index_1b = (*_myList)[i].fitAmp1().FitAmpPhase().p2().parSetIndex();
200 
201  if(FitComplex::CARTESIAN == t1){
202  complex<double> dpdx1 = p/z1;
203  complex<double> dpdy1 = number_i * p / z1;
204 
205  _dFitAmpPair_by_dFitParameters(i, index_1a) += dpdx1.real();
206  _dFitAmpPair_by_dFitParameters(i, index_1b) += dpdy1.real();
207  }else if(FitComplex::POLAR == t1){
208 
209  double degFac =
210  ((const FitComplexPolar&) ((*_myList)[i].fitAmp1().FitAmpPhase())
211  ).degFac();
212 
213  complex<double> r1 = abs(z1);
214 
215  complex<double> dpdr1 = p/r1;
216  complex<double> dpdphi1 = number_i * p * degFac;
217  _dFitAmpPair_by_dFitParameters(i, index_1a) += dpdr1.real();
218  _dFitAmpPair_by_dFitParameters(i, index_1b) += dpdphi1.real();
219  }else{
220  cout << "ERROR in FitAmpPairFitCovariance::"
221  << "make_dFitAmpPair_by_dFitParameters()"
222  << " don't know type of FitComplex in "
223  << (*_myList)[i].fitAmp1()
224  << endl;
225  throw "shouldn't be possible";
226  }
227  FitComplex::TYPE t2 = (*_myList)[i].fitAmp2().FitAmpPhase().type();
228 
229  int index_2a = (*_myList)[i].fitAmp2().FitAmpPhase().p1().parSetIndex();
230  int index_2b = (*_myList)[i].fitAmp2().FitAmpPhase().p2().parSetIndex();
231 
232  /*
233  dp/d(x2) = z1 a = p/z2*
234  dp/d(y2) = -iz1 a = -i p/z2*
235  dp/d(r2) = exp(-i degFac phi2) z1 a = p/r2
236  dp/d(phi2) = -i degFac r2 exp(-i phi2) z2 a = -i p
237  */
238 
239  if(FitComplex::CARTESIAN == t2){
240  complex<double> dpdx2 = p/z2star;
241  complex<double> dpdy2 = - number_i * p / z2star;
242 
243  _dFitAmpPair_by_dFitParameters(i, index_2a) += dpdx2.real();
244  _dFitAmpPair_by_dFitParameters(i, index_2b) += dpdy2.real();
245  }else if(FitComplex::POLAR == t2){
246  double degFac =
247  ((const FitComplexPolar&) ((*_myList)[i].fitAmp1().FitAmpPhase())
248  ).degFac();
249 
250  complex<double> r2 = abs(z2);
251 
252  complex<double> dpdr2 = p/r2;
253  complex<double> dpdphi2 = - number_i * p *degFac;
254  _dFitAmpPair_by_dFitParameters(i, index_2a) += dpdr2.real();
255  _dFitAmpPair_by_dFitParameters(i, index_2b) += dpdphi2.real();
256  }else{
257  cout << "ERROR in FitAmpPairFitCovariance::"
258  << "make_dFitAmpPair_by_dFitParameters()"
259  << " don't know type of FitComplex in " << (*_myList)[i].fitAmp2()
260  << endl;
261  throw "shouldn't be possible";
262  }
263  }
264  return true;
265 }
266 
268  bool dbThis=false;
269  if(dbThis) cout << "FitAmpPairFitCovariance::make_dFraction_by_dFitAmpPair()"
270  << " called" << endl;
271  /*
272  We are calculating trafo matrix for the following trafo:
273 
274  f_a = a/(a + b + c...)
275 
276 
277  diagonal terms:
278 
279  d(f_a)/d(a) = 1/(a + b + c +...) - a/(a + b + c ...)^2
280 
281  off-diagonal terms:
282 
283  d(f_a)/d(b) = -a/(a + b + c ...)^2
284  */
285 
286  double integ = _myList->integral();
287  double integ2 = integ*integ;
288 
289  for(int i=0; i < _dFraction_by_dFitAmpPair.GetNrows(); i++){
290  for(int j=0; j < _dFraction_by_dFitAmpPair.GetNcols(); j++){
292  }
293  }
294  for(unsigned int i=0; i < _myList->size(); i++){
295  double num = (*_myList)[i].integral();
296  for(unsigned int j=0; j < _myList->size(); j++){
297  _dFraction_by_dFitAmpPair(i,j) = -num/integ2;
298  }
299  _dFraction_by_dFitAmpPair(i, i) += 1.0/integ;
300  }
301  return true;
302 }
303 
305  bool dbThis=false;
306  if(dbThis) cout << "FitAmpPairFitCovariance::make_dFractionSum_by_dFraction()"
307  << " called" << endl;
308  /*
309  We are calculating trafo matrix for the trafo to the sum
310  of all terms entering the fraction calculation, this
311  excludes the terms for interference effect.
312 
313  It's just ones (those included) and zeros (for those not included)
314  */
315 
316  for(unsigned int i=0; i < _myList->size(); i++){
317  if((*_myList)[i].isSingleAmp()){
318  _dFractionSum_by_dFraction(0, i) = 1.0;
319  }else{
320  _dFractionSum_by_dFraction(0, i) = 0.0;
321  }
322  }
323  return true;
324 }
325 
327  bool dbThis=false;
328  if(dbThis) cout << "FitAmpPairFitCovariance::make_dInterferenceFracSum_by_dFraction()"
329  << " called" << endl;
330  /*
331  We are calculating trafo matrix for the trafo to the sum
332  of all terms entering the interference calculation, this
333  excludes all the usual fraction terms.
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)[i].isSingleAmp()){
341  }else{
343  }
344  }
345  return true;
346 }
347 
349  // used for debugging only
350  bool dbThis=false;
351  if(dbThis) cout << "FitAmpPairFitCovariance::"
352  << "make_dTotalFractionSum_by_dFraction()"
353  << " called" << endl;
354  /*
355  We are calculating trafo matrix for the trafo to the sum
356  of all terms entering the fraction calculation, this
357  excludes the terms for interference effect.
358 
359  It's just ones (those included - here all)
360  and zeros (for those not included - here none)
361  */
362 
363  for(unsigned int i=0; i < _myList->size(); i++){
365  }
366  return true;
367 }
368 
369 
371  bool dbThis=false;
372 
373  bool success = true;
374 
375  TMatrixT<double> dFitAmpPair_by_dFitParameters_Transpose(_dFitAmpPair_by_dFitParameters.GetNcols()
376  , _dFitAmpPair_by_dFitParameters.GetNrows());
377  dFitAmpPair_by_dFitParameters_Transpose.Transpose(_dFitAmpPair_by_dFitParameters);
378  if(dbThis){
379  cout << "FitAmpPairFitCovariance::makeFitAmpPairCov()"
380  << " _dFitAmpPair_by_dFitParameters(): "
381  << endl;
383  cout << "and its transpose:" << endl;
384  dFitAmpPair_by_dFitParameters_Transpose.Print();
385  }
386 
388  _fitCov *
389  dFitAmpPair_by_dFitParameters_Transpose);
390 
391  for(unsigned int i=0; i < _myList->size(); i++){
392  if(_pairCov(i, i) <= 0) continue;
393  for(unsigned int j=i; j < _myList->size(); j++){
394  double densq = _pairCov(i,i)*_pairCov(j,j);
395  if(densq > 0){
396  _pairCorr(i,j) = _pairCov(i,j)/sqrt(densq);
397  }
398  }
399  }
400 
401  if(dbThis){
402  cout << "After " << _Nevents << "events" << endl;
403  cout << "Covariance Matrix N" << endl;
404  _pairCov.Print();
405  cout << "Correlation Matrix N" << endl;
406  _pairCorr.Print();
407  }
408  return success;
409 }
410 
412  bool dbThis=false;
413 
414  int cols=_dFraction_by_dFitAmpPair.GetNcols();
415  int rows=_dFraction_by_dFitAmpPair.GetNrows();
416  TMatrixT<double> dFraction_by_dFitAmpPair_Transpose(cols,rows);
417  dFraction_by_dFitAmpPair_Transpose.Transpose(_dFraction_by_dFitAmpPair);
418 
420  _pairCov *
421  dFraction_by_dFitAmpPair_Transpose);
422 
423  if(dbThis){
424  cout << "Correlation Matrix Fractions" << endl;
425  _fractionCov.Print();
426  }
427 
428  return true;
429 }
431  bool dbThis=false;
432 
433  TMatrixT<double> dFractionSum_by_dFraction_Transpose(size(), 1);
434  dFractionSum_by_dFraction_Transpose.Transpose(_dFractionSum_by_dFraction);
435 
437  _fractionCov *
438  dFractionSum_by_dFraction_Transpose);
439 
440  if(dbThis){
441  cout << "After " << _Nevents << "events" << endl;
442  cout << "Covariance Matrix for FractionSum" << endl;
443  _fractionSumCov.Print();
444  }
445 
446  return true;
447 }
449  bool dbThis=false;
450 
451  TMatrixT<double> dInterferenceFracSum_by_dFraction_Transpose(size(), 1);
452  dInterferenceFracSum_by_dFraction_Transpose.Transpose(_dInterferenceFracSum_by_dFraction);
453 
455  _fractionCov *
456  dInterferenceFracSum_by_dFraction_Transpose);
457 
458  if(dbThis){
459  cout << "After " << _Nevents << "events" << endl;
460  cout << "Covariance Matrix for FractionSum" << endl;
461  _interferenceFracSumCov.Print();
462  }
463 
464  return true;
465 }
467  // used for debug only (should make a 1x1 matrix with 0 +/- numerical error)
468  bool dbThis=false;
469 
470  TMatrixT<double> dTotalFractionSum_by_dFraction_Transpose(size(), 1);
471  dTotalFractionSum_by_dFraction_Transpose.Transpose(_dTotalFractionSum_by_dFraction);
472 
474  _fractionCov *
475  dTotalFractionSum_by_dFraction_Transpose);
476 
477  if(dbThis){
478  cout << "After " << _Nevents << "events" << endl;
479  cout << "Covariance Matrix for TotalFractionSum" << endl;
480  _totalFractionSumCov.Print();
481  }
482 
483  return true;
484 }
485 
488  if(i < 0 || i >= _pairCov.GetNrows() ) return -9999.0;
489  if(j < 0 || j >= _fractionCov.GetNcols() ) return -9999.0;
490  return _pairCov(i, j);
491 }
493  double V = getPairVariance(i, j);
494  if(V < 0){
495  cout << "ERROR in FitAmpPairFitCovariance::getPairError"
496  << " variance is " << V << " which is < 0."
497  << " will return -9999."
498  << endl;
499  return 0;
500  }
501  return sqrt(V);
502 }
503 
506  if(i < 0 || i >= _fractionCov.GetNcols() ) return -9999.0;
507  if(i < 0 || i >= _fractionCov.GetNrows() ) return -9999.0;
508  return _fractionCov(i, i);
509 }
511  double V = getFractionVariance(i);
512  if(V < 0){
513  cout << "ERROR in FitAmpPairFitCovariance::getFractionError"
514  << " variance is " << V << " which is < 0."
515  << " will return -9999."
516  << endl;
517  return 0;
518  }
519  return sqrt(V);
520 }
521 
523  // use for debug only - should always return zero (ish)
525  return _totalFractionSumCov(0,0);
526 }
528  // use for debug only - should always return zero (ish)
529  double V = getTotalFractionSumVariance();
530  if(V < 0) return -sqrt(-V);
531  else return sqrt(V);
532 }
535  return _interferenceFracSumCov(0,0);
536 }
538  double V = getInterferenceFracSumVariance();
539  if(V < 0){
540  cout << "ERROR in FitAmpPairFitCovariance::getInterferenceFracSumError"
541  << " variance is " << V << " which is < 0."
542  << " will return 0."
543  << endl;
544  return 0;
545  }
546  return sqrt(V);
547 }
550  return _fractionSumCov(0,0);
551 }
552 
554  double V = getFractionSumVariance();
555  if(V < 0){
556  cout << "ERROR in FitAmpPairFitCovariance::getFractionSumError"
557  << " variance is " << V << " which is < 0."
558  << " will return 0."
559  << endl;
560  return 0;
561  }
562  return sqrt(V);
563 }
565  if(size() > maxSize()) return false;
566  if(size() <= 0)return false;
567  if((unsigned int) _pairCov.GetNcols() != size()) return false;
568  return true;
569 }
570 //
TMatrixTSym< double > _fitCov
TMatrixTSym< double > _totalFractionSumCov
TMatrixTSym< double > _fractionCov
const FitAmpPairList * _myList
TMatrixTSym< double > _interferenceFracSumCov
TMatrixTSym< double > _pairCorr
TMatrixTSym< double > _pairCov
TMatrixTSym< T > makeTMatrixTSym(const TMatrixT< T > &m)
Definition: Utils.h:45
double getPairVariance(int i, int j)
TMatrixT< double > _dTotalFractionSum_by_dFraction
TMatrixT< double > _dFractionSum_by_dFraction
virtual double integral() const
FitAmpPairFitCovariance(const FitAmpPairList *myList, const TMatrixTSym< double > &fitCov)
unsigned int maxSize() const
TMatrixT< double > _dFitAmpPair_by_dFitParameters
unsigned int size() const
TMatrixT< double > _dInterferenceFracSum_by_dFraction
std::string realName(int i) const
TMatrixTSym< double > _fractionSumCov
TMatrixT< double > _dFraction_by_dFitAmpPair
std::string imagName(int i) const