FairShip
Loading...
Searching...
No Matches
add_muonresponse.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# SPDX-License-Identifier: LGPL-3.0-or-later
3# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
4
5"""Script to add the incoming muon's MC hits in the SBT to the simulation file."""
6
7import argparse
8import logging
9import os
10
11import ROOT as r
12import rootUtils as ut
13from tabulate import tabulate
14
15logging.basicConfig(level=logging.DEBUG)
16
17parser = argparse.ArgumentParser(description=__doc__)
18parser.add_argument(
19 "-f",
20 "--inputfile",
21 required=True,
22 help="Simulation file produced from DIS, WARNING: File will be modified.",
23)
24parser.add_argument(
25 "-m",
26 "--muonfile",
27 required=True,
28 help="Muon file used for DIS production (muonDis_<>.root)",
29)
30args = parser.parse_args()
31headers = [
32 "Event",
33 "Muon vetoPoints Available",
34 "Original VetoPoint Count",
35 "Muon vetoPoints Added",
36 "Combined VetoPoint Count",
37 "Muon UBTPoints Available",
38 "Original UBTPoint Count",
39 "Muon UBTPoints Added",
40 "Combined UBTPoint Count",
41]
42
43
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
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
190muons_found = inspect_file(args.inputfile, args.muonfile)
191
192if not muons_found:
193 print("Incoming muon's vetoPoints & UpstreamTaggerPoints inf missing in file, proceeding with modification")
194 modify_file(args.inputfile, args.muonfile)
195else:
196 print("File is already updated with incoming muon info. Nothing to do.")
197 muons_found = inspect_file(args.inputfile, args.muonfile, print_table=True)
None modify_file(inputfile, muonfile)
bool inspect_file(inputfile, muonfile, print_table=False)