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