ATLAS Offline Software
module_selector_from_json.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 # 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.
4 # It has three sets of functions:
5 # find: this can look for specific set of modules based on parameters: bec(barrel or end cap), layer_disk, phi, eta and side.
6 # merge: to merge any two json files produced with different configuration
7 # 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
8 
9 import json
10 import random
11 
12 def find(bec=None, layer_disk=None, phi=None, eta=None, side=None, asdec=False, input_data="geometry.json", output_file=None):
13  data = {}
14 
15  with open(input_data) as f:
16  if input_data.endswith(".json"):
17  data = json.load(f)
18  else:
19  print("Unexpected input file or format.")
20  return []
21 
22  IDs = []
23 
24  for ID, info in data.items():
25  if bec is not None and info["BEC"] != str(bec):
26  continue
27 
28  if layer_disk is not None and info["LayerDisk"] != str(layer_disk):
29  continue
30 
31  if phi is not None and info["PhiModule"] not in str([i for i in phi]):
32  continue
33 
34  if eta is not None and info["EtaModule"] > str(eta):
35  continue
36 
37  if side is not None and info["Side"] != str(side):
38  continue
39 
40  if asdec:
41  info["Decimal_ID"] = str(int(ID, 16))
42 
43  IDs.append(ID)
44 
45  with open(output_file, "w") as f:
46  selected_data = {ID: data[ID] for ID in IDs}
47  json.dump(selected_data, f, indent=4)
48 
49  return IDs
50 
51 
52 def merge(file_1="selected_modules_1.json", file_2="selected_modules_2.json", output_file=None):
53  data1 = {}
54  data2 = {}
55  IDs = []
56 
57  with open(file_1) as f1:
58  if file_1.endswith(".json"):
59  data1 = json.load(f1)
60  else:
61  print("Unexpected input file_1 or format.")
62  return []
63 
64  IDs.extend(data1.keys())
65 
66  with open(file_2) as f2:
67  if file_2.endswith(".json"):
68  data2 = json.load(f2)
69  else:
70  print("Unexpected input file_2 or format.")
71  return []
72 
73  IDs.extend(data2.keys())
74 
75  with open(output_file, "w") as f3:
76  selected_data = {}
77  for ID in IDs:
78  if ID in data1:
79  selected_data[ID] = data1[ID]
80  elif ID in data2:
81  selected_data[ID] = data2[ID]
82  json.dump(selected_data, f3, indent=4)
83 
84  return IDs
85 
86 def select_random(frac = None, input_data=None, output_file=None):
87  data = {}
88 
89  with open(input_data) as f:
90  if input_data.endswith(".json"):
91  data = json.load(f)
92  else:
93  print("Unexpected input file or format.")
94  return []
95 
96  IDs = []
97 
98  num = int(round(frac * len(data), 0))
99  IDs = random.choices(list(data.keys()), k=num)
100 
101  for ID, info in data.items():
102  info["Decimal_ID"] = str(int(ID, 16))
103 
104  with open(output_file, "w") as f:
105  selected_data = {ID: data[ID] for ID in IDs}
106  json.dump(selected_data, f, indent=4)
107 
108  return IDs
109 
110 
111 if __name__ == "__main__":
112  #data = select_random(frac = 0.01, input_data="geometry.json", output_file="test_selected_frac_modules.json")
113  data = find(bec = 0, layer_disk = 0, phi = [0,1,2,3,4], asdec = True, output_file="test_selected_modules.json")
114  #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")
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
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:52
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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:86
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
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:12