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