Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file utils.py
1 #!/usr/bin/env python3
2 import numpy as np
3 import subprocess
4 import argparse
5 import os
6 import shutil
7 
8 parser = argparse.ArgumentParser()
9 subparser = parser.add_subparsers(dest='command')
10 
11 create = subparser.add_parser('create', help='Create file lists.')
12 run = subparser.add_parser('run', help='Run LEDTowerBuilder on the given file list.')
13 evtDisp = subparser.add_parser('evtDisp', help='Create event display (json) given prdf and event number.')
14 
15 create.add_argument('-i', '--run-list', type=str, nargs='+' , help='List of run numbers.')
16 create.add_argument('-p', '--prdf-dir', type=str, default='/direct/sphenix+lustre01/sphnxpro/rawdata/commissioning/emcal/calib', help='Directory containing the prdf files. Default: /direct/sphenix+lustre01/sphnxpro/rawdata/commissioning/emcal/calib')
17 create.add_argument('-o', '--output-dir', type=str, default='files', help='Directory to store the file lists. Default: files')
18 
19 run.add_argument('-i', '--file-list', type=str, help='File list containing prdfs to analyze.', required=True)
20 run.add_argument('-n', '--nevents', type=int, default = -1, help='Number of events to analyze. Default: -1 (analyze all)')
21 run.add_argument('-s', '--skip', type=int, default = 0, help='Number of events to skip. Default: 0 (no skip)')
22 run.add_argument('-o', '--output', type=str, default = 'data/LEDTowerBuilder.root', help='Output root file. Default: data/LEDTowerBuilder.root')
23 run.add_argument('-m', '--max', type=int, default = 10000, help='Maximum number of events to analyze at once. Default: 10000')
24 
25 evtDisp.add_argument('-i', '--prdf', type=str, help='Prdfs to analyze.', required=True)
26 evtDisp.add_argument('-r', '--run', type=str, help='Run number.', required=True)
27 evtDisp.add_argument('-n', '--event', type=str, default = '1', help='Event number to use. Default: 1.')
28 evtDisp.add_argument('-o', '--output', type=str, help='Output json file.')
29 
30 args = parser.parse_args()
31 
33  run_list = args.run_list
34  prdf_dir = os.path.abspath(args.prdf_dir)
35  output_dir = os.path.abspath(args.output_dir)
36 
37  print(f'run list: {run_list}')
38  print(f'prdf dir: {prdf_dir}')
39  print(f'output dir: {output_dir}')
40 
41  for run in run_list:
42  print(f'run: {run}')
43 
44  result = subprocess.run(['fd', run, prdf_dir], stdout=subprocess.PIPE, text=True)
45  print(result.stdout)
46 
47  with open(f'{output_dir}/file-list-{run}.txt',mode='w') as fw:
48  fw.write(result.stdout)
49 
51  file_list = os.path.abspath(args.file_list)
52  nevents = args.nevents
53  output = args.output
54  skip = args.skip
55  max_events_per_run = args.max
56 
57  print(f'file list: {file_list}')
58  print(f'output: {output}')
59 
60  total_events = 0
61  # Find total number of events in the file list
62  with open(file_list) as f:
63  for line in f:
64  line = line.strip()
65  command = f'dpipe -d n -s f -i {line}'
66  dpipe_process = subprocess.run(command.split(), stdout=subprocess.PIPE)
67  events = subprocess.run(['wc','-l'], input=dpipe_process.stdout, stdout=subprocess.PIPE)
68  events = int(events.stdout.decode('utf-8'))
69 
70  total_events += events
71  print(f'prdf: {line}, events: {events}')
72  if(total_events > nevents+skip and nevents >= 0):
73  break
74 
75  nevents = total_events if nevents == -1 else nevents
76  print(f'total events: {total_events}')
77  if(nevents != total_events):
78  print(f'events to analyze: {nevents}')
79  print(f'skip: {skip}')
80 
81  runs = int(np.ceil((nevents+skip) / max_events_per_run))
82  max_events_per_run = min(max_events_per_run, nevents)
83 
84  log = os.path.basename(output).split('.')[0]
85  output_dir = os.path.dirname(output)
86  process_events = max_events_per_run
87 
88  if(runs > 1):
89  print(f'Runs: {runs}')
90  print(f'Max events per run: {max_events_per_run}')
91 
92  merge_files = []
93  for i in range(runs):
94  subprocess.run(['echo', f'Run: {i}'])
95  command = f'./bin/Fun4All_LEDTowerBuilder {process_events} {skip} {file_list} {output_dir}/test-{i}.root &> {output_dir}/log/log-test-{i}.txt &'
96  print(command)
97  skip += max_events_per_run
98  process_events = min(max_events_per_run, nevents-(i+1)*max_events_per_run)
99  merge_files.append(f'{output_dir}/test-{i}.root')
100 
101  print('hadd command: ')
102  merge_files = ' '.join(merge_files)
103  print(f'hadd -n 50 {output} {merge_files}')
104 
105  else:
106  command = f'./bin/Fun4All_LEDTowerBuilder {process_events} {skip} {file_list} {output} &> {output_dir}/log/log-{log}.txt &'
107  print(command)
108 
109 
110  # command = f"root -b -l -q 'macro/Fun4All_LEDTowerBuilder.C({nevents}, \"{file_list}\", \"{output}\")'"
111  # command = f"./bin/Fun4All_LEDTowerBuilder {nevents} {file_list} {output} &> data/log/log-{log}.txt &"
112  # print(command)
113  # subprocess.run(['root','-b','-l','-q',f'macro/Fun4All_LEDTowerBuilder.C({nevents}, \"{file_list}\", \"{output}\")'])
114 
116  prdf_input = os.path.abspath(args.prdf)
117  run = args.run
118  event = args.event
119  output = args.output
120 
121  if(output is None):
122  output = f'event-display/run-{run}-event-{event}.json'
123 
124  print(f'prdf: {prdf_input}')
125  print(f'run: {run}')
126  print(f'event: {event}')
127  print(f'output: {output}')
128  print(f'display only: {display_only}')
129 
130  # isolate the specified event from the prdf
131  command = f'eventcombiner -v -i -e {event} -n 1 -f -p data/temp/test-{run}-{event}.prdf {prdf_input}'
132  print(f'command: {command.split()}')
133  subprocess.run(command.split())
134 
135  # create temp file list containing the prdf
136  with open('files/test.txt','w') as f:
137  f.write(f'data/temp/test-{run}-{event}.prdf\n')
138 
139  # running the LEDTowerBuilder over the prdf
140  command = f'bin/Fun4All_LEDTowerBuilder 1 files/test.txt data/temp test'
141  print(f'command: {command.split()}')
142  subprocess.run(command.split())
143 
144  # running the event-display which generates the json output
145  command = f'bin/event-display {run} {event} data/temp/test-CEMC.root {output} data/temp/test-HCALIN.root data/temp/test-HCALOUT.root'
146  print(f'command: {command.split()}')
147  subprocess.run(command.split())
148 
149 if __name__ == '__main__':
150  if(args.command == 'create'):
152  if(args.command == 'run'):
153  run_analysis()
154  if(args.command == 'evtDisp'):
155  event_display()