FairShip
Loading...
Searching...
No Matches
add_muonresponse Namespace Reference

Functions

bool inspect_file (inputfile, muonfile, print_table=False)
None modify_file (inputfile, muonfile)

Variables

 level
 parser = argparse.ArgumentParser(description=__doc__)
 required
 help
 args = parser.parse_args()
list headers
bool muons_found = inspect_file(args.inputfile, args.muonfile)

Detailed Description

Script to add the incoming muon's MC hits in the SBT to the simulation file.

Function Documentation

◆ inspect_file()

bool add_muonresponse.inspect_file ( inputfile,
muonfile,
print_table = False )
Inspecting the produced file for successfully added muon veto points.

Definition at line 44 of file add_muonresponse.py.

44def inspect_file(inputfile, muonfile, print_table=False) -> bool:
45 """Inspecting the produced file for successfully added muon veto points."""
46 input_file = r.TFile.Open(inputfile, "read")
47 input_tree = input_file["cbmsim"]
48
49 muon_file = r.TFile.Open(muonfile, "read")
50 muon_tree = muon_file["DIS"]
51
52 muons_found = False
53
54 table_data = []
55
56 for i, (muon_event, input_event) in enumerate(zip(muon_tree, input_tree)):
57 muon_hits = len(muon_event.muon_vetoPoints)
58 muoncount = 0
59 if muon_hits:
60 for hit in input_event.vetoPoint:
61 if hit.GetTrackID() == 0:
62 muons_found = True
63 muoncount += 1
64
65 muon_ubthits = len(muon_event.muon_UpstreamTaggerPoints)
66 muon_ubtcount = 0
67 if muon_ubthits:
68 for hit in input_event.UpstreamTaggerPoint:
69 if hit.GetTrackID() == 0:
70 muon_ubtcount += 1
71
72 table_data.append(
73 [
74 i,
75 muon_hits,
76 len(input_event.vetoPoint) - muoncount,
77 muoncount,
78 len(input_event.vetoPoint),
79 muon_ubthits,
80 len(input_event.UpstreamTaggerPoint) - muon_ubtcount,
81 muon_ubtcount,
82 len(input_event.UpstreamTaggerPoint),
83 ]
84 )
85
86 if print_table:
87 print(tabulate(table_data, headers=headers, tablefmt="grid"))
88
89 input_file.Close()
90 muon_file.Close()
91
92 return muons_found
93
94

◆ modify_file()

None add_muonresponse.modify_file ( inputfile,
muonfile )
Add information from original muon to input simulation file.

Definition at line 95 of file add_muonresponse.py.

95def modify_file(inputfile, muonfile) -> None:
96 """Add information from original muon to input simulation file."""
97 logging.warning(
98 f"vetoPoints & UpstreamTaggerPoints from the incoming muon (saved in {muonfile}) will be added to {inputfile}."
99 )
100
101 input_file = r.TFile.Open(inputfile, "read")
102 try:
103 input_tree = input_file["cbmsim"]
104 except Exception as e:
105 print(f"Error: {e}")
106 input_file.Close()
107 exit(1)
108
109 # Open the external file with additional vetoPoints
110 muon_file = r.TFile.Open(muonfile, "read")
111 try:
112 muon_tree = muon_file["DIS"]
113 except Exception as e:
114 print(f"Error: {e}")
115 muon_file.Close()
116 exit(1)
117
118 temp_filename = inputfile.replace(".root", ".tmp")
119 temp_file = r.TFile.Open(temp_filename, "recreate")
120 output_tree = input_tree.CloneTree(0) # Clone the structure of the existing tree, but do not copy the entries
121
122 combined_vetoPoint = r.TClonesArray("vetoPoint")
123 output_tree.SetBranchAddress("vetoPoint", combined_vetoPoint)
124
125 combined_UpstreamTaggerPoint = r.TClonesArray("UpstreamTaggerPoint")
126 output_tree.SetBranchAddress("UpstreamTaggerPoint", combined_UpstreamTaggerPoint)
127
128 table_data = []
129
130 for i, (input_event, muon_event) in enumerate(zip(input_tree, muon_tree)):
131 interaction_point = r.TVector3()
132 input_event.MCTrack[0].GetStartVertex(interaction_point)
133
134 combined_vetoPoint.Clear()
135
136 index = 0
137
138 for hit in input_event.vetoPoint:
139 ut.assignClonesArrayItem(combined_vetoPoint, index, hit)
140 index += 1
141
142 muoncount = 0
143 for hit in muon_event.muon_vetoPoints:
144 if hit.GetZ() < interaction_point.Z():
145 ut.assignClonesArrayItem(combined_vetoPoint, index, hit)
146 index += 1
147 muoncount += 1
148
149 combined_UpstreamTaggerPoint.Clear()
150
151 ubt_index = 0
152
153 for hit in input_event.UpstreamTaggerPoint:
154 ut.assignClonesArrayItem(combined_UpstreamTaggerPoint, ubt_index, hit)
155 ubt_index += 1
156
157 muon_ubtcount = 0
158 for hit in muon_event.muon_UpstreamTaggerPoints:
159 if hit.GetZ() < interaction_point.Z():
160 ut.assignClonesArrayItem(combined_UpstreamTaggerPoint, ubt_index, hit)
161 ubt_index += 1
162 muon_ubtcount += 1
163
164 table_data.append(
165 [
166 i,
167 len(muon_event.muon_vetoPoints),
168 len(input_event.vetoPoint),
169 muoncount,
170 len(combined_vetoPoint),
171 len(muon_event.muon_UpstreamTaggerPoints),
172 len(input_event.UpstreamTaggerPoint),
173 muon_ubtcount,
174 len(combined_UpstreamTaggerPoint),
175 ]
176 )
177 output_tree.Fill()
178
179 output_tree.Write("cbmsim", r.TObject.kOverwrite)
180 temp_file.Close()
181 input_file.Close()
182 muon_file.Close()
183
184 os.remove(inputfile)
185 os.rename(temp_filename, inputfile)
186 print(f"File updated with incoming muon info as {inputfile}")
187 print(tabulate(table_data, headers=headers, tablefmt="grid"))
188
189

Variable Documentation

◆ args

add_muonresponse.args = parser.parse_args()

Definition at line 30 of file add_muonresponse.py.

◆ headers

list add_muonresponse.headers
Initial value:
1= [
2 "Event",
3 "Muon vetoPoints Available",
4 "Original VetoPoint Count",
5 "Muon vetoPoints Added",
6 "Combined VetoPoint Count",
7 "Muon UBTPoints Available",
8 "Original UBTPoint Count",
9 "Muon UBTPoints Added",
10 "Combined UBTPoint Count",
11]

Definition at line 31 of file add_muonresponse.py.

◆ help

add_muonresponse.help

Definition at line 22 of file add_muonresponse.py.

◆ level

add_muonresponse.level

Definition at line 15 of file add_muonresponse.py.

◆ muons_found

bool add_muonresponse.muons_found = inspect_file(args.inputfile, args.muonfile)

Definition at line 190 of file add_muonresponse.py.

◆ parser

add_muonresponse.parser = argparse.ArgumentParser(description=__doc__)

Definition at line 17 of file add_muonresponse.py.

◆ required

add_muonresponse.required

Definition at line 21 of file add_muonresponse.py.