10#include <analysis/variables/FEIVariables.h>
13#include <analysis/VariableManager/Manager.h>
15#include <analysis/dataobjects/Particle.h>
16#include <analysis/dataobjects/ParticleList.h>
17#include <analysis/utility/PCmsLabTransform.h>
18#include <framework/gearbox/Const.h>
19#include <mdst/dataobjects/MCParticle.h>
20#include <framework/datastore/StoreArray.h>
21#include <framework/datastore/StoreObjPtr.h>
33 int mostcommonBTagIndex(
const Particle* part)
35 std::map <int, int> tag_candidates;
36 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
37 for (
const Particle* fsp : fsp_tag) {
38 const MCParticle* mc_fsp = fsp->getMCParticle();
40 int tag_index = finddescendant(mc_fsp);
41 if (tag_candidates.find(tag_index) == tag_candidates.end()) {
42 tag_candidates[tag_index] = 1;
44 tag_candidates[tag_index]++;
51 for (
const auto& [key, value] : tag_candidates) {
59 int finddescendant(
const MCParticle* mcpart)
61 const MCParticle* i_mcpart = mcpart;
63 auto* i_mcpart_mother = i_mcpart->getMother();
64 if (i_mcpart_mother) {
65 constexpr std::array<int, 2> B_PDG = {511, 521};
66 auto result = std::find(B_PDG.begin(), B_PDG.end(), abs(i_mcpart_mother->getPDG()));
67 if (result != B_PDG.end()) {
68 return i_mcpart_mother->getArrayIndex();
70 i_mcpart = i_mcpart_mother;
77 std::vector<int> truthFSPTag(
int BTag_index)
79 StoreArray<MCParticle> MC_Particle_list;
80 std::vector<int> fsp_truth_index;
81 for (
const MCParticle& iMCParticle : MC_Particle_list) {
85 fsp_truth_index.push_back(iMCParticle.getArrayIndex());
88 return fsp_truth_index;
90 double percentageMissingParticlesBTag(
const Particle* part)
92 int index = mostcommonBTagIndex(part);
96 std::vector<int> fsp_FEI_index;
97 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
98 for (
const Particle* fsp : fsp_tag) {
99 const MCParticle* mc_fsp = fsp->getMCParticle();
101 int tag_index = finddescendant(mc_fsp);
102 if (tag_index == index) {
103 fsp_FEI_index.push_back(mc_fsp->getArrayIndex());
107 std::vector<int> diff;
108 std::vector<int> fsp_truth_index = truthFSPTag(index);
109 std::sort(fsp_truth_index.begin(), fsp_truth_index.end());
110 std::sort(fsp_FEI_index.begin(), fsp_FEI_index.end());
111 std::set_difference(fsp_truth_index.begin(), fsp_truth_index.end(), fsp_FEI_index.begin(), fsp_FEI_index.end(), std::inserter(diff,
113 return double(diff.size()) / double(fsp_truth_index.size());
116 double percentageWrongParticlesBTag(
const Particle* part)
118 int index = mostcommonBTagIndex(part);
122 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
124 for (
const Particle* fsp : fsp_tag) {
125 const MCParticle* mc_fsp = fsp->getMCParticle();
127 int tag_index = finddescendant(mc_fsp);
128 if (tag_index != index) {
133 return double(wrong_FEI) / double(truthFSPTag(index).size());
136 double mostcommonBTagDeltaP(
const Particle* part)
138 int idx = mostcommonBTagIndex(part);
141 StoreArray<MCParticle> mcParticles;
143 ROOT::Math::XYZVector recoP3 = (T.rotateLabToCms() * part->get4Vector()).Vect();
144 ROOT::Math::XYZVector genP3 = (T.rotateLabToCms() * mcParticles[idx]->get4Vector()).Vect();
145 return (recoP3 - genP3).R();
148 double mostcommonBTagPDG(
const Particle* part)
150 int idx = mostcommonBTagIndex(part);
153 StoreArray<MCParticle> mcParticles;
154 return mcParticles[idx]->getPDG();
159 if (arguments.size() != 2) {
160 B2FATAL(
"sigProbRank requires exactly 2 arguments: bp_list and b0_list. "
161 "Received " << arguments.size() <<
" arguments.");
164 std::string bpListName = arguments[0];
165 std::string b0ListName = arguments[1];
167 auto func = [bpListName, b0ListName](
const Particle * particle) ->
int {
168 StoreObjPtr<ParticleList> bpList(bpListName);
169 StoreObjPtr<ParticleList> b0List(b0ListName);
171 if (!bpList.isValid() && !b0List.isValid())
172 B2FATAL(
"sigProbRank could not find either input list: bp_list='" << bpListName
173 <<
"', b0_list='" << b0ListName <<
"'");
174 if (!bpList.isValid())
175 B2FATAL(
"sigProbRank could not find the required bp_list: '" << bpListName <<
"'");
176 if (!b0List.isValid())
177 B2FATAL(
"sigProbRank could not find the required b0_list: '" << b0ListName <<
"'");
179 struct RankedCandidate {
180 double signalProbability;
181 const Particle* particle;
184 auto collectCandidates = [](
const StoreObjPtr<ParticleList>& list,
185 const std::string & listLabel,
186 std::vector<RankedCandidate>& rankedCandidates)
188 for (
unsigned int i = 0; i < list->getListSize(); ++i) {
189 const Particle* p = list->getParticle(i);
190 if (!p->hasExtraInfo(
"SignalProbability")) {
191 B2FATAL(
"sigProbRank requires every candidate in " << listLabel
192 <<
" to provide extraInfo('SignalProbability')");
194 rankedCandidates.push_back({p->getExtraInfo(
"SignalProbability"), p});
198 std::vector<RankedCandidate> rankedCandidates;
199 rankedCandidates.reserve(bpList->getListSize() + b0List->getListSize());
200 collectCandidates(bpList,
"bp_list", rankedCandidates);
201 collectCandidates(b0List,
"b0_list", rankedCandidates);
203 std::stable_sort(rankedCandidates.begin(), rankedCandidates.end(),
204 [](
const RankedCandidate & lhs,
const RankedCandidate & rhs)
206 return lhs.signalProbability > rhs.signalProbability;
209 for (
unsigned int i = 0; i < rankedCandidates.size(); ++i)
211 if (rankedCandidates[i].particle == particle) {
212 return static_cast<int>(i) + 1;
216 B2FATAL(
"sigProbRank: particle not found in either input list: "
217 "bp_list='" << bpListName <<
"', b0_list='" << b0ListName <<
"'");
223 VARIABLE_GROUP(
"FEIVariables");
224 REGISTER_VARIABLE(
"mostcommonBTagIndex", mostcommonBTagIndex,
225 "By giving e.g. a FEI B meson candidate the B meson index on generator level is determined, where most reconstructed particles can be assigned to. If no B meson found on generator level -1 is returned.");
226 REGISTER_VARIABLE(
"percentageMissingParticlesBTag", percentageMissingParticlesBTag,
227 "Get the percentage of missing particles by using the mostcommonBTagIndex. So the number of particles not reconstructed by e.g. the FEI are determined and divided by the number of generated particles using the given index of the B meson. If no B meson found on generator level -1 is returned.");
228 REGISTER_VARIABLE(
"percentageWrongParticlesBTag", percentageWrongParticlesBTag,
229 "Get the percentage of wrong particles by using the mostcommonBTagIndex. In this context wrong means that the reconstructed particles originated from the other B meson. The absolute number is divided by the total number of generated FSP from the given B meson index. If no B meson found on generator level -1 is returned.");
230 REGISTER_VARIABLE(
"mostcommonBTagDeltaP", mostcommonBTagDeltaP,
231 "Returns the magnitude of the 3-momentum difference in CMS frame between the "
232 "reconstructed particle and the generated B meson identified by mostcommonBTagIndex. "
233 "Returns NaN if no B meson is found on generator level.",
"GeV/c");
234 REGISTER_VARIABLE(
"mostcommonBTagPDG", mostcommonBTagPDG,
235 "Returns the PDG code of the generated B meson identified by mostcommonBTagIndex. "
236 "Returns NaN if no B meson is found on generator level. "
237 "Note: this is equivalent to ``genParticle(mostcommonBTagIndex, PDG)``. "
238 "Other variables can be accessed the same way by replacing ``PDG`` with any variable.");
239 REGISTER_METAVARIABLE(
"sigProbRank(bp_list, b0_list)", sigProbRank,
240 "Returns the rank (starting at 1) of the candidate when all candidates from the B+ list (bp_list) "
241 "and B0 list (b0_list) are ordered together by descending ``extraInfo(SignalProbability)``.", Manager::VariableDataType::c_int);
static const double doubleNaN
quiet_NaN
@ c_IsFSRPhoton
bit 7: Particle is from final state radiation
@ c_IsPHOTOSPhoton
bit 8: Particle is an radiative photon from PHOTOS
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
@ c_IsISRPhoton
bit 6: Particle is from initial state radiation
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
Abstract base class for different kinds of events.