Belle II Software development
ParticleWeightingModule.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// Own header.
10#include <analysis/modules/ParticleWeighting/ParticleWeightingModule.h>
11
12#include <framework/core/ModuleParam.templateDetails.h>
13#include <analysis/VariableManager/Manager.h>
14
15// framework aux
16#include <framework/logging/Logger.h>
17
18#include <memory>
19
20
21using namespace std;
22using namespace Belle2;
23
24//-----------------------------------------------------------------
25// Register module
26//-----------------------------------------------------------------
27
28REG_MODULE(ParticleWeighting);
29
30//-----------------------------------------------------------------
31// Implementation
32//-----------------------------------------------------------------
33
35{
36 setDescription("Append weights from the database into the extraInfo of Particles.");
37 addParam("tableName", m_tableName, "ID of table used for reweighing");
38 addParam("particleList", m_inputListName, "Name of the ParticleList to reduce to the best candidates");
39 addParam("selectedDaughters", m_selectedDaughters, "Daughters for which one wants to append weights", std::string(""));
40 addParam("allowToSkip", m_allowToSkip,
41 "If False (default), the basf2 process stops when the payload is not available. If True, this module is skipped.",
42 false);
43}
44
45
46// Getting LookUp info for given particle in given event
48{
49 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
50 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
51 std::map<std::string, double> values;
52 for (const auto& i_variable : variables) {
54 if (!var) {
55 B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
56 }
57 values.insert(std::make_pair(i_variable, std::get<double>(var->function(p))));
58 }
59
60 return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
61}
62
63
65{
67 m_inputList.isRequired(m_inputListName);
68 if (m_selectedDaughters != "")
70
71 m_ParticleWeightingLookUpTable = std::make_unique<OptionalDBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
72
74 if (m_allowToSkip)
75 B2INFO("The payload for the " << m_tableName << " is not available! This module will do nothing.");
76 else
77 B2ERROR("The payload for the " << m_tableName << " is not available! The basf2 process will not start.");
78 }
79}
80
81
83{
84 if (!m_inputList) {
85 B2WARNING("Input list " << m_inputList.getName() << " was not created?");
86 return;
87 }
88
89 if (!(*m_ParticleWeightingLookUpTable)) return;
90
91 const unsigned int numParticles = m_inputList->getListSize();
92 for (unsigned int i = 0; i < numParticles; i++) {
93 const Particle* ppointer = m_inputList->getParticle(i);
94 double index = ppointer->getArrayIndex();
95 Particle* p = m_particles[index];
96
97 if (m_selectedDaughters != "") {
98 auto selParticles = (m_decayDescriptor.getSelectionParticles(p));
99 for (auto& selParticle : selParticles) {
100 Particle* pp = m_particles[selParticle->getArrayIndex()];
101 WeightInfo info = getInfo(pp);
102 for (const auto& entry : info) {
103 pp->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
104 }
105 }
106 } else {
107 WeightInfo info = getInfo(p);
108 for (const auto& entry : info) {
109 p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
110 }
111 }
112 }
113}
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.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processing by the module.
WeightInfo getInfo(const Particle *p)
Get LookUp information for the particle.
StoreArray< Particle > m_particles
StoreArray of Particles.
std::string m_selectedDaughters
Daughters for which one wants to append weights.
StoreObjPtr< ParticleList > m_inputList
input particle list
DecayDescriptor m_decayDescriptor
Decay Descriptor to be initialized with m_selectedDaughters.
std::unique_ptr< OptionalDBObjPtr< ParticleWeightingLookUpTable > > m_ParticleWeightingLookUpTable
Pointer to the table in DB.
std::string m_tableName
Name of the table.
bool m_allowToSkip
Controls whether the process is skipped when the payload is missing.
std::string m_inputListName
name of input particle list.
Class to store reconstructed particles.
Definition: Particle.h:75
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
Definition: Manager.cc:179
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
std::map< std::string, double > WeightInfo
Weight information: a line from the weight lookup table.
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