ROOTPWA
massDepFitLikeli.cc
Go to the documentation of this file.
1 #include "massDepFitLikeli.h"
2 #include "TTree.h"
3 #include "TF1.h"
4 #include "TMatrixD.h"
5 #include "fitResult.h"
6 #include "TStopwatch.h"
7 #include <vector>
8 #include <complex>
9 #include <iostream>
10 
11 
12 
13 
14 using namespace std;
15 
16 unsigned int
17 rpwa::massDepFitLikeli::NDim() const {return _compset->numPar();}
18 
19 unsigned int
21  // loop through input data an check how many bins are in massrange
22  unsigned int nbins=_tree->GetEntries();
23  cout << nbins << " mass bins in input data." << endl;
24  unsigned int nbinsInRange=0;
25  // loop over mass-bins
26  for(unsigned im=0;im<nbins;++im){
27  _tree->GetEntry(im);
28  double mass=_rhom->massBinCenter();
29  if(mass>=_mmin && mass<=_mmax)++nbinsInRange;
30  }
31  cout << nbinsInRange << " mass bins in allowed mass range {"
32  << _mmin<<","<<_mmax<<"}"<< endl;
33 
34  // calculate data points, remember (Re,Im)=> factor 2:
35  // a complex, symmetric matrix with real diagonal has n^2 elements:
36  return nbinsInRange*_wlist.size()*_wlist.size();
37 }
38 
39 void
40 rpwa::massDepFitLikeli::init(TTree* sp, TF1* fsps, pwacompset* compset,
41  double mmin, double mmax, bool doCov){
42  _mmin=mmin;
43  _mmax=mmax;
44  _doCov=doCov;
45  _compset=compset;
46  _tree=sp;
47  _finalStatePS=fsps;
48  _rhom=NULL;//new fitResult();
49  if(_tree->SetBranchAddress("fitResult_v2",&_rhom))cerr<<"Branch not found!"<<endl;
50 
51  _tree->GetEntry(0);
52 
53  // build waveindex map;
54  _wlist.clear();
55  _wlist=_compset->wavelist();
56  _index.clear();
57 
58  cerr << "Number of components: "<<_compset->n()<< endl;
59  cerr << (*_compset) << endl;
60 
61  cerr << "Number of waves in fit: "<< _wlist.size() << endl;
62 // build map from waves as in _index to components and their channels
63  for(unsigned int i=0;i<_wlist.size();++i){
64  _index.push_back(_rhom->waveIndex(_wlist[i]));
65  cerr << _wlist[i] << endl;
66  }
67 
68  // read data into memory
69  unsigned int nbins=_tree->GetEntries();
70  unsigned int nwaves=_wlist.size();
71  _spindens.clear();
72  _cov.clear();
73  _mass.clear();
74  for(unsigned im=0;im<nbins;++im){
75  _tree->GetEntry(im);
76  _mass.push_back(_rhom->massBinCenter());
77 
78  ccmatrix mycov(nwaves,nwaves);
79  cmatrix myrhom(nwaves,nwaves);
80  // _vrhom.push_back(*_rhom);
81  for(unsigned int i=0; i<nwaves; ++i){
82  for(unsigned int j=i; j<nwaves; ++j){
83 
84  myrhom(i,j)=_rhom->spinDensityMatrixElem(_index[i],_index[j]);
85 
86  TMatrixD acov(2,2);
87  acov= _rhom->spinDensityMatrixElemCov(_index[i],_index[j]);
88  rmatrix c(2,2);c(0,0)=acov(0,0);c(1,0)=acov(1,0);c(1,1)=acov(1,1);c(0,1)=acov(0,1);
89  mycov(i,j)=c;
90 
91  }
92  }
93  _spindens.push_back(myrhom);
94  _cov.push_back(mycov);
95  } // end loop over mass bins
96 
97 
98 
99 
100 
101 
102 }
103 
104 
105 double
106 rpwa::massDepFitLikeli::DoEval(const double* par) const {
107  // rank==1 mass dependent fit
108 
109  // input values: m
110  unsigned int nwaves=_index.size();
111 
112  // set parameters for resonances, background and phase space
113  _compset->setPar(par);
114 
115  // Breitwigner parameters
116  //masses;
117  //widths;
118 
119  double chi2=0;
120  unsigned int nbins=_mass.size();
121 
122  TStopwatch dataW;dataW.Stop();dataW.Reset();
123  TStopwatch modelW;modelW.Stop();modelW.Reset();
124  TStopwatch calcW;calcW.Stop();calcW.Reset();
125 
126  // loop over mass-bins
127  for(unsigned im=0;im<nbins;++im){
128  //_tree->GetEntry(im);
129  const ccmatrix& mycov =_cov[im];
130  const cmatrix& myrhom =_spindens[im];
131 
132  //const fitResult* fitR=&_vrhom[im];
133  double mass=_mass[im];
134  if(mass<_mmin)continue;
135  if(mass>_mmax)continue;
136  //if(mass>2000)continue;
137  //cout << "Mass=" << mass << endl;
138  // inpu values: measured spin density matrix
139  //double FSPS=_finalStatePS->Eval(mass);
140 
141  // sum over the contributions to chi2 -> rho_ij
142  for(unsigned int i=0; i<nwaves; ++i){
143  for(unsigned int j=i; j<nwaves; ++j){
144  // calculate target spin density matrix element
145  complex<double> rho;
146  // loop over all waves
147  complex<double> f1;
148  complex<double> f2;
149  const string w1=_wlist[i];
150  const string w2=_wlist[j];
151  // loop over components and look which contains these waves
152  modelW.Start(0);
153  rho=_compset->overlap(i,j,mass);
154  //complex<double> rho2=_compset->overlap(w1,w2,mass);
155  //if(norm(rho-rho2)>1E-4)cerr<< " ##### " << norm(rho-rho2) << endl;
156  modelW.Stop();
157  // compare to measured spin density matrix element
158  dataW.Start(0);
159  complex<double> rhom=rho-myrhom(i,j);// fitR->spinDensityMatrixElem(_index[i],_index[j]);
160 
161  const rmatrix& cov= mycov(i,j);//fitR->spinDensityMatrixElemCov(_index[i],_index[j]);
162  dataW.Stop();
163 
164  calcW.Start(0);
165  double dchi=norm(rhom);
166  //cov.Print();
167  if(i==j)dchi/=cov(0,0);
168  else {
169 
170  if(_doCov){
171  dchi=rhom.real()*rhom.real()*cov(1,1)-(2*cov(0,1))*rhom.real()*rhom.imag()+cov(0,0)*rhom.imag()*rhom.imag();
172  dchi/=cov(0,0)*cov(1,1)-cov(0,1)*cov(0,1);
173  }
174  else dchi=rhom.real()*rhom.real()/cov(0,0)+rhom.imag()*rhom.imag()/cov(1,1);
175  }
176 
177  //cerr << "d-rho("<<i<<","<<j<<")=" << dchi<<endl;;
178  //cerr << "sigma ="<< sigma << endl;
179  //if(i==j)
180  chi2+=dchi;
181  calcW.Stop();
182  }
183  }// end loop over i
184  } // end loop over mass-bins
185 
186  //cerr << "dataAccessTime: " << maxPrecisionAlign(dataW.CpuTime()) << " sec" << endl;
187  //cerr << "modelAccessTime: " << maxPrecisionAlign(modelW.CpuTime()) << " sec" << endl;
188  //cerr << "calcTime: " << maxPrecisionAlign(calcW.CpuTime()) << " sec" << endl;
189 
190 
191  return chi2;
192 } // end DoEval