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 47 void clear(void);
48 48 void run(void);
49 49 void save(void);
50   - const GCTACubeBackground& bgcube(void) const;
51   - const GCTAEventCube& cube(void) const;
  50 + const GCTACubeBackground& cube(void) const;
52 51 const GModels& models(void) const;
53 52 };
54 53  
... ...
src/ctbkgcube/ctbkgcube.cpp
... ... @@ -209,9 +209,6 @@ void ctbkgcube::run(void)
209 209 log.cout(true);
210 210 }
211 211  
212   - // Initialise run parameters
213   - m_livetime = 0.0;
214   -
215 212 // Get task parameters
216 213 get_parameters();
217 214  
... ... @@ -248,9 +245,9 @@ void ctbkgcube::run(void)
248 245  
249 246 // Copy models from observation container and reset output model
250 247 // container
  248 + GModels models_orig = m_obs.models();
251 249 m_bkgmdl = m_obs.models();
252 250 m_outmdl.clear();
253   - m_cube_model = -1;
254 251  
255 252 // Remove all models that are not CTA background models from the
256 253 // container and put all removed components in the output
... ... @@ -309,59 +306,11 @@ void ctbkgcube::run(void)
309 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 315 // Create a background model for the output background cube and append
367 316 // that model to the input model in place of the original
... ... @@ -391,6 +340,9 @@ void ctbkgcube::run(void)
391 340 log << m_outmdl << std::endl;
392 341 }
393 342  
  343 + // Recover original models
  344 + m_obs.models(models_orig);
  345 +
394 346 // Return
395 347 return;
396 348 }
... ... @@ -444,13 +396,10 @@ void ctbkgcube::init_members(void)
444 396 m_outcube.clear();
445 397 m_outmodel.clear();
446 398 m_obs.clear();
447   - m_bkgcube.clear();
448 399 m_background.clear();
449 400 m_bkgmdl.clear();
450 401 m_outmdl.clear();
451 402 m_ebounds.clear();
452   - m_livetime = 0.0;
453   - m_cube_model = -1;
454 403  
455 404 // Return
456 405 return;
... ... @@ -468,13 +417,10 @@ void ctbkgcube::copy_members(const ctbkgcube&amp; app)
468 417 m_outmodel = app.m_outmodel;
469 418 m_outcube = app.m_outcube;
470 419 m_obs = app.m_obs;
471   - m_bkgcube = app.m_bkgcube;
472 420 m_background = app.m_background;
473 421 m_bkgmdl = app.m_bkgmdl;
474 422 m_outmdl = app.m_outmdl;
475 423 m_ebounds = app.m_ebounds;
476   - m_livetime = app.m_livetime;
477   - m_cube_model = app.m_cube_model;
478 424  
479 425 // Return
480 426 return;
... ... @@ -522,23 +468,18 @@ void ctbkgcube::get_parameters(void)
522 468 (gammalib::strip_whitespace(incube) == "")) {
523 469  
524 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 476 } // endif: filename was not valid
528 477  
529 478 // ... otherwise setup the background cube from the counts map
530 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 484 } // endelse: cube was loaded from file
544 485  
... ... @@ -557,8 +498,8 @@ void ctbkgcube::get_parameters(void)
557 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 504 // Read output filenames (if needed)
564 505 if (read_ahead()) {
... ... @@ -569,92 +510,3 @@ void ctbkgcube::get_parameters(void)
569 510 // Return
570 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 59 void clear(void);
60 60 void run(void);
61 61 void save(void);
62   - const GCTACubeBackground& bgcube(void) const;
63   - const GCTAEventCube& cube(void) const;
  62 + const GCTACubeBackground& cube(void) const;
64 63 const GModels& models(void) const;
65 64  
66 65 protected:
... ... @@ -75,27 +74,13 @@ protected:
75 74 std::string m_outcube; //!< Filename of output cube
76 75 std::string m_outmodel; //!< Filename of output XML model
77 76 GObservations m_obs; //!< Observation container
78   - GCTAEventCube m_bkgcube; //!< Background event cube
79 77 GCTACubeBackground m_background; //!< Background cube response
80 78 GModels m_bkgmdl; //!< CTA background models
81 79 GModels m_outmdl; //!< Output models
82 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 86 * @brief Return background response cube containing background rate
... ... @@ -103,7 +88,7 @@ const GCTAEventCube&amp; ctbkgcube::cube(void) const
103 88 * @return Background response cube.
104 89 ***************************************************************************/
105 90 inline
106   -const GCTACubeBackground& ctbkgcube::bgcube(void) const
  91 +const GCTACubeBackground& ctbkgcube::cube(void) const
107 92 {
108 93 // Return background cube
109 94 return (m_background);
... ...