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