Belle II Software  release-05-02-19
ParticleWeightingModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Ilya Komarov *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <analysis/modules/ParticleWeighting/ParticleWeightingModule.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/core/ModuleParam.templateDetails.h>
15 #include <framework/database/DBObjPtr.h>
16 #include <analysis/VariableManager/Manager.h>
17 
18 // framework aux
19 #include <framework/logging/Logger.h>
20 
21 #include <memory>
22 
23 
24 using namespace std;
25 
26 namespace Belle2 {
32  //-----------------------------------------------------------------
33  // Register module
34  //-----------------------------------------------------------------
35 
36  REG_MODULE(ParticleWeighting)
37 
38  //-----------------------------------------------------------------
39  // Implementation
40  //-----------------------------------------------------------------
41 
43 
44  {
45  setDescription("Weights particles according to LookUp table");
46  addParam("tableName", m_tableName, "ID of table used for reweighing");
47  addParam("particleList", m_inputListName, "Name of the ParticleList to reduce to the best candidates");
48  }
49 
50 
51  // Getting LookUp info for given particle in given event
52  WeightInfo ParticleWeightingModule::getInfo(const Particle* p)
53  {
54  std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
55  *m_ParticleWeightingLookUpTable.get())->getAxesNames());
56  std::map<std::string, double> values;
57  for (const auto& i_variable : variables) {
58  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(i_variable);
59  if (!var) {
60  B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
61  }
62  values.insert(std::make_pair(i_variable, var->function(p)));
63  }
64 
65  return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
66  }
67 
68 
69  void ParticleWeightingModule::initialize()
70  {
71  StoreArray<Particle>().isRequired();
72  m_inputList.isRequired(m_inputListName);
73  m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
74  }
75 
76 
77  void ParticleWeightingModule::event()
78  {
79  if (!m_inputList) {
80  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
81  return;
82  }
83  StoreArray<Particle> particles;
84  const unsigned int numParticles = m_inputList->getListSize();
85  for (unsigned int i = 0; i < numParticles; i++) {
86  const Particle* ppointer = m_inputList->getParticle(i);
87  double index = ppointer->getArrayIndex();
88  Particle* p = particles[index];
89  WeightInfo info = getInfo(p);
90  for (const auto& entry : info) {
91  p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
92  }
93  }
94  }
95 
97 } // end Belle2 namespace
98 
Belle2::WeightInfo
std::map< std::string, double > WeightInfo
Weight information: a line from the weight lookup table.
Definition: ParticleWeightingLookUpTable.h:31
Belle2::Variable::Manager::Var
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:137
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::ParticleWeightingModule
Module to apply weights from the database to particles and store added info in ExtraInfo.
Definition: ParticleWeightingModule.h:40
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::StoreArray< Particle >