Belle II Software  release-05-02-19
KLMExpertModule.cc
1 /**************************************************************************
2  *
3  *
4  * BASF2 (Belle Analysis Framework 2) *
5  * Copyright(C) 2016 - Belle II Collaboration *
6  * *
7  * Author: The Belle II Collaboration *
8  * Contributors: Jo-Frederik Krohn *
9  * *
10  * This software is provided "as is" without any warranty. *
11  **************************************************************************/
12 #include <reconstruction/modules/KlId/KLMExpert/KLMExpertModule.h>
13 #include <mdst/dataobjects/KlId.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/logging/Logger.h>
16 
17 #include <mdst/dataobjects/ECLCluster.h>
18 #include <tracking/dataobjects/TrackClusterSeparation.h>
19 
20 #include <mva/interface/Interface.h>
21 #include <boost/algorithm/string/predicate.hpp>
22 
23 // here's where the functions are hidden
24 #include "reconstruction/modules/KlId/KLMExpert/KlId.h"
25 
26 using namespace KlId;
27 using namespace Belle2;
28 using namespace std;
29 
30 REG_MODULE(KLMExpert);
31 
32 KLMExpertModule::KLMExpertModule(): Module(), m_feature_variables(18, 0) //12
33 {
34  setDescription("Use to calculate KlId for each KLM cluster.");
35  addParam("classifierPath", m_identifier,
36  "path to the classifier you want to use. It is recommended to use the default classifiers and not to mess around with this.",
37  m_identifier);
39 }
40 
41 
42 
44 {
45 }
46 
47 
48 // --------------------------------------Module----------------------------------------------
50 {
51  // require existence of necessary datastore obj
52 
53  m_klmClusters.isRequired();
54 
55  m_klids.registerInDataStore();
56  m_klmClusters.registerRelationTo(m_klids);
57 
58 
59  if (not(boost::ends_with(m_identifier, ".root") or boost::ends_with(m_identifier, ".xml"))) {
60  m_weightfile_representation = std::unique_ptr<DBObjPtr<DatabaseRepresentationOfWeightfile>>(new
62  }
63 
65 
66 }
67 
68 
70 {
72  if (m_weightfile_representation->hasChanged()) {
73  std::stringstream ss((*m_weightfile_representation)->m_data);
74  auto weightfile = MVA::Weightfile::loadFromStream(ss);
75  init_mva(weightfile);
76  }
77  } else {
79  init_mva(weightfile);
80 
81  }
82 }
85 {
86 
87  auto supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
88  MVA::GeneralOptions general_options;
89  weightfile.getOptions(general_options);
90 
91  m_expert = supported_interfaces[general_options.m_method]->getExpert();
92  m_expert->load(weightfile);
93 
94  std::vector<float> dummy;
95  dummy.resize(m_feature_variables.size(), 0);
96  m_dataset = std::unique_ptr<MVA::SingleDataset>(new MVA::SingleDataset(general_options, std::move(dummy), 0));
97 
98 }
99 
100 
102 {
103  // Use the neutralHadron hypothesis for the ECL
105 
106  //overwritten at the end of the cluster loop
107  KlId* klid = nullptr;
108 
109  // loop thru clusters in event and classify
110  for (KLMCluster& cluster : m_klmClusters) {
111 
112  const TVector3& clusterPos = cluster.getClusterPosition();
113 
114  // get various KLMCluster vars
115  m_KLMglobalZ = clusterPos.Z();
116  m_KLMnCluster = m_klmClusters.getEntries();
117  m_KLMnLayer = cluster.getLayers();
118  m_KLMnInnermostLayer = cluster.getInnermostLayer();
119  m_KLMtime = cluster.getTime();
120  m_KLMenergy = cluster.getEnergy();
121  m_KLMhitDepth = cluster.getClusterPosition().Mag();
122 
123  // find nearest ecl cluster and calculate distance
124  pair<ECLCluster*, double> closestECLAndDist = findClosestECLCluster(clusterPos, eclHypothesis);
125  ECLCluster* closestECLCluster = get<0>(closestECLAndDist);
126  m_KLMECLDist = get<1>(closestECLAndDist);
127 
128  // get variables of the closest ECL cluster might be removed in future
129  if (!(closestECLCluster == nullptr)) {
130  m_KLMECLE = closestECLCluster -> getEnergy(eclHypothesis);
131  m_KLMECLE9oE25 = closestECLCluster -> getE9oE21();
132  m_KLMECLTerror = closestECLCluster -> getDeltaTime99();
133  m_KLMECLTiming = closestECLCluster -> getTime();
134  m_KLMECLEerror = closestECLCluster -> getUncertaintyEnergy();
135  m_KLMECLdeltaL = closestECLCluster -> getDeltaL();
136  m_KLMECLminTrackDist = closestECLCluster -> getMinTrkDistance();
137  m_KLMECLZMVA = closestECLCluster -> getZernikeMVA();
138  m_KLMECLZ40 = closestECLCluster -> getAbsZernike40();
139  m_KLMECLZ51 = closestECLCluster -> getAbsZernike51();
140  } else {
141  m_KLMECLdeltaL = -999;
142  m_KLMECLminTrackDist = -999;
143  m_KLMECLE = -999;
144  m_KLMECLE9oE25 = -999;
145  m_KLMECLTiming = -999;
146  m_KLMECLTerror = -999;
147  m_KLMECLEerror = -999;
148  m_KLMECLZMVA = -999;
149  m_KLMECLZ40 = -999;
150  m_KLMECLZ51 = -999;
151  }
152 
153  // calculate distance to next cluster
154  tuple<const KLMCluster*, double, double> closestKLMAndDist = findClosestKLMCluster(clusterPos);
155  m_KLMnextCluster = get<1>(closestKLMAndDist);
156  m_KLMavInterClusterDist = get<2>(closestKLMAndDist);
157 
158  m_KLMTrackSepDist = -999;
159  m_KLMTrackSepAngle = -999;
163 
164  auto trackSeperations = cluster.getRelationsTo<TrackClusterSeparation>();
165  float best_dist = 1e10;
166  for (auto trackSeperation : trackSeperations) {
167  float dist = trackSeperation.getDistance();
168  if (dist < best_dist) {
169  best_dist = dist;
170  m_KLMTrackSepDist = trackSeperation.getDistance();
171  m_KLMTrackSepAngle = trackSeperation.getTrackClusterAngle();
172  m_KLMInitialTrackSepAngle = trackSeperation.getTrackClusterInitialSeparationAngle();
173  m_KLMTrackRotationAngle = trackSeperation.getTrackRotationAngle();
174  m_KLMTrackClusterSepAngle = trackSeperation.getTrackClusterSeparationAngle();
175  }
176  }
177 
178 // reduced vars set
197 
198 
199  for (unsigned int i = 0; i < m_feature_variables.size(); ++i) {
200  if (!std::isfinite(m_feature_variables[i])) { m_feature_variables[i] = -999; }
201  m_dataset->m_input[i] = m_feature_variables[i];
202  }
203 
204  double IDMVAOut = m_expert->apply(*m_dataset)[0];
205  B2DEBUG(175, "KLM Expert classification: " << IDMVAOut);
206  klid = m_klids.appendNew();
207  cluster.addRelationTo(klid, IDMVAOut);
208 
209  }// for cluster in clusters
210 } // event
Belle2::KLMExpertModule::m_KLMECLTiming
float m_KLMECLTiming
timing of associated ECL cluster
Definition: KLMExpertModule.h:127
Belle2::KLMExpertModule::beginRun
virtual void beginRun() override
beginn run
Definition: KLMExpertModule.cc:69
Belle2::KLMExpertModule::m_KLMInitialTrackSepAngle
float m_KLMInitialTrackSepAngle
angular distance from track to cluster at track starting point
Definition: KLMExpertModule.h:109
Belle2::KLMExpertModule::m_KLMnextCluster
float m_KLMnextCluster
distance to next KLM cluster
Definition: KLMExpertModule.h:101
Belle2::MVA::AbstractInterface::getSupportedInterfaces
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition: Interface.h:55
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::MVA::Weightfile::getOptions
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:76
Belle2::ECLCluster
ECL cluster data.
Definition: ECLCluster.h:39
Belle2::KLMExpertModule::m_KLMECLE
float m_KLMECLE
energy measured in associated ECL cluster
Definition: KLMExpertModule.h:119
KlId
Helper functions for all klid modules to improve readability of the code.
Definition: KlId.h:28
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module::c_ParallelProcessingCertified
@ 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:82
Belle2::KLMExpertModule::m_feature_variables
std::vector< float > m_feature_variables
vars to be classified
Definition: KLMExpertModule.h:146
Belle2::KLMExpertModule::m_KLMtime
float m_KLMtime
timing of KLM Cluster
Definition: KLMExpertModule.h:93
Belle2::ECLCluster::EHypothesisBit
EHypothesisBit
The hypothesis bits for this ECLCluster (Connected region (CR) is split using this hypothesis.
Definition: ECLCluster.h:43
Belle2::MVA::Weightfile
The Weightfile class serializes all information about a training into an xml tree.
Definition: Weightfile.h:40
Belle2::KLMExpertModule::m_KLMECLminTrackDist
float m_KLMECLminTrackDist
track distance between associated ECL cluster and track extrapolated into ECL
Definition: KLMExpertModule.h:123
Belle2::KLMExpertModule::event
virtual void event() override
process event
Definition: KLMExpertModule.cc:101
Belle2::KLMExpertModule::m_KLMTrackSepDist
float m_KLMTrackSepDist
distance from track separation object
Definition: KLMExpertModule.h:103
Belle2::KLMExpertModule::m_KLMavInterClusterDist
float m_KLMavInterClusterDist
average distance between all KLM clusters
Definition: KLMExpertModule.h:95
Belle2::KLMExpertModule::m_KLMECLTerror
float m_KLMECLTerror
uncertanty on time in associated ECL cluster
Definition: KLMExpertModule.h:129
Belle2::KLMExpertModule::m_KLMnLayer
float m_KLMnLayer
number of layers hit in KLM cluster
Definition: KLMExpertModule.h:87
Belle2::KLMExpertModule::initialize
virtual void initialize() override
init
Definition: KLMExpertModule.cc:49
Belle2::KLMExpertModule::m_KLMhitDepth
float m_KLMhitDepth
hit depth in KLM, distance to IP
Definition: KLMExpertModule.h:97
Belle2::KLMExpertModule::m_KLMTrackSepAngle
float m_KLMTrackSepAngle
angular distance from track separation object.
Definition: KLMExpertModule.h:106
KlId::findClosestECLCluster
std::pair< Belle2::ECLCluster *, double > findClosestECLCluster(const TVector3 &klmClusterPosition, const Belle2::ECLCluster::EHypothesisBit eclhypothesis=Belle2::ECLCluster::EHypothesisBit::c_neutralHadron)
Find the closest ECLCluster with a neutral hadron hypothesis, and return it with its distance.
Definition: KlId.h:210
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2::KLMExpertModule::m_KLMECLdeltaL
float m_KLMECLdeltaL
distance between track entry pofloat and cluster center, might be removed
Definition: KLMExpertModule.h:121
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::KLMExpertModule::~KLMExpertModule
virtual ~KLMExpertModule()
Destructor.
Definition: KLMExpertModule.cc:43
Belle2::KLMExpertModule::m_KLMECLZ51
float m_KLMECLZ51
zernike moment 5,1 of closest ECL
Definition: KLMExpertModule.h:138
Belle2::KLMExpertModule::m_KLMECLEerror
float m_KLMECLEerror
uncertanty on E in associated ECL cluster
Definition: KLMExpertModule.h:131
Belle2::KLMExpertModule::m_KLMnInnermostLayer
float m_KLMnInnermostLayer
number of innermost layers hit
Definition: KLMExpertModule.h:89
Belle2::KLMExpertModule::m_KLMECLZMVA
float m_KLMECLZMVA
output of a BDT fitted on various Z-moments for the closest ECL cluster
Definition: KLMExpertModule.h:134
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::KLMExpertModule::m_KLMTrackClusterSepAngle
float m_KLMTrackClusterSepAngle
angle between trach momentum and cluster (measured from ip)
Definition: KLMExpertModule.h:113
Belle2::MVA::Weightfile::loadFromFile
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
Definition: Weightfile.cc:215
Belle2::KLMExpertModule::m_KLMTrackRotationAngle
float m_KLMTrackRotationAngle
angle between track at poca and trackbeginning
Definition: KLMExpertModule.h:111
Belle2::KLMExpertModule::m_weightfile_representation
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the Database representation of the weightfile.
Definition: KLMExpertModule.h:154
Belle2::ECLCluster::EHypothesisBit::c_neutralHadron
@ c_neutralHadron
CR is reconstructed as a neutral hadron (N2)
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::KLMCluster
KLM cluster data.
Definition: KLMCluster.h:38
Belle2::KLMExpertModule::m_KLMECLDist
float m_KLMECLDist
distance associated ECL <-> KLM cluster, extrapolated by genfit
Definition: KLMExpertModule.h:117
Belle2::KLMExpertModule::m_klids
StoreArray< KlId > m_klids
storearray
Definition: KLMExpertModule.h:143
Belle2::KLMExpertModule::m_KLMglobalZ
float m_KLMglobalZ
global Z position in KLM
Definition: KLMExpertModule.h:91
Belle2::MVA::AbstractInterface::initSupportedInterfaces
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
Definition: Interface.cc:55
Belle2::MVA::SingleDataset
Wraps the data of a single event into a Dataset.
Definition: Dataset.h:136
Belle2::KLMExpertModule::m_klmClusters
StoreArray< KLMCluster > m_klmClusters
storearray
Definition: KLMExpertModule.h:141
Belle2::KLMExpertModule::m_KLMECLZ40
float m_KLMECLZ40
zernike moment 4,0 of closest ECL
Definition: KLMExpertModule.h:136
Belle2::TrackClusterSeparation::getDistance
double getDistance() const
Definition: TrackClusterSeparation.h:41
Belle2::Module::addParam
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:562
KlId::findClosestKLMCluster
std::tuple< const Belle2::KLMCluster *, double, double > findClosestKLMCluster(const TVector3 &klmClusterPosition)
find nearest KLMCluster, tis distance and the av intercluster distance
Definition: KlId.h:242
Belle2::MVA::Weightfile::loadFromStream
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Definition: Weightfile.cc:260
Belle2::KLMExpertModule::m_KLMenergy
float m_KLMenergy
Energy deposit in KLM (0.2 GeV * nHitCells)
Definition: KLMExpertModule.h:99
Belle2::KLMExpertModule::m_KLMnCluster
float m_KLMnCluster
varibales to write out.
Definition: KLMExpertModule.h:85
Belle2::KLMExpertModule::m_identifier
std::string m_identifier
mva identifier.
Definition: KLMExpertModule.h:149
Belle2::KLMExpertModule::m_KLMECLE9oE25
float m_KLMECLE9oE25
E in surrounding 9 crystals divided by surrounding 25 crydtalls.
Definition: KLMExpertModule.h:125
Belle2::TrackClusterSeparation
Store one Track-KLMCluster separation as a ROOT object.
Definition: TrackClusterSeparation.h:30
Belle2::KLMExpertModule::m_expert
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
Definition: KLMExpertModule.h:156
Belle2::KLMExpertModule::init_mva
void init_mva(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features Called everytime the weightfile in the database changes i...
Definition: KLMExpertModule.cc:84
Belle2::KLMExpertModule::m_dataset
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
Definition: KLMExpertModule.h:158