Belle II Software  release-08-01-10
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://confluence.desy.de/display/BI/Global+Tag+%28GT%29+page)
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 
44 import basf2
45 import sys
46 import re
47 import EventInspectorLookback
48 import rawdata
49 from optparse import OptionParser
50 import glob
51 
52 parser = OptionParser()
53 parser.add_option('-i', '--inputfile',
54  dest='infilename', default='',
55  help='Input [S]ROOT filename [no default]')
56 parser.add_option('-e', '--experiment',
57  dest='eNumber', default='',
58  help='Experiment number [no default]')
59 parser.add_option('-r', '--run',
60  dest='rNumber', default='',
61  help='Run number [no default]')
62 parser.add_option('-m', '--mode',
63  dest='mode', default='',
64  help='Lookback-window mode [no default]')
65 parser.add_option('-w', '--windowValues',
66  dest='window', nargs=3, type="int",
67  help='Lookback-window values (min, max, step) [no default]')
68 parser.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 
73 tagName = options.tagName
74 
75 modes = {"coarse-start": 0, "coarse-width": 1, "fine-start": 2, "fine-width": 3}
76 if options.mode.isdecimal():
77  mode = int(options.mode)
78 else:
79  mode = modes[options.mode] if options.mode in modes else -1
80 if (mode < 0) or (mode >= 4):
81  print(f"Illegal lookback-window mode {options.mode}; use one of {modes}")
82  sys.exit()
83 
84 if options.window is None:
85  print("Required lookback-window values (-w min max step) not present")
86  sys.exit()
87 if (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()
95 window = options.window
96 
97 inputName = re.sub(r"HLT.\.f0....", "HLT*.f*", options.infilename)
98 fileList = glob.glob(inputName)
99 if len(fileList) == 0:
100  print(f"No file(s) match {inputName}")
101  sys.exit()
102 print(fileList)
103 if not options.eNumber.isdecimal():
104  print(f"Experiment number ({options.eNumber}) is not valid")
105  sys.exit()
106 exp = f'{int(options.eNumber):04d}'
107 if not options.rNumber.isdecimal():
108  print(f"Run number ({options.rNumber}) is not valid")
109  sys.exit()
110 run = f'{int(options.rNumber):05d}'
111 
112 histName = f'bklmHists-e{exp}r{run}.root'
113 pdfName = f'bklmPlots-e{exp}r{run}.pdf'
114 
115 print('bklm-windowstart: exp=' + exp + ' run=' + run + ' input=' + inputName + '. Analyze all events using ' + tagName)
116 
117 basf2.conditions.prepend_globaltag(tagName)
118 
119 main = basf2.create_path()
120 if inputName.find(".sroot") >= 0:
121  main.add_module('SeqRootInput', inputFileNames=fileList)
122 else:
123  main.add_module('RootInput', inputFileName=inputName)
124 main.add_module('ProgressBar')
125 
126 eventInspector = EventInspectorLookback(exp, run, histName, pdfName, mode, window)
127 rawdata.add_unpackers(main, components=['KLM'])
128 main.add_module('KLMReconstructor')
129 main.add_module(eventInspector)
130 
131 basf2.process(main)
132 print(basf2.statistics)
def add_unpackers(path, components=None, writeKLMDigitRaws=False, addTOPRelations=False)
Definition: rawdata.py:67