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.");
38 addParam("tableName", m_tableName, "ID of table used for reweighing");
39 addParam("particleList", m_inputListName, "Name of the ParticleList to reduce to the best candidates");
40 addParam("selectedDaughters", m_selectedDaughters, "Daughters for which one wants to append weights", std::string(""));
41 addParam("allowToSkip", m_allowToSkip,
42 "If False (default), the basf2 process stops when the payload is not available. If True, this module is skipped.",
43 false);
44}
45
46
47// Getting LookUp info for given particle in given event
49{
50 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
51 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
52 std::map<std::string, double> values;
53 for (const auto& i_variable : variables) {
55 if (!var) {
56 B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
57 }
58 values.insert(std::make_pair(i_variable, std::get<double>(var->function(p))));
59 }
60
61 return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
62}
63
64
66{
68 m_inputList.isRequired(m_inputListName);
69 if (m_selectedDaughters != "")
71
72 m_ParticleWeightingLookUpTable = std::make_unique<OptionalDBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
73
75 if (m_allowToSkip)
76 B2INFO("The payload for the " << m_tableName << " is not available! This module will do nothing.");
77 else
78 B2ERROR("The payload for the " << m_tableName << " is not available! The basf2 process will not start.");
79 }
80}
81
82
84{
85 if (!m_inputList) {
86 B2WARNING("Input list " << m_inputList.getName() << " was not created?");
87 return;
88 }
89
90 if (!(*m_ParticleWeightingLookUpTable)) return;
91
92 const unsigned int numParticles = m_inputList->getListSize();
93 for (unsigned int i = 0; i < numParticles; i++) {
94 const Particle* ppointer = m_inputList->getParticle(i);
95 double index = ppointer->getArrayIndex();
96 Particle* p = m_particles[index];
97
98 if (m_selectedDaughters != "") {
99 auto selParticles = (m_decayDescriptor.getSelectionParticles(p));
100 for (auto& selParticle : selParticles) {
101 Particle* pp = m_particles[selParticle->getArrayIndex()];
102 WeightInfo info = getInfo(pp);
103 for (const auto& entry : info) {
104 pp->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
105 }
106 }
107 } else {
108 WeightInfo info = getInfo(p);
109 for (const auto& entry : info) {
110 p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
111 }
112 }
113 }
114}
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
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
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:76
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:180
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
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: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