2 from basf2.pickle_path import deserialize_path, deserialize_module, serialize_path, serialize_module
6 from ROOT
import Belle2
7 from caf.framework
import Calibration
9 from collections
import OrderedDict
11 b2.set_log_level(b2.LogLevel.INFO)
12 UVWABC = [1, 2, 3, 4, 5, 6]
15 def collect(calibration, basf2_args=None):
19 if basf2_args
is None:
22 main = b2.create_path()
23 main.add_module(
'RootInput', inputFileNames=calibration.input_files)
24 main.add_module(
'HistoManager', histoFileName=
'RootOutput.root')
25 main.add_path(calibration.pre_collector_path)
26 main.add_module(calibration.collector)
28 path_file_name = calibration.name +
'.path'
29 with open(path_file_name,
'bw')
as serialized_path_file:
30 pickle.dump(serialize_path(main), serialized_path_file)
32 subprocess.call([
"basf2",
"--execute-path", path_file_name] + basf2_args)
35 def calibrate(calibration, input_file='RootOutput.root'):
36 for algo
in calibration.algorithms:
37 algo.algorithm.setInputFileNames([input_file])
38 algo.algorithm.execute()
39 algo.algorithm.commit()
43 """ The generic Millepede calibration collector+algorithm """
50 primary_vertices=None,
53 components are the names of DB objects to calibrate (BeamSpot etc.)
54 tracks are collections of RecoTracks of fitted charged tracks (usually cosmic rays, no associated particle)
55 particles are names of ParticleLists with single charged particles
56 vertices are names ParticleLists with at least two body decays fitted with vertex constraint
57 primary_vertices are names of ParticleLists with at least two body decays fitted with beam+vertex constraint
60 if components
is None:
64 if (tracks
is None)
and (particles
is None)
and (vertices
is None)
and (primary_vertices
is None):
65 tracks = [
'RecoTracks']
73 if primary_vertices
is None:
77 path = b2.create_path()
78 path.add_module(
'Gearbox')
79 path.add_module(
'Geometry')
80 path.add_module(
'SetupGenfitExtrapolation', noiseBetheBloch=
False, noiseCoulomb=
False, noiseBrems=
False)
87 self.
collector = b2.register_module(
'MillepedeCollector')
96 self.
collector.param(
'particles', particles)
97 self.
collector.param(
'vertices', vertices)
98 self.
collector.param(
'primaryVertices', primary_vertices)
111 """ Select db objects for calibration from list of their names """
114 calibrate_vertex = ((components == [])
or (
'BeamSpot' in components))
116 std_components = ROOT.vector(
'string')()
117 for component
in components:
118 std_components.push_back(component)
120 self.
algo.setComponents(std_components)
121 self.
collector.param(
'components', components)
122 self.
collector.param(
'calibrateVertex', calibrate_vertex)
125 """ Set command for Millepede steering """
133 command = cmd_name +
' ' + str(command)
135 cmd_words = str(command).split(
' ')
136 self.
commands[cmd_words[0]] = command
139 """ Fcn to execute before algorithm... """
143 """ Create the CAF Calibration object """
147 for command
in self.commands.values():
148 self.algo.
steering().command(command)
150 self.algo.
steering().command(
'Parameters')
151 for command
in self.parameters.values():
152 self.algo.
steering().command(command)
157 if not self.get_param(
'useGblTree'):
158 self.set_param(
True,
'absFilePaths')
160 cal =
Calibration(name, self.collector, self.algo, input_files)
161 cal.output_patterns.append(
'Belle2FileCatalog.xml')
162 cal.output_patterns.append(
'constraints.txt')
163 cal.output_patterns.append(
'*.mille')
165 cal.pre_collector_path = self.path
166 cal.pre_algorithms = self.pre_algo
171 """ Get parameter of the collector module or any module in path (given its name) """
173 for mpi
in module.available_params():
174 if mpi.name == param:
179 """ Set parameter of the collector module or any module in path (given its name) """
181 if module
is not None:
182 module.param(param, value)
185 """ Get collector module or any other module from path (given its name) """
190 if mod.name() == module:
193 if isinstance(module, b2.Module):
198 """ serialization """
200 state = self.__dict__.copy()
201 state[
'path'] = serialize_path(self.
path)
202 state[
'collector'] = serialize_module(self.
collector)
206 """ de-serialization """
208 self.__dict__.update(state)
209 self.
collector = deserialize_module(state[
'collector'])
210 self.
path = deserialize_path(state[
'path'])
213 """ Generic fcn to manipulate (fix to given value) parameters identified by db object id (uid),
214 element and parameter number """
216 label.construct(uid, element, param)
217 self.
parameters[str(label.label())] = (str(label.label()) +
' ' + str(value) +
' -1.')
222 """ fix CDC layer X-shift """
226 """ fix CDC layer Y-shift """
230 """ fix CDC layer Phi rotation """
234 """ fix CDC time walk parameter for given board """
238 """ fix CDC tie zero parameter for given wire """
243 def fixVXDid(self, layer, ladder, sensor, segment=0, parameters=[1, 2, 3, 4, 5, 6]):
244 """ Fix VXD element parameters by layer,ladder,sensor and segment number """
245 for param
in parameters:
247 layer, ladder, sensor, segment).
getID(), param)
249 {{"PXD.Ying"}, {Belle2::VxdID(1, 0, 0, 1)}},
250 {{"PXD.Yang"}, {Belle2::VxdID(1, 0, 0, 2)}},
251 {{"SVD.Pat"}, {Belle2::VxdID(3, 0, 0, 1)}},
252 {{"SVD.Mat"}, {Belle2::VxdID(3, 0, 0, 2)}}
256 """ fix PXD Ying half-shell """
257 self.
fixVXDid(1, 0, 0, 1, parameters)
260 """ fix PXD Yang half-shell """
261 self.
fixVXDid(1, 0, 0, 2, parameters)
264 """ fix SVD Pat half-shell """
265 self.
fixVXDid(3, 0, 0, 1, parameters)
268 """ fix SVD Mat half-shell """
269 self.
fixVXDid(3, 0, 0, 2, parameters)
274 """ Fix EKLM sector parameters """
276 for ipar
in parameters:
278 Belle2.EKLMElementID(endcap, layer, sector).getGlobalNumber(),
281 def fixEKLMSegment(self, endcap, layer, sector, plane, segment, parameters=[1, 2, 6]):
282 """ Fix EKLM segment parameters """
283 for ipar
in parameters:
285 Belle2.EKLMElementID(endcap, layer, sector, plane, segment).getGlobalNumber(),
291 """ Fix a BKLM module """
292 for ipar
in parameters:
295 Belle2.BKLMElementID(
301 def fixBKLM(self, sectors=range(1, 9), layers=range(1, 16), forbackwards=[0, 1]):
302 """ Fix (all by default) BKLM modules """
303 for sector
in sectors:
305 for forward
in forbackwards: