Commit 5f3ab77ebc6db3785edfbe7d7096ccc24c01f5d8

Authored by Jürgen Knödlseder
1 parent 8567f16c

Close temporary file properly to avoid file opening error. Use new GCTAObservati…

…on::load method to avoid creation of temporary file in ctselect::select_events method.
Showing 1 changed file with 15 additions and 18 deletions
src/ctselect/ctselect.cpp
@@ -40,6 +40,7 @@ @@ -40,6 +40,7 @@
40 /* __ Debug definitions __________________________________________________ */ 40 /* __ Debug definitions __________________________________________________ */
41 41
42 /* __ Coding definitions _________________________________________________ */ 42 /* __ Coding definitions _________________________________________________ */
  43 +//#define G_USE_MKSTEMP //!< Use mkstemp for temporary filename
43 44
44 45
45 /*========================================================================== 46 /*==========================================================================
@@ -291,10 +292,13 @@ void ctselect::run(void) @@ -291,10 +292,13 @@ void ctselect::run(void)
291 m_infiles[i] = obs->eventfile(); 292 m_infiles[i] = obs->eventfile();
292 293
293 // Get temporary file name 294 // Get temporary file name
294 - //std::string filename = std::tmpnam(NULL);  
295 - char tpl[] = "ctselectXXXXXX";  
296 - mkstemp(tpl); 295 + #if G_USE_MKSTEMP
  296 + char tpl[] = "ctselectXXXXXX";
  297 + int fileid = mkstemp(tpl);
297 std::string filename(tpl); 298 std::string filename(tpl);
  299 + #else
  300 + std::string filename = std::tmpnam(NULL);
  301 + #endif
298 302
299 // Save observation in temporary file 303 // Save observation in temporary file
300 obs->save(filename, true); 304 obs->save(filename, true);
@@ -317,6 +321,11 @@ void ctselect::run(void) @@ -317,6 +321,11 @@ void ctselect::run(void)
317 // Load observation from temporary file, including event selection 321 // Load observation from temporary file, including event selection
318 select_events(obs, filename); 322 select_events(obs, filename);
319 323
  324 + // Close temporary file
  325 + #if G_USE_MKSTEMP
  326 + close(fileid);
  327 + #endif
  328 +
320 // Remove temporary file 329 // Remove temporary file
321 std::remove(filename.c_str()); 330 std::remove(filename.c_str());
322 331
@@ -415,7 +424,7 @@ void ctselect::get_parameters(void) @@ -415,7 +424,7 @@ void ctselect::get_parameters(void)
415 try { 424 try {
416 425
417 // Load event list in CTA observation 426 // Load event list in CTA observation
418 - obs.load_unbinned(m_infile); 427 + obs.load(m_infile);
419 428
420 // Append CTA observation to container 429 // Append CTA observation to container
421 m_obs.append(obs); 430 m_obs.append(obs);
@@ -628,20 +637,8 @@ void ctselect::select_events(GCTAObservation* obs, const std::string&amp; filename) @@ -628,20 +637,8 @@ void ctselect::select_events(GCTAObservation* obs, const std::string&amp; filename)
628 } 637 }
629 else { 638 else {
630 639
631 - // Get temporary file name  
632 - //std::string tmpname = std::tmpnam(NULL);  
633 - char tpl[] = "ctselectXXXXXX";  
634 - mkstemp(tpl);  
635 - std::string tmpname(tpl);  
636 -  
637 - // Save FITS file to temporary file  
638 - file.saveto(tmpname, true);  
639 -  
640 - // Load observation from temporary file  
641 - obs->load_unbinned(tmpname);  
642 -  
643 - // Remove temporary file  
644 - std::remove(tmpname.c_str()); 640 + // Readload observation from FITS file
  641 + obs->load(file);
645 642
646 } 643 }
647 644