/***********************************************************************//** * @brief Test unbinned optimizer ***************************************************************************/ void TestGCTAOptimize::test_unbinned_optimizer(void) { // Declare observations GObservations obs; GCTAObservation run; // Load unbinned CTA observation test_try("Load unbinned CTA observation"); try { run.load_unbinned(cta_events); run.response(cta_irf,cta_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(cta_model_xml); // Perform LM optimization double fit_results[] = {83.6331, 0, 22.0145, 0, 5.656246512e-16, 1.91458426e-17, -2.484100472, -0.02573396361, 300000, 0, 1, 0, 2.993705325, 0.03572658413, 6.490832107e-05, 1.749021094e-06, -1.833584022, -0.01512223495, 1000000, 0, 1, 0}; test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(100); obs.optimize(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar& par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.real_value(), fit_results[j++], 5.0e-5, msg); test_value(par.real_error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test binned optimizer ***************************************************************************/ void TestGCTAOptimize::test_binned_optimizer(void) { // Declare observations GObservations obs; GCTAObservation run; // Load binned CTA observation test_try("Load binned CTA observation"); try { run.load_binned(cta_cntmap); run.response(cta_irf,cta_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(cta_model_xml); // Perform LM optimization double fit_results[] = {83.6331, 0, 22.0145, 0, 5.616410411e-16, 1.904730785e-17, -2.481781246, -0.02580905077, 300000, 0, 1, 0, 2.933677595, 0.06639644824, 6.550723074e-05, 1.945714239e-06, -1.833781187, -0.0161464076, 1000000, 0, 1, 0}; test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(100); obs.optimize(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar& par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.real_value(), fit_results[j++], 5.0e-5, msg); test_value(par.real_error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test binned optimizer * * @param[in] datadir Directory of test data. * @param[in] irf Instrument response function. * @param[in] fit_results Expected fit result. * * Verifies the ability optimize binned Fermi/LAT data. ***************************************************************************/ void TestGLATOptimize::test_one_binned_optimizer(const std::string& datadir, const std::string& irf, const double* fit_results) { // Set filenames std::string lat_srcmap = datadir+"/srcmap.fits"; std::string lat_expmap = datadir+"/binned_expmap.fits"; std::string lat_ltcube = datadir+"/ltcube.fits"; std::string lat_model_xml = datadir+"/source_model.xml"; // Setup GObservations for optimizing GObservations obs; GLATObservation run; test_try("Setup for optimization"); try { run.load_binned(lat_srcmap, lat_expmap, lat_ltcube); run.response(irf, lat_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(lat_model_xml); // Setup LM optimizer test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(1000); obs.optimize(opt); obs.errors(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { const GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.value(), fit_results[j++], 5.0e-5, msg); test_value(par.error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test observations optimizer. * * @param[in] mode Testing mode. * * This method supports two testing modes: 0 = unbinned and 1 = binned. ***************************************************************************/ void TestOpenMP::test_observations_optimizer(const int& mode) { // Create Test Model GTestModelData model; // Create Models conteners GModels models; models.append(model); // Time iterval GTime tmin(0.0); GTime tmax(1800.0); // Rate : events/sec double rate = RATE; // Create observations GObservations obs; // Add some observation for (int i = 0; i < 6; ++i) { // Random Generator GRan ran; ran.seed(i); // Allocate events pointer GEvents *events; // Create either a event list or an event cube if (mode == UN_BINNED) { events = model.generateList(rate,tmin,tmax,ran); } else { events = model.generateCube(rate,tmin,tmax,ran); } // Create an observation GTestObservation ob; ob.id(gammalib::str(i)); // Add events to the observation ob.events(*events); ob.ontime(tmax.secs()-tmin.secs()); obs.append(ob); // Delete events pointer delete events; } // Add the model to the observation obs.models(models); // Create a GLog for show the interations of optimizer. GLog log; // Create an optimizer. GOptimizerLM opt(log); opt.max_stalls(50); // Optimize obs.optimize(opt); // Get the result GModelPar result = (*(obs.models()[0]))[0]; // Check if converged test_assert(opt.status()==0, "Check if converged", "Optimizer did not converge"); // Check if value is correct test_value(result.factor_value(),RATE,result.factor_error()*3); // Return return; }
/***********************************************************************//** * @brief Test unbinned observation handling ***************************************************************************/ void TestGCTAObservation::test_unbinned_obs(void) { // Set filenames const std::string file1 = "test_cta_obs_unbinned.xml"; // Declare observations GObservations obs; GCTAObservation run; // Load unbinned CTA observation test_try("Load unbinned CTA observation"); try { run.load_unbinned(cta_events); run.response(cta_irf,cta_caldb); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Add observation (twice) to data test_try("Load unbinned CTA observation"); try { obs.append(run); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Loop over all events using iterators int num = 0; for (GObservations::iterator event = obs.begin(); event != obs.end(); ++event) { num++; } test_value(num, 8794, 1.0e-20, "Test observation iterator"); // Loop over all events using iterator num = 0; GCTAEventList *ptr = static_cast<GCTAEventList*>(const_cast<GEvents*>(run.events())); for (GCTAEventList::iterator event = ptr->begin(); event != ptr->end(); ++event) { num++; } test_value(num, 4397, 1.0e-20, "Test event iterator"); // Test XML loading test_try("Test XML loading"); try { obs = GObservations(cta_unbin_xml); obs.save(file1); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test binned observation handling for a specific dataset * * @param[in] datadir Directory of test data. * @param[in] irf Instrument response function. * * Verifies the ability to handle binned Fermi/LAT data. ***************************************************************************/ void TestGLATObservation::test_one_binned_obs(const std::string& datadir, const std::string& irf) { // Set filenames std::string lat_cntmap = datadir+"/cntmap.fits"; std::string lat_srcmap = datadir+"/srcmap.fits"; std::string lat_expmap = datadir+"/binned_expmap.fits"; std::string lat_ltcube = datadir+"/ltcube.fits"; std::string lat_bin_xml = datadir+"/obs_binned.xml"; std::string file1 = "test_lat_obs_binned.xml"; // Declare observations GObservations obs; GLATObservation run; // Determine number of bins and events in counts map GFits cntmap(lat_cntmap); GFitsImage* image = cntmap.image(0); double nevents = 0.0; int nsize = image->size(); for (int i = 0; i < nsize; ++i) { nevents += image->pixel(i); } cntmap.close(); // Try loading event list GLATEventCube cube(lat_cntmap); test_value(cube.number(), nevents, "Test number of events in cube."); // Load LAT binned observation from counts map test_try("Load LAT binned observation"); try { run.load_binned(lat_cntmap, "", ""); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Reload LAT binned observation from source map test_try("Reload LAT binned observation"); try { run.load_binned(lat_srcmap, "", ""); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Add observation (twice) to data test_try("Append observation twice"); try { run.id("0001"); obs.append(run); run.id("0002"); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Loop over all events using iterator const GEvents* events = run.events(); int num = 0; int sum = 0; for (int i = 0; i < events->size(); ++i) { num++; sum += (int)((*events)[i]->counts()); } test_value(sum, nevents, 1.0e-20, "Test event iterator (counts)"); test_value(num, nsize, 1.0e-20, "Test event iterator (bins)"); // Test mean PSF test_try("Test mean PSF"); try { run.load_binned(lat_srcmap, lat_expmap, lat_ltcube); run.response(irf, lat_caldb); GSkyDir dir; GLATMeanPsf psf(dir, run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test XML loading test_try("Test XML loading"); try { setenv("CALDB", lat_caldb.c_str(), 1); obs = GObservations(lat_bin_xml); obs.save(file1); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test unbinned observation handling for a specific dataset * * @param[in] datadir Directory of test data. * * Verifies the ability to handle unbinned Fermi/LAT data. ***************************************************************************/ void TestGLATObservation::test_one_unbinned_obs(const std::string& datadir) { // Set filenames std::string lat_ft1 = datadir+"/ft1.fits"; std::string lat_ft2 = datadir+"/ft2.fits"; std::string lat_unbin_xml = datadir+"/obs_unbinned.xml"; std::string file1 = "test_lat_obs_unbinned.xml"; // Declare observations GObservations obs; GLATObservation run; // Determine number of events in FT1 file GFits ft1(lat_ft1); int nevents = ft1.table("EVENTS")->nrows(); ft1.close(); // Try loading event list GLATEventList list(lat_ft1); test_value(list.number(), nevents, "Test number of events in list."); // Load unbinned LAT observation test_try("Load unbinned LAT observation"); try { run.load_unbinned(lat_ft1, lat_ft2, ""); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Add observation (twice) to data test_try("Append observation twice"); try { run.id("0001"); obs.append(run); run.id("0002"); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Loop over all events const GEvents *ptr = run.events(); int num = 0; for (int i = 0; i < ptr->size(); ++i) { num++; } test_value(num, nevents, 1.0e-20, "Test event iterator"); // Test XML loading test_try("Test XML loading"); try { setenv("CALDB", lat_caldb.c_str(), 1); obs = GObservations(lat_unbin_xml); obs.save(file1); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Create output observation container. * * Creates an output observation container that combines all input CTA * observation into a single cube-style observation. All non CTA observations * present in the observation container are kept. The method furthermore * conserves any response information in case that a single CTA observation * is provided. This supports the original binned analysis. ***************************************************************************/ void ctbin::obs_cube(void) { // If we have only a single CTA observation in the container, then // keep that observation and just attach the event cube to it. Reset // the filename, otherwise we still will have the old event filename // in the log file. if (m_obs.size() == 1) { // Attach event cube to CTA observation GCTAObservation* obs = dynamic_cast<GCTAObservation*>(m_obs[0]); if (obs != NULL) { obs->events(this->cube()); obs->eventfile(""); } } // ... otherwise put a single CTA observation in container else { // Allocate observation container GObservations container; // Allocate CTA observation. GCTAObservation obs; // Attach event cube to CTA observation obs.events(this->cube()); // Set map centre as pointing GSkyPixel pixel(0.5*double(m_cube.nx()), 0.5*double(m_cube.ny())); GSkyDir centre = m_cube.pix2dir(pixel); GCTAPointing pointing(centre); // Compute deadtime correction double deadc = (m_ontime > 0.0) ? m_livetime / m_ontime : 0.0; // Set CTA observation attributes obs.pointing(pointing); obs.obs_id(0); obs.ra_obj(centre.ra_deg()); //!< Dummy obs.dec_obj(centre.dec_deg()); //!< Dummy obs.ontime(m_ontime); obs.livetime(m_livetime); obs.deadc(deadc); // Set models in observation container container.models(m_obs.models()); // Append CTA observation container.append(obs); // Copy over all remaining non-CTA observations for (int i = 0; i < m_obs.size(); ++i) { GCTAObservation* obs = dynamic_cast<GCTAObservation*>(m_obs[i]); if (obs == NULL) { container.append(*m_obs[i]); } } // Set observation container m_obs = container; } // endelse: there was not a single CTA observation // Return return; }