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