ATLAS Offline Software
GenevaUtils.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 
4 import subprocess, os, shlex, re, yaml
5 
6 from AthenaCommon import Logging
7 
8 
9 logger = Logging.logging.getLogger("Geneva_i")
10 
11 
13 
14  def __init__(self, runArgs):
15  self.genevapath = os.environ['GENEVA_DATA_DIR']
16  self.exe = 'geneva'
17  self.points = 2000
18  self.iterations = 4
19  self.factor = 250
21  self.numruns = 1
22  #Geneva specific variables for input.DAT, see writeInputDAT function for more elaboration
23  self.rts = 13000. #collision energy (GeV)
24  if hasattr(runArgs,"ecmEnergy"):
25  self.rts = runArgs.ecmEnergy
26 
27  self.card=''
28  self.yaml=yaml.safe_load(self.card)
29  self.cardname='input.yml'
30  self.sigmabelow=''
31 
32  self.iseed = 34
33  if hasattr(runArgs,"randomSeed"):
34  self.iseed = runArgs.randomSeed
35 
36 
37  self.nev = "500"
38  if hasattr(runArgs,"maxEvents"):
39  self.nev = runArgs.maxEvents
40 
41  def parseCard(self):
42  self.yaml=yaml.safe_load(self.card)
43  self.yaml['global']['run_name']="tutorial"
44  self.yaml['global']['num_events']= self.nev*self.factor
45  for a in self.yaml['process'].keys():
46  self.yaml['process'][a]['initial_state']['beams']='pp'
47  self.yaml['process'][a]['initial_state']['Ecm']=self.rts
48  self.yaml['process'][a]['initial_state']['pdf_provider']['LHAPDF']['set']="PDF4LHC15_nnlo_100"
49  return
50 
51  def outputLHEFile(self):
52  return "generate/tutorial_1.lhe.gz"
53 
54 
55 def writeInputDAT(Init):
56  with open(Init.cardname, "w") as outF:
57  yaml.dump(Init.yaml,outF)
58  return
59 
60 
61 def run_command(command, stdin = None):
62  """
63  Run a command and print output continuously
64  """
65  process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stdin=stdin)
66  while True:
67  output = process.stdout.readline().decode("utf-8")
68  if output == '' and process.poll() is not None:
69  break
70  if output:
71  # remove ANSI escape formatting characters
72  reaesc = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
73  text = reaesc.sub('', output.strip())
74  logger.info(text)
75 
76  rc = process.poll()
77  return rc
78 
79 
80 def GenevaInitialize(Init, stdin=None):
81 
82  logger.info("Starting Geneva Initialization")
83 
84  try:
85  open(Init.cardname)
86  except IOError:
87  raise Exception("problem with file IO; potentially input.DAT not created correctly")
88  return
89  rc = 0
90  try:
91  os.environ['OpenLoopsPath']=os.environ['OPENLOOPSPATH']
92  print(Init.exe + ' setup ' + Init.cardname + ' --points '+ str(Init.points) + ' --iterations '+ str(Init.iterations) + ' --num-runs ' + str(Init.numruns))
93  rc = run_command(Init.exe + ' setup ' + Init.cardname + ' --points '+ str(Init.points) + ' --iterations '+ str(Init.iterations) + ' --num-runs ' + str(Init.integrationnumruns))
94 
95  except OSError:
96  raise Exception("init executable or file not found")
97 
98  except Exception:
99  raise Exception("Non-OS Error or IOError in init execution block")
100 
101  if rc:
102  raise Exception('Unexpected error in geneva init execution')
103 
104  return
105 
106 
107 def GenevaExecute(Init):
108 
109  logger.info("Starting Geneva Itself")
110 
111  try:
112  open(Init.cardname)
113  except IOError:
114  raise Exception ("problem with IO; potentially input.DAT not created correctly")
115  return
116  rc=0
117  try:
118  os.environ['OpenLoopsPath']=os.environ['OPENLOOPSPATH']
119  rc = run_command(Init.exe + ' generate ' + Init.cardname + ' --num-runs ' + str(Init.numruns))
120  rc = run_command(Init.exe + ' reweight ' + Init.cardname + ' --sigma-below '+ Init.sigmabelow + ' --num-runs '+ str(Init.numruns))
121 
122  except OSError:
123  raise Exception("geneva executable or file not found")
124 
125  except Exception:
126  raise Exception("Non-OS Error or IOError in Superchic execution block")
127 
128  if rc:
129  raise Exception('Unexpected error in geneva execution')
130  return
131 
132 def GenevaRun(Init, genSeq):
133  genSeq.GenevaConfig = Init
134  writeInputDAT(Init)
135  GenevaInitialize(Init)
136  GenevaExecute(Init)
137  return
GenevaUtils.GenevaConfig.exe
exe
Definition: GenevaUtils.py:16
AtlasMcWeight::decode
double decode(number_type binnedWeight)
Convert weight from unsigned to double.
Definition: AtlasMcWeight.cxx:32
GenevaUtils.GenevaInitialize
def GenevaInitialize(Init, stdin=None)
Definition: GenevaUtils.py:80
GenevaUtils.GenevaConfig.cardname
cardname
Definition: GenevaUtils.py:29
GenevaUtils.GenevaConfig.factor
factor
Definition: GenevaUtils.py:19
GenevaUtils.GenevaConfig.points
points
Definition: GenevaUtils.py:17
GenevaUtils.GenevaConfig.iseed
iseed
Definition: GenevaUtils.py:32
GenevaUtils.writeInputDAT
def writeInputDAT(Init)
Definition: GenevaUtils.py:55
GenevaUtils.GenevaConfig.outputLHEFile
def outputLHEFile(self)
Definition: GenevaUtils.py:51
GenevaUtils.GenevaConfig
Definition: GenevaUtils.py:12
GenevaUtils.GenevaConfig.iterations
iterations
Definition: GenevaUtils.py:18
GenevaUtils.GenevaConfig.numruns
numruns
Definition: GenevaUtils.py:21
GenevaUtils.GenevaRun
def GenevaRun(Init, genSeq)
Definition: GenevaUtils.py:132
GenevaUtils.GenevaConfig.parseCard
def parseCard(self)
Definition: GenevaUtils.py:41
GenevaUtils.GenevaConfig.genevapath
genevapath
Definition: GenevaUtils.py:15
GenevaUtils.GenevaConfig.integrationnumruns
integrationnumruns
Definition: GenevaUtils.py:20
GenevaUtils.GenevaConfig.yaml
yaml
Definition: GenevaUtils.py:28
GenevaUtils.GenevaConfig.rts
rts
Definition: GenevaUtils.py:23
GenevaUtils.GenevaExecute
def GenevaExecute(Init)
Definition: GenevaUtils.py:107
GenevaUtils.run_command
def run_command(command, stdin=None)
Definition: GenevaUtils.py:61
GenevaUtils.GenevaConfig.nev
nev
Definition: GenevaUtils.py:37
GenevaUtils.GenevaConfig.sigmabelow
sigmabelow
Definition: GenevaUtils.py:30
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
GenevaUtils.GenevaConfig.card
card
Definition: GenevaUtils.py:27
GenevaUtils.GenevaConfig.__init__
def __init__(self, runArgs)
Definition: GenevaUtils.py:14