GammaLib  2.0.0
 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-2020 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  GModelSpectralTablePar& table_par(const int& index);
93  const GModelSpectralTablePar& table_par(const int& index) const;
94  GModelSpectralTablePar& table_par(const std::string& name);
95  const GModelSpectralTablePar& table_par(const std::string& name) const;
96  double norm(void) const;
97  void norm(const double& norm);
98  const GEbounds& ebounds(void) const;
99  void load(const GFilename& filename);
100  void save(const GFilename& filename,
101  const bool& clobber = false) const;
102  const GFilename& filename(void) const;
103 
104 protected:
105  // Protected methods
106  void init_members(void);
107  void copy_members(const GModelSpectralTable& model);
108  void free_members(void);
109  void set_par_pointers(void);
110  void set_energy_nodes(void);
111  GFitsBinTable create_par_table(void) const;
112  GFitsBinTable create_eng_table(void) const;
113  GFitsBinTable create_spec_table(void) const;
114  void load_par(const GFits& fits);
115  void load_eng(const GFits& fits);
116  void load_spec(const GFits& fits);
117  int par_index(const std::string& name) const;
118  void update(void) const;
119  void update_flux(void) const;
120  void update_mc(const GEnergy& emin, const GEnergy& emax) const;
121 
122  // Protected members
123  GModelPar m_norm; //!< Normalization factor
124  GModelSpectralTablePars m_table_pars; //!< Table model parameters
125  GNdarray m_spectra; //!< Spectra
126  GEbounds m_ebounds; //!< Energy boundaries
127  mutable GFilename m_filename; //!< Filename of table
128 
129  // Cached members used for pre-computations
130  mutable int m_npars; //!< Number of parameters
131  mutable int m_nebins; //!< Number of energy bins
132  mutable std::vector<double> m_last_values; //!< Last parameter values
133  mutable GNodeArray m_lin_nodes; //!< Energy nodes of function
134  mutable GNodeArray m_log_nodes; //!< log10(Energy) nodes of function
135  mutable GNdarray m_lin_values; //!< Function values and grad's
136  mutable GNdarray m_log_values; //!< log10(Function) values and grad's
137  mutable std::vector<double> m_prefactor; //!< Power-law normalisations
138  mutable std::vector<double> m_gamma; //!< Power-law indices
139  mutable std::vector<double> m_epivot; //!< Power-law pivot energies
140  mutable std::vector<double> m_flux; //!< Photon fluxes
141  mutable std::vector<double> m_eflux; //!< Energy fluxes
142  mutable GEnergy m_mc_emin; //!< Minimum energy
143  mutable GEnergy m_mc_emax; //!< Maximum energy
144  mutable std::vector<double> m_mc_cum; //!< Cumulative distribution
145  mutable std::vector<double> m_mc_min; //!< Lower boundary for MC
146  mutable std::vector<double> m_mc_max; //!< Upper boundary for MC
147  mutable std::vector<double> m_mc_exp; //!< Exponent for MC
148 };
149 
150 
151 /***********************************************************************//**
152  * @brief Return class name
153  *
154  * @return String containing the class name ("GModelSpectralTable").
155  ***************************************************************************/
156 inline
157 std::string GModelSpectralTable::classname(void) const
158 {
159  return ("GModelSpectralTable");
160 }
161 
162 
163 /***********************************************************************//**
164  * @brief Return model type
165  *
166  * @return "TableModel".
167  *
168  * Returns the type of the spectral model.
169  ***************************************************************************/
170 inline
171 std::string GModelSpectralTable::type(void) const
172 {
173  return ("TableModel");
174 }
175 
176 
177 /***********************************************************************//**
178  * @brief Return normalization factor
179  *
180  * @return Normalization factor.
181  *
182  * Returns the normalization factor.
183  ***************************************************************************/
184 inline
185 double GModelSpectralTable::norm(void) const
186 {
187  return (m_norm.value());
188 }
189 
190 
191 /***********************************************************************//**
192  * @brief Set normalization factor
193  *
194  * @param[in] norm Normalization factor.
195  *
196  * Sets the normalization factor.
197  ***************************************************************************/
198 inline
199 void GModelSpectralTable::norm(const double& norm)
200 {
201  m_norm.value(norm);
202  return;
203 }
204 
205 
206 /***********************************************************************//**
207  * @brief Return reference to energy boundaries
208  *
209  * @return Reference to energy boundaries.
210  *
211  * Returns a reference to energy boundaries.
212  ***************************************************************************/
213 inline
215 {
216  return (m_ebounds);
217 }
218 
219 
220 /***********************************************************************//**
221  * @brief Return file name
222  *
223  * @return Name of table file.
224  *
225  * Returns the name of the table file.
226  ***************************************************************************/
227 inline
229 {
230  return (m_filename);
231 }
232 
233 #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.
std::vector< double > m_mc_exp
Exponent for MC.
Abstract spectral model base class.
GModelPar m_norm
Normalization factor.
GModelSpectralTablePars m_table_pars
Table model parameters.
GEnergy m_mc_emax
Maximum energy.
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.
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.
const GFilename & filename(void) const
Return file name.
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 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