14 from AthenaCommon.Logging import logging
15 log = logging.getLogger('plugin_miniSim')
16
17
18 flags.DQ.doMonitoring=False
19 flags.Trigger.enableL1CaloPhase1=any([flags.Trigger.L1.doeFex,flags.Trigger.L1.dojFex,flags.Trigger.L1.dogFex])
20
21 flags.addFlag("GlobalSim.Algs",[])
22 flags.addFlag("GlobalSim.txtInputs",[])
23 flags.addFlag("GlobalSim.txtOutputs",[])
24 flags.addFlag("GlobalSim.poolOutputs",[])
25
26 from AthenaConfiguration.ComponentFactory import CompFactory
27 excludeAlgs = ["LArCellPreparationAlg", "GlobalSimulationAlg", "PU1SuppTestBenchAlg", "LArCellMuxAlg", "Egamma1_OnlineMapNbhoodAlg"]
28 availableAlgs = [f[:-3] for f in CompFactory.GlobalSim._getEntries()[-1] if f.endswith('Alg') and f not in excludeAlgs]
29
30
31 if flags.hasFlag("L1CaloAthMon.UnknownArgs"):
32
33 import argparse
34 parser = argparse.ArgumentParser(prog='l1global-minisim',formatter_class=argparse.RawTextHelpFormatter)
35 parser.add_argument('--presim',nargs='+',default=[],choices=["efex","jfex","gfex","muon"],help="What to pre-simulate")
36 parser.add_argument('--algs',nargs='*',default=[],help=f"algs to run. Available are: {', '.join(availableAlgs)}")
37 parser.add_argument('--filesInput',nargs='*',default=[],help="inputs. If nibbler, specify as <key>:<path>. <key> can be storegate key or algorithm input property (<alg>.<input>)")
38 parser.add_argument('--filesOutput',nargs='*',default=[],help="outputs to save, specify similarly to inputs")
39 if "--help" in flags.L1CaloAthMon.UnknownArgs:
40
41 epilog = "Availble Algorithm Parameters:\n"
42 from GaudiKernel.DataHandle import DataHandle
43 from AthenaCommon import CfgMgr
44 for algType in availableAlgs:
45 epilog += f" {algType}:\n"
46 propDocs = getattr(CfgMgr,f"GlobalSim__{algType}Alg")._propertyDocDct
47 for propName,propVal in getattr(CompFactory.GlobalSim,algType+"Alg").getDefaultProperties().items():
48
49 description = propDocs[propName]
50 if f"[GlobalSim::{algType}Alg]" not in description: continue
51 description = description.replace(f"[GlobalSim::{algType}Alg]","")
52 epilog += f" .{propName}: {description}\n"
53 parser.epilog = epilog + "\n"
54 args,unknown = parser.parse_known_args(flags.L1CaloAthMon.UnknownArgs)
55 flags.L1CaloAthMon.UnknownArgs = unknown
56 flags.GlobalSim.Algs = args.algs
57 flags.GlobalSim.txtOutputs = args.filesOutput
58
59 if "efex" in args.presim: flags.Trigger.L1.doeFex=True
60 if "jfex" in args.presim: flags.Trigger.L1.dojFex=True
61 if "gfex" in args.presim: flags.Trigger.L1.dogFex=True
62 if "muon" in args.presim: flags.Trigger.L1.doMuon=True
63 flags.Trigger.enableL1CaloPhase1=any([flags.Trigger.L1.doeFex,flags.Trigger.L1.dojFex,flags.Trigger.L1.dogFex])
64
65
68 inputsMap = {}
69 outputsMap = {}
70 knownTypes = {}
71 for alg in flags.GlobalSim.Algs:
72
73 algType,algName = (alg.split("/") if "/" in alg else [alg,alg])
74 if not hasattr(CompFactory.GlobalSim,algType+"Alg"):
75 log.fatal(f"Unknown Alg: {algType}")
76 log.fatal(f"Available Algs: {availableAlgs}")
77 exit(-1)
78
79 from GaudiKernel.DataHandle import DataHandle
80 from AthenaCommon import CfgMgr
81 propDocs = getattr(CfgMgr,f"GlobalSim__{algType}Alg")._propertyDocDct
82 for propName,propVal in getattr(CompFactory.GlobalSim,algType+"Alg").getDefaultProperties().items():
83 if not isinstance(propVal,DataHandle): continue
84 sgKey = propVal.Path.split('+')[-1]
85
86 for item
in propDocs[propName].
split(
";"):
87 item = item.strip()
88 if item.startswith("type="):
89 knownTypes[sgKey] = item[len("type="):]
90 knownTypes[f"{algName}.{propName}"] = item[len("type="):]
91 break
92 if propVal.Mode == 'W':
93 algOutputs.add(sgKey)
94 if sgKey not in outputsMap: outputsMap[sgKey] = []
95 outputsMap[sgKey] += [f"{algName}.{propName}"]
96 else:
97 algInputs.add(sgKey)
98 if sgKey not in inputsMap: inputsMap[sgKey] = []
99 inputsMap[sgKey] += [f"{algName}.{propName}"]
100
101
102
103 algInputs -= algOutputs
104
105 log.info(f"Your algorithms {flags.GlobalSim.Algs} require the following inputs: {[i for a in algInputs for i in inputsMap[a]]}")
106 log.info(f"and produces the following outputs: {[i for a in algOutputs for i in outputsMap[a]]}")
107
108
109 cleanList = []
110 for f in flags.Input.Files:
111 if f.endswith(".txt"):
112
113 if f.count(":") != 2:
114 if f.count(":")==1:
115 if f.split(":")[0] in knownTypes:
116 f = knownTypes[f.split(":")[0]] + ":" + f
117 else:
118 log.fatal(f"{f.split(':')[0]} has no known type. Add to algorithm metadata or otherwise specify it explicitly with '<type>:' prefix")
119 exit(-1)
120 else:
121 log.fatal("txt input must be specified in form <type>:<SGkey>:<filepath>")
122 exit(-1)
123 flags.GlobalSim.txtInputs += [f]
124 else:
125 cleanList += [f]
126 flags.Input.Files = cleanList
127
128 if len(flags.GlobalSim.txtInputs):
129 flags.Input.isMC = True
130
131 if flags.Exec.MaxEvents==-1:
132
133 maxVals = {}
134 for f in flags.GlobalSim.txtInputs:
135 typeAndName,path = f.rsplit(":",1)
136 if typeAndName not in maxVals: maxVals[typeAndName]=0
137 maxVals[typeAndName] += sum(1 for _ in open(path))
138 flags.Exec.MaxEvents =
max(maxVals.values())
139
140 log.info(f"Processing {flags.Exec.MaxEvents} events from txt filesInput")
141
142 txtOutputs = list(flags.GlobalSim.txtOutputs)
143 flags.GlobalSim.txtOutputs = []
144 filenameMap = {}
145 for f in txtOutputs:
146 if f.endswith(".txt"):
147
148 if f.count(":") != 2:
149 if f.count(":")==1:
150 if f.split(":")[0] in knownTypes:
151 f = knownTypes[f.split(":")[0]] + ":" + f
152 else:
153 log.fatal(f"{f.split(':')[0]} has no known type. Add to algorithm metadata or otherwise specify it explicitly with '<type>:' prefix")
154 exit(-1)
155 else:
156 log.fatal("txt output must be specified in form <type>:<SGkey>:<filepath>")
157 exit(-1)
158 flags.GlobalSim.txtOutputs += [f]
159 elif f.endswith(".pool.root"):
160
161 filename = f.split(":")[-1]
162 if filename not in filenameMap:
163 filenameMap[filename] = f"GSIM{len(filenameMap)}"
164 flags.addFlag("Output."+filenameMap[filename]+"FileName",filename)
165 flags.GlobalSim.poolOutputs += [f.replace(filename,filenameMap[filename])]
166
167
168
bool setup(asg::AnaToolHandle< Interface > &tool, const std::string &type, const std::vector< std::string > &config, const std::string &progressFile="")
mostly useful for athena, which will otherwise re-use the previous tool
std::vector< std::string > split(const std::string &s, const std::string &t=":")