Belle II Software development
ECLExpertModule.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 <reconstruction/modules/KlId/ECLExpert/ECLExpertModule.h>
10#include <mdst/dataobjects/KlId.h>
11#include <framework/datastore/StoreArray.h>
12#include <framework/logging/Logger.h>
13
14#include <mdst/dataobjects/ECLCluster.h>
15#include <tracking/dataobjects/TrackClusterSeparation.h>
16
17#include <mva/interface/Interface.h>
18#include <mva/interface/Weightfile.h>
19#include <mva/interface/Expert.h>
20#include <mva/dataobjects/DatabaseRepresentationOfWeightfile.h>
21
22// here's where the functions are hidden
23#include "reconstruction/modules/KlId/KLMExpert/KlId.h"
24
25using namespace Belle2;
26using namespace KlongId;
27using namespace std;
28
29REG_MODULE(ECLExpert);
30
32 //ECLExpertModule::ECLExpertModule(): Module(), m_feature_variables(9, 0) //12
33{
34 setDescription("Use to calculate KlId for each ECL 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.",
39}
40
41
42
44
45// --------------------------------------Module----------------------------------------------
47{
48 // require existence of necessary datastore obj
49
50 m_eclClusters.isRequired();
51
52 m_klids.registerInDataStore();
53 m_eclClusters.registerRelationTo(m_klids);
54
55
56 if (not(m_identifier.ends_with(".root") or m_identifier.ends_with(".xml"))) {
57 m_weightfile_representation = std::make_unique<DBObjPtr<DatabaseRepresentationOfWeightfile>>(MVA::makeSaveForDatabase(
59 }
60
62 B2INFO(getName().data() << "::initialize() Using BDT " << m_identifier.data() << " with " << m_feature_variables.size() <<
63 " variables");
64}
65
66
68{
70 if (m_weightfile_representation->hasChanged()) {
71 std::stringstream ss((*m_weightfile_representation)->m_data);
72 auto weightfile = MVA::Weightfile::loadFromStream(ss);
73 init_mva(weightfile);
74 }
75 } else {
77 init_mva(weightfile);
78
79 }
80}
81
83{
84
85 auto supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
86 MVA::GeneralOptions general_options;
87 weightfile.getOptions(general_options);
88
89 m_expert = supported_interfaces[general_options.m_method]->getExpert();
90 m_expert->load(weightfile);
91
92 std::vector<float> dummy;
93 dummy.resize(m_feature_variables.size(), 0);
94 m_dataset = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
95
96}
97
98
100{
101 // Use the neutralHadron hypothesis for the ECL
103
104 //overwritten at the end of the cluster loop
105 KlId* klid = nullptr;
106
107 // loop thru clusters in event and classify
108 for (ECLCluster& cluster : m_eclClusters) {
109
110 if (!cluster.hasHypothesis(eclHypothesis)) {continue;}
111
112 // Cluster properties
113 m_ECLminTrkDistance = cluster.getMinTrkDistance();
114 m_ECLDeltaL = cluster.getDeltaL();
115 m_ECLZMVA = cluster.getZernikeMVA();
116 m_ECLZ40 = cluster.getAbsZernike40();
117 m_ECLZ51 = cluster.getAbsZernike51();
118 m_ECLE1oE9 = cluster.getE1oE9();
119 m_ECLE9oE21 = cluster.getE9oE21();
120 m_ECLsecondMoment = cluster.getSecondMoment();
121 m_ECLLAT = cluster.getLAT();
122 m_ECLnumberOfCrystals = cluster.getNumberOfCrystals();
123 m_ECLtime = cluster.getTime();
124 m_ECLdeltaTime99 = cluster.getDeltaTime99();
125 m_ECLtheta = cluster.getTheta();
126 m_ECLphi = cluster.getPhi();
127 m_ECLr = cluster.getR();
128 m_ECLPulseShapeDiscriminationMVA = cluster.getPulseShapeDiscriminationMVA();
129 m_ECLNumberOfHadronDigits = cluster.getNumberOfHadronDigits();
130 m_ECLEnergy = cluster.getEnergy(eclHypothesis);
131 m_ECLlogEnergy = cluster.getEnergyRaw();
132 m_ECLlogEnergyHighestCrystal = cluster.getEnergyHighestCrystal();
133
134
135// reduced vars sets
136// m_feature_variables.clear();
143 // m_feature_variables[] = m_ECLE9oE21;
146 // m_feature_variables[] = m_ECLnumberOfCrystals;
153 // m_feature_variables[] = m_ECLNumberOfHadronDigits;
157
158 for (unsigned int i = 0; i < m_feature_variables.size(); ++i) {
159 if (!std::isfinite(m_feature_variables[i])) { m_feature_variables[i] = -999; }
160 m_dataset->m_input[i] = m_feature_variables[i];
161 }
162
163 double IDMVAOut = m_expert->apply(*m_dataset)[0];
164 B2DEBUG(175, "ECL Expert classification: " << IDMVAOut);
165 klid = m_klids.appendNew();
166 cluster.addRelationTo(klid, IDMVAOut);
167
168 }// for cluster in clusters
169} // event
ECL cluster data.
Definition ECLCluster.h:27
EHypothesisBit
The hypothesis bits for this ECLCluster (Connected region (CR) is split using this hypothesis.
Definition ECLCluster.h:31
@ c_neutralHadron
CR is reconstructed as a neutral hadron (N2)
Definition ECLCluster.h:43
Double32_t m_ECLtheta
Theta [rad].
StoreArray< KlId > m_klids
storearray
Double32_t m_ECLtime
Time.
Double32_t m_ECLphi
Phi [rad].
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
virtual void initialize() override
init
Double32_t m_ECLE1oE9
E1oE9.
Double32_t m_ECLminTrkDistance
Distance between cluster center and track extrapolation to ECL.
virtual void event() override
process event
Double32_t m_ECLEnergy
Energy [GeV].
Double32_t m_ECLlogEnergyHighestCrystal
Log.
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
Double32_t m_ECLE9oE21
E9oE21.
virtual ~ECLExpertModule()
Destructor.
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the Database representation of the weightfile.
Double32_t m_ECLdeltaTime99
Delta Time 99.
std::vector< float > m_feature_variables
vars to be classified
virtual void beginRun() override
begin run
Double32_t m_ECLZMVA
output of a BDT fitted on various Z-moments for the closest ECL cluster
Double32_t m_ECLnumberOfCrystals
Number of Crystals in a shower (sum of weights).
Double32_t m_ECLZ40
Zernike 40.
StoreArray< ECLCluster > m_eclClusters
storearray
Double32_t m_ECLr
Radius [cm].
Double32_t m_ECLPulseShapeDiscriminationMVA
MVA classifier that uses pulse shape discrimination to identify electromagnetic vs hadronic showers.
Double32_t m_ECLNumberOfHadronDigits
Number of hadron digits in cluster (pulse shape discrimination variable).
Double32_t m_ECLlogEnergy
Log.
Double32_t m_ECLsecondMoment
Second Moment.
Double32_t m_ECLDeltaL
DeltaL.
void init_mva(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features Called every time the weightfile in the database changes ...
Double32_t m_ECLZ51
Zernike 51.
std::string m_identifier
mva identifier.
Klong identification (KlId) datastore object to store results from KlId calculations.
Definition KlId.h:23
static void initSupportedInterfaces()
Static function which initializes all supported interfaces, has to be called once before getSupported...
Definition Interface.cc:46
static 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
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.
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
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
const std::string & getName() const
Returns the name of the module.
Definition Module.h:186
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
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Helper functions for all klid modules to improve readability of the code.
Definition KlId.h:27
Abstract base class for different kinds of events.
STL namespace.