3 from AthenaCommon.AppMgr
import ServiceMgr
as svcMgr
4 from GeneratorModules.EvgenAlg
import EvgenAlg
5 from AthenaPython.PyAthena
import StatusCode
6 import glob,subprocess,os,time,shutil
8 __author__ =
'Zach Marshall <ZLMarshall@lbl.gov>'
12 A python-only algorithm for running more events in MadGraph
15 def __init__(self, name="MG_TopUp", topUpSvcName="EvgenOTFTopUpSvc", threshold=50):
16 super(MadGraphTopUpAlg,self).
__init__(name=name)
22 if 'ATHENA_CORE_NUMBER' in os.environ:
23 self.
njobs = os.environ[
'ATHENA_CORE_NUMBER']
30 self.msg.
info(
'Got the top up service')
32 self.msg.warning(
'Could not get the top up service! Will be a null-op.' )
34 self.msg.
info(
'Lucky you - you are running on a full node queue. Will re-configure for '+
str(self.
njobs)+
' jobs.')
35 return StatusCode.Success
40 return StatusCode.Success
45 return StatusCode.Success
50 if os.access(
'madevent',os.R_OK):
51 self.msg.
info(
'Found a grid pack at madevent/ - using it for generation' )
53 elif len( glob.glob(
'*PROC*' ) )==0:
54 self.msg.
error(
'Need to re-run, but cannot find directory for running!' )
55 return StatusCode.Failure
57 a_dir =
sorted( glob.glob(
'*PROC*' ) )[-1]
58 self.msg.
info(
'Running from process directory '+a_dir )
64 oldcard_f =
open(
'Cards/run_card.dat',
'r')
65 oldlines = oldcard_f.readlines()
67 newcard =
open(
'Cards/run_card.dat',
'w')
69 if ' nevents ' in line:
70 newcard.write(
' %i = nevents ! Number of unweighted events requested \n'%(self.
topUpSvc.getNPerFile()))
71 elif ' iseed ' in line:
72 old_seed =
int( line.split()[0] )
73 newcard.write(
' %i = iseed ! rnd seed (0=assigned automatically=default)) \n'%(old_seed+2000000))
79 self.msg.
info(
'Started generating at '+
str(time.asctime()) )
81 self.msg.
info(
'Running parallel generation. Should be nice and fast.')
82 generate = subprocess.Popen([
'bin/generate_events',
str(self.
mode),
str(self.
njobs),
'OTFTopUp'],stdin=subprocess.PIPE)
84 self.msg.
info(
'Running serial generation. This will take a bit more time than parallel generation.')
85 generate = subprocess.Popen([
'bin/generate_events',
'0',
'OTFTopUp'],stdin=subprocess.PIPE)
87 self.msg.
info(
'Finished generating at'+
str(time.asctime()) )
90 a_new_file_name =
'otf_lhe.events'
91 if len( glob.glob( currdir+
'/*events.[*0-9]' ) )>0:
92 a_new_file_name =
sorted( glob.glob( currdir+
'/*events.[*0-9]' ) )[-1]
93 a_new_file_name = a_new_file_name[:a_new_file_name.rfind(
'.')]+
str(
int(a_new_file_name.split(
'.')[-1])+1 )
94 elif len( glob.glob( currdir+
'/*events' ) )>0:
95 a_new_file_name =
sorted( glob.glob( currdir+
'/*events' ) )[-1]+
'.1'
98 if '/' in a_new_file_name:
99 a_new_file_name = a_new_file_name.split(
'/')[-1]
102 unzip = subprocess.Popen([
'gunzip',
'Events/OTFTopUp/unweighted_events.lhe.gz'])
104 shutil.move(
'Events/OTFTopUp/unweighted_events.lhe',currdir+
'/'+a_new_file_name)
110 self.
fileList += [ a_new_file_name ]
114 if os.access( currdir+
'/'+self.
fileList[-3] , os.R_OK ):
115 remove_old = subprocess.Popen([
'rm',(currdir+
'/'+self.
fileList[-3])])
119 self.
topUpSvc.newFile( a_new_file_name )
121 return StatusCode.Success