Commit 14199ef481b7817a9c76e13632f11dabeadba704

Authored by Michael Mayer
1 parent d063ccd1

Aligned ctbkgcube to ctexpcube and ctpsfcube by using the GCTACubeBackground::fill function

pyext/ctbkgcube.i
@@ -47,8 +47,7 @@ public: @@ -47,8 +47,7 @@ public:
47 void clear(void); 47 void clear(void);
48 void run(void); 48 void run(void);
49 void save(void); 49 void save(void);
50 - const GCTACubeBackground& bgcube(void) const;  
51 - const GCTAEventCube& cube(void) const; 50 + const GCTACubeBackground& cube(void) const;
52 const GModels& models(void) const; 51 const GModels& models(void) const;
53 }; 52 };
54 53
src/ctbkgcube/ctbkgcube.cpp
@@ -209,9 +209,6 @@ void ctbkgcube::run(void) @@ -209,9 +209,6 @@ void ctbkgcube::run(void)
209 log.cout(true); 209 log.cout(true);
210 } 210 }
211 211
212 - // Initialise run parameters  
213 - m_livetime = 0.0;  
214 -  
215 // Get task parameters 212 // Get task parameters
216 get_parameters(); 213 get_parameters();
217 214
@@ -248,9 +245,9 @@ void ctbkgcube::run(void) @@ -248,9 +245,9 @@ void ctbkgcube::run(void)
248 245
249 // Copy models from observation container and reset output model 246 // Copy models from observation container and reset output model
250 // container 247 // container
  248 + GModels models_orig = m_obs.models();
251 m_bkgmdl = m_obs.models(); 249 m_bkgmdl = m_obs.models();
252 m_outmdl.clear(); 250 m_outmdl.clear();
253 - m_cube_model = -1;  
254 251
255 // Remove all models that are not CTA background models from the 252 // Remove all models that are not CTA background models from the
256 // container and put all removed components in the output 253 // container and put all removed components in the output
@@ -309,59 +306,11 @@ void ctbkgcube::run(void) @@ -309,59 +306,11 @@ void ctbkgcube::run(void)
309 log.header1("Generate background cube"); 306 log.header1("Generate background cube");
310 } 307 }
311 308
312 - // Initialise observation counter  
313 - int n_observations = 0;  
314 -  
315 - // Loop over all observations in the container  
316 - for (int i = 0; i < m_obs.size(); ++i) {  
317 -  
318 - // Get CTA observation  
319 - GCTAObservation* obs = dynamic_cast<GCTAObservation*>(m_obs[i]);  
320 -  
321 - // Continue only if observation is a CTA observation  
322 - if (obs != NULL) {  
323 -  
324 - // Write header for observation  
325 - if (logTerse()) {  
326 - if (obs->name().length() > 1) {  
327 - log.header3("Observation "+obs->name());  
328 - }  
329 - else {  
330 - log.header3("Observation "+gammalib::str(i));  
331 - }  
332 - }  
333 -  
334 - // Increment number of observations  
335 - n_observations++;  
336 -  
337 - // Fill the cube  
338 - fill_cube(obs);  
339 -  
340 - } // endif: CTA observation found  
341 -  
342 - } // endfor: looped over observations  
343 -  
344 - // Re-normalize cube to get units of counts/MeV/s/sr  
345 - if (m_livetime > 0.0) {  
346 -  
347 - // Loop over all bins in background cube and divide the content  
348 - // by the total livetime.  
349 - for (int i = 0; i < m_bkgcube.size(); ++i) {  
350 - GCTAEventBin* bin = m_bkgcube[i];  
351 - double rate = bin->counts() / m_livetime;  
352 - bin->counts(rate);  
353 - }  
354 -  
355 - } // endif: livetime was positive  
356 -  
357 - // Log results  
358 - if (logTerse()) {  
359 - log << gammalib::parformat("Number of observations");  
360 - log << n_observations << std::endl;  
361 - } 309 + // Assign background models to container
  310 + m_obs.models(m_bkgmdl);
362 311
363 - // Set cube background response  
364 - m_background.set(m_bkgcube.map(), m_bkgcube.ebounds()); 312 + // Fill background cube from observations
  313 + m_background.fill(m_obs);
365 314
366 // Create a background model for the output background cube and append 315 // Create a background model for the output background cube and append
367 // that model to the input model in place of the original 316 // that model to the input model in place of the original
@@ -391,6 +340,9 @@ void ctbkgcube::run(void) @@ -391,6 +340,9 @@ void ctbkgcube::run(void)
391 log << m_outmdl << std::endl; 340 log << m_outmdl << std::endl;
392 } 341 }
393 342
  343 + // Recover original models
  344 + m_obs.models(models_orig);
  345 +
394 // Return 346 // Return
395 return; 347 return;
396 } 348 }
@@ -444,13 +396,10 @@ void ctbkgcube::init_members(void) @@ -444,13 +396,10 @@ void ctbkgcube::init_members(void)
444 m_outcube.clear(); 396 m_outcube.clear();
445 m_outmodel.clear(); 397 m_outmodel.clear();
446 m_obs.clear(); 398 m_obs.clear();
447 - m_bkgcube.clear();  
448 m_background.clear(); 399 m_background.clear();
449 m_bkgmdl.clear(); 400 m_bkgmdl.clear();
450 m_outmdl.clear(); 401 m_outmdl.clear();
451 m_ebounds.clear(); 402 m_ebounds.clear();
452 - m_livetime = 0.0;  
453 - m_cube_model = -1;  
454 403
455 // Return 404 // Return
456 return; 405 return;
@@ -468,13 +417,10 @@ void ctbkgcube::copy_members(const ctbkgcube&amp; app) @@ -468,13 +417,10 @@ void ctbkgcube::copy_members(const ctbkgcube&amp; app)
468 m_outmodel = app.m_outmodel; 417 m_outmodel = app.m_outmodel;
469 m_outcube = app.m_outcube; 418 m_outcube = app.m_outcube;
470 m_obs = app.m_obs; 419 m_obs = app.m_obs;
471 - m_bkgcube = app.m_bkgcube;  
472 m_background = app.m_background; 420 m_background = app.m_background;
473 m_bkgmdl = app.m_bkgmdl; 421 m_bkgmdl = app.m_bkgmdl;
474 m_outmdl = app.m_outmdl; 422 m_outmdl = app.m_outmdl;
475 m_ebounds = app.m_ebounds; 423 m_ebounds = app.m_ebounds;
476 - m_livetime = app.m_livetime;  
477 - m_cube_model = app.m_cube_model;  
478 424
479 // Return 425 // Return
480 return; 426 return;
@@ -522,23 +468,18 @@ void ctbkgcube::get_parameters(void) @@ -522,23 +468,18 @@ void ctbkgcube::get_parameters(void)
522 (gammalib::strip_whitespace(incube) == "")) { 468 (gammalib::strip_whitespace(incube) == "")) {
523 469
524 // Create an event cube based on task parameters 470 // Create an event cube based on task parameters
525 - m_bkgcube = create_cube(m_obs); 471 + GCTAEventCube cube = create_cube(m_obs);
  472 +
  473 + // Define background cube
  474 + m_background = GCTACubeBackground(cube);
526 475
527 } // endif: filename was not valid 476 } // endif: filename was not valid
528 477
529 // ... otherwise setup the background cube from the counts map 478 // ... otherwise setup the background cube from the counts map
530 else { 479 else {
531 -  
532 - // Load event cube from filename  
533 - m_bkgcube.load(incube);  
534 - // Loop over all bins in background cube  
535 - for (int i = 0; i < m_bkgcube.size(); ++i) {  
536 480
537 - // Get event bin  
538 - GCTAEventBin* bin = m_bkgcube[i];  
539 - bin->counts(0.0);  
540 -  
541 - } 481 + // Define background cube from file
  482 + m_background = GCTACubeBackground(incube);
542 483
543 } // endelse: cube was loaded from file 484 } // endelse: cube was loaded from file
544 485
@@ -557,8 +498,8 @@ void ctbkgcube::get_parameters(void) @@ -557,8 +498,8 @@ void ctbkgcube::get_parameters(void)
557 m_obs.models(models); 498 m_obs.models(models);
558 } 499 }
559 500
560 - // Get energy definition  
561 - m_ebounds = m_bkgcube.ebounds(); 501 +// // Get energy definition
  502 +// m_ebounds = m_background.ebounds();
562 503
563 // Read output filenames (if needed) 504 // Read output filenames (if needed)
564 if (read_ahead()) { 505 if (read_ahead()) {
@@ -569,92 +510,3 @@ void ctbkgcube::get_parameters(void) @@ -569,92 +510,3 @@ void ctbkgcube::get_parameters(void)
569 // Return 510 // Return
570 return; 511 return;
571 } 512 }
572 -  
573 -/***********************************************************************//**  
574 - * @brief Generate background cube  
575 - *  
576 - * @param[in] obs Pointer to CTA observation.  
577 - *  
578 - * Fills the background cube with the model value for a CTA observation.  
579 - * This method will not need to load the event file as it gets the event  
580 - * ROI and GTI directly from the observation.  
581 - ***************************************************************************/  
582 -void ctbkgcube::fill_cube(GCTAObservation* obs)  
583 -{  
584 -  
585 - // Continue only if observation pointer is valid  
586 - if (obs != NULL) {  
587 -  
588 - // Initialise statistics  
589 - double sum = 0.0;  
590 - int n_bins_inside = 0;  
591 - int n_bins_outside = 0;  
592 -  
593 - // Extract region of interest from CTA observation  
594 - GCTARoi roi = obs->roi();  
595 -  
596 - // Set GTI of actual observations as the GTI of the event cube  
597 - m_bkgcube.gti(obs->gti());  
598 -  
599 - // Get observation livetime  
600 - double livetime = obs->livetime();  
601 -  
602 - // Loop over all bins in background cube  
603 - for (int i = 0; i < m_bkgcube.size(); ++i) {  
604 -  
605 - // Get event bin  
606 - GCTAEventBin* bin = m_bkgcube[i];  
607 -  
608 - // Continue only if binned in contained in ROI  
609 - if (roi.contains(*bin)) {  
610 -  
611 - // Compute model value for event bin. The model value is  
612 - // given in counts/MeV/s/sr.  
613 - double model = m_bkgmdl.eval(*bin, *obs);  
614 -  
615 - // Compute number events  
616 - sum += model * bin->size();  
617 -  
618 - // Multiply by livetime to get the correct weighting for  
619 - // each observation. We divide by the total livetime later  
620 - // to get the background model in units of counts/MeV/s/sr.  
621 - model *= livetime;  
622 -  
623 - // Add existing number of counts  
624 - model += bin->counts();  
625 -  
626 - // Store cumulated value (units: counts/MeV/sr)  
627 - bin->counts(model);  
628 -  
629 - // Increment contained bin counter  
630 - n_bins_inside++;  
631 -  
632 - } // endif: bin was contained in RoI  
633 -  
634 - // ... otherwise count bin as an outsider  
635 - else {  
636 - n_bins_outside++;  
637 - }  
638 -  
639 - } // endfor: looped over all bins  
640 -  
641 - // Accumulate livetime  
642 - m_livetime += livetime;  
643 -  
644 - // Log results  
645 - if (logTerse()) {  
646 - log << gammalib::parformat("Bins within RoI");  
647 - log << n_bins_inside << std::endl;  
648 - log << gammalib::parformat("Bins outside RoI");  
649 - log << n_bins_outside << std::endl;  
650 - log << gammalib::parformat("Background events in cube");  
651 - log << sum << std::endl;  
652 - log << gammalib::parformat("Cube livetime");  
653 - log << livetime << " sec" << std::endl;  
654 - }  
655 -  
656 - } // endif: observation pointer was not valid  
657 -  
658 - // Return  
659 - return;  
660 -}  
src/ctbkgcube/ctbkgcube.hpp
@@ -59,8 +59,7 @@ public: @@ -59,8 +59,7 @@ public:
59 void clear(void); 59 void clear(void);
60 void run(void); 60 void run(void);
61 void save(void); 61 void save(void);
62 - const GCTACubeBackground& bgcube(void) const;  
63 - const GCTAEventCube& cube(void) const; 62 + const GCTACubeBackground& cube(void) const;
64 const GModels& models(void) const; 63 const GModels& models(void) const;
65 64
66 protected: 65 protected:
@@ -75,27 +74,13 @@ protected: @@ -75,27 +74,13 @@ protected:
75 std::string m_outcube; //!< Filename of output cube 74 std::string m_outcube; //!< Filename of output cube
76 std::string m_outmodel; //!< Filename of output XML model 75 std::string m_outmodel; //!< Filename of output XML model
77 GObservations m_obs; //!< Observation container 76 GObservations m_obs; //!< Observation container
78 - GCTAEventCube m_bkgcube; //!< Background event cube  
79 GCTACubeBackground m_background; //!< Background cube response 77 GCTACubeBackground m_background; //!< Background cube response
80 GModels m_bkgmdl; //!< CTA background models 78 GModels m_bkgmdl; //!< CTA background models
81 GModels m_outmdl; //!< Output models 79 GModels m_outmdl; //!< Output models
82 GEbounds m_ebounds; //!< Energy boundaries 80 GEbounds m_ebounds; //!< Energy boundaries
83 - double m_livetime; //!< Total livetime for normalization  
84 - int m_cube_model; //!< Cube model index  
85 -};  
86 81
  82 +};
87 83
88 -/***********************************************************************//**  
89 - * @brief Return event cube containing background rate  
90 - *  
91 - * @return Background event cube.  
92 - ***************************************************************************/  
93 -inline  
94 -const GCTAEventCube& ctbkgcube::cube(void) const  
95 -{  
96 - // Return background cube  
97 - return (m_bkgcube);  
98 -}  
99 84
100 /***********************************************************************//** 85 /***********************************************************************//**
101 * @brief Return background response cube containing background rate 86 * @brief Return background response cube containing background rate
@@ -103,7 +88,7 @@ const GCTAEventCube&amp; ctbkgcube::cube(void) const @@ -103,7 +88,7 @@ const GCTAEventCube&amp; ctbkgcube::cube(void) const
103 * @return Background response cube. 88 * @return Background response cube.
104 ***************************************************************************/ 89 ***************************************************************************/
105 inline 90 inline
106 -const GCTACubeBackground& ctbkgcube::bgcube(void) const 91 +const GCTACubeBackground& ctbkgcube::cube(void) const
107 { 92 {
108 // Return background cube 93 // Return background cube
109 return (m_background); 94 return (m_background);