test_CTA.cpp
28.4 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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/***************************************************************************
* test_CTA.cpp - Test CTA classes *
* ----------------------------------------------------------------------- *
* copyright (C) 2010-2014 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file test_CTA.cpp
* @brief Implementation of CTA classes unit tests
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include "GCTALib.hpp"
#include "GTools.hpp"
#include "GNodeArray.hpp"
#include "test_CTA.hpp"
/* __ Namespaces _________________________________________________________ */
/* __ Globals ____________________________________________________________ */
/* __ Constants __________________________________________________________ */
const std::string datadir = PACKAGE_SOURCE"/inst/cta/test/data";
const std::string cta_caldb = PACKAGE_SOURCE"/inst/cta/caldb";
const std::string cta_irf = "kb_E_50h_v3";
const std::string cta_events = datadir+"/crab_events.fits.gz";
const std::string cta_cntmap = datadir+"/crab_cntmap.fits.gz";
const std::string cta_bin_xml = datadir+"/obs_binned.xml";
const std::string cta_unbin_xml = datadir+"/obs_unbinned.xml";
const std::string cta_model_xml = datadir+"/crab.xml";
const std::string cta_rsp_xml = datadir+"/rsp_models.xml";
const std::string cta_modbck_xml = datadir+"/cta_modelbg.xml";
const std::string cta_caldb_king = PACKAGE_SOURCE"/inst/cta/caldb/data/cta/e/bcf/000002";
const std::string cta_irf_king = "irf_test.fits";
const std::string cta_point_table= datadir+"/crab_pointing.fits.gz";
/***********************************************************************//**
* @brief Set CTA response test methods
***************************************************************************/
void TestGCTAResponse::set(void)
{
// Set test name
name("GCTAResponse");
// Append tests to test suite
append(static_cast<pfunction>(&TestGCTAResponse::test_response), "Test response");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_aeff), "Test effective area");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_psf), "Test PSF");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_psf_king), "Test King profile PSF");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_npsf), "Test integrated PSF");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_irf_diffuse), "Test diffuse IRF");
append(static_cast<pfunction>(&TestGCTAResponse::test_response_npred_diffuse), "Test diffuse IRF integration");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGCTAResponse* TestGCTAResponse::clone(void) const
{
// Clone test suite
return new TestGCTAResponse(*this);
}
/***********************************************************************//**
* @brief Set CTA model test methods
***************************************************************************/
void TestGCTAModelBackground::set(void)
{
// Set test name
name("GCTAModelBackground");
// Append tests to test suite
append(static_cast<pfunction>(&TestGCTAModelBackground::test_modelbg_npred), "Test background spatial npred integration");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGCTAModelBackground* TestGCTAModelBackground::clone(void) const
{
// Clone test suite
return new TestGCTAModelBackground(*this);
}
/***********************************************************************//**
* @brief Set CTA observation test methods
***************************************************************************/
void TestGCTAObservation::set(void)
{
// Set test name
name("GCTAObservation");
// Append tests to test suite
append(static_cast<pfunction>(&TestGCTAObservation::test_unbinned_obs), "Test unbinned observations");
append(static_cast<pfunction>(&TestGCTAObservation::test_binned_obs), "Test binned observation");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGCTAObservation* TestGCTAObservation::clone(void) const
{
// Clone test suite
return new TestGCTAObservation(*this);
}
/***********************************************************************//**
* @brief Set CTA optimizer test methods
***************************************************************************/
void TestGCTAOptimize::set(void)
{
// Set test name
name("CTA optimizers");
// Append tests to test suite
append(static_cast<pfunction>(&TestGCTAOptimize::test_unbinned_optimizer), "Test unbinned optimizer");
append(static_cast<pfunction>(&TestGCTAOptimize::test_binned_optimizer), "Test binned optimizer");
// Return
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGCTAOptimize* TestGCTAOptimize::clone(void) const
{
// Clone test suite
return new TestGCTAOptimize(*this);
}
/***********************************************************************//**
* @brief Set CTA pointing test methods
***************************************************************************/
void TestGCTAPointing::set(void)
{
// Set test name
name("GCTAPointing");
// Append tests to test suite
append(static_cast<pfunction>(&TestGCTAPointing::test_load_table),
"Test load pointing from table");
append(static_cast<pfunction>(&TestGCTAPointing::test_interpolate_altaz),
"Test alt/az interpolation given a time");
return;
}
/***********************************************************************//**
* @brief Clone test suite
*
* @return Pointer to deep copy of test suite.
***************************************************************************/
TestGCTAPointing* TestGCTAPointing::clone(void) const
{
// Clone test suite
return new TestGCTAPointing(*this);
}
/***********************************************************************//**
* @brief Test ability to load a CTA pointing table
*
***************************************************************************/
void TestGCTAPointing::test_load_table(void)
{
GCTAPointing pnt;
pnt.load_pointing_table( cta_point_table );
return;
}
/***********************************************************************//**
* @brief Test interpolation of alt/az pointing dir as a function of
* time
*
***************************************************************************/
void TestGCTAPointing::test_interpolate_altaz(void)
{
GCTAObservation run;
GCTAPointing pnt;
pnt.load_pointing_table( cta_point_table );
// test an out-of bounds time
try {
GTime time(155470378.7, "sec");
GHorizDir dir = pnt.dir_horiz( time );
}
catch (GException::out_of_range &exc) {
std::cout << exc.what() << std::endl;
}
// Get a time that is somewhere in the run:
GTime time(155470378.7, "sec");
GHorizDir dir = pnt.dir_horiz( time );
std::cout << "time="<<time<<" dir="<<dir<<std::endl;
return;
}
/***********************************************************************//**
* @brief Test CTA Aeff computation
***************************************************************************/
void TestGCTAResponse::test_response_aeff(void)
{
// Load response
GCTAResponse rsp;
rsp.caldb(cta_caldb);
rsp.load(cta_irf);
// Sum over effective area for control
GEnergy eng;
double sum = 0.0;
double ref = 154124059000.00006; //!< Adjust to actual value
for (int i = 0; i < 30; ++i) {
eng.TeV(pow(10.0, -1.7 + 0.1*double(i)));
double aeff = rsp.aeff(0.0, 0.0, 0.0, 0.0, eng.log10TeV());
//std::cout << eng << " " << eng.log10TeV() << " " << aeff << std::endl;
sum += aeff;
}
test_value(sum, ref, 0.1, "Effective area verification");
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA psf computation
*
* The Psf computation is tested by integrating numerically the Psf
* function. Integration is done in a rather simplistic way, by stepping
* radially away from the centre. The integration is done for a set of
* energies from 0.1-10 TeV.
***************************************************************************/
void TestGCTAResponse::test_response_psf(void)
{
// Load response
GCTAResponse rsp;
rsp.caldb(cta_caldb);
rsp.load(cta_irf);
// Integrate Psf
GEnergy eng;
for (double e = 0.1; e < 10.0; e *= 2.0) {
eng.TeV(e);
double r = 0.0;
double dr = 0.001;
int steps = int(1.0/dr);
double sum = 0.0;
for (int i = 0; i < steps; ++i) {
r += dr;
sum += rsp.psf(r * gammalib::deg2rad, 0.0, 0.0, 0.0, 0.0, eng.log10TeV()) *
gammalib::twopi * std::sin(r * gammalib::deg2rad) * dr *
gammalib::deg2rad;
}
test_value(sum, 1.0, 0.001, "PSF integration for "+eng.print());
}
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA King profile psf computation
*
* The Psf computation is tested by integrating numerically the Psf
* function. Integration is done in a rather simplistic way, by stepping
* radially away from the centre. The integration is done for a set of
* energies from 0.1-10 TeV.
***************************************************************************/
void TestGCTAResponse::test_response_psf_king(void)
{
// Load response
GCTAResponse rsp;
rsp.caldb(cta_caldb_king);
rsp.load(cta_irf_king);
// Integrate Psf
GEnergy eng;
for (double e = 0.1; e < 10.0; e *= 2.0) {
eng.TeV(e);
double r_max = rsp.psf()->delta_max(eng.log10TeV()) * gammalib::rad2deg;
double r = 0.0;
double dr = 0.0001;
int steps = int(r_max / dr);
double sum = 0.0;
for (int i = 0; i < steps; ++i) {
r += dr;
sum += rsp.psf(r * gammalib::deg2rad, 0.0, 0.0, 0.0, 0.0, eng.log10TeV()) *
gammalib::twopi * std::sin(r * gammalib::deg2rad) * dr *
gammalib::deg2rad;
}
test_value(sum, 1.0, 0.001, "PSF integration for "+eng.print());
}
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA npsf computation
***************************************************************************/
void TestGCTAResponse::test_response_npsf(void)
{
// Setup CTA response
GCTAResponse rsp;
rsp.caldb(cta_caldb);
rsp.load(cta_irf);
// Setup npsf computation
GSkyDir srcDir;
GEnergy srcEng;
GTime srcTime;
GCTAPointing pnt;
GCTARoi roi;
GCTAInstDir instDir;
instDir.dir().radec_deg(0.0, 0.0);
roi.centre(instDir);
roi.radius(2.0);
srcEng.TeV(0.1);
// Test PSF centred on ROI
srcDir.radec_deg(0.0, 0.0);
double npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi);
test_value(npsf, 1.0, 1.0e-3, "PSF(0,0) integration");
// Test PSF offset but inside ROI
srcDir.radec_deg(1.0, 1.0);
npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi);
test_value(npsf, 1.0, 1.0e-3, "PSF(1,1) integration");
// Test PSF outside and overlapping ROI
srcDir.radec_deg(0.0, 2.0);
npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi);
test_value(npsf, 0.492373, 1.0e-3, "PSF(0,2) integration");
// Test PSF outside ROI
srcDir.radec_deg(2.0, 2.0);
npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi);
test_value(npsf, 0.0, 1.0e-3, "PSF(2,2) integration");
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA IRF computation for diffuse source model
*
* Tests the IRF computation for the diffuse source model. This is done
* by calling the GCTAObservation::model method which in turn calls the
* GCTAResponse::irf_diffuse method. The test is done for a small counts
* map to keep the test executing reasonably fast.
***************************************************************************/
void TestGCTAResponse::test_response_irf_diffuse(void)
{
// Set reference value
//double ref = 13803.800313356;
//const double ref = 13803.5186374;
const double ref = 13803.6932774; // After GWcs::solidangle improvement
// Set parameters
double src_ra = 201.3651;
double src_dec = -43.0191;
int nebins = 5;
// Setup pointing on Cen A
GSkyDir skyDir;
skyDir.radec_deg(src_ra, src_dec);
GCTAPointing pnt;
pnt.dir(skyDir);
// Setup skymap (10 energy layers)
GSkymap map("CAR", "CEL", src_ra, src_dec, 0.5, 0.5, 10, 10, nebins);
// Setup time interval
GGti gti;
GTime tstart(0.0);
GTime tstop(1800.0);
gti.append(tstart, tstop);
// Setup energy boundaries
GEnergy emin(0.1, "TeV");
GEnergy emax(100.0, "TeV");
GEbounds ebounds(nebins, emin, emax);
// Setup event cube centreed on Cen A
GCTAEventCube cube(map, ebounds, gti);
// Setup dummy CTA observation
GCTAObservation obs;
obs.ontime(1800.0);
obs.livetime(1600.0);
obs.deadc(1600.0/1800.0);
obs.response(cta_irf, cta_caldb);
obs.events(cube);
obs.pointing(pnt);
// Load model for IRF computation
GModels models(cta_rsp_xml);
// Reset sum
double sum = 0.0;
// Iterate over all bins in event cube
for (int i = 0; i < obs.events()->size(); ++i) {
// Get event pointer
const GEventBin* bin = (*(static_cast<const GEventCube*>(obs.events())))[i];
// Get model and add to sum
double model = obs.model(models, *bin, NULL) * bin->size();
sum += model;
}
// Test sum
test_value(sum, ref, 1.0e-5, "Diffuse IRF computation");
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA Npred computation
*
* Tests the Npred computation for the diffuse source model. This is done
* by loading the model from the XML file and by calling the
* GCTAObservation::npred method which in turn calls the
* GCTAResponse::npred_diffuse method. The test takes a few seconds.
***************************************************************************/
void TestGCTAResponse::test_response_npred_diffuse(void)
{
// Set reference value
//double ref = 11212.26274; // npred_spec precision of 1e-6
//const double ref = 11212.437464; // npred_spec precision of 1e-5
const double ref = 11212.4370702; // After GWcs::solidangle improvement
// Set parameters
double src_ra = 201.3651;
double src_dec = -43.0191;
double roi_rad = 4.0;
// Setup ROI centred on Cen A with a radius of 4 deg
GCTARoi roi;
GCTAInstDir instDir;
instDir.dir().radec_deg(src_ra, src_dec);
roi.centre(instDir);
roi.radius(roi_rad);
// Setup pointing on Cen A
GSkyDir skyDir;
skyDir.radec_deg(src_ra, src_dec);
GCTAPointing pnt;
pnt.dir(skyDir);
// Setup dummy event list
GGti gti;
GEbounds ebounds;
GTime tstart(0.0);
GTime tstop(1800.0);
GEnergy emin(0.1, "TeV");
GEnergy emax(100.0, "TeV");
gti.append(tstart, tstop);
ebounds.append(emin, emax);
GCTAEventList events;
events.roi(roi);
events.gti(gti);
events.ebounds(ebounds);
// Setup dummy CTA observation
GCTAObservation obs;
obs.ontime(1800.0);
obs.livetime(1600.0);
obs.deadc(1600.0/1800.0);
obs.response(cta_irf, cta_caldb);
obs.events(events);
obs.pointing(pnt);
// Load models for Npred computation
GModels models(cta_rsp_xml);
// Perform Npred computation
double npred = obs.npred(models, NULL);
// Test Npred
test_value(npred, ref*1600.0/1800.0, 1.0e-5, "Diffuse Npred computation");
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA response handling
***************************************************************************/
void TestGCTAResponse::test_response(void)
{
// Test CTA response loading
test_try("Test CTA response loading");
try {
// Load response
GCTAResponse rsp;
rsp.caldb(cta_caldb);
rsp.load(cta_irf);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Return
return;
}
/***********************************************************************//**
* @brief Test CTA Model Background Npred computation
*
* Tests the Npred computation for the background source model. This is done
* by loading the model from the XML file and by calling the
* GCTAModelBackground::npred method. The test takes a few seconds.
***************************************************************************/
void TestGCTAModelBackground::test_modelbg_npred(void)
{
// Set reference value (~0.393469 result of a 2D Gaussian integrated
// within one sigma)
const double ref = 1.0 - 1.0 / std::sqrt(std::exp(1.0));
// Load models for Npred computation
GModels models(cta_modbck_xml);
// Get the CTABackgroundModel
const GCTAModelBackground* bck = dynamic_cast<const GCTAModelBackground*>(models[0]);
// Get the spectral and spatial components
const GModelSpectralPlaw* spec = dynamic_cast<const GModelSpectralPlaw*>(bck->spectral());
const GModelSpatialRadialGauss* spat = dynamic_cast<const GModelSpatialRadialGauss*>(bck->spatial());
// Get Integration centre for ROI position
double src_ra = spat->ra();
double src_dec = spat->dec();
double sigma = spat->sigma();
test_value(sigma, 1.0, 1e-7, "Input value from cta_modelbck.xml - file");
// Set ROI to sigma of Gaussian
double roi_rad = sigma;
// Setup ROI centred on the Gaussian mean with a radius of 1sigma
GCTARoi roi;
GCTAInstDir instDir;
instDir.dir().radec_deg(src_ra, src_dec);
roi.centre(instDir);
roi.radius(roi_rad);
// Setup pointing with the same centre as ROI
GSkyDir skyDir;
skyDir.radec_deg(src_ra, src_dec);
GCTAPointing pnt;
pnt.dir(skyDir);
// Setup dummy event list
GGti gti;
GEbounds ebounds;
GTime tstart(0.0);
GTime tstop(1800.0);
GEnergy emin(0.1, "TeV");
GEnergy emax(100.0, "TeV");
gti.append(tstart, tstop);
ebounds.append(emin, emax);
GCTAEventList events;
events.roi(roi);
events.gti(gti);
events.ebounds(ebounds);
// Setup dummy CTA observation without deadtime
GCTAObservation obs;
obs.ontime(1800.0);
obs.livetime(1800.0);
obs.deadc(1.0);
obs.response(cta_irf, cta_caldb);
obs.events(events);
obs.pointing(pnt);
// Perform Npred computation
double npred = bck->npred(spec->pivot(),tstart,obs);
// Divide npred by spectral normalisation to true containment fraction
npred /= spec->prefactor();
// Test Npred against the reference value
test_value(npred, ref , 1.0e-5, "Npred computation for CTA background model");
// Return
return;
}
/***********************************************************************//**
* @brief Test unbinned observation handling
***************************************************************************/
void TestGCTAObservation::test_unbinned_obs(void)
{
// Set filenames
const std::string file1 = "test_cta_obs_unbinned.xml";
// Declare observations
GObservations obs;
GCTAObservation run;
// Load unbinned CTA observation
test_try("Load unbinned CTA observation");
try {
run.load_unbinned(cta_events);
run.response(cta_irf,cta_caldb);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Add observation (twice) to data
test_try("Append observation twice");
try {
run.id("0001");
obs.append(run);
run.id("0002");
obs.append(run);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Loop over all events
const GEvents* events = run.events();
int num = 0;
for (int i = 0; i < events->size(); ++i) {
num++;
}
test_value(num, 4397, 1.0e-20, "Test event iterator");
// Test XML loading
test_try("Test XML loading");
try {
obs = GObservations(cta_unbin_xml);
obs.save(file1);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***********************************************************************//**
* @brief Test binned observation handling
***************************************************************************/
void TestGCTAObservation::test_binned_obs(void)
{
// Set filenames
const std::string file1 = "test_cta_obs_binned.xml";
// Declare observations
GObservations obs;
GCTAObservation run;
// Load binned CTA observation
test_try("Load unbinned CTA observation");
try {
run.load_binned(cta_cntmap);
run.response(cta_irf,cta_caldb);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Test XML loading
test_try("Test XML loading");
try {
obs = GObservations(cta_bin_xml);
obs.save(file1);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***********************************************************************//**
* @brief Test unbinned optimizer
***************************************************************************/
void TestGCTAOptimize::test_unbinned_optimizer(void)
{
// Declare observations
GObservations obs;
GCTAObservation run;
// Load unbinned CTA observation
test_try("Load unbinned CTA observation");
try {
run.load_unbinned(cta_events);
run.response(cta_irf,cta_caldb);
obs.append(run);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Load models from XML file
obs.models(cta_model_xml);
// Perform LM optimization
double fit_results[] = {83.6331, 0,
22.0145, 0,
5.656246512e-16, 1.91458426e-17,
-2.48426, -0.02573396361,
300000, 0,
1, 0,
2.993705325, 0.03572658413,
6.490832107e-05, 1.749021094e-06,
-1.833584022, -0.01512223495,
1000000, 0,
1, 0};
test_try("Perform LM optimization");
try {
GOptimizerLM opt;
opt.max_iter(100);
obs.optimize(opt);
test_try_success();
for (int i = 0, j = 0; i < obs.models().size(); ++i) {
const GModel* model = obs.models()[i];
for (int k = 0; k < model->size(); ++k) {
GModelPar par = (*model)[k];
std::string msg = "Verify optimization result for " + par.print();
test_value(par.value(), fit_results[j++], 5.0e-5, msg);
test_value(par.error(), fit_results[j++], 5.0e-5, msg);
}
}
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***********************************************************************//**
* @brief Test binned optimizer
***************************************************************************/
void TestGCTAOptimize::test_binned_optimizer(void)
{
// Declare observations
GObservations obs;
GCTAObservation run;
// Load binned CTA observation
test_try("Load binned CTA observation");
try {
run.load_binned(cta_cntmap);
run.response(cta_irf,cta_caldb);
obs.append(run);
test_try_success();
}
catch (std::exception &e) {
test_try_failure(e);
}
// Load models from XML file
obs.models(cta_model_xml);
// Perform LM optimization
double fit_results[] = {83.6331, 0,
22.0145, 0,
5.616410411e-16, 1.904730785e-17,
-2.48178, -0.02580905077,
300000, 0,
1, 0,
2.93368, 0.06639644824,
6.550723074e-05, 1.945714239e-06,
-1.833781187, -0.0160819,
1000000, 0,
1, 0};
test_try("Perform LM optimization");
try {
GOptimizerLM opt;
opt.max_iter(100);
obs.optimize(opt);
test_try_success();
for (int i = 0, j = 0; i < obs.models().size(); ++i) {
const GModel* model = obs.models()[i];
for (int k = 0; k < model->size(); ++k) {
GModelPar par = (*model)[k];
std::string msg = "Verify optimization result for " + par.print();
test_value(par.value(), fit_results[j++], 5.0e-5, msg);
test_value(par.error(), fit_results[j++], 5.0e-5, msg);
}
}
}
catch (std::exception &e) {
test_try_failure(e);
}
// Exit test
return;
}
/***************************************************************************
* @brief Main entry point for test executable
***************************************************************************/
int main(void)
{
// Allocate test suite container
GTestSuites testsuites("CTA instrument specific class testing");
// Check if data directory exists
bool has_data = (access(datadir.c_str(), R_OK) == 0);
if (has_data) {
std::string caldb = "CALDB="+cta_caldb;
putenv((char*)caldb.c_str());
}
// Initially assume that we pass all tests
bool success = true;
// Create test suites and append them to the container
TestGCTAResponse rsp;
TestGCTAObservation obs;
TestGCTAModelBackground bck;
TestGCTAOptimize opt;
TestGCTAPointing pnt;
testsuites.append(rsp);
if (has_data) {
testsuites.append(bck);
testsuites.append(obs);
testsuites.append(opt);
testsuites.append(pnt);
}
// Run the testsuites
success = testsuites.run();
// Save test report
testsuites.save("reports/GCTA.xml");
// Return success status
return (success ? 0 : 1);
}