Belle II Software  release-05-01-25
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("qpCategories", m_qpCategories, "Sets if individual categories output will be saved or not", false);
43  addParam("istrueCategories", m_istrueCategories, "Sets if individual MC truth for each category is saved or not", false);
44  addParam("targetProb", m_targetProb, "Sets if individual Categories output will be saved or not", false);
45  addParam("trackPointers", m_trackPointers, "Sets if track pointers to target tracks are saved or not", false);
46 
47 }
48 
49 void FlavorTaggerInfoFillerModule::initialize()
50 {
51 }
52 
53 void FlavorTaggerInfoFillerModule::event()
54 {
55 // StoreObjPtr<FlavorTaggerInfo> flavorTaggerInfo;
56  StoreObjPtr<EventExtraInfo> eventExtraInfo;
58 
59  auto* flavorTaggerInfo = RestOfEvent->getRelatedTo<FlavorTaggerInfo>();
60 
61  Variable::Manager& manager = Variable::Manager::Instance();
62 
63 
64  if (flavorTaggerInfo == nullptr) B2ERROR("flavorTaggerInfoFiller: FlavorTaggerInfo does not exist");
65 
66  flavorTaggerInfo -> setUseModeFlavorTagger("Expert");
67 
68  if (m_FANNmlp) {
69  flavorTaggerInfo -> addMethodMap("FANN");
70  FlavorTaggerInfoMap* infoMapsFANN = flavorTaggerInfo -> getMethodMap("FANN");
71  // float qrCombined = 2 * (eventExtraInfo->getExtraInfo("qrCombinedFANN") - 0.5);
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 = eventExtraInfo->getExtraInfo("qrCombinedFANN");
76  float B0barProbability = 1 - eventExtraInfo->getExtraInfo("qrCombinedFANN");
77  infoMapsFANN->setQrCombined(qrCombined);
78  infoMapsFANN->setB0Probability(B0Probability);
79  infoMapsFANN->setB0barProbability(B0barProbability);
80  }
81 
82  flavorTaggerInfo -> addMethodMap("FBDT");
83  FlavorTaggerInfoMap* infoMapsFBDT = flavorTaggerInfo -> getMethodMap("FBDT");
84 
85  if (m_TMVAfbdt) {
86  float qrCombined = 2 * (eventExtraInfo->getExtraInfo("qrCombinedFBDT") - 0.5);
87  if (qrCombined < 1.1 && qrCombined > 1.0) qrCombined = 1.0;
88  if (qrCombined > - 1.1 && qrCombined < -1.0) qrCombined = -1.0;
89  float B0Probability = eventExtraInfo->getExtraInfo("qrCombinedFBDT");
90  float B0barProbability = 1 - eventExtraInfo->getExtraInfo("qrCombinedFBDT");
91  infoMapsFBDT->setQrCombined(qrCombined);
92  infoMapsFBDT->setB0Probability(B0Probability);
93  infoMapsFBDT->setB0barProbability(B0barProbability);
94  }
95 
96  if (m_targetProb) {
97  for (auto& iTuple : m_trackLevelParticleLists) {
98  string particleListName = get<0>(iTuple);
99  string category = get<1>(iTuple);
100  StoreObjPtr<ParticleList> particleList(particleListName);
101 
102 
103  if (!particleList.isValid()) {
104  B2INFO("ParticleList " << particleListName << " not found");
105  } else {
106  if (particleList -> getListSize() == 0) {
107  infoMapsFBDT -> setProbTrackLevel(category, 0);
108  if (m_trackPointers) infoMapsFBDT -> setTargetTrackLevel(category, nullptr);
109  } else {
110 
111  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
112  Particle* iParticle = particleList ->getParticle(i);
113  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightTrack(" +
114  category + "))")-> function(iParticle);
115  if (hasMaxProb == 1) {
116  float targetProb = iParticle -> getExtraInfo("isRightTrack(" + category + ")");
117  infoMapsFBDT->setProbTrackLevel(category, targetProb);
118  if (m_trackPointers) {
119  infoMapsFBDT-> setTargetTrackLevel(category, iParticle -> getTrack());
120  }
121  break;
122  }
123  }
124  }
125  }
126  }
127  }
128 
129  if (m_qpCategories) {
130 
131  bool mcFlag = false;
132 
133  if (m_istrueCategories) {
134  StoreArray<MCParticle> mcparticles;
135  if (mcparticles.isValid()) mcFlag = true;
136  };
137 
138  for (auto& iTuple : m_eventLevelParticleLists) {
139  string particleListName = get<0>(iTuple);
140  string category = get<1>(iTuple);
141  string qpCategoryVariable = get<2>(iTuple);
142  StoreObjPtr<ParticleList> particleList(particleListName);
143 
144  if (!particleList.isValid()) {
145  B2INFO("ParticleList " << particleListName << " not found");
146  } else {
147  if (particleList -> getListSize() == 0) {
148  infoMapsFBDT -> setProbEventLevel(category, 0);
149  infoMapsFBDT -> setQpCategory(category, 0);
150  if (m_istrueCategories and mcFlag) {
151  infoMapsFBDT -> setHasTrueTarget(category, 0);
152  infoMapsFBDT -> setIsTrueCategory(category, 0);
153  }
154  if (m_trackPointers) infoMapsFBDT -> setTargetEventLevel(category, nullptr);
155  } else {
156 
157  for (unsigned int i = 0; i < particleList->getListSize(); ++i) {
158  Particle* iParticle = particleList ->getParticle(i);
159  float hasMaxProb = manager.getVariable("hasHighestProbInCat(" + particleListName + "," + "isRightCategory(" +
160  category + "))")-> function(iParticle);
161  if (hasMaxProb == 1) {
162  float categoryProb = iParticle -> getExtraInfo("isRightCategory(" + category + ")");
163  float qpCategory = manager.getVariable(qpCategoryVariable)-> function(iParticle);
164  infoMapsFBDT->setProbEventLevel(category, categoryProb);
165  infoMapsFBDT -> setQpCategory(category, qpCategory);
166  if (m_istrueCategories and mcFlag) {
167  float isTrueTarget = manager.getVariable("hasTrueTarget(" + category + ")")-> function(nullptr);
168  infoMapsFBDT -> setHasTrueTarget(category, isTrueTarget);
169  float isTrueCategory = manager.getVariable("isTrueCategory(" + category + ")")-> function(nullptr);
170  infoMapsFBDT -> setIsTrueCategory(category, isTrueCategory);
171  }
172  if (m_trackPointers) {
173  infoMapsFBDT-> setTargetEventLevel(category, iParticle -> getTrack());
174  }
175  break;
176  }
177  }
178  }
179  }
180  }
181  }
182 
183 }
184 
185 void FlavorTaggerInfoFillerModule::terminate()
186 {
187 }
188 
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::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