Belle II Software light-2406-ragdoll
SignalSideVariablesToExtraInfoModule.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/SignalSideVariablesToExtraInfo/SignalSideVariablesToExtraInfoModule.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(SignalSideVariablesToExtraInfo);
22
23//-----------------------------------------------------------------
24// Implementation
25//-----------------------------------------------------------------
26
28{
29 // Set module properties
30 setDescription("The module writes property (value of specified variable) of single particle\n"
31 "found in the input ParticleList as an ExtraInfo to the Particle related to\n"
32 "the current ROE. This module is intended to be executed only in for_each ROE\n"
33 "path.");
34
35 // Parameter definitions
36 std::map<std::string, std::string> emptymap;
37 addParam("particleListName", m_particleListName, "The input particleList name.\n"
38 "This list should either contain at most 1 particle or all requested variables should be event-based.",
39 std::string(""));
40 addParam("variableToExtraInfo", m_variableToExtraInfo,
41 "Dictionary of variables and extraInfo names to save in the extra-info field.",
42 emptymap);
43}
44
46{
49
50 for (const auto& pair : m_variableToExtraInfo) {
52 if (!var) {
53 B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
54 } else {
55 m_functions.push_back(var->function);
56 m_extraInfoNames.push_back(pair.second);
57 if (m_allVariablesEventbased and var->description.find("[Eventbased]") == std::string::npos)
59 }
60 }
61}
62
64{
66 unsigned int n = plist->getListSize();
67
68 if (n == 0)
69 return;
70
71 if (n > 1 and !m_allVariablesEventbased)
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
78 const unsigned int nVars = m_functions.size();
79 for (unsigned int iVar = 0; iVar < nVars; iVar++) {
80 if (signalSide->hasExtraInfo(m_extraInfoNames[iVar])) {
81 B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
82 } else {
83 double value = std::numeric_limits<double>::quiet_NaN();
84 auto var_result = m_functions[iVar](plist->getParticle(0));
85 if (std::holds_alternative<double>(var_result)) {
86 value = std::get<double>(var_result);
87 } else if (std::holds_alternative<int>(var_result)) {
88 value = std::get<int>(var_result);
89 } else if (std::holds_alternative<bool>(var_result)) {
90 value = std::get<bool>(var_result);
91 }
92 signalSide->addExtraInfo(m_extraInfoNames[iVar], value);
93 }
94 }
95 }
96}
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
std::string m_particleListName
The input particleList name.
bool m_allVariablesEventbased
Whether all variables are event-based.
virtual void initialize() override
Register input and output data.
StoreObjPtr< ParticleList > m_inputList
input particle list
std::map< std::string, std::string > m_variableToExtraInfo
Map of variable and extraInfo name to save in the extra-info field.
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