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