Belle II Software  release-05-02-19
ParticlePrinterModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric, Anze Zupanc *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <analysis/modules/ParticlePrinter/ParticlePrinterModule.h>
13 
14 // framework - DataStore
15 #include <framework/datastore/StoreObjPtr.h>
16 
17 // framework aux
18 #include <framework/logging/Logger.h>
19 
20 // dataobjects
21 #include <analysis/dataobjects/ParticleList.h>
22 #include <analysis/VariableManager/Manager.h>
23 
24 using namespace std;
25 
26 namespace Belle2 {
32  //-----------------------------------------------------------------
33  // Register module
34  //-----------------------------------------------------------------
35 
36  REG_MODULE(ParticlePrinter)
37 
38  //-----------------------------------------------------------------
39  // Implementation
40  //-----------------------------------------------------------------
41 
43 
44  {
45  setDescription("Prints specified variables for all particles in the specified particle list to screen (useful for debugging).\n"
46  "Event-based variables can be printed by not specifying the particle list (empty string).");
47  setPropertyFlags(c_ParallelProcessingCertified);
48 
49  // Add parameters
50  addParam("listName", m_listName, "name of ParticleList", string(""));
51  addParam("fullPrint", m_fullPrint, "execute Particle's internal print() function", true);
52  vector<string> defaultVariables;
53  addParam("variables", m_variables, "names of variables to be printed (see Variable::Manager)", defaultVariables);
54 
55  }
56 
57  void ParticlePrinterModule::initialize()
58  {
59  if (!m_listName.empty()) {
60  StoreObjPtr<ParticleList> plist(m_listName);
61  plist.isRequired();
62 
63  // obtain the input and output particle lists from the decay string
64  bool valid = m_decaydescriptor.init(m_listName);
65  if (!valid)
66  B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString: " << m_listName);
67 
68  int nProducts = m_decaydescriptor.getNDaughters();
69  if (nProducts > 0)
70  B2ERROR("ParticlePrinterModule::initialize Invalid input DecayString " << m_listName
71  << ". DecayString should not contain any daughters, only the mother particle.");
72  }
73  }
74 
75  void ParticlePrinterModule::event()
76  {
77  bool includingVars = !(m_variables.empty());
78 
79  // print event based variables (particle list is empty)
80  if (m_listName.empty() && includingVars) {
81  printVariables(nullptr);
82  return;
83  }
84 
85  // Print variables for all particles in the list
86  StoreObjPtr<ParticleList> plist(m_listName);
87  if (!plist) {
88  B2ERROR("ParticleList " << m_listName << " not found");
89  return;
90  }
91 
92  if (!m_listName.empty() && plist->getListSize() == 0) return;
93 
94  B2INFO("[ParticlePrinterModule] START ------------------------------");
95 
96  plist->print();
97 
98  for (unsigned i = 0; i < plist->getListSize(); i++) {
99  const Particle* particle = plist->getParticle(i);
100  if (m_fullPrint) {
101  particle->print();
102  }
103  if (includingVars) {
104  B2INFO(" - " << particle->getArrayIndex() << " = " << particle->getPDGCode() << "[" << i << "]");
105  if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite) {
106  std::string s;
107  for (int idx : particle->getDaughterIndices())
108  s += " " + std::to_string(idx);
109  B2INFO(" o) daughter indices =" << s);
110  }
111  printVariables(particle);
112  }
113  }
114  B2INFO("[ParticlePrinterModule] END ------------------------------");
115  }
116 
117 
118  void ParticlePrinterModule::printVariables(const Particle* particle) const
119  {
120  Variable::Manager& manager = Variable::Manager::Instance();
121 
122  for (auto& varName : m_variables) {
123  auto var = manager.getVariable(varName);
124  if (var == nullptr) {
125  B2ERROR("ParticlePrinter: Variable::Manager doesn't have variable" << varName);
126  } else {
127  double value = var->function(particle);
128  B2INFO(" o) " << varName << " = " << value);
129  }
130  }
131  }
132 
134 } // end Belle2 namespace
135 
Belle2::ParticlePrinterModule
prints particle list to screen
Definition: ParticlePrinterModule.h:39
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
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::Variable::Manager
Global list of available variables.
Definition: Manager.h:108