Commit 7ea034c7befae2fbc18209bb6b16118d77d581f2

Authored by Jürgen Knödlseder
1 parent 3c202bd5

Enhance unit test of ctprob and ctphase tools

src/ctphase/ctphase.cpp
@@ -504,8 +504,8 @@ void ctphase::get_parameters(void) @@ -504,8 +504,8 @@ void ctphase::get_parameters(void)
504 * @exception GException::invalid_value 504 * @exception GException::invalid_value
505 * No events found in observation. 505 * No events found in observation.
506 * 506 *
507 - * Append phase information to events from a FITS file by adding a "PHASE"  
508 - * column to the FITS file. 507 + * Computes the phase for all events in an observation based on a temporal
  508 + * phase curve model.
509 ***************************************************************************/ 509 ***************************************************************************/
510 void ctphase::phase_events(GCTAObservation* obs) 510 void ctphase::phase_events(GCTAObservation* obs)
511 { 511 {
@@ -525,20 +525,29 @@ void ctphase::phase_events(GCTAObservation* obs) @@ -525,20 +525,29 @@ void ctphase::phase_events(GCTAObservation* obs)
525 // Continue only if there are events 525 // Continue only if there are events
526 if (events->size() > 0) { 526 if (events->size() > 0) {
527 527
528 - // Print a warning into the log file if there are some precision concerns 528 + // Get MJD information of observation
529 GCTAEventAtom* event = (*events)[0]; 529 GCTAEventAtom* event = (*events)[0];
530 double dt = std::abs(event->time() - m_phase.mjd()); 530 double dt = std::abs(event->time() - m_phase.mjd());
531 - if (((m_phase.f1() * dt) > 1.0e8) ||  
532 - (m_phase.f2() * dt * dt) > 1.0e8) {  
533 - log_string(NORMAL,"*** WARNING ***");  
534 - log_string(NORMAL," Values supplied for reference MJD and/or f1 and/or f2");  
535 - log_string(NORMAL," are large and may result in numerical precision issues.");  
536 - log_string(NORMAL," Consider using an ephemeris derived from a date closer");  
537 - log_string(NORMAL," to the data being analyzed.");  
538 - log_value(NORMAL, " Observation ID", obs->obs_id());  
539 - log_value(NORMAL, " Reference MJD", m_phase.mjd().mjd());  
540 - log_value(NORMAL, " First event MJD", event->time().mjd());  
541 - log_string(NORMAL,"*** WARNING ***"); 531 + double f1 = m_phase.f1() * dt;
  532 + double f2 = m_phase.f2() * dt * dt;
  533 +
  534 + // Log information about MJD of the observation
  535 + log_value(NORMAL, "Reference MJD", m_phase.mjd().mjd());
  536 + log_value(NORMAL, "First event MJD", event->time().mjd());
  537 + log_value(NORMAL, "First event offset", gammalib::str(dt)+" s");
  538 + log_value(NORMAL, "F1 * dt", gammalib::str(f1)+" Hz");
  539 + log_value(NORMAL, "F2 * dt * dt", gammalib::str(f2)+" Hz");
  540 +
  541 + // Print a warning into the log file if there are some precision
  542 + // concerns
  543 + if ((f1 > 1.0e8) || (f2 > 1.0e8)) {
  544 + std::string msg = "\nWARNING: Values supplied for reference MJD "
  545 + "and/or F1 and/or F2 are large and may\n"
  546 + " result in numerical precision issues. "
  547 + "Consider using an ephemeris derived\n"
  548 + " from a date closer to the data being "
  549 + "analyzed.\n";
  550 + log_string(TERSE, msg);
542 } 551 }
543 552
544 // Compute the phase for all events 553 // Compute the phase for all events
test/test_ctbutterfly.py
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 # ========================================================================== 2 # ==========================================================================
3 # This scripts performs unit tests for the ctbutterfly tool. 3 # This scripts performs unit tests for the ctbutterfly tool.
4 # 4 #
5 -# Copyright (C) 2014-2016 Michal Mayer 5 +# Copyright (C) 2014-2017 Michal Mayer
6 # 6 #
7 # This program is free software: you can redistribute it and/or modify 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 8 # it under the terms of the GNU General Public License as published by
@@ -144,6 +144,7 @@ class Test(test): @@ -144,6 +144,7 @@ class Test(test):
144 butterfly['srcname'] = 'Crab' 144 butterfly['srcname'] = 'Crab'
145 butterfly['caldb'] = self._caldb 145 butterfly['caldb'] = self._caldb
146 butterfly['irf'] = self._irf 146 butterfly['irf'] = self._irf
  147 + butterfly['method'] = 'ENVELOPE'
147 butterfly['emin'] = 0.1 148 butterfly['emin'] = 0.1
148 butterfly['emax'] = 100.0 149 butterfly['emax'] = 100.0
149 butterfly['fit'] = True 150 butterfly['fit'] = True
test/test_ctphase.py
@@ -191,6 +191,72 @@ class Test(test): @@ -191,6 +191,72 @@ class Test(test):
191 # Check result file 191 # Check result file
192 self._check_result_file('ctphase_py3.fits') 192 self._check_result_file('ctphase_py3.fits')
193 193
  194 + # Now run ctphase tool without a model file and a significant
  195 + # MJD offset
  196 + phase['inobs'] = self._events
  197 + phase['inmodel'] = 'NONE'
  198 + phase['srcname'] = 'NONE'
  199 + phase['mjd'] = 51500.0
  200 + phase['phase'] = 0.0
  201 + phase['f0'] = 1.0
  202 + phase['f1'] = 0.1
  203 + phase['f2'] = 0.0001
  204 + phase['outobs'] = 'ctphase_py4.fits'
  205 + phase['logfile'] = 'ctphase_py4.log'
  206 + phase['chatter'] = 3
  207 +
  208 + # Execute ctphase tool
  209 + phase.logFileOpen() # Make sure we get a log file
  210 + phase.execute()
  211 +
  212 + # Check result file
  213 + self._check_result_file('ctphase_py4.fits')
  214 +
  215 + # Test invalid source name
  216 + self.test_try('Test ctphase with invalid source name')
  217 + try:
  218 + test = ctools.ctphase()
  219 + test['inobs'] = self._events
  220 + test['inmodel'] = self._model_file
  221 + test['srcname'] = 'Venus'
  222 + test['outobs'] = 'ctphase_py5.fits'
  223 + test['logfile'] = 'ctphase_py5.log'
  224 + test.logFileOpen()
  225 + test.execute()
  226 + self.test_try_failure('Exception not thrown')
  227 + except (ValueError):
  228 + self.test_try_success()
  229 +
  230 + # Test invalid model type
  231 + self.test_try('Test ctphase with invalid model')
  232 + try:
  233 + test = ctools.ctphase()
  234 + test['inobs'] = self._events
  235 + test['inmodel'] = self._model
  236 + test['srcname'] = 'Background'
  237 + test['outobs'] = 'ctphase_py6.fits'
  238 + test['logfile'] = 'ctphase_py6.log'
  239 + test.logFileOpen()
  240 + test.execute()
  241 + self.test_try_failure('Exception not thrown')
  242 + except (ValueError):
  243 + self.test_try_success()
  244 +
  245 + # Test invalid model without temporal phase curve model
  246 + self.test_try('Test ctphase with invalid model')
  247 + try:
  248 + test = ctools.ctphase()
  249 + test['inobs'] = self._events
  250 + test['inmodel'] = self._model
  251 + test['srcname'] = 'Crab'
  252 + test['outobs'] = 'ctphase_py7.fits'
  253 + test['logfile'] = 'ctphase_py7.log'
  254 + test.logFileOpen()
  255 + test.execute()
  256 + self.test_try_failure('Exception not thrown')
  257 + except (ValueError):
  258 + self.test_try_success()
  259 +
194 # Return 260 # Return
195 return 261 return
196 262
test/test_ctprob.py
@@ -139,6 +139,7 @@ class Test(test): @@ -139,6 +139,7 @@ class Test(test):
139 prob['caldb'] = self._caldb 139 prob['caldb'] = self._caldb
140 prob['irf'] = self._irf 140 prob['irf'] = self._irf
141 prob['edisp'] = False 141 prob['edisp'] = False
  142 + prob['publish'] = True
142 prob['logfile'] = 'ctprob_py1.log' 143 prob['logfile'] = 'ctprob_py1.log'
143 prob['chatter'] = 2 144 prob['chatter'] = 2
144 145
@@ -189,6 +190,23 @@ class Test(test): @@ -189,6 +190,23 @@ class Test(test):
189 # Check result file 190 # Check result file
190 self._check_result_file('ctprob_py3_crab_events.fits') 191 self._check_result_file('ctprob_py3_crab_events.fits')
191 192
  193 + # Copy ctprob tool
  194 + cpy_prob = prob.copy()
  195 +
  196 + # Clear phase tool
  197 + prob.clear()
  198 +
  199 + # Execute copy
  200 + cpy_prob['prefix'] = 'ctprob_py4_'
  201 + cpy_prob['outobs'] = 'ctprob_py4_obs.xml'
  202 + cpy_prob['logfile'] = 'ctprob_py4.log'
  203 + cpy_prob['chatter'] = 4
  204 + cpy_prob.logFileOpen() # Needed to get a new log file
  205 + cpy_prob.execute()
  206 +
  207 + # Check result file
  208 + self._check_result_file('ctprob_py4_ctprob_py3_crab_events.fits')
  209 +
192 # Return 210 # Return
193 return 211 return
194 212