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
 
argparse parser = argparse.ArgumentParser(description=__doc__)
 
 required
 
 help
 
argparse args = parser.parse_args()
 
list headers
 
bool muons_found = inspect_file(args.inputfile, args.muonfile)
 
 else :
 

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 43 of file add_muonresponse.py.

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

◆ modify_file()

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

Definition at line 94 of file add_muonresponse.py.

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

Variable Documentation

◆ args

argparse add_muonresponse.args = parser.parse_args()

Definition at line 29 of file add_muonresponse.py.

◆ else

add_muonresponse.else :

Definition at line 202 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 30 of file add_muonresponse.py.

◆ help

add_muonresponse.help

Definition at line 21 of file add_muonresponse.py.

◆ level

add_muonresponse.level

Definition at line 14 of file add_muonresponse.py.

◆ muons_found

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

Definition at line 197 of file add_muonresponse.py.

◆ parser

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

Definition at line 16 of file add_muonresponse.py.

◆ required

add_muonresponse.required

Definition at line 20 of file add_muonresponse.py.