Belle II Software  release-08-01-10
importChannelMask.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import basf2 as b2
13 from ROOT.Belle2 import TOPDatabaseImporter
14 import os
15 import sys
16 import glob
17 
18 # Create path
19 main = b2.create_path()
20 
21 # Event info setter - execute single event
22 main.add_module('EventInfoSetter', evtNumList=[1])
23 
24 # Initialize TOP geometry parameters using default GT
25 main.add_module('TOPGeometryParInitializer')
26 
27 # process single event
28 b2.process(main)
29 
30 # define local database with write access
31 b2.conditions.expert_settings(save_payloads="localDB/localDB.txt")
32 
33 # and then run the importer
34 dbImporter = TOPDatabaseImporter()
35 
36 # import constants
37 fileNames = sorted(glob.glob('masks/*.root'))
38 numFiles = len(fileNames)
39 if numFiles == 0:
40  print('No files found')
41  sys.exit()
42 
43 if not os.path.exists('masks/imported'):
44  os.makedirs('masks/imported')
45 
46 for i, fileName in enumerate(fileNames):
47  runFirst = int((fileName.split('_r')[1]).split('.')[0])
48  expNo = int((fileName.split('_e')[1]).split('_')[0])
49  runLast = runFirst
50  k = i + 1
51  if k < len(fileNames):
52  nextName = fileNames[k]
53  runLast = int((nextName.split('_r')[1]).split('.')[0]) - 1
54  expNext = int((nextName.split('_e')[1]).split('_')[0])
55  if expNext > expNo or k == len(fileNames): # last run in exp or last run in total
56  runLast = -1
57  elif runLast < runFirst:
58  b2.B2ERROR("first run:", runFirst, "last run:", runLast)
59  b2.B2ERROR("Last run is less than the first one: exiting!")
60  sys.exit()
61  dbImporter.importChannelMask(fileName, expNo, runFirst, runLast)
62  os.rename(fileName, fileName.replace('masks/', 'masks/imported/'))
63 b2.B2RESULT("Done")