<jupyter_start><jupyter_text>Spectral analysis  Prerequisites - Understanding how spectral extraction is performed in Cherenkov astronomy, in particular regarding OFF background measurements. - Understanding the basics data reduction and modeling/fitting process with the gammapy library API as shown in the [first gammapy analysis with the gammapy library API tutorial](../../starting/analysis_2.ipynb) ContextWhile 3D analyses allow in principle to consider complex field of views containing overlapping gamma-ray sources, in many cases we might have an observation with a single, strong, point-like source in the field of view. A spectral analysis, in that case, might consider all the events inside a source (or ON) region and bin them in energy only, obtaining 1D datasets. In classical Cherenkov astronomy, the background estimation technique associated with this method measures the number of events in OFF regions taken in regions of the field-of-view devoid of gamma-ray emitters, where the background rate is assumed to be equal to the one in the ON region. This allows to use a specific fit statistics for ON-OFF measurements, the wstat (see `~gammapy.stats.fit_statistics`), where no background model is assumed. Background is treated as a set of nuisance parameters. This removes some systematic effects connectedto the choice or the quality of the background model. But this comes at the expense of larger statistical uncertainties on the fitted model parameters.**Objective: perform a full region based spectral analysis of 4 Crab observations of  H.E.S.S. data release 1 and fit the resulting datasets.** IntroductionHere, as usual, we use the `~gammapy.data.DataStore` to retrieve a list of selected observations (`~gammapy.data.Observations`). Then, we define the ON region containing the source and the geometry of the `~gammapy.datasets.SpectrumDataset` object we want to produce. We then create the corresponding dataset Maker. We have to define the Maker object that will extract the OFF counts from reflected regions in the field-of-view. To ensure we use data in an energy range where the quality of the IRFs is good enough we also create a safe range Maker.We can then proceed with data reduction with a loop over all selected observations to produce datasets in the relevant geometry.We can then explore the resulting datasets and look at the cumulative signal and significance of our source. We finally proceed with model fitting. In practice, we have to:- Create a `~gammapy.data.DataStore` poiting to the relevant data - Apply an observation selection to produce a list of observations, a `~gammapy.data.Observations` object.- Define a geometry of the spectrum we want to produce:    - Create a `~regions.CircleSkyRegion` for the ON extraction region    - Create a `~gammapy.maps.MapAxis` for the energy binnings: one for the reconstructed (i.e. measured) energy, the other for the true energy (i.e. the one used by IRFs and models)- Create the necessary makers :     - the spectrum dataset maker : `~gammapy.makers.SpectrumDatasetMaker`    - the OFF background maker, here a `~gammapy.makers.ReflectedRegionsBackgroundMaker`    - and the safe range maker : `~gammapy.makers.SafeRangeMaker`- Perform the data reduction loop. And for every observation:    - Apply the makers sequentially to produce a `~gammapy.datasets.SpectrumDatasetOnOff`    - Append it to list of datasets- Define the `~gammapy.modeling.models.SkyModel` to apply to the dataset.- Create a `~gammapy.modeling.Fit` object and run it to fit the model parameters- Apply a `~gammapy.estimators.FluxPointsEstimator` to compute flux points for the spectral part of the fit.  SetupAs usual, we'll start with some setup ...<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
# Check package versions
import gammapy
import numpy as np
import astropy
import regions

print("gammapy:", gammapy.__version__)
print("numpy:", np.__version__)
print("astropy", astropy.__version__)
print("regions", regions.__version__)
from pathlib import Path
import astropy.units as u
from astropy.coordinates import SkyCoord, Angle
from regions import CircleSkyRegion
from gammapy.maps import MapAxis, RegionGeom, WcsGeom
from gammapy.modeling import Fit
from gammapy.data import DataStore
from gammapy.datasets import (
    Datasets,
    SpectrumDataset,
    SpectrumDatasetOnOff,
    FluxPointsDataset,
)
from gammapy.modeling.models import (
    ExpCutoffPowerLawSpectralModel,
    create_crab_spectral_model,
    SkyModel,
)
from gammapy.makers import (
    SafeMaskMaker,
    SpectrumDatasetMaker,
    ReflectedRegionsBackgroundMaker,
)
from gammapy.estimators import FluxPointsEstimator
from gammapy.visualization import plot_spectrum_datasets_off_regions<jupyter_output><empty_output><jupyter_text>Load DataFirst, we select and load some H.E.S.S. observations of the Crab nebula (simulated events for now).We will access the events, effective area, energy dispersion, livetime and PSF for containement correction.<jupyter_code>datastore = DataStore.from_dir("$GAMMAPY_DATA/hess-dl3-dr1/")
obs_ids = [23523, 23526, 23559, 23592]
observations = datastore.get_observations(obs_ids)<jupyter_output><empty_output><jupyter_text>Define Target RegionThe next step is to define a signal extraction region, also known as on region. In the simplest case this is just a [CircleSkyRegion](http://astropy-regions.readthedocs.io/en/latest/api/regions.CircleSkyRegion.html).<jupyter_code>target_position = SkyCoord(ra=83.63, dec=22.01, unit="deg", frame="icrs")
on_region_radius = Angle("0.11 deg")
on_region = CircleSkyRegion(center=target_position, radius=on_region_radius)<jupyter_output><empty_output><jupyter_text>Create exclusion maskWe will use the reflected regions method to place off regions to estimate the background level in the on region.To make sure the off regions don't contain gamma-ray emission, we create an exclusion mask.Using http://gamma-sky.net/ we find that there's only one known gamma-ray source near the Crab nebula: the AGN called [RGB J0521+212](http://gamma-sky.net//cat/tev/23) at GLON = 183.604 deg and GLAT = -8.708 deg.<jupyter_code>exclusion_region = CircleSkyRegion(
    center=SkyCoord(183.604, -8.708, unit="deg", frame="galactic"),
    radius=0.5 * u.deg,
)

skydir = target_position.galactic
geom = WcsGeom.create(
    npix=(150, 150), binsz=0.05, skydir=skydir, proj="TAN", frame="icrs"
)

exclusion_mask = ~geom.region_mask([exclusion_region])
exclusion_mask.plot();<jupyter_output><empty_output><jupyter_text>Run data reduction chainWe begin with the configuration of the maker classes:<jupyter_code>energy_axis = MapAxis.from_energy_bounds(
    0.1, 40, nbin=15, per_decade=True, unit="TeV", name="energy"
)
energy_axis_true = MapAxis.from_energy_bounds(
    0.05, 100, nbin=20, per_decade=True, unit="TeV", name="energy_true"
)

geom = RegionGeom.create(region=on_region, axes=[energy_axis])
dataset_empty = SpectrumDataset.create(
    geom=geom, energy_axis_true=energy_axis_true
)
dataset_maker = SpectrumDatasetMaker(
    containment_correction=True, selection=["counts", "exposure", "edisp"]
)
bkg_maker = ReflectedRegionsBackgroundMaker(exclusion_mask=exclusion_mask)
safe_mask_masker = SafeMaskMaker(methods=["aeff-max"], aeff_percent=10)
%%time
datasets = Datasets()

for obs_id, observation in zip(obs_ids, observations):
    dataset = dataset_maker.run(
        dataset_empty.copy(name=str(obs_id)), observation
    )
    dataset_on_off = bkg_maker.run(dataset, observation)
    dataset_on_off = safe_mask_masker.run(dataset_on_off, observation)
    datasets.append(dataset_on_off)<jupyter_output><empty_output><jupyter_text>Plot off regions<jupyter_code>plt.figure(figsize=(8, 8))
_, ax, _ = exclusion_mask.plot()
on_region.to_pixel(ax.wcs).plot(ax=ax, edgecolor="k")
plot_spectrum_datasets_off_regions(ax=ax, datasets=datasets)<jupyter_output><empty_output><jupyter_text>Source statisticNext we're going to look at the overall source statistics in our signal region.<jupyter_code>info_table = datasets.info_table(cumulative=True)
info_table
plt.plot(
    info_table["livetime"].to("h"), info_table["excess"], marker="o", ls="none"
)
plt.xlabel("Livetime [h]")
plt.ylabel("Excess");
plt.plot(
    info_table["livetime"].to("h"),
    info_table["sqrt_ts"],
    marker="o",
    ls="none",
)
plt.xlabel("Livetime [h]")
plt.ylabel("Sqrt(TS)");<jupyter_output><empty_output><jupyter_text>Finally you can write the extrated datasets to disk using the OGIP format (PHA, ARF, RMF, BKG, see [here](https://gamma-astro-data-formats.readthedocs.io/en/latest/spectra/ogip/index.html) for details):<jupyter_code>path = Path("spectrum_analysis")
path.mkdir(exist_ok=True)
for dataset in datasets:
    dataset.write(
        filename=path / f"obs_{dataset.name}.fits.gz", overwrite=True
    )<jupyter_output><empty_output><jupyter_text>If you want to read back the datasets from disk you can use:<jupyter_code>datasets = Datasets()

for obs_id in obs_ids:
    filename = path / f"obs_{obs_id}.fits.gz"
    datasets.append(SpectrumDatasetOnOff.read(filename))<jupyter_output><empty_output><jupyter_text>Fit spectrumNow we'll fit a global model to the spectrum. First we do a joint likelihood fit to all observations. If you want to stack the observations see below. We will also produce a debug plot in order to show how the global fit matches one of the individual observations.<jupyter_code>spectral_model = ExpCutoffPowerLawSpectralModel(
    amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"),
    index=2,
    lambda_=0.1 * u.Unit("TeV-1"),
    reference=1 * u.TeV,
)
model = SkyModel(spectral_model=spectral_model, name="crab")

datasets.models = [model]

fit_joint = Fit(datasets)
result_joint = fit_joint.run()

# we make a copy here to compare it later
model_best_joint = model.copy()<jupyter_output><empty_output><jupyter_text>Fit quality and model residuals We can access the results dictionary to see if the fit converged:<jupyter_code>print(result_joint)<jupyter_output><empty_output><jupyter_text>and check the best-fit parameters<jupyter_code>result_joint.parameters.to_table()<jupyter_output><empty_output><jupyter_text>A simple way to inspect the model residuals is using the function `~SpectrumDataset.plot_fit()`<jupyter_code>ax_spectrum, ax_residuals = datasets[0].plot_fit()
ax_spectrum.set_ylim(0.1, 40)<jupyter_output><empty_output><jupyter_text>For more ways of assessing fit quality, please refer to the dedicated [modeling and fitting tutorial](../2D/modeling_2D.ipynb).  Compute Flux PointsTo round up our analysis we can compute flux points by fitting the norm of the global model in energy bands. We'll use a fixed energy binning for now:<jupyter_code>e_min, e_max = 0.7, 30
energy_edges = np.geomspace(e_min, e_max, 11) * u.TeV<jupyter_output><empty_output><jupyter_text>Now we create an instance of the `~gammapy.estimators.FluxPointsEstimator`, by passing the dataset and the energy binning:<jupyter_code>fpe = FluxPointsEstimator(energy_edges=energy_edges, source="crab", selection_optional="all")
flux_points = fpe.run(datasets=datasets)<jupyter_output><empty_output><jupyter_text>Here is a the table of the resulting flux points:<jupyter_code>flux_points.table_formatted<jupyter_output><empty_output><jupyter_text>Now we plot the flux points and their likelihood profiles. For the plotting of upper limits we choose a threshold of TS < 4.<jupyter_code>plt.figure(figsize=(8, 5))
flux_points.table["is_ul"] = flux_points.ts < 4

ax = flux_points.plot(sed_type="e2dnde", color="darkorange")
flux_points.plot_ts_profiles(ax=ax, sed_type="e2dnde");<jupyter_output><empty_output><jupyter_text>The final plot with the best fit model, flux points and residuals can be quickly made like this:<jupyter_code>flux_points_dataset = FluxPointsDataset(
    data=flux_points, models=model_best_joint
)
flux_points_dataset.plot_fit();<jupyter_output><empty_output><jupyter_text>Stack observationsAnd alternative approach to fitting the spectrum is stacking all observations first and the fitting a model. For this we first stack the individual datasets:<jupyter_code>dataset_stacked = Datasets(datasets).stack_reduce()<jupyter_output><empty_output><jupyter_text>Again we set the model on the dataset we would like to fit (in this case it's only a single one) and pass it to the `~gammapy.modeling.Fit` object:<jupyter_code>dataset_stacked.models = model
stacked_fit = Fit([dataset_stacked])
result_stacked = stacked_fit.run()

# make a copy to compare later
model_best_stacked = model.copy()
print(result_stacked)
model_best_joint.parameters.to_table()
model_best_stacked.parameters.to_table()<jupyter_output><empty_output><jupyter_text>Finally, we compare the results of our stacked analysis to a previously published Crab Nebula Spectrum for reference. This is available in `~gammapy.modeling.models.create_crab_spectral_model`.<jupyter_code>plot_kwargs = {
    "energy_range": [0.1, 30] * u.TeV,
    "energy_power": 2,
    "flux_unit": "erg-1 cm-2 s-1",
}

# plot stacked model
model_best_stacked.spectral_model.plot(
    **plot_kwargs, label="Stacked analysis result"
)
model_best_stacked.spectral_model.plot_error(facecolor="blue", alpha=0.3, **plot_kwargs)

# plot joint model
model_best_joint.spectral_model.plot(
    **plot_kwargs, label="Joint analysis result", ls="--"
)
model_best_joint.spectral_model.plot_error(facecolor="orange", alpha=0.3, **plot_kwargs)

create_crab_spectral_model("hess_ecpl").plot(
    **plot_kwargs, label="Crab reference"
)
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09062684801892372, 'fraction_numerical': 0.01020106445890006, 'mean_word_length': 6.042686100989068, 'pattern_counts': {'":': 3, '<': 87, '<?xml version=': 0, '>': 85, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '44072', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4371, 'n_tokens_neox': 4173, 'n_words': 1295, 'path': 'docs/tutorials/analysis/1D/spectral_analysis.ipynb', 'repo_name': 'luca-giunti/gammapy', 'size': '13528'}