Belle II Software  release-08-01-10
BelleNbarMVAModule.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 // Own header
10 #include <analysis/modules/BelleNbarMVAModule/BelleNbarMVAModule.h>
11 
12 //Basf2 headers
13 #include <framework/database/DBObjPtr.h>
14 #include <framework/database/Database.h>
15 #include <mva/dataobjects/DatabaseRepresentationOfWeightfile.h>
16 #include <mdst/dataobjects/ECLCluster.h>
17 
18 // C++ headers
19 #include <string>
20 #include <vector>
21 
22 // frugally-deep header
23 #define FDEEP_FLOAT_TYPE double
24 #include <fdeep/fdeep.hpp>
25 
26 using namespace Belle2;
27 
28 REG_MODULE(BelleNbarMVA);
29 
31 {
33  R"DOC(Apply nbarMVA for Belle I, a DNN trained with ECLCluster variables for discrimination of anti-neutrons against photons. Anti-neutron-like particles have scores closer to 1; photon-like particles have scores closer to 0.)DOC");
34  addParam("identifier", m_identifier, "Identifier of the MVA", std::string(""));
35  addParam("particleList", m_particleList, "ParticleList to apply the MVA", std::string(""));
36 }
37 
39 {
40  m_model = std::make_unique<fdeep::model>(fdeep::read_model_from_string(((DatabaseRepresentationOfWeightfile*)
41  Database::Instance().getData(m_identifier, 0, 0))->m_data));
42  m_plist.isRequired(m_particleList);
43 }
44 
46 {
47  for (Particle& particle : *m_plist) {
48  const ECLCluster* cluster = particle.getECLCluster();
49  std::vector<double> v{
50  cluster->getEnergy(ECLCluster::EHypothesisBit::c_nPhotons),
51  cluster->getE9oE21(),
52  cluster->getEnergyHighestCrystal(),
53  cluster->getNumberOfCrystals(),
54  cluster->getLAT()
55  };
56  particle.addExtraInfo("nbarMVA", double(m_model->predict_single_output({fdeep::tensor(fdeep::tensor_shape(5), v)})));
57  }
58 }
59 
void initialize() override
Load the weight file.
void event() override
Perform the calculation here.
std::unique_ptr< fdeep::model > m_model
Model of the MVA.
std::string m_particleList
Name of the ParticleList to apply the MVA.
StoreObjPtr< ParticleList > m_plist
ParticeList to apply the MVA.
BelleNbarMVAModule()
Constructor: Sets the description, the properties and the parameters of the module.
std::string m_identifier
Identifier of the MVA.
Database representation of a Weightfile object.
ECL cluster data.
Definition: ECLCluster.h:27
@ c_nPhotons
CR is split into n photons (N1)
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class to store reconstructed particles.
Definition: Particle.h:75
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
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42
Abstract base class for different kinds of events.