Belle II Software  release-06-00-14
ExtModule.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 header. */
10 #include <tracking/modules/ext/ExtModule.h>
11 
12 /* Tracking headers. */
13 #include <tracking/trackExtrapolateG4e/TrackExtrapolateG4e.h>
14 
15 /* Belle 2 headers. */
16 #include <framework/logging/Logger.h>
17 #include <simulation/kernel/ExtManager.h>
18 
19 /* CLHEP headers. */
20 #include <CLHEP/Units/SystemOfUnits.h>
21 
22 /* Geant4 headers. */
23 #include <G4UImanager.hh>
24 
25 /* C++ headers. */
26 #include <vector>
27 
28 using namespace std;
29 using namespace Belle2;
30 
31 REG_MODULE(Ext)
32 
34  Module(),
35  m_MinPt(0.0),
36  m_MinKE(0.0),
37  m_MaxStep(0.0),
38  m_TrackingVerbosity(0),
39  m_EnableVisualization(false),
40  m_MagneticFieldStepperName(""),
41  m_MagneticCacheDistance(0.0),
42  m_DeltaChordInMagneticField(0.0)
43 {
44  m_Extrapolator = TrackExtrapolateG4e::getInstance();
45  m_PDGCodes.clear();
46  m_Hypotheses.clear();
47  m_UICommands.clear();
48  setDescription("Extrapolates tracks from CDC to outer detectors using geant4e");
49  setPropertyFlags(c_ParallelProcessingCertified);
50  addParam("pdgCodes", m_PDGCodes, "Positive-charge PDG codes for extrapolation hypotheses", m_PDGCodes);
51  addParam("MinPt", m_MinPt, "[GeV/c] Minimum transverse momentum of a particle that will be extrapolated (default 0.1)",
52  double(0.1));
53  addParam("MinKE", m_MinKE, "[GeV] Minimum kinetic energy of a particle to continue extrapolation (default 0.002)", double(0.002));
54  addParam("MaxStep", m_MaxStep, "[cm] Maximum step size during extrapolation (use 0 for infinity) (default 25)", double(25.0));
55  // Additional parameters copied from FullSimModule
56  addParam("TrackingVerbosity", m_TrackingVerbosity,
57  "Tracking verbosity: 0=Silent; 1=Min info per step; 2=sec particles; 3=pre/post step info; 4=like 3 but more info; 5=proposed step length info",
58  0);
59  addParam("EnableVisualization", m_EnableVisualization, "If set to True the Geant4 visualization support is enabled", false);
60  addParam("magneticFieldStepper", m_MagneticFieldStepperName,
61  "Chooses the magnetic field stepper used by Geant4. possible values are: default, nystrom, expliciteuler, simplerunge",
62  string("default"));
63  addParam("magneticCacheDistance", m_MagneticCacheDistance,
64  "Minimum distance for BField lookup in cm. If the next requested point is closer than this distance than return the last BField value. 0 means no caching",
65  0.0);
66  addParam("deltaChordInMagneticField", m_DeltaChordInMagneticField,
67  "[mm] The maximum miss-distance between the trajectory curve and its linear cord(s) approximation", 0.25);
68  vector<string> defaultCommands;
69  addParam("UICommands", m_UICommands, "A list of Geant4 UI commands that should be applied at the start of the job",
70  defaultCommands);
71 }
72 
73 ExtModule::~ExtModule()
74 {
75 }
76 
77 void ExtModule::initialize()
78 {
79  // Initialize the (singleton) extrapolation manager. It will check
80  // whether it will run with or without FullSimModule and with or without
81  // Muid (or any other geant4e-based extrapolation module).
82  Simulation::ExtManager* extMgr = Simulation::ExtManager::GetManager();
83  extMgr->Initialize("Ext", m_MagneticFieldStepperName, m_MagneticCacheDistance, m_DeltaChordInMagneticField,
84  m_EnableVisualization, m_TrackingVerbosity, m_UICommands);
85 
86  // Redefine geant4e step length, magnetic field step limitation (fraction of local curvature radius),
87  // and kinetic energy loss limitation (maximum fractional energy loss) by communicating with
88  // the geant4 UI. (Commands were defined in ExtMessenger when physics list was set up.)
89  // *NOTE* If module muid runs after this, its G4UImanager commands will override these.
90  m_MaxStep = ((m_MaxStep == 0.0) ? 10.0 : std::min(10.0, m_MaxStep)) * CLHEP::cm;
91  char stepSize[80];
92  std::sprintf(stepSize, "/geant4e/limits/stepLength %8.2f mm", m_MaxStep);
93  G4UImanager::GetUIpointer()->ApplyCommand(stepSize);
94  G4UImanager::GetUIpointer()->ApplyCommand("/geant4e/limits/magField 0.001");
95  G4UImanager::GetUIpointer()->ApplyCommand("/geant4e/limits/energyLoss 0.05");
96 
97  // Hypotheses for EXT extrapolation (default is every particle in
98  // Const::chargedStableSet, i.e., electron, muon, pion, kaon, proton, deuteron)
99  m_Hypotheses.clear();
100  if (m_PDGCodes.empty()) {
101  for (const Const::ChargedStable pdgIter : Const::chargedStableSet) {
102  m_Hypotheses.push_back(pdgIter);
103  }
104  } else { // user defined
105  std::vector<Const::ChargedStable> stack;
106  for (const Const::ChargedStable pdgIter : Const::chargedStableSet) {
107  stack.push_back(pdgIter);
108  }
109  for (unsigned int i = 0; i < m_PDGCodes.size(); ++i) {
110  for (unsigned int k = 0; k < stack.size(); ++k) {
111  if (abs(m_PDGCodes[i]) == stack[k].getPDGCode()) {
112  m_Hypotheses.push_back(stack[k]);
113  stack.erase(stack.begin() + k);
114  --k;
115  }
116  }
117  }
118  if (m_Hypotheses.empty()) B2ERROR("No valid PDG codes for extrapolation");
119  }
120 
121  for (unsigned int i = 0; i < m_Hypotheses.size(); ++i) {
122  B2INFO("Ext hypothesis for PDG code " << m_Hypotheses[i].getPDGCode() << " and its antiparticle will be extrapolated");
123  }
124 
125  // Initialize the extrapolator engine for EXT (vs MUID)
126  // *NOTE* that MinPt and MinKE are shared by MUID and EXT; only last caller wins
127  m_Extrapolator->initialize(m_MinPt, m_MinKE, m_Hypotheses);
128 }
129 
130 void ExtModule::beginRun()
131 {
132  m_Extrapolator->beginRun(false);
133 }
134 
135 void ExtModule::event()
136 {
137  m_Extrapolator->event(false);
138 }
139 
140 void ExtModule::endRun()
141 {
142  m_Extrapolator->endRun(false);
143 }
144 
145 void ExtModule::terminate()
146 {
147  m_Extrapolator->terminate(false);
148 }
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:470
The geant4e-based track extrapolation module.
Definition: ExtModule.h:38
Base class for Modules.
Definition: Module.h:72
It is the main interface for the user to define the setup and start the propagation.
Definition: ExtManager.h:50
void Initialize(const char[], const std::string &, double, double, bool, int, const std::vector< std::string > &)
Initialize Geant4 and Geant4e.
Definition: ExtManager.cc:176
#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.