Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-25 08:29:08

0001 #!/usr/bin/env python3
0002 
0003 import sys
0004 from collections import defaultdict
0005 
0006 from sphenixdbutils import cnxn_string_map, dbQuery # type: ignore
0007 
0008 def main():
0009     if len(sys.argv) < 4:
0010         script_name = sys.argv[0]
0011         print(f"usage: {script_name} <runnumber> <daqhost> <segswitch>")
0012         sys.exit(0)
0013 
0014     runnumber_str = sys.argv[1]
0015     try:
0016         runnumber = int(runnumber_str)
0017     except ValueError:
0018         print(f"Error: runnumber '{runnumber_str}' must be an integer.")
0019         sys.exit(1)
0020 
0021     daqhost = sys.argv[2]
0022 
0023     # Using a defaultdict to easily append to lists of filenames per host
0024     file_list_by_host = defaultdict(list)
0025 
0026     ### Important change, 07/15/2025: Usually only care about segment 0!
0027     segswitch=sys.argv[3]
0028     sql_query = f"""
0029     SELECT filename, daqhost
0030     FROM datasets
0031     WHERE runnumber = {runnumber}"""
0032     if segswitch == "seg0fromdb":
0033         sql_query += "\n\t AND (segment = 0)"
0034     elif segswitch == "allsegsfromdb":
0035         pass
0036     else:
0037         print("segswitch = {seg0fromdb|allsegsfromdb} must be explicitly provided")
0038         exit(1)
0039     sql_query += f"""
0040     AND (daqhost = '{daqhost}' OR daqhost = 'gl1daq')
0041     AND status=1
0042     ORDER BY filename
0043     """
0044     print(sql_query)
0045     rows = dbQuery( cnxn_string_map['rawr'], sql_query).fetchall()
0046     for row in rows:
0047         filename, host = row
0048         file_list_by_host[host].append(filename)
0049 
0050     if not file_list_by_host:
0051         print("No files found for the given criteria.")
0052 
0053     for host, filenames in file_list_by_host.items():
0054         list_filename = f"{host}.list"
0055         try:
0056             with open(list_filename, 'w') as f_out:
0057                 for fname in filenames:
0058                     f_out.write(f"{fname}\n")
0059         except IOError as e:
0060             print(f"Error writing to file {list_filename}: {e}")
0061 
0062 if __name__ == "__main__":
0063     main()