Belle II Software  release-08-00-04
cosmicsExtrapolationPlots.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 <header>
13  <contact>software-tracking@belle2.org</contact>
14  <input>CosmicsExtrapolation.root</input>
15  <output>CosmicsExtrapolationPlots.root</output>
16  <description>Validation of cosmic track extrapolation.</description>
17 </header>
18 """
19 
20 
21 import basf2
22 import ROOT
23 from ROOT import Belle2, TNamed
24 
25 contact = 'Kirill Chilikin (chilikin@lebedev.ru)'
26 
27 bklm_numbers = Belle2.BKLMElementNumbers()
29 
30 
31 class CosmicsExtapolationPlotModule(basf2.Module):
32  """ Class for creation of cosmics extrapolation plot module. """
33 
34  def set_options_coordinate(self, histogram, description, shifter):
35  """ Set optiions for coordinate plot. """
36  histogram.SetXTitle('cm')
37  histogram.SetYTitle('Events')
38  function_list = histogram.GetListOfFunctions()
39  function_list.Add(TNamed('Description', description))
40  function_list.Add(TNamed('Check', ' No bias, no large background, resolution ~ 2 cm.'))
41  function_list.Add(TNamed('Contact', contact))
42  if shifter:
43  basf2.B2WARNING("temporarily removed all shifter plots for the CosmicsExtrapolationPlots")
44  # function_list.Add(TNamed('MetaOptions', 'shifter'))
45 
46  def set_options_momentum(self, histogram, description):
47  """ Set options for momentum plot. """
48  histogram.SetXTitle('p [GeV]')
49  histogram.SetYTitle('Events')
50  function_list = histogram.GetListOfFunctions()
51  function_list.Add(TNamed('Description', description))
52  function_list.Add(TNamed('Check', ' No bias, no large background, resolution ~ 2 cm.'))
53  function_list.Add(TNamed('Contact', contact))
54 
55  def __init__(self):
56  """Initialization."""
57  super().__init__()
58 
59 
60  self.output_fileoutput_file = ROOT.TFile('CosmicsExtrapolationPlots.root',
61  'recreate')
62 
63 
64  self.hist_xres_forward_mumhist_xres_forward_mum = \
65  ROOT.TH1F('xres_forward_mum',
66  'X resolution (#mu^{-}, forward)',
67  100, -20, 20)
68  self.set_options_coordinateset_options_coordinate(
69  self.hist_xres_forward_mumhist_xres_forward_mum,
70  'Extrapolated hit X resolution (mu-, forward propagation).', True)
71 
72 
73  self.hist_xres_forward_muphist_xres_forward_mup = \
74  ROOT.TH1F('xres_forward_mup',
75  'X resolution (#mu^{+}, forward)',
76  100, -20, 20)
77  self.set_options_coordinateset_options_coordinate(
78  self.hist_xres_forward_muphist_xres_forward_mup,
79  'Extrapolated hit X resolution (mu+, forward propagation).', True)
80 
81 
82  self.hist_yres_forward_mumhist_yres_forward_mum = \
83  ROOT.TH1F('yres_forward_mum',
84  'Y resolution (#mu^{-}, forward)',
85  100, -20, 20)
86  self.set_options_coordinateset_options_coordinate(
87  self.hist_yres_forward_mumhist_yres_forward_mum,
88  'Extrapolated hit Y resolution (mu-, forward propagation).', False)
89 
90 
91  self.hist_yres_forward_muphist_yres_forward_mup = \
92  ROOT.TH1F('yres_forward_mup',
93  'Y resolution (#mu^{+}, forward)',
94  100, -20, 20)
95  self.set_options_coordinateset_options_coordinate(
96  self.hist_yres_forward_muphist_yres_forward_mup,
97  'Extrapolated hit Y resolution (mu+, forward propagation).', False)
98 
99 
100  self.hist_zres_forward_mumhist_zres_forward_mum = \
101  ROOT.TH1F('zres_forward_mum',
102  'Z resolution (#mu^{-}, forward)',
103  100, -20, 20)
104  self.set_options_coordinateset_options_coordinate(
105  self.hist_zres_forward_mumhist_zres_forward_mum,
106  'Extrapolated hit Z resolution (mu-, forward propagation).', False)
107 
108 
109  self.hist_zres_forward_muphist_zres_forward_mup = \
110  ROOT.TH1F('zres_forward_mup',
111  'Z resolution (#mu^{+}, forward)',
112  100, -20, 20)
113  self.set_options_coordinateset_options_coordinate(
114  self.hist_zres_forward_muphist_zres_forward_mup,
115  'Extrapolated hit Z resolution (mu+, forward propagation).', False)
116 
117 
118  self.hist_xres_backward_mumhist_xres_backward_mum = \
119  ROOT.TH1F('xres_backward_mum',
120  'X resolution (#mu^{-}, backward)',
121  100, -20, 20)
122  self.set_options_coordinateset_options_coordinate(
123  self.hist_xres_backward_mumhist_xres_backward_mum,
124  'Extrapolated hit X resolution (mu-, backward propagation).', True)
125 
126 
127  self.hist_xres_backward_muphist_xres_backward_mup = \
128  ROOT.TH1F('xres_backward_mup',
129  'X resolution (#mu^{+}, backward)',
130  100, -20, 20)
131  self.set_options_coordinateset_options_coordinate(
132  self.hist_xres_backward_muphist_xres_backward_mup,
133  'Extrapolated hit X resolution (mu+, backward propagation).', True)
134 
135 
136  self.hist_yres_backward_mumhist_yres_backward_mum = \
137  ROOT.TH1F('yres_backward_mum',
138  'Y resolution (#mu^{-}, backward)',
139  100, -20, 20)
140  self.set_options_coordinateset_options_coordinate(
141  self.hist_yres_backward_mumhist_yres_backward_mum,
142  'Extrapolated hit Y resolution (mu-, backward propagation).', False)
143 
144 
145  self.hist_yres_backward_muphist_yres_backward_mup = \
146  ROOT.TH1F('yres_backward_mup',
147  'Y resolution (#mu^{+}, backward)',
148  100, -20, 20)
149  self.set_options_coordinateset_options_coordinate(
150  self.hist_yres_backward_muphist_yres_backward_mup,
151  'Extrapolated hit Y resolution (mu+, backward propagation).', False)
152 
153 
154  self.hist_zres_backward_mumhist_zres_backward_mum = \
155  ROOT.TH1F('zres_backward_mum',
156  'Z resolution (#mu^{-}, backward)',
157  100, -20, 20)
158  self.set_options_coordinateset_options_coordinate(
159  self.hist_zres_backward_mumhist_zres_backward_mum,
160  'Extrapolated hit Z resolution (mu-, backward propagation).', False)
161 
162 
163  self.hist_zres_backward_muphist_zres_backward_mup = \
164  ROOT.TH1F('zres_backward_mup',
165  'Z resolution (#mu^{+}, backward)',
166  100, -20, 20)
167  self.set_options_coordinateset_options_coordinate(
168  self.hist_zres_backward_muphist_zres_backward_mup,
169  'Extrapolated hit Z resolution (mu+, backward propagation).', False)
170 
171 
172  self.hist_pres_forward_mumhist_pres_forward_mum = \
173  ROOT.TH1F('pres_forward_mum',
174  'Momentum resolution (#mu^{-}, forward)',
175  100, -10, 10)
176  self.set_options_momentumset_options_momentum(
177  self.hist_pres_forward_mumhist_pres_forward_mum,
178  'Momentum resolution (mu-, forward propagation).')
179 
180 
181  self.hist_pres_forward_muphist_pres_forward_mup = \
182  ROOT.TH1F('pres_forward_mup',
183  'Momentum resolution (#mu^{+}, forward)',
184  100, -10, 10)
185  self.set_options_momentumset_options_momentum(
186  self.hist_pres_forward_muphist_pres_forward_mup,
187  'Momentum resolution (mu+, forward propagation).')
188 
189 
190  self.hist_pres_backward_mumhist_pres_backward_mum = \
191  ROOT.TH1F('pres_backward_mum',
192  'Momentum resolution (#mu^{-}, backward)',
193  100, -10, 10)
194  self.set_options_momentumset_options_momentum(
195  self.hist_pres_backward_mumhist_pres_backward_mum,
196  'Momentum resolution (mu-, backward propagation).')
197 
198 
199  self.hist_pres_backward_muphist_pres_backward_mup = \
200  ROOT.TH1F('pres_backward_mup',
201  'Momentum resolution (#mu^{+}, backward)',
202  100, -10, 10)
203  self.set_options_momentumset_options_momentum(
204  self.hist_pres_backward_muphist_pres_backward_mup,
205  'Momentum resolution (mu+, backward propagation).')
206 
207  def fill_histograms_exthit(self, exthit, klmhit2d):
208  """ Fill histograms with ExtHit data. """
209  ext_position = exthit.getPosition()
210  if exthit.isBackwardPropagated():
211  if exthit.getPdgCode() == 13:
212  self.hist_xres_backward_mumhist_xres_backward_mum.Fill(
213  ext_position.X() - klmhit2d.getPositionX())
214  self.hist_yres_backward_mumhist_yres_backward_mum.Fill(
215  ext_position.Y() - klmhit2d.getPositionY())
216  self.hist_zres_backward_mumhist_zres_backward_mum.Fill(
217  ext_position.Z() - klmhit2d.getPositionZ())
218  elif exthit.getPdgCode() == -13:
219  self.hist_xres_backward_muphist_xres_backward_mup.Fill(
220  ext_position.X() - klmhit2d.getPositionX())
221  self.hist_yres_backward_muphist_yres_backward_mup.Fill(
222  ext_position.Y() - klmhit2d.getPositionY())
223  self.hist_zres_backward_muphist_zres_backward_mup.Fill(
224  ext_position.Z() - klmhit2d.getPositionZ())
225  else:
226  if exthit.getPdgCode() == 13:
227  self.hist_xres_forward_mumhist_xres_forward_mum.Fill(
228  ext_position.X() - klmhit2d.getPositionX())
229  self.hist_yres_forward_mumhist_yres_forward_mum.Fill(
230  ext_position.Y() - klmhit2d.getPositionY())
231  self.hist_zres_forward_mumhist_zres_forward_mum.Fill(
232  ext_position.Z() - klmhit2d.getPositionZ())
233  elif exthit.getPdgCode() == -13:
234  self.hist_xres_forward_muphist_xres_forward_mup.Fill(
235  ext_position.X() - klmhit2d.getPositionX())
236  self.hist_yres_forward_muphist_yres_forward_mup.Fill(
237  ext_position.Y() - klmhit2d.getPositionY())
238  self.hist_zres_forward_muphist_zres_forward_mup.Fill(
239  ext_position.Z() - klmhit2d.getPositionZ())
240 
241  def event(self):
242  """ Event function. """
243  klmhit2ds = Belle2.PyStoreArray('KLMHit2ds')
244  exthits = Belle2.PyStoreArray('ExtHits')
245  tracks = Belle2.PyStoreArray('Tracks')
246  mcparticles = Belle2.PyStoreArray('MCParticles')
247 
248  for klmhit2d in klmhit2ds:
249  subdetector = klmhit2d.getSubdetector()
250  section = klmhit2d.getSection()
251  sector = klmhit2d.getSector()
252  layer = klmhit2d.getLayer()
253  if (subdetector == Belle2.KLMElementNumbers.c_BKLM):
254  for exthit in exthits:
255  if exthit.getDetectorID() != Belle2.Const.BKLM:
256  continue
257  module = exthit.getCopyID()
258  section_ext = bklm_numbers.getSectionByModule(module)
259  sector_ext = bklm_numbers.getSectorByModule(module)
260  layer_ext = bklm_numbers.getLayerByModule(module)
261  if (section_ext != section or sector_ext != sector or
262  layer_ext != layer):
263  continue
264  self.fill_histograms_exthitfill_histograms_exthit(exthit, klmhit2d)
265  else:
266  section = klmhit2d.getSection()
267  sector = klmhit2d.getSector()
268  layer = klmhit2d.getLayer()
269  for exthit in exthits:
270  if exthit.getDetectorID() != Belle2.Const.EKLM:
271  continue
272  strip_global = exthit.getCopyID()
273  section_ext = eklm_numbers.getSectionByGlobalStrip(strip_global)
274  sector_ext = eklm_numbers.getSectorByGlobalStrip(strip_global)
275  layer_ext = eklm_numbers.getLayerByGlobalStrip(strip_global)
276  if (section_ext != section or sector_ext != sector or
277  layer_ext != layer):
278  continue
279  self.fill_histograms_exthitfill_histograms_exthit(exthit, klmhit2d)
280 
281  for track in tracks:
282  track_fit_result = track.getTrackFitResult(Belle2.Const.muon)
283  momentum = track_fit_result.getMomentum()
284  track_exthits = track.getRelationsTo('ExtHits')
285  if len(track_exthits) == 0:
286  continue
287  mc_momentum = mcparticles[0].getMomentum()
288  p_diff = momentum.R() - mc_momentum.R()
289  muon_found = False
290  for i in range(len(track_exthits)):
291  if abs(track_exthits[i].getPdgCode()) == 13:
292  exthit = track_exthits[i]
293  muon_found = True
294  if not muon_found:
295  continue
296  if exthit.isBackwardPropagated():
297  if exthit.getPdgCode() == 13:
298  self.hist_pres_backward_mumhist_pres_backward_mum.Fill(p_diff)
299  elif exthit.getPdgCode() == -13:
300  self.hist_pres_backward_muphist_pres_backward_mup.Fill(p_diff)
301  else:
302  if exthit.getPdgCode() == 13:
303  self.hist_pres_forward_mumhist_pres_forward_mum.Fill(p_diff)
304  elif exthit.getPdgCode() == -13:
305  self.hist_pres_forward_muphist_pres_forward_mup.Fill(p_diff)
306 
307  def terminate(self):
308  """ Termination function. """
309  self.output_fileoutput_file.cd()
310  self.hist_xres_forward_mumhist_xres_forward_mum.Write()
311  self.hist_xres_forward_muphist_xres_forward_mup.Write()
312  self.hist_yres_forward_mumhist_yres_forward_mum.Write()
313  self.hist_yres_forward_muphist_yres_forward_mup.Write()
314  self.hist_zres_forward_mumhist_zres_forward_mum.Write()
315  self.hist_zres_forward_muphist_zres_forward_mup.Write()
316  self.hist_xres_backward_mumhist_xres_backward_mum.Write()
317  self.hist_xres_backward_muphist_xres_backward_mup.Write()
318  self.hist_yres_backward_mumhist_yres_backward_mum.Write()
319  self.hist_yres_backward_muphist_yres_backward_mup.Write()
320  self.hist_zres_backward_mumhist_zres_backward_mum.Write()
321  self.hist_zres_backward_muphist_zres_backward_mup.Write()
322  self.hist_pres_forward_mumhist_pres_forward_mum.Write()
323  self.hist_pres_forward_muphist_pres_forward_mup.Write()
324  self.hist_pres_backward_mumhist_pres_backward_mum.Write()
325  self.hist_pres_backward_muphist_pres_backward_mup.Write()
326  self.output_fileoutput_file.Close()
327 
328 
329 # Input.
330 root_input = basf2.register_module('RootInput')
331 root_input.param('inputFileName', '../CosmicsExtrapolation.root')
332 
333 # Plotting.
335 
336 # Create main path.
337 main = basf2.create_path()
338 
339 # Add modules to main path
340 main.add_module(root_input)
341 main.add_module(plot)
342 
343 # Run.
344 basf2.process(main)
BKLM element numbers.
static const EKLMElementNumbers & Instance()
Instantiation.
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
hist_yres_forward_mum
Y resolution histogram (forward propagation, mu-).
hist_xres_backward_mup
X resolution histogram (backward propagation, mu+).
hist_pres_backward_mum
Momentum resolution histograms (backward propagation, mu-).
hist_zres_forward_mum
Z resolution histogram (forward propagation, mu-).
hist_pres_forward_mum
Momentum resolution histogram (forward propagation, mu-).
hist_xres_forward_mum
X resolution histogram (forward propagation, mu-).
hist_yres_backward_mum
Y resolution histogram (backward propagation, mu-).
hist_xres_backward_mum
X resolution histogram (backward propagation, mu-).
hist_yres_backward_mup
Y resolution histogram (backward propagation, mu+).
hist_xres_forward_mup
X resolution histogram (forward propagation, mu+).
hist_zres_backward_mum
Z resolution histogram (backward propagation, mu-).
hist_pres_forward_mup
Momentum resolution histogram (forward propagation, mu+).
hist_zres_forward_mup
Z resolution histogram (forward propagation, mu+).
hist_pres_backward_mup
Momentum resolution histograms (backward propagation, mu+).
def set_options_coordinate(self, histogram, description, shifter)
hist_zres_backward_mup
Z resolution histogram (backward propagation, mu+).
hist_yres_forward_mup
Y resolution histogram (forward propagation, mu+).