Commit 87e9392e155cd09d013f8ff8764aa9b40eb449be

Authored by Michael Mayer
1 parent a1ec394a

Introduced m_extname member to GCTAEventList to support writing to files includi…

…ng an extension name. Method extname() has also been added to set the extension name externally. GCTAEventList::save extracts and sets the extension name according to the input file name
inst/cta/include/GCTAEventList.hpp
... ... @@ -74,6 +74,7 @@ public:
74 74 virtual int number(void) const;
75 75 virtual void roi(const GRoi& roi);
76 76 virtual const GCTARoi& roi(void) const;
  77 + void extname(const std::string& extname);
77 78 std::string print(const GChatter& chatter = NORMAL) const;
78 79  
79 80 // Implement other methods
... ... @@ -101,9 +102,11 @@ protected:
101 102 std::vector<GCTAEventAtom> m_events; //!< Events
102 103 std::vector<GFitsTableCol*> m_columns; //!< Pointers to optional columns
103 104 std::string m_gti_extname; //!< GTI extension name
  105 + std::string m_extname; //!< Name of FITS extension
104 106 bool m_has_phase; //!< Signal presence of phase
105 107 bool m_has_detxy; //!< Signal presence of detector coordinates
106 108  
  109 +
107 110 // IRF cache for diffuse models
108 111 mutable std::vector<std::string> m_irf_names; //!< Model names
109 112 mutable std::vector<std::vector<double> > m_irf_values; //!< IRF values
... ... @@ -159,6 +162,21 @@ const GCTARoi&amp; GCTAEventList::roi(void) const
159 162  
160 163  
161 164 /***********************************************************************//**
  165 + * @brief Set extension name for event list
  166 + *
  167 + * @param[in] extname Name of FITS extension
  168 + *
  169 + * Sets the FITS extension name for writing
  170 + ***************************************************************************/
  171 +inline
  172 +void GCTAEventList::extname(const std::string& extname)
  173 +{
  174 + m_extname = extname;
  175 + return;
  176 +}
  177 +
  178 +
  179 +/***********************************************************************//**
162 180 * @brief Reserves space for events
163 181 *
164 182 * @param[in] number Number of events.
... ...
inst/cta/pyext/GCTAEventList.i
... ... @@ -56,6 +56,8 @@ public:
56 56 virtual int number(void) const;
57 57 virtual void roi(const GRoi& roi);
58 58 virtual const GCTARoi& roi(void) const;
  59 + void extname(const std::string& extname);
  60 +
59 61  
60 62 // Implement other methods
61 63 void append(const GCTAEventAtom& event);
... ...
inst/cta/src/GCTAEventList.cpp
... ... @@ -293,14 +293,20 @@ void GCTAEventList::load(const std::string&amp; filename)
293 293 void GCTAEventList::save(const std::string& filename,
294 294 const bool& clobber) const
295 295 {
  296 + // Initialise filename
  297 + GFilename fname(filename);
  298 +
  299 + // Set extension name (default: previous extension name)
  300 + const_cast<GCTAEventList*>(this)->extname(fname.extname(m_extname));
  301 +
296 302 // Create empty FITS file
297   - GFits fits;
  303 + GFits fits(fname.filename(), true);
298 304  
299 305 // Write event list
300 306 write(fits);
301 307  
302 308 // Save FITS file
303   - fits.saveto(filename, clobber);
  309 + fits.save(clobber);
304 310  
305 311 // Return
306 312 return;
... ... @@ -338,11 +344,11 @@ void GCTAEventList::read(const GFits&amp; fits)
338 344 // Initialise filename from fits file
339 345 GFilename fname = GFilename(fits.filename());
340 346  
341   - // Initialise extension name
342   - std::string extname = fname.extname("EVENTS");
  347 + // Store extension name
  348 + m_extname = fname.extname("EVENTS");
343 349  
344 350 // Get event list HDU
345   - const GFitsTable& events = *fits.table(extname);
  351 + const GFitsTable& events = *fits.table(m_extname);
346 352  
347 353 // Load event data
348 354 read_events(events);
... ... @@ -406,9 +412,13 @@ void GCTAEventList::read(const GFits&amp; fits)
406 412 ***************************************************************************/
407 413 void GCTAEventList::write(GFits& file) const
408 414 {
  415 +
409 416 // Allocate FITS binary table HDU
410 417 GFitsBinTable* events = new GFitsBinTable;
411 418  
  419 + // Set extension name
  420 + events->extname(m_extname);
  421 +
412 422 // Write events
413 423 write_events(*events);
414 424  
... ... @@ -594,6 +604,7 @@ void GCTAEventList::init_members(void)
594 604 m_events.clear();
595 605 m_columns.clear();
596 606 m_gti_extname = "GTI"; //!< Default GTI extension name
  607 + m_extname = "EVENTS"; //!< Default events extension name
597 608 m_has_phase = false;
598 609 m_has_detxy = true;
599 610  
... ... @@ -617,6 +628,7 @@ void GCTAEventList::copy_members(const GCTAEventList&amp; list)
617 628 m_roi = list.m_roi;
618 629 m_events = list.m_events;
619 630 m_gti_extname = list.m_gti_extname;
  631 + m_extname = list.m_extname;
620 632 m_has_phase = list.m_has_phase;
621 633 m_has_detxy = list.m_has_detxy;
622 634  
... ... @@ -806,9 +818,6 @@ void GCTAEventList::read_events(const GFitsTable&amp; table)
806 818 ***************************************************************************/
807 819 void GCTAEventList::write_events(GFitsBinTable& hdu) const
808 820 {
809   - // Set extension name
810   - hdu.extname("EVENTS");
811   -
812 821 // If there are events then write them now
813 822 if (size() > 0) {
814 823  
... ...
inst/cta/src/GCTAObservation.cpp
... ... @@ -1227,21 +1227,21 @@ void GCTAObservation::read(const GFits&amp; fits)
1227 1227 void GCTAObservation::write(GFits& fits, const std::string& extname) const
1228 1228 {
1229 1229 // Get pointers on event list
1230   - const GCTAEventList* list = dynamic_cast<const GCTAEventList*>(events());
  1230 + GCTAEventList* list = dynamic_cast<GCTAEventList*>(const_cast<GEvents*>(events()));
1231 1231 const GCTAEventCube* cube = dynamic_cast<const GCTAEventCube*>(events());
1232 1232  
1233 1233 // Case A: Observation contains an event list
1234 1234 if (list != NULL) {
1235 1235  
  1236 + // Set event list extension name
  1237 + list->extname(extname);
  1238 +
1236 1239 // Write event list into FITS file. This method also writes
1237 1240 // the GTI if provided on loading.
1238 1241 list->write(fits);
1239 1242  
1240 1243 // Get reference to EVENTS HDU
1241   - GFitsHDU& hdu = *fits.at("EVENTS");
1242   -
1243   - // Change extension name
1244   - hdu.extname(extname);
  1244 + GFitsHDU& hdu = *fits.at(extname);
1245 1245  
1246 1246 // Write observation attributes
1247 1247 write_attributes(hdu);
... ... @@ -1379,13 +1379,13 @@ void GCTAObservation::save(const std::string&amp; filename, const bool&amp; clobber) con
1379 1379 std::string extname = fname.extname("EVENTS");
1380 1380  
1381 1381 // Create FITS file
1382   - GFits fits;
  1382 + GFits fits(fname.filename(), true);
1383 1383  
1384 1384 // Write data into FITS file
1385 1385 write(fits, extname);
1386 1386  
1387 1387 // Save FITS file
1388   - fits.saveto(fname.filename(), clobber);
  1388 + fits.save(clobber);
1389 1389  
1390 1390 // Return
1391 1391 return;
... ...
inst/cta/test/data/obs_unbinned_gti.xml deleted
1   -<observation_list title="observation library">
2   - <observation name="Crab" id="00001" instrument="CTA">
3   - <parameter name="EventList" file="$(PACKAGE_SOURCE)/inst/cta/test/data/crab_events_gti.fits[EVENTS2]"/>
4   - <parameter name="GoodTimeIntervals" file="$(PACKAGE_SOURCE)/inst/cta/test/data/crab_events.fits[GTI]"/>
5   - </observation>
6   -</observation_list>
inst/cta/test/test_CTA.cpp
... ... @@ -1300,7 +1300,10 @@ void TestGCTAObservation::test_unbinned_obs(void)
1300 1300 {
1301 1301 // Set filenames
1302 1302 const std::string file1 = "test_cta_obs_unbinned.xml";
1303   - const std::string file2 = "test_cta_obs_unbinned_gti.xml";
  1303 + const std::string evfile = "test_cta_obs_unbinned.fits[TESTEVENTS]";
  1304 +// const std::string file2 = "test_cta_obs_unbinned_gti.xml";
  1305 +
  1306 +
1304 1307 /*
1305 1308 const std::string evfile = "test_cta_obs_unbinned.fits[TESTEVENTS]";
1306 1309 const std::string gtifile = "test_cta_obs_unbinned.fits[TESTGTI]";
... ... @@ -1362,6 +1365,33 @@ void TestGCTAObservation::test_unbinned_obs(void)
1362 1365 test_try_failure(e);
1363 1366 }
1364 1367  
  1368 + // Save unbinned CTA observation with non-default extension names
  1369 + test_try("Save unbinned CTA observation with non-default extension names");
  1370 + try {
  1371 + run.load(cta_events);
  1372 + run.save(evfile, true);
  1373 + run.load(evfile);
  1374 + test_value(run.roi().centre().dir().ra_deg(), 83.63, 1.0e-10);
  1375 + test_value(run.roi().centre().dir().dec_deg(), 22.01, 1.0e-10);
  1376 + test_value(run.roi().radius(), 5.0, 1.0e-10);
  1377 + test_value(run.ebounds().emin().TeV(), 0.1, 1.0e-10);
  1378 + test_value(run.ebounds().emax().TeV(), 100.0, 1.0e-10);
  1379 + test_value(run.gti().tstart().convert(run.gti().reference()), 0.0, 1.0e-10);
  1380 + test_value(run.gti().tstop().convert(run.gti().reference()), 1800.0, 1.0e-10);
  1381 + test_value(run.ontime(), 1800.0, 1.0e-10);
  1382 + test_value(run.livetime(), 1710.0, 1.0e-10);
  1383 + test_value(run.deadc(), 0.95, 1.0e-10);
  1384 + test_value(run.ra_obj(), 0.0, 1.0e-10);
  1385 + test_value(run.dec_obj(), 0.0, 1.0e-10);
  1386 + test_value(run.obs_id(), 0);
  1387 + test_value(run.pointing().dir().ra_deg(), 83.63, 1.0e-10);
  1388 + test_value(run.pointing().dir().dec_deg(), 22.01, 1.0e-10);
  1389 + test_try_success();
  1390 + }
  1391 + catch (std::exception &e) {
  1392 + test_try_failure(e);
  1393 + }
  1394 +
1365 1395 /*
1366 1396 test_try("Save and load unbinned CTA observation");
1367 1397 try {
... ...