Belle II Software  release-08-01-10
ParticlePrinterModule.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/ParticlePrinter/ParticlePrinterModule.h>
11 
12 // framework aux
13 #include <framework/logging/Logger.h>
14 
15 // dataobjects
16 #include <analysis/VariableManager/Manager.h>
17 
18 using namespace std;
19 using namespace Belle2;
20 
21 //-----------------------------------------------------------------
22 // Register module
23 //-----------------------------------------------------------------
24 
25 REG_MODULE(ParticlePrinter);
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 ParticlePrinterModule::ParticlePrinterModule() : Module()
32 
33 {
34  setDescription("Prints specified variables for all particles in the specified particle list to screen (useful for debugging).\n"
35  "Event-based variables can be printed by not specifying the particle list (empty string).");
37 
38  // Add parameters
39  addParam("listName", m_listName, "name of ParticleList", string(""));
40  addParam("fullPrint", m_fullPrint, "execute Particle's internal print() function", true);
41  vector<string> defaultVariables;
42  addParam("variables", m_variables, "names of variables to be printed (see Variable::Manager)", defaultVariables);
43 
44 }
45 
47 {
48  if (!m_listName.empty()) {
49  m_plist.isRequired(m_listName);
50 
51  // obtain the input and output particle lists from the decay string
52  bool valid = m_decaydescriptor.init(m_listName);
53  if (!valid)
54  B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString: " << m_listName);
55 
56  int nProducts = m_decaydescriptor.getNDaughters();
57  if (nProducts > 0)
58  B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString " << m_listName
59  << ". DecayString should not contain any daughters, only the mother particle.");
60  }
61 }
62 
64 {
65  bool includingVars = !(m_variables.empty());
66 
67  // print event based variables (particle list is empty)
68  if (m_listName.empty() && includingVars) {
69  printVariables(nullptr);
70  return;
71  }
72 
73  // Print variables for all particles in the list
74  if (!m_plist) {
75  B2ERROR("ParticleList " << m_listName << " not found");
76  return;
77  }
78 
79  if (!m_listName.empty() && m_plist->getListSize() == 0) return;
80 
81  B2INFO("[ParticlePrinterModule] START ------------------------------");
82 
83  m_plist->print();
84 
85  for (unsigned i = 0; i < m_plist->getListSize(); i++) {
86  const Particle* particle = m_plist->getParticle(i);
87  if (m_fullPrint) {
88  particle->print();
89  }
90  if (includingVars) {
91  B2INFO(" - " << particle->getArrayIndex() << " = " << particle->getPDGCode() << "[" << i << "]");
92  if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite) {
93  std::string s;
94  for (int idx : particle->getDaughterIndices())
95  s += " " + std::to_string(idx);
96  B2INFO(" o) daughter indices =" << s);
97  }
98  printVariables(particle);
99  }
100  }
101  B2INFO("[ParticlePrinterModule] END ------------------------------");
102 }
103 
104 
106 {
108 
109  for (auto& varName : m_variables) {
110  auto var = manager.getVariable(varName);
111  if (var == nullptr) {
112  B2ERROR("ParticlePrinter: Variable::Manager doesn't have variable" << varName);
113  } else {
114  Variable::Manager::VarVariant result = var->function(particle);
115  if (std::holds_alternative<double>(result)) {
116  double value = std::get<double>(result);
117  B2INFO(" o) " << varName << " = " << value);
118  } else if (std::holds_alternative<int>(result)) {
119  int value = std::get<int>(result);
120  B2INFO(" o) " << varName << " = " << value);
121  } else if (std::holds_alternative<bool>(result)) {
122  bool value = std::get<bool>(result);
123  B2INFO(" o) " << varName << " = " << value);
124  }
125  }
126  }
127 }
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
int getNDaughters() const
return number of direct daughters.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
std::vector< std::string > m_variables
names of variables to be printed
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
std::string m_listName
particle list name
DecayDescriptor m_decaydescriptor
Decay descriptor of the particle being selected.
StoreObjPtr< ParticleList > m_plist
particle list
void printVariables(const Particle *particle) const
Prints out the values of variables specified by the user via the Variables module parameter.
Class to store reconstructed particles.
Definition: Particle.h:75
Global list of available variables.
Definition: Manager.h:101
std::variant< double, int, bool > VarVariant
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:111
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
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.