GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralTable.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralTable.hpp - Spectral table model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2019-2022 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GModelSpectralTable.hpp
23  * @brief Spectral table model class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMODELSPECTRALTABLE_HPP
28 #define GMODELSPECTRALTABLE_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include "GModelSpectral.hpp"
34 #include "GEbounds.hpp"
35 #include "GModelPar.hpp"
36 #include "GNdarray.hpp"
37 #include "GNodeArray.hpp"
38 #include "GFilename.hpp"
40 
41 /* __ Forward declarations _______________________________________________ */
42 class GRan;
43 class GEnergy;
44 class GTime;
45 class GFits;
46 class GXmlElement;
47 class GFitsBinTable;
48 
49 
50 /***********************************************************************//**
51  * @class GModelSpectralTable
52  *
53  * @brief Spectral table model class
54  ***************************************************************************/
56 
57 public:
58  // Constructors and destructors
59  GModelSpectralTable(void);
60  GModelSpectralTable(const GFilename& filename, const double& norm);
62  const GModelSpectralTablePars& pars,
63  const GNdarray& spectra);
64  explicit GModelSpectralTable(const GXmlElement& xml);
66  virtual ~GModelSpectralTable(void);
67 
68  // Operators
69  virtual GModelSpectralTable& operator=(const GModelSpectralTable& model);
70 
71  // Implemented pure virtual base class methods
72  virtual void clear(void);
73  virtual GModelSpectralTable* clone(void) const;
74  virtual std::string classname(void) const;
75  virtual std::string type(void) const;
76  virtual double eval(const GEnergy& srcEng,
77  const GTime& srcTime = GTime(),
78  const bool& gradients = false) const;
79  virtual double flux(const GEnergy& emin,
80  const GEnergy& emax) const;
81  virtual double eflux(const GEnergy& emin,
82  const GEnergy& emax) const;
83  virtual GEnergy mc(const GEnergy& emin,
84  const GEnergy& emax,
85  const GTime& time,
86  GRan& ran) const;
87  virtual void read(const GXmlElement& xml);
88  virtual void write(GXmlElement& xml) const;
89  virtual std::string print(const GChatter& chatter = NORMAL) const;
90 
91  // Other methods
92  int nspectra(void) const;
93  GModelSpectralTablePar& table_par(const int& index);
94  const GModelSpectralTablePar& table_par(const int& index) const;
95  GModelSpectralTablePar& table_par(const std::string& name);
96  const GModelSpectralTablePar& table_par(const std::string& name) const;
97  double norm(void) const;
98  void norm(const double& norm);
99  const GEbounds& ebounds(void) const;
100  void load(const GFilename& filename);
101  void save(const GFilename& filename,
102  const bool& clobber = false) const;
103  const GFilename& filename(void) const;
104  void energy_scale(const std::string& name);
105 
106 protected:
107  // Protected methods
108  void init_members(void);
109  void copy_members(const GModelSpectralTable& model);
110  void free_members(void);
111  void set_par_pointers(void);
112  void set_energy_nodes(void);
113  void scale_energy(void);
114  bool has_energy_scale(void) const;
115  GFitsBinTable create_par_table(void) const;
116  GFitsBinTable create_eng_table(void) const;
117  GFitsBinTable create_spec_table(void) const;
118  void load_par(const GFits& fits);
119  void load_eng(const GFits& fits);
120  void load_spec(const GFits& fits);
121  int par_index(const std::string& name) const;
122  void update(void) const;
123  void update_flux(void) const;
124  void update_mc(const GEnergy& emin, const GEnergy& emax) const;
125 
126  // Protected members
127  GModelPar m_norm; //!< Normalization factor
128  GModelSpectralTablePars m_table_pars; //!< Table model parameters
129  GNdarray m_spectra; //!< Spectra
130  GEbounds m_ebounds; //!< Energy boundaries
131  mutable GFilename m_filename; //!< Filename of table
132  std::string m_escale_par; //!< Energy scaling parameter
133  double m_escale; //!< Energy scale
134  double m_log10escale; //!< log10 of energy scale
135 
136  // Cached members used for pre-computations
137  mutable int m_npars; //!< Number of parameters
138  mutable int m_nebins; //!< Number of energy bins
139  mutable std::vector<double> m_last_values; //!< Last parameter values
140  mutable GNodeArray m_lin_nodes; //!< Energy nodes of function
141  mutable GNodeArray m_log_nodes; //!< log10(Energy) nodes of function
142  mutable GNdarray m_lin_values; //!< Function values and grad's
143  mutable GNdarray m_log_values; //!< log10(Function) values and grad's
144  mutable std::vector<double> m_prefactor; //!< Power-law normalisations
145  mutable std::vector<double> m_gamma; //!< Power-law indices
146  mutable std::vector<double> m_epivot; //!< Power-law pivot energies
147  mutable std::vector<double> m_flux; //!< Photon fluxes
148  mutable std::vector<double> m_eflux; //!< Energy fluxes
149  mutable GEnergy m_mc_emin; //!< Minimum energy
150  mutable GEnergy m_mc_emax; //!< Maximum energy
151  mutable std::vector<double> m_mc_cum; //!< Cumulative distribution
152  mutable std::vector<double> m_mc_min; //!< Lower boundary for MC
153  mutable std::vector<double> m_mc_max; //!< Upper boundary for MC
154  mutable std::vector<double> m_mc_exp; //!< Exponent for MC
155 };
156 
157 
158 /***********************************************************************//**
159  * @brief Return class name
160  *
161  * @return String containing the class name ("GModelSpectralTable").
162  ***************************************************************************/
163 inline
164 std::string GModelSpectralTable::classname(void) const
165 {
166  return ("GModelSpectralTable");
167 }
168 
169 
170 /***********************************************************************//**
171  * @brief Return model type
172  *
173  * @return "TableModel".
174  *
175  * Returns the type of the spectral model.
176  ***************************************************************************/
177 inline
178 std::string GModelSpectralTable::type(void) const
179 {
180  return ("TableModel");
181 }
182 
183 
184 /***********************************************************************//**
185  * @brief Return normalization factor
186  *
187  * @return Normalization factor.
188  *
189  * Returns the normalization factor.
190  ***************************************************************************/
191 inline
192 double GModelSpectralTable::norm(void) const
193 {
194  return (m_norm.value());
195 }
196 
197 
198 /***********************************************************************//**
199  * @brief Set normalization factor
200  *
201  * @param[in] norm Normalization factor.
202  *
203  * Sets the normalization factor.
204  ***************************************************************************/
205 inline
206 void GModelSpectralTable::norm(const double& norm)
207 {
208  m_norm.value(norm);
209  return;
210 }
211 
212 
213 /***********************************************************************//**
214  * @brief Return reference to energy boundaries
215  *
216  * @return Reference to energy boundaries.
217  *
218  * Returns a reference to energy boundaries.
219  ***************************************************************************/
220 inline
222 {
223  return (m_ebounds);
224 }
225 
226 
227 /***********************************************************************//**
228  * @brief Return file name
229  *
230  * @return Name of table file.
231  *
232  * Returns the name of the table file.
233  ***************************************************************************/
234 inline
236 {
237  return (m_filename);
238 }
239 
240 #endif /* GMODELSPECTRALTABLE_HPP */
GModelSpectralTable(void)
Void constructor.
std::vector< double > m_mc_max
Upper boundary for MC.
double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:864
Node array class.
Definition: GNodeArray.hpp:60
GFitsBinTable create_spec_table(void) const
Create SPECTRA FITS table.
void energy_scale(const std::string &name)
Set energy scale.
std::vector< double > m_mc_exp
Exponent for MC.
Abstract spectral model base class.
std::string m_escale_par
Energy scaling parameter.
GModelPar m_norm
Normalization factor.
GModelSpectralTablePars m_table_pars
Table model parameters.
GEnergy m_mc_emax
Maximum energy.
double m_escale
Energy scale.
virtual void read(const GXmlElement &xml)
Read model from XML element.
GNdarray m_lin_values
Function values and grad&#39;s.
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns MC energy between [emin, emax].
const GEbounds & ebounds(void) const
Return reference to energy boundaries.
XML element node class.
Definition: GXmlElement.hpp:48
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
Random number generator class.
Definition: GRan.hpp:44
void free_members(void)
Delete class members.
virtual ~GModelSpectralTable(void)
Destructor.
double norm(void) const
Return normalization factor.
void set_energy_nodes(void)
Set energy nodes from energy boundaries.
Time class.
Definition: GTime.hpp:55
void update_mc(const GEnergy &emin, const GEnergy &emax) const
Update MC cache.
GNodeArray m_log_nodes
log10(Energy) nodes of function
FITS file class.
Definition: GFits.hpp:63
std::vector< double > m_epivot
Power-law pivot energies.
std::vector< double > m_flux
Photon fluxes.
void init_members(void)
Initialise class members.
double m_log10escale
log10 of energy scale
Spectral table model parameter container class.
std::vector< double > m_prefactor
Power-law normalisations.
void update_flux(void) const
Update flux cache.
void set_par_pointers(void)
Set parameter pointers.
Model parameter class interface definition.
std::vector< double > m_mc_cum
Cumulative distribution.
void load(const GFilename &filename)
Load table from file.
void load_eng(const GFits &fits)
Load data from ENERGIES extension.
Model parameter class.
Definition: GModelPar.hpp:87
virtual std::string classname(void) const
Return class name.
GFitsBinTable create_eng_table(void) const
Create ENERGIES FITS table.
void copy_members(const GModelSpectralTable &model)
Copy class members.
Node array class interface definition.
GNodeArray m_lin_nodes
Energy nodes of function.
int nspectra(void) const
Return number of spectra in table model.
const GFilename & filename(void) const
Return file name.
bool has_energy_scale(void) const
Signal that energy scale was set.
Energy boundaries container class.
Definition: GEbounds.hpp:60
std::vector< double > m_last_values
Last parameter values.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate spectral table model.
Filename class.
Definition: GFilename.hpp:62
N-dimensional array class interface definition.
virtual void clear(void)
Clear table model.
GNdarray m_spectra
Spectra.
int m_nebins
Number of energy bins.
GFilename m_filename
Filename of table.
std::vector< double > m_gamma
Power-law indices.
Spectral table model class.
GEbounds m_ebounds
Energy boundaries.
GChatter
Definition: GTypemaps.hpp:33
GModelSpectralTablePar & table_par(const int &index)
Return reference to table parameter.
int par_index(const std::string &name) const
Return index for parameter name.
std::vector< double > m_mc_min
Lower boundary for MC.
N-dimensional array class.
Definition: GNdarray.hpp:44
void load_par(const GFits &fits)
Load data from PARAMETERS extension.
virtual GModelSpectralTable * clone(void) const
Clone table model.
Abstract spectral model base class interface definition.
virtual std::string type(void) const
Return model type.
Spectral table model parameter container class definition.
GNdarray m_log_values
log10(Function) values and grad&#39;s
int m_npars
Number of parameters.
double value(void) const
Return parameter value.
FITS binary table class.
std::vector< double > m_eflux
Energy fluxes.
Energy boundaries class interface definition.
void scale_energy(void)
Scale energy.
void save(const GFilename &filename, const bool &clobber=false) const
Save table into file.
GEnergy m_mc_emin
Minimum energy.
GFitsBinTable create_par_table(void) const
Create PARAMETERS FITS table.
void update(void) const
Update cache for spectral table model computation.
virtual GModelSpectralTable & operator=(const GModelSpectralTable &model)
Assignment operator.
Spectral table model parameter class.
virtual void write(GXmlElement &xml) const
Write model into XML element.
void load_spec(const GFits &fits)
Load data from SPECTRA extension.
Filename class interface definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print table model information.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48