Belle II Software  release-05-02-19
TreeFitterModule.cc
1 /* BASF2 (Belle Analysis Framework 2) *
2 * *
3 * Copyright(C) 2013 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributor: Francesco Tenchini, Jo-Frederik Krohn *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 
11 //Implementation of Decay Tree Fitter based on arXiv:physics/0503191
12 //Main module implementation
13 
14 #include <analysis/modules/TreeFitter/TreeFitterModule.h>
15 #include <analysis/VertexFitting/TreeFitter/FitManager.h>
16 
17 #include <analysis/dataobjects/ParticleList.h>
18 #include <analysis/dataobjects/Particle.h>
19 
20 #include <framework/datastore/StoreArray.h>
21 #include <framework/datastore/StoreObjPtr.h>
22 #include <framework/particledb/EvtGenDatabasePDG.h>
23 
24 #include <analysis/utility/ParticleCopy.h>
25 
26 #include <analysis/VertexFitting/TreeFitter/ConstraintConfiguration.h>
27 #include <analysis/VertexFitting/TreeFitter/FitParameterDimensionException.h>
28 
29 using namespace Belle2;
30 
31 REG_MODULE(TreeFitter)
32 
33 TreeFitterModule::TreeFitterModule() : Module(), m_nCandidatesBeforeFit(-1), m_nCandidatesAfter(-1)
34 {
35  setDescription("Tree Fitter module. Performs simultaneous fit of all vertices in a decay chain. Can also be used to just fit a single vertex.");
36  setPropertyFlags(c_ParallelProcessingCertified);
37  //
38  addParam("particleList", m_particleList,
39  "Type::[string]. Input mother of the decay tree to fit. For example 'B0:myB0particleList'.");
40  addParam("confidenceLevel", m_confidenceLevel,
41  "Type::[double]. Confidence level to accept fitted decay tree. Candidates with < confidenceLevel will be removed from the particle list! Typical Values: -1: keep all particle candidates, 0: remove all that fail the fit, 0.001: standard cut, 0.1: (too) tight cut. Optimise using a figure of merit (for example S/(sqrt{S+B}) ) for your analysis.",
42  0.0);
43  addParam("convergencePrecision", m_precision,
44  "Type::[double]. Fractional upper limit for chi2 fluctuations to accept result. Larger value = less signal rejection but also less background rejection. Optimized for FOM on different topologies - don't touch unless you REALLY want this.",
45  0.01);
46  addParam("massConstraintList", m_massConstraintList,
47  "Type::[int]. List of particles to mass constrain with int = pdg code. Note that the variables 'M': fit result for the particle and 'InvM': calculated from the daughter momenta, will look different (especially if you don't update the daughters!).", {});
48  addParam("massConstraintListParticlename", m_massConstraintListParticlename,
49  "Type::[string]. List of particles to mass constrain with string = particle name.", {});
50 
51 
52  addParam("geoConstraintList", m_geoConstraintListPDG,
53  "Type::[int], if 'autoSetGeoConstraintAndMergeVertices==False' you can manually set the particles that will be geometrically constrained here.", {});
54  addParam("sharedVertexList", m_fixedToMotherVertexListPDG,
55  "Type::[int], if 'autoSetGeoConstraintAndMergeVertices==False' you can manually set the particles that share the vertex with their mother here.", {});
56  addParam("autoSetGeoConstraintAndMergeVertices", m_automatic_vertex_constraining,
57  "Type::bool, shall vertices of strong resonance be merged with their mothers? Can the particles vertex be constraint geometrically?",
58  true);
59 
60  addParam("customOriginVertex", m_customOriginVertex,
61  "Type::[double]. List of vertex coordinates to be used in the custom origin constraint.", {0.001, 0, 0.0116});
62  addParam("customOriginCovariance", m_customOriginCovariance,
63  "Type::[double]. List vertex covariance elements used in the custom origin constraint (as a vector). Default is meant for B0 decays and is taken from 100k generated B0 to mumu events.",
64  {
65  0.0048, 0, 0,
66  0, 0.003567, 0,
67  0, 0, 0.0400
68  }
69  );
70  addParam("customOriginConstraint", m_customOrigin,
71  "Type::[bool]. Use a custom vertex as the production point of the highest hierachy particle (register this as the mother of the list you specify). Like the beam constraint but you can specify the position its covariance yourself. ",
72  false);
73  addParam("ipConstraint", m_ipConstraint,
74  "Type::[bool]. Use the IP as the origin of the tree. This registers an internal IP particle as the mother of the list you give. Or in other words forces the PRODUCTION vertex of your particle to be the IP and its covariance as specified in the database.",
75  false);
76  addParam("originDimension", m_originDimension,
77  "Type int, default 3. If origin or ip constraint used, specify the dimension of the constraint 3->x,y,z; 2->x,y. This also changes the dimension of the geometric constraints! So you might want to turn them off for some particles. (That means turn auto off and manually on for the ones you want to cosntrain)",
78  3);
79  addParam("updateAllDaughters", m_updateDaughters,
80  "Type::[bool]. Update all daughters (vertex position and momenta) in the tree. If not set only the 4-momenta for the head of the tree will be updated. We also update the vertex position of the daughters regardless of what you put here, because otherwise the default when the particle list is created is {0,0,0}.",
81  false);
82  //
83  addParam("expertMassConstraintType", m_massConstraintType,
84  "Type::[int]. False(0): use particles parameters in mass constraint; True: use sum of daughter parameters for mass constraint. WAARNING not even guaranteed that it works.",
85  0);
86  addParam("expertRemoveConstraintList", m_removeConstraintList,
87  "Type::[string]. List of constraints that you do not want to be used in the fit. WARNING don't use if you don't know exactly what it does.", {});
88  addParam("expertUseReferencing", m_useReferencing,
89  "Type::[bool]. Use the Extended Kalman Filter. This implementation linearises around the previous state vector which gives smoother convergence.",
90  true);
91  addParam("inflationFactorCovZ", m_inflationFactorCovZ,
92  "Inflate the covariance of the beamspot by this number so that the 3d beam constraint becomes weaker in Z.And: thisnumber->infinity : dim(beamspot constr) 3d->2d.",
93  1);
94 }
95 
97 {
100  particles.isRequired();
102  m_nCandidatesAfter = 0;
103 
104  if ((m_massConstraintList.size()) == 0 && (m_massConstraintListParticlename.size()) > 0) {
105  for (auto& containedParticle : m_massConstraintListParticlename) {
106  TParticlePDG* particletemp = TDatabasePDG::Instance()->GetParticle((containedParticle).c_str());
107  m_massConstraintList.push_back(particletemp->PdgCode());
108  }
109  }
110 }
111 
113 {
114 }
115 
117 {
119 
120  if (!plist) {
121  B2ERROR("ParticleList " << m_particleList << " not found");
122  return;
123  }
124 
125  std::vector<unsigned int> toRemove;
126  const unsigned int n = plist->getListSize();
128 
129  for (unsigned i = 0; i < n; i++) {
130  Belle2::Particle* particle = plist->getParticle(i);
131 
132  if (m_updateDaughters == true) {
133  ParticleCopy::copyDaughters(particle);
134  }
135 
136  try {
137  const bool ok = fitTree(particle);
138  if (!ok) { particle->setPValue(-1); }
139  } catch (TreeFitter::FitParameterDimensionException const& e) {
140  B2ERROR(e.what());
141  }
142 
143  if (particle->getPValue() < m_confidenceLevel) {
144  toRemove.push_back(particle->getArrayIndex());
145  }
146 
147  }
148  plist->removeParticles(toRemove);
149  m_nCandidatesAfter += plist->getListSize();
150 }
151 
152 
154 {
155  if (m_nCandidatesAfter > 0) {
156  plotFancyASCII();
157  } else {
158  B2WARNING("Not a single candidate survived the fit. Candidates before fit: " << m_nCandidatesBeforeFit << " after: " <<
160  }
161 }
162 
164 {
165  const TreeFitter::ConstraintConfiguration constrConfig(
178  );
179 
180  std::unique_ptr<TreeFitter::FitManager> TreeFitter(
182  head,
183  constrConfig,
184  m_precision,
187  )
188  );
189  bool rc = TreeFitter->fit();
190  return rc;
191 }
192 
194 {
195  B2INFO("\033[1;35m================================================================================\033[0m");
196  B2INFO("\033[40;97m ,., \033[0m");
197  B2INFO("\033[40;97m ;%&M%;_ ,.., \033[0m");
198  B2INFO("\033[40;97m \"_ \"__\" % M % M %; , ..., , \033[0m");
199  B2INFO("\033[40;97m , ..., __.\" --\" , ., _ - \" % &W % WM %; \033[0m");
200  B2INFO("\033[40;97m ; % M&$M % \"___ \"_._ %M%\"_.\"\" _ \"\"\"\"\"\" \033[0m");
201  B2INFO("\033[40;97m \"\"\"\"\" \"\" , \\_. \"_. .\" \033[0m");
202  B2INFO("\033[40;97m , ., _\"__ \\__./ .\" \033[0m");
203  B2INFO("\033[40;97m ___ __ | y , .., \033[97;40mThank you for using TreeFitter. \033[0m");
204  B2INFO("\033[40;97m /)'\\ ''''| u \\ %W%W%%; \033[0m");
205  B2INFO("\033[40;97m ___)/ \"---\\_ \\ |____\" \033[97;40mPlease cite both: \033[0m");
206  B2INFO("\033[40;97m ;&&%%; (|__.|)./ ,.., \033[97;40m10.1016/j.nima.2020.164269 \033[0m");
207  B2INFO("\033[40;97m ,.., ___\\ |/ &&\" \033[97;40m 10.1016/j.nima.2005.06.078 \033[0m");
208  B2INFO("\033[40;97m &&%%& (| Uo / '\" \033[97;40mEmail: \033[0m");
209  B2INFO("\033[40;97m '''' \\ 7 \\ \033[97;40mfrancesco.tenchini@desy.de \033[0m");
210  B2INFO("\033[40;97m ._______________.-'____\"\"-.____. \033[97;40mjo-frederik.krohn@desy.de \033[0m");
211  B2INFO("\033[40;97m \\ / \033[0m");
212  B2INFO("\033[40;97m \\ \033[0m\033[32;40mTREEFITTER\033[0m\033[40;97m / \033[0m");
213  B2INFO("\033[40;97m \\_______________________/ \033[0m");
214  B2INFO("\033[40;97m (_) (_) \033[0m");
215  B2INFO("\033[40;97m \033[0m");
216  B2INFO("\033[1;35m============= TREEFIT STATISTICS ===============================================\033[0m");
217  B2INFO("\033[1;39mCandidates before fit: " << m_nCandidatesBeforeFit << "\033[0m");
218  B2INFO("\033[1;39mCandidates after fit: " << m_nCandidatesAfter << "\033[0m");
219  B2INFO("\033[1;39mA total of " << m_nCandidatesBeforeFit - m_nCandidatesAfter <<
220  " candidates were removed during the fit.\033[0m");
221  B2INFO("\033[1;39m" << (double)m_nCandidatesAfter / (double)m_nCandidatesBeforeFit * 100.0 <<
222  "% of candidates survived the fit.\033[0m");
223  B2INFO("\033[1;39m" << 100. - (double)m_nCandidatesAfter / (double)m_nCandidatesBeforeFit * 100.0 <<
224  "% of candidates did not.\033[0m");
225  B2INFO("\033[1;39mYou choose to drop all candidates with pValue < " << m_confidenceLevel << ".\033[0m");
226  B2INFO("\033[1;35m================================================================================\033[0m");
227 }
Belle2::TreeFitterModule
Module to fit an entire decay tree.
Definition: TreeFitterModule.h:34
Belle2::TreeFitterModule::m_nCandidatesBeforeFit
unsigned int m_nCandidatesBeforeFit
before the fit
Definition: TreeFitterModule.h:103
Belle2::TreeFitterModule::m_particleList
std::string m_particleList
name of the particle list fed to the fitter
Definition: TreeFitterModule.h:58
TreeFitter::FitManager
this class
Definition: FitManager.h:31
TreeFitter::ConstraintConfiguration
constainer
Definition: ConstraintConfiguration.h:25
Belle2::TreeFitterModule::m_precision
double m_precision
convergence precision for the newton method When the delta chiSquared between 2 iterations divided by...
Definition: TreeFitterModule.h:74
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TreeFitterModule::m_geoConstraintListPDG
std::vector< int > m_geoConstraintListPDG
list of pdg codes of particles to use a geo cosntraint for
Definition: TreeFitterModule.h:80
Belle2::TreeFitterModule::m_massConstraintListParticlename
std::vector< std::string > m_massConstraintListParticlename
vector carrying the names of the particles to be mass constraint
Definition: TreeFitterModule.h:87
Belle2::TreeFitterModule::m_ipConstraint
bool m_ipConstraint
Use x-y-z beamspot constraint.
Definition: TreeFitterModule.h:97
Belle2::TreeFitterModule::m_massConstraintType
int m_massConstraintType
type of the mass constraint false: use normal one.
Definition: TreeFitterModule.h:91
Belle2::TreeFitterModule::m_automatic_vertex_constraining
bool m_automatic_vertex_constraining
should the vertex be joined with the mother and should it be geometrically cosntrained?...
Definition: TreeFitterModule.h:134
Belle2::TreeFitterModule::m_nCandidatesAfter
unsigned int m_nCandidatesAfter
after the fit
Definition: TreeFitterModule.h:106
Belle2::TreeFitterModule::initialize
virtual void initialize() override
initialize
Definition: TreeFitterModule.cc:96
Belle2::TreeFitterModule::m_useReferencing
bool m_useReferencing
linearise around a previous state of the Kalman Filter
Definition: TreeFitterModule.h:118
Belle2::TreeFitterModule::m_massConstraintList
std::vector< int > m_massConstraintList
vector carrying the PDG codes of the particles to be mass constraint
Definition: TreeFitterModule.h:77
TreeFitter::FitParameterDimensionException
exception template, runtime_error implements what()
Definition: FitParameterDimensionException.h:22
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::TreeFitterModule::m_originDimension
int m_originDimension
dimension to use for beam/origin cosntraint
Definition: TreeFitterModule.h:137
Belle2::TreeFitterModule::terminate
virtual void terminate() override
stuff at the end
Definition: TreeFitterModule.cc:153
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::TreeFitterModule::m_customOriginVertex
std::vector< double > m_customOriginVertex
vertex coordinates of the custom origin
Definition: TreeFitterModule.h:121
Belle2::TreeFitterModule::m_confidenceLevel
double m_confidenceLevel
minimum confidence level to accept fit calculated as f(chiSquared, NDF) -2: accept all 0: only accept...
Definition: TreeFitterModule.h:67
Belle2::TreeFitterModule::fitTree
bool fitTree(Particle *head)
this fits all particle candidates contained in the m_particleList
Definition: TreeFitterModule.cc:163
Belle2::TreeFitterModule::m_customOriginCovariance
std::vector< double > m_customOriginCovariance
covariance of the custom origin
Definition: TreeFitterModule.h:124
Belle2::TreeFitterModule::m_inflationFactorCovZ
int m_inflationFactorCovZ
inflate beamspot covariance of z by this number
Definition: TreeFitterModule.h:140
Belle2::TreeFitterModule::m_removeConstraintList
std::vector< std::string > m_removeConstraintList
list of constraints not to be applied in tree fit WARNING only use if you know what you are doing
Definition: TreeFitterModule.h:129
Belle2::TreeFitterModule::m_fixedToMotherVertexListPDG
std::vector< int > m_fixedToMotherVertexListPDG
list of pdg codes of particles where we use thesame vertex for production and decay which is the vert...
Definition: TreeFitterModule.h:84
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
TreeFitter::FitManager::fit
bool fit()
main fit function that uses the kalman filter
Definition: FitManager.cc:74
Belle2::TreeFitterModule::m_updateDaughters
bool m_updateDaughters
flag if you want to update all particle momenta in the decay tree.
Definition: TreeFitterModule.h:111
Belle2::TreeFitterModule::plotFancyASCII
void plotFancyASCII()
plot ascii art and statistics
Definition: TreeFitterModule.cc:193
Belle2::StoreArray< Belle2::Particle >
Belle2::TreeFitterModule::event
virtual void event() override
performed for each event
Definition: TreeFitterModule.cc:116
Belle2::ParticleCopy::copyDaughters
void copyDaughters(Particle *mother)
Function copies all (grand-)^n-daughter particles of the argument mother Particle.
Definition: ParticleCopy.cc:48
Belle2::TreeFitterModule::m_customOrigin
bool m_customOrigin
use a custom vertex as the production vertex of the highest hierarchy particle
Definition: TreeFitterModule.h:115
Belle2::TreeFitterModule::beginRun
virtual void beginRun() override
performed at the start of run
Definition: TreeFitterModule.cc:112