Chandra X-Ray Observatory
Skip to the navigation links
Last modified: 22 Nov 2016


Creating an Input Spectrum for MARX and ChaRT Simulations

Sherpa Threads (CIAO 4.9 Sherpa v1)



This thread shows how to use Sherpa to create the source spectrum required as input to the two available Chandra PSF simulators, MARX and the Chandra Ray Tracer, ChaRT. One of the spectral specifications needed to run a MARX or ChaRT simulation is an unconvolved spectrum file, i.e., one containing the definition of the source spectrum at the entrance aperture of the HRMA for which a PSF should be simulated. This thread steps through an example Sherpa script which can be used to create such a spectrum for input to MARX or ChaRT. MARX and ChaRT are separate PSF simulators, but note the use of ChaRT requires MARX as a second step; the ChaRT output must be projected onto the detector plane with MARX; see the ChaRT threads for details.

MARX provides a detailed ray-trace simulation of how Chandra responds to a variety of astrophysical sources and can generate standard FITS event files and images as output. It contains detailed models for Chandra's High Resolution Mirror Assembly (HRMA), the HETG and LETG gratings, and all the focal plane detectors. ChaRT traces a collection of rays through the Chandra X-ray optics which are then projected onto the detector separately via MARX.

Last Update: 22 Nov 2016 - reviewed for CIAO 4.9; note bug in get_source_plot which shows the entire source model energy grid, even when an energy range argument is applied to the function.


Creating the Spectrum

The spectrum input to MARX must be in units of energy (keV) versus photon flux density (photons/cm2/s/keV), and the input to ChaRT must be in units of energy (keV) versus photon flux (photons/cm2/s). Model source spectra in Sherpa are automatically output in the required units for both interfaces, using the appropriate model command for each; and those created with the ChaRT routines contributed to Sherpa may also be used as input to ChaRT, as shown in the section "Plotting and Saving the Spectrum."

For this example, we choose to fit a source data spectrum with a one-dimensional power-law multiplied by a photoelectric absorption model:

unix% sherpa
Welcome to Sherpa: CXC's Modeling and Fitting Package
CIAO 4.9 Sherpa version 1 Friday, December 2, 2016

sherpa> load_pha("3c273.pi")
sherpa> subtract()
sherpa> notice(0.4, 6.0)
sherpa> set_source(xsphabs.abs1 * powlaw1d.p1)
sherpa> abs1.nh = 0.07
sherpa> freeze(abs1)
sherpa> guess(p1)
sherpa> fit()
Dataset               = 1
Method                = levmar
Statistic             = chi2gehrels
Initial fit statistic = 159.392
Final fit statistic   = 59.2606 at function evaluation 16
Data points           = 384
Degrees of freedom    = 382
Probability [Q-value] = 1
Reduced statistic     = 0.155132
Change in statistic   = 100.131
   p1.gamma       2.14376     
   p1.ampl        0.000119822 

The example script above does the following:

  • loads the source PHA data set "3c273.pi" into Sherpa and assigns it to default data set id=1 with load_pha;
  • subtracts the background from the source data with subtract;
  • restricts the energy range of the data set to 0.4-6.0 keV with notice;
  • defines the unconvolved source model expression with set_source;
  • freezes the photoelectric absorption model parameters so that they are not allowed to vary during the fit;
  • guesses reasonable initial parameter values for the power-law model;
  • fits the chosen source model to the data.

Note that the Sherpa fake_pha functionality is available for simulating a source spectrum independent of an actual data set; see the Sherpa simulation threads for details.

Plotting and Saving the Spectrum

In order to plot and save the best-fit source model spectrum to be input to ChaRT, use the functions plot_chart_spectrum and save_chart_spectrum, available in the chart_spectrum contributed Sherpa package. For MARX, the Sherpa plot_source, get_source_plot, and save_arrays commands should be used.


sherpa> from sherpa_contrib.chart import *

After importing the chart_spectrum routines, the source model spectrum may be plotted in Sherpa with plot_chart_spectrum as follows, where the optional elow and ehigh values specify the minimum and maximum energies to include in the plot:

sherpa> plot_chart_spectrum()
sherpa> plot_chart_spectrum(elow=1.0, ehigh=8.0)

Figure 1 shows the resulting plot.

[A plot of photon flux (photons cm-2 s-1) versus energy (keV).]
[Print media version: A plot of photon flux (photons cm-2 s-1) versus energy (keV).]

Figure 1: A plot of the spectrum input to ChaRT

The source model is plotted in flux units of photons cm-2 s-1 versus energy in keV, as required by ChaRT.

The save_chart_spectrum command is used to create the output spectrum file to be input to ChaRT. In this example, we use the optional elow and ehigh arguments to restrict the output to the 1-8 keV range. If the bounds are omitted, the full energy range as determined by Sherpa is used.

sherpa> save_chart_spectrum("source_flux_chart.dat", elow=1.0, ehigh=8.0)
Created: source_flux_chart.dat
sherpa> quit()

The output produced by save_chart_spectrum is an ASCII file that contains three columns: the first two columns must be the lower and upper energy boundaries, in keV, and the third column is the photon flux in units of photon/cm2/sec; the values in the flux column must be >0.

unix% more source_flux_chart.dat
# elo ehi spectrum
1.0 1.009999999999 1.8253763836987e-06
1.009999999999 1.019999999997 1.7956056475391e-06
1.019999999997 1.029999999996 1.7664013800372e-06
1.029999999996 1.039999999995 1.7377565271506e-06
1.039999999995 1.049999999993 1.7096638946840e-06
1.049999999993 1.059999999992 1.6821157063999e-06
1.059999999992 1.069999999990 1.6551041353458e-06
1.069999999990 1.079999999989 1.6282217511407e-06
1.079999999989 1.089999999988 1.6020206525494e-06
1.089999999988 1.099999999986 1.5765916012327e-06
7.899999999057 7.909999999055 2.6761012039117e-08
7.909999999055 7.919999999054 2.6689072832491e-08
7.919999999054 7.929999999053 2.6617416064974e-08
7.929999999053 7.939999999051 2.6546041856098e-08
7.939999999051 7.949999999050 2.6474947157054e-08
7.949999999050 7.959999999049 2.6404132097738e-08
7.959999999049 7.969999999047 2.6333593656606e-08
7.969999999047 7.979999999046 2.6263333541434e-08
7.979999999046 7.989999999044 2.6193345617934e-08
7.989999999044 7.999999999043 2.6123633167393e-08


Since the MARX software requires the input spectrum to be in photon flux density units (photons/s/cm2/keV), we use the Sherpa plot_source function and associated get_source_plot to view and access the spectral data arrays, since these functions automatically output the unconvolved spectrum in the required units.

sherpa> plot_source(lo=1.0,hi=8.0)

Figure 2 shows the resulting unconvolved model plot in the 1.0-8.0 keV energy range.

[A plot of photon flux density (photons cm-2 s-1 keV-1) versus energy (keV).]
[Print media version: A plot of photon flux density (photons cm-2 s-1 keV-1) versus energy (keV).]

Figure 2: A plot of the spectrum input to MARX

The source model is plotted in flux density units of photons cm-2 s-1 keV-1 versus energy in keV, as required by MARX.

The spectrum may be written to an ASCII file using the Sherpa save_arrays command, where the input x- and y- data arrays to be written to the file are taken from the plot of the spectrum we just created with the plot_source command. This is done by retrieving the energy and photon flux density arrays from the output of the get_source_plot command. Due to a bug in get_source_plot, the lo and hi arguments in the function cannot be used, and the entire energy grid defined by the ARF/RMF is used for the spectrum.

sherpa> set_analysis(1, "energy", "rate", factor=0) # reset to default flux units
sherpa> print(get_source_plot())
xlo    = [  0.1    0.11   0.12 ...,  10.97  10.98  10.99]
xhi    = [  0.11   0.12   0.13 ...,  10.98  10.99  11.  ]
y      = [  1.4874e-18   4.7047e-15   1.6421e-12 ...,   1.3301e-06   1.3275e-06   1.3250e-06]
xlabel = Energy (keV)
ylabel = f(E)  Photons/sec/cm^2/keV 
title  = Source Model of 3c273.pi
histo_prefs = {'yerrorbars': False, 'linethickness': 2, 'symbolcolor': None, 'symbolfill': None, 'symbolsize': None, 'xlog': False, 'ylog': False, 'symbolangle': None, 'fillstyle': None, 'errthickness': None, 'errcolor': None, 'fillcolor': None, 'linecolor': 'red', 'errstyle': None, 'linestyle': 1, 'symbolstyle': 0, 'fillopacity': None}

sherpa> fluxdensity = get_source_plot().y

sherpa> energy = get_source_plot().xhi

sherpa> save_arrays("source_flux_marx.dat",[energy,fluxdensity],["keV","photons/s/cm**2/keV"],ascii=True)

The ASCII file written by the above save_arrays command lists the energy upper-bin edge in keV in the first column, and the flux density in photons cm-2 s-1 keV-1 in the second column. Note that the MARX format for an input spectrum uses the upper-bin edge for energy and that the flux density in the first row may be any value, as the entry is not used.

unix% more source_flux_marx.dat
1.0999999940395e-01 1.4873732915649e-18
1.1999999731779e-01 4.7047097549045e-15
1.2999999523163e-01 1.6421338065759e-12
1.4000000059605e-01 1.2506516519562e-10
1.5000000596046e-01 3.2724720025442e-09
1.5999999642372e-01 3.9784974304574e-08
1.7000000178814e-01 2.7327706694442e-07
1.8000000715256e-01 1.2097588802466e-06
1.8999999761581e-01 4.0465976847202e-06
2.0000000298023e-01 1.0629700083308e-05
2.0999999344349e-01 2.3142278930328e-05
2.1999999880791e-01 4.3510970419516e-05
2.3000000417233e-01 7.2854357693330e-05
2.3999999463558e-01 1.1116905417320e-04
2.5000000000000e-01 1.5708965279766e-04
2.5999999046326e-01 2.0807720610748e-04
2.7000001072884e-01 2.6390105715809e-04
2.8000000119209e-01 3.2129585650911e-04
2.8999999165535e-01 3.5519795041674e-04
3.0000001192093e-01 3.4419346894314e-04

Scripting It

A script like the one used in this thread to create a source spectrum for input to MARX or ChaRT may be saved to an ASCII file—e.g.,—and executed on the command line or in a future Sherpa session as follows:

sherpa> exec(open("").read())     # within Sherpa


unix> sherpa            # at the Unix 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 3.4 scripts or save files, submit them to us via the CXC Helpdesk and we will provide the CIAO/Sherpa 4.9 syntax to you.


You are ready to run a MARX simulation once you have created a spectrum input file like example file source_flux_chart.dat, and have chosen an exposure time for the PSF simulation.


11 Mar 2010 original version
13 Jul 2010 updated for CIAO 4.2 Sherpa v2: removal of S-Lang version of thread
06 Jan 2012 reviewed for CIAO 4.4: updated Sherpa start-up banner
04 Dec 2013 reviewed for CIAO 4.6: no changes
20 Jan 2015 reviewed for CIAO 4.7, updated format for MARX
09 Dec 2015 reviewed for CIAO 4.8, updated to create input spectrum for ChaRT v2.
25 Jan 2016 updated to make use of fixed sherpa_contrib.chart Python module.
22 Nov 2016 reviewed for CIAO 4.9; note bug in get_source_plot which shows the entire source model energy grid, even when an energy range argument is applied to the function.

Last modified: 22 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: Smithsonian Institution, Copyright © 1998-2017. All rights reserved.