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
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)
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