GammaLib 2.0.0
Loading...
Searching...
No Matches
GCOMModelDRM.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCOMModelDRM.cpp - COMPTEL DRM 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 GCOMModelDRM.cpp
23 * @brief COMPTEL DRM model class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <typeinfo>
32#include "GException.hpp"
33#include "GTools.hpp"
34#include "GModelRegistry.hpp"
35#include "GCOMModelDRM.hpp"
36#include "GCOMObservation.hpp"
37#include "GCOMEventBin.hpp"
38
39/* __ Constants __________________________________________________________ */
40
41/* __ Globals ____________________________________________________________ */
43const GModelRegistry g_com_drm_registry(&g_com_drm_seed);
44
45/* __ Method name definitions ____________________________________________ */
46#define G_EVAL "GCOMModelDRM::eval(GEvent&, GObservation&, bool&)"
47#define G_NRED "GCOMModelDRM::npred(GEnergy&, GTime&, GObservation&)"
48#define G_MC "GCOMModelDRM::mc(GObservation&, GRan&)"
49#define G_READ "GCOMModelDRM::read(GXmlElement&)"
50#define G_WRITE "GCOMModelDRM::write(GXmlElement&)"
51
52/* __ Macros _____________________________________________________________ */
53
54/* __ Coding definitions _________________________________________________ */
55
56/* __ Debug definitions __________________________________________________ */
57
58
59/*==========================================================================
60 = =
61 = Constructors/destructors =
62 = =
63 ==========================================================================*/
64
65/***********************************************************************//**
66 * @brief Void constructor
67 *
68 * Constructs an empty COMPTEL DRM model.
69 ***************************************************************************/
71{
72 // Initialise members
74
75 // Return
76 return;
77}
78
79
80/***********************************************************************//**
81 * @brief XML constructor
82 *
83 * @param[in] xml XML element.
84 *
85 * Constructs a COMPTEL DRM model from the information that is found in an
86 * XML element. Please refer to the method GCOMModelDRM::read to learn more
87 * about the information that is expected in the XML element.
88 ***************************************************************************/
90{
91 // Initialise members
93
94 // Read XML
95 read(xml);
96
97 // Return
98 return;
99}
100
101
102/***********************************************************************//**
103 * @brief Copy constructor
104 *
105 * @param[in] model COMPTEL DRM model.
106 *
107 * Constructs a COMPTEL DRM model by copying information from an existing
108 * model. Note that the copy is a deep copy, so the original object can be
109 * destroyed after the copy without any loss of information.
110 ***************************************************************************/
112{
113 // Initialise private members for clean destruction
114 init_members();
115
116 // Copy members
117 copy_members(model);
118
119 // Return
120 return;
121}
122
123
124/***********************************************************************//**
125 * @brief Destructor
126 *
127 * Destroys a COMPTEL DRM model.
128 ***************************************************************************/
130{
131 // Free members
132 free_members();
133
134 // Return
135 return;
136}
137
138
139/*==========================================================================
140 = =
141 = Operators =
142 = =
143 ==========================================================================*/
144
145/***********************************************************************//**
146 * @brief Assignment operator
147 *
148 * @param[in] model COMPTEL DRM model.
149 * @return COMPTEL DRM model
150 *
151 * Assigns the information from a COMPTEL DRM model to the actual object.
152 * Note that a deep copy of the information is performed, so the original
153 * object can be destroyed after the assignment without any loss of
154 * information.
155 ***************************************************************************/
157{
158 // Execute only if object is not identical
159 if (this != &model) {
160
161 // Copy base class members
162 this->GModelData::operator=(model);
163
164 // Free members
165 free_members();
166
167 // Initialise members
168 init_members();
169
170 // Copy members
171 copy_members(model);
172
173 } // endif: object was not identical
174
175 // Return
176 return *this;
177}
178
179
180/*==========================================================================
181 = =
182 = Public methods =
183 = =
184 ==========================================================================*/
185
186/***********************************************************************//**
187 * @brief Clear instance
188 *
189 * Resets the object to a clean initial state. All information that resided
190 * in the object will be lost.
191 ***************************************************************************/
193{
194 // Free class members (base and derived classes, derived class first)
195 free_members();
197 this->GModel::free_members();
198
199 // Initialise members
200 this->GModel::init_members();
202 init_members();
203
204 // Return
205 return;
206}
207
208
209/***********************************************************************//**
210 * @brief Clone instance
211 *
212 * @return Pointer to deep copy of COMPTEL DRM model.
213 *
214 * Clone a COMPTEL DRM model. Cloning performs a deep copy of the
215 * information, so the original object can be destroyed after cloning without
216 * any loss of information.
217 ***************************************************************************/
219{
220 return new GCOMModelDRM(*this);
221}
222
223
224/***********************************************************************//**
225 * @brief Evaluate function
226 *
227 * @param[in] event Observed event.
228 * @param[in] obs Observation.
229 * @param[in] gradients Compute gradients?
230 * @return Background model value.
231 *
232 * @exception GException::invalid_argument
233 * Observation is not a COMPTEL observation.
234 * Event is not a COMPTEL event bin.
235 *
236 * Evaluates the COMPTEL DRM model.
237 ***************************************************************************/
238double GCOMModelDRM::eval(const GEvent& event,
239 const GObservation& obs,
240 const bool& gradients) const
241{
242 // Extract COMPTEL observation
243 const GCOMObservation* observation = dynamic_cast<const GCOMObservation*>(&obs);
244 if (observation == NULL) {
245 std::string cls = std::string(typeid(&obs).name());
246 std::string msg = "Observation of type \""+cls+"\" is not a COMPTEL "
247 "observations. Please specify a COMPTEL observation "
248 "as argument.";
250 }
251
252 // Extract COMPTEL event bin
253 const GCOMEventBin* bin = dynamic_cast<const GCOMEventBin*>(&event);
254 if (bin == NULL) {
255 std::string cls = std::string(typeid(&event).name());
256 std::string msg = "Event of type \""+cls+"\" is not a COMPTEL event. "
257 "Please specify a COMPTEL event as argument.";
259 }
260
261 // Initialise value
262 double value = 0.0;
263
264 // Optionally initialise gradients
265 if (gradients) {
267 }
268
269 // Get bin index
270 int index = bin->index();
271
272 // Get bin size.
273 double size = bin->size();
274
275 // Continue only if bin size is positive
276 if (size > 0.0) {
277
278 // Get DRM model value
279 value = m_drm.map().pixels()[index] / size;
280
281 // Optionally compute gradients
282 if (gradients) {
283
284 // Compute partial derivative
285 double grad = (m_norm.is_free()) ? value * m_norm.scale() : 0.0;
286
287 // Set gradient
289
290 } // endif: gradient computation was requested
291
292 // Compute model value
293 value *= m_norm.value();
294
295 // Compile option: Check for NaN/Inf
296 #if defined(G_NAN_CHECK)
297 if (gammalib::is_notanumber(value) || gammalib::is_infinite(value)) {
298 std::cout << "*** ERROR: GCOMModelDRM::eval";
299 std::cout << "(index=" << index << "):";
300 std::cout << " NaN/Inf encountered";
301 std::cout << " (value=" << value;
302 std::cout << ")" << std::endl;
303 }
304 #endif
305
306 } // endif: binsize was positive
307
308 // Return
309 return value;
310}
311
312
313/***********************************************************************//**
314 * @brief Return spatially integrated DRM model
315 *
316 * @param[in] obsEng Measured event energy.
317 * @param[in] obsTime Measured event time.
318 * @param[in] obs Observation.
319 * @return Spatially integrated DRM model.
320 *
321 * @exception GException::feature_not_implemented
322 * Method is not implemented.
323 *
324 * Spatially integrates the DRM model for a given measured event energy and
325 * event time.
326 *
327 * @todo Implement method.
328 ***************************************************************************/
329double GCOMModelDRM::npred(const GEnergy& obsEng,
330 const GTime& obsTime,
331 const GObservation& obs) const
332{
333 // Initialise result
334 double npred = 0.0;
335
336 // Method is not implemented
337 std::string msg = "Spatial integration of DRM model is not implemented.";
339
340 // Return
341 return npred;
342}
343
344
345/***********************************************************************//**
346 * @brief Return simulated list of events
347 *
348 * @param[in] obs Observation.
349 * @param[in] ran Random number generator.
350 * @return COMPTEL event cube.
351 *
352 * @exception GException::feature_not_implemented
353 * Method is not implemented.
354 *
355 * Draws a sample of events from the COMPTEL DRM model using a Monte Carlo
356 * simulation.
357 *
358 * @todo Implement method.
359 ***************************************************************************/
361{
362 // Initialise new event cube
363 GCOMEventCube* cube = new GCOMEventCube;
364
365 // Method is not implemented
366 std::string msg = "Monte Carlo simulation of DRM model is not implemented.";
368
369 // Return
370 return cube;
371}
372
373
374/***********************************************************************//**
375 * @brief Read model from XML element
376 *
377 * @param[in] xml XML element.
378 *
379 * Read the COMPTEL DRM model from an XML element.
380 *
381 * The model has the following XML file syntax:
382 *
383 * <source name="Model" type="DRM" file="drm.fits" instrument="COM">
384 * <parameter name="Normalization" ../>
385 * </source>
386 ***************************************************************************/
388{
389 // Verify number of model parameters
391
392 // Get parameter pointers
394
395 // Read parameters
396 m_norm.read(*norm);
397
398 // Read filename
400
401 // Load DRM
403
404 // Read model attributes
405 read_attributes(xml);
406
407 // Return
408 return;
409}
410
411
412/***********************************************************************//**
413 * @brief Write model into XML element
414 *
415 * @param[in] xml XML element.
416 *
417 * Write the COMPTEL DRM model into an XML element.
418 *
419 * The model has the following XML file syntax:
420 *
421 * <source name="Model" type="DRM" file="drm.fits" instrument="COM">
422 * <parameter name="Normalization" ../>
423 * </source>
424 ***************************************************************************/
426{
427 // Initialise pointer on source
428 GXmlElement* src = NULL;
429
430 // Search corresponding source
431 int n = xml.elements("source");
432 for (int k = 0; k < n; ++k) {
433 GXmlElement* element = xml.element("source", k);
434 if (element->attribute("name") == name()) {
435 src = element;
436 break;
437 }
438 }
439
440 // If no source with corresponding name was found then append one.
441 // Set also the type and the instrument.
442 if (src == NULL) {
443 src = xml.append("source");
444 src->attribute("name", name());
445 src->attribute("type", type());
446 if (instruments().length() > 0) {
447 src->attribute("instrument", instruments());
448 }
449 }
450
451 // Verify model type
453
454 // Get XML parameters
456
457 // Write parameters
458 m_norm.write(*norm);
459
460 // Set DRM file name
462
463 // Write model attributes
464 write_attributes(*src);
465
466 // Return
467 return;
468}
469
470
471/***********************************************************************//**
472 * @brief Print model information
473 *
474 * @param[in] chatter Chattiness.
475 * @return String containing model information.
476 ***************************************************************************/
477std::string GCOMModelDRM::print(const GChatter& chatter) const
478{
479 // Initialise result string
480 std::string result;
481
482 // Continue only if chatter is not silent
483 if (chatter != SILENT) {
484
485 // Append header
486 result.append("=== GCOMModelDRM ===");
487
488 // Append attributes
489 result.append("\n"+print_attributes());
490
491 // Append parameters
492 result.append("\n"+gammalib::parformat("DRM file"));
493 result.append(m_filename);
494 result.append("\n"+gammalib::parformat("Number of parameters"));
495 result.append(gammalib::str(size()));
496 for (int i = 0; i < size(); ++i) {
497 result.append("\n"+m_pars[i]->print(chatter));
498 }
499
500 } // endif: chatter was not silent
501
502 // Return result
503 return result;
504}
505
506
507/*==========================================================================
508 = =
509 = Private methods =
510 = =
511 ==========================================================================*/
512
513/***********************************************************************//**
514 * @brief Initialise class members
515 ***************************************************************************/
517{
518 // Initialise Value
519 m_norm.clear();
520 m_norm.name("Normalization");
521 m_norm.value(1.0);
522 m_norm.scale(1.0);
523 m_norm.range(0.0, 1.0e6);
524 m_norm.gradient(0.0);
525 m_norm.free();
526 m_norm.has_grad(true);
527
528 // Set parameter pointer(s)
529 m_pars.clear();
530 m_pars.push_back(&m_norm);
531
532 // Initialise other members
533 m_drm.clear();
535
536 // Return
537 return;
538}
539
540
541/***********************************************************************//**
542 * @brief Copy class members
543 *
544 * @param[in] model Model.
545 ***************************************************************************/
547{
548 // Copy members
549 m_norm = model.m_norm;
550 m_drm = model.m_drm;
551 m_filename = model.m_filename;
552
553 // Set parameter pointer(s)
554 m_pars.clear();
555 m_pars.push_back(&m_norm);
556
557 // Return
558 return;
559}
560
561
562/***********************************************************************//**
563 * @brief Delete class members
564 ***************************************************************************/
566{
567 // Return
568 return;
569}
#define G_WRITE
#define G_READ
COMPTEL event bin class interface definition.
#define G_NRED
const GCOMModelDRM g_com_drm_seed
COMPTEL DRM model class interface definition.
COMPTEL observation class interface definition.
Exception handler interface definition.
#define G_EVAL
Model registry class definition.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
double norm(const GVector &vector)
Computes vector norm.
Definition GVector.cpp:864
virtual void clear(void)
Clear COMPTEL Data Space.
Definition GCOMDri.cpp:228
void load(const GFilename &filename)
Load COMPTEL Data Space from DRI FITS file.
Definition GCOMDri.cpp:893
const GSkyMap & map(void) const
Return DRI sky map.
Definition GCOMDri.hpp:267
COMPTEL event bin class.
const int & index(void) const
Return bin index.
virtual double size(void) const
Return size of event bin.
COMPTEL event bin container class.
COMPTEL DRM model class.
GCOMModelDRM(void)
Void constructor.
virtual double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const
Evaluate function.
virtual void read(const GXmlElement &xml)
Read model from XML element.
void copy_members(const GCOMModelDRM &model)
Copy class members.
virtual GCOMEventCube * mc(const GObservation &obs, GRan &ran) const
Return simulated list of events.
virtual double npred(const GEnergy &obsEng, const GTime &obsTime, const GObservation &obs) const
Return spatially integrated DRM model.
virtual void clear(void)
Clear instance.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print model information.
virtual void write(GXmlElement &xml) const
Write model into XML element.
GModelPar m_norm
Normalisation.
void free_members(void)
Delete class members.
virtual GCOMModelDRM & operator=(const GCOMModelDRM &model)
Assignment operator.
virtual ~GCOMModelDRM(void)
Destructor.
void init_members(void)
Initialise class members.
GFilename m_filename
Name of DRM FITS file.
GCOMDri m_drm
Model.
virtual std::string type(void) const
Return model type.
virtual GCOMModelDRM * clone(void) const
Clone instance.
Interface class for COMPTEL observations.
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
Abstract interface for the event classes.
Definition GEvent.hpp:71
void clear(void)
Clear file name.
Abstract data model class.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
virtual GModelData & operator=(const GModelData &model)
Assignment operator.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Interface definition for the model registry class.
std::vector< GModelPar * > m_pars
Pointers to all model parameters.
Definition GModel.hpp:178
void write_attributes(GXmlElement &xml) const
Write model attributes.
Definition GModel.cpp:723
void free_members(void)
Delete class members.
Definition GModel.cpp:672
void init_members(void)
Initialise class members.
Definition GModel.cpp:622
std::string print_attributes(void) const
Print model attributes.
Definition GModel.cpp:770
int size(void) const
Return number of parameters in model.
Definition GModel.hpp:233
std::string instruments(void) const
Returns instruments to which model applies.
Definition GModel.cpp:310
void read_attributes(const GXmlElement &xml)
Read model attributes.
Definition GModel.cpp:684
const std::string & name(void) const
Return parameter name.
Definition GModel.hpp:261
Abstract observation base class.
bool is_free(void) const
Signal if parameter is free.
void free(void)
Free a parameter.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
void range(const double &min, const double &max)
Set minimum and maximum parameter boundaries.
const double & factor_gradient(void) const
Return parameter factor gradient.
double gradient(void) const
Return parameter gradient.
void clear(void)
Clear parameter.
double value(void) const
Return parameter value.
const std::string & name(void) const
Return parameter name.
Random number generator class.
Definition GRan.hpp:44
const double * pixels(void) const
Returns pointer to pixel data.
Definition GSkyMap.hpp:475
Time class.
Definition GTime.hpp:55
XML element node class.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
virtual GXmlNode * append(const GXmlNode &node)
Append XML child node.
Definition GXmlNode.cpp:287
virtual GXmlElement * element(const int &index)
Return pointer to GXMLElement child.
Definition GXmlNode.cpp:640
virtual int elements(void) const
Return number of GXMLElement children of node.
Definition GXmlNode.cpp:586
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1143
bool is_infinite(const double &x)
Signal if argument is infinite.
Definition GTools.hpp:184
bool is_notanumber(const double &x)
Signal if argument is not a number.
Definition GTools.hpp:201
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:489
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1689
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1637
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition GTools.cpp:1946
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Definition GTools.cpp:1777
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition GTools.cpp:1889
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1819