43#define G_MC "GCTABackground3D::mc(GEnergy&, GTime&, GRan&)"
44#define G_SET_MEMBERS "GCTABackground3D::set_members()"
45#define G_INIT_MC_CACHE "GCTABackground3D::init_mc_cache()"
193 const double& dety)
const
210 double rate_emin = this->
rate(energy_nodes.
inx_left(), detx, dety);
211 double rate_emax = this->
rate(energy_nodes.
inx_right(), detx, dety);
215 if (rate_emin > 0.0 && rate_emax > 0.0) {
218 rate = std::exp(energy_nodes.
wgt_left() * std::log(rate_emin) +
219 energy_nodes.
wgt_right() * std::log(rate_emax));
307 for (
int i = 0; i <
table.ncols(); ++i) {
308 std::string colname =
table[i]->name();
309 if ((colname !=
"DETX_LO") && (colname !=
"DETX_HI") &&
310 (colname !=
"DETY_LO") && (colname !=
"DETY_HI") &&
311 (colname !=
"ENERG_LO") && (colname !=
"ENERG_HI") &&
312 (colname !=
"BKG") && (colname !=
"BGD")) {
373 if (extname.empty()) {
431 table.extname(extname);
470 if (energy < emin || energy > emax) {
471 std::string msg =
"Event energy "+energy.
print()+
" is outside the "
472 "energy range ["+emin.
print()+
", "+emax.
print()+
"] "
473 "covered by the background response table. Please "
474 "restrict the energy range of the simulation to the "
475 "validity range of the background response table.";
495 int inx_left = energy_nodes.
inx_left();
496 int inx_right = energy_nodes.
inx_right();
500 double max_rate =
m_mc_max[inx_left];
501 if (
m_mc_max[inx_right] > max_rate) {
520 double value = this->
operator()(logE, detx, dety);
527 if (value > max_rate) {
528 std::string msg =
"Background rate "+
gammalib::str(value)+
" "
529 "for "+energy.
print()+
" at "
533 "is larger than the maximum expected rate "+
535 "wrong with the code.";
540 double uniform = ran.
uniform() * max_rate;
543 if (uniform <= value) {
591 double x1 = emin.
MeV();
617 if (f1 > 0.0 && f2 > 0.0) {
634 if (x1 < emax.
MeV()) {
637 if (f1 > 0.0 && f2 > 0.0) {
664 result.append(
"=== GCTABackground3D ===");
818 std::string msg =
"Expected three-dimensional background "
819 "response table but found "+
821 " dimensions. Please specify a three-dimensional "
902 const int& iebin)
const
929 const int nsubbins = 10;
978 for (
int i = 0; i < energies.
size(); ++i) {
981 double logE = energies[i].log10TeV();
986 double total_flux = 0.0;
987 double dx = 0.5 * detx_bin;
988 double dy = 0.5 * dety_bin;
990 for (
int ix = 0; ix <
m_num_detx; ++ix, detx += detx_bin) {
992 for (
int iy = 0; iy <
m_num_dety; ++iy, dety += dety_bin) {
995 double i0 = (*this)(logE, detx, dety);
996 double i1 = (*this)(logE, detx-dx, dety-dy);
997 double i2 = (*this)(logE, detx, dety-dy);
998 double i3 = (*this)(logE, detx+dx, dety-dy);
999 double i4 = (*this)(logE, detx+dx, dety);
1000 double i5 = (*this)(logE, detx+dx, dety+dy);
1001 double i6 = (*this)(logE, detx, dety+dy);
1002 double i7 = (*this)(logE, detx-dx, dety+dy);
1003 double i8 = (*this)(logE, detx-dx, dety);
1006 double s1 =
solid_angle(detx, dety, detx-dx, dety-dy, detx, dety-dy);
1007 double s2 =
solid_angle(detx, dety, detx, dety-dy, detx+dx, dety-dy);
1008 double s3 =
solid_angle(detx, dety, detx+dx, dety-dy, detx+dx, dety);
1009 double s4 =
solid_angle(detx, dety, detx+dx, dety, detx+dx, dety+dy);
1010 double s5 =
solid_angle(detx, dety, detx+dx, dety+dy, detx, dety+dy);
1011 double s6 =
solid_angle(detx, dety, detx, dety+dy, detx-dx, dety+dy);
1012 double s7 =
solid_angle(detx, dety, detx-dx, dety+dy, detx-dx, dety);
1013 double s8 =
solid_angle(detx, dety, detx-dx, dety, detx-dx, dety-dy);
1016 double flux1 = s1 * (i1 + i2 + i0);
1017 double flux2 = s2 * (i2 + i3 + i0);
1018 double flux3 = s3 * (i3 + i4 + i0);
1019 double flux4 = s4 * (i4 + i5 + i0);
1020 double flux5 = s5 * (i5 + i6 + i0);
1021 double flux6 = s6 * (i6 + i7 + i0);
1022 double flux7 = s7 * (i7 + i8 + i0);
1023 double flux8 = s8 * (i8 + i1 + i0);
1024 double flux = (flux1 + flux2 + flux3 + flux4 +
1025 flux5 + flux6 + flux7 + flux8) / 3.0;
1037 if (total_flux > 0.0) {
1042 #if defined(G_DEBUG_MC_INIT)
1043 std::cout <<
"Energy=" << energies[i];
1044 std::cout <<
" Rate_total=" << total_flux;
1045 std::cout <<
" events/(s MeV)" << std::endl;
1053 std::string msg =
"Background response table is empty. Please provide "
1054 "a valid three-dimensional background template.";
1079 double max_rate = 0.0;
1086 int inx =
index(ix, iy, iebin);
1093 if (
rate > max_rate) {
1142 const double& detx2,
const double& dety2,
1143 const double& detx3,
const double& dety3)
const
1146 double theta1 = std::sqrt(detx1 * detx1 + dety1 * dety1);
1147 double phi1 = std::atan2(dety1, detx1);
1148 double theta2 = std::sqrt(detx2 * detx2 + dety2 * dety2);
1149 double phi2 = std::atan2(dety2, detx2);
1150 double theta3 = std::sqrt(detx3 * detx3 + dety3 * dety3);
1151 double phi3 = std::atan2(dety3, detx3);
1160 double a12 = dir1.
dist(dir2);
1161 double a13 = dir1.
dist(dir3);
1162 double a23 = dir2.
dist(dir3);
1165 double s = 0.5 * (a12 + a23 + a13);
1166 double solidangle = 4.0 * std::atan(std::sqrt(std::tan(0.5*s) *
1167 std::tan(0.5*(s-a12)) *
1168 std::tan(0.5*(s-a23)) *
1169 std::tan(0.5*(s-a13))));
1186 const double& dety)
const
CTA 3D background class definition.
CTA instrument direction class interface definition.
Definition of support function used by CTA classes.
Exception handler interface definition.
Filename class interface definition.
FITS binary table class definition.
FITS file class interface definition.
Mathematical function definitions.
Random number generator class definition.
void init_members(void)
Initialise class members.
double m_dety_min
DETY minimum (radians)
double m_dety_max
DETY maximum (radians)
void init_mc_cache(void) const
Initialise Monte Carlo cache.
GCTABackground3D * clone(void) const
Clone background.
void free_members(void)
Delete class members.
void read(const GFitsTable &table)
Read background from FITS table.
void save(const GFilename &filename, const bool &clobber=false) const
Save background into FITS file.
GFilename m_filename
Name of background response file.
GFilename filename(void) const
Return filename.
const GCTAResponseTable & table(void) const
Return response table.
int m_num_detx
Number of DETX bins.
virtual ~GCTABackground3D(void)
Destructor.
int m_num_energy
Number of energy bins.
int m_num[3]
Array of number of bins.
GCTAResponseTable m_background
Background response table.
GCTABackground3D & operator=(const GCTABackground3D &bgd)
Assignment operator.
void load(const GFilename &filename)
Load background from FITS file.
std::string print(const GChatter &chatter=NORMAL) const
Print background information.
GCTAInstDir mc(const GEnergy &energy, const GTime &time, GRan &ran) const
Returns MC instrument direction.
double m_logE_min
Log10(E/TeV) minimum.
double solid_angle(const double &detx1, const double &dety1, const double &detx2, const double &dety2, const double &detx3, const double &dety3) const
Compute solid angle of pixel wedge.
int m_inx_detx
DETX index.
GEnergies m_energy
Vector of energies.
void write(GFitsBinTable &table) const
Write background into FITS table.
void copy_members(const GCTABackground3D &bgd)
Copy class members.
int m_inx_dety
DETY index.
int m_num_dety
Number of DETY bins.
GModelSpectralNodes m_mc_spectrum
Response cube spectrum.
int index(const int &idetx, const int &idety, const int &iebin) const
Return background rate bin index.
double rate(const int &iebin, const double &detx, const double &dety) const
Return background rate for a given energy bin and DETX-DETY value (events/s/MeV/sr)
GCTABackground3D(void)
Void constructor.
double rate_ebin(const GCTAInstDir &dir, const GEnergy &emin, const GEnergy &emax) const
Returns background rate integrated over energy interval in units of events s sr .
double m_logE_max
Log10(E/TeV) maximum.
int m_inx_energy
Energy index.
bool is_valid(void) const
Return validity of background model.
void init_mc_max_rate(void) const
Initialise array of maximum background rate.
void set_members(void)
Set members from background table.
std::vector< double > m_mc_max
Maximum background rate.
double m_detx_min
DETX minimum (radians)
double m_detx_max
DETX maximum (radians)
void clear(void)
Clear background.
int m_inx_bgd
Background index.
virtual double operator()(const double &logE, const double &detx, const double &dety) const
Return background rate in units of events MeV s sr .
Abstract base class for the CTA background model.
void init_members(void)
Initialise class members.
GCTABackground & operator=(const GCTABackground &bgd)
Assignment operator.
void free_members(void)
Delete class members.
CTA instrument direction class.
void dety(const double &y)
Set DETY coordinate (in radians)
void detx(const double &x)
Set DETX coordinate (in radians)
GCTAResponseTable * clone(void) const
Clone response table.
void read(const GFitsTable &table)
Read response table from FITS table HDU.
const std::string & axis_lo_unit(const int &axis) const
Return lower bin boundary unit for axis.
int table(const std::string &name) const
Determine index of table.
const std::string & axis_hi_unit(const int &axis) const
Return upper bin boundary unit for axis.
void axis_log10(const int &axis)
Set nodes for a logarithmic (base 10) axis.
void axis_radians(const int &axis)
Set nodes for a radians axis.
int axis(const std::string &name) const
Determine index of an axis.
const int & axes(void) const
Return number of axes of the tables.
void write(GFitsTable &table) const
Write response table into FITS table HDU.
int axis_bins(const int &axis) const
Return number bins in an axis.
const double & axis_hi(const int &axis, const int &bin) const
Return upper bin boundary for bin in axis.
bool has_table(const std::string &name) const
Check whether a table exists.
const GNodeArray & axis_nodes(const int &axis) const
Return axis nodes.
void clear(void)
Clear response table.
const double & axis_lo(const int &axis, const int &bin) const
Return lower bin boundary for bin in axis.
void clear(void)
Clear energy container.
int size(void) const
Return number of energies in container.
void extend(const GEnergies &energies)
Append energy container.
GEnergy & append(const GEnergy &energy)
Append energy to container.
void remove(const int &index)
Remove energy from container.
Class that handles energies in a unit independent way.
double log10TeV(void) const
Return log10 of energy in TeV.
double MeV(void) const
Return energy in MeV.
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
double TeV(void) const
Return energy in TeV.
std::string url(void) const
Return Uniform Resource Locator (URL)
std::string extname(const std::string &defaultname="") const
Return extension name.
void clear(void)
Clear file name.
Abstract interface for FITS table.
void remove(const int &colnum)
Remove column from the table.
bool contains(const int &extno) const
Check if HDU exists in FITS file.
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
void close(void)
Close FITS file.
void remove(const int &extno)
Remove HDU from FITS file.
void save(const bool &clobber=false)
Saves FITS file.
GFitsTable * table(const int &extno)
Get pointer to table HDU.
virtual void clear(void)
Clear spectral nodes model.
int nodes(void) const
Return number of nodes.
void append(const GEnergy &energy, const double &intensity)
Append node.
GEnergy energy(const int &index) const
Return node energy.
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
const int & inx_right(void) const
Returns right node index.
const int & inx_left(void) const
Returns left node index.
const double & wgt_right(void) const
Returns right node weight.
const double & wgt_left(void) const
Returns left node weight.
Random number generator class.
double uniform(void)
Returns random double precision floating value in range 0 to 1.
void radec(const double &ra, const double &dec)
Set equatorial sky direction (radians)
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
double plaw_integral(const double &x1, const double &f1, const double &x2, const double &f2)
Returns the integral of a power law.
const std::string extname_cta_background3d
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
std::string gadf_hduclas4(const GFits &fits, const std::string &hduclas4)
Return extension name for GADF response table of given HDU class 4.
void warning(const std::string &origin, const std::string &message)
Emits warning.