From 719953c4754c2832ecb1c4bf4e3577a59ef774e9 Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Fri, 9 Mar 2012 15:53:05 +0000 Subject: [PATCH] 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. --- src/ctselect/ctselect.cpp | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/ctselect/ctselect.cpp b/src/ctselect/ctselect.cpp index 8b96a9e..3dc3b20 100644 --- a/src/ctselect/ctselect.cpp +++ b/src/ctselect/ctselect.cpp @@ -34,6 +34,8 @@ /* __ Method name definitions ____________________________________________ */ #define G_RUN "ctselect::run()" +#define G_SELECT_EVENTS "ctselect::select_events(GCTAObservation*,"\ + "std::string&)" /* __ Debug definitions __________________________________________________ */ @@ -469,7 +471,6 @@ void ctselect::get_parameters(void) * interval [m_tmin, m_tmax]. If m_tmin=m_tmax=0, no time selection is * performed. * - * @todo Add an arbitrary selection string. * @todo Use INDEF instead of 0.0 for pointing as RA/DEC selection ***************************************************************************/ void ctselect::select_events(GCTAObservation* obs, const std::string& filename) @@ -581,14 +582,42 @@ void ctselect::select_events(GCTAObservation* obs, const std::string& filename) log << file << std::endl; } - // Get temporary file name - std::string tmpname = std::tmpnam(NULL); + // Check if we have an EVENTS HDU + if (!file.hashdu("EVENTS")) { + std::string message = "No \"EVENTS\" extension found in FITS file "+ + expression+"."; + throw GException::app_error(G_SELECT_EVENTS, message); + } + + // Determine number of events in EVENTS HDU + int nevents = file.table("EVENTS")->nrows(); + + // If the selected event list is empty then append an empty event list + // to the observation. Otherwise load the data from the temporary file. + if (nevents < 1) { + + // Create empty event list + GCTAEventList eventlist; - // Save FITS file to temporary file - file.saveto(tmpname, true); + // Append list to observation + obs->events(&eventlist); + + } + else { - // Load observation from temporary file - obs->load_unbinned(tmpname); + // Get temporary file name + std::string tmpname = std::tmpnam(NULL); + + // Save FITS file to temporary file + file.saveto(tmpname, true); + + // Load observation from temporary file + obs->load_unbinned(tmpname); + + // Remove temporary file + std::remove(tmpname.c_str()); + + } // Get CTA event list pointer GCTAEventList* list = (GCTAEventList*)(obs->events()); @@ -613,9 +642,6 @@ void ctselect::select_events(GCTAObservation* obs, const std::string& filename) ebounds.append(emin, emax); list->ebounds(ebounds); - // Remove temporary file - std::remove(tmpname.c_str()); - // Return return; } -- libgit2 0.21.4