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