File indexing completed on 2026-08-31 08:24:41
0001 import sys
0002 import requests
0003 import numpy as np
0004 import os
0005 import csv
0006 from collections import defaultdict
0007 from sqlalchemy import create_engine, MetaData, Table, text
0008 from sqlalchemy.orm import sessionmaker
0009 from datetime import datetime
0010
0011 variable_names = ['sphenix_tpc_hv_caen_IMon']
0012 output_folder = 'tpc_GEM_current_status'
0013
0014
0015 S = 1672549200
0016
0017
0018 def to_bco(ts):
0019 return int((ts - S) * 56299000 // 6)
0020
0021
0022 def process_run_data(runnumber):
0023
0024
0025
0026
0027 source_engine = create_engine('postgresql://phnxrc@sphnxdaqdbreplica.sdcc.bnl.gov/daq')
0028 Session = sessionmaker(bind=source_engine)
0029 session = Session()
0030
0031 query = text("""
0032 SELECT
0033 extract(epoch from brtimestamp AT TIME ZONE 'America/New_York') AS begin_run,
0034 extract(epoch from ertimestamp AT TIME ZONE 'America/New_York') AS end_run
0035 FROM run
0036 WHERE runnumber = :runnumber
0037 """)
0038
0039 result = session.execute(query, {'runnumber': runnumber}).fetchone()
0040 session.close()
0041
0042 if not result or result.begin_run is None or result.end_run is None:
0043 print("No valid run timing")
0044 return
0045
0046 begin_run = int(result.begin_run)
0047 end_run = int(result.end_run)
0048
0049 print("Begin:", begin_run)
0050 print("End:", end_run)
0051
0052
0053
0054
0055 bco_table = defaultdict(list)
0056
0057
0058
0059
0060 for variable_name in variable_names:
0061
0062 params = {
0063 'query': variable_name,
0064 'start': begin_run,
0065 'end': end_run,
0066 'step': '1m'
0067 }
0068
0069 resp = requests.get(
0070 "http://promspx01.sdcc.bnl.local:9090/api/v1/query_range",
0071 params=params
0072 )
0073
0074 if resp.status_code != 200:
0075 continue
0076
0077 result_json = resp.json()
0078 if result_json['status'] != 'success':
0079 continue
0080
0081 metrics = result_json['data']['result']
0082
0083
0084
0085
0086 for metric in metrics:
0087
0088 labels = metric['metric']
0089
0090 HV_Layer = labels.get('HV_Layer', 'unknown')
0091 if HV_Layer != 'G4':
0092 continue
0093
0094 side = labels.get('side', 'unknown')
0095 sector = labels.get('sector', '-1')
0096 R_Module = labels.get('R_Module', 'unknown')
0097
0098 key = (side, sector, R_Module, HV_Layer)
0099
0100 values = [
0101 (to_bco(v[0]), float(v[1]))
0102 for v in metric['values']
0103 ]
0104
0105 values.sort(key=lambda x: x[0])
0106
0107 bco_table[key].extend(values)
0108
0109
0110
0111
0112 if not bco_table:
0113 print("No data")
0114 return
0115
0116 all_bcos = set()
0117 channel_values = {}
0118
0119 for key, series in bco_table.items():
0120 values_by_bco = {}
0121
0122 for bco, current in series:
0123 values_by_bco[bco] = current
0124 all_bcos.add(bco)
0125
0126 channel_values[key] = values_by_bco
0127
0128 sorted_bcos = sorted(all_bcos)
0129 sorted_channels = sorted(channel_values)
0130
0131
0132
0133
0134 os.makedirs(output_folder, exist_ok=True)
0135
0136 out_file = f"{output_folder}/run_{runnumber}_GEM_BCO.csv"
0137
0138 with open(out_file, "w") as f:
0139
0140 for bco in sorted_bcos:
0141 f.write(f"bco {bco}\n")
0142
0143 for channel in sorted_channels:
0144 current = channel_values[channel].get(bco)
0145 if current == "" or current is None:
0146 continue
0147
0148 label = "".join(channel)
0149 f.write(f"{label} {current}\n")
0150
0151 print("Wrote:", out_file)
0152
0153
0154 if __name__ == "__main__":
0155
0156 if len(sys.argv) != 2:
0157 print("Usage: python3 script.py <runnumber>")
0158 sys.exit(1)
0159
0160 process_run_data(int(sys.argv[1]))