Belle II Software  release-05-01-25
TSF_OldVsNew.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import basf2
5 import numpy as np
6 import os
7 import sys
8 import glob
9 from ROOT import gROOT, Belle2
10 gROOT.ProcessLine("gErrorIgnoreLevel = 4000;") # ignore endless root errors for background files...
11 
12 """
13 Compare the CDCTriggerTSFModule output with the old TRGCDCModule,
14 to confirm that the behaviour of the new code is correct.
15 """
16 
17 # ------------ #
18 # user options #
19 # ------------ #
20 
21 # general options
22 seed = 1
23 evtnum = 100
24 clock = True
25 particlegun_params = {
26  'pdgCodes': [-13, 13],
27  'nTracks': 1,
28  'momentumGeneration': 'inversePt',
29  'momentumParams': [0.3, 10.],
30  'thetaGeneration': 'uniform',
31  'thetaParams': [35, 123],
32  'phiGeneration': 'uniform',
33  'phiParams': [0, 360],
34  'vertexGeneration': 'fixed',
35  'xVertexParams': [0.],
36  'yVertexParams': [0.],
37  'zVertexParams': [0.]}
38 usebkg = False
39 bkgdir = '/sw/belle2/bkg/'
40 
41 # ------------------------- #
42 # create path up to trigger #
43 # ------------------------- #
44 
45 # set random seed
46 basf2.set_random_seed(seed)
47 # suppress messages and warnings during processing:
48 basf2.set_log_level(basf2.LogLevel.ERROR)
49 
50 main = basf2.create_path()
51 
52 main.add_module('EventInfoSetter', evtNumList=evtnum)
53 main.add_module('Progress')
54 main.add_module('Gearbox')
55 main.add_module('Geometry', components=['CDC',
56  'MagneticFieldConstant4LimitedRCDC'])
57 particlegun = basf2.register_module('ParticleGun')
58 particlegun.param(particlegun_params)
59 main.add_module(particlegun)
60 main.add_module('FullSim')
61 if usebkg:
62  bkgmixer = basf2.register_module('BeamBkgMixer')
63  bkgfiles = glob.glob(os.path.join(bkgdir, '*[!(PXD)(ECL)]??.root'))
64  bkgmixer.param('backgroundFiles', bkgfiles)
65  bkgmixer.param('components', ['CDC'])
66  main.add_module(bkgmixer)
67 cdcdigitizer = basf2.register_module('CDCDigitizer')
68 if clock:
69  cdcdigitizer.param('TrigTimeJitter', 32.)
70 main.add_module(cdcdigitizer)
71 
72 # ----------- #
73 # CDC trigger #
74 # ----------- #
75 
76 trgcdc = basf2.register_module('TRGCDC')
77 simMode = 1 # 0: full trigger, 1: only TSF
78 if clock:
79  simMode += 2 # 2: full trigger (TSF with clock), 3: only TSF with clock
80 trgcdc_params = {
81  'ConfigFile': Belle2.FileSystem.findFile("data/trg/cdc/TRGCDCConfig_0_20101111.dat"),
82  'InnerTSLUTFile': Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_v3.0.coe"),
83  'OuterTSLUTFile': Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_v3.0.coe"),
84  'SimulationMode': 1, # only fast simulation
85  'FastSimulationMode': simMode,
86  'HoughFinderMappingFileMinus': Belle2.FileSystem.findFile("data/trg/cdc/HoughMappingMinus20160223.dat"),
87  'HoughFinderMappingFilePlus': Belle2.FileSystem.findFile("data/trg/cdc/HoughMappingPlus20160223.dat")}
88 trgcdc.param(trgcdc_params)
89 if clock:
90  trgcdc.param('inputCollection', 'CDCHits4Trg')
91 main.add_module(trgcdc)
92 
93 tsf = basf2.register_module('CDCTriggerTSF')
94 tsf_params = {
95  'InnerTSLUTFile': Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_v3.0.coe"),
96  'OuterTSLUTFile': Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_v3.0.coe"),
97  'TSHitCollectionName': 'TSHits', # to distinguish from old output
98  'ClockSimulation': clock}
99 tsf.param(tsf_params)
100 if clock:
101  tsf.param('CDCHitCollectionName', 'CDCHits4Trg')
102 main.add_module(tsf)
103 
104 
105 # ----------- #
106 # test module #
107 # ----------- #
108 
109 class TestModule(basf2.Module):
110  """
111  test module to compare the output of TRGCDC and CDCTriggerTSF
112  """
113 
114  def event(self):
115  """
116  give info for both output lists and warnings in the case of mismatches
117  """
118  oldHits = Belle2.PyStoreArray("CDCTriggerSegmentHits")
119  newHits = Belle2.PyStoreArray("TSHits")
120  if oldHits.getEntries() == newHits.getEntries():
121  basf2.B2INFO("%d hits" % oldHits.getEntries())
122  else:
123  basf2.B2WARNING("old version: %d, new version: %d" %
124  (oldHits.getEntries(), newHits.getEntries()))
125  for i in range(max(oldHits.getEntries(), newHits.getEntries())):
126  if i < oldHits.getEntries():
127  oldString = "ID %d priority %d LR %d fastest T %d priority T %d found T %d" % \
128  (oldHits[i].getSegmentID(), oldHits[i].getPriorityPosition(),
129  oldHits[i].getLeftRight(), oldHits[i].fastestTime(),
130  oldHits[i].priorityTime(), oldHits[i].foundTime())
131  else:
132  oldString = "no hit"
133  if i < newHits.getEntries():
134  newString = "ID %d priority %d LR %d fastest T %d priority T %d found T %d" % \
135  (newHits[i].getSegmentID(), newHits[i].getPriorityPosition(),
136  newHits[i].getLeftRight(), newHits[i].fastestTime(),
137  newHits[i].priorityTime(), newHits[i].foundTime())
138  else:
139  newString = "no hit"
140  if oldString == newString:
141  basf2.B2INFO(oldString)
142  else:
143  basf2.B2WARNING("old: " + oldString)
144  basf2.B2WARNING("new: " + newString)
145  # check relations
146  if clock:
147  oldCDCRels = oldHits[i].getRelationsTo("CDCHits4Trg")
148  newCDCRels = newHits[i].getRelationsTo("CDCHits4Trg")
149  else:
150  oldCDCRels = oldHits[i].getRelationsTo("CDCHits")
151  newCDCRels = newHits[i].getRelationsTo("CDCHits")
152  if len(oldCDCRels) == len(newCDCRels):
153  basf2.B2INFO("%d related CDCHits" % len(oldCDCRels))
154  else:
155  basf2.B2WARNING("old version: %d related CDCHits" % len(oldCDCRels))
156  basf2.B2WARNING("new version: %d related CDCHits" % len(newCDCRels))
157  for irel in range(max(len(oldCDCRels), len(newCDCRels))):
158  if irel < len(oldCDCRels):
159  oldString = "relation to hit %d, weight %.1f" % \
160  (oldCDCRels[irel].getArrayIndex(), oldCDCRels.weight(irel))
161  else:
162  oldString = "no relation"
163  if irel < len(newCDCRels):
164  newString = "relation to hit %d, weight %.1f" % \
165  (newCDCRels[irel].getArrayIndex(), newCDCRels.weight(irel))
166  else:
167  newString = "no relation"
168  if oldString == newString:
169  basf2.B2INFO(oldString)
170  else:
171  basf2.B2WARNING(oldString)
172  basf2.B2WARNING(newString)
173 
174 
175 main.add_module(TestModule(), logLevel=basf2.LogLevel.INFO)
176 
177 # Process events
178 basf2.process(main)
179 
180 # Print call statistics
181 print(basf2.statistics)
TSF_OldVsNew.TestModule
Definition: TSF_OldVsNew.py:109
TSF_OldVsNew.TestModule.event
def event(self)
Definition: TSF_OldVsNew.py:114
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
Belle2::FileSystem::findFile
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:147