Belle II Software  release-05-02-19
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  addParam("stableParticles", m_stableParticles, "if true apply the selection criteria for stable particles in the generator", false);
42 
43  addParam("nPhotonMin", m_nPhotonMin, "minimum number of photons", 0);
44  addParam("nPhotonMax", m_nPhotonMax, "maximum number of photons", 999);
45  addParam("MinPhotonEnergy", m_MinPhotonEnergy, "minimum photon energy [GeV]", -1.);
46  addParam("MinPhotonTheta", m_MinPhotonTheta, "minimum polar angle of photon [deg]", 15.);
47  addParam("MaxPhotonTheta", m_MaxPhotonTheta, "maximum polar angle of photon [deg]", 165.);
48 }
49 
50 void GeneratorPreselectionModule::initialize()
51 {
52  B2DEBUG(29, "GeneratorPreselectionModule initialize");
53 
54  //convert limits to radian
55  m_MinChargedTheta *= Unit::deg;
56  m_MaxChargedTheta *= Unit::deg;
57  m_MinPhotonTheta *= Unit::deg;
58  m_MaxPhotonTheta *= Unit::deg;
59 
60  m_mcparticles.isRequired(m_particleList);
61  if (m_applyInCMS) {
62  m_initial.isRequired();
63  }
64 }
65 
66 
67 void GeneratorPreselectionModule::event()
68 {
69  m_nCharged = 0;
70  m_nPhoton = 0.;
71 
72  for (int i = 0; i < m_mcparticles.getEntries(); i++) {
73  MCParticle& mc = *m_mcparticles[i];
74  checkParticle(mc);
75  }
76 
77  //check number of particles passing the cuts
78  B2DEBUG(250, "number of charged passing cuts: " << m_nCharged);
79  B2DEBUG(250, "number of photons passing cuts: " << m_nPhoton);
80 
81  //set return value
82  int retvalue = 0;
83  setReturnValue(retvalue);
84  if (m_nCharged >= m_nChargedMin && m_nCharged <= m_nChargedMax) {
85  retvalue = 1;
86  }
87  if (m_nPhoton >= m_nPhotonMin && m_nPhoton <= m_nPhotonMax) {
88  retvalue += 10;
89  }
90 
91  B2DEBUG(250, "return value: " << retvalue);
92  setReturnValue(retvalue);
93 
94  if (m_resultCounter.find(retvalue) == m_resultCounter.end()) {
95  m_resultCounter[retvalue] = 1;
96  } else {
97  m_resultCounter[retvalue] += 1;
98  }
99 }
100 
101 void GeneratorPreselectionModule::checkParticle(const MCParticle& mc)
102 {
103  if (!mc.hasStatus(MCParticle::c_PrimaryParticle)) return;
104  if (mc.hasStatus(MCParticle::c_Initial) or mc.hasStatus(MCParticle::c_IsVirtual)) return;
105  if (m_stableParticles)
106  if (!mc.hasStatus(MCParticle::c_StableInGenerator)) return;
107 
108  const TVector3& p = mc.getMomentum();
109  double energy = mc.getEnergy();
110  double mom = p.Mag();
111  double theta = p.Theta();
112 
113  if (m_applyInCMS) {
114  const TLorentzVector p_cms = m_initial->getLabToCMS() * mc.get4Vector();
115  energy = p_cms.E();
116  mom = p_cms.P();
117  theta = p_cms.Theta();
118  }
119 
120  if (mc.getPDG() == 22) {
121  B2DEBUG(250, "E = " << energy << " theta=" << theta << " thetamin=" << m_MinPhotonTheta << " thetamax=" << m_MaxPhotonTheta);
122  if (energy >= m_MinPhotonEnergy && theta >= m_MinPhotonTheta && theta <= m_MaxPhotonTheta) {
123  m_nPhoton++;
124  }
125  }
126 
127  if (abs(mc.getCharge()) > 0.) {
128  B2DEBUG(250, "pt = " << p.Pt() << " p=" << mom << " theta=" << theta << " thetamin=" << m_MinChargedTheta << " thetamax=" <<
129  m_MaxChargedTheta);
130  if (mom >= m_MinChargedP && p.Pt() >= m_MinChargedPt && theta >= m_MinChargedTheta && theta <= m_MaxChargedTheta) {
131  m_nCharged++;
132  }
133  }
134 
135 }
136 
137 void GeneratorPreselectionModule::terminate()
138 {
139  B2RESULT("Final results of the preselection module:");
140  for (const auto& finalResult : m_resultCounter) {
141  B2RESULT("\tPreselection with result " << finalResult.first << ": " << finalResult.second << " times.");
142  }
143  const unsigned int sumCounters = std::accumulate(m_resultCounter.begin(), m_resultCounter.end(), 0, [](auto lhs, auto rhs) {
144  return lhs + rhs.second;
145  });
146 
147  B2RESULT("Total number of tested events: " << sumCounters);
148 }
149 
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