From d4811766c050674e41f77d574bc71c535fa3378b Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 21 Jul 2012 00:26:56 +0200 Subject: [PATCH] Add support for writing of XML observation definition files. --- ChangeLog | 4 +++- NEWS | 7 +++++-- src/ctobssim/ctobssim.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------- src/ctobssim/ctobssim.hpp | 7 ++++++- src/ctobssim/ctobssim.par | 3 ++- src/ctobssim/main.cpp | 4 ++-- 6 files changed, 157 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6cddbda..177153a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,10 @@ -2012-07-18 Juergen Knoedlseder +2012-07-21 Juergen Knoedlseder * Version 0.5.0 released ======================== + Add observation definition XML files support to ctobssim + 2012-03-08 Juergen Knoedlseder * Version 0.4.0 released diff --git a/NEWS b/NEWS index ac942f8..eea9c74 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ New Features and Important Changes in ctools 00-05-00 Author(s) : Juergen Knoedlseder -July 18, 2012 +July 21, 2012 1. Introduction @@ -23,7 +23,10 @@ None 4. ctobssim - CTA observation simulation ---------------------------------------- -None +If several observations are processed at once, ctobssim now outputs +an observation definition XML file and separate FITS files for each +observation. The filenames of the FITS files will be written to the +observation definition file. 5. ctselect - CTA event selection diff --git a/src/ctobssim/ctobssim.cpp b/src/ctobssim/ctobssim.cpp index 57557f6..f09ca95 100644 --- a/src/ctobssim/ctobssim.cpp +++ b/src/ctobssim/ctobssim.cpp @@ -202,9 +202,8 @@ void ctobssim::clear(void) ***************************************************************************/ void ctobssim::execute(void) { - // Read ahead output filename so that it gets dumped correctly in the - // parameters log - m_outfile = (*this)["outfile"].filename(); + // Signal that some parameters should be read ahead + m_read_ahead = true; // Run the simulation run(); @@ -226,8 +225,9 @@ void ctobssim::execute(void) void ctobssim::run(void) { // Switch screen logging on in debug mode - if (logDebug()) + if (logDebug()) { log.cout(true); + } // Get parameters get_parameters(); @@ -261,6 +261,9 @@ void ctobssim::run(void) } } + // Initialise counters + int n_observations = 0; + // Loop over all observation in the container for (int i = 0; i < m_obs.size(); ++i) { @@ -280,6 +283,9 @@ void ctobssim::run(void) } } + // Increment counter + n_observations++; + // Simulate source events simulate_source(obs, m_obs.models()); @@ -290,18 +296,35 @@ void ctobssim::run(void) } // endfor: looped over observations + // If more than a single observation has been handled then make sure that + // an XML file will be used for storage + if (n_observations > 1) { + m_use_xml = true; + } + // Return return; } /***********************************************************************//** - * @brief Save simulated observation + * @brief Save the selected event list(s) * - * This method saves the results. + * This method saves the selected event list(s) into FITS file(s). There are + * two modes, depending on the m_use_xml flag. * - * @todo Implement XML file output in case that on observation list is - * provided on input. + * If m_use_xml is true, all selected event list(s) will be saved into FITS + * files, where the output filenames are constructued from the input + * filenames by prepending the m_prefix string to name. Any path information + * will be stripped form the input name, hence event files will be written + * into the local working directory (unless some path information is present + * in the prefix). In addition, an XML file will be created that gathers + * the filename information for the selected event list(s). If an XML file + * was present on input, all metadata information will be copied from this + * input file. + * + * If m_use_xml is false, the selected event list will be saved into a FITS + * file. ***************************************************************************/ void ctobssim::save(void) { @@ -316,35 +339,15 @@ void ctobssim::save(void) } } - // Get output filename - m_outfile = (*this)["outfile"].filename(); - - // Loop over all observation in the container - int file_num = 0; - for (int i = 0; i < m_obs.size(); ++i) { - - // Get CTA observation - GCTAObservation* obs = dynamic_cast(&m_obs[i]); - - // Save only if observation is a CTA observation - if (obs != NULL) { - - // Set filename. If more than one file will be created an - // index "_xxx" will be appended. - std::string filename = m_outfile; - if (file_num > 0) { - filename += "_"+str(file_num); - } - - // Save file - obs->save(filename, clobber()); - - // Increment file number - file_num++; - - } // endif: observation was a CTA observation + // Case A: Save event file(s) and XML metadata information + if (m_use_xml) { + save_xml(); + } - } // endfor: looped over files + // Case B: Save event file as FITS file + else { + save_fits(); + } // Return return; @@ -395,11 +398,26 @@ void ctobssim::get_parameters(void) // Load models into container m_obs.models(m_infile); + // Signal that no XML file should be used for storage + m_use_xml = false; + } // endif: there was no observation in the container + // ... otherwise we signal that an XML file should be written + else { + m_use_xml = true; + } + // Get other parameters m_seed = (*this)["seed"].integer(); + // Optionally read ahead parameters so that they get correctly + // dumped into the log file + if (m_read_ahead) { + m_outfile = (*this)["outfile"].value(); + m_prefix = (*this)["prefix"].value(); + } + // Initialise random number generator m_ran.seed(m_seed); @@ -801,6 +819,7 @@ void ctobssim::init_members(void) // Initialise user parameters m_infile.clear(); m_outfile.clear(); + m_prefix.clear(); m_caldb.clear(); m_irf.clear(); m_seed = 1; @@ -815,6 +834,8 @@ void ctobssim::init_members(void) // Initialise protected members m_obs.clear(); + m_use_xml = false; + m_read_ahead = false; // Set fixed parameters m_area = 19634954.0 * 1.0e4; //!< pi*(2500^2) m^2 @@ -838,6 +859,7 @@ void ctobssim::copy_members(const ctobssim& app) // Copy user parameters m_infile = app.m_infile; m_outfile = app.m_outfile; + m_prefix = app.m_prefix; m_caldb = app.m_caldb; m_irf = app.m_irf; m_seed = app.m_seed; @@ -851,10 +873,12 @@ void ctobssim::copy_members(const ctobssim& app) m_deadc = app.m_deadc; // Copy protected members - m_area = app.m_area; - m_time_max = app.m_time_max; - m_ran = app.m_ran; - m_obs = app.m_obs; + m_area = app.m_area; + m_time_max = app.m_time_max; + m_ran = app.m_ran; + m_obs = app.m_obs; + m_use_xml = app.m_use_xml; + m_read_ahead = app.m_read_ahead; // Return return; @@ -874,3 +898,78 @@ void ctobssim::free_members(void) // Return return; } + + +/***********************************************************************//** + * @brief Save event list in FITS format. + * + * Save the event list as a FITS file. The filename of the FITS file is + * specified by the outfile parameter. + ***************************************************************************/ +void ctobssim::save_fits(void) +{ + // Get output filename + m_outfile = (*this)["outfile"].value(); + + // Get CTA observation from observation container + GCTAObservation* obs = dynamic_cast(&m_obs[0]); + + // Save observation into FITS file + obs->save(m_outfile, clobber()); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Save event list(s) in XML format. + * + * Save the event list(s) into FITS files and write the file path information + * into a XML file. The filename of the XML file is specified by the outfile + * parameter, the filename(s) of the event lists are built by prepending a + * prefix to the input event list filenames. Any path present in the input + * filename will be stripped, i.e. the event list(s) will be written in the + * local working directory (unless a path is specified in the prefix). + ***************************************************************************/ +void ctobssim::save_xml(void) +{ + // Get output filename and prefix + m_outfile = (*this)["outfile"].value(); + m_prefix = (*this)["prefix"].value(); + + // Initialise file number + int file_num = 0; + + // Loop over all observation in the container + for (int i = 0; i < m_obs.size(); ++i) { + + // Get CTA observation + GCTAObservation* obs = dynamic_cast(&m_obs[i]); + + // Handle only CTA observations + if (obs != NULL) { + + // Set event output file name + std::string outfile = m_prefix + str(file_num) + ".fits"; + + // Store output file name in observation + obs->eventfile(outfile); + + // Save observation into FITS file + obs->save(outfile, clobber()); + + // Increment file number + file_num++; + + + } // endif: observation was a CTA observations + + } // endfor: looped over observations + + // Save observations in XML file + m_obs.save(m_outfile); + + // Return + return; +} diff --git a/src/ctobssim/ctobssim.hpp b/src/ctobssim/ctobssim.hpp index c1dc2fe..82f4610 100644 --- a/src/ctobssim/ctobssim.hpp +++ b/src/ctobssim/ctobssim.hpp @@ -33,7 +33,7 @@ /* __Definitions _________________________________________________________ */ #define CTOBSSIM_NAME "ctobssim" -#define CTOBSSIM_VERSION "00-04-00" +#define CTOBSSIM_VERSION "00-05-00" /***********************************************************************//** @@ -75,10 +75,13 @@ protected: void init_members(void); void copy_members(const ctobssim& app); void free_members(void); + void save_fits(void); + void save_xml(void); // User parameters std::string m_infile; //!< Input model std::string m_outfile; //!< Output events file + std::string m_prefix; //!< Prefix for multiple event lists std::string m_caldb; //!< Calibration database repository std::string m_irf; //!< Instrument response function int m_seed; //!< Random number generator seed @@ -96,6 +99,8 @@ protected: GTime m_time_max; //!< Maximum length of time slice (sec) GRan m_ran; //!< Random number generator GObservations m_obs; //!< Observation container + bool m_use_xml; //!< Use XML file instead of FITS file + bool m_read_ahead; //!< Read ahead parameters }; #endif /* CTOBSSIM_HPP */ diff --git a/src/ctobssim/ctobssim.par b/src/ctobssim/ctobssim.par index d82cfd4..32d1dbf 100644 --- a/src/ctobssim/ctobssim.par +++ b/src/ctobssim/ctobssim.par @@ -5,7 +5,7 @@ # ------------------------------------------------------------------------- # # # # File: ctobssim.par # -# Version: 00-04-00 # +# Version: 00-05-00 # # # # Author: Jurgen Knodlseder # # Jurgen.Knodlseder@irap.omp.eu # @@ -19,6 +19,7 @@ #================= infile, f, a, "test/data/crab.xml",,, "Model" outfile,f, a, "events.fits",,, "Output event data file" +prefix, s, h, "sim_events_",,, "Prefix for event lists in observation definition file" # # Observation parameters #======================= diff --git a/src/ctobssim/main.cpp b/src/ctobssim/main.cpp index 30023e7..1b0b031 100644 --- a/src/ctobssim/main.cpp +++ b/src/ctobssim/main.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * ctobssim - CTA observation simulator tool main code * * ----------------------------------------------------------------------- * - * copyright (C) 2011 by Jurgen Knodlseder * + * copyright (C) 2011-2012 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -21,7 +21,7 @@ /** * @file ctobssim.cpp * @brief CTA observation simulator tool main code - * @author J. Knodlseder + * @author J. Knoedlseder */ /* __ Includes ___________________________________________________________ */ -- libgit2 0.21.4