Belle II Software development
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#include <analysis/dataobjects/Particle.h>
15
16using namespace std;
17using namespace Belle2;
18
19REG_MODULE(VariablesToEventExtraInfo);
20
22{
23 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.");
25
26 std::map<std::string, std::string> emptymap;
27 addParam("particleList", m_inputListName, "Name of particle list with reconstructed particles.");
28 addParam("variables", m_variables,
29 "Dictionary of variables and extraInfo names to save in the event-extra-info field.\n"
30 "Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
31 emptymap);
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
37VariablesToEventExtraInfoModule::~VariablesToEventExtraInfoModule() = default;
38
40{
42 m_inputList.isRequired(m_inputListName);
43 m_eventExtraInfo.isRequired();
44
45 //collection function pointers
46 for (const auto& pair : m_variables) {
48 if (!var) {
49 B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
50 } else {
51 m_functions.push_back(var->function);
52 m_extraInfoNames.push_back(pair.second);
53 }
54 }
55
56}
57
59{
60 if (!m_inputList) {
61 B2WARNING("Input list " << m_inputList.getName() << " was not created?");
62 return;
63 }
64
65 const unsigned int numParticles = m_inputList->getListSize();
66 for (unsigned int i = 0; i < numParticles; i++) {
67 Particle* p = m_inputList->getParticle(i);
69 }
70
71}
72
74{
75 if (not m_eventExtraInfo.isValid()) m_eventExtraInfo.create();
76
77 const unsigned int nVars = m_functions.size();
78 for (unsigned int iVar = 0; iVar < nVars; iVar++) {
79 double value = std::numeric_limits<double>::quiet_NaN();
80 if (std::holds_alternative<double>(m_functions[iVar](source))) {
81 value = std::get<double>(m_functions[iVar](source));
82 } else if (std::holds_alternative<int>(m_functions[iVar](source))) {
83 value = std::get<int>(m_functions[iVar](source));
84 } else if (std::holds_alternative<bool>(m_functions[iVar](source))) {
85 value = std::get<bool>(m_functions[iVar](source));
86 }
87
88 if (m_eventExtraInfo->hasExtraInfo(m_extraInfoNames[iVar])) {
89 double current = m_eventExtraInfo->getExtraInfo(m_extraInfoNames[iVar]);
90 if (m_overwrite == -1) {
91 if (value < current)
92 m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
93 } else if (m_overwrite == 1) {
94 if (value > current)
95 m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
96 } else if (m_overwrite == 0) {
97 B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
98 } else if (m_overwrite == 2) {
99 m_eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
100 }
101
102 } else {
103 m_eventExtraInfo->addExtraInfo(m_extraInfoNames[iVar], value);
104 }
105 }
106}
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
Class to store reconstructed particles.
Definition Particle.h:76
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
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition Manager.cc:58
static Manager & Instance()
get singleton instance.
Definition Manager.cc:26
void addEventExtraInfo(const Particle *source)
Adds extra info to the event.
virtual void event() override
process event: actually adds the extra info
StoreObjPtr< ParticleList > m_inputList
input particle list
int m_overwrite
-1/0/1/2: Overwrite if lower / don't overwrite / overwrite if higher / always overwrite,...
std::vector< Variable::Manager::FunctionPtr > m_functions
Vector of function pointers corresponding to given variables.
std::map< std::string, std::string > m_variables
Map of variables and extraInfo names to save in the event-extra-info field.
StoreObjPtr< EventExtraInfo > m_eventExtraInfo
event extra info object pointer
std::vector< std::string > m_extraInfoNames
Vector of extra info names.
std::string m_inputListName
name of input particle list.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.
A variable returning a floating-point value for a given Particle.
Definition Manager.h:145