9#include <analysis/modules/MCDecayFinder/MCDecayFinderModule.h>
10#include <framework/gearbox/Const.h>
11#include <analysis/DecayDescriptor/ParticleListName.h>
12#include <analysis/utility/EvtPDLUtil.h>
13#include <analysis/utility/MCMatching.h>
27 setDescription(
"Find decays in MCParticle list matching a given DecayString and create Particles from them.");
32 "If true, all daughters of the matched MCParticle will be added in the order defined at the MCParticle. "
33 "If false, only the daughters described in the given decayString will be appended to the output particle.",
36 "If true, the secondary MC daughters will be skipped to append to the output particles. Default: true",
39 "If true, the output ParticleList will be saved by RootOutput. If false, it will be ignored when writing the file.",
false);
46 B2ERROR(
"Invalid input DecayString: " <<
m_strDecay);
51 B2DEBUG(10,
"particle list name: " <<
m_listName);
87 for (
int i = 0; i < nMCParticles; i++) {
92 for (
int iCC = 0; iCC < 2; iCC++) {
99 B2DEBUG(19,
"Match!");
116 }
else if (arrayIndex == -1) {
142 const int iPDGD = d->getMother()->getPDGCode();
143 const int iPDGP = mcp->
getPDG();
146 if (!isCC && iPDGD != iPDGP)
return decay;
147 else if (isCC && (iPDGD != -iPDGP && !isSelfConjugatedParticle))
return decay;
148 else if (isCC && (iPDGD != iPDGP && isSelfConjugatedParticle))
return decay;
149 B2DEBUG(19,
"PDG code matched: " << iPDGP);
153 const int nDaughtersD = d->getNDaughters();
154 if (nDaughtersD == 0) {
155 B2DEBUG(19,
"DecayDescriptor has no Daughters, everything OK!");
161 vector<const MCParticle*> daughtersP;
162 int nDaughtersRecursiveD = 0;
163 if (d->isIgnoreIntermediate()) {
168 const vector<MCParticle*>& tmpDaughtersP = mcp->
getDaughters();
169 for (
auto daug : tmpDaughtersP)
170 daughtersP.push_back(daug);
171 nDaughtersRecursiveD = nDaughtersD;
175 if (nDaughtersRecursiveD > (
int)daughtersP.size()) {
176 B2DEBUG(10,
"DecayDescriptor has more daughters than MCParticle!");
181 std::vector<std::pair<int, int>> daughtersDepthMapD;
182 for (
int iDD = 0; iDD < nDaughtersD; iDD++) {
186 daughtersDepthMapD.push_back({-1 * depth, iDD});
189 std::sort(daughtersDepthMapD.begin(), daughtersDepthMapD.end());
192 for (
auto pair : daughtersDepthMapD) {
193 int iDD = pair.second;
195 bool isMatchDaughter =
false;
196 auto itDP = daughtersP.begin();
197 while (itDP != daughtersP.end()) {
202 if (!daughter->getObj()) {
208 decay->append(daughter);
209 isMatchDaughter =
true;
210 itDP = daughtersP.erase(itDP);
213 if (d->isIgnoreIntermediate() and d->getDaughter(iDD)->getNDaughters() != 0) {
214 vector<const MCParticle*> grandDaughtersP;
215 if (d->getDaughter(iDD)->isIgnoreIntermediate()) {
218 const vector<MCParticle*>& tmpGrandDaughtersP = daugP->
getDaughters();
219 for (
auto grandDaugP : tmpGrandDaughtersP)
220 grandDaughtersP.push_back(grandDaugP);
223 for (
auto grandDaugP : grandDaughtersP) {
225 while (jtDP != daughtersP.end()) {
228 if (grandDaugP == daugP_j)
229 jtDP = daughtersP.erase(jtDP);
238 if (!isMatchDaughter) {
249 B2DEBUG(10,
"isSignal is not True. There was an additional particle left.");
250 decay->setObj(
nullptr);
256 B2DEBUG(19,
"Match found!");
270 vector<const MCParticle*> tmp_children;
271 const vector<MCParticle*>& genDaughters = gen->
getDaughters();
272 for (
auto daug : genDaughters) {
273 tmp_children.push_back(daug);
274 children.push_back(daug);
276 for (
auto child : tmp_children) {
283 const int nDaughter = d->getNDaughters();
284 if (nDaughter == 0)
return nDaughter;
286 int nDaughterRecursive = nDaughter;
287 for (
int iDaug = 0; iDaug < nDaughter; iDaug++) {
293 return nDaughterRecursive;
299 for (
int i = 0; i < d->getNDaughters(); i++) {
303 if (tmp_depth > maxDepth)
304 maxDepth = tmp_depth;
321 const vector<DecayTree<MCParticle>*> decayDaughters = decay->
getDaughters();
324 if ((
int)decayDaughters.size() != nDaughters) {
325 B2ERROR(
"MCDecayFinderModule::buildParticleFromDecayTree Inconsistency on the number of daughters between DecayTree and DecayDescriptor");
352 std::vector<std::pair<int, int>> daughtersDepthMapD;
353 for (
int iDD = 0; iDD < nDaughters; iDD++) {
358 daughtersDepthMapD.push_back({-1 * depth, iDD});
360 std::sort(daughtersDepthMapD.begin(), daughtersDepthMapD.end());
362 for (
int iDD = 0; iDD < nDaughters; iDD++) {
364 int index_decayDaughter = 0;
365 for (
auto pair : daughtersDepthMapD) {
366 if (pair.second == iDD)
break;
367 index_decayDaughter++;
373 newParticle->
appendDaughter(partDaughter,
false, daughterProperty);
398 if (existingParticle.isCopyOf(newParticle))
int getPDGCode() const
PDG code.
static const ParticleType Lambda
Lambda particle.
static const ParticleType Kshort
K^0_S particle.
EStoreFlags
Flags describing behaviours of objects etc.
@ c_WriteOut
Object/array should be saved by output modules.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
int getPDGCode() const
Return PDG code.
int getProperty() const
return property of the particle.
The DecayDescriptor stores information about a decay tree or parts of a decay tree.
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
const DecayDescriptor * getDaughter(int i) const
return i-th daughter (0 based index).
int getNDaughters() const
return number of direct daughters.
int getProperty() const
return property of the particle.
const DecayDescriptorParticle * getMother() const
return mother.
This is a helper class for the MCDecayFinderModule.
Particle * createParticleRecursively(const MCParticle *mcp, bool skipNonPrimaryDaughters)
Create a Particle from the given MCParticle appending all daughters of the MCParticle.
bool m_isSelfConjugatedParticle
Is the particle list for a self-conjugated particle.
std::string m_antiListName
Name of output anti-particle list.
DecayTree< MCParticle > * match(const MCParticle *mcp, const DecayDescriptor *d, bool isCC, int &arrayIndex)
Search for MCParticles matching the given DecayString.
Particle * buildParticleFromDecayTree(const DecayTree< MCParticle > *decay, const DecayDescriptor *dd)
Build a Particle from the given DecayTree and DecayDescriptor.
int getNDaughtersRecursive(const DecayDescriptor *d)
Recursively get number of daughters of given DecayDescriptor.
virtual void initialize() override
Initialises the module.
bool performMCMatching(const DecayTree< MCParticle > *decay, const DecayDescriptor *dd)
Perform the MCMatching on the Particle built from the given DecayTree and DecayDescriptor.
virtual void event() override
Method called for each event.
StoreArray< MCParticle > m_mcparticles
StoreArray of MCParticles.
std::string m_listName
Name of output particle list.
StoreArray< Particle > m_particles
StoreArray of Particles.
StoreObjPtr< ParticleList > m_outputList
output particle list
std::string m_strDecay
Decay string to build the decay descriptor.
void appendParticles(const MCParticle *gen, std::vector< const MCParticle * > &children)
Recursively gather all MC daughters of gen.
void addUniqueParticleToList(Particle *newParticle)
Add a new Particle into the output ParticleList if it does not exist already.
bool m_appendAllDaughters
if true, all daughters are appended
bool m_skipNonPrimaryDaughters
toggle skip of secondary MC daughters
MCDecayFinderModule()
Constructor.
StoreObjPtr< ParticleList > m_antiOutputList
output anti-particle list
DecayDescriptor m_decaydescriptor
Decay descriptor of decays to look for.
bool m_writeOut
toggle output particle list btw.
void countMaxDepthOfNest(const DecayDescriptor *d, int &depth)
Count the max depth of nest from the given DecayDescriptor.
StoreObjPtr< ParticleExtraInfoMap > m_extraInfoMap
object pointer to ParticleExtraInfoMaps
A Class to store the Monte Carlo particle information.
@ c_PrimaryParticle
bit 0: Particle is primary particle.
std::vector< Belle2::MCParticle * > getDaughters() const
Get vector of all daughter particles, empty vector if none.
int getPDG() const
Return PDG code of particle.
void setDescription(const std::string &description)
Sets the description of the module.
Class to store reconstructed particles.
void setProperty(const int properties)
sets m_properties
void appendDaughter(const Particle *daughter, const bool updateType=true, const int daughterProperty=c_Ordinary)
Appends index of daughter to daughters index array.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
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 registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
T * appendNew()
Construct a new T object at the end of the array.
int getEntries() const
Get the number of objects in the array.
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.
T * getObj() const
Return the decaying object itself, e.g.
std::vector< DecayTree< T > * > getDaughters() const
Return list of decay daughters.
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.
bool hasAntiParticle(int pdgCode)
Checks if the particle with given pdg code has an anti-particle or not.
std::string antiParticleListName(const std::string &listName)
Returns name of anti-particle-list corresponding to listName.
Abstract base class for different kinds of events.
@ c_Correct
This Particle and all its daughters are perfectly reconstructed.
static int getMCErrors(const Belle2::Particle *particle, const Belle2::MCParticle *mcParticle=nullptr)
Returns quality indicator of the match as a bit pattern where the individual bits indicate the the ty...
static bool isFSP(int pdg)
Returns true if given PDG code indicates a FSP.