9#include <analysis/modules/MCMatcherParticles/MCMatcherParticlesModule.h>
12#include <analysis/utility/MCMatching.h>
13#include <analysis/utility/AnalysisConfiguration.h>
16#include <unordered_map>
18typedef std::unordered_map<unsigned int, unsigned int> CounterMap;
35 setDescription(
"Performs MC matching (sets relation Particle->MCParticle) for all particles\n"
36 "(and its (grand)^N-daughter particles) in the ParticleList. The relation can\n"
37 "be used in conjunction with MCMatching::MCErrorFlags flags, e.g. using the\n"
38 "isSignal or mcPDG & mcErrors variables.\n"
40 "In addition to the usual mc matching algorithm the module can run also loose mc\n"
41 "matching. The difference between loose and normal mc matching algorithm is that\n"
42 "the loose algorithm will find the common mother of the majority of daughter\n"
43 "particles while the normal algorithm finds the common mother of all daughters.\n"
44 "The results of loose mc matching algorithm are stored to the following extraInfo\n"
46 "- looseMCMotherPDG: PDG code of most common mother\n"
47 "- looseMCMotherIndex: 1-based StoreArray<MCParticle> index of most common mother\n"
48 "- looseMCWrongDaughterN: number of daughters that don't originate from the most"
50 "- looseMCWrongDaughterPDG: PDG code of the daughter that doesn't originate from\n"
51 " the most common mother (only if looseMCWrongDaughterN = 1)\n"
52 "- looseMCWrongDaughterBiB: 1 if the wrong daughter is Beam Induced Background\n"
66 B2WARNING(
"No MCParticles array found!"
67 <<
" This is obvously fine if you're analysing real data,"
68 <<
" but you have added the MCMatcher module to your path,"
69 <<
" did you mean to do this?");
79 B2INFO(
"MCMatcher module will search for Particle -> MCParticle associations for the ParticleList " <<
m_listName <<
".");
81 B2INFO(
" - The MCMatcher will use legacy algorithm suitable for analysis of Belle MC.");
83 B2INFO(
" - The MCMatcher will use default algorithm suitable for analysis of Belle II MC.");
93 B2ERROR(
"ParticleList " <<
m_listName <<
" not found");
97 const unsigned int n =
m_plist->getListSize();
98 for (
unsigned i = 0; i < n; i++) {
117 CounterMap motherCount;
119 for (
auto daughter : fsDaughters) {
124 vector<int> genMothers;
127 for (
auto motherIndex : genMothers) {
130 if ((motherPDG == 553) ||
131 (motherPDG == 100553) ||
132 (motherPDG == 200553) ||
133 (motherPDG == 300553) ||
134 (motherPDG == 9000553) ||
135 (motherPDG == 9010553) ||
136 (motherPDG == 10022))
139 motherCount[motherIndex]++;
144 auto commonMother = std::max_element
146 std::begin(motherCount), std::end(motherCount),
147 [](std::pair <unsigned int, unsigned int> p1, std::pair <unsigned int, unsigned int> p2) {
148 bool returnValue =
false;
149 if (p1.second < p2.second)
151 else if (p1.second == p2.second)
152 returnValue = p2.first > p1.first;
159 if (commonMother == std::end(motherCount)) {
163 thisParticle->
addExtraInfo(
"looseMCWrongDaughterN", -1);
164 thisParticle->
addExtraInfo(
"looseMCWrongDaughterPDG", -1);
165 thisParticle->
addExtraInfo(
"looseMCWrongDaughterBiB", -1);
172 thisParticle->
addExtraInfo(
"looseMCMotherPDG", mcMother->getPDG());
173 thisParticle->
addExtraInfo(
"looseMCMotherIndex", mcMother->getArrayIndex());
174 thisParticle->
addExtraInfo(
"looseMCWrongDaughterN", fsDaughters.size() - commonMother->second);
181 int wrongParticlePDG = 0;
182 int wrongParticleBiB = 0;
183 if (fsDaughters.size() - commonMother->second == 1) {
184 for (
auto daughter : fsDaughters) {
187 wrongParticlePDG = daughter->getPDGCode();
188 wrongParticleBiB = 1;
191 vector<int> genMothers;
195 if (find(genMothers.begin(), genMothers.end(), commonMother->first) != genMothers.end())
199 wrongParticlePDG = daughter->getPDGCode();
203 thisParticle->
addExtraInfo(
"looseMCWrongDaughterPDG", wrongParticlePDG);
204 thisParticle->
addExtraInfo(
"looseMCWrongDaughterBiB", wrongParticleBiB);
void useLegacyMCMatching(const bool flag)
Determines whether to use the legacy MCMatching algorithm (true) or not (false).
static AnalysisConfiguration * instance()
Returns a pointer to the singleton instance.
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
StoreArray< MCParticle > m_mcparticles
the array of MCParticles.
std::string m_listName
steering variable: name of the input ParticleList
StoreArray< Particle > m_particles
the array of Particles.
bool m_looseMatching
perform loose mc matching
void setLooseMCMatch(const Particle *particle)
Finds common mother of the majority of daughters.
MCMatcherParticlesModule()
Constructor.
StoreObjPtr< ParticleList > m_plist
the input ParticleList.
A Class to store the Monte Carlo particle information.
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Class to store reconstructed particles.
std::vector< const Belle2::Particle * > getFinalStateDaughters() const
Returns a vector of pointers to Final State daughter particles.
bool hasExtraInfo(const std::string &name) const
Return whether the extra info with the given name is set.
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
static bool setMCTruth(const Belle2::Particle *particle)
This is the main function of MC matching algorithm.
static void fillGenMothers(const Belle2::MCParticle *mcP, std::vector< int > &genMCPMothers)
Fills vector with array (1-based) indices of all generator ancestors of given MCParticle.