Belle II Software  release-05-02-19
prompt_utils.py
1 def fix_mille_paths_for_algo(algo):
2  """
3  Add pre.algorithm to fix mille file paths
4 
5  Attach a pre-algorithm which loops over collector files
6  and fixes the paths to mille binary files (in case they have
7  been moved since creation - otherwise the effect is null)
8 
9  If previous pre_algorithm exists, it is run after the fix
10  is applied.
11 
12  Parameters
13  ----------
14  algo : caf.framework.Algorithm
15  The algorithm to which to attach pre_algorithm
16  """
17 
18  prev_prealgo = algo.pre_algorithm
19 
20  def fixMillePaths(algorithm, iteration):
21  import ROOT
22  import os
23 
24  print("Now fixing .mille binary paths in CollectorOutput.root files")
25 
26  for path in algorithm.getInputFileNames():
27  file = ROOT.TFile(path, "UPDATE")
28  dirname = os.path.dirname(path)
29 
30  runRange = file.Get("MillepedeCollector/RunRange")
31  runSet = [(-1, -1)] if runRange.getGranularity() == "all" else [(e, r) for e, r in runRange.getExpRunSet()]
32 
33  for exp, run in runSet:
34  milleData = file.Get(f"MillepedeCollector/mille/mille_{exp}.{run}/mille_1")
35 
36  fixed_milleFiles = [os.path.join(dirname, os.path.basename(milleFile)) for milleFile in milleData.getFiles()]
37 
38  milleData.clear()
39  for f in fixed_milleFiles:
40  milleData.addFile(f)
41 
42  file.cd(f"MillepedeCollector/mille/mille_{exp}.{run}/")
43  milleData.Write("", ROOT.TObject.kOverwrite)
44  file.Write()
45  file.Close()
46 
47  # Run the previously set pre-algorithm if any
48  if prev_prealgo is not None:
49  prev_prealgo(algorithm, iteration)
50 
51  algo.pre_algorithm = fixMillePaths