DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tde_stream_db_gen.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2import click
3import os
4
5import pandas as pd
6import numpy as np
7
8import conffwk
9
10from rich import print
11
12
13def get_includes() -> list[str]:
14 include_files = [
15 "schema/confmodel/dunedaq.schema.xml",
16 "schema/appmodel/application.schema.xml",
17 "schema/appmodel/fdmodules.schema.xml",
18 "schema/appmodel/tde.schema.xml",
19 ]
20 return include_files
21
22
23def get_mapping(det_name : str, base_sid : int) -> pd.DataFrame:
24 df = pd.read_csv(f"{os.environ['DBT_AREA_ROOT']}/sourcecode/tdemodules/config/mapping.txt", delim_whitespace = True, names = ["Crate", "AMC", "AMC_channel", "CRP", "view_type", "view_channel"])
25 if det_name == "tde-testcrate":
26 df = df[df["Crate"] == 0]
27 df = df[np.any([df["AMC"] == i for i in [1, 6, 7]], 0)]
28 # there may be less channels in the testcrate, but for now this is fine.
29
30 df["CRP"] += base_sid # This is needed to assign the source ID correctly for the TDE ()
31 return df
32
33
34def get_mapping_from_channel_map(channel_map : str):
35 df = pd.read_csv(channel_map, sep=r'\s+', names = ['offlchan', 'detid', 'detelement', 'crate', 'slot', 'stream', 'streamchan', 'plane', 'chan_in_plane', 'femb', 'asic ', 'asicchan'])
36
37 df23 = df[df['detelement'] < 4]
38
39 df_map = df23[['crate','slot', 'streamchan', 'detelement', 'plane', 'chan_in_plane']]
40 df_map.columns = ["Crate", "AMC", "AMC_channel", "CRP", "view_type", "view_channel"]
41 return df_map
42
43
44def create_db(oksfile : str, include_files : list[str]) -> conffwk.Configuration:
45 db = conffwk.Configuration("oksconflibs")
46 if not oksfile.endswith(".data.xml"):
47 oksfile = oksfile + ".data.xml"
48 print(f"Creating OKS database file {oksfile}")
49 db.create_db(oksfile, include_files)
50 db.set_active(oksfile)
51 return db
52
53
54def calculate_amc_net_info(crate, slot):
55 return {
56 "ip_1g" : f"10.73.{crate + 32}.{slot + 1}",
57 "ip_10g" : f"10.73.{crate + 32}.{slot + 13}",
58 "mac_1g" : "00:07:ED:A1:%02x:%02x" % (crate + 1, slot + 1),
59 "mac_10g" : "00:07:ED:A2:%02x:%02x" % (crate + 1, slot + 13),
60 }
61
62@click.command
63@click.option("--det_name", "-d", type = click.Choice(["tde", "tde-testcrate"]), default = "tde", help = "Detector element readout.")
64@click.option("--source_id", "-s", type = int, default = 2, help = "Base source ID number.")
65@click.option("--det_id", "-D", type = int, default = 11, help = "Detector id.")
66@click.option("--sid_suffix", is_flag = "store_true", help = "Use source ID as the suffix for the AMCDetDataSenders.")
67@click.option("channel_map", "-c", type = str, default = None, help = "ProtoDUNE channel map to infer the Crate/AMC mapping.")
68def main(det_name, source_id, det_id, sid_suffix, channel_map):
69 # key is crate number, so 10.73.(n+32).128, value is the number of AMCs each crate has installed.
70
71 if channel_map:
72 mapping = get_mapping_from_channel_map(channel_map)
73 else:
74 mapping = get_mapping(det_id, source_id)
75 print(mapping)
76
77 db = create_db(f"crp23-det-senders", get_includes())
78
79 sid_counters = {i : i * 100 for i in pd.unique(mapping["CRP"])}
80 for cg in [[0, 1, 2, 6, 7], [3, 4, 5, 8, 9]]:
81 streams = []
82 for crate in cg:
83 crate_map = mapping[mapping["Crate"] == crate]
84 amcs = pd.unique(crate_map["AMC"])
85 for amc in amcs:
86 name = f"crate{crate}-slot{amc}"
87 base_sid = pd.unique(crate_map[crate_map["AMC"] == amc]["CRP"])[0]
88 sid_counters[base_sid] += 1
89
90 amc_net_info = calculate_amc_net_info(crate, amc)
91 geo = db.create_obj(class_name = "GeoId", uid = f"geoId-{det_name}-amc-{sid_counters[base_sid]}")
92 geo["detector_id"] = det_id # channel map may be required for this ()
93 geo["crate_id"] = crate
94 geo["slot_id"] = amc
95
96 ds = db.create_obj(class_name = "DetectorStream", uid = f"DetStream-{sid_counters[base_sid]}")
97 ds["source_id"] = sid_counters[base_sid]
98 ds["geo_id"] = geo
99
100 nw_send = db.create_obj(class_name = "NetworkInterface", uid = f"nw-{det_name}-amc-{name}-10g")
101 nw_send["mac_address"] = amc_net_info["mac_10g"]
102 nw_send["ip_address"] = [amc_net_info["ip_10g"]]
103 nw_send["network_name"] = "Data"
104
105 nw_rec = db.create_obj(class_name = "NetworkInterface", uid = f"nw-{det_name}-amc-{name}-1g")
106 nw_rec["mac_address"] = amc_net_info["mac_1g"]
107 nw_rec["ip_address"] = [amc_net_info["ip_1g"]]
108 nw_rec["network_name"] = "Control"
109
110 sender_names = sid_counters[base_sid] if sid_suffix else name
111 dds = db.create_obj(class_name = "TdeAmcDetDataSender", uid = f"dds-{det_name}-amc-{sender_names}")
112 dds["port"] = 54321 + amc + 1
113 dds["control_host"] = f"np02-amc-{sid_counters[base_sid]}" # This should be the source ID
114 dds["uses"] = nw_send
115 dds["control_endpoint"] = nw_rec
116 dds["streams"] = [ds]
117 streams.append(dds)
118 db.commit()
119 return
120
121
122if __name__ == "__main__":
123 main()
get_mapping_from_channel_map(str channel_map)
calculate_amc_net_info(crate, slot)
main(det_name, source_id, det_id, sid_suffix, channel_map)
pd.DataFrame get_mapping(str det_name, int base_sid)
conffwk.Configuration create_db(str oksfile, list[str] include_files)