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();
219 for (
int i = 0; i < ebounds.
size(); ++i) {
222 double etrue_min = ebounds.
emin(i).
MeV();
223 double etrue_max = ebounds.
emax(i).
MeV();
226 if (ebounds_model.size() > 0) {
227 if (ebounds_model.emin(0).MeV() > etrue_min) {
228 etrue_min = ebounds_model.emin(0).MeV();
230 if (ebounds_model.emax(0).MeV() < etrue_max) {
231 etrue_max = ebounds_model.emax(0).MeV();
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++];
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();
425 for (
int i = 0; i < ebounds.
size(); ++i) {
428 double etrue_min = ebounds.
emin(i).
MeV();
429 double etrue_max = ebounds.
emax(i).
MeV();
432 if (ebounds_model.
size() > 0) {
433 if (ebounds_model.
emin(0).
MeV() > etrue_min) {
434 etrue_min = ebounds_model.
emin(0).
MeV();
436 if (ebounds_model.
emax(0).
MeV() < etrue_max) {
437 etrue_max = ebounds_model.
emax(0).
MeV();
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);
1157 irf += this->
irf(event, photon, obs) * value * skymap.
solidangle(i);
1173 for (
int i = 0; i < skymap.
npix(); ++i) {
1179 GPhoton photon(srcDir, srcEng, srcTime);
1182 double value = cube->
eval(photon);
1192 irf += this->
irf(event, photon, obs) * value * skymap.
solidangle(i);
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);
1232 irf += this->
irf(event, photon, obs) * value * skymap.
solidangle(i);
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
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) {
1994 ebounds.
append(ebound_min, ebound_max);
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) *
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);
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;
double m_irf_diffuse_resolution
Angular resolution for diffuse model.
double eval(const double &phi)
Azimuth angle integration kernel for radial model.
int scales(void) const
Return number of scale parameters in model.
bool m_use_irf_cache
Control usage of irf cache.
bool m_grad
Gradient flag.
const GObservation * m_obs
Observation.
const double & factor_gradient(void) const
Return parameter factor gradient.
double dec_deg(void) const
Returns Declination in degrees.
double romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
Point source spatial model class interface definition.
Energy value class definition.
Abstract elliptical spatial model base class.
const std::string & name(void) const
Return parameter name.
Sparse matrix class interface definition.
virtual std::string instrument(void) const =0
GModelSpectral * spectral(void) const
Return spectral model component.
GResponse(void)
Void constructor.
const GSkyDir & dir(void) const
Return position of point source.
int size(void) const
Return number of parameters.
bool has_free_pars(void) const
Checks if the spatial model has free parameters.
int size(void) const
Return number of parameters.
const GModelSpatial * component(const int &index) const
Returns pointer to spatial component element.
virtual double irf(const GEvent &event, const GPhoton &photon, const GObservation &obs) const =0
int size(void) const
Return number of energy boundaries.
void append(const GEnergy &emin, const GEnergy &emax)
Append energy interval.
GVector cos(const GVector &vector)
Computes cosine of vector elements.
const GSkyMap & map(void) const
Get map.
Generic matrix class definition.
virtual double eval(const GEnergy &srcEng, const GTime &srcTime=GTime(), const bool &gradients=false) const =0
GModelTemporal * temporal(void) const
Return temporal model component.
Abstract radial spatial model base class interface definition.
virtual void remove_response_cache(const std::string &name)
Remove response cache for model.
Abstract interface for the event classes.
void fixed_iter(const int &iter)
Set fixed number of iterations.
const GSkyDir & dir(void) const
Return position of elliptical spatial model.
virtual double irf_composite(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to composite source.
void set(const std::string &cache_id, const GVector &vector)
Set cache value.
double sum(const GVector &vector)
Computes vector sum.
std::vector< GModelPar * > m_pars
Parameter pointers.
double eval(const double &phi)
Azimuth angle integration kernel for elliptical model.
int m_irf_elliptical_iter_theta
Elliptical model integration theta iterations.
double MeV(void) const
Return energy in MeV.
double eval(const double &phi)
Zenith angle integration kernel for radial model.
GIntegral class interface definition.
const std::string & name(void) const
Return model name.
void init_members(void)
Initialise class members.
const GEvent * m_event
Event.
Sky map class definition.
virtual GClassCode code(void) const =0
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.
void eulery(const double &angle)
Set Euler rotation matrix around y axis.
virtual double theta_max(void) const =0
GResponseVectorCache m_irf_vector_cache
void id(const std::string &id)
Set observation identifier.
Gaussian spectral model class.
GEbounds ebounds_model(const GModelSky &model) const
Return true energy intervals for sky model.
Abstract instrument direction base class.
bool is_notanumber(const double &x)
Signal if argument is not a number.
int size(void) const
Return number of parameters in model.
double sum_of_scales(void) const
Returns sum of all model scales.
Class that handles photons.
virtual double irf_elliptical(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to elliptical source.
const double & scale(void) const
Return parameter scale.
bool is_infinite(const double &x)
Signal if argument is infinite.
virtual double irf_diffuse(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to diffuse source.
void remove(const std::string &name)
Remove cache for source.
int m_irf_elliptical_iter_phi
Elliptical model integration phi iterations.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
double eval(const double &phi)
Zenith angle integration kernel for elliptical model.
virtual double eval(const GPhoton &photon, const bool &gradients=false) const =0
const GSkyDir & dir(void) const
Return position of radial spatial model.
Energy boundaries container class.
GSkyDir inx2dir(const int &index) const
Returns sky direction of pixel.
const std::string & name(void) const
Return parameter name.
const int & rows(void) const
Return number of matrix rows.
const GEnergy & emin(void) const
Return minimum energy of all intervals.
void eulerz(const double &angle)
Set Euler rotation matrix around z axis.
Integration class for set of functions interface definition.
int size(void) const
Return number of parameters.
const GResponse * m_parent
Response.
virtual const GEnergy & energy(void) const =0
Abstract event base class definition.
GEnergy mean(void) const
Return mean energy.
void remove(const std::string &cache_id)
Remove cache.
double ra_deg(void) const
Returns Right Ascension in degrees.
bool m_use_nroi_cache
Control usage of nroi cache.
virtual ~GResponse(void)
Destructor.
const GTime & time(void) const
Return photon arrival time.
virtual GEbounds ebounds(const GEnergy &obsEng) const =0
GResponseCache m_nroi_cache
virtual double eval(const GTime &srcTime, const bool &gradients=false) const =0
Spatial composite model class interface definition.
const GModelSpatial * model(void) const
Return spatial model component.
Abstract observation base class.
const GEnergy & energy(void) const
Return photon energy.
Vector class interface definition.
bool has_scales(void) const
Signals that model has scales.
Abstract observation base class interface definition.
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
Point source spatial model.
void free_members(void)
Delete class members.
Abstract elliptical spatial model base class interface definition.
Abstract diffuse spatial model base class interface definition.
virtual bool use_edisp(void) const =0
Abstract response base class definition.
int m_irf_radial_iter_phi
Radial model integration phi iterations.
virtual GResponse & operator=(const GResponse &rsp)
Assignment operator.
virtual double irf_radial(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to radial source.
int components(void) const
Return number of model components.
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.
void clear(void)
Clear response vector cache.
Sky model class interface definition.
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 GVector column(const int &column) const
Extract column as vector from matrix.
Spatial map cube model class interface definition.
Sparse matrix class definition.
virtual double eval(const GPhoton &photon, const bool &gradients=false) const
Evaluate function.
void copy_members(const GResponse &rsp)
Copy class members.
bool contains(const std::string &cache_id, GVector *irfs=NULL) const
Check if cache contains a value for specific parameters.
GVector sin(const GVector &vector)
Computes sine of vector elements.
virtual GEvents * events(void)
Return events.
int m_irf_radial_iter_theta
Radial model integration theta iterations.
Energy boundaries class interface definition.
int size_edisp_vector(const GModelSky &model, const GObservation &obs, const bool &grad) const
Return size of vector for energy dispersion computation.
GVector romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
Exception handler interface definition.
virtual double theta_max(void) const =0
int m_size
Array of values and gradients.
const GModelSky * m_model
Sky model.
void fixed_iter(const int &iter)
Set fixed number of iterations.
GModelSpatial * spatial(void) const
Return spatial model component.
virtual double irf_ptsrc(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to point source.
Abstract spatial model base class.
GResponseCache m_irf_cache
Abstract radial spatial model base class.
Generic matrix class definition.
const GEnergy & emax(void) const
Return maximum energy of all intervals.
void clear(void)
Clear response cache.
Abstract diffuse spatial model base class.
double solidangle(const int &index) const
Returns solid angle of pixel.
GVector eval(const double &etrue)
Evaluate energy dispersion integration kernel.
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.
Integration class for set of functions.
Abstract instrument response base class.
Class that handles gamma-ray sources.
void set(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, const double &value)
Set cache value.
const int & npix(void) const
Returns number of pixels.
virtual double convolve(const GModelSky &model, const GEvent &event, const GObservation &obs, const bool &grad=true) const
Convolve sky model with the instrument response.
Integration class interface definition.
const GSkyMap & cube(void) const
Get map cube.
const int & columns(void) const
Return number of matrix columns.
Time class interface definition.
GTime m_srcTime
True arrival time.
Spatial map model class interface definition.
double scale(const int &index) const
Returns scale of spatial component.
Gaussian spectral model class interface definition.
GVector eval_probs(const GModelSky &model, const GObservation &obs, GMatrixSparse *gradients=NULL) const
Convolve sky model with the instrument response.
GEnergy sigma(void) const
Return energy width.
Mathematical function definitions.
Class that handles energies in a unit independent way.
virtual int size(void) const =0
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.