Belle II Software  release-08-01-10
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 
23 FlavorTaggerInfoFillerModule::FlavorTaggerInfoFillerModule() : Module()
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.");
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 
44 {
45 }
46 
48 {
49  auto* flavorTaggerInfo = m_roe->getRelatedTo<FlavorTaggerInfo>();
50 
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->getRelatedFrom<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  bool hasMaxProb = std::get<bool>(manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightTrack(" + category +
119  "))")->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);
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  bool hasMaxProb = std::get<bool>(manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightCategory(" + category
158  + "))")-> function(iParticle));
159  if (hasMaxProb == 1) {
160  float categoryProb = iParticle -> getExtraInfo("isRightCategory(" + category + ")");
161  float qpCategory = std::get<double>(manager.getVariable(qpCategoryVariable)-> function(iParticle));
162  infoMapsFBDT->setProbEventLevel(category, categoryProb);
163  infoMapsFBDT -> setQpCategory(category, qpCategory);
165  float isTrueTarget = std::get<double>(manager.getVariable("hasTrueTarget(" + category + ")")-> function(nullptr));
166  infoMapsFBDT -> setHasTrueTarget(category, isTrueTarget);
167  float isTrueCategory = std::get<double>(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 
184 {
185 }
186 
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:75
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Global list of available variables.
Definition: Manager.h:101
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
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:560
#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.