Belle II Software development
DecayDescriptor.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <analysis/DecayDescriptor/DecayDescriptor.h>
10#include <analysis/DecayDescriptor/DecayString.h>
11#include <analysis/DecayDescriptor/DecayStringDecay.h>
12#include <analysis/DecayDescriptor/DecayStringGrammar.h>
13#include <analysis/utility/EvtPDLUtil.h>
14#include <analysis/dataobjects/Particle.h>
15
16#include <analysis/utility/AnalysisConfiguration.h>
17
18#include <mdst/dataobjects/MCParticle.h>
19
20#include <framework/gearbox/Const.h>
21#include <framework/logging/Logger.h>
22
23#include <TDatabasePDG.h>
24
25#include <boost/variant/get.hpp>
26#include <boost/spirit/include/qi.hpp>
27#include <algorithm>
28#include <set>
29#include <utility>
30
31using namespace Belle2;
32using namespace std;
33
35 m_mother(),
36 m_iDaughter_p(-1),
37 m_daughters(),
38 m_properties(0),
39 m_isNULL(false),
40 m_isInitOK(false)
41{
42}
43
44bool DecayDescriptor::init(const std::string& str)
45{
46 // The decay string grammar
49 std::string::const_iterator iter = str.begin();
50 std::string::const_iterator end = str.end();
51 bool r = phrase_parse(iter, end, g, boost::spirit::unicode::space, s);
52 if (!r || iter != end) return false;
53 return init(s);
54}
55
57{
58 // The DecayString is a hybrid, it can be
59 // a) DecayStringParticleList
60 // b) DecayStringDecay
61
62 if (const DecayStringParticle* p = boost::get< DecayStringParticle >(&s)) {
64 if (!m_isInitOK) {
65 B2WARNING("Could not initialise mother particle " << p->m_strName);
66 return false;
67 }
68 return true;
69 } else if (const DecayStringDecay* d = boost::get< DecayStringDecay > (&s)) {
70 // Initialise list of mother particles
71 m_isInitOK = m_mother.init(d->m_mother);
72 if (!m_isInitOK) {
73 B2WARNING("Could not initialise mother particle " << d->m_mother.m_strName);
74 return false;
75 }
76
77 // Identify arrow type
78 if (d->m_strArrow == "->") {
81 } else if (d->m_strArrow == "=norad=>") {
83 } else if (d->m_strArrow == "=direct=>") {
85 } else if (d->m_strArrow == "=exact=>") {
86 // do nothing
87 } else {
88 B2WARNING("Unknown arrow: " << d->m_strArrow);
89 m_isInitOK = false;
90 return false;
91 }
92
93 // Initialise list of daughters
94 if (d->m_daughters.empty()) {
95 m_isInitOK = false;
96 return false;
97 }
98 int nDaughters = d->m_daughters.size();
99 for (int iDaughter = 0; iDaughter < nDaughters; iDaughter++) {
100 DecayDescriptor daughter;
101 m_isInitOK = daughter.init(d->m_daughters[iDaughter]);
102 if (!m_isInitOK) {
103 B2WARNING("Could not initialise daughter!");
104 return false;
105 }
106 m_daughters.push_back(daughter);
107 }
108
109 // Initialise list of keywords
110 // For neutrino
111 if ((std::find(d->m_keywords.begin(), d->m_keywords.end(), "?nu")) != d->m_keywords.end()) {
113 }
114 // For gamma
115 if ((std::find(d->m_keywords.begin(), d->m_keywords.end(), "?gamma")) != d->m_keywords.end()) {
117 }
118 // For massive FSP
119 if ((std::find(d->m_keywords.begin(), d->m_keywords.end(), "...")) != d->m_keywords.end()) {
121 }
122 // For brems photons
123 if ((std::find(d->m_keywords.begin(), d->m_keywords.end(), "?addbrems")) != d->m_keywords.end()) {
125 }
126
127 return true;
128 }
129 m_isInitOK = false;
130 return false;
131}
132
133template <class T>
134int DecayDescriptor::match(const T* p, int iDaughter_p)
135{
136 // this DecayDescriptor was not matched or
137 // it is not the daughter of another DecayDescriptor
138 m_iDaughter_p = -1;
139
140 if (!p) {
141 B2WARNING("NULL pointer provided instead of particle.");
142 return 0;
143 }
144
145 int iPDGCode_p = 0;
146 if (const auto* part_test = dynamic_cast<const Particle*>(p))
147 iPDGCode_p = part_test->getPDGCode();
148 else if (const auto* mc_test = dynamic_cast<const MCParticle*>(p))
149 iPDGCode_p = mc_test->getPDG();
150 else {
151 B2WARNING("Template type not supported!");
152 return 0;
153 }
154
155 int iPDGCodeCC_p = TDatabasePDG::Instance()->GetParticle(iPDGCode_p)->AntiParticle()->PdgCode();
156 int iPDGCode_d = m_mother.getPDGCode();
157 if (abs(iPDGCode_d) != abs(iPDGCode_p)) return 0;
158 int iCC = 0;
159 if (iPDGCode_p == iPDGCodeCC_p) iCC = 3;
160 else if (iPDGCode_d == iPDGCode_p) iCC = 1;
161 else if (iPDGCode_d == iPDGCodeCC_p) iCC = 2;
162
163 const std::vector<T*> daughterList = p->getDaughters();
164 int nDaughters_p = daughterList.size();
165
166 // 1st case: the descriptor has no daughters => nothing to check
167 if (getNDaughters() == 0) {
168 m_iDaughter_p = iDaughter_p;
169 return iCC;
170 }
171
172 // 2nd case: the descriptor has daughters, but not the particle
173 // => that is not allowed!
174 if (nDaughters_p == 0) return 0;
175
176 // 3rd case: the descriptor and the particle have daughters
177 // There are two cases that can happen when matching the
178 // DecayDescriptor daughters to the particle daughters:
179 // 1. The match is unambiguous -> no problem
180 // 2. Multiple particle daughters match the same DecayDescriptor daughter
181 // -> in the latter case the ambiguity is resolved later
182
183 // 1. DecayDescriptor -> Particle relation for the cases where only one particle matches
184 vector< pair< int, int > > singlematch;
185 // 2. DecayDescriptor -> Particle relation for the cases where multiple particles match
186 vector< pair< int, set<int> > > multimatch;
187 // Are there ambiguities in the match?
188 bool isAmbiguities = false;
189 // The particle daughters that have been matched
190 set<int> matches_global;
191
192 // check if the daughters match
193 for (int iDaughter_d = 0; iDaughter_d < getNDaughters(); iDaughter_d++) {
194 set<int> matches;
195 for (int jDaughter_p = 0; jDaughter_p < nDaughters_p; jDaughter_p++) {
196 const T* daughter = daughterList[jDaughter_p];
197 int iPDGCode_daughter_p = 0;
198 if (const auto* part_test = dynamic_cast<const Particle*>(daughter))
199 iPDGCode_daughter_p = part_test->getPDGCode();
200 else if (const auto* mc_test = dynamic_cast<const MCParticle*>(daughter))
201 iPDGCode_daughter_p = mc_test->getPDG();
202
203 if (iDaughter_d == 0 && (this->isIgnoreRadiatedPhotons() or this->isIgnoreGamma() or this->isIgnoreBrems())
204 && iPDGCode_daughter_p == Const::photon.getPDGCode())
205 matches_global.insert(jDaughter_p);
206
207 int iMatchResult = m_daughters[iDaughter_d].match(daughter, jDaughter_p);
208 if (iMatchResult < 0) isAmbiguities = true;
209 if (abs(iMatchResult) == 2 && iCC == 1) continue;
210 if (abs(iMatchResult) == 1 && iCC == 2) continue;
211 if (abs(iMatchResult) == 2 && iCC == 3) continue;
212 matches.insert(jDaughter_p);
213 matches_global.insert(jDaughter_p);
214 }
215 if (matches.empty()) return 0;
216 if (matches.size() == 1) {
217 int jDaughter_p = *(matches.begin());
218 singlematch.emplace_back(iDaughter_d, jDaughter_p);
219 } else multimatch.emplace_back(iDaughter_d, matches);
220 }
221
222 // Now, all daughters of the particles should be matched to at least one DecayDescriptor daughter
223 if (!(this->isIgnoreIntermediate() or this->isIgnoreMassive() or this->isIgnoreNeutrino())
224 && int(matches_global.size()) != nDaughters_p) return 0;
225
226 // In case that there are DecayDescriptor daughters with multiple matches, try to solve the problem
227 // by removing the daughter candidates which are already used in other unambiguous relations.
228 // This is done iteratively. We limit the maximum number of attempts to 20 to avoid an infinite loop.
229 bool isModified = true;
230 for (int iTry = 0; iTry < 20; iTry++) {
231 if (int(singlematch.size()) == getNDaughters()) break;
232 if (!isModified) break;
233 isModified = false;
234 for (auto& itMulti : multimatch) {
235 for (auto& itSingle : singlematch) {
236 // try to remove particle from the multimatch list
237 if (itMulti.second.erase(itSingle.second)) {
238 B2FATAL("Trying to execute part of the code with known bug, which is not fixed yet! Send email to anze.zupanc@ijs.si with notification that this happens!");
239 /*
240 This part of the code is commented, because of the following error:
241 Iterator 'itMulti' used after element has been erased.
242
243 // if multimatch list contains only one particle candidate, move the entry to the singlematch list
244 if (itMulti->second.size() == 1) {
245 int iDaughter_d = itMulti->first;
246 int iDaughter_p = *(itMulti->second.begin());
247 singlematch.push_back(make_pair(iDaughter_d, iDaughter_p));
248 multimatch.erase(itMulti);
249 // call match function again to set the correct daughter
250 if (!isAmbiguities) {
251 const T* daughter = daughterList[iDaughter_p];
252 if (!daughter) continue;
253 m_daughters[iDaughter_d].match(daughter, iDaughter_p);
254 }
255 --itMulti;
256 isModified = true;
257 break;
258 }
259 */
260 }
261 }
262 }
263 }
264
265 if (!multimatch.empty()) isAmbiguities = true;
266 if (isAmbiguities) return -iCC;
267 else {
268 m_iDaughter_p = iDaughter_p;
269 return iCC;
270 }
271 return 0;
272}
273
275{
276 m_iDaughter_p = -1;
277 int nDaughters = m_daughters.size();
278 for (int iDaughter = 0; iDaughter < nDaughters; iDaughter++) m_daughters[iDaughter].resetMatch();
279}
280
281vector<const Particle*> DecayDescriptor::getSelectionParticles(const Particle* particle)
282{
283 // Create vector for output
284 vector<const Particle*> selparticles;
285 if (m_mother.isSelected()) {
286 int motherPDG = abs(particle->getPDGCode());
287 int decayDescriptorMotherPDG = abs(m_mother.getPDGCode());
288 if (motherPDG != decayDescriptorMotherPDG)
289 B2ERROR("The PDG code of the mother particle (" << motherPDG <<
290 ") does not match the PDG code of the DecayDescriptor mother (" << decayDescriptorMotherPDG <<
291 ")! Check the order of the decay string is the same you expect in the reconstructed Particles.");
292 selparticles.push_back(particle);
293 }
294 int nDaughters_d = getNDaughters();
295 for (int iDaughter_d = 0; iDaughter_d < nDaughters_d; ++iDaughter_d) {
296 // retrieve the particle daughter ID from this DecayDescriptor daughter
297 int iDaughter_p = m_daughters[iDaughter_d].getMatchedDaughter();
298 // If the particle daughter ID is below one, the match function was not called before
299 // or the match was ambiguous. In this case try to use the daughter ID of the DecayDescriptor.
300 // This corresponds to using the particle order in the decay string.
301 if (iDaughter_p < 0) iDaughter_p = iDaughter_d;
302 const Particle* daughter = particle->getDaughter(iDaughter_p);
303 if (!daughter) {
304 B2WARNING("Could not find daughter!");
305 continue;
306 }
307 // check if the daughter has the correct PDG code
308 int daughterPDG = abs(daughter->getPDGCode());
309 int decayDescriptorDaughterPDG = abs(m_daughters[iDaughter_d].getMother()->getPDGCode());
310 if (daughterPDG != decayDescriptorDaughterPDG) {
311 B2ERROR("The PDG code of the particle daughter (" << daughterPDG <<
312 ") does not match the PDG code of the DecayDescriptor daughter (" << decayDescriptorDaughterPDG <<
313 ")! Check the order of the decay string is the same you expect in the reconstructed Particles.");
314 break;
315 }
316 vector<const Particle*> seldaughters = m_daughters[iDaughter_d].getSelectionParticles(daughter);
317 selparticles.insert(selparticles.end(), seldaughters.begin(), seldaughters.end());
318 }
319 return selparticles;
320}
321
323{
324
325 std::vector<int> decay, decaybar;
326 for (int i = 0; i < getNDaughters(); ++i) {
327 const DecayDescriptorParticle* daughter = getDaughter(i)->getMother();
328 int pdg = daughter->getPDGCode();
329 decay.push_back(pdg);
330 decaybar.push_back(Belle2::EvtPDLUtil::hasAntiParticle(pdg) ? -pdg : pdg);
331 }
332
333 std::sort(decay.begin(), decay.end());
334 std::sort(decaybar.begin(), decaybar.end());
335
336 return (not Belle2::EvtPDLUtil::hasAntiParticle(getMother()->getPDGCode())) || (decay == decaybar);
337
338}
339
341{
342 vector<string> strNames;
343 if (m_mother.isSelected()) strNames.push_back(m_mother.getNameSimple());
344 for (auto& daughter : m_daughters) {
345 vector<string> strDaughterNames = daughter.getSelectionNames();
346 int nDaughters = strDaughterNames.size();
347 for (int iDaughter = 0; iDaughter < nDaughters; iDaughter++) {
348 // Checking variable naming scheme from AnalysisConfiguratin
349 // For example, effect of possible schemes for PX variable
350 // of pi0 from D in decay B->(D->pi0 pi) pi0:
351 // default: B_D_pi0_PX
352 // semidefault: D_pi0_PX
353 // laconic: pi01_PX
354 if (AnalysisConfiguration::instance()->getTupleStyle() == "laconic") continue;
355 if ((AnalysisConfiguration::instance()->getTupleStyle() == "semilaconic") && (iDaughter == nDaughters)) continue;
356 strDaughterNames[iDaughter] = m_mother.getNameSimple() + "_" + strDaughterNames[iDaughter];
357 }
358 strNames.insert(strNames.end(), strDaughterNames.begin(), strDaughterNames.end());
359 }
360
361 // search for multiple occurrence of the same name and then distinguish by attaching a number
362
363 for (auto itName = strNames.begin(); itName != strNames.end(); ++itName) {
364 if (count(itName, strNames.end(), *itName) == 1) continue;
365 // multiple occurrence found!
366 string strNameOld = *itName;
367 auto itOccurrence = strNames.begin();
368 int iOccurrence = 0;
369 while (iOccurrence <= 10) {
370 // find next occurrence of the identical particle name defined in DecayDescriptor
371 itOccurrence = find(itOccurrence, strNames.end(), strNameOld);
372 // stop, if nothing found
373 if (itOccurrence == strNames.end()) break;
374 // create new particle name by attaching a number
375 string strNameNew = strNameOld + std::to_string(iOccurrence);
376 // check if the new particle name exists already, if not, then it is OK to use it
377 if (count(strNames.begin(), strNames.end(), strNameNew) == 0) {
378 *itOccurrence = strNameNew;
379 ++itOccurrence;
380 }
381 iOccurrence++;
382 }
383 if (iOccurrence == 10) {
384 B2ERROR("DecayDescriptor::getSelectionNames - Something is wrong! More than 10x the same name!");
385 break;
386 }
387 }
388 return strNames;
389}
390
392{
393 vector<int> listPDG;
394 if (m_mother.isSelected()) listPDG.push_back(m_mother.getPDGCode());
395 for (auto& daughter : m_daughters) {
396 vector<int> listPDGDaughters = daughter.getSelectionPDGCodes();
397 listPDG.insert(listPDG.end(), listPDGDaughters.begin(), listPDGDaughters.end());
398 }
399 return listPDG;
400}
401
402
403std::vector<std::vector<std::pair<int, std::string>>> DecayDescriptor::getHierarchyOfSelected()
404{
405 if (not m_hierarchy.empty()) {
406 std::vector<std::vector<std::pair<int, std::string>>> hierarchy = m_hierarchy;
407 return hierarchy;
408 }
409 std::vector<std::pair<int, std::string>> currentPath;
410 currentPath.emplace_back(0, m_mother.getNameSimple());
411 return getHierarchyOfSelected(currentPath);
412}
413
414std::vector<std::vector<std::pair<int, std::string>>> DecayDescriptor::getHierarchyOfSelected(
415 const std::vector<std::pair<int, std::string>>& currentPath)
416{
417 if (m_mother.isSelected()) m_hierarchy.push_back(currentPath);
418 for (std::size_t i = 0; i < m_daughters.size(); i++) {
419 std::vector<std::pair<int, std::string>> newPath = currentPath;
420 newPath.emplace_back(i, m_daughters[i].getMother()->getNameSimple());
421 std::vector<std::vector<std::pair<int, std::string>>> foundPathes = m_daughters[i].getHierarchyOfSelected(newPath);
422 for (auto& path : foundPathes) m_hierarchy.push_back(path);
423 }
424 std::vector<std::vector<std::pair<int, std::string>>> hierarchy = m_hierarchy;
425 return hierarchy;
426}
static AnalysisConfiguration * instance()
Returns a pointer to the singleton instance.
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ParticleType photon
photon particle
Definition: Const.h:673
Represents a particle in the DecayDescriptor.
int getPDGCode() const
Return PDG code.
std::string getNameSimple() const
Return the name from getName() without + - * or anti-.
bool init(const DecayStringParticle &p)
initialise member variables from std::string member variables contained in a DecayStringParticle stru...
bool isSelected() const
Is the particle selected in the decay string?
The DecayDescriptor stores information about a decay tree or parts of a decay tree.
DecayDescriptor()
Default ctor.
bool isIgnoreBrems() const
Check if added Brems gammas shall be ignored.
bool isIgnoreRadiatedPhotons() const
Check if additional radiated photons shall be ignored.
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
bool isSelfConjugated() const
Is the decay or the particle self conjugated.
const DecayDescriptor * getDaughter(int i) const
return i-th daughter (0 based index).
DecayDescriptorParticle m_mother
Mother of the decay ('left side').
bool m_isInitOK
Is this object initialized correctly?
bool isIgnoreNeutrino() const
Check if missing neutrinos shall be ignored.
std::vector< int > getSelectionPDGCodes()
Return list of PDG codes of selected particles.
int getNDaughters() const
return number of direct daughters.
void resetMatch()
Reset results from previous call of the match() function.
bool isIgnoreMassive() const
Check if missing massive final state particles shall be ignored.
std::vector< std::vector< std::pair< int, std::string > > > getHierarchyOfSelected()
Function to get hierarchy of selected particles and their names (for python use)
bool isIgnoreGamma() const
Check if missing gammas shall be ignored.
std::vector< std::string > getSelectionNames()
Return list of human readable names of selected particles.
std::vector< DecayDescriptor > m_daughters
Direct daughters of the decaying particle.
std::vector< std::vector< std::pair< int, std::string > > > m_hierarchy
Collection of hierarchy paths of selected particles.
std::vector< const Particle * > getSelectionParticles(const Particle *particle)
Get a vector of pointers with selected daughters in the decay tree.
int m_iDaughter_p
ID of the Daughter Particle* matched to this DecayDescriptor.
int match(const T *p, int iDaughter_p)
Internally called by match(Particle*) and match(MCParticle*) function.
bool isIgnoreIntermediate() const
Check if intermediate resonances/particles shall be ignored.
const DecayDescriptorParticle * getMother() const
return mother.
int m_properties
Particle property.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Class to store reconstructed particles.
Definition: Particle.h:76
@ c_IsIgnoreNeutrino
Is the particle MC matched with the ignore missing neutrino flag set?
Definition: Particle.h:124
@ c_IsIgnoreRadiatedPhotons
Is the particle MC matched with the ignore radiated photon flag set?
Definition: Particle.h:121
@ c_IsIgnoreGamma
Is the particle MC matched with the ignore missing gamma flag set?
Definition: Particle.h:125
@ c_IsIgnoreBrems
Is the particle MC matched with the ignore added Brems gamma flag set?
Definition: Particle.h:126
@ c_IsIgnoreIntermediate
Is the particle MC matched with the ignore intermediate resonances flag set?
Definition: Particle.h:122
@ c_IsIgnoreMassive
Is the particle MC matched with the ignore missing massive particle flag set?
Definition: Particle.h:123
boost::variant< boost::recursive_wrapper< DecayStringDecay >, DecayStringParticle > DecayString
The DecayStringElement can be either a DecayStringDecay or a vector of mother particles.
Definition: DecayString.h:23
bool hasAntiParticle(int pdgCode)
Checks if the particle with given pdg code has an anti-particle or not.
Definition: EvtPDLUtil.cc:12
Abstract base class for different kinds of events.
STL namespace.
Holds the information of a decay.
This class describes the grammar and the syntax elements of decay strings.
Holds the information of a particle in the decay string.