Belle II Software  light-2212-foldex
LowEnergyPi0IdentificationExpertModule.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/LowEnergyPi0IdentificationExpert/LowEnergyPi0IdentificationExpertModule.h>
11 
12 /* Belle 2 headers. */
13 #include <analysis/variables/ECLVariables.h>
14 #include <analysis/variables/HelicityVariables.h>
15 #include <mva/interface/Interface.h>
16 #include <boost/algorithm/string/predicate.hpp>
17 
18 using namespace Belle2;
19 
20 REG_MODULE(LowEnergyPi0IdentificationExpert);
21 
23 {
24  setDescription("Low-energy pi0 identification.");
25  addParam("Pi0ListName", m_Pi0ListName, "Pi0 particle list name.",
26  std::string("pi0"));
27  addParam("Belle1", m_Belle1, "Belle 1 data analysis.", false);
28  addParam("identifier", m_identifier,
29  "Database identifier or file used to load the weights.",
30  m_identifier);
32 }
33 
35 {
36 }
37 
39 {
40  m_ListPi0.isRequired(m_Pi0ListName);
41  if (not(boost::ends_with(m_identifier, ".root") or boost::ends_with(m_identifier, ".xml"))) {
42  m_weightfile_representation = std::unique_ptr<DBObjPtr<DatabaseRepresentationOfWeightfile>>(new
44  }
46 }
47 
49 {
50  m_expert.reset();
51  m_dataset.reset();
52 }
53 
55 {
57  if (m_weightfile_representation->hasChanged()) {
58  std::stringstream ss((*m_weightfile_representation)->m_data);
59  auto weightfile = MVA::Weightfile::loadFromStream(ss);
60  init_mva(weightfile);
61  }
62  } else {
64  init_mva(weightfile);
65  }
66 }
67 
69 {
70 }
71 
73 {
74  auto supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
75  MVA::GeneralOptions general_options;
76  weightfile.getOptions(general_options);
77  weightfile.addSignalFraction(0.5);
78  m_expert = supported_interfaces[general_options.m_method]->getExpert();
79  m_expert->load(weightfile);
80  std::vector<float> dummy;
81  /* The number of input variables depends on the experiment. */
82  int nInputVariables;
83  if (m_Belle1)
84  nInputVariables = 6;
85  else
86  nInputVariables = 10;
87  dummy.resize(nInputVariables, 0);
88  m_dataset = std::unique_ptr<MVA::SingleDataset>(new MVA::SingleDataset(general_options, std::move(dummy), 0));
89 }
90 
92 {
93  int n = m_ListPi0->getListSize();
94  for (int i = 0; i < n; ++i) {
95  Particle* pi0 = m_ListPi0->getParticle(i);
96  const Particle* gamma1 = pi0->getDaughter(0);
97  const Particle* gamma2 = pi0->getDaughter(1);
98  const Particle* gammaLowEnergy, *gammaHighEnergy;
99  if (gamma1->getEnergy() > gamma2->getEnergy()) {
100  gammaLowEnergy = gamma2;
101  gammaHighEnergy = gamma1;
102  } else {
103  gammaLowEnergy = gamma1;
104  gammaHighEnergy = gamma2;
105  }
106  double gammaLowEnergyPi0Veto, gammaHighEnergyPi0Veto;
107  double gammaLowEnergyE9E21, gammaHighEnergyE9E21;
108  double gammaLowEnergyClusterTheta, gammaHighEnergyClusterTheta;
109  double gammaLowEnergyZernikeMVA, gammaHighEnergyZernikeMVA;
110  double gammaLowEnergyIsolation, gammaHighEnergyIsolation;
111  gammaLowEnergyPi0Veto = pi0->getExtraInfo("lowEnergyPi0VetoGammaLowEnergy");
112  gammaHighEnergyPi0Veto =
113  pi0->getExtraInfo("lowEnergyPi0VetoGammaHighEnergy");
114  gammaLowEnergyE9E21 = Variable::eclClusterE9E21(gammaLowEnergy);
115  gammaHighEnergyE9E21 = Variable::eclClusterE9E21(gammaHighEnergy);
116  gammaLowEnergyClusterTheta = Variable::eclClusterTheta(gammaLowEnergy);
117  gammaHighEnergyClusterTheta = Variable::eclClusterTheta(gammaHighEnergy);
118  if (!m_Belle1) {
119  gammaLowEnergyZernikeMVA = Variable::eclClusterZernikeMVA(gammaLowEnergy);
120  gammaHighEnergyZernikeMVA =
121  Variable::eclClusterZernikeMVA(gammaHighEnergy);
122  gammaLowEnergyIsolation = Variable::eclClusterIsolation(gammaLowEnergy);
123  gammaHighEnergyIsolation = Variable::eclClusterIsolation(gammaHighEnergy);
124  }
125  m_dataset->m_input[0] = gammaLowEnergyPi0Veto;
126  m_dataset->m_input[1] = gammaHighEnergyPi0Veto;
127  m_dataset->m_input[2] = gammaLowEnergyE9E21;
128  m_dataset->m_input[3] = gammaHighEnergyE9E21;
129  m_dataset->m_input[4] = gammaLowEnergyClusterTheta;
130  m_dataset->m_input[5] = gammaHighEnergyClusterTheta;
131  if (!m_Belle1) {
132  m_dataset->m_input[6] = gammaLowEnergyZernikeMVA;
133  m_dataset->m_input[7] = gammaHighEnergyZernikeMVA;
134  m_dataset->m_input[8] = gammaLowEnergyIsolation;
135  m_dataset->m_input[9] = gammaHighEnergyIsolation;
136  }
137  float identification = m_expert->apply(*m_dataset)[0];
138  pi0->addExtraInfo("lowEnergyPi0Identification", identification);
139  }
140 }
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
void event() override
This method is called for each event.
void endRun() override
This method is called if the current run ends.
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.
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the database representation of the weightfile.
void beginRun() override
Called when entering a new run.
void init_mva(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features Called everytime the weightfile in the database changes i...
std::string m_identifier
Database identifier or file used to load the weights.
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
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.
Definition: Weightfile.cc:250
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:66
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
Definition: Weightfile.cc:205
void addSignalFraction(float signal_fraction)
Saves the signal fraction in the xml tree.
Definition: Weightfile.cc:94
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
Class to store reconstructed particles.
Definition: Particle.h:74
double getEnergy() const
Returns total energy.
Definition: Particle.h:522
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1363
const Particle * getDaughter(unsigned i) const
Returns a pointer to the i-th daughter particle.
Definition: Particle.cc:663
double getExtraInfo(const std::string &name) const
Return given value if set.
Definition: Particle.cc:1316
REG_MODULE(B2BIIConvertBeamParams)
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.
Definition: ClusterUtils.h:23