47#define G_IRF "GCOSResponse::irf(GEvent&, GSource&, GObservation&)"
48#define G_NROI "GCOSResponse::nroi(GModelSky&, GEnergy&, GTime&, "\
50#define G_EBOUNDS "GCOSResponse::ebounds(GEnergy&)"
51#define G_LOAD "GCOSResponse::load(GFilename&)"
52#define G_LOAD_RESPONSE_CHUNK "GCOSResponse::load_response_chunk(int&)"
53#define G_READ_BOUNDS "GCOSResponse::read_bounds(FILE*, GXmlElement*, "\
54 "GXmlElement*, GXmlElement*, std::string&)"
55#define G_READ_EBOUNDS "GCOSResponse::read_ebounds(FILE*, GXmlElement*, "\
56 "GXmlElement*, GXmlElement*, std::string&)"
57#define G_READ_NDARRAY "GCOSResponse::read_ndarray(FILE*, GXmlElement*, "\
58 "GXmlElement*, GXmlElement*)"
59#define G_READ_RESPONSE_MATRIX "GCOSResponse::read_response_matrix(FILE*, "\
60 "GXmlElement*, GXmlElement*, GXmlElement*, GXmlElement*)"
61#define G_GET_RESPONSE_CHUNK_KEY "GCOSResponse::get_response_chunk_key(int&)"
255 if (observation == NULL) {
256 std::string cls = std::string(
typeid(&obs).name());
257 std::string msg =
"Observation of type \""+cls+
"\" is not a COSI "
258 "observation. Please specify a COSI observation "
266 std::string cls = std::string(
typeid(&event).name());
267 std::string msg =
"Event of type \""+cls+
"\" is not a COSI event. "
268 "Please specify a COSI event as argument.";
276 int itdir = int(
src_dirs().dir2pix(photon.
dir()).index() + 0.5);
281 if ((itdir >= 0) && (iteng >= 0)) {
290 int imdir = int(
obs_dirs().dir2pix(bin->
dir().
dir()).index() + 0.5);
293 if ((imdir >= 0) && (imphi >= 0) && (imeng >= 0)) {
315 #if defined(G_NAN_CHECK)
317 std::cout <<
"*** ERROR: GCOSResponse::irf:";
318 std::cout <<
" NaN/Inf encountered";
319 std::cout <<
" (irf=" <<
irf;
321 std::cout << std::endl;
344 const GTime& obsTime,
348 std::string msg =
"Spatial integration of sky model over the data space "
349 "is not implemented.";
374 std::string msg =
"Energy dispersion not implemented.";
403 const int& ipol)
const
417 if (idir < 0 || idir >= ndirs) {
419 "True photon direction index",
422 if (ieng < 0 || ieng >= nengs) {
424 "True photon energy index",
427 if ((npols > 0) && (ipol < 0 || ipol >= npols)) {
429 "True polarisation angle index",
449 FILE* fptr = std::fopen(fname.c_str(),
"rb");
451 std::string msg =
"Unable to open file \""+fname+
"\" for "
452 "read access. Please load a readable "
453 "HDF5 file before calling that method.";
468 int length = data.length();
470 int elements = length / size;
473 std::vector<uint16_t> chunk(elements, 0);
476 const char* ptr = data.c_str();
479 for (
int i = 0; i < elements; ++i, ptr += size) {
522 GFits fits(filename);
539 FILE* fptr = std::fopen(fname.c_str(),
"rb");
541 std::string msg =
"Unable to open file \""+fname+
"\" for read access. "
542 "Please specify a readable file.";
584 fits.
saveto(filename, clobber);
607 int size = eff_area->
nrows();
609 for (
int i = 0; i < size; ++i) {
621 const GFitsTable* src_pol = fits.
table(
"TRUE POLARISATION BOUNDARIES");
650 int chunks = src_healpix->
nrows();
653 for (
int i = 0; i < chunks; ++i) {
659 std::string extname = col_name->
string(i);
668 int npix = col_chunk->
nrows();
669 int ncols = col_chunk->
number();
675 for (
int col = 0, inx = 0; col < ncols; ++col) {
676 for (
int ipix = 0; ipix < npix; ++ipix, ++inx) {
714 eff_area.
extname(
"EFFECTIVE AREAS");
719 for (
int i = 0; i < size; ++i) {
722 eff_area.
append(col_eff_area);
730 src_healpix.
extname(
"TRUE SKY DIRECTIONS");
734 for (
int i = 0; i < chunks; ++i) {
738 src_healpix.
append(col_index);
739 src_healpix.
append(col_name);
761 obs_healpix.
extname(
"MEASURED SKY DIRECTIONS");
765 for (
int i = 0; i < chunks; ++i) {
771 int ncols = elements / npix;
773 for (
int col = 0, inx = 0; col < ncols; ++col) {
774 for (
int ipix = 0; ipix < npix; ++ipix, ++inx) {
802 result.append(
"=== GCOSResponse ===");
808 result.append(
"not defined");
819 result.append(
"not defined");
823 result.append(
" - ");
825 result.append(
" keV");
831 result.append(
"not defined");
835 result.append(
" - ");
837 result.append(
" deg");
843 result.append(
"not defined");
854 result.append(
"not defined");
858 result.append(
" - ");
860 result.append(
" deg");
866 result.append(
"not defined");
870 result.append(
" - ");
872 result.append(
" keV");
878 result.append(
"not defined");
882 result.append(
" - ");
915 int naxes = axes_table->
elements(
"entry");
919 int ndesc = axis_desc_header->
elements(
"message");
922 for (
int i = 0; i < naxes; ++i) {
946 if (desc.length() > 0) {
950 if (unit.length() > 0) {
953 if (scale.length() > 0) {
956 if (nside.length() > 0) {
959 if (scheme.length() > 0) {
962 if (coordsys.length() > 0) {
1079 int naxes = axes_table->
elements(
"entry");
1082 for (
int i = 0; i < naxes; ++i) {
1102 if (label ==
"Ei") {
1105 else if (label ==
"Em") {
1108 else if (label ==
"Phi") {
1112 else if (label ==
"NuLambda") {
1115 else if (label ==
"PsiChi") {
1118 else if (label ==
"Pol") {
1174 if (dataspace == NULL) {
1175 std::string msg =
"Invalid dataspace pointer specified. No dataspace "
1176 "information was found in the COSI response file. "
1177 "Please specify a valid COSI response file.";
1180 if (datatype == NULL) {
1181 std::string msg =
"Invalid datatype pointer specified. No datatype "
1182 "information was found in the COSI response file. "
1183 "Please specify a valid COSI response file.";
1186 if (datalayout == NULL) {
1187 std::string msg =
"Invalid datalayout pointer specified. No datalayout "
1188 "information was found in the COSI response file. "
1189 "Please specify a valid COSI response file.";
1194 std::string data =
fread_data(fptr, dataspace, datatype, datalayout);
1202 const char* ptr = data.c_str();
1203 for (
int i = 0; i < elements; ++i, ptr += size) {
1240 const std::string& unit)
1246 if (dataspace == NULL) {
1247 std::string msg =
"Invalid dataspace pointer specified. No dataspace "
1248 "information was found in the COSI response file. "
1249 "Please specify a valid COSI response file.";
1252 if (datatype == NULL) {
1253 std::string msg =
"Invalid datatype pointer specified. No datatype "
1254 "information was found in the COSI response file. "
1255 "Please specify a valid COSI response file.";
1258 if (datalayout == NULL) {
1259 std::string msg =
"Invalid datalayout pointer specified. No datalayout "
1260 "information was found in the COSI response file. "
1261 "Please specify a valid COSI response file.";
1266 std::string data =
fread_data(fptr, dataspace, datatype, datalayout);
1273 std::string str_unit = (unit.empty()) ?
"keV" : unit;
1277 const char* ptr = data.c_str();
1278 for (
int i = 0; i < elements; ++i, ptr += size) {
1319 if (dataspace == NULL) {
1320 std::string msg =
"Invalid dataspace pointer specified. No dataspace "
1321 "information was found in the COSI response file. "
1322 "Please specify a valid COSI response file.";
1325 if (datatype == NULL) {
1326 std::string msg =
"Invalid datatype pointer specified. No datatype "
1327 "information was found in the COSI response file. "
1328 "Please specify a valid COSI response file.";
1331 if (datalayout == NULL) {
1332 std::string msg =
"Invalid datalayout pointer specified. No datalayout "
1333 "information was found in the COSI response file. "
1334 "Please specify a valid COSI response file.";
1339 std::string data =
fread_data(fptr, dataspace, datatype, datalayout);
1349 const char* ptr = data.c_str();
1350 for (
int i = 0; i < elements; ++i, ptr += size) {
1380 if (dataspace == NULL) {
1381 std::string msg =
"Invalid dataspace pointer provided for response "
1382 "matrix. No dataspace information was provided "
1383 "in the COSI response file. Please specify a "
1384 "valid COSI response file.";
1387 if (datatype == NULL) {
1388 std::string msg =
"Invalid datatype pointer provided for response "
1389 "matrix. No datatype information was provided "
1390 "in the COSI response file. Please specify a "
1391 "valid COSI response file.";
1394 if (datalayout == NULL) {
1395 std::string msg =
"Invalid datalayout pointer provided for response "
1396 "matrix. No datalayout information was provided "
1397 "in the COSI response file. Please specify a "
1398 "valid COSI response file.";
1464 const int& ipol)
const
1479 int nkeys = btree->
elements(
"key");
1486 int ikey_max = nkeys;
1489 int ikey = nkeys / 2;
1492 while (ikey_min != ikey_max) {
1495 key = btree->
element(
"key", ikey);
1502 int index_key =
flat_index(idir_key, ieng_key, ipol_key);
1505 if (index_key > index) {
1507 ikey = ikey_min + (ikey_max-ikey_min) / 2;
1508 if (ikey == ikey_max) {
1514 else if (index_key < index) {
1516 ikey = ikey_min + (ikey_max-ikey_min) / 2;
1517 if (ikey == ikey_min) {
1528 if (ikey_min == ikey_max) {
1541 btree = key->
element(
"btree");
1550 std::string msg =
"Response chunk with index "+
gammalib::str(index)+
1551 " not found. This should never happen if the HDF5 "
1561 int index_key =
flat_index(idir_key, ieng_key, ipol_key);
1562 if (index_key != index) {
1563 std::string msg =
"Bad response chunk with index " +
1565 " found while index " +
1567 "should never happen, the code is not doing "
1568 "what it is expected to do.";
1591 const int& ipol)
const
1606 for (
int i = 0; i < n; ++i) {
1638 const int& ipol)
const
1645 int index = (npols == 0) ? idir * nengs + ieng
1646 : (idir * nengs + ieng) * npols + ipol;
COSI observation class definition.
#define G_READ_RESPONSE_MATRIX
#define G_LOAD_RESPONSE_CHUNK
#define G_GET_RESPONSE_CHUNK_KEY
COSI instrument response function class definition.
Exception handler interface definition.
FITS binary table class definition.
FITS table double column class interface definition.
FITS table long integer column class interface definition.
FITS table string column class interface definition.
FITS table unsigned short integer column class interface definition.
FITS file class interface definition.
Point source spatial model class interface definition.
double min(const GVector &vector)
Computes minimum vector element.
double max(const GVector &vector)
Computes maximum vector element.
Boundaries container class.
void read(const GFitsTable &table)
Read boundaries from FITS table.
void clear(void)
Clear boundaries.
const std::string & unit(void) const
Return boundary units.
void append(const double &min, const double &max)
Append interval.
double max(void) const
Return maximum value of all intervals.
double min(void) const
Return minimum value of all intervals.
void write(GFits &file, const std::string &extname=gammalib::extname_bounds) const
Write boundaries into FITS object.
int size(void) const
Return number of boundaries.
int index(const double &value) const
Returns bin index for a value.
virtual const GCOSInstDir & dir(void) const
Return instrument direction of event bin.
virtual const GEnergy & energy(void) const
Return energy of event bin.
void phi(const double &phi)
Set event Compton scatter angle.
void dir(const GSkyDir &dir)
Set event scatter direction in celestial coordinates.
const GCOSSpaceCraft & spacecraft(void) const
Return space craft information.
COSI instrument response function class.
const GEbounds & obs_ebounds(void) const
Return measured event energy boundaries.
const GXmlElement * m_rsp_chunk_dataspace
Data space.
const GXmlElement * get_response_chunk_key(const int &idir, const int &ieng=0, const int &ipol=0) const
GCOSResponse(void)
Void constructor.
GBounds m_obs_phis
Boundaries for observed scatter angles.
virtual void clear(void)
Clear instance.
virtual double irf(const GEvent &event, const GPhoton &photon, const GObservation &obs) const
GEbounds m_src_ebounds
Energy boundaries for source photons.
void copy_members(const GCOSResponse &rsp)
const GXmlElement * m_rsp_chunk_datatype
Data type.
void read_response_matrix(FILE *fptr, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout, const GXmlElement *datafilter)
GEbounds read_ebounds(FILE *fptr, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout, const std::string &unit)
int get_response_chunk(const int &idir, const int &ieng=0, const int &ipol=0) const
const GHealpix & obs_dirs(void) const
Return HealPix projection of measured scatter directions.
virtual double nroi(const GModelSky &model, const GEnergy &obsEng, const GTime &obsTime, const GObservation &obs) const
const GXmlElement * m_rsp_chunk_datafilter
Data filter.
const GNdarray & eff_areas(void) const
Return effective area array.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print content of object.
void set_response_chunk_pointers(void)
const GHealpix & src_dirs(void) const
Return HealPix projection of true photon directions.
const GXmlElement * m_rsp_chunk_datalayout
Data layout.
GNdarray read_ndarray(FILE *fptr, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout)
void save(const GFilename &filename, const bool &clobber=false) const
GEbounds m_obs_ebounds
Energy boundaries for observed events.
GFilename m_rspfile
Response file name.
GBounds read_bounds(FILE *fptr, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout)
virtual GCOSResponse & operator=(const GCOSResponse &rsp)
Assignment operator.
void read(const GFits &fits)
GHealpix m_obs_dirs
Sky projection for observed events.
GNdarray m_eff_areas
Array of effective areas.
virtual ~GCOSResponse(void)
Destructor.
int get_response_chunk_index(const int &idir, const int &ieng=0, const int &ipol=0) const
GHealpix m_src_dirs
Sky projection for source photons.
void write(GFits &fits) const
int flat_index(const int &idir, const int &ieng=0, const int &ipol=0) const
std::vector< int > m_rsp_chunk_indices
Loaded indices.
void read_response(FILE *fptr)
virtual GEbounds ebounds(const GEnergy &obsEnergy) const
const GEbounds & src_ebounds(void) const
Return true photon energy boundaries.
virtual GCOSResponse * clone(void) const
Clone instance.
GBounds m_src_pols
Boundaries for observed polarisation.
const GBounds & obs_phis(void) const
Return measured scatter angle boundaries.
void load(const GFilename &filename)
std::vector< std::vector< uint16_t > > m_rsp_chunks
Loaded chunks.
const double & livetime(void) const
Return livetime in seconds.
Energy boundaries container class.
int index(const GEnergy &eng) const
Returns energy bin index for a given energy.
const GEnergy & emax(void) const
Return maximum energy of all intervals.
void read(const GFitsTable &table)
Read energy boundaries from FITS table.
void append(const GEnergy &emin, const GEnergy &emax)
Append energy interval.
int size(void) const
Return number of energy boundaries.
void clear(void)
Clear energy boundaries.
bool is_empty(void) const
Signal if there are no energy boundaries.
const GEnergy & emin(void) const
Return minimum energy of all intervals.
void write(GFits &file, const std::string &extname=gammalib::extname_ebounds, const std::string &unit="keV") const
Write energy boundaries into FITS object.
Class that handles energies in a unit independent way.
double keV(void) const
Return energy in keV.
Abstract interface for the event classes.
bool is_fits(void) const
Checks whether file is a FITS file.
std::string url(void) const
Return Uniform Resource Locator (URL)
void clear(void)
Clear file name.
const std::string & extname(void) const
Return extension name.
Abstract interface for FITS table column.
virtual int integer(const int &row, const int &inx=0) const =0
void number(const int &number)
Set number of elements in column.
virtual double real(const int &row, const int &inx=0) const =0
virtual std::string string(const int &row, const int &inx=0) const =0
void nrows(const int &nrows)
Set number of rows in column.
FITS table double column.
FITS table long integer column.
FITS table string column.
FITS table unsigned short integer column.
Abstract interface for FITS table.
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
bool is_empty(void) const
Signals if the FITS table has no row or columns.
const int & nrows(void) const
Return number of rows in table.
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
GFitsTable * table(const int &extno)
Get pointer to table HDU.
const GXmlElement * xml_hdf5_entry(const std::string &name) const
Return XML element to entry tag.
void clear(void)
Clear instance.
void read(FILE *fptr)
Read data from HDF5 file into instance.
bool is_empty(void) const
Signals if HDF5 instance is empty.
HealPix projection class interface definition.
virtual void clear(void)
Clear object.
virtual void write(GFitsHDU &hdu) const
Write Healpix definition into FITS HDU.
std::string ordering(void) const
Returns ordering parameter.
const int & nside(void) const
Returns number of divisions of the side of each base pixel.
const int & npix(void) const
Returns number of pixels.
virtual void read(const GFitsHDU &hdu)
Read Healpix definition from FITS header.
N-dimensional array class.
int size(void) const
Return number of elements in array.
void clear(void)
Clear array.
Abstract observation base class.
Class that handles photons.
const GSkyDir & dir(void) const
Return photon sky direction.
const GEnergy & energy(void) const
Return photon energy.
Abstract instrument response base class.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
virtual GResponse & operator=(const GResponse &rsp)
Assignment operator.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
virtual GXmlElement * element(const int &index)
Return pointer to GXMLElement child.
virtual int elements(void) const
Return number of GXMLElement children of node.
const GXmlElement * xml_msg_datalayout(const GXmlElement *header)
Return "DataStorageLayout" message XML element.
std::string fread_data_chunk(FILE *fptr, const GXmlElement *chunk, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout, const GXmlElement *datafilter=NULL)
Read data chunk.
int data_to_int(const char *data, const GXmlElement *datatype)
Convert data into integer value.
const GXmlElement * xml_msg_datafilter(const GXmlElement *header)
Return "DataStorageFilterPipeline" message XML element.
std::string fread_data(FILE *fptr, const int &nbytes)
Read data from HDF5 file.
double data_to_double(const char *data, const GXmlElement *datatype)
Convert data into double precision floating point value.
const GXmlElement * xml_msg_datatype(const GXmlElement *header)
Return "Datatype" message XML element.
const GXmlElement * xml_msg_attribute(const GXmlElement *header, const std::string &name, const int &number=0)
Return "Attribute" message XML element with specified name.
const GXmlElement * xml_msg_dataspace(const GXmlElement *header)
Return "Dataspace" message XML element.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
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.
int toint(const std::string &arg)
Convert string into integer value.
std::string expand_env(const std::string &arg)
Expand environment variables in string.