ATLAS Offline Software
Loading...
Searching...
No Matches
createDCubeSimHistograms.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3# this script can be used to create DCube histograms from the output ntuples of NSWPRDValAlg
4
5import os, sys, ROOT, argparse
6import math
7from DCubeHistograms import MyHistoFiller
8
9if __name__ == "__main__":
10 parser = argparse.ArgumentParser(prog='createDCubeSimHistograms', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
11 parser.add_argument('-i', '--inputFile', help='choose input ROOT file', default='NSWPRDValAlg.sim.ntuple.root', type=str)
12 parser.add_argument('-o', '--outputFile', help='choose output ROOT file', default='NSWPRDValAlg.dcube.root', type=str)
13 parser.add_argument('--doCSC', help='turn off CSC if using Run4 input ROOT file', default=False, action='store_true')
14 parser.add_argument('--doMM', help='turn off MM if using Run2 input ROOT file', default=False, action='store_true')
15 parser.add_argument('--doSTGC', help='turn off STGC if using Run2 input ROOT file', default=False, action='store_true')
16 parser.add_argument('--RPCsel', help='Choose eta_sector selections for RPC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
17 parser.add_argument('--MDTsel', help='Choose eta_sector selections for MDT, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
18 parser.add_argument('--CSCsel', help='Choose eta_sector selections for CSC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
19 parser.add_argument('--TGCsel', help='Choose eta_sector selections for TGC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
20 parser.add_argument('--MMsel', help='Choose eta_sector selections for MM, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
21 parser.add_argument('--sTGCsel', help='Choose eta_sector selections for sTGC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
22 Options = parser.parse_args()
23
24 ROOT.gROOT.SetBatch(True)
25
26 if not os.path.exists(Options.inputFile):
27 print ('ERROR: File %s does not exist'%Options.inputFile)
28 sys.exit(1)
29
30 inputFile = ROOT.TFile(Options.inputFile, "READ")
31 if not inputFile:
32 print ('ERROR: Failed to open file %s'%Options.inputFile)
33 sys.exit(1)
34 inputTree = inputFile.Get("HitValidTree")
35 if not inputTree:
36 print ('ERROR: HitValidTree does not exist in file %s'%Options.inputFile)
37 sys.exit(1)
38
39 nEntries = inputTree.GetEntries()
40 if nEntries==0:
41 print ('ERROR: HitValidTree of file %s has 0 entries'%Options.inputFile)
42 sys.exit(1)
43
44 outputFile = ROOT.TFile(Options.outputFile, "RECREATE")
45 if not outputFile:
46 print ('ERROR: Failed to open file %s'%Options.outputFile)
47 sys.exit(1)
48
49 outputFile.cd()
50 outputFile.mkdir("simulation/")
51 ODir = outputFile.GetDirectory("simulation/")
52 ODir.cd()
53
54 RPCselections = Options.RPCsel.split("_")
55 RPC_eta = RPCselections[0]
56 if RPCselections[1] != "None":
57 RPC_sector = int (RPCselections[1])
58 else:
59 RPC_sector = RPCselections[1]
60
61 MDTselections = Options.MDTsel.split("_")
62 MDT_eta = MDTselections[0]
63 if MDTselections[1] != "None":
64 MDT_sector = int (MDTselections[1])
65 else:
66 MDT_sector = MDTselections[1]
67
68 CSCselections = Options.CSCsel.split("_")
69 CSC_eta = CSCselections[0]
70 if CSCselections[1] != "None":
71 CSC_sector = int (CSCselections[1])
72 else:
73 CSC_sector = CSCselections[1]
74
75 TGCselections = Options.TGCsel.split("_")
76 TGC_eta = TGCselections[0]
77 if TGCselections[1] != "None":
78 TGC_sector = int (TGCselections[1])
79 else:
80 TGC_sector = TGCselections[1]
81
82 MMselections = Options.MMsel.split("_")
83 MM_eta = MMselections[0]
84 if MMselections[1] != "None":
85 MM_sector = int (MMselections[1])
86 else:
87 MM_sector = MMselections[1]
88
89 sTGCselections = Options.sTGCsel.split("_")
90 sTGC_eta = sTGCselections[0]
91 if sTGCselections[1] != "None":
92 sTGC_sector = int (sTGCselections[1])
93 else:
94 sTGC_sector = sTGCselections[1]
95
96 # Filling
97 for i in range(inputTree.GetEntries()):
98 inputTree.GetEntry(i)
99 truthhists = []
100 rpchists = []
101 mdthists = []
102 cschists = []
103 tgchists = []
104 mmhists = []
105 stgchists = []
106
107 # truth information
108 for ntruth in range(0,len(inputTree.MuEntry_ParticlePt)):
109 truthhists += [MyHistoFiller( chamber_name = "TruthInfo", eta_sel = None, sector_sel = None )]
110 truthhists[ntruth].fill(inputTree,ntruth)
111
112
113 #RPCs
114 if RPC_eta == "positive":
115 rpc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.RPC_SIM_stationEta[nrpcHit])) >= 0
116 elif RPC_eta == "negative":
117 rpc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.RPC_SIM_stationEta[nrpcHit])) < 0
118 else:
119 rpc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.RPC_SIM_stationEta[nrpcHit])) < 9
120
121 if RPC_sector == "None":
122 rpc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.RPC_SIM_stationPhi[nrpcHit])) < 10
123 else:
124 rpc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.RPC_SIM_stationPhi[nrpcHit])) == RPC_sector
125
126 for nrpcHit in range(0,len(inputTree.RPC_SIM_localPostionX)):
127 rpchists += [MyHistoFiller( chamber_name = "RPC_Sim", eta_sel = rpc_eta_sel, sector_sel = rpc_sector_sel )]
128 rpchists[nrpcHit].fill(inputTree, nrpcHit)
129
130 # MDTs
131 if MDT_eta == "positive":
132 mdt_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.MDT_Sim_stationEta[nmdtHit])) >= 0
133 elif MDT_eta == "negative":
134 mdt_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.MDT_Sim_stationEta[nmdtHit])) < 0
135 else:
136 mdt_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.MDT_Sim_stationEta[nmdtHit])) < 9
137
138 if MDT_sector == "None":
139 mdt_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.MDT_Sim_stationPhi[nmdtHit])) < 10
140 else:
141 mdt_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.MDT_Sim_stationPhi[nmdtHit])) == MDT_sector
142
143 for nmdtHit in range(0,len(inputTree.MDT_Sim_hitLocalPositionX)):
144 mdthists += [MyHistoFiller( chamber_name = "MDT_Sim", eta_sel = mdt_eta_sel, sector_sel = mdt_sector_sel )]
145 mdthists[nmdtHit].fill(inputTree, nmdtHit)
146
147 # CSCs
148 if Options.doCSC == True:
149 if CSC_eta == "positive":
150 csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.CSC_Sim_stationEta[ncscHit])) >= 0
151 elif CSC_eta == "negative":
152 csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.CSC_Sim_stationEta[ncscHit])) < 0
153 else:
154 csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.CSC_Sim_stationEta[ncscHit])) < 9
155
156 if CSC_sector == "None":
157 csc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.CSC_Sim_stationPhi[ncscHit])) < 10
158 else:
159 csc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.CSC_Sim_stationPhi[ncscHit])) == CSC_sector
160
161 for ncscHit in range(0,len(inputTree.CSC_Sim_hitGlobalPositionX)):
162 cschists += [MyHistoFiller( chamber_name = "CSC_Sim", eta_sel = csc_eta_sel, sector_sel = csc_sector_sel )]
163 cschists[ncscHit].fill(inputTree, ncscHit)
164
165 # TGCs
166 if TGC_eta == "positive":
167 tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.TGC_Sim_stationEta[ntgcHit])) >= 0
168 elif TGC_eta == "negative":
169 tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.TGC_Sim_stationEta[ntgcHit])) < 0
170 else:
171 tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.TGC_Sim_stationEta[ntgcHit])) < 9
172
173 if TGC_sector == "None":
174 tgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.TGC_Sim_stationPhi[ntgcHit])) < 51
175 else:
176 tgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.TGC_Sim_stationPhi[ntgcHit])) == TGC_sector
177
178 for ntgcHit in range(0,len(inputTree.TGC_Sim_hitLocalPositionX)):
179 tgchists += [MyHistoFiller( chamber_name = "TGC_Sim", eta_sel = tgc_eta_sel, sector_sel = tgc_sector_sel )]
180 tgchists[ntgcHit].fill(inputTree, ntgcHit)
181
182 # MMs
183 if Options.doMM == True:
184 if MM_eta == "positive":
185 mm_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_MM_off_stationEta[nmmHit])) >= 0
186 elif MM_eta == "negative":
187 mm_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_MM_off_stationEta[nmmHit])) < 0
188 else:
189 mm_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_MM_off_stationEta[nmmHit])) < 9
190
191 if MM_sector == "None":
192 mm_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.Hits_MM_off_stationPhi[nmmHit])) < 10
193 else:
194 mm_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.Hits_MM_off_stationPhi[nmmHit])) == MM_sector
195
196 for nmmHit in range(0,len(inputTree.Hits_MM_GlobalPositionX)):
197 mmhists += [MyHistoFiller( chamber_name = "MM_Sim", eta_sel = mm_eta_sel, sector_sel = mm_sector_sel )]
198 mmhists[nmmHit].fill(inputTree, nmmHit)
199
200 # sTGCs
201 if Options.doSTGC == True:
202 if sTGC_eta == "positive":
203 stgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_sTGC_off_stationEta[nstgcHit])) >= 0
204 elif sTGC_eta == "negative":
205 stgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_sTGC_off_stationEta[nstgcHit])) < 0
206 else:
207 stgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.Hits_sTGC_off_stationEta[nstgcHit])) < 9
208
209 if sTGC_sector == "None":
210 stgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.Hits_sTGC_off_stationPhi[nstgcHit])) < 10
211 else:
212 stgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.Hits_sTGC_off_stationPhi[nstgcHit])) == sTGC_sector
213
214 for nstgcHit in range(0,len(inputTree.Hits_sTGC_GlobalPositionX)):
215 stgchists += [MyHistoFiller( chamber_name = "sTGC_Sim", eta_sel = stgc_eta_sel, sector_sel = stgc_sector_sel )]
216 stgchists[nstgcHit].fill(inputTree, nstgcHit)
217
218
219 # Writing
220 truthhist = MyHistoFiller( chamber_name = "TruthInfo", eta_sel = None, sector_sel = None )
221 truthhist.write(ODir)
222
223 rpchist = MyHistoFiller( chamber_name = "RPC_Sim", eta_sel = None, sector_sel = None )
224 rpchist.write(ODir)
225
226 mdthist = MyHistoFiller( chamber_name = "MDT_Sim", eta_sel = None, sector_sel = None )
227 mdthist.write(ODir)
228
229 if Options.doCSC == True:
230 cschist = MyHistoFiller( chamber_name = "CSC_Sim", eta_sel = None, sector_sel = None )
231 cschist.write(ODir)
232
233 tgchist = MyHistoFiller( chamber_name = "TGC_Sim", eta_sel = None, sector_sel = None )
234 tgchist.write(ODir)
235
236 if Options.doMM == True:
237 mmhist = MyHistoFiller( chamber_name = "MM_Sim", eta_sel = None, sector_sel = None )
238 mmhist.write(ODir)
239
240 if Options.doSTGC == True:
241 stgchist = MyHistoFiller( chamber_name = "sTGC_Sim", eta_sel = None, sector_sel = None )
242 stgchist.write(ODir)
void fill(H5::Group &out_file, size_t iterations)