Belle II Software development
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
14using namespace std;
15using namespace Belle2;
16
17REG_MODULE(VariablesToExtraInfo);
18
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.");
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
37VariablesToExtraInfoModule::~VariablesToExtraInfoModule() = default;
38
40{
42 m_inputList.isRequired(m_inputListName);
43
44 //collection function pointers
45 for (const auto& pair : m_variables) {
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
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
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}
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
std::vector< const Particle * > getSelectionParticles(const Particle *particle)
Get a vector of pointers with selected daughters in the decay tree.
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:75
void setExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1317
bool hasExtraInfo(const std::string &name) const
Return whether the extra info with the given name is set.
Definition: Particle.cc:1266
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1336
double getExtraInfo(const std::string &name) const
Return given value if set.
Definition: Particle.cc:1289
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:57
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
bool m_writeToDaughter
if true the extraInfo is written to daughter specified by the decay string
virtual void initialize() override
initialise
virtual void event() override
process event: actually adds the extra info
std::string m_decayString
DecayString specifying the daughter Particle to which the extra-info field will be added.
StoreArray< Particle > m_particles
StoreArray of Particles.
DecayDescriptor m_pDDescriptor
Decay descriptor of the particle being selected.
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 extra-info field.
std::vector< std::string > m_extraInfoNames
Vector of extra info names.
std::string m_inputListName
name of input particle list.
void addExtraInfo(const Particle *source, Particle *destination)
Adds extra info to the particle.
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:560
#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.
STL namespace.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146