ATLAS Offline Software
MadGraphTopUpAlg.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2 
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
7 
8 __author__ = 'Zach Marshall <ZLMarshall@lbl.gov>'
9 
10 class MadGraphTopUpAlg(EvgenAlg):
11  """
12  A python-only algorithm for running more events in MadGraph
13  """
14 
15  def __init__(self, name="MG_TopUp", topUpSvcName="EvgenOTFTopUpSvc", threshold=50):
16  super(MadGraphTopUpAlg,self).__init__(name=name)
17  self.topUpSvcName = topUpSvcName
18  self.topUpSvc = None
19  self.threshold = threshold
20 
21  self.njobs = 1
22  if 'ATHENA_CORE_NUMBER' in os.environ:
23  self.njobs = os.environ['ATHENA_CORE_NUMBER']
24  self.mode = 0 if self.njobs==1 else 2
25  self.fileList = []
26 
27  def initialize(self):
28  self.topUpSvc = getattr(svcMgr, self.topUpSvcName, None)
29  if self.topUpSvc is not None:
30  self.msg.info('Got the top up service')
31  else:
32  self.msg.warning( 'Could not get the top up service! Will be a null-op.' )
33  if self.njobs>1:
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
36 
37  def execute(self):
38  # If we did not manage to get the top up service, just get out of here
39  if self.topUpSvc is None:
40  return StatusCode.Success
41 
42  # See if it's time to make more events
43  if float(self.topUpSvc.getNPerFile()-self.topUpSvc.getNUsedSoFar())*self.topUpSvc.getEfficiency()>self.threshold:
44  # Plenty of events left - get out of here
45  return StatusCode.Success
46 
47  # Not enough events! Top it up!
48  a_dir = None
49  # Call another generate - first find out where to go
50  if os.access('madevent',os.R_OK):
51  self.msg.info( 'Found a grid pack at madevent/ - using it for generation' )
52  a_dir = 'madevent'
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
56  else:
57  a_dir = sorted( glob.glob( '*PROC*' ) )[-1]
58  self.msg.info( 'Running from process directory '+a_dir )
59 
60  currdir=os.getcwd()
61  os.chdir(a_dir)
62 
63  # Modify the random number seed - increment by 2M
64  oldcard_f = open('Cards/run_card.dat','r')
65  oldlines = oldcard_f.readlines()
66  oldcard_f.close()
67  newcard = open('Cards/run_card.dat','w')
68  for line in oldlines:
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))
74  else:
75  newcard.write(line)
76  newcard.close()
77 
78  # Do the actual generation - should we use the generate* functions in MGC_Utils directly?
79  self.msg.info( 'Started generating at '+str(time.asctime()) )
80  if self.njobs>1:
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)
83  else:
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)
86  generate.wait()
87  self.msg.info( 'Finished generating at'+str(time.asctime()) )
88 
89  # Move the events to something local...
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'
96 
97  # Make sure we have trimmed off any directory names
98  if '/' in a_new_file_name:
99  a_new_file_name = a_new_file_name.split('/')[-1]
100 
101  # Unzip the files and move them in place
102  unzip = subprocess.Popen(['gunzip','Events/OTFTopUp/unweighted_events.lhe.gz'])
103  unzip.wait()
104  shutil.move('Events/OTFTopUp/unweighted_events.lhe',currdir+'/'+a_new_file_name)
105 
106  # Go back
107  os.chdir(currdir)
108 
109  # Remove the oldest file to save disk space
110  self.fileList += [ a_new_file_name ]
111  if len(self.fileList)>2:
112  # One old file, one new file, and delete the third
113  self.msg.info('Removing old file '+self.fileList[-3])
114  if os.access( currdir+'/'+self.fileList[-3] , os.R_OK ):
115  remove_old = subprocess.Popen(['rm',(currdir+'/'+self.fileList[-3])])
116  remove_old.wait()
117 
118  # Pass the information back to the top up service
119  self.topUpSvc.newFile( a_new_file_name )
120 
121  return StatusCode.Success
grepfile.info
info
Definition: grepfile.py:38
python.MadGraphTopUpAlg.MadGraphTopUpAlg
Definition: MadGraphTopUpAlg.py:10
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.MadGraphTopUpAlg.MadGraphTopUpAlg.execute
def execute(self)
Definition: MadGraphTopUpAlg.py:37
python.MadGraphTopUpAlg.MadGraphTopUpAlg.topUpSvcName
topUpSvcName
Definition: MadGraphTopUpAlg.py:17
python.MadGraphTopUpAlg.MadGraphTopUpAlg.mode
mode
Definition: MadGraphTopUpAlg.py:24
python.MadGraphTopUpAlg.MadGraphTopUpAlg.__init__
def __init__(self, name="MG_TopUp", topUpSvcName="EvgenOTFTopUpSvc", threshold=50)
Definition: MadGraphTopUpAlg.py:15
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
Trk::open
@ open
Definition: BinningType.h:40
python.MadGraphTopUpAlg.MadGraphTopUpAlg.initialize
def initialize(self)
Definition: MadGraphTopUpAlg.py:27
python.MadGraphTopUpAlg.MadGraphTopUpAlg.njobs
njobs
Definition: MadGraphTopUpAlg.py:21
python.MadGraphTopUpAlg.MadGraphTopUpAlg.fileList
fileList
Definition: MadGraphTopUpAlg.py:25
str
Definition: BTagTrackIpAccessor.cxx:11
python.MadGraphTopUpAlg.MadGraphTopUpAlg.topUpSvc
topUpSvc
Definition: MadGraphTopUpAlg.py:18
python.MadGraphTopUpAlg.MadGraphTopUpAlg.threshold
threshold
Definition: MadGraphTopUpAlg.py:19
error
Definition: IImpactPoint3dEstimator.h:70
readCCLHist.float
float
Definition: readCCLHist.py:83