ATLAS Offline Software
AnalysisApp.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 #
4 # The AnalysisApp module assists with the creation of analysis-type applications for athena
5 # Here is a skeleton application (script) that uses AnalysisApp:
6 #
7 # if __name__=="__main__":
8 # from AthenaConfiguration import AnalysisApp
9 # from AthenaConfiguration.ComponentFactory import CompFactory
10 # flags = AnalysisApp.initFlags() # creates a flag container (AthConfigFlags) appropriate for analysis uses
11 # flags.addFlag("MyPackage.myArg", "whatever", help="A demo flag") # user flag example
12 # flags.parser().add_argument('--someArg',default="whatever") # user arg example
13 # ca = AnalysisApp.initCfg(flags) # locks the flags, which triggers arg parsing
14 # ca.addEventAlgo( CompFactory.MyPackageAlg(MyProperty = flags.MyPackage.myArg), sequenceName="AthAlgSeq" ) # example alg configuring
15 # AnalysisApp.launch(flags,ca) # runs the job
16 #
17 # If this code is located in MyPackage/python/MyModule.py you can run it with:
18 # python -m MyPackage.MyModule [options] [flags]
19 # Pass the "--help" option to see available options (both pre-defined and user-defined)
20 
21 
22 def initFlags():
23  """
24  Creates a flag container (AthConfigFlags) appropriate for typical
25  Analysis athena applications.
26  :return: flags
27  """
28  from AthenaConfiguration import AthConfigFlags
29  from AthenaConfiguration.AutoConfigFlags import GetFileMD
30  from Campaigns.Utils import Campaign
32 
33  #Flags steering the job execution:
34  from AthenaCommon.Constants import INFO
35  from AthenaConfiguration.Enums import ProductionStep
36  import argparse
37  acf.addFlag('Exec.OutputLevel',INFO) #Global Output Level
38  acf.addFlag('Exec.MaxEvents',-1)
39  acf.addFlag('Exec.SkipEvents',0)
40  acf.addFlag('Exec.DebugStage','', help=argparse.SUPPRESS)
41  acf.addFlag('Exec.FPE',-2) #-2: No FPE check at all, -1: Abort with core-dump, 0: FPE Auditor w/o stack-tace (default) , >0: number of stack-trace printed by the job
42 
43 
44  #Custom messaging for components, see Utils.setupLoggingLevels
45  acf.addFlag('Exec.VerboseMessageComponents',[])
46  acf.addFlag('Exec.DebugMessageComponents',[])
47  acf.addFlag('Exec.InfoMessageComponents',[])
48  acf.addFlag('Exec.WarningMessageComponents',[])
49  acf.addFlag('Exec.ErrorMessageComponents',[])
50 
51  acf.addFlag('Common.MsgSuppression',True) # Enable suppression of printout in MessageSvc
52  acf.addFlag('Common.MsgSourceLength',50) #Length of the source-field in the format str of MessageSvc
53  acf.addFlag('Common.ProductionStep', ProductionStep.Default, type=ProductionStep, help=argparse.SUPPRESS)
54  acf.addFlag('Common.isOverlay', False, help=argparse.SUPPRESS)
55 
56  #Flags describing the input data
57  acf.addFlag('Input.Files', ["_ATHENA_GENERIC_INPUTFILE_NAME_",]) # former global.InputFiles
58  acf.addFlag('Input.OverrideRunNumber', False, help=argparse.SUPPRESS )
59  acf.addFlag('Input.SecondaryFiles', [], help=argparse.SUPPRESS) # secondary input files for DoubleEventSelector
60  acf.addFlag('Input.ProcessingTags', lambda prevFlags : GetFileMD(prevFlags.Input.Files).get("processingTags", []), help="expert flag, do not override" ) # list of names of streams written to this file
61  acf.addFlag('Input.ProjectName', lambda prevFlags : GetFileMD(prevFlags.Input.Files).get("project_name", "data17_13TeV"), help="expert flag, do not override") # former global.ProjectName
62  acf.addFlag('Input.MCCampaign', lambda prevFlags : Campaign(GetFileMD(prevFlags.Input.Files).get("mc_campaign", "")), type=Campaign, help="expert flag, do not override")
63 
64 
65  acf.addFlag('Concurrency.NumProcs', 0, help="0 = disables MP, otherwise is # of processes to use in MP mode")
66  acf.addFlag('Concurrency.NumThreads', 0, help="0 = disables MT, otherwise is # of threads to use in MT mode" )
67  acf.addFlag('Concurrency.NumConcurrentEvents', lambda prevFlags : prevFlags.Concurrency.NumThreads)
68  acf.addFlag('Concurrency.DebugWorkers', False )
69 
70  # output
71  acf.addFlag('Output.HISTOutputs', [],help="ROOT output files. Specify in form of 'STREAM:filename.root'")
72  acf.addFlag('Output.TreeAutoFlush', {}, help="{} = automatic for all streams, otherwise {'STREAM': 123}")
73 
74  acf.addFlag("PoolSvc.MaxFilesOpen", 0, help=argparse.SUPPRESS)
75 
76  # analysis-specific arguments
77  acf.parser().add_argument('--accessMode',default="POOLAccess",choices={"POOLAccess","ClassAccess"},help="Input file reading mode") # named arg
78  acf.parser().add_argument('--postExec',default=None,help="Any postconfig execution required")
79 
80 
81  return acf
82 
83 def initCfg(flags):
84  """
85  Creates a ComponentAccumulator appropriate for typical Analysis
86  type jobs.
87  :param flags:
88  :return:
89  """
90  flags.lock()
91  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
92  from AthenaConfiguration.ComponentFactory import CompFactory
93 
94  ca = MainServicesCfg(flags)
95 
96  if flags.args().accessMode == "POOLAccess":
97  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
98  ca.merge(PoolReadCfg(flags))
99  else:
100  from AthenaRootComps.xAODEventSelectorConfig import xAODReadCfg,xAODAccessMode
101  ca.merge(xAODReadCfg(flags, AccessMode = xAODAccessMode.CLASS_ACCESS))
102 
103  outputs = ["{} DATAFILE='{}' OPT='RECREATE'".format(*file.split(":",1)) for file in flags.Output.HISTOutputs]
104  if len(outputs): ca.addService(CompFactory.THistSvc(Output = outputs))
105 
106  ca.getService("MessageSvc").setWarning += ["ClassIDSvc","PoolSvc","AthDictLoaderSvc","AthenaPoolAddressProviderSvc",
107  "ProxyProviderSvc","DBReplicaSvc","MetaDataSvc","MetaDataStore","AthenaPoolCnvSvc",
108  "TagMetaDataStore","EventSelector",
109  #"ApplicationMgr", can't silence because otherwise ATN tests fail, see ATLINFR-1235
110  "CoreDumpSvc","AthMasterSeq","EventPersistencySvc","ActiveStoreSvc",
111  "AthenaEventLoopMgr","AthOutSeq","AthRegSeq"]
112 
113  return ca
114 
115 def launch(flags,ca):
116  """
117  Launches the job (includes executing any postExec)
118  :param flags:
119  :param ca:
120  :return:
121  """
122  from AthenaConfiguration.Utils import setupLoggingLevels
123  setupLoggingLevels(flags,ca)
124  if flags.args().postExec: eval(flags.args().postExec)
125  if ca.run().isFailure():
126  import sys
127  sys.exit(1)
128 
vtune_athena.format
format
Definition: vtune_athena.py:14
python.AnalysisApp.initFlags
def initFlags()
Definition: AnalysisApp.py:22
python.AnalysisApp.initCfg
def initCfg(flags)
Definition: AnalysisApp.py:83
python.AutoConfigFlags.GetFileMD
def GetFileMD(filenames, allowEmpty=True)
Definition: AutoConfigFlags.py:51
python.AthConfigFlags.AthConfigFlags
Definition: AthConfigFlags.py:219
python.Utils.setupLoggingLevels
def setupLoggingLevels(flags, ca)
Definition: Control/AthenaConfiguration/python/Utils.py:46
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:256
Constants
some useful constants -------------------------------------------------—
python.xAODEventSelectorConfig.xAODReadCfg
def xAODReadCfg(flags, AccessMode=xAODAccessMode.CLASS_ACCESS)
Definition: xAODEventSelectorConfig.py:35
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
python.AnalysisApp.launch
def launch(flags, ca)
Definition: AnalysisApp.py:115