Commit 69d60cfec4092d62a4453eea97b37593e4cf650e
1 parent
201ceb15
read model from XML file
Showing
3 changed files
with
45 additions
and
59 deletions
src/ctlike/ctlike.cpp
| @@ -26,54 +26,36 @@ | @@ -26,54 +26,36 @@ | ||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | /***********************************************************************//** | 28 | /***********************************************************************//** |
| 29 | - * @brief Setup Crab point source powerlaw model. | ||
| 30 | - ***************************************************************************/ | ||
| 31 | -GModels crab_plaw(void) | ||
| 32 | -{ | ||
| 33 | - // Setup GModels for optimizing | ||
| 34 | - GModelSpatialPtsrc point_source; | ||
| 35 | - GModelSpectralPlaw power_law; | ||
| 36 | - GModel crab; | ||
| 37 | - GModels models; | ||
| 38 | - try { | ||
| 39 | - GSkyDir dir; | ||
| 40 | - //dir.radec_deg(83.6331, +22.0145); | ||
| 41 | - dir.radec_deg(117.0, -33.0); // Adapt to source position in file | ||
| 42 | - point_source = GModelSpatialPtsrc(dir); | ||
| 43 | - power_law = GModelSpectralPlaw(1.0e-7, -2.1); | ||
| 44 | - power_law.par(0)->min(1.0e-12); | ||
| 45 | - crab = GModel(point_source, power_law); | ||
| 46 | - crab.name("Crab"); | ||
| 47 | - models.append(crab); | ||
| 48 | - } | ||
| 49 | - catch (std::exception &e) { | ||
| 50 | - std::cout << std::endl | ||
| 51 | - << "TEST ERROR: Unable setup GModels for optimizing." | ||
| 52 | - << std::endl; | ||
| 53 | - std::cout << e.what() << std::endl; | ||
| 54 | - throw; | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - // Return model | ||
| 58 | - return models; | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | - | ||
| 62 | -/***********************************************************************//** | ||
| 63 | * @brief Run maximum likelihood analysis | 29 | * @brief Run maximum likelihood analysis |
| 64 | ***************************************************************************/ | 30 | ***************************************************************************/ |
| 65 | int ctlike::run(void) | 31 | int ctlike::run(void) |
| 66 | { | 32 | { |
| 67 | // Initialise return code | 33 | // Initialise return code |
| 68 | int rc = 0; | 34 | int rc = 0; |
| 69 | - | 35 | + |
| 36 | + // Get standard parameters | ||
| 37 | + m_method = toupper(par("method")->value()); | ||
| 38 | + m_caldb = par("caldb")->value(); | ||
| 39 | + m_irf = par("irf")->value(); | ||
| 40 | + m_srcmdl = par("srcmdl")->value(); | ||
| 41 | + m_models = GModels(m_srcmdl); | ||
| 42 | + | ||
| 43 | + // Set fixed parameters | ||
| 44 | + m_max_iter = 1000; | ||
| 45 | + m_opt = new GOptimizerLM; | ||
| 46 | + | ||
| 47 | + // Set optimizer parameters | ||
| 48 | + ((GOptimizerLM*)m_opt)->max_iter(m_max_iter); | ||
| 49 | + | ||
| 70 | // Branch on method | 50 | // Branch on method |
| 71 | - std::string method = par("method")->value(); | ||
| 72 | - if (toupper(method) == "UNBINNED") | 51 | + if (m_method == "UNBINNED") |
| 73 | rc = unbinned(); | 52 | rc = unbinned(); |
| 74 | else | 53 | else |
| 75 | rc = binned(); | 54 | rc = binned(); |
| 76 | - | 55 | + |
| 56 | + // Free optimizer | ||
| 57 | + delete m_opt; | ||
| 58 | + | ||
| 77 | // Return | 59 | // Return |
| 78 | return rc; | 60 | return rc; |
| 79 | } | 61 | } |
| @@ -89,8 +71,6 @@ int ctlike::unbinned(void) | @@ -89,8 +71,6 @@ int ctlike::unbinned(void) | ||
| 89 | 71 | ||
| 90 | // Get parameters | 72 | // Get parameters |
| 91 | std::string evfile = par("evfile")->value(); | 73 | std::string evfile = par("evfile")->value(); |
| 92 | - std::string caldb = par("caldb")->value(); | ||
| 93 | - std::string irf = par("irf")->value(); | ||
| 94 | 74 | ||
| 95 | // Declare observations | 75 | // Declare observations |
| 96 | GObservations obs; | 76 | GObservations obs; |
| @@ -118,20 +98,17 @@ int ctlike::unbinned(void) | @@ -118,20 +98,17 @@ int ctlike::unbinned(void) | ||
| 118 | // Load data and response and set ROI, energy range and time range | 98 | // Load data and response and set ROI, energy range and time range |
| 119 | // for analysis | 99 | // for analysis |
| 120 | run.load_unbinned(evfile); | 100 | run.load_unbinned(evfile); |
| 121 | - run.response(irf,caldb); | 101 | + run.response(m_irf, m_caldb); |
| 122 | run.roi(roi); | 102 | run.roi(roi); |
| 123 | run.gti()->add(tstart, tstop); | 103 | run.gti()->add(tstart, tstop); |
| 124 | run.ebounds()->append(emin, emax); | 104 | run.ebounds()->append(emin, emax); |
| 125 | obs.append(run); | 105 | obs.append(run); |
| 126 | 106 | ||
| 127 | - // Setup GModels for optimizing | ||
| 128 | - GModels models = crab_plaw(); | ||
| 129 | - obs.models(models); | 107 | + // Setup models for optimizing |
| 108 | + obs.models(m_models); | ||
| 130 | 109 | ||
| 131 | // Perform LM optimization | 110 | // Perform LM optimization |
| 132 | - GOptimizerLM opt; | ||
| 133 | - opt.max_iter(1000); | ||
| 134 | - obs.optimize(opt); | 111 | + obs.optimize(*m_opt); |
| 135 | std::cout << obs << std::endl; | 112 | std::cout << obs << std::endl; |
| 136 | 113 | ||
| 137 | // Return | 114 | // Return |
| @@ -149,8 +126,6 @@ int ctlike::binned(void) | @@ -149,8 +126,6 @@ int ctlike::binned(void) | ||
| 149 | 126 | ||
| 150 | // Get parameters | 127 | // Get parameters |
| 151 | std::string cntmap = par("cntmap")->value(); | 128 | std::string cntmap = par("cntmap")->value(); |
| 152 | - std::string caldb = par("caldb")->value(); | ||
| 153 | - std::string irf = par("irf")->value(); | ||
| 154 | 129 | ||
| 155 | // Declare observations | 130 | // Declare observations |
| 156 | GObservations obs; | 131 | GObservations obs; |
| @@ -158,17 +133,14 @@ int ctlike::binned(void) | @@ -158,17 +133,14 @@ int ctlike::binned(void) | ||
| 158 | 133 | ||
| 159 | // Load binned CTA observation | 134 | // Load binned CTA observation |
| 160 | run.load_binned(cntmap); | 135 | run.load_binned(cntmap); |
| 161 | - run.response(irf,caldb); | 136 | + run.response(m_irf, m_caldb); |
| 162 | obs.append(run); | 137 | obs.append(run); |
| 163 | 138 | ||
| 164 | - // Setup GModels for optimizing | ||
| 165 | - GModels models = crab_plaw(); | ||
| 166 | - obs.models(models); | 139 | + // Setup models for optimizing |
| 140 | + obs.models(m_models); | ||
| 167 | 141 | ||
| 168 | // Perform LM optimization | 142 | // Perform LM optimization |
| 169 | - GOptimizerLM opt; | ||
| 170 | - opt.max_iter(1000); | ||
| 171 | - obs.optimize(opt); | 143 | + obs.optimize(*m_opt); |
| 172 | std::cout << obs << std::endl; | 144 | std::cout << obs << std::endl; |
| 173 | 145 | ||
| 174 | // Return | 146 | // Return |
src/ctlike/ctlike.hpp
| @@ -20,6 +20,7 @@ | @@ -20,6 +20,7 @@ | ||
| 20 | #define CTLIKE_HPP | 20 | #define CTLIKE_HPP |
| 21 | 21 | ||
| 22 | /* __ Includes ___________________________________________________________ */ | 22 | /* __ Includes ___________________________________________________________ */ |
| 23 | +#include <string> | ||
| 23 | #include "GammaLib.hpp" | 24 | #include "GammaLib.hpp" |
| 24 | #include "GCTALib.hpp" | 25 | #include "GCTALib.hpp" |
| 25 | 26 | ||
| @@ -45,6 +46,18 @@ public: | @@ -45,6 +46,18 @@ public: | ||
| 45 | int run(void); | 46 | int run(void); |
| 46 | int unbinned(void); | 47 | int unbinned(void); |
| 47 | int binned(void); | 48 | int binned(void); |
| 49 | + | ||
| 50 | +protected: | ||
| 51 | + // User parameters | ||
| 52 | + std::string m_method; //!< Likelihood fitting method (binned/unbinned) | ||
| 53 | + std::string m_caldb; //!< Calibration database | ||
| 54 | + std::string m_irf; //!< Instrument response functions | ||
| 55 | + std::string m_srcmdl; //!< Source model XML file | ||
| 56 | + GModels m_models; //!< Source models | ||
| 57 | + | ||
| 58 | + // Fixed parameters | ||
| 59 | + int m_max_iter; //!< Maximum number of iterations | ||
| 60 | + GOptimizer* m_opt; //!< Optimizer | ||
| 48 | }; | 61 | }; |
| 49 | 62 | ||
| 50 | #endif /* CTLIKE_HPP */ | 63 | #endif /* CTLIKE_HPP */ |
src/ctlike/ctlike.par
| @@ -19,17 +19,18 @@ | @@ -19,17 +19,18 @@ | ||
| 19 | # | 19 | # |
| 20 | # Unbinned analysis | 20 | # Unbinned analysis |
| 21 | #================== | 21 | #================== |
| 22 | -evfile, f, a, "../gammalib/inst/cta/test/data/run_00006028_eventlist_reco.fits.gz",,,"Event data file name" | 22 | +evfile, f, a, "test/data/run_00006028_eventlist_reco.fits.gz",,,"Event data file name" |
| 23 | # | 23 | # |
| 24 | # Binned analysis | 24 | # Binned analysis |
| 25 | #================== | 25 | #================== |
| 26 | -cntmap, f, a, "../gammalib/inst/cta/test/data/run_00006028_cntmap.fits.gz",,, "Counts map" | 26 | +cntmap, f, a, "test/data/run_00006028_cntmap.fits.gz",,, "Counts map" |
| 27 | # | 27 | # |
| 28 | # General parameters | 28 | # General parameters |
| 29 | #=================== | 29 | #=================== |
| 30 | method, s, a, "UNBINNED",,, "Maximum likelihood method" | 30 | method, s, a, "UNBINNED",,, "Maximum likelihood method" |
| 31 | -caldb, s, a, "../gammalib/inst/cta/test/irf",,, "Calibration database" | 31 | +caldb, s, a, "test/irf",,, "Calibration database" |
| 32 | irf, s, a, "kb_E_50h_v3",,, "Instrument response function" | 32 | irf, s, a, "kb_E_50h_v3",,, "Instrument response function" |
| 33 | +srcmdl, f, a, "test/data/source1.xml",,, "Source model" | ||
| 33 | # | 34 | # |
| 34 | # Standard parameters | 35 | # Standard parameters |
| 35 | #==================== | 36 | #==================== |