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
12from tabulate import tabulate
13
14logging.basicConfig(level=logging.DEBUG)
15
16parser = argparse.ArgumentParser(description=__doc__)
17parser.add_argument(
18 "-f",
19 "--inputfile",
20 required=True,
21 help="Simulation file produced from DIS, WARNING: File will be modified.",
22)
23parser.add_argument(
24 "-m",
25 "--muonfile",
26 required=True,
27 help="Muon file used for DIS production (muonDis_<>.root)",
28)
29args = parser.parse_args()
30headers = [
31 "Event",
32 "Muon vetoPoints Available",
33 "Original VetoPoint Count",
34 "Muon vetoPoints Added",
35 "Combined VetoPoint Count",
36 "Muon UBTPoints Available",
37 "Original UBTPoint Count",
38 "Muon UBTPoints Added",
39 "Combined UBTPoint Count",
40]
41
42
43def inspect_file(inputfile, muonfile, print_table=False) -> bool:
44 """Inspecting the produced file for successfully added muon veto points."""
45 input_file = r.TFile.Open(inputfile, "read")
46 input_tree = input_file.cbmsim
47
48 muon_file = r.TFile.Open(muonfile, "read")
49 muon_tree = muon_file.DIS
50
51 muons_found = False
52
53 table_data = []
54
55 for i, (muon_event, input_event) in enumerate(zip(muon_tree, input_tree)):
56 muon_hits = len(muon_event.muon_vetoPoints)
57 muoncount = 0
58 if muon_hits:
59 for hit in input_event.vetoPoint:
60 if hit.GetTrackID() == 0:
61 muons_found = True
62 muoncount += 1
63
64 muon_ubthits = len(muon_event.muon_UpstreamTaggerPoints)
65 muon_ubtcount = 0
66 if muon_ubthits:
67 for hit in input_event.UpstreamTaggerPoint:
68 if hit.GetTrackID() == 0:
69 muon_ubtcount += 1
70
71 table_data.append(
72 [
73 i,
74 muon_hits,
75 len(input_event.vetoPoint) - muoncount,
76 muoncount,
77 len(input_event.vetoPoint),
78 muon_ubthits,
79 len(input_event.UpstreamTaggerPoint) - muon_ubtcount,
80 muon_ubtcount,
81 len(input_event.UpstreamTaggerPoint),
82 ]
83 )
84
85 if print_table:
86 print(tabulate(table_data, headers=headers, tablefmt="grid"))
87
88 input_file.Close()
89 muon_file.Close()
90
91 return muons_found
92
93
94def modify_file(inputfile, muonfile) -> None:
95 """Add information from original muon to input simulation file."""
96 logging.warning(
97 f"vetoPoints & UpstreamTaggerPoints from the incoming muon (saved in {muonfile}) will be added to {inputfile}."
98 )
99
100 input_file = r.TFile.Open(inputfile, "read")
101 try:
102 input_tree = input_file.cbmsim
103 except Exception as e:
104 print(f"Error: {e}")
105 input_file.Close()
106 exit(1)
107
108 # Open the external file with additional vetoPoints
109 muon_file = r.TFile.Open(muonfile, "read")
110 try:
111 muon_tree = muon_file.DIS
112 except Exception as e:
113 print(f"Error: {e}")
114 muon_file.Close()
115 exit(1)
116
117 temp_filename = inputfile.replace(".root", ".tmp")
118 temp_file = r.TFile.Open(temp_filename, "recreate")
119 output_tree = input_tree.CloneTree(0) # Clone the structure of the existing tree, but do not copy the entries
120
121 combined_vetoPoint = r.TClonesArray("vetoPoint")
122 output_tree.SetBranchAddress("vetoPoint", combined_vetoPoint)
123
124 combined_UpstreamTaggerPoint = r.TClonesArray("UpstreamTaggerPoint")
125 output_tree.SetBranchAddress("UpstreamTaggerPoint", combined_UpstreamTaggerPoint)
126
127 table_data = []
128
129 for i, (input_event, muon_event) in enumerate(zip(input_tree, muon_tree)):
130 interaction_point = r.TVector3()
131 input_event.MCTrack[0].GetStartVertex(interaction_point)
132
133 combined_vetoPoint.Clear()
134
135 index = 0
136
137 for hit in input_event.vetoPoint:
138 if combined_vetoPoint.GetSize() == index:
139 combined_vetoPoint.Expand(index + 1)
140 combined_vetoPoint[index] = hit # pending fix to support ROOT 6.32+
141 index += 1
142
143 muoncount = 0
144 for hit in muon_event.muon_vetoPoints:
145 if hit.GetZ() < interaction_point.Z():
146 if combined_vetoPoint.GetSize() == index:
147 combined_vetoPoint.Expand(index + 1)
148 combined_vetoPoint[index] = hit # pending fix to support ROOT 6.32+
149 index += 1
150 muoncount += 1
151
152 combined_UpstreamTaggerPoint.Clear()
153
154 ubt_index = 0
155
156 for hit in input_event.UpstreamTaggerPoint:
157 if combined_UpstreamTaggerPoint.GetSize() == ubt_index:
158 combined_UpstreamTaggerPoint.Expand(ubt_index + 1)
159 combined_UpstreamTaggerPoint[ubt_index] = hit # pending fix to support ROOT 6.32+
160 ubt_index += 1
161
162 muon_ubtcount = 0
163 for hit in muon_event.muon_UpstreamTaggerPoints:
164 if hit.GetZ() < interaction_point.Z():
165 if combined_UpstreamTaggerPoint.GetSize() == ubt_index:
166 combined_UpstreamTaggerPoint.Expand(ubt_index + 1)
167 combined_UpstreamTaggerPoint[ubt_index] = hit # pending fix to support ROOT 6.32+
168 ubt_index += 1
169 muon_ubtcount += 1
170
171 table_data.append(
172 [
173 i,
174 len(muon_event.muon_vetoPoints),
175 len(input_event.vetoPoint),
176 muoncount,
177 len(combined_vetoPoint),
178 len(muon_event.muon_UpstreamTaggerPoints),
179 len(input_event.UpstreamTaggerPoint),
180 muon_ubtcount,
181 len(combined_UpstreamTaggerPoint),
182 ]
183 )
184 output_tree.Fill()
185
186 output_tree.Write("cbmsim", r.TObject.kOverwrite)
187 temp_file.Close()
188 input_file.Close()
189 muon_file.Close()
190
191 os.remove(inputfile)
192 os.rename(temp_filename, inputfile)
193 print(f"File updated with incoming muon info as {inputfile}")
194 print(tabulate(table_data, headers=headers, tablefmt="grid"))
195
196
197muons_found = inspect_file(args.inputfile, args.muonfile)
198
199if not muons_found:
200 print("Incoming muon's vetoPoints & UpstreamTaggerPoints inf missing in file, proceeding with modification")
201 modify_file(args.inputfile, args.muonfile)
202else:
203 print("File is already updated with incoming muon info. Nothing to do.")
204 muons_found = inspect_file(args.inputfile, args.muonfile, print_table=True)
None modify_file(inputfile, muonfile)
bool inspect_file(inputfile, muonfile, print_table=False)