Belle II Software  release-05-01-25
beast_tuple_producer.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
5 # This steering script steers the production of ROOT tuples for beast.
6 #
7 # Before running, you have to put a file to IoV mapping called 'file_iov_map.pkl' in your
8 # working directory. You can create such a file using the script 'create_file_to_iov_map.py'
9 # in basf2 folder calibration/examples.
10 #
11 # basf2 beast_tuple_producer.py -- --runLow=5613 --runHigh=5613 --expNo=3
12 #
13 # This will compute tuples run-by-run for all runs between runLow and runHigh for the
14 # given experiment number.
15 #
16 # author: benjamin.schwenker@pyhs.uni-goettingen.de
17 
18 NUMBER_OF_PROCESSES = 20
19 
20 from basf2 import *
21 set_log_level(LogLevel.ERROR)
22 
23 reset_database()
24 use_central_database("Calibration_Offline_Development")
25 
26 from caf.utils import IoV
27 import SetMetaTimeModule
28 
29 import multiprocessing
30 
31 # Some ROOT tools
32 import ROOT
33 from ROOT import Belle2
34 
35 
36 class CalculationProcess(multiprocessing.Process):
37  """ Main class to steer the production of ROOT tuples for beast """
38  def __init__(self, iov, file_paths, output_dir):
39  """ Constructor """
40  super(CalculationProcess, self).__init__()
41 
42  self.iov = iov
43 
44  self.file_paths = file_paths
45 
46  self.output_dir = output_dir
47 
48  def run(self):
49  """ Run """
50  # Register modules
51  rootinput = register_module('RootInput')
52  rootinput.param('inputFileNames', self.file_paths)
53  rootinput.param('branchNames', ['EventMetaData', 'RawPXDs', 'RawSVDs', 'RawCDCs'])
54  gearbox = register_module('Gearbox')
55  gearbox.param('fileName', 'geometry/Beast2_phase2.xml')
56  geometry = register_module('Geometry')
57  geometry.param('components', ['PXD', 'SVD', 'CDC'])
58  pxdclusterizer = register_module('PXDClusterizer')
59  pxdclusterizer.param('ElectronicNoise', 1.0)
60  pxdclusterizer.param('SeedSN', 9.0)
61  pxdtupleproducer = register_module('PXDBgTupleProducer')
62  pxdtupleproducer.param('outputFileName',
63  '{}/pxd_beast_tuple_exp_{}_run_{}.root'.format(self.output_dir,
64  self.iov.exp_low,
65  self.iov.run_low))
66 
67  # Create the path
68  main = create_path()
69  main.add_module(rootinput)
70  main.add_module(SetMetaTimeModule.SetMetaTimeModule())
71  main.add_module(gearbox)
72  main.add_module(geometry)
73  main.add_module('PXDUnpacker')
74  main.add_module("ActivatePXDPixelMasker")
75  main.add_module("ActivatePXDGainCalibrator")
76  main.add_module("PXDRawHitSorter")
77  main.add_module(pxdclusterizer)
78  main.add_module(pxdtupleproducer)
79  main.add_module(register_module('Progress'))
80 
81  # Process the run
82  process(main)
83 
84 
85 #
86 # Function run by worker processes
87 #
88 
89 def worker(task_q, done_q):
90  for iov, file_paths, output_dir in iter(task_q.get, 'STOP'):
91  print("Start processing IoV={}".format(str(iov)))
92  p = CalculationProcess(iov, file_paths, output_dir)
93  p.start()
94  p.join()
95  done_q.put(iov)
96 
97 
98 if __name__ == "__main__":
99 
100  import pickle
101  import argparse
102  parser = argparse.ArgumentParser(description="Produce pxd tuples and histofiles from ROOT formatted raw data")
103  parser.add_argument('--runLow', default=0, type=int, help='Compute mask for specific IoV')
104  parser.add_argument('--runHigh', default=-1, type=int, help='Compute mask for specific IoV')
105  parser.add_argument('--expNo', default=3, type=int, help='Compute mask for specific IoV')
106  parser.add_argument('--outputDir', default='./', type=str, help='Name of output directory for tuples')
107  args = parser.parse_args()
108 
109  # Set the IoV range for this calibration
110  iov_to_calibrate = IoV(exp_low=args.expNo, run_low=args.runLow, exp_high=args.expNo, run_high=args.runHigh)
111 
112  map_file_path = "file_iov_map.pkl"
113  with open(map_file_path, 'br') as map_file:
114  files_to_iovs = pickle.load(map_file)
115 
116  # Set of all currently known single run iovs
117  iov_set = set(files_to_iovs.values())
118 
119  # Dict mapping single run iovs to their input files
120  iovs_to_files = {}
121  for iov in iov_set:
122  if iov_to_calibrate.contains(iov):
123  file_paths = [k for k, v in files_to_iovs.items() if v == iov]
124  iovs_to_files[iov] = file_paths
125 
126  # Create queues
127  task_queue = multiprocessing.Queue()
128  done_queue = multiprocessing.Queue()
129 
130  # Submit tasks
131  for iov, file_paths in iovs_to_files.items():
132  task_queue.put((iov, file_paths, args.outputDir))
133 
134  # Start worker processes
135  for i in range(NUMBER_OF_PROCESSES):
136  multiprocessing.Process(target=worker, args=(task_queue, done_queue)).start()
137 
138  # Get and print results
139  print('Unordered results:')
140  for i in range(len(iovs_to_files)):
141  print('\t', done_queue.get())
142 
143  # Tell child processes to stop
144  for i in range(NUMBER_OF_PROCESSES):
145  task_queue.put('STOP')
beast_tuple_producer.CalculationProcess.iov
iov
interval of validity
Definition: beast_tuple_producer.py:42
beast_tuple_producer.CalculationProcess.run
def run(self)
Definition: beast_tuple_producer.py:48
beast_tuple_producer.CalculationProcess
Definition: beast_tuple_producer.py:36
SetMetaTimeModule.SetMetaTimeModule
Definition: SetMetaTimeModule.py:9
beast_tuple_producer.CalculationProcess.file_paths
file_paths
path to files
Definition: beast_tuple_producer.py:44
beast_tuple_producer.CalculationProcess.output_dir
output_dir
output directory
Definition: beast_tuple_producer.py:46
beast_tuple_producer.CalculationProcess.__init__
def __init__(self, iov, file_paths, output_dir)
Definition: beast_tuple_producer.py:38