Belle II Software development
FEIVariables.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/FEIVariables.h>
11
12// include VariableManager
13#include <analysis/VariableManager/Manager.h>
14
15#include <analysis/dataobjects/Particle.h>
16#include <mdst/dataobjects/MCParticle.h>
17#include <framework/datastore/StoreArray.h>
18#include <map>
19#include <algorithm>
20#include <iterator>
21
22namespace Belle2 {
27 namespace Variable {
28
29 int mostcommonBTagIndex(const Particle* part)
30 {
31 std::map <int, int> tag_candidates;
32 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
33 for (const Particle* fsp : fsp_tag) {
34 const MCParticle* mc_fsp = fsp->getMCParticle();
35 if (mc_fsp) {
36 int tag_index = finddescendant(mc_fsp);
37 if (tag_candidates.find(tag_index) == tag_candidates.end()) {
38 tag_candidates[tag_index] = 1;
39 } else {
40 tag_candidates[tag_index]++;
41 }
42 }
43 }
44 int tag_index = -1;
45 int tag_N = 0;
46 // cppcheck-suppress unassignedVariable
47 for (const auto& [key, value] : tag_candidates) {
48 if (value > tag_N) {
49 tag_index = key;
50 tag_N = value;
51 }
52 }
53 return tag_index;
54 }
55 int finddescendant(const MCParticle* mcpart)
56 {
57 const MCParticle* i_mcpart = mcpart;
58 while (i_mcpart) {
59 auto* i_mcpart_mother = i_mcpart->getMother();
60 if (i_mcpart_mother) {
61 std::vector<int> B_PDG = {511, 521};
62 auto result = std::find(std::begin(B_PDG), std::end(B_PDG), abs(i_mcpart_mother->getPDG()));
63 if (result != std::end(B_PDG)) {
64 return i_mcpart_mother->getArrayIndex();
65 }
66 i_mcpart = i_mcpart_mother;
67 } else {
68 return -1;
69 }
70 }
71 return -2;
72 }
73 std::vector<int> truthFSPTag(int BTag_index)
74 {
75 StoreArray<MCParticle> MC_Particle_list;
76 std::vector<int> fsp_truth_index;
77 for (const MCParticle& iMCParticle : MC_Particle_list) {
78 if ((BTag_index == finddescendant(&iMCParticle)) && (iMCParticle.hasStatus(MCParticle::c_StableInGenerator) == true)
79 && (iMCParticle.hasStatus(MCParticle::c_IsISRPhoton) == false) && iMCParticle.hasStatus(MCParticle::c_IsFSRPhoton) == false
80 && (iMCParticle.hasStatus(MCParticle::c_IsPHOTOSPhoton) == false)) {
81 fsp_truth_index.push_back(iMCParticle.getArrayIndex());
82 }
83 }
84 return fsp_truth_index;
85 }
86 double percentageMissingParticlesBTag(const Particle* part)
87 {
88 int index = mostcommonBTagIndex(part);
89 if (index == -1) {
90 return -1;
91 } else {
92 std::vector<int> fsp_FEI_index;
93 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
94 for (const Particle* fsp : fsp_tag) {
95 const MCParticle* mc_fsp = fsp->getMCParticle();
96 if (mc_fsp) {
97 int tag_index = finddescendant(mc_fsp);
98 if (tag_index == index) {
99 fsp_FEI_index.push_back(mc_fsp->getArrayIndex());
100 }
101 }
102 }
103 std::vector<int> diff;
104 std::vector<int> fsp_truth_index = truthFSPTag(index);
105 std::sort(fsp_truth_index.begin(), fsp_truth_index.end());
106 std::sort(fsp_FEI_index.begin(), fsp_FEI_index.end());
107 std::set_difference(fsp_truth_index.begin(), fsp_truth_index.end(), fsp_FEI_index.begin(), fsp_FEI_index.end(), std::inserter(diff,
108 diff.begin()));
109 return double(diff.size()) / double(fsp_truth_index.size());
110 }
111 }
112 double percentageWrongParticlesBTag(const Particle* part)
113 {
114 int index = mostcommonBTagIndex(part);
115 if (index == -1) {
116 return -1;
117 } else {
118 const std::vector<const Particle*>& fsp_tag = part->getFinalStateDaughters();
119 int wrong_FEI = 0;
120 for (const Particle* fsp : fsp_tag) {
121 const MCParticle* mc_fsp = fsp->getMCParticle();
122 if (mc_fsp) {
123 int tag_index = finddescendant(mc_fsp);
124 if (tag_index != index) {
125 wrong_FEI ++;
126 }
127 }
128 }
129 return double(wrong_FEI) / double(truthFSPTag(index).size());
130 }
131 }
132 VARIABLE_GROUP("FEIVariables");
133 REGISTER_VARIABLE("mostcommonBTagIndex", mostcommonBTagIndex,
134 "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.");
135 REGISTER_VARIABLE("percentageMissingParticlesBTag", percentageMissingParticlesBTag,
136 "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.");
137 REGISTER_VARIABLE("percentageWrongParticlesBTag", percentageWrongParticlesBTag,
138 "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.");
139 }
141}
@ c_IsFSRPhoton
bit 7: Particle is from final state radiation
Definition: MCParticle.h:61
@ c_IsPHOTOSPhoton
bit 8: Particle is an radiative photon from PHOTOS
Definition: MCParticle.h:63
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
Definition: MCParticle.h:49
@ c_IsISRPhoton
bit 6: Particle is from initial state radiation
Definition: MCParticle.h:59
Abstract base class for different kinds of events.