Belle II Software  release-08-01-10
bklm-eventdisplay.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Purpose:
12 # Analyze a DST file and write a PDF of "interesting" event displays that includes
13 # BKLMHit2ds, ExtHits, and MuidHits.
14 # This script cannot analyze MDST files because they don't contain RawKLMs, BKLMHit2ds, nor MuidHits.
15 #
16 # Prerequisites (on kekcc):
17 # Before running this script, type
18 # source /cvmfs/belle.cern.ch/tools/b2setup release-02-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 external python script bklmDB.py must be in the same folder as this script.
22 #
23 # Usage:
24 # basf2 bklm-eventdisplay.py -- -e # -r # -i infilename -n # -d # -m # -u # -t tagname
25 # You need the '--' before these options to tell basf2 that these are options to this script.
26 # Required arguments:
27 # either -i infilename or -e # -r # (can supply all three)
28 # -i infilename to specify the full pathname of the input ROOT DST file (no default)
29 # -e # to specify the experiment number, e.g., -e 1 (no default)
30 # -r # to specify the run number, e.g., -r 4794 (no default)
31 # Optional arguments:
32 # -n # to specify the maximum number of events to analyze (no default -> all events)
33 # -d # to specify the maximum number of displayed events to write to the PDF file (default = 100)
34 # -m # to specify the minimum number of RPC hits in one sector (default = 4)
35 # -u # to specify the minimum number of Muid hits in the event (default = 1)
36 # -t tagname to specify the name of conditions database global tag (no default)
37 #
38 # Input:
39 # ROOT DST file written by basf2 (may include multiple folios for one expt/run). For example,
40 # /ghi/fs01/belle2/bdata/Data/Raw/e0003/r04794/sub00/physics.0003.r04794.HLT2.f*.root
41 # /ghi/fs01/belle2/bdata/Data/Raw/e0004/r06380/sub00/cosmic.0004.r06380.HLT2.f00000.root
42 #
43 # Output:
44 # PDF file named bklmEvents-e#r#.pdf, using the experiment number and run number
45 #
46 
47 import basf2
48 import EventDisplayer
49 import sys
50 from tracking import add_tracking_reconstruction
51 import rawdata
52 from optparse import OptionParser
53 import glob
54 
55 parser = OptionParser()
56 parser.add_option('-i', '--inputfile',
57  dest='infilename', default='',
58  help='Input ROOT filename [no default]')
59 parser.add_option('-e', '--experiment',
60  dest='eNumber', default='',
61  help='Experiment number [no default]')
62 parser.add_option('-r', '--run',
63  dest='rNumber', default='',
64  help='Run number [no default]')
65 parser.add_option('-n', '--nEvents',
66  dest='nEvents', default='',
67  help='Max # of analyzed events [no default]')
68 parser.add_option('-d', '--displays',
69  dest='displays', default='100',
70  help='Max # of displayed events [default=100]')
71 parser.add_option('-m', '--minRPCHits',
72  dest='minRPCHits', default='4',
73  help='Minimum # of RPC hits in one sector [default=4]')
74 parser.add_option('-u', '--muids',
75  dest='minMuidHits', default='1',
76  help='Minimum # of Muid hits in the event [default=1]')
77 parser.add_option('-t', '--tag',
78  dest='tagName', default='data_reprocessing_prompt',
79  help='Conditions-database global-tag name [data_reprocessing_prompt]')
80 (options, args) = parser.parse_args()
81 
82 maxCount = -1
83 if options.nEvents != '':
84  maxCount = int(options.nEvents)
85  if maxCount <= 0:
86  print("Maximum number of events to analyze is", maxCount, " - nothing to do.")
87  sys.exit()
88 
89 maxDisplays = int(options.displays)
90 
91 minRPCHits = int(options.minRPCHits)
92 
93 minMuidHits = int(options.minMuidHits)
94 
95 tagName = options.tagName
96 
97 inputName = ''
98 exp = ''
99 run = ''
100 if options.infilename != '':
101  inputName = options.infilename
102  fileList = glob.glob(inputName)
103  if len(fileList) == 0:
104  print(f'No file(s) match {inputName}')
105  sys.exit()
106 if options.eNumber != '':
107  if not options.eNumber.isdecimal():
108  print(f'Experiment number ({options.eNumber}) is not valid')
109  sys.exit()
110  exp = f'{int(options.eNumber):04d}'
111 else:
112  eStart = inputName.find('/e') + 2
113  if eStart < 0:
114  print('Input filename does not contain the required experiment number')
115  sys.exit()
116  eEnd = inputName.find('/', eStart)
117  exp = inputName[eStart:eEnd]
118  if not exp.isdecimal():
119  print(f'Input filename experiment number({exp}) is not valid')
120  sys.exit()
121 if options.rNumber != '':
122  if not options.rNumber.isdecimal():
123  print(f'Run number ({options.rNumber}) is not valid')
124  sys.exit()
125  run = f'{int(options.rNumber):05d}'
126 else:
127  rStart = inputName.find('/r') + 2
128  if rStart < 0:
129  print('Input filename does not contain the required run number')
130  sys.exit()
131  rEnd = inputName.find('/', rStart)
132  run = inputName[rStart:rEnd]
133  if not run.isdecimal():
134  print(f'Input filename run number({run}) is not valid')
135  sys.exit()
136 if len(inputName) == 0:
137  fileList = glob.glob(f'/ghi/fs01/belle2/bdata/Data/Raw/e{exp}/r{run}/sub00/*.{exp}.{run}.HLT2.f00000.root')
138  if len(fileList) == 0:
139  print(f'No file(s) found for experiment <{options.eNumber}> run <{options.rNumber}>')
140  sys.exit()
141  inputName = fileList[0].replace('f00000', 'f*')
142 
143 eventPdfName = f'bklmEvents-e{exp}r{run}.pdf'
144 
145 if maxCount >= 0:
146  print('bklm-display: exp=' + exp + ' run=' + run + ' input=' + inputName + '. Analyze', maxCount, 'events using ' + tagName)
147  print(' Write at most', maxDisplays, 'event displays, requiring # RPC hits per sector >=', minRPCHits,
148  ' # Muids in event >=', minMuidHits)
149 else:
150  print('bklm-display: exp=' + exp + ' run=' + run + ' input=' + inputName + '. Analyze all events using ' + tagName)
151  print(' Write at most', maxDisplays, 'event displays, requiring # RPC hits per sector >=', minRPCHits,
152  ' # Muids in event >=', minMuidHits)
153 
154 basf2.conditions.prepend_globaltag(tagName)
155 
156 main = basf2.create_path()
157 main.add_module('RootInput', inputFileName=inputName)
158 main.add_module('ProgressBar')
159 
160 eventDisplayer = EventDisplayer(exp, run, eventPdfName, maxDisplays, minRPCHits, minMuidHits)
162 main.add_module('KLMReconstructor')
163 add_tracking_reconstruction(main)
164 ext = main.add_module('Ext')
165 ext.param('pdgCodes', [13])
166 muid = main.add_module('Muid')
167 # muid.param('MaxDistSigma', 10.0)
168 main.add_module(eventDisplayer)
169 
170 basf2.process(main, max_event=maxCount)
171 print(basf2.statistics)
def add_unpackers(path, components=None, writeKLMDigitRaws=False, addTOPRelations=False)
Definition: rawdata.py:67