Belle II Software  release-08-01-10
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 
21 using namespace std;
22 using namespace Belle2;
23 
24 //-----------------------------------------------------------------
25 // Register module
26 //-----------------------------------------------------------------
27 
28 REG_MODULE(ParticleWeighting);
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
34 ParticleWeightingModule::ParticleWeightingModule() : Module()
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 }
41 
42 
43 // Getting LookUp info for given particle in given event
45 {
46  std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
47  *m_ParticleWeightingLookUpTable.get())->getAxesNames());
48  std::map<std::string, double> values;
49  for (const auto& i_variable : variables) {
51  if (!var) {
52  B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
53  }
54  values.insert(std::make_pair(i_variable, std::get<double>(var->function(p))));
55  }
56 
57  return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
58 }
59 
60 
62 {
63  m_particles.isRequired();
64  m_inputList.isRequired(m_inputListName);
65  if (m_selectedDaughters != "")
67  m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
68 }
69 
70 
72 {
73  if (!m_inputList) {
74  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
75  return;
76  }
77 
78  const unsigned int numParticles = m_inputList->getListSize();
79  for (unsigned int i = 0; i < numParticles; i++) {
80  const Particle* ppointer = m_inputList->getParticle(i);
81  double index = ppointer->getArrayIndex();
82  Particle* p = m_particles[index];
83 
84  if (m_selectedDaughters != "") {
85  auto selParticles = (m_decayDescriptor.getSelectionParticles(p));
86  for (auto& selParticle : selParticles) {
87  Particle* pp = m_particles[selParticle->getArrayIndex()];
88  WeightInfo info = getInfo(pp);
89  for (const auto& entry : info) {
90  pp->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
91  }
92  }
93  } else {
94  WeightInfo info = getInfo(p);
95  for (const auto& entry : info) {
96  p->addExtraInfo(m_tableName + "_" + entry.first, entry.second);
97  }
98  }
99  }
100 }
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.
std::unique_ptr< DBObjPtr< ParticleWeightingLookUpTable > > m_ParticleWeightingLookUpTable
Pointer to the table in DB.
StoreObjPtr< ParticleList > m_inputList
input particle list
DecayDescriptor m_decayDescriptor
Decay Descriptor to be initialized with m_selectedDaughters.
std::string m_tableName
Name of the table.
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.
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.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146