Belle II Software  release-05-02-19
VariablesToEventExtraInfoModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Yo Sato *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <analysis/modules/VariablesToEventExtraInfo/VariablesToEventExtraInfoModule.h>
12 
13 #include <framework/logging/Logger.h>
14 #include <framework/core/ModuleParam.templateDetails.h>
15 #include <framework/datastore/StoreArray.h>
16 
17 #include <analysis/dataobjects/EventExtraInfo.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 REG_MODULE(VariablesToEventExtraInfo)
23 
25 {
26  setDescription("For each particle in the input list the selected variables are saved in an event-extra-info field with the given name. Can be used to save MC truth information, for example, in a ntuple of reconstructed particles.");
27  setPropertyFlags(c_ParallelProcessingCertified);
28 
29  std::map<std::string, std::string> emptymap;
30  addParam("particleList", m_inputListName, "Name of particle list with reconstructed particles.");
31  addParam("variables", m_variables,
32  "Dictionary of variables and extraInfo names to save in the event-extra-info field.\n"
33  "Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
34  emptymap);
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 VariablesToEventExtraInfoModule::~VariablesToEventExtraInfoModule() = default;
41 
42 void VariablesToEventExtraInfoModule::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 }
59 
60 void VariablesToEventExtraInfoModule::event()
61 {
62  StoreArray<Particle> particles;
63 
64  if (!m_inputList) {
65  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
66  return;
67  }
68 
69  const unsigned int numParticles = m_inputList->getListSize();
70  for (unsigned int i = 0; i < numParticles; i++) {
71  Particle* p = m_inputList->getParticle(i);
72  addEventExtraInfo(p);
73  }
74 
75 }
76 
77 void VariablesToEventExtraInfoModule::addEventExtraInfo(const Particle* source)
78 {
79  StoreObjPtr<EventExtraInfo> eventExtraInfo;
80  if (not eventExtraInfo.isValid()) eventExtraInfo.create();
81 
82  const unsigned int nVars = m_functions.size();
83  for (unsigned int iVar = 0; iVar < nVars; iVar++) {
84  double value = m_functions[iVar](source);
85 
86  if (eventExtraInfo->hasExtraInfo(m_extraInfoNames[iVar])) {
87  double current = eventExtraInfo->getExtraInfo(m_extraInfoNames[iVar]);
88  if (m_overwrite == -1) {
89  if (value < current)
90  eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
91  } else if (m_overwrite == 1) {
92  if (value > current)
93  eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
94  } else if (m_overwrite == 0) {
95  B2WARNING("Extra info with given name " << m_extraInfoNames[iVar] << " already set, I won't set it again.");
96  } else if (m_overwrite == 2) {
97  eventExtraInfo->setExtraInfo(m_extraInfoNames[iVar], value);
98  }
99 
100  } else {
101  eventExtraInfo->addExtraInfo(m_extraInfoNames[iVar], value);
102  }
103  }
104 }
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::VariablesToEventExtraInfoModule
For each particle in the input list the selected variables are saved in an event-extra-info field wit...
Definition: VariablesToEventExtraInfoModule.h:46
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::StoreArray< Particle >