Loading [MathJax]/jax/output/SVG/config.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
module_selector_from_json.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 
4 # This scripts selects the required set of modules from the geometry.json (contains all the modules from the Strip Detector) obtained using geometry_dat_to_joson.py script.
5 # It has three sets of functions:
6 # find: this can look for specific set of modules based on parameters: bec(barrel or end cap), layer_disk, phi, eta and side.
7 # merge: to merge any two json files produced with different configuration
8 # select_random: to select a given fraction of modules randomly from all the modules (using geometry.json as input) or any other config using its list of json
9 
10 import json
11 import random
12 
13 def find(bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="geometry.json", output_file=None):
14  data = {}
15 
16  with open(input_data) as f:
17  if input_data.endswith(".json"):
18  data = json.load(f)
19  else:
20  print("Unexpected input file or format.")
21  return []
22 
23  IDs = []
24 
25  for ID, info in data.items():
26  if bec is not None and int(info["BEC"]) != bec:
27  continue
28 
29  if layer_disk is not None and int(info["LayerDisk"]) != layer_disk:
30  continue
31 
32  if phi is not None and int(info["PhiModule"]) not in [i for i in phi]:
33  continue
34 
35  if eta is not None and int(info["EtaModule"]) > eta:
36  continue
37 
38  if side is not None and int(info["Side"]) != side:
39  continue
40 
41  if asdec:
42  info["Decimal_ID"] = str(int(ID, 16))
43 
44  IDs.append(ID)
45 
46  with open(output_file, "w") as f:
47  selected_data = {ID: data[ID] for ID in IDs}
48  json.dump(selected_data, f, indent=4)
49 
50  return IDs
51 
52 
53 def merge(file_1="selected_modules_1.json", file_2="selected_modules_2.json", output_file=None):
54  data1 = {}
55  data2 = {}
56  IDs = []
57 
58  with open(file_1) as f1:
59  if file_1.endswith(".json"):
60  data1 = json.load(f1)
61  else:
62  print("Unexpected input file_1 or format.")
63  return []
64 
65  IDs.extend(data1.keys())
66 
67  with open(file_2) as f2:
68  if file_2.endswith(".json"):
69  data2 = json.load(f2)
70  else:
71  print("Unexpected input file_2 or format.")
72  return []
73 
74  IDs.extend(data2.keys())
75 
76  with open(output_file, "w") as f3:
77  selected_data = {}
78  for ID in IDs:
79  if ID in data1:
80  selected_data[ID] = data1[ID]
81  elif ID in data2:
82  selected_data[ID] = data2[ID]
83  json.dump(selected_data, f3, indent=4)
84 
85  return IDs
86 
87 def select_random(frac = None, input_data=None, output_file=None):
88  data = {}
89 
90  with open(input_data) as f:
91  if input_data.endswith(".json"):
92  data = json.load(f)
93  else:
94  print("Unexpected input file or format.")
95  return []
96 
97  IDs = []
98 
99  num = int(round(frac * len(data), 0))
100  IDs = random.sample(list(data.keys()), k=num)
101 
102  for ID, info in data.items():
103  info["Decimal_ID"] = str(int(ID, 16))
104 
105  with open(output_file, "w") as f:
106  selected_data = {ID: data[ID] for ID in IDs}
107  json.dump(selected_data, f, indent=4)
108 
109  return IDs
110 
111 
112 if __name__ == "__main__":
113  #data = select_random(frac = 0.01, input_data="geometry.json", output_file="test_selected_frac_modules.json")
114  data = find(bec = 0, layer_disk = 0, asdec = True, input_data="PixelGeometry.json", output_file="IBL_selected_modules.json")
115  #data = merge(file_1="selected_modules_ec_minus_2_layer_0_side_0.json", file_2="selected_modules_ec_minus_2_layer_1_side_0.json", output_file="test_merged_file.json")
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
module_selector_from_json.merge
def merge(file_1="selected_modules_1.json", file_2="selected_modules_2.json", output_file=None)
Definition: module_selector_from_json.py:53
python.LArMinBiasAlgConfig.int
int
Definition: LArMinBiasAlgConfig.py:59
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
Trk::open
@ open
Definition: BinningType.h:40
module_selector_from_json.select_random
def select_random(frac=None, input_data=None, output_file=None)
Definition: module_selector_from_json.py:87
str
Definition: BTagTrackIpAccessor.cxx:11
module_selector_from_json.find
def find(bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="geometry.json", output_file=None)
Definition: module_selector_from_json.py:13