Esempio n. 1
0
    def FitOnePixel(self,ra,dec,i,j) :
        """Run a evaluation of the pixel (i,j) corresponding to position (ra,dec)"""
        outXml = utils._dump_xml(self.config)
        folder = self.config['out']

        Fit = GetFitObjectForTSmap(self.config,xmlfile=outXml) #get the Fit object

        src = GetSrc(Fit,ra,dec) # get the Source object at postion ra dec

        if self.config['TSMap']['RemoveTarget'] : # remove the target is asked
            Fit.deleteSource(self.config['target']['name'])

        if self.config['TSMap']['Re-Fit']: # reoptimze before is asked
            Fit.fit(0,optimizer=self.config['fitting']['optimizer'])

        for par in xrange(Fit.logLike.getNumParams()): # freeze all the source parameters
            Fit[par].setFree(0)

        Fit.addSource(src)# add a spurious source
        # dump the *new* xml file
        Fit.writeXml(self.tsfolder+"/model_"+str(ra)+"_"+str(dec)+".xml") 

        # a new Fit object with the new xml file is needed.
        # just changing the position of the spurious source does not work 
        TSFit = GetFitObjectForTSmap(self.config,xmlfile=self.tsfolder+"/model_"+str(ra)+"_"+str(dec)+".xml")

        TSFit.fit(0,optimizer=self.config['fitting']['optimizer'])

        # save the result
        fsave = open(self._PixelFile(i,j),'w')
        fsave.write(str(ra)+"\t"+str(dec)+"\t"+
                    str(TSFit.Ts("Spurious"))+"\t"+
                    str(TSFit.logLike.value()))
        fsave.close()
Esempio n. 2
0
def run(infile):
    """Run an entire Fermi analysis (spectrum) by reading a config file"""
    config = get_config(infile)
    folder = config['out']
    os.system('mkdir -p ' + folder)

    FitRunner,Fit = GenAnalysisObjects(config)
    # create all the fit files and run gtlike
    FitRunner.PerformFit(Fit)

    Result = FitRunner.GetAndPrintResults(Fit)#Get and dump the target specific results
    if config['verbose'] == 'yes' :
        utils.GetFluxes(Fit,FitRunner.obs.Emin,FitRunner.obs.Emax) #print the flux of all the sources

    utils.DumpResult(Result, config)

    #plot the SED and model map if possible and asked
    if config['Spectrum']['ResultPlots'] == 'yes' :
    	os.system("mkdir -p "+config['out'] + '/Spectrum/')
        if float(config['UpperLimit']['TSlimit']) < Fit.Ts(config['target']['name']):
            FitRunner.ComputeSED(Fit)
        outXml = utils._dump_xml(config)
        if config['Spectrum']['SummedLike'] == 'yes': # the possiblity of making the model map is checked inside the function
            FitRunnerback.ModelMap(outXml)
            FitRunnerfront.ModelMap(outXml)
        else:
            FitRunner.ModelMap(outXml)

    #  Make energy bins by running a *new* analysis
    Nbin = config['Ebin']['NumEnergyBins']
    energybin.RunEbin(folder,Nbin,Fit,FitRunner)
Esempio n. 3
0
def run(infile):
    """Run an entire Fermi analysis (spectrum) by reading a config file"""
    config = get_config(infile)
    folder = config['out']
    os.system('mkdir -p ' + folder)

    runfit,Fit = GenAnalysisObject(config)
    # create all the fit files and run gtlike
    runfit.PerformFit(Fit)

    if config['verbose'] == 'yes' :
        utils.GetFluxes(Fit,runfit.obs.Emin,runfit.obs.Emax) #print the flux of all the sources

    Result = runfit.GetAndPrintResults(Fit)#Get and dump the target specific results
    utils.DumpResult(Result, config)

    #plot the SED and model map if possible and asked
    if config['Spectrum']['ResultPlots'] == 'yes' :
    	os.system("mkdir -p "+config['out'] + '/Spectrum/')
        if float(config['UpperLimit']['TSlimit']) < Fit.Ts(config['target']['name']):
            runfit.ComputeSED(Fit)
        outXml = utils._dump_xml(config)
        if SummedLike == 'yes': # the possiblity of making the model map is checked inside the function
            runfitback.ModelMap(outXml)
            runfitfront.ModelMap(outXml)
        else:
            runfit.ModelMap(outXml)

    #  Make energy bins by run a *new* analysis
    Nbin = config['Ebin']['NumEnergyBins']
    if int(Nbin) > 0:
        configfiles = utils.PrepareEbin(Fit, runfit)
        ind = 0
        enricodir = environ.DIRS.get('ENRICO_DIR')
        fermidir = environ.DIRS.get('FERMI_DIR')
        for conf in configfiles:
             pathconf = folder + "/Ebin" + str(Nbin) +"/" + conf
             Newconfig = get_config(pathconf)
             cmd = enricodir+"/enrico/RunGTlike.py "+pathconf
             if Newconfig['Ebin']['Submit'] == 'no' : #run directly
                 os.system(cmd)
             else : #submit a job to a cluster
                 prefix = Newconfig['out'] + "/Ebin" + str(ind) 
                 scriptname = prefix + "_Script.sh"
                 JobLog = prefix + "_Job.log"
                 JobName = (Newconfig['target']['name'] + "_" +
                           Newconfig['analysis']['likelihood'] +
                           "_Ebin_" + str(ind) + "_" + Newconfig['file']['tag'])
                 call(cmd, enricodir, fermidir, scriptname, JobLog, JobName)# submition
             ind+=1
Esempio n. 4
0
    def PerformFit(self, Fit):
        """Run gtlile tool. First it run gtlike with the DRNMGB optimizer
        and the user optimizer after. A dictionnay is return with all
        the releveant results"""

        self._log('gtlike', 'Run likelihood analysis')
        try:
            Fit.fit(0) #first try to run gtlike to approche the minimum
        except:
            pass
        # Now the precise fit will be done
        #change the fit tolerance to the one given by the user
        Fit.ftol = float(self.config['fitting']['ftol'])
        #fit with the user optimizer and ask gtlike to compute the covariance matrix 
        self.log_like = Fit.fit(0,covar=True, optimizer=self.config['fitting']['optimizer'])
        #fit with the user optimizer and ask gtlike to compute the covariance matrix 
        Fit.writeXml(utils._dump_xml(self.config))