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