 |
Belle II Software
release-05-02-19
|
11 #include <analysis/modules/ParticleMCDecayString/ParticleMCDecayStringModule.h>
13 #include <analysis/dataobjects/Particle.h>
14 #include <analysis/dataobjects/ParticleList.h>
15 #include <mdst/dataobjects/MCParticle.h>
16 #include <analysis/dataobjects/StringWrapper.h>
18 #include <framework/logging/Logger.h>
19 #include <framework/pcore/ProcHandler.h>
21 #include <framework/datastore/StoreArray.h>
22 #include <framework/datastore/StoreObjPtr.h>
27 #include <boost/algorithm/string.hpp>
38 DataStore::c_Persistent), m_decayHash(0.0), m_decayHashExtended(0.0)
40 setDescription(
"Creates the Monte Carlo decay string of a Particle and its daughters. "
41 "The MC decay string of the particle is hashed and saved as a 32bit pattern in the extra info field decayHash of the particle. "
42 "The MC decay string of the particle + its daughters is hashed as well and saved as another 32bit pattern in the extra info field decayHashExtended of the particle. "
43 "The mapping hash <-> MC decay string in saved in a TTree by this module. "
44 "The 32bit pattern must be saved as a float (because our extra info field, variable manager and ntuple output only supports float) "
45 "but they just represent 32 bits of a hash! "
46 "The MC decay string can also be stored in an analysis ROOT file using the MCDecayString NtupleTool. "
47 "Details on the MC decay string format can be found here: `MCDecayString`");
49 addParam(
"listName",
m_listName,
"Particles from these ParticleList are used as input.");
50 addParam(
"fileName",
m_fileName,
"Filename in which the hash strings are saved, if empty the strings are not saved",
52 addParam(
"treeName",
m_treeName,
"Tree name in which the hash strings are saved", std::string(
"hashtable"));
53 addParam(
"conciseString",
m_useConciseString,
"If set to true, the code will use a more concise format for the string.",
false);
54 addParam(
"identifiers",
m_identifiers,
"Identifiers used to identify particles in the concise format.",
55 std::string(
"abcdefghijklmnopqrstuvwxyz"));
66 particles.isRequired();
69 stringWrapperArray.registerInDataStore();
70 particles.registerRelationTo(stringWrapperArray);
77 B2WARNING(
"Could not create file " <<
m_fileName);
85 B2WARNING(
"Tree with this name already exists: " <<
m_fileName);
94 m_tree->get().SetBasketSize(
"*", 1600);
95 m_tree->get().SetCacheSize(100000);
107 StoreArray<StringWrapper> stringWrapperArray;
109 for (
unsigned iParticle = 0; iParticle < pList->getListSize(); ++iParticle) {
110 Particle* particle = pList->getParticle(iParticle);
117 uint32_t decayHash =
m_hasher(decayString);
118 uint32_t decayHashExtended =
m_hasher(decayStringExtended);
120 uint64_t m_decayHashFull = decayHash;
121 m_decayHashFull <<= 32;
122 m_decayHashFull += decayHashExtended;
125 assert(
sizeof(
float) ==
sizeof(uint32_t));
131 convert bitconverter;
133 bitconverter.i = decayHash;
139 bitconverter.i = decayHashExtended;
145 StringWrapper* stringWrapper = stringWrapperArray.appendNew();
146 particle->addRelationTo(stringWrapper);
149 auto it =
m_hashset->get().find(m_decayHashFull);
151 m_hashset->get().insert(m_decayHashFull);
168 const bool writeError =
m_file->TestBit(TFile::kWriteError);
172 B2FATAL(
"A write error occurred while saving '" <<
m_fileName <<
"', please check if enough disk space is available.");
184 const MCParticle* mcPMother = mcP->getMother();
185 if (mcPMother ==
nullptr) {
227 std::string output =
" ";
229 output += std::to_string(p->getPDGCode());
231 if (not
isFSP(p->getPDGCode())) {
233 for (
auto daughter : p->getDaughters()) {
250 if (not
isFSP(p->getPDGCode())) {
251 for (
auto& daughter : p->getDaughters()) {
263 if (mcPMatched ==
nullptr)
271 if (mcPMatched->getPDG() == 10022)
272 return decayString +
" (Virtual gamma match)";
280 std::stringstream ss;
286 ss << mcPMother->
getPDG();
302 std::vector<std::string> decayStrings;
303 boost::split(decayStrings,
string, boost::is_any_of(
"|"));
305 if (decayStrings.empty()) {
306 B2WARNING(
"ParticleMCDecayStringModule: unable to convert decay string to concise format.");
310 unsigned int nParticles(decayStrings.size() - 1);
312 B2WARNING(
"ParticleMCDecayStringModule: not enough identifiers have been specified to use the concise string format:"
313 << std::endl <<
"Number of particles in your decay mode = " << nParticles << std::endl
315 <<
"Standard format will be used instead.");
320 std::string mode(
"");
321 std::vector<int> caretPositions;
322 for (
auto& decayString : decayStrings) {
323 std::string thisString(decayString);
329 int caretPosition(thisString.find(
'^'));
330 caretPositions.push_back(caretPosition);
331 if (caretPosition > -1) {
332 decayString.erase(caretPosition, 1);
337 std::string theDecayString(
"");
338 for (
auto thisString : decayStrings) {
339 if (thisString == mode) {
continue;}
342 char finalChar(thisString.back());
343 if (finalChar !=
' ') {thisString = thisString +
" ";}
345 if (
" (No match) " != thisString) {
346 if (
"" == theDecayString) {
347 theDecayString = thisString;
349 if (theDecayString != thisString) {
357 std::string modifiedString(theDecayString);
360 int nStrings(caretPositions.size());
361 for (
int iString(0); iString < nStrings; ++iString) {
363 int insertPosition(caretPositions.at(iString));
364 if (insertPosition > -1) {
365 for (
int jString(0); jString < iString; ++jString) {
366 if (caretPositions.at(jString) > -1 && caretPositions.at(jString) <= caretPositions.at(iString)) {
370 modifiedString.insert(insertPosition, identifier);
374 modifiedString = mode +
"|" + modifiedString;
377 bool noMatchStringAdded(
false);
378 for (
int iString(0); iString < nStrings; ++iString) {
379 int insertPosition(caretPositions.at(iString));
380 if (-1 == insertPosition) {
381 if (!noMatchStringAdded) {
382 modifiedString +=
" | No match: ";
383 noMatchStringAdded =
true;
389 string = modifiedString;
StoreObjPtr< RootMergeable< TTree > > m_tree
ROOT TNtuple containting the saved hashes and strings.
TFile * m_file
ROOT file to store the hashes and strings.
ParticleMCDecayStringModule()
Constructor.
void setDescription(const std::string &description)
Sets the description of the module.
bool isFSP(int pdg)
defines what is a final state particle for this purpose.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
const std::string c_ExtraInfoName
Name of the extraInfo, which is stored in each Particle.
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
float m_decayHash
Decay hash -> The hash of the decay string of the mother particle.
virtual void event() override
Called for each event.
std::string getMCDecayStringFromParticle(const Particle *p)
get mc decay string from particle
std::string m_identifiers
Characters used to identify particles in the concise decay string format (default: alphabet).
const std::string c_ExtraInfoNameExtended
Name of the extraInfo, which is stored in each Particle.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
static bool isOutputProcess()
Return true if the process is an output process.
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
std::string getDecayStringFromParticle(const Particle *p)
get decay string of particle
virtual void initialize() override
Initialize the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
float m_decayHashExtended
Extended decay hash -> The hash of the decay string of all daughter particles.
StoreObjPtr< SetMergeable< std::unordered_set< uint64_t > > > m_hashset
Mergeable unordered set containing the encountered hashes.
std::string getDecayString(const Particle &p)
get the decay string for p.
Abstract base class for different kinds of events.
Type-safe access to single objects in the data store.
virtual void terminate() override
Terminate modules.
int getPDG() const
Return PDG code of particle.
std::string m_fileName
Filename in which the hash strings are saved, if empty the strings are not saved.
const MCParticle * getInitialParticle(const MCParticle *mcP)
search from mcP upwards for a particle that matches specified mother PDG codes.
void convertToConciseString(std::string &string)
Convert the extended string to a more concise format.
bool m_useConciseString
Switch to use concise format for the extended string.
std::string m_listName
Name of the particle list.
std::vector< Belle2::MCParticle * > getDaughters() const
Get vector of all daughter particles, empty vector if none.
Class to store reconstructed particles.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
std::string m_decayString
The complete decay string.
std::string m_treeName
Tree name in which the hash strings are saved.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
std::hash< std::string > m_hasher
Hash function.
A Class to store the Monte Carlo particle information.
std::string buildMCDecayString(const MCParticle *mcPMother, const MCParticle *mcPMatched)
return decay string for mcPMother, highlight mcPMatched.
std::string getMCDecayStringFromMCParticle(const MCParticle *mcPMatched)
get mc decay string from mc particle