Belle II Software  release-08-01-10
setup_modules.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
13 
19 
20 
21 import basf2 as b2
22 from ROOT import Belle2
23 
24 import os
25 
26 # sets up the geometry. The file for the default geometry for Belle2 is geometry/Belle2.xml
27 
28 
29 def setup_Geometry(path=None):
30  """
31  Sets the geometry. Should be used in all VXDTF2 related scripts to ensure to use the same geometry in all
32  trainings / validation steps!
33  param path: the path to append the geometry
34 
35  """
36 
37  b2.B2WARNING("This function is deprecated and should not be used anymore! The geometry should now be loaded from the DB")
38 
39 
40 def setup_VXDTF2(path=None,
41  use_pxd=False,
42  use_svd=True,
43  filter_overlapping=True,
44  use_segment_network_filters=True,
45  observerType=0,
46  quality_estimator='circleFit',
47  overlap_filter='greedy',
48  sec_map_file=None,
49  setup_name='SVDOnlyDefault',
50  log_level=b2.LogLevel.INFO,
51  debug_level=1):
52  """
53  Convenience Method to setup the redesigned vxd track finding module chain.
54  Reuslt is a store array containing reco tracks called 'RecoTracks'.
55  :param path: basf2.Path
56  :param sec_map_file: training data for segment network.
57  :param setup_name: name of the setup within all the sectormaps in the sec_map_file which should be used,
58  :param use_pxd: if true use pxd hits. Default False.
59  :param use_svd: if true use svd hits. Default True.
60  :param quality_estimator: which fit to use to determine track quality. Options 'circle', 'random'. Default 'circle'.
61  :param filter_overlapping: if true overlapping tracks are reduced to a single track using the qualitiy indicator.
62  :param use_segment_network_filters: if true use filters for segmentMap training. Default True.
63  :param observe_network_filters: FOR DEBUG ONLY! If true results for FilterVariables are stored to a root file. Default False.
64  :param overlap_filter: which filter network to use. Options 'hopfield', 'greedy'. Default 'hopfield'.
65  :param log_level: LogLevel of all modules in the chain
66  :param debug_level: debug level of all modules in the chain.
67  :return:
68  """
69 
70  # List containing all modules either to be added to the path or to be returned.
71  modules = []
72 
73 
77  if use_pxd:
78  spCreatorPXD = b2.register_module('PXDSpacePointCreator')
79  spCreatorPXD.logging.log_level = log_level
80  spCreatorPXD.logging.debug_level = debug_level
81  spCreatorPXD.param('NameOfInstance', 'PXDSpacePoints')
82  spCreatorPXD.param('SpacePoints', 'SpacePoints')
83  modules.append(spCreatorPXD)
84 
85  if use_svd:
86  spCreatorSVD = b2.register_module('SVDSpacePointCreator')
87  spCreatorSVD.logging.log_level = log_level
88  spCreatorSVD.logging.debug_level = debug_level
89  spCreatorSVD.param('OnlySingleClusterSpacePoints', False)
90  spCreatorSVD.param('NameOfInstance', 'SVDSpacePoints')
91  spCreatorSVD.param('SpacePoints', 'SpacePoints')
92  modules.append(spCreatorSVD)
93 
94  # SecMap Bootstrap
95  secMapBootStrap = b2.register_module('SectorMapBootstrap')
96  secMapBootStrap.param('ReadSectorMap', True)
97  if sec_map_file:
98  secMapBootStrap.param('SectorMapsInputFile', sec_map_file)
99  secMapBootStrap.param('WriteSectorMap', False)
100  secMapBootStrap.param('SetupToRead', "")
101  modules.append(secMapBootStrap)
102 
103  # Register EventLevelTrackingInfo
104  regEventLevelTrackingInfo = b2.register_module('RegisterEventLevelTrackingInfo')
105  modules.append(regEventLevelTrackingInfo)
106 
107 
111 
112  segNetProducer = b2.register_module('SegmentNetworkProducer')
113  # segNetProducer.param('CreateNeworks', 3)
114  segNetProducer.param('NetworkOutputName', 'test2Hits')
115  segNetProducer.param('allFiltersOff', not use_segment_network_filters)
116  segNetProducer.param('SpacePointsArrayNames', ['SpacePoints'])
117  segNetProducer.param('printNetworks', False)
118  segNetProducer.param('sectorMapName', setup_name)
119  segNetProducer.param('addVirtualIP', False)
120  # segNetProducer.param('observerType', observerType)
121  segNetProducer.logging.log_level = log_level
122  segNetProducer.logging.debug_level = debug_level
123  modules.append(segNetProducer)
124 
125 
129 
130  cellOmat = b2.register_module('TrackFinderVXDCellOMat')
131  cellOmat.param('NetworkName', 'test2Hits')
132  cellOmat.param('SpacePointTrackCandArrayName', 'SPTCs')
133  cellOmat.param('printNetworks', False)
134  cellOmat.param('strictSeeding', True)
135  cellOmat.logging.log_level = log_level
136  cellOmat.logging.debug_level = debug_level
137  modules.append(cellOmat)
138 
139 
143 
144  # Quality
145 
146  qualityEstimator = b2.register_module('QualityEstimatorVXD')
147  qualityEstimator.param('EstimationMethod', quality_estimator)
148  qualityEstimator.param('SpacePointTrackCandsStoreArrayName', 'SPTCs')
149  qualityEstimator.logging.log_level = log_level
150  qualityEstimator.logging.debug_level = debug_level
151  modules.append(qualityEstimator)
152 
153  maxCandidateSelection = b2.register_module('BestVXDTrackCandidatesSelector')
154  maxCandidateSelection.param('NameSpacePointTrackCands', 'SPTCs')
155  maxCandidateSelection.param('SubsetCreation', True)
156  maxCandidateSelection.param('NewNameSpacePointTrackCands', '')
157  maxCandidateSelection.logging.log_level = log_level
158  maxCandidateSelection.logging.debug_level = debug_level
159  modules.append(maxCandidateSelection)
160 
161  # Properties
162  vIPRemover = b2.register_module('SPTCvirtualIPRemover')
163  vIPRemover.param('tcArrayName', '')
164  vIPRemover.param('maxTCLengthForVIPKeeping', 0) # want to remove virtualIP for any track length
165  vIPRemover.logging.log_level = log_level
166  vIPRemover.logging.debug_level = debug_level
167  modules.append(vIPRemover)
168 
169 
173 
174  if filter_overlapping:
175  overlapResolver = b2.register_module('SVDOverlapResolver')
176  overlapResolver.logging.log_level = log_level
177  overlapResolver.logging.debug_level = debug_level
178  overlapResolver.param('NameSpacePointTrackCands', '')
179  overlapResolver.param('ResolveMethod', overlap_filter.lower())
180  overlapResolver.param('NameSVDClusters', '')
181  modules.append(overlapResolver)
182 
183 
187  momSeedRetriever = b2.register_module('SPTCmomentumSeedRetriever')
188  momSeedRetriever.param('tcArrayName', '')
189  momSeedRetriever.logging.log_level = log_level
190  momSeedRetriever.logging.debug_level = debug_level
191  modules.append(momSeedRetriever)
192 
193  converter = b2.register_module('SPTC2RTConverter')
194  converter.param('spacePointsTCsStoreArrayName', '')
195  converter.logging.log_level = log_level
196  converter.logging.debug_level = debug_level
197  modules.append(converter)
198 
199  if path:
200  for module in modules:
201  path.add_module(module)
202  else:
203  return modules
204 
205 
206 def setup_RTCtoSPTCConverters(
207  path=0,
208  SVDSPscollection='SVDSpacePoints',
209  PXDSPscollection='PXDSpacePoints',
210  RTCinput='mcTracks',
211  sptcOutput='checkedSPTCs',
212  usePXD=True,
213  logLevel=b2.LogLevel.INFO,
214  debugVal=1,
215  useNoKick=False,
216  useOnlyFittedTracks=False):
217  """This function adds the modules needed to convert Reco-TCs to SpacePointTCs to given path.
218 
219  @param path if set to 0 (standard) the created modules will not be added, but returned.
220  If a path is given, 'None' is returned but will be added to given path instead.
221 
222  @param SPscollection the name of the storeArray containing SPs of both SVD and PXD.
223 
224  @param RTCinput defines the name of input-Reco-TCs.
225 
226  @param sptcOutput defines the name of output-SPTCs.
227 
228  @param usePXD If False pxdClusters are ignored.
229 
230  @param logLevel set to logLevel level of your choice.
231 
232  @param debugVal set to debugLevel of choice - will be ignored if logLevel is not set to LogLevel.DEBUG
233 
234  @param useNoKick enable the training sample selection based on track parameters (and produce a TFile of its effect)
235 
236  @param useOnlyFittedTracks: if True only fitted RecoTracks will be transformed to SpacePointTrackCands
237  """
238  print("setup RTCtoSPTCConverters...")
239 
240  spacePointNames = []
241  detectorTypes = []
242  trueHitNames = []
243  clusterNames = []
244  if usePXD:
245  detectorTypes.append('PXD')
246  # PXD SpacePoints and SVD SpacePoints are assumed to be in the same StoreArray
247  spacePointNames.append(PXDSPscollection)
248  trueHitNames.append('')
249  clusterNames.append('')
250  # PXD SpacePoints and SVD SpacePoints are assumed to be in the same StoreArray
251  spacePointNames.append(SVDSPscollection)
252  detectorTypes.append('SVD')
253  trueHitNames.append('')
254  clusterNames.append('')
255 
256  # module to create relations between SpacePoints and TrueHits -> some of
257  # the following modules will be utilizing these relations!
258  sp2thConnector = b2.register_module('SpacePoint2TrueHitConnector')
259  sp2thConnector.logging.log_level = logLevel
260  sp2thConnector.param('DetectorTypes', detectorTypes)
261  sp2thConnector.param('TrueHitNames', trueHitNames)
262  sp2thConnector.param('ClusterNames', clusterNames)
263  sp2thConnector.param('SpacePointNames', spacePointNames)
264  sp2thConnector.param('outputSuffix', '_relTH')
265  sp2thConnector.param('storeSeperate', False)
266  sp2thConnector.param('registerAll', False)
267  sp2thConnector.param('maxGlobalPosDiff', 0.05)
268  sp2thConnector.param('maxPosSigma', 5)
269  sp2thConnector.param('minWeight', 0)
270  sp2thConnector.param('requirePrimary', True)
271  sp2thConnector.param('positionAnalysis', False)
272 
273  # TCConverter, RecoTrack -> SPTC
274  recoTrackCandConverter = b2.register_module('RT2SPTCConverter')
275  recoTrackCandConverter.logging.log_level = logLevel
276  recoTrackCandConverter.param('RecoTracksName', RTCinput)
277  recoTrackCandConverter.param('SpacePointTCName', 'SPTracks')
278  recoTrackCandConverter.param('SVDSpacePointStoreArrayName', SVDSPscollection)
279  recoTrackCandConverter.param('PXDSpacePointStoreArrayName', None)
280  if usePXD:
281  recoTrackCandConverter.param('PXDSpacePointStoreArrayName', PXDSPscollection)
282  recoTrackCandConverter.param('useTrueHits', True)
283  recoTrackCandConverter.param('ignorePXDHits', not usePXD) # if True PXD hits will be ignored
284  recoTrackCandConverter.param('useSingleClusterSP', False)
285  recoTrackCandConverter.param('minSP', 3)
286  recoTrackCandConverter.param('skipProblematicCluster', False)
287  recoTrackCandConverter.param('convertFittedOnly', useOnlyFittedTracks)
288 
289  if os.environ.get('USE_BEAST2_GEOMETRY'):
290  NoKickCuts = Belle2.FileSystem.findFile("data/tracking/NoKickCutsPhase2.root")
291  else:
292  NoKickCuts = Belle2.FileSystem.findFile("data/tracking/NoKickCuts.root")
293 
294  if useNoKick:
295  recoTrackCandConverter.param('noKickCutsFile', NoKickCuts) # NoKickCuts applied
296  recoTrackCandConverter.param('noKickOutput', True) # produce output TFile of NoKickCuts
297  else:
298  recoTrackCandConverter.param('noKickCutsFile', "") # NoKickCuts not applied
299 
300  # SpacePointTrackCand referee
301  sptcReferee = b2.register_module('SPTCReferee')
302  sptcReferee.logging.log_level = logLevel
303  sptcReferee.param('sptcName', 'SPTracks')
304  sptcReferee.param('newArrayName', sptcOutput)
305  sptcReferee.param('storeNewArray', True)
306  sptcReferee.param('checkCurling', True)
307  sptcReferee.param('splitCurlers', True)
308  sptcReferee.param('keepOnlyFirstPart', True)
309  sptcReferee.param('kickSpacePoint', True)
310  sptcReferee.param('checkSameSensor', True)
311  sptcReferee.param('useMCInfo', True)
312  # sptcReferee.logging.log_level = LogLevel.DEBUG
313 
314  if path == 0:
315  return [sp2thConnector, recoTrackCandConverter, sptcReferee]
316  else:
317  path.add_module(sp2thConnector)
318  path.add_module(recoTrackCandConverter)
319  path.add_module(sptcReferee)
320  return None
321 
322 
323 def setup_sim(path=0, useEDeposit=True, useMultipleScattering=True, allowDecay=True, verbose=False):
324  """This function adds the g4simulation to given path.
325 
326  @param path if set to 0 (standard) g4sim will not be added, but returned.
327  If a path is given, 'None' is returned but will be added to given path instead.
328 
329  @param useEDeposit if False, particles will not have eDeposit. WARMING: if you still want to get hits for that case:
330  useEDeposit: If you want to work w/o E-deposit, edit pxd/data/PXD.xml and svd/data/SVD.xml,
331  where you have to activate see neutrons = true.
332 
333  @param useMultipleScattering if False, multiple scattering in matter is deactivated.
334 
335  @param allowDecay if False, particles can not decay.
336 
337  @param verbose if True, some extra debug output is given.
338  """
339  print("setup FullSim...")
340  g4sim = b2.register_module('FullSim')
341  g4sim.param('StoreAllSecondaries', True)
342  uiCommandList = []
343  if verbose:
344  uiCommandList.append('/process/list') # prints list of processes available
345  if allowDecay:
346  uiCommandList.append('/process/inactivate Decay all')
347  if not useEDeposit:
348  uiCommandList.append('/process/inactivate StepLimiter all')
349  uiCommandList.append('/process/inactivate muIoni all')
350  uiCommandList.append('/process/inactivate hIoni all')
351  uiCommandList.append('/process/inactivate ionIoni all')
352  uiCommandList.append('/process/inactivate eIoni all')
353  uiCommandList.append('/process/inactivate eBrem all')
354  uiCommandList.append('/process/inactivate hBrems all')
355  uiCommandList.append('/process/inactivate muBrems all')
356  uiCommandList.append('/process/inactivate hPairProd all')
357  uiCommandList.append('/process/inactivate muPairProd all')
358  uiCommandList.append('/process/inactivate annihil all')
359  uiCommandList.append('/process/inactivate phot all')
360  uiCommandList.append('/process/inactivate compt all')
361  uiCommandList.append('/process/inactivate conv all')
362  # uiCommandList.append('/process/inactivate PhotonInelastic')
363  # uiCommandList.append('/process/inactivate photonInelastic')
364  uiCommandList.append('/process/inactivate electronNuclear all')
365  uiCommandList.append('/process/inactivate positronNuclear all')
366  uiCommandList.append('/process/inactivate muonNuclear all')
367  uiCommandList.append('/process/inactivate photonNuclear all')
368  uiCommandList.append('/process/inactivate Decay all')
369  uiCommandList.append('/process/inactivate hadElastic all')
370  uiCommandList.append('/process/inactivate neutronInelastic all')
371  uiCommandList.append('/process/inactivate nCapture all')
372  uiCommandList.append('/process/inactivate nFission all')
373  uiCommandList.append('/process/inactivate protonInelastic all')
374  uiCommandList.append('/process/inactivate ionInelastic all')
375  uiCommandList.append('/process/inactivate pi+Inelastic all')
376  uiCommandList.append('/process/inactivate pi-Inelastic all')
377  uiCommandList.append('/process/inactivate kaon+Inelastic all')
378  uiCommandList.append('/process/inactivate kaon-Inelastic all')
379  uiCommandList.append('/process/inactivate kaon0LInelastic all')
380  uiCommandList.append('/process/inactivate kaon0SInelastic all')
381  uiCommandList.append('/process/inactivate anti_protonInelastic all')
382  uiCommandList.append('/process/inactivate anti_neutronInelastic all')
383  uiCommandList.append('/process/inactivate lambdaInelastic all')
384  uiCommandList.append('/process/inactivate anti-lambdaInelastic all')
385  uiCommandList.append('/process/inactivate sigma-Inelastic all')
386  uiCommandList.append('/process/inactivate anti_sigma-Inelastic all')
387  uiCommandList.append('/process/inactivate sigma+Inelastic all')
388  uiCommandList.append('/process/inactivate anti_sigma+Inelastic all')
389  uiCommandList.append('/process/inactivate xi-Inelastic all')
390  uiCommandList.append('/process/inactivate anti_xi-Inelastic all')
391  uiCommandList.append('/process/inactivate xi0Inelastic all')
392  uiCommandList.append('/process/inactivate anti_xi0Inelastic all')
393  uiCommandList.append('/process/inactivate omega-Inelastic all')
394  uiCommandList.append('/process/inactivate anti_omega-Inelastic all')
395  # uiCommandList.append('/process/inactivate CHIPSNuclearCaptureAtRest')
396  uiCommandList.append('/process/inactivate hFritiofCaptureAtRest all')
397  uiCommandList.append('/process/inactivate hBertiniCaptureAtRest all')
398  uiCommandList.append('/process/inactivate muMinusCaptureAtRest all')
399  uiCommandList.append('/process/inactivate dInelastic all')
400  uiCommandList.append('/process/inactivate anti_deuteronInelastic all')
401  uiCommandList.append('/process/inactivate tInelastic all')
402  uiCommandList.append('/process/inactivate anti_tritonInelastic all')
403  uiCommandList.append('/process/inactivate He3Inelastic all')
404  uiCommandList.append('/process/inactivate anti_He3Inelastic all')
405  uiCommandList.append('/process/inactivate alphaInelastic all')
406  uiCommandList.append('/process/inactivate anti_alphaInelastic all')
407  uiCommandList.append('/process/inactivate ExtEnergyLoss all')
408  uiCommandList.append('/process/inactivate OpAbsorption all')
409  uiCommandList.append('/process/inactivate OpRayleigh all')
410  uiCommandList.append('/process/inactivate OpMieHG all')
411  uiCommandList.append('/process/inactivate OpBoundary all')
412  uiCommandList.append('/process/inactivate OpWLS all')
413  uiCommandList.append('/process/inactivate Cerenkov all')
414  uiCommandList.append('/process/inactivate Scintillation all')
415 
416  if not useMultipleScattering:
417  uiCommandList.append('/process/inactivate msc all')
418  uiCommandList.append('/process/inactivate CoulombScat all')
419  # uiCommandList.append('/process/inactivate muMsc')
420  # uiCommandList.append('/process/list')
421  # if (useMultipleScattering == False or useMultipleScattering == False):
422  if verbose:
423  uiCommandList.append('/particle/select pi+')
424  uiCommandList.append('/particle/process/dump')
425  uiCommandList.append('/particle/select pi-')
426  uiCommandList.append('/particle/process/dump')
427  uiCommandList.append('/particle/select e+')
428  uiCommandList.append('/particle/process/dump')
429  uiCommandList.append('/particle/select e-')
430  uiCommandList.append('/particle/process/dump')
431  uiCommandList.append('/particle/select mu+')
432  uiCommandList.append('/particle/process/dump')
433  uiCommandList.append('/particle/select mu-')
434  uiCommandList.append('/particle/select kaon+')
435  uiCommandList.append('/particle/process/dump')
436  uiCommandList.append('/particle/select kaon-')
437  uiCommandList.append('/particle/process/dump')
438  g4sim.param('UICommandsAtIdle', uiCommandList)
439 
440  # print(uiCommandList)
441  if (path == 0):
442  return g4sim
443  else:
444  path.add_module(g4sim)
445  return None
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:148