Belle II Software  release-08-01-10
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 <boost/algorithm/string/predicate.hpp>
19 
20 // here's where the functions are hidden
21 #include "reconstruction/modules/KlId/KLMExpert/KlId.h"
22 
23 using namespace Belle2;
24 using namespace KlongId;
25 using namespace std;
26 
27 REG_MODULE(ECLExpert);
28 
29 ECLExpertModule::ECLExpertModule(): Module(), m_feature_variables(17, 0) //12
30  //ECLExpertModule::ECLExpertModule(): Module(), m_feature_variables(9, 0) //12
31 {
32  setDescription("Use to calculate KlId for each ECL cluster.");
33  addParam("classifierPath", m_identifier,
34  "path to the classifier you want to use. It is recommended to use the default classifiers and not to mess around with this.",
35  m_identifier);
37 }
38 
39 
40 
42 
43 // --------------------------------------Module----------------------------------------------
45 {
46  // require existence of necessary datastore obj
47 
48  m_eclClusters.isRequired();
49 
50  m_klids.registerInDataStore();
51  m_eclClusters.registerRelationTo(m_klids);
52 
53 
54  if (not(boost::ends_with(m_identifier, ".root") or boost::ends_with(m_identifier, ".xml"))) {
55  m_weightfile_representation = std::make_unique<DBObjPtr<DatabaseRepresentationOfWeightfile>>(MVA::makeSaveForDatabase(
56  m_identifier));
57  }
58 
60  B2INFO(getName().data() << "::initialize() Using BDT " << m_identifier.data() << " with " << m_feature_variables.size() <<
61  " variables");
62 }
63 
64 
66 {
68  if (m_weightfile_representation->hasChanged()) {
69  std::stringstream ss((*m_weightfile_representation)->m_data);
70  auto weightfile = MVA::Weightfile::loadFromStream(ss);
71  init_mva(weightfile);
72  }
73  } else {
75  init_mva(weightfile);
76 
77  }
78 }
81 {
82 
83  auto supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
84  MVA::GeneralOptions general_options;
85  weightfile.getOptions(general_options);
86 
87  m_expert = supported_interfaces[general_options.m_method]->getExpert();
88  m_expert->load(weightfile);
89 
90  std::vector<float> dummy;
91  dummy.resize(m_feature_variables.size(), 0);
92  m_dataset = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
93 
94 }
95 
96 
98 {
99  // Use the neutralHadron hypothesis for the ECL
101 
102  //overwritten at the end of the cluster loop
103  KlId* klid = nullptr;
104 
105  // loop thru clusters in event and classify
106  for (ECLCluster& cluster : m_eclClusters) {
107 
108  if (!cluster.hasHypothesis(eclHypothesis)) {continue;}
109 
110  // Cluster properties
111  m_ECLminTrkDistance = cluster.getMinTrkDistance();
112  m_ECLDeltaL = cluster.getDeltaL();
113  m_ECLZMVA = cluster.getZernikeMVA();
114  m_ECLZ40 = cluster.getAbsZernike40();
115  m_ECLZ51 = cluster.getAbsZernike51();
116  m_ECLE1oE9 = cluster.getE1oE9();
117  m_ECLE9oE21 = cluster.getE9oE21();
118  m_ECLsecondMoment = cluster.getSecondMoment();
119  m_ECLLAT = cluster.getLAT();
120  m_ECLnumberOfCrystals = cluster.getNumberOfCrystals();
121  m_ECLtime = cluster.getTime();
122  m_ECLdeltaTime99 = cluster.getDeltaTime99();
123  m_ECLtheta = cluster.getTheta();
124  m_ECLphi = cluster.getPhi();
125  m_ECLr = cluster.getR();
126  m_ECLPulseShapeDiscriminationMVA = cluster.getPulseShapeDiscriminationMVA();
127  m_ECLNumberOfHadronDigits = cluster.getNumberOfHadronDigits();
128  m_ECLEnergy = cluster.getEnergy(eclHypothesis);
129  m_ECLlogEnergy = cluster.getEnergyRaw();
130  m_ECLlogEnergyHighestCrystal = cluster.getEnergyHighestCrystal();
131 
132 
133 // reduced vars sets
134 // m_feature_variables.clear();
141  // m_feature_variables[] = m_ECLE9oE21;
144  // m_feature_variables[] = m_ECLnumberOfCrystals;
151  // m_feature_variables[] = m_ECLNumberOfHadronDigits;
155 
156  for (unsigned int i = 0; i < m_feature_variables.size(); ++i) {
157  if (!std::isfinite(m_feature_variables[i])) { m_feature_variables[i] = -999; }
158  m_dataset->m_input[i] = m_feature_variables[i];
159  }
160 
161  double IDMVAOut = m_expert->apply(*m_dataset)[0];
162  B2DEBUG(175, "ECL Expert classification: " << IDMVAOut);
163  klid = m_klids.appendNew();
164  cluster.addRelationTo(klid, IDMVAOut);
165 
166  }// for cluster in clusters
167 } // 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)
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.
ECLExpertModule()
Constructor.
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 identifcation (KlId) datastore object to store results from KlId calculations.
Definition: KlId.h:23
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
Definition: Interface.h:53
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
Definition: Interface.cc:45
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.
Definition: Weightfile.cc:251
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:67
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
Definition: Weightfile.cc:206
Base class for Modules.
Definition: Module.h:72
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
@ 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
const std::string & getName() const
Returns the name of the module.
Definition: Module.h:187
REG_MODULE(arichBtest)
Register the Module.
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:560
Abstract base class for different kinds of events.