Belle II Software  release-06-01-15
importCommonT0.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # Example of importing common T0 constants determined with cdst_calibrateCommonT0.py
14 # ---------------------------------------------------------------------------------------
15 
16 import basf2 as b2
17 from ROOT.Belle2 import TOPDatabaseImporter
18 from ROOT import TFile
19 import sys
20 import glob
21 
22 
23 # Create path
24 main = b2.create_path()
25 
26 # Event info setter - execute single event
27 eventinfosetter = b2.register_module('EventInfoSetter')
28 eventinfosetter.param('evtNumList', [1])
29 main.add_module(eventinfosetter)
30 
31 # Gearbox - access to xml files
32 gearbox = b2.register_module('Gearbox')
33 main.add_module(gearbox)
34 
35 # Initialize TOP geometry parameters from gearbox
36 main.add_module('TOPGeometryParInitializer', useDB=False)
37 
38 # process single event
39 b2.process(main)
40 
41 # define a local database (will be created automatically, if doesn't exist)
42 b2.use_local_database("localDB/localDB.txt", "localDB", False)
43 
44 # and then run the importer
45 dbImporter = TOPDatabaseImporter()
46 
47 # import constants
48 
49 # root file names are supposed to be 'commonT0_r*.root', where * is a run number
50 pathToFiles = '.' # set the correct path to root files!
51 allFileNames = sorted(glob.glob(pathToFiles + '/commonT0_r*.root'))
52 fileNames = []
53 for fileName in allFileNames:
54  file = TFile.Open(fileName)
55  h = file.Get('commonT0')
56  if not h:
57  b2.B2ERROR('no histogram with name commonT0')
58  file.Close()
59  continue
60  tree = file.Get('tree')
61  if not tree:
62  b2.B2ERROR('no TTree with name tree')
63  file.Close()
64  continue
65  if tree.GetEntries() > 10: # require some minimal number of tracks in a run
66  fileNames.append(fileName)
67  file.Close()
68 
69 numFiles = len(fileNames)
70 if numFiles == 0:
71  print('No files found')
72  sys.exit()
73 
74 expNo = 3 # set the correct experiment number
75 lastRunNo = 5613 # set the correct last run number
76 bunchTimeSep = 47.163878 / 24
77 for i, fileName in enumerate(fileNames):
78  runFirst = int((fileName.split('commonT0_r')[1]).split('.')[0])
79  runLast = lastRunNo
80  k = i + 1
81  if k < len(fileNames):
82  nextName = fileNames[k]
83  runLast = int((nextName.split('commonT0_r')[1]).split('.')[0]) - 1
84  if runLast < runFirst:
85  b2.B2ERROR("first run:", runFirst, "last run:", runLast)
86  b2.B2ERROR("Last run is less than the first one: exiting!")
87  sys.exit()
88 
89  file = TFile.Open(fileName)
90  h = file.Get('commonT0')
91  t0 = h.GetBinContent(1)
92  t0 -= round(t0 / bunchTimeSep, 0) * bunchTimeSep
93  err = h.GetBinError(1)
94  dbImporter.importCommonT0(t0, err, expNo, runFirst, runLast)
95  file.Close()
96 
97 b2.B2RESULT("Done")