GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralTable Class Reference

Spectral table model class. More...

#include <GModelSpectralTable.hpp>

Inheritance diagram for GModelSpectralTable:
GModelSpectral GBase

Public Member Functions

 GModelSpectralTable (void)
 Void constructor. More...
 
 GModelSpectralTable (const GFilename &filename, const double &norm)
 File constructor. More...
 
 GModelSpectralTable (const GEbounds &ebounds, const GModelSpectralTablePars &pars, const GNdarray &spectra)
 Table model constructor. More...
 
 GModelSpectralTable (const GXmlElement &xml)
 XML constructor. More...
 
 GModelSpectralTable (const GModelSpectralTable &model)
 Copy constructor. More...
 
virtual ~GModelSpectralTable (void)
 Destructor. More...
 
virtual GModelSpectralTableoperator= (const GModelSpectralTable &model)
 Assignment operator. More...
 
virtual void clear (void)
 Clear table model. More...
 
virtual GModelSpectralTableclone (void) const
 Clone table model. More...
 
virtual std::string classname (void) const
 Return class name. More...
 
virtual std::string type (void) const
 Return model type. More...
 
virtual double eval (const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
 Evaluate spectral table model. More...
 
virtual double flux (const GEnergy &emin, const GEnergy &emax) const
 Returns model photon flux between emin, emax More...
 
virtual double eflux (const GEnergy &emin, const GEnergy &emax) const
 Returns model energy flux between emin, emax More...
 
virtual GEnergy mc (const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
 Returns MC energy between [emin, emax]. More...
 
virtual void read (const GXmlElement &xml)
 Read model from XML element. More...
 
virtual void write (GXmlElement &xml) const
 Write model into XML element. More...
 
virtual std::string print (const GChatter &chatter=NORMAL) const
 Print table model information. More...
 
int nspectra (void) const
 Return number of spectra in table model. More...
 
GModelSpectralTablePartable_par (const int &index)
 Return reference to table parameter. More...
 
const GModelSpectralTablePartable_par (const int &index) const
 Return const reference to table parameter. More...
 
GModelSpectralTablePartable_par (const std::string &name)
 Return reference to table parameter. More...
 
const GModelSpectralTablePartable_par (const std::string &name) const
 Return const reference to table parameter. More...
 
double norm (void) const
 Return normalization factor. More...
 
void norm (const double &norm)
 Set normalization factor. More...
 
const GEboundsebounds (void) const
 Return reference to energy boundaries. More...
 
void load (const GFilename &filename)
 Load table from file. More...
 
void save (const GFilename &filename, const bool &clobber=false) const
 Save table into file. More...
 
const GFilenamefilename (void) const
 Return file name. More...
 
void energy_scale (const std::string &name)
 Set energy scale. More...
 
- Public Member Functions inherited from GModelSpectral
 GModelSpectral (void)
 Void constructor. More...
 
 GModelSpectral (const GModelSpectral &model)
 Copy constructor. More...
 
virtual ~GModelSpectral (void)
 Destructor. More...
 
virtual GModelSpectraloperator= (const GModelSpectral &model)
 Assignment operator. More...
 
virtual GModelParoperator[] (const int &index)
 Returns model parameter. More...
 
virtual const GModelParoperator[] (const int &index) const
 Returns model parameter (const version) More...
 
virtual GModelParoperator[] (const std::string &name)
 Returns reference to model parameter. More...
 
virtual const GModelParoperator[] (const std::string &name) const
 Returns reference to model parameter (const version) More...
 
GModelParat (const int &index)
 Returns model parameter. More...
 
const GModelParat (const int &index) const
 Returns model parameter (const version) More...
 
bool has_par (const std::string &name) const
 Checks if parameter name exists. More...
 
int size (void) const
 Return number of parameters. More...
 
void autoscale (void)
 Autoscale parameters. More...
 
- Public Member Functions inherited from GBase
virtual ~GBase (void)
 Destructor. More...
 

Protected Member Functions

void init_members (void)
 Initialise class members. More...
 
void copy_members (const GModelSpectralTable &model)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
void set_par_pointers (void)
 Set parameter pointers. More...
 
void set_energy_nodes (void)
 Set energy nodes from energy boundaries. More...
 
void scale_energy (void)
 Scale energy. More...
 
bool has_energy_scale (void) const
 Signal that energy scale was set. More...
 
GFitsBinTable create_par_table (void) const
 Create PARAMETERS FITS table. More...
 
GFitsBinTable create_eng_table (void) const
 Create ENERGIES FITS table. More...
 
GFitsBinTable create_spec_table (void) const
 Create SPECTRA FITS table. More...
 
void load_par (const GFits &fits)
 Load data from PARAMETERS extension. More...
 
void load_eng (const GFits &fits)
 Load data from ENERGIES extension. More...
 
void load_spec (const GFits &fits)
 Load data from SPECTRA extension. More...
 
int par_index (const std::string &name) const
 Return index for parameter name. More...
 
void update (void) const
 Update cache for spectral table model computation. More...
 
void update_flux (void) const
 Update flux cache. More...
 
void update_mc (const GEnergy &emin, const GEnergy &emax) const
 Update MC cache. More...
 
- Protected Member Functions inherited from GModelSpectral
void init_members (void)
 Initialise class members. More...
 
void copy_members (const GModelSpectral &model)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 

Protected Attributes

GModelPar m_norm
 Normalization factor. More...
 
GModelSpectralTablePars m_table_pars
 Table model parameters. More...
 
GNdarray m_spectra
 Spectra. More...
 
GEbounds m_ebounds
 Energy boundaries. More...
 
GFilename m_filename
 Filename of table. More...
 
std::string m_escale_par
 Energy scaling parameter. More...
 
double m_escale
 Energy scale. More...
 
double m_log10escale
 log10 of energy scale More...
 
int m_npars
 Number of parameters. More...
 
int m_nebins
 Number of energy bins. More...
 
std::vector< double > m_last_values
 Last parameter values. More...
 
GNodeArray m_lin_nodes
 Energy nodes of function. More...
 
GNodeArray m_log_nodes
 log10(Energy) nodes of function More...
 
GNdarray m_lin_values
 Function values and grad's. More...
 
GNdarray m_log_values
 log10(Function) values and grad's More...
 
std::vector< double > m_prefactor
 Power-law normalisations. More...
 
std::vector< double > m_gamma
 Power-law indices. More...
 
std::vector< double > m_epivot
 Power-law pivot energies. More...
 
std::vector< double > m_flux
 Photon fluxes. More...
 
std::vector< double > m_eflux
 Energy fluxes. More...
 
GEnergy m_mc_emin
 Minimum energy. More...
 
GEnergy m_mc_emax
 Maximum energy. More...
 
std::vector< double > m_mc_cum
 Cumulative distribution. More...
 
std::vector< double > m_mc_min
 Lower boundary for MC. More...
 
std::vector< double > m_mc_max
 Upper boundary for MC. More...
 
std::vector< double > m_mc_exp
 Exponent for MC. More...
 
- Protected Attributes inherited from GModelSpectral
std::vector< GModelPar * > m_pars
 Parameter pointers. More...
 

Detailed Description

Spectral table model class.

Definition at line 55 of file GModelSpectralTable.hpp.

Constructor & Destructor Documentation

GModelSpectralTable::GModelSpectralTable ( void  )

Void constructor.

Definition at line 88 of file GModelSpectralTable.cpp.

References init_members().

Referenced by clone().

GModelSpectralTable::GModelSpectralTable ( const GFilename filename,
const double &  norm 
)

File constructor.

Parameters
[in]filenameFile name of table model.
[in]normNormalization factor.

Constructs a spectral table model from a FITS file. See the load() method for more information about the expected structure of the FITS file.

Definition at line 107 of file GModelSpectralTable.cpp.

References init_members(), load(), m_norm, GOptimizerPar::range(), GOptimizerPar::remove_range(), GOptimizerPar::scale(), and GOptimizerPar::value().

GModelSpectralTable::GModelSpectralTable ( const GEbounds ebounds,
const GModelSpectralTablePars pars,
const GNdarray spectra 
)

Table model constructor.

Parameters
[in]eboundsEnergy boundaries.
[in]parsTable model parameters.
[in]spectraSpectra.

Constructs a spectral table model from energy boundaries, table model parameters, and spectra.

Definition at line 139 of file GModelSpectralTable.cpp.

References GNdarray::dim(), ebounds(), G_CONST, init_members(), m_ebounds, m_spectra, m_table_pars, set_energy_nodes(), set_par_pointers(), GNdarray::shape(), GModelSpectralTablePars::size(), GEbounds::size(), and gammalib::str().

GModelSpectralTable::GModelSpectralTable ( const GXmlElement xml)
explicit

XML constructor.

Parameters
[in]xmlXML element.

Constructs a spectral table model by extracting information from an XML element. See the read() method for more information about the expected structure of the XML element.

Definition at line 207 of file GModelSpectralTable.cpp.

References init_members(), and read().

GModelSpectralTable::GModelSpectralTable ( const GModelSpectralTable model)

Copy constructor.

Parameters
[in]modelTable model.

Definition at line 226 of file GModelSpectralTable.cpp.

References copy_members(), and init_members().

GModelSpectralTable::~GModelSpectralTable ( void  )
virtual

Destructor.

Definition at line 243 of file GModelSpectralTable.cpp.

References free_members().

Member Function Documentation

std::string GModelSpectralTable::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GModelSpectralTable").

Implements GModelSpectral.

Definition at line 164 of file GModelSpectralTable.hpp.

void GModelSpectralTable::clear ( void  )
virtual

Clear table model.

Implements GModelSpectral.

Definition at line 298 of file GModelSpectralTable.cpp.

References free_members(), GModelSpectral::free_members(), init_members(), and GModelSpectral::init_members().

Referenced by load().

GModelSpectralTable * GModelSpectralTable::clone ( void  ) const
virtual

Clone table model.

Implements GModelSpectral.

Definition at line 316 of file GModelSpectralTable.cpp.

References GModelSpectralTable().

void GModelSpectralTable::copy_members ( const GModelSpectralTable model)
protected
GFitsBinTable GModelSpectralTable::create_eng_table ( void  ) const
protected

Create ENERGIES FITS table.

Returns
Binary FITS table containing table model energies

The method creates a binary FITS table that is compliant with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/node7.html

Definition at line 1636 of file GModelSpectralTable.cpp.

References GFitsTable::append(), GFitsHDU::card(), GEbounds::emax(), GEbounds::emin(), GFitsHDU::extname(), GEnergy::keV(), m_ebounds, and GEbounds::size().

Referenced by save().

GFitsBinTable GModelSpectralTable::create_par_table ( void  ) const
protected
GFitsBinTable GModelSpectralTable::create_spec_table ( void  ) const
protected

Create SPECTRA FITS table.

Returns
Binary FITS table containing table model spectra

The method creates a binary FITS table that is compliant with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/node8.html

Definition at line 1681 of file GModelSpectralTable.cpp.

References GFitsTable::append(), GFitsHDU::card(), GNdarray::dim(), GFitsHDU::extname(), m_ebounds, m_spectra, m_table_pars, GNdarray::shape(), GModelSpectralTablePars::size(), GEbounds::size(), and GFitsTableCol::unit().

Referenced by save().

const GEbounds & GModelSpectralTable::ebounds ( void  ) const
inline

Return reference to energy boundaries.

Returns
Reference to energy boundaries.

Returns a reference to energy boundaries.

Definition at line 221 of file GModelSpectralTable.hpp.

References m_ebounds.

Referenced by GModelSpectralTable(), and update().

double GModelSpectralTable::eflux ( const GEnergy emin,
const GEnergy emax 
) const
virtual

Returns model energy flux between emin, emax

Parameters
[in]eminMinimum photon energy.
[in]emaxMaximum photon energy.
Returns
Energy flux (erg/cm2/s).

Computes

\[ \int_{\tt emin}^{\tt emax} S_{\rm E}(E | t) E \, dE \]

where

  • [emin, emax] is an energy interval, and
  • \(S_{\rm E}(E | t)\) is the spectral model (ph/cm2/s/MeV).

Implements GModelSpectral.

Definition at line 584 of file GModelSpectralTable.cpp.

References GNodeArray::inx_left(), m_eflux, m_epivot, m_gamma, m_lin_nodes, m_prefactor, GEnergy::MeV(), gammalib::MeV2erg, norm(), gammalib::plaw_energy_flux(), GNodeArray::set_value(), update(), and update_flux().

Referenced by update_flux().

void GModelSpectralTable::energy_scale ( const std::string &  name)

Set energy scale.

Parameters
[in]nameTable parameter name for energy scaling.
Exceptions
GException::invalid_argumentTable model parameter name not found.s

Sets a given table parameter name as energy scale for table model interpolation.

Definition at line 1029 of file GModelSpectralTable.cpp.

References G_READ, m_escale_par, m_log10escale, m_table_pars, GOptimizerPar::name(), scale_energy(), and GModelSpectralTablePars::size().

Referenced by read().

double GModelSpectralTable::eval ( const GEnergy srcEng,
const GTime srcTime = GTime(),
const bool &  gradients = false 
) const
virtual

Evaluate spectral table model.

Parameters
[in]srcEngTrue photon energy.
[in]srcTimeTrue photon arrival time.
[in]gradientsCompute gradients?
Returns
Model value (ph/cm2/s/MeV).

Evaluates

\[ S_{\rm E}(E | t) = {\tt m\_norm} \times \left( w_l F_l(p) + w_r F_r(p) \right) \]

where

  • \({\tt m\_norm}\) is the normalization factor of the spectral table model,
  • \(w_l\) is the weight of the spectral vector \(F_l(p)\) with the largest energy \(E_l\) that satisfies \(E<E_l\), and
  • \(w_r\) is the weight of the spectral vector \(F_r(p)\) with the smallest energy \(E_r\) that satisfies \(E>E_r\).

The weights are computed using

\[ w_r = \frac{\log_{10} E - \log_{10} E_l}{\log_{10} E_r - \log_{10} E_l} \]

and \(w_l = 1 - w_r\).

If gradient is true, the method also computes the parameter gradients using

\[ \frac{\delta S_{\rm E}(E | t)}{\delta {\tt m\_norm}} = \frac{S_{\rm E}(E | t)}{{\tt m\_norm}} \]

and

\[ \frac{\delta S_{\rm E}(E | t)}{\delta p} = {\tt m\_norm} \times \left( w_l \frac{\delta F_l(p)}{\delta p} + w_r \frac{\delta F_r(p)}{\delta p} \right) \]

for all other parameters.

For the computation of \(F_l(p)\), \(F_r(p)\), \(\frac{\delta F_l(p)}{\delta p}\), and \(\frac{\delta F_r(p)}{\delta p}\) see the update() method.

Implements GModelSpectral.

Definition at line 377 of file GModelSpectralTable.cpp.

References GOptimizerPar::factor_gradient(), GOptimizerPar::factor_value(), GNodeArray::inx_left(), GNodeArray::inx_right(), GOptimizerPar::is_free(), gammalib::is_infinite(), gammalib::is_notanumber(), gammalib::ln10, log10(), GEnergy::log10MeV(), m_escale_par, m_lin_values, m_log10escale, m_log_nodes, m_norm, m_table_pars, GOptimizerPar::name(), norm(), GOptimizerPar::scale(), GNodeArray::set_value(), GModelSpectralTablePars::size(), table_par(), update(), GOptimizerPar::value(), GNodeArray::wgt_grad_left(), GNodeArray::wgt_grad_right(), GNodeArray::wgt_left(), and GNodeArray::wgt_right().

const GFilename & GModelSpectralTable::filename ( void  ) const
inline

Return file name.

Returns
Name of table file.

Returns the name of the table file.

Definition at line 235 of file GModelSpectralTable.hpp.

References m_filename.

Referenced by load(), and save().

double GModelSpectralTable::flux ( const GEnergy emin,
const GEnergy emax 
) const
virtual

Returns model photon flux between emin, emax

Parameters
[in]eminMinimum photon energy.
[in]emaxMaximum photon energy.
Returns
Photon flux (ph/cm2/s).

Computes

\[ \int_{\tt emin}^{\tt emax} S_{\rm E}(E | t) dE \]

where

  • [emin, emax] is an energy interval, and
  • \(S_{\rm E}(E | t)\) is the spectral model (ph/cm2/s/MeV).

Implements GModelSpectral.

Definition at line 491 of file GModelSpectralTable.cpp.

References GNodeArray::inx_left(), m_epivot, m_flux, m_gamma, m_lin_nodes, m_prefactor, GEnergy::MeV(), norm(), gammalib::plaw_photon_flux(), GNodeArray::set_value(), update(), and update_flux().

Referenced by update_flux(), and update_mc().

void GModelSpectralTable::free_members ( void  )
protected

Delete class members.

Definition at line 1272 of file GModelSpectralTable.cpp.

Referenced by clear(), operator=(), and ~GModelSpectralTable().

bool GModelSpectralTable::has_energy_scale ( void  ) const
protected

Signal that energy scale was set.

Returns
True if energy scale was set

Definition at line 1479 of file GModelSpectralTable.cpp.

References m_escale.

Referenced by set_energy_nodes().

void GModelSpectralTable::load ( const GFilename filename)

Load table from file.

Parameters
[in]filenameFile name.

Loads table model from FITS file. The format of the FITS file complies with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/ogip_92_009.html

Definition at line 827 of file GModelSpectralTable.cpp.

References clear(), GFits::close(), filename(), GFilename::is_empty(), load_eng(), load_par(), load_spec(), m_filename, set_energy_nodes(), and set_par_pointers().

Referenced by GModelSpectralTable(), and read().

void GModelSpectralTable::load_eng ( const GFits fits)
protected

Load data from ENERGIES extension.

Parameters
[in]fitsFITS file.

The method loads data from the ENERGIES binary table. The format of the table needs to be compliant with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/node7.html

If energy units are provided in the ENERGIES extension the method decodes the units and interprets the energy values correspondingly.

Definition at line 1867 of file GModelSpectralTable.cpp.

References GEbounds::append(), GFitsHDU::integer(), m_ebounds, and GFits::table().

Referenced by load().

void GModelSpectralTable::load_par ( const GFits fits)
protected

Load data from PARAMETERS extension.

Parameters
[in]fitsFITS file.
Exceptions
GException::invalid_valueNon-positive parameter value encountered for logarithmic parameters.

The method loads data from the PARAMETERS binary table. The format of the table needs to be compliant with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/node6.html

Definition at line 1771 of file GModelSpectralTable.cpp.

References GModelSpectralTablePars::append(), GOptimizerPar::factor_range(), GOptimizerPar::fix(), GOptimizerPar::free(), GOptimizerPar::has_grad(), GFitsHDU::integer(), m_table_pars, max(), GModelSpectralTablePar::method(), min(), GFitsHDU::real(), GOptimizerPar::scale(), and GFits::table().

Referenced by load().

void GModelSpectralTable::load_spec ( const GFits fits)
protected

Load data from SPECTRA extension.

Parameters
[in]fitsFITS file.

The method loads data from the SPECTRA binary table. The format of the table needs to be compliant with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/node8.html

Definition at line 1911 of file GModelSpectralTable.cpp.

References GFitsHDU::integer(), GNodeArray::inx_left(), GNodeArray::inx_right(), m_spectra, m_table_pars, GFitsHDU::real(), GNodeArray::set_value(), GFits::table(), GNodeArray::wgt_left(), and GNodeArray::wgt_right().

Referenced by load().

GEnergy GModelSpectralTable::mc ( const GEnergy emin,
const GEnergy emax,
const GTime time,
GRan ran 
) const
virtual

Returns MC energy between [emin, emax].

Parameters
[in]eminMinimum photon energy.
[in]emaxMaximum photon energy.
[in]timeTrue photon arrival time.
[in,out]ranRandom number generator.
Returns
Energy.

Returns Monte Carlo energy by randomly drawing from a broken power law defined by the file function.

Implements GModelSpectral.

Definition at line 675 of file GModelSpectralTable.cpp.

References gammalib::check_energy_interval(), exp(), G_MC, log(), m_mc_cum, m_mc_exp, m_mc_max, m_mc_min, GEnergy::MeV(), GRan::uniform(), update(), update_flux(), and update_mc().

double GModelSpectralTable::norm ( void  ) const
inline

Return normalization factor.

Returns
Normalization factor.

Returns the normalization factor.

Definition at line 192 of file GModelSpectralTable.hpp.

References m_norm, and GOptimizerPar::value().

Referenced by eflux(), eval(), flux(), read(), update_mc(), and write().

void GModelSpectralTable::norm ( const double &  norm)
inline

Set normalization factor.

Parameters
[in]normNormalization factor.

Sets the normalization factor.

Definition at line 206 of file GModelSpectralTable.hpp.

References m_norm, and GOptimizerPar::value().

int GModelSpectralTable::nspectra ( void  ) const

Return number of spectra in table model.

Returns
Number of spectra.

If the table model is empty the method returns zero.

Definition at line 925 of file GModelSpectralTable.cpp.

References m_table_pars, and GModelSpectralTablePars::size().

Referenced by print(), and scale_energy().

GModelSpectralTable & GModelSpectralTable::operator= ( const GModelSpectralTable model)
virtual

Assignment operator.

Parameters
[in]modelTable model.
Returns
Table model.

Definition at line 265 of file GModelSpectralTable.cpp.

References copy_members(), free_members(), init_members(), and GModelSpectral::operator=().

int GModelSpectralTable::par_index ( const std::string &  name) const
protected

Return index for parameter name.

Parameters
[in]nameParameter name.
Returns
Parameter index.
Exceptions
GException::invalid_argumentParameter name not found in spectral table.

Definition at line 1991 of file GModelSpectralTable.cpp.

References G_PAR_INDEX, m_table_pars, and GModelSpectral::size().

Referenced by scale_energy(), and table_par().

std::string GModelSpectralTable::print ( const GChatter chatter = NORMAL) const
virtual
void GModelSpectralTable::read ( const GXmlElement xml)
virtual

Read model from XML element.

Parameters
[in]xmlXML element containing power law model information.

Reads the spectral information from an XML element. The format of the XML elements is

<spectrum type="TableModel" file="..">
  <parameter name="Normalization" scale=".." value=".." min=".." max=".." free=".."/>
</spectrum>

Optionally, values for the model table parameters can also be provided.

Implements GModelSpectral.

Definition at line 739 of file GModelSpectralTable.cpp.

References GXmlElement::attribute(), energy_scale(), G_READ, GXmlElement::has_attribute(), load(), m_norm, m_table_pars, GOptimizerPar::name(), norm(), GModelPar::read(), GModelSpectralTablePars::size(), gammalib::xml_file_expand(), gammalib::xml_get_par(), and gammalib::xml_has_par().

Referenced by GModelSpectralTable().

void GModelSpectralTable::save ( const GFilename filename,
const bool &  clobber = false 
) const

Save table into file.

Parameters
[in]filenameFile name.
[in]clobberOverwrite existing file?

Save the table model into a FITS file. The FITS file will contain three binary table extensions:

  * PARAMETERS - Table model parameters
  * ENERGIES - Table model energies
  * SPECTRA - Table model spectra

The format of the FITS file complies with https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_92_009/ogip_92_009.html

Definition at line 878 of file GModelSpectralTable.cpp.

References GFits::append(), GFitsHDU::card(), create_eng_table(), create_par_table(), create_spec_table(), filename(), m_filename, GFits::saveto(), and GFilename::url().

void GModelSpectralTable::scale_energy ( void  )
protected

Scale energy.

Scales the energy axis so that all spectra are relative to the energy scaling parameter. This scaling is useful for better interpolation of spectra that have a feature that depends of an energy parameter.

Definition at line 1359 of file GModelSpectralTable.cpp.

References GNdarray::index(), GNodeArray::inx_left(), GNodeArray::inx_right(), log10(), m_ebounds, m_escale, m_escale_par, m_last_values, m_lin_nodes, m_log10escale, m_log_nodes, m_spectra, m_table_pars, nspectra(), par_index(), GNodeArray::set_value(), GModelSpectralTablePars::size(), GNodeArray::size(), GEbounds::size(), table_par(), GModelSpectralTablePar::values(), GNodeArray::wgt_left(), and GNodeArray::wgt_right().

Referenced by energy_scale().

void GModelSpectralTable::set_energy_nodes ( void  )
protected
void GModelSpectralTable::set_par_pointers ( void  )
protected

Set parameter pointers.

Definition at line 1282 of file GModelSpectralTable.cpp.

References m_norm, GModelSpectral::m_pars, m_table_pars, and GModelSpectralTablePars::size().

Referenced by copy_members(), GModelSpectralTable(), init_members(), and load().

GModelSpectralTablePar & GModelSpectralTable::table_par ( const int &  index)

Return reference to table parameter.

Parameters
[in]indexTable parameter index [0,...,size()[.
Returns
Reference to table parameter.

Definition at line 954 of file GModelSpectralTable.cpp.

References G_TABLE_PAR, m_table_pars, and GModelSpectral::size().

Referenced by eval(), and scale_energy().

const GModelSpectralTablePar & GModelSpectralTable::table_par ( const int &  index) const

Return const reference to table parameter.

Parameters
[in]indexTable parameter index [0,...,size()[.
Returns
Const reference to table parameter.

Definition at line 973 of file GModelSpectralTable.cpp.

References G_TABLE_PAR, m_table_pars, and GModelSpectral::size().

GModelSpectralTablePar & GModelSpectralTable::table_par ( const std::string &  name)

Return reference to table parameter.

Parameters
[in]nameTable parameter name.
Returns
Reference to table parameter.

Definition at line 992 of file GModelSpectralTable.cpp.

References m_table_pars, and par_index().

const GModelSpectralTablePar & GModelSpectralTable::table_par ( const std::string &  name) const

Return const reference to table parameter.

Parameters
[in]nameTable parameter name.
Returns
Const reference to table parameter.

Definition at line 1008 of file GModelSpectralTable.cpp.

References m_table_pars, and par_index().

std::string GModelSpectralTable::type ( void  ) const
inlinevirtual

Return model type.

Returns
"TableModel".

Returns the type of the spectral model.

Implements GModelSpectral.

Definition at line 178 of file GModelSpectralTable.hpp.

Referenced by write().

void GModelSpectralTable::update ( void  ) const
protected

Update cache for spectral table model computation.

Update interval vectors for function values and gradients. An update is performed in case that some of the parameter values have changed. The method sets the following cache members:

 m_npars (number of model parameters)
 m_nebins (number of energy bins in spectra)
 m_last_values (last model parameter values)
 m_lin_values (function values)

The array \({\tt m\_lin\_values}\) holds the vector \(F(E|p)\) as function of energy \(E\), computed for the current set of parameters \(p\). The computation is done using the N-dimensional linear interpolation

\[ F(E|p) = \sum_{k=1}^M \left( \prod_{i=1}^N w_x(p) \right) F_p(E) \]

where

  • \(M=2^N\) is the number of parameter combinations,
  • \(N\) is the number of table model parameters,
  • \(w_x(p) = \{w_0^l, w_0^r, w_1^l, w_1^r, ...\}\) is an array of subsequently arranged left and right weighting factors for the linear interpolation of parameters, where \(w_0^l\) and \(w_0^r\) are the left and right weighting factors for the first parameter, \(w_1^l\) and \(w_1^r\) are the left and right weighting factors for the second parameter, and so on,
  • \(x=2 k + i/2^k \mod 2\) is the index in the array of weighting factors for parameter combination \(k\) and parameter \(i\), and
  • \(F_p(E)\) are the table model spectra, computed for a grid of possible parameter values.

For each parameter \(i\), the weighting factors \(w_i^l(p)\) and \(w_i^r(p)\) are computed using

\[ w_i^r(p) = \frac{p - p_l}{p_r - p_l} \]

and \(w_i^l(p) = 1 - w_i^r(p)\), where \(p_l\) is the largest parameter value that satisfies \(p<p_l\) and \(p_r\) is the smallest parameter value that satisfies \(p>p_r\).

The method also computes the gradients

\[ \frac{\delta F(E|p)}{\delta p} = \sum_{k=1}^M \left( \frac{\delta w_{x_p}(p)}{\delta p} \prod_{i=1 \land i \neq i_p}^N w_x(p) \right) F_p(E) \]

where \(x_p = 2 k + i_p/2^k \mod 2\).

Definition at line 2078 of file GModelSpectralTable.cpp.

References ebounds(), GNodeArray::inx_left(), GNodeArray::inx_right(), m_last_values, m_lin_values, m_log_values, m_nebins, m_npars, m_spectra, m_table_pars, GNodeArray::set_value(), GModelSpectralTablePars::size(), GEbounds::size(), GOptimizerPar::value(), GNodeArray::wgt_grad_left(), GNodeArray::wgt_grad_right(), GNodeArray::wgt_left(), and GNodeArray::wgt_right().

Referenced by eflux(), eval(), flux(), and mc().

void GModelSpectralTable::update_flux ( void  ) const
protected
void GModelSpectralTable::update_mc ( const GEnergy emin,
const GEnergy emax 
) const
protected

Update MC cache.

Parameters
[in]eminMinimum energy.
[in]emaxMaximum energy.

This method sets up an array of indices and the cumulative distribution function needed for MC simulations.

Definition at line 2356 of file GModelSpectralTable.cpp.

References abs(), flux(), GNodeArray::inx_left(), log(), m_epivot, m_flux, m_gamma, m_lin_nodes, m_mc_cum, m_mc_emax, m_mc_emin, m_mc_exp, m_mc_max, m_mc_min, m_prefactor, GEnergy::MeV(), norm(), gammalib::plaw_photon_flux(), pow(), and GNodeArray::set_value().

Referenced by mc().

void GModelSpectralTable::write ( GXmlElement xml) const
virtual

Write model into XML element.

Parameters
[in]xmlXML element into which model information is written.

Writes the spectral information into an XML element. The format of the XML element is

<spectrum type="FileFunction" file="..">
  <parameter name="Normalization" scale=".." value=".." min=".." max=".." free=".."/>
</spectrum>

In addition, the method writes the model table parameters into the XML file.

Note that the function nodes will not be written since they will not be altered by any method.

Implements GModelSpectral.

Definition at line 788 of file GModelSpectralTable.cpp.

References GXmlElement::attribute(), G_WRITE, m_escale_par, m_filename, m_norm, m_table_pars, GOptimizerPar::name(), norm(), GModelSpectralTablePars::size(), type(), GModelPar::write(), gammalib::xml_check_type(), gammalib::xml_file_reduce(), and gammalib::xml_need_par().

Member Data Documentation

GEbounds GModelSpectralTable::m_ebounds
protected
std::vector<double> GModelSpectralTable::m_eflux
mutableprotected

Energy fluxes.

Definition at line 148 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eflux(), init_members(), and update_flux().

std::vector<double> GModelSpectralTable::m_epivot
mutableprotected

Power-law pivot energies.

Definition at line 146 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eflux(), flux(), init_members(), update_flux(), and update_mc().

double GModelSpectralTable::m_escale
protected

Energy scale.

Definition at line 133 of file GModelSpectralTable.hpp.

Referenced by copy_members(), has_energy_scale(), init_members(), scale_energy(), and set_energy_nodes().

std::string GModelSpectralTable::m_escale_par
protected

Energy scaling parameter.

Definition at line 132 of file GModelSpectralTable.hpp.

Referenced by copy_members(), energy_scale(), eval(), init_members(), scale_energy(), and write().

GFilename GModelSpectralTable::m_filename
mutableprotected

Filename of table.

Definition at line 131 of file GModelSpectralTable.hpp.

Referenced by copy_members(), filename(), init_members(), load(), print(), save(), and write().

std::vector<double> GModelSpectralTable::m_flux
mutableprotected

Photon fluxes.

Definition at line 147 of file GModelSpectralTable.hpp.

Referenced by copy_members(), flux(), init_members(), update_flux(), and update_mc().

std::vector<double> GModelSpectralTable::m_gamma
mutableprotected

Power-law indices.

Definition at line 145 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eflux(), flux(), init_members(), update_flux(), and update_mc().

std::vector<double> GModelSpectralTable::m_last_values
mutableprotected

Last parameter values.

Definition at line 139 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), scale_energy(), and update().

GNodeArray GModelSpectralTable::m_lin_nodes
mutableprotected

Energy nodes of function.

Definition at line 140 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eflux(), flux(), init_members(), scale_energy(), set_energy_nodes(), update_flux(), and update_mc().

GNdarray GModelSpectralTable::m_lin_values
mutableprotected

Function values and grad's.

Definition at line 142 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eval(), init_members(), set_energy_nodes(), update(), and update_flux().

double GModelSpectralTable::m_log10escale
protected

log10 of energy scale

Definition at line 134 of file GModelSpectralTable.hpp.

Referenced by copy_members(), energy_scale(), eval(), init_members(), scale_energy(), and set_energy_nodes().

GNodeArray GModelSpectralTable::m_log_nodes
mutableprotected

log10(Energy) nodes of function

Definition at line 141 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eval(), init_members(), scale_energy(), and set_energy_nodes().

GNdarray GModelSpectralTable::m_log_values
mutableprotected

log10(Function) values and grad's

Definition at line 143 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), set_energy_nodes(), and update().

std::vector<double> GModelSpectralTable::m_mc_cum
mutableprotected

Cumulative distribution.

Definition at line 151 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), mc(), and update_mc().

GEnergy GModelSpectralTable::m_mc_emax
mutableprotected

Maximum energy.

Definition at line 150 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), and update_mc().

GEnergy GModelSpectralTable::m_mc_emin
mutableprotected

Minimum energy.

Definition at line 149 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), and update_mc().

std::vector<double> GModelSpectralTable::m_mc_exp
mutableprotected

Exponent for MC.

Definition at line 154 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), mc(), and update_mc().

std::vector<double> GModelSpectralTable::m_mc_max
mutableprotected

Upper boundary for MC.

Definition at line 153 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), mc(), and update_mc().

std::vector<double> GModelSpectralTable::m_mc_min
mutableprotected

Lower boundary for MC.

Definition at line 152 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), mc(), and update_mc().

int GModelSpectralTable::m_nebins
mutableprotected

Number of energy bins.

Definition at line 138 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), and update().

GModelPar GModelSpectralTable::m_norm
protected

Normalization factor.

Definition at line 127 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eval(), GModelSpectralTable(), init_members(), norm(), read(), set_par_pointers(), and write().

int GModelSpectralTable::m_npars
mutableprotected

Number of parameters.

Definition at line 137 of file GModelSpectralTable.hpp.

Referenced by copy_members(), init_members(), and update().

std::vector<double> GModelSpectralTable::m_prefactor
mutableprotected

Power-law normalisations.

Definition at line 144 of file GModelSpectralTable.hpp.

Referenced by copy_members(), eflux(), flux(), init_members(), update_flux(), and update_mc().

GNdarray GModelSpectralTable::m_spectra
protected

The documentation for this class was generated from the following files: