Belle II Software  light-2212-foldex
LowEnergyPi0VetoExpertModule.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/LowEnergyPi0VetoExpert/LowEnergyPi0VetoExpertModule.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 /* ROOT headers. */
19 #include <Math/Vector3D.h>
20 #include <Math/Vector4D.h>
21 #include <Math/VectorUtil.h>
22 
23 using namespace Belle2;
24 
25 REG_MODULE(LowEnergyPi0VetoExpert);
26 
28 {
29  setDescription("Low-energy pi0 veto.");
30  addParam(
31  "VetoPi0Daughters", m_VetoPi0Daughters,
32  "Veto for pi0 daughters (maximum over all pairs excluding this pi0).",
33  false);
34  addParam("GammaListName", m_GammaListName, "Gamma particle list name.",
35  std::string("gamma"));
36  addParam("Pi0ListName", m_Pi0ListName, "Pi0 particle list name.",
37  std::string("pi0"));
38  addParam("Belle1", m_Belle1, "Belle 1 data analysis.", false);
39  addParam("identifier", m_identifier,
40  "Database identifier or file used to load the weights.",
41  m_identifier);
43 }
44 
46 {
47 }
48 
50 {
51  m_ListGamma.isRequired(m_GammaListName);
53  m_ListPi0.isRequired(m_Pi0ListName);
54  if (not(boost::ends_with(m_identifier, ".root") or boost::ends_with(m_identifier, ".xml"))) {
55  m_weightfile_representation = std::unique_ptr<DBObjPtr<DatabaseRepresentationOfWeightfile>>(new
57  }
59 }
60 
62 {
63  m_expert.reset();
64  m_dataset.reset();
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 
82 {
83 }
84 
86 {
87  auto supported_interfaces = MVA::AbstractInterface::getSupportedInterfaces();
88  MVA::GeneralOptions general_options;
89  weightfile.getOptions(general_options);
90  weightfile.addSignalFraction(0.5);
91  m_expert = supported_interfaces[general_options.m_method]->getExpert();
92  m_expert->load(weightfile);
93  std::vector<float> dummy;
94  /* The number of input variables depends on the experiment. */
95  int nInputVariables;
96  if (m_Belle1)
97  nInputVariables = 7;
98  else
99  nInputVariables = 11;
100  dummy.resize(nInputVariables, 0);
101  m_dataset = std::unique_ptr<MVA::SingleDataset>(new MVA::SingleDataset(general_options, std::move(dummy), 0));
102 }
103 
105  const Particle* pi0Gamma)
106 {
107  float maxVeto = 0;
108  int n = m_ListGamma->getListSize();
109  for (int i = 0; i < n; ++i) {
110  Particle* gamma2 = m_ListGamma->getParticle(i);
111  if (gamma1 == gamma2)
112  continue;
113  if (pi0Gamma != nullptr) {
114  if (pi0Gamma == gamma2)
115  continue;
116  }
117  double pi0Mass = (gamma1->get4Vector() + gamma2->get4Vector()).M();
118  if (pi0Mass < 0.07 || pi0Mass > 0.20)
119  continue;
120  const Particle* gammaLowEnergy, *gammaHighEnergy;
121  if (gamma1->getEnergy() > gamma2->getEnergy()) {
122  gammaLowEnergy = gamma2;
123  gammaHighEnergy = gamma1;
124  } else {
125  gammaLowEnergy = gamma1;
126  gammaHighEnergy = gamma2;
127  }
128  double gammaLowEnergyEnergy, gammaHighEnergyEnergy;
129  double gammaLowEnergyE9E21, gammaHighEnergyE9E21;
130  double gammaLowEnergyClusterTheta, gammaHighEnergyClusterTheta;
131  double gammaLowEnergyZernikeMVA, gammaHighEnergyZernikeMVA;
132  double gammaLowEnergyIsolation, gammaHighEnergyIsolation;
133  double cosHelicityAngleMomentum;
134  gammaLowEnergyEnergy = gammaLowEnergy->getEnergy();
135  gammaHighEnergyEnergy = gammaHighEnergy->getEnergy();
136  ROOT::Math::PxPyPzEVector gammaHighEnergyMomentum(
137  gammaHighEnergy->getPx(), gammaHighEnergy->getPy(),
138  gammaHighEnergy->getPz(), gammaHighEnergyEnergy);
139  ROOT::Math::PxPyPzEVector gammaLowEnergyMomentum(
140  gammaLowEnergy->getPx(), gammaLowEnergy->getPy(),
141  gammaLowEnergy->getPz(), gammaLowEnergyEnergy);
142  ROOT::Math::PxPyPzEVector momentum = gammaHighEnergyMomentum +
143  gammaLowEnergyMomentum;
144  ROOT::Math::XYZVector boost = momentum.BoostToCM();
145  gammaHighEnergyMomentum =
146  ROOT::Math::VectorUtil::boost(gammaHighEnergyMomentum, boost);
147  cosHelicityAngleMomentum =
148  fabs(ROOT::Math::VectorUtil::CosTheta(momentum.Vect(),
149  gammaHighEnergyMomentum.Vect()));
150  gammaLowEnergyE9E21 = Variable::eclClusterE9E21(gammaLowEnergy);
151  gammaHighEnergyE9E21 = Variable::eclClusterE9E21(gammaHighEnergy);
152  gammaLowEnergyClusterTheta = Variable::eclClusterTheta(gammaLowEnergy);
153  gammaHighEnergyClusterTheta = Variable::eclClusterTheta(gammaHighEnergy);
154  if (!m_Belle1) {
155  gammaLowEnergyZernikeMVA =
156  Variable::eclClusterZernikeMVA(gammaLowEnergy);
157  gammaHighEnergyZernikeMVA =
158  Variable::eclClusterZernikeMVA(gammaHighEnergy);
159  gammaLowEnergyIsolation = Variable::eclClusterIsolation(gammaLowEnergy);
160  gammaHighEnergyIsolation =
161  Variable::eclClusterIsolation(gammaHighEnergy);
162  }
163  m_dataset->m_input[0] = gammaLowEnergyEnergy;
164  m_dataset->m_input[1] = pi0Mass;
165  m_dataset->m_input[2] = cosHelicityAngleMomentum;
166  m_dataset->m_input[3] = gammaLowEnergyE9E21;
167  m_dataset->m_input[4] = gammaHighEnergyE9E21;
168  m_dataset->m_input[5] = gammaLowEnergyClusterTheta;
169  m_dataset->m_input[6] = gammaHighEnergyClusterTheta;
170  if (!m_Belle1) {
171  m_dataset->m_input[7] = gammaLowEnergyZernikeMVA;
172  m_dataset->m_input[8] = gammaHighEnergyZernikeMVA;
173  m_dataset->m_input[9] = gammaLowEnergyIsolation;
174  m_dataset->m_input[10] = gammaHighEnergyIsolation;
175  }
176  float veto = m_expert->apply(*m_dataset)[0];
177  if (veto > maxVeto)
178  maxVeto = veto;
179  }
180  return maxVeto;
181 }
182 
184 {
185  if (m_VetoPi0Daughters) {
186  int n = m_ListPi0->getListSize();
187  for (int i = 0; i < n; ++i) {
188  Particle* pi0 = m_ListPi0->getParticle(i);
189  const Particle* gamma1 = pi0->getDaughter(0);
190  const Particle* gamma2 = pi0->getDaughter(1);
191  const Particle* gammaLowEnergy, *gammaHighEnergy;
192  if (gamma1->getEnergy() > gamma2->getEnergy()) {
193  gammaLowEnergy = gamma2;
194  gammaHighEnergy = gamma1;
195  } else {
196  gammaLowEnergy = gamma1;
197  gammaHighEnergy = gamma2;
198  }
199  float maxVeto = getMaximumVeto(gammaLowEnergy, gammaHighEnergy);
200  pi0->addExtraInfo("lowEnergyPi0VetoGammaLowEnergy", maxVeto);
201  maxVeto = getMaximumVeto(gammaHighEnergy, gammaLowEnergy);
202  pi0->addExtraInfo("lowEnergyPi0VetoGammaHighEnergy", maxVeto);
203  }
204  } else {
205  int n = m_ListGamma->getListSize();
206  for (int i = 0; i < n; ++i) {
207  Particle* gamma = m_ListGamma->getParticle(i);
208  float maxVeto = getMaximumVeto(gamma, nullptr);
209  gamma->addExtraInfo("lowEnergyPi0Veto", maxVeto);
210  }
211  }
212 }
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
StoreObjPtr< ParticleList > m_ListGamma
Gamma candidates.
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.
bool m_VetoPi0Daughters
Calculate veto for pi0 daughter photons (maximum over all pairs excluding this pi0).
StoreObjPtr< ParticleList > m_ListPi0
Pi0 candidates.
void init_mva(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features Called everytime the weightfile in the database changes i...
std::string m_GammaListName
Gamma particle list name.
std::string m_Pi0ListName
Pi0 particle list name.
float getMaximumVeto(const Particle *gamma1, const Particle *pi0Gamma)
Get maximum veto value over all gamma pairs including the photon gamma1.
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 getPx() const
Returns x component of momentum.
Definition: Particle.h:568
double getPz() const
Returns z component of momentum.
Definition: Particle.h:586
double getEnergy() const
Returns total energy.
Definition: Particle.h:522
double getPy() const
Returns y component of momentum.
Definition: Particle.h:577
ROOT::Math::PxPyPzEVector get4Vector() const
Returns Lorentz vector.
Definition: Particle.h:532
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
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