SND@LHC Software
Loading...
Searching...
No Matches
shipStrawTracking_prev.py
Go to the documentation of this file.
1#!/usr/bin/env python
2#first version of digitization & pattern recognition
3#for documentation, see CERN-SHiP-NOTE-2015-002, https://cds.cern.ch/record/2005715/files/main.pdf
4#17-04-2015 comments to EvH
5
6from __future__ import print_function
7import global_variables
8import shipPatRec_prev
9import ROOT,os,sys,getopt
10import shipDet_conf
11import shipunit as u
12
13nEvents = 999999
14
15from optparse import OptionParser
16parser = OptionParser()
17parser.add_option("-c","--cheated", dest="chtd", help="cheated=1 to use MC hit x,y instead of wire left&right coordinates",default=0)
18parser.add_option("-d","--debug", dest="dbg", help="debug=1 to print debug information",default=0)
19parser.add_option("-f","--inputFile", dest="input", help="input file", default="$FAIRSHIP/ship.10.0.Pythia8-TGeant4.root")
20parser.add_option("-g","--geoFile", dest="geometry", help="input geometry file", default='$FAIRSHIP/geofile_full.10.0.Pythia8-TGeant4.root')
21parser.add_option("-m","--monitor", dest="mntr", help="monitor=1 to obtain the PatRec efficiency wrt MC truth",default=0)
22parser.add_option("-o","--options", dest="helptext", help="Available options:",default=0)
23parser.add_option("-r","--reconstructible", dest="rectracks", help="number of reconstructible tracks required",default=2)
24parser.add_option("-s","--saveDisk", dest="saveDisk", help="save file to disk",default=0)
25parser.add_option("-t","--threeprong", dest="tp", help="threeprong=1 mumunu decay",default=0)
26
27(options,args)=parser.parse_args()
28
29shipPatRec_prev.cheated=bool(options.chtd)
30global_variables.debug = (int(options.dbg) == 1)
31inputFile = options.input
32shipPatRec_prev.geoFile = options.geometry
33shipPatRec_prev.monitor=bool(options.mntr)
34shipPatRec_prev.printhelp=bool(options.helptext)
35shipPatRec_prev.reconstructiblerequired = int(options.rectracks)
36shipPatRec_prev.threeprong = int(options.tp)
37saveDisk = options.saveDisk
38
39if (shipPatRec_prev.printhelp==True or len(sys.argv)==1):
40 print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
41 print("shipStrawTracking runs the straw tracker pattern recognition and fits the resulting tracks with DAF. Available options:")
42 print("-c 1 : uses MC true hit x,y instead of wire left&right coordinates and stereo projections. default 0")
43 print("-d 1 : print a lot of debug messages. default 0")
44 print("-g name_of_geometry_file : input geometry file. default='$FAIRSHIP/geofile_full.10.0.Pythia8-TGeant4.root'")
45 print("-f name_of_input_file : input file name.default='$FAIRSHIP/ship.10.0.Pythia8-TGeant4.root'")
46 print("-m 1 : obtain the PatRec efficiency wrt MC truth. default 0")
47 print("-o 1 : prints this message.")
48 print("-r reconstructible tracks : number of reconstructible tracks required when monitoring. default 2")
49 print("-s 1 : to save output to disk. default 0")
50 print("-t 1 : to be used when monitoring the threeprong mumunu decay. default 0")
51 print("runing example : ")
52 print("python python/shipStrawTracking.py -i ship.10.0.Pythia8-TGeant4-1000.root -g geofile_full.10.0.Pythia8-TGeant4-1000.root")
53 print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
54 sys.exit()
55
56if not inputFile.find('_patrec.root') < 0:
57 outFile = inputFile
58 inputFile = outFile.replace('_patrec.root','.root')
59else:
60 outFile = inputFile.replace('.root','_patrec.root')
61 if saveDisk: os.system('mv '+inputFile+' '+outFile)
62 else : os.system('cp '+inputFile+' '+outFile)
63
64if not shipPatRec_prev.geoFile:
65 tmp = inputFile.replace('ship.','geofile_full.')
66 shipPatRec_prev.geoFile = tmp.replace('_rec','')
67
68run = ROOT.FairRunSim()
69shipDet_conf.configure(run,shipPatRec_prev.ship_geo)
70modules = {}
71for x in run.GetListOfModules(): modules[x.GetName()]=x
72
73fgeo = ROOT.TFile(shipPatRec_prev.geoFile)
74sGeo = fgeo.FAIRGeom
75
76
77bfield = ROOT.genfit.BellField(shipPatRec_prev.ship_geo.Bfield.max ,shipPatRec_prev.ship_geo.Bfield.z,2,shipPatRec_prev.ship_geo.Yheight/2.*u.m)
78fM = ROOT.genfit.FieldManager.getInstance()
79fM.init(bfield)
80
81geoMat = ROOT.genfit.TGeoMaterialInterface()
82ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
83
84
85fn=ROOT.TFile(outFile,'update')
86sTree = fn.cbmsim
87fout=outFile
88
89if sTree.GetBranch("FitTracks_PR"):
90 print("remove RECO branches and rerun reconstruction")
91 fn.Close()
92 f=ROOT.TFile(fout,'update')
93 sTree=f.cbsim
94 sTree.SetBranchStatus("FitTracks_PR",0)
95 sTree.SetBranchStatus("SmearedHits",0)
96 sTree.SetBranchStatus("Particles",0)
97 sTree.SetBranchStatus("fitTrack2MC_PR",0)
98 sTree.SetBranchStatus("EcalClusters",0)
99 rawFile = fout.replace("_patrec.root","_raw.root")
100 recf = ROOT.TFile(rawFile,"recreate")
101 newTree = sTree.CloneTree(0)
102 for n in range(sTree.GetEntries()):
103 sTree.GetEntry(n)
104 rc = newTree.Fill()
105 sTree.Clear()
106 newTree.AutoSave()
107 f.Close()
108 recf.Close()
109 os.system('cp '+rawFile +' '+fout)
110 fn = ROOT.TFile(fout,'update')
111 sTree = fn.cbmsim
112
113if sTree.GetBranch("GeoTracks"): sTree.SetBranchStatus("GeoTracks",0)
114
115nEvents = min(sTree.GetEntries(),nEvents)
116
117fPartArray_PR = ROOT.TClonesArray("TParticle")
118fGenFitArray_PR = ROOT.TClonesArray("genfit::Track")
119fGenFitArray_PR.BypassStreamer(ROOT.kFALSE)
120fitTrack2MC_PR = ROOT.std.vector('int')()
121SmearedHits = ROOT.TClonesArray("TVectorD")
122
123Particles_PR = sTree.Branch("Particles_PR", fPartArray_PR,32000,-1)
124SHbranch = sTree.Branch("SmearedHits",SmearedHits,32000,-1)
125fitTracks_PR = sTree.Branch("FitTracks_PR", fGenFitArray_PR,32000,-1)
126mcLink_PR = sTree.Branch("fitTrack2MC_PR",fitTrack2MC_PR,32000,-1)
127
128if global_variables.debug:
129 print("Straw tracker geometry parameters (cm)")
130 print("--------------------------------------")
131 print("Strawlength :",2*shipPatRec_prev.ship_geo.strawtubes.StrawLength)
132 print("Inner straw diameter :",shipPatRec_prev.ship_geo.strawtubes.InnerStrawDiameter)
133 print("Straw wall thickness :",shipPatRec_prev.ship_geo.strawtubes.WallThickness)
134 print("Outer straw diameter :",shipPatRec_prev.ship_geo.strawtubes.OuterStrawDiameter)
135 print("Wire thickness :",shipPatRec_prev.ship_geo.strawtubes.WireThickness)
136 print("Straw pitch :",shipPatRec_prev.ship_geo.strawtubes.StrawPitch)
137 print("z-offset between layers:",shipPatRec_prev.ship_geo.strawtubes.DeltazLayer)
138 print("z-offset between planes:",shipPatRec_prev.ship_geo.strawtubes.DeltazPlane)
139 print("Straws per layer :",shipPatRec_prev.ship_geo.strawtubes.StrawsPerLayer)
140 print("z-offset between planes:",shipPatRec_prev.ship_geo.strawtubes.DeltazPlane)
141 print("z-offset between views :",shipPatRec_prev.ship_geo.strawtubes.DeltazView)
142
144
145def EventLoop(SmearedHits):
146 #loop over events
147 for n in range(nEvents):
148 fittedtrackids=[]
149 SmearedHits.Delete()
150 fGenFitArray_PR.Delete()
151 fitTrack2MC_PR.clear()
152 fPartArray_PR.Delete()
153
154 rc = sTree.GetEvent(n)
155
156 if shipPatRec_prev.monitor==True:
157 shipPatRec_prev.ReconstructibleMCTracks=shipPatRec_prev.getReconstructibleTracks(n,sTree,sGeo)
158 if len(shipPatRec_prev.ReconstructibleMCTracks)!=shipPatRec_prev.reconstructiblerequired :
159 if global_variables.debug:
160 print(
161 "Number of reconstructible tracks =", len(shipPatRec_prev.ReconstructibleMCTracks),
162 "but number of reconstructible required=", shipPatRec_prev.reconstructiblerequired,
163 ". Rejecting event."
164 )
165 continue
166
167 if global_variables.debug:
168 print("Reconstructible track ids", shipPatRec_prev.ReconstructibleMCTracks)
169
170
171 #n = current event number, False=wire endpoints, True=MC truth
172
173 SmearedHits = shipPatRec_prev.SmearHits(n,sTree,modules,SmearedHits,shipPatRec_prev.ReconstructibleMCTracks)
174
175 fittedtrackids=shipPatRec_prev.execute(n,SmearedHits,sTree,shipPatRec_prev.ReconstructibleMCTracks)
176 if fittedtrackids:
177 tracknbr=0
178 for ids in fittedtrackids:
179 nTrack = fGenFitArray_PR.GetEntries()
180 fGenFitArray_PR[nTrack] = shipPatRec_prev.theTracks[tracknbr]
181 fitTrack2MC_PR.push_back(ids)
182 tracknbr+=1
183
184 Particles_PR.Fill()
185 fitTracks_PR.Fill()
186 mcLink_PR.Fill()
187 SHbranch.Fill()
188
189
190 if global_variables.debug:
191 print(shipPatRec_prev.falsenegative,"matched tracks with wrong negative charge from deflection.")
192 print(shipPatRec_prev.falsepositive,"matched tracks with wrong positive charge from deflection.")
193 print(shipPatRec_prev.morethan500,"events with more than 500 hits.")
194 print(shipPatRec_prev.morethan100tracks,"events with more than 100 tracks.")
195
196 return
197
198if global_variables.debug:
199 if shipPatRec_prev.threeprong==1:
200 debugrootfile = ROOT.TFile(str(shipPatRec_prev.reconstructiblerequired)+"track-debug-mumunu-"+str(nEvents)+".root","RECREATE")
201 else:
202 debugrootfile = ROOT.TFile(str(shipPatRec_prev.reconstructiblerequired)+"track-debug-"+str(nEvents)+".root","RECREATE")
203
204EventLoop(SmearedHits)
205
206#1/0
207shipPatRec_prev.h['pinvvstruepinv'].Draw('box')
208scale=1.
209if shipPatRec_prev.h['fracsame12'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame12'].Integral()
210shipPatRec_prev.h['fracsame12'].Scale(scale)
211scale=1.
212if shipPatRec_prev.h['fracsame12-y'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame12-y'].Integral()
213shipPatRec_prev.h['fracsame12-y'].Scale(scale)
214scale=1.
215if shipPatRec_prev.h['fracsame12-stereo'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame12-stereo'].Integral()
216shipPatRec_prev.h['fracsame12-stereo'].Scale(scale)
217scale=1.
218if shipPatRec_prev.h['fracsame34'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame34'].Integral()
219shipPatRec_prev.h['fracsame34'].Scale(scale)
220scale=1.
221if shipPatRec_prev.h['fracsame34-y'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame34-y'].Integral()
222shipPatRec_prev.h['fracsame34-y'].Scale(scale)
223scale=1.
224if shipPatRec_prev.h['fracsame34-stereo'].Integral() !=0 : scale=1/shipPatRec_prev.h['fracsame34-stereo'].Integral()
225shipPatRec_prev.h['fracsame34-stereo'].Scale(scale)
226
227scale=1.
228
229if shipPatRec_prev.monitor==1:
230 shipPatRec_prev.totalafterpatrec=shipPatRec_prev.totalafterpatrec/(shipPatRec_prev.reconstructiblerequired**2)
231 shipPatRec_prev.totalaftermatching=shipPatRec_prev.totalaftermatching/shipPatRec_prev.reconstructiblerequired
232
233rc=shipPatRec_prev.h['eventspassed'].SetBinContent(1,shipPatRec_prev.reconstructibleevents)
234rc=shipPatRec_prev.h['eventspassed'].SetBinContent(2,shipPatRec_prev.reconstructiblehorizontalidsfound12)
235rc=shipPatRec_prev.h['eventspassed'].SetBinContent(3,shipPatRec_prev.reconstructiblestereoidsfound12)
236rc=shipPatRec_prev.h['eventspassed'].SetBinContent(4,shipPatRec_prev.reconstructibleidsfound12)
237rc=shipPatRec_prev.h['eventspassed'].SetBinContent(5,shipPatRec_prev.reconstructiblehorizontalidsfound34)
238rc=shipPatRec_prev.h['eventspassed'].SetBinContent(6,shipPatRec_prev.reconstructiblestereoidsfound34)
239rc=shipPatRec_prev.h['eventspassed'].SetBinContent(7,shipPatRec_prev.reconstructibleidsfound34)
240rc=shipPatRec_prev.h['eventspassed'].SetBinContent(8,shipPatRec_prev.totalafterpatrec)
241rc=shipPatRec_prev.h['eventspassed'].SetBinContent(9,shipPatRec_prev.totalaftermatching)
242
243sTree.Write()
244
245if shipPatRec_prev.threeprong==1:
246 shipPatRec_prev.ut.writeHists(shipPatRec_prev.h,"patrec-"+str(shipPatRec_prev.reconstructiblerequired)+"track-mumunu-"+str(nEvents)+".root")
247else:
248 shipPatRec_prev.ut.writeHists(shipPatRec_prev.h,"patrec-"+str(shipPatRec_prev.reconstructiblerequired)+"track-"+str(nEvents)+".root")
configure(run, ship_geo)
execute(SmearedHits, sTree, ReconstructibleMCTracks)
getReconstructibleTracks(iEvent, sTree, sGeo)
SmearHits(iEvent, sTree, modules, SmearedHits, ReconstructibleMCTracks)