ATLAS Offline Software
MadGraphUtilsHelpers.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 def getDictFromCard(card_loc,lowercase=False):
4  card=open(card_loc)
5  mydict={}
6  for line in iter(card):
7  if not line.strip().startswith('#'): # line commented out
8  command = line.split('!', 1)[0]
9  if '=' in command:
10  setting = command.split('=')[-1].strip()
11  value = '='.join(command.split('=')[:-1]).strip()
12  if lowercase:
13  value=value.lower()
14  setting=setting.lower()
15  mydict[setting]=value
16  card.close()
17  return mydict
18 
19 def settingIsTrue(setting):
20  if setting.replace("'",'').replace('"','').replace('.','').lower() in ['t','true']:
21  return True
22  return False
23 
25  y=str(x).lower().strip()
26  # remove leading and trailing "/'
27  while len(y)>0 and (y[0]=='"' or y[0]=="'"):
28  y=y[1:]
29  while len(y)>0 and (y[-1]=='"' or y[-1]=="'"):
30  y=y[:-1]
31  return y
32 
33 def checkSetting(key_,value_,mydict_):
34  key=totallyStripped(key_)
35  value=totallyStripped(value_)
36  mydict={}
37  for k in mydict_:
38  mydict[totallyStripped(k)]=totallyStripped(mydict_[k])
39  return key in mydict and mydict[key]==value
40 
41 def checkSettingIsTrue(key_,mydict_):
42  key=totallyStripped(key_)
43  mydict={}
44  for k in mydict_:
45  mydict[totallyStripped(k)]=totallyStripped(mydict_[k])
46  return key in mydict and mydict[key] in ['t','true']
47 
48 def checkSettingExists(key_,mydict_):
49  key=totallyStripped(key_)
50  keys=[]
51  for k in mydict_:
52  keys+=[totallyStripped(k)]
53  return key in keys
54 
56  # also need to find out the version (copied from generate)
57  import os
58  version=None
59  version_file = open(os.environ['MADPATH']+'/VERSION','r')
60 
61  for line in version_file:
62  if 'version' in line:
63  version=line.split('=')[1].strip()
64  version_file.close()
65 
66  if not version:
67  raise RuntimeError('Failed to find MadGraph/MadGraph5_aMC@NLO version in '+version_file)
68 
69  vs=[int(v) for v in version.split('.')]
70 
71  # this is lazy, let's hope there wont be a subversion > 100...
72  y=int(100**max(len(vs),len(args)))
73  testnumber=0
74  for x in args:
75  testnumber+=x*y
76  y/=100
77 
78  y=int(100**max(len(vs),len(args)))
79  versionnumber=0
80  for x in vs:
81  versionnumber+=x*y
82  y/=100
83  return versionnumber>=testnumber
84 
85 def isNLO_from_run_card(run_card):
86  f = open(run_card,'r')
87  if "parton_shower" in f.read().lower():
88  f.close()
89  return True
90  else:
91  f.close()
92  return False
93 
94 def get_runArgs_info(runArgs):
95  if runArgs is None:
96  raise RuntimeError('runArgs must be provided!')
97  if hasattr(runArgs,'ecmEnergy'):
98  beamEnergy = runArgs.ecmEnergy / 2.
99  else:
100  raise RuntimeError("No center of mass energy found in runArgs.")
101  if hasattr(runArgs,'randomSeed'):
102  random_seed = runArgs.randomSeed
103  else:
104  raise RuntimeError("No random seed found in runArgs.")
105  return beamEnergy,random_seed
106 
108  import os
109  FIRST_DIR = (os.environ['JOBOPTSEARCHPATH']).split(":")[0]
110  jofiles = [f for f in os.listdir(FIRST_DIR) if (f.startswith('mc') and f.endswith('.py'))]
111  if len(jofiles)==0:
112  raise RuntimeError('No job options found in '+FIRST_DIR)
113  joparts = os.path.basename(jofiles[0]).split('.')
114  if len(joparts)<2:
115  raise RuntimeError('Malformed job options file name: '+jofiles[0])
116  return joparts[1]
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
python.MadGraphUtilsHelpers.isNLO_from_run_card
def isNLO_from_run_card(run_card)
Definition: MadGraphUtilsHelpers.py:85
python.MadGraphUtilsHelpers.settingIsTrue
def settingIsTrue(setting)
Definition: MadGraphUtilsHelpers.py:19
max
#define max(a, b)
Definition: cfImp.cxx:41
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.MadGraphUtilsHelpers.checkSettingIsTrue
def checkSettingIsTrue(key_, mydict_)
Definition: MadGraphUtilsHelpers.py:41
python.MadGraphUtilsHelpers.get_runArgs_info
def get_runArgs_info(runArgs)
Definition: MadGraphUtilsHelpers.py:94
python.MadGraphUtilsHelpers.checkSetting
def checkSetting(key_, value_, mydict_)
Definition: MadGraphUtilsHelpers.py:33
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.MadGraphUtilsHelpers.checkSettingExists
def checkSettingExists(key_, mydict_)
Definition: MadGraphUtilsHelpers.py:48
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
python.MadGraphUtilsHelpers.totallyStripped
def totallyStripped(x)
Definition: MadGraphUtilsHelpers.py:24
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.MadGraphUtilsHelpers.get_physics_short
def get_physics_short()
Definition: MadGraphUtilsHelpers.py:107
python.MadGraphUtilsHelpers.getDictFromCard
def getDictFromCard(card_loc, lowercase=False)
Definition: MadGraphUtilsHelpers.py:3
python.MadGraphUtilsHelpers.is_version_or_newer
def is_version_or_newer(args)
Definition: MadGraphUtilsHelpers.py:55