GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpectral.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpectral.cpp - Abstract spectral model base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2016 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 GModelSpectral.cpp
23  * @brief Abstract spectral model base class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GModelSpectral.hpp"
33 #include "GModelPar.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_ACCESS "GModelSpectral::operator[](std::string&)"
37 #define G_AT "GModelPar& GModelSpectral::at(int&)"
38 
39 /* __ Macros _____________________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 /* __ Debug definitions __________________________________________________ */
44 
45 
46 /*==========================================================================
47  = =
48  = Constructors/destructors =
49  = =
50  ==========================================================================*/
51 
52 /***********************************************************************//**
53  * @brief Void constructor
54  ***************************************************************************/
56 {
57  // Initialise members
58  init_members();
59 
60  // Return
61  return;
62 }
63 
64 
65 /***********************************************************************//**
66  * @brief Copy constructor
67  *
68  * @param[in] model Spectral model.
69  ***************************************************************************/
71 {
72  // Initialise members
73  init_members();
74 
75  // Copy members
76  copy_members(model);
77 
78  // Return
79  return;
80 }
81 
82 
83 /***********************************************************************//**
84  * @brief Destructor
85  ***************************************************************************/
87 {
88  // Free members
89  free_members();
90 
91  // Return
92  return;
93 }
94 
95 
96 /*==========================================================================
97  = =
98  = Operators =
99  = =
100  ==========================================================================*/
101 
102 /***********************************************************************//**
103  * @brief Assignment operator
104  *
105  * @param[in] model Spectral model.
106  * @return Spectral model.
107  ***************************************************************************/
109 {
110  // Execute only if object is not identical
111  if (this != &model) {
112 
113  // Free members
114  free_members();
115 
116  // Initialise private members for clean destruction
117  init_members();
118 
119  // Copy members
120  copy_members(model);
121 
122  } // endif: object was not identical
123 
124  // Return
125  return *this;
126 }
127 
128 
129 /***********************************************************************//**
130  * @brief Returns reference to model parameter
131  *
132  * @param[in] name Parameter name.
133  *
134  * @exception GException::par_not_found
135  * Parameter with specified name not found in container.
136  ***************************************************************************/
137 GModelPar& GModelSpectral::operator[](const std::string& name)
138 {
139  // Get parameter index
140  int index = 0;
141  for (; index < size(); ++index) {
142  if (m_pars[index]->name() == name) {
143  break;
144  }
145  }
146 
147  // Throw exception if parameter name was not found
148  if (index >= size()) {
149  throw GException::par_not_found(G_ACCESS, name);
150  }
151 
152  // Return reference
153  return *(m_pars[index]);
154 }
155 
156 
157 /***********************************************************************//**
158  * @brief Returns reference to model parameter (const version)
159  *
160  * @param[in] name Parameter name.
161  *
162  * @exception GException::par_not_found
163  * Parameter with specified name not found in container.
164  ***************************************************************************/
165 const GModelPar& GModelSpectral::operator[](const std::string& name) const
166 {
167  // Get parameter index
168  int index = 0;
169  for (; index < size(); ++index) {
170  if (m_pars[index]->name() == name) {
171  break;
172  }
173  }
174 
175  // Throw exception if parameter name was not found
176  if (index >= size()) {
177  throw GException::par_not_found(G_ACCESS, name);
178  }
179 
180  // Return reference
181  return *(m_pars[index]);
182 }
183 
184 
185 /*==========================================================================
186  = =
187  = Public methods =
188  = =
189  ==========================================================================*/
190 
191 /***********************************************************************//**
192  * @brief Returns model parameter
193  *
194  * @param[in] index Parameter index [0,...,size()-1].
195  * @return Model parameter.
196  *
197  * @exception GException::out_of_range
198  * Parameter index is out of range.
199  *
200  * Returns model parameter with @p index range checking.
201  ***************************************************************************/
202 GModelPar& GModelSpectral::at(const int& index)
203 {
204  // Compile option: raise exception if index is out of range
205  if (index < 0 || index >= size()) {
206  throw GException::out_of_range(G_AT, index, 0, size()-1);
207  }
208 
209  // Return reference
210  return *(m_pars[index]);
211 }
212 
213 
214 /***********************************************************************//**
215  * @brief Returns model parameter (const version)
216  *
217  * @param[in] index Parameter index [0,...,size()-1].
218  * @return Model parameter.
219  *
220  * @exception GException::out_of_range
221  * Parameter index is out of range.
222  *
223  * Returns model parameter with @p index range checking.
224  ***************************************************************************/
225 const GModelPar& GModelSpectral::at(const int& index) const
226 {
227  // Compile option: raise exception if index is out of range
228  if (index < 0 || index >= size()) {
229  throw GException::out_of_range(G_AT, index, 0, size()-1);
230  }
231 
232  // Return reference
233  return *(m_pars[index]);
234 }
235 
236 
237 /***********************************************************************//**
238  * @brief Checks if parameter name exists
239  *
240  * @param[in] name Parameter name.
241  * @return True if parameter with specified @p name exists.
242  *
243  * Searches all parameter names for a match with the specified @p name. If
244  * the specified name has been found, true is returned.
245  ***************************************************************************/
246 bool GModelSpectral::has_par(const std::string& name) const
247 {
248  // Default found flag to false
249  bool found = false;
250 
251  // Search for parameter name
252  for (int i = 0; i < size(); ++i) {
253  if (m_pars[i]->name() == name) {
254  found = true;
255  break;
256  }
257  }
258 
259  // Return
260  return found;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Autoscale parameters
266  *
267  * Sets the scale factors for all parameters so that the values are unity.
268  ***************************************************************************/
270 {
271  // Loop over all parameters
272  for (int i = 0; i < m_pars.size(); ++i) {
273  if (m_pars[i] != NULL) {
274  m_pars[i]->autoscale();
275  }
276  }
277 
278  // Return
279  return;
280 }
281 
282 
283 /*==========================================================================
284  = =
285  = Private methods =
286  = =
287  ==========================================================================*/
288 
289 /***********************************************************************//**
290  * @brief Initialise class members
291  ***************************************************************************/
293 {
294  // Initialise members
295  m_pars.clear();
296 
297  // Return
298  return;
299 }
300 
301 
302 /***********************************************************************//**
303  * @brief Copy class members
304  *
305  * @param[in] model Spectral model.
306  ***************************************************************************/
308 {
309  // Copy members
310  m_pars = model.m_pars;
311 
312  // Return
313  return;
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Delete class members
319  ***************************************************************************/
321 {
322  // Return
323  return;
324 }
Abstract spectral model base class.
int size(void) const
Return number of parameters.
virtual GModelSpectral & operator=(const GModelSpectral &model)
Assignment operator.
std::vector< GModelPar * > m_pars
Parameter pointers.
GModelSpectral(void)
Void constructor.
virtual ~GModelSpectral(void)
Destructor.
#define G_AT
Model parameter class interface definition.
Model parameter class.
Definition: GModelPar.hpp:87
GModelPar & at(const int &index)
Returns model parameter.
bool has_par(const std::string &name) const
Checks if parameter name exists.
void copy_members(const GModelSpectral &model)
Copy class members.
#define G_ACCESS
void autoscale(void)
Autoscale parameters.
virtual GModelPar & operator[](const int &index)
Returns model parameter.
Abstract spectral model base class interface definition.
void init_members(void)
Initialise class members.
Exception handler interface definition.
void free_members(void)
Delete class members.