Belle II Software  release-05-01-25
BelleBremRecoveryModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributor : Soumen Halder and Saurabh Sandilya *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <analysis/modules/BremsCorrection/BelleBremRecoveryModule.h>
13 // framework aux
14 #include <framework/logging/Logger.h>
15 #include <framework/datastore/RelationArray.h>
16 #include <framework/datastore/StoreArray.h>
17 
18 // dataobjects
19 #include <analysis/dataobjects/Particle.h>
20 #include <mdst/dataobjects/MCParticle.h>
21 #include <mdst/dataobjects/PIDLikelihood.h>
22 #include <mdst/dataobjects/Track.h>
23 
24 // utilities
25 #include <analysis/DecayDescriptor/ParticleListName.h>
26 
27 // variables
28 #include <analysis/variables/ECLVariables.h>
29 
30 #include <algorithm>
31 #include <TMatrixFSym.h>
32 
33 #include <vector>
34 using namespace std;
35 
36 namespace Belle2 {
42 //-----------------------------------------------------------------
43 // Register module
44 //-----------------------------------------------------------------
45 
46  REG_MODULE(BelleBremRecovery)
47 
48 //-----------------------------------------------------------------
49 // Implementation
50 //-----------------------------------------------------------------
51 
53  Module(), m_pdgCode(0)
54 
55  {
56  // set module description (e.g. insert text)
57  setDescription(R"DOC(
58  Takes the charged particle from the given charged particle list(``inputListName``) and
59  copies them to the output list(``outputListName``) and adds the 4-vector of nearest(all) photon(s)
60  from ``gammaListName`` (considered as radiative) to the charged particle, if the given
61  criteria for maximum angle(``angleThreshold``) and minimum energy(``minimumEnergy``) are fulfilled.
62  )DOC");
63  setPropertyFlags(c_ParallelProcessingCertified);
64 
65  // Add parameters
66  addParam("inputListName", m_inputListName,
67  "The initial charged particle list containing the charged particles to correct, should already exist.");
68  addParam("outputListName", m_outputListName, "The output charged particle list containing the corrected charged particles.");
69  addParam("gammaListName", m_gammaListName, "The gammas list containing possibly radiative gammas, should already exist.");
70  addParam("angleThreshold", m_angleThres,
71  "The maximum angle in radians between the charged particle and the (radiative) gamma to be accepted.", 0.05);
72  addParam("minimumEnergy", m_minimumEnergy, "The minimum energy in GeV of the (radiative) gamma to be accepted.", 0.05);
73  addParam("multiplePhotons", m_isMultiPho, "If only the nearest photon to add then make it False otherwise true", true);
74  addParam("writeOut", m_writeOut,
75  "Set to false if you want to add only the nearest photon, otherwise all eligible photons will be added", false);
76 
77  }
78 
79  void BelleBremRecoveryModule::initialize()
80  {
81  // check the validity of output ParticleList name
82  bool valid = m_decaydescriptor.init(m_outputListName);
83  if (!valid)
84  B2ERROR("[BelleBremRecoveryModule] Invalid output ParticleList name: " << m_outputListName);
85 
86  // output particle
87  const DecayDescriptorParticle* mother = m_decaydescriptor.getMother();
88  m_pdgCode = mother->getPDGCode();
89  m_outputAntiListName = ParticleListName::antiParticleListName(m_outputListName);
90 
91  // get existing particle lists
92  if (m_inputListName == m_outputListName) {
93  B2ERROR("[BelleBremRecoveryModule] Input and output particle list names are the same: " << m_inputListName);
94  } else if (!m_decaydescriptor.init(m_inputListName)) {
95  B2ERROR("[BelleBremRecoveryModule] Invalid input particle list name: " << m_inputListName);
96  } else {
97  m_inputparticleList.isRequired(m_inputListName);
98  }
99 
100  if (!m_decaydescriptorGamma.init(m_gammaListName)) {
101  B2ERROR("[BelleBremRecoveryModule] Invalid gamma particle list name: " << m_gammaListName);
102  } else {
103  m_gammaList.isRequired(m_gammaListName);
104  }
105 
106  // make output list
107  DataStore::EStoreFlags flags = m_writeOut ? DataStore::c_WriteOut : DataStore::c_DontWriteOut;
108  m_outputparticleList.registerInDataStore(m_outputListName, flags);
109  m_outputAntiparticleList.registerInDataStore(m_outputAntiListName, flags);
110 
111  StoreArray<Particle> particles;
112  StoreArray<PIDLikelihood> pidlikelihoods;
113  particles.registerRelationTo(pidlikelihoods);
114  }
115 
116 
117  void BelleBremRecoveryModule::event()
118  {
119  StoreArray<Particle> particles;
120  StoreArray<MCParticle> mcParticles;
121 
122  RelationArray particlesToMCParticles(particles, mcParticles);
123 
124  // new output particle list
125  m_outputparticleList.create();
126  m_outputparticleList->initialize(m_pdgCode, m_outputListName);
127 
128  m_outputAntiparticleList.create();
129  m_outputAntiparticleList->initialize(-1 * m_pdgCode, m_outputAntiListName);
130  m_outputAntiparticleList->bindAntiParticleList(*(m_outputparticleList));
131 
132  // loop over charged particles, correct them and add them to the output list
133  const unsigned int nLep = m_inputparticleList->getListSize();
134  const unsigned int nGam = m_gammaList->getListSize();
135  for (unsigned i = 0; i < nLep; i++) {
136  const Particle* lepton = m_inputparticleList->getParticle(i);
137  TLorentzVector lepton4Vector = lepton->get4Vector();
138  TLorentzVector new4Vec = lepton->get4Vector();
139  std::vector<Particle*> selectedGammas;
140  double bremsGammaEnergySum = 0.0;
141  // look for all possible (radiative) gamma
142  for (unsigned j = 0; j < nGam; j++) {
143  Particle* gamma = m_gammaList->getParticle(j);
144  // check if gamma energy is within allowed energy range
145  if (gamma->getEnergy() < m_minimumEnergy) continue;
146  // get angle (in lab system)
147  TVector3 pi = lepton->getMomentum();
148  TVector3 pj = gamma->getMomentum();
149  double angle = (pi.Angle(pj));
150  //Instead of first-come-first-serve, serve all charged particle equally.
151  // https://indico.belle2.org/event/946/contributions/4007/attachments/1946/2967/SCunliffe190827.pdf
152  if (m_angleThres > angle) {
153  gamma->writeExtraInfo("theta_e_gamma", angle);
154  selectedGammas.push_back(gamma);
155  }
156  }
157  //sorting the bremphotons in ascending order of the angle with the charged particle
158  std::sort(selectedGammas.begin(), selectedGammas.end(), [](const Particle * photon1, const Particle * photon2) {
159  return photon1->getExtraInfo("theta_e_gamma") < photon2->getExtraInfo("theta_e_gamma");
160  });
161  //Preparing 4-momentum vector of charged particle by adding bremphoton momenta
162  for (auto const& fsrgamma : selectedGammas) {
163  new4Vec += fsrgamma->get4Vector();
164  bremsGammaEnergySum += Variable::eclClusterE(fsrgamma);
165  if (!m_isMultiPho) break;
166  }
167  Particle correctedLepton(new4Vec, lepton->getPDGCode(), Particle::EFlavorType::c_Flavored, Particle::c_Track,
168  lepton->getTrack()->getArrayIndex());
169  correctedLepton.appendDaughter(lepton, false);
170 
171  //Calculating matrix element of corrected charged particle
172  const TMatrixFSym& lepErrorMatrix = lepton->getMomentumVertexErrorMatrix();
173  TMatrixFSym corLepMatrix = lepErrorMatrix;
174  for (auto const& fsrgamma : selectedGammas) {
175  const TMatrixFSym& fsrErrorMatrix = fsrgamma->getMomentumVertexErrorMatrix();
176  for (int irow = 0; irow <= 3 ; irow++)
177  for (int icol = irow; icol <= 3; icol++)
178  corLepMatrix(irow, icol) += fsrErrorMatrix(irow, icol);
179  correctedLepton.appendDaughter(fsrgamma, false);
180  B2DEBUG(19, "[BelleBremRecoveryModule] Found a radiative gamma and added its 4-vector to the charge particle");
181  if (!m_isMultiPho) break;
182  }
183  correctedLepton.setMomentumVertexErrorMatrix(corLepMatrix);
184  correctedLepton.setVertex(lepton->getVertex());
185  correctedLepton.setPValue(lepton->getPValue());
186  correctedLepton.addExtraInfo("bremsCorrected", float(selectedGammas.size() > 0));
187  correctedLepton.addExtraInfo("bremsCorrectedPhotonEnergy", bremsGammaEnergySum);
188  // add the mc relation
189  Particle* newLepton = particles.appendNew(correctedLepton);
190  const MCParticle* mcLepton = lepton->getRelated<MCParticle>();
191  const PIDLikelihood* pid = lepton->getPIDLikelihood();
192  if (pid)
193  newLepton->addRelationTo(pid);
194  if (mcLepton != nullptr) newLepton->addRelationTo(mcLepton);
195  m_outputparticleList->addParticle(newLepton);
196  }
197  }
199 } // end Belle2 namespace
200 
Belle2::Particle::getPDGCode
int getPDGCode(void) const
Returns PDG code.
Definition: Particle.h:380
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::Particle::getVertex
TVector3 getVertex() const
Returns vertex position (POCA for charged, IP for neutral FS particles)
Definition: Particle.h:529
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Particle::addExtraInfo
void addExtraInfo(const std::string &name, float value)
Sets the user-defined data of given name to the given value.
Definition: Particle.cc:1224
Belle2::DataStore::EStoreFlags
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:71
Belle2::DecayDescriptorParticle
Represents a particle in the DecayDescriptor.
Definition: DecayDescriptorParticle.h:37
Belle2::RelationsInterface::addRelationTo
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
Definition: RelationsObject.h:144
Belle2::RelationsInterface::getRelated
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
Definition: RelationsObject.h:280
Belle2::PIDLikelihood
Class to collect log likelihoods from TOP, ARICH, dEdx, ECL and KLM aimed for output to mdst includes...
Definition: PIDLikelihood.h:37
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Particle::getPIDLikelihood
const PIDLikelihood * getPIDLikelihood() const
Returns the pointer to the PIDLikelihood object that is related to the Track, which was used to creat...
Definition: Particle.cc:796
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Particle::getMomentumVertexErrorMatrix
TMatrixFSym getMomentumVertexErrorMatrix() const
Returns 7x7 error matrix.
Definition: Particle.cc:386
Belle2::Particle::appendDaughter
void appendDaughter(const Particle *daughter, const bool updateType=true)
Appends index of daughter to daughters index array.
Definition: Particle.cc:633
Belle2::Particle::setMomentumVertexErrorMatrix
void setMomentumVertexErrorMatrix(const TMatrixFSym &errMatrix)
Sets 7x7 error matrix.
Definition: Particle.cc:373
Belle2::Particle::setVertex
void setVertex(const TVector3 &vertex)
Sets position (decay vertex)
Definition: Particle.h:291
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
Belle2::Particle::getMomentum
TVector3 getMomentum() const
Returns momentum vector.
Definition: Particle.h:475
Belle2::Particle::getTrack
const Track * getTrack() const
Returns the pointer to the Track object that was used to create this Particle (ParticleType == c_Trac...
Definition: Particle.cc:770
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< Particle >
Belle2::Particle::get4Vector
TLorentzVector get4Vector() const
Returns Lorentz vector.
Definition: Particle.h:464
Belle2::Particle::setPValue
void setPValue(float pValue)
Sets chi^2 probability of fit.
Definition: Particle.h:308
Belle2::Particle::getPValue
float getPValue() const
Returns chi^2 probability of fit if done or -1.
Definition: Particle.h:565
Belle2::BelleBremRecoveryModule
Brem recovery module (used in past belle analyses) This module add four vector of all the brem photon...
Definition: BelleBremRecoveryModule.h:44