Commit a7a3be59f887fe655ac09149db8bb665ee50c537

Authored by Jürgen Knödlseder
1 parent a03d24de

Enhance ctobssim.

src/ctbin/ctbin.cpp
@@ -70,7 +70,7 @@ ctbin::ctbin(void) : ctool(CTBIN_NAME, CTBIN_VERSION) @@ -70,7 +70,7 @@ ctbin::ctbin(void) : ctool(CTBIN_NAME, CTBIN_VERSION)
70 * 70 *
71 * param[in] obs Observation container. 71 * param[in] obs Observation container.
72 * 72 *
73 - * Constructs a ctbin tool from an observation container. 73 + * Constructs ctbin tool from an observation container.
74 ***************************************************************************/ 74 ***************************************************************************/
75 ctbin::ctbin(const GObservations& obs) : ctool(CTBIN_NAME, CTBIN_VERSION) 75 ctbin::ctbin(const GObservations& obs) : ctool(CTBIN_NAME, CTBIN_VERSION)
76 { 76 {
@@ -92,7 +92,8 @@ ctbin::ctbin(const GObservations& obs) : ctool(CTBIN_NAME, CTBIN_VERSION) @@ -92,7 +92,8 @@ ctbin::ctbin(const GObservations& obs) : ctool(CTBIN_NAME, CTBIN_VERSION)
92 * @param[in] argc Number of arguments in command line. 92 * @param[in] argc Number of arguments in command line.
93 * @param[in] argv Array of command line arguments. 93 * @param[in] argv Array of command line arguments.
94 * 94 *
95 - * Constructs ctbin tool from command line arguments. 95 + * Constructs ctbin tool using command line arguments for user parameter
  96 + * setting.
96 ***************************************************************************/ 97 ***************************************************************************/
97 ctbin::ctbin(int argc, char *argv[]) : 98 ctbin::ctbin(int argc, char *argv[]) :
98 ctool(CTBIN_NAME, CTBIN_VERSION, argc, argv) 99 ctool(CTBIN_NAME, CTBIN_VERSION, argc, argv)
@@ -149,7 +150,7 @@ ctbin::~ctbin(void) @@ -149,7 +150,7 @@ ctbin::~ctbin(void)
149 /***********************************************************************//** 150 /***********************************************************************//**
150 * @brief Assignment operator 151 * @brief Assignment operator
151 * 152 *
152 - * @param[in] app Application. 153 + * @param[in] app ctbin tool.
153 * @return ctbin tool. 154 * @return ctbin tool.
154 * 155 *
155 * Assigns ctbin tool. 156 * Assigns ctbin tool.
@@ -240,14 +241,7 @@ void ctbin::run(void) @@ -240,14 +241,7 @@ void ctbin::run(void)
240 241
241 // Write header for observation 242 // Write header for observation
242 if (logTerse()) { 243 if (logTerse()) {
243 - std::string header = m_obs[i]->instrument() + " observation";  
244 - if (!m_obs[i]->name().empty()) {  
245 - header += " \"" + m_obs[i]->name() + "\"";  
246 - }  
247 - if (!m_obs[i]->id().empty()) {  
248 - header += " (id=" + m_obs[i]->id() +")";  
249 - }  
250 - log.header3(header); 244 + log.header3(get_obs_header(m_obs[i]));
251 } 245 }
252 246
253 // Get CTA observation 247 // Get CTA observation
@@ -328,8 +322,8 @@ void ctbin::save(void) @@ -328,8 +322,8 @@ void ctbin::save(void)
328 322
329 // Log filename 323 // Log filename
330 if (logTerse()) { 324 if (logTerse()) {
331 - log << "Save counts cube into file \""+m_outcube+"\".";  
332 - log << std::endl; 325 + log << gammalib::parformat("Counts cube file");
  326 + log << m_outcube.url() << std::endl;
333 } 327 }
334 328
335 // Save cube 329 // Save cube
src/ctobssim/ctobssim.cpp
@@ -60,6 +60,8 @@ const double g_roi_margin = 0.5; //!&lt; Simulation radius margin (degrees) @@ -60,6 +60,8 @@ const double g_roi_margin = 0.5; //!&lt; Simulation radius margin (degrees)
60 60
61 /***********************************************************************//** 61 /***********************************************************************//**
62 * @brief Void constructor 62 * @brief Void constructor
  63 + *
  64 + * Constructs an empty ctobssim tool.
63 ***************************************************************************/ 65 ***************************************************************************/
64 ctobssim::ctobssim(void) : ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION) 66 ctobssim::ctobssim(void) : ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION)
65 { 67 {
@@ -76,7 +78,7 @@ ctobssim::ctobssim(void) : ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION) @@ -76,7 +78,7 @@ ctobssim::ctobssim(void) : ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION)
76 * 78 *
77 * @param[in] obs Observation container. 79 * @param[in] obs Observation container.
78 * 80 *
79 - * Constructs application by initialising from an observation container. 81 + * Constructs ctobssim tool from an observation container.
80 ***************************************************************************/ 82 ***************************************************************************/
81 ctobssim::ctobssim(const GObservations& obs) : 83 ctobssim::ctobssim(const GObservations& obs) :
82 ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION) 84 ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION)
@@ -98,7 +100,8 @@ ctobssim::ctobssim(const GObservations&amp; obs) : @@ -98,7 +100,8 @@ ctobssim::ctobssim(const GObservations&amp; obs) :
98 * @param[in] argc Number of arguments in command line. 100 * @param[in] argc Number of arguments in command line.
99 * @param[in] argv Array of command line arguments. 101 * @param[in] argv Array of command line arguments.
100 * 102 *
101 - * Constructs application using command line arguments for parameter setting. 103 + * Constructs ctobssim tool using command line arguments for user parameter
  104 + * setting.
102 ***************************************************************************/ 105 ***************************************************************************/
103 ctobssim::ctobssim(int argc, char *argv[]) : 106 ctobssim::ctobssim(int argc, char *argv[]) :
104 ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION, argc, argv) 107 ctool(CTOBSSIM_NAME, CTOBSSIM_VERSION, argc, argv)
@@ -115,6 +118,8 @@ ctobssim::ctobssim(int argc, char *argv[]) : @@ -115,6 +118,8 @@ ctobssim::ctobssim(int argc, char *argv[]) :
115 * @brief Copy constructor 118 * @brief Copy constructor
116 * 119 *
117 * @param[in] app Application. 120 * @param[in] app Application.
  121 + *
  122 + * Constructs ctobssim tool from another ctobssim instance.
118 ***************************************************************************/ 123 ***************************************************************************/
119 ctobssim::ctobssim(const ctobssim& app) : ctool(app) 124 ctobssim::ctobssim(const ctobssim& app) : ctool(app)
120 { 125 {
@@ -131,6 +136,8 @@ ctobssim::ctobssim(const ctobssim&amp; app) : ctool(app) @@ -131,6 +136,8 @@ ctobssim::ctobssim(const ctobssim&amp; app) : ctool(app)
131 136
132 /***********************************************************************//** 137 /***********************************************************************//**
133 * @brief Destructor 138 * @brief Destructor
  139 + *
  140 + * Destructs ctobssim tool.
134 ***************************************************************************/ 141 ***************************************************************************/
135 ctobssim::~ctobssim(void) 142 ctobssim::~ctobssim(void)
136 { 143 {
@@ -151,8 +158,10 @@ ctobssim::~ctobssim(void) @@ -151,8 +158,10 @@ ctobssim::~ctobssim(void)
151 /***********************************************************************//** 158 /***********************************************************************//**
152 * @brief Assignment operator 159 * @brief Assignment operator
153 * 160 *
154 - * @param[in] app Application.  
155 - * @return Application. 161 + * @param[in] app ctobssim tool.
  162 + * @return ctobssim tool.
  163 + *
  164 + * Assigns ctobssim tool.
156 ***************************************************************************/ 165 ***************************************************************************/
157 ctobssim& ctobssim::operator=(const ctobssim& app) 166 ctobssim& ctobssim::operator=(const ctobssim& app)
158 { 167 {
@@ -185,7 +194,9 @@ ctobssim&amp; ctobssim::operator=(const ctobssim&amp; app) @@ -185,7 +194,9 @@ ctobssim&amp; ctobssim::operator=(const ctobssim&amp; app)
185 ==========================================================================*/ 194 ==========================================================================*/
186 195
187 /***********************************************************************//** 196 /***********************************************************************//**
188 - * @brief Clear application 197 + * @brief Clear ctobssim tool
  198 + *
  199 + * Clears ctobssim tool.
189 ***************************************************************************/ 200 ***************************************************************************/
190 void ctobssim::clear(void) 201 void ctobssim::clear(void)
191 { 202 {
@@ -205,10 +216,10 @@ void ctobssim::clear(void) @@ -205,10 +216,10 @@ void ctobssim::clear(void)
205 216
206 217
207 /***********************************************************************//** 218 /***********************************************************************//**
208 - * @brief Simulate event data 219 + * @brief Run the ctobssim tool.
209 * 220 *
210 - * This method runs the simulation. Results are not saved by this method.  
211 - * Invoke "save" to save the results. 221 + * Gets the user parameters, loops over all CTA observations in the
  222 + * observation container, and simulate events for each observation.
212 ***************************************************************************/ 223 ***************************************************************************/
213 void ctobssim::run(void) 224 void ctobssim::run(void)
214 { 225 {
@@ -286,24 +297,14 @@ void ctobssim::run(void) @@ -286,24 +297,14 @@ void ctobssim::run(void)
286 // Write observation(s) into logger 297 // Write observation(s) into logger
287 if (logTerse()) { 298 if (logTerse()) {
288 log << std::endl; 299 log << std::endl;
289 - if (m_obs.size() > 1) {  
290 - log.header1("Observations");  
291 - }  
292 - else {  
293 - log.header1("Observation");  
294 - } 300 + log.header1(gammalib::number("Observation", m_obs.size()));
295 log << m_obs << std::endl; 301 log << m_obs << std::endl;
296 } 302 }
297 303
298 // Write header 304 // Write header
299 if (logTerse()) { 305 if (logTerse()) {
300 log << std::endl; 306 log << std::endl;
301 - if (m_obs.size() > 1) {  
302 - log.header1("Simulate observations");  
303 - }  
304 - else {  
305 - log.header1("Simulate observation");  
306 - } 307 + log.header1(gammalib::number("Simulate observation", m_obs.size()));
307 } 308 }
308 309
309 // From here on the code can be parallelized if OpenMP support 310 // From here on the code can be parallelized if OpenMP support
@@ -334,14 +335,7 @@ void ctobssim::run(void) @@ -334,14 +335,7 @@ void ctobssim::run(void)
334 335
335 // Write header for observation 336 // Write header for observation
336 if (logTerse()) { 337 if (logTerse()) {
337 - std::string header = m_obs[i]->instrument() + " observation";  
338 - if (m_obs[i]->name().length() > 1) {  
339 - header += " \"" + m_obs[i]->name() + "\"";  
340 - }  
341 - if (m_obs[i]->id().length() > 1) {  
342 - header += " (id=" + m_obs[i]->id() +")";  
343 - }  
344 - wrklog.header3(header); 338 + wrklog.header3(get_obs_header(m_obs[i]));
345 } 339 }
346 340
347 // Get pointer on CTA observation 341 // Get pointer on CTA observation
@@ -1455,8 +1449,8 @@ void ctobssim::save_fits(void) @@ -1455,8 +1449,8 @@ void ctobssim::save_fits(void)
1455 1449
1456 // Log filename 1450 // Log filename
1457 if (logTerse()) { 1451 if (logTerse()) {
1458 - log << "Save event list into file \""+m_outevents+"\".";  
1459 - log << std::endl; 1452 + log << gammalib::parformat("Event list file");
  1453 + log << m_outevents << std::endl;
1460 } 1454 }
1461 1455
1462 // Save observation into FITS file 1456 // Save observation into FITS file
@@ -1483,7 +1477,7 @@ void ctobssim::save_xml(void) @@ -1483,7 +1477,7 @@ void ctobssim::save_xml(void)
1483 { 1477 {
1484 // Get output filename and prefix 1478 // Get output filename and prefix
1485 m_outevents = (*this)["outevents"].filename(); 1479 m_outevents = (*this)["outevents"].filename();
1486 - m_prefix = (*this)["prefix"].string(); 1480 + m_prefix = (*this)["prefix"].string();
1487 1481
1488 // Issue warning if output filename has no .xml suffix 1482 // Issue warning if output filename has no .xml suffix
1489 std::string suffix = gammalib::tolower(m_outevents.substr(m_outevents.length()-4,4)); 1483 std::string suffix = gammalib::tolower(m_outevents.substr(m_outevents.length()-4,4));
@@ -1513,15 +1507,16 @@ void ctobssim::save_xml(void) @@ -1513,15 +1507,16 @@ void ctobssim::save_xml(void)
1513 if (obs->events()->size() != 0) { 1507 if (obs->events()->size() != 0) {
1514 1508
1515 // Set event output file name 1509 // Set event output file name
1516 - std::string outfile = m_prefix + gammalib::str(i) + ".fits"; 1510 + std::string outfile = m_prefix + gammalib::str(i) +
  1511 + ".fits";
1517 1512
1518 // Store output file name in observation 1513 // Store output file name in observation
1519 obs->eventfile(outfile); 1514 obs->eventfile(outfile);
1520 1515
1521 // Log filename 1516 // Log filename
1522 if (logTerse()) { 1517 if (logTerse()) {
1523 - log << "Save event list into file \""+outfile+"\".";  
1524 - log << std::endl; 1518 + log << gammalib::parformat("Event list file");
  1519 + log << outfile << std::endl;
1525 } 1520 }
1526 1521
1527 // Save observation into FITS file 1522 // Save observation into FITS file
src/ctobssim/ctobssim.par
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 # File: ctobssim.par # 7 # File: ctobssim.par #
8 # Version: 1.1.0 # 8 # Version: 1.1.0 #
9 # # 9 # #
10 -# Author: Jurgen Knodlseder # 10 +# Author: Juergen Knoedlseder #
11 # Jurgen.Knodlseder@irap.omp.eu # 11 # Jurgen.Knodlseder@irap.omp.eu #
12 # IRAP # 12 # IRAP #
13 # # 13 # #
@@ -17,13 +17,13 @@ @@ -17,13 +17,13 @@
17 # 17 #
18 # General parameters 18 # General parameters
19 #=================== 19 #===================
20 -inobs, f, h, "NONE",,, "Input event list or observation definition XML file"  
21 -inmodel, f, a, "$CTOOLS/share/models/crab.xml",,, "Input model XML file"  
22 -caldb, s, a, "prod2",,, "Calibration database"  
23 -irf, s, a, "South_0.5h",,, "Instrument response function" 20 +inobs, f, h, NONE,,, "Input event list or observation definition XML file"
  21 +inmodel, f, a, $CTOOLS/share/models/crab.xml,,, "Input model XML file"
  22 +caldb, s, a, prod2,,, "Calibration database"
  23 +irf, s, a, South_0.5h,,, "Instrument response function"
24 edisp, b, h, no,,, "Apply energy dispersion?" 24 edisp, b, h, no,,, "Apply energy dispersion?"
25 -outevents, f, a, "events.fits",,, "Output event data file or observation definition XML file"  
26 -prefix, s, h, "sim_events_",,, "Prefix for event lists in observation definition XML file" 25 +outevents, f, a, events.fits,,, "Output event data file or observation definition XML file"
  26 +prefix, s, h, sim_events_,,, "Prefix for event lists in observation definition XML file"
27 27
28 # 28 #
29 # Simulation parameters 29 # Simulation parameters
@@ -38,7 +38,7 @@ emin, r, a, 0.1,,, &quot;Lower energy limit (TeV)&quot; @@ -38,7 +38,7 @@ emin, r, a, 0.1,,, &quot;Lower energy limit (TeV)&quot;
38 emax, r, a, 100.0,,, "Upper energy limit (TeV)" 38 emax, r, a, 100.0,,, "Upper energy limit (TeV)"
39 deadc, r, h, 0.95,0,1, "Average deadtime correction factor" 39 deadc, r, h, 0.95,0,1, "Average deadtime correction factor"
40 maxrate, r, h, 1.0e6,,, "Maximum photon rate" 40 maxrate, r, h, 1.0e6,,, "Maximum photon rate"
41 -eslices, i, h, 10,1,100,"Number of energy slices" 41 +eslices, i, h, 10,1,100, "Number of energy slices"
42 42
43 # 43 #
44 # Standard parameters 44 # Standard parameters
@@ -46,5 +46,5 @@ eslices, i, h, 10,1,100,&quot;Number of energy slices&quot; @@ -46,5 +46,5 @@ eslices, i, h, 10,1,100,&quot;Number of energy slices&quot;
46 chatter, i, h, 2,0,4, "Chattiness of output" 46 chatter, i, h, 2,0,4, "Chattiness of output"
47 clobber, b, h, yes,,, "Overwrite existing output files with new output files?" 47 clobber, b, h, yes,,, "Overwrite existing output files with new output files?"
48 debug, b, h, no,,, "Debugging mode activated" 48 debug, b, h, no,,, "Debugging mode activated"
49 -mode, s, h, "ql",,, "Mode of automatic parameters"  
50 -logfile, f, h, "ctobssim.log",,, "Log filename" 49 +mode, s, h, ql,,, "Mode of automatic parameters"
  50 +logfile, f, h, ctobssim.log,,, "Log filename"
test/test_ctbin.py
@@ -22,9 +22,9 @@ import gammalib @@ -22,9 +22,9 @@ import gammalib
22 import ctools 22 import ctools
23 23
24 24
25 -# ========================= #  
26 -# Test class for ctbin tool #  
27 -# ========================= # 25 +# =========================== #
  26 +# Test class for ctbin module #
  27 +# =========================== #
28 class Test(gammalib.GPythonTestSuite): 28 class Test(gammalib.GPythonTestSuite):
29 """ 29 """
30 Test class for ctbin module. 30 Test class for ctbin module.
@@ -39,7 +39,7 @@ class Test(gammalib.GPythonTestSuite): @@ -39,7 +39,7 @@ class Test(gammalib.GPythonTestSuite):
39 gammalib.GPythonTestSuite.__init__(self) 39 gammalib.GPythonTestSuite.__init__(self)
40 40
41 # Set members 41 # Set members
42 - self.events_name = "data/crab_events.fits" 42 + self._events_name = "data/crab_events.fits"
43 43
44 # Return 44 # Return
45 return 45 return
@@ -65,7 +65,7 @@ class Test(gammalib.GPythonTestSuite): @@ -65,7 +65,7 @@ class Test(gammalib.GPythonTestSuite):
65 """ 65 """
66 # Set-up ctbin 66 # Set-up ctbin
67 bin = ctools.ctbin() 67 bin = ctools.ctbin()
68 - bin["inobs"] = self.events_name 68 + bin["inobs"] = self._events_name
69 bin["outcube"] = "cntmap.fits" 69 bin["outcube"] = "cntmap.fits"
70 bin["ebinalg"] = "LOG" 70 bin["ebinalg"] = "LOG"
71 bin["emin"] = 0.1 71 bin["emin"] = 0.1
@@ -111,20 +111,14 @@ class Test(gammalib.GPythonTestSuite): @@ -111,20 +111,14 @@ class Test(gammalib.GPythonTestSuite):
111 #Please specify a valid parameter name. 111 #Please specify a valid parameter name.
112 112
113 # Save counts cube 113 # Save counts cube
114 - self.test_try("Save counts cube")  
115 - try:  
116 - bin.save()  
117 - self.test_try_success()  
118 - except:  
119 - msg = "Exception occured in saving counts cube."  
120 - self.test_try_failure(msg) 114 + bin.save()
121 115
122 # Load counts cube and check content. 116 # Load counts cube and check content.
123 evt = gammalib.GCTAEventCube("cntmap.fits") 117 evt = gammalib.GCTAEventCube("cntmap.fits")
124 self._test_cube(evt, 5542) 118 self._test_cube(evt, 5542)
125 119
126 # Prepare observation container for stacked analysis 120 # Prepare observation container for stacked analysis
127 - cta = gammalib.GCTAObservation(self.events_name) 121 + cta = gammalib.GCTAObservation(self._events_name)
128 obs = gammalib.GObservations() 122 obs = gammalib.GObservations()
129 cta.id("0001") 123 cta.id("0001")
130 obs.append(cta) 124 obs.append(cta)
test/test_ctobssim.py
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 # ========================================================================== 2 # ==========================================================================
3 -# This scripts performs unit tests for the ctobssim tool. 3 +# This scripts performs unit tests for the ctobssim module.
4 # 4 #
5 # Copyright (C) 2014-2016 Juergen Knoedlseder 5 # Copyright (C) 2014-2016 Juergen Knoedlseder
6 # 6 #
@@ -20,15 +20,17 @@ @@ -20,15 +20,17 @@
20 # ========================================================================== 20 # ==========================================================================
21 import gammalib 21 import gammalib
22 import ctools 22 import ctools
  23 +from cscripts import obsutils
23 24
24 25
25 -# ============================ #  
26 -# Test class for ctobssim tool #  
27 -# ============================ # 26 +# ============================== #
  27 +# Test class for ctobssim module #
  28 +# ============================== #
28 class Test(gammalib.GPythonTestSuite): 29 class Test(gammalib.GPythonTestSuite):
29 """ 30 """
30 - Test class for ctobssim tool. 31 + Test class for ctobssim module.
31 """ 32 """
  33 +
32 # Constructor 34 # Constructor
33 def __init__(self): 35 def __init__(self):
34 """ 36 """
@@ -38,9 +40,9 @@ class Test(gammalib.GPythonTestSuite): @@ -38,9 +40,9 @@ class Test(gammalib.GPythonTestSuite):
38 gammalib.GPythonTestSuite.__init__(self) 40 gammalib.GPythonTestSuite.__init__(self)
39 41
40 # Set members 42 # Set members
41 - self.model_name = "data/crab.xml"  
42 - self.caldb = "irf"  
43 - self.irf = "cta_dummy_irf" 43 + self._model_name = "data/crab.xml"
  44 + self._caldb = "irf"
  45 + self._irf = "cta_dummy_irf"
44 46
45 # Return 47 # Return
46 return 48 return
@@ -54,23 +56,23 @@ class Test(gammalib.GPythonTestSuite): @@ -54,23 +56,23 @@ class Test(gammalib.GPythonTestSuite):
54 self.name("ctobssim") 56 self.name("ctobssim")
55 57
56 # Append tests 58 # Append tests
57 - self.append(self.test_functional, "Test ctobssim functionality")  
58 - self.append(self.test_container, "Test ctobssim on observation container") 59 + self.append(self._test_ctobssim, "Test ctobssim")
  60 + #self.append(self._test_container, "Test ctobssim on observation container")
59 61
60 # Return 62 # Return
61 return 63 return
62 64
63 - # Test ctobssim functionnality  
64 - def test_functional(self): 65 + # Test ctobssim
  66 + def _test_ctobssim(self):
65 """ 67 """
66 - Test ctobssim functionnality. 68 + Test ctobssim.
67 """ 69 """
68 # Set-up ctobssim 70 # Set-up ctobssim
69 sim = ctools.ctobssim() 71 sim = ctools.ctobssim()
70 - sim["inmodel"] = self.model_name 72 + sim["inmodel"] = self._model_name
71 sim["outevents"] = "events.fits" 73 sim["outevents"] = "events.fits"
72 - sim["caldb"] = self.caldb  
73 - sim["irf"] = self.irf 74 + sim["caldb"] = self._caldb
  75 + sim["irf"] = self._irf
74 sim["ra"] = 83.63 76 sim["ra"] = 83.63
75 sim["dec"] = 22.01 77 sim["dec"] = 22.01
76 sim["rad"] = 10.0 78 sim["rad"] = 10.0
@@ -80,154 +82,123 @@ class Test(gammalib.GPythonTestSuite): @@ -80,154 +82,123 @@ class Test(gammalib.GPythonTestSuite):
80 sim["emax"] = 100.0 82 sim["emax"] = 100.0
81 83
82 # Run tool 84 # Run tool
83 - self.test_try("Run ctobssim")  
84 - try:  
85 - sim.run()  
86 - self.test_try_success()  
87 - #except Exception as e:  
88 - # msg = "Exception occured in ctobssim: %s." % (e,)  
89 - except:  
90 - msg = "Exception occured in ctobssim."  
91 - self.test_try_failure(msg) 85 + sim.run()
92 86
93 - # Retrieve observation and check content  
94 - obs = gammalib.GCTAObservation(sim.obs()[0])  
95 - pnt = obs.pointing()  
96 - evt = obs.events()  
97 - self.test_assert(obs.instrument() == "CTA", "Observation not a CTA observation")  
98 - self.test_value(sim.obs().size(), 1, "There is not a single observation")  
99 - self.test_value(obs.ontime(), 1800.0, 1.0e-6, "Ontime is not 1800 sec")  
100 - self.test_value(obs.livetime(), 1710.0, 1.0e-6, "Livetime is not 1710 sec")  
101 - self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, "ROI Right Ascension is not 83.63 deg")  
102 - self.test_value(pnt.dir().dec_deg(), 22.01, 1.0e-6, "ROI Declination is not 22.01 deg")  
103 - self.test_value(evt.size(), 4119, "Number of events is not 4119") 87 + # Check content of observation
  88 + self._test_observation(sim)
  89 + self._test_list(sim.obs()[0].events(), 4119)
104 90
105 # Save events 91 # Save events
106 - self.test_try("Save events")  
107 - try:  
108 - sim.save()  
109 - self.test_try_success()  
110 - #except Exception as e:  
111 - # msg = "Exception occured in saving events: %s." % (e,)  
112 - except:  
113 - msg = "Exception occured in saving events."  
114 - self.test_try_failure(msg) 92 + sim.save()
  93 +
  94 + # Load counts cube and check content.
  95 + evt = gammalib.GCTAEventList("events.fits")
  96 + self._test_list(evt, 4119)
115 97
116 - # Test ctobssim on observation container  
117 - def test_container(self):  
118 - """  
119 - Test ctobssim on observation container.  
120 - """  
121 # Set-up observation container 98 # Set-up observation container
122 - obs = self.set_obs(4) 99 + pnts = [{'ra': 83.63, 'dec': 21.01},
  100 + {'ra': 84.63, 'dec': 22.01},
  101 + {'ra': 83.63, 'dec': 23.01},
  102 + {'ra': 82.63, 'dec': 22.01}]
  103 + obs = obsutils.set_obs_list(pnts, caldb=self._caldb, irf=self._irf)
123 104
124 - # Set-up ctobssim 105 + # Set-up ctobssim from observation container
125 sim = ctools.ctobssim(obs) 106 sim = ctools.ctobssim(obs)
126 sim["outevents"] = "sim_events.xml" 107 sim["outevents"] = "sim_events.xml"
127 - sim["inmodel"] = self.model_name 108 + sim["inmodel"] = self._model_name
  109 +
128 # Run tool 110 # Run tool
129 - self.test_try("Run ctobssim")  
130 - try:  
131 - sim.run()  
132 - self.test_try_success()  
133 - #except Exception as e:  
134 - # msg = "Exception occured in ctobssim: %s." % (e,)  
135 - except:  
136 - msg = "Exception occured in ctobssim."  
137 - self.test_try_failure(msg) 111 + sim.run()
138 112
139 # Retrieve observation and check content 113 # Retrieve observation and check content
140 - self.test_value(sim.obs().size(), 4, "There are not 4 observations") 114 + self._test_observation(sim, nobs=4, pnts=pnts)
  115 + self._test_list(sim.obs()[0].events(), 4063)
  116 + self._test_list(sim.obs()[1].events(), 4059)
  117 + self._test_list(sim.obs()[2].events(), 4010)
  118 + self._test_list(sim.obs()[3].events(), 4138)
141 119
142 # Save events 120 # Save events
143 - self.test_try("Save events") 121 + sim.save()
  122 +
  123 + # Load events
  124 + obs = gammalib.GObservations("sim_events.xml")
  125 +
  126 + # Retrieve observation and check content
  127 + self._test_list(obs[0].events(), 4063)
  128 + self._test_list(obs[1].events(), 4059)
  129 + self._test_list(obs[2].events(), 4010)
  130 + self._test_list(obs[3].events(), 4138)
  131 +
  132 + # Set-up ctobssim with invalid event file
  133 + sim = ctools.ctobssim()
  134 + sim["inmodel"] = "model_file_that_does_not_exist.xml"
  135 + sim["outevents"] = "events.fits"
  136 + sim["caldb"] = self._caldb
  137 + sim["irf"] = self._irf
  138 + sim["ra"] = 83.63
  139 + sim["dec"] = 22.01
  140 + sim["rad"] = 10.0
  141 + sim["tmin"] = 0.0
  142 + sim["tmax"] = 1800.0
  143 + sim["emin"] = 0.1
  144 + sim["emax"] = 100.0
  145 +
  146 + # Run ctbin tool
  147 + self.test_try("Run ctobssim with invalid model file")
144 try: 148 try:
145 - sim.save()  
146 - self.test_try_success()  
147 - #except Exception as e:  
148 - # msg = "Exception occured in saving events: %s." % (e,) 149 + sim.run()
  150 + self.test_try_failure()
149 except: 151 except:
150 - msg = "Exception occured in saving events."  
151 - self.test_try_failure(msg) 152 + self.test_try_success()
  153 +
  154 + # Return
  155 + return
152 156
153 - # Setup observation container  
154 - def set_obs(self, number): 157 + # Check observation
  158 + def _test_observation(self, ctobssim, nobs=1,
  159 + pnts=[{'ra': 83.63, 'dec': 22.01}]):
155 """ 160 """
156 - Setup observation container. 161 + Test content of an observation.
  162 +
  163 + Args:
  164 + ctobssim: ctobssim instance
  165 +
  166 + Kwargs:
  167 + nobs: Number of observations
  168 + pnts: List of pointing dictionaries
157 """ 169 """
158 - # Initialise empty observation container  
159 - obs = gammalib.GObservations()  
160 -  
161 - # Initialise first time and identifier  
162 - tstart = 0.0  
163 - offset = -float(number)/2.0  
164 -  
165 - # Loop over number of observations  
166 - for i in range(number):  
167 - id = "%6.6d" % i  
168 - obs_cta = self.set_one_obs(id, offset)  
169 - offset += 1.0  
170 - tstart += 1830.0  
171 - obs.append(obs_cta)  
172 -  
173 - # Return observation container  
174 - return obs  
175 -  
176 - # Setup one observation  
177 - def set_one_obs(self, id, offset, \  
178 - tstart=0.0, duration=1800.0, deadc=0.95, \  
179 - emin=0.1, emax=100.0, rad=5.0, \  
180 - irf="cta_dummy_irf", caldb="dummy"): 170 + # Test observation container
  171 + self.test_value(ctobssim.obs().size(), nobs,
  172 + "There is one observation")
  173 + for i in range(ctobssim.obs().size()):
  174 + obs = gammalib.GCTAObservation(ctobssim.obs()[i])
  175 + pnt = obs.pointing()
  176 + self.test_assert(obs.instrument() == "CTA",
  177 + "Observation is CTA observation")
  178 + self.test_value(obs.ontime(), 1800.0, 1.0e-6,
  179 + "Ontime is 1800 sec")
  180 + self.test_value(obs.livetime(), 1710.0, 1.0e-6,
  181 + "Livetime is 1710 sec")
  182 + self.test_value(pnt.dir().ra_deg(), pnts[i]['ra'], 1.0e-6,
  183 + "Pointing Right Ascension is "+str(pnts[i]['ra'])+" deg")
  184 + self.test_value(pnt.dir().dec_deg(), pnts[i]['dec'], 1.0e-6,
  185 + "Pointing Declination is "+str(pnts[i]['dec'])+" deg")
  186 +
  187 + # Return
  188 + return
  189 +
  190 + # Check event list
  191 + def _test_list(self, list, nevents):
181 """ 192 """
182 - Setup one observation for test purposes. 193 + Test content of event list."
  194 +
  195 + Args:
  196 + list: Event list
  197 + nevents: Expected number of events.
183 """ 198 """
184 - # Allocate CTA observation  
185 - obs_cta = gammalib.GCTAObservation()  
186 -  
187 - # Set calibration database  
188 - db = gammalib.GCaldb()  
189 - if (gammalib.dir_exists(self.caldb)):  
190 - db.rootdir(self.caldb)  
191 - else:  
192 - db.open("cta", self.caldb)  
193 -  
194 - # Set pointing direction  
195 - pntdir = gammalib.GSkyDir()  
196 - pntdir.radec_deg(83.63, 22.01+offset)  
197 - pnt = gammalib.GCTAPointing()  
198 - pnt.dir(pntdir)  
199 - obs_cta.pointing(pnt)  
200 -  
201 - # Set ROI  
202 - roi = gammalib.GCTARoi()  
203 - instdir = gammalib.GCTAInstDir()  
204 - instdir.dir(pntdir)  
205 - roi.centre(instdir)  
206 - roi.radius(rad)  
207 -  
208 - # Set GTI  
209 - gti = gammalib.GGti()  
210 - gti.append(gammalib.GTime(tstart), gammalib.GTime(tstart+duration))  
211 -  
212 - # Set energy boundaries  
213 - ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"), \  
214 - gammalib.GEnergy(emax, "TeV"))  
215 -  
216 - # Allocate event list  
217 - events = gammalib.GCTAEventList()  
218 - events.roi(roi)  
219 - events.gti(gti)  
220 - events.ebounds(ebounds)  
221 - obs_cta.events(events)  
222 -  
223 - # Set instrument response  
224 - obs_cta.response(self.irf, db)  
225 -  
226 - # Set ontime, livetime, and deadtime correction factor  
227 - obs_cta.ontime(duration)  
228 - obs_cta.livetime(duration*deadc)  
229 - obs_cta.deadc(deadc)  
230 - obs_cta.id(id)  
231 -  
232 - # Return CTA observation  
233 - return obs_cta 199 + # Test event list
  200 + self.test_value(list.size(), nevents, str(nevents)+" elements")
  201 + self.test_value(list.number(), nevents, str(nevents)+" events")
  202 +
  203 + # Return
  204 + return