Belle II Software  release-08-01-10
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 <analysis/utility/PCmsLabTransform.h>
12 #include <framework/gearbox/Unit.h>
13 #include <numeric>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 //-----------------------------------------------------------------
19 // Register the Module
20 //-----------------------------------------------------------------
21 REG_MODULE(GeneratorPreselection);
22 
23 //-----------------------------------------------------------------
24 // Implementation
25 //-----------------------------------------------------------------
26 
27 GeneratorPreselectionModule::GeneratorPreselectionModule() : Module()
28 {
29  // Set module properties
30  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 
33  addParam("nChargedMin", m_nChargedMin, "minimum number of charged particles", 0);
34  addParam("nChargedMax", m_nChargedMax, "maximum number of charged particles", 999);
35  addParam("MinChargedP", m_MinChargedP, "minimum charged momentum [GeV]", 0.25);
36  addParam("MinChargedPt", m_MinChargedPt, "minimum charged transverse momentum (pt) [GeV]", 0.1);
37  addParam("MinChargedTheta", m_MinChargedTheta, "minimum polar angle of charged particle [deg]", 17.);
38  addParam("MaxChargedTheta", m_MaxChargedTheta, "maximum polar angle of charged particle [deg]", 150.);
39  addParam("applyInCMS", m_applyInCMS, "if true apply the P,Pt,theta, and energy cuts in the center of mass frame", false);
40  addParam("stableParticles", m_stableParticles, "if true apply the selection criteria for stable particles in the generator", 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 
50 {
51  B2DEBUG(29, "GeneratorPreselectionModule initialize");
52 
53  //convert limits to radian
58 
60  if (m_applyInCMS) {
61  m_initial.isRequired();
62  }
63 }
64 
65 
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);
84  retvalue = 1;
85  }
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 
101 {
102  if (!mc.hasStatus(MCParticle::c_PrimaryParticle)) return;
103  if (mc.hasStatus(MCParticle::c_Initial) or mc.hasStatus(MCParticle::c_IsVirtual)) return;
104  if (m_stableParticles)
105  if (!mc.hasStatus(MCParticle::c_StableInGenerator)) return;
106 
107  const ROOT::Math::XYZVector& p = mc.getMomentum();
108  double energy = mc.getEnergy();
109  double mom = p.R();
110  double theta = p.Theta();
111 
112  if (m_applyInCMS) {
113  const ROOT::Math::PxPyPzEVector p_cms = PCmsLabTransform::labToCms(mc.get4Vector());
114  energy = p_cms.E();
115  mom = p_cms.P();
116  theta = p_cms.Theta();
117  }
118 
119  if (mc.getPDG() == 22) {
120  B2DEBUG(250, "E = " << energy << " theta=" << theta << " thetamin=" << m_MinPhotonTheta << " thetamax=" << m_MaxPhotonTheta);
121  if (energy >= m_MinPhotonEnergy && theta >= m_MinPhotonTheta && theta <= m_MaxPhotonTheta) {
122  m_nPhoton++;
123  }
124  }
125 
126  if (abs(mc.getCharge()) > 0.) {
127  B2DEBUG(250, "pt = " << p.Rho() << " p=" << mom << " theta=" << theta << " thetamin=" << m_MinChargedTheta << " thetamax=" <<
129  if (mom >= m_MinChargedP && p.Rho() >= m_MinChargedPt && theta >= m_MinChargedTheta && theta <= m_MaxChargedTheta) {
130  m_nCharged++;
131  }
132  }
133 
134 }
135 
137 {
138  B2RESULT("Final results of the preselection module:");
139  for (const auto& finalResult : m_resultCounter) {
140  B2RESULT("\tPreselection with result " << finalResult.first << ": " << finalResult.second << " times.");
141  }
142  const unsigned int sumCounters = std::accumulate(m_resultCounter.begin(), m_resultCounter.end(), 0, [](auto lhs, auto rhs) {
143  return lhs + rhs.second;
144  });
145 
146  B2RESULT("Total number of tested events: " << sumCounters);
147 }
148 
bool m_stableParticles
if true apply the selection criteria for only stable particles in the generator
void initialize() override
Initialize the parameters.
int m_nPhotonMin
minimum number of photons.
double m_MaxPhotonTheta
maximum theta for each photon.
double m_MinPhotonTheta
minimum theta for each photon.
double m_MaxChargedTheta
maximum theta for each charged particle.
StoreArray< MCParticle > m_mcparticles
store array for the MCParticles
void terminate() override
Print the results of the cuts.
double m_MinChargedP
selection criteria for charged
double m_MinChargedPt
minimum pT for each charged particle.
int m_nChargedMin
minimum number of charged particles.
bool m_applyInCMS
if true apply the selection criteria for charged in the center of mass system
int m_nPhotonMax
minimum number of photons.
void checkParticle(const MCParticle &mc)
called for each particle, checks for cuts.
StoreObjPtr< MCInitialParticles > m_initial
pointer to the actual beam parameters
std::string m_particleList
The name of the MCParticle collection.
std::map< double, unsigned int > m_resultCounter
final result
double m_MinPhotonEnergy
selection criteria for photons
double m_MinChargedTheta
minimum theta for each charged particle.
int m_nChargedMax
minimum number of charged particles.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
@ c_Initial
bit 5: Particle is initial such as e+ or e- and not going to Geant4
Definition: MCParticle.h:57
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:47
@ c_IsVirtual
bit 4: Particle is virtual and not going to Geant4.
Definition: MCParticle.h:55
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
Definition: MCParticle.h:49
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:220
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
static ROOT::Math::PxPyPzMVector labToCms(const ROOT::Math::PxPyPzMVector &vec)
Transforms Lorentz vector into CM System.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
static const double deg
degree to radians
Definition: Unit.h:109
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#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.