/***********************************************************************//** * @brief Evaluate function * * @param[in] event Observed event. * @param[in] obs Observation. * @return Function value. * * @exception GException::invalid_argument * Specified observation is not of the expected type. * * @todo Make sure that DETX and DETY are always set in GCTAInstDir. ***************************************************************************/ double GCTAModelIrfBackground::eval(const GEvent& event, const GObservation& obs) const { // Get pointer on CTA observation const GCTAObservation* cta = dynamic_cast<const GCTAObservation*>(&obs); if (cta == NULL) { std::string msg = "Specified observation is not a CTA observation.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Get pointer on CTA IRF response const GCTAResponseIrf* rsp = dynamic_cast<const GCTAResponseIrf*>(cta->response()); if (rsp == NULL) { std::string msg = "Specified observation does not contain an IRF response.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Retrieve pointer to CTA background const GCTABackground* bgd = rsp->background(); if (bgd == NULL) { std::string msg = "Specified observation contains no background" " information.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Extract CTA instrument direction from event const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&(event.dir())); if (dir == NULL) { std::string msg = "No CTA instrument direction found in event."; throw GException::invalid_argument(G_EVAL, msg); } // Set DETX and DETY in instrument direction GCTAInstDir inst_dir = cta->pointing().instdir(dir->dir()); // Evaluate function double logE = event.energy().log10TeV(); double spat = (*bgd)(logE, inst_dir.detx(), inst_dir.dety()); double spec = (spectral() != NULL) ? spectral()->eval(event.energy(), event.time()) : 1.0; double temp = (temporal() != NULL) ? temporal()->eval(event.time()) : 1.0; // Compute value double value = spat * spec * temp; // Apply deadtime correction value *= obs.deadc(event.time()); // Return value return value; }
/***********************************************************************//** * @brief Evaluate function * * @param[in] event Observed event. * @param[in] obs Observation. * @return Function value. * * @exception GException::invalid_argument * Specified observation is not of the expected type. ***************************************************************************/ double GCTAModelCubeBackground::eval(const GEvent& event, const GObservation& obs) const { // Get pointer on CTA observation const GCTAObservation* cta = dynamic_cast<const GCTAObservation*>(&obs); if (cta == NULL) { std::string msg = "Specified observation is not a CTA observation."; throw GException::invalid_argument(G_EVAL, msg); } // Get pointer on CTA IRF response const GCTAResponseCube* rsp = dynamic_cast<const GCTAResponseCube*>(cta->response()); if (rsp == NULL) { std::string msg = "Specified observation does not contain a cube response."; throw GException::invalid_argument(G_EVAL, msg); } // Extract CTA instrument direction from event const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&(event.dir())); if (dir == NULL) { std::string msg = "No CTA instrument direction found in event."; throw GException::invalid_argument(G_EVAL, msg); } // Retrieve reference to CTA cube background const GCTACubeBackground& bgd = rsp->background(); // Evaluate function //double logE = event.energy().log10TeV(); double spat = bgd((*dir), event.energy()); double spec = (spectral() != NULL) ? spectral()->eval(event.energy(), event.time()) : 1.0; double temp = (temporal() != NULL) ? temporal()->eval(event.time()) : 1.0; // Compute value. Note that background rates are already per // livetime, hence no deadtime correction is needed here. double value = spat * spec * temp; // Return value return value; }
/***********************************************************************//** * @brief Evaluate function * * @param[in] event Observed event. * @param[in] obs Observation. * @return Function value. * * @exception GException::invalid_argument * No CTA instrument direction found in event. * * Evaluates tha CTA background model which is a factorization of a * spatial, spectral and temporal model component. This method also applies * a deadtime correction factor, so that the normalization of the model is * a real rate (counts/exposure time). * * @todo Add bookkeeping of last value and evaluate only if argument * changed ***************************************************************************/ double GCTAModelBackground::eval(const GEvent& event, const GObservation& obs) const { // Get pointer on CTA observation const GCTAObservation* ctaobs = dynamic_cast<const GCTAObservation*>(&obs); if (ctaobs == NULL) { std::string msg = "Specified observation is not a CTA observation.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Extract CTA instrument direction const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&(event.dir())); if (dir == NULL) { std::string msg = "No CTA instrument direction found in event."; throw GException::invalid_argument(G_EVAL, msg); } // Create a Photon from the event. // We need the GPhoton to evaluate the spatial model. // For the background, GEvent and GPhoton are identical // since the IRFs are not folded in GPhoton photon(dir->dir(), event.energy(), event.time()); // Evaluate function and gradients double spat = (spatial() != NULL) ? spatial()->eval(photon) : 1.0; double spec = (spectral() != NULL) ? spectral()->eval(event.energy(), event.time()) : 1.0; double temp = (temporal() != NULL) ? temporal()->eval(event.time()) : 1.0; // Compute value double value = spat * spec * temp; // Apply deadtime correction value *= obs.deadc(event.time()); // Return return value; }
/***********************************************************************//** * @brief Perform integration over temporal component * * @param[in] event Observed event. * @param[in] obs Observation. * @param[in] grad Evaluate gradients. * * @exception GException::no_response * Observation has no valid instrument response * @exception GException::feature_not_implemented * Temporal integration not yet implemented * * This method integrates the source model over the temporal component. If * the response function has no time dispersion then no temporal integration * is needed and the observed photon arrival time is identical to the true * photon arrival time. * * @todo Needs implementation of temporal integration to handle time * dispersion. ***************************************************************************/ double GModelSky::temporal(const GEvent& event, const GObservation& obs, bool grad) const { // Initialise result double value = 0.0; // Get response function GResponse* rsp = obs.response(); if (rsp == NULL) { throw GException::no_response(G_TEMPORAL); } // Determine if time integration is needed bool integrate = rsp->hastdisp(); // Case A: Integraion if (integrate) { throw GException::feature_not_implemented(G_TEMPORAL); } // Case B: No integration (assume no time dispersion) else { value = spectral(event, event.time(), obs, grad); } // Compile option: Check for NaN/Inf #if defined(G_NAN_CHECK) if (isnotanumber(value) || isinfinite(value)) { std::cout << "*** ERROR: GModelSky::temporal:"; std::cout << " NaN/Inf encountered"; std::cout << " (value=" << value; std::cout << ", event=" << event; std::cout << ")" << std::endl; } #endif // Return value return value; }
/***********************************************************************//** * @brief Evaluate function and gradients * * @param[in] event Observed event. * @param[in] obs Observation. * @return Function value. * * @exception GException::invalid_argument * No CTA instrument direction found in event. * * Evaluates tha CTA background model and parameter gradients. The CTA * background model is a factorization of a spatial, spectral and * temporal model component. This method also applies a deadtime correction * factor, so that the normalization of the model is a real rate * (counts/exposure time). * * @todo Add bookkeeping of last value and evaluate only if argument * changed ***************************************************************************/ double GCTAModelBackground::eval_gradients(const GEvent& event, const GObservation& obs) const { // Get pointer on CTA observation const GCTAObservation* ctaobs = dynamic_cast<const GCTAObservation*>(&obs); if (ctaobs == NULL) { std::string msg = "Specified observation is not a CTA observation.\n" + obs.print(); throw GException::invalid_argument(G_EVAL_GRADIENTS, msg); } // Extract CTA instrument direction const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&(event.dir())); if (dir == NULL) { std::string msg = "No CTA instrument direction found in event."; throw GException::invalid_argument(G_EVAL_GRADIENTS, msg); } // Create a Photon from the event // We need the photon to evaluate the spatial model // For the background, GEvent and GPhoton are identical // since the IRFs are not folded in GPhoton photon = GPhoton(dir->dir(), event.energy(),event.time()); // Evaluate function and gradients double spat = (spatial() != NULL) ? spatial()->eval_gradients(photon) : 1.0; double spec = (spectral() != NULL) ? spectral()->eval_gradients(event.energy(), event.time()) : 1.0; double temp = (temporal() != NULL) ? temporal()->eval_gradients(event.time()) : 1.0; // Compute value double value = spat * spec * temp; // Apply deadtime correction double deadc = obs.deadc(event.time()); value *= deadc; // Multiply factors to spatial gradients if (spatial() != NULL) { double fact = spec * temp * deadc; if (fact != 1.0) { for (int i = 0; i < spatial()->size(); ++i) (*spatial())[i].factor_gradient( (*spatial())[i].factor_gradient() * fact ); } } // Multiply factors to spectral gradients if (spectral() != NULL) { double fact = spat * temp * deadc; if (fact != 1.0) { for (int i = 0; i < spectral()->size(); ++i) (*spectral())[i].factor_gradient( (*spectral())[i].factor_gradient() * fact ); } } // Multiply factors to temporal gradients if (temporal() != NULL) { double fact = spat * spec * deadc; if (fact != 1.0) { for (int i = 0; i < temporal()->size(); ++i) (*temporal())[i].factor_gradient( (*temporal())[i].factor_gradient() * fact ); } } // Return value return value; }
/***********************************************************************//** * @brief Evaluate function * * @param[in] event Observed event. * @param[in] obs Observation. * @param[in] gradients Compute gradients? * @return Function value. * * @exception GException::invalid_argument * Specified observation is not of the expected type. * * If the @p gradients flag is true the method will also set the parameter * gradients of the model parameters. * * @todo Make sure that DETX and DETY are always set in GCTAInstDir. ***************************************************************************/ double GCTAModelAeffBackground::eval(const GEvent& event, const GObservation& obs, const bool& gradients) const { // Get pointer on CTA observation const GCTAObservation* cta = dynamic_cast<const GCTAObservation*>(&obs); if (cta == NULL) { std::string msg = "Specified observation is not a CTA observation.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Get pointer on CTA IRF response const GCTAResponseIrf* rsp = dynamic_cast<const GCTAResponseIrf*>(cta->response()); if (rsp == NULL) { std::string msg = "Specified observation does not contain an IRF response.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Retrieve pointer to CTA Effective Area const GCTAAeff* aeff = rsp->aeff(); if (aeff == NULL) { std::string msg = "Specified observation contains no effective area" " information.\n" + obs.print(); throw GException::invalid_argument(G_EVAL, msg); } // Extract CTA instrument direction from event const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&(event.dir())); if (dir == NULL) { std::string msg = "No CTA instrument direction found in event."; throw GException::invalid_argument(G_EVAL, msg); } // Set DETX and DETY in instrument direction GCTAInstDir inst_dir = cta->pointing().instdir(dir->dir()); // Set theta and phi from instrument coordinates double theta = std::sqrt(inst_dir.detx() * inst_dir.detx() + inst_dir.dety() * inst_dir.dety()); double phi = gammalib::atan2d(inst_dir.dety(), inst_dir.detx()) * gammalib::deg2rad; // Evaluate function double logE = event.energy().log10TeV(); double spat = (*aeff)(logE, theta, phi, cta->pointing().zenith(), cta->pointing().azimuth(), false); double spec = (spectral() != NULL) ? spectral()->eval(event.energy(), event.time(), gradients) : 1.0; double temp = (temporal() != NULL) ? temporal()->eval(event.time(), gradients) : 1.0; // Compute value double value = spat * spec * temp; // Apply deadtime correction double deadc = obs.deadc(event.time()); value *= deadc; // Optionally compute partial derivatives if (gradients) { // Multiply factors to spectral gradients if (spectral() != NULL) { double fact = spat * temp * deadc; if (fact != 1.0) { for (int i = 0; i < spectral()->size(); ++i) (*spectral())[i].factor_gradient((*spectral())[i].factor_gradient() * fact ); } } // Multiply factors to temporal gradients if (temporal() != NULL) { double fact = spat * spec * deadc; if (fact != 1.0) { for (int i = 0; i < temporal()->size(); ++i) (*temporal())[i].factor_gradient((*temporal())[i].factor_gradient() * fact ); } } } // endif: computed partial derivatives // Return value return value; }
virtual double eval_gradients(const GEvent& event, const GObservation& obs) const { double result = m_modelTps->eval_gradients(event.time()); return result; }
/***********************************************************************//** * @brief Convolve sky model with the instrument response * * @param[in] model Sky model. * @param[in] event Event. * @param[in] obs Observation. * @param[in] grad Should model gradients be computed? (default: true) * @return Event probability. * * Computes the event probability * * \f[ * P(p',E',t') = \int \int \int * S(p,E,t) \times R(p',E',t'|p,E,t) \, dp \, dE \, dt * \f] * * without taking into account any time dispersion. Energy dispersion is * correctly handled by this method. If time dispersion is indeed needed, * an instrument specific method needs to be provided. ***************************************************************************/ double GResponse::convolve(const GModelSky& model, const GEvent& event, const GObservation& obs, const bool& grad) const { // Set number of iterations for Romberg integration. static const int iter = 6; // Initialise result double prob = 0.0; // Continue only if the model has a spatial component if (model.spatial() != NULL) { // Get source time (no dispersion) GTime srcTime = event.time(); // Case A: Integration if (use_edisp()) { // Retrieve true energy boundaries GEbounds ebounds = this->ebounds(event.energy()); // Loop over all boundaries for (int i = 0; i < ebounds.size(); ++i) { // Get boundaries in MeV double emin = ebounds.emin(i).MeV(); double emax = ebounds.emax(i).MeV(); // Continue only if valid if (emax > emin) { // Setup integration function edisp_kern integrand(this, &obs, &model, &event, srcTime, grad); GIntegral integral(&integrand); // Set number of iterations integral.fixed_iter(iter); // Do Romberg integration emin = std::log(emin); emax = std::log(emax); prob += integral.romberg(emin, emax); } // endif: interval was valid } // endfor: looped over intervals } // Case B: No integration (assume no energy dispersion) else { // Get source energy (no dispersion) GEnergy srcEng = event.energy(); // Evaluate probability prob = eval_prob(model, event, srcEng, srcTime, obs, grad); } // Compile option: Check for NaN/Inf #if defined(G_NAN_CHECK) if (gammalib::is_notanumber(prob) || gammalib::is_infinite(prob)) { std::cout << "*** ERROR: GResponse::convolve:"; std::cout << " NaN/Inf encountered"; std::cout << " (prob=" << prob; std::cout << ", event=" << event; std::cout << ", srcTime=" << srcTime; std::cout << ")" << std::endl; } #endif } // endif: spatial component valid // Return probability return prob; }