Belle II Software  release-05-02-19
VariablesToExtraInfoModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Simon Kohl, Anze Zupanc *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <analysis/modules/VariablesToExtraInfo/VariablesToExtraInfoModule.h>
12 
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/core/ModuleParam.templateDetails.h>
16 
17 using namespace std;
18 using namespace Belle2;
19 
20 REG_MODULE(VariablesToExtraInfo)
21 
23 {
24  setDescription("For each particle in the input list the selected variables are saved in an extra-info field with the given name. Can be used when wanting to save variables before modifying them, e.g. when performing vertex fits.");
25  setPropertyFlags(c_ParallelProcessingCertified);
26 
27  std::map<std::string, std::string> emptymap;
28  addParam("particleList", m_inputListName, "Name of particle list with reconstructed particles.");
29  addParam("variables", m_variables,
30  "Dictionary of variables and extraInfo names to save in the extra-info field.\n"
31  "Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
32  emptymap);
33  addParam("decayString", m_decayString, "DecayString specifying the daughter Particle to be included in the ParticleList",
34  std::string(""));
35  addParam("overwrite", m_overwrite,
36  "-1/0/1/2: Overwrite if lower / don't overwrite / overwrite if higher / always overwrite, in case if extra info with given name already exists",
37  0);
38 }
39 
40 VariablesToExtraInfoModule::~VariablesToExtraInfoModule() = default;
41 
42 void VariablesToExtraInfoModule::initialize()
43 {
44  StoreArray<Particle>().isRequired();
45  m_inputList.isRequired(m_inputListName);
46 
47  //collection function pointers
48  for (const auto& pair : m_variables) {
49  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(pair.first);
50  if (!var) {
51  B2ERROR("Variable '" << pair.first << "' is not available in Variable::Manager!");
52  } else {
53  m_functions.push_back(var->function);
54  m_extraInfoNames.push_back(pair.second);
55  }
56  }
57 
58  if (not m_decayString.empty()) {
59  m_writeToDaughter = true;
60 
61  bool valid = m_pDDescriptor.init(m_decayString);
62  if (!valid)
63  B2ERROR("VariablesToExtraInfoModule::initialize Invalid Decay Descriptor: " << m_decayString);
64  }
65 }
66 
67 void VariablesToExtraInfoModule::event()
68 {
69  StoreArray<Particle> particles;
70 
71  if (!m_inputList) {
72  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
73  return;
74  }
75 
76 
77  const unsigned int numParticles = m_inputList->getListSize();
78  for (unsigned int i = 0; i < numParticles; i++) {
79  Particle* p = m_inputList->getParticle(i);
80 
81  if (not m_writeToDaughter) {
82  addExtraInfo(p, p);
83  } else {
84  std::vector<const Particle*> selparticles = m_pDDescriptor.getSelectionParticles(p);
85  for (auto& selparticle : selparticles) {
86  Particle* daug = particles[selparticle->getArrayIndex()];
87  addExtraInfo(p, daug);
88  }
89  }
90  }
91 }
92 
93 void VariablesToExtraInfoModule::addExtraInfo(const Particle* source, Particle* destination)
94 {
95  const unsigned int nVars = m_functions.size();
96  for (unsigned int iVar = 0; iVar < nVars; iVar++) {
97  double value = m_functions[iVar](source);
98  if (destination->hasExtraInfo(m_extraInfoNames[iVar])) {
99  double current = destination->getExtraInfo(m_extraInfoNames[iVar]);
100  if (m_overwrite == -1) {
101  if (value < current)
102  destination->setExtraInfo(m_extraInfoNames[iVar], value);
103  } else if (m_overwrite == 1) {
104  if (value > current)
105  destination->setExtraInfo(m_extraInfoNames[iVar], value);
106  } else if (m_overwrite == 0) {
107  B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
108  } else if (m_overwrite == 2) {
109  destination->setExtraInfo(m_extraInfoNames[iVar], value);
110  }
111 
112  } else {
113  destination->addExtraInfo(m_extraInfoNames[iVar], value);
114  }
115  }
116 }
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::Particle::addExtraInfo
void addExtraInfo(const std::string &name, float value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1224
Belle2::Particle::setExtraInfo
void setExtraInfo(const std::string &name, float value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1205
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Particle::getExtraInfo
float getExtraInfo(const std::string &name) const
Return given value if set.
Definition: Particle.cc:1177
Belle2::VariablesToExtraInfoModule
For each particle in the input list the selected variables are saved in an extra-info field with the ...
Definition: VariablesToExtraInfoModule.h:54
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::StoreArray< Particle >
Belle2::Particle::hasExtraInfo
bool hasExtraInfo(const std::string &name) const
Return wether the extra info with the given name is set.
Definition: Particle.cc:1154