Commit f2bf0f4696a383fc11d2deadf27268e95301c76f

Authored by Michael Mayer
1 parent 06ecf576

Modified GCTAResponseIrf class to use GFilename implementation

inst/cta/src/GCTAResponseIrf.cpp
... ... @@ -34,6 +34,7 @@
34 34 #include "GFits.hpp"
35 35 #include "GTools.hpp"
36 36 #include "GMath.hpp"
  37 +#include "GFilename.hpp"
37 38 #include "GIntegral.hpp"
38 39 #include "GCaldb.hpp"
39 40 #include "GSource.hpp"
... ... @@ -1074,11 +1075,11 @@ void GCTAResponseIrf::load(const std::string& rspname)
1074 1075 * File not found.
1075 1076 *
1076 1077 * Loads the effective area from a response file. If the file is a FITS file
1077   - * the method first checks whether it contains an "EFFECTIVE AREA" extension.
1078   - * If such an extension is found, a GCTAAeff2D effective area will be
1079   - * allocated. Otherwise, the method checks whether the FITS file contains
1080   - * a "SPECRESP" extension, and if this is the case, the method will allocate
1081   - * a GCTAAeffArf effective area object. In both cases, the file will
  1078 + * the method will try decode the extension name from the filename.
  1079 + * If no extension if given, "EFFECTIVE AREA" will be used as default.
  1080 + * If the FITS table contains a column called "SPECRESP the method
  1081 + * will allocate a GCTAAeffArf effective area object. Otherwise, a GCTAAeff2D
  1082 + * effective area will be allocated. In both cases, the file will
1082 1083 * extract the optional "LO_THRES" and "HI_THRES" save energy thresholds
1083 1084 * from the FITS file header. If the file is not a FITS file, it will be
1084 1085 * interpreted as a GCTAAeffPerfTable performance table.
... ... @@ -1089,66 +1090,54 @@ void GCTAResponseIrf::load_aeff(const std::string& filename)
1089 1090 if (m_aeff != NULL) delete m_aeff;
1090 1091 m_aeff = NULL;
1091 1092  
  1093 + // Initialise filename
  1094 + GFilename fname(filename);
  1095 +
1092 1096 // Check for existence of file
1093   - if (!gammalib::file_exists_gzip(filename)) {
  1097 + if (!gammalib::file_exists_gzip(fname.filename())) {
1094 1098 std::string msg = "File \""+filename+"\" not found. Please specify "
1095 1099 "a valid effective area response file.";
1096 1100 throw GException::file_error(G_LOAD_AEFF, msg);
1097 1101 }
1098 1102  
1099   - // If file is a FITS file ...
1100 1103 if (gammalib::is_fits(filename)) {
1101 1104  
1102 1105 // Open FITS file
1103   - GFits file(filename);
1104   -
1105   - // If file contains an "EFFECTIVE AREA" extension then load it
1106   - // as CTA response table
1107   - if (file.contains("EFFECTIVE AREA")) {
  1106 + GFits file(fname.filename());
1108 1107  
1109   - // Get HDU
1110   - const GFitsHDU* hdu = file.at("EFFECTIVE AREA");
1111   -
1112   - // Read save energy thresholds if available
1113   - if (hdu->has_card("LO_THRES")) {
1114   - m_lo_save_thres = hdu->real("LO_THRES");
1115   - }
1116   - if (hdu->has_card("HI_THRES")) {
1117   - m_hi_save_thres = hdu->real("HI_THRES");
1118   - }
  1108 + // Get extension name
  1109 + std::string extname = fname.extname("EFFECTIVE AREA");
1119 1110  
1120   - // Close file
1121   - file.close();
1122   -
1123   - // Allocate Aeff from file
1124   - m_aeff = new GCTAAeff2D(filename);
  1111 + // Get FITS table
  1112 + const GFitsTable& table = *file.table(extname);
1125 1113  
  1114 + // Read save energy thresholds if available
  1115 + if (table.has_card("LO_THRES")) {
  1116 + m_lo_save_thres = table.real("LO_THRES");
  1117 + }
  1118 + if (table.has_card("HI_THRES")) {
  1119 + m_hi_save_thres = table.real("HI_THRES");
1126 1120 }
1127 1121  
1128   - // ... else if file contains a "SPECRESP" extension then load it
1129   - // as ARF
1130   - else if (file.contains("SPECRESP")) {
  1122 + // Check for specific table column
  1123 + if (table.contains("SPECRESP")) {
1131 1124  
1132   - // Get HDU
1133   - const GFitsHDU* hdu = file.at("SPECRESP");
  1125 + // Close file
  1126 + file.close();
1134 1127  
1135   - // Read save energy thresholds if available
1136   - if (hdu->has_card("LO_THRES")) {
1137   - m_lo_save_thres = hdu->real("LO_THRES");
1138   - }
1139   - if (hdu->has_card("LO_THRES")) {
1140   - m_hi_save_thres = hdu->real("HI_THRES");
1141   - }
  1128 + // Allocate Aeff from file
  1129 + m_aeff = new GCTAAeffArf(filename);
1142 1130  
  1131 + } // endif: load as Arf
  1132 + else {
1143 1133 // Close file
1144 1134 file.close();
1145 1135  
1146 1136 // Allocate Aeff from file
1147   - m_aeff = new GCTAAeffArf(filename);
1148   -
1149   - }
  1137 + m_aeff = new GCTAAeff2D(filename);
  1138 + } // load as Aeff2D
1150 1139  
1151   - }
  1140 + } // endif: file was FITS file
1152 1141  
1153 1142 // ... otherwise handle ASCII performance tables
1154 1143 else {
... ... @@ -1156,7 +1145,7 @@ void GCTAResponseIrf::load_aeff(const std::string& filename)
1156 1145 // Allocate a performance table
1157 1146 m_aeff = new GCTAAeffPerfTable(filename);
1158 1147  
1159   - }
  1148 + } // endelse: load as ascii
1160 1149  
1161 1150 // Return
1162 1151 return;
... ... @@ -1172,13 +1161,12 @@ void GCTAResponseIrf::load_aeff(const std::string& filename)
1172 1161 * File not found.
1173 1162 *
1174 1163 * Loads the point spead function from a response file. If the file is a
1175   - * FITS file, the method first checks whether it contains a
1176   - * "POINT SPREAD FUNCTION" extension. If such an extension is found,
1177   - * either a GCTAPsfKing or a GCTAPsf2D point spread function will be
1178   - * allocated depending on the column names that are found in the extension.
1179   - * If the "POINT SPREAD FUNCTION" extension is not found, the FITS file will
1180   - * be interpreted as a GCTAPsfVector point spread function. If the file is
1181   - * not a FITS file, it will be interpreted as a GCTAPsfPerfTable performance
  1164 + * FITS file, the method will try decode the extension name from the filename.
  1165 + * If no extension is given, "POINT SPREAD FUNCTION" will be used as default.
  1166 + * Depending on the column names, either a GCTAPsfKing or a GCTAPsf2D
  1167 + * point spread function will be allocated. If the column names do not match either
  1168 + * format, the FITS file will be interpreted as a GCTAPsfVector point spread function.
  1169 + * If the file is not a FITS file, it will be interpreted as a GCTAPsfPerfTable performance
1182 1170 * table.
1183 1171 ***************************************************************************/
1184 1172 void GCTAResponseIrf::load_psf(const std::string& filename)
... ... @@ -1187,8 +1175,11 @@ void GCTAResponseIrf::load_psf(const std::string& filename)
1187 1175 if (m_psf != NULL) delete m_psf;
1188 1176 m_psf = NULL;
1189 1177  
  1178 + // Initialise filename
  1179 + GFilename fname(filename);
  1180 +
1190 1181 // Check for existence of file
1191   - if (!gammalib::file_exists_gzip(filename)) {
  1182 + if (!gammalib::file_exists_gzip(fname.filename())) {
1192 1183 std::string msg = "File \""+filename+"\" not found. Please specify "
1193 1184 "a valid point spread function response file.";
1194 1185 throw GException::file_error(G_LOAD_PSF, msg);
... ... @@ -1198,34 +1189,30 @@ void GCTAResponseIrf::load_psf(const std::string& filename)
1198 1189 if (gammalib::is_fits(filename)) {
1199 1190  
1200 1191 // Open FITS file
1201   - GFits file(filename);
1202   -
1203   - // If file contains a "POINT SPREAD FUNCTION" extension then load it
1204   - // as either a King profile PSF or a 2D PSF
1205   - if (file.contains("POINT SPREAD FUNCTION")) {
1206   - const GFitsTable& table = *file.table("POINT SPREAD FUNCTION");
1207   - if (table.contains("GAMMA") && table.contains("SIGMA")) {
1208   - file.close();
1209   - m_psf = new GCTAPsfKing(filename);
1210   - }
1211   - else if (table.contains("SCALE") && table.contains("SIGMA_1") &&
1212   - table.contains("AMPL_2") && table.contains("SIGMA_2") &&
1213   - table.contains("AMPL_3") && table.contains("SIGMA_3")) {
1214   - file.close();
1215   - m_psf = new GCTAPsf2D(filename);
1216   - }
1217   - else {
1218   - file.close();
1219   - }
1220   - }
  1192 + GFits file(fname.filename());
  1193 +
  1194 + // Get extension name
  1195 + std::string extname = fname.extname("POINT SPREAD FUNCTION");
  1196 +
  1197 + // Get FITS table
  1198 + const GFitsTable& table = *file.table(extname);
1221 1199  
1222   - // ... else load it has PSF vector
  1200 + if (table.contains("GAMMA") && table.contains("SIGMA")) {
  1201 + file.close();
  1202 + m_psf = new GCTAPsfKing(filename);
  1203 + }
  1204 + else if (table.contains("SCALE") && table.contains("SIGMA_1") &&
  1205 + table.contains("AMPL_2") && table.contains("SIGMA_2") &&
  1206 + table.contains("AMPL_3") && table.contains("SIGMA_3")) {
  1207 + file.close();
  1208 + m_psf = new GCTAPsf2D(filename);
  1209 + }
1223 1210 else {
1224 1211 file.close();
1225 1212 m_psf = new GCTAPsfVector(filename);
1226 1213 }
1227 1214  
1228   - }
  1215 + } // endif: file was FITS file
1229 1216  
1230 1217 // ... otherwise load file as a performance table
1231 1218 else {
... ... @@ -1247,12 +1234,11 @@ void GCTAResponseIrf::load_psf(const std::string& filename)
1247 1234 * @exception GException::file_error
1248 1235 * File not found.
1249 1236 *
1250   - * Loads the energy dispersion from a response file. If the file is a FITS
1251   - * file, the method first checks whether it contains a "ENERGY DISPERSION"
1252   - * extension. If such an extension is found, a GCTAEdisp2D energy dispersion
1253   - * is allocated on the basis of the information found in the file. If the
1254   - * "ENERGY DISPERSION" extension is not found, the FITS file will be
1255   - * interpreted as a GCTAEdispRmf energy dispersion. If the file is not a
  1237 + * Loads the energy dispersion from a response file. If the file is a
  1238 + * FITS file, the method will try decode the extension name from the filename.
  1239 + * If no extension is given, "ENERGY DISPERSION" will be used as default.
  1240 + * Depending on the column names, either a GCTAEdisp2D or a GCTARmf
  1241 + * is allocated on the basis of the information found in the file. If the file is not a
1256 1242 * FITS file, it will be interpreted as a GCTAEdispPerfTable performance
1257 1243 * table.
1258 1244 ***************************************************************************/
... ... @@ -1262,8 +1248,11 @@ void GCTAResponseIrf::load_edisp(const std::string& filename)
1262 1248 if (m_edisp != NULL) delete m_edisp;
1263 1249 m_edisp = NULL;
1264 1250  
  1251 + // Initialise filename
  1252 + GFilename fname(filename);
  1253 +
1265 1254 // Check for existence of file
1266   - if (!gammalib::file_exists_gzip(filename)) {
  1255 + if (!gammalib::file_exists_gzip(fname.filename())) {
1267 1256 std::string msg = "File \""+filename+"\" not found. Please specify "
1268 1257 "a valid energy dispersion response file.";
1269 1258 throw GException::file_error(G_LOAD_EDISP, msg);
... ... @@ -1273,11 +1262,15 @@ void GCTAResponseIrf::load_edisp(const std::string& filename)
1273 1262 if (gammalib::is_fits(filename)) {
1274 1263  
1275 1264 // Open FITS file
1276   - GFits file(filename);
  1265 + GFits file(fname.filename());
1277 1266  
1278   - // If file contains an "ENERGY DISPERSION" extension then load it
1279   - // as CTA response table
1280   - if (file.contains("ENERGY DISPERSION")) {
  1267 + // Get extension name
  1268 + std::string extname = fname.extname("ENERGY DISPERSION");
  1269 +
  1270 + // Get FITS table
  1271 + const GFitsTable& table = *file.table(extname);
  1272 +
  1273 + if (table.contains("MIGRA_LO") && table.contains("MIGRA_HI")) {
1281 1274 file.close();
1282 1275 m_edisp = new GCTAEdisp2D(filename);
1283 1276 }
... ... @@ -1285,10 +1278,10 @@ void GCTAResponseIrf::load_edisp(const std::string& filename)
1285 1278 // ... otherwise load it as RMF
1286 1279 else {
1287 1280 file.close();
1288   - m_edisp = new GCTAEdispRmf(filename);
  1281 + m_edisp = new GCTAEdispRmf(fname.filename());
1289 1282 }
1290 1283  
1291   - }
  1284 + } //endif: file was FITS file
1292 1285  
1293 1286 // ... otherwise load file as a performance table
1294 1287 else {
... ... @@ -1322,8 +1315,11 @@ void GCTAResponseIrf::load_background(const std::string& filename)
1322 1315 if (m_background != NULL) delete m_background;
1323 1316 m_background = NULL;
1324 1317  
  1318 + // Initialise filename
  1319 + GFilename fname(filename);
  1320 +
1325 1321 // Check for existence of file
1326   - if (!gammalib::file_exists_gzip(filename)) {
  1322 + if (!gammalib::file_exists_gzip(fname.filename())) {
1327 1323 std::string msg = "File \""+filename+"\" not found. Please specify "
1328 1324 "a valid background response file.";
1329 1325 throw GException::file_error(G_LOAD_BACKGROUND, msg);
... ...
inst/cta/test/data/irf_1dc.xml
1 1 <observation_list title="observation library">
2 2 <observation name="Crab" id="000001" instrument="CTA">
3 3 <parameter name="CountsCube" file="$(PACKAGE_SOURCE)/inst/cta/test/data/crab_cntmap.fits"/>
4   - <parameter name="EffectiveArea" file="$(PACKAGE_SOURCE)/inst/cta/test/caldb/dc1/arf.fits"/>
  4 + <parameter name="EffectiveArea" file="$(PACKAGE_SOURCE)/inst/cta/test/caldb/dc1/arf.fits[SPECRESP]"/>
5 5 <parameter name="PointSpreadFunction" file="$(PACKAGE_SOURCE)/inst/cta/test/caldb/dc1/psf.fits"/>
6   - <parameter name="EnergyDispersion" file="$(PACKAGE_SOURCE)/inst/cta/test/caldb/dc1/rmf.fits"/>
  6 + <parameter name="EnergyDispersion" file="$(PACKAGE_SOURCE)/inst/cta/test/caldb/dc1/rmf.fits[MATRIX]"/>
7 7 <parameter name="Background" file=""/>
8 8 </observation>
9 9 </observation_list>
... ...