Commit 69d60cfec4092d62a4453eea97b37593e4cf650e

Authored by knodlseder
1 parent 201ceb15

read model from XML file

src/ctlike/ctlike.cpp
... ... @@ -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 29 * @brief Run maximum likelihood analysis
64 30 ***************************************************************************/
65 31 int ctlike::run(void)
66 32 {
67 33 // Initialise return code
68 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 50 // Branch on method
71   - std::string method = par("method")->value();
72   - if (toupper(method) == "UNBINNED")
  51 + if (m_method == "UNBINNED")
73 52 rc = unbinned();
74 53 else
75 54 rc = binned();
76   -
  55 +
  56 + // Free optimizer
  57 + delete m_opt;
  58 +
77 59 // Return
78 60 return rc;
79 61 }
... ... @@ -89,8 +71,6 @@ int ctlike::unbinned(void)
89 71  
90 72 // Get parameters
91 73 std::string evfile = par("evfile")->value();
92   - std::string caldb = par("caldb")->value();
93   - std::string irf = par("irf")->value();
94 74  
95 75 // Declare observations
96 76 GObservations obs;
... ... @@ -118,20 +98,17 @@ int ctlike::unbinned(void)
118 98 // Load data and response and set ROI, energy range and time range
119 99 // for analysis
120 100 run.load_unbinned(evfile);
121   - run.response(irf,caldb);
  101 + run.response(m_irf, m_caldb);
122 102 run.roi(roi);
123 103 run.gti()->add(tstart, tstop);
124 104 run.ebounds()->append(emin, emax);
125 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 110 // Perform LM optimization
132   - GOptimizerLM opt;
133   - opt.max_iter(1000);
134   - obs.optimize(opt);
  111 + obs.optimize(*m_opt);
135 112 std::cout << obs << std::endl;
136 113  
137 114 // Return
... ... @@ -149,8 +126,6 @@ int ctlike::binned(void)
149 126  
150 127 // Get parameters
151 128 std::string cntmap = par("cntmap")->value();
152   - std::string caldb = par("caldb")->value();
153   - std::string irf = par("irf")->value();
154 129  
155 130 // Declare observations
156 131 GObservations obs;
... ... @@ -158,17 +133,14 @@ int ctlike::binned(void)
158 133  
159 134 // Load binned CTA observation
160 135 run.load_binned(cntmap);
161   - run.response(irf,caldb);
  136 + run.response(m_irf, m_caldb);
162 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 142 // Perform LM optimization
169   - GOptimizerLM opt;
170   - opt.max_iter(1000);
171   - obs.optimize(opt);
  143 + obs.optimize(*m_opt);
172 144 std::cout << obs << std::endl;
173 145  
174 146 // Return
... ...
src/ctlike/ctlike.hpp
... ... @@ -20,6 +20,7 @@
20 20 #define CTLIKE_HPP
21 21  
22 22 /* __ Includes ___________________________________________________________ */
  23 +#include <string>
23 24 #include "GammaLib.hpp"
24 25 #include "GCTALib.hpp"
25 26  
... ... @@ -45,6 +46,18 @@ public:
45 46 int run(void);
46 47 int unbinned(void);
47 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 63 #endif /* CTLIKE_HPP */
... ...
src/ctlike/ctlike.par
... ... @@ -19,17 +19,18 @@
19 19 #
20 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 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 28 # General parameters
29 29 #===================
30 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 32 irf, s, a, "kb_E_50h_v3",,, "Instrument response function"
  33 +srcmdl, f, a, "test/data/source1.xml",,, "Source model"
33 34 #
34 35 # Standard parameters
35 36 #====================
... ...