Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
EventLoopCPRunScript.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 from AnalysisAlgorithmsConfig.CPBaseRunner import CPBaseRunner
3 from AnalysisAlgorithmsConfig.ConfigAccumulator import ConfigAccumulator
4 
5 class EventLoopCPRunScript(CPBaseRunner):
6  def __init__(self):
7  super().__init__()
8  self.logger.info("EventLoopCPRunScript initialized")
9  self.addCustomArguments()
10  # Avoid putting call to parse_args() here! Otherwise it is hard to retrieve the parser infos
11 
12  def addCustomArguments(self):
13  # add arguments here
14  derivedGroup = self.parser.add_argument_group('EventLoop specific arguments')
15  derivedGroup.add_argument('--direct-driver', dest='direct_driver',
16  action='store_true', help='Run the job with the direct driver')
17  derivedGroup.add_argument('--strip', dest='strip', action='store_true', help='Move the analysis root file to the top level, and delete the work directory.'
18  ' Mainly useful for standardizing the output with the Athena framework.')
19  derivedGroup.add_argument('--work-dir', dest='work_dir', default='workDir', help='The work directory for the EL job')
20  return
21 
22  def makeAlgSequence(self):
23  from AnaAlgorithm.AlgSequence import AlgSequence
24  algSeq = AlgSequence()
25  self.logger.info("Configuring algorithms based on YAML file")
26  configSeq = self.config.configure()
27  self.logger.info("Configuring common services")
28  configAccumulator = ConfigAccumulator(autoconfigFromFlags=self.flags,
29  algSeq=algSeq,
30  noSystematics=self.args.no_systematics)
31  self.logger.info("Configuring algorithms")
32  configSeq.fullConfigure(configAccumulator)
33  return algSeq
34 
35  def readSamples(self):
36  import ROOT
37  self.sampleHandler = ROOT.SH.SampleHandler()
38  sampleFiles = ROOT.SH.SampleLocal(f"{self.args.output_name}")
39  self.logger.info("Adding files to the sample handler")
40  for file in self.inputList:
41  sampleFiles.add(file)
42  self.sampleHandler.add(sampleFiles)
43 
44  def stripPath(self):
45  import os
46  import shutil
47  self.logger.info("Moving the analysis root file to the top level, and deleting the work directory. (--strip option)")
48  workDir = os.path.realpath(self.args.work_dir)
49  rootfilePath = os.path.realpath(os.path.join(workDir, 'data-ANALYSIS', f'{self.args.output_name}.root'))
50  currentDir = os.getcwd()
51  shutil.move(rootfilePath, os.path.join(currentDir, f"{self.args.output_name}.root"))
52  shutil.rmtree(workDir)
53  os.remove(os.path.join(currentDir, self.args.work_dir))
54 
55  def driverSubmit(self, driver):
56  '''
57  Important if you want to run code after submitting the job, with external driver e.g., ExecDriver.
58  Assistant function to call driver submit. Move the submission to a child process to avoid the main process being terminated.
59  Directly calling external driver submission will not return controls to the main process, the main thread will be terminated.
60  '''
61  import os
62  if (pid := os.fork()) == 0: # child process
63  driver.submit(self.job, self.args.work_dir)
64  exit(0)
65  else:
66  os.waitpid(pid, 0) # parent waits for child process to finish
67  return
68 
69  def run(self):
70  self.setup()
71  # importing ROOT has a long upfront time, so we do it here
72  import ROOT
73  ROOT.xAOD.Init().ignore()
74  self.readSamples()
75  self.flags.lock()
76  self.printFlags()
77 
78  self.job = ROOT.EL.Job()
80  self.job.options().setDouble(ROOT.EL.Job.optMaxEvents, self.flags.Exec.MaxEvents)
81  self.job.options().setString(ROOT.EL.Job.optSubmitDirMode, 'unique-link')
82  for alg in self.makeAlgSequence():
83  self.job.algsAdd(alg)
84  self.job.outputAdd(ROOT.EL.OutputStream('ANALYSIS'))
85 
86  driver = ROOT.EL.DirectDriver() if self.args.direct_driver else ROOT.EL.ExecDriver()
87  self.driverSubmit(driver)
88  if self.args.strip: self.stripPath()
grepfile.info
info
Definition: grepfile.py:38
python.EventLoopCPRunScript.EventLoopCPRunScript.__init__
def __init__(self)
Definition: EventLoopCPRunScript.py:6
configure
bool configure(asg::AnaToolHandle< ITrigGlobalEfficiencyCorrectionTool > &tool, ToolHandleArray< IAsgElectronEfficiencyCorrectionTool > &electronEffToolsHandles, ToolHandleArray< IAsgElectronEfficiencyCorrectionTool > &electronSFToolsHandles, ToolHandleArray< CP::IMuonTriggerScaleFactors > &muonToolsHandles, ToolHandleArray< IAsgPhotonEfficiencyCorrectionTool > &photonEffToolsHandles, ToolHandleArray< IAsgPhotonEfficiencyCorrectionTool > &photonSFToolsHandles, const std::string &triggers, const std::map< std::string, std::string > &legsPerTool, unsigned long nToys, bool debug)
Definition: TrigGlobEffCorrValidation.cxx:514
python.AlgSequence.AlgSequence
AlgSequence
Definition: PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py:7
python.EventLoopCPRunScript.EventLoopCPRunScript.stripPath
def stripPath(self)
Definition: EventLoopCPRunScript.py:44
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:58
python.EventLoopCPRunScript.EventLoopCPRunScript.readSamples
def readSamples(self)
Definition: EventLoopCPRunScript.py:35
python.EventLoopCPRunScript.EventLoopCPRunScript.driverSubmit
def driverSubmit(self, driver)
Definition: EventLoopCPRunScript.py:55
add
bool add(const std::string &hname, TKey *tobj)
Definition: fastadd.cxx:55
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
python.EventLoopCPRunScript.EventLoopCPRunScript.run
def run(self)
Definition: EventLoopCPRunScript.py:69
calibdata.exit
exit
Definition: calibdata.py:236
python.EventLoopCPRunScript.EventLoopCPRunScript
Definition: EventLoopCPRunScript.py:5
python.EventLoopCPRunScript.EventLoopCPRunScript.sampleHandler
sampleHandler
Definition: EventLoopCPRunScript.py:37
python.EventLoopCPRunScript.EventLoopCPRunScript.makeAlgSequence
def makeAlgSequence(self)
Definition: EventLoopCPRunScript.py:22
python.EventLoopCPRunScript.EventLoopCPRunScript.addCustomArguments
def addCustomArguments(self)
Definition: EventLoopCPRunScript.py:12
python.EventLoopCPRunScript.EventLoopCPRunScript.job
job
Definition: EventLoopCPRunScript.py:78