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