class HDecay: def __init__(self,infile,outfile,template=None): self.infile = infile #Either the file to use as input, or the name to given the input file which will be created from the file 'template'. self.outfile = outfile hdecayroot = '/home/565/bff565/projects/pysusyMSSM/hdecay_ratio' self.root = hdecayroot #default extra input files hdecayindef = hdecayroot+'/hdecay.in' #extra hdecay input files (specifying constants etc) brinputdef = hdecayroot+'/br.input' #new names of extra input files #use the output file name as the base name for these, as it should have a unique identifier in it to prevent clashes between processes self.hdecayin = outfile+'.hdecay.in' self.brinput = outfile+'.br.input' #copy default files to new location #copy default input files to temporary directory (assume this is given as part of the output file name) shutil.copyfile(hdecayindef, self.hdecayin) shutil.copyfile(brinputdef, self.brinput) self.SLHAout = SLHAFile(name=self.outfile, directory='') #self.exe = 'run' def run(self): """Execute process""" """ cwd=os.getcwd() os.chdir(self.root) sp.call([self.exe, self.outfile]) os.chdir(cwd) """ #REALLY WEIRD ERRORS GOING ON IF I RUN THIS IN THE MAIN PYTHON PROCESS #DOING THE FORK NOW ONLY TO PREVENT SUCH THINGS OCCURRING sys.stdout.flush() #os.fork() clones the io buffer for these files, so make sure they get dumped to disk before the fork or the output will get duplicated. sys.stderr.flush() childpid = os.fork() #spawn child process and get its pid (or 0 if this is the child process) if childpid == 0: hdecay.ratios(self.infile, self.hdecayin, self.brinput, self.outfile) os._exit(0) #exit the child process with error code 0 (even if isajet fails we'll still do this coz we can tell how things went from the output file) else: os.waitpid(childpid, 0) """ p = Process(target=hdecay.ratios, args=(self.infile, self.hdecayin, self.brinput, self.outfile)) p.start() p.join() """ def getobs(self,skiperrors=True): """Gather results of computations""" self.SLHAout.readdata() return self.SLHAout.getobs(blockdict) #extract observables from block format file using translation dictionary
class MicrOmegas: def __init__(self, infile, outfile, silenceobjs=None): self.infile = infile self.outfile = outfile self.SLHA = SLHAFile(name=outfile, directory="") # save info about stdout for use by silence functions # NOW GET THESE EXTERNALLY SO THEY CAN BE SHARED BY ALL PROGRAMS ##self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in xrange(2)] # open 2 fds ##self.save = os.dup(1), os.dup(2) # save the current file descriptors to a tuple # self.null_fds, self.save = silenceobjs def silence(self): """Kill the stdout of the current process. Lets us silence the output of the wrapped fortran code, which sys.stdout = open('/dev/null'), for example, does not""" os.dup2(self.null_fds[0], 1) # put /dev/null fds on 1 and 2 os.dup2(self.null_fds[1], 2) def unsilence(self): """undoes the action of silence""" os.dup2(self.save[0], 1) # restore file descriptors so I can print the results os.dup2(self.save[1], 2) # close the temporary fds # os.close(null_fds[0]) #don't think I want to do this, may as well keep using the same ones every loop # os.close(null_fds[1]) def run(self): """Execute process""" # self.silence() # Don't want default output clogging up our log files so dump it all to dev/null # sys.stdout = open('/dev/null') # micromegas.rundarkomega(self.infile, self.outfile) # sys.stdout = sys.__stdout__ # self.unsilence() sys.stdout.flush() # os.fork() clones the io buffer for these files, so make sure they get dumped to disk before the fork or the output will get duplicated. sys.stderr.flush() childpid = os.fork() # spawn child process and get its pid (or 0 if this is the child process) if childpid == 0: # self.silence() # Don't want default output clogging up our log files so dump it all to dev/null with Silence(): micromegas.rundarkomega( self.infile, self.outfile ) # run micromegas in the child process; it was causing some weird errors in superiso that were had to fathom (with bsgmo calculation, was returning nan) os._exit( 0 ) # exit the child process with error code 0 (even if isajet fails we'll still do this coz we can tell how things went from the output file) else: os.waitpid(childpid, 0) # self.unsilence() #do the unsilencing out here, since the child process can be killed by isajet """ p = Process(target=micromegas.rundarkomega, args=(self.infile, self.outfile)) p.start() p.join() """ def getobs(self, skiperrors=True): """Gather results of computations""" self.SLHA.readdata() return self.SLHA.getobs(blockdict) # extract observables from SLHA file using translation dictionary """