Simulating Chandra ACIS-S Spectra with Sherpa
![[CXC Logo]](/ciao/imgs/cxc-logo.gif)
Sherpa Threads (CIAO 4.5 Sherpa v1)
Overview
Synopsis:
This thread illustrates the use of the Sherpa fake_pha command to simulate a spectrum of a point source obtained with the ACIS-S detector aboard Chandra, with and without consideration of a background component.
If you do not have experience with simulating X-ray spectral data in Sherpa, you may wish to follow the "step-by-step" example in the introductory simulation thread, before attempting the sample analysis in this thread.
Last Update: 13 Dec 2012 - updated for CIAO 4.5: group commands no longer clear the existing data filter
Contents
- Getting started - downloading calibration response files for simulations
- Defining the Instrument Response
- Defining a Source Model Expression
- Running the Simulation with fake_pha
- Defining the Model Normalization for the Simulated Data
- Writing the Simulated Data to Output Files
- Fitting the Simulated Data
- Including a Background Component
- Scripting It
- History
-
Images
- Figure 1: Plot of simulated source spectrum
- Figure 2: Plot of fit to simulated source spectrum
- Figure 3: Plot of fit to simulated source spectrum, with residuals
- Figure 4: Plot of simulated source-plus-background spectrum
- Figure 5: Plot of fit to simulated source-plus-background spectrum
- Figure 6: Plot of fit to simulated source-plus-background spectrum, with residuals
Getting started - downloading calibration response files for simulations
In order to simulate a Chandra ACIS-S spectrum with Sherpa, we must define an instrument response with the appropriate ARF (auxiliary response function) and RMF (redistribution matrix function) files. These files may be downloaded from the Chandra Proposal Planning page of the CalDB (Calibration Database) website, where sample RMFs and corresponding ARFs positioned at the aimpoint of the ACIS-S array (and at selected off-axis points) are available.
In this thread, we use the files aciss_aimpt_cy14.arf and aciss_aimpt_cy14.rmf, positioned at the default pointing for ACIS-S.
The Sherpa fake_pha command calculates a 1-D spectral data set based on a defined instrument response and source model expression. For extensive details on the fake_pha functionality, see the introductory simulation thread Simulating X-ray Spectral Data (PHA): the fake_pha command.
To learn how to simulate the spectrum of a point source which includes a background component, follow the second half of this thread, "Includng a background component".
Defining the Instrument Response
We begin by establishing the instrument response corresponding to the default pointing of the ACIS-S detector:
sherpa> arf1=unpack_arf("aciss_aimpt_cy14.arf") sherpa> print(arf1) name = aciss_aimpt_cy14.arf energ_lo = Float64[1024] energ_hi = Float64[1024] specresp = Float64[1024] bin_lo = None bin_hi = None exposure = 8034.23745459 sherpa> rmf1=unpack_rmf("aciss_aimpt_cy14.rmf") name = aciss_aimpt_cy14.rmf detchans = 1024 energ_lo = Float64[1024] energ_hi = Float64[1024] n_grp = UInt64[1024] f_chan = UInt32[1504] n_chan = UInt32[1504] matrix = Float64[387227] offset = 1 e_min = Float64[1024] e_max = Float64[1024]
Here, the ARF and RMF data are loaded and assigned to a set of variables with the unpack_* commands. These variables will be used to assign the instrument response to the faked data set we will create in the next section, "Defining a Source Model Expression".
Defining a Source Model Expression
Now that we have loaded the ARF and RMF instrument responses, we use the set_source command to establish a source model expression and name the faked data set which will be produced in our simulation.
sherpa> set_source("faked", xsphabs.abs1*powlaw1d.m1) sherpa> m1.gamma=2 sherpa> abs1.nh=0.2
We have defined a source model expression for this simulation using an absorbed 1-D power law model with a Galactic neutral hydrogen column density of 2 x 1021 cm-2 and a power law photon index of 2.
Running the Simulation with fake_pha
Simulating the Chandra spectrum means taking the defined model expression, folding it through the Chandra ACIS-S response, and applying Poisson noise to the counts predicted by the model. The simulation is run with fake_pha, which has four required arguments: dataset ID, ARF, RMF, and exposure time. We decide to simulate an ACIS-S spectrum resulting from a 50 ks exposure of a point source.
sherpa> fake_pha("faked", arf1, rmf1, exposure=50000, grouped=False)
This command associates the data ID "faked" with a simulated data set based on the assumed exposure time, instrument response, and source model expression we defined earlier; Poisson noise is added to the modeled data.
Note that as of Sherpa in CIAO 4.2, the 'arf' and 'rmf' arguments of the fake_pha command can accept filenames directly; e.g., we could have done the following:
sherpa> fake_pha("faked", arf="aciss_aimpt_cy14.arf", rmf="aciss_aimpt_cy14.rmf", exposure=50000, grouped=False)
For detailed information on the available options for setting the 'arf' and 'rmf' arguments of fake_pha, refer to the fake_pha ahelp file.
We may inspect some basic properties of the new data set with the show_data command:
sherpa> show_data() Data Set: faked Filter: 0.0110-14.9431 Energy (keV) Bkg Scale: 1 Noticed Channels: 1-1024 name = faked channel = Float64[1024] counts = Float64[1024] staterror = None syserror = None bin_lo = None bin_hi = None grouping = None quality = None exposure = 50000 backscal = None areascal = None grouped = False subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1] RMF Data Set: faked:1 name = aciss_aimpt_cy14.rmf detchans = 1024 energ_lo = Float64[1024] energ_hi = Float64[1024] n_grp = UInt64[1024] f_chan = UInt32[1504] n_chan = UInt32[1504] matrix = Float64[387227] offset = 1 e_min = Float64[1024] e_max = Float64[1024] ARF Data Set: faked:1 name = aciss_aimpt_cy14.arf energ_lo = Float64[1024] energ_hi = Float64[1024] specresp = Float64[1024] bin_lo = None bin_hi = None exposure = 8034.23745459
Note that the simulated data set currently does not have the correct normalization - the flux of the simulated data is incorrect because the default power law normalization is arbitrarily set to 1.0, as shown with the show_model command:
sherpa> show_model() Model: faked apply_rmf(apply_arf((50000 * (xsphabs.abs1 * powlaw1d.m1)))) Param Type Value Min Max Units ----- ---- ----- --- --- ----- abs1.nh thawed 0.2 0 100000 10^22 atoms / cm^2 m1.gamma thawed 2 -10 10 m1.ref frozen 1 -3.40282e+38 3.40282e+38 m1.ampl thawed 1 0 3.40282e+38
To correct the flux we need to adjust the normalization, as demonstrated in the section "Defining the Model Normalization for the Simulated Data".
Defining the Model Normalization for the Simulated Data
Before we can use the simulated data set for scientific analysis, it must be re-normalized to match the flux (or total counts) required by our selected source.
The .2-10 keV flux in the simulated spectrum is 3.86e-9 ergs cm-2 s-1:
sherpa> calc_energy_flux(0.2,10,"faked") 3.8623970060321874e-09
The .2-10 keV flux of a source in our Chandra proposal, for example, has been measured at 1.e-12 ergs cm-2 s-1. Therefore, the correct normalization is (1.e-12)/(3.8623970060321874e-09)=0.000258906580147:
sherpa> my_flux=1.e-12 sherpa> norm=my_flux/calc_energy_flux(0.2,10,"faked") sherpa> print(norm) 0.000258906580147 sherpa> set_par(m1.ampl,norm) sherpa> show_model("faked") Model: faked apply_rmf(apply_arf((50000 * (xsphabs.abs1 * powlaw1d.m1)))) Param Type Value Min Max Units ----- ---- ----- --- --- ----- abs1.nh thawed 0.2 0 100000 10^22 atoms / cm^2 m1.gamma thawed 2 -10 10 m1.ref frozen 1 -3.40282e+38 3.40282e+38 m1.ampl thawed 0.000258907 0 3.40282e+38 sherpa> fake_pha("faked",arf1,rmf1,exposure=50000) sherpa> prefs = get_data_plot_prefs() sherpa> prefs["yerrorbars"] = 0 # remove y-error bars from plot sherpa> plot_data("faked")
With the new normalization, the simulated flux is correctly set at the measured flux of 1.e-12 ergs cm-2 s-1. A plot of the data is shown in Figure 1.
Note that we could have chosen to re-normalize the simulated data set to match the required total counts instead of flux. For example:
sherpa> my_counts=10000 sherpa> norm_counts=my_counts/calc_data_sum(0.5,8.,"faked") sherpa> print(norm_counts) 1.67588402883
Writing the Simulated Data to Output Files
We may use the save_pha command to write the simulated data as a PHA file, with a header containing the exposure time value and paths to the ARF and RMF files:
# NOTE: the following two 'get_data' lines are necessary to
# work around a CIAO 4.4 bug with the save_pha() function; normally, one would
# just need to use save_pha() to write a simulated PHA data set to file.
sherpa> get_data("faked").header = {} # Make header an empty dictionary
sherpa> get_data("faked").name = "simulation1.pha" # Update name for save_pha
sherpa> save_pha("faked", "simulation1.pha")
We also have the option to save the data to a FITS or ASCII table file with the save_arrays command:
sherpa> save_arrays ("my_sim_data.fits", [get_model_plot("faked").xlo, get_model_plot("faked").y], ascii=False) sherpa> save_arrays ("my_sim_data.txt", [get_model_plot("faked").xlo, get_model_plot("faked").y], ascii=True)
Fitting the Simulated Data
The simulated data set may be filtered and fit as any other data set in Sherpa. For example, we can choose to filter the simulated data to include only the counts in a restricted energy range, such as 0.5 keV - 7.0 keV.:
sherpa> print calc_energy_flux(.2,10,"faked") # ergs cm-2 s-1 1e-12 sherpa> print calc_energy_flux(.5,7,"faked") 8.3483818796e-13 sherpa> print calc_data_sum(.5,7) # counts 5862.0 sherpa> notice(0.5,7) sherpa> show_filter() Data Set Filter: faked 0.5037-7.0007 Energy (keV)
Then, we can fit the simulated data set with the source model expression we used to create it:
sherpa> set_method("neldermead") sherpa> set_stat("cstat") sherpa> fit() Dataset = faked Method = neldermead Statistic = cstat Initial fit statistic = 511.365 Final fit statistic = 506.649 at function evaluation 475 Data points = 446 Degrees of freedom = 443 Probability [Q-value] = 0.0194203 Reduced statistic = 1.14368 Change in statistic = 4.7161 abs1.nH 0.181058 m1.gamma 1.96732 m1.ampl 0.00024272 sherpa> plot_fit("faked") WARNING: unable to calculate errors using current statistic: cstat
The resulting plot is shown in Figure 2.
Next, we examine the quality of the fit with the confidence command (conf), and print the fit and confidence results with show_fit and get_conf_results, respectively.
sherpa> conf() abs1.nH lower bound: -0.0131574 m1.gamma lower bound: -0.0435393 abs1.nH upper bound: 0.0131574 m1.gamma upper bound: 0.0435393 m1.ampl lower bound: -9.86121e-06 m1.ampl upper bound: 1.03345e-05 Dataset = faked Confidence Method = confidence Fitting Method = neldermead Statistic = cstat confidence 1-sigma (68.2689%) bounds: Param Best-Fit Lower Bound Upper Bound ----- -------- ----------- ----------- abs1.nH 0.181058 -0.0131574 0.0131574 m1.gamma 1.96732 -0.0435393 0.0435393 m1.ampl 0.00024272 -9.86121e-06 1.03345e-05 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: CStat Maximum likelihood function (XSPEC style) Fit:Dataset = faked Method = neldermead Statistic = cstat Initial fit statistic = 511.365 Final fit statistic = 506.649 at function evaluation 475 Data points = 446 Degrees of freedom = 443 Probability [Q-value] = 0.0194203 Reduced statistic = 1.14368 Change in statistic = 4.7161 abs1.nH 0.181058 m1.gamma 1.96732 m1.ampl 0.00024272 sherpa> print get_conf_results() datasets = ('faked',) methodname = confidence fitname = neldermead statname = cstat sigma = 1 percent = 68.2689492137 parnames = ('abs1.nH', 'm1.gamma', 'm1.ampl') parvals = (0.18105766123241832, 1.9673247181265501, 0.00024272013078268823) parmins = (-0.013157357251254914, -0.043539257014869381, -9.8612095575289962e-06) parmaxes = (0.013157357251254914, 0.043539257014869603, 1.0334547616290313e-05) nfits = 48
Note that the cstat statistic is appropriate for fitting low-counts data, but it does not calculate errors for the data points. We can group the data so that each bin contains a specified minimum number of counts, and then change the fit statistic to something more suitable to calculate the errors. Finally, we can view the results of the new fit with the plot_fit_delchi command:
sherpa> group_counts("faked", 15) sherpa> set_stat("chi2xspecvar") sherpa> plot_fit_delchi("faked")
The new fit to the grouped simulated spectrum, along with the residuals divided by the uncertainties, is shown in Figure 3.
The plot may be saved as a PostScript file with the ChIPS print_window command:
sherpa> print_window("simulation_fit")
Including a Background Component
In this section, we repeat the steps above to simulate a source PHA data set, but this time, including a background component. This involves adding new Sherpa commands along the way to define settings for the background data.
Defining the Instrument Response
As before, we begin by establishing the instrument response corresponding to the default pointing of the ACIS-S detector, for both a source and background component:
sherpa> arf1=unpack_arf("aciss_aimpt_cy14.arf") sherpa> bkg1_arf = arf1 sherpa> rmf1=unpack_rmf("aciss_aimpt_cy14.rmf") sherpa> bkg1_rmf = rmf1
The source ARF and RMF data are loaded and assigned to a set of variables with the unpack_* commands. These variables will be used to assign the instrument response to both the source and background components of the faked data set we will create in the next section, "Defining Source Model Expressions --> with a background component". If the background response is different than the source response, we load the appropriate background ARF and RMF files accordingly:
sherpa> bkg1_rmf = unpack_rmf("background.rmf") # separate background response
sherpa> bkg1_arf = unpack_arf("background.arf")
Defining Source and Background Model Expressions
We define both the source and background model expressions for our simulation with set_source command, as follows:
sherpa> set_source("faked", xsphabs.abs1*powlaw1d.m1) sherpa> m1.gamma=2 sherpa> abs1.nh=0.2 sherpa> set_source("faked_bkg", polynom1d.bkgA) sherpa> bkgA.c0=1.
For the source simulation, we use an absorbed 1-D power law model with a Galactic neutral hydrogen column density of 2 x 1021 cm-2 and a photon index of 2. For the background simulation, we assume a flat profile with a 1-D polynomial function.
Running the Simulation with fake_pha
Here we run an additional fake_pha simulation for the background data set:
sherpa> fake_pha("faked", arf1, rmf1, exposure=50000, grouped=False) sherpa> fake_pha("faked_bkg", bkg1_arf, bkg1_rmf, 50000, grouped=False)
These commands associate the data IDS "faked" and "faked_bkg" with simulated source and background data sets, respectively, based on the assumed exposure times, instrument responses, and model expressions defined earlier; Poisson noise is added to the modeled data.
We may inspect some basic properties of the new simulated data sets with the show_data command:
sherpa> show_data() Data Set: faked Filter: 0.0110-14.9431 Energy (keV) Bkg Scale: 1 Noticed Channels: 1-1024 name = faked channel = Float64[1024] counts = Float64[1024] staterror = None syserror = None bin_lo = None bin_hi = None grouping = None quality = None exposure = 50000 backscal = None areascal = None grouped = False subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1] RMF Data Set: faked:1 name = aciss_aimpt_cy14.rmf detchans = 1024 energ_lo = Float64[1024] energ_hi = Float64[1024] n_grp = UInt64[1024] f_chan = UInt32[1504] n_chan = UInt32[1504] matrix = Float64[387227] offset = 1 e_min = Float64[1024] e_max = Float64[1024] ARF Data Set: faked:1 name = aciss_aimpt_cy14.arf energ_lo = Float64[1024] energ_hi = Float64[1024] specresp = Float64[1024] bin_lo = None bin_hi = None exposure = 8034.23745459 Background Data Set: faked:1 Filter: 0.0110-14.9431 Energy (keV) Noticed Channels: 1-1024 name = faked channel = Float64[1024] counts = Float64[1024] staterror = None syserror = None bin_lo = None bin_hi = None grouping = None quality = None exposure = 50000 backscal = None areascal = None grouped = False subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background RMF Data Set: faked:1 name = aciss_aimpt_cy14.rmf detchans = 1024 energ_lo = Float64[1024] energ_hi = Float64[1024] n_grp = UInt64[1024] f_chan = UInt32[1504] n_chan = UInt32[1504] matrix = Float64[387227] offset = 1 e_min = Float64[1024] e_max = Float64[1024] Background ARF Data Set: faked:1 name = aciss_aimpt_cy14.arf energ_lo = Float64[1024] energ_hi = Float64[1024] specresp = Float64[1024] bin_lo = None bin_hi = None exposure = 8034.23745459 Data Set: faked_bkg Filter: 0.0110-14.9431 Energy (keV) Noticed Channels: 1-1024 name = faked channel = Float64[1024] counts = Float64[1024] staterror = None syserror = None bin_lo = None bin_hi = None grouping = None quality = None exposure = 50000 backscal = None areascal = None grouped = False subtracted = False bin_hi = None grouping = None quality = None exposure = 50000 backscal = None areascal = None grouped = False subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] RMF Data Set: faked_bkg:1 name = aciss_aimpt_cy14.rmf detchans = 1024 energ_lo = Float64[1024] energ_hi = Float64[1024] n_grp = UInt64[1024] f_chan = UInt32[1504] n_chan = UInt32[1504] matrix = Float64[387227] offset = 1 e_min = Float64[1024] e_max = Float64[1024] ARF Data Set: faked_bkg:1 name = aciss_aimpt_cy14.arf energ_lo = Float64[1024] energ_hi = Float64[1024] specresp = Float64[1024] bin_lo = None bin_hi = None exposure = 8034.23745459
Now, we assign the "faked_bkg" data set as the background component of the faked source data set "faked", using the set_bkg command.
sherpa> set_bkg("faked", get_data("faked_bkg"))
In the next section, we will correct the normalization of the simulated source and background data sets.
Defining the Model Normalization for the Simulation
We determine the normalization for the background data set in the same way as with the source data set, except we use a measure of total counts instead of flux to specify that we want 200 counts in the background simulation:
sherpa> my_flux=1.e-12 sherpa> norm=my_flux/calc_energy_flux(0.2,10,id="faked") sherpa> print(norm) 0.000258906580147 sherpa> bkg_counts = 200 sherpa> bkg_norm = bkg_counts/calc_data_sum(0.2,10.,id="faked_bkg") sherpa> print(bkg_norm) 1.59122975264e-06
Now we apply the calculated values to the amplitude parameters of each model, and re-evaluate the simulated data sets with the desired normaliztion using fake_pha:
sherpa> set_par(m1.ampl,norm) sherpa> set_par(bkgA.c0,bkg_norm) sherpa> show_model() Model: faked apply_rmf(apply_arf((50000 * (xsphabs.abs1 * powlaw1d.m1)))) Param Type Value Min Max Units ----- ---- ----- --- --- ----- abs1.nh thawed 0.2 0 100000 10^22 atoms / cm^2 m1.gamma thawed 2 -10 10 m1.ref frozen 1 -3.40282e+38 3.40282e+38 m1.ampl thawed 0.000258907 0 3.40282e+38 Model: faked_bkg apply_rmf(apply_arf((50000 * polynom1d.bkgA))) Param Type Value Min Max Units ----- ---- ----- --- --- ----- bkgA.c0 thawed 1.59123e-06 -3.40282e+38 3.40282e+38 bkgA.c1 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c2 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c3 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c4 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c5 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c6 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c7 frozen 0 -3.40282e+38 3.40282e+38 bkgA.c8 frozen 0 -3.40282e+38 3.40282e+38 bkgA.offset frozen 0 -3.40282e+38 3.40282e+38 sherpa> fake_pha("faked",arf1,rmf1,exposure=50000) sherpa> fake_pha("faked_bkg",bkg1_arf,bkg1_rmf,50000)
Finally, we re-assign background data set "faked_bkg" as the background component of source data set "faked" with set_bkg , to produce a single source-plus-background simulated data set:
sherpa> set_bkg("faked", get_data("faked_bkg"))
sherpa> prefs = get_data_plot_prefs()
sherpa> prefs["yerrorbars"] = 0 # remove y-error bars from plot
sherpa> plot_data("faked")
The resulting plot is shown in Figure 4.
Fitting the Simulated Data
The simulated source-plus-background data set ("faked") is filtered to include only the counts in the energy range 0.5 keV - 7.0 keV (recalling that data set "faked" now contains the background information stored in data set "faked_bkg"; "faked_bkg" is no longer needed in the context of this thread).
sherpa> notice(0.5,7) sherpa> show_filter("faked") Data Set Filter: faked 0.5037-7.0007 Energy (keV)
Next, we fit the simulated source data with the source model expression we used to create it, and use the set_bkg_model command to incorporate the background model into the fit:
sherpa> set_bkg_model("faked", bkgA, 1) # set model for bkg_id=1 of data set id="faked" sherpa> set_method("neldermead") sherpa> set_stat("cstat") sherpa> fit("faked") Dataset = faked Method = neldermead Statistic = cstat Initial fit statistic = 918.797 Final fit statistic = 905.281 at function evaluation 4087 Data points = 892 Degrees of freedom = 888 Probability [Q-value] = 0.336069 Reduced statistic = 1.01946 Change in statistic = 13.5159 abs1.nH 0.206547 m1.gamma 2.10173 m1.ampl 0.000266902 bkgA.c0 1.67244e-06 sherpa> plot_fit("faked") WARNING: unable to calculate errors using current statistic: cstat
The resulting plot is shown in Figure 5.
Now we can examine the quality of the fit with the confidence command (conf), and return the fit and confidence results with show_fit and get_conf_results, respectively.
sherpa> conf("faked") abs1.nH lower bound: -0.0138074 abs1.nH upper bound: 0.0138074 m1.gamma lower bound: -0.0480364 m1.gamma upper bound: 0.0480364 bkgA.c0 lower bound: -1.15945e-07 m1.ampl lower bound: -1.1321e-05 bkgA.c0 upper bound: 1.19626e-07 m1.ampl upper bound: 1.17738e-05 Dataset = faked Confidence Method = confidence Fitting Method = neldermead Statistic = cstat confidence 1-sigma (68.2689%) bounds: Param Best-Fit Lower Bound Upper Bound ----- -------- ----------- ----------- abs1.nH 0.206547 -0.0138074 0.0138074 m1.gamma 2.10173 -0.0480364 0.0480364 m1.ampl 0.000266902 -1.1321e-05 1.17738e-05 bkgA.c0 1.67244e-06 -1.15945e-07 1.19626e-07 sherpa> print show_fit() Optimization Method: NelderMead name = simplex ftol = 1.19209289551e-07 maxfev = None initsimplex = 0 finalsimplex = 9 step = None iquad = 1 verbose = 0 Statistic: CStat Maximum likelihood function (XSPEC style) Fit:Dataset = faked Method = neldermead Statistic = cstat Initial fit statistic = 918.797 Final fit statistic = 905.281 at function evaluation 4087 Data points = 892 Degrees of freedom = 888 Probability [Q-value] = 0.336069 Reduced statistic = 1.01946 Change in statistic = 13.5159 abs1.nH 0.206547 m1.gamma 2.10173 m1.ampl 0.000266902 bkgA.c0 1.67244e-06 sherpa> print get_conf_results("faked") datasets = ('faked',) methodname = confidence fitname = neldermead statname = cstat sigma = 1 percent = 68.2689492137 parnames = ('abs1.nH', 'm1.gamma', 'm1.ampl', 'bkgA.c0') parvals = (0.20654663372380078, 2.1017345148018389, 0.000266902070948682, 1.672439050377569e-06) parmins = (-0.013807370816206865, -0.048036393113886522, -1.1320993543851134e-05, -1.159451035286979e-07) parmaxes = (0.013807370816206865, 0.048036393113886522, 1.1773833285605093e-05, 1.1962590046611675e-07) nfits = 83
Since the cstat fit statistic does not calculate errors for the data points, we group the data and change the fit statistic to chi2xspecvar to do so. Finally, we view the results of the new fit with the plot_fit_delchi command:
sherpa> group_counts("faked", 15) WARNING: grouping flags have changed, noticing all bins sherpa> set_stat("chi2xspecvar") sherpa> plot_fit_delchi("faked")
The new fit to the grouped simulated source-plus-background spectrum, along with the residuals divided by the uncertainties, is shown in Figure 6.
The plot may now be saved as a PostScript file:
sherpa> print_window("simulation_fit_w_bkg")
Scripting It
The file fit.py is a Python script which performs the primary commands used above; it can be executed by typing execfile("fit.py") 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, etcetera.)
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.5 syntax to you.
History
| 02 Feb 2009 | created for CIAO/Sherpa 4.1 |
| 29 Apr 2009 | new script command is available with CIAO 4.1.2 |
| 12 Jan 2010 | updated for CIAO 4.2 |
| 13 Jul 2010 | updated for CIAO 4.2 Sherpa v2: removal of S-Lang version of thread. |
| 15 Dec 2011 | reviewed for CIAO 4.4: a work-around for a save_pha bug was added; response files used in examples updated for Chandra proposal cycle 14 |
| 13 Dec 2012 | updated for CIAO 4.5: group commands no longer clear the existing data filter |

![[Plot of simulated source spectrum]](1.png)
![[Plot of fit to simulated source spectrum]](3.png)
![[Plot of fit to simulated source spectrum, with residuals]](4.png)
![[Plot of simulated source+background spectrum]](2.png)
![[Plot of fit to simulated source-plus-background spectrum]](5.png)
![[Plot of fit to simulated source-plus-background spectrum, with residuals]](6.png)