Belle II Software  release-06-00-14
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 include
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 
21 using namespace std;
22 
23 namespace Belle2 {
29  //-----------------------------------------------------------------
30  // Register module
31  //-----------------------------------------------------------------
32 
33  REG_MODULE(ParticleWeighting)
34 
35  //-----------------------------------------------------------------
36  // Implementation
37  //-----------------------------------------------------------------
38 
40 
41  {
42  setDescription("Weights particles according to LookUp table");
43  addParam("tableName", m_tableName, "ID of table used for reweighing");
44  addParam("particleList", m_inputListName, "Name of the ParticleList to reduce to the best candidates");
45  }
46 
47 
48  // Getting LookUp info for given particle in given event
49  WeightInfo ParticleWeightingModule::getInfo(const Particle* p)
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) {
55  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(i_variable);
56  if (!var) {
57  B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
58  }
59  values.insert(std::make_pair(i_variable, var->function(p)));
60  }
61 
62  return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
63  }
64 
65 
66  void ParticleWeightingModule::initialize()
67  {
68  m_particles.isRequired();
69  m_inputList.isRequired(m_inputListName);
70  m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
71  }
72 
73 
74  void ParticleWeightingModule::event()
75  {
76  if (!m_inputList) {
77  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
78  return;
79  }
80  const unsigned int numParticles = m_inputList->getListSize();
81  for (unsigned int i = 0; i < numParticles; i++) {
82  const Particle* ppointer = m_inputList->getParticle(i);
83  double index = ppointer->getArrayIndex();
84  Particle* p = m_particles[index];
85  WeightInfo info = getInfo(p);
86  for (const auto& entry : info) {
87  p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
88  }
89  }
90  }
91 
93 } // end Belle2 namespace
94 
Base class for Modules.
Definition: Module.h:72
Module to apply weights from the database to particles and store added info in ExtraInfo.
Class to store reconstructed particles.
Definition: Particle.h:74
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
std::map< std::string, double > WeightInfo
Weight information: a line from the weight lookup table.
#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.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:133