def PerformLikelihoodAnalysis(self): print "\nPerforming likelihood analysis on position: ra=%s, dec=%s" % ( self.xref, self.yref) # Wait a random amount of time between 1 and 5 minutes before starting in order to not crash the asf/nsf disks at SLAC waitTime = random.random() * 300 time.sleep(waitTime) # Defind the scratch directory JobID = os.environ.get('LSB_JOBID') Username = getpass.getuser() ScratchDirectory = "/scratch/%s/%s/" % (Username, JobID) # Define the pfile directory if JobID == 'None': PFILESDirectory = "%s/pfiles_%s/" % (self.outdir, self.binNumber) else: PFILESDirectory = "%s/pfiles/" % ScratchDirectory # Create the output directory if it doesn't already exist if (os.path.isdir(self.outdir) == False): print "\n >> Creating Directory: " + self.outdir cmd = "mkdir " + self.outdir os.system(cmd) # Define where to save the results likelihoodResults = '%s/likelihoodResults_bin%s.txt' % (self.outdir, self.binNumber) # Remove any pre-existing pfiles if (os.path.isdir(PFILESDirectory) == True): cmd = "rm -r %s" % PFILESDirectory os.system(cmd) # Set the new pfiles directory SetPfilesDirectory(PFILESDirectory) # Make a copy of the source model xmlModelWithPutativeSource = '%s/ModelSource_bin%s.xml' % ( self.outdir, self.binNumber) cmd = "cp " + self.srcmdl + " " + xmlModelWithPutativeSource print cmd os.system(cmd) # Add a putative point source at the requested location # AddCandidateSource(self.xref, self.yref, xmlModelWithPutativeSource) ModifySourceModel(xmlModelWithPutativeSource, self.xref, self.yref) # # Import the necessary gtapps # gtlike = GtApp('gtlike') # # Run the likelihood analysis # print '\nPerforming the likelihood fit:' # gtlike.run(statistic=self.statistic, # scfile=self.scfile, # evfile=self.evfile, # expmap=self.expmap, # expcube=self.expcube, # srcmdl=xmlModelWithPutativeSource, # irfs=self.irfs, # optimizer=self.optimizer, # results=likelihoodResults, # plot='no', # save='yes') # Setup the unbinned likelihood object print '\nPerforming the likelihood fit:' try: obs = UnbinnedObs(self.evfile, self.scfile, expMap=self.expmap, expCube=self.expcube, irfs=self.irfs) # Define the likelihood object #like = UnbinnedAnalysis(obs,xmlModelWithPutativeSource,optimizer=self.optimizer) like = UnbinnedAnalysis(obs, xmlModelWithPutativeSource, optimizer='MINUIT') # Setup the likelihood parameters Source = 'CandidateSource' Integral = like.par_index(Source, 'Integral') Index = like.par_index(Source, 'Index') LowerLimit = like.par_index(Source, 'LowerLimit') UpperLimit = like.par_index(Source, 'UpperLimit') # Setup the likelihood bounds like[Integral].setScale(1e-3) like[Index].setBounds(-5, -0.5) # like[LowerLimit] = emin # like[UpperLimit] = emax # Perform the likelihood fit #optObject = pyLike.NewMinuit(like.logLike) #like.fit(verbosity=0,covar=True,tol=0.02,optObject=optObject) like.fit(verbosity=1, covar=True, tol=1e-10, optimizer='MINUIT', optObject=None) # Extract the best fit index IndexValue = like[Index].value() IndexError = like[Index].error() # Extract the best fit flux FluxValue = like.flux(Source, emin=100, emax=3e5) FluxError = like.fluxError(Source, emin=100, emax=3e5) # Extract likelihood fit results print '\nLikelihood Results:' print like.model[Source] print "TS = %s" % like.Ts(Source) print "Flux = %s +/- %s" % (FluxValue, FluxError) print "Index = %s +/- %s" % (IndexValue, IndexError) # Save the xml file like.writeXml(xmlFile=xmlModelWithPutativeSource) except Exception, message: print traceback.format_exc()
like.tol like.tolType like.tol = 0.0001 if optimizerType=='Minuit': likeobj = pyLike.Minuit(like.logLike) elif optimizerType=='NewMinuit': likeobj = pyLike.NewMinuit(like.logLike) else: print "Error: Bad Optimizer. Exiting" exit() like.fit(verbosity=0,covar=True,optObject=likeobj) # Warning: This takes VERY long ~ 30 minutes # Get all the TS values of the sources in the model sourceDetails = {} for source in like.sourceNames(): sourceDetails[source] = like.Ts(source) # Output for data os.system('mkdir results') printDictionaryToFile(sourceDetails,'TSValues_preDelete.txt') # N.B A source with a TS value less than 0 should never happen unless the minimization failed. Remove that source and try fitting again and check the return code again. converganceCheckOne="Zero?: %d" %(likeobj.getRetCode()) converganceCheckTwo="NA" print converganceCheckOne # This value should be zero, refit using the method below if not if not likeobj.getRetCode() ==0 or eMin<100000: # If energy is too high, just don't refit because it causes problems print colors.OKBLUE+"Minimization Failed. Removing sources with TS < 9.0 and refitting!"
def main(NAME, RA, DEC, TSTART, TSTOP, EMIN, EMAX, SC, ROIu, xml): ROIue = float(ROIu) + 10 os.system('ls -1 *PH*.fits > %s_events.list' % (NAME)) my_apps.filter['evclass'] = 128 my_apps.filter['evtype'] = 3 my_apps.filter['ra'] = RA my_apps.filter['dec'] = DEC my_apps.filter['rad'] = ROIu my_apps.filter['emin'] = EMIN my_apps.filter['emax'] = EMAX my_apps.filter['zmax'] = 90 my_apps.filter['tmin'] = TSTART my_apps.filter['tmax'] = TSTOP my_apps.filter['infile'] = '@%s_events.list' % (NAME) my_apps.filter['outfile'] = '%s_filtered.fits' % (NAME) my_apps.filter.run() # maketime my_apps.maketime['scfile'] = SC my_apps.maketime['filter'] = '(DATA_QUAL>0)&&(LAT_CONFIG==1)' my_apps.maketime['roicut'] = 'no' my_apps.maketime['evfile'] = '%s_filtered.fits' % (NAME) my_apps.maketime['outfile'] = '%s_filtered_gti.fits' % (NAME) my_apps.maketime.run() # my_apps.counts_map['evfile'] = '%s_filtered_gti.fits' % (NAME) my_apps.counts_map['scfile'] = SC my_apps.counts_map['outfile'] = '%s_CountMap.fits' % (NAME) # my_apps.counts_map.run() # my_apps.expCube['evfile'] = '%s_filtered_gti.fits' % (NAME) my_apps.expCube['scfile'] = SC my_apps.expCube['zmax'] = 90 my_apps.expCube['outfile'] = 'expCube.fits' my_apps.expCube['dcostheta'] = 0.025 my_apps.expCube['binsz'] = 1 my_apps.expCube.run() my_apps.expMap['evfile'] = '%s_filtered_gti.fits' % (NAME) my_apps.expMap['scfile'] = SC my_apps.expMap['expcube'] = 'expCube.fits' my_apps.expMap['outfile'] = 'expMap.fits' my_apps.expMap['irfs'] = 'CALDB' my_apps.expMap['srcrad'] = ROIue my_apps.expMap['nlong'] = 120 my_apps.expMap['nlat'] = 120 my_apps.expMap['nenergies'] = 20 my_apps.expMap.run() # sara xml model roiname = '%s_filtered_gti.fits' % NAME if float(xml) == 0: xml_creator_P8_v1.main(NAME, float(RA), float(DEC), float(EMIN), float(EMAX), 15) xmlmodelname = '%s_model.xml' % NAME my_apps.diffResps['evfile'] = '%s_filtered_gti.fits' % (NAME) my_apps.diffResps['scfile'] = SC my_apps.diffResps['srcmdl'] = xmlmodelname my_apps.diffResps['irfs'] = 'CALDB' my_apps.diffResps.run() xmlfitname = '%s_fit1.xml' % NAME obs = UnbinnedObs(roiname, SC, expMap='expMap.fits', expCube='expCube.fits', irfs='CALDB') # like1 = UnbinnedAnalysis(obs,xmlmodelname,optimizer='MINUIT') like1 = UnbinnedAnalysis(obs, xmlmodelname, optimizer='NewMinuit') likeobj = pyLike.NewMinuit(like1.logLike) like1.fit(verbosity=0, optObject=likeobj) print likeobj.getRetCode() sourceDetails = {} for source in like1.sourceNames(): sourceDetails[source] = like1.Ts(source) for source, TS in sourceDetails.iteritems(): if (TS < 2): print "Deleting...", source, " TS = ", TS like1.deleteSource(source) like1.fit(verbosity=0, optObject=likeobj) print "0 is converged", likeobj.getRetCode() like1.logLike.writeXml(xmlfitname) numl = search(NAME, xmlfitname) numlg = str(numl + 3) os.system("sed '" + numlg + "," + numlg + " s/free=\"1\"/free=\"0\"/' " + xmlfitname + " > xml_sed.xml ") inputs = likeInput(like1, NAME, model="xml_sed.xml", nbins=6, phCorr=1.0) inputs.plotBins() inputs.fullFit(CoVar=True) sed = likeSED(inputs) sed.getECent() sed.fitBands() sed.Plot() result = like1.model[NAME] TS = like1.Ts(NAME) flux = like1.flux(NAME, emin=100) gamma = like1.model[NAME].funcs['Spectrum'].getParam('Index').value() cov_gg = like1.model[NAME].funcs['Spectrum'].getParam('Index').error() # cov_II = like1.model[NAME].funcs['Spectrum'].getParam('Integral').error() flux_err = like1.fluxError(NAME, emin=100) like1.plot() fitsedname = '%s_6bins_likeSEDout.fits' % NAME sedtool(fitsedname) print NAME, " TS=", TS print result if float(xml) == 1: xmlmodelname = '%s_model.xml' % NAME xmlfitname = '%s_fit1.xml' % NAME obs = UnbinnedObs(roiname, SC, expMap='expMap.fits', expCube='expCube.fits', irfs='CALDB') # like1 = UnbinnedAnalysis(obs,xmlmodelname,optimizer='MINUIT') like1 = UnbinnedAnalysis(obs, xmlmodelname, optimizer='NewMinuit') likeobj = pyLike.NewMinuit(like1.logLike) like1.fit(verbosity=0, optObject=likeobj) print likeobj.getRetCode() sourceDetails = {} for source in like1.sourceNames(): sourceDetails[source] = like1.Ts(source) for source, TS in sourceDetails.iteritems(): if (TS < 2): print "Deleting...", source, " TS = ", TS like1.deleteSource(source) like1.fit(verbosity=0, optObject=likeobj) print "0 is converged", likeobj.getRetCode() like1.logLike.writeXml(xmlfitname) numl = search(NAME, xmlfitname) numlg = str(numl + 3) os.system("sed '" + numlg + "," + numlg + " s/free=\"1\"/free=\"0\"/' " + xmlfitname + " > xml_sed.xml ") inputs = likeInput(like1, NAME, model="xml_sed.xml", nbins=6, phCorr=1.0) inputs.plotBins() inputs.fullFit(CoVar=True) sed = likeSED(inputs) sed.getECent() sed.fitBands() sed.Plot() result = like1.model[NAME] TS = like1.Ts(NAME) flux = like1.flux(NAME, emin=100) gamma = like1.model[NAME].funcs['Spectrum'].getParam('Index').value() cov_gg = like1.model[NAME].funcs['Spectrum'].getParam('Index').error() # cov_II = like1.model[NAME].funcs['Spectrum'].getParam('Integral').error() flux_err = like1.fluxError(NAME, emin=100) like1.plot() fitsedname = '%s_6bins_likeSEDout.fits' % NAME sedtool(fitsedname) print NAME, " TS=", TS print result
like.tol like.tolType like.tol = 0.0001 if optimizerType=='Minuit': likeobj = pyLike.Minuit(like.logLike) elif optimizerType=='NewMinuit': likeobj = pyLike.NewMinuit(like.logLike) else: print "Error: Bad Optimizer. Exiting" exit() like.fit(verbosity=0,covar=True,optObject=likeobj) # Warning: This takes VERY long ~ 1 hour # Get all the TS values of the sources in the model sourceDetails = {} for source in like.sourceNames(): sourceDetails[source] = like.Ts(source) # Output for data os.system('mkdir results') printDictionaryToFile(sourceDetails,'TSValues_preDelete.txt') # N.B A source with a TS value less than 0 should never happen unless the minimization failed. Remove that source and try fitting again and check the return code again. print "Zero?: %d" %(likeobj.getRetCode()) # This value should be zero, refit using the method below if not # Delete useing like.deleteSource('_2FGLJ1625.2-0020') # Protip, you can really simplify the model by removing sources with TS levels below 9 (about 3 sigma) for source,TS in sourceDetails.iteritems(): print source, TS if (TS < 9.0): print "Deleting..."
def runFermiTools(Name, RA, DEC, minEnergy, maxEnergy, SCFile, radius, binsz, TSTART, TSTOP, Evfile, bins, zmax, evclass, evtype, TSul, NMtol, lc_bin_num, runMRM): print "Working on bin " + str(lc_bin_num) + " for the light curve." f = FermiObject() """ Following steps execute Fermi Tool gtselect """ print('\nWorking on file.') print('Cutting file to fit desired parameters . . .\n') f._setEvclass(evclass) f._setEvtype(evtype) f._setRa(RA) f._setDec(DEC) f._setRad(radius) f._setEmin(minEnergy) f._setEmax(maxEnergy) f._setZmax(zmax) f._setTmin(TSTART) f._setTmax(TSTOP) f._setInfile(Evfile) f._setOutfile(Name + '_gtselect' + str(lc_bin_num) + '_lc.fits') f.amonSelect() print( 'File cuts have been made. Now making cuts for GTI using spacecraft file.' ) """ Following steps execute Fermi Tool gtmktime """ f._setScfile(SCFile) f._setRoicut('no') f._setEvfile(Name + '_gtselect' + str(lc_bin_num) + '_lc.fits') f._setOutfile(Name + '_gtmktime' + str(lc_bin_num) + '_lc.fits') ############################################### # Filter expression # Filter = '(DATA_QUAL>0)&&(LAT_CONFIG==1)' ############################################### f._setFilter(Filter) print('Working on file ' + str(f.getOutfile()) + '. . .') f.amonTime() print('File cuts have been made.') print('Using XML model from whole dataset.\n Moving on to gtltcube.') print "Now working on ltcube file using gtltcube\n" my_apps.expCube['evfile'] = Name + '_gtmktime' + str( lc_bin_num) + '_lc.fits' my_apps.expCube['scfile'] = SCFile my_apps.expCube['outfile'] = Name + '_ltcube' + str( lc_bin_num) + '_lc.fits' my_apps.expCube['dcostheta'] = 0.025 my_apps.expCube['binsz'] = 1 my_apps.expCube['phibins'] = 0 my_apps.expCube['zmax'] = zmax my_apps.expCube['chatter'] = 0 my_apps.expCube.run() print "\nltcube complete.\nMoving to compute exposure map with gtexpmap.\n" my_apps.expMap['evfile'] = Name + '_gtmktime' + str( lc_bin_num) + '_lc.fits' my_apps.expMap['scfile'] = SCFile my_apps.expMap['expcube'] = Name + '_ltcube' + str(lc_bin_num) + '_lc.fits' my_apps.expMap['outfile'] = Name + '_expMap' + str(lc_bin_num) + '_lc.fits' my_apps.expMap['irfs'] = 'CALDB' my_apps.expMap['srcrad'] = radius + 10 my_apps.expMap['nlong'] = 4 * (radius + 10) my_apps.expMap['nlat'] = 4 * (radius + 10) ebin = int(10 * log10(maxEnergy / minEnergy)) print "There are " + str(ebin) + " energy bans." my_apps.expMap['nenergies'] = ebin my_apps.expMap.run() print "Finnished making exposure map.\n" print "Calcualting the diffuse response for photons in this bin." my_apps.diffResps['evfile'] = Name + '_gtmktime' + str( lc_bin_num) + '_lc.fits' my_apps.diffResps['scfile'] = SCFile my_apps.diffResps['srcmdl'] = Name + '_output_model.xml' my_apps.diffResps['irfs'] = 'CALDB' my_apps.diffResps.run() print "Finished calculating diffuse response. Now moving to conduct a UNBINNED likelihood analysis." obs = UnbinnedObs(Name + '_gtmktime' + str(lc_bin_num) + '_lc.fits', SCFile, expMap=Name + '_expMap' + str(lc_bin_num) + '_lc.fits', expCube=Name + '_ltcube' + str(lc_bin_num) + '_lc.fits', irfs='P8R2_SOURCE_V6') analysis = UnbinnedAnalysis(obs, Name + '_output_model.xml', optimizer='NewMinuit') likeObj = pyLike.NewMinuit(analysis.logLike) analysis.tol = NMtol LIKE = analysis.fit(verbosity=0, covar=True, optObject=likeObj) fit = likeObj.getRetCode() print "Likelihood has converged whith Code " + str(likeObj.getRetCode()) Flux = analysis.flux(Name, emin=minEnergy, emax=maxEnergy) Ferr = analysis.fluxError(Name, emin=minEnergy, emax=maxEnergy) MeVtoErg = 1.602e-6 ef = analysis.energyFlux(Name, minEnergy, maxEnergy) * MeVtoErg ef_err = analysis.energyFluxError(Name, minEnergy, maxEnergy) * MeVtoErg UL = False TSUM = TSTART + TSTOP TMID = TSUM / 2 limit = Flux if analysis.Ts(Name) < TSul: UL = True limit, results = IUL.calc_int(analysis, Name, cl=0.90, emin=minEnergy, emax=maxEnergy) #Do second likelihood with constant flux to calculate the TS variability obsC = UnbinnedObs(Name + '_gtmktime' + str(lc_bin_num) + '_lc.fits', SCFile, expMap=Name + '_expMap' + str(lc_bin_num) + '_lc.fits', expCube=Name + '_ltcube' + str(lc_bin_num) + '_lc.fits', irfs='P8R2_SOURCE_V6') analysisC = UnbinnedAnalysis(obsC, Name + '_var_model.xml', optimizer='NewMinuit') likeObjC = pyLike.NewMinuit(analysisC.logLike) analysisC.tol = NMtol LIKEC = analysisC.fit(verbosity=0, covar=True, optObject=likeObjC) #Run gtselect to make smaller data fits file to compute the exposure, set to 3 degrees around source of interest f._setRad(3) f._setInfile(Evfile) f._setOutfile(Name + '_gtselect' + str(lc_bin_num) + '_exposure.fits') print "Creating file " + Name + "_gtselect_exposure.fits" f.amonSelect() #Run gtmaketime on this small region f._setEvfile(Name + '_gtselect' + str(lc_bin_num) + '_exposure.fits') f._setOutfile(Name + '_gtmktime' + str(lc_bin_num) + '_exposure.fits') print('Working on file ' + str(f.getOutfile())) f.amonTime() my_apps.evtbin['algorithm'] = 'LC' my_apps.evtbin['evfile'] = f.getOutfile() my_apps.evtbin['outfile'] = Name + '_LC' + str( lc_bin_num) + '_exposure.fits' my_apps.evtbin['scfile'] = f.getScfile() my_apps.evtbin['tbinalg'] = 'LIN' my_apps.evtbin['tstart'] = f.getTmin() my_apps.evtbin['tstop'] = f.getTmax() my_apps.evtbin['dtime'] = TSTOP - TSTART my_apps.evtbin.run() yes = subprocess.call([ 'gtexposure', Name + '_LC' + str(lc_bin_num) + '_exposure.fits', f.getScfile(), 'P8R2_SOURCE_V6', Name + '_output_model.xml', Name ]) if yes == 0: print "Exposure map has been created" else: print "Subprocessing failed. Unable to create exposure map with gtexposure." print "Time bin complete." hdulist = pyfits.open(Name + '_LC' + str(lc_bin_num) + '_exposure.fits') tbdata = hdulist[1].data z = tbdata['EXPOSURE'] exp = z[0] ################################################################ # This portion prints to the text file # ################################################################ f = open("lc_output.txt", "a") f.write( str(Flux) + ',' + str(Ferr) + ',' + str(ef) + ',' + str(ef_err) + ',' + str(limit) + ',' + str(analysis.Ts(Name)) + ',' + str(UL) + ',' + str(TMID) + ',' + str(exp) + ',' + str(LIKE) + ',' + str(LIKEC) + '\n') f.close() print "Likelihood analysis on this band is complete." yes = subprocess.call([ 'rm', Name + '_gtselect' + str(lc_bin_num) + '_lc.fits', Name + '_gtmktime' + str(lc_bin_num) + '_lc.fits', Name + '_cmap' + str(lc_bin_num) + '_lc.fits', Name + '_ccube' + str(lc_bin_num) + '_lc.fits', Name + '_ltcube' + str(lc_bin_num) + '_lc.fits', Name + '_expMap' + str(lc_bin_num) + '_lc.fits', Name + '_LC' + str(lc_bin_num) + '_exposure.fits', Name + '_srcmaps' + str(lc_bin_num) + '_lc.fits', Name + '_gtselect' + str(lc_bin_num) + '_exposure.fits', Name + '_gtmktime' + str(lc_bin_num) + '_exposure.fits' ]) if yes == 0: print 'Files for bin have been deleted' else: print "Subprocessing failed. Unable to delete files for bin."
def run(Name, Ra, Dec, minEnergy, maxEnergy, SCFile, ulTS, NMtol,evclass,model): if evclass == 512: irf = "P8R2_ULTRACLEAN_V6" elif evclass == 128: irf = "P8R2_SOURCE_V6" elif evclass == 256: irf = "P8R2_CLEAN_V6" elif evclass == 1024: irf = "P8R2_ULTRACLEANVETO_V6" print "This is multiUBLike.\nPlease make sure that you have added a model for your source with the name: " + str(Name) print "Calcualting the diffuse response for photons in this bin." my_apps.diffResps['evfile'] = '../' + Name + '_gtmktime.fits' my_apps.diffResps['scfile'] = SCFile my_apps.diffResps['srcmdl'] = model my_apps.diffResps['irfs'] = irf my_apps.diffResps.run() print "Finished calculating diffuse response. Now moving to conduct a UNBINNED likelihood analysis." obs = UnbinnedObs('../' + Name + '_gtmktime.fits', SCFile,expMap= '../' + Name + '_expMap.fits',expCube= '../' + Name + '_ltcube.fits', irfs=irf) analysis = UnbinnedAnalysis(obs,model,optimizer='NewMinuit') likeObj = pyLike.NewMinuit(analysis.logLike) analysis.tol = NMtol lkl = analysis.fit(verbosity=0,covar=True,optObject=likeObj) analysis.writeXml( Name + '_output_model.xml') fit = likeObj.getRetCode() print "Likelihood has converged whith Code " + str(likeObj.getRetCode()) multiLike.printResults(analysis,Name, minEnergy,maxEnergy) print "Fit has likelihood: " + str(lkl) print "\nThe TS is below the threshold, calculating 95% confidence-level Bayesian upper limit." limit,results = IUL.calc_int(analysis,Name,cl=0.95,emin=minEnergy, emax=maxEnergy) print "Bayesian upper limit: " + str(limit) + " photons/cm^2/s" #Array that returns the results of the unbinned analysis #log-likelihood,flux,flux_err,test statisitc Return = [lkl,analysis.flux(Name, emin=minEn, emax=maxEn),analysis.fluxError(Name, emin=minEn, emax=maxEn),limit,analysis.Ts(Name,reoptimize=False)] return Return
def main(NAME,RA,DEC,TSTART,TSTOP,EMIN,EMAX,Np, path, ROIu): #outdir = os.environ["FERMI_TMPLATAREA"] gtliketxt=open("%s/%s_gtlike.txt"%(path,Np),'w') gtsedtxt=open("%s/%s_sed.txt"%(path,Np),'w') SCC='%s_SC00.fits'%(Np) SC=path+SCC Npp=path+Np print SC ROIue=float(ROIu)+10 os.system("ls -1 '"+Npp+"'_PH*.fits > %s/%s_events.list" %(path,Np)) # os.system('ls -1 'Np'+'PH*.fits > %s/%s_events.list' %(path,Np) my_apps.filter['evclass'] = 128 my_apps.filter['evtype'] = 3 # my_apps.filter['evclsmin'] = 3 # my_apps.filter['evclsmax'] = 4 my_apps.filter['ra'] = RA my_apps.filter['dec'] = DEC my_apps.filter['rad'] = ROIu my_apps.filter['emin'] = EMIN my_apps.filter['emax'] = EMAX my_apps.filter['zmax'] = 90 my_apps.filter['tmin'] = TSTART my_apps.filter['tmax'] = TSTOP my_apps.filter['infile'] = '@%s/%s_events.list' %(path,Np) my_apps.filter['outfile'] = '%s/%s_filtered.fits'%(path,Np) my_apps.filter.run() # maketime my_apps.maketime['scfile'] = SC my_apps.maketime['filter'] = '(DATA_QUAL>0)&&(LAT_CONFIG==1)' my_apps.maketime['roicut'] = 'no' my_apps.maketime['evfile'] = '%s/%s_filtered.fits' %(path,Np) my_apps.maketime['outfile'] = '%s/%s_filtered_gti.fits' %(path,Np) my_apps.maketime.run() # # my_apps.counts_map['evfile'] = '%s/%s_filtered_gti.fits'%(path,Np) # my_apps.counts_map['scfile'] = SC # my_apps.counts_map['outfile'] = '%s/%s_CountMap.fits'%(path,Np) # my_apps.counts_map.run() # my_apps.expCube['evfile'] = '%s/%s_filtered_gti.fits'%(path,Np) my_apps.expCube['scfile'] = SC my_apps.expCube['zmax'] = 90 my_apps.expCube['outfile'] = '%s/%s_expCube.fits' %(path,Np) my_apps.expCube['dcostheta'] = 0.025 my_apps.expCube['binsz'] = 1 my_apps.expCube.run() my_apps.expMap['evfile'] = '%s/%s_filtered_gti.fits'%(path,Np) my_apps.expMap['scfile'] = SC my_apps.expMap['expcube'] ='%s/%s_expCube.fits' %(path,Np) my_apps.expMap['outfile'] ='%s/%s_expMap.fits' %(path,Np) # my_apps.expMap['irfs'] ='P7REP_SOURCE_V15' my_apps.expMap['irfs'] ='CALDB' my_apps.expMap['srcrad'] = ROIue my_apps.expMap['nlong'] =120 my_apps.expMap['nlat'] =120 my_apps.expMap['nenergies'] =20 my_apps.expMap.run() #sara xml model roiname='%s/%s_filtered_gti.fits' %(path,Np) xml_creator_P7_v1.main(path,NAME,float(RA),float(DEC),float(EMIN), float(EMAX), 20,Np) xmlmodelname='%s/%s_model.xml' %(path,Np) my_apps.diffResps['evfile'] = '%s/%s_filtered_gti.fits'%(path,Np) my_apps.diffResps['scfile'] = SC my_apps.diffResps['srcmdl'] = xmlmodelname my_apps.diffResps['irfs'] = 'CALDB' my_apps.diffResps.run() xmlfitname='%s/%s_fit1.xml' %(path,Np) expMapFile='%s/%s_expMap.fits' %(path,Np) expCubeFile='%s/%s_expCube.fits' %(path,Np) obs = UnbinnedObs(roiname,SC ,expMap=expMapFile,expCube=expCubeFile,irfs='CALDB') like1 = UnbinnedAnalysis(obs,xmlmodelname,optimizer='NewMinuit') like1.fit(verbosity=0) like1.logLike.writeXml(xmlfitname) # numl=search(NAME,xmlfitname) # numlg=str(numl+3) # os.system("sed '"+numlg+","+numlg+" s/free=\"1\"/free=\"0\"/' "+xmlfitname+ " > xml_sed.xml ") # inputs=likeInput(like1,NAME,model="xml_sed.xml",nbins=9,phCorr=1.0) #low_edges = [200.,914.61,1955.87,8944.27,19127.05,40902.61] #high_edges = [427.69,1955.87,8944.27,19127.05,40902.61,187049.69] #centers = [0.2767, 1.265, 5.787, 12.37, 26.46, 86.60] #inputs.customBins(low_edges,high_edges) # inputs.plotBins() # inputs.fullFit(CoVar=True) # sed = likeSED(inputs) # sed.getECent() # sed.fitBands() # sed.Plot() result=like1.model[NAME] TS=like1.Ts(NAME) # I = like1.model[NAME].funcs['Spectrum'].getParam('Integral').value() flux = like1.flux(NAME,emin=100) # flux=I*1e-9 gamma = like1.model[NAME].funcs['Spectrum'].getParam('Index').value() cov_gg =like1.model[NAME].funcs['Spectrum'].getParam('Index').error() # cov_II = like1.model[NAME].funcs['Spectrum'].getParam('Integral').error() flux_err = like1.fluxError(NAME,emin=100) # flux_err=cov_II*1e-9 e=1000.0 a=1 b=1.e-18 lenergy_bin=log10(double(EMIN))+(log10(double(EMAX))-log10(double(EMIN)))/2 energy_bin=pow(10,lenergy_bin) freq=2.42e22*energy_bin/100.0 ums = 1.-gamma conv=ums*pow(energy_bin,(-gamma))/(pow(double(EMAX),ums)-pow(double(EMIN),ums))*6.62e-2*(energy_bin/100.0) # conv is in Jy # now convert in nufnu erg/cm2/s convjy=conv*freq*1.e-23 nufnu=flux*convjy b=flux_err*convjy err_log=log10((nufnu+b)/nufnu) #cout<<freq<<" "<<a<<" "<<nufnu<<" "<<b<<endl; #cout<<log10(freq)<<" "<<log10(a)<<" "<<log10(nufnu)<<" "<<err_log<<endl; date_start=computeDate(float(TSTART)) date_stop=computeDate(float(TSTOP)) # like1.plot() # fitsedname='%s_9bins_likeSEDout.fits' %NAME # sedtool(fitsedname) print NAME, " TS=", TS # print result # print like1.model print "spectral index= ", gamma, " +/-", cov_gg print " Flux=", flux, "+/-", flux_err print "freq", freq, " nuFnu=", nufnu, b, # print "'UL': ", results_ul, err gtliketxt.write(NAME) gtliketxt.write(" RA=") gtliketxt.write(RA) gtliketxt.write(" DEC= ") gtliketxt.write(DEC) gtliketxt.write(" TS= ") gtliketxt.write(str(TS)) gtliketxt.write("\n") gtliketxt.write(" Time Interval (MJD) ") gtliketxt.write(str(date_start)) gtliketxt.write(" ") gtliketxt.write(str(date_stop)) gtliketxt.write("\n ") gtliketxt.write("Flux ") if TS <25: obs = UnbinnedObs(roiname,SC ,expMap=expMapFile,expCube=expCubeFile,irfs='CALDB') like1 = UnbinnedAnalysis(obs,xmlmodelname,optimizer='NewMinuit') like1.fit(verbosity=0) ul=UpperLimits(like1) UL=ul[NAME].compute(emin=double(EMIN),emax=double(EMAX)) results_ul=UL[1]*1E-9 err=0 print "'UL': ", results_ul, err gamma_ul=2.0 ums_ul = 1.-gamma_ul conv_ul=ums_ul*pow(energy_bin,(-gamma_ul))/(pow(double(EMAX),ums_ul)-pow(double(EMIN),ums_ul))*6.62e-2*(energy_bin/100.0) convjy_ul=conv_ul*freq*1.e-23 nufnu_ul=results_ul*convjy_ul b=err*convjy_ul print "freq", freq, "0 nuFnu=", nufnu_ul, b, gtliketxt.write(str(results_ul)) gtliketxt.write(" 0 ") #gtliketxt.write(err) gtsedtxt.write(str(freq)) gtsedtxt.write(" | 0 ") gtsedtxt.write(" | ") gtsedtxt.write(str(nufnu_ul)) gtsedtxt.write(" | ") gtsedtxt.write(str(b)) gtsedtxt.write(" | ") gtsedtxt.write(str(date_start)) gtsedtxt.write(" | ") gtsedtxt.write(str(date_stop)) gtsedtxt.write(" | ") gtsedtxt.write(" UL ") gtsedtxt.write(" | ") else: gtliketxt.write(str(flux)) gtliketxt.write(" ") gtliketxt.write(str(flux_err)) gtliketxt.write("\n") gtliketxt.write("Spectral Index = ") gtliketxt.write(str(gamma)) gtliketxt.write(" ") gtliketxt.write(str(cov_gg)) gtsedtxt.write(" ") gtsedtxt.write(str(freq)) gtsedtxt.write(" | 0 ") gtsedtxt.write(" | ") gtsedtxt.write(str(nufnu)) gtsedtxt.write(" | ") gtsedtxt.write(str(b)) gtsedtxt.write(" | ") gtsedtxt.write(str(date_start)) gtsedtxt.write(" | ") gtsedtxt.write(str(date_stop)) gtsedtxt.write(" | ")