Belle II Software  release-05-01-25
bklm-lookback.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # Purpose:
5 # Analyze DST [S]ROOT files with a sequence of RPC lookback-window settings and write
6 # resulting histograms/scatterplots to ROOT and PDF files.
7 #
8 # Prerequisite (on kekcc):
9 # Before running this script, type
10 # source /cvmfs/belle.cern.ch/tools/b2setup release-03-01-00 <or higher release>
11 # then verify that the corresponding proper global tag is used near the end of this script.
12 # (Global tags are tabulated at https://confluence.desy.de/display/BI/Global+Tag+%28GT%29+page)
13 # The scripts bklmDB.py and EventInspectorLookback.py must be in the same folder as this script.
14 #
15 # Usage:
16 # basf2 bklm-lookback.py -- -m mode -w # # # -e # -r # -i infilenames -t tagname
17 # You need the '--' before these options to tell basf2 that these are options to this script.
18 # Required arguments:
19 # -i infilename to specify the full pathname of the input [S]ROOT files, including wildcards (no default)
20 # -e # to specify the experiment number (no default)
21 # -r # to specify the run number (no default)
22 # -m mode to specify the lookback-window mode, one of coarse-start, coarse-width, fine-start, fine-width
23 # -w # # # to specify the minimum, maximum and step in the lookback-window values
24 # Optional arguments:
25 # -t tagName to specify the name of conditions-database global tag (no default)
26 #
27 # Input:
28 # [S]ROOT file written by basf2 (may include multiple folios for one expt/run). For example,
29 # /ghi/fs01/belle2/bdata/group/detector/BKLM/localRun/e0008/r00550/debug.0008.00550.HLT\*.f\*.sroot
30 # /ghi/fs01/belle2/bdata/Data/Raw/e0008/r00550/sub00/debug.0008.00550.HLT\*.f\*.root
31 #
32 # Output:
33 # ROOT histogram file named bklmHists-e#r#.root, using the experiment number and run number
34 # PDF file named bklmHists-e#r#.pdf, using the experiment number and run number
35 #
36 
37 import basf2
38 from basf2 import *
39 import sys
40 import re
41 import EventInspectorLookback
42 from EventInspectorLookback import *
43 import rawdata
44 from optparse import Option, OptionValueError, OptionParser
45 import glob
46 
47 parser = OptionParser()
48 parser.add_option('-i', '--inputfile',
49  dest='infilename', default='',
50  help='Input [S]ROOT filename [no default]')
51 parser.add_option('-e', '--experiment',
52  dest='eNumber', default='',
53  help='Experiment number [no default]')
54 parser.add_option('-r', '--run',
55  dest='rNumber', default='',
56  help='Run number [no default]')
57 parser.add_option('-m', '--mode',
58  dest='mode', default='',
59  help='Lookback-window mode [no default]')
60 parser.add_option('-w', '--windowValues',
61  dest='window', nargs=3, type="int",
62  help='Lookback-window values (min, max, step) [no default]')
63 parser.add_option('-t', '--tagName',
64  dest='tagName', default='data_reprocessing_prompt',
65  help='Conditions-database global-tag name [data_reprocessing_prompt]')
66 (options, args) = parser.parse_args()
67 
68 tagName = options.tagName
69 
70 modes = {"coarse-start": 0, "coarse-width": 1, "fine-start": 2, "fine-width": 3}
71 if options.mode.isdecimal():
72  mode = int(options.mode)
73 else:
74  mode = modes[options.mode] if options.mode in modes else -1
75 if (mode < 0) or (mode >= 4):
76  print("Illegal lookback-window mode {0}; use one of {1}".format(options.mode, modes))
77  sys.exit()
78 
79 if options.window is None:
80  print("Required lookback-window values (-w min max step) not present")
81  sys.exit()
82 if (options.window[0] < 0) or (options.window[1] < options.window[0]) or (options.window[2] <= 0):
83  if options.window[0] < 0:
84  print("Lookback-window min value {0} cannot be negative".format(options.window[0]))
85  if options.window[1] < options.window[0]:
86  print("Lookback-window max value {1} cannot be less than min value {0}".format(options.window[0], options.window[1]))
87  if options.window[2] <= 0:
88  print("Lookback-window step value {0} must be positive definite".format(options.window[2]))
89  sys.exit()
90 window = options.window
91 
92 inputName = re.sub("HLT.\.f0....", "HLT*.f*", options.infilename)
93 fileList = glob.glob(inputName)
94 if len(fileList) == 0:
95  print("No file(s) match {0}".format(inputName))
96  sys.exit()
97 print(fileList)
98 if not options.eNumber.isdecimal():
99  print("Experiment number ({0}) is not valid".format(options.eNumber))
100  sys.exit()
101 exp = '{0:04d}'.format(int(options.eNumber))
102 if not options.rNumber.isdecimal():
103  print("Run number ({0}) is not valid".format(options.rNumber))
104  sys.exit()
105 run = '{0:05d}'.format(int(options.rNumber))
106 
107 histName = 'bklmHists-e{0}r{1}.root'.format(exp, run)
108 pdfName = 'bklmPlots-e{0}r{1}.pdf'.format(exp, run)
109 
110 print('bklm-windowstart: exp=' + exp + ' run=' + run + ' input=' + inputName + '. Analyze all events using ' + tagName)
111 
112 reset_database()
113 use_database_chain()
114 use_central_database(tagName)
115 
116 main = create_path()
117 if inputName.find(".sroot") >= 0:
118  main.add_module('SeqRootInput', inputFileNames=fileList)
119 else:
120  main.add_module('RootInput', inputFileName=inputName)
121 main.add_module('ProgressBar')
122 
123 eventInspector = EventInspectorLookback(exp, run, histName, pdfName, mode, window)
124 rawdata.add_unpackers(main, components=['KLM'])
125 main.add_module('KLMReconstructor')
126 main.add_module(eventInspector)
127 
128 process(main)
129 print(statistics)
EventInspectorLookback
Definition: EventInspectorLookback.py:1
rawdata.add_unpackers
def add_unpackers(path, components=None, writeKLMDigitRaws=False)
Definition: rawdata.py:62