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

Spectral function model class. More...

#include <GModelSpectralFunc.hpp>

Inheritance diagram for GModelSpectralFunc:
GModelSpectral GBase

Public Member Functions

 GModelSpectralFunc (void)
 Void constructor. More...
 
 GModelSpectralFunc (const GFilename &filename, const double &norm)
 File constructor. More...
 
 GModelSpectralFunc (const GXmlElement &xml)
 XML constructor. More...
 
 GModelSpectralFunc (const GModelSpectralFunc &model)
 Copy constructor. More...
 
virtual ~GModelSpectralFunc (void)
 Destructor. More...
 
virtual GModelSpectralFuncoperator= (const GModelSpectralFunc &model)
 Assignment operator. More...
 
virtual void clear (void)
 Clear file function. More...
 
virtual GModelSpectralFuncclone (void) const
 Clone file function. 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 function. 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 file function information. More...
 
const GFilenamefilename (void) const
 Return file name. More...
 
void filename (const GFilename &filename)
 Loads nodes from node file and set filename. More...
 
double norm (void) const
 Return normalization factor. More...
 
void norm (const double &norm)
 Set normalization factor. 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 GModelSpectralFunc &model)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
void load_nodes (const GFilename &filename)
 Load nodes from file. More...
 
void set_cache (void) const
 Set pre-computation cache. More...
 
void mc_update (const GEnergy &emin, const GEnergy &emax) const
 Set MC pre-computation 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...
 
GNodeArray m_lin_nodes
 Energy nodes of function. More...
 
GNodeArray m_log_nodes
 lof10(Energy) nodes of function More...
 
std::vector< double > m_lin_values
 Function values at nodes. More...
 
std::vector< double > m_log_values
 log10(Function) values at nodes More...
 
GFilename m_filename
 Name of file function. 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 function model class.

This class implements an arbitrary function as the spectral model component. The model is defined by

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

where

  • \({\tt m\_norm}\) is the normalization of the function.

Definition at line 60 of file GModelSpectralFunc.hpp.

Constructor & Destructor Documentation

GModelSpectralFunc::GModelSpectralFunc ( void  )

Void constructor.

Definition at line 69 of file GModelSpectralFunc.cpp.

References init_members().

Referenced by clone().

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

File constructor.

Parameters
[in]filenameFile name of nodes.
[in]normNormalization factor.

Constructs spectral file function model from a list of nodes that is found in the specified ASCII file. See the load_nodes() method for more information about the expected structure of the file.

Definition at line 89 of file GModelSpectralFunc.cpp.

References init_members(), load_nodes(), m_norm, and GOptimizerPar::value().

GModelSpectralFunc::GModelSpectralFunc ( const GXmlElement xml)
explicit

XML constructor.

Parameters
[in]xmlXML element.

Constructs spectral file function 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 116 of file GModelSpectralFunc.cpp.

References init_members(), and read().

GModelSpectralFunc::GModelSpectralFunc ( const GModelSpectralFunc model)

Copy constructor.

Parameters
[in]modelFile function model.

Definition at line 135 of file GModelSpectralFunc.cpp.

References copy_members(), and init_members().

GModelSpectralFunc::~GModelSpectralFunc ( void  )
virtual

Destructor.

Definition at line 152 of file GModelSpectralFunc.cpp.

References free_members().

Member Function Documentation

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

Return class name.

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

Implements GModelSpectral.

Definition at line 140 of file GModelSpectralFunc.hpp.

void GModelSpectralFunc::clear ( void  )
virtual

Clear file function.

Implements GModelSpectral.

Definition at line 207 of file GModelSpectralFunc.cpp.

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

GModelSpectralFunc * GModelSpectralFunc::clone ( void  ) const
virtual

Clone file function.

Implements GModelSpectral.

Definition at line 225 of file GModelSpectralFunc.cpp.

References GModelSpectralFunc().

void GModelSpectralFunc::copy_members ( const GModelSpectralFunc model)
protected

Copy class members.

Parameters
[in]modelSpectral function model.

Definition at line 750 of file GModelSpectralFunc.cpp.

References m_eflux, m_epivot, m_filename, m_flux, m_gamma, m_lin_nodes, m_lin_values, m_log_nodes, m_log_values, m_mc_cum, m_mc_emax, m_mc_emin, m_mc_exp, m_mc_max, m_mc_min, m_norm, GModelSpectral::m_pars, and m_prefactor.

Referenced by GModelSpectralFunc(), and operator=().

double GModelSpectralFunc::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 404 of file GModelSpectralFunc.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(), and GNodeArray::set_value().

Referenced by set_cache().

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

Evaluate function.

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} \]

where

  • \({\tt m\_norm}\) is the normalization factor.

If the gradients flag is true the method will also compute the partial derivatives of the model with respect to the parameters using

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

Implements GModelSpectral.

Definition at line 257 of file GModelSpectralFunc.cpp.

References GOptimizerPar::factor_gradient(), GNodeArray::interpolate(), GOptimizerPar::is_free(), gammalib::is_infinite(), gammalib::is_notanumber(), GEnergy::log10MeV(), m_log_nodes, m_log_values, m_norm, norm(), pow(), GOptimizerPar::scale(), and GOptimizerPar::value().

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

Return file name.

Returns
Name of node file.

Returns the name of the file function node file.

Definition at line 197 of file GModelSpectralFunc.hpp.

References m_filename.

Referenced by load_nodes().

void GModelSpectralFunc::filename ( const GFilename filename)
inline

Loads nodes from node file and set filename.

Parameters
[in]filenameNode file name.

Loads the nodes from a file function node file and sets the filename.

Definition at line 211 of file GModelSpectralFunc.hpp.

References load_nodes().

double GModelSpectralFunc::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 316 of file GModelSpectralFunc.cpp.

References GNodeArray::inx_left(), m_epivot, m_flux, m_gamma, m_lin_nodes, m_norm, m_prefactor, GEnergy::MeV(), gammalib::plaw_photon_flux(), GNodeArray::set_value(), and GOptimizerPar::value().

Referenced by mc_update(), and set_cache().

void GModelSpectralFunc::free_members ( void  )
protected

Delete class members.

Definition at line 787 of file GModelSpectralFunc.cpp.

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

void GModelSpectralFunc::load_nodes ( const GFilename filename)
protected

Load nodes from file.

Parameters
[in]filenameFile name.
Exceptions
GException::file_function_dataFile contains less than 2 nodes
GException::file_function_columnsFile contains less than 2 columns
GException::file_function_valueFile contains invalid value

The file function is stored as a column separated value table (CSV) in an ASCII file with (at least) 2 columns. The first column specifies the energy in MeV while the second column specifies the intensity at this energy in units of ph/cm2/s/MeV. The node energies and values will be stored both linearly and as log10. The log10 storing requires that node energies and node values are positive. Also, at least 2 nodes and 2 columns are required in the file function.

Definition at line 815 of file GModelSpectralFunc.cpp.

References GNodeArray::append(), GNodeArray::clear(), filename(), G_LOAD_NODES, GFilename::is_empty(), log10(), m_filename, m_lin_nodes, m_lin_values, m_log_nodes, m_log_values, GCsv::ncols(), GCsv::nrows(), GCsv::real(), set_cache(), and GFilename::url().

Referenced by filename(), GModelSpectralFunc(), and read().

GEnergy GModelSpectralFunc::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.
Exceptions
GException::erange_invalidEnergy range is invalid (emin < emax required).

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

Implements GModelSpectral.

Definition at line 493 of file GModelSpectralFunc.cpp.

References exp(), G_MC, log(), m_mc_cum, m_mc_exp, m_mc_max, m_mc_min, mc_update(), GEnergy::MeV(), and GRan::uniform().

void GModelSpectralFunc::mc_update ( const GEnergy emin,
const GEnergy emax 
) const
protected

Set MC pre-computation 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 958 of file GModelSpectralFunc.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().

double GModelSpectralFunc::norm ( void  ) const
inline

Return normalization factor.

Returns
Normalization factor.

Returns the normalization factor.

Definition at line 168 of file GModelSpectralFunc.hpp.

References m_norm, and GOptimizerPar::value().

Referenced by eflux(), eval(), and mc_update().

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

Set normalization factor.

Parameters
[in]normNormalization factor.

Sets the normalization factor.

Definition at line 182 of file GModelSpectralFunc.hpp.

References m_norm, and GOptimizerPar::value().

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

Assignment operator.

Parameters
[in]modelFile function model.
Returns
File function model.

Definition at line 174 of file GModelSpectralFunc.cpp.

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

std::string GModelSpectralFunc::print ( const GChatter chatter = NORMAL) const
virtual

Print file function information.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing file function information.

Implements GModelSpectral.

Definition at line 661 of file GModelSpectralFunc.cpp.

References m_eflux, m_epivot, m_filename, m_flux, m_gamma, m_lin_nodes, GModelSpectral::m_pars, m_prefactor, gammalib::parformat(), SILENT, GNodeArray::size(), GModelSpectral::size(), gammalib::str(), and GFilename::url().

void GModelSpectralFunc::read ( const GXmlElement xml)
virtual

Read model from XML element.

Parameters
[in]xmlXML element containing power law model information.
Exceptions
GException::model_invalid_parnumInvalid number of model parameters found in XML element.
GException::model_invalid_parnamesInvalid model parameter name found in XML element.

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

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

Implements GModelSpectral.

Definition at line 561 of file GModelSpectralFunc.cpp.

References GXmlElement::attribute(), GXmlNode::element(), GXmlNode::elements(), G_READ, load_nodes(), m_norm, GModelPar::read(), and gammalib::xml_file_expand().

Referenced by GModelSpectralFunc().

void GModelSpectralFunc::set_cache ( void  ) const
protected
std::string GModelSpectralFunc::type ( void  ) const
inlinevirtual

Return model type.

Returns
"FileFunction".

Returns the type of the spectral function model.

Implements GModelSpectral.

Definition at line 154 of file GModelSpectralFunc.hpp.

void GModelSpectralFunc::write ( GXmlElement xml) const
virtual

Write model into XML element.

Parameters
[in]xmlXML element into which model information is written.
Exceptions
GException::model_invalid_spectralExisting XML element is not of type "FileFunction"
GException::model_invalid_parnumInvalid number of model parameters or nodes found in XML element.
GException::model_invalid_parnamesInvalid model parameter names found in XML element.

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>

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

Implements GModelSpectral.

Definition at line 611 of file GModelSpectralFunc.cpp.

References GXmlNode::append(), GXmlElement::attribute(), GXmlNode::element(), GXmlNode::elements(), G_WRITE, m_filename, m_norm, GModelPar::write(), and gammalib::xml_file_reduce().

Member Data Documentation

std::vector<double> GModelSpectralFunc::m_eflux
mutableprotected

Energy fluxes.

Definition at line 122 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eflux(), print(), and set_cache().

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

Power-law pivot energies.

Definition at line 120 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eflux(), flux(), mc_update(), print(), and set_cache().

GFilename GModelSpectralFunc::m_filename
protected

Name of file function.

Definition at line 115 of file GModelSpectralFunc.hpp.

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

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

Photon fluxes.

Definition at line 121 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), flux(), mc_update(), print(), and set_cache().

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

Power-law indices.

Definition at line 119 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eflux(), flux(), mc_update(), print(), and set_cache().

GNodeArray GModelSpectralFunc::m_lin_nodes
mutableprotected

Energy nodes of function.

Definition at line 111 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eflux(), flux(), init_members(), load_nodes(), mc_update(), print(), and set_cache().

std::vector<double> GModelSpectralFunc::m_lin_values
protected

Function values at nodes.

Definition at line 113 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), init_members(), load_nodes(), and set_cache().

GNodeArray GModelSpectralFunc::m_log_nodes
mutableprotected

lof10(Energy) nodes of function

Definition at line 112 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eval(), init_members(), and load_nodes().

std::vector<double> GModelSpectralFunc::m_log_values
protected

log10(Function) values at nodes

Definition at line 114 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eval(), init_members(), and load_nodes().

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

Cumulative distribution.

Definition at line 127 of file GModelSpectralFunc.hpp.

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

GEnergy GModelSpectralFunc::m_mc_emax
mutableprotected

Maximum energy.

Definition at line 126 of file GModelSpectralFunc.hpp.

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

GEnergy GModelSpectralFunc::m_mc_emin
mutableprotected

Minimum energy.

Definition at line 125 of file GModelSpectralFunc.hpp.

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

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

Exponent for MC.

Definition at line 130 of file GModelSpectralFunc.hpp.

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

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

Upper boundary for MC.

Definition at line 129 of file GModelSpectralFunc.hpp.

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

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

Lower boundary for MC.

Definition at line 128 of file GModelSpectralFunc.hpp.

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

GModelPar GModelSpectralFunc::m_norm
protected

Normalization factor.

Definition at line 110 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eval(), flux(), GModelSpectralFunc(), init_members(), norm(), read(), and write().

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

Power-law normalisations.

Definition at line 118 of file GModelSpectralFunc.hpp.

Referenced by copy_members(), eflux(), flux(), mc_update(), print(), and set_cache().


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