# ## Power law # # In this section we will simulate one observation using a power law model. # In[ ]: pwl = PowerLaw(index=2.3, amplitude=1e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) print(pwl) # In[ ]: livetime = 2 * u.h sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=pwl, livetime=livetime) sim.simulate_obs(seed=2309, obs_id=1) print(sim.obs) # In[ ]: fit = SpectrumFit(obs_list=sim.obs, model=pwl.copy(), stat="cash") fit.fit_range = [1, 10] * u.TeV fit.run() print(fit.result[0]) # ## Include background # # In this section we will include a background component. Furthermore, we will also simulate more than one observation and fit each one individuallt in order to get average fit results.
amplitude = 2.5 * 1e-10 * u.Unit('cm-2 s-1 TeV-1') reference = 1 * u.TeV model = PowerLaw(index=index, amplitude=amplitude, reference=reference) edisp = edisp.to_energy_dispersion(offset=offset) aeff = aeff.to_effective_area_table(offset=offset) #fig, axes = plt.subplots(1, 2, figsize=(12, 6)) #edisp.plot_matrix(ax=axes[0]) #aeff.plot(ax=axes[1]) #plt.show() aeff.lo_threshold = lo_threshold aeff.hi_threshold = hi_threshold sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) sim.obs.peek() print sim.obs plt.show() fit = SpectrumFit(obs_list=sim.obs, model=model, stat='cash') fit.run() result = fit.result[0] print result energy_range = [0.1, 100] * u.TeV model.plot(energy_range=energy_range, energy_power=2) result.model.plot(energy_range=energy_range, energy_power=2)
plt.loglog() print(cta_irf["aeff"].data) # In[ ]: edisp = cta_irf["edisp"].to_energy_dispersion(offset=offset, e_true=energy, e_reco=energy) edisp.plot_matrix() print(edisp.data) # In[ ]: # Simulate data sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) # In[ ]: sim.obs.peek() print(sim.obs) # ## Spectral analysis # # Now that we have some simulated CTA counts spectrum, let's analyse it. # In[ ]: # Fit data
bkg_model = PowerLaw(index=bkg_index, amplitude=bkg_amplitude, reference=reference) #====================================== #SIMULATE SPECTRA livetime1 = time1 * u.h livetime2 = time2 * u.h n_obs = 100 seeds = np.arange(n_obs) sim1 = SpectrumSimulation(aeff=aeff1, edisp=edisp1, source_model=pwl, livetime=livetime1, background_model=bkg_model, alpha=alpha1) sim1.run(seeds) #print(sim1.result) #sim2 = SpectrumSimulation(aeff=aeff2, # edisp=edisp2, # source_model=pwl, # livetime=livetime2, # background_model=bkg_model, # alpha=alpha2) #sim2.run(seeds) #print(sim2.result)
aeff.lo_threshold = lo_threshold aeff.hi_threshold = hi_threshold get_ipython().magic(u'pinfo fakerun.bkg') inubilutbkh=fakerun.bkg() inubilutbkh=fakerun.bkg.evaluate_at_offset(offset=offset) inubuiltbkg=fakerun.bkg.evaluate_at_offset(offset=offset) inbuiltbkg=fakerun.bkg.evaluate_at_offset(offset=offset) fig, axes = plt.subplots(1, 2, figsize=(12, 6) ) edisp.plot_matrix(ax=axes[0]) aeff.plot(ax=axes[1]) plt.show() aeff.lo_threshold = lo_threshold aeff.hi_threshold = hi_threshold sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) sim.obs.peek() print sim.obs plt.show() amplitude = 2.5 * 1e-10 * u.Unit('cm-2 s-1 TeV-1') sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) sim.obs.peek() print sim.obs plt.show() amplitude model = PowerLaw(index=index, amplitude=amplitude, reference=reference) sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime) sim.simulate_obs(seed=42, obs_id=0) sim.obs.peek()
aeff.hi_threshold = hi_threshold #inbuiltbkg=fakerun.bkg.evaluate_at_offset(offset=offset) #fig, axes = plt.subplots(1, 2, figsize=(12, 6)) #edisp.plot_matrix(ax=axes[0]) #aeff.plot(ax=axes[1]) #plt.show() #aeff.lo_threshold = lo_threshold #aeff.hi_threshold = hi_threshold n_obs=1 seeds = np.arange(n_obs) sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=model, livetime=livetime_src) sim.run(seeds) n_on=sim.result[0].total_stats.n_on # now fitting different backgrounds. EXCLUSION_FILE = '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits' allsky_mask = SkyImage.read(EXCLUSION_FILE) exclusion_mask = allsky_mask.cutout( position=src, size=Angle('6 deg'), ) on_region=CircleSkyRegion(src,0.11 * u.deg) background_estimator = ReflectedRegionsBackgroundEstimator(obs_list=mylist, on_region=on_region, exclusion_mask = exclusion_mask)