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