Belle II Software  release-05-02-19
FlavorTaggerInfoFillerModule.cc
1 /**************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2010 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Fernando Abudinen *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 
11 #include <analysis/modules/FlavorTaggerInfoFiller/FlavorTaggerInfoFillerModule.h>
12 #include <framework/core/ModuleParam.templateDetails.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <analysis/dataobjects/ParticleList.h>
16 #include <analysis/dataobjects/EventExtraInfo.h>
17 #include <analysis/dataobjects/FlavorTaggerInfo.h>
18 #include <analysis/dataobjects/RestOfEvent.h>
19 #include <analysis/VariableManager/Manager.h>
20 #include <mdst/dataobjects/MCParticle.h>
21 
22 #include <iostream>
23 
24 using namespace std;
25 using namespace Belle2;
26 
27 // Register module in the framework
28 REG_MODULE(FlavorTaggerInfoFiller)
29 
31 {
32  //Set module properties
33  setDescription("Creates a new flavorTaggerInfoMap DataObject for the specific methods. Saves there all the relevant information of the flavorTagger.");
34  setPropertyFlags(c_ParallelProcessingCertified);
35  //Parameter definition
36  addParam("trackLevelParticleLists", m_trackLevelParticleLists, "Used Flavor Tagger trackLevel Categories of the lists ",
37  vector<tuple<string, string>>());
38  addParam("eventLevelParticleLists", m_eventLevelParticleLists, "Used Flavor Tagger eventLevel Categories of the lists ",
39  vector<tuple<string, string, string>>());
40  addParam("FANNmlp", m_FANNmlp, "Sets if FANN Combiner output will be saved or not", false);
41  addParam("TMVAfbdt", m_TMVAfbdt, "Sets if FANN Combiner output will be saved or not", false);
42  addParam("DNNmlp", m_DNNmlp, "Sets if DNN Tagger output will be saved or not", false);
43  addParam("qpCategories", m_qpCategories, "Sets if individual categories output will be saved or not", false);
44  addParam("istrueCategories", m_istrueCategories, "Sets if individual MC truth for each category is saved or not", false);
45  addParam("targetProb", m_targetProb, "Sets if individual Categories output will be saved or not", false);
46  addParam("trackPointers", m_trackPointers, "Sets if track pointers to target tracks are saved or not", false);
47 
48 }
49 
50 void FlavorTaggerInfoFillerModule::initialize()
51 {
52 }
53 
54 void FlavorTaggerInfoFillerModule::event()
55 {
56 // StoreObjPtr<FlavorTaggerInfo> flavorTaggerInfo;
57  StoreObjPtr<EventExtraInfo> eventExtraInfo;
59 
60  auto* flavorTaggerInfo = RestOfEvent->getRelatedTo<FlavorTaggerInfo>();
61 
62  Variable::Manager& manager = Variable::Manager::Instance();
63 
64 
65  if (flavorTaggerInfo == nullptr) B2ERROR("flavorTaggerInfoFiller: FlavorTaggerInfo does not exist");
66 
67  flavorTaggerInfo -> setUseModeFlavorTagger("Expert");
68 
69  if (m_FANNmlp) {
70  FlavorTaggerInfoMap* infoMapsFANN = flavorTaggerInfo -> getMethodMap("FANN");
71  // For FANN, the output is mapped to be qr
72  float qrCombined = eventExtraInfo->getExtraInfo("qrCombinedFANN");
73  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
74  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
75  float B0Probability = qrCombined / 2 + 0.5;
76  float B0barProbability = 1 - B0Probability;
77  infoMapsFANN->setQrCombined(qrCombined);
78  infoMapsFANN->setB0Probability(B0Probability);
79  infoMapsFANN->setB0barProbability(B0barProbability);
80  }
81 
82  FlavorTaggerInfoMap* infoMapsFBDT = flavorTaggerInfo -> getMethodMap("FBDT");
83 
84  if (m_TMVAfbdt) {
85  float B0Probability = eventExtraInfo->getExtraInfo("qrCombinedFBDT");
86  float B0barProbability = 1 - B0Probability;
87  float qrCombined = 2 * (B0Probability - 0.5);
88  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
89  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
90  infoMapsFBDT->setQrCombined(qrCombined);
91  infoMapsFBDT->setB0Probability(B0Probability);
92  infoMapsFBDT->setB0barProbability(B0barProbability);
93  }
94 
95  if (m_DNNmlp) {
96  FlavorTaggerInfoMap* infoMapsDNN = flavorTaggerInfo -> getMethodMap("DNN");
97  const Particle* particle = RestOfEvent->getRelated<Particle>();
98  float B0Probability = particle->getExtraInfo("dnn_output");
99  float B0barProbability = 1 - B0Probability;
100  float qrCombined = 2 * (B0Probability - 0.5);
101  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
102  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
103  infoMapsDNN->setQrCombined(qrCombined);
104  infoMapsDNN->setB0Probability(B0Probability);
105  infoMapsDNN->setB0barProbability(B0barProbability);
106  }
107 
108 
109  if (m_targetProb) {
110  for (auto& iTuple : m_trackLevelParticleLists) {
111  string particleListName = get<0>(iTuple);
112  string category = get<1>(iTuple);
113  StoreObjPtr<ParticleList> particleList(particleListName);
114 
115 
116  if (!particleList.isValid()) {
117  B2INFO("ParticleList " << particleListName << " not found");
118  } else {
119  if (particleList -> getListSize() == 0) {
120  infoMapsFBDT -> setProbTrackLevel(category, 0);
121  if (m_trackPointers) infoMapsFBDT -> setTargetTrackLevel(category, nullptr);
122  } else {
123 
124  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
125  Particle* iParticle = particleList ->getParticle(i);
126  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightTrack(" +
127  category + "))")-> function(iParticle);
128  if (hasMaxProb == 1) {
129  float targetProb = iParticle -> getExtraInfo("isRightTrack(" + category + ")");
130  infoMapsFBDT->setProbTrackLevel(category, targetProb);
131  if (m_trackPointers) {
132  infoMapsFBDT-> setTargetTrackLevel(category, iParticle -> getTrack());
133  }
134  break;
135  }
136  }
137  }
138  }
139  }
140  }
141 
142  if (m_qpCategories) {
143 
144  bool mcFlag = false;
145 
146  if (m_istrueCategories) {
147  StoreArray<MCParticle> mcparticles;
148  if (mcparticles.isValid()) mcFlag = true;
149  };
150 
151  for (auto& iTuple : m_eventLevelParticleLists) {
152  string particleListName = get<0>(iTuple);
153  string category = get<1>(iTuple);
154  string qpCategoryVariable = get<2>(iTuple);
155  StoreObjPtr<ParticleList> particleList(particleListName);
156 
157  if (!particleList.isValid()) {
158  B2INFO("ParticleList " << particleListName << " not found");
159  } else {
160  if (particleList -> getListSize() == 0) {
161  infoMapsFBDT -> setProbEventLevel(category, 0);
162  infoMapsFBDT -> setQpCategory(category, 0);
163  if (m_istrueCategories and mcFlag) {
164  infoMapsFBDT -> setHasTrueTarget(category, 0);
165  infoMapsFBDT -> setIsTrueCategory(category, 0);
166  }
167  if (m_trackPointers) infoMapsFBDT -> setTargetEventLevel(category, nullptr);
168  } else {
169 
170  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
171  Particle* iParticle = particleList ->getParticle(i);
172  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightCategory(" +
173  category + "))")-> function(iParticle);
174  if (hasMaxProb == 1) {
175  float categoryProb = iParticle -> getExtraInfo("isRightCategory(" + category + ")");
176  float qpCategory = manager.getVariable(qpCategoryVariable)-> function(iParticle);
177  infoMapsFBDT->setProbEventLevel(category, categoryProb);
178  infoMapsFBDT -> setQpCategory(category, qpCategory);
179  if (m_istrueCategories and mcFlag) {
180  float isTrueTarget = manager.getVariable("hasTrueTarget(" + category + ")")-> function(nullptr);
181  infoMapsFBDT -> setHasTrueTarget(category, isTrueTarget);
182  float isTrueCategory = manager.getVariable("isTrueCategory(" + category + ")")-> function(nullptr);
183  infoMapsFBDT -> setIsTrueCategory(category, isTrueCategory);
184  }
185  if (m_trackPointers) {
186  infoMapsFBDT-> setTargetEventLevel(category, iParticle -> getTrack());
187  }
188  break;
189  }
190  }
191  }
192  }
193  }
194  }
195 
196 }
197 
198 void FlavorTaggerInfoFillerModule::terminate()
199 {
200 }
201 
Belle2::FlavorTaggerInfoMap::setProbTrackLevel
void setProbTrackLevel(const std::string &category, float probability)
Map filler: Set the category name and the corresponding highest target track probability.
Definition: FlavorTaggerInfoMap.cc:26
Belle2::FlavorTaggerInfoMap::setB0Probability
void setB0Probability(float B0Probability)
Saves the B0Probability output of the Combiner.
Definition: FlavorTaggerInfoMap.cc:65
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::FlavorTaggerInfoMap
This class stores the Flavor Tagger information for a specific method and particle filled in the Flav...
Definition: FlavorTaggerInfoMap.h:48
Belle2::RelationsInterface::getRelated
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
Definition: RelationsObject.h:280
Belle2::RestOfEvent
This is a general purpose class for collecting reconstructed MDST data objects that are not used in r...
Definition: RestOfEvent.h:59
Belle2::RelationsInterface::getRelatedTo
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
Definition: RelationsObject.h:250
Belle2::FlavorTaggerInfoMap::setB0barProbability
void setB0barProbability(float B0barProbability)
Saves the B0barProbability output of the Combiner.
Definition: FlavorTaggerInfoMap.cc:71
Belle2::FlavorTaggerInfoMap::setProbEventLevel
void setProbEventLevel(const std::string &category, float probability)
Map filler: Set the category name and the highest category probability for the corresponding category...
Definition: FlavorTaggerInfoMap.cc:38
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::FlavorTaggerInfoFillerModule
Creates a new flavorTaggerInfoMap DataObject for the specific methods.
Definition: FlavorTaggerInfoFillerModule.h:43
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::StoreArray::isValid
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:298
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::StoreArray< MCParticle >
Belle2::FlavorTaggerInfo
This class stores the relevant information for the TagV vertex fit, extracted mainly from the Flavor ...
Definition: FlavorTaggerInfo.h:57
Belle2::Variable::Manager
Global list of available variables.
Definition: Manager.h:108
Belle2::FlavorTaggerInfoMap::setQrCombined
void setQrCombined(float qr)
Saves qr Output of the Combiner.
Definition: FlavorTaggerInfoMap.cc:59