Belle II Software  release-06-01-15
MuidModule.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/muid/MuidModule.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(Muid)
32 
34  Module(),
35  m_MeanDt(0.0),
36  m_MaxDt(0.0),
37  m_MinPt(0.0),
38  m_MinKE(0.0),
39  m_MaxStep(0.0),
40  m_MaxDistSqInVariances(0.0),
41  m_MaxKLMTrackClusterDistance(0.0),
42  m_MaxECLTrackClusterDistance(0.0),
43  m_TrackingVerbosity(0),
44  m_EnableVisualization(false),
45  m_MagneticFieldStepperName(""),
46  m_MagneticCacheDistance(0.0),
47  m_DeltaChordInMagneticField(0.0)
48 {
49  m_Extrapolator = TrackExtrapolateG4e::getInstance();
50  m_PDGCodes.clear();
51  m_Hypotheses.clear();
52  m_UICommands.clear();
53  setDescription("Identifies muons by extrapolating tracks from CDC to KLM using geant4e");
54  setPropertyFlags(c_ParallelProcessingCertified);
55  addParam("pdgCodes", m_PDGCodes, "Positive-charge PDG codes for extrapolation hypotheses", m_PDGCodes);
56  addParam("MeanDt", m_MeanDt, "[ns] Mean hit-trigger time for coincident hits (default 0)", double(0.0));
57  // Raw KLM scintillator hit times are in the range from -5000 to -4000 ns
58  // approximately. The time window can be readjusted after completion of
59  // the implementation of KLM time calibration.
60  addParam("MaxDt", m_MaxDt, "[ns] Coincidence window half-width for in-time KLM hits.", double(10000.0));
61  addParam("MinPt", m_MinPt, "[GeV/c] Minimum transverse momentum of a particle that will be extrapolated (default 0.1)",
62  double(0.1));
63  addParam("MinKE", m_MinKE, "[GeV] Minimum kinetic energy of a particle to continue extrapolation (default 0.002)", double(0.002));
64  addParam("MaxStep", m_MaxStep, "[cm] Maximum step size during extrapolation (use 0 for infinity; default 25)", double(25.0));
65  addParam("MaxDistSigma", m_MaxDistSqInVariances, "[#sigmas] Maximum hit-to-extrapolation difference (default 3.5)", double(3.5));
66  addParam("MaxKLMTrackClusterDistance", m_MaxKLMTrackClusterDistance,
67  "[cm] Maximum distance of closest approach of track to KLM cluster for match (default 150)", double(150.0));
68  addParam("MaxECLTrackClusterDistance", m_MaxECLTrackClusterDistance,
69  "[cm] Maximum distance of closest approach of track to ECL cluster for match (default 100)", double(100.0));
70  // Additional parameters copied from FullSimModule
71  addParam("TrackingVerbosity", m_TrackingVerbosity,
72  "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.",
73  0);
74  addParam("EnableVisualization", m_EnableVisualization, "If set to True the Geant4 visualization support is enabled.", false);
75  addParam("magneticFieldStepper", m_MagneticFieldStepperName,
76  "Chooses the magnetic field stepper used by Geant4. possible values are: default, nystrom, expliciteuler, simplerunge",
77  string("default"));
78  addParam("magneticCacheDistance", m_MagneticCacheDistance,
79  "Minimum distance for BField lookup in cm. If the next requested point is closer than this distance than return the flast BField value. 0 means no caching",
80  0.0);
81  addParam("deltaChordInMagneticField", m_DeltaChordInMagneticField,
82  "[mm] The maximum miss-distance between the trajectory curve and its linear cord(s) approximation", 0.25);
83  addParam("addHitsToRecoTrack", m_addHitsToRecoTrack,
84  "Parameter to add the found hits also to the reco tracks or not. Is turned off by default. "
85  "Make sure to refit the track afterwards.",
86  m_addHitsToRecoTrack);
87  vector<string> defaultCommands;
88  addParam("UICommands", m_UICommands, "A list of Geant4 UI commands that should be applied at the start of the job.",
89  defaultCommands);
90 }
91 
92 MuidModule::~MuidModule()
93 {
94 }
95 
96 void MuidModule::initialize()
97 {
98  // Initialize the (singleton) extrapolation manager. It will check
99  // whether it will run with or without FullSimModule and with or without
100  // Ext (or any other geant4e-based extrapolation module).
101  Simulation::ExtManager* extMgr = Simulation::ExtManager::GetManager();
102  extMgr->Initialize("Muid", m_MagneticFieldStepperName, m_MagneticCacheDistance, m_DeltaChordInMagneticField,
103  m_EnableVisualization, m_TrackingVerbosity, m_UICommands);
104 
105  // Redefine geant4e step length, magnetic field step limitation (fraction of local curvature radius),
106  // and kinetic energy loss limitation (maximum fractional energy loss) by communicating with
107  // the geant4 UI. (Commands were defined in ExtMessenger when physics list was set up.)
108  // *NOTE* If module muid runs after this, its G4UImanager commands will override these.
109  m_MaxStep = ((m_MaxStep == 0.0) ? 10.0 : std::min(10.0, m_MaxStep)) * CLHEP::cm;
110  char stepSize[80];
111  std::sprintf(stepSize, "/geant4e/limits/stepLength %8.2f mm", m_MaxStep);
112  G4UImanager::GetUIpointer()->ApplyCommand(stepSize);
113  G4UImanager::GetUIpointer()->ApplyCommand("/geant4e/limits/magField 0.001");
114  G4UImanager::GetUIpointer()->ApplyCommand("/geant4e/limits/energyLoss 0.05");
115 
116  // Hypotheses for MUID extrapolation (default is muon only)
117  if (m_PDGCodes.empty()) {
118  m_Hypotheses.push_back(Const::muon);
119  } else { // user defined - intended for debugging only!
120  std::vector<Const::ChargedStable> stack;
121  for (const Const::ChargedStable pdgIter : Const::chargedStableSet) {
122  stack.push_back(pdgIter);
123  }
124  for (unsigned int i = 0; i < m_PDGCodes.size(); ++i) {
125  for (unsigned int k = 0; k < stack.size(); ++k) {
126  if (abs(m_PDGCodes[i]) == stack[k].getPDGCode()) {
127  m_Hypotheses.push_back(stack[k]);
128  stack.erase(stack.begin() + k);
129  --k;
130  }
131  }
132  }
133  if (m_Hypotheses.empty()) B2ERROR("No valid PDG codes for extrapolation");
134  }
135 
136  for (unsigned int i = 0; i < m_Hypotheses.size(); ++i) {
137  B2INFO("Muid hypothesis for PDG code " << m_Hypotheses[i].getPDGCode() << " and its antiparticle will be extrapolated");
138  }
139 
140  // Initialize the extrapolator engine for MUID (vs EXT)
141  // *NOTE* that MinPt and MinKE are shared by MUID and EXT; only last caller wins
142  m_Extrapolator->initialize(m_MeanDt, m_MaxDt, m_MaxDistSqInVariances, m_MaxKLMTrackClusterDistance,
143  m_MaxECLTrackClusterDistance, m_MinPt, m_MinKE, m_addHitsToRecoTrack, m_Hypotheses);
144  return;
145 }
146 
147 void MuidModule::beginRun()
148 {
149  m_Extrapolator->beginRun(true);
150 }
151 
152 void MuidModule::event()
153 {
154  m_Extrapolator->event(true);
155 }
156 
157 void MuidModule::endRun()
158 {
159  m_Extrapolator->endRun(true);
160 }
161 
162 void MuidModule::terminate()
163 {
164  m_Extrapolator->terminate(true);
165 }
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:470
Base class for Modules.
Definition: Module.h:72
The geant4e-based muon identification module.
Definition: MuidModule.h:38
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.