Belle II Software  release-08-01-10
Alignment.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # KLM alignment: alignment using the collected data.
12 
13 import sys
14 import basf2
15 import ROOT
16 from ROOT import Belle2
17 from ROOT.Belle2 import KLMChannelIndex, KLMElementNumbers
18 import numpy as np
19 from alignment import MillepedeCalibration
20 
21 basf2.set_log_level(basf2.LogLevel.INFO)
22 
23 basf2.conditions.append_testing_payloads('localdb/database.txt')
24 
25 # Create the algorithm.
26 millepede = MillepedeCalibration(['BKLMAlignment', 'EKLMAlignment', 'EKLMSegmentAlignment'])
27 
28 # Fix module parameters.
29 index = KLMChannelIndex(KLMChannelIndex.c_IndexLevelLayer)
30 index2 = KLMChannelIndex(KLMChannelIndex.c_IndexLevelLayer)
31 while (index != index2.end()):
32  module = index.getKLMModuleNumber()
33  if (index.getSubdetector() == KLMElementNumbers.c_BKLM):
34  for ipar in [1, 2, 3, 4, 5, 6]:
35  # Free parameters are idU, dV, dGamma.
36  if ipar in [1, 2, 6]:
37  continue
38  millepede.fixGlobalParam(Belle2.BKLMAlignment.getGlobalUniqueID(),
39  module, ipar)
40  else:
41  for ipar in [1, 2, 6]:
42  # No parameters are fixed; if necessary, uncomment the following:
43  # millepede.fixGlobalParam(Belle2.EKLMAlignment.getGlobalUniqueID(),
44  # module, ipar)
45  continue
46  index.increment()
47 
48 # Fix EKLM segment parameters.
49 index.setIndexLevel(KLMChannelIndex.c_IndexLevelStrip)
50 index2.setIndexLevel(KLMChannelIndex.c_IndexLevelStrip)
51 index = index2.beginEKLM()
52 index.useEKLMSegments()
53 while (index != index2.endEKLM()):
54  segment = index.getEKLMSegmentNumber()
55  for ipar in [2, 6]:
56  millepede.fixGlobalParam(
58  segment, ipar)
59  index.increment()
60 
61 # Execute the algorithm over all collected data (auto-merged).
62 input_files = sys.argv[1:]
63 calibration = millepede.create('klm_alignment', input_files)
64 millepede.algo.setInputFileNames(input_files)
65 millepede.algo.ignoreUndeterminedParams(True)
66 millepede.algo.invertSign()
67 millepede.algo.execute()
68 millepede.algo.commit()
69 
70 # Get payloads.
71 payloads = list(millepede.algo.getPayloads())
72 bklm_alignment = None
73 eklm_alignment = None
74 eklm_segment_alignment = None
75 for payload in payloads:
76  if payload.name == 'BKLMAlignment':
77  bklm_alignment = payload.object.IsA().DynamicCast(Belle2.BKLMAlignment().IsA(), payload.object, False)
78  elif payload.name == 'EKLMAlignment':
79  eklm_alignment = payload.object.IsA().DynamicCast(Belle2.EKLMAlignment().IsA(), payload.object, False)
80  elif payload.name == 'EKLMAlignment':
81  eklm_segment_alignment = payload.object.IsA().DynamicCast(Belle2.EKLMSegmentAlignment().IsA(), payload.object, False)
82 
83 # Profile plot for all determined parameters
84 profile = ROOT.TH1F(
85  "profile",
86  "correction & errors",
87  millepede.algo.result().getNoDeterminedParameters(),
88  1,
89  millepede.algo.result().getNoDeterminedParameters())
90 
91 # Define some branch variables
92 param = np.zeros(1, dtype=int)
93 value = np.zeros(1, dtype=np.float32)
94 correction = np.zeros(1, dtype=np.float32)
95 error = np.zeros(1, dtype=np.float32)
96 section = np.zeros(1, dtype=int)
97 sector = np.zeros(1, dtype=int)
98 layer = np.zeros(1, dtype=int)
99 sensor = np.zeros(1, dtype=int)
100 plane = np.zeros(1, dtype=int)
101 segment = np.zeros(1, dtype=int)
102 
103 alignment_file = ROOT.TFile('alignment.root', 'recreate')
104 # Tree with BKLM module data.
105 bklm_module_tree = ROOT.TTree('bklm_module', 'BKLM module alignment data')
106 bklm_module_tree.Branch('section', section, 'section/I')
107 bklm_module_tree.Branch('sector', sector, 'sector/I')
108 bklm_module_tree.Branch('layer', layer, 'layer/I')
109 bklm_module_tree.Branch('param', param, 'param/I')
110 bklm_module_tree.Branch('value', value, 'value/F')
111 bklm_module_tree.Branch('correction', correction, 'correction/F')
112 bklm_module_tree.Branch('error', error, 'error/F')
113 # Tree with EKLM module data.
114 eklm_module_tree = ROOT.TTree('eklm_module', 'EKLM module alignment data')
115 eklm_module_tree.Branch('section', section, 'section/I')
116 eklm_module_tree.Branch('sector', sector, 'sector/I')
117 eklm_module_tree.Branch('layer', layer, 'layer/I')
118 eklm_module_tree.Branch('param', param, 'param/I')
119 eklm_module_tree.Branch('value', value, 'value/F')
120 eklm_module_tree.Branch('correction', correction, 'correction/F')
121 eklm_module_tree.Branch('error', error, 'error/F')
122 # Tree with EKLM segment data.
123 eklm_segment_tree = ROOT.TTree('eklm_segment', 'EKLM segment alignment data')
124 eklm_segment_tree.Branch('section', section, 'section/I')
125 eklm_segment_tree.Branch('sector', sector, 'sector/I')
126 eklm_segment_tree.Branch('layer', layer, 'layer/I')
127 eklm_segment_tree.Branch('plane', plane, 'plane/I')
128 eklm_segment_tree.Branch('segment', segment, 'segment/I')
129 eklm_segment_tree.Branch('param', param, 'param/I')
130 eklm_segment_tree.Branch('value', value, 'value/F')
131 eklm_segment_tree.Branch('correction', correction, 'correction/F')
132 eklm_segment_tree.Branch('error', error, 'error/F')
133 
134 # Index of determined parameter.
135 ibin = 0
136 
137 for ipar in range(0, millepede.algo.result().getNoParameters()):
138  label = Belle2.GlobalLabel(millepede.algo.result().getParameterLabel(ipar))
139  param[0] = label.getParameterId()
140  value[0] = 0.0
141 
142  if millepede.algo.result().isParameterDetermined(ipar):
143  correction[0] = millepede.algo.result().getParameterCorrection(ipar)
144  error[0] = millepede.algo.result().getParameterError(ipar)
145  else:
146  correction[0] = 0.0
147  error[0] = -1.0
148 
149  # Module alignment.
150  if (label.getUniqueId() == Belle2.BKLMAlignment.getGlobalUniqueID() or
151  label.getUniqueId() == Belle2.EKLMAlignment.getGlobalUniqueID()):
152  module = label.getElementId()
153  index.setKLMModule(module)
154  section[0] = index.getSection()
155  sector[0] = index.getSector()
156  layer[0] = index.getLayer()
157  if (index.getSubdetector() == KLMElementNumbers.c_BKLM):
158  if (bklm_alignment is not None):
159  value[0] = bklm_alignment.getGlobalParam(module, int(param[0]))
160  bklm_module_tree.Fill()
161  else:
162  if (eklm_alignment is not None):
163  value[0] = eklm_alignment.getGlobalParam(module, int(param[0]))
164  eklm_module_tree.Fill()
165 
166  # EKLM segments alignment.
167  elif (label.getUniqueId() ==
169  segment_global = label.getElementId()
170  index.setEKLMSegment(segment_global)
171  section[0] = index.getSection()
172  sector[0] = index.getSector()
173  layer[0] = index.getLayer()
174  plane[0] = index.getPlane()
175  segment[0] = index.getStrip()
176  if (eklm_segment_alignment is not None):
177  value[0] = eklm_segment_alignment.getGlobalParam(segment, int(param[0]))
178  eklm_segment_tree.Fill()
179 
180  if not millepede.algo.result().isParameterDetermined(ipar):
181  continue
182 
183  ibin = ibin + 1
184  profile.SetBinContent(ibin, value[0])
185  profile.SetBinError(ibin, error[0])
186 
187 alignment_file.cd()
188 profile.Write()
189 bklm_module_tree.Write()
190 eklm_module_tree.Write()
191 eklm_segment_tree.Write()
192 alignment_file.Close()
Class to store BKLM alignment data in the database.
Definition: BKLMAlignment.h:30
static unsigned short getGlobalUniqueID()
Get global unique identifier.
Definition: BKLMAlignment.h:63
Class to store EKLM alignment data in the database.
Definition: EKLMAlignment.h:30
static unsigned short getGlobalUniqueID()
Get global unique identifier.
Definition: EKLMAlignment.h:63
Class to store EKLM alignment data in the database.
static unsigned short getGlobalUniqueID()
Get global unique identifier.
Class to convert to/from global labels for Millepede II to/from detector & parameter identificators.
Definition: GlobalLabel.h:41