6from __future__
import print_function
9import ROOT,os,sys,getopt
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)
27(options,args)=parser.parse_args()
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
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(
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
56if not inputFile.find(
'_patrec.root') < 0:
58 inputFile = outFile.replace(
'_patrec.root',
'.root')
60 outFile = inputFile.replace(
'.root',
'_patrec.root')
61 if saveDisk: os.system(
'mv '+inputFile+
' '+outFile)
62 else : os.system(
'cp '+inputFile+
' '+outFile)
64if not shipPatRec_prev.geoFile:
65 tmp = inputFile.replace(
'ship.',
'geofile_full.')
66 shipPatRec_prev.geoFile = tmp.replace(
'_rec',
'')
68run = ROOT.FairRunSim()
71for x
in run.GetListOfModules(): modules[x.GetName()]=x
73fgeo = ROOT.TFile(shipPatRec_prev.geoFile)
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()
81geoMat = ROOT.genfit.TGeoMaterialInterface()
82ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
85fn=ROOT.TFile(outFile,
'update')
89if sTree.GetBranch(
"FitTracks_PR"):
90 print(
"remove RECO branches and rerun reconstruction")
92 f=ROOT.TFile(fout,
'update')
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()):
109 os.system(
'cp '+rawFile +
' '+fout)
110 fn = ROOT.TFile(fout,
'update')
113if sTree.GetBranch(
"GeoTracks"): sTree.SetBranchStatus(
"GeoTracks",0)
115nEvents = min(sTree.GetEntries(),nEvents)
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")
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)
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)
147 for n
in range(nEvents):
150 fGenFitArray_PR.Delete()
151 fitTrack2MC_PR.clear()
152 fPartArray_PR.Delete()
154 rc = sTree.GetEvent(n)
156 if shipPatRec_prev.monitor==
True:
158 if len(shipPatRec_prev.ReconstructibleMCTracks)!=shipPatRec_prev.reconstructiblerequired :
159 if global_variables.debug:
161 "Number of reconstructible tracks =", len(shipPatRec_prev.ReconstructibleMCTracks),
162 "but number of reconstructible required=", shipPatRec_prev.reconstructiblerequired,
167 if global_variables.debug:
168 print(
"Reconstructible track ids", shipPatRec_prev.ReconstructibleMCTracks)
178 for ids
in fittedtrackids:
179 nTrack = fGenFitArray_PR.GetEntries()
180 fGenFitArray_PR[nTrack] = shipPatRec_prev.theTracks[tracknbr]
181 fitTrack2MC_PR.push_back(ids)
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.")
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")
202 debugrootfile = ROOT.TFile(str(shipPatRec_prev.reconstructiblerequired)+
"track-debug-"+str(nEvents)+
".root",
"RECREATE")
207shipPatRec_prev.h[
'pinvvstruepinv'].Draw(
'box')
209if shipPatRec_prev.h[
'fracsame12'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame12'].Integral()
210shipPatRec_prev.h[
'fracsame12'].Scale(scale)
212if shipPatRec_prev.h[
'fracsame12-y'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame12-y'].Integral()
213shipPatRec_prev.h[
'fracsame12-y'].Scale(scale)
215if shipPatRec_prev.h[
'fracsame12-stereo'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame12-stereo'].Integral()
216shipPatRec_prev.h[
'fracsame12-stereo'].Scale(scale)
218if shipPatRec_prev.h[
'fracsame34'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame34'].Integral()
219shipPatRec_prev.h[
'fracsame34'].Scale(scale)
221if shipPatRec_prev.h[
'fracsame34-y'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame34-y'].Integral()
222shipPatRec_prev.h[
'fracsame34-y'].Scale(scale)
224if shipPatRec_prev.h[
'fracsame34-stereo'].Integral() !=0 : scale=1/shipPatRec_prev.h[
'fracsame34-stereo'].Integral()
225shipPatRec_prev.h[
'fracsame34-stereo'].Scale(scale)
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
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)
245if shipPatRec_prev.threeprong==1:
246 shipPatRec_prev.ut.writeHists(shipPatRec_prev.h,
"patrec-"+str(shipPatRec_prev.reconstructiblerequired)+
"track-mumunu-"+str(nEvents)+
".root")
248 shipPatRec_prev.ut.writeHists(shipPatRec_prev.h,
"patrec-"+str(shipPatRec_prev.reconstructiblerequired)+
"track-"+str(nEvents)+
".root")
execute(SmearedHits, sTree, ReconstructibleMCTracks)
getReconstructibleTracks(iEvent, sTree, sGeo)
SmearHits(iEvent, sTree, modules, SmearedHits, ReconstructibleMCTracks)