Belle II Software light-2406-ragdoll
SignalSideVariablesToDaughterExtraInfoModule.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/SignalSideVariablesToDaughterExtraInfo/SignalSideVariablesToDaughterExtraInfoModule.h>
10#include <framework/core/ModuleParam.templateDetails.h>
11#include <framework/datastore/StoreArray.h>
12
13#include <analysis/dataobjects/Particle.h>
14#include <analysis/dataobjects/RestOfEvent.h>
15
16using namespace Belle2;
17
18//-----------------------------------------------------------------
19// Register the Module
20//-----------------------------------------------------------------
21REG_MODULE(SignalSideVariablesToDaughterExtraInfo);
22
23//-----------------------------------------------------------------
24// Implementation
25//-----------------------------------------------------------------
26
28{
29 // Set module properties
30
31 setDescription("The module writes properties (values of specified variables) of the particle related to the current ROE\n"
32 "as an ExtraInfo to the single particle in the input ParticleList.\n"
33 "This module is intended to be executed only in for_each ROE path.");
34
35 // Parameter definitions
36 std::map<std::string, std::string> emptymap;
37 addParam("particleListName", m_particleListName, "The input particleList name. This list should contain at most 1 particle",
38 std::string(""));
39 addParam("variablesToExtraInfo", m_variablesToExtraInfo,
40 "Dictionary of variables and extraInfo names to save in the extra-info field.",
41 emptymap);
42 addParam("overwrite", m_overwrite,
43 "-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",
44 0);
45}
46
48{
51
52 for (const auto& pair : m_variablesToExtraInfo) {
54 if (!var) {
55 B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
56 } else {
57 m_functions.push_back(var->function);
58 m_extraInfoNames.push_back(pair.second);
59 }
60 }
61}
62
64{
66 unsigned int n = plist->getListSize();
67
68 if (n == 0)
69 return;
70
71 if (n > 1)
72 B2WARNING("Input ParticleList " << m_particleListName << " contains more than 1 particle. plist.size = " << n);
73
74 StoreObjPtr<RestOfEvent> roe("RestOfEvent");
75 if (roe.isValid()) {
76 auto* signalSide = roe->getRelatedFrom<Particle>();
77 Particle* daughter = plist->getParticle(0);
78
79 const unsigned int nVars = m_functions.size();
80 for (unsigned int iVar = 0; iVar < nVars; iVar++) {
81 double value = std::numeric_limits<double>::quiet_NaN();
82 if (std::holds_alternative<double>(m_functions[iVar](signalSide))) {
83 value = std::get<double>(m_functions[iVar](signalSide));
84 } else if (std::holds_alternative<int>(m_functions[iVar](signalSide))) {
85 value = std::get<int>(m_functions[iVar](signalSide));
86 } else if (std::holds_alternative<bool>(m_functions[iVar](signalSide))) {
87 value = std::get<bool>(m_functions[iVar](signalSide));
88 }
89 if (daughter->hasExtraInfo(m_extraInfoNames[iVar])) {
90 double current = daughter->getExtraInfo(m_extraInfoNames[iVar]);
91 if (m_overwrite == -1) {
92 if (value < current)
93 daughter->setExtraInfo(m_extraInfoNames[iVar], value);
94 } else if (m_overwrite == 1) {
95 if (value > current)
96 daughter->setExtraInfo(m_extraInfoNames[iVar], value);
97 } else if (m_overwrite == 0) {
98 B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
99 } else if (m_overwrite == 2) {
100 daughter->setExtraInfo(m_extraInfoNames[iVar], value);
101 }
102 } else {
103 daughter->addExtraInfo(m_extraInfoNames[iVar], value);
104 }
105 }
106 }
107}
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class to store reconstructed particles.
Definition: Particle.h:75
virtual void initialize() override
Register input and output data.
std::map< std::string, std::string > m_variablesToExtraInfo
Map of variable and extraInfo names to save in the extra-info field.
SignalSideVariablesToDaughterExtraInfoModule()
Constructor: Sets the description, the properties and the parameters of the module.
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::vector< std::string > m_extraInfoNames
Vector of extra info names.
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
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
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
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.
Definition: ClusterUtils.h:24
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146