2 from basf2.pickle_path import deserialize_path, deserialize_module, serialize_path, serialize_module
5 from ROOT
import Belle2
6 from caf.framework
import Calibration
8 from collections
import OrderedDict
10 b2.set_log_level(b2.LogLevel.INFO)
11 UVWABC = [1, 2, 3, 4, 5, 6]
14 def collect(calibration, basf2_args=None):
18 if basf2_args
is None:
21 main = b2.create_path()
22 main.add_module(
'RootInput', inputFileNames=calibration.input_files)
23 main.add_module(
'HistoManager', histoFileName=
'RootOutput.root')
24 main.add_path(calibration.pre_collector_path)
25 main.add_module(calibration.collector)
27 path_file_name = calibration.name +
'.path'
28 with open(path_file_name,
'bw')
as serialized_path_file:
29 pickle.dump(serialize_path(main), serialized_path_file)
31 subprocess.call([
"basf2",
"--execute-path", path_file_name] + basf2_args)
34 def calibrate(calibration, input_file='RootOutput.root'):
35 for algo
in calibration.algorithms:
36 algo.algorithm.setInputFileNames([input_file])
37 algo.algorithm.execute()
38 algo.algorithm.commit()
42 """ The generic Millepede calibration collector+algorithm """
49 primary_vertices=None,
52 components are the names of DB objects to calibrate (BeamSpot etc.)
53 tracks are collections of RecoTracks of fitted charged tracks (usually cosmic rays, no associated particle)
54 particles are names of ParticleLists with single charged particles
55 vertices are names ParticleLists with at least two body decays fitted with vertex constraint
56 primary_vertices are names of ParticleLists with at least two body decays fitted with beam+vertex constraint
59 if components
is None:
63 if (tracks
is None)
and (particles
is None)
and (vertices
is None)
and (primary_vertices
is None):
64 tracks = [
'RecoTracks']
72 if primary_vertices
is None:
76 path = b2.create_path()
77 path.add_module(
'Gearbox')
78 path.add_module(
'Geometry')
79 path.add_module(
'SetupGenfitExtrapolation', noiseBetheBloch=
False, noiseCoulomb=
False, noiseBrems=
False)
86 self.
collector = b2.register_module(
'MillepedeCollector')
95 self.
collector.param(
'particles', particles)
96 self.
collector.param(
'vertices', vertices)
97 self.
collector.param(
'primaryVertices', primary_vertices)
110 """ Select db objects for calibration from list of their names """
113 calibrate_vertex = ((components == [])
or (
'BeamSpot' in components))
115 std_components = ROOT.vector(
'string')()
116 for component
in components:
117 std_components.push_back(component)
119 self.
algo.setComponents(std_components)
120 self.
collector.param(
'components', components)
121 self.
collector.param(
'calibrateVertex', calibrate_vertex)
124 """ Set command for Millepede steering """
132 command = cmd_name +
' ' + str(command)
134 cmd_words = str(command).split(
' ')
135 self.
commands[cmd_words[0]] = command
138 """ Fcn to execute before algorithm... """
142 """ Create the CAF Calibration object """
146 for command
in self.commands.values():
147 self.algo.
steering().command(command)
149 self.algo.
steering().command(
'Parameters')
150 for command
in self.parameters.values():
151 self.algo.
steering().command(command)
156 if not self.get_param(
'useGblTree'):
157 self.set_param(
True,
'absFilePaths')
159 cal =
Calibration(name, self.collector, self.algo, input_files)
160 cal.output_patterns.append(
'Belle2FileCatalog.xml')
161 cal.output_patterns.append(
'constraints.txt')
162 cal.output_patterns.append(
'*.mille')
164 cal.pre_collector_path = self.path
165 cal.pre_algorithms = self.pre_algo
170 """ Get parameter of the collector module or any module in path (given its name) """
172 for mpi
in module.available_params():
173 if mpi.name == param:
178 """ Set parameter of the collector module or any module in path (given its name) """
180 if module
is not None:
181 module.param(param, value)
184 """ Get collector module or any other module from path (given its name) """
189 if mod.name() == module:
192 if isinstance(module, b2.Module):
197 """ serialization """
199 state = self.__dict__.copy()
200 state[
'path'] = serialize_path(self.
path)
201 state[
'collector'] = serialize_module(self.
collector)
205 """ de-serialization """
207 self.__dict__.update(state)
208 self.
collector = deserialize_module(state[
'collector'])
209 self.
path = deserialize_path(state[
'path'])
212 """ Generic fcn to manipulate (fix to given value) parameters identified by db object id (uid),
213 element and parameter number """
215 label.construct(uid, element, param)
216 self.
parameters[str(label.label())] = (str(label.label()) +
' ' + str(value) +
' -1.')
221 """ fix CDC layer X-shift """
225 """ fix CDC layer Y-shift """
229 """ fix CDC layer Phi rotation """
233 """ fix CDC time walk parameter for given board """
237 """ fix CDC tie zero parameter for given wire """
242 def fixVXDid(self, layer, ladder, sensor, segment=0, parameters=[1, 2, 3, 4, 5, 6]):
243 """ Fix VXD element parameters by layer,ladder,sensor and segment number """
244 for param
in parameters:
246 layer, ladder, sensor, segment).
getID(), param)
248 {{"PXD.Ying"}, {Belle2::VxdID(1, 0, 0, 1)}},
249 {{"PXD.Yang"}, {Belle2::VxdID(1, 0, 0, 2)}},
250 {{"SVD.Pat"}, {Belle2::VxdID(3, 0, 0, 1)}},
251 {{"SVD.Mat"}, {Belle2::VxdID(3, 0, 0, 2)}}
255 """ fix PXD Ying half-shell """
256 self.
fixVXDid(1, 0, 0, 1, parameters)
259 """ fix PXD Yang half-shell """
260 self.
fixVXDid(1, 0, 0, 2, parameters)
263 """ fix SVD Pat half-shell """
264 self.
fixVXDid(3, 0, 0, 1, parameters)
267 """ fix SVD Mat half-shell """
268 self.
fixVXDid(3, 0, 0, 2, parameters)
273 """ Fix EKLM sector parameters """
275 for ipar
in parameters:
277 Belle2.EKLMElementID(endcap, layer, sector).getGlobalNumber(),
280 def fixEKLMSegment(self, endcap, layer, sector, plane, segment, parameters=[1, 2, 6]):
281 """ Fix EKLM segment parameters """
282 for ipar
in parameters:
284 Belle2.EKLMElementID(endcap, layer, sector, plane, segment).getGlobalNumber(),
290 """ Fix a BKLM module """
291 for ipar
in parameters:
294 Belle2.BKLMElementID(
300 def fixBKLM(self, sectors=range(1, 9), layers=range(1, 16), forbackwards=[0, 1]):
301 """ Fix (all by default) BKLM modules """
302 for sector
in sectors:
304 for forward
in forbackwards: