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