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