Commit d4811766c050674e41f77d574bc71c535fa3378b

Authored by Jürgen Knödlseder
1 parent 72e0b749

Add support for writing of XML observation definition files.

In case that the observation container holds more than a single observation, ctobssim now outputs an observation definition XML file in addition to the event files. The filenames of the event files will be stored in the observation definition XML file so that subsequent ctools can handle all observational data together.
ChangeLog
1 -2012-07-18 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> 1 +2012-07-21 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
2 2
3 * Version 0.5.0 released 3 * Version 0.5.0 released
4 ======================== 4 ========================
5 5
  6 + Add observation definition XML files support to ctobssim
  7 +
6 2012-03-08 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> 8 2012-03-08 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
7 9
8 * Version 0.4.0 released 10 * Version 0.4.0 released
@@ -2,7 +2,7 @@ New Features and Important Changes in ctools 00-05-00 @@ -2,7 +2,7 @@ New Features and Important Changes in ctools 00-05-00
2 2
3 Author(s) : Juergen Knoedlseder 3 Author(s) : Juergen Knoedlseder
4 4
5 -July 18, 2012 5 +July 21, 2012
6 6
7 7
8 1. Introduction 8 1. Introduction
@@ -23,7 +23,10 @@ None @@ -23,7 +23,10 @@ None
23 23
24 4. ctobssim - CTA observation simulation 24 4. ctobssim - CTA observation simulation
25 ---------------------------------------- 25 ----------------------------------------
26 -None 26 +If several observations are processed at once, ctobssim now outputs
  27 +an observation definition XML file and separate FITS files for each
  28 +observation. The filenames of the FITS files will be written to the
  29 +observation definition file.
27 30
28 31
29 5. ctselect - CTA event selection 32 5. ctselect - CTA event selection
src/ctobssim/ctobssim.cpp
@@ -202,9 +202,8 @@ void ctobssim::clear(void) @@ -202,9 +202,8 @@ void ctobssim::clear(void)
202 ***************************************************************************/ 202 ***************************************************************************/
203 void ctobssim::execute(void) 203 void ctobssim::execute(void)
204 { 204 {
205 - // Read ahead output filename so that it gets dumped correctly in the  
206 - // parameters log  
207 - m_outfile = (*this)["outfile"].filename(); 205 + // Signal that some parameters should be read ahead
  206 + m_read_ahead = true;
208 207
209 // Run the simulation 208 // Run the simulation
210 run(); 209 run();
@@ -226,8 +225,9 @@ void ctobssim::execute(void) @@ -226,8 +225,9 @@ void ctobssim::execute(void)
226 void ctobssim::run(void) 225 void ctobssim::run(void)
227 { 226 {
228 // Switch screen logging on in debug mode 227 // Switch screen logging on in debug mode
229 - if (logDebug()) 228 + if (logDebug()) {
230 log.cout(true); 229 log.cout(true);
  230 + }
231 231
232 // Get parameters 232 // Get parameters
233 get_parameters(); 233 get_parameters();
@@ -261,6 +261,9 @@ void ctobssim::run(void) @@ -261,6 +261,9 @@ void ctobssim::run(void)
261 } 261 }
262 } 262 }
263 263
  264 + // Initialise counters
  265 + int n_observations = 0;
  266 +
264 // Loop over all observation in the container 267 // Loop over all observation in the container
265 for (int i = 0; i < m_obs.size(); ++i) { 268 for (int i = 0; i < m_obs.size(); ++i) {
266 269
@@ -280,6 +283,9 @@ void ctobssim::run(void) @@ -280,6 +283,9 @@ void ctobssim::run(void)
280 } 283 }
281 } 284 }
282 285
  286 + // Increment counter
  287 + n_observations++;
  288 +
283 // Simulate source events 289 // Simulate source events
284 simulate_source(obs, m_obs.models()); 290 simulate_source(obs, m_obs.models());
285 291
@@ -290,18 +296,35 @@ void ctobssim::run(void) @@ -290,18 +296,35 @@ void ctobssim::run(void)
290 296
291 } // endfor: looped over observations 297 } // endfor: looped over observations
292 298
  299 + // If more than a single observation has been handled then make sure that
  300 + // an XML file will be used for storage
  301 + if (n_observations > 1) {
  302 + m_use_xml = true;
  303 + }
  304 +
293 // Return 305 // Return
294 return; 306 return;
295 } 307 }
296 308
297 309
298 /***********************************************************************//** 310 /***********************************************************************//**
299 - * @brief Save simulated observation 311 + * @brief Save the selected event list(s)
300 * 312 *
301 - * This method saves the results. 313 + * This method saves the selected event list(s) into FITS file(s). There are
  314 + * two modes, depending on the m_use_xml flag.
302 * 315 *
303 - * @todo Implement XML file output in case that on observation list is  
304 - * provided on input. 316 + * If m_use_xml is true, all selected event list(s) will be saved into FITS
  317 + * files, where the output filenames are constructued from the input
  318 + * filenames by prepending the m_prefix string to name. Any path information
  319 + * will be stripped form the input name, hence event files will be written
  320 + * into the local working directory (unless some path information is present
  321 + * in the prefix). In addition, an XML file will be created that gathers
  322 + * the filename information for the selected event list(s). If an XML file
  323 + * was present on input, all metadata information will be copied from this
  324 + * input file.
  325 + *
  326 + * If m_use_xml is false, the selected event list will be saved into a FITS
  327 + * file.
305 ***************************************************************************/ 328 ***************************************************************************/
306 void ctobssim::save(void) 329 void ctobssim::save(void)
307 { 330 {
@@ -316,35 +339,15 @@ void ctobssim::save(void) @@ -316,35 +339,15 @@ void ctobssim::save(void)
316 } 339 }
317 } 340 }
318 341
319 - // Get output filename  
320 - m_outfile = (*this)["outfile"].filename();  
321 -  
322 - // Loop over all observation in the container  
323 - int file_num = 0;  
324 - for (int i = 0; i < m_obs.size(); ++i) {  
325 -  
326 - // Get CTA observation  
327 - GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]);  
328 -  
329 - // Save only if observation is a CTA observation  
330 - if (obs != NULL) {  
331 -  
332 - // Set filename. If more than one file will be created an  
333 - // index "_xxx" will be appended.  
334 - std::string filename = m_outfile;  
335 - if (file_num > 0) {  
336 - filename += "_"+str(file_num);  
337 - }  
338 -  
339 - // Save file  
340 - obs->save(filename, clobber());  
341 -  
342 - // Increment file number  
343 - file_num++;  
344 -  
345 - } // endif: observation was a CTA observation 342 + // Case A: Save event file(s) and XML metadata information
  343 + if (m_use_xml) {
  344 + save_xml();
  345 + }
346 346
347 - } // endfor: looped over files 347 + // Case B: Save event file as FITS file
  348 + else {
  349 + save_fits();
  350 + }
348 351
349 // Return 352 // Return
350 return; 353 return;
@@ -395,11 +398,26 @@ void ctobssim::get_parameters(void) @@ -395,11 +398,26 @@ void ctobssim::get_parameters(void)
395 // Load models into container 398 // Load models into container
396 m_obs.models(m_infile); 399 m_obs.models(m_infile);
397 400
  401 + // Signal that no XML file should be used for storage
  402 + m_use_xml = false;
  403 +
398 } // endif: there was no observation in the container 404 } // endif: there was no observation in the container
399 405
  406 + // ... otherwise we signal that an XML file should be written
  407 + else {
  408 + m_use_xml = true;
  409 + }
  410 +
400 // Get other parameters 411 // Get other parameters
401 m_seed = (*this)["seed"].integer(); 412 m_seed = (*this)["seed"].integer();
402 413
  414 + // Optionally read ahead parameters so that they get correctly
  415 + // dumped into the log file
  416 + if (m_read_ahead) {
  417 + m_outfile = (*this)["outfile"].value();
  418 + m_prefix = (*this)["prefix"].value();
  419 + }
  420 +
403 // Initialise random number generator 421 // Initialise random number generator
404 m_ran.seed(m_seed); 422 m_ran.seed(m_seed);
405 423
@@ -801,6 +819,7 @@ void ctobssim::init_members(void) @@ -801,6 +819,7 @@ void ctobssim::init_members(void)
801 // Initialise user parameters 819 // Initialise user parameters
802 m_infile.clear(); 820 m_infile.clear();
803 m_outfile.clear(); 821 m_outfile.clear();
  822 + m_prefix.clear();
804 m_caldb.clear(); 823 m_caldb.clear();
805 m_irf.clear(); 824 m_irf.clear();
806 m_seed = 1; 825 m_seed = 1;
@@ -815,6 +834,8 @@ void ctobssim::init_members(void) @@ -815,6 +834,8 @@ void ctobssim::init_members(void)
815 834
816 // Initialise protected members 835 // Initialise protected members
817 m_obs.clear(); 836 m_obs.clear();
  837 + m_use_xml = false;
  838 + m_read_ahead = false;
818 839
819 // Set fixed parameters 840 // Set fixed parameters
820 m_area = 19634954.0 * 1.0e4; //!< pi*(2500^2) m^2 841 m_area = 19634954.0 * 1.0e4; //!< pi*(2500^2) m^2
@@ -838,6 +859,7 @@ void ctobssim::copy_members(const ctobssim&amp; app) @@ -838,6 +859,7 @@ void ctobssim::copy_members(const ctobssim&amp; app)
838 // Copy user parameters 859 // Copy user parameters
839 m_infile = app.m_infile; 860 m_infile = app.m_infile;
840 m_outfile = app.m_outfile; 861 m_outfile = app.m_outfile;
  862 + m_prefix = app.m_prefix;
841 m_caldb = app.m_caldb; 863 m_caldb = app.m_caldb;
842 m_irf = app.m_irf; 864 m_irf = app.m_irf;
843 m_seed = app.m_seed; 865 m_seed = app.m_seed;
@@ -851,10 +873,12 @@ void ctobssim::copy_members(const ctobssim&amp; app) @@ -851,10 +873,12 @@ void ctobssim::copy_members(const ctobssim&amp; app)
851 m_deadc = app.m_deadc; 873 m_deadc = app.m_deadc;
852 874
853 // Copy protected members 875 // Copy protected members
854 - m_area = app.m_area;  
855 - m_time_max = app.m_time_max;  
856 - m_ran = app.m_ran;  
857 - m_obs = app.m_obs; 876 + m_area = app.m_area;
  877 + m_time_max = app.m_time_max;
  878 + m_ran = app.m_ran;
  879 + m_obs = app.m_obs;
  880 + m_use_xml = app.m_use_xml;
  881 + m_read_ahead = app.m_read_ahead;
858 882
859 // Return 883 // Return
860 return; 884 return;
@@ -874,3 +898,78 @@ void ctobssim::free_members(void) @@ -874,3 +898,78 @@ void ctobssim::free_members(void)
874 // Return 898 // Return
875 return; 899 return;
876 } 900 }
  901 +
  902 +
  903 +/***********************************************************************//**
  904 + * @brief Save event list in FITS format.
  905 + *
  906 + * Save the event list as a FITS file. The filename of the FITS file is
  907 + * specified by the outfile parameter.
  908 + ***************************************************************************/
  909 +void ctobssim::save_fits(void)
  910 +{
  911 + // Get output filename
  912 + m_outfile = (*this)["outfile"].value();
  913 +
  914 + // Get CTA observation from observation container
  915 + GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[0]);
  916 +
  917 + // Save observation into FITS file
  918 + obs->save(m_outfile, clobber());
  919 +
  920 + // Return
  921 + return;
  922 +}
  923 +
  924 +
  925 +/***********************************************************************//**
  926 + * @brief Save event list(s) in XML format.
  927 + *
  928 + * Save the event list(s) into FITS files and write the file path information
  929 + * into a XML file. The filename of the XML file is specified by the outfile
  930 + * parameter, the filename(s) of the event lists are built by prepending a
  931 + * prefix to the input event list filenames. Any path present in the input
  932 + * filename will be stripped, i.e. the event list(s) will be written in the
  933 + * local working directory (unless a path is specified in the prefix).
  934 + ***************************************************************************/
  935 +void ctobssim::save_xml(void)
  936 +{
  937 + // Get output filename and prefix
  938 + m_outfile = (*this)["outfile"].value();
  939 + m_prefix = (*this)["prefix"].value();
  940 +
  941 + // Initialise file number
  942 + int file_num = 0;
  943 +
  944 + // Loop over all observation in the container
  945 + for (int i = 0; i < m_obs.size(); ++i) {
  946 +
  947 + // Get CTA observation
  948 + GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]);
  949 +
  950 + // Handle only CTA observations
  951 + if (obs != NULL) {
  952 +
  953 + // Set event output file name
  954 + std::string outfile = m_prefix + str(file_num) + ".fits";
  955 +
  956 + // Store output file name in observation
  957 + obs->eventfile(outfile);
  958 +
  959 + // Save observation into FITS file
  960 + obs->save(outfile, clobber());
  961 +
  962 + // Increment file number
  963 + file_num++;
  964 +
  965 +
  966 + } // endif: observation was a CTA observations
  967 +
  968 + } // endfor: looped over observations
  969 +
  970 + // Save observations in XML file
  971 + m_obs.save(m_outfile);
  972 +
  973 + // Return
  974 + return;
  975 +}
src/ctobssim/ctobssim.hpp
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
33 33
34 /* __Definitions _________________________________________________________ */ 34 /* __Definitions _________________________________________________________ */
35 #define CTOBSSIM_NAME "ctobssim" 35 #define CTOBSSIM_NAME "ctobssim"
36 -#define CTOBSSIM_VERSION "00-04-00" 36 +#define CTOBSSIM_VERSION "00-05-00"
37 37
38 38
39 /***********************************************************************//** 39 /***********************************************************************//**
@@ -75,10 +75,13 @@ protected: @@ -75,10 +75,13 @@ protected:
75 void init_members(void); 75 void init_members(void);
76 void copy_members(const ctobssim& app); 76 void copy_members(const ctobssim& app);
77 void free_members(void); 77 void free_members(void);
  78 + void save_fits(void);
  79 + void save_xml(void);
78 80
79 // User parameters 81 // User parameters
80 std::string m_infile; //!< Input model 82 std::string m_infile; //!< Input model
81 std::string m_outfile; //!< Output events file 83 std::string m_outfile; //!< Output events file
  84 + std::string m_prefix; //!< Prefix for multiple event lists
82 std::string m_caldb; //!< Calibration database repository 85 std::string m_caldb; //!< Calibration database repository
83 std::string m_irf; //!< Instrument response function 86 std::string m_irf; //!< Instrument response function
84 int m_seed; //!< Random number generator seed 87 int m_seed; //!< Random number generator seed
@@ -96,6 +99,8 @@ protected: @@ -96,6 +99,8 @@ protected:
96 GTime m_time_max; //!< Maximum length of time slice (sec) 99 GTime m_time_max; //!< Maximum length of time slice (sec)
97 GRan m_ran; //!< Random number generator 100 GRan m_ran; //!< Random number generator
98 GObservations m_obs; //!< Observation container 101 GObservations m_obs; //!< Observation container
  102 + bool m_use_xml; //!< Use XML file instead of FITS file
  103 + bool m_read_ahead; //!< Read ahead parameters
99 }; 104 };
100 105
101 #endif /* CTOBSSIM_HPP */ 106 #endif /* CTOBSSIM_HPP */
src/ctobssim/ctobssim.par
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 # ------------------------------------------------------------------------- # 5 # ------------------------------------------------------------------------- #
6 # # 6 # #
7 # File: ctobssim.par # 7 # File: ctobssim.par #
8 -# Version: 00-04-00 # 8 +# Version: 00-05-00 #
9 # # 9 # #
10 # Author: Jurgen Knodlseder # 10 # Author: Jurgen Knodlseder #
11 # Jurgen.Knodlseder@irap.omp.eu # 11 # Jurgen.Knodlseder@irap.omp.eu #
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 #================= 19 #=================
20 infile, f, a, "test/data/crab.xml",,, "Model" 20 infile, f, a, "test/data/crab.xml",,, "Model"
21 outfile,f, a, "events.fits",,, "Output event data file" 21 outfile,f, a, "events.fits",,, "Output event data file"
  22 +prefix, s, h, "sim_events_",,, "Prefix for event lists in observation definition file"
22 # 23 #
23 # Observation parameters 24 # Observation parameters
24 #======================= 25 #=======================
src/ctobssim/main.cpp
1 /*************************************************************************** 1 /***************************************************************************
2 * ctobssim - CTA observation simulator tool main code * 2 * ctobssim - CTA observation simulator tool main code *
3 * ----------------------------------------------------------------------- * 3 * ----------------------------------------------------------------------- *
4 - * copyright (C) 2011 by Jurgen Knodlseder * 4 + * copyright (C) 2011-2012 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- * 5 * ----------------------------------------------------------------------- *
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 *
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 /** 21 /**
22 * @file ctobssim.cpp 22 * @file ctobssim.cpp
23 * @brief CTA observation simulator tool main code 23 * @brief CTA observation simulator tool main code
24 - * @author J. Knodlseder 24 + * @author J. Knoedlseder
25 */ 25 */
26 26
27 /* __ Includes ___________________________________________________________ */ 27 /* __ Includes ___________________________________________________________ */