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