Belle II Software  release-06-01-15
ParticleMCDecayStringModule.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/modules/ParticleMCDecayString/ParticleMCDecayStringModule.h>
10 
11 #include <mdst/dataobjects/MCParticle.h>
12 
13 #include <framework/logging/Logger.h>
14 #include <framework/pcore/ProcHandler.h>
15 
16 #include <string>
17 #include <vector>
18 
19 #include <boost/algorithm/string.hpp>
20 
21 namespace Belle2 {
27  REG_MODULE(ParticleMCDecayString)
28 
29  ParticleMCDecayStringModule::ParticleMCDecayStringModule() : Module(), m_tree("", DataStore::c_Persistent), m_hashset("",
30  DataStore::c_Persistent), m_decayHash(0.0), m_decayHashExtended(0.0)
31  {
32  setDescription("Creates the Monte Carlo decay string of a Particle and its daughters. "
33  "The MC decay string of the particle is hashed and saved as a 32bit pattern in the extra info field decayHash of the particle. "
34  "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. "
35  "The mapping hash <-> MC decay string in saved in a TTree by this module. "
36  "The 32bit pattern must be saved as a float (because our extra info field, variable manager and ntuple output only supports float) "
37  "but they just represent 32 bits of a hash! "
38  "The MC decay string can also be stored in an analysis ROOT file using the MCDecayString NtupleTool. "
39  "Details on the MC decay string format can be found here: `MCDecayString`");
40  setPropertyFlags(c_ParallelProcessingCertified | c_TerminateInAllProcesses);
41  addParam("listName", m_listName, "Particles from these ParticleList are used as input.");
42  addParam("fileName", m_fileName, "Filename in which the hash strings are saved, if empty the strings are not saved",
43  std::string(""));
44  addParam("treeName", m_treeName, "Tree name in which the hash strings are saved", std::string("hashtable"));
45  addParam("conciseString", m_useConciseString, "If set to true, the code will use a more concise format for the string.", false);
46  addParam("identifiers", m_identifiers, "Identifiers used to identify particles in the concise format.",
47  std::string("abcdefghijklmnopqrstuvwxyz"));
48 
49  m_file = nullptr;
50  }
51 
53  {
54  m_pList.isRequired(m_listName);
55 
56  //This might not work for non-default names of Particle array:
58 
59  m_stringWrapperArray.registerInDataStore();
61 
62 
63  // Initializing the output root file
64  if (m_fileName != "") {
65  m_file = new TFile(m_fileName.c_str(), "RECREATE");
66  if (!m_file->IsOpen()) {
67  B2WARNING("Could not create file " << m_fileName);
68  return;
69  }
70 
71  m_file->cd();
72 
73  // check if TTree with that name already exists
74  if (m_file->Get(m_treeName.c_str())) {
75  B2WARNING("Tree with this name already exists: " << m_fileName);
76  return;
77  }
78 
79  m_tree.registerInDataStore(m_fileName + m_treeName, DataStore::c_DontWriteOut);
80  m_tree.construct(m_treeName.c_str(), "Decay Hash Map");
81  m_tree->get().Branch("decayHash", &m_decayHash);
82  m_tree->get().Branch("decayHashExtended", &m_decayHashExtended);
83  m_tree->get().Branch("decayString", &m_decayString);
84  m_tree->get().SetBasketSize("*", 1600);
85  m_tree->get().SetCacheSize(100000);
86  }
87 
88  m_hashset.registerInDataStore(m_fileName + m_treeName + "_hashset", DataStore::c_DontWriteOut);
89  m_hashset.construct();
90 
91  }
92 
94  {
95 
96  for (unsigned iParticle = 0; iParticle < m_pList->getListSize(); ++iParticle) {
97  Particle* particle = m_pList->getParticle(iParticle);
98 
99  const std::string decayString = getMCDecayStringFromMCParticle(particle->getRelatedTo<MCParticle>());
100  std::string decayStringExtended = getDecayString(*particle); //removed const to allow string to be modified to a different format.
101 
102  if (m_useConciseString) {convertToConciseString(decayStringExtended);}
103 
104  uint32_t decayHash = m_hasher(decayString);
105  uint32_t decayHashExtended = m_hasher(decayStringExtended);
106 
107  uint64_t m_decayHashFull = decayHash;
108  m_decayHashFull <<= 32;
109  m_decayHashFull += decayHashExtended;
110 
111  // Convert unsigned int decay hash into a float keeping the same bit pattern
112  assert(sizeof(float) == sizeof(uint32_t));
113 
114  union convert {
115  uint32_t i;
116  float f;
117  };
118  convert bitconverter;
119 
120  bitconverter.i = decayHash;
121  m_decayHash = bitconverter.f;
122  particle->addExtraInfo(c_ExtraInfoName, m_decayHash);
123 
124  // cppcheck doesn't like this use of union and throws warnings
125  // cppcheck-suppress redundantAssignment
126  bitconverter.i = decayHashExtended;
127  m_decayHashExtended = bitconverter.f;
128  particle->addExtraInfo(c_ExtraInfoNameExtended, m_decayHashExtended);
129 
130  m_decayString = decayStringExtended;
131 
132  StringWrapper* stringWrapper = m_stringWrapperArray.appendNew();
133  particle->addRelationTo(stringWrapper);
134  stringWrapper->setString(m_decayString);
135 
136  auto it = m_hashset->get().find(m_decayHashFull);
137  if (it == m_hashset->get().end()) {
138  m_hashset->get().insert(m_decayHashFull);
139 
140  if (m_tree.isValid()) {
141  m_tree->get().Fill();
142  }
143  }
144 
145  }
146  }
147 
149  {
151  if (m_tree.isValid()) {
152  B2INFO("Writing NTuple " << m_treeName);
153  m_tree->write(m_file);
154 
155  const bool writeError = m_file->TestBit(TFile::kWriteError);
156  if (writeError) {
157  //m_file deleted first so we have a chance of closing it (though that will probably fail)
158  delete m_file;
159  B2FATAL("A write error occurred while saving '" << m_fileName << "', please check if enough disk space is available.");
160  }
161 
162  B2INFO("Closing file " << m_fileName);
163  delete m_file;
164  }
165  }
166  }
167 
168 
170  {
171  const MCParticle* mcPMother = mcP->getMother();
172  if (mcPMother == nullptr) {
173  return mcP;
174  } else {
175  return getInitialParticle(mcPMother);
176  }
177  }
178 
180  bool isFSP(int pdg)
181  {
182  switch (abs(pdg)) {
183  case 211: //pi^+
184  case 321: //K^+
185  case 11: //e
186  case 12: //nu_e
187  case 13: //mu
188  case 14: //nu_mu
189  case 16: //nu_tau
190  case 22: //gamma
191  case 310: //K_S
192  case 130: //K_L
193  case 2112: //n
194  case 2212: //p
195  return true;
196  default:
197  return false;
198  }
199  }
200 
202  {
203 
204  std::string output;
205  output += getDecayStringFromParticle(&p) + " | ";
206  output += getMCDecayStringFromParticle(&p);
207  return output;
208 
209  }
210 
212  {
213 
214  std::string output = " ";
215 
216  output += std::to_string(p->getPDGCode());
217 
218  if (not isFSP(p->getPDGCode())) {
219  output += " (-->";
220  for (auto daughter : p->getDaughters()) {
221  output += getDecayStringFromParticle(daughter);
222  }
223  output += ")";
224  }
225 
226  return output;
227 
228  }
229 
231  {
232 
233  std::string output;
234 
235  output = getMCDecayStringFromMCParticle(p->getRelatedTo<MCParticle>());
236  // Some FSPs can have daughters, e.g. converted Photons and K-Shorts
237  if (not isFSP(p->getPDGCode())) {
238  for (auto& daughter : p->getDaughters()) {
239  output += " | " + getMCDecayStringFromParticle(daughter);
240  }
241  }
242 
243  return output;
244 
245  }
246 
248  {
249 
250  if (mcPMatched == nullptr)
251  return "(No match)";
252 
253  // TODO Performance can be optimized, this mcPMother does not change during the construction
254  const MCParticle* mcPMother = getInitialParticle(mcPMatched);
255 
256  std::string decayString = buildMCDecayString(mcPMother, mcPMatched);
257 
258  if (mcPMatched->getPDG() == 10022)
259  return decayString + " (Virtual gamma match)";
260  return decayString;
261  }
262 
263 
264  std::string ParticleMCDecayStringModule::buildMCDecayString(const MCParticle* mcPMother, const MCParticle* mcPMatched)
265  {
266 
267  std::stringstream ss;
268  ss << " ";
269  if (mcPMother->getArrayIndex() == mcPMatched->getArrayIndex()) {
270  ss << "^";
271  }
272 
273  ss << mcPMother->getPDG();
274 
275  if (not isFSP(mcPMother->getPDG())) {
276  ss << " (-->";
277  for (auto daughter : mcPMother->getDaughters()) {
278  ss << buildMCDecayString(daughter, mcPMatched);
279  }
280  ss << ")";
281  }
282 
283  return ss.str();
284  }
285 
287  {
288 
289  std::vector<std::string> decayStrings;
290  boost::split(decayStrings, string, boost::is_any_of("|"));
291 
292  if (decayStrings.empty()) {
293  B2WARNING("ParticleMCDecayStringModule: unable to convert decay string to concise format.");
294  return;
295  }
296 
297  unsigned int nParticles(decayStrings.size() - 1);
298  if (nParticles > m_identifiers.size()) {
299  B2WARNING("ParticleMCDecayStringModule: not enough identifiers have been specified to use the concise string format:"
300  << std::endl << "Number of particles in your decay mode = " << nParticles << std::endl
301  << "Available identifiers: " << m_identifiers << std::endl
302  << "Standard format will be used instead.");
303  return;
304  }
305 
306  //Find positions of carets in original strings, store them, and then erase them.
307  std::string mode("");
308  std::vector<int> caretPositions;
309  for (auto& decayString : decayStrings) {
310  std::string thisString(decayString);
311  if ("" == mode) {
312  mode = thisString;
313  continue;
314  }
315 
316  int caretPosition(thisString.find('^')); // -1 if no match.
317  caretPositions.push_back(caretPosition);
318  if (caretPosition > -1) {
319  decayString.erase(caretPosition, 1);
320  }
321  }
322 
323  //Check if all of the decay strings are the same (except for No matches):
324  std::string theDecayString("");
325  for (auto thisString : decayStrings) {
326  if (thisString == mode) {continue;}
327 
328  //last decay string does not have a space at the end, don't want this to stop a match.
329  char finalChar(thisString.back());
330  if (finalChar != ' ') {thisString = thisString + " ";}
331 
332  if (" (No match) " != thisString) {
333  if ("" == theDecayString) {
334  theDecayString = thisString;
335  } else {
336  if (theDecayString != thisString) {
337  //TODO: add string format if multiple decay strings are present (e.g. pile-up events).
338  return;
339  }
340  }
341  }
342  }
343 
344  std::string modifiedString(theDecayString);
345 
346  //insert identifiers in positions where carets were:
347  int nStrings(caretPositions.size());
348  for (int iString(0); iString < nStrings; ++iString) {
349  std::string identifier(m_identifiers.substr(iString, 1));
350  int insertPosition(caretPositions.at(iString));
351  if (insertPosition > -1) {
352  for (int jString(0); jString < iString; ++jString) {
353  if (caretPositions.at(jString) > -1 && caretPositions.at(jString) <= caretPositions.at(iString)) {
354  ++insertPosition;
355  }
356  }
357  modifiedString.insert(insertPosition, identifier);
358  }
359  }
360 
361  modifiedString = mode + "|" + modifiedString;
362 
363  //add a list of the unmatched particles at the end of the string:
364  bool noMatchStringAdded(false);
365  for (int iString(0); iString < nStrings; ++iString) {
366  int insertPosition(caretPositions.at(iString));
367  if (-1 == insertPosition) {
368  if (!noMatchStringAdded) {
369  modifiedString += " | No match: ";
370  noMatchStringAdded = true;
371  }
372  modifiedString += m_identifiers.substr(iString, 1);
373  }
374  }
375 
376  string = modifiedString;
377  return;
378  }
379 
381 } // Belle2 namespace
382 
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:71
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
std::vector< Belle2::MCParticle * > getDaughters() const
Get vector of all daughter particles, empty vector if none.
Definition: MCParticle.cc:50
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:244
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:112
Base class for Modules.
Definition: Module.h:72
Adds the Monte Carlo decay string to a Particle.
float m_decayHash
Decay hash -> The hash of the decay string of the mother particle.
const std::string c_ExtraInfoName
Name of the extraInfo, which is stored in each Particle.
std::string m_decayString
The complete decay string.
std::string m_listName
Name of the particle list.
std::string m_fileName
Filename in which the hash strings are saved, if empty the strings are not saved.
std::hash< std::string > m_hasher
Hash function.
TFile * m_file
ROOT file to store the hashes and strings.
bool m_useConciseString
Switch to use concise format for the extended string.
StoreObjPtr< RootMergeable< TTree > > m_tree
ROOT TNtuple containing the saved hashes and strings.
const std::string c_ExtraInfoNameExtended
Name of the extraInfo, which is stored in each Particle.
StoreObjPtr< SetMergeable< std::unordered_set< uint64_t > > > m_hashset
Mergeable unordered set containing the encountered hashes.
StoreArray< StringWrapper > m_stringWrapperArray
StoreArray of StringWrappers.
std::string m_treeName
Tree name in which the hash strings are saved.
std::string m_identifiers
Characters used to identify particles in the concise decay string format (default: alphabet).
float m_decayHashExtended
Extended decay hash -> The hash of the decay string of all daughter particles.
StoreObjPtr< ParticleList > m_pList
input particle list
Class to store reconstructed particles.
Definition: Particle.h:74
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:232
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
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.
Definition: StoreArray.h:140
This class is a wrapper for strings, such as MCDecayStrings, to allow them to be associated with part...
Definition: StringWrapper.h:23
void setString(const std::string &inputstring)
Set string.
Definition: StringWrapper.h:41
bool isFSP(int pdg)
defines what is a final state particle for this purpose.
std::string getDecayStringFromParticle(const Particle *p)
get decay string of particle
virtual void initialize() override
Initialize the module.
virtual void event() override
Called for each event.
virtual void terminate() override
Terminate modules.
std::string getMCDecayStringFromMCParticle(const MCParticle *mcPMatched)
get mc decay string from mc particle
void convertToConciseString(std::string &string)
Convert the extended string to a more concise format.
std::string buildMCDecayString(const MCParticle *mcPMother, const MCParticle *mcPMatched)
return decay string for mcPMother, highlight mcPMatched.
const MCParticle * getInitialParticle(const MCParticle *mcP)
search from mcP upwards for a particle that matches specified mother PDG codes.
std::string getMCDecayStringFromParticle(const Particle *p)
get mc decay string from particle
std::string getDecayString(const Particle &p)
get the decay string for p.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:582
Abstract base class for different kinds of events.