source: trunk/Mars/hawc/stripandreco.py@ 19969

Last change on this file since 19969 was 19953, checked in by giangdo, 5 years ago
stripandreco.py: python script to extract the HAWC's Eye triggered data in HAWC and do the reconstruction using AERIE
File size: 5.1 KB
Line 
1import os
2import argparse
3import subprocess
4import time
5
6if (os.environ.get('CONFIG_HAWC') == None):
7 print "Did not find HAWC enviroment (try hawc-sw)!"
8 exit()
9
10parser = argparse.ArgumentParser()
11parser.add_argument("-p", "--path", dest="path", help="Search path (recursive!) for trigger files (xcd)",type=str,required=True)
12parser.add_argument("-s", "--strip", action="store_true", help="Only strip all files in path", required=False)
13parser.add_argument("-r", "--reco", action="store_true", help="Only reconstruct all files in path", required=False)
14parser.add_argument("-c", "--root", action="store_true", help="Only convert all files to root in path", required=False)
15parser.add_argument("-j", "--proc", dest="proc", help="Number of processes (max. cores + 1)",type=int , required=False)
16parser.add_argument("--filter", dest="filter", help="Only use files containing string FILTER", type=str, required=False)
17parser.add_argument("--channel", dest="channel", help="Specify a channel for the trigger flag (Default: 31,E16C)",type=int , required=False)
18
19args = parser.parse_args()
20
21channel_id = 31
22max_proc = 2
23
24if args.channel:
25 channel_id = args.channel
26
27if args.proc:
28 max_proc = args.proc
29
30print """
31************************************
32* *
33* Welcome to HAWCs Eye *
34* *
35* Filter and reconstruction tool *
36* *
37* Version 1.0 01.08.2017 *
38* *
39************************************
40
41Search path: {}
42Channel ID : {}
43""".format(args.path,channel_id)
44
45process_list = []
46if not args.reco and not args.root:
47 strip_flist = []
48 #Search for .xcd files in args.path without "strp" or "reco" in the filename
49 for root,dirs,files in os.walk(args.path):
50 if args.filter is None:
51 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and not ('strp' in xcdf) and not ('reco' in xcdf))]:
52 strip_flist.append(root+"/"+xcdfile)
53 else:
54 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and not ('strp' in xcdf) and not ('reco' in xcdf) and (args.filter in xcdf))]:
55 strip_flist.append(root+"/"+xcdfile)
56
57
58 #Filter files for a hit in channel_id
59 for xcdfile in strip_flist:
60 process_string = 'xcdf select "trig.channelID_4Edge == {}" -o {}_strp.xcd {}'.format(channel_id,xcdfile[:-4],xcdfile)
61 print "Starting: " + process_string
62 process_list.append(subprocess.Popen(process_string,shell=True))
63 while([status.poll() for status in process_list].count(None) >= max_proc):
64 time.sleep(0.2)
65
66#Wait for all strip processes to finish:
67while([status.poll() for status in process_list].count(None) > 0):
68 time.sleep(0.2)
69#Reset process list:
70process_list = []
71
72if not args.strip and not args.root:
73 reco_flist = []
74 #Search for .xcd files in args.path with "strp" in the filename
75 for root,dirs,files in os.walk(args.path):
76 if args.filter is None:
77 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and ('strp' in xcdf[-8:-4]))]:
78 reco_flist.append(root+"/"+xcdfile)
79 else:
80 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and ('strp' in xcdf[-8:-4]) and (args.filter in xcdf))]:
81 reco_flist.append(root+"/"+xcdfile)
82
83 #Reco strp files:
84 for xcdfile in reco_flist:
85 if (args.filter == None):
86 process_string = 'offline-reconstructor --input {} -o {}_reco.xcd'.format(xcdfile,xcdfile[:-9])
87 else:
88 process_string = 'offline-reconstructor --input {} -o {}_reco.xcd'.format(xcdfile,xcdfile[:-4])
89 print "Starting: " + process_string
90 process_list.append(subprocess.Popen(process_string,shell=True))
91 while([status.poll() for status in process_list].count(None) >= max_proc):
92 time.sleep(0.2)
93
94#Wait for all reconstruction processes to finish
95while([status.poll() for status in process_list].count(None) > 0):
96 time.sleep(0.2)
97
98#Reset process list:
99process_list = []
100
101if not args.strip and not args.reco:
102 root_flist = []
103 #Search for .xcd files in args.path with "reco" in the filename
104 for root,dirs,files in os.walk(args.path):
105 if args.filter is None:
106 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and ('reco' in xcdf))]:
107 root_flist.append(root+"/"+xcdfile)
108 else:
109 for xcdfile in [xcdf for xcdf in files if (('.xcd' in xcdf[-4:]) and ('reco' in xcdf) and (args.filter in xcdf))]:
110 root_flist.append(root+"/"+xcdfile)
111
112 #xcd to root files:
113 for xcdfile in root_flist:
114 process_string = 'xcdf-root --input {} -o {}.root'.format(xcdfile,xcdfile[:-4])
115 print "Starting: " + process_string
116 process_list.append(subprocess.Popen(process_string,shell=True))
117 while([status.poll() for status in process_list].count(None) >= max_proc):
118 time.sleep(0.2)
119
120#Wait for all files beeing converted
121while([status.poll() for status in process_list].count(None) > 0):
122 time.sleep(0.2)
Note: See TracBrowser for help on using the repository browser.