Belle II Software  release-08-01-10
VariablesToExtraInfoModule.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/VariablesToExtraInfo/VariablesToExtraInfoModule.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/core/ModuleParam.templateDetails.h>
13 
14 using namespace std;
15 using namespace Belle2;
16 
17 REG_MODULE(VariablesToExtraInfo);
18 
19 VariablesToExtraInfoModule::VariablesToExtraInfoModule()
20 {
21  setDescription("For each particle in the input list the selected variables are saved in an extra-info field with the given name. Can be used when wanting to save variables before modifying them, e.g. when performing vertex fits.");
22  setPropertyFlags(c_ParallelProcessingCertified);
23 
24  std::map<std::string, std::string> emptymap;
25  addParam("particleList", m_inputListName, "Name of particle list with reconstructed particles.");
26  addParam("variables", m_variables,
27  "Dictionary of variables and extraInfo names to save in the extra-info field.\n"
28  "Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
29  emptymap);
30  addParam("decayString", m_decayString, "DecayString specifying the daughter Particle to be included in the ParticleList",
31  std::string(""));
32  addParam("overwrite", m_overwrite,
33  "-1/0/1/2: Overwrite if lower / don't overwrite / overwrite if higher / always overwrite, in case if extra info with given name already exists",
34  0);
35 }
36 
37 VariablesToExtraInfoModule::~VariablesToExtraInfoModule() = default;
38 
39 void VariablesToExtraInfoModule::initialize()
40 {
41  m_particles.isRequired();
42  m_inputList.isRequired(m_inputListName);
43 
44  //collection function pointers
45  for (const auto& pair : m_variables) {
46  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(pair.first);
47  if (!var) {
48  B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
49  } else {
50  m_functions.push_back(var->function);
51  m_extraInfoNames.push_back(pair.second);
52  }
53  }
54 
55  if (not m_decayString.empty()) {
56  m_writeToDaughter = true;
57 
58  bool valid = m_pDDescriptor.init(m_decayString);
59  if (!valid)
60  B2ERROR("VariablesToExtraInfoModule::initialize Invalid Decay Descriptor: " << m_decayString);
61  }
62 }
63 
64 void VariablesToExtraInfoModule::event()
65 {
66  if (!m_inputList) {
67  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
68  return;
69  }
70 
71  const unsigned int numParticles = m_inputList->getListSize();
72  for (unsigned int i = 0; i < numParticles; i++) {
73  Particle* p = m_inputList->getParticle(i);
74 
75  if (not m_writeToDaughter) {
76  addExtraInfo(p, p);
77  } else {
78  std::vector<const Particle*> selparticles = m_pDDescriptor.getSelectionParticles(p);
79  for (auto& selparticle : selparticles) {
80  Particle* daug = m_particles[selparticle->getArrayIndex()];
81  addExtraInfo(p, daug);
82  }
83  }
84  }
85 }
86 
87 void VariablesToExtraInfoModule::addExtraInfo(const Particle* source, Particle* destination)
88 {
89  const unsigned int nVars = m_functions.size();
90  for (unsigned int iVar = 0; iVar < nVars; iVar++) {
91  double value = std::numeric_limits<double>::quiet_NaN();
92  if (std::holds_alternative<double>(m_functions[iVar](source))) {
93  value = std::get<double>(m_functions[iVar](source));
94  } else if (std::holds_alternative<int>(m_functions[iVar](source))) {
95  value = std::get<int>(m_functions[iVar](source));
96  } else if (std::holds_alternative<bool>(m_functions[iVar](source))) {
97  value = std::get<bool>(m_functions[iVar](source));
98  }
99  if (destination->hasExtraInfo(m_extraInfoNames[iVar])) {
100  double current = destination->getExtraInfo(m_extraInfoNames[iVar]);
101  if (m_overwrite == -1) {
102  if (value < current)
103  destination->setExtraInfo(m_extraInfoNames[iVar], value);
104  } else if (m_overwrite == 1) {
105  if (value > current)
106  destination->setExtraInfo(m_extraInfoNames[iVar], value);
107  } else if (m_overwrite == 0) {
108  B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
109  } else if (m_overwrite == 2) {
110  destination->setExtraInfo(m_extraInfoNames[iVar], value);
111  }
112 
113  } else {
114  destination->addExtraInfo(m_extraInfoNames[iVar], value);
115  }
116  }
117 }
Class to store reconstructed particles.
Definition: Particle.h:75
void setExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1318
bool hasExtraInfo(const std::string &name) const
Return whether the extra info with the given name is set.
Definition: Particle.cc:1267
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1337
double getExtraInfo(const std::string &name) const
Return given value if set.
Definition: Particle.cc:1290
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146