Belle II Software  release-06-01-15
importModuleT0.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # Importing module T0 constants saved in histograms to local DB.
14 # Input: root files from cdst_calibrateModuleT0.py or cdst_chi2ModuleT0calibration.py
15 #
16 # usage: basf2 importModuleT0.py pathToRootFiles localDB
17 #
18 # ---------------------------------------------------------------------------------------
19 
20 import basf2 as b2
21 from ROOT.Belle2 import TOPDatabaseImporter
22 from ROOT import TFile
23 import sys
24 import glob
25 
26 # Argument parsing
27 argvs = sys.argv
28 if len(argvs) < 3:
29  print("usage: basf2", argvs[0], "pathToRootFiles localDB")
30  sys.exit()
31 pathToFiles = argvs[1]
32 localDB = argvs[2]
33 
34 # Determine experiment number from the path name
35 try:
36  expNo = int((pathToFiles.split('/e')[-1]).split('/')[0])
37 except BaseException:
38  b2.B2ERROR("Cannot determine experiment number from path name: " + pathToFiles)
39  sys.exit()
40 
41 # Check root files and prepare IOV's
42 allFileNames = sorted(glob.glob(pathToFiles + '/moduleT0*.root'))
43 fileNames = []
44 runFirst = [0, ]
45 runLast = []
46 for fileName in allFileNames:
47  file = TFile.Open(fileName)
48  h = file.Get('moduleT0')
49  if not h:
50  b2.B2WARNING(fileName + ': no histogram with name moduleT0, file skipped.')
51  file.Close()
52  continue
53  file.Close()
54  try:
55  runNum = int(fileName.split('_to_r')[-1].split('.root')[0])
56  runFirst.append(runNum + 1)
57  runLast.append(runNum)
58  fileNames.append(fileName)
59  except BaseException:
60  b2.B2WARNING(fileName +
61  ': cannot determine last run number from file name, file skipped.')
62  continue
63 
64 if len(fileNames) == 0:
65  b2.B2ERROR('No selected files found in ' + pathToFiles)
66  sys.exit()
67 
68 runFirst.pop()
69 runLast[-1] = -1
70 
71 # Create path
72 main = b2.create_path()
73 
74 # Event info setter - execute single event
75 eventinfosetter = b2.register_module('EventInfoSetter')
76 eventinfosetter.param('evtNumList', [1])
77 main.add_module(eventinfosetter)
78 
79 # Gearbox - access to xml files
80 gearbox = b2.register_module('Gearbox')
81 main.add_module(gearbox)
82 
83 # Initialize TOP geometry parameters from gearbox
84 main.add_module('TOPGeometryParInitializer', useDB=False)
85 
86 # process single event
87 b2.process(main)
88 
89 # define a local database (will be created automatically, if doesn't exist)
90 b2.use_local_database(localDB, readonly=False)
91 
92 # and then run the importer
93 dbImporter = TOPDatabaseImporter()
94 
95 # import constants
96 print()
97 print('---- Importing constants for experiment ' + str(expNo) + ' ---->')
98 print()
99 for i, fileName in enumerate(fileNames):
100  dbImporter.importModuleT0(fileName, expNo, runFirst[i], runLast[i])
101  print()
102 
103 b2.B2RESULT("Done. Constants imported to " + localDB)