Belle II Software development
ndFinderTest.py
1#!/usr/bin/env python3
2
3
10
11import basf2
12from generators import add_phokhara_generator
13from simulation import add_simulation
14from reconstruction import add_reconstruction
15from ROOT import Belle2
16import numpy as np
17import glob
18
19
20def addGen(main, args):
21 main.add_module('EventInfoSetter',
22 runList=[1],
23 expList=[0],
24 evtNumList=args.nevents
25 )
26 main.add_module('Progress')
27 main.add_module('Gearbox')
28 main.add_module('Geometry')
29 add_phokhara_generator(main, finalstate=args.finalState)
30
31 if args.bkgfiles is not None:
32 def strip(x): return x.split('/')[-1].split('.')[0]
33 bkgraws = args.bkgfiles.strip("'")
34 bkgfiles = glob.glob(bkgraws)
35 print('bkgfiles', bkgfiles)
36 add_simulation(main, bkgfiles=bkgfiles)
37 else:
38 add_simulation(main)
39 add_reconstruction(main)
40
41
42def addRead(main, args):
43 main.add_module('RootInput', inputFileName=args.genFileName)
44 main.add_module('Progress')
45 main.add_module('Gearbox')
46 main.add_module('Geometry')
47
48
49def add_cdc_finders(path):
50 """
51 This function adds the CDC trigger TSF and Finder modules to a path.
52 @path modules are added to this path
53 """
54 # TSF
55 InTS = Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_Bkg_p0.70_b0.80.coe")
56 OutTS = Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_Bkg_p0.70_b0.80.coe")
57 path.add_module('CDCTriggerTSF',
58 InnerTSLUTFile=InTS,
59 OuterTSLUTFile=OutTS,
60 TSHitCollectionName="CDCTriggerSegmentHits")
61 # 2D finder
62 path.add_module('CDCTrigger2DFinder')
63 # 3D finder
64 axialFile = Belle2.FileSystem.findFile(args.axialFile)
65 stereoFile = Belle2.FileSystem.findFile(args.stereoFile)
66 path.add_module("CDCTriggerNDFinder",
67 logLevel=basf2.LogLevel.DEBUG, debugLevel=1000,
68 minhits=args.minhits,
69 minweight=args.minweight,
70 minpts=args.minpts,
71 mincells=args.mincells,
72 minhits_axial=args.minhits_axial,
73 minassign=args.minassign,
74 thresh=args.thresh,
75 diagonal=args.diagonal,
76 axialFile=axialFile,
77 stereoFile=stereoFile,
78 verbose=args.verbose
79 )
80
81
82
83
84class QuickCheck(basf2.Module):
85 def initialize(self):
86 self.storen = np.zeros(10, dtype='int')
87 self.storem = np.zeros(10, dtype='int')
88 self.store2d = np.zeros(10, dtype='int')
89 self.store3d = np.zeros(10, dtype='int')
90 self.storeunits = np.zeros(10, dtype='int')
91
92 def getUniqRecTS(self, rec):
93 sls = []
94 for tshit in rec.getRelationsTo('CDCTriggerSegmentHits'):
95 sls.append(tshit.getISuperLayer())
96 return np.unique(sls)
97
98 def event(self):
99 recos = Belle2.PyStoreArray("RecoTracks")
100 nrecos = len(recos)
101 mrecos = 0
102 unitsrecos = 0
103 for rec in recos:
104 cdcuniq = np.unique([cdchit.getICLayer() for cdchit in rec.getCDCHitList()])
105 ncdc = len(cdcuniq)
106 if ncdc > 25:
107 mrecos += 1
108
109 nunits = len(self.getUniqRecTS(rec))
110 if nunits >= 4:
111 unitsrecos += 1
112
113 find2ds = Belle2.PyStoreArray("TRGCDC2DFinderTracks")
114 n2ds = len(find2ds)
115
116 find3ds = Belle2.PyStoreArray("CDCTrigger3DFinderTracks")
117 n3ds = len(find3ds)
118
119 [nrecos, mrecos, unitsrecos, n2ds, n3ds] = self.setMax([nrecos, mrecos, unitsrecos, n2ds, n3ds])
120
121 self.storen[nrecos] += 1
122 self.storem[mrecos] += 1
123 self.store2d[n2ds] += 1
124 self.store3d[n3ds] += 1
125 self.storeunits[unitsrecos] += 1
126
127 def setMax(self, valList, maxval=10):
128 trimmed = []
129 for value in valList:
130 if value >= maxval:
131 value = maxval - 1
132 trimmed.append(value)
133 return trimmed
134
135 def terminate(self):
136 def mystr(val):
137 return f"{f'{val:d}':>3s}"
138 headline0 = [' ' for i in range(6)]
139 headline0.insert(4, 'TrackType')
140 headline1 = [' NTrack', '__|'] + ['___' for i in range(5)]
141 headline2 = [' ' * 3, '|', 'Reco', 'Reco ', 'Reco', '2DFinder', '3DFinder']
142 headline3 = [' ' * 3, '|', ' ', '>25CDC', '>4TS', ' ', ' ']
143
144 def uline(strarr, sep=' ', fmt='%.9s', fmtOut='%9s'):
145 return sep.join([fmtOut % (fmt % x) for x in strarr])
146 print('number of tracks (NTrack in rows) of type TrackType (in columns) in number of events (elements)')
147 print(uline(headline0))
148 print(uline(headline1))
149 print(uline(headline2))
150 print(uline(headline3))
151 print(uline(['_' * 5 for i in range(7)]))
152
153 for itrack, (nevents, mevents, unitsevents, events2d, events3d) in enumerate(
154 zip(self.storen, self.storem, self.storeunits, self.store2d, self.store3d)):
155 entryline = [mystr(itrack), '|',
156 mystr(nevents),
157 mystr(mevents),
158 mystr(unitsevents),
159 mystr(events2d),
160 mystr(events3d)]
161 print(uline(entryline))
162
163
164
165def addTrgOutput(main, args):
166 quickCheck = QuickCheck()
167 main.add_module(quickCheck)
168
169
170def add_matching(path, finderNames):
171 for finderName in finderNames:
172 path.add_module('CDCTriggerRecoMatcher',
173 TrgTrackCollectionName=finderName)
174
175
176def mainFunc(args):
177 basf2.set_log_level(basf2.LogLevel.WARNING)
178 basf2.set_random_seed(args.seed)
179 main = basf2.create_path()
180 addGen(main, args)
181 main.add_module('RootOutput',
182 outputFileName=args.genFileName,
183 updateFileCatalog=False)
184 add_cdc_finders(main)
185 add_matching(main, ["CDCTrigger3DFinderTracks", "TRGCDC2DFinderTracks"])
186 addTrgOutput(main, args)
187 main.add_module('RootOutput',
188 outputFileName=args.outputFileName,
189 updateFileCatalog=False)
190 basf2.process(main)
191 print(basf2.statistics)
192
193
194def mainFuncRead(args):
195 basf2.set_log_level(basf2.LogLevel.WARNING)
196 basf2.set_random_seed(args.seed)
197 main = basf2.create_path()
198 addRead(main, args)
199 add_cdc_finders(main)
200 add_matching(main, ["CDCTrigger3DFinderTracks", "TRGCDC2DFinderTracks"])
201 addTrgOutput(main, args)
202 main.add_module('RootOutput',
203 outputFileName=args.outputFileName,
204 updateFileCatalog=False)
205
206 basf2.process(main, max_event=args.nevents)
207 print(basf2.statistics)
208
209
210if __name__ == "__main__":
211 import argparse
212 parser = argparse.ArgumentParser(description='Test NDFinder in TRG with ISR simulation', epilog='NDFinder Test')
213 parser.add_argument('--finalState', default='pi+pi-', help='final state: mu+mu- pi+pi- pi+pi-pi0')
214 parser.add_argument('--seed', default=1234567, help='random seed', type=int)
215 parser.add_argument('--nevents', default=100, help='number of events to generate', type=int)
216 parser.add_argument('--bkgfiles', default=None, help='path to background mixing files')
217 parser.add_argument('--outputFileName', default="ndFinderTest.root", help='output trg file name')
218 parser.add_argument('--genFileName', default="ndFinderGen.root", help='output gen file name')
219 parser.add_argument('--minweight', default=12, type=int, help='NDFinder')
220 parser.add_argument('--minhits', default=4, type=int, help='NDFinder')
221 parser.add_argument('--minhits_axial', default=2, type=int, help='NDFinder')
222 parser.add_argument('--minpts', default=2, type=int, help='NDFinder')
223 parser.add_argument('--diagonal', action='store_true', help='NDFinder')
224 parser.add_argument('--mincells', default=5, type=int, help='NDFinder')
225 parser.add_argument('--minassign', default=0.8, type=float, help='NDFinder')
226 parser.add_argument('--thresh', default=0.85, type=float, help='NDFinder')
227 parser.add_argument('--readGen', action='store_true', help='skip generation, read from file')
228 parser.add_argument('--axialFile',
229 default="data/trg/cdc/ndFinderAxialShallow.txt.gz",
230 # default="data/trg/cdc/ndFinderArrayAxialComp.txt.gz",
231 help='NDFinder File name of the axial hit patterns')
232 parser.add_argument('--stereoFile',
233 default="data/trg/cdc/ndFinderStereoShallow.txt.gz",
234 # default="data/trg/cdc/ndFinderArrayStereoComp.txt.gz",
235 help='NDFinder File name of the stereo hit patterns')
236 parser.add_argument('--verbose', action='store_true', help='NDFinder')
237
238 args = parser.parse_args()
239 if args.readGen:
240 mainFuncRead(args)
241 else:
242 mainFunc(args)
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:151
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72