28 using namespace DecayTreeFitter;
48 , m_extrapolator( aExtrapolator)
51 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") A - config"<<std::endl;
54 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") A - tree"<<std::endl;
57 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") A - Fitparams"<<std::endl;
58 m_fitparams =
new FitParams(m_decaychain->dim()) ;
59 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") A - done"<<std::endl;
74 , m_status ( UnFitted )
79 , m_extrapolator ( aExtrapolator)
83 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - config"<<std::endl;
85 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - tree "<<std::flush;
87 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - fitparams "<<std::flush;
88 m_fitparams =
new FitParams(m_decaychain->dim()) ;
89 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - done"<<std::endl;
103 , m_status ( UnFitted )
108 , m_extrapolator ( aExtrapolator)
112 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - config"<<std::endl;
114 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - tree "<<std::flush;
116 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - fitparams "<<std::flush;
117 m_fitparams =
new FitParams(m_decaychain->dim()) ;
118 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") D - done"<<std::endl;
133 , m_status ( UnFitted )
138 , m_extrapolator ( aExtrapolator)
142 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") C - config"<<std::endl;
144 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") C - tree "<<std::flush;
145 m_decaychain =
new DecayChain(bc,lv,pv,config) ;
146 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") C - fitparams "<<std::flush;
147 m_fitparams =
new FitParams(m_decaychain->dim()) ;
148 if(
vtxverbose>5) std::cout<<
"Fitter::Fitter() ("<<
this<<
") C - done"<<std::endl;
155 { m_extrapolator = aExtrapolator; }
163 if(m_decaychain)
delete m_decaychain ;
165 if(m_fitparams)
delete m_fitparams ;
166 if(m_extrapolator)
delete m_extrapolator;
173 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - start"<<std::endl;}
177 const int maxndiverging=3 ;
185 if( m_status == UnFitted )
187 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - unfitted: init decaychain"<<std::endl;}
188 m_errCode = m_decaychain->init(m_fitparams).flag() ;
190 if(
vtxverbose>3){std::cout<<
"Fitter:fit(): decaychain init ended with errcode "<<m_errCode<<std::endl;}
192 if ( 0 != m_errCode )
195 m_status = BadInput ;
199 m_status = UnFitted ;
202 bool finished = false ;
204 for(m_niter=0; m_niter<nitermax && !finished; ++m_niter)
206 TVectorD prevpar = m_fitparams->par() ;
207 bool firstpass = m_niter==0 ;
208 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - filtering"<<std::endl;}
209 m_errCode = m_decaychain->filter(m_fitparams,firstpass).flag() ;
210 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - filtering done"<<std::endl;}
211 double chisq = m_fitparams->chiSquare() ;
212 double deltachisq = chisq - fChiSquare ;
214 const double dChisqQuit =
std::max(
double(2*nDof()),2*fChiSquare) ;
217 if( 0 != m_errCode ) {
220 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - ow, we got an error: "<<m_errCode<<std::endl;}
223 if(
fabs( deltachisq ) < dChisqConv ) {
227 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - yay! we filtered successfully"<<std::endl;}
228 }
else if( m_niter>1 && deltachisq > dChisqQuit ) {
229 m_fitparams->par() = prevpar ;
233 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - fast diverging"<<std::endl;}
234 }
else if( deltachisq > 0 && ++ndiverging>=maxndiverging) {
235 m_fitparams->par() = prevpar ;
236 m_status = NonConverged ;
239 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - slow diverging"<<std::endl;}
240 }
else if( deltachisq > 0 ) {
241 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - well, smaller steps now"<<std::endl;}
243 m_fitparams->par() += prevpar ;
244 m_fitparams->par() *= 0.5 ;
248 if ( deltachisq < 0 ) ndiverging=0 ;
249 if(!finished) fChiSquare = chisq ;
251 fNDegreesOfFreedom = m_fitparams->nDof();
254 std::cout <<
"Fitter::fit(): step, stat, errcode, ndf (ncon, dim) , chiSquare, dchisq: "
255 << std::setw(3) << m_niter
256 << std::setw(3) << m_status<<
"\t"
257 << std::setw(3) << m_errCode<<
"\t"
258 << std::setw(3) << nDof() <<
" ("
259 << std::setw(3) << m_fitparams->nConstraints() <<
", "
260 << std::setw(3) << m_fitparams->dim() <<
") "
261 << std::setprecision(6)
262 << std::setw(12) << chisq
263 << std::setw(12) << deltachisq << std::endl ;
267 std::cout <<
print() << std::endl;
268 std::cout <<
"press a key ...." << std::endl ;
273 if( m_niter == nitermax && m_status != Success )
274 { m_status = NonConverged ; }
278 if(m_status == Success ) {
279 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - test cov"<<std::endl;}
280 if( !(m_fitparams->testCov() ) ) {
283 std::cout <<
"DecayTreeFitterter::Fitter: Error matrix not positive definite. "
284 <<
"Changing status to failed." << std::endl ;
290 if(
vtxverbose>5){std::cout<<
"Fitter::fit(): - done"<<std::endl;}
296 bool firstpass = m_status==UnFitted ;
297 if( firstpass ) m_decaychain->init(m_fitparams) ;
298 m_decaychain->filter(m_fitparams,firstpass) ;
299 fChiSquare = m_fitparams->chiSquare() ;
300 fNDegreesOfFreedom = m_fitparams->nDof();
302 std::cout <<
"In Fitter::fitOneStep(): " << m_status <<
" " << firstpass <<
" " << fChiSquare <<
" " << fNDegreesOfFreedom << std::endl ;
309 std::ostringstream
s ;
310 m_decaychain->mother()->print(m_fitparams) ;
311 s <<
"chisq,ndof,ncontr,niter,status,errcode: "
312 << chiSquare() <<
" "
313 << nDof() <<
" " << m_fitparams->nConstraints() <<
" "
314 << nIter() <<
" " <<
status() <<
" " << m_errCode << std::endl ;
320 return fNDegreesOfFreedom;
330 std::cout<<
"Fitter::add(RhoCandidate*) - start"<<std::endl;
332 int offset = m_fitparams->dim() ;
333 double deltachisq(999) ;
340 std::cout<<
"Fitter::add(RhoCandidate*) - resize"<<std::endl;
341 m_fitparams->resize(offset) ;
347 std::cout<<
"Fitter::add(RhoCandidate*) - constrains"<<std::endl;
350 double chisq = m_fitparams->chiSquare() ;
352 for( ParticleBase::constraintlist::const_iterator it = constraints.begin() ;
353 it != constraints.end(); ++it)
354 estatus |= it->filter(m_fitparams) ;
356 deltachisq = m_fitparams->chiSquare() - chisq ;
357 fChiSquare = m_fitparams->chiSquare() ;
359 std::cout<<
"Fitter::add(RhoCandidate*) - init dchain"<<std::endl;
361 decaychain()->initConstraintList() ;
365 std::cout <<
"cannot add track to this vertex ..."
366 << m_decaychain->mother()->type() << std::endl ;
380 double chisq = m_fitparams->chiSquare() ;
381 for( ParticleBase::constraintlist::iterator it = constraints.begin() ;
382 it != constraints.end(); ++it) {
384 estatus |= it->filter(m_fitparams) ;
386 dchisq = chisq - m_fitparams->chiSquare() ;
396 m_decaychain->setMassConstraint(bc,badd) ;
397 m_status = UnFitted ;
402 m_decaychain->setMassConstraint(bc,mass) ;
403 m_status = UnFitted ;
420 std::cout<<
"Fitter::updateIndex() start"<<std::endl;
422 m_decaychain->mother()->updateIndex(offset) ;
423 std::cout<<
"Fitter::updateIndex() resize"<<std::endl;
424 m_fitparams->resize(offset) ;
429 return m_decaychain->chiSquare(m_fitparams) ;
523 if( lenindexA>=0 && lenindexB>=0) {
524 double lenA = m_fitparams->par()(lenindexA) ;
525 double lenB = m_fitparams->par()(lenindexB) ;
527 m_fitparams->cov()(lenindexA, lenindexA) +
528 m_fitparams->cov()(lenindexB, lenindexB) +
529 2*m_fitparams->cov()(lenindexA, lenindexB) ;
542 return pb ? pb->
name() :
"Not found" ;
744 if(posindex<0) posindex = pb.
posIndex() ;
747 TVector3
pos(m_fitparams->par()(posindex+0),
748 m_fitparams->par()(posindex+1),
749 m_fitparams->par()(posindex+2)) ;
751 p4.SetPx( m_fitparams->par()(momindex+0) ) ;
752 p4.SetPy( m_fitparams->par()(momindex+1) ) ;
753 p4.SetPz( m_fitparams->par()(momindex+2) ) ;
755 TMatrixDSym cov8(8) ;
759 p4.SetE( m_fitparams->par()(momindex+3) ) ;
761 for(
int i=0;
i<3; ++
i) parmap[
i] = posindex +
i ;
762 for(
int i=0;
i<4; ++
i) parmap[
i+3] = momindex +
i ;
763 parmap[7] = lenindex ;
764 int maxrow = lenindex >=0 ? 8 : 7 ;
768 cov8(
row,
col) = m_fitparams->cov()(parmap[
row],parmap[
col]) ;
771 TMatrixDSym cov7(7) ;
773 for(
int i=0;
i<3; ++
i) parmap[
i] = posindex +
i ;
774 for(
int i=0;
i<3; ++
i) parmap[
i+3] = momindex +
i ;
775 parmap[6] = lenindex ;
776 int maxrow = lenindex >=0 ? 7 : 6 ;
780 cov7(
row,
col) = m_fitparams->cov()(parmap[
row],parmap[
col]) ;
784 double energy2 = mass*mass ;
786 double px = m_fitparams->par()(momindex+
row) ;
794 jacobian(6,
col) = m_fitparams->par()(parmap[
col])/energy ;
797 cov8 = cov7.Similarity(jacobian);
805 aParticle->
SetFit(fittedcand);
808 fittedcand->
SetP4(p4);
809 fittedcand->
SetCov7(cov8.GetSub(0,6,0,6));
879 if(pb) updateCand(*pb,aParticle) ;
897 if ( (rc = updateCand(p) ) ) {
901 updateTree( const_cast<RhoCandidate*>(p->
Daughter(iDau)) ) ;
909 {
return s <<
print() ; }
914 return m_decaychain->chiSquare(aParticle, m_fitparams) ;
bool updateTree(RhoCandidate *cand) const
update a particlular candidate in the tree
std::vector< DecayTreeFitter::Constraint > constraintlist
const ParticleBase * locate(RhoCandidate *bc) const
void SetPos(const TVector3 &pos)
friend F32vec4 sqrt(const F32vec4 &a)
virtual void addToConstraintList(constraintlist &alist, int depth) const =0
virtual int momIndex() const
void fit(int maxNumberOfIterations=10, double deltaChisquareConverged=0.01)
Fit the decay tree.
RhoCandidate * Daughter(Int_t n)
virtual int lenIndex() const
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
std::string name(RhoCandidate *cand) const
Name of a particle in the decay tree.
virtual ErrCode initCov(FitParams *) const
virtual bool hasEnergy() const
virtual ErrCode initPar2(FitParams *)=0
void SetP4(Double_t mass, const TVector3 &p3)
const std::string & name() const
virtual int posIndex() const
double add(RhoCandidate *cand)
virtual void updateIndex(int &offset)
void RemoveAssociations()
void removeDaughter(const ParticleBase *pb)
double globalChiSquare() const
friend F32vec4 fabs(const F32vec4 &a)
static RhoFactory * Instance()
void setMassConstraint(RhoCandidate *cand, bool add=true)
Add or remove a mass constraint.
void setStateProvider(RecoTrackStateProvider *extrapolator)
set the track extrapolator
bool updateCand(RhoCandidate *cand) const
update a particlular candidate in the tree
void SetFit(RhoCandidate *b)
std::ostream & fillStream(std::ostream &s) const
Print the result of the fit.
void setVerbose(int i)
set the verbosity level (for debugging only)
void fitOneStep()
Fit just one step.
int nDof() const
Total number of DOFs.
static RhoCandidate * NewCandidate()
Fitter()
default constructor is disabled
RhoCandidate * GetFit() const
RhoDoubleErr decayLengthSum(RhoCandidate *, RhoCandidate *) const
virtual ErrCode initPar1(FitParams *)=0
double chiSquare() const
Total chisquare.
double remove(RhoCandidate *cand)
virtual ~Fitter()
destructor
void SetCov7(const TMatrixD &cov7)
const ParticleBase * mother() const
TMatrixT< double > TMatrixD
std::string print() const
Print the result of the fit.