Belle II Software prerelease-10-00-00a
neurotrigger.py
1
8
9import basf2
10from ROOT import Belle2
11import nntd
12
13
16
17# standard name for hardware tracks coming from the unpacker
18hwneurotracks = 'CDCTriggerNeuroTracks'
19hwneuroinput2dfindertracks = 'CDCTriggerNNInput2DFinderTracks'
20hwneuroinputsegmenthits = 'CDCTriggerNNInputSegmentHits'
21
22
23# software neurotracks, that were calculated from hardware 2d-inputtracks
24# and hardware stereo track segments coming over all 48cc within one event
25# OR
26# standard name for tracks resulting from the software neurotrigger
27# module when it is rerun on the hardware tracks
28hwsimneurotracks = 'TSimNeuroTracks'
29
30# software neurotracks, which were all simulated in software starting only
31# from real CDCHITs. Also simulated track segments and 2dtracks are needed therefore.
32simneurotracks_swtssw2d = 'TRGCDCNeuroTracks'
33simsegmenthits = 'SimSegmentHits'
34sim2dtracks_swts = 'TRGCDC2DFinderTracks'
35
36
39
40
41def filterTRG(path):
42 nullpath = basf2.create_path()
43 mfilter = basf2.register_module("CDCTriggerUnpacker", unpackNeuro=True)
44 path.add_module(mfilter)
45 mfilter.if_value('<1', nullpath)
46
47
48class casefilter(basf2.Module):
49 def initialize(self):
50 self.filterarrayname = "RecoTracks"
51 self.lowerthan = 0
52 self.higherthan = -1
53
54 def param(self, params):
55 for key, value in params.items():
56 setattr(self, key, value)
57
58 def event(self):
59 lower = self.lowerthan == 0 or bool(len(Belle2.PyStoreArray(self.filterarrayname)) < self.lowerthan)
60 higher = bool(len(Belle2.PyStoreArray(self.filterarrayname)) > self.higherthan)
61 self.return_value(lower and higher)
62
63
64lt2reco = basf2.register_module(casefilter())
65lt2reco.param({"filterarrayname": "RecoTracks", "lowerthan": 2})
66
67lt4reco = basf2.register_module(casefilter())
68lt4reco.param({"filterarrayname": "RecoTracks", "lowerthan": 4})
69
70
71class nnt_eventfilter(basf2.Module):
72 def initialize(self):
73 self.tracksegmentsname = hwneuroinputsegmenthits,
74 self.twodtracksname = hwneuroinput2dfindertracks,
75 self.neurotracksname = hwneurotracks,
76 self.recotracksname = "RecoTracks"
77 self.nullpath = basf2.create_path()
78
79 def event(self):
80 self.return_value(bool(self.neurotrack_allgoodquality()))
81 self.if_false(self.nullpath)
82
83 def hastrginfo(self):
84 return bool(Belle2.PyStoreArray(self.twodtracksname).getEntries() > 0)
85
86 def neurotrack_allgoodquality(self):
87 isgoodquality = True
88 for tr in Belle2.PyStoreArray("CDCTriggerNeuroTracks"):
89 if tr.getQualityVector() > 0:
90 isgoodquality = False
91 break
92 return isgoodquality
93
94
95class randommaker(basf2.Module):
96 def initialize(self):
97 self.counter = 0
98
99 def event(self):
100 # print("counter is " + str(self.counter))
101 if self.counter % 100 == 0:
102 # print("case 0")
103 self.return_value(0)
104 elif self.counter % 3 == 0:
105 self.return_value(1)
106 elif self.counter % 3 == 1:
107 self.return_value(2)
108 elif self.counter % 3 == 2:
109 self.return_value(3)
110 else:
111 print("some kind of error")
112 self.counter += 1
113
114
115def add_nnta_gzip_test_output(
116 path,
117 baseAnaFileName,
118 configFileName,
119 baseGzipFileName,
120 baseOutputFileName,
121 excludeBranchNames=[],
122 branchNames=[]):
123 # create 4 output paths:
124 outpaths = []
125 for x in range(4):
126 outpaths.append([".random_"+str(x), basf2.create_path()])
127 # add the randommaker module:
128 rm = basf2.register_module(randommaker())
129 path.add_module(rm)
130
131 # also add nnta module:
132
133 rm.if_value('==0', outpaths[0][1])
134 rm.if_value('==1', outpaths[1][1])
135 rm.if_value('==2', outpaths[2][1])
136 rm.if_value('==3', outpaths[3][1])
137 for p in outpaths:
138 nnta = basf2.register_module(nntd.nntd())
139 nnta.param({"filename": baseAnaFileName+p[0]+".pkl", "netname": "hardware"})
140 p[1].add_module(nnta)
141 p[1].add_module('CDCTriggerNeuroData',
142 # input and target arrays
143 NeuroTrackInputMode=False,
144 inputCollectionName=hwneuroinput2dfindertracks, # the hardware input tracks
145 # inputCollectionName=sim2dtracks_swts, # the software 2dtracks from real hits
146 # inputCollectionName='TRGCDC2DFinderTracks', # the mcparticle based 2d tracks
147 hitCollectionName=hwneuroinputsegmenthits, # simsegmenthits, #'SimSegmentHits',
148 EventTimeName="CDCTriggerNeuroETFT0",
149 targetCollectionName='RecoTracks',
150 trainOnRecoTracks=True,
151 gzipFilename=baseGzipFileName+p[0]+".gz",
152 configFileName=configFileName,
153 writeconfigFileName=baseGzipFileName.split("/gzip")[0]+"/"+configFileName.split('/')[-1],
154 )
155 if p[0] == ".random_3":
156 p[1].add_module("RootOutput", outputFileName=baseOutputFileName +
157 p[0]+".root", excludeBranchNames=excludeBranchNames, branchNames=branchNames)
158
159
160def add_nnta_gzip_output(path, baseAnaFileName, configFileName, baseGzipFileName):
161 # create 4 output paths:
162 outpaths = []
163 for x in range(4):
164 outpaths.append([".random_"+str(x), basf2.create_path()])
165 # add the randommaker module:
166 rm = basf2.register_module(randommaker())
167 path.add_module(rm)
168
169 # also add nnta module:
170
171 rm.if_value('==0', outpaths[0][1])
172 rm.if_value('==1', outpaths[1][1])
173 rm.if_value('==2', outpaths[2][1])
174 rm.if_value('==3', outpaths[3][1])
175 for p in outpaths:
176 nnta = basf2.register_module(nntd.nntd())
177 nnta.param({"filename": baseAnaFileName+p[0]+".pkl", "netname": "hardware"})
178 p[1].add_module(nnta)
179 p[1].add_module('CDCTriggerNeuroData',
180 # input and target arrays
181 NeuroTrackInputMode=False,
182 inputCollectionName=hwneuroinput2dfindertracks, # the hardware input tracks
183 # inputCollectionName=sim2dtracks_swts, # the software 2dtracks from real hits
184 # inputCollectionName='TRGCDC2DFinderTracks', # the mcparticle based 2d tracks
185 hitCollectionName=hwneuroinputsegmenthits, # simsegmenthits, #'SimSegmentHits',
186 EventTimeName="CDCTriggerNeuroETFT0",
187 targetCollectionName='RecoTracks',
188 trainOnRecoTracks=True,
189 gzipFilename=baseGzipFileName+p[0]+".gz",
190 configFileName=configFileName,
191 writeconfigFileName=baseGzipFileName.split("/gzip")[0]+"/"+configFileName.split('/')[-1],
192 )
193
194
195def add_all_output(
196 path,
197 baseOutputFileName,
198 baseAnaFileName,
199 configFileName,
200 baseGzipFileName,
201 excludeBranchNames=[],
202 branchNames=[]):
203 # create 4 output paths:
204 outpaths = []
205 for x in range(4):
206 outpaths.append([".random_"+str(x), basf2.create_path()])
207 # add the randommaker module:
208 rm = basf2.register_module(randommaker())
209 path.add_module(rm)
210
211 # also add nnta module:
212
213 rm.if_value('==0', outpaths[0][1])
214 rm.if_value('==1', outpaths[1][1])
215 rm.if_value('==2', outpaths[2][1])
216 rm.if_value('==3', outpaths[3][1])
217 for p in outpaths:
218 nnta = basf2.register_module(nntd.nntd())
219 nnta.param({"filename": baseAnaFileName+p[0]+".pkl", "netname": "hardware"})
220 p[1].add_module("RootOutput", outputFileName=baseOutputFileName +
221 p[0]+".root", excludeBranchNames=excludeBranchNames, branchNames=branchNames)
222 p[1].add_module(nnta)
223 p[1].add_module('CDCTriggerNeuroData',
224 # input and target arrays
225 NeuroTrackInputMode=False,
226 inputCollectionName=hwneuroinput2dfindertracks, # the hardware input tracks
227 # inputCollectionName=sim2dtracks_swts, # the software 2dtracks from real hits
228 # inputCollectionName='TRGCDC2DFinderTracks', # the mcparticle based 2d tracks
229 hitCollectionName=hwneuroinputsegmenthits, # simsegmenthits, #'SimSegmentHits',
230 EventTimeName="CDCTriggerNeuroETFT0",
231 targetCollectionName='RecoTracks',
232 trainOnRecoTracks=True,
233 gzipFilename=baseGzipFileName+p[0]+".gz",
234 configFileName=configFileName,
235 writeconfigFileName=baseGzipFileName.split("/gzip")[0]+"/"+configFileName.split('/')[-1],
236 )
237
238
239def add_nnta_root_output(path, baseOutputFileName, baseAnaFileName, excludeBranchNames=[], branchNames=[]):
240 # create 4 output paths:
241 outpaths = []
242 for x in range(4):
243 outpaths.append([".random_"+str(x), basf2.create_path()])
244 # add the randommaker module:
245 rm = basf2.register_module(randommaker())
246 path.add_module(rm)
247
248 # also add nnta module:
249
250 rm.if_value('==0', outpaths[0][1])
251 rm.if_value('==1', outpaths[1][1])
252 rm.if_value('==2', outpaths[2][1])
253 rm.if_value('==3', outpaths[3][1])
254 for p in outpaths:
255 nnta = basf2.register_module(nntd.nntd())
256 nnta.param({"filename": baseAnaFileName+p[0]+".pkl", "netname": "hardware"})
257 p[1].add_module("RootOutput", outputFileName=baseOutputFileName +
258 p[0]+".root", excludeBranchNames=excludeBranchNames, branchNames=branchNames)
259 p[1].add_module(nnta)
260
261
262def add_neuro_unpacker(path, debug_level=4, debugout=False, **kwargs):
263 #
264 unpacker = basf2.register_module('CDCTriggerUnpacker')
265 if debugout:
266 unpacker.logging.log_level = basf2.LogLevel.DEBUG
267 unpacker.logging.debug_level = debug_level
268 unpacker.logging.set_info(basf2.LogLevel.DEBUG, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
269 # size (number of words) of the Belle2Link header
270 unpacker.param('headerSize', 3)
271 # unpack the data from the 2D tracker and save its Bitstream
272 unpacker.param('unpackTracker2D', False)
273 # make CDCTriggerTrack and CDCTriggerSegmentHit objects from the 2D output
274 unpacker.param('decode2DFinderTrack', False)
275 # make CDCTriggerSegmentHit objects from the 2D input
276 unpacker.param('decode2DFinderInput', False)
277 unpacker.param('2DNodeId', [
278 [0x11000001, 0],
279 [0x11000001, 1],
280 [0x11000002, 0],
281 [0x11000002, 1]])
282 unpacker.param('NeuroNodeId', [
283 [0x11000005, 0],
284 [0x11000005, 1],
285 [0x11000006, 0],
286 [0x11000006, 1],
287 ])
288 if 'useDB' in kwargs:
289 unpacker.param('useDB', kwargs['useDB'])
290 else:
291 unpacker.param('useDB', True)
292
293 if 'sim13dt' in kwargs:
294 unpacker.param('sim13dt', kwargs['sim13dt'])
295 else:
296 unpacker.param('sim13dt', False)
297 unpacker.param('unpackNeuro', True)
298 unpacker.param('decodeNeuro', True)
299 path.add_module(unpacker)
300
301
302def add_neuro_2d_unpackers(path, debug_level=4, debugout=False, **kwargs):
303 #
304 unpacker = basf2.register_module('CDCTriggerUnpacker')
305 if debugout:
306 unpacker.logging.log_level = basf2.LogLevel.DEBUG
307 unpacker.logging.debug_level = debug_level
308 unpacker.logging.set_info(basf2.LogLevel.DEBUG, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
309 # size (number of words) of the Belle2Link header
310 unpacker.param('headerSize', 3)
311 # unpack the data from the 2D tracker and save its Bitstream
312 unpacker.param('unpackTracker2D', True)
313 # make CDCTriggerTrack and CDCTriggerSegmentHit objects from the 2D output
314 unpacker.param('decode2DFinderTrack', True)
315 # make CDCTriggerSegmentHit objects from the 2D input
316 unpacker.param('decode2DFinderInput', True)
317 unpacker.param('2DNodeId', [
318 [0x11000001, 0],
319 [0x11000001, 1],
320 [0x11000002, 0],
321 [0x11000002, 1],
322 ])
323 unpacker.param('NeuroNodeId', [
324 [0x11000005, 0],
325 [0x11000005, 1],
326 [0x11000006, 0],
327 [0x11000006, 1],
328 ])
329 if 'useDB' in kwargs:
330 unpacker.param('useDB', kwargs['useDB'])
331 else:
332 unpacker.param('useDB', True)
333
334 if 'sim13dt' in kwargs:
335 unpacker.param('sim13dt', kwargs['sim13dt'])
336 else:
337 unpacker.param('sim13dt', False)
338 unpacker.param('unpackNeuro', True)
339 unpacker.param('decodeNeuro', True)
340 path.add_module(unpacker)
341
342 if (('unpackDNN' in kwargs) and (kwargs['unpackDNN'])):
343 # DNN unpacker, using same module
344 DNN_unpacker = basf2.register_module('CDCTriggerUnpacker')
345 if debugout:
346 DNN_unpacker.logging.log_level = basf2.LogLevel.DEBUG
347 DNN_unpacker.logging.debug_level = debug_level
348 DNN_unpacker.logging.set_info(basf2.LogLevel.DEBUG, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
349
350 # size (number of words) of the Belle2Link header
351 DNN_unpacker.param('headerSize', 3)
352 # always use DB for DNNT
353 DNN_unpacker.param('useDB', True)
354 # sim 13 bit drift time if required
355 if 'sim13dt' in kwargs:
356 DNN_unpacker.param('sim13dt', kwargs['sim13dt'])
357 else:
358 DNN_unpacker.param('sim13dt', False)
359 # DNN board ID
360 DNN_unpacker.param('NeuroNodeId_pcie40', [
361 # UT4 3D
362 [0x10000001, 4],
363 [0x10000001, 5],
364 [0x10000001, 6],
365 [0x10000001, 7]])
366 # unpack DNN track, decodeing and related 2D track
367 DNN_unpacker.param('unpackNeuro', True)
368 DNN_unpacker.param('decodeNeuro', True)
369 DNN_unpacker.param('unpackMerger', True)
370 DNN_unpacker.param('unpackTracker2D', True)
371 DNN_unpacker.param('decode2DFinderTrack', True)
372 # DNN track default name
373 DNN_unpacker.param("NNTrackName", "CDCTriggerDNNTracks")
374 # DNN relate TS default name
375 DNN_unpacker.param("NNInStereoTS", "CDCTriggerDNNInputAllStereoSegmentHits")
376 # DNN select TS default name
377 DNN_unpacker.param("NNInSelectTS", "CDCTriggerDNNInputSegmentHits")
378 # DNN 2D input
379 DNN_unpacker.param("NNIn2DTrack", "CDCTriggerDNNInput2DFinderTracks")
380 # DNN ETF input
381 DNN_unpacker.param("NNInETFT0", "CDCTriggerDNNETFT0")
382 # DNN scaled input
383 DNN_unpacker.param("NNScaledInput", "CDCTriggerDNNTracksInput")
384 # DNNT bit map
385 DNN_unpacker.param("configName", "DNNT_Config")
386 # flag to use DNN unpacker
387 DNN_unpacker.param("isDNN", True)
388 path.add_module(DNN_unpacker)
389
390
391def add_neurotrigger_sim(path, nntweightfile=None, debug_level=4, debugout=False, **kwargs):
392 nnt = basf2.register_module('CDCTriggerNeuro')
393 if 'inputCollectionName' in kwargs:
394 nnt.param('inputCollectionName', kwargs['inputCollectionName'])
395 else:
396 nnt.param('inputCollectionName', hwneuroinput2dfindertracks)
397 if 'outputCollectionName' in kwargs:
398 nnt.param('outputCollectionName', kwargs['outputCollectionName'])
399 else:
400 nnt.param('outputCollectionName', hwsimneurotracks)
401 if 'hitCollectionName' in kwargs:
402 nnt.param('hitCollectionName', kwargs['hitCollectionName'])
403 else:
404 nnt.param('hitCollectionName', hwneuroinputsegmenthits)
405 if 'writeMLPinput' in kwargs:
406 nnt.param('writeMLPinput', kwargs['writeMLPinput'])
407 else:
408 nnt.param('writeMLPinput', True)
409 if 'fixedPoint' in kwargs:
410 nnt.param('fixedPoint', kwargs['fixedPoint'])
411 else:
412 nnt.param('fixedPoint', True)
413 if nntweightfile is not None:
414 nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
415
416 if 'et_option' in kwargs:
417 nnt.param('et_option', kwargs['et_option'])
418 if 'EventTimeName' in kwargs:
419 nnt.param('EventTimeName', kwargs['EventTimeName'])
420 if debugout:
421 nnt.logging.log_level = basf2.LogLevel.DEBUG
422 nnt.logging.debug_level = debug_level
423 path.add_module(nnt)
424
425
426def add_neurotrigger_hw(path, nntweightfile=None, debug_level=4, debugout=False, **kwargs):
427 nnt = basf2.register_module('CDCTriggerNeuro')
428 if 'inputCollectionName' in kwargs:
429 nnt.param('inputCollectionName', kwargs['inputCollectionName'])
430 else:
431 nnt.param('inputCollectionName', hwneurotracks)
432 if 'outputCollectionName' in kwargs:
433 nnt.param('outputCollectionName', kwargs['outputCollectionName'])
434 else:
435 nnt.param('outputCollectionName', hwsimneurotracks)
436 if 'hitCollectionName' in kwargs:
437 nnt.param('hitCollectionName', kwargs['hitCollectionName'])
438 else:
439 nnt.param('hitCollectionName', hwneuroinputsegmenthits)
440 if 'writeMLPinput' in kwargs:
441 nnt.param('writeMLPinput', kwargs['writeMLPinput'])
442 else:
443 nnt.param('writeMLPinput', True)
444 if 'fixedPoint' in kwargs:
445 nnt.param('fixedPoint', kwargs['fixedPoint'])
446 else:
447 nnt.param('fixedPoint', True)
448 if 'realinputCollectonName' in kwargs:
449 nnt.param('realinputCollectionName', kwargs['realinputCollectionName'])
450 else:
451 nnt.param('realinputCollectionName', hwneuroinput2dfindertracks)
452
453 if nntweightfile is not None:
454 nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
455 nnt.param('NeuroHWTrackInputMode', True)
456 if 'et_option' in kwargs:
457 nnt.param('et_option', kwargs['et_option'])
458 else:
459 nnt.param('et_option', 'etfhwin')
460
461 if 'EventTimeName' in kwargs:
462 nnt.param('EventTimeName', kwargs['EventTimeName'])
463 else:
464 nnt.param('EventTimeName', 'CDCTriggerNeuroETFT0')
465 if debugout:
466 nnt.logging.log_level = basf2.LogLevel.DEBUG
467 nnt.logging.debug_level = debug_level
468 path.add_module(nnt)
469
470
471def add_neuro_simulation(path, nntweightfile=None, **kwargs):
472 nnt = basf2.register_module('CDCTriggerNeuro')
473 tsf = basf2.register_module('CDCTriggerTSF')
474 if "InnerTSLUTFile" in kwargs:
475 tsf.param("InnerTSLUTFile", kwargs["InnerTSLUTFile"])
476 else:
477 tsf.param("InnerTSLUTFile", Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_Bkg_p0.70_b0.80.coe"))
478 if "OuterTSLUTFile" in kwargs:
479 tsf.param("OuterTSLUTFile", kwargs["OuterTSLUTFile"])
480 else:
481 tsf.param("OuterTSLUTFile", Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_Bkg_p0.70_b0.80.coe"))
482 tsf.param("TSHitCollectionName", simsegmenthits)
483 tsf.param("CDCHitCollectionName", "CDCHits")
484 if "relateAllHits" in kwargs:
485 tsf.param("relateAllHits", kwargs["relateAllHits"])
486 if "makeRecoLRTable" in kwargs:
487 tsf.param("makeRecoLRTable", kwargs["makeRecoLRTable"])
488 if "outerRecoLRTableFilename" in kwargs:
489 tsf.param("outerRecoLRTableFilename", kwargs["outerRecoLRTableFilename"])
490 if "innerRecoLRTableFilename" in kwargs:
491 tsf.param("innerRecoLRTableFilename", kwargs["innerRecoLRTableFilename"])
492 if "makeTrueLRTable" in kwargs:
493 tsf.param("makeTrueLRTable", kwargs["makeTrueLRTable"])
494 path.add_module(tsf)
495 path.add_module('CDCTrigger2DFinder',
496 minHits=4, minHitsShort=4, minPt=0.3,
497 hitCollectionName=simsegmenthits,
498 outputCollectionName=sim2dtracks_swts)
499 if nntweightfile is not None:
500 nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
501 if 'et_option' in kwargs:
502 nnt.param('et_option', kwargs['et_option'])
503 nnt.param('inputCollectionName', sim2dtracks_swts)
504 nnt.param('outputCollectionName', simneurotracks_swtssw2d)
505 nnt.param('hitCollectionName', simsegmenthits)
506 nnt.param('writeMLPinput', True)
507 nnt.param('fixedPoint', False)
508 path.add_module(nnt)
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...
A (simplified) python wrapper for StoreArray.