Belle II Software  release-06-01-15
bklm-pocketdaq.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # Purpose:
13 # Analyze PocketDAQ data created by the test stand at Indiana University
14 # and write a PDF file of the resulting histograms/scatterplots.
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 # and translate the test-stand raw data file to a ROOT file using Richard Peschke's app
20 #
21 # Usage:
22 # basf2 bklm-pocketdaq.py -- -i infilename -e # -r # -n # -f #
23 # You need the '--' before these options to tell basf2 that these are options to this script.
24 # Required argument:
25 # -i infilename to specify the full pathname of the input ROOT file
26 # Optional arguments:
27 # -e # to specify the experiment number, e.g., -e 1 (no default)
28 # -r # to specify the run number, e.g., -r 0109 (no default)
29 # -n # to specify the number of events in the run to be processed (no default -> all events)
30 # -f # to filter only the even-numbered events (-f 0) or the odd-numbered events (-f 1) (no default -> all events)
31 #
32 # Input:
33 # ROOT file written by Richard Peschke's translator app whose input was a test-stand raw data file. For example,
34 # /home/belle2/dbiswas/Documents/PocketDAQ/klm_offline_entpacker/build/klm_unpacker/brandon_data/dblpls_30k_fine_20190109.root
35 #
36 # Output:
37 # ROOT histogram file named bklmHists-e#r#.root, using the experiment number and run number
38 # PDF file named bklmPlots-e#r#.pdf, using the experiment number and run number
39 #
40 
41 import EventInspectorPocketDAQ
42 from ROOT import TFile
43 import sys
44 from optparse import OptionParser
45 import glob
46 
47 parser = OptionParser()
48 parser.add_option('-i', '--inputfile',
49  dest='infilename', default='',
50  help='Input 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('-n', '--nEvents',
58  dest='nEvents', default='',
59  help='# of events to process [no default]')
60 parser.add_option('-f', '--filter',
61  dest='eventFilter', default='',
62  help='Event filter (0=evens, 1=odds) [no default]')
63 (options, args) = parser.parse_args()
64 
65 maxCount = -1
66 if options.nEvents != '':
67  maxCount = int(options.nEvents)
68  if maxCount <= 0:
69  print("Maximum number of events to analyze is", maxCount, " - nothing to do.")
70  sys.exit()
71 
72 eventFilter = -1
73 if (options.eventFilter == '0') or (options.eventFilter == '1'):
74  eventFilter = int(options.eventFilter)
75 
76 inputName = ''
77 exp = ''
78 run = ''
79 if options.infilename != '':
80  inputName = options.infilename
81  fileList = glob.glob(inputName)
82  if len(fileList) == 0:
83  print("No file(s) match {0}".format(inputName))
84  sys.exit()
85  inputName = fileList[0].replace("f00000", "f*")
86 else:
87  print("Missing input filename (required parameter) for experiment <{0}> run <{1}>".format(options.eNumber, options.rNumber))
88  sys.exit()
89 if not options.eNumber.isdecimal():
90  print("Experiment number ({0}) is not valid".format(options.eNumber))
91  sys.exit()
92 exp = '{0:04d}'.format(int(options.eNumber))
93 if not options.rNumber.isdecimal():
94  print("Run number ({0}) is not valid".format(options.rNumber))
95  sys.exit()
96 run = '{0:05d}'.format(int(options.rNumber))
97 
98 infile = TFile(fileList[0])
99 histName = 'bklmHists-e{0}r{1}.root'.format(exp, run)
100 pdfName = 'bklmPlots-e{0}r{1}.pdf'.format(exp, run)
101 
102 if eventFilter == 0:
103  print('bklm-pocketdaq: exp=' + exp + ' run=' + run + ' input=', options.infilename + ' - processing even-numbered events')
104 elif eventFilter == 1:
105  print('bklm-pocketdaq: exp=' + exp + ' run=' + run + ' input=', options.infilename + ' - processing odd-numbered events')
106 else:
107  print('bklm-pocketdaq: exp=' + exp + ' run=' + run + ' input=', options.infilename)
108 
109 inspector = EventInspectorPocketDAQ(exp, run, histName, pdfName)
110 inspector.initialize()
111 inspector.beginRun()
112 count = 0
113 eventNumber = -1
114 eventHits = []
115 tt_ctime = 0
116 raw_time = 0
117 for row in infile.Get('KLM_raw_hits'):
118  # (optional) stop processing after maxCount
119  if (maxCount > 0) and (row.eventNr >= maxCount):
120  break
121  # (optional) process only the filtered events
122  if (eventFilter >= 0) and (row.eventNr % 2) != eventFilter:
123  continue
124  items = (row.lane, row.channel, row.axis, row.ctime, row.tdc, row.charge)
125  newEventNumber = row.eventNr
126  if newEventNumber == eventNumber:
127  eventHits.append(items)
128  else:
129  if eventNumber >= 0:
130  inspector.event(eventHits, tt_ctime, raw_time)
131  count = count + 1
132  eventNumber = newEventNumber
133  if row.broken != 0:
134  print('*** Event {0} is broken!'.format(eventNumber))
135  eventHits.clear()
136  eventHits.append(items)
137  tt_ctime = row.tt_ctime << 3
138  raw_time = (row.raw_time >> 16) << 3
139 if eventNumber >= 0:
140  inspector.event(eventHits, tt_ctime, raw_time)
141 inspector.endRun()
142 inspector.terminate()
143 print('# of analyzed events = ', count)