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 <framework/database/DBObjPtr.h>
14#include <analysis/VariableManager/Manager.h>
15
16// framework aux
17#include <framework/logging/Logger.h>
18
19#include <memory>
20
21
22using namespace std;
23using namespace Belle2;
24
25//-----------------------------------------------------------------
26// Register module
27//-----------------------------------------------------------------
28
29REG_MODULE(ParticleWeighting);
30
31//-----------------------------------------------------------------
32// Implementation
33//-----------------------------------------------------------------
34
36{
37 setDescription("Append weights from the database into the extraInfo of Particles.");
39 addParam("tableName", m_tableName, "ID of table used for reweighing");
40 addParam("particleList", m_inputListName, "Name of the ParticleList to reduce to the best candidates");
41 addParam("selectedDaughters", m_selectedDaughters, "Daughters for which one wants to append weights", std::string(""));
42 addParam("allowToSkip", m_allowToSkip,
43 "If False (default), the basf2 process stops when the payload is not available. If True, this module is skipped.",
44 false);
45}
46
47
48// Getting LookUp info for given particle in given event
50{
51 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
52 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
53 std::map<std::string, double> values;
54 for (const auto& i_variable : variables) {
56 if (!var) {
57 B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
58 }
59 values.insert(std::make_pair(i_variable, std::get<double>(var->function(p))));
60 }
61
62 return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
63}
64
65
67{
68 m_particles.isRequired();
69 m_inputList.isRequired(m_inputListName);
70 if (m_selectedDaughters != "")
72
73 m_ParticleWeightingLookUpTable = std::make_unique<OptionalDBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
74
76 if (m_allowToSkip)
77 B2INFO("The payload for the " << m_tableName << " is not available! This module will do nothing.");
78 else
79 B2ERROR("The payload for the " << m_tableName << " is not available! The basf2 process will not start.");
80 }
81}
82
83
85{
86 if (!m_inputList) {
87 B2WARNING("Input list " << m_inputList.getName() << " was not created?");
88 return;
89 }
90
91 if (!(*m_ParticleWeightingLookUpTable)) return;
92
93 const unsigned int numParticles = m_inputList->getListSize();
94 for (unsigned int i = 0; i < numParticles; i++) {
95 const Particle* ppointer = m_inputList->getParticle(i);
96 double index = ppointer->getArrayIndex();
97 Particle* p = m_particles[index];
98
99 if (m_selectedDaughters != "") {
100 auto selParticles = (m_decayDescriptor.getSelectionParticles(p));
101 for (auto& selParticle : selParticles) {
102 Particle* pp = m_particles[selParticle->getArrayIndex()];
103 WeightInfo info = getInfo(pp);
104 for (const auto& entry : info) {
105 pp->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
106 }
107 }
108 } else {
109 WeightInfo info = getInfo(p);
110 for (const auto& entry : info) {
111 p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
112 }
113 }
114 }
115}
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
Module()
Constructor.
Definition Module.cc:30
@ 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.
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