Commit 719953c4754c2832ecb1c4bf4e3577a59ef774e9

Authored by knodlseder
1 parent e4c14102

Correctly deal with the case that no events are selected. In this case an empty …

…FITS file will be written. Note that the empty FITS file will have no columns. This should probably be changed on the lower (GammaLib) level.
Showing 1 changed file with 36 additions and 10 deletions
src/ctselect/ctselect.cpp
@@ -34,6 +34,8 @@ @@ -34,6 +34,8 @@
34 34
35 /* __ Method name definitions ____________________________________________ */ 35 /* __ Method name definitions ____________________________________________ */
36 #define G_RUN "ctselect::run()" 36 #define G_RUN "ctselect::run()"
  37 +#define G_SELECT_EVENTS "ctselect::select_events(GCTAObservation*,"\
  38 + "std::string&)"
37 39
38 /* __ Debug definitions __________________________________________________ */ 40 /* __ Debug definitions __________________________________________________ */
39 41
@@ -469,7 +471,6 @@ void ctselect::get_parameters(void) @@ -469,7 +471,6 @@ void ctselect::get_parameters(void)
469 * interval [m_tmin, m_tmax]. If m_tmin=m_tmax=0, no time selection is 471 * interval [m_tmin, m_tmax]. If m_tmin=m_tmax=0, no time selection is
470 * performed. 472 * performed.
471 * 473 *
472 - * @todo Add an arbitrary selection string.  
473 * @todo Use INDEF instead of 0.0 for pointing as RA/DEC selection 474 * @todo Use INDEF instead of 0.0 for pointing as RA/DEC selection
474 ***************************************************************************/ 475 ***************************************************************************/
475 void ctselect::select_events(GCTAObservation* obs, const std::string& filename) 476 void ctselect::select_events(GCTAObservation* obs, const std::string& filename)
@@ -581,14 +582,42 @@ void ctselect::select_events(GCTAObservation* obs, const std::string& filename) @@ -581,14 +582,42 @@ void ctselect::select_events(GCTAObservation* obs, const std::string& filename)
581 log << file << std::endl; 582 log << file << std::endl;
582 } 583 }
583 584
584 - // Get temporary file name  
585 - std::string tmpname = std::tmpnam(NULL); 585 + // Check if we have an EVENTS HDU
  586 + if (!file.hashdu("EVENTS")) {
  587 + std::string message = "No \"EVENTS\" extension found in FITS file "+
  588 + expression+".";
  589 + throw GException::app_error(G_SELECT_EVENTS, message);
  590 + }
  591 +
  592 + // Determine number of events in EVENTS HDU
  593 + int nevents = file.table("EVENTS")->nrows();
  594 +
  595 + // If the selected event list is empty then append an empty event list
  596 + // to the observation. Otherwise load the data from the temporary file.
  597 + if (nevents < 1) {
  598 +
  599 + // Create empty event list
  600 + GCTAEventList eventlist;
586 601
587 - // Save FITS file to temporary file  
588 - file.saveto(tmpname, true); 602 + // Append list to observation
  603 + obs->events(&eventlist);
  604 +
  605 + }
  606 + else {
589 607
590 - // Load observation from temporary file  
591 - obs->load_unbinned(tmpname); 608 + // Get temporary file name
  609 + std::string tmpname = std::tmpnam(NULL);
  610 +
  611 + // Save FITS file to temporary file
  612 + file.saveto(tmpname, true);
  613 +
  614 + // Load observation from temporary file
  615 + obs->load_unbinned(tmpname);
  616 +
  617 + // Remove temporary file
  618 + std::remove(tmpname.c_str());
  619 +
  620 + }
592 621
593 // Get CTA event list pointer 622 // Get CTA event list pointer
594 GCTAEventList* list = (GCTAEventList*)(obs->events()); 623 GCTAEventList* list = (GCTAEventList*)(obs->events());
@@ -613,9 +642,6 @@ void ctselect::select_events(GCTAObservation* obs, const std::string&amp; filename) @@ -613,9 +642,6 @@ void ctselect::select_events(GCTAObservation* obs, const std::string&amp; filename)
613 ebounds.append(emin, emax); 642 ebounds.append(emin, emax);
614 list->ebounds(ebounds); 643 list->ebounds(ebounds);
615 644
616 - // Remove temporary file  
617 - std::remove(tmpname.c_str());  
618 -  
619 // Return 645 // Return
620 return; 646 return;
621 } 647 }