Belle II Software development
KLMMuonIDDNNExpertModule.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
10#include <reconstruction/modules/KLMMuonIDDNNExpert/KLMMuonIDDNNExpertModule.h>
11
12#include <klm/bklm/geometry/GeometryPar.h>
13#include <klm/bklm/geometry/Module.h>
14#include <klm/eklm/geometry/TransformDataGlobalAligned.h>
15#include <klm/dataobjects/KLMHit2d.h>
16#include <klm/dataobjects/KLMMuidLikelihood.h>
17
18#include <reconstruction/dataobjects/KLMMuonIDDNNInputVariable.h>
19
20#include <CLHEP/Units/SystemOfUnits.h>
21
22#include <framework/logging/Logger.h>
23#include <framework/gearbox/Const.h>
24
25#include <tracking/dataobjects/ExtHit.h>
26
27#include <mdst/dataobjects/PIDLikelihood.h>
28
29#include <mdst/dataobjects/Track.h>
30
31#include <mva/interface/Interface.h>
32#include <mva/dataobjects/DatabaseRepresentationOfWeightfile.h>
33#include <mva/interface/Expert.h>
34#include <mva/interface/Weightfile.h>
35
36#include <iostream>
37#include <fstream>
38#include <sstream>
39
40using namespace Belle2;
41
42REG_MODULE(KLMMuonIDDNNExpert);
43
45{
46 // Set module properties
47 setDescription(R"DOC(Get information from KLMMuIDLikelihood)DOC");
49}
50
54
56{
57 m_tracks.isRequired();
58
59 m_inputVariable.registerInDataStore();
60 m_tracks.registerRelationTo(m_inputVariable);
61
62 // setup KLM geometry
63 const bklm::GeometryPar* bklmGeometry = bklm::GeometryPar::instance();
65
66 m_EndcapScintWidth = eklmGeometry.getStripGeometry()->getWidth() / CLHEP::cm; // in G4e units (cm)
67
68 for (int layer = 1; layer <= m_maxBKLMLayers; ++layer) {
69 const bklm::Module* module =
70 bklmGeometry->findModule(BKLMElementNumbers::c_ForwardSection, 1, layer);
71 m_BarrelPhiStripWidth[layer - 1] = module->getPhiStripWidth(); // in G4e units (cm)
72 m_BarrelZStripWidth[layer - 1] = module->getZStripWidth(); // in G4e units (cm)
73 }
74
76}
77
79{
80 m_expert.reset();
81 m_dataset.reset();
82}
83
85{
86 if (m_weightfile_representation.isValid()) {
87 if (m_weightfile_representation.hasChanged()) {
88 std::stringstream ss(m_weightfile_representation->m_data);
89 auto weightfile = MVA::Weightfile::loadFromStream(ss);
90 initializeMVA(weightfile);
91 }
92 } else {
93 B2FATAL("Payload " << m_identifier << " is not valid!");
94 }
95}
96
98{
99 const auto& supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
100 MVA::GeneralOptions general_options;
101 weightfile.getOptions(general_options);
102 m_expert = supported_interfaces.at(general_options.m_method)->getExpert();
103 m_expert->load(weightfile);
104 std::vector<float> dummy;
105 int nInputVariables = general_options.m_variables.size();
106 if (nInputVariables != 5 + 4 * (m_maxBKLMLayers + m_maxEKLMLayers)) {
107 B2FATAL("Number of input variables mismatch. Required " << 5 + 4 * (m_maxBKLMLayers + m_maxEKLMLayers) << " but " << nInputVariables
108 << " given. ");
109 }
110 dummy.resize(nInputVariables, 0);
111 m_dataset = std::unique_ptr<MVA::SingleDataset>(new MVA::SingleDataset(general_options, std::move(dummy), 0));
112}
113
114
116{
117 for (Track& track : m_tracks) {
118
119 const KLMMuidLikelihood* klmll = track.getRelatedTo<KLMMuidLikelihood>();
120
121 if (!klmll) continue;
122
123 // initialize hit pattern arrays
124 m_hitpattern_steplength.fill(-1.);
125 m_hitpattern_width.fill(-1.);
126 m_hitpattern_chi2.fill(-1.);
127 m_hitpattern_hasext.fill(0);
128
129 bool hasExtInKLM = false;
130 for (const ExtHit& exthit : track.getRelationsTo<ExtHit>()) {
131
132 if (exthit.getDetectorID() != Const::EDetector::BKLM
133 and exthit.getDetectorID() != Const::EDetector::EKLM) continue;
134
135 int layer;
136 bool inBKLM = (exthit.getDetectorID() == Const::EDetector::BKLM);
137 int copyid = exthit.getCopyID();
138
139 int section, sector, plane, strip;
140
141 if (inBKLM) {
142 BKLMElementNumbers::moduleNumberToElementNumbers(copyid, &section, &sector, &layer);
143 if (layer > m_maxBKLMLayers) continue;
144 m_hitpattern_hasext.at(layer - 1) = 1;
145 } else {
146 EKLMElementNumbers::Instance().stripNumberToElementNumbers(copyid, &section, &layer, &sector, &plane, &strip);
147 if (layer > m_maxEKLMLayers) continue;
148 m_hitpattern_hasext.at(m_maxBKLMLayers + layer - 1) = 1;
149 }
150
151 hasExtInKLM = true;
152
153 }
154
155 RelationVector<KLMHit2d> KLMHit2drelation = track.getRelationsTo<KLMHit2d>();
156
157 // only apply NN muonID to tracks with at least one KLMHit2d or one ExtHit in KLM.
158 if (not(hasExtInKLM || KLMHit2drelation.size())) continue;
159
160 std::map<int, int> Hit2dMap; // arrange KLMHit2d in the order of layer
161 for (long unsigned int ii = 0; ii < KLMHit2drelation.size(); ii++) {
162 const KLMHit2d* klmhit = KLMHit2drelation[ii];
163 bool hit_inBKLM = (klmhit->getSubdetector() == KLMElementNumbers::c_BKLM);
164 unsigned long int hit_layer = klmhit->getLayer();
165
166 int index = hit_layer - 1 + m_maxBKLMLayers * (1 - hit_inBKLM); // BKLM hits are in front of EKLM hits
167 if (index > (m_maxBKLMLayers + m_maxEKLMLayers)) continue;
168 Hit2dMap.insert(std::pair<int, int> {index, ii});
169 }
170
171 int nklmhits = 0;
172 ROOT::Math::XYZVector previousPosition(0., 0., 0.);
173 for (auto itermap = Hit2dMap.begin(); itermap != Hit2dMap.end(); itermap ++) {
174
175 nklmhits += 1;
176
177 const KLMHit2d* klmhit = KLMHit2drelation[itermap->second];
178
179 float KFchi2 = KLMHit2drelation.weight(itermap->second);
180 float width = getHitWidth(klmhit);
181
182 ROOT::Math::XYZVector hitPosition = klmhit->getPosition();
183 float steplength = 0.;
184 if (nklmhits > 1) {
185 steplength = (hitPosition - previousPosition).R();
186 }
187 previousPosition = hitPosition;
188
189 // hit pattern creation.
190 int hitpatternindex = itermap->first;
191 m_hitpattern_chi2.at(hitpatternindex) = KFchi2;
192 m_hitpattern_steplength.at(hitpatternindex) = steplength;
193 m_hitpattern_width.at(hitpatternindex) = width;
194 } // loop of Hit2dMap
195
196 Hit2dMap.clear();
197
198 double muprob_nn = getNNmuProbability(&track, klmll);
199 PIDLikelihood* pid = track.getRelated<PIDLikelihood>();
200 pid->addPreOfficialLikelihood("klmMuonIDDNN", muprob_nn);
201 } // loop of tracks
202}
203
205{
206 m_dataset->m_input[0] = klmll->getChiSquared();
207 m_dataset->m_input[1] = klmll->getDegreesOfFreedom();
208 m_dataset->m_input[2] = klmll->getExtLayer() - klmll->getHitLayer();
209 m_dataset->m_input[3] = klmll->getExtLayer();
210 m_dataset->m_input[4] = track->getTrackFitResultWithClosestMass(Const::muon)->getTransverseMomentum();
211
212 for (int layer = 0; layer < (m_maxBKLMLayers + m_maxEKLMLayers); layer ++) {
213 m_dataset->m_input[5 + 4 * layer + 0] = m_hitpattern_width.at(layer); // width
214 m_dataset->m_input[5 + 4 * layer + 1] = m_hitpattern_steplength.at(layer); // steplength
215 m_dataset->m_input[5 + 4 * layer + 2] = m_hitpattern_chi2.at(layer); // chi2
216 m_dataset->m_input[5 + 4 * layer + 3] = m_hitpattern_hasext.at(layer); // hasext
217 }
218
219 KLMMuonIDDNNInputVariable* inputVariable = m_inputVariable.appendNew();
220 inputVariable->setKLMMuonIDDNNInputVariable(m_dataset->m_input);
221 track->addRelationTo(inputVariable);
222
223 float muprob_nn = m_expert->apply(*m_dataset)[0];
224 return muprob_nn;
225}
226
228{
229 float stripwidth1 = 0; // strip width of phi or X direction
230 float stripwidth2 = 0; // strip width of Z or Y direction
231 float stripdiff1 = 0; // max minus min strip number in phi or X direction
232 float stripdiff2 = 0; // max minus min strip number in Z or Y direction
233 if (klmhit->getSubdetector() == KLMElementNumbers::c_BKLM) {
234 stripwidth1 = m_BarrelPhiStripWidth[klmhit->getLayer() - 1];
235 stripwidth2 = m_BarrelZStripWidth[klmhit->getLayer() - 1];
236 stripdiff1 = (klmhit->getPhiStripMax() - klmhit->getPhiStripMin() + 1) * 0.5;
237 stripdiff2 = (klmhit->getZStripMax() - klmhit->getZStripMin() + 1) * 0.5;
238 } else {
239 stripwidth1 = m_EndcapScintWidth;
240 stripwidth2 = m_EndcapScintWidth;
241 stripdiff1 = (klmhit->getXStripMax() - klmhit->getXStripMin() + 1) * 0.5;
242 stripdiff2 = (klmhit->getYStripMax() - klmhit->getYStripMin() + 1) * 0.5;
243 }
244 float width1 = stripwidth1 * stripdiff1;
245 float width2 = stripwidth2 * stripdiff2;
246 return std::sqrt(width1 * width1 + width2 * width2);
247}
248
249
double R
typedef autogenerated by FFTW
static void moduleNumberToElementNumbers(KLMModuleNumber module, int *section, int *sector, int *layer)
Get element numbers by module number.
static const ChargedStable muon
muon particle
Definition Const.h:661
static const EKLMElementNumbers & Instance()
Instantiation.
void stripNumberToElementNumbers(int stripGlobal, int *section, int *layer, int *sector, int *plane, int *strip) const
Get element numbers by strip global number.
double getWidth() const
Get width.
const StripGeometry * getStripGeometry() const
Get strip geometry data.
EKLM geometry data.
static const GeometryData & Instance(enum DataSource dataSource=c_Database, const GearDir *gearDir=nullptr)
Instantiation.
Store one Ext hit as a ROOT object.
Definition ExtHit.h:31
KLM 2d hit.
Definition KLMHit2d.h:33
Class to store the likelihoods from KLM with additional information related to the extrapolation.
int getDegreesOfFreedom() const
Get the number of degrees of freedom (= 2 times the number of KLM hits) for the chi-squared computati...
int getHitLayer() const
Get the outermost KLM layer actually crossed by the track.
int getExtLayer() const
Get the outermost KLM layer crossed in the extrapolation.
double getChiSquared() const
Get the chi-squared of the extrapolation.
std::array< float, m_maxBKLMLayers+m_maxEKLMLayers > m_hitpattern_width
Container of hit widths of one track.
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
float getHitWidth(const KLMHit2d *klmhit)
Get Hit width (cluster size) of a KLMHit2d.
void event() override
This method is called for each event.
float m_EndcapScintWidth
EKLM scintillator strip width (cm).
static constexpr int m_maxBKLMLayers
Total BKLM layers.
std::array< float, m_maxBKLMLayers+m_maxEKLMLayers > m_hitpattern_chi2
Container of hit chi2 of one track.
void terminate() override
This method is called at the end of the event processing.
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA expert.
void beginRun() override
Called when entering a new run.
StoreArray< Track > m_tracks
Required array for Tracks.
std::array< float, m_maxBKLMLayers+m_maxEKLMLayers > m_hitpattern_steplength
Container of hit steplength of one track.
float getNNmuProbability(const Track *track, const KLMMuidLikelihood *klmll)
Get the NN-based muon probability.
std::array< float, m_maxBKLMLayers > m_BarrelPhiStripWidth
BKLM phi-measuring strip width (cm) by layer.
DBObjPtr< DatabaseRepresentationOfWeightfile > m_weightfile_representation
Database pointer to the database representation of the weightfile.
void initializeMVA(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features.
std::array< float, m_maxBKLMLayers > m_BarrelZStripWidth
BKLM Z-measuring strip width (cm) by layer.
std::array< bool, m_maxBKLMLayers+m_maxEKLMLayers > m_hitpattern_hasext
Container of extrapolation situation at each KLM layer of one track.
const std::string m_identifier
Database identifier or file used to load the weights.
StoreArray< KLMMuonIDDNNInputVariable > m_inputVariable
Input variables of DNN.
static constexpr int m_maxEKLMLayers
Total EKLM layers.
KLM MuonID DNN input variables datastore object to store the input variables for retraining KLMMuonID...
void setKLMMuonIDDNNInputVariable(const std::vector< float > &inputdata)
set the DNN input variables.
static void initSupportedInterfaces()
Static function which initializes all supported interfaces, has to be called once before getSupported...
Definition Interface.cc:46
static const std::map< std::string, AbstractInterface * > & getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition Interface.h:53
General options which are shared by all MVA trainings.
Definition Options.h:62
Wraps the data of a single event into a Dataset.
Definition Dataset.h:135
The Weightfile class serializes all information about a training into an xml tree.
Definition Weightfile.h:38
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
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
Module()
Constructor.
Definition Module.cc:30
@ 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 collect log likelihoods from TOP, ARICH, dEdx, ECL and KLM aimed for output to mdst includes...
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
Class that bundles various TrackFitResults.
Definition Track.h:25
Provides BKLM geometry parameters for simulation, reconstruction etc (from Gearbox or DataBase)
Definition GeometryPar.h:37
const Module * findModule(int section, int sector, int layer) const
Get the pointer to the definition of a module.
static GeometryPar * instance(void)
Static method to get a reference to the singleton GeometryPar instance.
Define the geometry of a BKLM module Each sector [octant] contains Modules.
Definition Module.h:76
#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.