Belle II Software  release-08-01-10
plot_LL_diff.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
21 
22 import basf2 as b2
23 
24 from ROOT import Belle2
25 from ROOT import TCanvas, TH2F
26 from simulation import add_simulation
27 from reconstruction import add_reconstruction
28 
29 # the histograms to fill
30 hist = [TH2F(
31  'hist' + str(i),
32  'hist' + str(i),
33  50,
34  0.0,
35  3.5,
36  50,
37  -100,
38  100,
39 ) for i in range(4)]
40 
41 hist[0].SetTitle('True pions, dE/dx')
42 hist[1].SetTitle('True kaons, dE/dx')
43 hist[2].SetTitle('True pions, TOP')
44 hist[3].SetTitle('True kaons, TOP')
45 
46 # define the python module to extract and plot PID information
47 
48 
49 class MinModule(b2.Module):
50 
51  """
52  Get LL differences from PIDLikelihood and fill them into histograms
53  """
54 
55  def __init__(self):
56  """
57  call constructor of base class, required.
58  """
59 
60  super(MinModule, self).__init__()
61 
62  def event(self):
63  """
64  reimplement Module::event()
65  """
66 
67  pids = Belle2.PyStoreArray('PIDLikelihoods')
68  for pid in pids:
69  track = pid.getRelatedFrom('Tracks')
70  if track:
71  mcpart = track.getRelatedTo('MCParticles')
72  if not track or not mcpart:
73  # some tracks don't have an mcparticle
74  b2.B2WARNING('problems with track <-> mcparticle relations')
75  event = Belle2.PyStoreObj('EventMetaData').obj().getEvent()
76  print('event: %d, track: %d' % (event, track.getArrayIndex()))
77  else:
78  pdg = abs(mcpart.getPDG())
79  momentumVec = mcpart.getMomentum()
80  momentum = momentumVec.Mag()
81  if momentum > 3.5: # cut off
82  continue
83  # theta = momentumVec.CosTheta()
84 
85  # particle to compare with pions
86  selectedpart = Belle2.Const.kaon
87  pid_dedx = Belle2.Const.PIDDetectorSet(Belle2.Const.CDC)
88  pid_top = Belle2.Const.PIDDetectorSet(Belle2.Const.TOP)
89  logl_sel = pid.getLogL(selectedpart, pid_dedx)
90  logl_pi = pid.getLogL(Belle2.Const.pion, pid_dedx)
91  dedx_DLL = logl_pi - logl_sel
92 
93  logl_sel = pid.getLogL(selectedpart, pid_top)
94  logl_pi = pid.getLogL(Belle2.Const.pion, pid_top)
95  top_DLL = logl_pi - logl_sel
96 
97  if pdg == selectedpart.getPDGCode():
98  hist[0].Fill(momentum, dedx_DLL)
99  hist[2].Fill(momentum, top_DLL)
100  elif pdg == 211:
101  hist[1].Fill(momentum, dedx_DLL)
102  hist[3].Fill(momentum, top_DLL)
103 
104  def terminate(self):
105  """
106  Draw histograms on canvas and save image.
107  """
108 
109  c1 = TCanvas('c', 'c', 800, 600)
110  c1.Divide(2, 2)
111  c1.Show()
112  for i in range(4):
113  c1.cd(i + 1)
114  hist[i].Draw('colz')
115  # c1.Draw()
116 
117  c1.SaveAs('ll_diff.png')
118  print('Output plots saved in ll_diff.png')
119 
120 
121 # create path
122 main = b2.create_path()
123 
124 # define number of events
125 eventinfosetter = b2.register_module('EventInfoSetter')
126 eventinfosetter.param('evtNumList', [20])
127 main.add_module(eventinfosetter)
128 main.add_module('EvtGenInput')
129 
130 # do full simulation and reconstruction
131 add_simulation(main)
132 add_reconstruction(main)
133 
134 # draw histograms of log likelihood differences
135 main.add_module(MinModule())
136 
137 # process events and print call statistics
138 b2.process(main)
139 print(b2.statistics)
A class for sets of detector IDs whose content is limited to restricted set of valid detector IDs.
Definition: Const.h:287
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67