trackmatch.py
Go to the documentation of this file.
1 import garanainit # loads garana libs
2 import ROOT #load ROOT libs
3 from ROOT import TCanvas
4 from ROOT import TH1F
5 from ROOT import TH2F
6 from ROOT import TMath
7 from ROOT import TEfficiency
8 
9 def trackmatch():
10 
12  #ROOT.gROOT.SetBatch(True) # run in batch mode (don't display plots)
13 
14  tm = ROOT.TreeManager("./test.root") #structuredtree.root")
15 
16  # truth matching utility
17  bt = ROOT.Backtracker(tm);
18 
19  print("constructed treemanager")
20 
21  #tree accessors
22  #gen = tm.GetGenTree()
23  g4 = tm.GetG4Tree()
24  rec = tm.GetRecoTree()
25 
26  nmatch = 0 # number of reco tracks matched to a G4Particle
27  ntrack = 0 # number of reco tracks
28  nprimary = 0 # true number of primaries
29  nprimary_match = 0 #number of primaries matched to at least one track
30 
31  hg4_nmatch = TH1F('hg4_nmatch','G4Particle -> Track matching;N^{0} matches',10,0,10)
32 
33  hfrac = TH1F("hfrac","particle contributing most energy;fraction of total",50,0,1.1)
34  hncont = TH1F("hncont","particles contributing energy;N^{0} particles",10,0,10)
35  hncontfrac = TH2F('hncontfrac','particles contributing energy;N^{0} particles;max fraction of total',10,0,25,0,1.1)
36 
37  eff_g4match = TEfficiency('eff_g4match','G4Particle -> Track matching; initial energy [GeV]; efficiency',10,0,5)
38 
39  # loop over events
40  for ientry in range(rec.NEntries()):
41 
42  if ientry%100 == 0 : print('processing event '+str(ientry))
43 
44  tm.GetEntry(ientry)
45  bt.FillMaps() #load associations for this entry
46 
47  #nprimary += g4.NPrimary()
48  for ig4 in range(g4.NSim()):
49  if not g4.IsPrimary(ig4) : continue # only consider primaries
50  if not abs(g4.PDG(ig4)) == 2212 : continue # muons only
51  nprimary += 1
52  if bt.G4ParticleToTracks(ig4).size() > 0 : nprimary_match += 1 # matched to something?
53  hg4_nmatch.Fill(bt.G4ParticleToTracks(ig4).size())
54  eff_g4match.Fill(bt.G4ParticleToTracks(ig4).size() > 0, g4.SimMomEnter(ig4,0).E())
55 
56  ntrack += rec.NTrack()
57  for itrack in range(rec.NTrack()):
58 
59  imatch = bt.TrackToG4Particle(itrack);
60  if imatch < g4.NSim(): nmatch += 1
61 
62  #imatches = bt.TrackToG4Particles(itrack)
63  hncont.Fill(rec.TrackNTrueTrack(itrack)) #len(imatches))
64  for ip in range(rec.TrackNTrueTrack(itrack)):
65  hfrac.Fill(rec.TrackMaxDepositFrac(itrack))
66  hncontfrac.Fill(rec.TrackNTrueTrack(itrack),rec.TrackMaxDepositFrac(itrack))
67 
68  if nprimary != 0 :
69  print('number of primaries found: ',nprimary)
70  print('number of matched primaries: ',nprimary_match)
71  print('g4particle -> track matching efficiency: ',1.0*nprimary_match/nprimary)
72  else : print('no G4particles found!')
73 
74  if ntrack != 0 :print('track -> g4particle matching efficiency: ',1.0*nmatch/ntrack)
75  else : print('no tracks found!')
76 
77  print('hncont size is ',hncont.Integral())
78  print('hfrac size is ',hfrac.Integral())
79 
80  cg4_nmatch = TCanvas()
81  hg4_nmatch.Draw()
82  cg4_nmatch.SaveAs('g4_nmatch.png')
83 
84  cncont = TCanvas()
85  hncont.Draw()
86  cncont.SaveAs('ncont.png')
87 
88  cfrac = TCanvas()
89  hfrac.Draw()
90  cfrac.SaveAs('frac.png')
91 
92  cncontfrac = TCanvas()
93  hncontfrac.Draw()
94  cncontfrac.SaveAs('ncont_v_frac.png')
95 
96  ceff_g4match = TCanvas()
97  eff_g4match.Draw()
98  ceff_g4match.SaveAs('g4match_eff.png')
99 
100  # ask for user input as a way to keep the program from ending so we can
101  # see the histogram.
102  # python doesn't like empty input strings so I abuse exception handling
103  try:
104  null = input("press <Enter> to close canvas and exit program.")
105 
106  except:
107  null = 'null'
108 
109 #execute as a script
110 if __name__ == '__main__':
111  trackmatch()
def trackmatch()
Definition: trackmatch.py:9
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
T abs(T value)
static int input(void)
Definition: code.cpp:15695
def init()
Definition: garanainit.py:12
static QCString str