2 @author: Claus Kleinwort (DESY), Tadeas Bilka
5 from ROOT
import Belle2
13 Class representing a linear constraint for global parameters
23 The constant term of the constraint: sum(c_i * par_i) = value
32 def add(self, label, coeff):
34 Add coefficient for a global parameter
39 global parameter id (GlobalLabel::label())
41 coefficent of the parameter in constraint
43 entry = (label, coeff)
44 self.
data.append(entry)
47 """Get a checksum to distinguish constraints quickly (computed only from labels)
49 labels_only = [label
for (label, coeff)
in self.
data]
50 checksum = hash(str(labels_only))
56 Base class representing a "generator" for file with set of constraints
58 Can be used directly if you already have a file with constraints.
64 Desired filename for the constraint file
71 Should be overriden by the deriving classes and actually
72 fill the dictionary with the constraints (this runs withing a basf2 module
73 event function - so you have geometry available)
80 Can be overriden be child classes to pass additional configuration to the
81 MillepedeCollector (activated by the use of the constraints)
87 Run the constraints generation and return file names
91 constraint_sets : list (alignment.Constraints)
92 List of sets of constraints
93 timedep_config : list(tuple(list(int), list(tuple(int, int, int))))
94 Time-depence configuration.
95 Each list item is 2-tuple with list of parameter numbers (use alignment.parameters to get them) and
96 the (event, run, exp) numbers at which values of these parameters can change.
97 global_tags : list (str)
98 List of global tag names and/or (absolute) file paths to local databases
99 init_event : tuple( int, int, int)
100 Event (event, run, exp) at which to initialize time-INdependent constraints
103 for filename
in [consts.filename
for consts
in constraint_sets]:
104 files.append(os.path.abspath(filename))
107 ccfn = save_config(constraint_sets, timedep, global_tags, init_event)
108 os.system(
'basf2 {} {}'.format(b2.find_file(
'alignment/scripts/alignment/constraints_generator.py'), ccfn))
117 Constraints for VXD hierarchy
119 They come in 3 types:
121 0 : no hierarchy, just sensors aligned in their local system
122 1 : sensors -> half-shells (no ladders)
123 2 : sensors -> ladders ->half-shells (aka full hierarchy)
125 and additionaly can use them for PXD, SVD or both (default) (setting pxd/svd to False
126 will disble the hierarchy for that sub-detector)
128 NOTE: the filename cannot be currently changed, it is hard-coded in the collector module
129 as these constraints are actually generated by C++ code and not by python modules as others.
135 Type of constraints (0 - no, 1 - half-shells, 2 -full)
137 Use constraints for PXD?
139 Use constraints for SVD?
143 super(VXDHierarchyConstraints, self).
__init__(
"constraints.txt")
153 Propagate the hierarchy configuration to the collector
155 collector.param(
'hierarchyType', self.
type)
156 collector.param(
'enablePXDHierarchy', self.
pxd)
157 collector.param(
'enableSVDHierarchy', self.
svd)
161 Generate the constraints - does nothing. The collector will make the job
163 print(
"Generating constraints for VXD (no-op, file created by collector) ...")
169 Various constraints for CDC layers
171 (Code from Claus Kleinwort)
175 cdc = [[
'A1', 8, 160, 16.80, 23.80, 0., -35.9, 67.9],
176 [
'U2', 6, 160, 25.70, 34.80, 0.068, -51.4, 98.6],
177 [
'A3', 6, 192, 36.52, 45.57, 0., -57.5, 132.9],
178 [
'V4', 6, 224, 47.69, 56.69, -0.060, -59.6, 144.7],
179 [
'A5', 6, 256, 58.41, 67.41, 0., -61.8, 146.8],
180 [
'U6', 6, 288, 69.53, 78.53, 0.064, -63.9, 148.9],
181 [
'A7', 6, 320, 80.25, 89.25, 0., -66.0, 151.0],
182 [
'V8', 6, 352, 91.37, 100.37, -0.072, -68.2, 153.2],
183 [
'A9', 6, 384, 102.00, 111.14, 0., -70.2, 155.3]]
186 parameter = [(1,
'x-offset bwd'), (2,
'y-offset bwd'), (6,
'z-rotation bwd'),
187 (11,
'x-offset fwd-bwd'), (12,
'y-offset fwd-bwd'), (16,
'z-rotation fwd-bwd')]
189 def __init__(self, filename='cdc-constraints.txt', rigid=True, z_offset=False, r_scale=False, z_scale=False):
192 Filename with constraints (probably generated later)
196 Constraint for Z-offset
198 Constraint for R-scale
200 Constraint for Z-scale
202 super(CDCLayerConstraints, self).
__init__(filename)
213 """Generate constraints from CDC geometry
215 print(
"Generating constraints for CDC layers...")
217 def cdc_layer_label(layer, param):
225 return (a > b) - (a < b)
233 for lyr
in range(nlyr):
246 for lyr
in range(nlyr):
247 label = cdc_layer_label(nTot + lyr, par[0])
249 const.add(label, 1.0)
268 dr = (rOuter - rInner) / (nlyr - 1)
270 for lyr
in range(nlyr):
271 label = cdc_layer_label(nTot + lyr, par[0])
273 const.add(label, stereo * rWire)
289 for lyr
in range(nlyr):
290 label = cdc_layer_label(nTot + lyr, par[0])
291 der = cmp(2.*float(nTot + lyr) + 0.5, float(nLayer))
293 const.add(label, der)
309 for lyr
in range(nlyr):
310 label = cdc_layer_label(nTot + lyr, par[0])
312 const.add(label, stereo)
323 Constraint to fix sum of corrections to CDC T0's (per wire) to zero
328 160, 160, 160, 160, 160, 160, 160, 160,
329 160, 160, 160, 160, 160, 160,
330 192, 192, 192, 192, 192, 192,
331 224, 224, 224, 224, 224, 224,
332 256, 256, 256, 256, 256, 256,
333 288, 288, 288, 288, 288, 288,
334 320, 320, 320, 320, 320, 320,
335 352, 352, 352, 352, 352, 352,
336 384, 384, 384, 384, 384, 384]
338 def __init__(self, filename='cdc-T0-constraints.txt'):
341 Can use different filename
343 super(CDCTimeZerosConstraint, self).
__init__(filename)
347 Generate the constraints
349 print(
"Generating constraints for CDC T0's ...")
353 for layer
in range(0, 56):
357 const.add(label.label(), 1.0)
365 Various constraints for CDC wires (w.r.t. layers)
370 160, 160, 160, 160, 160, 160, 160, 160,
371 160, 160, 160, 160, 160, 160,
372 192, 192, 192, 192, 192, 192,
373 224, 224, 224, 224, 224, 224,
374 256, 256, 256, 256, 256, 256,
375 288, 288, 288, 288, 288, 288,
376 320, 320, 320, 320, 320, 320,
377 352, 352, 352, 352, 352, 352,
378 384, 384, 384, 384, 384, 384]
380 def __init__(self, filename='cdc-wire-constraints.txt', layers=None, layer_rigid=True, layer_radius=False, cdc_radius=False):
381 """Initialize constraint
386 Can override the default file name
388 List of layer numbers for which to generate the constraints (default is 0..55)
390 6 constraints - fix sum of shifts of wire ends in x/y/rotation at fwd/bwd end-plate
392 2 constraints to fix average change of layer radius (wires in layer moving away from origin)
394 1 constraint - fix average change in CDC radius from all wires
398 super(CDCWireConstraints, self).
__init__(filename)
401 layers = [lst
for lst
in range(0, 56)]
416 """Enables wire-by-wire derivatives in collector
418 b2.B2WARNING(
"Adding CDC wire constraints -> enabling wire-by-wire alignment derivatives")
419 collector.param(
'enableWireByWireAlignment',
True)
422 """Return GlobalLabel for wire parameter"""
430 """Generate constraints """
431 print(
"Generating constraints for CDC wires...")
441 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdX), 1.)
448 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdY), 1.)
458 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdX), -math.sin(wirePhi))
459 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdY), +math.cos(wirePhi))
466 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdX), 1.)
473 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdY), 1.)
483 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdX), -math.sin(wirePhi))
484 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdY), +math.cos(wirePhi))
495 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdX), +math.cos(wirePhi))
496 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdY), +math.sin(wirePhi))
497 const.add(self.
get_label(layer, 0, 0), 0.)
507 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdX), +math.cos(wirePhi))
508 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdY), +math.sin(wirePhi))
509 const.add(self.
get_label(layer, 0, 0), 0.)
518 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdX), +math.cos(wirePhi))
519 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireBwdY), +math.sin(wirePhi))
527 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdX), +math.cos(wirePhi))
528 const.add(self.
get_label(layer, wire, Belle2.CDCAlignment.wireFwdY), +math.sin(wirePhi))
536 if __name__ ==
'__main__':
538 consts6 =
CDCLayerConstraints(
'cdc-layer-constraints-6D.txt', rigid=
True, z_offset=
False, r_scale=
False, z_scale=
False)
540 consts7 =
CDCLayerConstraints(
'cdc-layer-constraints-7D.txt', rigid=
True, z_offset=
True, r_scale=
False, z_scale=
False)
542 consts10 =
CDCLayerConstraints(
'cdc-layer-constraints-10D.txt', rigid=
True, z_offset=
True, r_scale=
True, z_scale=
True)
557 init_event = (0, 0, 0)
571 init_event=init_event)