ATLAS Offline Software
Loading...
Searching...
No Matches
dumpTruth.py
Go to the documentation of this file.
1#!/usr/bin/env python
2#
3# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4#
5
6'''@file dumpTruth.py
7@author RD Schaffer
8@date 2023-08-04
9@brief Script to print out truth events for HepMC (HITS, RDO) or TruthEvent (AOD)
10'''
11
12if __name__=='__main__':
13 # from AthenaCommon.Constants import INFO
14 from argparse import RawTextHelpFormatter
15 from AthenaConfiguration.AllConfigFlags import initConfigFlags
16 flags = initConfigFlags()
17 parser = flags.getArgumentParser(description='Run ASCII dumper of Truth on files with either HepMC or TruthEvent format.\
18 User MUST provide file(s) to read (--filesInput).\
19 Also, use --doPtEtaPhi for pt/eta/phi printout, and --skipEvents, --evtMax to select events.')
20 parser.add_argument('--doPUEventPrintout', action='store_true',
21 help='Print out PU event for xAODTruthReader')
22 parser.add_argument('--doPtEtaPhi', action='store_true',
23 help='Print out particle 4-mom as pt,eta,phi. Default is px,py,pz.')
24 parser.set_defaults(threads=1)
25 args, _ = parser.parse_known_args()
26
27 from AthenaCommon.Logging import log
28
29 flags.Input.Files = args.filesInput
30 log.info("Checking collections in the input file")
31 collTypes = [ c.split("#")[0] for c in flags.Input.TypedCollections]
32 MCCollectionName = None
33 TruthCollectionName = None
34 getName = lambda t: [ c.split("#")[1] for c in flags.Input.TypedCollections if c.split("#")[0] == t][0]
35
36 if "McEventCollection" in collTypes:
37 MCCollectionName = getName("McEventCollection")
38 log.info("Found McEventCollection#%s in the input, assuming it is HepMC", MCCollectionName)
39 elif "xAOD::TruthEventContainer" in collTypes:
40 TruthCollectionName = getName("xAOD::TruthEventContainer")
41 log.info("Found xAOD::TruthEventContainer#%s in the input", TruthCollectionName)
42 else:
43 log.error("Neither McEventCollection or xAOD::TruthEventContainer in the input file, can not dump truth")
44 import sys
45 sys.exit(1)
46
47
48 # set up defaults for either HepMCReader or xAODTruthReader
49 if MCCollectionName:
50 flags.addFlag("HepMCContainerKey", MCCollectionName)
51 else:
52 flags.addFlag("xAODTruthEventContainerKey", "TruthEvents")
53 flags.addFlag("xAODTruthPileupEventContainerKey", "TruthPileupEvents")
54 if args.doPUEventPrintout:
55 flags.addFlag("DoPUEventPrintout", True)
56 else:
57 flags.addFlag("DoPUEventPrintout", False)
58
59 # default file name
60 flags.Input.Files = ['truth.pool.root']
61 if args.doPtEtaPhi:
62 flags.addFlag("Do4momPtEtaPhi", True)
63 else:
64 flags.addFlag("Do4momPtEtaPhi", False)
65
66 flags.fillFromArgs(parser=parser)
67 flags.lock()
68
69 from AthenaConfiguration.MainServicesConfig import MainServicesCfg
70 from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
71 acc=MainServicesCfg(flags)
72 acc.merge(PoolReadCfg(flags))
73 if MCCollectionName:
74 from xAODTruthCnv.xAODTruthCnvConfig import HepMCTruthReaderCfg
75 acc.merge(HepMCTruthReaderCfg(flags))
76 else:
77 from xAODTruthCnv.xAODTruthCnvConfig import xAODTruthReaderCfg
78 acc.merge(xAODTruthReaderCfg(flags))
79
80 acc.store(open("HepMCTruthReader.pkl","wb"))
81
82 from AthenaConfiguration.Utils import setupLoggingLevels
83 setupLoggingLevels(flags, acc)
84
85 statusCode = acc.run()
86 assert statusCode.isSuccess() is True, "Application execution did not succeed"