Belle II Software development
Alignment.py
1#!/usr/bin/env python3
2
3
10
11# KLM alignment: alignment using the collected data.
12
13import sys
14import basf2
15import ROOT
16from ROOT import Belle2
17from ROOT.Belle2 import KLMChannelIndex, KLMElementNumbers
18import numpy as np
19from alignment import MillepedeCalibration
20
21basf2.set_log_level(basf2.LogLevel.INFO)
22
23basf2.conditions.append_testing_payloads('localdb/database.txt')
24
25# Create the algorithm.
26millepede = MillepedeCalibration(['BKLMAlignment', 'EKLMAlignment', 'EKLMSegmentAlignment'])
27
28# Fix module parameters.
29index = KLMChannelIndex(KLMChannelIndex.c_IndexLevelLayer)
30index2 = KLMChannelIndex(KLMChannelIndex.c_IndexLevelLayer)
31while (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.
49index.setIndexLevel(KLMChannelIndex.c_IndexLevelStrip)
50index2.setIndexLevel(KLMChannelIndex.c_IndexLevelStrip)
51index = index2.beginEKLM()
52index.useEKLMSegments()
53while (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).
62input_files = sys.argv[1:]
63calibration = millepede.create('klm_alignment', input_files)
64millepede.algo.setInputFileNames(input_files)
65millepede.algo.ignoreUndeterminedParams(True)
66millepede.algo.invertSign()
67millepede.algo.execute()
68millepede.algo.commit()
69
70# Get payloads.
71payloads = list(millepede.algo.getPayloads())
72bklm_alignment = None
73eklm_alignment = None
74eklm_segment_alignment = None
75for 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
84profile = 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
92param = np.zeros(1, dtype=int)
93value = np.zeros(1, dtype=np.float32)
94correction = np.zeros(1, dtype=np.float32)
95error = np.zeros(1, dtype=np.float32)
96section = np.zeros(1, dtype=int)
97sector = np.zeros(1, dtype=int)
98layer = np.zeros(1, dtype=int)
99sensor = np.zeros(1, dtype=int)
100plane = np.zeros(1, dtype=int)
101segment = np.zeros(1, dtype=int)
102
103alignment_file = ROOT.TFile('alignment.root', 'recreate')
104# Tree with BKLM module data.
105bklm_module_tree = ROOT.TTree('bklm_module', 'BKLM module alignment data')
106bklm_module_tree.Branch('section', section, 'section/I')
107bklm_module_tree.Branch('sector', sector, 'sector/I')
108bklm_module_tree.Branch('layer', layer, 'layer/I')
109bklm_module_tree.Branch('param', param, 'param/I')
110bklm_module_tree.Branch('value', value, 'value/F')
111bklm_module_tree.Branch('correction', correction, 'correction/F')
112bklm_module_tree.Branch('error', error, 'error/F')
113# Tree with EKLM module data.
114eklm_module_tree = ROOT.TTree('eklm_module', 'EKLM module alignment data')
115eklm_module_tree.Branch('section', section, 'section/I')
116eklm_module_tree.Branch('sector', sector, 'sector/I')
117eklm_module_tree.Branch('layer', layer, 'layer/I')
118eklm_module_tree.Branch('param', param, 'param/I')
119eklm_module_tree.Branch('value', value, 'value/F')
120eklm_module_tree.Branch('correction', correction, 'correction/F')
121eklm_module_tree.Branch('error', error, 'error/F')
122# Tree with EKLM segment data.
123eklm_segment_tree = ROOT.TTree('eklm_segment', 'EKLM segment alignment data')
124eklm_segment_tree.Branch('section', section, 'section/I')
125eklm_segment_tree.Branch('sector', sector, 'sector/I')
126eklm_segment_tree.Branch('layer', layer, 'layer/I')
127eklm_segment_tree.Branch('plane', plane, 'plane/I')
128eklm_segment_tree.Branch('segment', segment, 'segment/I')
129eklm_segment_tree.Branch('param', param, 'param/I')
130eklm_segment_tree.Branch('value', value, 'value/F')
131eklm_segment_tree.Branch('correction', correction, 'correction/F')
132eklm_segment_tree.Branch('error', error, 'error/F')
133
134# Index of determined parameter.
135ibin = 0
136
137for 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
187alignment_file.cd()
188profile.Write()
189bklm_module_tree.Write()
190eklm_module_tree.Write()
191eklm_segment_tree.Write()
192alignment_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