GammaLib 2.0.0
Loading...
Searching...
No Matches
GModelSpectralTablePars.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelSpectralTablePars.cpp - Spectral table model par container class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2019-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 GModelSpectralTablePars.cpp
23 * @brief Spectral table model parameter container class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <cmath>
32#include "GException.hpp"
33#include "GTools.hpp"
35
36/* __ Constants __________________________________________________________ */
37
38/* __ Globals ____________________________________________________________ */
39
40/* __ Method name definitions ____________________________________________ */
41#define G_ACCESS "GModelSpectralTablePars::operator[](std::string&)"
42#define G_AT "GModelSpectralTablePars::at(int&)"
43#define G_SET1 "GModelSpectralTablePars::set(int&, GModelSpectralTablePar&)"
44#define G_SET2 "GModelSpectralTablePars::set(std::string&, "\
45 "GModelSpectralTablePar&)"
46#define G_APPEND "GModelSpectralTablePars::append(GModelSpectralTablePar&)"
47#define G_INSERT1 "GModelSpectralTablePars::insert(int&, "\
48 "GModelSpectralTablePar&)"
49#define G_INSERT2 "GModelSpectralTablePars::insert(std::string&, "\
50 "GModelSpectralTablePar&)"
51#define G_REMOVE1 "GModelSpectralTablePars::remove(int&)"
52#define G_REMOVE2 "GModelSpectralTablePars::remove(const std::string& name)"
53#define G_EXTEND "GModelSpectralTablePars::extend(GModelSpectralTablePars&)"
54
55/* __ Macros _____________________________________________________________ */
56
57/* __ Coding definitions _________________________________________________ */
58
59/* __ Debug definitions __________________________________________________ */
60
61
62/*==========================================================================
63 = =
64 = Constructors/destructors =
65 = =
66 ==========================================================================*/
67
68/***********************************************************************//**
69 * @brief Void constructor
70 ***************************************************************************/
72{
73 // Initialise members
75
76 // Return
77 return;
78}
79
80
81/***********************************************************************//**
82 * @brief Copy constructor
83 *
84 * @param[in] pars Table model parameters.
85 ***************************************************************************/
87{
88 // Initialise members
90
91 // Copy members
92 copy_members(pars);
93
94 // Return
95 return;
96}
97
98
99/***********************************************************************//**
100 * @brief Destructor
101 ***************************************************************************/
103{
104 // Free members
105 free_members();
106
107 // Return
108 return;
109}
110
111
112/*==========================================================================
113 = =
114 = Operators =
115 = =
116 ==========================================================================*/
117
118/***********************************************************************//**
119 * @brief Assignment operator
120 *
121 * @param[in] pars Table model parameters.
122 * @return Table model parameters.
123 ***************************************************************************/
125{
126 // Execute only if object is not identical
127 if (this != &pars) {
128
129 // Free members
130 free_members();
131
132 // Initialise members
133 init_members();
134
135 // Copy members
136 copy_members(pars);
137
138 } // endif: object was not identical
139
140 // Return
141 return *this;
142}
143
144
145/***********************************************************************//**
146 * @brief Return pointer to table model parameter (const version)
147 *
148 * @param[in] name Table model parameter name.
149 *
150 * @exception GException::invalid_argument
151 * Table model parameter with specified name not found in
152 * container.
153 *
154 * Returns a const pointer to the table model parameter with the specified
155 * @p name.
156 ***************************************************************************/
158{
159 // Get model index
160 int index = get_index(name);
161
162 // Throw exception if model name was not found
163 if (index == -1) {
164 std::string msg = "Table model parameter \""+name+"\" not found. "
165 "Please specify a valid parameter name.";
167 }
168
169 // Return pointer
170 return m_pars[index];
171}
172
173
174/*==========================================================================
175 = =
176 = Public methods =
177 = =
178 ==========================================================================*/
179
180/***********************************************************************//**
181 * @brief Clear table model parameters
182***************************************************************************/
184{
185 // Free class members (base and derived classes, derived class first)
186 free_members();
187
188 // Initialise members
189 init_members();
190
191 // Return
192 return;
193}
194
195
196/***********************************************************************//**
197 * @brief Clone table model parameters
198***************************************************************************/
200{
201 // Clone table model parameters
202 return new GModelSpectralTablePars(*this);
203}
204
205
206/***********************************************************************//**
207 * @brief Return pointer to table model parameter
208 *
209 * @param[in] index Parameter index [0,...,size()-1].
210 *
211 * @exception GException::out_of_range
212 * Table model parameter index is out of range.
213 *
214 * Returns a pointer to the table model parameter with the specified
215 * @p index.
216 ***************************************************************************/
218{
219 // Compile option: raise an exception if index is out of range
220 #if defined(G_RANGE_CHECK)
221 if (index < 0 || index >= size()) {
222 throw GException::out_of_range(G_AT, "Parameter index", index, size());
223 }
224 #endif
225
226 // Return pointer
227 return m_pars[index];
228}
229
230
231/***********************************************************************//**
232 * @brief Return pointer to table model parameter (const version)
233 *
234 * @param[in] index Parameter index [0,...,size()-1].
235 *
236 * @exception GException::out_of_range
237 * Table model parameter index is out of range.
238 *
239 * Returns a pointer to the table model parameter with the specified
240 * @p index.
241 ***************************************************************************/
243{
244 // Compile option: raise an exception if index is out of range
245 #if defined(G_RANGE_CHECK)
246 if (index < 0 || index >= size()) {
247 throw GException::out_of_range(G_AT, "Parameter index", index, size());
248 }
249 #endif
250
251 // Return pointer
252 return m_pars[index];
253}
254
255
256/***********************************************************************//**
257 * @brief Set table model parameter in container
258 *
259 * @param[in] index Table model parameter index [0,...,size()[.
260 * @param[in] par Table model parameter.
261 * @return Pointer to deep copy of table model parameter.
262 *
263 * @exception GException::out_of_range
264 * Table model parameter index is out of range.
265 * @exception GException::invalid_value
266 * Name of table model parameter exists already in container.
267 *
268 * Set table model parameter at position @p index in the container. The
269 * method will overwrite the table model parameter that existed in the
270 * specified slot. The method will store a deep copy of the table model
271 * parameter in the container.
272 ***************************************************************************/
274 const GModelSpectralTablePar& par)
275{
276 // Compile option: raise exception if index is out of range
277 #if defined(G_RANGE_CHECK)
278 if (index < 0 || index >= size()) {
279 throw GException::out_of_range(G_SET1, "Table model parameter index",
280 index, size());
281 }
282 #endif
283
284 // Check if a parameter with specified name does not yet exist
285 int inx = get_index(par.par().name());
286 if (inx != -1 && inx != index) {
287 std::string msg =
288 "Attempt to set table model parameter with name \""+
289 par.par().name()+"\" in table model parameter container at index "+
290 gammalib::str(index)+", but a table model parameter with the "
291 "same name exists already at index "+gammalib::str(inx)+
292 " in the container. Every table model parameter in the container "
293 "needs a unique name.";
295 }
296
297 // Free existing table model parameter only if it differs from current
298 // table model parameter. This prevents unintential deallocation of the
299 // argument
300 if ((m_pars[index] != NULL) && (m_pars[index] != &par)) {
301 delete m_pars[index];
302 }
303
304 // Assign new table model parameter by cloning
305 m_pars[index] = par.clone();
306
307 // Return pointer to table model parameter
308 return m_pars[index];
309}
310
311
312/***********************************************************************//**
313 * @brief Set table model parameter in container
314 *
315 * @param[in] name Table model parameter name.
316 * @param[in] par Table model parameter.
317 * @return Pointer to deep copy of table model parameter.
318 *
319 * @exception GException::invalid_argument
320 * Table model parameter with @p name not found.
321 *
322 * Set table model parameter at the position of the parameter with the
323 * specified @p name in the container. The method will overwrite the table
324 * model parameter with the specified @p name. The method will store a deep
325 * copy of the table model parameter in the container.
326 ***************************************************************************/
328 const GModelSpectralTablePar& par)
329{
330 // Get parameter index
331 int index = get_index(name);
332
333 // Throw exception if model name was not found
334 if (index == -1) {
335 std::string msg = "Table model parameter \""+name+"\" not found. "
336 "Please specify a valid parameter name.";
338 }
339
340 // Set parameter and return pointer to table model parameter
341 return (set(index, par));
342}
343
344
345/***********************************************************************//**
346 * @brief Append table model parameter to container
347 *
348 * @param[in] par Table model parameter.
349 * @return Pointer to deep copy of table model parameter.
350 *
351 * @exception GException::invalid_argument
352 * Name of table model parameter exists already in container.
353 *
354 * Appends table model parameter to the container by making a deep copy of
355 * the table model parameter and storing its pointer.
356 ***************************************************************************/
358{
359 // Check if a model with specified name does not yet exist
360 int inx = get_index(par.par().name());
361 if (inx != -1) {
362 std::string msg =
363 "Attempt to append table model parameter with name \""+
364 par.par().name()+"\" to table model parameter container, but a "
365 "table model parameter with the same name exists already at "
366 "index "+gammalib::str(inx)+" in the container. Every table "
367 "model parameter in the container needs a unique name.";
369 }
370
371 // Create deep copy of table model parameter
372 GModelSpectralTablePar* ptr = par.clone();
373
374 // Append deep copy of table model parameter
375 m_pars.push_back(ptr);
376
377 // Return pointer to table model parameter
378 return ptr;
379}
380
381
382/***********************************************************************//**
383 * @brief Insert table model parameter into container
384 *
385 * @param[in] index Table model parameter index [0,...,size()[.
386 * @param[in] par Table model parameter.
387 * @return Pointer to deep copy of table model parameter.
388 *
389 * @exception GException::out_of_range
390 * Model index is out of range.
391 * @exception GException::invalid_value
392 * Name of model exists already in container.
393 *
394 * Inserts a @p model into the container before the model with the specified
395 * @p index.
396 ***************************************************************************/
398 const GModelSpectralTablePar& par)
399{
400 // Compile option: raise exception if index is out of range
401 #if defined(G_RANGE_CHECK)
402 if (is_empty()) {
403 if (index > 0) {
404 throw GException::out_of_range(G_INSERT1, "Table model parameter index",
405 index, size());
406 }
407 }
408 else {
409 if (index < 0 || index >= size()) {
410 throw GException::out_of_range(G_INSERT1, "Table model parameter index",
411 index, size());
412 }
413 }
414 #endif
415
416 // Check if a model with specified name does not yet exist
417 int inx = get_index(par.par().name());
418 if (inx != -1) {
419 std::string msg =
420 "Attempt to insert table model parameter with name \""+
421 par.par().name()+"\" in table model parameter container before "
422 "index "+gammalib::str(index)+", but table model parameter with "
423 "the same name exists already at index "+
424 gammalib::str(inx)+" in the container. Every model in the table "
425 "model parameter container needs a unique name.";
427 }
428
429 // Create deep copy of table model parameter
430 GModelSpectralTablePar* ptr = par.clone();
431
432 // Inserts deep copy of table model parameter
433 m_pars.insert(m_pars.begin()+index, ptr);
434
435 // Return pointer to table model parameter
436 return ptr;
437}
438
439
440/***********************************************************************//**
441 * @brief Insert table model parameter into container
442 *
443 * @param[in] name Table model parameter name.
444 * @param[in] par Table model parameter.
445 * @return Pointer to deep copy of table model parameter.
446 *
447 * @exception GException::invalid_argument
448 * Table model parameter with @p name not found.
449 *
450 * Inserts a table model parameter into the container before the table model
451 * parameter with the specified @p name.
452 ***************************************************************************/
454 const GModelSpectralTablePar& par)
455{
456 // Get parameter index
457 int index = get_index(name);
458
459 // Throw exception if model name was not found
460 if (index == -1) {
461 std::string msg = "Table model parameter \""+name+"\" not found. "
462 "Please specify a valid parameter name.";
464 }
465
466 // Insert table model parameter and return pointer to it
467 return (insert(index, par));
468}
469
470
471/***********************************************************************//**
472 * @brief Remove table model parameter from container
473 *
474 * @param[in] index Table model parameter index [0,...,size()[.
475 *
476 * @exception GException::out_of_range
477 * Table model parameter index is out of range.
478 *
479 * Remove table model parameter of specified @p index from container.
480 ***************************************************************************/
482{
483 // Compile option: raise exception if index is out of range
484 #if defined(G_RANGE_CHECK)
485 if (index < 0 || index >= size()) {
486 throw GException::out_of_range(G_REMOVE1, "Table model parameter index",
487 index, size());
488 }
489 #endif
490
491 // Delete model
492 delete m_pars[index];
493
494 // Erase model component from container
495 m_pars.erase(m_pars.begin() + index);
496
497 // Return
498 return;
499}
500
501
502/***********************************************************************//**
503 * @brief Remove table model parameter from container
504 *
505 * @param[in] name Table model parameter name.
506 *
507 * @exception GException::invalid_argument
508 * Table model parameter with @p name not found.
509 *
510 * Remove table model parameter with @p name from container.
511 ***************************************************************************/
512void GModelSpectralTablePars::remove(const std::string& name)
513{
514 // Get parameter index
515 int index = get_index(name);
516
517 // Throw exception if model name was not found
518 if (index == -1) {
519 std::string msg = "Table model parameter \""+name+"\" not found. "
520 "Please specify a valid parameter name.";
522 }
523
524 // Remove model
525 remove(index);
526
527 // Return
528 return;
529}
530
531
532/***********************************************************************//**
533 * @brief Append table model parameter container
534 *
535 * @param[in] pars Table model parameter container.
536 *
537 * Append table model parameter container to the container.
538 ***************************************************************************/
540{
541 // Do nothing if table model parameter container is empty
542 if (!pars.is_empty()) {
543
544 // Get size. Note that we extract the size first to avoid an
545 // endless loop that arises when a container is appended to
546 // itself.
547 int num = pars.size();
548
549 // Reserve enough space
550 reserve(size() + num);
551
552 // Loop over all table model parameters and append pointers to deep
553 // copies
554 for (int i = 0; i < num; ++i) {
555
556 // Check if table model parameter name does not yet exist
557 int inx = get_index(pars[i]->par().name());
558 if (inx != -1) {
559 std::string msg =
560 "Attempt to append table model parameter with name \""+
561 pars[i]->par().name()+"\" to table model parameter "
562 "container, but a table model parameter with the same "
563 "name exists already at index "+gammalib::str(inx)+" "
564 "in the container. Every table model parameter in the "
565 "container needs a unique name.";
567 }
568
569 // Append model to container
570 m_pars.push_back(pars[i]->clone());
571
572 } // endfor: looped over all models
573
574 } // endif: model container was not empty
575
576 // Return
577 return;
578}
579
580
581/***********************************************************************//**
582 * @brief Signals if table model parameter exists
583 *
584 * @param[in] name Table model parameter name.
585 * @return True if table model parameter with specified @p name exists.
586 *
587 * Searches all table model parameters for a match with the specified
588 * @p name. If the specified name has been found, true is returned.
589 ***************************************************************************/
590bool GModelSpectralTablePars::contains(const std::string& name) const
591{
592 // Get model index
593 int index = get_index(name);
594
595 // Return
596 return (index != -1);
597}
598
599
600/***********************************************************************//**
601 * @brief Print table model parameters
602 *
603 * @param[in] chatter Chattiness.
604 * @return String containing file table model parameters information.
605 ***************************************************************************/
606std::string GModelSpectralTablePars::print(const GChatter& chatter) const
607{
608 // Initialise result string
609 std::string result;
610
611 // Continue only if chatter is not silent
612 if (chatter != SILENT) {
613
614 // Append header
615 result.append("=== GModelSpectralTablePars ===");
616
617 // Append information
618 result.append("\n"+gammalib::parformat("Number of parameters"));
619 result.append(gammalib::str(size()));
620 for (int i = 0; i < size(); ++i) {
621 result.append("\n"+m_pars[i]->print(chatter));
622 }
623
624 } // endif: chatter was not silent
625
626 // Return result
627 return result;
628}
629
630
631/*==========================================================================
632 = =
633 = Private methods =
634 = =
635 ==========================================================================*/
636
637/***********************************************************************//**
638 * @brief Initialise class members
639 ***************************************************************************/
641{
642 // Initialise members
643 m_pars.clear();
644
645 // Return
646 return;
647}
648
649
650/***********************************************************************//**
651 * @brief Copy class members
652 *
653 * @param[in] pars Table model parameters.
654 ***************************************************************************/
656{
657 // Copy parameters
658 m_pars.clear();
659 for (int i = 0; i < pars.m_pars.size(); ++i) {
660 m_pars.push_back((pars.m_pars[i]->clone()));
661 }
662
663 // Return
664 return;
665}
666
667
668/***********************************************************************//**
669 * @brief Delete class members
670 ***************************************************************************/
672{
673 // Free models
674 for (int i = 0; i < m_pars.size(); ++i) {
675 delete m_pars[i];
676 m_pars[i] = NULL;
677 }
678
679 // Return
680 return;
681}
682
683
684/***********************************************************************//**
685 * @brief Return parameter index by name
686 *
687 * @param[in] name Parameter name.
688 * @return Parameter index (-1 if parameter name has not been found)
689 *
690 * Returns parameter index based on the specified @p name. If no parameter
691 * with the specified @p name is found the method returns -1.
692 ***************************************************************************/
693int GModelSpectralTablePars::get_index(const std::string& name) const
694{
695 // Initialise index
696 int index = -1;
697
698 // Search model with specified name
699 for (int i = 0; i < size(); ++i) {
700 if (m_pars[i]->par().name() == name) {
701 index = i;
702 break;
703 }
704 }
705
706 // Return index
707 return index;
708}
#define G_AT
#define G_REMOVE1
#define G_INSERT2
#define G_APPEND
#define G_INSERT1
#define G_REMOVE2
#define G_EXTEND
#define G_ACCESS
Exception handler interface definition.
#define G_SET1
Definition GFits.cpp:57
#define G_SET2
Definition GFits.cpp:58
Spectral table model parameter container class definition.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
Spectral table model parameter class.
GModelSpectralTablePar * clone(void) const
Clone table model parameter.
GModelPar & par(void)
Return reference to table model parameter.
Spectral table model parameter container class.
void extend(const GModelSpectralTablePars &pars)
Append table model parameter container.
void copy_members(const GModelSpectralTablePars &model)
Copy class members.
GModelSpectralTablePar * set(const int &index, const GModelSpectralTablePar &par)
Set table model parameter in container.
std::vector< GModelSpectralTablePar * > m_pars
List of parameters.
GModelSpectralTablePar * append(const GModelSpectralTablePar &par)
Append table model parameter to container.
void clear(void)
Clear table model parameters.
bool contains(const std::string &name) const
Signals if table model parameter exists.
GModelSpectralTablePars * clone(void) const
Clone table model parameters.
int size(void) const
Return number of table model parameters in container.
bool is_empty(void) const
Signals if there are no table model parameters in container.
GModelSpectralTablePars & operator=(const GModelSpectralTablePars &pars)
Assignment operator.
void reserve(const int &num)
Reserves space for table model parameters in container.
GModelSpectralTablePar * at(const int &index)
Return pointer to table model parameter.
GModelSpectralTablePars(void)
Void constructor.
std::string print(const GChatter &chatter=NORMAL) const
Print table model parameters.
void remove(const int &index)
Remove table model parameter from container.
virtual ~GModelSpectralTablePars(void)
Destructor.
GModelSpectralTablePar * insert(const int &index, const GModelSpectralTablePar &par)
Insert table model parameter into container.
GModelSpectralTablePar * operator[](const int &index)
Return pointer to table model parameter.
int get_index(const std::string &name) const
Return parameter index by name.
void free_members(void)
Delete class members.
void init_members(void)
Initialise class members.
const std::string & name(void) const
Return parameter name.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1143
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:489