Belle II Software  release-05-01-25
DecayChain.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributor: *
7  * Wouter Hulsbergen, Francesco Tenchini, Jo-Frederik Krohn *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 
13 #include <algorithm>
14 #include <framework/logging/Logger.h>
15 
16 #include <analysis/VertexFitting/TreeFitter/FitParams.h>
17 #include <analysis/VertexFitting/TreeFitter/ParticleBase.h>
18 #include <analysis/VertexFitting/TreeFitter/DecayChain.h>
19 
20 
21 namespace TreeFitter {
22 
24  const ConstraintConfiguration& config,
25  bool forceFitAll
26  ) :
27  m_dim(0),
28  m_headOfChain(nullptr),
29  m_isOwner(true),
30  m_config(config)
31  {
32 
33  if (config.m_ipConstraint && config.m_customOrigin) {
34  B2FATAL("Setup error. Cant have both custom origin and ip constraint.");
35  }
36  config.m_headOfTreePDG = std::abs(particle->getPDGCode());
37  if (config.m_ipConstraint || config.m_customOrigin) {
38  m_headOfChain = ParticleBase::createOrigin(particle,
39  config,
40  forceFitAll
41  );
42  } else if ((!config.m_customOrigin) && (!config.m_ipConstraint)) {
43 
44  m_headOfChain = ParticleBase::createParticle(particle, nullptr, config, forceFitAll);
45  }
46 
47  m_headOfChain->updateIndex(m_dim);
48 
49  m_cand = locate(particle);
50  }
51 
53  {
54  if (m_headOfChain && m_isOwner) {
55  delete m_headOfChain;
56  }
57  }
58 
60  {
64  std::sort(m_constraintlist.begin(), m_constraintlist.end());
65  }
66 
68  {
69  for (auto removeConstraint : m_config.m_removeConstraintList) {
70  m_constraintlist.erase(std::remove_if(m_constraintlist.begin(), m_constraintlist.end(),
71  [&](const Constraint & constraint) { return constraint.name() == removeConstraint ;}),
72  m_constraintlist.end());
73  }
74  }
75 
77  {
78  ErrCode status;
79  par.resetStateVector();
80  status |= m_headOfChain->initMotherlessParticle(par);
81  par.resetCovariance();
82  status |= m_headOfChain->initCovariance(par);
84  return status;
85  }
86 
88  {
89  ErrCode status;
90  par.resetCovariance();
91  status |= m_headOfChain->initCovariance(par);
92  par.resetChiSquare();
93  for (auto constraint : m_constraintlist) {
94  status |= constraint.filter(par);
95  }
96  return status;
97  }
98 
100  {
101  ErrCode status;
102  par.resetCovariance();
103  status |= m_headOfChain->initCovariance(par);
104  par.resetChiSquare();
105  for (auto constraint : m_constraintlist) {
106  status |= constraint.filterWithReference(par, ref);
107  }
108  return status;
109  }
110 
111 
112  double DecayChain::chiSquare(const FitParams& par) const
113  {
114  return m_headOfChain->chiSquare(par);
115  }
116 
117  const ParticleBase* DecayChain::locate(Belle2::Particle* particle) const
118  {
119  const ParticleBase* rc(nullptr);
120  const auto mapRow = m_particleMap.find(particle) ;
121 
122  if (mapRow == m_particleMap.end()) {
123  //take head of chain and recursively find particle in it
124  rc = m_headOfChain->locate(particle);
125 
126  if (rc && rc->particle()) {
127  const_cast<DecayChain*>(this)->m_particleMap[rc->particle()] = rc;
128  }
129  } else {
130  //only used for "head of tree"
131  rc = mapRow->second;// (B2::Particle, Particlebase)
132  }
133  return rc;
134  }
135 
136  int DecayChain::index(Belle2::Particle* particle) const
137  {
138  const ParticleBase* base = locate(particle);
139  if (base) {
140  return base->index();
141  }
142  return -1;
143  }
144 
146  {
147  const ParticleBase* base = locate(particle);
148  if (base) {
149  return base->posIndex();
150  }
151  return -1;
152  }
153 
155  {
156  const ParticleBase* base = locate(particle);
157  if (base) {
158  return base->momIndex();
159  }
160  return -1;
161  }
162 
164  {
165  const ParticleBase* base = locate(particle);
166  if (base) {
167  return base->tauIndex();
168  }
169  return -1;
170  }
171 }
TreeFitter::DecayChain::m_constraintlist
ParticleBase::constraintlist m_constraintlist
list of constraints
Definition: DecayChain.h:107
TreeFitter::Constraint::m_dim
unsigned int m_dim
dimension of constraint
Definition: Constraint.h:151
TreeFitter::DecayChain::initialize
ErrCode initialize(FitParams &par)
initalize the chain
Definition: DecayChain.cc:85
TreeFitter::ParticleBase::createParticle
static ParticleBase * createParticle(Belle2::Particle *particle, const ParticleBase *mother, const ConstraintConfiguration &config, bool forceFitAll=false)
create the according treeFitter particle obj for a basf2 particle type
Definition: ParticleBase.cc:156
TreeFitter::DecayChain::m_particleMap
ParticleMap m_particleMap
the map from Belle2::Particles to TreeFitter::ParticleBase
Definition: DecayChain.h:113
TreeFitter::DecayChain::m_isOwner
const bool m_isOwner
internal class member to check if we own the chain
Definition: DecayChain.h:116
TreeFitter::DecayChain::filter
ErrCode filter(FitParams &par)
filter down the chain
Definition: DecayChain.cc:96
TreeFitter::ParticleBase::locate
const ParticleBase * locate(Belle2::Particle *particle) const
get particle base from basf2 particle
Definition: ParticleBase.cc:321
TreeFitter::DecayChain::DecayChain
DecayChain()
empty constructor
Definition: DecayChain.h:39
TreeFitter::ErrCode
abstract errorocode be aware that the default is succes
Definition: ErrCode.h:23
TreeFitter::DecayChain::tauIndex
int tauIndex(Belle2::Particle *bc) const
get tau (i.e.
Definition: DecayChain.cc:172
TreeFitter::ParticleBase
base class for all particles
Definition: ParticleBase.h:36
TreeFitter::ParticleBase::chiSquare
virtual double chiSquare(const FitParams &) const
get chi2
Definition: ParticleBase.cc:341
TreeFitter::FitParams
Class to store and manage fitparams (statevector)
Definition: FitParams.h:29
TreeFitter::DecayChain::locate
const ParticleBase * locate(Belle2::Particle *bc) const
convert Belle2::particle into particle base(fitter base particle)
Definition: DecayChain.cc:126
TreeFitter::DecayChain::posIndex
int posIndex(Belle2::Particle *bc) const
get the vertex index of the particle in state vector
Definition: DecayChain.cc:154
TreeFitter::DecayChain::filterWithReference
ErrCode filterWithReference(FitParams &par, const FitParams &ref)
filter with respect to a previous iteration for better stability
Definition: DecayChain.cc:108
TreeFitter::ParticleBase::addToConstraintList
virtual void addToConstraintList(constraintlist &alist, int depth) const =0
add to constraint list
TreeFitter::ParticleBase::createOrigin
static ParticleBase * createOrigin(Belle2::Particle *daughter, const ConstraintConfiguration &config, bool forceFitAll)
create a custom origin particle or a beamspot
Definition: ParticleBase.cc:147
TreeFitter::DecayChain
this class does a lot of stuff: Build decaytree structure allowing to index particles and handle the ...
Definition: DecayChain.h:34
TreeFitter::Constraint
class to manage the order of contraints and their filtering
Definition: Constraint.h:29
TreeFitter::DecayChain::m_config
const ConstraintConfiguration m_config
config container
Definition: DecayChain.h:119
TreeFitter::DecayChain::chiSquare
double chiSquare(const FitParams &par) const
get the chi2 for the head of the chain
Definition: DecayChain.cc:121
TreeFitter::ParticleBase::initCovariance
virtual ErrCode initCovariance(FitParams &) const
init covariance matrix
Definition: ParticleBase.cc:268
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
TreeFitter::DecayChain::~DecayChain
~DecayChain()
destructor
Definition: DecayChain.cc:61
TreeFitter::DecayChain::removeConstraintFromList
void removeConstraintFromList()
remove constraints from list
Definition: DecayChain.cc:76
TreeFitter::DecayChain::initConstraintList
void initConstraintList()
init contraintlist
Definition: DecayChain.cc:68
TreeFitter::ParticleBase::initMotherlessParticle
virtual ErrCode initMotherlessParticle(FitParams &)=0
init particle that does not need a mother vertex
TreeFitter::DecayChain::m_headOfChain
ParticleBase * m_headOfChain
head of decay tree
Definition: DecayChain.h:102
TreeFitter::DecayChain::index
int index(Belle2::Particle *bc) const
get the particle index
Definition: DecayChain.cc:145
TreeFitter::DecayChain::momIndex
int momIndex() const
!NOT IMPLEMENTED
TreeFitter::ConstraintConfiguration::m_removeConstraintList
const std::vector< std::string > m_removeConstraintList
list of constraints not to apply in tree fit
Definition: ConstraintConfiguration.h:95