5"""Script to add the incoming muon's MC hits in the SBT to the simulation file."""
12from tabulate
import tabulate
14logging.basicConfig(level=logging.DEBUG)
16parser = argparse.ArgumentParser(description=__doc__)
21 help=
"Simulation file produced from DIS, WARNING: File will be modified.",
27 help=
"Muon file used for DIS production (muonDis_<>.root)",
29args = parser.parse_args()
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",
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
48 muon_file = r.TFile.Open(muonfile,
"read")
49 muon_tree = muon_file.DIS
55 for i, (muon_event, input_event)
in enumerate(zip(muon_tree, input_tree)):
56 muon_hits = len(muon_event.muon_vetoPoints)
59 for hit
in input_event.vetoPoint:
60 if hit.GetTrackID() == 0:
64 muon_ubthits = len(muon_event.muon_UpstreamTaggerPoints)
67 for hit
in input_event.UpstreamTaggerPoint:
68 if hit.GetTrackID() == 0:
75 len(input_event.vetoPoint) - muoncount,
77 len(input_event.vetoPoint),
79 len(input_event.UpstreamTaggerPoint) - muon_ubtcount,
81 len(input_event.UpstreamTaggerPoint),
86 print(tabulate(table_data, headers=headers, tablefmt=
"grid"))
95 """Add information from original muon to input simulation file."""
97 f
"vetoPoints & UpstreamTaggerPoints from the incoming muon (saved in {muonfile}) will be added to {inputfile}."
100 input_file = r.TFile.Open(inputfile,
"read")
102 input_tree = input_file.cbmsim
103 except Exception
as e:
109 muon_file = r.TFile.Open(muonfile,
"read")
111 muon_tree = muon_file.DIS
112 except Exception
as e:
117 temp_filename = inputfile.replace(
".root",
".tmp")
118 temp_file = r.TFile.Open(temp_filename,
"recreate")
119 output_tree = input_tree.CloneTree(0)
121 combined_vetoPoint = r.TClonesArray(
"vetoPoint")
122 output_tree.SetBranchAddress(
"vetoPoint", combined_vetoPoint)
124 combined_UpstreamTaggerPoint = r.TClonesArray(
"UpstreamTaggerPoint")
125 output_tree.SetBranchAddress(
"UpstreamTaggerPoint", combined_UpstreamTaggerPoint)
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)
133 combined_vetoPoint.Clear()
137 for hit
in input_event.vetoPoint:
138 if combined_vetoPoint.GetSize() == index:
139 combined_vetoPoint.Expand(index + 1)
140 combined_vetoPoint[index] = hit
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
152 combined_UpstreamTaggerPoint.Clear()
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
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
174 len(muon_event.muon_vetoPoints),
175 len(input_event.vetoPoint),
177 len(combined_vetoPoint),
178 len(muon_event.muon_UpstreamTaggerPoints),
179 len(input_event.UpstreamTaggerPoint),
181 len(combined_UpstreamTaggerPoint),
186 output_tree.Write(
"cbmsim", r.TObject.kOverwrite)
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"))
200 print(
"Incoming muon's vetoPoints & UpstreamTaggerPoints inf missing in file, proceeding with modification")
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)