Example #1
0
def rescheduleFailedJob(jobID, message):
    try:
        import DIRAC
        global jobReport

        gLogger.warn('Failure during %s' % (message))

        #Setting a job parameter does not help since the job will be rescheduled,
        #instead set the status with the cause and then another status showing the
        #reschedule operation.

        if not jobReport:
            gLogger.info('Creating a new JobReport Object')
            jobReport = JobReport(int(jobID), 'JobWrapperTemplate')

        jobReport.setApplicationStatus('Failed %s ' % message, sendFlag=False)
        jobReport.setJobStatus('Rescheduled', message, sendFlag=False)

        # We must send Job States and Parameters before it gets reschedule
        jobReport.sendStoredStatusInfo()
        jobReport.sendStoredJobParameters()

        gLogger.info(
            'Job will be rescheduled after exception during execution of the JobWrapper'
        )

        jobManager = RPCClient('WorkloadManagement/JobManager')
        result = jobManager.rescheduleJob(int(jobID))
        if not result['OK']:
            gLogger.warn(result)

        # Send mail to debug errors
        mailAddress = DIRAC.alarmMail
        site = DIRAC.siteName()
        subject = 'Job rescheduled at %s' % site
        ret = systemCall(0, 'hostname')
        wn = ret['Value'][1]
        msg = 'Job %s rescheduled at %s, wn=%s\n' % (jobID, site, wn)
        msg += message

        NotificationClient().sendMail(mailAddress,
                                      subject,
                                      msg,
                                      fromAddress="*****@*****.**",
                                      localAttempt=False)

        return
    except Exception, x:
        gLogger.exception('JobWrapperTemplate failed to reschedule Job')
        return
def rescheduleFailedJob(jobID,message):
  try:
    import DIRAC
    global jobReport

    gLogger.warn('Failure during %s' %(message))

    #Setting a job parameter does not help since the job will be rescheduled,
    #instead set the status with the cause and then another status showing the
    #reschedule operation.

    if not jobReport:
      gLogger.info('Creating a new JobReport Object')
      jobReport = JobReport(int(jobID),'JobWrapperTemplate')

    jobReport.setApplicationStatus( 'Failed %s ' % message, sendFlag = False )
    jobReport.setJobStatus( 'Rescheduled', message, sendFlag = False )

    # We must send Job States and Parameters before it gets reschedule
    jobReport.sendStoredStatusInfo()
    jobReport.sendStoredJobParameters()

    gLogger.info('Job will be rescheduled after exception during execution of the JobWrapper')

    jobManager  = RPCClient('WorkloadManagement/JobManager')
    result = jobManager.rescheduleJob(int(jobID))
    if not result['OK']:
      gLogger.warn(result)

    # Send mail to debug errors
    mailAddress = DIRAC.alarmMail
    site        = DIRAC.siteName()
    subject     = 'Job rescheduled at %s' % site
    ret         = systemCall(0,'hostname')
    wn          = ret['Value'][1]
    msg         = 'Job %s rescheduled at %s, wn=%s\n' % ( jobID, site, wn )
    msg        += message

    NotificationClient().sendMail(mailAddress,subject,msg,fromAddress="*****@*****.**",localAttempt=False)

    return
  except Exception,x:
    gLogger.exception('JobWrapperTemplate failed to reschedule Job')
    return
Example #3
0
def execute_module(ed, executable, args):

    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    DIRAC.gLogger.notice('Executable:', executable)
    DIRAC.gLogger.notice('Arguments:', args)
    ed.edExe = executable
    ed.edArguments = args
    evnDisplayReturnCode = ed.execute()

    if evnDisplayReturnCode != 0:
        DIRAC.gLogger.error('Failed to execute EvnDisp Application')
        jobReport.setApplicationStatus('EvnDisp Application: Failed')
        DIRAC.exit(-1)

    return DIRAC.S_OK
Example #4
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("T:", "template=", "Corsika Template")
    Script.registerSwitch("S:", "simtelConfig=", "SimtelConfig")
    Script.registerSwitch("p:", "run_number=",
                          "Do not use: Run Number automatically set")
    Script.registerSwitch("E:", "executable=",
                          "Executable (Use SetExecutable)")
    Script.registerSwitch("V:", "version=", "Version (Use setVersion)")
    Script.registerSwitch("M:", "mode=",
                          "Mode (corsika_standalone/corsika_simtelarray)")

    Script.parseCommandLine(ignoreErrors=True)

    ## default values ##############
    run_number = None
    corsikaTemplate = None
    simtelConfig = None
    executable = None
    version = None
    mode = 'corsika_simtelarray'

    ### set switch values ###
    for switch in Script.getUnprocessedSwitches():
        if switch[0] == "run_number" or switch[0] == "p":
            run_number = switch[1].split('ParametricParameters=')[1]
        elif switch[0] == "template" or switch[0] == "T":
            corsikaTemplate = switch[1]
        elif switch[0] == "simtelConfig" or switch[0] == "S":
            simtelConfig = switch[1]
        elif switch[0] == "executable" or switch[0] == "E":
            executable = switch[1]
        elif switch[0] == "version" or switch[0] == "V":
            version = switch[1]
        elif switch[0] == "mode" or switch[0] == "M":
            mode = switch[1]

    if version == None or executable == None or run_number == None or corsikaTemplate == None:
        Script.showHelp()
        jobReport.setApplicationStatus('Missing options')
        DIRAC.exit(-1)

    from CTADIRAC.Core.Workflow.Modules.CorsikaApp import CorsikaApp
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    createGlobalsFromConfigFiles(corsikaTemplate, version)

    ############ Producing Corsika File

    install_CorsikaSimtelPack(version)
    CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'
    cs = CorsikaApp()
    cs.setSoftwarePackage(CorsikaSimtelPack)
    cs.csExe = executable
    cs.csArguments = [
        '--run-number', run_number, '--run', 'corsika', corsikaTemplate
    ]
    corsikaReturnCode = cs.execute()

    if corsikaReturnCode != 0:
        DIRAC.gLogger.error('Corsika Application: Failed')
        jobReport.setApplicationStatus('Corsika Application: Failed')
        DIRAC.exit(-1)

###################### rename of corsika output file #######################
    rundir = 'run' + run_number
    filein = rundir + '/' + corsikaOutputFileName
    corsikaFileName = particle + '_' + thetaP + '_' + phiP + '_alt' + obslev + '_' + 'run' + run_number + '.corsika.gz'
    mv_cmd = 'mv ' + filein + ' ' + corsikaFileName
    if (os.system(mv_cmd)):
        DIRAC.exit(-1)

    ### create corsika tar luisa ####################
    corsikaTarName = particle + '_' + thetaP + '_' + phiP + '_alt' + obslev + '_' + 'run' + run_number + '.corsika.tar.gz'
    filetar1 = rundir + '/' + 'input'
    filetar2 = rundir + '/' + 'DAT' + run_number + '.dbase'
    filetar3 = rundir + '/run' + str(int(run_number)) + '.log'
    cmdTuple = [
        '/bin/tar', 'zcf', corsikaTarName, filetar1, filetar2, filetar3
    ]
    DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
    ret = systemCall(0, cmdTuple, sendOutput)
    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute tar')
        DIRAC.exit(-1)

    if (mode == 'corsika_standalone'):
        DIRAC.exit()

############ Producing SimTel File
######################Building simtel Directory Metadata #######################

    cfg_dict = {
        "4MSST": 'cta-prod2-4m-dc',
        "SCSST": 'cta-prod2-sc-sst',
        "STD": 'cta-prod2',
        "NSBX3": 'cta-prod2',
        "ASTRI": 'cta-prod2-astri',
        "NORTH": 'cta-prod2n',
        "SCMST": 'cta-prod2-sc3'
    }

    #if simtelConfig=="6INROW":
    #  all_configs=["4MSST","SCSST","ASTRI","NSBX3","STD","SCMST"]
    #elif simtelConfig=="5INROW":
    #  all_configs=["4MSST","SCSST","ASTRI","NSBX3","STD"]
    #else:
    #  all_configs=[simtelConfig]

    all_configs = [simtelConfig]

    for current_conf in all_configs:

        DIRAC.gLogger.notice('current conf is', current_conf)
        current_version = version
        DIRAC.gLogger.notice('current version is', current_version)

        #if current_conf == "SCMST":
        #  current_version = version + '_sc3'
        #  DIRAC.gLogger.notice('current version is', current_version)
        #  if os.path.isdir('sim_telarray'):
        #    DIRAC.gLogger.notice('Package found in the local area. Removing package...')
        #    cmd = 'rm -R sim_telarray corsika-6990 hessioxxx corsika-run'
        #    if(os.system(cmd)):
        #      DIRAC.exit( -1 )
        #    install_CorsikaSimtelPack(current_version)
        #else:
        # current_version = version
        # DIRAC.gLogger.notice('current version is', current_version)

        #### execute simtelarray ################
        fd = open('run_sim.sh', 'w')
        fd.write("""#! /bin/sh  
    export SVNPROD2=$PWD
    export SVNTAG=SVN-PROD2
    export CORSIKA_IO_BUFFER=800MB
    ./grid_prod2-repro.sh %s %s""" % (corsikaFileName, current_conf))
        fd.close()
        ####################################

        os.system('chmod u+x run_sim.sh')
        os.system('chmod u+x grid_prod2-repro.sh')
        cmdTuple = ['./run_sim.sh']
        ret = systemCall(0, cmdTuple, sendSimtelOutput)
        simtelReturnCode, stdout, stderr = ret['Value']

        if (os.system('grep Broken simtel.log') == 0):
            DIRAC.gLogger.error('Broken string found in simtel.log')
            jobReport.setApplicationStatus('Broken pipe')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute run_sim.sh')
            DIRAC.gLogger.error('run_sim.sh status is:', simtelReturnCode)
            DIRAC.exit(-1)

##   check simtel data/log/histo Output File exist
        cfg = cfg_dict[current_conf]
        simtelFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.simtel.gz'
        cmd = 'mv Data/sim_telarray/' + cfg + '/*/Data/*.simtel.gz ' + simtelFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

        simtelLogFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.log.gz'
        cmd = 'mv Data/sim_telarray/' + cfg + '/*/Log/*.log.gz ' + simtelLogFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

        simtelHistFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.hdata.gz'
        cmd = 'mv Data/sim_telarray/' + cfg + '/*/Histograms/*.hdata.gz ' + simtelHistFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)


################################################################

    DIRAC.exit()
Example #5
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("S:", "simtelConfig=", "SimtelConfig", setConfig)
    Script.registerSwitch("V:", "version=", "Version", setVersion)

    from DIRAC.Resources.Catalog.FileCatalogClient import FileCatalogClient
    from DIRAC.Resources.Catalog.FileCatalog import FileCatalog

    Script.parseCommandLine()
    DIRAC.gLogger.setLevel('INFO')

    global fcc, fcL

    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    global jobID
    jobID = os.environ['JOBID']
    jobReport = JobReport(int(jobID))

    ###########
    ## Checking MD coherence
    fc = FileCatalog('LcgFileCatalog')
    res = fc._getCatalogConfigDetails('DIRACFileCatalog')
    print 'DFC CatalogConfigDetails:', res
    res = fc._getCatalogConfigDetails('LcgFileCatalog')
    print 'LCG CatalogConfigDetails:', res

    fcc = FileCatalogClient()
    fcL = FileCatalog('LcgFileCatalog')

    from DIRAC.Interfaces.API.Dirac import Dirac
    dirac = Dirac()
    ############################

    install_CorsikaSimtelPack(version)
    #############
    # simtelConfigFile should be built from ???
    #simtelConfigFilesPath = 'sim_telarray/multi'
    #simtelConfigFile = simtelConfigFilesPath + '/multi_cta-ultra5.cfg'
    #createGlobalsFromConfigFiles(simtelConfigFile)
    #createGlobalsFromConfigFiles(current_version)
    #######################
    ## files spread in 1000-runs subDirectories

    global corsikaFileLFN
    corsikaFileLFN = dirac.getJobJDL(jobID)['Value']['InputData']
    print 'corsikaFileLFN is ' + corsikaFileLFN
    corsikaFileName = os.path.basename(corsikaFileLFN)
    run_number = corsikaFileName.split('run')[1].split('.corsika.gz')[
        0]  # run001412.corsika.gz

    runNum = int(run_number)
    subRunNumber = '%03d' % runNum
    runNumModMille = runNum % 1000
    runNumTrunc = (runNum - runNumModMille) / 1000
    runNumSeriesDir = '%03dxxx' % runNumTrunc
    print 'runNumSeriesDir=', runNumSeriesDir

    f = open('DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK', 'w')
    f.close()

    ############ Producing SimTel File
    ######################Building simtel Directory Metadata #######################

    cfg_dict = {
        "4MSST": 'cta-prod2-4m-dc',
        "SCSST": 'cta-prod2-sc-sst',
        "STD": 'cta-prod2',
        "NSBX3": 'cta-prod2',
        "ASTRI": 'cta-prod2-astri',
        "SCMST": 'cta-prod2-sc3',
        "NORTH": 'cta-prod2n'
    }

    if simtelConfig == "6INROW":
        all_configs = ["4MSST", "SCSST", "ASTRI", "NSBX3", "STD", "SCMST"]
    elif simtelConfig == "5INROW":
        all_configs = ["4MSST", "SCSST", "ASTRI", "NSBX3", "STD"]
    elif simtelConfig == "3INROW":
        all_configs = ["SCSST", "STD", "SCMST"]
    else:
        all_configs = [simtelConfig]

    for current_conf in all_configs:

        DIRAC.gLogger.notice('current conf is', current_conf)

        if current_conf == "SCMST":
            current_version = version + '_sc3'
            DIRAC.gLogger.notice('current version is', current_version)
            if os.path.isdir('sim_telarray'):
                DIRAC.gLogger.notice(
                    'Package found in the local area. Removing package...')
                cmd = 'rm -R sim_telarray corsika-6990 hessioxxx corsika-run'
                if (os.system(cmd)):
                    DIRAC.exit(-1)
                install_CorsikaSimtelPack(current_version)
        else:
            current_version = version
            DIRAC.gLogger.notice('current version is', current_version)

########################################################

        createGlobalsFromConfigFiles(current_version)

        resultCreateSimtelDirMD = createSimtelFileSystAndMD(
            current_conf, current_version)
        if not resultCreateSimtelDirMD['OK']:
            DIRAC.gLogger.error('Failed to create simtelArray Directory MD')
            jobReport.setApplicationStatus(
                'Failed to create simtelArray Directory MD')
            DIRAC.gLogger.error(
                'Metadata coherence problem, no simtelArray File produced')
            DIRAC.exit(-1)
        else:
            print 'simtel Directory MD successfully created'

############## introduce file existence check here ########################
        simtelFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.simtel.gz'
        simtelDirPath_conf = simtelDirPath + '_' + current_conf
        simtelOutFileDir = os.path.join(simtelDirPath_conf, 'Data',
                                        runNumSeriesDir)
        simtelOutFileLFN = os.path.join(simtelOutFileDir, simtelFileName)

        res = CheckCatalogCoherence(simtelOutFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Current conf already done', current_conf)
            continue

#### execute simtelarray ################
        fd = open('run_sim.sh', 'w')
        fd.write("""#! /bin/sh  
  export SVNPROD2=$PWD
  export SVNTAG=SVN-PROD2
  export CORSIKA_IO_BUFFER=800MB
  ./grid_prod2-repro.sh %s %s""" % (corsikaFileName, current_conf))
        fd.close()

        os.system('chmod u+x grid_prod2-repro.sh')
        os.system('chmod u+x run_sim.sh')
        cmdTuple = ['./run_sim.sh']
        ret = systemCall(0, cmdTuple, sendOutputSimTel)
        simtelReturnCode, stdout, stderr = ret['Value']

        if (os.system('grep Broken simtel.log')):
            DIRAC.gLogger.notice('not broken')
        else:
            DIRAC.gLogger.notice('broken')
            jobReport.setApplicationStatus('Broken pipe')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute run_sim.sh')
            DIRAC.gLogger.error('run_sim.sh status is:', simtelReturnCode)
            DIRAC.exit(-1)

## putAndRegister simtel data/log/histo Output File:
        cfg = cfg_dict[current_conf]
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Data/*.simtel.gz ' + simtelFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

############################################
        simtelRunNumberSeriesDirExist = fcc.isDirectory(
            simtelOutFileDir)['Value']['Successful'][simtelOutFileDir]
        newSimtelRunFileSeriesDir = (
            simtelRunNumberSeriesDirExist != True
        )  # if new runFileSeries, will need to add new MD

        simtelLogFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.log.gz'
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Log/*.log.gz ' + simtelLogFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)
        simtelOutLogFileDir = os.path.join(simtelDirPath_conf, 'Log',
                                           runNumSeriesDir)
        simtelOutLogFileLFN = os.path.join(simtelOutLogFileDir,
                                           simtelLogFileName)

        simtelHistFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.hdata.gz'
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Histograms/*.hdata.gz ' + simtelHistFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)
        simtelOutHistFileDir = os.path.join(simtelDirPath_conf, 'Histograms',
                                            runNumSeriesDir)
        simtelOutHistFileLFN = os.path.join(simtelOutHistFileDir,
                                            simtelHistFileName)

        ########### quality check on Histo Missing because it needs the NSHOW #############################################
        ########## quality check on Log #############################
        cmd = 'zcat %s | grep Finished.' % simtelLogFileName
        DIRAC.gLogger.notice('Executing system call:', cmd)
        if (os.system(cmd)):
            jobReport.setApplicationStatus('Log check Failed')
            DIRAC.exit(-1)

################################################
        from DIRAC.Core.Utilities import List
        from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations
        opsHelper = Operations()

        global seList
        seList = opsHelper.getValue('ProductionOutputs/SimtelProd', [])
        seList = List.randomize(seList)

        DIRAC.gLogger.notice('SeList is:', seList)

        #########  Upload simtel data/log/histo ##############################################
        res = upload_to_seList(simtelOutFileLFN, simtelFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('OutputData Upload Error', simtelOutFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        res = CheckCatalogCoherence(simtelOutLogFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Log file already exists. Removing:',
                                 simtelOutLogFileLFN)
            ret = dirac.removeFile(simtelOutLogFileLFN)

        res = upload_to_seList(simtelOutLogFileLFN, simtelLogFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload simtel Log Error', simtelOutLogFileLFN)
            DIRAC.gLogger.notice('Removing simtel data file:',
                                 simtelOutFileLFN)
            ret = dirac.removeFile(simtelOutFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        res = CheckCatalogCoherence(simtelOutHistFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Histo file already exists. Removing:',
                                 simtelOutHistFileLFN)
            ret = dirac.removeFile(simtelOutHistFileLFN)

        res = upload_to_seList(simtelOutHistFileLFN, simtelHistFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload simtel Histo Error',
                                simtelOutHistFileLFN)
            DIRAC.gLogger.notice('Removing simtel data file:',
                                 simtelOutFileLFN)
            ret = dirac.removeFile(simtelOutFileLFN)
            DIRAC.gLogger.notice('Removing simtel log file:',
                                 simtelOutLogFileLFN)
            ret = dirac.removeFile(simtelOutLogFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)
####################################################################

        if newSimtelRunFileSeriesDir:
            insertRunFileSeriesMD(simtelOutFileDir, runNumTrunc)
            insertRunFileSeriesMD(simtelOutLogFileDir, runNumTrunc)
            insertRunFileSeriesMD(simtelOutHistFileDir, runNumTrunc)


###### simtel File level metadata ############################################
        simtelFileMD = {}
        simtelFileMD['runNumber'] = int(run_number)
        simtelFileMD['jobID'] = jobID
        simtelFileMD['simtelReturnCode'] = simtelReturnCode

        result = fcc.setMetadata(simtelOutFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(simtelOutLogFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(simtelOutHistFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.addFileAncestors(
            {simtelOutFileLFN: {
                'Ancestors': [corsikaFileLFN]
            }})
        print 'result addFileAncestor:', result

        result = fcc.addFileAncestors(
            {simtelOutLogFileLFN: {
                'Ancestors': [corsikaFileLFN]
            }})
        print 'result addFileAncestor:', result

        result = fcc.addFileAncestors(
            {simtelOutHistFileLFN: {
                'Ancestors': [corsikaFileLFN]
            }})
        print 'result addFileAncestor:', result

        result = fcc.setMetadata(simtelOutFileLFN, simtelFileMD)
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

    DIRAC.exit()
Example #6
0
'''
Created on 2015-05-19 21:45:37

@author: suo
'''
import sys
from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport
from DIRAC.Core.Base import Script
Script.parseCommandLine(ignoreErrors=False)

jobID = sys.argv[1]
experiment = sys.argv[2]
message = sys.argv[3]

jobReport = JobReport(jobID, experiment)
result = jobReport.setApplicationStatus(message)
if not result['OK']:
    try:
        with open('job.err', 'a') as errFile:
            print >> errFile, 'setJobStatus error: %s' % result
    except IOError:
        print 'IOError:', str(e)
Example #7
0
def main():

    from DIRAC.Core.Base import Script
    Script.initialize()

    DIRAC.gLogger.notice('Platform is:')
    os.system('dirac-platform')
    from CTADIRAC.Core.Workflow.Modules.Read_CtaApp import Read_CtaApp
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    version = sys.argv[3]
    DIRAC.gLogger.notice('Version:', version)
    install_CorsikaSimtelPack(version)

    ######### run read_cta #######################################

    rcta = Read_CtaApp()
    CorsikaSimtelPack = os.path.join('corsika_simhessarray', version,
                                     'corsika_simhessarray')
    rcta.setSoftwarePackage(CorsikaSimtelPack)
    rcta.rctaExe = 'read_cta'

    # add arguments for read_cta specified by user ######
    args = []
    rctaparfile = open('read_cta.par', 'r').readlines()
    for line in rctaparfile:
        for word in line.split():
            args.append(word)

    simtelFileLFN = sys.argv[-1].split('ParametricInputData=LFN:')[1]
    simtelFileName = os.path.basename(simtelFileLFN)
    dstFileName = simtelFileName.replace('simtel.gz', 'simtel-dst0.gz')
    dstHistoFileName = simtelFileName.replace('simtel.gz', 'hdata-dst0.gz')

    args.extend([
        '--dst-file', dstFileName, '--histogram-file', dstHistoFileName,
        simtelFileName
    ])
    rcta.rctaArguments = args

    rctaReturnCode = rcta.execute()

    if rctaReturnCode != 0:
        DIRAC.gLogger.error('read_cta Application: Failed')
        jobReport.setApplicationStatus('read_cta Application: Failed')
        DIRAC.exit(-1)


#################################################################
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    ret = getSoftwareEnviron(CorsikaSimtelPack)

    if not ret['OK']:
        error = ret['Message']
        DIRAC.gLogger.error(error, CorsikaSimtelPack)
        DIRAC.exit(-1)

    read_ctaEnviron = ret['Value']

    ######## run dst quality checks ######################################

    fd = open('check_dst_histo.sh', 'w')
    fd.write("""#! /bin/sh  
dsthistfilename=%s
dstfile=%s
n6="$(list_histograms -h 6 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')"
n12001="$(list_histograms -h 12001 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')"
if [ $n6 -ne $n12001 ]; then
echo 'n6 found:' $n6
echo 'n12001 found:' $n12001
exit 1
else
echo 'n6 found:' $n6
echo 'n12001 found:' $n12001
fi

n12002="$(list_histograms -h 12002 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')"
nev="$(statio ${dstfile} | egrep '^2010' | cut -f2)"
if [ -z "$nev" ]; then nev="0"; fi

if [ $nev -ne $n12002 ]; then
echo 'nev found:' $nev
echo 'n12002 found:' $n12002
exit 1
else
echo 'nev found:' $nev
echo 'n12002 found:' $n12002
fi
""" % (dstHistoFileName, dstFileName))
    fd.close()

    os.system('chmod u+x check_dst_histo.sh')
    cmdTuple = ['./check_dst_histo.sh']
    DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
    ret = systemCall(0, cmdTuple, sendOutput, env=read_ctaEnviron)
    checkHistoReturnCode, stdout, stderr = ret['Value']

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute check_dst_histo.sh')
        DIRAC.gLogger.error('check_dst_histo.sh status is:',
                            checkHistoReturnCode)
        DIRAC.exit(-1)

    if (checkHistoReturnCode != 0):
        DIRAC.gLogger.error('Failure during check_dst_histo.sh')
        DIRAC.gLogger.error('check_dst_histo.sh status is:',
                            checkHistoReturnCode)
        jobReport.setApplicationStatus('Histo check Failed')
        DIRAC.exit(-1)

    DIRAC.exit()
Example #8
0
#!/usr/bin/env python

import os
import sys

from DIRAC.Core.Base import Script

Script.initialize(ignoreErrors=True)

from DIRAC.Interfaces.API.Dirac import Dirac
from DIRAC.Interfaces.API.Job import Job

from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

jobID = os.environ.get('DIRACJOBID', '0')

if not jobID:
    print 'DIRAC job ID not found'
    sys.exit(1)

jobReport = JobReport(jobID, 'JUNO_JobScript')
result = jobReport.setApplicationStatus(', '.join(sys.argv[1:]))
if not result['OK']:
    print 'Set application status error: %s' % result
Example #9
0
def main():

    from DIRAC.Core.Base import Script

    #### eventio_cta options ##########################################
    Script.registerSwitch("T:", "tellist=", "Tellist", setTellist)
    Script.registerSwitch("F:", "Nfirst_mcevt=", "Nfirst_mcevt",
                          setNfirst_mcevt)
    Script.registerSwitch("L:", "Nlast_mcevt=", "Nlast_mcevt", setNlast_mcevt)
    ## add other eventio_cta options ################################
    #  Script.registerSwitch( "N:", "num=", "Num", setNum)
    ##  Script.registerSwitch( "L:", "limitmc=", "Limitmc", setLimitmc)
    #  Script.registerSwitch( "S:", "telidoffset=", "Telidoffset", setTelidoffset)
    Script.registerSwitch("P:", "pixelslices=", "setPixelslices (true/false)",
                          setPixelslices)
    Script.registerSwitch("p:", "run_number=",
                          "Run Number (set automatically)", setRunNumber)
    ### other options ###############################################
    Script.registerSwitch("V:", "version=", "HAP version", setVersion)

    Script.parseCommandLine(ignoreErrors=True)

    args = Script.getPositionalArgs()

    if len(args) < 1:
        Script.showHelp()

    if tellist == None or version == None:
        Script.showHelp()
        jobReport.setApplicationStatus('Options badly specified')
        DIRAC.exit(-1)

    from CTADIRAC.Core.Workflow.Modules.HapApplication import HapApplication
    from CTADIRAC.Core.Workflow.Modules.HapRootMacro import HapRootMacro
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    HapPack = 'HAP/' + version + '/HAP'

    packs = ['HESS/v0.2/lib', 'HESS/v0.3/root', HapPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                continue
        if localArea:
            if checkSoftwarePackage(package, localArea())['OK']:
                DIRAC.gLogger.notice('Package found in Local Area:', package)
                continue
            if installSoftwarePackage(package, localArea())['OK']:
                continue
        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

    telconf = os.path.join(localArea(),
                           'HAP/%s/config/%s' % (version, tellist))

    ha = HapApplication()
    ha.setSoftwarePackage(HapPack)
    ha.hapExecutable = 'eventio_cta'

    fileout = 'raw_' + part_type + '_run' + run_number + '.root'
    infile = build_infile()
    ha.hapArguments = ['-file', infile, '-o', fileout, '-tellist', telconf]

    try:
        ha.hapArguments.extend(
            ['-Nfirst_mcevt', Nfirst_mcevt, '-Nlast_mcevt', Nlast_mcevt])
    except NameError:
        DIRAC.gLogger.info('Nfirst_mcevt/Nlast_mcevt options are not used')

    try:
        if (pixelslices == 'true'):
            ha.hapArguments.extend(['-pixelslices'])
    except NameError:
        DIRAC.gLogger.info('pixelslices option is not used')

    DIRAC.gLogger.notice('Executing Hap Converter Application')
    res = ha.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Failed to execute eventio_cta Application')
        jobReport.setApplicationStatus('eventio_cta: Failed')
        DIRAC.exit(-1)

    if not os.path.isfile(fileout):
        error = 'raw file was not created:'
        DIRAC.gLogger.error(error, fileout)
        jobReport.setApplicationStatus('eventio_cta: RawData not created')
        DIRAC.exit(-1)

###################### Check RAW DATA: step0 #######################
    hr = HapRootMacro()
    hr.setSoftwarePackage(HapPack)

    DIRAC.gLogger.notice('Executing RAW check step0')
    hr.rootMacro = '/hapscripts/dst/Open_Raw.C+'
    outfilestr = '"' + fileout + '"'
    args = [outfilestr]
    DIRAC.gLogger.notice('Open_Raw macro Arguments:', args)
    hr.rootArguments = args
    DIRAC.gLogger.notice('Executing Hap Open_Raw macro')
    res = hr.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Open_Raw: Failed')
        DIRAC.exit(-1)


#####################Check RAW DATA: step1 ##################
    DIRAC.gLogger.notice('Executing Raw Check step1')

    ret = getSoftwareEnviron(HapPack)
    if not ret['OK']:
        error = ret['Message']
        DIRAC.gLogger.error(error, HapPack)
        DIRAC.exit(-1)

    hapEnviron = ret['Value']
    hessroot = hapEnviron['HESSROOT']
    check_script = hessroot + '/hapscripts/dst/check_raw.csh'
    cmdTuple = [check_script]
    ret = systemCall(0, cmdTuple, sendOutput)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute RAW Check step1')
        jobReport.setApplicationStatus('Check_raw: Failed')
        DIRAC.exit(-1)

    status, stdout, stderr = ret['Value']
    if status == 1:
        jobReport.setApplicationStatus(
            'RAW Check step1: Big problem during RAW production')
        DIRAC.gLogger.error('Check_raw: Big problem during RAW production')
        DIRAC.exit(-1)

    DIRAC.exit()
Example #10
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("T:", "template=", "Corsika Template")
    Script.registerSwitch("p:", "run_number=",
                          "Do not use: Run Number automatically set")
    Script.registerSwitch("E:", "executable=",
                          "Executable (Use SetExecutable)")
    Script.registerSwitch("v:", "version=", "Version (Use setVersion)")
    Script.registerSwitch("D:", "dcta=", "dcta")
    Script.registerSwitch("I:", "icta=", "icta")
    Script.registerSwitch("C:", "c_cta=", "c_cta")

    Script.parseCommandLine(ignoreErrors=False)

    ## default values ##############
    run_number = None
    template = None
    executable = None
    version = None

    ### set switch values ###
    for switch in Script.getUnprocessedSwitches():
        if switch[0] == "run_number" or switch[0] == "p":
            run_number = switch[1].split('ParametricParameters=')[1]
        elif switch[0] == "template" or switch[0] == "T":
            template = switch[1]
        elif switch[0] == "executable" or switch[0] == "E":
            executable = switch[1]
        elif switch[0] == "version" or switch[0] == "v":
            version = switch[1]

    if version == None or executable == None or run_number == None or template == None:
        Script.showHelp()
        jobReport.setApplicationStatus('Missing options')
        DIRAC.exit(-1)

    from CTADIRAC.Core.Workflow.Modules.CorsikaApp import CorsikaApp
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'

    packs = [CorsikaSimtelPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                installSoftwareEnviron(package, workingArea())
                packageTuple = package.split('/')
                corsika_subdir = sharedArea(
                ) + '/' + packageTuple[0] + '/' + version
                cmd = 'cp -u -r ' + corsika_subdir + '/* .'
                os.system(cmd)
                continue
        if workingArea:
            if checkSoftwarePackage(package, workingArea())['OK']:
                DIRAC.gLogger.notice('Package found in Local Area:', package)
                continue
            if installSoftwarePackage(package, workingArea())['OK']:
                ############## compile #############################
                cmdTuple = ['./build_all', 'prod2', 'qgs2']
                ######### special case for Astri ############################
                if version == 'prod-2_08072014_to':
                    ############## compile #############################
                    fd = open('run_compile.sh', 'w')
                    fd.write("""#! /bin/sh  
source ./build_all prod2 qgs2
#
echo " Let's check that build_all did its work " 
ls -alFsh 
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo " Let's see what files are in the corsika-run directory " 
ls -alFsh ./corsika-run
#
if [ ! -x ./corsika-run/corsika ]
then 
    echo " ERROR: Corsika executable found. Exit " 
    exit 1
fi
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
#
echo " Now let's try to compile hessio according to Federico's recipe "
cd ./hessioxxx 
make clean 
make EXTRA_DEFINES="-DCTA_PROD2 -DWITH_LOW_GAIN_CHANNEL"
# 
echo " Let's see what files are in the lib directory " 
ls -alFsh ./lib
#
if [ ! -f ./lib/libhessio.so ]
then 
    echo " ERROR: libhessio library not found. Exit " 
    exit 1
fi
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
#
cd .. # come back to original dir
# 
echo " Now let's try to compile simtel according to Federico's recipe "
cd ./sim_telarray
make clean 
make EXTRA_DEFINES="-DCTA_PROD2 -DWITH_LOW_GAIN_CHANNEL"
make install 
# 
echo " Let's see what files are in the bin directory " 
ls -alFsh ./bin
#
if [ ! -x ./bin/sim_telarray ]
then 
    echo " ERROR: sim_telarray excutable not found. Exit " 
    exit 1
fi
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
#
echo " Everything was compiled and linked properly" """)
                    fd.close()
                    os.system('chmod u+x run_compile.sh')
                    cmdTuple = ['./run_compile.sh']
##########################################################################
                ret = systemCall(0, cmdTuple, sendOutput)
                if not ret['OK']:
                    DIRAC.gLogger.error('Failed to execute build')
                    DIRAC.exit(-1)
                continue

        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

###### execute corsika ###############
    cs = CorsikaApp()
    cs.setSoftwarePackage(CorsikaSimtelPack)
    cs.csExe = executable
    cs.csArguments = ['--run-number', run_number, '--run', 'corsika', template]
    corsikaReturnCode = cs.execute()

    if corsikaReturnCode != 0:
        DIRAC.gLogger.error('Failed to execute corsika Application')
        jobReport.setApplicationStatus('Corsika Application: Failed')
        DIRAC.exit(-1)

###### rename corsika file #################################
    rundir = 'run' + run_number
    corsikaKEYWORDS = ['TELFIL']
    dictCorsikaKW = fileToKWDict(template, corsikaKEYWORDS)
    corsikafilename = rundir + '/' + dictCorsikaKW['TELFIL'][0]
    destcorsikafilename = 'corsika_run' + run_number + '.corsika.gz'
    cmd = 'mv ' + corsikafilename + ' ' + destcorsikafilename
    os.system(cmd)

    ### create corsika tar ####################
    corsika_tar = 'corsika_run' + run_number + '.tar.gz'
    filetar1 = rundir + '/' + 'input'
    filetar2 = rundir + '/' + 'DAT' + run_number + '.dbase'
    filetar3 = rundir + '/run' + str(int(run_number)) + '.log'
    cmdTuple = ['/bin/tar', 'zcf', corsika_tar, filetar1, filetar2, filetar3]
    DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
    ret = systemCall(0, cmdTuple, sendOutput)
    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute tar')
        DIRAC.exit(-1)

###### execute sim_telarray ###############
    ret = getSoftwareEnviron(CorsikaSimtelPack)
    if not ret['OK']:
        error = ret['Message']
        DIRAC.gLogger.error(error, CorsikaSimtelPack)
        DIRAC.exit(-1)

    corsikaEnviron = ret['Value']
    cmdTuple = ['sim_telarray']
    # add input file argument for sim_telarray  ###################
    inputfile = 'input_file=' + destcorsikafilename
    inputfileopt = ['-C', inputfile]
    cmdTuple.extend(inputfileopt)
    # add output file argument for sim_telarray
    destsimtelfilename = 'simtel_run' + run_number + '.simtel.gz'
    outputfile = 'output_file=' + destsimtelfilename
    outputfileopt = ['-C', outputfile]
    cmdTuple.extend(outputfileopt)
    # add histo argument for sim_telarray
    desthistofilename = 'simtel_run' + run_number + '.hdata.gz'
    histofile = 'histogram_file=' + desthistofilename
    histofileopt = ['-C', histofile]
    cmdTuple.extend(histofileopt)

    # add other arguments for sim_telarray specified by user ######
    simtelparfile = open('simtel.par', 'r').readlines()

    for line in simtelparfile:
        for word in line.split():
            cmdTuple.append(word)

    DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
    ret = systemCall(0, cmdTuple, sendSimtelOutput, env=corsikaEnviron)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute:', simexe)
        DIRAC.exit(-1)

    DIRAC.exit()
Example #11
0
def main():

  from DIRAC.Core.Base import Script

  Script.registerSwitch( "p:", "run_number=", "Run Number", setRunNumber )
  Script.registerSwitch( "R:", "run=", "Run", setRun )
  Script.registerSwitch( "P:", "config_path=", "Config Path", setConfigPath )
  Script.registerSwitch( "T:", "template=", "Template", setTemplate )
  Script.registerSwitch( "E:", "executable=", "Executable", setExecutable )
  Script.registerSwitch( "V:", "version=", "Version", setVersion )
  Script.registerSwitch( "M:", "mode=", "Mode", setMode )
  
  Script.parseCommandLine( ignoreErrors = True )
  args = Script.getPositionalArgs()

  if len( args ) < 1:
    Script.showHelp()
  
  if version == None or executable == None or run_number == None or run == None or template == None:
    Script.showHelp()
    jobReport.setApplicationStatus('Options badly specified')
    DIRAC.exit( -1 )

  from CTADIRAC.Core.Workflow.Modules.CorsikaApp import CorsikaApp
  from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
  from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
  from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
  from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
  from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
  from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
  from DIRAC.Core.Utilities.Subprocess import systemCall
  from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

  jobID = os.environ['JOBID']
  jobID = int( jobID )
  jobReport = JobReport( jobID )

  CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'

  packs = [CorsikaSimtelPack]

  for package in packs:
    DIRAC.gLogger.notice( 'Checking:', package )
    if sharedArea:
      if checkSoftwarePackage( package, sharedArea() )['OK']:
        DIRAC.gLogger.notice( 'Package found in Shared Area:', package )
        installSoftwareEnviron( package, workingArea() )
        packageTuple =  package.split('/')
        corsika_subdir = sharedArea() + '/' + packageTuple[0] + '/' + version
        cmd = 'cp -r ' + corsika_subdir + '/* .'        
        os.system(cmd)
        continue
    if workingArea:
      if checkSoftwarePackage( package, workingArea() )['OK']:
        DIRAC.gLogger.notice( 'Package found in Local Area:', package )
        continue
      if installSoftwarePackage( package, workingArea() )['OK']:
      ############## compile #############################
        if version == 'clean_23012012':
          cmdTuple = ['./build_all','ultra','qgs2']
        elif version in ['prod-2_21122012','prod-2_08032013','prod-2_06052013']:
          cmdTuple = ['./build_all','prod2','qgs2']
        ret = systemCall( 0, cmdTuple, sendOutput)
        if not ret['OK']:
          DIRAC.gLogger.error( 'Failed to execute build')
          DIRAC.exit( -1 )
        continue

    DIRAC.gLogger.error( 'Check Failed for software package:', package )
    DIRAC.gLogger.error( 'Software package not available')
    DIRAC.exit( -1 )  


  cs = CorsikaApp()

  cs.setSoftwarePackage(CorsikaSimtelPack)

  cs.csExe = executable

  cs.csArguments = ['--run-number',run_number,'--run',run,template]

  corsikaReturnCode = cs.execute()
  
  if corsikaReturnCode != 0:
    DIRAC.gLogger.error( 'Failed to execute corsika Application')
    jobReport.setApplicationStatus('Corsika Application: Failed')
    DIRAC.exit( -1 )
    
###### rename corsika file #################################
  rundir = 'run' + run_number
  corsikaKEYWORDS = ['TELFIL']
  dictCorsikaKW = fileToKWDict(template,corsikaKEYWORDS)
  corsikafilename = rundir + '/' + dictCorsikaKW['TELFIL'][0]
  destcorsikafilename = 'corsika_run' + run_number + '.corsika.gz'
  cmd = 'mv ' + corsikafilename + ' ' + destcorsikafilename
  os.system(cmd)
 
  ### create corsika tar ####################
  corsika_tar = 'corsika_run' + run_number + '.tar.gz'
  filetar1 = rundir + '/'+'input'
  filetar2 = rundir + '/'+ 'DAT' + run_number + '.dbase'
  filetar3 = rundir + '/run' + str(int(run_number)) + '.log'
  cmdTuple = ['/bin/tar','zcf',corsika_tar, filetar1,filetar2,filetar3]
  DIRAC.gLogger.notice( 'Executing command tuple:', cmdTuple )
  ret = systemCall( 0, cmdTuple, sendOutput)
  if not ret['OK']:
    DIRAC.gLogger.error( 'Failed to execute tar')
    DIRAC.exit( -1 )
    
  DIRAC.exit()
Example #12
0
class FailoverRequest(ModuleBase):
  """ Handle the failover requests issued by previous steps. Used in production. 
  """
  #############################################################################
  def __init__(self):
    """Module initialization.
    """
    super(FailoverRequest, self).__init__()
    self.version = __RCSID__
    self.log = gLogger.getSubLogger( "FailoverRequest" )
    #Internal parameters
    self.enable = True
    self.jobID = ''
    self.productionID = None
    self.prodJobID = None
    #Workflow parameters
    self.jobReport  = None
    self.fileReport = None
    self.request = None

  #############################################################################
  def applicationSpecificInputs(self):
    """ By convention the module input parameters are resolved here.
    """
    self.log.debug(self.workflow_commons)
    self.log.debug(self.step_commons)

    if os.environ.has_key('JOBID'):
      self.jobID = os.environ['JOBID']
      self.log.verbose('Found WMS JobID = %s' %self.jobID)
    else:
      self.log.info('No WMS JobID found, disabling module via control flag')
      self.enable = False

    if self.step_commons.has_key('Enable'):
      self.enable = self.step_commons['Enable']
      if not type(self.enable) == type(True):
        self.log.warn('Enable flag set to non-boolean value %s, setting to False' % self.enable)
        self.enable = False

    #Earlier modules will have populated the report objects
    if self.workflow_commons.has_key('JobReport'):
      self.jobReport = self.workflow_commons['JobReport']

    if self.workflow_commons.has_key('FileReport'):
      self.fileReport = self.workflow_commons['FileReport']

    if self.InputData:
      if type(self.InputData) != type([]):
        self.InputData = self.InputData.split(';')

      self.InputData = [x.replace('LFN:','') for x in self.InputData]

    if self.workflow_commons.has_key('Request'):
      self.request = self.workflow_commons['Request']
    if not self.request:
      self.request = RequestContainer()
      self.request.setRequestName('job_%s_request.xml' % self.jobID)
      self.request.setJobID(self.jobID)
      self.request.setSourceComponent("Job_%s" % self.jobID)

    if self.workflow_commons.has_key('PRODUCTION_ID'):
      self.productionID = self.workflow_commons['PRODUCTION_ID']

    if self.workflow_commons.has_key('JOB_ID'):
      self.prodJobID = self.workflow_commons['JOB_ID']

    return S_OK('Parameters resolved')

  #############################################################################
  def execute(self):
    """ Main execution function.
    """
    self.log.info('Initializing %s' % self.version)
    result = self.resolveInputVariables()
    if not result['OK']:
      self.log.error(result['Message'])
      return result

    if not self.fileReport:
      self.fileReport =  FileReport('Transformation/TransformationManager')

    if self.InputData:
      inputFiles = self.fileReport.getFiles()
      for lfn in self.InputData:
        if not lfn in inputFiles:
          self.log.verbose('No status populated for input data %s, setting to "Unused"' % lfn)
          result = self.fileReport.setFileStatus(int(self.productionID), lfn, 'Unused')

    if not self.workflowStatus['OK'] or not self.stepStatus['OK']:
      self.log.info('Workflow status = %s, step status = %s' %(self.workflowStatus['OK'], self.stepStatus['OK']))
      inputFiles = self.fileReport.getFiles()
      for lfn in inputFiles:
        if inputFiles[lfn] != 'ApplicationCrash':
          self.log.info('Forcing status to "Unused" due to workflow failure for: %s' % (lfn))
          self.fileReport.setFileStatus(int(self.productionID), lfn, 'Unused')
    else:
      inputFiles = self.fileReport.getFiles()
      if inputFiles:
        self.log.info('Workflow status OK, setting input file status to Processed')                
      for lfn in inputFiles:
        self.log.info('Setting status to "Processed" for: %s' % (lfn))
        self.fileReport.setFileStatus(int(self.productionID), lfn, 'Processed')  

    result = self.fileReport.commit()
    if not result['OK']:
      self.log.error('Failed to report file status to ProductionDB, request will be generated', result['Message'])
    else:
      self.log.info('Status of files have been properly updated in the ProcessingDB')

    # Must ensure that the local job report instance is used to report the final status
    # in case of failure and a subsequent failover operation
    if self.workflowStatus['OK'] and self.stepStatus['OK']: 
      if not self.jobReport:
        self.jobReport = JobReport(int(self.jobID))
      jobStatus = self.jobReport.setApplicationStatus('Job Finished Successfully')
      if not jobStatus['OK']:
        self.log.warn(jobStatus['Message'])

    # Retrieve the accumulated reporting request
    reportRequest = None
    if self.jobReport:
      result = self.jobReport.generateRequest()
      if not result['OK']:
        self.log.warn('Could not generate request for job report with result:\n%s' % (result))
      else:
        reportRequest = result['Value']
    if reportRequest:
      self.log.info('Populating request with job report information')
      self.request.update(reportRequest)

    fileReportRequest = None
    if self.fileReport:
      result = self.fileReport.generateRequest()
      if not result['OK']:
        self.log.warn('Could not generate request for file report with result:\n%s' % (result))
      else:
        fileReportRequest = result['Value']
    if fileReportRequest:
      self.log.info('Populating request with file report information')
      result = self.request.update(fileReportRequest)

    accountingReport = None
    if self.workflow_commons.has_key('AccountingReport'):
      accountingReport = self.workflow_commons['AccountingReport']
    if accountingReport:
      result = accountingReport.commit()
      if not result['OK']:
        self.log.info('Populating request with accounting report information')
        self.request.setDISETRequest(result['rpcStub'])

    if self.request.isEmpty()['Value']:
      self.log.info('Request is empty, nothing to do.')
      return self.finalize()

    request_string = self.request.toXML()['Value']
    self.log.debug(request_string)
    # Write out the request string
    fname = '%s_%s_request.xml' % (self.productionID, self.prodJobID)
    xmlfile = open(fname, 'w')
    xmlfile.write(request_string)
    xmlfile.close()
    self.log.info('Creating failover request for deferred operations for job %s:' % self.jobID)
    result = self.request.getDigest()
    if result['OK']:
      digest = result['Value']
      self.log.info(digest)

    if not self.enable:
      self.log.info('Module is disabled by control flag')
      return S_OK('Module is disabled by control flag')

    return self.finalize()

  #############################################################################
  def finalize(self):
    """ Finalize and report correct status for the workflow based on the workflow
        or step status.
    """
    self.log.verbose('Workflow status = %s, step status = %s' % (self.workflowStatus['OK'], self.stepStatus['OK']))
    if not self.workflowStatus['OK'] or not self.stepStatus['OK']:
      self.log.warn('Workflow status is not ok, will not overwrite status')
      self.log.info('Workflow failed, end of FailoverRequest module execution.')
      return S_ERROR('Workflow failed, FailoverRequest module completed')

    self.log.info('Workflow successful, end of FailoverRequest module execution.')
    return S_OK('FailoverRequest module completed')

#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#
Example #13
0
def main():

    from DIRAC.Core.Base import Script
    Script.initialize()

    DIRAC.gLogger.notice('Platform is:')
    os.system('dirac-platform')
    from DIRAC.DataManagementSystem.Client.DataManager import DataManager
    from CTADIRAC.Core.Workflow.Modules.EvnDispApp import EvnDispApp
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    version = sys.argv[3]
    DIRAC.gLogger.notice('Version:', version)

    EvnDispPack = os.path.join('evndisplay', version, 'evndisplay')

    packs = [EvnDispPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if checkSoftwarePackage(package, sharedArea())['OK']:
            DIRAC.gLogger.notice('Package found in Shared Area:', package)
            installSoftwareEnviron(package, sharedArea())
            continue
        else:
            installSoftwarePackage(package, workingArea())
            DIRAC.gLogger.notice('Package found in workingArea:', package)
            continue

        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

    ed = EvnDispApp()
    ed.setSoftwarePackage(EvnDispPack)

    dstFileLFNList = sys.argv[-1].split('ParametricParameters={')[1].split(
        '}')[0].replace(',', ' ')

    args = []
    i = 0
    for word in dstFileLFNList.split():
        i = i + 1
        dstfile = os.path.basename(word)
        ###### execute evndisplay stage1 ###############
        executable = sys.argv[5]
        logfileName = executable + '_' + str(i) + '.log'
        args = ['-sourcefile', dstfile, '-outputdirectory', 'outdir']
        # add other arguments for evndisp specified by user ######
        evndispparfile = open('evndisp.par', 'r').readlines()
        for line in evndispparfile:
            for word in line.split():
                args.append(word)

        execute_module(ed, executable, args)

        for name in glob.glob('outdir/*.root'):
            evndispOutFile = name.split('.root')[0] + '_' + str(
                jobID) + '_evndisp.root'
            cmd = 'mv ' + name + ' ' + os.path.basename(evndispOutFile)
            if (os.system(cmd)):
                DIRAC.exit(-1)

########### quality check on Log #############################################
        cmd = 'mv ' + executable + '.log' + ' ' + logfileName
        if (os.system(cmd)):
            DIRAC.exit(-1)
        fd = open('check_log.sh', 'w')
        fd.write("""#! /bin/sh
if grep -i "error" %s; then
exit 1
fi
if grep "Final checks on result file (seems to be OK):" %s; then
exit 0
else
exit 1
fi
""" % (logfileName, logfileName))
        fd.close()

        os.system('chmod u+x check_log.sh')
        cmd = './check_log.sh'
        DIRAC.gLogger.notice('Executing system call:', cmd)
        if (os.system(cmd)):
            jobReport.setApplicationStatus('EvnDisp Log Check Failed')
            DIRAC.exit(-1)


##################################################################
########### remove the dst file #############################################
        cmd = 'rm ' + dstfile
        if (os.system(cmd)):
            DIRAC.exit(-1)

    DIRAC.exit()
Example #14
0
def main():

    from DIRAC.Core.Base import Script
    ### DoCtaIrf options ##########################################################
    Script.registerSwitch("A:", "analysis=", "Analysis Type", setAnalysisType)
    Script.registerSwitch("C:", "cuts=", "Cuts Config", setCutsConfig)
    Script.registerSwitch("R:", "runlist=", "Runlist", setRunlist)
    Script.registerSwitch("Z:", "zenith=", "Zenith", setZenith)
    Script.registerSwitch("O:", "offset=", "Offset", setOffset)
    Script.registerSwitch("M:", "energy=", "Energy Method", setEnergyMethod)
    Script.registerSwitch("T:", "arrayconfig=", "Array Configuration",
                          setArrayConfig)
    Script.registerSwitch("P:", "particle=", "Particle Type", setParticleType)
    ## other options
    Script.registerSwitch("V:", "version=", "HAP version", setVersion)

    Script.parseCommandLine(ignoreErrors=True)

    args = Script.getPositionalArgs()
    if len(args) < 1:
        Script.showHelp()

    from CTADIRAC.Core.Workflow.Modules.HapApplication import HapApplication
    from CTADIRAC.Core.Workflow.Modules.HapRootMacro import HapRootMacro
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    ha = HapApplication()

    HapPack = 'HAP/' + version + '/HAP'

    packs = ['HESS/v0.2/lib', 'HESS/v0.3/root', HapPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                continue
        if localArea:
            if checkSoftwarePackage(package, localArea())['OK']:
                DIRAC.gLogger.notice('Package found in Local Area:', package)
                continue
            if installSoftwarePackage(package, localArea())['OK']:
                continue
        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

    ha.setSoftwarePackage(HapPack)

    ha.hapExecutable = 'DoCtaIrf'

    runlistdir = os.environ['PWD']

    build_infile(runlist)

    ha.hapArguments = [
        analysistype, cutsconfig, runlistdir, runlist, zenith, offset,
        arrayconfig, energymethod, particle
    ]

    DIRAC.gLogger.notice('Executing Hap Application')
    res = ha.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Failed to execute Hap Application')
        jobReport.setApplicationStatus('Hap Application: Failed')
        DIRAC.exit(-1)

###################### Check TTree Output File #######################
    outfile = 'MVAFile_' + runlist + ".root"

    if not os.path.isfile(outfile):
        error = 'TTree file was not created:'
        DIRAC.gLogger.error(error, outfile)
        jobReport.setApplicationStatus('DoCtaIrf: TTree file not created')
        DIRAC.exit(-1)

###################### Quality Check for TTree Output File: step0######################
    hr = HapRootMacro()
    hr.setSoftwarePackage(HapPack)

    DIRAC.gLogger.notice('Executing TTree check step0')
    hr.rootMacro = '/hapscripts/mva/Open_TT.C+'
    outfilestr = '"' + outfile + '"'
    args = [outfilestr]
    DIRAC.gLogger.notice('Open_TT macro Arguments:', args)
    hr.rootArguments = args
    DIRAC.gLogger.notice('Executing Hap Open_TT macro')
    res = hr.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Open_TT: Failed')
        DIRAC.exit(-1)

#########################Quality Check for TTree Output File: step1####################
    DIRAC.gLogger.notice('Executing TTree check step1')

    ret = getSoftwareEnviron(HapPack)
    if not ret['OK']:
        error = ret['Message']
        DIRAC.gLogger.error(error, HapPack)
        DIRAC.exit(-1)

    hapEnviron = ret['Value']
    hessroot = hapEnviron['HESSROOT']

    check_script = hessroot + '/hapscripts/mva/check_TT.csh'

    cmdTuple = [check_script]
    ret = systemCall(0, cmdTuple, sendOutput)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute TTree Check step1')
        jobReport.setApplicationStatus('Check_TTree: Failed')
        DIRAC.exit(-1)


#############################################

    DIRAC.exit()
Example #15
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("p:", "run_number=", "Run Number", setRunNumber)
    Script.registerSwitch("T:", "template=", "Template", setCorsikaTemplate)
    Script.registerSwitch("E:", "executable=", "Executable", setExecutable)
    Script.registerSwitch("S:", "simtelConfig=", "SimtelConfig", setConfig)
    Script.registerSwitch("V:", "version=", "Version", setVersion)
    Script.registerSwitch("M:", "mode=", "Mode", setMode)
    Script.registerSwitch("C:", "savecorsika=", "Save Corsika", setSaveCorsika)

    from DIRAC.Resources.Catalog.FileCatalogClient import FileCatalogClient
    from DIRAC.Resources.Catalog.FileCatalog import FileCatalog

    Script.parseCommandLine()
    global fcc, fcL, storage_element

    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from CTADIRAC.Core.Workflow.Modules.CorsikaApp import CorsikaApp
    from CTADIRAC.Core.Workflow.Modules.Read_CtaApp import Read_CtaApp
    from DIRAC.Core.Utilities.Subprocess import systemCall

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    global jobReport
    jobReport = JobReport(jobID)

    ###########
    ## Checking MD coherence
    fc = FileCatalog('LcgFileCatalog')
    res = fc._getCatalogConfigDetails('DIRACFileCatalog')
    print 'DFC CatalogConfigDetails:', res
    res = fc._getCatalogConfigDetails('LcgFileCatalog')
    print 'LCG CatalogConfigDetails:', res

    fcc = FileCatalogClient()
    fcL = FileCatalog('LcgFileCatalog')

    from DIRAC.Interfaces.API.Dirac import Dirac
    dirac = Dirac()

    #############
    simtelConfigFilesPath = 'sim_telarray/multi'
    simtelConfigFile = simtelConfigFilesPath + '/multi_cta-ultra5.cfg'
    #simtelConfigFile = simtelConfigFilesPath + '/multi_cta-prod1s.cfg'
    createGlobalsFromConfigFiles('prodConfigFile', corsikaTemplate, version)

    ######################Building prod Directory Metadata #######################
    resultCreateProdDirMD = createProdFileSystAndMD()
    if not resultCreateProdDirMD['OK']:
        DIRAC.gLogger.error('Failed to create prod Directory MD')
        jobReport.setApplicationStatus('Failed to create prod Directory MD')
        DIRAC.gLogger.error('Metadata coherence problem, no file produced')
        DIRAC.exit(-1)
    else:
        print 'prod Directory MD successfully created'

    ######################Building corsika Directory Metadata #######################

    resultCreateCorsikaDirMD = createCorsikaFileSystAndMD()
    if not resultCreateCorsikaDirMD['OK']:
        DIRAC.gLogger.error('Failed to create corsika Directory MD')
        jobReport.setApplicationStatus('Failed to create corsika Directory MD')
        DIRAC.gLogger.error(
            'Metadata coherence problem, no corsikaFile produced')
        DIRAC.exit(-1)
    else:
        print 'corsika Directory MD successfully created'

    ############ Producing Corsika File
    global CorsikaSimtelPack
    CorsikaSimtelPack = os.path.join('corsika_simhessarray', version,
                                     'corsika_simhessarray')
    install_CorsikaSimtelPack(version, 'sim')
    cs = CorsikaApp()
    cs.setSoftwarePackage(CorsikaSimtelPack)
    cs.csExe = executable
    cs.csArguments = [
        '--run-number', run_number, '--run', 'corsika', corsikaTemplate
    ]
    corsikaReturnCode = cs.execute()

    if corsikaReturnCode != 0:
        DIRAC.gLogger.error('Corsika Application: Failed')
        jobReport.setApplicationStatus('Corsika Application: Failed')
        DIRAC.exit(-1)
###################### rename of corsika output file #######################
    rundir = 'run' + run_number
    filein = rundir + '/' + corsikaOutputFileName
    corsikaFileName = particle + '_' + thetaP + '_' + phiP + '_alt' + obslev + '_' + 'run' + run_number + '.corsika.gz'
    mv_cmd = 'mv ' + filein + ' ' + corsikaFileName
    if (os.system(mv_cmd)):
        DIRAC.exit(-1)
########################

########################
## files spread in 1000-runs subDirectories
    runNum = int(run_number)
    subRunNumber = '%03d' % runNum
    runNumModMille = runNum % 1000
    runNumTrunc = (runNum - runNumModMille) / 1000
    runNumSeriesDir = '%03dxxx' % runNumTrunc
    print 'runNumSeriesDir=', runNumSeriesDir

    ### create corsika tar luisa ####################
    corsikaTarName = particle + '_' + thetaP + '_' + phiP + '_alt' + obslev + '_' + 'run' + run_number + '.corsika.tar.gz'
    filetar1 = rundir + '/' + 'input'
    filetar2 = rundir + '/' + 'DAT' + run_number + '.dbase'
    filetar3 = rundir + '/run' + str(int(run_number)) + '.log'
    cmdTuple = [
        '/bin/tar', 'zcf', corsikaTarName, filetar1, filetar2, filetar3
    ]
    DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
    ret = systemCall(0, cmdTuple, sendOutput)
    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute tar')
        DIRAC.exit(-1)

######################################################
    corsikaOutFileDir = os.path.join(corsikaDirPath, particle, 'Data',
                                     runNumSeriesDir)
    corsikaOutFileLFN = os.path.join(corsikaOutFileDir, corsikaFileName)
    corsikaRunNumberSeriesDirExist = fcc.isDirectory(
        corsikaOutFileDir)['Value']['Successful'][corsikaOutFileDir]
    newCorsikaRunNumberSeriesDir = (
        corsikaRunNumberSeriesDirExist != True
    )  # if new runFileSeries, will need to add new MD

    #### create a file to DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK ################
    f = open('DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK', 'w')
    f.close()

    if savecorsika == 'True':
        DIRAC.gLogger.notice('Put and register corsika File in LFC and DFC:',
                             corsikaOutFileLFN)
        ret = dirac.addFile(corsikaOutFileLFN, corsikaFileName,
                            storage_element)

        res = CheckCatalogCoherence(corsikaOutFileLFN)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Job failed: Catalog Coherence problem found')
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Error during addFile call:', ret['Message'])
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

    # put and register corsikaTarFile:
        corsikaTarFileDir = os.path.join(corsikaDirPath, particle, 'Log',
                                         runNumSeriesDir)
        corsikaTarFileLFN = os.path.join(corsikaTarFileDir, corsikaTarName)

        ##### If storage element is IN2P3-tape save simtel file on disk ###############
        if storage_element == 'CC-IN2P3-Tape':
            storage_element = 'CC-IN2P3-Disk'

        DIRAC.gLogger.notice(
            'Put and register corsikaTar File in LFC and DFC:',
            corsikaTarFileLFN)
        ret = dirac.addFile(corsikaTarFileLFN, corsikaTarName, storage_element)

        ####Checking and restablishing catalog coherence #####################
        res = CheckCatalogCoherence(corsikaTarFileLFN)
        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Job failed: Catalog Coherence problem found')
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Error during addFile call:', ret['Message'])
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)
######################################################################

        if newCorsikaRunNumberSeriesDir:
            insertRunFileSeriesMD(corsikaOutFileDir, runNumTrunc)
            insertRunFileSeriesMD(corsikaTarFileDir, runNumTrunc)

###### insert corsika File Level metadata ############################################
        corsikaFileMD = {}
        corsikaFileMD['runNumber'] = int(run_number)
        corsikaFileMD['jobID'] = jobID
        corsikaFileMD['corsikaReturnCode'] = corsikaReturnCode
        corsikaFileMD['nbShowers'] = nbShowers

        result = fcc.setMetadata(corsikaOutFileLFN, corsikaFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(corsikaTarFileLFN, corsikaFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

#####  Exit now if only corsika simulation required
    if (mode == 'corsika_standalone'):
        DIRAC.exit()

############ Producing SimTel File
######################Building simtel Directory Metadata #######################

    cfg_dict = {
        "4MSST": 'cta-prod2-4m-dc',
        "SCSST": 'cta-prod2-sc-sst',
        "STD": 'cta-prod2',
        "NSBX3": 'cta-prod2',
        "ASTRI": 'cta-prod2-astri',
        "SCMST": 'cta-prod2-sc3',
        "NORTH": 'cta-prod2n'
    }

    if simtelConfig == "6INROW":
        all_configs = ["4MSST", "SCSST", "ASTRI", "NSBX3", "STD", "SCMST"]
    elif simtelConfig == "5INROW":
        all_configs = ["4MSST", "SCSST", "ASTRI", "NSBX3", "STD"]
    elif simtelConfig == "3INROW":
        all_configs = ["SCSST", "STD", "SCMST"]
    else:
        all_configs = [simtelConfig]

############################################
#for current_conf in all_configs:
#DIRAC.gLogger.notice('current conf is',current_conf)
#if current_conf == "SCMST":
#current_version = version + '_sc3'
#DIRAC.gLogger.notice('current version is', current_version)
#if os.path.isdir('sim_telarray'):
#DIRAC.gLogger.notice('Package found in the local area. Removing package...')
#cmd = 'rm -R sim_telarray corsika-6990 hessioxxx corsika-run'
#if(os.system(cmd)):
#DIRAC.exit( -1 )
#install_CorsikaSimtelPack(current_version)
#else:
#current_version = version
#DIRAC.gLogger.notice('current version is', current_version)
#############################################################

    for current_conf in all_configs:
        DIRAC.gLogger.notice('current conf is', current_conf)
        if current_conf == "SCMST":
            current_version = version + '_sc3'
            DIRAC.gLogger.notice('current version is', current_version)
            installSoftwareEnviron(CorsikaSimtelPack, workingArea(), 'sim-sc3')
        else:
            current_version = version
            DIRAC.gLogger.notice('current version is', current_version)

########################################################

        global simtelDirPath
        global simtelProdVersion

        simtelProdVersion = current_version + '_simtel'
        simtelDirPath = os.path.join(corsikaParticleDirPath, simtelProdVersion)

        resultCreateSimtelDirMD = createSimtelFileSystAndMD(current_conf)
        if not resultCreateSimtelDirMD['OK']:
            DIRAC.gLogger.error('Failed to create simtelArray Directory MD')
            jobReport.setApplicationStatus(
                'Failed to create simtelArray Directory MD')
            DIRAC.gLogger.error(
                'Metadata coherence problem, no simtelArray File produced')
            DIRAC.exit(-1)
        else:
            DIRAC.gLogger.notice('simtel Directory MD successfully created')

############## check simtel data file LFN exists ########################
        simtelFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.simtel.gz'
        simtelDirPath_conf = simtelDirPath + '_' + current_conf
        simtelOutFileDir = os.path.join(simtelDirPath_conf, 'Data',
                                        runNumSeriesDir)
        simtelOutFileLFN = os.path.join(simtelOutFileDir, simtelFileName)
        res = CheckCatalogCoherence(simtelOutFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Current conf already done', current_conf)
            continue

#### execute simtelarray ################
        fd = open('run_sim.sh', 'w')
        fd.write("""#! /bin/sh  
source ./Corsika_simhessarrayEnv.sh
export SVNPROD2=$PWD
export SVNTAG=SVN-PROD2_rev10503
export CORSIKA_IO_BUFFER=800MB
cp ../grid_prod2-repro.sh .
ln -s ../%s
ln -s ../$SVNTAG
./grid_prod2-repro.sh %s %s""" %
                 (corsikaFileName, corsikaFileName, current_conf))
        fd.close()
        ####################################

        os.system('chmod u+x run_sim.sh')
        cmdTuple = ['./run_sim.sh']
        ret = systemCall(0, cmdTuple, sendOutputSimTel)
        simtelReturnCode, stdout, stderr = ret['Value']

        if (os.system('grep Broken simtel.log') == 0):
            DIRAC.gLogger.error('Broken string found in simtel.log')
            jobReport.setApplicationStatus('Broken pipe')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute run_sim.sh')
            DIRAC.gLogger.error('run_sim.sh status is:', simtelReturnCode)
            DIRAC.exit(-1)

##   check simtel data/log/histo Output File exist
        cfg = cfg_dict[current_conf]
        #cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Data/*.simtel.gz ' + simtelFileName
        if current_conf == "SCMST":
            cmdprefix = 'mv sim-sc3/Data/sim_telarray/' + cfg + '/0.0deg/'
        else:
            cmdprefix = 'mv sim/Data/sim_telarray/' + cfg + '/0.0deg/'

        cmd = cmdprefix + 'Data/*' + cfg + '_*.simtel.gz ' + simtelFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

############################################
        simtelRunNumberSeriesDirExist = fcc.isDirectory(
            simtelOutFileDir)['Value']['Successful'][simtelOutFileDir]
        newSimtelRunFileSeriesDir = (
            simtelRunNumberSeriesDirExist != True
        )  # if new runFileSeries, will need to add new MD

        simtelLogFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.log.gz'
        #cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Log/*.log.gz ' + simtelLogFileName
        cmd = cmdprefix + 'Log/*' + cfg + '_*.log.gz ' + simtelLogFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)
        simtelOutLogFileDir = os.path.join(simtelDirPath_conf, 'Log',
                                           runNumSeriesDir)
        simtelOutLogFileLFN = os.path.join(simtelOutLogFileDir,
                                           simtelLogFileName)

        simtelHistFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.hdata.gz'
        #cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Histograms/*.hdata.gz ' + simtelHistFileName
        cmd = cmdprefix + 'Histograms/*' + cfg + '_*.hdata.gz ' + simtelHistFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)
        simtelOutHistFileDir = os.path.join(simtelDirPath_conf, 'Histograms',
                                            runNumSeriesDir)
        simtelOutHistFileLFN = os.path.join(simtelOutHistFileDir,
                                            simtelHistFileName)

        ########### quality check on Histo #############################################
        fd = open('check_histo.sh', 'w')
        fd.write("""#! /bin/sh  
nsim=$(list_histograms %s|fgrep 'Histogram 6 '|sed 's/^.*contents: //'| sed 's:/.*$::')
nevents=%d
if [ $nsim -lt $(( $nevents - 20 )) ]; then
echo 'nsim found:' $nsim
echo 'nsim expected:' $nevents
exit 1
else
echo 'nsim found:' $nsim
echo 'nsim expected:' $nevents
fi
""" % (simtelHistFileName, int(nbShowers) * int(cscat)))
        fd.close()

        ret = getSoftwareEnviron(CorsikaSimtelPack)

        if not ret['OK']:
            error = ret['Message']
            DIRAC.gLogger.error(error, CorsikaSimtelPack)
            DIRAC.exit(-1)

        corsikaEnviron = ret['Value']

        os.system('chmod u+x check_histo.sh')
        cmdTuple = ['./check_histo.sh']
        DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
        ret = systemCall(0, cmdTuple, sendOutput, env=corsikaEnviron)
        checkHistoReturnCode, stdout, stderr = ret['Value']

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute check_histo.sh')
            DIRAC.gLogger.error('check_histo.sh status is:',
                                checkHistoReturnCode)
            DIRAC.exit(-1)

        if (checkHistoReturnCode != 0):
            DIRAC.gLogger.error('Failure during check_histo.sh')
            DIRAC.gLogger.error('check_histo.sh status is:',
                                checkHistoReturnCode)
            jobReport.setApplicationStatus('Histo check Failed')
            DIRAC.exit(-1)

########## quality check on Log #############################
        cmd = 'zcat %s | grep Finished.' % simtelLogFileName
        DIRAC.gLogger.notice('Executing system call:', cmd)
        if (os.system(cmd)):
            jobReport.setApplicationStatus('Log check Failed')
            DIRAC.exit(-1)

################################################
        from DIRAC.Core.Utilities import List
        from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations
        opsHelper = Operations()

        global seList
        seList = opsHelper.getValue('ProductionOutputs/SimtelProd', [])
        seList = List.randomize(seList)

        DIRAC.gLogger.notice('SeList is:', seList)

        #########  Upload simtel data/log/histo ##############################################

        res = upload_to_seList(simtelOutFileLFN, simtelFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('OutputData Upload Error', simtelOutFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        res = CheckCatalogCoherence(simtelOutLogFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Log file already exists. Removing:',
                                 simtelOutLogFileLFN)
            ret = dirac.removeFile(simtelOutLogFileLFN)

        res = upload_to_seList(simtelOutLogFileLFN, simtelLogFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload simtel Log Error', simtelOutLogFileLFN)
            DIRAC.gLogger.notice('Removing simtel data file:',
                                 simtelOutFileLFN)
            ret = dirac.removeFile(simtelOutFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

        res = CheckCatalogCoherence(simtelOutHistFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('Histo file already exists. Removing:',
                                 simtelOutHistFileLFN)
            ret = dirac.removeFile(simtelOutHistFileLFN)

        res = upload_to_seList(simtelOutHistFileLFN, simtelHistFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload simtel Histo Error',
                                simtelOutHistFileLFN)
            DIRAC.gLogger.notice('Removing simtel data file:',
                                 simtelOutFileLFN)
            ret = dirac.removeFile(simtelOutFileLFN)
            DIRAC.gLogger.notice('Removing simtel log file:',
                                 simtelOutLogFileLFN)
            ret = dirac.removeFile(simtelOutLogFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

#    simtelRunNumberSeriesDirExist = fcc.isDirectory(simtelOutFileDir)['Value']['Successful'][simtelOutFileDir]
#    newSimtelRunFileSeriesDir = (simtelRunNumberSeriesDirExist != True)  # if new runFileSeries, will need to add new MD

        if newSimtelRunFileSeriesDir:
            print 'insertRunFileSeriesMD'
            insertRunFileSeriesMD(simtelOutFileDir, runNumTrunc)
            insertRunFileSeriesMD(simtelOutLogFileDir, runNumTrunc)
            insertRunFileSeriesMD(simtelOutHistFileDir, runNumTrunc)
        else:
            print 'NotinsertRunFileSeriesMD'

###### simtel File level metadata ############################################
        simtelFileMD = {}
        simtelFileMD['runNumber'] = int(run_number)
        simtelFileMD['jobID'] = jobID
        simtelFileMD['simtelReturnCode'] = simtelReturnCode

        result = fcc.setMetadata(simtelOutFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(simtelOutLogFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(simtelOutHistFileLFN, simtelFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        if savecorsika == 'True':
            result = fcc.addFileAncestors(
                {simtelOutFileLFN: {
                    'Ancestors': [corsikaOutFileLFN]
                }})
            print 'result addFileAncestor:', result

            result = fcc.addFileAncestors(
                {simtelOutLogFileLFN: {
                    'Ancestors': [corsikaOutFileLFN]
                }})
            print 'result addFileAncestor:', result

            result = fcc.addFileAncestors(
                {simtelOutHistFileLFN: {
                    'Ancestors': [corsikaOutFileLFN]
                }})
            print 'result addFileAncestor:', result

#####  Exit now if only corsika simulation required
        if (mode == 'corsika_simtel'):
            continue

######### run read_cta #######################################

        rcta = Read_CtaApp()
        rcta.setSoftwarePackage(CorsikaSimtelPack)
        rcta.rctaExe = 'read_cta'

        powerlaw_dict = {
            'gamma': '-2.57',
            'gamma_ptsrc': '-2.57',
            'proton': '-2.70',
            'electron': '-3.21'
        }
        dstFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.simtel-dst0.gz'
        dstHistoFileName = particle + '_' + str(thetaP) + '_' + str(
            phiP) + '_alt' + str(
                obslev) + '_' + 'run' + run_number + '.hdata-dst0.gz'

        ## added some options starting from Armazones_2K prod.
        rcta.rctaArguments = [
            '-r', '4', '-u', '--integration-scheme', '4',
            '--integration-window', '7,3', '--tail-cuts', '6,8', '--min-pix',
            '2', '--min-amp', '20', '--type', '1,0,0,400', '--tail-cuts',
            '9,12', '--min-amp', '20', '--type', '2,0,0,100', '--tail-cuts',
            '8,11', '--min-amp', '19', '--type', '3,0,0,40', '--tail-cuts',
            '6,9', '--min-amp', '15', '--type', '4,0,0,15', '--tail-cuts',
            '3.7,5.5', '--min-amp', '8', '--type', '5,0,0,70,5.6',
            '--tail-cuts', '2.4,3.2', '--min-amp', '5.6', '--dst-level', '0',
            '--dst-file', dstFileName, '--histogram-file', dstHistoFileName,
            '--powerlaw', powerlaw_dict[particle], simtelFileName
        ]

        rctaReturnCode = rcta.execute()

        if rctaReturnCode != 0:
            DIRAC.gLogger.error('read_cta Application: Failed')
            jobReport.setApplicationStatus('read_cta Application: Failed')
            DIRAC.exit(-1)

######## run dst quality checks ######################################

        fd = open('check_dst_histo.sh', 'w')
        fd.write("""#! /bin/sh  
dsthistfilename=%s
dstfile=%s
n6="$(list_histograms -h 6 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')" 
n12001="$(list_histograms -h 12001 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')" 
if [ $n6 -ne $n12001 ]; then
echo 'n6 found:' $n6
echo 'n12001 found:' $n12001
exit 1
else
echo 'n6 found:' $n6
echo 'n12001 found:' $n12001
fi

n12002="$(list_histograms -h 12002 ${dsthistfilename} | grep 'Histogram of type' | sed 's/.*bins, //' | sed 's/ entries.//')" 
nev="$(statio ${dstfile} | egrep '^2010' | cut -f2)"
if [ -z "$nev" ]; then nev="0"; fi

if [ $nev -ne $n12002 ]; then
echo 'nev found:' $nev
echo 'n12002 found:' $n12002
exit 1
else
echo 'nev found:' $nev
echo 'n12002 found:' $n12002
fi
""" % (dstHistoFileName, dstFileName))
        fd.close()

        os.system('chmod u+x check_dst_histo.sh')
        cmdTuple = ['./check_dst_histo.sh']
        DIRAC.gLogger.notice('Executing command tuple:', cmdTuple)
        ret = systemCall(0, cmdTuple, sendOutput, env=corsikaEnviron)
        checkHistoReturnCode, stdout, stderr = ret['Value']

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute check_dst_histo.sh')
            DIRAC.gLogger.error('check_dst_histo.sh status is:',
                                checkHistoReturnCode)
            DIRAC.exit(-1)

        if (checkHistoReturnCode != 0):
            DIRAC.gLogger.error('Failure during check_dst_histo.sh')
            DIRAC.gLogger.error('check_dst_histo.sh status is:',
                                checkHistoReturnCode)
            jobReport.setApplicationStatus('Histo check Failed')
            DIRAC.exit(-1)

############create MD and upload dst data/histo ##########################################################

        global dstDirPath
        global dstProdVersion

        dstProdVersion = current_version + '_dst'
        dstDirPath = os.path.join(simtelDirPath_conf, dstProdVersion)

        dstOutFileDir = os.path.join(dstDirPath, 'Data', runNumSeriesDir)
        dstOutFileLFN = os.path.join(dstOutFileDir, dstFileName)

        resultCreateDstDirMD = createDstFileSystAndMD()
        if not resultCreateDstDirMD['OK']:
            DIRAC.gLogger.error('Failed to create Dst Directory MD')
            jobReport.setApplicationStatus('Failed to create Dst Directory MD')
            DIRAC.gLogger.error(
                'Metadata coherence problem, no Dst File produced')
            DIRAC.exit(-1)
        else:
            DIRAC.gLogger.notice('Dst Directory MD successfully created')
############################################################

        res = CheckCatalogCoherence(dstOutFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('dst file already exists. Removing:',
                                 dstOutFileLFN)
            ret = dirac.removeFile(dstOutFileLFN)

        res = upload_to_seList(dstOutFileLFN, dstFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload dst Error', dstOutFileLFN)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

##############################################################
        dstHistoFileDir = os.path.join(dstDirPath, 'Histograms',
                                       runNumSeriesDir)
        dstHistoFileLFN = os.path.join(dstHistoFileDir, dstHistoFileName)

        res = CheckCatalogCoherence(dstHistoFileLFN)
        if res == DIRAC.S_OK:
            DIRAC.gLogger.notice('dst histo file already exists. Removing:',
                                 dstHistoFileLFN)
            ret = dirac.removeFile(dstHistoFileLFN)

        res = upload_to_seList(dstHistoFileLFN, dstHistoFileName)

        if res != DIRAC.S_OK:
            DIRAC.gLogger.error('Upload dst Error', dstHistoFileName)
            jobReport.setApplicationStatus('OutputData Upload Error')
            DIRAC.exit(-1)

########### Insert RunNumSeries MD ##########################

        dstRunNumberSeriesDirExist = fcc.isDirectory(
            dstOutFileDir)['Value']['Successful'][dstOutFileDir]
        newDstRunFileSeriesDir = (
            dstRunNumberSeriesDirExist != True
        )  # if new runFileSeries, will need to add new MD

        if newDstRunFileSeriesDir:
            insertRunFileSeriesMD(dstOutFileDir, runNumTrunc)
            insertRunFileSeriesMD(dstHistoFileDir, runNumTrunc)

####### dst File level metadata ###############################################
        dstFileMD = {}
        dstFileMD['runNumber'] = int(run_number)
        dstFileMD['jobID'] = jobID
        dstFileMD['rctaReturnCode'] = rctaReturnCode

        result = fcc.setMetadata(dstOutFileLFN, dstFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        result = fcc.setMetadata(dstHistoFileLFN, dstFileMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

########## set the ancestors for dst #####################################

        result = fcc.addFileAncestors(
            {dstOutFileLFN: {
                'Ancestors': [simtelOutFileLFN]
            }})
        print 'result addFileAncestor:', result

        result = fcc.addFileAncestors(
            {dstHistoFileLFN: {
                'Ancestors': [simtelOutFileLFN]
            }})
        print 'result addFileAncestor:', result


######################################################

    DIRAC.exit()
Example #16
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("p:", "inputfile=", "Input File")
    Script.registerSwitch("S:", "simtelConfig=", "SimtelConfig")
    Script.registerSwitch("V:", "version=", "Version (Use setVersion)")
    Script.registerSwitch("C:", "comp=", "Compile (True/False)")

    Script.parseCommandLine(ignoreErrors=True)

    ## default values ##############
    simtelConfig = None
    version = None
    comp = True

    ### set switch values ###
    for switch in Script.getUnprocessedSwitches():
        if switch[0] == "inputfile" or switch[0] == "p":
            corsikaFileLFN = switch[1].split('ParametricInputData=LFN:')[1]
        elif switch[0] == "simtelConfig" or switch[0] == "S":
            simtelConfig = switch[1]
        elif switch[0] == "version" or switch[0] == "V":
            version = switch[1]
        elif switch[0] == "comp" or switch[0] == "C":
            comp = switch[1]

    if version == None:
        Script.showHelp()
        jobReport.setApplicationStatus('Missing options')
        DIRAC.exit(-1)

    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    ############ Producing SimTel File
    if comp == True:
        install_CorsikaSimtelPack(version)
    else:
        CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'
        res = installSoftwarePackage(CorsikaSimtelPack, workingArea())
        if not res['OK']:
            DIRAC.gLogger.error('Failed to execute installSoftwarePackage',
                                res)

    CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'

    cfg_dict = {
        "4MSST": 'cta-prod2-4m-dc',
        "SCSST": 'cta-prod2-sc-sst',
        "STD": 'cta-prod2',
        "NSBX3": 'cta-prod2',
        "ASTRI": 'cta-prod2-astri',
        "NORTH": 'cta-prod2n',
        "ASTRISLN": 'cta-astri-sln'
    }

    #if simtelConfig=="6INROW":
    #  all_configs=["4MSST","SCSST","ASTRI","NSBX3","STD","SCMST"]
    #elif simtelConfig=="5INROW":
    #  all_configs=["4MSST","SCSST","ASTRI","NSBX3","STD"]
    #else:
    #  all_configs=[simtelConfig]

    all_configs = [simtelConfig]

    for current_conf in all_configs:

        DIRAC.gLogger.notice('current conf is', current_conf)
        current_version = version
        DIRAC.gLogger.notice('current version is', current_version)

        #if current_conf == "SCMST":
        #  current_version = version + '_sc3'
        #  DIRAC.gLogger.notice('current version is', current_version)
        #  if os.path.isdir('sim_telarray'):
        #    DIRAC.gLogger.notice('Package found in the local area. Removing package...')
        #    cmd = 'rm -R sim_telarray corsika-6990 hessioxxx corsika-run'
        #    if(os.system(cmd)):
        #      DIRAC.exit( -1 )
        #    install_CorsikaSimtelPack(current_version)
        #else:
        # current_version = version
        # DIRAC.gLogger.notice('current version is', current_version)

        #### execute simtelarray ################
        fd = open('run_sim.sh', 'w')
        fd.write("""#! /bin/sh  
    export SVNPROD2=$PWD
    export SVNTAG=SVN-PROD2
    export CORSIKA_IO_BUFFER=800MB
    ./grid_prod2-repro.sh %s %s""" %
                 (os.path.basename(corsikaFileLFN), current_conf))
        fd.close()
        ####################################

        os.system('chmod u+x run_sim.sh')
        os.system('chmod u+x grid_prod2-repro.sh')
        cmdTuple = ['./run_sim.sh']
        ret = systemCall(0, cmdTuple, sendSimtelOutput)
        simtelReturnCode, stdout, stderr = ret['Value']

        if (os.system('grep Broken simtel.log') == 0):
            DIRAC.gLogger.error('Broken string found in simtel.log')
            jobReport.setApplicationStatus('Broken pipe')
            DIRAC.exit(-1)

        if not ret['OK']:
            DIRAC.gLogger.error('Failed to execute run_sim.sh')
            DIRAC.gLogger.error('run_sim.sh status is:', simtelReturnCode)
            DIRAC.exit(-1)

##   check simtel data/log/histo Output File exist
        cfg = cfg_dict[current_conf]
        simtelFileName = os.path.basename(corsikaFileLFN).replace(
            'corsika.gz', 'simtel.gz')
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Data/*.simtel.gz ' + simtelFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

        simtelLogFileName = simtelFileName.replace('simtel.gz', 'log.gz')
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Log/*.log.gz ' + simtelLogFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)

        simtelHistFileName = simtelFileName.replace('simtel.gz', 'hdata.gz')
        cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Histograms/*.hdata.gz ' + simtelHistFileName
        if (os.system(cmd)):
            DIRAC.exit(-1)


################################################################

    DIRAC.exit()
Example #17
0
'''
Created on 2015-05-19 21:45:37

@author: suo
'''
import sys
from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport
from DIRAC.Core.Base import Script
Script.parseCommandLine( ignoreErrors = False )

jobID = sys.argv[1]
experiment = sys.argv[2]
message = sys.argv[3]

jobReport = JobReport(jobID,experiment)
result = jobReport.setApplicationStatus(message)
if not result['OK']:
    try:
        with open('job.err','a') as errFile:
            print >> errFile, 'setJobStatus error: %s' % result
    except IOError:
        print 'IOError:',str(e)
Example #18
0
def main():

    from DIRAC.Core.Base import Script

    ### make_CTA_DST options ###############################################
    Script.registerSwitch("T:", "tellist=", "Tellist", setTellist)
    Script.registerSwitch("N:", "nevent=", "Nevent", setNevent)
    Script.registerSwitch("p:", "run_number=",
                          "Run Number (set automatically)", setRunNumber)
    ### other options ###############################################
    Script.registerSwitch("V:", "version=", "HAP version", setVersion)

    Script.parseCommandLine(ignoreErrors=True)

    args = Script.getPositionalArgs()

    if len(args) < 1:
        Script.showHelp()

    if tellist == None or version == None:
        Script.showHelp()
        jobReport.setApplicationStatus('Options badly specified')
        DIRAC.exit(-1)

    from CTADIRAC.Core.Workflow.Modules.HapRootMacro import HapRootMacro
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    HapPack = 'HAP/' + version + '/HAP'

    packs = ['HESS/v0.2/lib', 'HESS/v0.3/root', HapPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                continue
        if localArea:
            if checkSoftwarePackage(package, localArea())['OK']:
                DIRAC.gLogger.notice('Package found in Local Area:', package)
                continue
            if installSoftwarePackage(package, localArea())['OK']:
                continue
        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

    hr = HapRootMacro()
    hr.setSoftwarePackage(HapPack)

    telconf = os.path.join(localArea(),
                           'HAP/%s/config/%s' % (version, tellist))
    infile = build_infile()
    infilestr = '"' + infile + '"'
    telconfstr = '"' + telconf + '"'
    args = [str(int(run_number)), infilestr, telconfstr]

    try:
        args.extend([nevent])
    except NameError:
        DIRAC.gLogger.info('nevent arg not used')

    DIRAC.gLogger.notice('make_CTA_DST macro Arguments:', args)
    hr.rootMacro = '/hapscripts/dst/make_CTA_DST.C+'
    hr.rootArguments = args
    DIRAC.gLogger.notice('Executing Hap make_CTA_DST macro')
    res = hr.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Failed to execute make_CTA_DST macro')
        jobReport.setApplicationStatus('Failure during make_CTA_DST')
        DIRAC.exit(-1)

############ check existance of output file ####
    filedst = 'dst_CTA_%08d' % int(run_number) + '.root'

    if not os.path.isfile(filedst):
        DIRAC.gLogger.error('dst file not found:', filedst)
        jobReport.setApplicationStatus('make_CTA_DST.C: DST file not created')
        DIRAC.exit(-1)

    fileout = 'dst_' + part_type + '_run' + run_number + '.root'
    cmd = 'mv ' + filedst + ' ' + fileout
    os.system(cmd)

    ###################Check std out #############################
    DIRAC.gLogger.notice('Executing DST Check step0')

    ret = getSoftwareEnviron(HapPack)
    if not ret['OK']:
        error = ret['Message']
        DIRAC.gLogger.error(error, HapPack)
        DIRAC.exit(-1)

    hapEnviron = ret['Value']
    hessroot = hapEnviron['HESSROOT']
    check_script = hessroot + '/hapscripts/dst/check_dst0.csh'
    cmdTuple = [check_script]
    ret = systemCall(0, cmdTuple, sendOutput)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute DST Check step0')
        jobReport.setApplicationStatus('Check_dst0: Failed')
        DIRAC.exit(-1)

    status, stdout, stderr = ret['Value']
    if status == 1:
        jobReport.setApplicationStatus(
            'Check_dst0: Big problem during the DST production')
        DIRAC.gLogger.error(
            'DST Check step0 reports: Big problem during the DST production')
        DIRAC.exit(-1)
    if status == 2:
        jobReport.setApplicationStatus('Check_dst0: No triggered events')
        DIRAC.gLogger.notice('DST Check step0 reports: No triggered events')
        DIRAC.exit()

############# run the CheckDST macro #################
    DIRAC.gLogger.notice('Executing DST check step1')
    hr.rootMacro = '/hapscripts/dst/CheckDST.C+'
    fileoutstr = '"' + fileout + '"'
    args = [fileoutstr]
    DIRAC.gLogger.notice('CheckDST macro Arguments:', args)
    hr.rootArguments = args
    DIRAC.gLogger.notice('Executing Hap CheckDST macro')
    res = hr.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Failure during DST Check step1')
        jobReport.setApplicationStatus('Check_dst1: Failed')
        DIRAC.exit(-1)


#######################Check std out of CheckDST macro ##########################
    DIRAC.gLogger.notice('Executing DST Check step2')
    check_script = hessroot + '/hapscripts/dst/check_dst2.csh'
    cmdTuple = [check_script]
    ret = systemCall(0, cmdTuple, sendOutput)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute DST Check step2')
        jobReport.setApplicationStatus('Check_dst2: Failed')
        DIRAC.exit(-1)

    status, stdout, stderr = ret['Value']
    if status == 1:
        jobReport.setApplicationStatus(
            'DST Check step2: Big problem during the DST production')
        DIRAC.gLogger.error(
            'DST Check step2 reports: Big problem during the DST production')
        DIRAC.exit(-1)
    if status == 2:
        jobReport.setApplicationStatus('DST Check step2: No triggered events')
        DIRAC.gLogger.notice('DST Check step2 reports: No triggered events')
        DIRAC.exit()

    DIRAC.exit()
Example #19
0
def main():

    from DIRAC.Core.Base import Script
    ### DoCtaIrf options ##########################################################
    Script.registerSwitch("A:", "analysis=", "Analysis Type", setAnalysisType)
    Script.registerSwitch("C:", "cuts=", "Cuts Config", setCutsConfig)
    Script.registerSwitch("R:", "runlist=", "Runlist", setRunlist)
    Script.registerSwitch("Z:", "zenith=", "Zenith", setZenith)
    Script.registerSwitch("O:", "offset=", "Offset", setOffset)
    Script.registerSwitch("M:", "energy=", "Energy Method", setEnergyMethod)
    Script.registerSwitch("T:", "arrayconfig=", "Array Configuration",
                          setArrayConfig)
    Script.registerSwitch("P:", "particle=", "Particle Type", setParticleType)
    ## other options
    Script.registerSwitch("V:", "version=", "HAP version", setVersion)

    Script.parseCommandLine(ignoreErrors=True)

    args = Script.getPositionalArgs()
    if len(args) < 1:
        Script.showHelp()

    from CTADIRAC.Core.Workflow.Modules.HapApplication import HapApplication
    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import getSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    ha = HapApplication()

    HapPack = 'HAP/' + version + '/HAP'

    packs = ['HESS/v0.2/lib', 'HESS/v0.3/root', HapPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                continue
        if localArea:
            if checkSoftwarePackage(package, localArea())['OK']:
                DIRAC.gLogger.notice('Package found in Local Area:', package)
                continue
            if installSoftwarePackage(package, localArea())['OK']:
                continue
        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

    ha.setSoftwarePackage(HapPack)

    ha.hapExecutable = 'DoCtaIrf'

    runlistdir = os.environ['PWD']

    build_infile(runlist)

    ha.hapArguments = [
        analysistype, cutsconfig, runlistdir, runlist, zenith, offset,
        arrayconfig, energymethod, particle
    ]

    DIRAC.gLogger.notice('Executing Hap Application')
    res = ha.execute()

    if not res['OK']:
        DIRAC.gLogger.error('Failed to execute Hap Application')
        jobReport.setApplicationStatus('Hap Application: Failed')
        DIRAC.exit(-1)

    DIRAC.exit()
Example #20
0
def main():

    from DIRAC.Core.Base import Script

    Script.registerSwitch("p:", "inputfile=", "Input File", setInputFile)
    Script.registerSwitch("E:", "simtelExecName=", "SimtelExecName",
                          setExecutable)
    Script.registerSwitch("S:", "simtelConfig=", "SimtelConfig", setConfig)
    Script.registerSwitch("V:", "version=", "Version", setVersion)
    Script.registerSwitch("D:", "storage_element=", "Storage Element",
                          setStorageElement)

    from DIRAC.Resources.Catalog.FileCatalogClient import FileCatalogClient
    from DIRAC.Resources.Catalog.FileCatalog import FileCatalog

    Script.parseCommandLine()
    DIRAC.gLogger.setLevel('INFO')

    global fcc, fcL, storage_element

    from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
    from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
    from CTADIRAC.Core.Utilities.SoftwareInstallation import localArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
    from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
    from DIRAC.Core.Utilities.Subprocess import systemCall
    from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

    jobID = os.environ['JOBID']
    jobID = int(jobID)
    jobReport = JobReport(jobID)

    CorsikaSimtelPack = 'corsika_simhessarray/' + version + '/corsika_simhessarray'

    packs = [CorsikaSimtelPack]

    for package in packs:
        DIRAC.gLogger.notice('Checking:', package)
        if sharedArea:
            if checkSoftwarePackage(package, sharedArea())['OK']:
                DIRAC.gLogger.notice('Package found in Shared Area:', package)
                installSoftwareEnviron(package, workingArea())
                packageTuple = package.split('/')
                corsika_subdir = sharedArea(
                ) + '/' + packageTuple[0] + '/' + version
                cmd = 'cp -u -r ' + corsika_subdir + '/* .'
                os.system(cmd)
                continue

        DIRAC.gLogger.error('Check Failed for software package:', package)
        DIRAC.gLogger.error('Software package not available')
        DIRAC.exit(-1)

###########
## Checking MD coherence
    fc = FileCatalog('LcgFileCatalog')
    res = fc._getCatalogConfigDetails('DIRACFileCatalog')
    print 'DFC CatalogConfigDetails:', res
    res = fc._getCatalogConfigDetails('LcgFileCatalog')
    print 'LCG CatalogConfigDetails:', res

    fcc = FileCatalogClient()
    fcL = FileCatalog('LcgFileCatalog')

    from DIRAC.Interfaces.API.Dirac import Dirac
    dirac = Dirac()
    ############################

    #############
    # CLAUDIA: simtelConfigFile should be built from ???
    #simtelConfigFilesPath = 'sim_telarray/multi'
    #simtelConfigFile = simtelConfigFilesPath + '/multi_cta-ultra5.cfg'
    #createGlobalsFromConfigFiles(simtelConfigFile)
    createGlobalsFromConfigFiles()
    #######################
    ## files spread in 1000-runs subDirectories

    corsikaFileName = os.path.basename(corsikaFileLFN)
    run_number = corsikaFileName.split('run')[1].split('.corsika.gz')[
        0]  # run001412.corsika.gz

    runNum = int(run_number)
    subRunNumber = '%03d' % runNum
    runNumModMille = runNum % 1000
    runNumTrunc = (runNum - runNumModMille) / 1000
    runNumSeriesDir = '%03dxxx' % runNumTrunc
    print 'runNumSeriesDir=', runNumSeriesDir

    f = open('DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK', 'w')
    f.close()

    ##### If storage element is IN2P3-tape save simtel file on disk ###############
    if storage_element == 'CC-IN2P3-Tape':
        storage_element = 'CC-IN2P3-Disk'

############ Producing SimTel File
######################Building simtel Directory Metadata #######################

    resultCreateSimtelDirMD = createSimtelFileSystAndMD()
    if not resultCreateSimtelDirMD['OK']:
        DIRAC.gLogger.error('Failed to create simtelArray Directory MD')
        jobReport.setApplicationStatus(
            'Failed to create simtelArray Directory MD')
        DIRAC.gLogger.error(
            'Metadata coherence problem, no simtelArray File produced')
        DIRAC.exit(-1)
    else:
        print 'simtel Directory MD successfully created'

#### execute simtelarray ################
#  fd = open('run_sim.sh', 'w' )
#  fd.write( """#! /bin/sh
#echo "go for sim_telarray"
#. ./examples_common.sh
#export CORSIKA_IO_BUFFER=800MB
#zcat %s | $SIM_TELARRAY_PATH/run_sim_%s""" % (corsikaFileName, simtelExecName))
#  fd.close()

#### execute simtelarray ################
    fd = open('run_sim.sh', 'w')
    fd.write("""#! /bin/sh  
export SVNPROD2=$PWD
export SVNTAG=SVN-PROD2
export CORSIKA_IO_BUFFER=800MB
./grid_prod2-repro.sh %s %s""" % (corsikaFileName, simtelConfig))
    fd.close()

    os.system('chmod u+x run_sim.sh')

    cmdTuple = ['./run_sim.sh']
    ret = systemCall(0, cmdTuple, sendOutputSimTel)
    simtelReturnCode, stdout, stderr = ret['Value']

    if (os.system('grep Broken simtel.log')):
        print 'not broken'
    else:
        print 'broken'

        # Tag corsika File if Broken Pipe
        corsikaTagMD = {}
        corsikaTagMD['CorsikaToReprocess'] = 'CorsikaToReprocess'
        result = fcc.setMetadata(corsikaFileLFN, corsikaTagMD)
        print "result setMetadata=", result
        if not result['OK']:
            print 'ResultSetMetadata:', result['Message']

        jobReport.setApplicationStatus('Broken pipe')
        DIRAC.exit(-1)

    if not ret['OK']:
        DIRAC.gLogger.error('Failed to execute run_sim.sh')
        DIRAC.gLogger.error('run_sim.sh status is:', simtelReturnCode)
        DIRAC.exit(-1)

## putAndRegister simtel data/log/histo Output File:
    simtelFileName = particle + '_' + str(thetaP) + '_' + str(
        phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.simtel.gz'
    cfg = simtelExecName
    if simtelExecName == "cta-prod2-nsbx3":
        cfg = "cta-prod2"
    cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Data/*.simtel.gz ' + simtelFileName
    if (os.system(cmd)):
        DIRAC.exit(-1)

    simtelOutFileDir = os.path.join(simtelDirPath, 'Data', runNumSeriesDir)
    simtelOutFileLFN = os.path.join(simtelOutFileDir, simtelFileName)
    simtelRunNumberSeriesDirExist = fcc.isDirectory(
        simtelOutFileDir)['Value']['Successful'][simtelOutFileDir]
    newSimtelRunFileSeriesDir = (
        simtelRunNumberSeriesDirExist != True
    )  # if new runFileSeries, will need to add new MD

    simtelLogFileName = particle + '_' + str(thetaP) + '_' + str(
        phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.log.gz'
    cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Log/*.log.gz ' + simtelLogFileName
    if (os.system(cmd)):
        DIRAC.exit(-1)
    simtelOutLogFileDir = os.path.join(simtelDirPath, 'Log', runNumSeriesDir)
    simtelOutLogFileLFN = os.path.join(simtelOutLogFileDir, simtelLogFileName)

    simtelHistFileName = particle + '_' + str(thetaP) + '_' + str(
        phiP) + '_alt' + str(obslev) + '_' + 'run' + run_number + '.hdata.gz'
    cmd = 'mv Data/sim_telarray/' + cfg + '/0.0deg/Histograms/*.hdata.gz ' + simtelHistFileName
    if (os.system(cmd)):
        DIRAC.exit(-1)
    simtelOutHistFileDir = os.path.join(simtelDirPath, 'Histograms',
                                        runNumSeriesDir)
    simtelOutHistFileLFN = os.path.join(simtelOutHistFileDir,
                                        simtelHistFileName)

    ################################################
    DIRAC.gLogger.notice('Put and register simtel File in LFC and DFC:',
                         simtelOutFileLFN)
    ret = dirac.addFile(simtelOutFileLFN, simtelFileName, storage_element)

    res = CheckCatalogCoherence(simtelOutFileLFN)
    if res != DIRAC.S_OK:
        DIRAC.gLogger.error('Job failed: Catalog Coherence problem found')
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)

    if not ret['OK']:
        DIRAC.gLogger.error('Error during addFile call:', ret['Message'])
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)
######################################################################

    DIRAC.gLogger.notice('Put and register simtel Log File in LFC and DFC:',
                         simtelOutLogFileLFN)
    ret = dirac.addFile(simtelOutLogFileLFN, simtelLogFileName,
                        storage_element)

    res = CheckCatalogCoherence(simtelOutLogFileLFN)
    if res != DIRAC.S_OK:
        DIRAC.gLogger.error('Job failed: Catalog Coherence problem found')
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)

    if not ret['OK']:
        DIRAC.gLogger.error('Error during addFile call:', ret['Message'])
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)
######################################################################

    DIRAC.gLogger.notice('Put and register simtel Histo File in LFC and DFC:',
                         simtelOutHistFileLFN)
    ret = dirac.addFile(simtelOutHistFileLFN, simtelHistFileName,
                        storage_element)

    res = CheckCatalogCoherence(simtelOutHistFileLFN)
    if res != DIRAC.S_OK:
        DIRAC.gLogger.error('Job failed: Catalog Coherence problem found')
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)

    if not ret['OK']:
        DIRAC.gLogger.error('Error during addFile call:', ret['Message'])
        jobReport.setApplicationStatus('OutputData Upload Error')
        DIRAC.exit(-1)
######################################################################

    if newSimtelRunFileSeriesDir:
        insertRunFileSeriesMD(simtelOutFileDir, runNumTrunc)
        insertRunFileSeriesMD(simtelOutLogFileDir, runNumTrunc)
        insertRunFileSeriesMD(simtelOutHistFileDir, runNumTrunc)


###### simtel File level metadata ############################################

    simtelFileMD = {}
    simtelFileMD['runNumber'] = int(run_number)
    simtelFileMD['jobID'] = jobID
    simtelFileMD['simtelReturnCode'] = simtelReturnCode

    result = fcc.setMetadata(simtelOutFileLFN, simtelFileMD)
    print "result setMetadata=", result
    if not result['OK']:
        print 'ResultSetMetadata:', result['Message']

    result = fcc.setMetadata(simtelOutLogFileLFN, simtelFileMD)
    print "result setMetadata=", result
    if not result['OK']:
        print 'ResultSetMetadata:', result['Message']

    result = fcc.setMetadata(simtelOutHistFileLFN, simtelFileMD)
    print "result setMetadata=", result
    if not result['OK']:
        print 'ResultSetMetadata:', result['Message']

    result = fcc.addFileAncestors(
        {simtelOutFileLFN: {
            'Ancestors': [corsikaFileLFN]
        }})
    print 'result addFileAncestor:', result

    result = fcc.addFileAncestors(
        {simtelOutLogFileLFN: {
            'Ancestors': [corsikaFileLFN]
        }})
    print 'result addFileAncestor:', result

    result = fcc.addFileAncestors(
        {simtelOutHistFileLFN: {
            'Ancestors': [corsikaFileLFN]
        }})
    print 'result addFileAncestor:', result

    result = fcc.setMetadata(simtelOutFileLFN, simtelFileMD)
    if not result['OK']:
        print 'ResultSetMetadata:', result['Message']

    DIRAC.exit()
Example #21
0
def main():

  from DIRAC.Core.Base import Script
  Script.initialize() 

  DIRAC.gLogger.notice('Platform is:')
  os.system('dirac-platform')
  from DIRAC.DataManagementSystem.Client.DataManager import DataManager
  from CTADIRAC.Core.Workflow.Modules.EvnDispApp import EvnDispApp
  from CTADIRAC.Core.Utilities.SoftwareInstallation import checkSoftwarePackage
  from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwarePackage
  from CTADIRAC.Core.Utilities.SoftwareInstallation import installSoftwareEnviron
  from CTADIRAC.Core.Utilities.SoftwareInstallation import sharedArea
  from CTADIRAC.Core.Utilities.SoftwareInstallation import workingArea
  from DIRAC.Core.Utilities.Subprocess import systemCall
  from DIRAC.WorkloadManagementSystem.Client.JobReport import JobReport

  jobID = os.environ['JOBID']
  jobID = int( jobID )
  jobReport = JobReport( jobID )

  version = sys.argv[3]
  DIRAC.gLogger.notice( 'Version:', version )

  EvnDispPack = os.path.join('evndisplay',version,'evndisplay')

  packs = [EvnDispPack]

  for package in packs:
    DIRAC.gLogger.notice( 'Checking:', package )
    if checkSoftwarePackage( package, sharedArea() )['OK']:
      DIRAC.gLogger.notice( 'Package found in Shared Area:', package )
      installSoftwareEnviron( package, sharedArea() )
#      cmd = 'cp -r ' + os.path.join(sharedArea(),'evndisplay',version,'EVNDISP.CTA.runparameter') + ' .'
#      if(os.system(cmd)):
#        DIRAC.exit( -1 )
#      cmd = 'cp -r ' + os.path.join(sharedArea(),'evndisplay',version,'Calibration') + ' .'
#      if(os.system(cmd)):
#        DIRAC.exit( -1 )
      continue
    else:
      installSoftwarePackage( package, workingArea() )
      DIRAC.gLogger.notice( 'Package found in workingArea:', package )
      continue

    DIRAC.gLogger.error( 'Check Failed for software package:', package )
    DIRAC.gLogger.error( 'Software package not available')
    DIRAC.exit( -1 )  

  ed = EvnDispApp()
  ed.setSoftwarePackage(EvnDispPack)

########## Use of trg mask file #######################
  usetrgfile = sys.argv[7]
  DIRAC.gLogger.notice( 'Usetrgfile:', usetrgfile )

####### Use of multiple inputs per job ###
  simtelFileLFNList = sys.argv[-1].split('ParametricParameters={')[1].split('}')[0].replace(',',' ')
  # first element of the list
  simtelFileLFN = simtelFileLFNList.split(' ')[0]  
  ## convert the string into a list and get the basename
  simtelFileList = []
  for word in simtelFileLFNList.split():
    simtelFileList.append(os.path.basename(word))

####  Parse the Layout List #################
  layoutList = parseLayoutList(sys.argv[9])
#############################################

####  Loop over the Layout List #################
  for layout in layoutList: 
    args = []
########## download trg mask file #######################
    if usetrgfile == 'True':
      trgmaskFileLFN = simtelFileLFN.replace( 'simtel.gz', 'trgmask.gz' )
      DIRAC.gLogger.notice( 'Trying to download the trgmask File', trgmaskFileLFN )
      result = DataManager().getFile( trgmaskFileLFN )
      if not result['OK']:
        DIRAC.gLogger.error( 'Failed to download trgmakfile:', result )
        jobReport.setApplicationStatus( 'Trgmakfile download Error' )
        DIRAC.exit( -1 )
      args.extend( ['-t', os.path.basename( trgmaskFileLFN )] )
############################################################
###### execute evndisplay converter ##################
    executable = sys.argv[5]

############ dst file Name ############################
    run_number = simtelFileList[-1].split( 'run' )[1].split( '.simtel.gz' )[0]
    runNum = int( run_number )
    subRunNumber = '%06d' % runNum
    particle = simtelFileList[-1].split( '_' )[0]
    if 'ptsrc' in simtelFileList[-1]:
      particle = particle + '_' + 'ptsrc'
    dstfile = particle + '_run' + subRunNumber + '_' + str( jobID ) + '_' + os.path.basename( layout ) + '_dst.root'
###########################################

    logfileName = executable + '_' + layout + '.log'
    layout = os.path.join( 'EVNDISP.CTA.runparameter/DetectorGeometry', layout )
    DIRAC.gLogger.notice( 'Layout is:', layout )

  # add other arguments for evndisplay converter specified by user ######
    converterparfile = open( 'converter.par', 'r' ).readlines()
    for line in converterparfile:
      for word in line.split():
        args.append( word )
#########################################################
    args.extend( ['-a', layout] )
    args.extend( ['-o', dstfile] )
    args.extend( simtelFileList )
    execute_module( ed, executable, args )
########### check existence of DST file ###############
    if not os.path.isfile( dstfile ):
      DIRAC.gLogger.error( 'DST file Missing:', dstfile )
      jobReport.setApplicationStatus( 'DST file Missing' )
      DIRAC.exit( -1 )

########### quality check on Log #############################################
    cmd = 'mv ' + executable + '.log' + ' ' + logfileName
    if( os.system( cmd ) ):
      DIRAC.exit( -1 )

    fd = open( 'check_log.sh', 'w' )
    fd.write( """#! /bin/sh
MCevts=$(grep writing  %s | grep "MC events" | awk '{print $2}')
if [ $MCevts -gt 0 ]; then
    exit 0
else
    echo "MCevts is zero"
    exit -1
fi
""" % (logfileName))
    fd.close()

    os.system( 'chmod u+x check_log.sh' )
    cmd = './check_log.sh'
    DIRAC.gLogger.notice( 'Executing system call:', cmd )
    if( os.system( cmd ) ):
      jobReport.setApplicationStatus( 'Converter Log Check Failed' )
      DIRAC.exit( -1 )

   ####  Check the mode #################
    mode = sys.argv[11]
    if( mode == 'convert_standalone' ):
      #DIRAC.exit()
      continue

###### execute evndisplay stage1 ###############
    executable = 'evndisp'
    logfileName = executable + '_' + os.path.basename( layout ) + '.log'

    args = ['-sourcefile', dstfile, '-outputdirectory', 'outdir']
  # add other arguments for evndisp specified by user ######
    evndispparfile = open( 'evndisp.par', 'r' ).readlines()
    for line in evndispparfile:
      for word in line.split():
        args.append( word )

    execute_module( ed, executable, args )

    for name in glob.glob( 'outdir/*.root' ):
      evndispOutFile = name.split( '.root' )[0] + '_' + str( jobID ) + '_' + os.path.basename( layout ) + '_evndisp.root'
      cmd = 'mv ' + name + ' ' + os.path.basename( evndispOutFile )
      if( os.system( cmd ) ):
        DIRAC.exit( -1 )

########### quality check on Log #############################################
    cmd = 'mv ' + executable + '.log' + ' ' + logfileName
    if( os.system( cmd ) ):
      DIRAC.exit( -1 )
    fd = open( 'check_log.sh', 'w' )
    fd.write( """#! /bin/sh
if grep -i "error" %s; then
exit 1
fi
if grep "Final checks on result file (seems to be OK):" %s; then
exit 0
else
exit 1
fi
""" % (logfileName,logfileName))
    fd.close()

    os.system( 'chmod u+x check_log.sh' )
    cmd = './check_log.sh'
    DIRAC.gLogger.notice( 'Executing system call:', cmd )
    if( os.system( cmd ) ):
      jobReport.setApplicationStatus( 'EvnDisp Log Check Failed' )
      DIRAC.exit( -1 )
##################################################################
########### remove the converted dst file #############################################
    cmd = 'rm ' + dstfile
    if( os.system( cmd ) ):
      DIRAC.exit( -1 )
 
  DIRAC.exit()