Belle II Software  release-08-01-10
VariablesToEventExtraInfoModule.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/VariablesToEventExtraInfo/VariablesToEventExtraInfoModule.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/core/ModuleParam.templateDetails.h>
13 #include <framework/datastore/StoreArray.h>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 REG_MODULE(VariablesToEventExtraInfo);
19 
20 VariablesToEventExtraInfoModule::VariablesToEventExtraInfoModule()
21 {
22  setDescription("For each particle in the input list the selected variables are saved in an event-extra-info field with the given name. Can be used to save MC truth information, for example, in a ntuple of reconstructed particles.");
23  setPropertyFlags(c_ParallelProcessingCertified);
24 
25  std::map<std::string, std::string> emptymap;
26  addParam("particleList", m_inputListName, "Name of particle list with reconstructed particles.");
27  addParam("variables", m_variables,
28  "Dictionary of variables and extraInfo names to save in the event-extra-info field.\n"
29  "Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
30  emptymap);
31  addParam("overwrite", m_overwrite,
32  "-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",
33  0);
34 }
35 
36 VariablesToEventExtraInfoModule::~VariablesToEventExtraInfoModule() = default;
37 
38 void VariablesToEventExtraInfoModule::initialize()
39 {
41  m_inputList.isRequired(m_inputListName);
42 
43  //collection function pointers
44  for (const auto& pair : m_variables) {
45  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(pair.first);
46  if (!var) {
47  B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
48  } else {
49  m_functions.push_back(var->function);
50  m_extraInfoNames.push_back(pair.second);
51  }
52  }
53 
54 }
55 
56 void VariablesToEventExtraInfoModule::event()
57 {
58  if (!m_inputList) {
59  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
60  return;
61  }
62 
63  const unsigned int numParticles = m_inputList->getListSize();
64  for (unsigned int i = 0; i < numParticles; i++) {
65  Particle* p = m_inputList->getParticle(i);
66  addEventExtraInfo(p);
67  }
68 
69 }
70 
71 void VariablesToEventExtraInfoModule::addEventExtraInfo(const Particle* source)
72 {
73  if (not m_eventExtraInfo.isValid()) m_eventExtraInfo.create();
74 
75  const unsigned int nVars = m_functions.size();
76  for (unsigned int iVar = 0; iVar < nVars; iVar++) {
77  double value = std::numeric_limits<double>::quiet_NaN();
78  if (std::holds_alternative<double>(m_functions[iVar](source))) {
79  value = std::get<double>(m_functions[iVar](source));
80  } else if (std::holds_alternative<int>(m_functions[iVar](source))) {
81  value = std::get<int>(m_functions[iVar](source));
82  } else if (std::holds_alternative<bool>(m_functions[iVar](source))) {
83  value = std::get<bool>(m_functions[iVar](source));
84  }
85 
86  if (m_eventExtraInfo->hasExtraInfo(m_extraInfoNames[iVar])) {
87  double current = m_eventExtraInfo->getExtraInfo(m_extraInfoNames[iVar]);
88  if (m_overwrite == -1) {
89  if (value < current)
90  m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
91  } else if (m_overwrite == 1) {
92  if (value > current)
93  m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
94  } else if (m_overwrite == 0) {
95  B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
96  } else if (m_overwrite == 2) {
97  m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
98  }
99 
100  } else {
101  m_eventExtraInfo->addExtraInfo(m_extraInfoNames[iVar], value);
102  }
103  }
104 }
Class to store reconstructed particles.
Definition: Particle.h:75
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
#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