Chandra X-Ray Observatory
	(CXC)
Skip to the navigation links
Last modified: 15 Nov 2016

URL: http://cxc.harvard.edu/sherpa/threads/grating/

Fitting Grating Data

Sherpa Threads (CIAO 4.9 Sherpa v1)


Overview

Synopsis:

This thread provides a general introduction to fitting grating data in Sherpa. Loading and filtering data are covered, as well as defining instrument responses and source models.

Users working with HRC-S/LETG grating data will also find the Fitting Multiple Orders of HRC-S/LETG Data thread helpful for their analysis.

Last Update: 15 Nov 2016 - reviewed for CIAO 4.9, no content changes, updated fit results.


Contents


Getting Started

Download the sample data: 459 (HETG/ACIS-S, 3C 273)

The files used in this example were created by following several of the CIAO Grating threads:

Here is a list of all the necessary files:

spectra:
459_heg_m1_bin10.pha
459_heg_p1_bin10.pha
459_meg_m1_bin10.pha
459_meg_p1_bin10.pha

gARFs:
459_heg_m1.garf
459_heg_p1.garf
459_meg_m1.garf
459_meg_p1.garf

The spectrum that will be used in this session has been binned by a factor of 10.

Users may also choose to run the ACIS Grating RMFs thread. Creating observation-specific gRMFs is optional, and is discussed further in the Loading the Instrument Responses section.

The data files are available in sherpa.tar.gz, as explained in the Sherpa Getting Started thread.


Reading the Spectrum Files

The source data are input to Sherpa with the load_pha command, where any associated background or response files will automatically be read in if the corresponding filenames are recorded in the header of the source data files:

sherpa> load_pha(1, "459_heg_m1_bin10.pha")
WARNING: systematic errors were not found in file '459_heg_m1_bin10.pha'
statistical errors were found in file '459_heg_m1_bin10.pha' 
but not used; to use them, re-read with use_errors=True
read background_up into a dataset from file 459_heg_m1_bin10.pha
read background_down into a dataset from file 459_heg_m1_bin10.pha

sherpa> load_pha(2, "459_heg_p1_bin10.pha")
WARNING: systematic errors were not found in file '459_heg_p1_bin10.pha'
statistical errors were found in file '459_heg_p1_bin10.pha' 
but not used; to use them, re-read with use_errors=True
read background_up into a dataset from file 459_heg_p1_bin10.pha
read background_down into a dataset from file 459_heg_p1_bin10.pha

sherpa> load_pha(3, "459_meg_m1_bin10.pha")
WARNING: systematic errors were not found in file '459_meg_m1_bin10.pha'
statistical errors were found in file '459_meg_m1_bin10.pha' 
but not used; to use them, re-read with use_errors=True
read background_up into a dataset from file 459_meg_m1_bin10.pha
read background_down into a dataset from file 459_meg_m1_bin10.pha

sherpa> load_pha(4, "459_meg_p1_bin10.pha")
WARNING: systematic errors were not found in file '459_meg_p1_bin10.pha'
statistical errors were found in file '459_meg_p1_bin10.pha' 
but not used; to use them, re-read with use_errors=True
read background_up into a dataset from file 459_meg_p1_bin10.pha
read background_down into a dataset from file 459_meg_p1_bin10.pha

Sherpa now refers to the spectra as follows:

  • HEG, -1 order = dataset 1
  • HEG, +1 order = dataset 2
  • MEG, -1 order = dataset 3
  • MEG, +1 order = dataset 4

Note that there are two background data sets associated with each Chandra source grating data set, 'background up' and 'background down', which Sherpa assigns to background IDs 1 and 2. These contain data extracted from background regions adjacent to the source region. The two background data sets are added and scaled to the source data when being subtracted with the Sherpa subtract command, or modeled with set_bkg_model.

[TIP]
Using Only One of the Associated Background

In order to work with only one of the two background data sets associated with a source, the get_data command may be used to remove the unneeded background, as shown below:

sherpa> print(get_data(1).background_ids)
[1,2]

sherpa> get_data(1).background_ids = [1]  # set background 1 as the only background to fit.

Loading the Instrument Responses

The instrument response is established when the appropriate response files (ARF, RMF) are input to the Sherpa session. If the names of the ARF and RMF response files are recorded in the header of the PHA file, Sherpa will load them automatically when the PHA file is read with load_pha; if not, they need to be loaded manually with the load_arf and load_rmf commands.

Since we are working with grating data in this example, we load only the ARF files corresponding to each of the four orders and the associated backgrounds:

sherpa> load_arf(1, "459_heg_m1.arf")
sherpa> load_arf(2, "459_heg_p1.arf")
sherpa> load_arf(3, "459_meg_m1.arf")
sherpa> load_arf(4, "459_meg_p1.arf")

sherpa> load_arf(1, "459_heg_m1.arf", bkg_id=1)
sherpa> load_arf(1, "459_heg_m1.arf", bkg_id=2)

sherpa> load_arf(2, "459_heg_p1.arf", bkg_id=1)
sherpa> load_arf(2, "459_heg_p1.arf", bkg_id=2)

sherpa> load_arf(3, "459_meg_m1.arf", bkg_id=1)
sherpa> load_arf(3, "459_meg_m1.arf", bkg_id=2)

sherpa> load_arf(4, "459_meg_p1.arf", bkg_id=1)
sherpa> load_arf(4, "459_meg_p1.arf", bkg_id=2)

The current definition of the instrument response, along with information about the loaded data sets, may be examined using the commands show_data and show_bkg:

sherpa> show_data()
Data Set: 1
Filter: 0.5775-12.3676 Energy (keV)
Bkg Scale: 0.248829
Noticed Channels: 1-8192
name           = 459_heg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 1.0
areascal       = 1.0
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = [1, 2]

ARF Data Set: 1:1
name     = 459_heg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2847986

Background Data Set: 1:1
Filter: 0.5775-12.3676 Energy (keV)
Noticed Channels: 1-8192
name           = 459_heg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 1:1
name     = 459_heg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2847986

Background Data Set: 1:2
Filter: 0.5775-12.3676 Energy (keV)
Noticed Channels: 1-8192
name           = 459_heg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 1:2
name     = 459_heg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2847986

Data Set: 2
Filter: 0.5775-12.3676 Energy (keV)
Bkg Scale: 0.248829
Noticed Channels: 1-8192
name           = 459_heg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 1.0
areascal       = 1.0
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = [1, 2]

ARF Data Set: 2:1
name     = 459_heg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0133422

Background Data Set: 2:1
Filter: 0.5775-12.3676 Energy (keV)
Noticed Channels: 1-8192
name           = 459_heg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 2:1
name     = 459_heg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0133422

Background Data Set: 2:2
Filter: 0.5775-12.3676 Energy (keV)
Noticed Channels: 1-8192
name           = 459_heg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 2:2
name     = 459_heg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0133422

Data Set: 3
Filter: 0.2957-12.3370 Energy (keV)
Bkg Scale: 0.248829
Noticed Channels: 1-8192
name           = 459_meg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 1.0
areascal       = 1.0
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = [1, 2]

ARF Data Set: 3:1
name     = 459_meg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2855739

Background Data Set: 3:1
Filter: 0.2957-12.3370 Energy (keV)
Noticed Channels: 1-8192
name           = 459_meg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 3:1
name     = 459_meg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2855739

Background Data Set: 3:2
Filter: 0.2957-12.3370 Energy (keV)
Noticed Channels: 1-8192
name           = 459_meg_m1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 3:2
name     = 459_meg_m1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38565.2855739

Data Set: 4
Filter: 0.2957-12.3370 Energy (keV)
Bkg Scale: 0.248829
Noticed Channels: 1-8192
name           = 459_meg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 1.0
areascal       = 1.0
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = [1, 2]

ARF Data Set: 4:1
name     = 459_meg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0141169

Background Data Set: 4:1
Filter: 0.2957-12.3370 Energy (keV)
Noticed Channels: 1-8192
name           = 459_meg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 4:1
name     = 459_meg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0141169

Background Data Set: 4:2
Filter: 0.2957-12.3370 Energy (keV)
Noticed Channels: 1-8192
name           = 459_meg_p1_bin10.pha
channel        = Float64[8192]
counts         = Float64[8192]
staterror      = None
syserror       = None
bin_lo         = Float64[8192]
bin_hi         = Float64[8192]
grouping       = Int16[8192]
quality        = Int16[8192]
exposure       = 38564.6089269
backscal       = 4.0188284
areascal       = None
grouped        = True
subtracted     = False
units          = energy
rate           = True
plot_fac       = 0
response_ids   = [1]
background_ids = []

Background ARF Data Set: 4:2
name     = 459_meg_p1.arf
energ_lo = Float64[8192]
energ_hi = Float64[8192]
specresp = Float64[8192]
bin_lo   = Float64[8192]
bin_hi   = Float64[8192]
exposure = 38563.0141169

Before plotting the data with the plot command, ensure that the units field of each data set is set to "wavelength" with the set_analysis command, as in the following example:

sherpa> show_filter() 

Data Set Filter: 1
0.5775-12.3676 Energy (keV)

Data Set Filter: 2
0.5775-12.3676 Energy (keV)

Data Set Filter: 3
0.2957-12.3370 Energy (keV)

Data Set Filter: 4
0.2957-12.3370 Energy (keV)


sherpa> set_analysis("wave")
sherpa> show_filter()    

Data Set Filter: 1
1.0025-21.4675 Wavelength (Angstrom)

Data Set Filter: 2
1.0025-21.4675 Wavelength (Angstrom)

Data Set Filter: 3
1.0050-41.9350 Wavelength (Angstrom)

Data Set Filter: 4
1.0050-41.9350 Wavelength (Angstrom)

The data may now be plotted:

sherpa> plot("data", 1, "data", 2, "data", 3, "data", 4)

Figure 1 shows the resulting plot.

[Plot of ACIS HEG and MEG +/- 1 orders for 3C 273]
[Print media version: Plot of ACIS HEG and MEG +/- 1 orders for 3C 273]

Figure 1: Plotting the four orders

Plot of ACIS HEG and MEG +/- 1 orders for 3C 273


Filtering the Data

We choose to filter the data to focus on an area of interest:

sherpa> ignore()
sherpa> notice(1., 15.)

The ignore command is used to ignore all the data in every data set, then notice is used to select the desired data range in all data sets. You may wish to adjust the limits to exclude more or less of your data. To apply ignore() or notice() data constraints to a specific data set, use the ignore_id and notice_id commands, respectively.

Each filtered data set may then be plotted:

sherpa> plot("data", 1, "data", 2, "data", 3, "data", 4)

Notice that the plot now includes only the data in the specified wavelength range. Figure 2 shows the resulting plot.

[Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms]
[Print media version: Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms]

Figure 2: Filtering the data sets

Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms.


Defining the Source and Background Models

We plan on simultaneously fitting the background data with the source data (rather than subtracting it), so we need to create a model expression for both the source and background. When modeling (or subtracting) the grating background, Sherpa adds the two background up/down counts arrays and scales the result to the associated source data.

We model this source with a broken power-law (bpl1d) absorbed by the interstellar medium (ISM) (atten). The background will be modeled by a one-dimensional power-law (powlaw1d), also absorbed by the ISM (the same atten model).

First, we set up the model components with create_model_component, and set some initial parameter values. The absorption model is referred to as "abs1", the broken power-law is "bpow1", and the 1-D power-law is "pow1d":

sherpa> create_model_component("atten", "abs1")
           <BPL1D model instance 'bpl1d.bpow1'>
sherpa> abs1.integrate = "false"
sherpa> abs1.hcol = 1e+20
sherpa> abs1.heiRatio = 0.1 
sherpa> abs1.heiiRatio = 0.01

sherpa> create_model_component("bpl1d", "bpow1")
           <BPL1D model instance 'bpl1d.bpow1'>
sherpa> bpow1.gamma1 = 0 
sherpa> bpow1.gamma2 = 0 
sherpa> bpow1.eb = 7.99625 
sherpa> freeze(bpow1.ref)
sherpa> bpow1.ampl = 0.001

sherpa> create_model_component("powlaw1d","pow1d")
           <PowLaw1D model instance 'powlaw1d.pow1d'>
sherpa> pow1d.gamma = 1 
sherpa> freeze(pow1d.ref)
sherpa> pow1d.ampl = 1e-5

We freeze the normalization parameters for bpow1 and pow1d (bpow1.ref and pow1d.ref) without changing the default values. For the bpow1 and pow1d parameters for which we did set initial values, we could have used the Sherpa guess() function to estimate reasonable starting values, based on the data input to the Sherpa session. To have Sherpa automatically query for initial parameter values when a model is established, set 'paramprompt(True)' (it is 'False' by default).

The model parameter values can be listed with print() command (note that show_model/show_source is appropriate once the full model expression has been assigned to the data with set_source:

sherpa> print(abs1)
atten.abs1
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   abs1.hcol     thawed        1e+20        1e+17        1e+24           
   abs1.heiRatio thawed          0.1            0            1           
   abs1.heiiRatio thawed         0.01            0            1           

sherpa> print(bpow1)
bpl1d.bpow1
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   bpow1.gamma1  thawed            0          -10           10           
   bpow1.gamma2  thawed            0          -10           10           
   bpow1.eb      thawed      7.99625            0        1e+03           
   bpow1.ref     frozen            1 -3.40282e+38  3.40282e+38           
   bpow1.ampl    thawed        0.001        1e-20  3.40282e+38           

sherpa> print(pow1d)
powlaw1d.pow1d
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   pow1d.gamma  thawed            1          -10           10           
   pow1d.ref    frozen            1 -3.40282e+38  3.40282e+38           
   pow1d.ampl   thawed        1e-05            0  3.40282e+38                

Next we modify the initial parameter value for abs1.hcol:

sherpa> abs1.hcol = 1.81e20 
sherpa> freeze(abs1)

The hydrogen column density (hcol) is set to the Galactic value. All the abs1 parameters are then frozen, which means they will not be allowed to vary during the fit.

Now that the model components have been established, the product of abs1 and bpow1 is assigned as the source model for all data sets :

sherpa> set_source(1, abs1*bpow1)
sherpa> set_source(2, abs1*bpow1)
sherpa> set_source(3, abs1*bpow1)
sherpa> set_source(4, abs1*bpow1)

while the background model is set as the product of abs1 and pow1d:

sherpa> set_bkg_model(1, abs1*pow1d, 1)
sherpa> set_bkg_model(1, abs1*pow1d, 2)
sherpa> set_bkg_model(2, abs1*pow1d, 1)
sherpa> set_bkg_model(2, abs1*pow1d, 2)
sherpa> set_bkg_model(3, abs1*pow1d, 1)
sherpa> set_bkg_model(3, abs1*pow1d, 2)
sherpa> set_bkg_model(4, abs1*pow1d, 1)
sherpa> set_bkg_model(4, abs1*pow1d, 2)

The source and background model definitions can be listed with show_model and show_bkg_model:

sherpa> show_model()
Model: 1
apply_arf((38564.6089269 * ((atten.abs1 * bpl1d.bpow1) + scale_factor * ((atten.abs1 * powlaw1d.pow1d) + (atten.abs1 * powlaw1d.pow1d)))))
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   abs1.hcol    frozen     1.81e+20        1e+17        1e+24           
   abs1.heiRatio frozen          0.1            0            1           
   abs1.heiiRatio frozen         0.01            0            1           
   bpow1.gamma1 thawed            0          -10           10           
   bpow1.gamma2 thawed            0          -10           10           
   bpow1.eb     thawed      7.99625            0         1000           
   bpow1.ref    frozen            1 -3.40282e+38  3.40282e+38           
   bpow1.ampl   thawed        0.001        1e-20  3.40282e+38           
   pow1d.gamma  thawed            1          -10           10           
   pow1d.ref    frozen            1 -3.40282e+38  3.40282e+38           
   pow1d.ampl   thawed        1e-05            0  3.40282e+38           

Model: 2
{same as above}

Model: 3
{same as above}

Model: 4
{same as above}

sherpa> show_bkg_model()
Background Model: 1:1
apply_arf((38564.6089269 * (atten.abs1 * powlaw1d.pow1d)))
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   abs1.hcol    frozen     1.81e+20        1e+17        1e+24           
   abs1.heiRatio frozen          0.1            0            1           
   abs1.heiiRatio frozen         0.01            0            1           
   pow1d.gamma  thawed            1          -10           10           
   pow1d.ref    frozen            1 -3.40282e+38  3.40282e+38           
   pow1d.ampl   thawed        1e-05            0  3.40282e+38          

Background Model: 1:2
{same as above}

Background Model: 2:2
{same as above}

Background Model: 3:2
{same as above}

Background Model: 4:2
{same as above}

Examining Method & Statistic Settings

Next we check the current method and statistics settings:

sherpa> show_method()
Optimization Method: LevMar
name    = levmar
ftol    = 1.19209289551e-07
xtol    = 1.19209289551e-07
gtol    = 1.19209289551e-07
maxfev  = None
epsfcn  = 1.19209289551e-07
factor  = 100.0
verbose = 0

sherpa> show_stat()
Statistic: Chi2Gehrels
Chi Squared with Gehrels variance.

    The variance is estimated from the number of counts in each bin,
    but unlike `Chi2DataVar`, the Gaussian approximation is not
    used. This makes it more-suitable for use with low-count data.

    The standard deviation for each bin is calculated using the
    approximation from [1]_:

    sigma(i,S) = 1 + sqrt(N(i,s) + 0.75)

    where the higher-order terms have been dropped. This is accurate
    to approximately one percent. For data where the background has
    not been subtracted then the error term is:

    sigma(i) = sigma(i,S)

    whereas with background subtraction,

    sigma(i)^2 = sigma(i,S)^2 + [A(S)/A(B)]^2 sigma(i,B)^2

    Notes
    -----
    The accuracy of the error term when the background has been
    subtracted has not been determined. A preferable approach to
    background subtraction is to model the background as well as the
    source signal.

    References
    ----------

    .. [1] "Confidence limits for small numbers of events in
           astrophysical data", Gehrels, N. 1986, ApJ, vol 303,
           p. 336-346.
           http://adsabs.harvard.edu/abs/1986ApJ...303..336G

The Sherpa default fitting statistic and optimization method are χ2-Gehrels and Levenberg-Marquardt, respectively. For this fit, we will use the Nelder-Mead Simplex method and the CStat statistic; this is because χ2-Gehrels could bias the fit results and yield an artificially low reduced statistic for this data, and the CStat (and Cash) statistic is appropriate for simultaneously fitting source and background data. For a list of all the available methods and statistic settings, see the Sherpa Statistics and Optimization Methods pages.

To change the current method and statistic, we use set_method and set_stat.

sherpa> set_method("neldermead")
sherpa> set_stat("cstat")

Fitting

The data sets are now fit:

sherpa> fit()   
Datasets              = 1, 2, 3, 4
Method                = neldermead
Statistic             = cstat
Initial fit statistic = 100816
Final fit statistic   = 5976.06 at function evaluation 1548
Data points           = 5052
Degrees of freedom    = 5046
Probability [Q-value] = 1.06549e-18
Reduced statistic     = 1.18432
Change in statistic   = 94840
   bpow1.gamma1   0.424705    
   bpow1.gamma2   0.0496687   
   bpow1.eb       10.6201     
   bpow1.ampl     0.00548922  
   pow1d.gamma    0.33154     
   pow1d.ampl     8.87688e-05 

To plot the fits:

sherpa> plot("fit", 1, "fit", 2, "fit", 3, "fit", 4)
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat

sherpa> current_plot("all")
sherpa> set_plot_title("3C 273 (ObsID 459)")

sherpa> current_plot("plot1")
sherpa> add_label(10, 0.1, "HEG -1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot2")
sherpa> add_label(10, 0.1, "HEG +1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot3")
sherpa> add_label(10, 0.15, "MEG -1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot4")
sherpa> add_label(10, 0.15, "MEG +1")
sherpa> set_label(["color","green"])

The ChIPS commands are used to add a title and labels to the drawing area. The plot is shown in Figure 3.

[Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273]
[Print media version: Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273]

Figure 3: Results of Simultaneous Fit

Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273.

Note that the CStat statistic does not calculate errors for the data points. Since it is useful to do so, we change the fit statistic to something suitable for calculating errors, and view the residuals of the fit with the plot_fit_delchi command:

sherpa> set_stat('chi2xspecvar') 
sherpa> plot_fit_delchi()

This plot is shown in Figure 4.

After creating a plot, it may be saved as a PostScript file; in this example, "all.ps" is returned:


Examining Fit Results

The χ2 goodness-of-fit is reported with the best-fit values after a fit and the commands get_fit_results and show_fit allow access to this information after the fit has been performed:

sherpa> show_fit()
Optimization Method: NelderMead
name         = simplex
ftol         = 1.19209289551e-07
maxfev       = None
initsimplex  = 0
finalsimplex = 9
step         = None
iquad        = 1
verbose      = 0

Statistic: Chi2XspecVar
Chi Squared with data variance (XSPEC style).

    The variance in each bin is estimated from the data value in that
    bin. See also `Chi2DataVar`, `Chi2Gehrels`, and `Chi2ModVar`.

    The calculation of the variance is the same as `Chi2DataVar`
    except that if the number of counts in a bin is less than 1
    then the variance for that bin is set to 1.

    

Fit:Datasets              = 1, 2, 3, 4
Method                = neldermead
Statistic             = cstat
Initial fit statistic = 100816
Final fit statistic   = 5976.06 at function evaluation 1548
Data points           = 5052
Degrees of freedom    = 5046
Probability [Q-value] = 1.06549e-18
Reduced statistic     = 1.18432
Change in statistic   = 94840
   bpow1.gamma1   0.424705    
   bpow1.gamma2   0.0496687   
   bpow1.eb       10.6201     
   bpow1.ampl     0.00548922  
   pow1d.gamma    0.33154     
   pow1d.ampl     8.87688e-05 

The number of bins in the fit (Data points), the number of degrees of freedom (i.e. the number of bins minus the number of free parameters), and the final fit statistic value are reported. If the chosen statistic is one of the χ2 statistics, as in this example, the reduced statistic, i.e. the statistic value divided by the number of degrees of freedom, and the probability (Q-value) are included as well.

The calc_chisqr command calculates the statistic contribution per bin; in this example, the results for data set 1 are returned:

sherpa> calc_chisqr()
   array([ 0.,  0.,  0., 0., ... , 1.17970306e-06,   9.99029138e-01,   
   8.38311915e-08, 2.42387626e-08,   3.06732525e-09,   1.39226106e-09, 
   1.18170110e-10,   4.34535088e-11,   1.37691508e-12])

The confidence (conf), covariance (covar) and projection (proj) commands can be used to estimate confidence intervals for the thawed parameters:

sherpa> set_stat("cstat")
sherpa> conf()
pow1d.ampl lower bound:	-5.50626e-06
bpow1.gamma1 -: WARNING: The confidence level lies within (4.157243e-01, 4.202145e-01)
bpow1.gamma1 lower bound:	-0.00673539
bpow1.eb lower bound:	-0.318481
pow1d.gamma lower bound:	-0.0325211
bpow1.eb upper bound:	0.281067
pow1d.gamma upper bound:	0.0322908
bpow1.ampl -: WARNING: The confidence level lies within (5.395501e-03, 5.442362e-03)
bpow1.ampl lower bound:	-7.02911e-05
pow1d.ampl upper bound:	5.84816e-06
bpow1.gamma1 upper bound:	0.00708619
bpow1.ampl upper bound:	7.32199e-05
bpow1.gamma2 lower bound:	-0.0636349
bpow1.gamma2 upper bound:	0.0683684
Datasets              = 1, 2, 3, 4
Confidence Method     = confidence
Iterative Fit Method  = None
Fitting Method        = neldermead
Statistic             = cstat
confidence 1-sigma (68.2689%) bounds:
   Param            Best-Fit  Lower Bound  Upper Bound
   -----            --------  -----------  -----------
   bpow1.gamma1     0.424705  -0.00673539   0.00708619
   bpow1.gamma2    0.0496687   -0.0636349    0.0683684
   bpow1.eb          10.6201    -0.318481     0.281067
   bpow1.ampl     0.00548922 -7.02911e-05  7.32199e-05
   pow1d.gamma       0.33154   -0.0325211    0.0322908
   pow1d.ampl    8.87688e-05 -5.50626e-06  5.84816e-06

Saving and Quitting the Session

Before exiting Sherpa, you may wish to save the session in order to return to the analysis at a later point:

The save function records all the information about the current session to the binary file 459_fitting_session.save, and the save_all function records the session settings to an editable ASCII file.

To restore the session that was saved to the binary file 459_fitting_session.save or ASCII file 459_fitting_session.ascii:

sherpa> restore("session1.save")
sherpa> exec(open("session1.ascii").read())

Finally, quit the session:

sherpa> quit

Scripting It

The file fit.py is a Python script which performs the primary commands used above; it can be executed by typing exec(open("fit.py").read()) on the Sherpa command line.

The Sherpa script command may be used to save everything typed on the command line in a Sherpa session:

sherpa> script(filename="sherpa.log", clobber=False)

(Note that restoring a Sherpa session from such a file could be problematic since it may include syntax errors, unwanted fitting trials, et cetera.)

The CXC is committed to helping Sherpa users transition to new syntax as smoothly as possible. If you have existing Sherpa scripts or save files, submit them to us via the CXC Helpdesk and we will provide the CIAO/Sherpa 4.9 syntax to you.


History

18 Jul 2008 updated for CIAO 4.1
04 Dec 2008 set_analysis(), show_data(), and show_fit() are available in Sherpa 4.1
12 Dec 2008 create_model_component is available in Sherpa 4.1
28 Apr 2009 replaced use of atten model with Sherpa user model "atten_wave"; new script command is available with CIAO 4.1.2
08 Jan 2010 updated for CIAO 4.2
13 Jul 2010 updated for CIAO 4.2 Sherpa v2: removal of S-Lang version of thread.
19 Aug 2011 updated the Reading the Spectrum Files section with information on the two background up/down data sets included in Chandra grating PHA files
20 Jan 2012 reviewed for CIAO 4.4 (no changes)
10 Dec 2013 reviewed for CIAO 4.6: no changes
25 Feb 2015 updated for CIAO 4.7, no content changes
14 Dec 2015 reviewed for CIAO 4.8, no content changes
15 Nov 2016 reviewed for CIAO 4.9, no content changes, updated fit results.


Last modified: 15 Nov 2016
Smithsonian Institute Smithsonian Institute

The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory. 60 Garden Street, Cambridge, MA 02138 USA.   Email:   cxchelp@head.cfa.harvard.edu Smithsonian Institution, Copyright © 1998-2017. All rights reserved.