-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatejsonfororca_sipmmapping.py
More file actions
185 lines (152 loc) · 5.71 KB
/
createjsonfororca_sipmmapping.py
File metadata and controls
185 lines (152 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""CreateJsonForOrca_SiPMMapping.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1dEgKzaV2BIGVXMPJP0BuCy_eSOpX2KVc
"""
import pandas as pd
import numpy as np
import json
import matplotlib.pyplot as plt
import re
#show evrything in dataframe
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
#open empty json file
# Define the file path
file_path = 'Automated_SIPM_Det.json'
# Create an empty dictionary
data = {}
# Write the empty dictionary to the JSON file
with open(file_path, 'w') as f:
json.dump(data, f)
#open the corresponding file
#always choose .tsv file
df = pd.read_csv('LAr_SIPmsIB_OB_2nd_immersion_Dec24.tsv', sep='\t')
def format_to_orca(df: pd.DataFrame) -> pd.DataFrame:
# Replace NaN values with '-'
df = df.fillna('-')
# Retain only columns up to and including 'HV Channel'
if 'Voltage' in df.columns:
df = df.iloc[:, :df.columns.get_loc('Voltage') + 1]
# Rename specific columns
rename_mapping = {' ': 'Det_name', 'HV Flange.1': 'HV Flange Pos'}
df = df.rename(columns=rename_mapping)
# Remove rows that contain 'String' in any column
df = df[~df.apply(lambda row: row.astype(str).str.contains('String', na=False).any(), axis=1)]
# Remove rows where all values are '-'
df = df[~(df == '-').all(axis=1)]
# Reset index
df = df.reset_index(drop=True)
# Limit to first 60 rows
return df.iloc[:60]
#df=format_to_orca(df)
def cleandf(df):
columns_to_drop = ['Data ID ', 'DSUB ID']
# Drop only existing columns to avoid errors
df = df.drop(columns=[col for col in columns_to_drop if col in df.columns])
df = df.fillna('--') # Replace NaN with an empty string
df = df.replace(['-','--','---'], '--') # Replace '--', '-', and '---' with an empty string
# Remove rows where all values are '-'
df = df[~(df == '--').all(axis=1)]
return df
#df=cleandf(df)
def add_loc_FormatSiPM(df):
df_i= df[df["Fiber ID"].str.contains("IB", na=False)].copy()
# Sort by "angle" in ascending order
df_i = df_i.sort_values(by="Angle")
# Create a new column increasing linearly with "angle"
df_i["loc"] = pd.factorize(df_i["Angle"])[0]
df_i["loc"] = df_i["loc"]+1
df_i = df_i.sort_values(by="Fiber ID")
df_i
df_o= df[df["Fiber ID"].str.contains("OB", na=False)].copy()
# Sort by "angle" in ascending order
df_o = df_o.sort_values(by="Angle")
# Create a new column increasing linearly with "angle"
df_o["loc"] = pd.factorize(df_o["Angle"])[0]
df_o["loc"] = df_o["loc"]+1
df_o = df_o.sort_values(by="Fiber ID")
df_o
df_t = pd.concat([df_i, df_o], axis=0)
#format sipm id
df_t['SiPM ID'] = df_t['SiPM ID'].astype(str)
# Apply formatting only if the ID doesn't start with 'S'
df_t['SiPM ID'] = df_t['SiPM ID'].apply(lambda x: 'S' + x.zfill(2) if not x.startswith('S') else x)
#add sipm type as inner and outer barrel
df_t['SiPM Type'] = df_t['Fiber ID'].str[:2]
return df_t
def process_entry(entry):
if isinstance(entry, str): # Ensure it's a string
parts = entry.split('-') if '-' in entry else entry.split('_')
if len(parts) < 2:
return None # Handle unexpected formats
prefix, num = parts[0], int(parts[1])
if prefix == 'IB':
return '3' if num % 2 == 0 else '2' # IB cases
elif prefix == 'OB ':
return '4' if num % 2 == 0 else '1' # OB cases
return None # Default case for missing/invalid values
# add board id with mapping
# Define mapping dictionary
mapping = {
(2, 0): '0x200',
(2, 1): '0x210',
(2, 2): '0x220',
(2, 3): '0x230',
(2, 4): '0x240',
(2, 5): '0x250',
(2, 6): '0x260',
(2, 7): '0x270',
(2, 8): '0x280',
(2, 9): '0x290',
(2, 10): '0x2A0',
(2, 11): '0x2B0',
(2, 12): '0x2C0'
}
def add_entry_to_json(df: pd.DataFrame, json_file: str):
"""
Adds entries from a DataFrame to a JSON file following the specified format.
Parameters:
df (pd.DataFrame): DataFrame containing the new data.
json_file (str): Path to the JSON file.
"""
# Load existing JSON data or initialize an empty dictionary
try:
with open(json_file, 'r') as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = {}
# Iterate over the DataFrame rows and add entries to JSON
for _, row in df.iterrows():
key = row['SiPM ID'] # Unique identifier for the entry (e.g., 'BF862_1', 'BF862_2', etc.)
data[key] = {
"system": str("spm"),
"daq": {
"board_ch": str(row.get("Ch", "--")),
"adc_serial": str(row.get("ADC Serial", "--")),
"board_slot": str(row.get("Slot", "--")),
"board_id": str(row.get("Flashcam Address", "--")),
"crate": str(row.get("Crate", "--"))
},
"low_voltage": {
"board_chan": str(row.get("HV Channel", "--")),
"board_slot": str(row.get("HV Module", "--")),
"board_B_pos": str(row.get("pos", "--")),
"board_string": str(row.get("loc", "--")),
"crate": str('0')
},
"det_type": str(row.get("SiPM Type", "--")),
}
# Write back to JSON file
with open(json_file, 'w') as file:
json.dump(data, file, indent=4)
print(f"Entries successfully added to {json_file}")
# Apply the function to the DataFrame
df=format_to_orca(df)
df = cleandf(df)
df = add_loc_FormatSiPM(df)
df['pos'] = df['Fiber ID'].apply(process_entry)
df['Flashcam Address'] = df.apply(lambda row: mapping.get((row['Crate'], row['Slot']), '--'), axis=1)
add_entry_to_json(df,'Automated_SIPM_Det.json')
df