FairRoot/PandaRoot
Constraint.cxx
Go to the documentation of this file.
1 // ******************************************************
2 // DecayTreeFitter Package
3 // We thank the original author Wouter Hulsbergen
4 // (BaBar, LHCb) for providing the sources.
5 // http://arxiv.org/abs/physics/0503191v1 (2005)
6 // Adaptation & Development for PANDA: Ralf Kliemt (2015)
7 // ******************************************************
8 #include <iomanip>
9 #include "FitParams.h"
10 #include "ParticleBase.h"
11 #include "Constraint.h"
12 #include "KalmanCalculator.h"
13 
14 using namespace DecayTreeFitter;
15 
17 
18 extern int vtxverbose ;
19 
21 {
22  // the simple way
23  return m_type < rhs.m_type ||
24  (m_type == rhs.m_type && m_depth < rhs.m_depth) ;
25 
26  // this is probably the second most complicated routine: how do we
27  // order the constraints. there is one very special case:
28  // Ks->pipi0 requires the pi0 mass constraints at the very
29  // end. otherwise, it just doesn't work. in all other cases, we
30  // prefer to fit 'down' the tree'. the 'external' constraints must
31  // be filtered first, but soft pions must be fitted after the
32  // geometric constraints of the D. You see, this is horrible.
33 
34  // if either of the two is external, or either of the two is a
35  // mass constraint, we order by m_typem_
36 
37  // if( (m_type <= Constraint::composite ||
38  // rhs.m_type <= Constraint::composite ) ||
39  // (m_type >= Constraint::mass ||
40  // rhs.m_type >= Constraint::mass ) ) {
41  // return m_type < rhs.m_type ||
42  // (m_type == rhs.m_type && m_depth < rhs.m_depth) ;
43  // }
45  //return m_depth < rhs.m_depth ||
46  //(m_depth == rhs.m_depth && m_type < rhs.m_type ) ;
47 
48 }
49 
50 ErrCode
52 {
53  // this one will be overruled by the MergedConstraint
54  p.setParticle( *m_node ) ;
55  return m_node->projectConstraint(m_type,fitpar,p) ;
56 }
57 
58 ErrCode
60 {
61  ErrCode status ;
62  if(m_type<=Constraint::unknown || m_type>=Constraint::ntypes) {
63  std::cout << "Constraint::filter(FitParams*): unknown constraint: " << m_type
64  << std::endl ;
65  status |= ErrCode::badsetup ;
66  } else if (m_type!=merged && !m_node) {
67  std::cout << "Constraint::filter(FitParams*): constraint without a node"
68  << std::endl ;
69  status |= ErrCode::badsetup ;
70  } else {
71  if(vtxverbose>=3) { std::cout << "Constraint::filter(FitParams*): filtering " ; print() ;}
72  // save the unfiltered ('predicted') parameters. we need to
73  // store them if we want to iterate constraints.
74  const TVectorD* pred(0) ;
75  if(m_maxNIter>1) pred = new TVectorD(fitpar->par()) ;
76 
77  Projection p(fitpar->dim(),m_dim) ;
79  double chisq(0) ;
80  int iter(0) ;
81  bool finished(false) ;
82  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*): starting loop " ; print() ;}
83  while (!finished && !status.failure()) {
84  p.reset() ;
85  status |= project(fitpar,p) ;
86  if(!status.failure()) {
87  status |= kalman.init( p.r(), p.H(), fitpar, p.V() ) ;
88  //status |= kalman.init( p.r(), p.H(), fitpar, (&p.V()) ? ( p.V().NonZeros()>0 ? &p.V() : 0 ) : 0 ) ;
89  if( !status.failure()) {
90  if(iter==0 || !pred) {
91  kalman.updatePar( fitpar ) ;
92  } else {
93  kalman.updatePar( *pred, fitpar ) ;
94  }
95  const double dchisqconverged = 0.001 ;
96  double newchisq = kalman.chisq() ;
97  double dchisq = newchisq - chisq ;
98  bool diverging = iter > 0 && dchisq>0 ;
99  bool converged = fabs(dchisq) < dchisqconverged ;
100  finished = ++iter >= m_maxNIter || diverging || converged ;
101 
102  if(vtxverbose>=3) {
103  std::cout << "chi2,niter: "
104  << iter << " "<< std::setprecision(7)
105  << std::setw(12) << chisq << " "
106  << std::setw(12)<< newchisq << " "
107  << std::setw(12)<< dchisq << " "
108  << diverging << " "
109  << converged << " ";
110  status.Print(std::cout);
111  std::cout << std::endl ;
112  }
113  chisq = newchisq ;
114  }
115  }
116  }
117  if(!status.failure()) {
118  kalman.updateCov( fitpar ) ;
119  fitpar->addChiSquare( kalman.chisq(), m_dim, p.particle() ) ;
120  }
121  if(pred) delete pred ;
122  if(vtxverbose>=4 &&m_node&&
123  m_node->mother() ) { m_node->mother()->print(fitpar) ; }
124  }
125  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*): done" ; print() ;}
126  return status ;
127 }
128 
129 
130 ErrCode
132 {
133  // filter but linearize around reference
134  ErrCode status ;
135  if(m_type<=Constraint::unknown || m_type>=Constraint::ntypes) {
136  std::cout << "Constraint::filter(FitParams*,FitParams*): unknown constraint: " << m_type
137  << std::endl ;
138  status |= ErrCode::badsetup ;
139  } else if (m_type!=merged && !m_node) {
140  std::cout << "Constraint::filter(FitParams*,FitParams*): filter constraint without a node"
141  << std::endl ;
142  status |= ErrCode::badsetup ;
143  } else {
144  if(vtxverbose>=3) { std::cout << "Constraint::filter(FitParams*,FitParams*): filtering (par&refpar) " ; print() ;}
145 
146  // project using the reference
147  Projection p(fitpar->dim(),m_dim) ;
148  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*,FitParams*): project"<<std::endl;}
149  status = project(reference,p) ;
150  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*,FitParams*): residual"<<std::endl;}
151  // now update the residual
152  p.r() += p.H() * (fitpar->par() - reference->par()) ;
153  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*,FitParams*): kalman"<<std::endl;}
154  // now call the Kalman update as usual
156  status |= kalman.init( p.r(), p.H(), fitpar, p.V() ) ;
157  //status |= kalman.init( p.r(), p.H(), fitpar, (&p.V()) ? ( p.V().NonZeros()>0 ? &p.V() : 0 ) : 0 ) ;
158  kalman.updatePar( fitpar ) ;
159  kalman.updateCov( fitpar ) ;
160  fitpar->addChiSquare( kalman.chisq(), m_dim, p.particle() ) ;
161  if(vtxverbose>=3) { std::cout << "Constraint::filter(FitParams*,FitParams*): \""<<name()<<"\" Chisquare contribution: " <<kalman.chisq()<<" with "<<m_dim<<" dimensions from particle "<<p.particle()->name()<<std::endl;}
162  if(vtxverbose>=5) { std::cout << "Constraint::filter(FitParams*,FitParams*): done"<<std::endl;}
163  }
164  if( status.failure()){
165  std::cout << "Constraint::filter(FitParams*,FitParams*): error filtering constraint: "
166  << name() << " "; status.Print(std::cout); std::cout << std::endl ;}
167 
168  return status ;
169 }
170 
172 {
173  os << "node index = " << m_node->index()
174  << " name = " << m_node->name().c_str()
175  << " constraint type = " << m_type
176  << " depth = " << m_depth << std::endl ;
177 }
178 
180 {
181  std::string rc = "unknown constraint!" ;
182  switch(m_type)
183  {
184  case beamspot: rc = "beamspot" ; break ;
185  case beamenergy: rc = "beamenergy" ; break ;
186  case composite: rc = "composite" ; break ;
187  case resonance: rc = "resonance" ; break ;
188  case track: rc = "track" ; break ;
189  case photon: rc = "photon" ; break ;
190  case kinematic: rc = "kinematic" ; break ;
191  case geometric: rc = "geometric" ; break ;
192  case mass: rc = "mass" ; break ;
193  case massEnergy: rc = "massEnergy" ; break ;
194  case lifetime: rc = "lifetime" ; break ;
195  case merged: rc = "merged" ; break ;
196  case conversion: rc = "conversion" ; break ;
197  case ntypes:
198  case unknown:
199  break ;
200  }
201  return rc ;
202 }
Double_t p
Definition: anasim.C:58
bool failure() const
Definition: ErrCode.h:45
virtual ErrCode filter(FitParams *fitpar) const
Definition: Constraint.cxx:59
std::string name() const
Definition: Constraint.cxx:179
void addChiSquare(double chi2, int nconstraints, const ParticleBase *p)
Definition: FitParams.cxx:141
KalmanTask * kalman
void setParticle(const ParticleBase &p)
Definition: Projection.h:83
virtual void print(std::ostream &os=std::cout) const
Definition: Constraint.cxx:171
void Print(std::ostream &os)
Definition: ErrCode.cxx:34
int vtxverbose
geoFace print()
virtual ErrCode project(const FitParams *fitpar, Projection &p) const
Definition: Constraint.cxx:51
PndMCTrack * track
Definition: anaLmdCluster.C:89
void updatePar(FitParams *fitparams)
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
TString name
ClassImp(PndAnaContFact)
bool operator<(const Constraint &rhs) const
Definition: Constraint.cxx:20
void updateCov(FitParams *fitparams)
int status[10]
Definition: f_Init.h:28
ErrCode init(const TVectorD &value, const TMatrixD &G, const FitParams *fitparams, const TMatrixDSym &V, int weight=1)