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