61#define G_CONVOLVE "GResponse::convolve(GModelSky&, GObservation&, "\
63#define G_IRF_RADIAL "GResponse::irf_radial(GEvent&, GSource&,"\
65#define G_IRF_ELLIPTICAL "GResponse::irf_elliptical(GEvent&, GSource&,"\
67#define G_IRF_DIFFUSE "GResponse::irf_diffuse(GEvent&, GSource&,"\
189 const bool& grad)
const
192 static const int iter = 6;
201 GTime srcTime =
event.time();
236 if (etrue_max > etrue_min) {
239 edisp_kerns integrand(
this, &obs, &model, &event, srcTime, grad);
246 array += integral.
romberg(etrue_min, etrue_max);
256 prob = array[index++];
263 for (
int i = 0; i < model.
spectral()->size(); ++i) {
283 for (
int i = 0; i < model.
scales(); ++i) {
301 GEnergy srcEng =
event.energy();
304 prob =
eval_prob(model, event, srcEng, srcTime, obs, grad);
309 #if defined(G_NAN_CHECK)
311 std::cout <<
"*** ERROR: GResponse::convolve:";
312 std::cout <<
" NaN/Inf encountered";
313 std::cout <<
" (prob=" << prob;
314 std::cout <<
", event=" << event;
315 std::cout <<
", srcTime=" << srcTime;
316 std::cout <<
")" << std::endl;
351 static const int iter = 6;
354 int npars = model.
size();
358 bool grad = (gradients != NULL);
364 int ncolumns = (nevents > 0) ? npars : 0;
365 if (gradients->
columns() != ncolumns) {
367 "columns in gradient matrix differs from expected "
369 "specify compatible arguments.";
374 int nrows = (ncolumns > 0) ? nevents : 0;
375 if (gradients->
rows() != nrows) {
377 "rows in gradient matrix differs from expected "
379 "specify compatible arguments.";
397 tmp_gradients =
new GVector[npars];
398 for (
int i = 0; i < npars; ++i) {
399 tmp_gradients[i] =
GVector(nevents);
410 for (
int k = 0; k < nevents; ++k) {
416 GTime srcTime =
event.time();
442 if (etrue_max > etrue_min) {
445 edisp_kerns integrand(
this, &obs, &model, &event, srcTime, grad);
452 array += integral.
romberg(etrue_min, etrue_max);
462 probs[k] = array[index++];
475 for (
int i = 0; i < n_spec; ++i) {
478 tmp_gradients[offset+i][k] = array[index++];
485 int offset = n_spat + n_spec;
486 for (
int i = 0; i < n_temp; ++i) {
489 tmp_gradients[offset+i][k] = array[index++];
496 int offset = n_spat + n_spec + n_temp;
497 for (
int i = 0; i < model.
scales(); ++i) {
501 tmp_gradients[offset+i][k] = array[index++];
510 #if defined(G_NAN_CHECK)
512 std::cout <<
"*** ERROR: GResponse::convolve:";
513 std::cout <<
" NaN/Inf encountered";
514 std::cout <<
" (probs[" << k <<
"]=" << probs[k];
515 std::cout <<
", event=" << event;
516 std::cout <<
", srcTime=" << srcTime;
517 std::cout <<
")" << std::endl;
527 for (
int i = 0; i < npars; ++i) {
528 gradients->
column(i, tmp_gradients[i]);
532 delete [] tmp_gradients;
604 std::string name = obs.
id() +
":" + source.
name();
606 const GEnergy& ereco =
event.energy();
701 int npars = model.
size();
711 std::string cache_id = obs.
id() +
":" + model.
name();
864 double irf = this->
irf(event, photon, obs);
924 double theta_min = 0.0;
925 double theta_max = model->
theta_max() - 1.0e-12;
930 if (theta_max > theta_min) {
940 GMatrix rot = (ry * rz).transpose();
960 #if defined(G_NAN_CHECK)
962 std::cout <<
"*** ERROR: GResponse::irf_radial:";
963 std::cout <<
" NaN/Inf encountered";
964 std::cout <<
" (irf=" <<
irf;
965 std::cout <<
", theta_min=" << theta_min;
966 std::cout <<
", theta_max=" << theta_max <<
")";
967 std::cout << std::endl;
1031 double theta_min = 0.0;
1032 double theta_max = model->
theta_max() - 1.0e-6;
1037 if (theta_max > theta_min) {
1047 GMatrix rot = (ry * rz).transpose();
1067 #if defined(G_NAN_CHECK)
1069 std::cout <<
"*** ERROR: GResponse::irf_elliptical:";
1070 std::cout <<
" NaN/Inf encountered";
1071 std::cout <<
" (irf=" <<
irf;
1072 std::cout <<
", theta_min=" << theta_min;
1073 std::cout <<
", theta_max=" << theta_max <<
")";
1074 std::cout << std::endl;
1138 for (
int i = 0; i < skymap.
npix(); ++i) {
1141 double value = skymap(i);
1152 GPhoton photon(srcDir, srcEng, srcTime);
1173 for (
int i = 0; i < skymap.
npix(); ++i) {
1179 GPhoton photon(srcDir, srcEng, srcTime);
1182 double value = cube->
eval(photon);
1204 double dx = 360.0 / double(nx);
1205 double dy = 180.0 / double(ny);
1206 GSkyMap skymap =
GSkyMap(
"CAR",
"CEL", 0.0, 0.0, -dx, dy, nx, ny);
1213 for (
int i = 0; i < skymap.
npix(); ++i) {
1219 GPhoton photon(srcDir, srcEng, srcTime);
1222 double value = model->
eval(photon);
1267 for (
int i = 0; i < model->
components(); ++i) {
1313 for (
int k = 0; k < nevents; ++k) {
1319 GEnergy srcEng =
event.energy();
1320 GTime srcTime =
event.time();
1326 irfs[k] = this->
irf_ptsrc(event, source, obs);
1357 for (
int k = 0; k < nevents; ++k) {
1363 GEnergy srcEng =
event.energy();
1364 GTime srcTime =
event.time();
1370 irfs[k] = this->
irf_radial(event, source, obs);
1401 for (
int k = 0; k < nevents; ++k) {
1407 GEnergy srcEng =
event.energy();
1408 GTime srcTime =
event.time();
1445 for (
int k = 0; k < nevents; ++k) {
1451 GEnergy srcEng =
event.energy();
1452 GTime srcTime =
event.time();
1496 for (
int i = 0; i < composite->
components(); ++i) {
1539 const GTime& srcTime,
1541 const bool& grad)
const
1547 if (model.
spatial() != NULL) {
1563 double spec = (model.
spectral() != NULL)
1565 double temp = (model.
temporal() != NULL)
1569 prob = spec * temp *
irf * scale;
1576 double fact = temp *
irf * scale;
1579 (*model.
spectral())[i].factor_gradient((*model.
spectral())[i].factor_gradient() * fact);
1586 double fact = spec *
irf * scale;
1589 (*model.
temporal())[i].factor_gradient((*model.
temporal())[i].factor_gradient() * fact);
1596 for (
int i = 0; i < model.
scales(); ++i) {
1597 double g_scale = 0.0;
1610 #if defined(G_NAN_CHECK)
1612 std::cout <<
"*** ERROR: GResponse::eval_prob:";
1613 std::cout <<
" NaN/Inf encountered";
1614 std::cout <<
" (prob=" << prob;
1615 std::cout <<
", spec=" << spec;
1616 std::cout <<
", temp=" << temp;
1617 std::cout <<
", irf=" <<
irf;
1618 std::cout <<
")" << std::endl;
1631 (*model.
spectral())[i].factor_gradient(0.0);
1638 (*model.
temporal())[i].factor_gradient(0.0);
1644 for (
int i = 0; i < model.
scales(); ++i) {
1679 int npars = model.
size();
1683 bool grad = (gradients != NULL);
1689 if (model.
spatial() != NULL) {
1702 GVector* tmp_gradients = NULL;
1704 tmp_gradients =
new GVector[npars];
1705 for (
int i = 0; i < npars; ++i) {
1706 tmp_gradients[i] =
GVector(nevents);
1711 for (
int k = 0; k < nevents; ++k) {
1714 double spat = probs[k];
1723 GEnergy srcEng =
event.energy();
1724 GTime srcTime =
event.time();
1727 double spec = (model.
spectral() != NULL)
1730 double temp = (model.
temporal() != NULL)
1735 probs[k] = spat * spec * temp * scale;
1746 if (model.
spatial() != NULL) {
1747 double fact = spec * temp * scale;
1748 for (
int i = 0; i < n_spat; ++i) {
1749 tmp_gradients[i][k] = spat_gradients(k,i) * fact;
1755 int offset = n_spat;
1756 double fact = spat * temp * scale;
1757 for (
int i = 0; i < n_spec; ++i) {
1758 tmp_gradients[offset+i][k] =
1759 (*(model.
spectral()))[i].factor_gradient() * fact;
1765 int offset = n_spat + n_spec;
1766 double fact = spat * spec * scale;
1767 for (
int i = 0; i < n_temp; ++i) {
1768 tmp_gradients[offset+i][k] =
1769 (*(model.
temporal()))[i].factor_gradient() * fact;
1775 int offset = n_spat + n_spec + n_temp;
1776 double fact = spat * spec * temp;
1777 for (
int i = 0; i < model.
scales(); ++i) {
1779 double g_scale = 0.0;
1782 tmp_gradients[offset+i][k] = par.
scale() * fact;
1793 #if defined(G_NAN_CHECK)
1795 std::cout <<
"*** ERROR: GResponse::eval_prob:";
1796 std::cout <<
" NaN/Inf encountered";
1797 std::cout <<
" (probs[" << k <<
"]=" << probs[k];
1798 std::cout <<
", spat=" << spat;
1799 std::cout <<
")" << std::endl;
1809 for (
int i = 0; i < npars; ++i) {
1810 gradients->
column(i, tmp_gradients[i]);
1814 delete [] tmp_gradients;
1838 const bool& grad)
const
1846 for (
int i = 0; i < model.
spectral()->size(); ++i) {
1862 for (
int i = 0; i < model.
scales(); ++i) {
1895 const GTime& srcTime,
1973 if (gauss != NULL) {
1976 const double width = 5.0;
1981 GEnergy ebound_min = mean - width * sigma;
1982 GEnergy ebound_max = mean + width * sigma;
1985 if (ebound_min.
MeV() < 0.0) {
1986 ebound_min.
MeV(0.0);
1988 if (ebound_max.
MeV() < 0.0) {
1989 ebound_max.
MeV(0.0);
1993 if (ebound_max > ebound_min) {
2025 kernels[index++] = m_parent->eval_prob(*m_model, *m_event,
2026 srcEng, m_srcTime, *m_obs,
2032 for (
int i = 0; i < m_pars.size(); ++i, ++index) {
2033 kernels[index] = m_pars[i]->factor_gradient();
2038 #if defined(G_NAN_CHECK)
2040 std::cout <<
"*** ERROR: GResponse::edisp_kern::eval";
2041 std::cout <<
"(etrue=" << etrue <<
"): NaN/Inf encountered";
2042 std::cout <<
" (value=" << kernels[0] <<
")" << std::endl;
2070 double model = m_model->eval(theta, *m_srcEng, *m_srcTime);
2076 double phi_min = 0.0;
2093 irf = integral.
romberg(phi_min, phi_max, m_iter_phi) *
2094 model * std::sin(theta);
2117 double cos_phi = std::cos(phi);
2118 double sin_phi = std::sin(phi);
2119 GVector native(-cos_phi*m_sin_theta, sin_phi*m_sin_theta, m_cos_theta);
2122 GVector vector = (*m_rot) * native;
2128 GPhoton photon(dir, *m_srcEng, *m_srcTime);
2131 double irf = m_rsp->irf(*m_event, photon, *m_obs);
2157 double phi_min = 0.0;
2175 irf = integral.
romberg(phi_min, phi_max, m_iter_phi) * std::sin(theta);
2199 double model = m_model->eval(m_theta, phi, *m_srcEng, *m_srcTime);
2205 double cos_phi = std::cos(phi);
2206 double sin_phi = std::sin(phi);
2207 GVector native(-cos_phi*m_sin_theta, sin_phi*m_sin_theta, m_cos_theta);
2210 GVector vector = (*m_rot) * native;
2216 GPhoton photon(dir, *m_srcEng, *m_srcTime);
2219 irf = m_rsp->irf(*m_event, photon, *m_obs) * model;
Energy boundaries class interface definition.
Energy value class definition.
Abstract event base class definition.
Exception handler interface definition.
Integration class interface definition.
Integration class for set of functions interface definition.
Mathematical function definitions.
Sparse matrix class definition.
Generic matrix class definition.
Sky model class interface definition.
Spatial composite model class interface definition.
Spatial map cube model class interface definition.
Spatial map model class interface definition.
Abstract diffuse spatial model base class interface definition.
Abstract elliptical spatial model base class interface definition.
Point source spatial model class interface definition.
Abstract radial spatial model base class interface definition.
Gaussian spectral model class interface definition.
Abstract observation base class interface definition.
Abstract response base class definition.
Sky map class definition.
Time class interface definition.
@ GMODEL_SPATIAL_ELLIPTICAL
@ GMODEL_SPATIAL_COMPOSITE
@ GMODEL_SPATIAL_POINT_SOURCE
double sum(const GVector &vector)
Computes vector sum.
Vector class interface definition.
Energy boundaries container class.
const GEnergy & emax(void) const
Return maximum energy of all intervals.
void append(const GEnergy &emin, const GEnergy &emax)
Append energy interval.
int size(void) const
Return number of energy boundaries.
const GEnergy & emin(void) const
Return minimum energy of all intervals.
Class that handles energies in a unit independent way.
double MeV(void) const
Return energy in MeV.
Abstract interface for the event classes.
virtual const GEnergy & energy(void) const =0
virtual int size(void) const =0
Abstract instrument direction base class.
GIntegral class interface definition.
double romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
void fixed_iter(const int &iter)
Set fixed number of iterations.
Integration class for set of functions.
GVector romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
void fixed_iter(const int &iter)
Set fixed number of iterations.
const int & rows(void) const
Return number of matrix rows.
const int & columns(void) const
Return number of matrix columns.
Sparse matrix class interface definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Generic matrix class definition.
void eulerz(const double &angle)
Set Euler rotation matrix around z axis.
void eulery(const double &angle)
Set Euler rotation matrix around y axis.
GModelSpectral * spectral(void) const
Return spectral model component.
GModelTemporal * temporal(void) const
Return temporal model component.
GModelSpatial * spatial(void) const
Return spatial model component.
const GModelSpatial * component(const int &index) const
Returns pointer to spatial component element.
int components(void) const
Return number of model components.
double sum_of_scales(void) const
Returns sum of all model scales.
double scale(const int &index) const
Returns scale of spatial component.
const GSkyMap & cube(void) const
Get map cube.
virtual double eval(const GPhoton &photon, const bool &gradients=false) const
Evaluate function.
const GSkyMap & map(void) const
Get map.
Abstract diffuse spatial model base class.
virtual double eval(const GPhoton &photon, const bool &gradients=false) const =0
Abstract elliptical spatial model base class.
const GSkyDir & dir(void) const
Return position of elliptical spatial model.
virtual double theta_max(void) const =0
Point source spatial model.
const GSkyDir & dir(void) const
Return position of point source.
Abstract radial spatial model base class.
virtual double theta_max(void) const =0
const GSkyDir & dir(void) const
Return position of radial spatial model.
Abstract spatial model base class.
bool has_free_pars(void) const
Checks if the spatial model has free parameters.
virtual GClassCode code(void) const =0
int size(void) const
Return number of parameters.
Gaussian spectral model class.
GEnergy mean(void) const
Return mean energy.
GEnergy sigma(void) const
Return energy width.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const =0
int size(void) const
Return number of parameters.
virtual double eval(const GTime &srcTime, const bool &gradients=false) const =0
int size(void) const
Return number of parameters.
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
bool has_scales(void) const
Signals that model has scales.
int size(void) const
Return number of parameters in model.
const std::string & name(void) const
Return parameter name.
int scales(void) const
Return number of scale parameters in model.
Abstract observation base class.
virtual GEvents * events(void)
Return events.
virtual std::string instrument(void) const =0
void id(const std::string &id)
Set observation identifier.
bool is_free(void) const
Signal if parameter is free.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const double & factor_gradient(void) const
Return parameter factor gradient.
const std::string & name(void) const
Return parameter name.
Class that handles photons.
bool contains(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, double *value=NULL) const
Check if cache contains a value for specific parameters.
void remove(const std::string &name)
Remove cache for source.
void set(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, const double &value)
Set cache value.
void clear(void)
Clear response cache.
bool contains(const std::string &cache_id, GVector *irfs=NULL) const
Check if cache contains a value for specific parameters.
void remove(const std::string &cache_id)
Remove cache.
void clear(void)
Clear response vector cache.
void set(const std::string &cache_id, const GVector &vector)
Set cache value.
const GEvent * m_event
Event.
const GResponse * m_parent
Response.
std::vector< GModelPar * > m_pars
Parameter pointers.
GVector eval(const double &etrue)
Evaluate energy dispersion integration kernel.
bool m_grad
Gradient flag.
int m_size
Array of values and gradients.
const GModelSky * m_model
Sky model.
edisp_kerns(const GResponse *parent, const GObservation *obs, const GModelSky *model, const GEvent *event, const GTime &srcTime, const bool &grad)
Constructor for energy dispersion integration kernels class.
GTime m_srcTime
True arrival time.
const GObservation * m_obs
Observation.
double eval(const double &phi)
Azimuth angle integration kernel for elliptical model.
double eval(const double &phi)
Zenith angle integration kernel for elliptical model.
double eval(const double &phi)
Azimuth angle integration kernel for radial model.
double eval(const double &phi)
Zenith angle integration kernel for radial model.
Abstract instrument response base class.
GResponseCache m_irf_cache
GVector eval_probs(const GModelSky &model, const GObservation &obs, GMatrixSparse *gradients=NULL) const
Convolve sky model with the instrument response.
virtual double irf_spatial(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response integrated over the spatial model.
int m_irf_elliptical_iter_theta
Elliptical model integration theta iterations.
virtual void remove_response_cache(const std::string &name)
Remove response cache for model.
int m_irf_radial_iter_phi
Radial model integration phi iterations.
void copy_members(const GResponse &rsp)
Copy class members.
virtual ~GResponse(void)
Destructor.
virtual double irf_ptsrc(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to point source.
GResponseCache m_nroi_cache
double eval_prob(const GModelSky &model, const GEvent &event, const GEnergy &srcEng, const GTime &srcTime, const GObservation &obs, const bool &grad) const
Convolve sky model with the instrument response.
virtual double irf(const GEvent &event, const GPhoton &photon, const GObservation &obs) const =0
GResponse(void)
Void constructor.
virtual double convolve(const GModelSky &model, const GEvent &event, const GObservation &obs, const bool &grad=true) const
Convolve sky model with the instrument response.
virtual double irf_radial(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to radial source.
GEbounds ebounds_model(const GModelSky &model) const
Return true energy intervals for sky model.
int size_edisp_vector(const GModelSky &model, const GObservation &obs, const bool &grad) const
Return size of vector for energy dispersion computation.
int m_irf_elliptical_iter_phi
Elliptical model integration phi iterations.
bool m_use_nroi_cache
Control usage of nroi cache.
virtual GEbounds ebounds(const GEnergy &obsEng) const =0
virtual double irf_elliptical(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to elliptical source.
virtual double irf_diffuse(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to diffuse source.
virtual bool use_edisp(void) const =0
GResponseVectorCache m_irf_vector_cache
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
bool m_use_irf_cache
Control usage of irf cache.
virtual double irf_composite(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to composite source.
virtual GResponse & operator=(const GResponse &rsp)
Assignment operator.
double m_irf_diffuse_resolution
Angular resolution for diffuse model.
int m_irf_radial_iter_theta
Radial model integration theta iterations.
double dec_deg(void) const
Returns Declination in degrees.
double ra_deg(void) const
Returns Right Ascension in degrees.
double solidangle(const int &index) const
Returns solid angle of pixel.
GSkyDir inx2dir(const int &index) const
Returns sky direction of pixel.
const int & npix(void) const
Returns number of pixels.
Class that handles gamma-ray sources.
const GEnergy & energy(void) const
Return photon energy.
const std::string & name(void) const
Return model name.
const GTime & time(void) const
Return photon arrival time.
const GModelSpatial * model(void) const
Return spatial model component.
bool is_infinite(const double &x)
Signal if argument is infinite.
bool is_notanumber(const double &x)
Signal if argument is not a number.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.