GModel.cpp
24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/***************************************************************************
* GModel.cpp - Model class *
* ----------------------------------------------------------------------- *
* copyright (C) 2009-2010 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/**
* @file GModel.cpp
* @brief GModel class implementation.
* @author J. Knodlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "GTools.hpp"
#include "GException.hpp"
#include "GModel.hpp"
#include "GModelTemporalConst.hpp"
#include "GModelSpatialPtsrc.hpp"
#include "GModelSpatialConst.hpp"
#include "GModelSpatialCube.hpp"
#include "GModelSpectralPlaw.hpp"
#include "GModelSpectralPlaw2.hpp"
#include "GModelSpectralExpPlaw.hpp"
#include "GModelSpectralFunc.hpp"
#include "GModelSpectralConst.hpp"
#include "GEvent.hpp"
#include "GPointing.hpp"
#include "GResponse.hpp"
#include "GObservation.hpp"
/* __ Method name definitions ____________________________________________ */
#define G_ACCESS "GModel::operator() (int)"
#define G_XML_SPATIAL "GModel::xml_spatial(GXmlElement&)"
#define G_XML_SPECTRAL "GModel::xml_spectral(GXmlElement&)"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
***************************************************************************/
GModel::GModel(void)
{
// Initialise private members for clean destruction
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] model Model from which the instance should be built.
***************************************************************************/
GModel::GModel(const GModel& model)
{
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(model);
// Return
return;
}
/***********************************************************************//**
* @brief Construct from spatial and spectral models
*
* @param[in] spatial Spatial model.
* @param[in] spectral Spectral model.
***************************************************************************/
GModel::GModel(const GModelSpatial& spatial, const GModelSpectral& spectral)
{
// Initialise private members for clean destruction
init_members();
// Allocate constant
GModelTemporalConst temporal;
// Clone spatial and spectral models
m_spatial = spatial.clone();
m_spectral = spectral.clone();
m_temporal = temporal.clone();
// Set parameter pointers
set_pointers();
// Return
return;
}
/***********************************************************************//**
* @brief Construct from spatial and spectral XML elements
*
* @param[in] spatial Spatial XML element.
* @param[in] spectral Spectral XML element.
***************************************************************************/
GModel::GModel(const GXmlElement& spatial, const GXmlElement& spectral)
{
// Initialise private members for clean destruction
init_members();
// Allocate constant
GModelTemporalConst temporal;
// Clone spatial and spectral models
m_spatial = xml_spatial(spatial);
m_spectral = xml_spectral(spectral);
m_temporal = temporal.clone();
// Set parameter pointers
set_pointers();
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GModel::~GModel(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Returns reference to model parameter
*
* @param[in] index Parameter index [0,...,size()-1].
*
* @exception GException::out_of_range
* Parameter index is out of range.
***************************************************************************/
GModelPar& GModel::operator() (int index)
{
// Compile option: raise exception if index is out of range
#if defined(G_RANGE_CHECK)
if (index < 0 || index >= m_npars)
throw GException::out_of_range(G_ACCESS, index, 0, m_npars-1);
#endif
// Return pointer
return *(m_par[index]);
}
/***********************************************************************//**
* @brief Returns reference to model parameter (const version)
*
* @param[in] index Parameter index [0,...,size()-1].
*
* @exception GException::out_of_range
* Parameter index is out of range.
***************************************************************************/
const GModelPar& GModel::operator() (int index) const
{
// Compile option: raise exception if index is out of range
#if defined(G_RANGE_CHECK)
if (index < 0 || index >= m_npars)
throw GException::out_of_range(G_ACCESS, index, 0, m_npars-1);
#endif
// Return pointer
return *(m_par[index]);
}
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] model Model which should be assigned.
***************************************************************************/
GModel& GModel::operator= (const GModel& model)
{
// Execute only if object is not identical
if (this != &model) {
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(model);
} // endif: object was not identical
// Return
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Clear instance
*
* This method properly resets the instance to an initial state.
***************************************************************************/
void GModel::clear(void)
{
// Free class members
free_members();
// Initialise members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Clone instance
***************************************************************************/
GModel* GModel::clone(void) const
{
return new GModel(*this);
}
/***********************************************************************//**
* @brief Returns value of source model
*
* @param[in] srcDir True photon direction.
* @param[in] srcEng True photon energy.
* @param[in] srcTime True photon arrival time.
*
* This method evaluates the factorized source model for a given true photon
* arrival direction, true photon energy and true photon arrival time.
***************************************************************************/
double GModel::value(const GSkyDir& srcDir, const GEnergy& srcEng,
const GTime& srcTime)
{
// Initialise source model
double source = 1.0;
// Evaluate source model
if (m_spatial != NULL) source *= m_spatial->eval(srcDir);
if (m_spectral != NULL) source *= m_spectral->eval(srcEng);
if (m_temporal != NULL) source *= m_temporal->eval(srcTime);
// Return
return source;
}
/***********************************************************************//**
* @brief Returns parameter gradients of source model
*
* @param[in] srcDir True photon direction.
* @param[in] srcEng True photon energy.
* @param[in] srcTime True photon arrival time.
*
* This method returns the parameter gradients of the factorized source model
* for a given true photon arrival direction, true photon energy and true
* photon arrival time.
***************************************************************************/
GVector GModel::gradients(const GSkyDir& srcDir, const GEnergy& srcEng,
const GTime& srcTime)
{
// Evaluate source model gradients
if (m_spatial != NULL) m_spatial->eval_gradients(srcDir);
if (m_spectral != NULL) m_spectral->eval_gradients(srcEng);
if (m_temporal != NULL) m_temporal->eval_gradients(srcTime);
// Set vector of gradients
GVector gradients;
if (size() > 0) {
gradients = GVector(size());
for (int i = 0; i < size(); ++i)
gradients(i) = m_par[i]->gradient();
}
// Return gradients
return gradients;
}
/***********************************************************************//**
* @brief Evaluate model
*
* @param[in] event Observed event.
* @param[in] obs Observation.
*
* This method evaluates the source model for a given event within a given
* observation.
***************************************************************************/
double GModel::eval(const GEvent& event, const GObservation& obs)
{
// Evaluate function
double value = temporal(event, obs, false);
// Return
return value;
}
/***********************************************************************//**
* @brief Evaluate model and parameter gradients
*
* @param[in] event Observed event.
* @param[in] obs Observation.
*
* This method evaluates the source model and model gradients for a given
* event within a given observation.
***************************************************************************/
double GModel::eval_gradients(const GEvent& event, const GObservation& obs)
{
// Evaluate function
double value = temporal(event, obs, true);
// Return
return value;
}
/***********************************************************************//**
* @brief Verifies if model is valid for a given instrument
*
* @param[in] name Instrument name for which validity is to be checked.
*
* @todo Implement method (dummy method always returns true)
***************************************************************************/
bool GModel::isvalid(const std::string& name) const
{
// Return
return true;
}
/***********************************************************************//**
* @brief Print model information
***************************************************************************/
std::string GModel::print(void) const
{
// Initialise result string
std::string result;
// Determine number of parameters per type
int n_spatial = (m_spatial != NULL) ? m_spatial->size() : 0;
int n_spectral = (m_spectral != NULL) ? m_spectral->size() : 0;
int n_temporal = (m_temporal != NULL) ? m_temporal->size() : 0;
// Append header
result.append("=== GModel ===\n");
result.append(parformat("Name")+name()+"\n");
result.append(parformat("Number of parameters")+str(size())+"\n");
result.append(parformat("Number of spatial par's")+str(n_spatial));
int i;
for (i = 0; i < n_spatial; ++i) {
result.append("\n"+parformat("Parameter"));
result.append((*this)(i).print());
}
result.append(parformat("Number of spectral par's")+str(n_spectral));
for (; i < n_spatial+n_spectral; ++i) {
result.append("\n"+parformat("Parameter"));
result.append((*this)(i).print());
}
result.append(parformat("Number of temporal par's")+str(n_temporal));
for (; i < n_spatial+n_spectral+n_temporal; ++i) {
result.append("\n"+parformat("Parameter"));
result.append((*this)(i).print());
}
// Return result
return result;
}
/*==========================================================================
= =
= Private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GModel::init_members(void)
{
// Initialise members
m_name.clear();
m_npars = 0;
m_par = NULL;
m_spatial = NULL;
m_spectral = NULL;
m_temporal = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] model GModel members which should be copied.
***************************************************************************/
void GModel::copy_members(const GModel& model)
{
// Copy attributes
m_name = model.m_name;
m_npars = model.m_npars;
// Clone spatial and spectral models
m_spatial = (model.m_spatial != NULL) ? model.m_spatial->clone() : NULL;
m_spectral = (model.m_spectral != NULL) ? model.m_spectral->clone() : NULL;
m_temporal = (model.m_temporal != NULL) ? model.m_temporal->clone() : NULL;
// Set parameter pointers
set_pointers();
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GModel::free_members(void)
{
// Free memory
if (m_par != NULL) delete [] m_par;
if (m_spatial != NULL) delete m_spatial;
if (m_spectral != NULL) delete m_spectral;
if (m_temporal != NULL) delete m_temporal;
// Signal free pointers
m_par = NULL;
m_spatial = NULL;
m_spectral = NULL;
m_temporal = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Set parameter pointers
*
* Gathers all parameter pointers from the model.
***************************************************************************/
void GModel::set_pointers(void)
{
// Delete old pointers (if they exist)
if (m_par != NULL) delete [] m_par;
m_par = NULL;
// Determine the number of parameters
int n_spatial = (m_spatial != NULL) ? m_spatial->size() : 0;
int n_spectral = (m_spectral != NULL) ? m_spectral->size() : 0;
int n_temporal = (m_temporal != NULL) ? m_temporal->size() : 0;
m_npars = n_spatial + n_spectral + n_temporal;
// Continue only if there are parameters
if (m_npars > 0) {
// Allocate parameter pointers
m_par = new GModelPar*[m_npars];
// Initialise pointer on pointer array
GModelPar** ptr = m_par;
// Gather spatial parameter pointers
for (int i = 0; i < n_spatial; ++i)
*ptr++ = &((*m_spatial)(i));
// Gather spectral parameters
for (int i = 0; i < n_spectral; ++i)
*ptr++ = &((*m_spectral)(i));
// Gather temporal parameters
for (int i = 0; i < n_temporal; ++i)
*ptr++ = &((*m_temporal)(i));
}
// Return
return;
}
/***********************************************************************//**
* @brief Construct spatial model from XML element
*
* @param[in] spatial XML element containing spatial model information.
*
* @exception GException::model_invalid_spatial
* Invalid spatial model type encountered.
***************************************************************************/
GModelSpatial* GModel::xml_spatial(const GXmlElement& spatial) const
{
// Initialize spatial pointer
GModelSpatial* ptr = NULL;
// Get spatial model type
std::string type = spatial.attribute("type");
// Type=SkyDirFunction
if (type == "SkyDirFunction")
ptr = new GModelSpatialPtsrc(spatial);
// Type=SpatialMap
else if (type == "SpatialMap")
throw GException::model_invalid_spatial(G_XML_SPATIAL, type);
// Type=MapCubeFunction
else if (type == "MapCubeFunction")
ptr = new GModelSpatialCube(spatial);
// Type=ConstantValue
else if (type == "ConstantValue")
ptr = new GModelSpatialConst(spatial);
// Unknown spatial model type
else
throw GException::model_invalid_spatial(G_XML_SPATIAL, type);
// Return pointer
return ptr;
}
/***********************************************************************//**
* @brief Construct spectral model from XML element
*
* @param[in] spectral XML element containing spectral model information.
*
* @exception GException::model_invalid_spectral
* Invalid spatial model type encountered.
***************************************************************************/
GModelSpectral* GModel::xml_spectral(const GXmlElement& spectral) const
{
// Initialize spectral pointer
GModelSpectral* ptr = NULL;
// Get spatial model type
std::string type = spectral.attribute("type");
// Type=PowerLaw
if (type == "PowerLaw")
ptr = new GModelSpectralPlaw(spectral);
// Type=PowerLaw2
else if (type == "PowerLaw2")
ptr = new GModelSpectralPlaw2(spectral);
// Type=ExpCutoff
else if (type == "ExpCutoff")
ptr = new GModelSpectralExpPlaw(spectral);
// Type=FileFunction
else if (type == "FileFunction")
ptr = new GModelSpectralFunc(spectral);
// Type=ConstantValue
else if (type == "ConstantValue")
ptr = new GModelSpectralConst(spectral);
// Unknown spectral model type
else
throw GException::model_invalid_spectral(G_XML_SPECTRAL, type);
// Return pointer
return ptr;
}
/***********************************************************************//**
* @brief Returns spatial model component
*
* @param[in] event Observed event.
* @param[in] srcEng True photon energy.
* @param[in] srcTime True photon arrival time.
* @param[in] obs Observation.
* @parem[in] grad Compute also model gradients? (default=false)
*
* This method computes the spatial model component for a given true photon
* energy and arrival time.
***************************************************************************/
double GModel::spatial(const GEvent& event,
const GEnergy& srcEng, const GTime& srcTime,
const GObservation& obs, bool grad)
{
// Initialise result
double value = 0.0;
// Continue only if the model has a spatial component
if (m_spatial != NULL) {
// Get current pointing and response function
GResponse* rsp = obs.response(event.time());
// Get IRF value
double irf = rsp->irf(event, *this, srcEng, srcTime, obs);
// Compute source model
double source = 1.0;
// Evaluate model in all components
if (grad) {
// Evaluate source model
if (m_spectral != NULL) source *= m_spectral->eval_gradients(srcEng);
if (m_temporal != NULL) source *= m_temporal->eval_gradients(srcTime);
// Set value
value = source * irf;
// Set gradients
for (int i = 0; i < m_npars; ++i)
m_par[i]->gradient(m_par[i]->gradient() * irf);
}
else {
// Evaluate source model
if (m_spectral != NULL) source *= m_spectral->eval(srcEng);
if (m_temporal != NULL) source *= m_temporal->eval(srcTime);
// Set value
value = source * irf;
}
} // endif: Gamma-ray source model had a spatial component
// Return value
return value;
}
/***********************************************************************//**
* @brief Perform integration over spectral component
*
* @param[in] event Observed event.
* @param[in] srcTime True photon arrival time.
* @param[in] obs Observation.
* @parem[in] grad Compute also model gradients (default=false).
*
* This method integrates the source model over the spectral component. If
* the response function has no energy dispersion then no spectral
* integration is needed and the observed photon energy is identical to the
* true photon energy.
*
* @todo Needs implementation of spectral integration to handle energy
* dispersion.
***************************************************************************/
double GModel::spectral(const GEvent& event, const GTime& srcTime,
const GObservation& obs, bool grad)
{
// Initialise result
double value = 0.0;
// Determine if energy integration is needed
bool integrate = obs.response(event.time())->hasedisp();
// Case A: Integraion
if (integrate) {
std::cout << "GModel::spectral: Integration not implemented." << std::endl;
}
// Case B: No integration (assume no energy dispersion)
else
value = spatial(event, event.energy(), srcTime, obs, grad);
// Return value
return value;
}
/***********************************************************************//**
* @brief Perform integration over temporal component
*
* @param[in] event Observed event.
* @param[in] obs Observation.
* @parem[in] grad Compute also model gradients (default=false).
*
* This method integrates the source model over the temporal component. If
* the response function has no time dispersion then no temporal integration
* is needed and the observed photon arrival time is identical to the true
* photon arrival time.
*
* @todo Needs implementation of temporal integration to handle time
* dispersion.
***************************************************************************/
double GModel::temporal(const GEvent& event, const GObservation& obs, bool grad)
{
// Initialise result
double value = 0.0;
// Determine if time integration is needed
bool integrate = obs.response(event.time())->hastdisp();
// Case A: Integraion
if (integrate) {
std::cout << "GModel::temporal: Integration not implemented." << std::endl;
}
// Case B: No integration (assume no time dispersion)
else
value = spectral(event, event.time(), obs, grad);
// Return value
return value;
}
/*==========================================================================
= =
= Friends =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Output operator
*
* @param[in] os Output stream.
* @param[in] model Model.
***************************************************************************/
std::ostream& operator<< (std::ostream& os, const GModel& model)
{
// Write spectrum in output stream
os << model.print();
// Return output stream
return os;
}
/***********************************************************************//**
* @brief Log operator
*
* @param[in] log Logger.
* @param[in] model Model.
***************************************************************************/
GLog& operator<< (GLog& log, const GModel& model)
{
// Write spectrum into logger
log << model.print();
// Return logger
return log;
}