Belle II Software  release-06-02-00
GeneratorPreselectionModule.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 <generators/modules/GeneratorPreselectionModule.h>
11 #include <framework/gearbox/Unit.h>
12 #include <numeric>
13 
14 using namespace std;
15 using namespace Belle2;
16 
17 //-----------------------------------------------------------------
18 // Register the Module
19 //-----------------------------------------------------------------
20 REG_MODULE(GeneratorPreselection)
21 
22 //-----------------------------------------------------------------
23 // Implementation
24 //-----------------------------------------------------------------
25 
27 {
28  // Set module properties
29  setDescription("Preselection based on generator truth information. Returns 0 if no cut have been passed, 1 if only the charged cut has been passed, 10 if only the photon cut has been passed, and 11 if both charged and photon cuts have been passed.");
30  setPropertyFlags(c_ParallelProcessingCertified);
31 
32  addParam("nChargedMin", m_nChargedMin, "minimum number of charged particles", 0);
33  addParam("nChargedMax", m_nChargedMax, "maximum number of charged particles", 999);
34  addParam("MinChargedP", m_MinChargedP, "minimum charged momentum [GeV]", 0.25);
35  addParam("MinChargedPt", m_MinChargedPt, "minimum charged transverse momentum (pt) [GeV]", 0.1);
36  addParam("MinChargedTheta", m_MinChargedTheta, "minimum polar angle of charged particle [deg]", 17.);
37  addParam("MaxChargedTheta", m_MaxChargedTheta, "maximum polar angle of charged particle [deg]", 150.);
38  addParam("applyInCMS", m_applyInCMS, "if true apply the P,Pt,theta, and energy cuts in the center of mass frame", false);
39  addParam("stableParticles", m_stableParticles, "if true apply the selection criteria for stable particles in the generator", false);
40 
41  addParam("nPhotonMin", m_nPhotonMin, "minimum number of photons", 0);
42  addParam("nPhotonMax", m_nPhotonMax, "maximum number of photons", 999);
43  addParam("MinPhotonEnergy", m_MinPhotonEnergy, "minimum photon energy [GeV]", -1.);
44  addParam("MinPhotonTheta", m_MinPhotonTheta, "minimum polar angle of photon [deg]", 15.);
45  addParam("MaxPhotonTheta", m_MaxPhotonTheta, "maximum polar angle of photon [deg]", 165.);
46 }
47 
48 void GeneratorPreselectionModule::initialize()
49 {
50  B2DEBUG(29, "GeneratorPreselectionModule initialize");
51 
52  //convert limits to radian
53  m_MinChargedTheta *= Unit::deg;
54  m_MaxChargedTheta *= Unit::deg;
55  m_MinPhotonTheta *= Unit::deg;
56  m_MaxPhotonTheta *= Unit::deg;
57 
58  m_mcparticles.isRequired(m_particleList);
59  if (m_applyInCMS) {
60  m_initial.isRequired();
61  }
62 }
63 
64 
65 void GeneratorPreselectionModule::event()
66 {
67  m_nCharged = 0;
68  m_nPhoton = 0.;
69 
70  for (int i = 0; i < m_mcparticles.getEntries(); i++) {
71  MCParticle& mc = *m_mcparticles[i];
72  checkParticle(mc);
73  }
74 
75  //check number of particles passing the cuts
76  B2DEBUG(250, "number of charged passing cuts: " << m_nCharged);
77  B2DEBUG(250, "number of photons passing cuts: " << m_nPhoton);
78 
79  //set return value
80  int retvalue = 0;
81  setReturnValue(retvalue);
82  if (m_nCharged >= m_nChargedMin && m_nCharged <= m_nChargedMax) {
83  retvalue = 1;
84  }
85  if (m_nPhoton >= m_nPhotonMin && m_nPhoton <= m_nPhotonMax) {
86  retvalue += 10;
87  }
88 
89  B2DEBUG(250, "return value: " << retvalue);
90  setReturnValue(retvalue);
91 
92  if (m_resultCounter.find(retvalue) == m_resultCounter.end()) {
93  m_resultCounter[retvalue] = 1;
94  } else {
95  m_resultCounter[retvalue] += 1;
96  }
97 }
98 
99 void GeneratorPreselectionModule::checkParticle(const MCParticle& mc)
100 {
101  if (!mc.hasStatus(MCParticle::c_PrimaryParticle)) return;
102  if (mc.hasStatus(MCParticle::c_Initial) or mc.hasStatus(MCParticle::c_IsVirtual)) return;
103  if (m_stableParticles)
104  if (!mc.hasStatus(MCParticle::c_StableInGenerator)) return;
105 
106  const TVector3& p = mc.getMomentum();
107  double energy = mc.getEnergy();
108  double mom = p.Mag();
109  double theta = p.Theta();
110 
111  if (m_applyInCMS) {
112  const TLorentzVector p_cms = m_initial->getLabToCMS() * mc.get4Vector();
113  energy = p_cms.E();
114  mom = p_cms.P();
115  theta = p_cms.Theta();
116  }
117 
118  if (mc.getPDG() == 22) {
119  B2DEBUG(250, "E = " << energy << " theta=" << theta << " thetamin=" << m_MinPhotonTheta << " thetamax=" << m_MaxPhotonTheta);
120  if (energy >= m_MinPhotonEnergy && theta >= m_MinPhotonTheta && theta <= m_MaxPhotonTheta) {
121  m_nPhoton++;
122  }
123  }
124 
125  if (abs(mc.getCharge()) > 0.) {
126  B2DEBUG(250, "pt = " << p.Pt() << " p=" << mom << " theta=" << theta << " thetamin=" << m_MinChargedTheta << " thetamax=" <<
127  m_MaxChargedTheta);
128  if (mom >= m_MinChargedP && p.Pt() >= m_MinChargedPt && theta >= m_MinChargedTheta && theta <= m_MaxChargedTheta) {
129  m_nCharged++;
130  }
131  }
132 
133 }
134 
135 void GeneratorPreselectionModule::terminate()
136 {
137  B2RESULT("Final results of the preselection module:");
138  for (const auto& finalResult : m_resultCounter) {
139  B2RESULT("\tPreselection with result " << finalResult.first << ": " << finalResult.second << " times.");
140  }
141  const unsigned int sumCounters = std::accumulate(m_resultCounter.begin(), m_resultCounter.end(), 0, [](auto lhs, auto rhs) {
142  return lhs + rhs.second;
143  });
144 
145  B2RESULT("Total number of tested events: " << sumCounters);
146 }
147 
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
#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.