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