GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectralBins.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectralBins.hpp - Spectral bins model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2021 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 GModelSpectralBins.hpp
23  * @brief Spectral bins model class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMODELSPECTRALBINS_HPP
28 #define GMODELSPECTRALBINS_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include "GModelSpectral.hpp"
34 #include "GModelPar.hpp"
35 #include "GEnergy.hpp"
36 
37 /* __ Forward declarations _______________________________________________ */
38 class GRan;
39 class GTime;
40 class GEbounds;
41 class GXmlElement;
42 
43 
44 /***********************************************************************//**
45  * @class GModelSpectralBins
46  *
47  * @brief Spectral bins model class
48  *
49  * This class implements spectral bins that have a constant intensity within
50  * their boundaries. A spectral bin is defined by a lower and upper energy
51  * limit, with the lower limit included and the upper limit excluded from an
52  * energy bin.
53  ***************************************************************************/
55 
56 public:
57  // Constructors and destructors
58  GModelSpectralBins(void);
60  const GEbounds& ebounds,
61  const double& index = -2.0);
62  explicit GModelSpectralBins(const GXmlElement& xml);
64  virtual ~GModelSpectralBins(void);
65 
66  // Operators
67  virtual GModelSpectralBins& operator=(const GModelSpectralBins& model);
68 
69  // Implemented pure virtual base class methods
70  virtual void clear(void);
71  virtual GModelSpectralBins* clone(void) const;
72  virtual std::string classname(void) const;
73  virtual std::string type(void) const;
74  virtual double eval(const GEnergy& srcEng,
75  const GTime& srcTime = GTime(),
76  const bool& gradients = false) const;
77  virtual double flux(const GEnergy& emin,
78  const GEnergy& emax) const;
79  virtual double eflux(const GEnergy& emin,
80  const GEnergy& emax) const;
81  virtual GEnergy mc(const GEnergy& emin,
82  const GEnergy& emax,
83  const GTime& time,
84  GRan& ran) const;
85  virtual void read(const GXmlElement& xml);
86  virtual void write(GXmlElement& xml) const;
87  virtual std::string print(const GChatter& chatter = NORMAL) const;
88 
89  // Other methods
90  int bins(void) const;
91  void append(const GEnergy& emin,
92  const GEnergy& emax,
93  const double& intensity);
94  void insert(const int& index,
95  const GEnergy& emin,
96  const GEnergy& emax,
97  const double& intensity);
98  void remove(const int& index);
99  void reserve(const int& num);
100  void extend(const GModelSpectralBins& bins);
101  double index(void) const;
102  void index(const double& index);
103  GEnergy emin(const int& index) const;
104  GEnergy emax(const int& index) const;
105  void emin(const int& index, const GEnergy& emin);
106  void emax(const int& index, const GEnergy& emax);
107  double intensity(const int& index) const;
108  void intensity(const int& index, const double& intensity);
109  double error(const int& index) const;
110 
111 protected:
112  // Protected methods
113  void init_members(void);
114  void copy_members(const GModelSpectralBins& model);
115  void free_members(void);
116  int bin_index(const GEnergy& energy) const;
117  void update_pars(void);
118  void set_cache(void) const;
119  void mc_update(const GEnergy& emin, const GEnergy& emax) const;
120 
121  // Protected members
122  GModelPar m_index; //!< Spectral index of all bins
123  std::vector<GModelPar> m_emin; //!< Lower energy limits
124  std::vector<GModelPar> m_emax; //!< Upper energy limits
125  std::vector<GModelPar> m_values; //!< Bin values
126 
127  // Evaluation cache
128  mutable std::vector<double> m_epivot; //!< Power-law pivot energies in MeV
129 
130  // MC cache
131  mutable GEnergy m_mc_emin; //!< Minimum energy
132  mutable GEnergy m_mc_emax; //!< Maximum energy
133  mutable std::vector<double> m_mc_cum; //!< Cumulative distribution
134  mutable std::vector<double> m_mc_min; //!< Lower boundary for MC
135  mutable std::vector<double> m_mc_max; //!< Upper boundary for MC
136  mutable std::vector<double> m_mc_exp; //!< Exponent for MC
137 };
138 
139 
140 /***********************************************************************//**
141  * @brief Return class name
142  *
143  * @return String containing the class name ("GModelSpectralBins").
144  ***************************************************************************/
145 inline
146 std::string GModelSpectralBins::classname(void) const
147 {
148  return ("GModelSpectralBins");
149 }
150 
151 
152 /***********************************************************************//**
153  * @brief Return model type
154  *
155  * @return "BinFunction".
156  *
157  * Returns the type of the spectral bin function model.
158  ***************************************************************************/
159 inline
160 std::string GModelSpectralBins::type(void) const
161 {
162  return "BinFunction";
163 }
164 
165 
166 /***********************************************************************//**
167  * @brief Return number of bins
168  *
169  * @return Number of bins.
170  *
171  * Returns the number of bins in the bin function model.
172  ***************************************************************************/
173 inline
175 {
176  return (int)m_values.size();
177 }
178 
179 #endif /* GMODELSPECTRALBINS_HPP */
virtual double flux(const GEnergy &emin, const GEnergy &emax) const
Returns model photon flux between emin, emax
GEnergy emax(const int &index) const
Return upper energy limit of bin.
virtual GModelSpectralBins & operator=(const GModelSpectralBins &model)
Assignment operator.
GEnergy emin(const int &index) const
Return lower energy limit of bin.
double intensity(const int &index) const
Return bin intensity.
void insert(const int &index, const GEnergy &emin, const GEnergy &emax, const double &intensity)
Insert bin.
std::vector< GModelPar > m_emin
Lower energy limits.
Energy value class definition.
virtual std::string classname(void) const
Return class name.
void init_members(void)
Initialise class members.
Abstract spectral model base class.
std::vector< double > m_mc_max
Upper boundary for MC.
XML element node class.
Definition: GXmlElement.hpp:48
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const
Evaluate function.
Random number generator class.
Definition: GRan.hpp:44
virtual std::string print(const GChatter &chatter=NORMAL) const
Print bin function information.
GEnergy m_mc_emax
Maximum energy.
virtual void read(const GXmlElement &xml)
Read model from XML element.
Spectral bins model class.
Time class.
Definition: GTime.hpp:55
virtual GModelSpectralBins * clone(void) const
Clone spectral bins model.
virtual std::string type(void) const
Return model type.
std::vector< GModelPar > m_values
Bin values.
void update_pars(void)
Update parameter mapping.
void mc_update(const GEnergy &emin, const GEnergy &emax) const
Set MC pre-computation cache.
Model parameter class interface definition.
Model parameter class.
Definition: GModelPar.hpp:87
std::vector< GModelPar > m_emax
Upper energy limits.
Energy boundaries container class.
Definition: GEbounds.hpp:60
virtual double eflux(const GEnergy &emin, const GEnergy &emax) const
Returns model energy flux between emin, emax
void reserve(const int &num)
Reserve space for bins.
GChatter
Definition: GTypemaps.hpp:33
void append(const GEnergy &emin, const GEnergy &emax, const double &intensity)
Append bin.
std::vector< double > m_epivot
Power-law pivot energies in MeV.
void extend(const GModelSpectralBins &bins)
Append bins from bin function.
virtual ~GModelSpectralBins(void)
Destructor.
void free_members(void)
Delete class members.
GModelSpectralBins(void)
Void constructor.
int bins(void) const
Return number of bins.
Abstract spectral model base class interface definition.
GEnergy m_mc_emin
Minimum energy.
std::vector< double > m_mc_cum
Cumulative distribution.
void copy_members(const GModelSpectralBins &model)
Copy class members.
void set_cache(void) const
Set cache.
GModelPar m_index
Spectral index of all bins.
double index(void) const
Return spectral power law index.
virtual void clear(void)
Clear spectral bins model.
int bin_index(const GEnergy &energy) const
Return bin index for energy.
std::vector< double > m_mc_min
Lower boundary for MC.
std::vector< double > m_mc_exp
Exponent for MC.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual GEnergy mc(const GEnergy &emin, const GEnergy &emax, const GTime &time, GRan &ran) const
Returns MC energy between [emin, emax].
double error(const int &index) const
Return intensity error of bin.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48