Belle II Software development
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/dataobjects/Particle.h>
17
18#include <analysis/VariableManager/Manager.h>
19
20using namespace std;
21using namespace Belle2;
22
23//-----------------------------------------------------------------
24// Register module
25//-----------------------------------------------------------------
26
27REG_MODULE(ParticlePrinter);
28
29//-----------------------------------------------------------------
30// Implementation
31//-----------------------------------------------------------------
32
34
35{
36 setDescription("Prints specified variables for all particles in the specified particle list to screen (useful for debugging).\n"
37 "Event-based variables can be printed by not specifying the particle list (empty string).");
39
40 // Add parameters
41 addParam("listName", m_listName, "name of ParticleList", string(""));
42 addParam("fullPrint", m_fullPrint, "execute Particle's internal print() function", true);
43 vector<string> defaultVariables;
44 addParam("variables", m_variables, "names of variables to be printed (see Variable::Manager)", defaultVariables);
45
46}
47
49{
50 if (!m_listName.empty()) {
51 m_plist.isRequired(m_listName);
52
53 // obtain the input and output particle lists from the decay string
54 bool valid = m_decaydescriptor.init(m_listName);
55 if (!valid)
56 B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString: " << m_listName);
57
58 int nProducts = m_decaydescriptor.getNDaughters();
59 if (nProducts > 0)
60 B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString " << m_listName
61 << ". DecayString should not contain any daughters, only the mother particle.");
62 }
63}
64
66{
67 bool includingVars = !(m_variables.empty());
68
69 // print event based variables (particle list is empty)
70 if (m_listName.empty() && includingVars) {
71 printVariables(nullptr);
72 return;
73 }
74
75 // Print variables for all particles in the list
76 if (!m_plist) {
77 B2ERROR("ParticleList " << m_listName << " not found");
78 return;
79 }
80
81 if (!m_listName.empty() && m_plist->getListSize() == 0) return;
82
83 B2INFO("[ParticlePrinterModule] START ------------------------------");
84
85 m_plist->print();
86
87 for (unsigned i = 0; i < m_plist->getListSize(); i++) {
88 const Particle* particle = m_plist->getParticle(i);
89 if (m_fullPrint) {
90 particle->print();
91 }
92 if (includingVars) {
93 B2INFO(" - " << particle->getArrayIndex() << " = " << particle->getPDGCode() << "[" << i << "]");
94 if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite) {
95 std::string s;
96 for (int idx : particle->getDaughterIndices())
97 s += " " + std::to_string(idx);
98 B2INFO(" o) daughter indices =" << s);
99 }
100 printVariables(particle);
101 }
102 }
103 B2INFO("[ParticlePrinterModule] END ------------------------------");
104}
105
106
108{
110
111 for (auto& varName : m_variables) {
112 auto var = manager.getVariable(varName);
113 if (var == nullptr) {
114 B2ERROR("ParticlePrinter: Variable::Manager doesn't have variable" << varName);
115 } else {
116 Variable::Manager::VarVariant result = var->function(particle);
117 if (std::holds_alternative<double>(result)) {
118 double value = std::get<double>(result);
119 B2INFO(" o) " << varName << " = " << value);
120 } else if (std::holds_alternative<int>(result)) {
121 int value = std::get<int>(result);
122 B2INFO(" o) " << varName << " = " << value);
123 } else if (std::holds_alternative<bool>(result)) {
124 bool value = std::get<bool>(result);
125 B2INFO(" o) " << varName << " = " << value);
126 }
127 }
128 }
129}
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
Module()
Constructor.
Definition Module.cc:30
@ 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:76
Global list of available variables.
Definition Manager.h:100
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:110
static Manager & Instance()
get singleton instance.
Definition Manager.cc:26
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.