Commit d664295a5886906fc25bfa7b7b952ddf857ccbd6

Authored by Jürgen Knödlseder
1 parent e8e73b77

Use additional ARF columns to store background and alpha vectors

inst/cta/include/GCTAOnOffObservation.hpp
... ... @@ -88,7 +88,6 @@ public:
88 88 const GPha& on_spec(void) const;
89 89 const GPha& off_spec(void) const;
90 90 const GArf& arf(void) const;
91   - const GArf& bgd(void) const;
92 91 const GRmf& rmf(void) const;
93 92 void fill(const GCTAObservation& obs);
94 93 void compute_response(const GCTAObservation& obs,
... ... @@ -107,19 +106,17 @@ protected:
107 106 double model_off(const GModels& models, int ibin, GVector* mod_grad) const;
108 107  
109 108 // Protected data members
110   - std::string m_instrument; //!< Instrument name
111   - GCTAResponse* m_response; //!< Pointer to instrument response functions
112   - double m_ontime; //!< Ontime (seconds)
113   - double m_livetime; //!< Livetime (seconds)
114   - double m_deadc; //!< Deadtime correction (livetime/ontime)
115   - GPha m_on_spec; //!< On counts spectrum
116   - GPha m_off_spec; //!< Off counts spectrum
117   - GArf m_arf; //!< Effective area vector
118   - GArf m_bgd; //!< Background rate vector
119   - GRmf m_rmf; //!< Energy dispersion matrix
120   - GSkyRegions m_on_regions; //!< Container of On region
121   - GSkyRegions m_off_regions; //!< Container of Off regions
122   - std::vector<double> m_alpha; //!< Ratios of On/Off exposure
  109 + std::string m_instrument; //!< Instrument name
  110 + GCTAResponse* m_response; //!< Pointer to IRFs
  111 + double m_ontime; //!< Ontime (seconds)
  112 + double m_livetime; //!< Livetime (seconds)
  113 + double m_deadc; //!< Deadtime correction (livetime/ontime)
  114 + GPha m_on_spec; //!< On counts spectrum
  115 + GPha m_off_spec; //!< Off counts spectrum
  116 + GArf m_arf; //!< Auxiliary Response Function vector
  117 + GRmf m_rmf; //!< Redistribution matrix
  118 + GSkyRegions m_on_regions; //!< Container of On region
  119 + GSkyRegions m_off_regions; //!< Container of Off regions
123 120 };
124 121  
125 122  
... ... @@ -267,18 +264,6 @@ const GArf&amp; GCTAOnOffObservation::arf(void) const
267 264  
268 265  
269 266 /***********************************************************************//**
270   - * @brief Return Background Rate
271   - *
272   - * @return Background Rate.
273   - ***************************************************************************/
274   -inline
275   -const GArf& GCTAOnOffObservation::bgd(void) const
276   -{
277   - return (m_bgd);
278   -}
279   -
280   -
281   -/***********************************************************************//**
282 267 * @brief Return Redistribution Matrix File
283 268 *
284 269 * @return Redistribution Matrix File.
... ... @@ -291,9 +276,9 @@ const GRmf&amp; GCTAOnOffObservation::rmf(void) const
291 276  
292 277  
293 278 /***********************************************************************//**
294   - * @brief Return Redistribution Matrix File
  279 + * @brief Return number of observed events
295 280 *
296   - * @return Redistribution Matrix File.
  281 + * @return Number of observed events.
297 282 ***************************************************************************/
298 283 inline
299 284 int GCTAOnOffObservation::nobserved(void) const
... ...
inst/cta/pyext/GCTAOnOffObservation.i
... ... @@ -95,7 +95,6 @@ public:
95 95 const GPha& on_spec(void) const;
96 96 const GPha& off_spec(void) const;
97 97 const GArf& arf(void) const;
98   - const GArf& bgd(void) const;
99 98 const GRmf& rmf(void) const;
100 99 void fill(const GCTAObservation& obs);
101 100 void compute_response(const GCTAObservation& obs,
... ...
inst/cta/src/GCTAOnOffObservation.cpp
... ... @@ -369,7 +369,7 @@ void GCTAOnOffObservation::read(const GXmlElement&amp; xml)
369 369 // Get filename
370 370 std::string filename = par->attribute("file");
371 371  
372   - // load on regions
  372 + // Load on regions
373 373 m_on_regions.load(filename);
374 374  
375 375 // Increase number of parameters
... ... @@ -395,7 +395,7 @@ void GCTAOnOffObservation::read(const GXmlElement&amp; xml)
395 395 // Get filename
396 396 std::string filename = par->attribute("file");
397 397  
398   - // load arf
  398 + // Load arf
399 399 m_arf.load(filename);
400 400  
401 401 // Increase number of parameters
... ... @@ -408,7 +408,7 @@ void GCTAOnOffObservation::read(const GXmlElement&amp; xml)
408 408 // Get filename
409 409 std::string filename = par->attribute("file");
410 410  
411   - // load Rmf
  411 + // Load Rmf
412 412 m_rmf.load(filename);
413 413  
414 414 // Increase number of parameters
... ... @@ -736,6 +736,9 @@ double GCTAOnOffObservation::likelihood(const GModels&amp; models,
736 736  
737 737 // Allocate working array
738 738 GVector colvar(npars);
  739 +
  740 + // Get reference to alpha parameters
  741 + const std::vector<double>& alpha = m_arf["ALPHA"];
739 742  
740 743 // Check that there is at least one parameter
741 744 if (npars > 0) {
... ... @@ -759,7 +762,7 @@ double GCTAOnOffObservation::likelihood(const GModels&amp; models,
759 762 double nbgd = model_off(models, i, &bgd_grad);
760 763  
761 764 // Skip bin if model is too small (avoids -Inf or NaN gradients)
762   - double nonpred = ngam + m_alpha[i] * nbgd;
  765 + double nonpred = ngam + alpha[i] * nbgd;
763 766 if ((nbgd <= minmod) || (nonpred <= minmod)) {
764 767 #if defined(G_LIKELIHOOD_DEBUG)
765 768 n_small_model++;
... ... @@ -783,10 +786,10 @@ double GCTAOnOffObservation::likelihood(const GModels&amp; models,
783 786 // Fill derivatives
784 787 double fa = non/nonpred;
785 788 double fb = fa/nonpred;
786   - double fc = m_alpha[i] * fb;
787   - double fd = fc * m_alpha[i] + noff/(nbgd*nbgd);
  789 + double fc = alpha[i] * fb;
  790 + double fd = fc * alpha[i] + noff/(nbgd*nbgd);
788 791 double sky_factor = 1.0 - fa;
789   - double bgd_factor = 1.0 + m_alpha[i] - m_alpha[i] * fa - noff/nbgd;
  792 + double bgd_factor = 1.0 + alpha[i] - alpha[i] * fa - noff/nbgd;
790 793  
791 794 // Loop over all parameters
792 795 for (int j = 0; j < npars; ++j) {
... ... @@ -977,11 +980,9 @@ void GCTAOnOffObservation::init_members(void)
977 980 m_on_spec.clear();
978 981 m_off_spec.clear();
979 982 m_arf.clear();
980   - m_bgd.clear();
981 983 m_rmf.clear();
982 984 m_on_regions.clear();
983 985 m_off_regions.clear();
984   - m_alpha.clear();
985 986  
986 987 // Return
987 988 return;
... ... @@ -1003,11 +1004,9 @@ void GCTAOnOffObservation::copy_members(const GCTAOnOffObservation&amp; obs)
1003 1004 m_on_spec = obs.m_on_spec;
1004 1005 m_off_spec = obs.m_off_spec;
1005 1006 m_arf = obs.m_arf;
1006   - m_bgd = obs.m_bgd;
1007 1007 m_rmf = obs.m_rmf;
1008 1008 m_on_regions = obs.m_on_regions;
1009 1009 m_off_regions = obs.m_off_regions;
1010   - m_alpha = obs.m_alpha;
1011 1010  
1012 1011 // Clone members
1013 1012 m_response = (obs.m_response != NULL) ? obs.m_response->clone() : NULL;
... ... @@ -1054,7 +1053,7 @@ void GCTAOnOffObservation::compute_alpha(const GCTAObservation&amp; obs)
1054 1053 if (nreco > 0) {
1055 1054  
1056 1055 // Initialise On/Off exposure ratios
1057   - m_alpha.assign(nreco, 0.0);
  1056 + std::vector<double> alpha(nreco, 0.0);
1058 1057  
1059 1058 // Get CTA response pointer. Throw an exception if no response is found
1060 1059 const GCTAResponseIrf* response =
... ... @@ -1130,10 +1129,13 @@ void GCTAOnOffObservation::compute_alpha(const GCTAObservation&amp; obs)
1130 1129  
1131 1130 // Compute alpha for this energy bin
1132 1131 if (aoff > 0.0) {
1133   - m_alpha[i] = aon/aoff;
  1132 + alpha[i] = aon/aoff;
1134 1133 }
1135 1134  
1136 1135 } // endfor: looped over reconstructed energies
  1136 +
  1137 + // Append alpha vector to ARF
  1138 + m_arf.append("ALPHA", alpha);
1137 1139  
1138 1140 } // endif: there were energy bins
1139 1141  
... ... @@ -1246,15 +1248,15 @@ void GCTAOnOffObservation::compute_bgd(const GCTAObservation&amp; obs)
1246 1248 // Continue only if there are spectral bins
1247 1249 int nreco = ereco.size();
1248 1250 if (nreco > 0) {
  1251 +
  1252 + // Initialise background rayes
  1253 + std::vector<double> background(nreco, 0.0);
1249 1254  
1250 1255 // Get CTA observation pointing direction, zenith, and azimuth
1251 1256 GCTAPointing obspnt(obs.pointing());
1252 1257 double zenith = obspnt.zenith();
1253 1258 double azimuth = obspnt.azimuth();
1254 1259  
1255   - // Initialize background rate array
1256   - m_bgd = GArf(ereco);
1257   -
1258 1260 // Get pointer on CTA IRF response
1259 1261 const GCTAResponseIrf* rsp =
1260 1262 dynamic_cast<const GCTAResponseIrf*>(obs.response());
... ... @@ -1296,12 +1298,16 @@ void GCTAOnOffObservation::compute_bgd(const GCTAObservation&amp; obs)
1296 1298 double logEreco = m_on_spec.ebounds().elogmean(i).log10TeV();
1297 1299  
1298 1300 // Get rate in events/s/MeV
1299   - m_bgd[i] += (*bgd)(logEreco, offdir.detx(), offdir.dety()) *
1300   - skyreg->solidangle();
  1301 + background[i] += (*bgd)(logEreco,
  1302 + offdir.detx(),
  1303 + offdir.dety()) * skyreg->solidangle();
1301 1304  
1302 1305 } // endfor: looped over energy bins
1303 1306  
1304 1307 } // endfor: looped over sky regions
  1308 +
  1309 + // Append background vector to ARF
  1310 + m_arf.append("BACKGROUND", background);
1305 1311  
1306 1312 } // endif: there were spectral bins
1307 1313  
... ... @@ -1557,6 +1563,9 @@ double GCTAOnOffObservation::model_off(const GModels&amp; models,
1557 1563  
1558 1564 // Continue only if bin number is in range and if there are parameters
1559 1565 if ((ibin < m_off_spec.size()) && (npars > 0)) {
  1566 +
  1567 + // Get reference to background rates
  1568 + const std::vector<double>& background = m_arf["BACKGROUND"];
1560 1569  
1561 1570 // Get energy bin mean and width
1562 1571 const GEnergy emean = m_off_spec.ebounds().elogmean(ibin);
... ... @@ -1564,7 +1573,7 @@ double GCTAOnOffObservation::model_off(const GModels&amp; models,
1564 1573  
1565 1574 // Compute normalisation factor
1566 1575 double exposure = m_off_spec.exposure();
1567   - double norm = m_bgd[ibin] * exposure * ewidth;
  1576 + double norm = background[ibin] * exposure * ewidth;
1568 1577  
1569 1578 // Loop over models
1570 1579 for (int j = 0; j < models.size(); ++j) {
... ...