Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:13:32

0001 import pyodbc
0002 
0003 def get_run_numbers(cursor):
0004     # query = """
0005     # SELECT runnumber
0006     # FROM datasets
0007     # WHERE filename like 'DST_CALO%'
0008     # GROUP BY runnumber
0009     # HAVING SUM(events) >= 1000000 AND runnumber > 46619;
0010     # """
0011     query = """
0012     SELECT runnumber
0013     FROM datasets
0014     WHERE filename like 'DST_CALO%'
0015     GROUP BY runnumber
0016     HAVING SUM(events) >= 500000;
0017     """
0018     cursor.execute(query)
0019     run_numbers = [row.runnumber for row in cursor.fetchall()]
0020     return run_numbers
0021 
0022 def filter_golden_runs(file_catalog_run_numbers, production_cursor):
0023     query = """
0024     SELECT runnumber
0025     FROM goodruns
0026     """
0027     production_cursor.execute(query)
0028     all_good_runs = {row.runnumber for row in production_cursor.fetchall()}
0029 
0030     runs_not_in_goodruns = set(file_catalog_run_numbers) - all_good_runs
0031     print(f"Number of runs not in the goodruns table: {len(runs_not_in_goodruns)}")
0032 
0033     query = """
0034     SELECT runnumber
0035     FROM goodruns
0036     WHERE (emcal_auto).runclass = 'GOLDEN'
0037     """
0038     production_cursor.execute(query)
0039     golden_runs = {row.runnumber for row in production_cursor.fetchall()}
0040 
0041     # Return the intersection of file_catalog_run_numbers and golden_runs
0042     return list(golden_runs.intersection(file_catalog_run_numbers))
0043 
0044 def main():
0045     # Connect to the FileCatalog database
0046     file_catalog_conn = pyodbc.connect("DSN=FileCatalog;UID=phnxrc;READONLY=True")
0047     file_catalog_cursor = file_catalog_conn.cursor()
0048 
0049     # Get unique run numbers with at least 1 million total events
0050     file_catalog_run_numbers = get_run_numbers(file_catalog_cursor)
0051     print(f"Number of runs found in the File Catalog: {len(file_catalog_run_numbers)}")
0052 
0053     # Close the FileCatalog database connection
0054     file_catalog_conn.close()
0055 
0056     # Connect to the Production database
0057     production_conn = pyodbc.connect("DSN=Production_write")
0058     production_cursor = production_conn.cursor()
0059 
0060     # Filter run numbers based on 'GOLDEN' status
0061     golden_run_numbers = filter_golden_runs(file_catalog_run_numbers, production_cursor)
0062     golden_run_numbers.sort()
0063 
0064     # Save run numbers to a text file
0065     runlistfile = 'emcal_goodruns.txt'
0066     with open(runlistfile, 'w') as f:
0067         for run_number in golden_run_numbers:
0068             f.write(f"{run_number}\n")
0069     print(f"Number of GOLDEN runs saved to {runlistfile}: {len(golden_run_numbers)}")
0070 
0071     # Close the Production database connection
0072     production_conn.close()
0073 
0074 if __name__ == "__main__":
0075     main()