Belle II Software  release-06-02-00
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 
15 #include <iostream>
16 
17 using namespace std;
18 using namespace Belle2;
19 
20 // Register module in the framework
21 REG_MODULE(FlavorTaggerInfoFiller)
22 
24 {
25  //Set module properties
26  setDescription("Creates a new flavorTaggerInfoMap DataObject for the specific methods. Saves there all the relevant information of the flavorTagger.");
27  setPropertyFlags(c_ParallelProcessingCertified);
28  //Parameter definition
29  addParam("trackLevelParticleLists", m_trackLevelParticleLists, "Used Flavor Tagger trackLevel Categories of the lists ",
30  vector<tuple<string, string>>());
31  addParam("eventLevelParticleLists", m_eventLevelParticleLists, "Used Flavor Tagger eventLevel Categories of the lists ",
32  vector<tuple<string, string, string>>());
33  addParam("FANNmlp", m_FANNmlp, "Sets if FANN Combiner output will be saved or not", false);
34  addParam("TMVAfbdt", m_TMVAfbdt, "Sets if FANN Combiner output will be saved or not", false);
35  addParam("DNNmlp", m_DNNmlp, "Sets if DNN Tagger output will be saved or not", false);
36  addParam("qpCategories", m_qpCategories, "Sets if individual categories output will be saved or not", false);
37  addParam("istrueCategories", m_istrueCategories, "Sets if individual MC truth for each category is saved or not", false);
38  addParam("targetProb", m_targetProb, "Sets if individual Categories output will be saved or not", false);
39  addParam("trackPointers", m_trackPointers, "Sets if track pointers to target tracks are saved or not", false);
40 
41 }
42 
43 void FlavorTaggerInfoFillerModule::initialize()
44 {
45 }
46 
47 void FlavorTaggerInfoFillerModule::event()
48 {
49  auto* flavorTaggerInfo = m_roe->getRelatedTo<FlavorTaggerInfo>();
50 
51  Variable::Manager& manager = Variable::Manager::Instance();
52 
53 
54  if (flavorTaggerInfo == nullptr) {
55  B2ERROR("flavorTaggerInfoFiller: FlavorTaggerInfo does not exist");
56  return;
57  }
58 
59  flavorTaggerInfo -> setUseModeFlavorTagger("Expert");
60 
61  if (m_FANNmlp) {
62  FlavorTaggerInfoMap* infoMapsFANN = flavorTaggerInfo -> getMethodMap("FANN");
63  // For FANN, the output is mapped to be qr
64  float qrCombined = m_eventExtraInfo->getExtraInfo("qrCombinedFANN");
65  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
66  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
67  float B0Probability = qrCombined / 2 + 0.5;
68  float B0barProbability = 1 - B0Probability;
69  infoMapsFANN->setQrCombined(qrCombined);
70  infoMapsFANN->setB0Probability(B0Probability);
71  infoMapsFANN->setB0barProbability(B0barProbability);
72  }
73 
74  FlavorTaggerInfoMap* infoMapsFBDT = flavorTaggerInfo -> getMethodMap("FBDT");
75 
76  if (m_TMVAfbdt) {
77  float B0Probability = m_eventExtraInfo->getExtraInfo("qrCombinedFBDT");
78  float B0barProbability = 1 - B0Probability;
79  float qrCombined = 2 * (B0Probability - 0.5);
80  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
81  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
82  infoMapsFBDT->setQrCombined(qrCombined);
83  infoMapsFBDT->setB0Probability(B0Probability);
84  infoMapsFBDT->setB0barProbability(B0barProbability);
85  }
86 
87  if (m_DNNmlp) {
88  FlavorTaggerInfoMap* infoMapsDNN = flavorTaggerInfo -> getMethodMap("DNN");
89  const Particle* particle = m_roe->getRelated<Particle>();
90  float B0Probability = particle->getExtraInfo("dnn_output");
91  float B0barProbability = 1 - B0Probability;
92  float qrCombined = 2 * (B0Probability - 0.5);
93  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
94  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
95  infoMapsDNN->setQrCombined(qrCombined);
96  infoMapsDNN->setB0Probability(B0Probability);
97  infoMapsDNN->setB0barProbability(B0barProbability);
98  }
99 
100 
101  if (m_targetProb) {
102  for (auto& iTuple : m_trackLevelParticleLists) {
103  string particleListName = get<0>(iTuple);
104  string category = get<1>(iTuple);
105  StoreObjPtr<ParticleList> particleList(particleListName);
106 
107 
108  if (!particleList.isValid()) {
109  B2INFO("ParticleList " << particleListName << " not found");
110  } else {
111  if (particleList -> getListSize() == 0) {
112  infoMapsFBDT -> setProbTrackLevel(category, 0);
113  if (m_trackPointers) infoMapsFBDT -> setTargetTrackLevel(category, nullptr);
114  } else {
115 
116  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
117  Particle* iParticle = particleList ->getParticle(i);
118  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightTrack(" +
119  category + "))")-> function(iParticle);
120  if (hasMaxProb == 1) {
121  float targetProb = iParticle -> getExtraInfo("isRightTrack(" + category + ")");
122  infoMapsFBDT->setProbTrackLevel(category, targetProb);
123  if (m_trackPointers) {
124  infoMapsFBDT-> setTargetTrackLevel(category, iParticle -> getTrack());
125  }
126  break;
127  }
128  }
129  }
130  }
131  }
132  }
133 
134  if (m_qpCategories) {
135 
136  for (auto& iTuple : m_eventLevelParticleLists) {
137  string particleListName = get<0>(iTuple);
138  string category = get<1>(iTuple);
139  string qpCategoryVariable = get<2>(iTuple);
140  StoreObjPtr<ParticleList> particleList(particleListName);
141 
142  if (!particleList.isValid()) {
143  B2INFO("ParticleList " << particleListName << " not found");
144  } else {
145  if (particleList -> getListSize() == 0) {
146  infoMapsFBDT -> setProbEventLevel(category, 0);
147  infoMapsFBDT -> setQpCategory(category, 0);
148  if (m_istrueCategories and m_mcparticles.isValid()) {
149  infoMapsFBDT -> setHasTrueTarget(category, 0);
150  infoMapsFBDT -> setIsTrueCategory(category, 0);
151  }
152  if (m_trackPointers) infoMapsFBDT -> setTargetEventLevel(category, nullptr);
153  } else {
154 
155  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
156  Particle* iParticle = particleList ->getParticle(i);
157  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightCategory(" +
158  category + "))")-> function(iParticle);
159  if (hasMaxProb == 1) {
160  float categoryProb = iParticle -> getExtraInfo("isRightCategory(" + category + ")");
161  float qpCategory = manager.getVariable(qpCategoryVariable)-> function(iParticle);
162  infoMapsFBDT->setProbEventLevel(category, categoryProb);
163  infoMapsFBDT -> setQpCategory(category, qpCategory);
164  if (m_istrueCategories and m_mcparticles.isValid()) {
165  float isTrueTarget = manager.getVariable("hasTrueTarget(" + category + ")")-> function(nullptr);
166  infoMapsFBDT -> setHasTrueTarget(category, isTrueTarget);
167  float isTrueCategory = manager.getVariable("isTrueCategory(" + category + ")")-> function(nullptr);
168  infoMapsFBDT -> setIsTrueCategory(category, isTrueCategory);
169  }
170  if (m_trackPointers) {
171  infoMapsFBDT-> setTargetEventLevel(category, iParticle -> getTrack());
172  }
173  break;
174  }
175  }
176  }
177  }
178  }
179  }
180 
181 }
182 
183 void FlavorTaggerInfoFillerModule::terminate()
184 {
185 }
186 
Creates a new flavorTaggerInfoMap DataObject for the specific methods.
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
Class to store reconstructed particles.
Definition: Particle.h:74
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Global list of available variables.
Definition: Manager.h:98
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.