241 rc = sTree.GetEvent(iEvent)
242 nMCTracks = sTree.MCTrack.GetEntriesFast()
244 if global_variables.debug:
245 print(
"event nbr", iEvent,
"has", nMCTracks,
"tracks")
247 for i
in reversed(range(nMCTracks)):
248 atrack = sTree.MCTrack.At(i)
251 if PDG.GetParticle(atrack.GetPdgCode()):
252 if PDG.GetParticle(atrack.GetPdgCode()).GetName()[:5]==
"nu_mu":
253 if (atrack.GetStartZ() < TStation1StartZ
and atrack.GetStartZ() > VetoStationEndZ)
and i
not in MCTrackIDs:
256 if atrack.GetStartZ() > TStation4EndZ :
257 motherId=atrack.GetMotherId()
259 mothertrack=sTree.MCTrack.At(motherId)
260 mothertrackZ=mothertrack.GetStartZ()
263 if mothertrackZ < TStation1StartZ
and mothertrackZ > VetoStationEndZ:
264 if motherId
not in MCTrackIDs:
265 MCTrackIDs.append(motherId)
268 if atrack.GetStartZ() > TStation4EndZ :
269 motherId=atrack.GetMotherId()
271 mothertrack=sTree.MCTrack.At(motherId)
272 mothertrackZ=mothertrack.GetStartZ()
275 if mothertrackZ < TStation1StartZ
and mothertrackZ > VetoStationEndZ:
276 if motherId
not in MCTrackIDs:
277 MCTrackIDs.append(motherId)
278 if global_variables.debug:
279 print(
"Tracks with origin in decay volume", MCTrackIDs)
280 if len(MCTrackIDs)==0:
return MCTrackIDs
283 nVetoHits = sTree.vetoPoint.GetEntriesFast()
285 for i
in range(nVetoHits):
286 avetohit = sTree.vetoPoint.At(i)
288 if sGeo.FindNode(avetohit.GetX(),avetohit.GetY(),avetohit.GetZ()).GetName() ==
'TimeDet_1':
289 if avetohit.GetTrackID()
not in hitsinTimeDet:
290 hitsinTimeDet.append(avetohit.GetTrackID())
294 for item
in MCTrackIDs:
297 if PDG.GetParticle(sTree.MCTrack.At(item).GetPdgCode()).GetName()[:5]!=
"nu_mu" and item
not in hitsinTimeDet:
298 itemstoremove.append(item)
300 if item
not in hitsinTimeDet:
301 itemstoremove.append(item)
302 for item
in itemstoremove:
303 MCTrackIDs.remove(item)
305 if global_variables.debug:
306 print(
"Tracks with hits in timedet", MCTrackIDs)
307 if len(MCTrackIDs)==0:
return MCTrackIDs
309 nHits = sTree.strawtubesPoint.GetEntriesFast()
312 if global_variables.debug:
313 print(
"Nbr of Rawhits=", nHits)
315 for i
in range(nHits):
316 ahit = sTree.strawtubesPoint[i]
317 if (str(ahit.GetDetectorID())[:1]==
"5") :
318 if global_variables.debug:
319 print(
"Hit in straw Veto detector. Rejecting.")
321 strawname=str(ahit.GetDetectorID())
323 if strawname
in hitstraws:
325 if ahit.GetX()>hitstraws[strawname][1]:
327 duplicatestrawhit.append(i)
330 duplicatestrawhit.append(hitstraws[strawname][0])
331 hitstraws[strawname]=[i,ahit.GetX()]
333 hitstraws[strawname]=[i,ahit.GetX()]
340 trackoutsidestations=[]
341 for i
in range(nHits):
342 if i
in duplicatestrawhit:
343 if global_variables.debug:
344 print(
"Duplicate hit", i,
"not reconstructible, rejecting.")
346 ahit = sTree.strawtubesPoint[i]
348 if (((ahit.GetX()/245.)**2 + (ahit.GetY()/495.)**2) >= 1.):
349 if ahit.GetTrackID()
not in trackoutsidestations:
350 trackoutsidestations.append(ahit.GetTrackID())
351 if ahit.GetTrackID()
not in MCTrackIDs:
353 if global_variables.debug:
354 print(
"Hit not on reconstructible track. Rejecting.")
357 if str(ahit.GetDetectorID())[:1]==
"1" :
358 if ahit.GetTrackID()
in hits1:
359 hits1[ahit.GetTrackID()]=[hits1[ahit.GetTrackID()][0],i]
361 hits1[ahit.GetTrackID()]=[i]
362 if str(ahit.GetDetectorID())[:1]==
"2" :
363 if ahit.GetTrackID()
in hits2:
364 hits2[ahit.GetTrackID()]=[hits2[ahit.GetTrackID()][0],i]
366 hits2[ahit.GetTrackID()]=[i]
367 if str(ahit.GetDetectorID())[:1]==
"3" :
368 if ahit.GetTrackID()
in hits3:
369 hits3[ahit.GetTrackID()]=[hits3[ahit.GetTrackID()][0],i]
371 hits3[ahit.GetTrackID()]=[i]
372 if str(ahit.GetDetectorID())[:1]==
"4" :
373 if ahit.GetTrackID()
in hits4:
374 hits4[ahit.GetTrackID()]=[hits4[ahit.GetTrackID()][0],i]
376 hits4[ahit.GetTrackID()]=[i]
379 tracks_with_hits_in_all_stations=[]
380 for key
in hits1.keys():
381 if (key
in hits2
and key
in hits3 )
and key
in hits4:
382 if key
not in tracks_with_hits_in_all_stations
and key
not in trackoutsidestations:
383 tracks_with_hits_in_all_stations.append(key)
384 for key
in hits2.keys():
385 if (key
in hits1
and key
in hits3 )
and key
in hits4:
386 if key
not in tracks_with_hits_in_all_stations
and key
not in trackoutsidestations:
387 tracks_with_hits_in_all_stations.append(key)
388 for key
in hits3.keys():
389 if ( key
in hits2
and key
in hits1 )
and key
in hits4:
390 if key
not in tracks_with_hits_in_all_stations
and key
not in trackoutsidestations:
391 tracks_with_hits_in_all_stations.append(key)
392 for key
in hits4.keys():
393 if (key
in hits2
and key
in hits3)
and key
in hits1:
394 if key
not in tracks_with_hits_in_all_stations
and key
not in trackoutsidestations:
395 tracks_with_hits_in_all_stations.append(key)
399 for item
in MCTrackIDs:
402 if PDG.GetParticle(sTree.MCTrack.At(item).GetPdgCode()).GetName()[:5]!=
"nu_mu" and item
not in tracks_with_hits_in_all_stations:
403 itemstoremove.append(item)
405 if item
not in tracks_with_hits_in_all_stations:
406 itemstoremove.append(item)
407 for item
in itemstoremove:
408 MCTrackIDs.remove(item)
410 if global_variables.debug:
411 print(
"tracks_with_hits_in_all_stations",tracks_with_hits_in_all_stations)
412 print(
"Tracks with hits in all stations & inside acceptance ellipse",MCTrackIDs)
413 if len(MCTrackIDs)==0:
return MCTrackIDs
415 for i
in range(nHits):
416 if i
in duplicatestrawhit:
419 ahit = sTree.strawtubesPoint[i]
420 if ahit.GetTrackID()>-1
and ahit.GetTrackID()
in MCTrackIDs:
421 atrack = sTree.MCTrack.At(ahit.GetTrackID())
422 for j
in range(ahit.GetTrackID()+1,nMCTracks) :
423 childtrack = sTree.MCTrack.At(j)
424 if childtrack.GetMotherId() == ahit.GetTrackID():
425 trackmomentum=atrack.GetP()
426 trackweight=atrack.GetWeight()
427 rc=h[
'reconstructiblemomentum'].Fill(trackmomentum,trackweight)
428 motherId=atrack.GetMotherId()
430 HNLmomentum=sTree.MCTrack.At(1).GetP()
431 rc=h[
'HNLmomentumvsweight'].Fill(trackweight,HNLmomentum)
433 trackmomentum=atrack.GetP()
434 trackweight=atrack.GetWeight()
435 rc=h[
'reconstructiblemomentum'].Fill(trackmomentum,trackweight)
436 if atrack.GetMotherId()==1 :
437 HNLmomentum=sTree.MCTrack.At(1).GetP()
438 rc=h[
'HNLmomentumvsweight'].Fill(trackweight,HNLmomentum)
440 for item
in MCTrackIDs:
441 atrack = sTree.MCTrack.At(item)
442 motherId=atrack.GetMotherId()
444 itemstoremove.append(item)
445 for item
in itemstoremove:
446 MCTrackIDs.remove(item)
447 if global_variables.debug:
448 print(
"After removing the non HNL track, MCTrackIDs", MCTrackIDs)
449 if global_variables.debug:
450 print(
"Tracks with HNL mother", MCTrackIDs)
458 for item
in MCTrackIDs:
460 if PDG.GetParticle(sTree.MCTrack.At(item).GetPdgCode()).GetName()[:2]==
"mu" : mufound+=1
461 if PDG.GetParticle(sTree.MCTrack.At(item).GetPdgCode()).GetName()[:2]==
"pi" : pifound+=1
462 if PDG.GetParticle(sTree.MCTrack.At(item).GetPdgCode()).GetName()[:5]==
"nu_mu":
464 itemstoremove.append(item)
466 if global_variables.debug:
467 print(
"Unknown particle with pdg code:", sTree.MCTrack.At(item).GetPdgCode())
468 if reconstructiblerequired == 1 :
469 if mufound!=1
and pifound!=1:
470 if global_variables.debug:
471 print(
"No reconstructible pion or muon.")
473 if reconstructiblerequired == 2 :
475 if mufound!=2
or nu_mufound!=1 :
476 if global_variables.debug:
477 print(
"No reconstructible mu-mu-nu.")
481 for item
in itemstoremove:
482 MCTrackIDs.remove(item)
484 if mufound!=1
or pifound!=1 :
485 if global_variables.debug:
486 print(
"No reconstructible pion and muon.")
488 if len(MCTrackIDs)>0:
489 rc=h[
'nbrhits'].Fill(nHits)
490 rc=h[
'nbrtracks'].Fill(nMCTracks)
491 if global_variables.debug:
492 print(
"Tracks with required HNL decay particles", MCTrackIDs)
495def SmearHits(iEvent,sTree,modules,SmearedHits,ReconstructibleMCTracks):
502 random = ROOT.TRandom()
503 ROOT.gRandom.SetSeed(13)
505 rc = sTree.GetEvent(iEvent)
506 nHits = sTree.strawtubesPoint.GetEntriesFast()
507 withNoStrawSmearing=
None
511 for i
in range(nHits):
512 ahit = sTree.strawtubesPoint[i]
513 if (str(ahit.GetDetectorID())[:1]==
"5") :
continue
514 strawname=str(ahit.GetDetectorID())
515 if strawname
in hitstraws:
517 if ahit.GetX()>hitstraws[strawname][1]:
519 duplicatestrawhit.append(i)
522 duplicatestrawhit.append(hitstraws[strawname][0])
523 hitstraws[strawname]=[i,ahit.GetX()]
525 hitstraws[strawname]=[i,ahit.GetX()]
529 if global_variables.debug:
530 print(
"nbr of hits=", nHits,
"in event", iEvent)
538 for i
in range(nHits):
539 ahit = sTree.strawtubesPoint[i]
540 strawname=str(ahit.GetDetectorID())
542 if i
in duplicatestrawhit:
545 if (str(ahit.GetDetectorID())[:1]==
"5") :
continue
547 if (((ahit.GetX()/245.)**2 + (ahit.GetY()/495.)**2) >= 1.):
continue
549 modules[
"Strawtubes"].StrawEndPoints(ahit.GetDetectorID(),bot,top)
552 sm =
hit2wire(ahit,bot,top,withNoStrawSmearing)
553 m = array(
'd',[i,sm[
'xtop'],sm[
'ytop'],sm[
'z'],sm[
'xbot'],sm[
'ybot'],sm[
'z'],sm[
'dist'],ahit.GetDetectorID()])
556 m = array(
'd',[i,ahit.GetX(),ahit.GetY(),top[2],ahit.GetX(),ahit.GetY(),top[2],ahit.dist2Wire(),ahit.GetDetectorID()])
558 measurement = ROOT.TVectorD(9,m)
560 smHits = SmearedHits.GetEntries()
561 if SmearedHits.GetSize() == smHits: SmearedHits.Expand(smHits+1000)
562 SmearedHits[smHits] = measurement
566 if (str(ahit.GetDetectorID())[1:2]==
"1"): angle=ShipGeo.strawtubes.ViewAngle
567 if (str(ahit.GetDetectorID())[1:2]==
"2"): angle=ShipGeo.strawtubes.ViewAngle*-1.
568 if (str(ahit.GetDetectorID())[:1]==
"1") :
569 if ahit.GetTrackID()
in station1hits:
570 station1hits[ahit.GetTrackID()]+=1
571 rc=h[
'hits1xy'].Fill(ahit.GetX(),ahit.GetY())
573 station1hits[ahit.GetTrackID()]=1
574 if (str(ahit.GetDetectorID())[:1]==
"2") :
575 if ahit.GetTrackID()
in station2hits:
576 station2hits[ahit.GetTrackID()]+=1
578 station2hits[ahit.GetTrackID()]=1
579 if (str(ahit.GetDetectorID())[:1]==
"3") :
580 if ahit.GetTrackID()
in station3hits:
581 station3hits[ahit.GetTrackID()]+=1
583 station3hits[ahit.GetTrackID()]=1
584 if (str(ahit.GetDetectorID())[:1]==
"4") :
585 if ahit.GetTrackID()
in station4hits:
586 station4hits[ahit.GetTrackID()]+=1
588 station4hits[ahit.GetTrackID()]=1
590 if ((str(ahit.GetDetectorID())[:2]==
"11" or str(ahit.GetDetectorID())[:2]==
"12")
or (str(ahit.GetDetectorID())[:2]==
"21" or str(ahit.GetDetectorID())[:2]==
"22")):
591 if ahit.GetTrackID()
in station12xhits:
592 station12xhits[ahit.GetTrackID()]+=1
594 station12xhits[ahit.GetTrackID()]=1
596 if ((str(ahit.GetDetectorID())[:2]==
"10" or str(ahit.GetDetectorID())[:2]==
"13")
or (str(ahit.GetDetectorID())[:2]==
"20" or str(ahit.GetDetectorID())[:2]==
"23")):
597 if ahit.GetTrackID()
in station12yhits:
598 station12yhits[ahit.GetTrackID()]+=1
600 station12yhits[ahit.GetTrackID()]=1
614 for items
in station1hits:
617 if items
in ReconstructibleMCTracks: total1hits=total1hits+station1hits[items]
618 else : total1hits=total1hits+station1hits[items]
619 if len(station1hits) > 0 :
620 hits1pertrack=total1hits/len(station1hits)
621 for items
in station12xhits:
623 if items
in ReconstructibleMCTracks: total12xhits=total12xhits+station12xhits[items]
624 else: total12xhits=total12xhits+station12xhits[items]
625 if len(station12xhits) > 0 : hits12xpertrack=total12xhits/len(station12xhits)
626 for items
in station12yhits:
628 if items
in ReconstructibleMCTracks: total12yhits=total12yhits+station12yhits[items]
629 else: total12yhits=total12yhits+station12yhits[items]
630 if len(station12yhits) > 0 : hits12ypertrack=total12yhits/len(station12yhits)
631 for items
in station2hits:
633 if items
in ReconstructibleMCTracks: total2hits=total2hits+station2hits[items]
634 else: total2hits=total2hits+station2hits[items]
635 if len(station2hits) > 0 :
636 hits2pertrack=total2hits/len(station2hits)
637 for items
in station3hits:
639 if items
in ReconstructibleMCTracks: total3hits=total3hits+station3hits[items]
640 else: total3hits=total3hits+station3hits[items]
641 if len(station3hits) > 0 :
642 hits3pertrack=total3hits/len(station3hits)
643 for items
in station4hits:
645 if items
in ReconstructibleMCTracks: total4hits=total4hits+station4hits[items]
646 else: total4hits=total4hits+station4hits[items]
647 if len(station4hits) > 0 :
648 hits4pertrack=total4hits/len(station4hits)
650 rc=h[
'hits1-4'].Fill(hits1pertrack+hits2pertrack+hits3pertrack+hits4pertrack)
651 rc=h[
'hits1'].Fill(hits1pertrack)
652 rc=h[
'hits12x'].Fill(hits12xpertrack)
653 rc=h[
'hits12y'].Fill(hits12ypertrack)
654 rc=h[
'hits2'].Fill(hits2pertrack)
655 rc=h[
'hits3'].Fill(hits3pertrack)
656 rc=h[
'hits4'].Fill(hits4pertrack)
694def PatRec(firsttwo,zlayer,zlayerv2,StrawRaw,StrawRawLink,ReconstructibleMCTracks):
695 global reconstructiblehorizontalidsfound12,reconstructiblestereoidsfound12,reconstructiblehorizontalidsfound34,reconstructiblestereoidsfound34
696 global reconstructibleidsfound12,reconstructibleidsfound34,rawhits,totalaftermatching
714 resolution=ShipGeo.strawtubes.sigma_spatial
716 for item
in StrawRaw:
718 rawhits[j]=copy.deepcopy(((StrawRaw[item][1]+StrawRaw[item][4])/2,(StrawRaw[item][2]+StrawRaw[item][5])/2,StrawRaw[item][6]))
720 if global_variables.debug:
722 "rawhits[", j,
"]=", rawhits[j],
723 "trackid", StrawRawLink[item][0].GetTrackID(),
724 "strawname", StrawRawLink[item][0].GetDetectorID(),
725 "true x", StrawRawLink[item][0].GetX(),
726 "true y", StrawRawLink[item][0].GetY(),
727 "true z", StrawRawLink[item][0].GetZ()
731 sortedrawhits=OrderedDict(sorted(rawhits.items(),key=
lambda t:t[1][1]))
732 if global_variables.debug:
734 print(
"horizontal view (y) hits ordered by plane: plane nr, zlayer, hits")
744 for i
in range(i1,i2+1):
748 for item
in sortedrawhits:
749 if (float(sortedrawhits[item][1])==float(zlayer[i][0])) :
751 if sortedrawhits[item][0]
not in a:
752 a.append(float(sortedrawhits[item][0]))
755 if global_variables.debug:
756 print(
" wire hit again", sortedrawhits[item],
"strawname=", StrawRawLink[item][0].GetDetectorID())
757 a.append(float(sortedrawhits[item][0]))
758 duplicates.append(float(zlayer[i][0]))
762 for item
in sortedrawhits:
763 if ((float(sortedrawhits[item][0])==value) & (float(sortedrawhits[item][1])==float(zlayer[i][0]))) :
766 d.append(float(sortedrawhits[item][2]))
773 if global_variables.debug:
774 print(i, zlayer[i], hits[i])
778 for item
in range(len(hits[i][0])):
779 if StrawRawLink[hits[i][2][item]][0].GetTrackID() == 3:
780 y3.append(float(hits[i][0][item]))
781 z3.append(float(zlayer[i][0]))
783 if StrawRawLink[hits[i][2][item]][0].GetTrackID() == 2:
784 y2.append(float(hits[i][0][item]))
785 z2.append(float(zlayer[i][0]))
787 if StrawRawLink[hits[i][2][item]][0].GetTrackID() == -2:
788 ymin2.append(float(hits[i][0][item]))
789 zmin2.append(float(zlayer[i][0]))
791 yother.append(float(hits[i][0][item]))
792 zother.append(float(zlayer[i][0]))
795 if threeprong==1 : nrtrcandv1,trcandv1=
ptrack(zlayer,hits,6,0.6)
796 else : nrtrcandv1,trcandv1=
ptrack(zlayer,hits,7,0.5)
798 if threeprong==1 : nrtrcandv1,trcandv1=
ptrack(zlayer,hits,6,0.6)
799 else: nrtrcandv1,trcandv1=
ptrack(zlayer,hits,7,0.7)
801 foundhorizontaltrackids=[]
802 horizontaltrackids=[]
806 if global_variables.debug:
808 print(
'track found in Y view:',t,
' indices of hits in planes',trcandv1[t])
809 for ipl
in range(len(trcandv1[t])):
810 indx= trcandv1[t][ipl]
812 if global_variables.debug:
814 ' plane nr, z-position, y of hit, trackid:',
815 ipl, zlayer[ipl], hits[ipl][0][indx],
816 StrawRawLink[hits[ipl][2][indx]][0].GetTrackID()
818 trackids.append(StrawRawLink[hits[ipl][2][indx]][0].GetTrackID())
819 if global_variables.debug:
821 " Largest fraction of hits in Y view:",
fracMCsame(trackids)[0],
825 h[
'fracsame12-y'].Fill(
fracMCsame(trackids)[0])
827 h[
'fracsame34-y'].Fill(
fracMCsame(trackids)[0])
829 if fracMCsame(trackids)[1]
in ReconstructibleMCTracks:
830 horizontaltrackids.append(
fracMCsame(trackids)[1])
831 if fracMCsame(trackids)[1]
not in foundhorizontaltrackids:
832 foundhorizontaltrackids.append(
fracMCsame(trackids)[1])
835 if global_variables.debug:
836 print(
"Y view track with trackid",
fracMCsame(trackids)[1],
"is not reconstructible. Removing it.")
837 removetrackids.append(t)
839 if (len(foundhorizontaltrackids) == 0
or len(horizontaltrackids)==0 ):
840 if global_variables.debug:
841 print(
"No reconstructible Y view tracks found.")
842 if monitor==
True :
return 0,[],[],[],[],[],[],{},{},{},{},{}
844 for i
in range(0,len(removetrackids)):
845 del trcandv1[removetrackids[i]]
846 nrtrcandv1=nrtrcandv1-1
849 if len(removetrackids)>0:
854 trcandv1[j]=trcandv1[key]
858 if len(ReconstructibleMCTracks)>0:
860 if len(foundhorizontaltrackids)>=len(ReconstructibleMCTracks) : reconstructiblehorizontalidsfound12+=1
862 if global_variables.debug:
863 print(
"!!!!!!!!!!!!!!!! Difference between Y-view tracks found and reconstructible tracks (station 1&2). Quitting patrec.")
864 debugevent(iEvent,
False,y2,y3,ymin2,yother,z2,z3,zmin2,zother,foundhorizontaltrackids)
865 return 0,[],[],[],[],[],[],{},{},{},{},{}
867 if len(foundhorizontaltrackids)>=len(ReconstructibleMCTracks) : reconstructiblehorizontalidsfound34+=1
869 if global_variables.debug:
870 print(
"!!!!!!!!!!!!!!!! Difference between Y-view tracks found and reconstructible tracks (station 3&4). Quitting patrec.")
871 debugevent(iEvent,
False,y2,y3,ymin2,yother,z2,z3,zmin2,zother,foundhorizontaltrackids)
872 return 0,[],[],[],[],[],[],{},{},{},{},{}
874 if len(foundhorizontaltrackids) != reconstructiblerequired :
875 if global_variables.debug:
876 print(len(foundhorizontaltrackids),
"Y view tracks found, but ", reconstructiblerequired,
"required.")
877 return 0,[],[],[],[],[],[],{},{},{},{},{}
879 if firsttwo==
True: reconstructiblehorizontalidsfound12+=1
880 else: reconstructiblehorizontalidsfound34+=1
884 if global_variables.debug:
885 print(
"0 tracks found in Y view. Reconstructible:", len(ReconstructibleMCTracks))
886 if monitor==
True:
return 0,[],[],[],[],[],[],{},{},{},{},{}
888 if global_variables.debug:
889 print(nrtrcandv1,
"tracks found in Y view. Reconstructible:", len(ReconstructibleMCTracks))
891 if global_variables.debug:
892 print(
"***************** Start of Stereo PatRec **************************")
900 for item
in StrawRaw:
901 rawxhits[j]=copy.deepcopy((StrawRaw[item][0],StrawRaw[item][1],StrawRaw[item][3],StrawRaw[item][4],StrawRaw[item][2],StrawRaw[item][6]))
903 if global_variables.debug:
905 "rawxhits[", j,
"]=", rawxhits[j],
906 "trackid", StrawRawLink[item][0].GetTrackID(),
907 "true x", StrawRawLink[item][0].GetX(),
908 "true y",StrawRawLink[item][0].GetY()
912 sortedrawxhits=OrderedDict(sorted(rawxhits.items(),key=
lambda t:t[1][4]))
914 if global_variables.debug:
915 print(
"stereo view hits ordered by plane:")
916 for i
in range(i1,i2+1):
923 for item
in sortedrawxhits:
924 if (float(sortedrawxhits[item][4])==float(zlayerv2[i][0])) :
925 xt.append(float(sortedrawxhits[item][0]))
926 yt.append(float(sortedrawxhits[item][1]))
927 xb.append(float(sortedrawxhits[item][2]))
928 yb.append(float(sortedrawxhits[item][3]))
932 d.append(float(sortedrawxhits[item][5]))
933 uvview[i]=[xb,yb,xt,yt,c,d]
935 if global_variables.debug:
937 ' uv hits, z,xb,yb,xt,yt,dist ',
938 i, zlayerv2[i], uvview[i][0], uvview[i][1], uvview[i][2],
939 uvview[i][3], uvview[i][4], uvview[i][5]
950 if global_variables.debug:
951 if (firsttwo==
True) :
952 print(
'Loop over tracks found in Y view, stations 1&2')
954 print(
'Loop over tracks found in Y view, stations 3&4')
956 foundstereotrackids=[]
973 fitt,fitc=
fitline(trcandv1[t],hits,zlayer,resolution)
974 if global_variables.debug:
975 print(
' Track nr', t,
'in Y view: tangent, constant=', fitt, fitc)
983 if firsttwo==
True: looplist=reversed(list(range(len(trcandv1[t]))))
984 else: looplist=list(range(len(trcandv1[t])))
986 indx= trcandv1[t][ipl]
988 if global_variables.debug:
989 print(
' Plane nr, z-position, y of hit:', ipl, zlayer[ipl], hits[ipl][0][indx])
990 hitpoint=[zlayer[ipl],hits[ipl][0][indx]]
991 rc=h[
'disthittoYviewtrack'].Fill(
dist2line(fitt,fitc,hitpoint))
995 px=StrawRawLink[hits[ipl][2][indx]][0].GetPx()
996 py=StrawRawLink[hits[ipl][2][indx]][0].GetPy()
997 pz=StrawRawLink[hits[ipl][2][indx]][0].GetPz()
998 ptmp=math.sqrt(px**2+py**2+pz**2)
999 if global_variables.debug:
1000 print(
" p",ptmp,
"px",px,
"py",py,
"pz",pz)
1001 if tan==0. : tan=py/pz
1002 if cst==0. : cst=StrawRawLink[hits[ipl][2][indx]][0].GetY()-tan*zlayer[ipl][0]
1003 rc=h[
'disthittoYviewMCtrack'].Fill(
dist2line(tan,cst,hitpoint))
1005 if global_variables.debug:
1006 print(
' Track nr', t,
'in Y view: MC tangent, MC constant=', tan, cst)
1008 for ipl
in range(len(trcandv1[t])):
1009 indx= trcandv1[t][ipl]
1011 diffy=hits[ipl][0][indx]-StrawRawLink[hits[ipl][2][indx]][0].GetY()
1012 h[
'digi-truevstruey'].Fill(diffy)
1014 y11trackids.append(StrawRawLink[hits[ipl][2][indx]][0].GetTrackID())
1015 y11hitids.append([hits[ipl][2][indx]][0])
1016 if (ipl>4
and ipl<9):
1017 y12trackids.append(StrawRawLink[hits[ipl][2][indx]][0].GetTrackID())
1018 y12hitids.append([hits[ipl][2][indx]][0])
1019 if (ipl>9
and ipl<13):
1020 y21trackids.append(StrawRawLink[hits[ipl][2][indx]][0].GetTrackID())
1021 y21hitids.append([hits[ipl][2][indx]][0])
1023 y22trackids.append(StrawRawLink[hits[ipl][2][indx]][0].GetTrackID())
1024 y22hitids.append([hits[ipl][2][indx]][0])
1027 y11trackids.append(999)
1028 y11hitids.append(999)
1029 if (ipl>4
and ipl<9):
1030 y12trackids.append(999)
1031 y12hitids.append(999)
1032 if (ipl>9
and ipl<13):
1033 y21trackids.append(999)
1034 y21hitids.append(999)
1036 y22trackids.append(999)
1037 y22hitids.append(999)
1049 for ipl
in range(1,len(zlayerv2)+1):
1054 xclean,items=
line2plane(fitt,fitc,uvview[ipl],zlayerv2[ipl][0])
1057 xclean=copy.deepcopy(uvview[ipl][0])
1058 items=copy.deepcopy(uvview[ipl][4])
1060 distances=copy.deepcopy(uvview[ipl][5])
1062 xcleanunsorted=list(xclean)
1069 d.append(items[xcleanunsorted.index(item)])
1070 e.append(distances[xcleanunsorted.index(item)])
1072 v2hits[ipl]=[xclean,b,d,e]
1073 if global_variables.debug:
1074 print(
' Plane,z, projected hits:', ipl , zlayerv2[ipl], v2hits[ipl])
1076 for item
in range(len(v2hits[ipl][0])):
1077 if StrawRawLink[v2hits[ipl][2][item]][0].GetTrackID() == 3:
1078 v2y3.append(float(v2hits[ipl][0][item]))
1079 v2z3.append(float(zlayerv2[ipl][0]))
1081 if StrawRawLink[v2hits[ipl][2][item]][0].GetTrackID() == 2:
1082 v2y2.append(float(v2hits[ipl][0][item]))
1083 v2z2.append(float(zlayerv2[ipl][0]))
1085 if StrawRawLink[v2hits[ipl][2][item]][0].GetTrackID() == -2:
1086 v2ymin2.append(float(v2hits[ipl][0][item]))
1087 v2zmin2.append(float(zlayerv2[ipl][0]))
1089 v2yother.append(float(v2hits[ipl][0][item]))
1090 v2zother.append(float(zlayerv2[ipl][0]))
1096 nrtrcandv2,trcandv2=
ptrack(zlayerv2,v2hits,7,0.7)
1098 nrtrcandv2,trcandv2=
ptrack(zlayerv2,v2hits,6,15.)
1100 if global_variables.debug:
1101 print(
" len(trcandv2) in stereo", len(trcandv2))
1105 if global_variables.debug:
1106 print(
" 0 tracks found in stereo view, for Y-view track nr", t)
1113 if global_variables.debug:
1114 print(
' Track belonging to Y-view view track', t,
'found in stereo view:', t1, trcandv2[t1])
1116 stereofitt,stereofitc=
fitline(trcandv2[t1],v2hits,zlayerv2,resolution)
1122 if firsttwo==
True: looplist=reversed(list(range(len(trcandv2[t1]))))
1123 else: looplist=list(range(len(trcandv2[t1])))
1124 for ipl
in looplist:
1125 indx= trcandv2[t1][ipl]
1127 hitpointx=[zlayerv2[ipl],v2hits[ipl][0][indx]]
1128 rc=h[
'disthittostereotrack'].Fill(
dist2line(stereofitt,stereofitc,hitpointx))
1129 if pxMC==0. : pxMC=StrawRawLink[v2hits[ipl][2][indx]][0].GetPx()
1130 if pzMC ==0. : pzMC=StrawRawLink[v2hits[ipl][2][indx]][0].GetPz()
1131 if stereotanMCv==0. : stereotanMCv=pxMC/pzMC
1132 if stereocstMCv==0. : stereocstMCv=StrawRawLink[v2hits[ipl][2][indx]][0].GetX()-stereotanMCv*zlayerv2[ipl][0]
1133 rc=h[
'disthittostereoMCtrack'].Fill(
dist2line(stereotanMCv,stereocstMCv,hitpointx))
1149 for ipl
in range(len(trcandv2[t1])):
1150 indx= trcandv2[t1][ipl]
1152 stereotrackids.append(StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID())
1153 if global_variables.debug:
1155 " plane nr, zpos, x of hit, hitid, trackid",
1156 ipl, zlayerv2[ipl], v2hits[ipl][0][indx], v2hits[ipl][2][indx],
1157 StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID()
1159 xdiff=v2hits[ipl][0][indx]-StrawRawLink[v2hits[ipl][2][indx]][0].GetX()
1160 h[
'digi-truevstruex'].Fill(xdiff)
1163 stereo11trackids.append(StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID())
1164 stereo11hitids.append(v2hits[ipl][2][indx])
1166 stereo11trackids.append(999)
1167 stereo11hitids.append(999)
1168 if (ipl>4
and ipl<9):
1170 stereo12trackids.append(StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID())
1171 stereo12hitids.append(v2hits[ipl][2][indx])
1173 stereo12trackids.append(999)
1174 stereo12hitids.append(999)
1175 if (ipl>9
and ipl<13):
1177 stereo21trackids.append(StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID())
1178 stereo21hitids.append(v2hits[ipl][2][indx])
1180 stereo21trackids.append(999)
1181 stereo21hitids.append(999)
1184 stereo22trackids.append(StrawRawLink[v2hits[ipl][2][indx]][0].GetTrackID())
1185 stereo22hitids.append(v2hits[ipl][2][indx])
1187 stereo22trackids.append(999)
1188 stereo22hitids.append(999)
1190 if firsttwo==
True: h[
'fracsame12-stereo'].Fill(
fracMCsame(stereotrackids)[0])
1191 else: h[
'fracsame34-stereo'].Fill(
fracMCsame(stereotrackids)[0])
1193 if global_variables.debug:
1194 print(
" Largest fraction of hits in stereo view:",
fracMCsame(stereotrackids)[0],
"on MC track with id",
fracMCsame(stereotrackids)[1])
1196 if monitor==
True: print(
" fracMCsame(stereotrackids)[1]",
fracMCsame(stereotrackids)[1],
"foundhorizontaltrackids[",t-1,
"]",foundhorizontaltrackids[t-1])
1199 if fracMCsame(stereotrackids)[1] != horizontaltrackids[t-1] :
1200 if global_variables.debug:
1202 " Stereo track with trackid",
1204 "does not belong to the Y-view track with id=",
1205 horizontaltrackids[t-1]
1210 if fracMCsame(stereotrackids)[1]
in ReconstructibleMCTracks:
1211 if fracMCsame(stereotrackids)[1]
not in foundstereotrackids:
1212 foundstereotrackids.append(
fracMCsame(stereotrackids)[1])
1214 if global_variables.debug:
1216 " Stereo track with trackid",
1218 "is not reconstructible. Removing it."
1222 if fracMCsame(stereotrackids)[1]
not in foundstereotrackids:
1223 foundstereotrackids.append(
fracMCsame(stereotrackids)[1])
1226 alltrackids=y11trackids+stereo11trackids+stereo12trackids+y12trackids+y21trackids+stereo21trackids+stereo22trackids+y22trackids
1227 if firsttwo==
True: h[
'fracsame12'].Fill(
fracMCsame(alltrackids)[0])
1228 else: h[
'fracsame34'].Fill(
fracMCsame(alltrackids)[0])
1229 if global_variables.debug:
1231 " Largest fraction of hits in horizontal and stereo view:",
fracMCsame(alltrackids)[0],
1232 "on MC track with id",
fracMCsame(alltrackids)[1]
1237 if fracMCsame(alltrackids)[1]
in ReconstructibleMCTracks:
1238 if fracMCsame(alltrackids)[1]
not in foundtrackids:
1239 foundtrackids.append(
fracMCsame(alltrackids)[1])
1241 tracks[trackkey*1000+nstereotracks]=alltrackids
1242 hitids[trackkey*1000+nstereotracks]=y11hitids+stereo11hitids+stereo12hitids+y12hitids+y21hitids+stereo21hitids+stereo22hitids+y22hitids
1244 ytan[trackkey*1000+nstereotracks]=fitt
1245 ycst[trackkey*1000+nstereotracks]=fitc
1247 ytan[trackkey*1000+nstereotracks]=tan
1248 ycst[trackkey*1000+nstereotracks]=cst
1251 fraction[trackkey*1000+nstereotracks]=
fracMCsame(alltrackids)[0]
1252 trackid[trackkey*1000+nstereotracks]=
fracMCsame(alltrackids)[1]
1255 horpx[trackkey*1000+nstereotracks]=px
1256 horpy[trackkey*1000+nstereotracks]=py
1257 horpz[trackkey*1000+nstereotracks]=pz
1260 stereotan[trackkey*1000+nstereotracks]=stereofitt
1261 stereocst[trackkey*1000+nstereotracks]=stereofitc
1263 stereotan[trackkey*1000+nstereotracks]=stereotanMCv
1264 stereocst[trackkey*1000+nstereotracks]=stereocstMCv
1266 if global_variables.debug:
1267 print(
"Track with trackid",
fracMCsame(alltrackids)[1],
"is not reconstructible. Removing it.")
1270 if fracMCsame(alltrackids)[1]
not in foundtrackids:
1271 foundtrackids.append(
fracMCsame(alltrackids)[1])
1273 tracks[trackkey*1000+nstereotracks]=alltrackids
1274 hitids[trackkey*1000+nstereotracks]=y11hitids+stereo11hitids+stereo12hitids+y12hitids+y21hitids+stereo21hitids+stereo22hitids+y22hitids
1276 ytan[trackkey*1000+nstereotracks]=fitt
1277 ycst[trackkey*1000+nstereotracks]=fitc
1279 ytan[trackkey*1000+nstereotracks]=tan
1280 ycst[trackkey*1000+nstereotracks]=cst
1283 fraction[trackkey*1000+nstereotracks]=
fracMCsame(alltrackids)[0]
1284 trackid[trackkey*1000+nstereotracks]=
fracMCsame(alltrackids)[1]
1287 horpx[trackkey*1000+nstereotracks]=px
1288 horpy[trackkey*1000+nstereotracks]=py
1289 horpz[trackkey*1000+nstereotracks]=pz
1292 stereotan[trackkey*1000+nstereotracks]=stereofitt
1293 stereocst[trackkey*1000+nstereotracks]=stereofitc
1295 stereotan[trackkey*1000+nstereotracks]=stereotanMCv
1296 stereocst[trackkey*1000+nstereotracks]=stereocstMCv
1299 if len(foundstereotrackids)>=len(ReconstructibleMCTracks) :
1300 if firsttwo==
True: reconstructiblestereoidsfound12+=1
1301 else: reconstructiblestereoidsfound34+=1
1303 if global_variables.debug:
1304 debugevent(iEvent,
True,v2y2,v2y3,v2ymin2,v2yother,v2z2,v2z3,v2zmin2,v2zother,foundstereotrackids)
1305 print(
"Nbr of reconstructible tracks after stereo",len(foundstereotrackids),
" but ",len(ReconstructibleMCTracks),
" reconstructible tracks in this event. Quitting.")
1306 return 0,[],[],[],[],[],[],{},{},{},{},{}
1308 if len(foundtrackids)>=len(ReconstructibleMCTracks):
1309 if firsttwo==
True: reconstructibleidsfound12+=1
1310 else: reconstructibleidsfound34+=1
1312 if global_variables.debug:
1313 debugevent(iEvent,
True,v2y2,v2y3,v2ymin2,v2yother,v2z2,v2z3,v2zmin2,v2zother,foundstereotrackids)
1314 print(
"Nbr of reconstructed tracks ",len(foundtrackids),
" but",len(ReconstructibleMCTracks),
" reconstructible tracks in this event. Quitting.")
1315 return 0,[],[],[],[],[],[],{},{},{},{},{}
1317 if firsttwo==
True: reconstructiblestereoidsfound12+=1
1318 else: reconstructiblestereoidsfound34+=1
1319 if firsttwo==
True: reconstructibleidsfound12+=1
1320 else: reconstructibleidsfound34+=1
1323 return ntracks,tracks,hitids,ytan,ycst,stereotan,stereocst,horpx,horpy,horpz,fraction,trackid
1684def execute(SmearedHits,sTree,ReconstructibleMCTracks):
1685 global totalaftermatching,morethan500,falsepositive,falsenegative,totalafterpatrec
1686 global reconstructibleevents,morethan100tracks,theTracks
1689 reconstructibles12=0
1690 reconstructibles34=0
1693 if global_variables.debug:
1694 print(
"************* PatRect START **************")
1696 nShits=sTree.strawtubesPoint.GetEntriesFast()
1697 nMCTracks = sTree.MCTrack.GetEntriesFast()
1703 reconstructibleevents+=1
1704 nr12tracks,tracks12,hitids12,xtan12,xcst12,stereotan12,stereocst12,px12,py12,pz12,fraction12,trackid12=
PatRec(
True,zlayer,zlayerv2,StrawRaw,StrawRawLink,ReconstructibleMCTracks)
1707 for item
in ReconstructibleMCTracks:
1708 for value
in trackid12.values():
1709 if item == value
and item
not in tracksfound:
1710 reconstructibles12+=1
1711 tracksfound.append(item)
1713 if global_variables.debug:
1715 "tracksfound", tracksfound,
1716 "reconstructibles12", reconstructibles12,
1717 "ReconstructibleMCTracks", ReconstructibleMCTracks
1719 if len(tracksfound)==0
and len(ReconstructibleMCTracks)>0:
1720 if global_variables.debug:
1721 print(
"No tracks found in event after stations 1&2. Rejecting event.")
1724 nr34tracks,tracks34,hitids34,xtan34,xcst34,stereotan34,stereocst34,px34,py34,pz34,fraction34,trackid34=
PatRec(
False,z34layer,z34layerv2,StrawRaw,StrawRawLink,ReconstructibleMCTracks)
1728 for item
in ReconstructibleMCTracks:
1729 for value
in trackid34.values():
1730 if item == value
and item
not in tracksfound:
1731 reconstructibles34+=1
1732 tracksfound.append(item)
1733 if len(tracksfound)==0
and len(ReconstructibleMCTracks)>0:
1734 if global_variables.debug:
1735 print(
"No tracks found in event after stations 3&4. Rejecting event.")
1737 MatchedReconstructibleMCTracks=len(ReconstructibleMCTracks)*[0]
1740 if global_variables.debug:
1742 print(nr12tracks,
"tracks12 ",tracks12)
1743 print(
"hitids12 ",hitids12)
1744 print(
"xtan ",xtan12,
"xcst",xcst12)
1745 print(
"stereotan ",stereotan12,
"stereocst ",stereocst12)
1746 print(
"fraction12",fraction12,
"trackid12",trackid12)
1748 print(
"No tracks found in stations 1&2.")
1751 print(nr34tracks,
"tracks34 ",tracks34)
1752 print(
"hitids34 ",hitids34)
1753 print(
"xtan34 ",xtan34,
"xcst34 ",xcst34)
1754 print(
"stereotan34 ",stereotan34,
"stereocst34 ",stereocst34)
1757 print(
"No tracks found in stations 3&4.")
1759 if monitor==
False: totalafterpatrec+=1
1761 zmagnet=ShipGeo.Bfield.z
1768 tgx1=stereotan12[item]
1770 x1=stereocst12[item]
1771 t1=[x1,y1,z1,tgx1,tgy1]
1772 for ids
in hitids12[item]:
1776 particle12 = PDG.GetParticle(StrawRawLink[ids][0].PdgCode())
1778 charge12=particle12.Charge()/3.
1783 falsenegativethistrack=0
1784 falsepositivethistrack=0
1785 for item1
in xtan34:
1786 if monitor==
True: totalafterpatrec+=1
1787 for k,v
in enumerate(ReconstructibleMCTracks):
1788 if v
not in tracksfound:
1789 tracksfound.append(v)
1791 for ids
in hitids34[item1]:
1793 particle34 = PDG.GetParticle(StrawRawLink[ids][0].PdgCode())
1795 charge34=particle34.Charge()/3.
1800 tgx2=stereotan34[item1]
1803 x2=stereocst34[item1]
1804 t2=[x2,y2,z2,tgx2,tgy2]
1810 p2true=1./math.sqrt(p2x**2+p2y**2+p2z**2)
1811 if global_variables.debug:
1813 "Matching 1&2 track", item,
1814 "with 3&4 track", item1,
1815 "dx", dx,
"dy", dy,
"alpha", alpha,
"pinv", pinv,
"1/p2true", p2true
1817 rc=h[
'dx-matchedtracks'].Fill(dx)
1818 rc=h[
'dy-matchedtracks'].Fill(dy)
1819 if ((abs(dx)<20) & (abs(dy)<2)) :
1823 tantheta=(tgy1-tgy2)/(1+tgy1*tgy2)
1828 falsenegativethistrack=1
1833 falsepositivethistrack=1
1837 if (falsepositivethistrack==0 & falsenegativethistrack==0):
1838 totalaftermatching+=1
1840 if global_variables.debug:
1841 print(
"Charge from track deflection doesn't match MC truth. Rejecting it.")
1846 totalaftermatching+=1
1848 if global_variables.debug:
1849 print(
"******* MATCH FOUND stations 12 track id",trackid12[item],
"(fraction",fraction12[item]*100,
"%) and stations 34 track id",trackid34[item1],
"(fraction",fraction34[item1]*100,
"%)")
1850 print(
"******* Reconstructible tracks ids",ReconstructibleMCTracks)
1852 rc=h[
'matchedtrackefficiency'].Fill(fraction12[item],fraction34[item1])
1853 for k,v
in enumerate(ReconstructibleMCTracks):
1854 if global_variables.debug:
1855 print(
"MatchedReconstructibleMCTracks", MatchedReconstructibleMCTracks,
"k", k,
"v", v)
1856 if v
not in MatchedReconstructibleMCTracks:
1857 if global_variables.debug:
1858 print(
"ReconstructibleMCTracks", ReconstructibleMCTracks,
"trackid34[item1]", trackid34[item1])
1859 if v==trackid34[item1]: MatchedReconstructibleMCTracks[k]=v
1860 p2true=p2true*int(charge)
1861 rc=h[
'pinvvstruepinv'].Fill(p2true,pinv)
1862 if math.fabs(pinv) > 0.0 : rc=h[
'ptrue-p/ptrue'].Fill((pinv-p2true)/pinv)
1866 for ids
in range(0,len(hitids12[item])):
1868 if hitids12[item][ids]!=999 :
1870 if ids
in range(0,32):
1871 for z
in range(0,7):
1872 m.append(StrawRaw[hitids12[item][ids]][z])
1873 hitPosList.append(m)
1874 for ids
in range(0,len(hitids34[item1])):
1875 if hitids34[item1][ids]!=999 :
1877 if ids
in range(0,32):
1878 for z
in range(0,7):
1879 m.append(StrawRaw[hitids34[item1][ids]][z])
1880 hitPosList.append(m)
1881 nM = len(hitPosList)
1883 if global_variables.debug:
1884 print(
"Only", nM,
"hits on this track. Insufficient for fitting.")
1891 rep = ROOT.genfit.RKTrackRep(pdg)
1892 posM = ROOT.TVector3(0, 0, 0)
1894 if math.fabs(pinv) > 0.0 : momM = ROOT.TVector3(0,0,int(charge)/pinv)
1895 else: momM = ROOT.TVector3(0,0,999)
1896 covM = ROOT.TMatrixDSym(6)
1897 resolution = ShipGeo.strawtubes.sigma_spatial
1898 for i
in range(3): covM[i][i] = resolution*resolution
1899 covM[0][0]=resolution*resolution*100.
1900 for i
in range(3,6): covM[i][i] = ROOT.TMath.Power(resolution / nM / ROOT.TMath.Sqrt(3), 2)
1902 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
1903 rep.setPosMomCov(stateSmeared, posM, momM, covM)
1905 seedState = ROOT.TVectorD(6)
1906 seedCov = ROOT.TMatrixDSym(6)
1907 rep.get6DStateCov(stateSmeared, seedState, seedCov)
1908 theTrack=ROOT.genfit.Track(rep, seedState, seedCov)
1909 TrackFit(hitPosList,theTrack,charge,pinv)
1910 fittedtrackids.append(trackid34[item1])
1912 if global_variables.debug:
1914 "******* MATCH NOT FOUND stations 12 track id", trackid12[item],
1915 "(fraction", fraction12[item]*100,
"%) and stations 34 track id", trackid34[item1],
1916 "(fraction", fraction34[item1]*100,
"%)"
1918 if trackid12[item]==trackid34[item1] :
1919 if global_variables.debug:
1920 print(
"trackids the same, but not matched.")
1930 return fittedtrackids