Belle II Software development
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 header.
10#include <analysis/variables/InclusiveVariables.h>
11
12#include <analysis/dataobjects/Particle.h>
13
14#include <framework/gearbox/Const.h>
15#include <framework/logging/Logger.h>
16
17using namespace std;
18
19namespace Belle2 {
24 namespace Variable {
25
26 int 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 int 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 int nDaughterCharged(const Particle* particle, const std::vector<double>& argument)
52 {
53 int absPDGCode = 0;
54 if (argument.size() == 1) {
55 absPDGCode = abs(std::lround(argument[0]));
56 }
57
58 int result = 0;
59 auto fspDaughters = particle->getFinalStateDaughters();
60 for (auto* daughter : fspDaughters) {
61 if (absPDGCode != 0) {
62 if (abs(daughter->getPDGCode()) == absPDGCode) {
63 result++;
64 }
65 } else if (abs(daughter->getCharge()) > 0) {
66 result++;
67 }
68 }
69 return result;
70 }
71
72 int nCompositeDaughters(const Particle* particle, const std::vector<double>& argument)
73 {
74 int absPDGCode = 0;
75 if (argument.size() == 1) {
76 absPDGCode = abs(std::lround(argument[0]));
77 }
78
79 int result = 0;
80 auto primaryDaughters = particle->getDaughters();
81 for (auto* daughter : primaryDaughters) {
82 if (daughter->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
83 daughter->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
84 if (absPDGCode != 0) {
85 if (abs(daughter->getPDGCode()) == absPDGCode) {
86 result++;
87 }
88 } else {
89 result++;
90 }
91 }
92 }
93 return result;
94 }
95
96 int nCompositeAllGenerationDaughters(const Particle* particle, const std::vector<double>& argument)
97 {
98 int absPDGCode = 0;
99 if (argument.size() == 1) {
100 absPDGCode = abs(std::lround(argument[0]));
101 }
102
103 int result = 0;
104 auto allDaughters = particle->getAllDaughters();
105 for (auto* daughter : allDaughters) {
106 if (daughter->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
107 daughter->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
108 if (absPDGCode != 0) {
109 if (abs(daughter->getPDGCode()) == absPDGCode) {
110 result++;
111 }
112 } else {
113 result++;
114 }
115 }
116 }
117 return result;
118 }
119
120 Manager::FunctionPtr daughterAverageOf(const std::vector<std::string>& arguments)
121 {
122 if (arguments.size() == 1) {
123 const Variable::Manager::Var* var = Manager::Instance().getVariable(arguments[0]);
124 auto func = [var](const Particle * particle) -> double {
125 double sum = 0.0;
126 if (particle->getNDaughters() == 0)
127 {
128 return Const::doubleNaN;
129 }
130 if (std::holds_alternative<double>(var->function(particle->getDaughter(0))))
131 {
132 for (unsigned j = 0; j < particle->getNDaughters(); ++j) {
133 sum += std::get<double>(var->function(particle->getDaughter(j)));
134 }
135 } else if (std::holds_alternative<int>(var->function(particle->getDaughter(0))))
136 {
137 for (unsigned j = 0; j < particle->getNDaughters(); ++j) {
138 sum += std::get<int>(var->function(particle->getDaughter(j)));
139 }
140 }
141 return sum / particle->getNDaughters();
142 };
143 return func;
144 } else {
145 B2FATAL("The meta variable daughterAverageOf requires only one argument!");
146 }
147 }
148
149 // ---
150
151 VARIABLE_GROUP("For fully-inclusive particles");
152
153 REGISTER_VARIABLE("nDaughterPhotons", nDaughterPhotons,
154 "Returns the number of final state daughter photons.");
155 REGISTER_VARIABLE("nDaughterNeutralHadrons", nDaughterNeutralHadrons,
156 "Returns the number of K_L0 or neutrons among the final state daughters.");
157 REGISTER_VARIABLE("nDaughterCharged(pdg)", nDaughterCharged,
158 "Returns the number of charged daughters with the provided PDG code or the number "
159 "of all charged daughters if no argument has been provided. "
160 "The variable is flavor agnostic and it returns the sum of the number of particle and anti-particle.");
161 REGISTER_VARIABLE("nCompositeDaughters(pdg)", nCompositeDaughters,
162 "Returns the number of primary composite daughters with the provided PDG code or the number"
163 "of all primary composite daughters if no argument has been provided. "
164 "The variable is flavor agnostic and it returns the sum of the number of particle and anti-particle.");
165 REGISTER_VARIABLE("nCompositeAllGenerationDaughters(pdg)", nCompositeAllGenerationDaughters,
166 "Returns the number of all generations' composite daughters with the provided PDG code or the number"
167 "of all generations' composite daughters if no argument has been provided. "
168 "The variable is flavor agnostic and it returns the sum of the number of particle and anti-particle.");
169 REGISTER_METAVARIABLE("daughterAverageOf(variable)", daughterAverageOf,
170 "Returns the mean value of a variable over all daughters.", Manager::VariableDataType::c_double)
171 }
173}
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ParticleType neutron
neutron particle
Definition: Const.h:675
static const ParticleType Klong
K^0_L particle.
Definition: Const.h:678
static const double doubleNaN
quiet_NaN
Definition: Const.h:703
static const ParticleType photon
photon particle
Definition: Const.h:673
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
Definition: Manager.h:112
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:58
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:26
Abstract base class for different kinds of events.
STL namespace.