77 #define G_CONSTRUCTOR1 "GCTAOnOffObservation::GCTAOnOffObservation(GPha&, "\
78 "GPha&, GArf&, GRmf&)"
79 #define G_CONSTRUCTOR2 "GCTAOnOffObservation(GObservations& obs)"
80 #define G_RESPONSE_SET "GCTAOnOffObservation::response(GResponse&)"
81 #define G_RESPONSE_GET "GCTAOnOffObservation::response()"
82 #define G_WRITE "GCTAOnOffObservation::write(GXmlElement&)"
83 #define G_READ "GCTAOnOffObservation::read(GXmlElement&)"
84 #define G_LIKELIHOOD "GCTAOnOffObservation::likelihood(GModels&, "\
85 "GOptimizerPars&, GMatrixSparse&, "\
86 "GVector&, double&, double&)"
87 #define G_SET "GCTAOnOffObservation::set(GCTAObservation&, GModelSpatial&, "\
88 "GSkyRegionMap&, GSkyRegionMap&)"
89 #define G_COMPUTE_ARF "GCTAOnOffObservation::compute_arf(GCTAObservation&, "\
90 "GModelSpatial&, GSkyRegionMap&)"
91 #define G_COMPUTE_ARF_CUT "GCTAOnOffObservation::compute_arf_cut("\
92 "GCTAObservation&, GModelSpatial&, GSkyRegionMap&)"
93 #define G_COMPUTE_BGD "GCTAOnOffObservation::compute_bgd(GCTAObservation&, "\
95 #define G_COMPUTE_ALPHA "GCTAOnOffObservation::compute_alpha("\
96 "GCTAObservation&, GSkyRegionMap&, GSkyRegionMap&)"
97 #define G_COMPUTE_RMF "GCTAOnOffObservation::compute_rmf(GCTAObservation&, "\
99 #define G_MODEL_BACKGROUND "GCTAOnOffObservation::model_background(GModels&)"
100 #define G_ARF_RADIUS_CUT "GCTAOnOffObservation::arf_radius_cut("\
101 "GCTAObservation&, GSkyRegions&)"
152 const std::string& instrument) :
248 const std::string& srcname,
253 const bool& use_model_bkg)
268 set(obs, obs, models, srcname, on, off, use_model_bkg);
304 const std::string& srcname,
309 const bool& use_model_bkg)
324 set(obs_on, obs_off, models, srcname, on, off, use_model_bkg);
401 double total_exposure_on = 0.0;
402 double total_exposure_off = 0.0;
412 for (
int i = 0; i < obs.
size(); ++i) {
426 if (instrument.empty()) {
455 m_arf = arf_stacked * exposure_on;
457 total_exposure_on = exposure_on;
458 total_exposure_off = exposure_off;
465 std::vector<double>& backresp =
m_off_spec[
"BACKRESP"];
466 for (
int k = 0; k < backresp.size(); ++k) {
467 backresp[k] *= exposure_off;
472 double background = onoff->
off_spec()[
"BACKRESP"][k];
474 double scale = alpha * background * exposure_off;
479 for (
int itrue = 0; itrue <
m_rmf.
ntrue(); ++itrue) {
500 std::string msg =
"Incompatible energy binning of On spectrum.";
506 std::string msg =
"Incompatible energy binning of Off spectrum.";
512 std::string msg =
"Incompatible energy binning of ARF.";
518 std::string msg =
"Incompatible true energy binning of RMF.";
522 std::string msg =
"Incompatible measured energy binning of RMF.";
532 double background = onoff->
off_spec()[
"BACKRESP"][k];
534 double scale = alpha * background * exposure_off;
539 m_arf += arf_stacked * exposure_on;
542 const std::vector<double>& src = onoff->
off_spec()[
"BACKRESP"];
543 std::vector<double>& dst =
m_off_spec[
"BACKRESP"];
544 for (
int k = 0; k < dst.size(); ++k) {
545 dst[k] += src[k] * exposure_off;
549 for (
int itrue = 0; itrue <
m_rmf.
ntrue(); ++itrue) {
550 double arf = arf_stacked[itrue] * exposure_on;
557 total_exposure_on += exposure_on;
558 total_exposure_off += exposure_off;
563 if (emin < emin_obs) {
566 if (emax > emax_obs) {
589 for (
int itrue = 0; itrue <
m_rmf.
ntrue(); ++itrue) {
599 if (total_exposure_on > 0.0) {
600 m_arf /= total_exposure_on;
604 if (total_exposure_off > 0.0) {
605 std::vector<double>& backresp =
m_off_spec[
"BACKRESP"];
606 for (
int k = 0; k < backresp.size(); ++k) {
607 backresp[k] /= total_exposure_off;
760 std::string msg =
"No valid response function found in CTA On/Off "
809 if ((statistic !=
"POISSON") &&
810 (statistic !=
"CSTAT") &&
811 (statistic !=
"WSTAT")) {
812 std::string msg =
"Invalid statistic \""+statistic+
"\" encountered "
813 "in observation definition XML file for "
815 "\""+xml.
attribute(
"id")+
"\". Only \"POISSON\" "
816 ", \"CSTAT\" or \"WSTAT\" are supported.";
926 if ((statistic ==
"POISSON") || (statistic ==
"CSTAT")) {
931 else if (statistic ==
"WSTAT") {
937 std::string msg =
"Invalid statistic \""+statistic+
"\" encountered. "
938 "Either specify \"POISSON\", \"CSTAT\" or "
960 int npars = models.
npars();
995 int npars = models.
npars();
1016 if ((statistic ==
"POISSON") || (statistic ==
"CSTAT")) {
1017 nbgd =
N_bgd(models, i, &dummy_grad);
1021 else if (statistic ==
"WSTAT") {
1032 double ngam =
N_gamma(models, i, &dummy_grad);
1035 double nonpred = 0.0;
1036 double dlogLdsky = 0.0;
1037 double d2logLdsky2 = 0.0;
1041 wstat_value(non, noff, alpha, ngam, nonpred, nbgd,
1042 dlogLdsky,d2logLdsky2);
1048 std::string msg =
"Invalid statistic \""+statistic+
"\" encountered. "
1049 "Either specify \"POISSON\", \"CSTAT\" or "
1079 result.append(
"=== GCTAOnOffObservation ===");
1198 std::string msg =
"On and Off spectra are incompatible.";
1204 std::string msg =
"Redistribution Matrix File is incompatible with "
1211 std::string msg =
"Redistribution Matrix File is incompatible with "
1212 "Auxiliary Response File.";
1244 for (
int itrue = 0; itrue < etrue.
size(); ++itrue) {
1245 if ((etrue.
emax(itrue) < emin) || (etrue.
emin(itrue) > emax)) {
1246 arf_stacked[itrue] = 0.0;
1284 std::vector<double> sums(nreco, 0.0);
1285 for (
int ireco = 0; ireco < nreco; ++ireco) {
1286 for (
int itrue = 0; itrue < etrue.
size(); ++itrue) {
1292 for (
int itrue = 0; itrue < etrue.
size(); ++itrue) {
1293 if ((etrue.
emax(itrue) < emin) || (etrue.
emin(itrue) > emax)) {
1294 for (
int ireco = 0; ireco < nreco; ++ireco) {
1301 for (
int ireco = 0; ireco < nreco; ++ireco) {
1303 for (
int itrue = 0; itrue < etrue.
size(); ++itrue) {
1307 double norm = sums[ireco] /
sum;
1308 for (
int itrue = 0; itrue < etrue.
size(); ++itrue) {
1346 const std::string& srcname,
1349 const bool& use_model_bkg)
1353 if (events_on == NULL) {
1354 std::string msg =
"No event list found in CTA On observation \""+
1355 obs_on.
name()+
"\" (ID="+obs_on.
id()+
"). ON/OFF observation "
1356 "can only be filled from event list.";
1360 if (events_off == NULL) {
1361 std::string msg =
"No event list found in CTA Off observation \""+
1362 obs_off.
name()+
"\" (ID="+obs_off.
id()+
"). ON/OFF observation "
1363 "can only be filled from event list.";
1369 if (model == NULL) {
1370 std::string msg =
"Model \""+srcname+
"\" is not a sky model. Please "
1371 "specify the name of a sky model.";
1379 if (use_model_bkg) {
1380 for (
int i = 0; i < models.
size(); ++i) {
1381 if ((dynamic_cast<const GModelData*>(models[i]) != NULL) &&
1382 (models[i]->
classname().substr(0,4) ==
"GCTA")) {
1383 bkg_models.
append(*models[i]);
1389 for (
int i = 0; i < events_on->
size(); ++i) {
1403 for (
int i = 0; i < events_off->
size(); ++i) {
1429 for (
int i = 0; i < on.
size(); ++i) {
1432 for (
int i = 0; i < off.
size(); ++i) {
1440 if (rad_max > 0.0) {
1446 compute_bgd(obs_off, reg_off, bkg_models, use_model_bkg);
1447 compute_alpha(obs_on, obs_off, reg_on, reg_off, bkg_models, use_model_bkg);
1461 std::string mission;
1463 std::string rspname;
1475 if (!mission.empty()) {
1480 if (!instrument.empty()) {
1485 if (!rspname.empty()) {
1490 if (rad_max > 0.0) {
1530 int ntrue = etrue.
size();
1548 for (
int i = 0; i < ntrue; ++i) {
1554 GSource source(
"", const_cast<GModelSpatial*>(&spatial), energy, time);
1560 for (
int k = 0; k < on.
size(); ++k) {
1576 double pixsolid = on_map->
map().solidangle(pixidx);
1581 event.energy(energy);
1589 m_arf[i] += irf * pixsolid;
1630 int ntrue = etrue.
size();
1641 double zenith = obspnt.
zenith();
1642 double azimuth = obspnt.
azimuth();
1645 for (
int i = 0; i < ntrue; ++i) {
1655 for (
int k = 0; k < on.
size(); ++k) {
1668 GSkyDir pixdir = on_map->
map().inx2dir(pixidx);
1671 double pixsolid = on_map->
map().solidangle(pixidx);
1674 double theta = obsdir.
dist(pixdir);
1675 double phi = obsdir.
posang(pixdir);
1680 logEtrue) * pixsolid;
1758 const bool& use_model_bkg)
1762 int nreco = ereco.
size();
1768 std::vector<double> background(nreco, 0.0);
1774 if (use_model_bkg) {
1777 for (
int k = 0; k < off.
size(); ++k) {
1791 GSkyDir pixdir = off_map->
map().inx2dir(pixidx);
1797 double pixsolid = off_map->
map().solidangle(pixidx);
1800 for (
int i = 0; i < nreco; ++i) {
1806 background[i] += models.
eval(event, obs) * pixsolid;
1820 for (
int k = 0; k < off.
size(); ++k) {
1834 GSkyDir pixdir = off_map->
map().inx2dir(pixidx);
1840 double pixsolid = off_map->
map().solidangle(pixidx);
1843 for (
int i = 0; i < nreco; ++i) {
1846 background[i] += pixsolid;
1927 const bool& use_model_bkg)
1931 int nreco = ereco.
size();
1942 if (use_model_bkg) {
1945 for (
int i = 0; i < nreco; ++i) {
1952 for (
int k = 0; k < on.
size(); ++k) {
1965 GSkyDir pixdir = on_map->
map().inx2dir(pixidx);
1971 double pixsolid = on_map->
map().solidangle(pixidx);
1977 aon += models.
eval(event, obs_on) * pixsolid;
1984 for (
int k = 0; k < off.
size(); ++k) {
1997 GSkyDir pixdir = off_map->
map().inx2dir(pixidx);
2003 double pixsolid = off_map->
map().solidangle(pixidx);
2009 aoff += models.
eval(event, obs_off) * pixsolid;
2016 double alpha = (aoff > 0.0) ? aon/aoff : 1.0;
2038 for (
int k = 0; k < on.
size(); ++k) {
2051 GSkyDir pixdir = on_map->
map().inx2dir(pixidx);
2057 aon += on_map->
map().solidangle(pixidx);
2064 for (
int k = 0; k < off.
size(); ++k) {
2077 GSkyDir pixdir = off_map->
map().inx2dir(pixidx);
2083 aoff += off_map->
map().solidangle(pixidx);
2090 double alpha = (aoff > 0.0) ? aon/aoff : 1.0;
2098 for (
int i = 0; i < nreco; ++i) {
2129 int ntrue = etrue.
size();
2130 int nreco = ereco.
size();
2133 if (ntrue > 0 && nreco > 0) {
2141 double zenith = obspnt.
zenith();
2142 double azimuth = obspnt.
azimuth();
2145 for (
int itrue = 0; itrue < ntrue; ++itrue) {
2146 for (
int ireco = 0; ireco < nreco; ++ireco) {
2147 m_rmf(itrue, ireco) = 0.0;
2155 for (
int k = 0; k < on.
size(); ++k) {
2167 GSkyDir pixdir = on_map->
map().inx2dir(pixidx);
2170 double theta = obsdir.
dist(pixdir);
2171 double phi = obsdir.
posang(pixdir);
2174 for (
int itrue = 0; itrue < ntrue; ++itrue) {
2180 double aeff = rsp.
aeff(theta, phi,
2185 for (
int ireco = 0; ireco < nreco; ++ireco) {
2194 m_rmf(itrue, ireco) += value * aeff;
2195 weight(itrue, ireco) += aeff;
2206 for (
int itrue = 0; itrue < ntrue; ++itrue) {
2207 for (
int ireco = 0; ireco < nreco; ++ireco) {
2208 if (weight(itrue, ireco) > 0.0) {
2209 m_rmf(itrue, ireco) /= weight(itrue, ireco);
2246 const GEnergy energy_margin(0.01,
"GeV");
2251 int ntrue = etrue.
size();
2252 int nreco = ereco.
size();
2258 std::vector<double>& background =
m_off_spec[
"BACKRESP"];
2261 for (
int ireco = 0; ireco < nreco; ++ireco) {
2263 ereco.
emax(ireco) - energy_margin)) {
2266 background[ireco] = 0.0;
2267 for (
int itrue = 0; itrue < ntrue; ++itrue) {
2268 m_rmf(itrue, ireco) = 0.0;
2297 double rad_max = 0.0;
2312 if (rad_max > 0.0) {
2317 for (
int i = 0; i < on.
size(); ++i) {
2322 if (region == NULL) {
2323 std::string msg =
"An effective area IRF with a theta "
2324 "cut was specified, but the On region "
2325 "is not a circle, hence the "
2326 "consistency of the event selection "
2327 "could not be checked. Please specify "
2328 "a circular On region if an effective "
2329 "area with a theta cut is used.";
2335 std::string msg =
"An effective area IRF with a theta "
2337 "was specified but an On region with "
2340 "was encountered. Please specify On "
2341 "regions with a radius of "+
2386 int npars = models.
npars();
2390 for (
int i = 0; i < npars; ++i) {
2395 if ((ibin >= 0) && (ibin <
m_on_spec.
size()) && (npars > 0)) {
2401 for (
int j = 0; j < models.
size(); ++j) {
2404 const GModel* mptr = models[j];
2412 ipar += mptr->
size();
2419 ipar += mptr->
size();
2425 if (spectral != NULL) {
2428 #if defined(G_N_GAMMA_DEBUG)
2429 double rmf_sum = 0.0;
2434 std::vector<int> inx_spec;
2435 for (
int k = 0; k < mptr->
size(); ++k) {
2438 inx_spec.push_back(k);
2446 for (
int itrue = 0; itrue <
m_arf.
size(); ++itrue) {
2461 #if defined(G_N_GAMMA_DEBUG)
2475 double norm_scale = arf * exposure *
rmf;
2476 double norm_flux = norm_scale * scale;
2477 double norm_grad = norm_flux * etruewidth;
2483 double flux = spectral->
flux(etruemin, etruemax);
2484 value += flux * norm_flux;
2490 spectral->
eval(etruemean,
GTime(),
true);
2493 for (
int k = 0; k < inx_spec.size(); ++k) {
2494 const GModelPar& par = (*mptr)[inx_spec[k]];
2495 if (par.
is_free() && ipar+k < npars) {
2502 for (
int k = 0; k < mptr->
size(); ++k) {
2506 (*grad)[ipar+k] += par.
scale() * flux * norm_scale;
2514 #if defined(G_N_GAMMA_DEBUG)
2515 std::cout <<
"sum(RMF) = " << rmf_sum << std::endl;
2521 ipar += mptr->
size();
2555 int npars = models.
npars();
2559 for (
int i = 0; i < npars; ++i) {
2564 if ((ibin >= 0) && (ibin <
m_on_spec.
size()) && (npars > 0)) {
2572 double background =
m_off_spec[
"BACKRESP"][ibin];
2575 if (background > 0.0) {
2579 double norm = background * exposure * ewidth;
2582 for (
int j = 0, ipar = 0; j < models.
size(); ++j) {
2585 const GModel* mptr = models[j];
2592 if (!mptr->
is_valid(this->instrument(), this->
id())) {
2593 ipar += mptr->
size();
2600 ipar += mptr->
size();
2608 if (spectral != NULL) {
2619 for (
int k = 0; k < mptr->
size(); ++k) {
2621 if (par.
is_free() && ipar+k < npars) {
2629 ipar += mptr->
size();
2747 double* npred)
const
2750 #if defined(G_LIKELIHOOD_DEBUG)
2752 double t_start = omp_get_wtime();
2754 clock_t t_start = clock();
2759 #if defined(G_LIKELIHOOD_DEBUG)
2762 int n_small_model = 0;
2763 int n_zero_data = 0;
2764 double sum_data = 0.0;
2765 double sum_model = 0.0;
2772 int npars = models.
npars();
2785 for (
int j = 0; j < npars; ++j) {
2799 double ngam =
N_gamma(models, i, &sky_grad);
2800 double nbgd =
N_bgd(models, i, &bgd_grad);
2803 double nonpred = ngam + alpha * nbgd;
2808 #if defined(G_LIKELIHOOD_DEBUG)
2816 value += -non *
std::log(nonpred) + nonpred - noff *
std::log(nbgd) + nbgd;
2819 #if defined(G_LIKELIHOOD_DEBUG)
2822 sum_model += nonpred;
2826 double fa = non/nonpred;
2827 double fb = fa/nonpred;
2828 double fc = alpha * fb;
2829 double fd = fc * alpha + noff/(nbgd*nbgd);
2830 double sky_factor = 1.0 - fa;
2831 double bgd_factor = 1.0 + alpha - alpha * fa - noff/nbgd;
2834 for (
int j = 0; j < npars; ++j) {
2842 (*gradient)[j] += sky_factor * sky_grad[j];
2845 for (
int k = 0; k < npars; ++k) {
2850 if (sky_grad[k] != 0.0 &&
2852 colvar[k] = sky_grad[j] * sky_grad[k] * fb;
2859 else if (bgd_grad[k] != 0.0 &&
2861 colvar[k] = sky_grad[j] * bgd_grad[k] * fc;
2880 else if (bgd_grad[j] != 0.0 &&
2884 (*gradient)[j] += bgd_factor * bgd_grad[j];
2887 for (
int k = 0; k < npars; ++k) {
2892 if (sky_grad[k] != 0.0 &&
2894 colvar[k] = bgd_grad[j] * sky_grad[k] * fc;
2900 else if (bgd_grad[k] != 0.0 &&
2902 colvar[k] = bgd_grad[j] * bgd_grad[k] * fd;
2922 #if defined(G_LIKELIHOOD_DEBUG)
2923 std::cout <<
"Number of parameters: " << npars << std::endl;
2924 std::cout <<
"Number of bins: " << n_bins << std::endl;
2925 std::cout <<
"Number of bins used for computation: " << n_used << std::endl;
2926 std::cout <<
"Number of bins excluded due to small model: ";
2927 std::cout << n_small_model << std::endl;
2928 std::cout <<
"Number of bins with zero data: " << n_zero_data << std::endl;
2929 std::cout <<
"Sum of data (On): " << sum_data << std::endl;
2930 std::cout <<
"Sum of model (On): " << sum_model << std::endl;
2931 std::cout <<
"Statistic: " << value << std::endl;
2935 #if defined(G_LIKELIHOOD_DEBUG)
2936 std::cout << *gradient << std::endl;
2937 std::cout << *curvature << std::endl;
2941 #if defined(G_LIKELIHOOD_DEBUG)
2943 double t_elapse = omp_get_wtime()-t_start;
2945 double t_elapse = (double)(clock() - t_start) / (
double)CLOCKS_PER_SEC;
2947 std::cout <<
"GCTAOnOffObservation::optimizer::likelihood_cstat: CPU usage = "
2948 << t_elapse <<
" sec" << std::endl;
2986 double* npred)
const
2989 #if defined(G_LIKELIHOOD_DEBUG)
2990 std::cout <<
"GCTAOnOffObservation::likelihood_wstat: enter" << std::endl;
2994 #if defined(G_LIKELIHOOD_DEBUG)
2996 double t_start = omp_get_wtime();
2998 clock_t t_start = clock();
3003 #if defined(G_LIKELIHOOD_DEBUG)
3006 double sum_data = 0.0;
3007 double sum_model = 0.0;
3014 int npars = models.
npars();
3026 for (
int j = 0; j < npars; ++j) {
3039 double ngam =
N_gamma(models, i, &sky_grad);
3042 double nonpred = 0.0;
3044 double dlogLdsky = 0.0;
3045 double d2logLdsky2 = 0.0;
3049 nonpred, nbgd, dlogLdsky, d2logLdsky2);
3056 #if defined(G_LIKELIHOOD_DEBUG)
3059 sum_model += nonpred;
3063 for (
int j = 0; j < npars; ++j) {
3071 (*gradient)[j] += dlogLdsky * sky_grad[j];
3074 for (
int k = 0; k < npars; ++k) {
3079 if (sky_grad[k] != 0.0 &&
3081 colvar[k] = sky_grad[j] * sky_grad[k] * d2logLdsky2;
3102 #if defined(G_LIKELIHOOD_DEBUG)
3103 std::cout <<
"Number of parameters: " << npars << std::endl;
3104 std::cout <<
"Number of bins: " << n_bins << std::endl;
3105 std::cout <<
"Number of bins used for computation: " << n_used << std::endl;
3106 std::cout <<
"Sum of data (On): " << sum_data << std::endl;
3107 std::cout <<
"Sum of model (On): " << sum_model << std::endl;
3108 std::cout <<
"Statistic: " << value << std::endl;
3112 #if defined(G_LIKELIHOOD_DEBUG)
3113 std::cout << *gradient << std::endl;
3114 std::cout << *curvature << std::endl;
3118 #if defined(G_LIKELIHOOD_DEBUG)
3120 double t_elapse = omp_get_wtime()-t_start;
3122 double t_elapse = (double)(clock() - t_start) / (
double)CLOCKS_PER_SEC;
3124 std::cout <<
"GCTAOnOffObservation::optimizer::likelihood_wstat: CPU usage = "
3125 << t_elapse <<
" sec" << std::endl;
3129 #if defined(G_LIKELIHOOD_DEBUG)
3130 std::cout <<
"GCTAOnOffObservation::likelihood_wstat: exit" << std::endl;
3345 const double& alpha,
3350 double& d2logLdsky2)
const
3356 double alphap1 = alpha + 1.0;
3357 double alpharat = alpha / alphap1;
3374 else if (ngam < non * alpharat) {
3375 nbgd = non / alphap1 - ngam / alpha;
3376 nonpred = ngam + alpha * nbgd;
3377 logL = -ngam / alpha - non *
std::log(alpharat);
3378 dlogLdsky = -1.0/alpha;
3387 dlogLdsky = 1.0 - non / ngam;
3388 d2logLdsky2 = non / (ngam * ngam);
3394 else if (non == 0.0) {
3395 nbgd = noff / alphap1;
3396 nonpred = ngam + alpharat * noff;
3397 logL = ngam + noff *
std::log(alphap1);
3406 double alphat2 = 2.0 * alpha;
3407 double C = alpha * (non + noff) - alphap1 * ngam;
3408 double D =
std::sqrt(C*C + 4.0 * alpha * alphap1 * noff * ngam);
3409 nbgd = (C + D) / (alphat2 * alphap1);
3410 nonpred = ngam + alpha * nbgd;
3411 logL = ngam + alphap1 * nbgd - non - noff -
3416 double f0 = alphat2 * noff - C;
3417 double dCds = -alphap1;
3418 double dDds = (C * dCds + 2.0 * alphap1 * alpha * noff) / D;
3419 double dbds = (f0 / D - 1.0) / alphat2;
3420 double d2bds2 = (-dCds / D - f0 / (D*D) * dDds) / alphat2;
3421 double f1 = alphap1 - alpha*non/nonpred - noff/nbgd;
3422 double f2 = nonpred * nonpred;
3423 double dpds = 1.0 + alpha * dbds;
3424 double f3 = non / f2 * dpds;
3425 dlogLdsky = 1.0 - non / nonpred + dbds * f1;
3426 d2logLdsky2 = f3 + d2bds2 * f1 +
3427 dbds * (alpha * f3 + noff / (nbgd*nbgd) * dbds);
3432 #if defined(G_NAN_CHECK)
3436 std::cout <<
"*** ERROR: GCTAOnOffObservation::wstat_value";
3437 std::cout <<
"(noff=" << noff;
3438 std::cout <<
", alpha=" << alpha;
3439 std::cout <<
", ngam=" << ngam <<
"):";
3440 std::cout <<
" NaN/Inf encountered";
3441 std::cout <<
" (logL=" << logL;
3442 std::cout <<
", nonpred=" << nonpred;
3443 std::cout <<
", nbgd=" << nbgd;
3444 std::cout <<
", dlogLdsky=" << dlogLdsky;
3445 std::cout <<
", d2logLdsky2=" << d2logLdsky2;
3446 std::cout <<
" )" << std::endl;
double m_livetime
Livetime of On observation (seconds)
virtual double likelihood_wstat(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis in the case of Poisson signal with measured Pois...
std::string print(const GChatter &chatter=NORMAL) const
Print Redistribution Matrix File.
const std::string & statistic(void) const
Return optimizer statistic.
double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const
Evaluate sum of all models.
std::string m_instrument
Instrument name.
void apply_ebounds(const GCTAObservation &obs)
Apply CTA observation energy boundaries to On/Off observation.
const double & factor_gradient(void) const
Return parameter factor gradient.
void init_members(void)
Initialise class members.
void append(const std::string &name, const std::vector< double > &column)
Append additional column to spectrum.
double norm(const GVector &vector)
Computes vector norm.
virtual bool apply_edisp(void) const
Signal if energy dispersion should be applied.
std::string m_name
Observation name.
virtual const GCTAResponse * response(void) const
Return pointer to CTA response function.
virtual ~GCTAOnOffObservation(void)
Destructor.
CTA event atom class definition.
const std::string & name(void) const
Return parameter name.
virtual GCTAOnOffObservation * clone(void) const
Clone instance.
void emin_obs(const GEnergy &emin_obs)
Set minimum energy of observations.
Abstract spectral model base class.
Sparse matrix class interface definition.
GModelSpectral * spectral(void) const
Return spectral model component.
virtual const GCTAInstDir & dir(void) const
Return instrument direction of event bin.
Model container class definition.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
int size(void) const
Return number of energy boundaries.
virtual void clear(void)
Clear instance.
GCTAEventBin class interface definition.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const =0
const GEbounds & ebounds(void) const
Return energy boundaries.
Sky regions container class definition.
GEbounds ebounds(void) const
Get energy boundaries.
virtual double livetime(void) const
Return livetime.
const GCTAOnOffObservation g_onoff_obs_hess_seed(true,"HESSOnOff")
bool contains(const GEnergy &eng) const
Checks whether energy boundaries contain energy.
const GCTAOnOffObservation g_onoff_obs_astri_seed(true,"ASTRIOnOff")
CTA instrument response function class definition.
void compute_bgd(const GCTAObservation &obs, const GSkyRegions &off, const GModels &models, const bool &use_model_bkg)
Compute background rate in Off regions.
virtual void response(const GResponse &rsp)
Set response function.
Redistribution Matrix File class.
GPha model_gamma(const GModels &models) const
const double & zenith(void) const
Return pointing zenith angle.
const std::string & rspname(void) const
Return response name.
const GCTAOnOffObservation g_onoff_obs_magic_seed(true,"MAGICOnOff")
std::string m_id
Observation identifier.
double sum(const GVector &vector)
Computes vector sum.
double log10TeV(void) const
Return log10 of energy in TeV.
Abstract spatial model base class interface definition.
void backscal(const int &index, const double &backscal)
Set background scaling factor.
double MeV(void) const
Return energy in MeV.
const GCTAOnOffObservation g_onoff_obs_fact_seed(true,"FACTOnOff")
int nmeasured(void) const
Return number of measured energy bins in redistribution matrix.
const GPha & off_spec(void) const
Return Off spectrum.
virtual double wstat_value(const double &non, const double &noff, const double &alpha, const double &ngam, double &nonpred, double &nbgd, double &dlogLdsky, double &d2logLdsky2) const
Evaluate log-likelihood value in energy bin for On/Off analysis by profiling over the number of backg...
GEnergy emean(const int &index) const
Returns mean energy for a given energy interval.
const GCTAOnOffObservation g_onoff_obs_veritas_seed(true,"VERITASOnOff")
const GArf & arf(void) const
Return Auxiliary Response File.
Interface for the circular sky region class.
virtual GCTAResponse * clone(void) const =0
Clones object.
void fill(const GEnergy &energy, const double &value=1.0)
Fill spectrum with a value.
GCTAResponse * m_response
Pointer to IRFs.
void pointing(const GCTAPointing &pointing)
Set CTA pointing.
virtual double deadc(const GTime &time=GTime()) const
Return deadtime correction factor.
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
GEnergy ewidth(const int &index) const
Returns energy interval width.
void dir(const GSkyDir &dir)
Set sky direction.
bool is_free(void) const
Signal if parameter is free.
virtual double irf_spatial(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response integrated over the spatial model.
Auxiliary Response File class.
GArf m_arf
Auxiliary Response Function vector.
void id(const std::string &id)
Set observation identifier.
void load(const GFilename &filename)
Load Pulse Height Analyzer spectrum.
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
bool is_notanumber(const double &x)
Signal if argument is not a number.
const double & radius(void) const
Return circular region radius (in degrees)
CTA cube background class definition.
void map(const GSkyMap &map)
Set sky map.
void compute_alpha(const GCTAObservation &obs_on, const GCTAObservation &obs_off, const GSkyRegions &on, const GSkyRegions &off, const GModels &models, const bool &use_model_bkg)
Compute vector of alpha parameters.
const GEbounds & emeasured(void) const
Return measured energy boundaries.
const GFilename & filename(void) const
Return file name.
int size(void) const
Return number of parameters in model.
virtual double ontime(void) const
Return ontime.
const GFitsHeader & header(void) const
Return FITS header.
const double & scale(void) const
Return parameter scale.
bool is_infinite(const double &x)
Signal if argument is infinite.
const double minerr
Minimum statistical error.
bool is_valid(const std::string &instruments, const std::string &ids) const
Verifies if model is valid for a given instrument and identifier.
Definition of support function used by CTA classes.
double N_gamma(const GModels &models, const int &ibin, GVector *grad) const
const GRmf & rmf(void) const
Return Redistribution Matrix File.
void set(const GCTAObservation &obs_on, const GCTAObservation &obs_off, const GModels &models, const std::string &srcname, const GSkyRegions &on, const GSkyRegions &off, const bool &use_model_bkg)
Set On/Off observation from a CTA observation.
const GFilename & filename(void) const
Return file name.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Abstract temporal model base class interface definition.
virtual double livetime(void) const
Return livetime.
const std::vector< int > & nonzero_indices(void) const
Get non-zero index vector.
CTA 2D effective area class.
virtual void read(const GXmlElement &xml)
Read On/Off observation from an XML element.
const std::string & id(void) const
Return observation identifier.
GCTAOnOffObservation & operator=(const GCTAOnOffObservation &obs)
Assignment operator.
void set_exposure(void)
Set ontime, livetime and deadtime correction factor.
CTA On/Off observation class definition.
Energy boundaries container class.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void copy_members(const GCTAOnOffObservation &obs)
Copy class members.
std::string classname(void) const
Return class name.
const GCTAResponse * cta_rsp(const std::string &origin, const GResponse &rsp)
Retrieve CTA response from generic observation.
bool has_par(const std::string &name) const
Checks if parameter name exists.
virtual void write(GXmlElement &xml) const
write observation to an xml element
const GCTAAeff * aeff(void) const
Return pointer to effective area response.
virtual double model(const GModels &models, const GEvent &event, GVector *gradients=NULL) const
Return model value and (optionally) gradients.
const GCTAOnOffObservation g_onoff_obs_cta_seed(true,"CTAOnOff")
std::string print(const GChatter &chatter=NORMAL) const
Print Auxiliary Response File.
const GEnergy & emin(void) const
Return minimum energy of all intervals.
Abstract data model base class interface definition.
CTA 2D effective area class definition.
GCTAInstDir instdir(const GSkyDir &skydir) const
Get instrument direction from sky direction.
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
virtual std::string instrument(void) const
Return instrument name.
Interface definition for the observation registry class.
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
virtual double likelihood_cstat(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis in the case of Poisson signal with modeled Poiss...
Observation registry class definition.
Abstract data model class.
virtual std::string instrument(void) const
Return instrument.
int ntrue(void) const
Return number of true energy bins in redistribution matrix.
const GModelSpectral * cta_model_spectral(const GModelData &model)
Retrieve spectral component from CTA background model.
Interface for a sky region map.
GEnergy elogmean(const int &index) const
Returns logarithmic mean energy for a given energy interval.
Optimizer parameters base class definition.
int size(void) const
Return number of regions in container.
const double & rad_max(void) const
Return radius cut value.
const GPha & on_spec(void) const
Return On spectrum.
Abstract observation base class.
int size(void) const
Return number of observations in container.
void clear(void)
Clear object.
bool has_scales(void) const
Signals that model has scales.
GRmf m_rmf
Redistribution matrix.
CTA observation class interface definition.
GPha model_background(const GModels &models) const
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
void clear(void)
Clear object.
Observation container class.
const double minmod
Minimum model value.
CTA instrument response function class.
double m_deadc
Deadtime correction (livetime/ontime)
CTA instrument response function class.
void exposure(const double &exposure)
Set exposure time.
Sky region container class.
Abstract spectral model base class interface definition.
void compute_arf_cut(const GCTAObservation &obs, const GModelSpatial &spatial, const GSkyRegions &on)
Compute ARF of On/Off observation for a IRF with radius cut.
const double & azimuth(void) const
Return pointing azimuth angle.
const GFitsHeader & header(void) const
Return FITS header.
void name(const std::string &name)
Set observation name.
double posang(const GSkyDir &dir, const std::string &coordsys="CEL") const
Compute position angle between sky directions in radians.
void compute_arf(const GCTAObservation &obs, const GModelSpatial &spatial, const GSkyRegions &on)
Compute ARF of On/Off observation.
void free_members(void)
Delete class members.
Sky model class interface definition.
double N_bgd(const GModels &models, const int &ibin, GVector *grad) const
virtual std::string print(const GChatter &chatter=NORMAL) const
Print On/Off observation information.
const GFitsHeader & header(void) const
Return FITS header.
std::string print(const GChatter &chatter=NORMAL) const
Print Pulse Height Analyzer spectrum.
GCTAOnOffObservation(void)
Void constructor.
virtual double ontime(void) const
Return ontime.
Sky region map class interface definition.
int size(void) const
Return number of bins in spectrum.
double value(void) const
Return parameter value.
GChatter reduce(const GChatter &chatter)
Reduce chattiness by one level.
Sparse matrix class definition.
CTA event bin container class interface definition.
GPha m_off_spec
Off counts spectrum.
void check_consistency(const std::string &method) const
Check consistency of data members.
void clear(void)
Clear object.
virtual GObservation & operator=(const GObservation &obs)
Assignment operator.
virtual GEvents * events(void)
Return events.
virtual double likelihood(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis.
virtual double prob_erecobin(const GEnergy &ereco_min, const GEnergy &ereco_max, const GEnergy &etrue, const double &theta) const =0
GRmf rmf_stacked(const GRmf &rmf, const GEnergy &emin, const GEnergy &emax) const
Return RMF for stacking.
void compute_rmf(const GCTAObservation &obs, const GSkyRegions &on)
Compute RMF of On/Off observation.
int npars(void) const
Return number of model parameters in container.
virtual int size(void) const
Return number of events in list.
GPha m_on_spec
On counts spectrum.
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
std::string toupper(const std::string &s)
Convert string to upper case.
const GFilename & filename(void) const
Return file name.
GModelSpatial * spatial(void) const
Return spatial model component.
CTA instrument direction class.
const GSkyDir & dir(void) const
Return pointing sky direction.
std::string xml_get_attr(const std::string &origin, const GXmlElement &xml, const std::string &name, const std::string &attribute)
Return attribute value for a given parameter in XML element.
Abstract spatial model base class.
#define G_MODEL_BACKGROUND
double m_ontime
Ontime of On observation (seconds)
const GEnergy & emax(void) const
Return maximum energy of all intervals.
virtual double flux(const GEnergy &emin, const GEnergy &emax) const =0
const GEbounds & etrue(void) const
Return true energy boundaries.
Abstract instrument response base class.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
const GCTAEdisp * edisp(void) const
Return pointer to energy dispersion.
GArf arf_stacked(const GArf &arf, const GEnergy &emin, const GEnergy &emax) const
Return ARF for stacking.
Class that handles gamma-ray sources.
Pulse Height Analyzer class.
Integration class interface definition.
Observations container class interface definition.
int size(void) const
Return number of spectral bins.
void load(const GFilename &filename)
Load Auxiliary Response File.
bool contains(const GSkyDir &dir) const
Check if direction is contained in one of the regions.
Circular sky region class interface definition.
int size(void) const
Return number of models in container.
void caldb(const GCaldb &caldb)
Set calibration database.
GModel * append(const GModel &model)
Append model to container.
double arf_rad_max(const GCTAObservation &obs, const GSkyRegions &on) const
Check if effective area IRF has a radius cut.
const GEnergy & energy(void) const
Return energy.
const GCTAInstDir & dir(void) const
Return instrument direction.
const GCTAResponseIrf & cta_rsp_irf(const std::string &origin, const GObservation &obs)
Retrieve CTA IRF response from generic observation.
void load(const GFilename &filename)
Load Redistribution Matrix File.
const GEbounds & ebounds(void) const
Return energy boundaries.
Class that handles energies in a unit independent way.
void emax_obs(const GEnergy &emax_obs)
Set maximum energy of observations.
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
CTA On/Off observation class.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
GSkyRegion * append(const GSkyRegion ®ion)
Append region to container.