def extract_spectra_iact(dataset): """Extract 1d spectra for IACT dataset""" log.info(f"Extracting 1d spectra for {dataset.name} dataset") datastore = DataStore.from_dir(f"data/{dataset.name}") observations = datastore.get_observations(dataset.obs_ids) on_region = CircleSkyRegion(center=config.source_pos, radius=dataset.on_radius) exclusion_mask = config.get_exclusion_mask() bkg_estimate = ReflectedRegionsBackgroundEstimator( observations=observations, on_region=on_region, exclusion_mask=exclusion_mask) bkg_estimate.run() extract = SpectrumExtraction( observations=observations, bkg_estimate=bkg_estimate.result, e_true=config.energy_bins, e_reco=config.energy_bins, containment_correction=dataset.containment_correction, ) extract.run() path = f"results/spectra/{dataset.name}/" log.info(f"Writing to {path}") if dataset.name == "fact": # For FACT the IRFs are the same for all observations # So we only store a stacked spectrum and response # plus we add a LO_THRESHOLD keyword was missing obs = extract.spectrum_observations.stack() obs.lo_threshold = 0.4 * u.TeV # we are writing a single observation, as for Fermi obs.write(path, use_sherpa=True, overwrite=True) else: extract.write(path, ogipdir="", use_sherpa=True, overwrite=True)
def extract_spectra_IACT(dataset): """Extract 1d spectra for IACT dataset""" log.info(f"Extracting 1d spectra for {dataset.name} dataset") # Dataset class has already the method to obtain the gammapy DataStore object datastore = dataset.get_DataStore() obs_list = datastore.obs_list(dataset.obs_ids) on_region = CircleSkyRegion(center=dataset.source_pos, radius=dataset.on_radius) exclusion_mask = config.get_exclusion_mask() bkg_estimate = ReflectedRegionsBackgroundEstimator( obs_list=obs_list, on_region=on_region, exclusion_mask=exclusion_mask) bkg_estimate.run() extract = SpectrumExtraction( obs_list=obs_list, bkg_estimate=bkg_estimate.result, containment_correction=dataset.containment_correction, ) extract.run() path = f"{config.repo_path}/results/spectra/{dataset.name}/" log.info(f"Writing to {path}") if dataset.name == "fact": # For FACT the IRFs are the same for all observations # So we only store a stacked spectrum and response # plus we add a LO_THRESHOLD keyword was missing obs = extract.observations.stack() obs.lo_threshold = 0.4 * u.TeV # we are writing a single observation, as for Fermi obs.write(path, use_sherpa=True, overwrite=True) else: extract.write(path, ogipdir="", use_sherpa=True, overwrite=True)
obs_summary.plot_excess_vs_livetime(ax=ax1) ax2=fig.add_subplot(122) obs_summary.plot_significance_vs_livetime(ax=ax2) plt.plot() plt.show() e_reco = EnergyBounds.equal_log_spacing(0.1, 40, 40, unit='TeV') e_true = EnergyBounds.equal_log_spacing(0.05, 100., 200, unit='TeV') ANALYSIS_DIR = 'crab_analysis' extraction = SpectrumExtraction(obs_list=crablist, bkg_estimate=background_estimator.result, containment_correction=False,) extraction.run() extraction.compute_energy_threshold(method_lo='area_max', area_percent_lo=10.0) print(extraction.observations[0]) extraction.run(obs_list=obs_list, bkg_estimate=background_estimator.result, outdir=ANALYSIS_DIR) extraction.run(obs_list=crablist, bkg_estimate=background_estimator.result, outdir=ANALYSIS_DIR) extraction.write(obs_list=crablist, bkg_estimate=background_estimator.result, outdir=ANALYSIS_DIR) extraction.write(crablist) extraction.write(crablist, bkg_estimate=background_estimator.result, outdir=ANALYSIS_DIR) extraction.write(crablist, background_estimator.result, outdir=ANALYSIS_DIR) get_ipython().magic(u'pinfo extraction.write') extraction.observations[0].peek() model = PowerLaw( index=2 * u.Unit(''), amplitude=2e-11 * u.Unit('cm-2 s-1 TeV-1'), reference=1 * u.TeV, ) joint_fit = SpectrumFit(obs_list=extraction.observations, model=model) joint_fit.fit() joint_fit.est_errors() #fit.run(outdir = ANALYSIS_DIR)