Belle II Software  release-05-01-25
testVXDTFRelatedModules.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import os
5 from basf2 import *
6 from subprocess import call
7 from sys import argv
8 import time
9 
10 from VXDTF.setup_modules import (setup_gfTCtoSPTCConverters,
11  setup_spCreatorPXD,
12  setup_spCreatorSVD,
13  setup_sp2thConnector,
14  setup_qualityEstimators,
15  setup_trackSetEvaluators)
16 
17 from VXDTF.setup_modules_ml import *
18 
19 # ################
20 # rootInputFileName = "seed4nEv100000pGun1_1T.root"
21 # rootInputFileName = "seed14nEv100000pGun1_1T.root" # phi 0-90, theta 60-85, pT 100-145 MeV/c, PDG 13
22 # rootInputFileName = "seed13nEv100000pGun1_2T.root" # phi 0-90, theta 60-85, pT 100-145 MeV/c, PDG 13
23 # rootInputFileName = "evtGenseed6nEv100000.root" #evtGenSinglePassTest-TrainSample. skipCluster = False
24 # rootInputFileName = "evtGenseed5nEv10000.root" #testSample. skipCluster = False
25 # rootInputFileName = "evtGenseed7nEv10000.root" # testSample. skipCluster = True
26 # rootInputFileName = "evtGenseed8nEv200000.root" # trainSample. skipCluster = True
27 # rootInputFileName = "evtGenseed8nEv100000.root" #evtGenSinglePassTest-TrainSample. skipCluster = True (TODO)
28 
29 # rootInputFileName = "seed11nEv100pGun1_1T.root" # test- and TrainSample
30 # rootInputFileName = "seed12nEv200pGun1_2T.root" # test- and TrainSample 0-90° phi, 60-85° Theta
31 rootInputFileName = "seed12345nEv1000pGun1_20T.root" # test- and TrainSample 0-90° phi, 60-85° Theta
32 rootInputFileName = "MyRootFile.root" # test- and TrainSample 0-90° phi, 60-85° Theta
33 # rootInputFileName = "TestFile.root"
34 
35 # file name into which the segNetAnalize stores its stuff
36 fitType = 'circleFit' # currently supported: 'random' and 'circleFit'
37 # fitType = 'random' # currently supported: 'random' and 'circleFit'
38 setFilterType = 'hopfield' # currently supported: 'greedy' and 'hopfield'
39 
40 usePXD = False
41 useDisplay = False
42 newTrain = False # if true, rawSecMap-Data is collected. IF false, new TF will be executed
43 printNetworks = False # creates graphs for each network for each event if true
44 useOldTFinstead = False # if true, the old vxdtf is used instead of the new one.
45 oldTFNoSubsetSelection = True # if true, the old vxdtf does not its hopfield-part, which allows using new modules for that
46 ignoreDeadTCs = True # if true, the TrackFinderVXDAnalizer will not add dead TCs to the efficiencies
47 bypassCA = False # if true, no CA will be used but the BasicPathFinder instead...
48 
49 activateSegNetAnalizer = False # only needed when studying FastBDT-behavior
50 
51 doStrictSeeds = False # if true, a smaller amount of TCs are created from the same segment-tree
52 doNewSubsetSelection = True # if true, then NewSubsetSelection will be executed
53 doVirtualIPRemovalb4Fit = True # if false, the vIP willbe removed after the fit
54 doEventSummary = True # if true TFVXDAnalizer will produce event-wise results
55 switchFiltersOff = False # if true, SegNetProducer does not apply any filters of the sectorMap.
56 
57 trainFBDT = False # with the current settings: collects samples but does not train a FastBDT!
58 useFBDT = False # use the ML Filter for creating the SegmentNetwork instead of the SectorMap filters
59 
60 segNetAnaRFN = 'SegNetAnalyzer_SM_train.root'
61 fbdtSamplesFN = 'FBDTClassifier_samples_train_10k.dat'
62 fbdtFN = 'FBDTClassifier_1000_3.dat'
63 if useFBDT:
64  cNetworks = int(2)
65 else:
66  cNetworks = int(3)
67 
68 # Important parameters:
69 
70 # tempStringList = rootInputFileName.split('nEv', 1)
71 # stringInitialValue = tempStringList[0].split("seed", 1)
72 # print("found seed: " + stringInitialValue[1])
73 # numEvents = 20 # WARNING has to be identical with the value named in rootInputFileName!
74 # comment Martin: this doesn't seem to be true. With
75 # initialValue = int(stringInitialValue[1])
76 initialValue = 0
77 
78 set_log_level(LogLevel.ERROR)
79 set_random_seed(initialValue)
80 
81 trainerVXDTFLogLevel = LogLevel.INFO
82 trainerVXDTFDebugLevel = 10
83 
84 TFlogLevel = LogLevel.INFO
85 TFDebugLevel = 1
86 
87 CAlogLevel = LogLevel.DEBUG
88 CADebugLevel = 1
89 
90 AnalizerlogLevel = LogLevel.INFO
91 AnalizerDebugLevel = 1
92 
93 # acceptedRawSecMapFiles = ['lowTestRedesign_202608818.root']
94 if (initialValue == 2):
95  print("chosen initialvalue 2! " + rootInputFileName)
96  acceptedRawSecMapFiles = ['lowTestRedesign_1373026662.root'] # 42
97 elif (initialValue == 0):
98  print("chosen initialvalue 0! " + rootInputFileName)
99  acceptedRawSecMapFiles = ['lowTestRedesign.root'] # 23
100 elif (initialValue == 3):
101  print("chosen initialvalue 3! " + rootInputFileName)
102  acceptedRawSecMapFiles = ['lowTestRedesign_202608818.root'] # 23
103 elif (initialValue == 4):
104  print("chosen initialvalue 4!! " + rootInputFileName)
105  acceptedRawSecMapFiles = ['lowTestRedesign_293660864.root'] # 24
106 # elif (initialValue == 5):
107  # print("chosen initialvalue 5! (with background)" + rootInputFileName)
108  # acceptedRawSecMapFiles = ['lowTestRedesign_753986291.root'] # 25
109 elif (initialValue == 5):
110  print("chosen initialvalue 5! " + rootInputFileName)
111  acceptedRawSecMapFiles = ['lowTestRedesign_1120112796.root'] # lowTestRedesign_1120112796.root
112 elif (initialValue == 6):
113  print("chosen initialvalue 6! " + rootInputFileName)
114  acceptedRawSecMapFiles = ['lowTestRedesign_1120112796.root']
115  # acceptedRawSecMapFiles = ['lowTestRedesign_1667035383.root'] # 26 - single track, single event raw data
116 elif (initialValue == 7):
117  print("chosen initialvalue 7! (skipCluster-setting=True) " + rootInputFileName)
118  acceptedRawSecMapFiles = ['lowTestRedesign_1332084337.root'] # 27 - single track, single event raw data
119 elif (initialValue == 8):
120  print("chosen initialvalue 8! (skipCluster-setting=True): 200k evtGen events " + rootInputFileName)
121  acceptedRawSecMapFiles = ['lowTestRedesign_1332084337.root'] # 28 - single track, single event raw data
122 elif (initialValue == 11):
123  print("chosen initialvalue 11! (skipCluster-setting=True): 100 pGun events " + rootInputFileName)
124  acceptedRawSecMapFiles = ['lowTestRedesign_1017144726.root'] # 28 - single track, single event raw data
125 elif (initialValue == 12):
126  print("chosen initialvalue 12! (skipCluster-setting=True): 200 pGun events " + rootInputFileName)
127  acceptedRawSecMapFiles = ['lowTestRedesign_1196763558.root'] # 28 - single track, single event raw data
128 elif (initialValue == 13):
129  print("chosen initialvalue 13! (skipCluster-setting=True): 100k pGun events " + rootInputFileName)
130  acceptedRawSecMapFiles = ['lowTestRedesign_1874442389.root'] # 28 - single track, single event raw data
131 # elif (initialValue == 14):
132 # print("chosen initialvalue 14! (skipCluster-setting=True): 100k pGun events " + rootInputFileName)
133 # acceptedRawSecMapFiles = ['lowTestRedesign_1054912153.root'] # 28 - single track, single event raw data
134 elif (initialValue == 57):
135  print("chosen initialvalue 57! setup remark: train: 10k events, 10 tracks per event, theta 60-85°, phi 0-360°, pT 100-145MeV.")
136  acceptedRawSecMapFiles = ['lowTestRedesign_779994078.root'] # 55 - single track, single event raw data
137 elif (initialValue == 12345):
138  print("chosen initialvalue 12345! some dummy setup!")
139  acceptedRawSecMapFiles = ['lowTestRedesign_349397772.root']
140 else:
141  print("ERROR! no valid initialvalue chosen!")
142  acceptedRawSecMapFiles = [""]
143  time.sleep(60)
144 
145 print('')
146 time.sleep(5) # sleep for 5 seconds
147 print('')
148 
149 rootInputM = register_module('RootInput')
150 rootInputM.param('inputFileName', rootInputFileName)
151 
152 # rootInputM.param('skipNEvents', int(10000))
153 
154 eventinfoprinter = register_module('EventInfoPrinter')
155 
156 
157 gearbox = register_module('Gearbox')
158 
159 secMapBootStrap = register_module('SectorMapBootstrap')
160 secMapBootStrap.param('ReadSectorMap', False)
161 secMapBootStrap.param('WriteSectorMap', True)
162 
163 evtStepSize = 1
164 if newTrain:
165  newSecMapTrainerBase = register_module('SecMapTrainerBase')
166  newSecMapTrainerBase.logging.log_level = trainerVXDTFLogLevel
167  newSecMapTrainerBase.logging.debug_level = trainerVXDTFDebugLevel
168  newSecMapTrainerBase.param('spTCarrayName', 'checkedSPTCs')
169  newSecMapTrainerBase.param('allowTraining', True)
170 
171  evtStepSize = 100
172 else:
173  merger = register_module('RawSecMapMerger')
174  merger.logging.log_level = trainerVXDTFLogLevel
175  merger.logging.debug_level = trainerVXDTFDebugLevel
176  merger.param('rootFileNames', acceptedRawSecMapFiles)
177  # merger.param('spTCarrayName', 'checkedSPTCs')
178 
179 if useOldTFinstead:
180  evtStepSize = 100
181 
182 geometry = register_module('Geometry')
183 geometry.param('components', ['BeamPipe', 'MagneticFieldConstant4LimitedRSVD',
184  'PXD', 'SVD'])
185 
186 
187 eventCounter = register_module('EventCounter')
188 eventCounter.logging.log_level = LogLevel.INFO
189 eventCounter.param('stepSize', evtStepSize)
190 
191 if useOldTFinstead:
192  tuneValue = 0.06
193  secSetup = [
194  'shiftedL3IssueTestSVDStd-moreThan400MeV_SVD',
195  'shiftedL3IssueTestSVDStd-100to400MeV_SVD',
196  'shiftedL3IssueTestSVDStd-25to100MeV_SVD']
197  if usePXD:
198  secSetup = \
199  ['shiftedL3IssueTestVXDStd-moreThan400MeV_PXDSVD',
200  'shiftedL3IssueTestVXDStd-100to400MeV_PXDSVD',
201  'shiftedL3IssueTestVXDStd-25to100MeV_PXDSVD'
202  ]
203  tuneValue = 0.22
204  vxdtf = register_module('VXDTF') # VXDTF TFRedesign
205  vxdtf.logging.log_level = LogLevel.DEBUG
206  vxdtf.logging.debug_level = 1
207  vxdtf.param('sectorSetup', secSetup)
208  vxdtf.param('GFTrackCandidatesColName', 'caTracks')
209  vxdtf.param('tuneCutoffs', tuneValue)
210  vxdtf.param('displayCollector', 2)
211  if oldTFNoSubsetSelection:
212  vxdtf.param('filterOverlappingTCs', 'none') # shall provide overlapping TCs
213  # vxdtf.param('useTimeSeedAsQI', True) # hack for storing QIs in TimeSeed-Variable for genfit::TrackCand
214 
215  oldAnalyzer = register_module('TFAnalizer')
216  oldAnalyzer.logging.log_level = LogLevel.INFO
217  oldAnalyzer.param('printExtentialAnalysisData', False)
218  oldAnalyzer.param('caTCname', 'caTracks')
219  oldAnalyzer.param('acceptedTCname', 'VXDTFoldAcceptedTCS')
220  oldAnalyzer.param('lostTCname', 'VXDTFoldLostTCS')
221 
222  # TCConverter, genfit -> SPTC
223  trackCandConverter = register_module('GFTC2SPTCConverter')
224  trackCandConverter.logging.log_level = LogLevel.WARNING
225  trackCandConverter.param('genfitTCName', 'caTracks')
226  trackCandConverter.param('SpacePointTCName', 'caSPTCs')
227  trackCandConverter.param('NoSingleClusterSVDSP', 'nosingleSP')
228  trackCandConverter.param('PXDClusterSP', 'pxdOnly')
229  trackCandConverter.param('checkNoSingleSVDSP', True)
230  trackCandConverter.param('checkTrueHits', False)
231  trackCandConverter.param('useSingleClusterSP', False)
232  trackCandConverter.param('skipCluster', True)
233 else:
234  segNetProducer = register_module('SegmentNetworkProducer')
235  segNetProducer.param('CreateNeworks', cNetworks)
236  segNetProducer.param('NetworkOutputName', 'test2Hits')
237  segNetProducer.param('printNetworks', printNetworks)
238  segNetProducer.param('allFiltersOff', switchFiltersOff)
239  # segNetProducer.param('SpacePointsArrayNames', ['pxdOnly', 'nosingleSP'])
240  segNetProducer.param('SpacePointsArrayNames', ['nosingleSP_relTH'])
241  segNetProducer.logging.log_level = TFlogLevel
242  segNetProducer.logging.debug_level = TFDebugLevel
243 
244  if activateSegNetAnalizer:
245  segNetAnalyzer = register_module('SegmentNetworkAnalyzer')
246  segNetAnalyzer.param('networkInputName', 'test2Hits')
247  segNetAnalyzer.param('rootFileName', segNetAnaRFN)
248  segNetAnalyzer.logging.log_level = LogLevel.INFO
249  segNetAnalyzer.logging.debug_level = 100
250 
251  if bypassCA:
252  cellOmat = register_module('TrackFinderVXDBasicPathFinder')
253  else:
254  cellOmat = register_module('TrackFinderVXDCellOMat')
255  cellOmat.param('printNetworks', printNetworks)
256  cellOmat.param('SpacePointTrackCandArrayName', 'caSPTCs')
257  cellOmat.param('NetworkName', 'test2Hits')
258  cellOmat.param('removeVirtualIP', False)
259  cellOmat.param('strictSeeding', doStrictSeeds)
260  cellOmat.logging.log_level = CAlogLevel
261  cellOmat.logging.debug_level = CADebugLevel
262 
263 
264 print("spot 10")
265 
266 vxdAnal = register_module('TrackFinderVXDAnalizer')
267 vxdAnal.param('referenceTCname', 'SPTracks')
268 vxdAnal.param('testTCname', 'caSPTCs')
269 vxdAnal.param('purityThreshold', 0.7)
270 vxdAnal.param('ignoreDeadTCs', ignoreDeadTCs)
271 vxdAnal.param('doEventSummary', doEventSummary)
272 vxdAnal.logging.log_level = AnalizerlogLevel
273 vxdAnal.logging.debug_level = AnalizerDebugLevel
274 
275 print("spot 11")
276 if newTrain:
277  log_to_file('testRedesign' + str(initialValue) + '.log', append=False)
278 else:
279  log_to_file('testsegNetExecute' + str(initialValue) + '.log', append=False)
280 # Create paths
281 main = create_path()
282 
283 
284 main.add_module(rootInputM)
285 main.add_module(eventinfoprinter)
286 main.add_module(gearbox)
287 main.add_module(geometry)
288 main.add_module(eventCounter)
289 main.add_module(secMapBootStrap)
290 
291 setup_spCreatorSVD(path=main, nameOutput='nosingleSP', createSingleClusterSPs=False, logLevel=LogLevel.INFO)
292 setup_spCreatorPXD(path=main, nameOutput='pxdOnly', logLevel=LogLevel.INFO) # needed since 2gftc-converter does not work without it
293 setup_gfTCtoSPTCConverters(
294  path=main,
295  pxdSPs='pxdOnly',
296  svdSPs='nosingleSP',
297  gfTCinput='mcTracks',
298  sptcOutput='checkedSPTCs',
299  usePXD=usePXD,
300  logLevel=LogLevel.WARNING)
301 
302 vIPRemover = register_module('SPTCvirtualIPRemover')
303 vIPRemover.param('maxTCLengthForVIPKeeping', 0) # want to remove virtualIP for any track length
304 vIPRemover.param('tcArrayName', 'caSPTCs')
305 
306 # connect all SpacePoints to all possible TrueHits and store them in a new
307 # StoreArray (to not interfere with the SpacePoints of the reference
308 # TrackCands)
309 setup_sp2thConnector(main, 'pxdOnly', 'nosingleSP', '_relTH', True, LogLevel.ERROR, 1)
310 if newTrain:
311  main.add_module(newSecMapTrainerBase)
312 else:
313  if useOldTFinstead:
314  main.add_module(register_module('SetupGenfitExtrapolation'))
315  main.add_module(vxdtf)
316  main.add_module(oldAnalyzer)
317  main.add_module(trackCandConverter)
318  else:
319  main.add_module(merger)
320  main.add_module(segNetProducer)
321  if trainFBDT: # collect in this step
322  add_fbdtclassifier_training(main, 'test2Hits', 'FBDTClassifier.dat', False, True,
323  False, fbdtSamplesFN, 100, 3, 0.15, 0.5, LogLevel.DEBUG, 10)
324  if useFBDT: # apply the filters
325  add_ml_threehitfilters(main, 'test2Hits', fbdtFN, 0.989351, True)
326  if activateSegNetAnalizer:
327  main.add_module(segNetAnalyzer)
328  main.add_module(cellOmat)
329 
330  if doVirtualIPRemovalb4Fit:
331  main.add_module(vIPRemover)
332 
333  setup_qualityEstimators(main, fitType, 'caSPTCs', LogLevel.INFO, 1)
334  # setup_qualityEstimators(main, fitType, 'SPTracks', LogLevel.DEBUG, 1)
335 
336  if doVirtualIPRemovalb4Fit is False:
337  main.add_module(vIPRemover)
338 
339  if doNewSubsetSelection:
340 
341  tcNetworkProducer = register_module('SPTCNetworkProducer')
342  tcNetworkProducer.param('tcArrayName', 'caSPTCs')
343  tcNetworkProducer.param('tcNetworkName', 'tcNetwork')
344  main.add_module(tcNetworkProducer)
345 
346  tsEvaluator = register_module('TrackSetEvaluatorHopfieldNN')
347  tsEvaluator.logging.log_level = LogLevel.DEBUG
348  tsEvaluator.logging.debug_level = 3
349  tsEvaluator.param('tcArrayName', 'caSPTCs')
350  tsEvaluator.param('tcNetworkName', 'tcNetwork')
351  main.add_module(tsEvaluator)
352 
353  svdOverlapResolver = register_module('SVDOverlapResolver')
354  svdOverlapResolver.param('NameSpacePointTrackCands', 'caSPTCs')
355  svdOverlapResolver.param('resolveMethod', 'greedy')
356  # svdOverlapResolver.param('resolveMethod', 'hopfield')
357  svdOverlapResolver.logging.log_level = LogLevel.DEBUG
358 
359  main.add_module(vxdAnal)
360 
361 if useDisplay:
362  display = register_module('Display')
363  display.param('showAllPrimaries', True)
364  main.add_module(display)
365 
366 # Process events
367 process(main)
368 
369 print(statistics)
setup_modules_ml
tracking.validation.run.TrackingValidationRun.create_path
def create_path(self)
Definition: run.py:114