Belle II Software  release-08-01-10
neurotrigger.py
1 
8 
9 import basf2
10 from ROOT import Belle2
11 import nntd
12 
13 
16 
17 # standard name for hardware tracks coming from the unpacker
18 hwneurotracks = 'CDCTriggerNeuroTracks'
19 hwneuroinput2dfindertracks = 'CDCTriggerNNInput2DFinderTracks'
20 hwneuroinputsegmenthits = 'CDCTriggerNNInputSegmentHits'
21 
22 
23 # sofware 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
28 hwsimneurotracks = '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.
32 simneurotracks_swtssw2d = 'TRGCDCNeuroTracks'
33 simsegmenthits = 'SimSegmentHits'
34 sim2dtracks_swts = 'TRGCDC2DFinderTracks'
35 
36 
39 
40 
41 def 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 
48 class casefilter(basf2.Module):
49  def initialize(self):
50  self.filterarraynamefilterarrayname = "RecoTracks"
51  self.lowerthanlowerthan = 0
52  self.higherthanhigherthan = -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.lowerthanlowerthan == 0 or bool(len(Belle2.PyStoreArray(self.filterarraynamefilterarrayname)) < self.lowerthanlowerthan)
60  higher = bool(len(Belle2.PyStoreArray(self.filterarraynamefilterarrayname)) > self.higherthanhigherthan)
61  self.return_value(lower and higher)
62 
63 
64 lt2reco = basf2.register_module(casefilter())
65 lt2reco.param({"filterarrayname": "RecoTracks", "lowerthan": 2})
66 
67 lt4reco = basf2.register_module(casefilter())
68 lt4reco.param({"filterarrayname": "RecoTracks", "lowerthan": 4})
69 
70 
71 class nnt_eventfilter(basf2.Module):
72  def initialize(self):
73  self.tracksegmentsnametracksegmentsname = hwneuroinputsegmenthits,
74  self.twodtracksnametwodtracksname = hwneuroinput2dfindertracks,
75  self.neurotracksnameneurotracksname = hwneurotracks,
76  self.recotracksnamerecotracksname = "RecoTracks"
77  self.nullpathnullpath = basf2.create_path()
78 
79  def event(self):
80  self.return_value(bool(self.neurotrack_allgoodqualityneurotrack_allgoodquality()))
81  self.if_false(self.nullpathnullpath)
82 
83  def hastrginfo(self):
84  return bool(Belle2.PyStoreArray(self.twodtracksnametwodtracksname).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 
95 class randommaker(basf2.Module):
96  def initialize(self):
97  self.countercounter = 0
98 
99  def event(self):
100  # print("counter is " + str(self.counter))
101  if self.countercounter % 100 == 0:
102  # print("case 0")
103  self.return_value(0)
104  elif self.countercounter % 3 == 0:
105  self.return_value(1)
106  elif self.countercounter % 3 == 1:
107  self.return_value(2)
108  elif self.countercounter % 3 == 2:
109  self.return_value(3)
110  else:
111  print("some kind of error")
112  self.countercounter += 1
113 
114 
115 def 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 
160 def 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 
195 def 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 
239 def 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 
262 def 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 
302 def 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 
343 def add_neurotrigger_sim(path, nntweightfile=None, debug_level=4, debugout=False, **kwargs):
344  nnt = basf2.register_module('CDCTriggerNeuro')
345  if 'inputCollectionName' in kwargs:
346  nnt.param('inputCollectionName', kwargs['inputCollectionName'])
347  else:
348  nnt.param('inputCollectionName', hwneuroinput2dfindertracks)
349  if 'outputCollectionName' in kwargs:
350  nnt.param('outputCollectionName', kwargs['outputCollectionName'])
351  else:
352  nnt.param('outputCollectionName', hwsimneurotracks)
353  if 'hitCollectionName' in kwargs:
354  nnt.param('hitCollectionName', kwargs['hitCollectionName'])
355  else:
356  nnt.param('hitCollectionName', hwneuroinputsegmenthits)
357  if 'writeMLPinput' in kwargs:
358  nnt.param('writeMLPinput', kwargs['writeMLPinput'])
359  else:
360  nnt.param('writeMLPinput', True)
361  if 'fixedPoint' in kwargs:
362  nnt.param('fixedPoint', kwargs['fixedPoint'])
363  else:
364  nnt.param('fixedPoint', True)
365  if nntweightfile is not None:
366  nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
367 
368  if 'et_option' in kwargs:
369  nnt.param('et_option', kwargs['et_option'])
370  if 'EventTimeName' in kwargs:
371  nnt.param('EventTimeName', kwargs['EventTimeName'])
372  if debugout:
373  nnt.logging.log_level = basf2.LogLevel.DEBUG
374  nnt.logging.debug_level = debug_level
375  path.add_module(nnt)
376 
377 
378 def add_neurotrigger_hw(path, nntweightfile=None, debug_level=4, debugout=False, **kwargs):
379  nnt = basf2.register_module('CDCTriggerNeuro')
380  if 'inputCollectionName' in kwargs:
381  nnt.param('inputCollectionName', kwargs['inputCollectionName'])
382  else:
383  nnt.param('inputCollectionName', hwneurotracks)
384  if 'outputCollectionName' in kwargs:
385  nnt.param('outputCollectionName', kwargs['outputCollectionName'])
386  else:
387  nnt.param('outputCollectionName', hwsimneurotracks)
388  if 'hitCollectionName' in kwargs:
389  nnt.param('hitCollectionName', kwargs['hitCollectionName'])
390  else:
391  nnt.param('hitCollectionName', hwneuroinputsegmenthits)
392  if 'writeMLPinput' in kwargs:
393  nnt.param('writeMLPinput', kwargs['writeMLPinput'])
394  else:
395  nnt.param('writeMLPinput', True)
396  if 'fixedPoint' in kwargs:
397  nnt.param('fixedPoint', kwargs['fixedPoint'])
398  else:
399  nnt.param('fixedPoint', True)
400  if 'realinputCollectonName' in kwargs:
401  nnt.param('realinputCollectionName', kwargs['realinputCollectionName'])
402  else:
403  nnt.param('realinputCollectionName', hwneuroinput2dfindertracks)
404 
405  if nntweightfile is not None:
406  nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
407  nnt.param('NeuroHWTrackInputMode', True)
408  if 'et_option' in kwargs:
409  nnt.param('et_option', kwargs['et_option'])
410  else:
411  nnt.param('et_option', 'etfhwin')
412 
413  if 'EventTimeName' in kwargs:
414  nnt.param('EventTimeName', kwargs['EventTimeName'])
415  else:
416  nnt.param('EventTimeName', 'CDCTriggerNeuroETFT0')
417  if debugout:
418  nnt.logging.log_level = basf2.LogLevel.DEBUG
419  nnt.logging.debug_level = debug_level
420  path.add_module(nnt)
421 
422 
423 def add_neuro_simulation(path, nntweightfile=None, **kwargs):
424  nnt = basf2.register_module('CDCTriggerNeuro')
425  tsf = basf2.register_module('CDCTriggerTSF')
426  if "InnerTSLUTFile" in kwargs:
427  tsf.param("InnerTSLUTFile", kwargs["InnerTSLUTFile"])
428  else:
429  tsf.param("InnerTSLUTFile", Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_Bkg_p0.70_b0.80.coe"))
430  if "OuterTSLUTFile" in kwargs:
431  tsf.param("OuterTSLUTFile", kwargs["OuterTSLUTFile"])
432  else:
433  tsf.param("OuterTSLUTFile", Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_Bkg_p0.70_b0.80.coe"))
434  tsf.param("TSHitCollectionName", simsegmenthits)
435  tsf.param("CDCHitCollectionName", "CDCHits")
436  if "relateAllHits" in kwargs:
437  tsf.param("relateAllHits", kwargs["relateAllHits"])
438  if "makeRecoLRTable" in kwargs:
439  tsf.param("makeRecoLRTable", kwargs["makeRecoLRTable"])
440  if "outerRecoLRTableFilename" in kwargs:
441  tsf.param("outerRecoLRTableFilename", kwargs["outerRecoLRTableFilename"])
442  if "innerRecoLRTableFilename" in kwargs:
443  tsf.param("innerRecoLRTableFilename", kwargs["innerRecoLRTableFilename"])
444  if "makeTrueLRTable" in kwargs:
445  tsf.param("makeTrueLRTable", kwargs["makeTrueLRTable"])
446  path.add_module(tsf)
447  path.add_module('CDCTrigger2DFinder',
448  minHits=4, minHitsShort=4, minPt=0.3,
449  hitCollectionName=simsegmenthits,
450  outputCollectionName=sim2dtracks_swts)
451  if nntweightfile is not None:
452  nnt.param('filename', Belle2.FileSystem.findFile(nntweightfile))
453  if 'et_option' in kwargs:
454  nnt.param('et_option', kwargs['et_option'])
455  nnt.param('inputCollectionName', sim2dtracks_swts)
456  nnt.param('outputCollectionName', simneurotracks_swtssw2d)
457  nnt.param('hitCollectionName', simsegmenthits)
458  nnt.param('writeMLPinput', True)
459  nnt.param('fixedPoint', False)
460  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...
Definition: FileSystem.cc:148
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72