4 from __future__
import print_function
12 if evt.contains(typ, key):
13 return evt.retrieve(typ, key)
14 print(f
'WARNING: Cannot find object {typ}/{key}')
21 for i
in range(0, evt.getEntries()):
22 if (counter
and (i % 100) == 0):
23 print(
"Processing event %s" % i)
26 ei = evt.retrieve(
"xAOD::EventInfo",
"EventInfo")
27 runnbr = ei.runNumber()
28 evtnbr = ei.eventNumber()
31 evt,
"xAOD::CaloClusterContainer",
"CaloCalTopoClusters")
35 "xAOD::TrackParticleContainer",
"InDetTrackParticles")
36 nIdTracks = len(idTracks)
38 tautracks =
safeRetrieve(evt,
"xAOD::TauTrackContainer",
"TauTracks")
39 nTauTracks = len(tautracks)
40 taus =
safeRetrieve(evt,
"xAOD::TauJetContainer",
"TauJets")
44 tau1eta = taus[0].eta()
45 tau1phi = taus[0].phi()
47 tau1pt = tau1eta = tau1phi = 0
49 muons =
safeRetrieve(evt,
"xAOD::MuonContainer",
"Muons")
52 muon1pt = muons[0].
pt()
53 muon1eta = muons[0].eta()
54 muon1phi = muons[0].phi()
56 muon1pt = muon1eta = muon1phi = 0
58 electrons =
safeRetrieve(evt,
"xAOD::ElectronContainer",
"Electrons")
59 nElec = len(electrons)
61 elec1pt = electrons[0].
pt()
62 elec1eta = electrons[0].eta()
63 elec1phi = electrons[0].phi()
65 elec1pt = elec1eta = elec1phi = 0
67 photons =
safeRetrieve(evt,
"xAOD::PhotonContainer",
"Photons")
70 phot1pt = photons[0].
pt()
71 phot1eta = photons[0].eta()
72 phot1phi = photons[0].phi()
74 phot1pt = phot1eta = phot1phi = 0
77 jets =
safeRetrieve(evt,
"xAOD::JetContainer",
"AntiKt4EMPFlowJets")
81 jet1eta = jets[0].eta()
82 jet1phi = jets[0].phi()
84 jet1pt = jet1eta = jet1phi = 0
86 met =
safeRetrieve(evt,
"xAOD::MissingETContainer",
"MET_Reference_AntiKt4EMPFlow")
89 metx = met[nmet-1].mpx()
90 mety = met[nmet-1].mpy()
91 sumet = met[nmet-1].sumet()
93 metx = mety = sumet = 0
97 acc = ROOT.SG.ConstAccessor(
98 'ElementLink< xAOD::TruthParticleContainer>')(
'truthParticleLink')
100 if nElec > 0
and acc.isAvailable(electrons.at(0)):
101 for i
in range(nElec):
102 truthLink =
acc(electrons.at(i))
103 if(truthLink.isValid()):
104 pdgId = truthLink.pdgId()
108 if nPhot > 0
and acc.isAvailable(photons.at(0)):
109 for i
in range(nPhot):
110 truthLink =
acc(photons.at(i))
111 if(truthLink.isValid()):
112 pdgId = truthLink.pdgId()
116 nFakeElectrons = nElec - nTrueElectrons
117 nFakePhotons = nPhot - nTruePhotons
120 result.append((runnbr, evtnbr, nclus, nIdTracks,
121 nTauTracks, nTaus, tau1pt, tau1eta, tau1phi,
122 nMuons, muon1pt, muon1eta, muon1phi,
123 nElec, elec1pt, elec1eta, elec1phi, nTrueElectrons, nFakeElectrons,
124 nPhot, phot1pt, phot1eta, phot1phi ,nTruePhotons, nFakePhotons,
125 nJet, jet1pt, jet1eta, jet1phi, nmet, metx, mety, sumet))
127 result.append((runnbr, evtnbr, nclus, nIdTracks, nTauTracks, nTaus, nMuons,
128 nElec, nTrueElectrons, nFakeElectrons,
129 nPhot, nTruePhotons, nFakePhotons))
134 result.sort(key=
lambda er: er[0] << 32 | er[1])
139 parser = argparse.ArgumentParser(
140 description=
"Extracts a few basic quantities from the xAOD file and dumps them into a text file")
141 parser.add_argument(
"xAODFile", nargs=
'?', type=str,
142 help=
"xAOD filename", action=
"store")
143 parser.add_argument(
"outfilename", nargs=
'?',
144 help=
"output text file for results", action=
"store", default=
None)
146 "--outputfilename", help=
"output text file for results", action=
"store", default=
None)
147 parser.add_argument(
"--extravars", help=
"Extract extra variables: pt/eta/phi",
148 action=
"store_true", default=
False)
149 parser.add_argument(
"--counter", help=
"Print event counter mod 100",
150 action=
"store_true", default=
False)
151 parser.add_argument(
"--inputlist", help=
"Optional list of xAOD file instead of xAODFile parameter",
152 nargs=
'+', action=
"store", default=
False)
153 parser.add_argument(
"--inputisESD", help=
"Set if input is ESD",
154 action=
"store_true", default=
False)
156 args = parser.parse_args()
158 if len(sys.argv) < 2:
163 if not args.inputlist
and not os.access(args.xAODFile, os.R_OK):
164 print(
"ERROR, can't access file {}".
format(args.xAODFile))
170 outfilename = args.outfilename
171 elif args.outputfilename:
172 outfilename = args.outputfilename
175 print(
"Writing to file ", outfilename)
179 filelist = ROOT.TChain()
180 for filename
in args.inputlist:
181 filelist.AddFile(filename)
183 filelist = args.xAODFile
186 evt = ROOT.POOL.TEvent(
187 ROOT.POOL.TEvent.kPOOLAccess
if args.inputisESD
else ROOT.POOL.TEvent.kClassAccess)
188 stat = evt.readFrom(filelist)
190 print(
"ERROR, failed to open file {} with POOL.TEvent".
format(
195 digest =
xAODDigest(evt, args.counter, args.extravars)
198 outstr =
open(outfilename,
"w")
203 header = (
"run",
"event",
"nTopo",
"nIdTracks",
204 "nTauTracks",
"nTaus",
"tau1pt",
"tau1eta",
"tau1phi",
205 "nMuons",
"muon1pt",
"muon1eta",
"muon1phi",
206 "nElec",
"elec1pt",
"elec1eta",
"elec1phi",
"nTrueElec",
"nFakeElec",
207 "nPhot",
"phot1pt",
"phot1eta",
"phot1phi",
"nTruePhot",
"nFakePhot",
208 "nJet",
"jet1pt",
"jet1eta",
"jet1phi",
"nmet",
"metx",
"mety",
"sumet" )
209 row_format_header =
"{:>20}" * len(header)
210 row_format_header += os.linesep
211 row_format_data =
"{:d} {:d} " +
"{:20.4f}" * (len(header)-2)
212 row_format_data += os.linesep
214 header = (
"run",
"event",
"nTopo",
"nIdTracks",
"nTauTracks",
"nTaus",
"nMuons",
215 "nElec",
"nTrueElec",
"nFakeElec",
216 "nPhot",
"nTruePhot",
"nFakePhot")
217 row_format_header =
"{:>12}" * len(header)
218 row_format_header += os.linesep
219 row_format_data =
"{:>12}" * len(header)
220 row_format_data += os.linesep
222 outstr.write(row_format_header.format(*header))
224 outstr.write(row_format_data.format(*evt))
232 if __name__ ==
"__main__":