.. SPDX-FileCopyrightText: 1992-2026 NWO-I/SRON Space Research Organisation Netherlands
..
.. SPDX-License-Identifier: CC-BY-4.0

.. _sec:twosame:

.. highlight:: none

Fitting multiple spectra simultaneously with the same model
===========================================================

Goal
----

If one has spectra from the same source, taken with multiple instruments,
that need to be fit simultaneously. For example, you have spectra
from MOS1, MOS2, and PN aboard XMM-Newton. How to deal with potential
small cross-calibration uncertainties between instruments?

Converting into SPEX format
---------------------------

In this case, converting to SPEX format can be done for each spectrum
separately. One can use trafo, ogiptospex, or the ``data ogip`` commands
in SPEX itself to convert OGIP spectra to a res and spo file.

Loading data into SPEX
----------------------

Once you have converted each spectrum into a res and spo file, you can
load them one by one into SPEX. Suppose we have an observation done
by XMM-Newton and we have 6 files (MOS1.spo, MOS1.res, MOS2.spo, MOS2.res,
PN.spo, PN.res). We can load them into SPEX like this::

    SPEX4> data MOS1 MOS1
    SPEX4> data MOS2 MOS2
    SPEX4> data PN PN

This will create 3 'instruments' in SPEX containing the spectra from the
XMM detectors. Each instrument will contain one region containing the
spectra. This is important when we will ignore or bin the data.

Ignoring and binning data
-------------------------

Now that we have multiple datasets loaded, we need to provide a bit more
information to the ignore and bin commands to operate on the right spectrum.

Let us ignore the lower and higher part of the spectrum for the instruments
and treat PN a bit differently, since its effective area and calibration
are slightly different::

    SPEX4> ignore ins 1 reg 1 0.0:0.5 unit kev
    SPEX4> ignore ins 1 reg 1 10.0:100.0 unit kev

The commands above ignore everything below 0.5 keV and above 10.0 keV for MOS1.
To do the same for MOS2, we need to select instrument 2::

    SPEX4> ignore ins 2 reg 1 0.0:0.5 unit kev
    SPEX4> ignore ins 2 reg 1 10.0:100. unit kev

Suppose that we want to ignore a slightly different interval for the PN instrument::

    SPEX4> ignore ins 3 reg 1 0.0:0.8 unit kev
    SPEX4> ignore ins 3 reg 1 12.0:100. unit kev

The ranges above are an example. Please adapt the ranges properly for your science case.

The same syntax holds for the bin command (the region specification can even be skipped)::

    SPEX4> bin ins 1 0.5:10.0 15 unit keV
    SPEX4> bin ins 2 0.5:10.0 15 unit keV
    SPEX4> bin ins 3 0.8:12.0 5 unit keV

Fitting the spectra
-------------------

Defining a model and fitting the spectra can be done as usual and do not require
a specific sector setup. This is because we are only fitting the same model to
three different instruments. If one wants to fit multiple different models, then
one needs to look at :ref:`sec:twospec`.

Leaving instrument normalisation free
'''''''''''''''''''''''''''''''''''''

SPEX has a built-in function to leave the instrument normalisations free
to correct for instrument calibration differences. This is only a
multiplication factor that can be freed. This can be done with the ``par``
command.

The instrument normalisation can be viewed in the instrument section of
the ``par show`` output. To instruct ``par`` to change an instrument parameter,
one gives the first number in the command a minus::

    SPEX4> par -2 1 norm s t
    SPEX4> par -3 1 norm s t

This frees the instrument normalisations for MOS2 and PN. By giving the first
number a minus sign, SPEX will interpret it like an instrument number. The
second number will be interpreted as the region number (which is 1 in this case).

The instrument normalisation is now free in the fit.

These are the major things to consider when fitting multiple spectra
with the same model. If you need something more complicated with multiple models,
then look at :ref:`sec:twospec`.
