def GenAnalysisObjects(config, verbose=1, xmlfile=""): mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) folder = config['out'] # If there are no xml files, create it and print a warning if len(glob.glob(config['file']['xml'].replace('.xml', '*.xml'))) == 0: mes.warning("Xml not found, creating one for the given config %s" % config['file']['xml']) XmlMaker(config) Fit = SummedLikelihood.SummedLikelihood() if hasKey(config, 'ComponentAnalysis') == True: # Create one obs instance for each component configs = [None] * 4 Fits = [None] * 4 Analyses = [None] * 4 if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes': from enrico.data import fermievtypes mes.info("Breaking the analysis in Front/Back events") # Set Summed Likelihood to True oldxml = config['file']['xml'] for k, TYPE in enumerate(["FRONT", "BACK"]): configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError, e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" % (TYPE, str(e))) FitRunner = Analyses[0]
def GenAnalysisObjects(config, verbose = 1, xmlfile =""): mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) folder = config['out'] # If there are no xml files, create it and print a warning if len(glob.glob(config['file']['xml'].replace('.xml','*.xml')))==0: mes.warning("Xml not found, creating one for the given config %s" %config['file']['xml']) XmlMaker(config) Fit = SummedLikelihood.SummedLikelihood() if hasKey(config,'ComponentAnalysis') == True: # Create one obs instance for each component configs = [None]*4 Fits = [None]*4 Analyses = [None]*4 if isKey(config['ComponentAnalysis'],'FrontBack') == 'yes': from enrico.data import fermievtypes mes.info("Breaking the analysis in Front/Back events") # Set Summed Likelihood to True oldxml = config['file']['xml'] for k,TYPE in enumerate(["FRONT", "BACK"]): configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not(xmlfile ==""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError,e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" %(TYPE,str(e))) FitRunner = Analyses[0]
Fit.addComponent(Fits[k]) except RuntimeError, e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" % (TYPE, str(e))) FitRunner = Analyses[0] EUnBinned = config['ComponentAnalysis']['EUnBinned'] emintotal = float(config['energy']['emin']) emaxtotal = float(config['energy']['emax']) evtnum = [config["event"]["evtype"]] #for std analysis evtold = evtnum[0] #for std analysis # Create one obs instance for each component if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes': evtnum = [1, 2] config['analysis']['likelihood'] = "binned" if isKey(config['ComponentAnalysis'], 'PSF') == 'yes': evtnum = [4, 8, 16, 32] config['analysis']['likelihood'] = "binned" if isKey(config['ComponentAnalysis'], 'EDISP') == 'yes': evtnum = [64, 128, 256, 521] config['analysis']['likelihood'] = "binned" oldxml = config['file']['xml'] for k, evt in enumerate(evtnum): config['event']['evtype'] = evt config["file"]["xml"] = oldxml.replace( ".xml", "_" + typeirfs[evt] + ".xml").replace("_.xml", ".xml") if EUnBinned > emintotal and EUnBinned < emaxtotal:
def GenAnalysisObjects(config, verbose=1, xmlfile=""): mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) folder = config['out'] Fit = SummedLikelihood.SummedLikelihood() EUnBinned = config['ComponentAnalysis']['EUnBinned'] emintotal = float(config['energy']['emin']) emaxtotal = float(config['energy']['emax']) evtnum = [config["event"]["evtype"]] #for std analysis evtold = evtnum[0] #for std analysis # Create one obs instance for each component if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes': evtnum = [1, 2] if isKey(config['ComponentAnalysis'], 'PSF') == 'yes': evtnum = [4, 8, 16, 32] if isKey(config['ComponentAnalysis'], 'EDISP') == 'yes': evtnum = [64, 128, 256, 521] oldxml = config['file']['xml'] for k, evt in enumerate(evtnum): config['event']['evtype'] = evt config["file"]["xml"] = oldxml.replace( ".xml", "_" + typeirfs[evt] + ".xml").replace("_.xml", ".xml") if EUnBinned > emintotal and EUnBinned < emaxtotal: mes.info( "Breaking the analysis in Binned (low energy) and Unbinned (high energies)" ) analysestorun = ["lowE", "highE"] for k, TYPE in enumerate(analysestorun): tag = TYPE if typeirfs[evt] != "": tag += "_" + typeirfs[evt] # handle name of fits file # Tune parameters if TYPE is "lowE": config['energy']['emin'] = emintotal config['energy']['emax'] = min(config['energy']['emax'], EUnBinned) config['analysis']['likelihood'] = "binned" config['analysis']['ComputeDiffrsp'] = "no" elif TYPE is "highE": config['energy']['emin'] = max(config['energy']['emin'], EUnBinned) config['energy']['emax'] = emaxtotal config['analysis']['likelihood'] = "unbinned" config['analysis']['ComputeDiffrsp'] = "yes" Analyse = Analysis(folder, config, \ configgeneric=config,\ tag=TYPE,\ verbose=verbose) Fit_component = Analyse.CreateLikeObject() Fit.addComponent(Fit_component) FitRunner = Analyse FitRunner.obs.Emin = emintotal FitRunner.obs.Emax = emaxtotal else: Analyse = Analysis(folder, config, \ configgeneric=config,\ tag=typeirfs[evt], verbose = verbose) # if not(xmlfile ==""): Analyse.obs.xmlfile = xmlfile Fit_component = Analyse.CreateLikeObject() Fit.addComponent(Fit_component) FitRunner = Analyse config["event"]["evtype"] = evtold FitRunner.config = config return FitRunner, Fit
Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError,e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" %(TYPE,str(e))) FitRunner = Analyses[0] EUnBinned = config['ComponentAnalysis']['EUnBinned'] emintotal = float(config['energy']['emin']) emaxtotal = float(config['energy']['emax']) evtnum = [config["event"]["evtype"]] #for std analysis evtold = evtnum[0] #for std analysis # Create one obs instance for each component if isKey(config['ComponentAnalysis'],'FrontBack') == 'yes': evtnum = [1, 2] config['analysis']['likelihood'] = "binned" if isKey(config['ComponentAnalysis'],'PSF') == 'yes': evtnum = [4,8,16,32] config['analysis']['likelihood'] = "binned" if isKey(config['ComponentAnalysis'],'EDISP') == 'yes': evtnum = [64,128,256,521] config['analysis']['likelihood'] = "binned" oldxml = config['file']['xml'] for k,evt in enumerate(evtnum): config['event']['evtype'] = evt config["file"]["xml"] = oldxml.replace(".xml","_"+typeirfs[evt]+".xml").replace("_.xml",".xml") if EUnBinned>emintotal and EUnBinned<emaxtotal: mes.info("Breaking the analysis in Binned (low energy) and Unbinned (high energies)")
def GenAnalysisObjects(config, verbose=1, xmlfile=""): import os import os.path import math import SummedLikelihood from enrico.xml_model import XmlMaker from enrico.extern.configobj import ConfigObj from utils import hasKey, isKey import Loggin mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) SummedLike = config['Spectrum']['SummedLike'] folder = config['out'] # If there is no xml file, create it and print a warning if (not os.path.isfile(config['file']['xml'])): mes.warning("Xml not found, creating one for the given config %s" % config['file']['xml']) XmlMaker(config) Fit = SummedLikelihood.SummedLikelihood() if hasKey(config, 'ComponentAnalysis') == True: # Create one obs instance for each component configs = [None] * 4 Fits = [None] * 4 Analyses = [None] * 4 if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes': from enrico.data import fermievtypes mes.info("Breaking the analysis in Front/Back events") # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' oldxml = config['file']['xml'] for k, TYPE in enumerate(["FRONT", "BACK"]): configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError, e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" % (TYPE, str(e))) FitRunner = Analyses[0] elif isKey(config['ComponentAnalysis'], 'PSF') == 'yes': from enrico.data import fermievtypes mes.info("Breaking the analysis in PSF 0,1,2,3.") # Clone the configs # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' for k, TYPE in enumerate(["PSF0", "PSF1", "PSF2", "PSF3"]): configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError, e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" % (TYPE, str(e))) FitRunner = Analyses[0]
configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k]) except RuntimeError, e: if 'RuntimeError: gtltcube execution failed' in str(e): mes.warning("Event type %s is empty! Error is %s" % (TYPE, str(e))) FitRunner = Analyses[0] elif isKey(config['ComponentAnalysis'], 'EDISP') == 'yes': from enrico.data import fermievtypes mes.info("Breaking the analysis in EDISP 0,1,2,3.") # Clone the configs # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' for k, TYPE in enumerate(["EDISP0", "EDISP1", "EDISP2", "EDISP3"]): configs[k] = ConfigObj(config) configs[k]['event']['evtype'] = fermievtypes[TYPE] try: Analyses[k] = Analysis(folder, configs[k], \ configgeneric=config,\ tag=TYPE, verbose = verbose) if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile Fits[k] = Analyses[k].CreateLikeObject() Fit.addComponent(Fits[k])
def GenAnalysisObjects(config, verbose = 1, xmlfile =""): # Array containing the list of analysis objects (needed to produce the individual residual maps) ListOfAnalysisObjects = [] mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) folder = config['out'] # If there are no xml files, create it and print a warning <--- Not sure if this is needed here. Fit = SummedLikelihood.SummedLikelihood() EUnBinned = config['ComponentAnalysis']['EUnBinned'] emintotal = float(config['energy']['emin']) emaxtotal = float(config['energy']['emax']) evtnum = [config["event"]["evtype"]] #for std analysis evtold = evtnum[0] #for std analysis # Create one obs instance for each component. # The first 3 can be combined with splitting in energy. The 4th tries to mimick 4FGL. if isKey(config['ComponentAnalysis'],'FrontBack') == 'yes': evtnum = [1, 2] config['analysis']['likelihood'] = "binned" elif isKey(config['ComponentAnalysis'],'PSF') == 'yes': evtnum = [4,8,16,32] config['analysis']['likelihood'] = "binned" elif isKey(config['ComponentAnalysis'],'EDISP') == 'yes': evtnum = [64,128,256,521] config['analysis']['likelihood'] = "binned" elif isKey(config['ComponentAnalysis'],'FGL4') == 'yes': # Special case of the PSF component analysis, # where up to 15 components (energy+PSF) are created following # 4FGL prescription. from catalogComponents import evtnum, energybins, nbinsbins, zmaxbins, ringwidths, pixelsizes config['analysis']['likelihood'] = "binned" oldxml = config['file']['xml'] bin_i = 0 roi = 0 # energybins is a dictionary containing an index and a pair of energies for ebin_i in energybins: # Restrict the analysis to the specified energy range in all cases. if emintotal>=energybins[ebin_i][1]: continue if emaxtotal<=energybins[ebin_i][0]: continue if (roi==0): roi = 2.*ringwidths[ebin_i]+4. zmax = zmaxbins[ebin_i] nbinsE = nbinsbins[ebin_i] energybin = energybins[ebin_i] for k,evt in enumerate(evtnum): pixel_size = pixelsizes[ebin_i][k] if pixel_size<0: continue tag = "{0}_En{1}".format(typeirfs[evt],ebin_i) # Approximation, in the 4FGL the core radius changes from src to src! mes.info("Breaking the analysis in bins ~ 4FGL") config['event']['evtype'] = evt config["file"]["xml"] = oldxml.replace(".xml","_")+typeirfs[evt]+"_"+\ "En{0}.xml".format(ebin_i) config["energy"]["emin"] = max(emintotal,energybin[0]) config["energy"]["emax"] = min(emaxtotal,energybin[1]) config["analysis"]["likelihood"] = "binned" config["analysis"]["ComputeDiffrsp"] = "no" config["analysis"]["enumbins_per_decade"] = \ int(1.*nbinsE/math.log10(energybin[1]/energybin[0])+0.5) config["space"]["rad"] = roi config["analysis"]["zmax"] = zmax Analyse = Analysis(folder, config, \ configgeneric=config,\ tag=tag, verbose=verbose) ListOfAnalysisObjects.append(Analyse) if not(xmlfile ==""): Analyse.obs.xmlfile = xmlfile mes.info('Creating Likelihood object for component.') Fit_component = Analyse.CreateLikeObject() mes.info('Adding component to the summed likelihood.') Fit.addComponent(Fit_component) FitRunner = Analyse FitRunner.obs.Emin = emintotal FitRunner.obs.Emax = emaxtotal config["energy"]["emin"] = emintotal config["energy"]["emax"] = emaxtotal config["event"]["evtype"] = evtold FitRunner.config = config return FitRunner,Fit,ListOfAnalysisObjects # Standard (non-4FGL) analysis components oldxml = config['file']['xml'] for k,evt in enumerate(evtnum): config['event']['evtype'] = evt if typeirfs[evt] != "": config["file"]["xml"] = oldxml.replace(".xml","_"+typeirfs[evt]+".xml") if EUnBinned>emintotal and EUnBinned<emaxtotal: mes.info("Breaking the analysis in Binned (low energy) and Unbinned (high energies)") analysestorun = ["lowE","highE"] for j,TYPE in enumerate(analysestorun): tag = TYPE if typeirfs[evt] != "" : tag += "_"+typeirfs[evt]# handle name of fits file config["file"]["xml"] = oldxml.replace(".xml","_"+tag+".xml") # Tune parameters if TYPE is "lowE": config['energy']['emin'] = emintotal config['energy']['emax'] = min(config['energy']['emax'],EUnBinned) config['analysis']['likelihood'] = "binned" config['analysis']['ComputeDiffrsp'] = "no" elif TYPE is "highE": config['energy']['emin'] = max(config['energy']['emin'],EUnBinned) config['energy']['emax'] = emaxtotal config['analysis']['likelihood'] = "unbinned" config['analysis']['ComputeDiffrsp'] = "yes" Analyse = Analysis(folder, config, \ configgeneric=config,\ tag=tag,\ verbose=verbose) ListOfAnalysisObjects.append(Analyse) mes.info('Creating Likelihood object for component.') Fit_component = Analyse.CreateLikeObject() mes.info('Adding component to the summed likelihood.') Fit.addComponent(Fit_component) FitRunner = Analyse FitRunner.obs.Emin = emintotal FitRunner.obs.Emax = emaxtotal config["energy"]["emin"] = emintotal config["energy"]["emax"] = emaxtotal else: Analyse = Analysis(folder, config, \ configgeneric=config,\ tag=typeirfs[evt], verbose = verbose) ListOfAnalysisObjects.append(Analyse) if not(xmlfile ==""): Analyse.obs.xmlfile = xmlfile mes.info('Creating Likelihood object for component.') Fit_component = Analyse.CreateLikeObject() mes.info('Adding component to the summed likelihood.') Fit.addComponent(Fit_component) FitRunner = Analyse config["event"]["evtype"] = evtold FitRunner.config = config return FitRunner,Fit,ListOfAnalysisObjects
def GenAnalysisObjects(config, verbose = 1, xmlfile =""): import os import os.path import math import SummedLikelihood from enrico.xml_model import XmlMaker from enrico.extern.configobj import ConfigObj from utils import hasKey, isKey import Loggin mes = Loggin.Message() #check is the summed likelihood method should be used and get the #Analysis objects (observation and (Un)BinnedAnalysis objects) SummedLike = config['Spectrum']['SummedLike'] folder = config['out'] # If there is no xml file, create it and print a warning if (not os.path.isfile(config['file']['xml'])): mes.warning("Xml not found, creating one for the given config %s" %config['file']['xml']) XmlMaker(config) Fit = SummedLikelihood.SummedLikelihood() if hasKey(config,'ComponentAnalysis') == True: # Create one obs instance for each component if isKey(config['ComponentAnalysis'],'FrontBack') == 'yes': mes.info("Breaking the analysis in Front/Back events") # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' FitRunnerfront = Analysis(folder, config, \ configgeneric=config,\ tag="FRONT", verbose = verbose) FitRunnerback = Analysis(folder, config, \ configgeneric=config,\ tag="BACK", verbose = verbose) if not(xmlfile ==""): FitRunnerfront.obs.xmlfile = xmlfile FitRunnerback.obs.xmlfile = xmlfile FitB = FitRunnerback.CreateLikeObject() FitF = FitRunnerfront.CreateLikeObject() Fit.addComponent(FitB) Fit.addComponent(FitF) FitRunner = FitRunnerback elif isKey(config['ComponentAnalysis'],'PSF') == 'yes': mes.info("Breaking the analysis in PSF 0,1,2,3.") # Clone the configs config_psfs = [None]*4 config_xmls = [None]*4 FitPSFs = [None]*4 AnalysisPSFs = [None]*4 # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' for k in xrange(4): config_psfs[k] = ConfigObj(config) # Tune parameters config_psfs[k]['event']['evtype'] = int(2**(k+2)) oldxml = config_psfs[k]['file']['xml'] AnalysisPSFs[k] = Analysis(folder, config_psfs[k], \ configgeneric=config,\ tag="PSF%d"%k,\ verbose = verbose) if not(xmlfile ==""): AnalysisPSF[k].obs.xmlfile = xmlfile FitPSFs[k] = AnalysisPSFs[k].CreateLikeObject() Fit.addComponent(FitPSFs[k]) FitRunner = AnalysisPSFs[0] elif isKey(config['ComponentAnalysis'],'EUnBinned')>=0: mes.info("Breaking the analysis in Binned (low energy) and Unbinned (high energies)") # Clone the configs config_bin = [None]*2 config_xmls = [None]*2 FitBIN = [None]*2 AnalysisBIN = [None]*2 # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' EUnBinned = config['ComponentAnalysis']['EUnBinned'] emintotal = float(config['energy']['emin']) emaxtotal = float(config['energy']['emax']) # Run the following analysis depending on the case # this is general, no matter if we are in the total fit or the Ebin #N fit. if EUnBinned<=emintotal: analysestorun = ["highE"] elif EUnBinned>=emaxtotal: analysestorun = ["lowE"] else: analysestorun = ["lowE","highE"] print(analysestorun) oldxml = config['file']['xml'] for k,name in enumerate(analysestorun): config_bin[k] = ConfigObj(config) # Tune parameters if name is "lowE": config_bin[k]['energy']['emin'] = emintotal config_bin[k]['energy']['emax'] = min(config['energy']['emax'],EUnBinned) config_bin[k]['analysis']['likelihood'] = "binned" config_bin[k]['analysis']['ComputeDiffrsp'] = "no" elif name is "highE": config_bin[k]['energy']['emin'] = max(config['energy']['emin'],EUnBinned) config_bin[k]['energy']['emax'] = emaxtotal config_bin[k]['analysis']['likelihood'] = "unbinned" config_bin[k]['analysis']['ComputeDiffrsp'] = "yes" AnalysisBIN[k] = Analysis(folder, config_bin[k], \ configgeneric=config,\ tag=name,\ verbose=verbose) if not(xmlfile ==""): AnalysisBIN[k].obs.xmlfile = xmlfile FitBIN[k] = AnalysisBIN[k].CreateLikeObject() Fit.addComponent(FitBIN[k]) FitRunner = AnalysisBIN[0] FitRunner.obs.Emin = emintotal FitRunner.obs.Emax = emaxtotal elif isKey(config['ComponentAnalysis'],'EDISP') == 'yes': mes.info("Breaking the analysis in EDISP 0,1,2,3.") # Clone the configs config_edisps = [None]*4 config_xmls = [None]*4 FitEDISPs = [None]*4 AnalysisEDISPs = [None]*4 # Set Summed Likelihood to True config['Spectrum']['SummedLike'] = 'yes' for k in xrange(4): config_edisps[k] = ConfigObj(config) # Tune parameters config_edisps[k]['event']['evtype'] = int(2**(k+6)) oldxml = config_edisps[k]['file']['xml'] AnalysisEDISPs[k] = Analysis(folder, config_edisps[k], \ configgeneric=config,\ tag="EDISP%d"%k,\ verbose = verbose) if not(xmlfile ==""): AnalysisEDISP[k].obs.xmlfile = xmlfile FitEDISPs[k] = AnalysisEDISPs[k].CreateLikeObject() Fit.addComponent(FitEDISPs[k]) print(Fit.components) FitRunner = AnalysisEDISPs[0] try: FitRunner except NameError: # Create one obs instance FitRunner = Analysis(folder, config, tag="", verbose = verbose) Fit.addComponent(FitRunner.CreateLikeObject()) if not(xmlfile ==""): FitRunner.obs.xmlfile = xmlfile FitRunner.config = config return FitRunner,Fit