Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:14:30

0001 #!/usr/bin/env python3
0002 
0003 import pyodbc
0004 
0005 def get_nEvents(cursor, runlist):
0006     with open(runlist, 'r') as f:
0007         runstrings = f.readlines()
0008     runnumbers = [int(run) for run in runstrings]
0009     nEvents = []
0010     for runnumber in runnumbers:
0011         query = f"""
0012         SELECT SUM(events)
0013         FROM datasets
0014         WHERE runnumber = {runnumber} AND filename LIKE 'DST_CALO_run2pp_ana437_2024p007-%';
0015         """
0016         cursor.execute(query)
0017         events = [row.sum for row in cursor.fetchall()]
0018         if len(events) != 1:
0019             print(f'Found {len(events)} rows in cursor.fetchall()')
0020         else:
0021             nEvents.append(events[0])
0022     print(f'Found {len(nEvents)} runs in file {runlist}')
0023     print(f'Found {sum(nEvents)} total events in file {runlist}')
0024     return sum(nEvents)
0025 
0026 
0027 def get_all_run_numbers(cursor):
0028     query = """
0029     SELECT runnumber, SUM(events)
0030     FROM datasets
0031     WHERE filename like 'DST_CALO_run2pp_ana437_2024p007-%'
0032     GROUP BY runnumber
0033     HAVING SUM(events) >= 500000;
0034     """
0035     cursor.execute(query)
0036     # run_numbers = [row.runnumber for row in cursor.fetchall()]
0037     run_numbers = []
0038     nEvents = []
0039     for row in cursor.fetchall():
0040         run_numbers.append(row.runnumber)
0041         nEvents.append(row.sum)
0042     print(f'Total events in all p007 runs with >= 500k events: {sum(nEvents)}')
0043     return run_numbers
0044 
0045 
0046 def get_all_run_numbers_no_event_count(cursor):
0047     query = """
0048     SELECT runnumber, SUM(events)
0049     FROM datasets
0050     WHERE filename like 'DST_CALO_run2pp_ana437_2024p007-%'
0051     GROUP BY runnumber;
0052     """
0053     cursor.execute(query)
0054     # all_runs_no_event_count = [row.runnumber for row in cursor.fetchall()]
0055     # all_runs_nEvents = [row.sum for row in cursor.fetchall()]
0056     all_runs_no_event_count = []
0057     all_runs_nEvents = []
0058     for row in cursor.fetchall():
0059         all_runs_no_event_count.append(row.runnumber)
0060         all_runs_nEvents.append(row.sum)
0061     print(f'Total events in all p007 runs: {sum(all_runs_nEvents)}')
0062     return all_runs_no_event_count
0063 
0064 
0065 def get_good_run_numbers(production_cursor):
0066     query = """
0067     SELECT runnumber
0068     FROM goodruns
0069     """
0070     production_cursor.execute(query)
0071     good_run_numbers = {row.runnumber for row in production_cursor.fetchall()}
0072     return list(good_run_numbers)
0073 
0074 def get_emcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor):
0075     query = """
0076     SELECT runnumber
0077     FROM goodruns
0078     WHERE (emcal_auto).runclass = 'GOLDEN'
0079     """
0080     production_cursor.execute(query)
0081     emcal_auto_golden_run_numbers = {row.runnumber for row in production_cursor.fetchall()}
0082     return list(
0083         emcal_auto_golden_run_numbers.intersection(
0084             set(file_catalog_run_numbers)
0085         )
0086     )
0087 
0088 def get_ihcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor):
0089     query = """
0090     SELECT runnumber
0091     FROM goodruns
0092     WHERE (ihcal_auto).runclass = 'GOLDEN'
0093     """
0094     production_cursor.execute(query)
0095     ihcal_auto_golden_run_numbers = {row.runnumber for row in production_cursor.fetchall()}
0096     return list(
0097         ihcal_auto_golden_run_numbers.intersection(
0098             set(file_catalog_run_numbers)
0099         )
0100     )
0101 
0102 def get_ohcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor):
0103     query = """
0104     SELECT runnumber
0105     FROM goodruns
0106     WHERE (ohcal_auto).runclass = 'GOLDEN'
0107     """
0108     production_cursor.execute(query)
0109     ohcal_auto_golden_run_numbers = {row.runnumber for row in production_cursor.fetchall()}
0110     return list(
0111         ohcal_auto_golden_run_numbers.intersection(
0112             set(file_catalog_run_numbers)
0113         )
0114     )
0115 
0116 def main():
0117     # Connect to the FileCatalog database
0118     file_catalog_conn = pyodbc.connect("DSN=FileCatalog;UID=phnxrc;READONLY=True")
0119     file_catalog_cursor = file_catalog_conn.cursor()
0120 
0121     all_runs_no_event_count = get_all_run_numbers_no_event_count(file_catalog_cursor)
0122     with open('runNumbers_all_p007.txt', 'w') as f:
0123         for run_number in all_runs_no_event_count:
0124             f.write(f"{run_number}\n")
0125     print(f"Total number of p007 runs (regardless of event count): {len(all_runs_no_event_count)}")
0126     # nEvents_all_no_event_count = get_nEvents(file_catalog_cursor, 'runNumbers_all_p007.txt')
0127     # print(f'Total number of p007 events: {nEvents_all_no_event_count}')
0128 
0129 
0130     # Get unique run numbers with at least 500k total events
0131     file_catalog_run_numbers = get_all_run_numbers(file_catalog_cursor)
0132     file_catalog_run_numbers.sort()
0133     with open('runNumbers_with_AtLeast_500kEvents.txt', 'w') as f:
0134         for run_number in file_catalog_run_numbers:
0135             f.write(f"{run_number}\n")
0136     print(f"Number of all runs saved to runNumbers_with_AtLeast_500kEvents.txt that have at least 500k events: {len(file_catalog_run_numbers)}")
0137 
0138     # Connect to the Production database
0139     production_conn = pyodbc.connect("DSN=Production_write")
0140     production_cursor = production_conn.cursor()
0141 
0142     # Filter good run numbers
0143     good_run_numbers = get_good_run_numbers(production_cursor)
0144     runs_not_in_goodruns = set(file_catalog_run_numbers) - set(good_run_numbers)
0145     print(f"Number of runs not in the goodruns table: {len(runs_not_in_goodruns)}")
0146 
0147     # Filter 'GOLDEN' run numbers
0148     emcal_auto_golden_run_numbers = get_emcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor)
0149     emcal_auto_golden_run_numbers.sort()
0150     with open('GoldenEmcalRunList.txt', 'w') as f:
0151         for run_number in emcal_auto_golden_run_numbers:
0152             f.write(f"{run_number}\n")
0153     print(f"Number of EMCal GOLDEN runs saved to GoldenEmcalRunList.txt: {len(emcal_auto_golden_run_numbers)}")
0154     nEventsEMCal = get_nEvents(file_catalog_cursor, 'GoldenEmcalRunList.txt')
0155 
0156     ihcal_auto_golden_run_numbers = get_ihcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor)
0157     ihcal_auto_golden_run_numbers.sort()
0158     with open('GoldenIHCalRunList.txt', 'w') as f:
0159         for run_number in ihcal_auto_golden_run_numbers:
0160             f.write(f"{run_number}\n")
0161     print(f"Number of IHCal GOLDEN runs saved to GoldenIHCalRunList.txt: {len(ihcal_auto_golden_run_numbers)}")
0162 
0163     ohcal_auto_golden_run_numbers = get_ohcal_auto_golden_run_numbers(file_catalog_run_numbers, production_cursor)
0164     ohcal_auto_golden_run_numbers.sort()
0165     with open('GoldenOHCalRunList.txt', 'w') as f:
0166         for run_number in ohcal_auto_golden_run_numbers:
0167             f.write(f"{run_number}\n")
0168     print(f"Number of OHCal GOLDEN runs saved to GoldenOHCalRunList.txt: {len(ohcal_auto_golden_run_numbers)}")
0169 
0170     # Get the intersection of all sets
0171     golden_run_numbers = emcal_auto_golden_run_numbers
0172     golden_run_numbers.sort()
0173     with open('GoldenCalorimeterRunList.txt', 'w') as f:
0174         for run_number in golden_run_numbers:
0175             f.write(f"{run_number}\n")
0176     print(f"Number of Calo GOLDEN runs saved to GoldenCalorimeterRunList.txt: {len(golden_run_numbers)}")
0177 
0178     # Close the Production database connection
0179     production_conn.close()
0180 
0181     nEvents_GL1 = get_nEvents(file_catalog_cursor, 'GoldenGL1RunList.txt')
0182     nEvents_FEM = get_nEvents(file_catalog_cursor, 'GoldenFEMrunList.txt')
0183     nEvents_withmaps = get_nEvents(file_catalog_cursor, '../RD/2024/Nov7/emcal_withmaps.txt')
0184     nEvents_validspin = get_nEvents(file_catalog_cursor, '../RD/2024/Nov7/validspinruns.txt')
0185 
0186     # Close the FileCatalog database connection
0187     file_catalog_conn.close()
0188 
0189 if __name__ == "__main__":
0190     main()
0191 
0192