Belle II Software  release-06-02-00
InclusiveVariables.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/variables/InclusiveVariables.h>
11 #include <analysis/VariableManager/Manager.h>
12 
13 #include <framework/gearbox/Const.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/utilities/Conversion.h>
16 
17 using namespace std;
18 
19 namespace Belle2 {
24  namespace Variable {
25 
26  double nDaughterPhotons(const Particle* particle)
27  {
28  int result = 0;
29  auto fspDaughters = particle->getFinalStateDaughters();
30  for (auto* daughter : fspDaughters) {
31  if (abs(daughter->getPDGCode()) == Const::photon.getPDGCode()) {
32  result++;
33  }
34  }
35  return result;
36  }
37 
38  double nDaughterNeutralHadrons(const Particle* particle)
39  {
40  int result = 0;
41  auto fspDaughters = particle->getFinalStateDaughters();
42  for (auto* daughter : fspDaughters) {
43  if (abs(daughter->getPDGCode()) == Const::neutron.getPDGCode()
44  or abs(daughter->getPDGCode()) == Const::Klong.getPDGCode()) {
45  result++;
46  }
47  }
48  return result;
49  }
50 
51  Manager::FunctionPtr nDaughterCharged(const std::vector<std::string>& arguments)
52  {
53 
54  int pdgCode = 0;
55  if (arguments.size() == 1) {
56  try {
57  pdgCode = Belle2::convertString<int>(arguments[0]);
58  } catch (std::invalid_argument&) {
59  B2ERROR("If an argument is provided to the meta variable nDaughterCharged it has to be an integer!");
60  return nullptr;
61  }
62  }
63  auto func = [pdgCode](const Particle * particle) -> double {
64  int result = 0;
65  auto fspDaughters = particle->getFinalStateDaughters();
66  for (auto* daughter : fspDaughters)
67  {
68  if (pdgCode != 0) {
69  if (abs(daughter->getPDGCode()) == pdgCode) {
70  result++;
71  }
72  } else if (abs(daughter->getCharge()) > 0) {
73  result++;
74  }
75  }
76  return result;
77  };
78  return func;
79  }
80 
81  double nCompositeDaughters(const Particle* particle)
82  {
83  int result = 0;
84  auto fspDaughters = particle->getDaughters();
85  for (auto* daughter : fspDaughters) {
86  if (daughter->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
87  daughter->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
88  result++;
89  }
90  }
91  return result;
92  }
93 
94  Manager::FunctionPtr daughterAverageOf(const std::vector<std::string>& arguments)
95  {
96  if (arguments.size() == 1) {
97  const Variable::Manager::Var* var = Manager::Instance().getVariable(arguments[0]);
98  auto func = [var](const Particle * particle) -> double {
99  double sum = 0.0;
100  if (particle->getNDaughters() == 0)
101  {
102  return std::numeric_limits<double>::quiet_NaN();
103  }
104  for (unsigned j = 0; j < particle->getNDaughters(); ++j)
105  {
106  sum += var->function(particle->getDaughter(j));
107  }
108  return sum / particle->getNDaughters();
109  };
110  return func;
111  } else {
112  B2FATAL("The meta variable daughterAverageOf requires only one argument!");
113  }
114  }
115 
116 
117 
118  // ---
119 
120  VARIABLE_GROUP("For fully-inclusive particles");
121 
122  REGISTER_VARIABLE("nDaughterPhotons", nDaughterPhotons,
123  "Returns the number of final state daughter photons.");
124  REGISTER_VARIABLE("nDaughterNeutralHadrons", nDaughterNeutralHadrons,
125  "Returns the number of K_L0 or neutrons among the final state daughters.");
126  REGISTER_VARIABLE("nDaughterCharged(pdg)", nDaughterCharged,
127  "Returns the number of charged daughters with the provided PDG code or the number "
128  "of all charged daughters if no argument has been provided.");
129  REGISTER_VARIABLE("nCompositeDaughters", nCompositeDaughters,
130  "Returns the number of final state composite daughters.");
131  REGISTER_VARIABLE("daughterAverageOf(variable)", daughterAverageOf,
132  "Returns the mean value of a variable over all daughters.")
133  }
135 }
Abstract base class for different kinds of events.