Belle II Software development
FlavorTaggerInfoFillerModule.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#include <analysis/modules/FlavorTaggerInfoFiller/FlavorTaggerInfoFillerModule.h>
10#include <framework/core/ModuleParam.templateDetails.h>
11#include <analysis/dataobjects/ParticleList.h>
12#include <analysis/dataobjects/FlavorTaggerInfo.h>
13#include <analysis/VariableManager/Manager.h>
14
15using namespace std;
16using namespace Belle2;
17
18// Register module in the framework
19REG_MODULE(FlavorTaggerInfoFiller);
20
22{
23 //Set module properties
24 setDescription("Creates a new flavorTaggerInfoMap DataObject for the specific methods. Saves there all the relevant information of the flavorTagger.");
26 //Parameter definition
27 addParam("trackLevelParticleLists", m_trackLevelParticleLists, "Used Flavor Tagger trackLevel Categories of the lists ",
28 vector<tuple<string, string>>());
29 addParam("eventLevelParticleLists", m_eventLevelParticleLists, "Used Flavor Tagger eventLevel Categories of the lists ",
30 vector<tuple<string, string, string>>());
31 addParam("FANNmlp", m_FANNmlp, "Sets if FANN Combiner output will be saved or not", false);
32 addParam("TMVAfbdt", m_TMVAfbdt, "Sets if FANN Combiner output will be saved or not", false);
33 addParam("DNNmlp", m_DNNmlp, "Sets if DNN Tagger output will be saved or not", false);
34 addParam("qpCategories", m_qpCategories, "Sets if individual categories output will be saved or not", false);
35 addParam("istrueCategories", m_istrueCategories, "Sets if individual MC truth for each category is saved or not", false);
36 addParam("targetProb", m_targetProb, "Sets if individual Categories output will be saved or not", false);
37 addParam("trackPointers", m_trackPointers, "Sets if track pointers to target tracks are saved or not", false);
38
39}
40
42{
43 if (m_FANNmlp or m_TMVAfbdt) m_eventExtraInfo.isRequired();
44}
45
47{
48 auto* flavorTaggerInfo = m_roe->getRelatedTo<FlavorTaggerInfo>();
49
51
52
53 if (flavorTaggerInfo == nullptr) {
54 B2ERROR("flavorTaggerInfoFiller: FlavorTaggerInfo does not exist");
55 return;
56 }
57
58 flavorTaggerInfo -> setUseModeFlavorTagger("Expert");
59
60 if (m_FANNmlp) {
61 FlavorTaggerInfoMap* infoMapsFANN = flavorTaggerInfo -> getMethodMap("FANN");
62 // For FANN, the output is mapped to be qr
63 float qrCombined = m_eventExtraInfo->getExtraInfo("qrCombinedFANN");
64 if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
65 if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
66 float B0Probability = qrCombined / 2 + 0.5;
67 float B0barProbability = 1 - B0Probability;
68 infoMapsFANN->setQrCombined(qrCombined);
69 infoMapsFANN->setB0Probability(B0Probability);
70 infoMapsFANN->setB0barProbability(B0barProbability);
71 }
72
73 FlavorTaggerInfoMap* infoMapsFBDT = flavorTaggerInfo -> getMethodMap("FBDT");
74
75 if (m_TMVAfbdt) {
76 float B0Probability = m_eventExtraInfo->getExtraInfo("qrCombinedFBDT");
77 float B0barProbability = 1 - B0Probability;
78 float qrCombined = 2 * (B0Probability - 0.5);
79 if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
80 if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
81 infoMapsFBDT->setQrCombined(qrCombined);
82 infoMapsFBDT->setB0Probability(B0Probability);
83 infoMapsFBDT->setB0barProbability(B0barProbability);
84 }
85
86 if (m_DNNmlp) {
87 FlavorTaggerInfoMap* infoMapsDNN = flavorTaggerInfo -> getMethodMap("DNN");
88 const Particle* particle = m_roe->getRelatedFrom<Particle>();
89 float B0Probability = particle->getExtraInfo("dnn_output");
90 float B0barProbability = 1 - B0Probability;
91 float qrCombined = 2 * (B0Probability - 0.5);
92 if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
93 if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
94 infoMapsDNN->setQrCombined(qrCombined);
95 infoMapsDNN->setB0Probability(B0Probability);
96 infoMapsDNN->setB0barProbability(B0barProbability);
97 }
98
99
100 if (m_targetProb) {
101 for (auto& iTuple : m_trackLevelParticleLists) {
102 string particleListName = get<0>(iTuple);
103 string category = get<1>(iTuple);
104 StoreObjPtr<ParticleList> particleList(particleListName);
105
106
107 if (!particleList.isValid()) {
108 B2INFO("ParticleList " << particleListName << " not found");
109 } else {
110 if (particleList -> getListSize() == 0) {
111 infoMapsFBDT -> setProbTrackLevel(category, 0);
112 if (m_trackPointers) infoMapsFBDT -> setTargetTrackLevel(category, nullptr);
113 } else {
114
115 for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
116 Particle* iParticle = particleList ->getParticle(i);
117 bool hasMaxProb = std::get<bool>(manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightTrack(" + category +
118 "))")->function(iParticle));
119 if (hasMaxProb == 1) {
120 float targetProb = iParticle -> getExtraInfo("isRightTrack(" + category + ")");
121 infoMapsFBDT->setProbTrackLevel(category, targetProb);
122 if (m_trackPointers) {
123 infoMapsFBDT-> setTargetTrackLevel(category, iParticle -> getTrack());
124 }
125 break;
126 }
127 }
128 }
129 }
130 }
131 }
132
133 if (m_qpCategories) {
134
135 for (auto& iTuple : m_eventLevelParticleLists) {
136 string particleListName = get<0>(iTuple);
137 string category = get<1>(iTuple);
138 string qpCategoryVariable = get<2>(iTuple);
139 StoreObjPtr<ParticleList> particleList(particleListName);
140
141 if (!particleList.isValid()) {
142 B2INFO("ParticleList " << particleListName << " not found");
143 } else {
144 if (particleList -> getListSize() == 0) {
145 infoMapsFBDT -> setProbEventLevel(category, 0);
146 infoMapsFBDT -> setQpCategory(category, 0);
148 infoMapsFBDT -> setHasTrueTarget(category, 0);
149 infoMapsFBDT -> setIsTrueCategory(category, 0);
150 }
151 if (m_trackPointers) infoMapsFBDT -> setTargetEventLevel(category, nullptr);
152 } else {
153
154 for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
155 Particle* iParticle = particleList ->getParticle(i);
156 bool hasMaxProb = std::get<bool>(manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightCategory(" + category
157 + "))")-> function(iParticle));
158 if (hasMaxProb == 1) {
159 float categoryProb = iParticle -> getExtraInfo("isRightCategory(" + category + ")");
160 float qpCategory = std::get<double>(manager.getVariable(qpCategoryVariable)-> function(iParticle));
161 infoMapsFBDT->setProbEventLevel(category, categoryProb);
162 infoMapsFBDT -> setQpCategory(category, qpCategory);
164 float isTrueTarget = std::get<double>(manager.getVariable("hasTrueTarget(" + category + ")")-> function(nullptr));
165 infoMapsFBDT -> setHasTrueTarget(category, isTrueTarget);
166 float isTrueCategory = std::get<double>(manager.getVariable("isTrueCategory(" + category + ")")-> function(nullptr));
167 infoMapsFBDT -> setIsTrueCategory(category, isTrueCategory);
168 }
169 if (m_trackPointers) {
170 infoMapsFBDT-> setTargetEventLevel(category, iParticle -> getTrack());
171 }
172 break;
173 }
174 }
175 }
176 }
177 }
178 }
179
180}
181
183{
184}
185
std::vector< std::tuple< std::string, std::string > > m_trackLevelParticleLists
Used Flavor Tagger trackLevel Categories of the lists.
StoreObjPtr< RestOfEvent > m_roe
ROE object pointer.
virtual void initialize() override
Initialises the module.
virtual void event() override
Method called for each event.
bool m_targetProb
Sets if individual Categories output will be saved or not.
StoreArray< MCParticle > m_mcparticles
StoreArray of MCParticles.
virtual void terminate() override
Write TTree to file, and close file if necessary.
bool m_TMVAfbdt
Sets if FastBDT Combiner output will be saved or not.
bool m_FANNmlp
Sets if FANN Combiner output will be saved or not.
bool m_DNNmlp
Sets if DNN tagger output will be saved or not.
bool m_qpCategories
Sets if individual Categories output will be saved or not.
bool m_istrueCategories
Sets if individual MC thruth for each Category is saved or not.
StoreObjPtr< EventExtraInfo > m_eventExtraInfo
event extra info object pointer
std::vector< std::tuple< std::string, std::string, std::string > > m_eventLevelParticleLists
Used Flavor Tagger eventLevel Categories of the lists.
bool m_trackPointers
Sets if track pointers to target tracks are saved or not.
This class stores the Flavor Tagger information for a specific method and particle filled in the Flav...
void setProbEventLevel(const std::string &category, float probability)
Map filler: Set the category name and the highest category probability for the corresponding category...
void setProbTrackLevel(const std::string &category, float probability)
Map filler: Set the category name and the corresponding highest target track probability.
void setB0Probability(float B0Probability)
Saves the B0Probability output of the Combiner.
void setB0barProbability(float B0barProbability)
Saves the B0barProbability output of the Combiner.
void setQrCombined(float qr)
Saves qr Output of the Combiner.
This class stores the relevant information for the TagV vertex fit, extracted mainly from the Flavor ...
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
Class to store reconstructed particles.
Definition: Particle.h:76
bool isValid() const
Check whether the array was registered.
Definition: StoreArray.h:288
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Global list of available variables.
Definition: Manager.h:100
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:26
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:649
Abstract base class for different kinds of events.
STL namespace.