Commit 2ec91cda56897e05e6cb8c8c8a6bc1491340c41b

Authored by knodlseder
1 parent 69d60cfe

add model saving and extend class

src/ctlike/ctlike.cpp
@@ -25,6 +25,67 @@ @@ -25,6 +25,67 @@
25 #include "GTools.hpp" 25 #include "GTools.hpp"
26 26
27 27
  28 +/*==========================================================================
  29 + = =
  30 + = Constructors/destructors =
  31 + = =
  32 + ==========================================================================*/
  33 +
  34 +/***********************************************************************//**
  35 + * @brief Void constructor
  36 + ***************************************************************************/
  37 +ctlike::ctlike(void) : GApplication(CTLIKE_NAME, CTLIKE_VERSION)
  38 +{
  39 + // Initialise members
  40 + init_members();
  41 +
  42 + // Return
  43 + return;
  44 +}
  45 +
  46 +
  47 +/***********************************************************************//**
  48 + * @brief Command line constructor
  49 + *
  50 + * @param[in] argc Number of arguments in command line.
  51 + * @param[in] argv Array of command line arguments.
  52 + ***************************************************************************/
  53 +ctlike::ctlike(int argc, char *argv[]) :
  54 + GApplication(CTLIKE_NAME, CTLIKE_VERSION, argc, argv)
  55 +{
  56 + // Initialise members
  57 + init_members();
  58 +
  59 + // Return
  60 + return;
  61 +}
  62 +
  63 +
  64 +/***********************************************************************//**
  65 + * @brief Destructor
  66 + ***************************************************************************/
  67 +ctlike::~ctlike(void)
  68 +{
  69 + // Free members
  70 + free_members();
  71 +
  72 + // Return
  73 + return;
  74 +}
  75 +
  76 +
  77 +/*==========================================================================
  78 + = =
  79 + = Operators =
  80 + = =
  81 + ==========================================================================*/
  82 +
  83 +/*==========================================================================
  84 + = =
  85 + = Public methods =
  86 + = =
  87 + ==========================================================================*/
  88 +
28 /***********************************************************************//** 89 /***********************************************************************//**
29 * @brief Run maximum likelihood analysis 90 * @brief Run maximum likelihood analysis
30 ***************************************************************************/ 91 ***************************************************************************/
@@ -38,11 +99,11 @@ int ctlike::run(void) @@ -38,11 +99,11 @@ int ctlike::run(void)
38 m_caldb = par("caldb")->value(); 99 m_caldb = par("caldb")->value();
39 m_irf = par("irf")->value(); 100 m_irf = par("irf")->value();
40 m_srcmdl = par("srcmdl")->value(); 101 m_srcmdl = par("srcmdl")->value();
  102 + m_outmdl = par("outmdl")->value();
41 m_models = GModels(m_srcmdl); 103 m_models = GModels(m_srcmdl);
42 104
43 // Set fixed parameters 105 // Set fixed parameters
44 - m_max_iter = 1000;  
45 - m_opt = new GOptimizerLM; 106 + m_opt = new GOptimizerLM;
46 107
47 // Set optimizer parameters 108 // Set optimizer parameters
48 ((GOptimizerLM*)m_opt)->max_iter(m_max_iter); 109 ((GOptimizerLM*)m_opt)->max_iter(m_max_iter);
@@ -53,6 +114,13 @@ int ctlike::run(void) @@ -53,6 +114,13 @@ int ctlike::run(void)
53 else 114 else
54 rc = binned(); 115 rc = binned();
55 116
  117 + // Write result as XML model
  118 + if (toupper(m_outmdl) != "NONE")
  119 + m_models.save(m_outmdl);
  120 +
  121 + // Dump results
  122 + std::cout << m_models << std::endl;
  123 +
56 // Free optimizer 124 // Free optimizer
57 delete m_opt; 125 delete m_opt;
58 126
@@ -73,7 +141,6 @@ int ctlike::unbinned(void) @@ -73,7 +141,6 @@ int ctlike::unbinned(void)
73 std::string evfile = par("evfile")->value(); 141 std::string evfile = par("evfile")->value();
74 142
75 // Declare observations 143 // Declare observations
76 - GObservations obs;  
77 GCTAObservation run; 144 GCTAObservation run;
78 145
79 // Setup ROI covered by data 146 // Setup ROI covered by data
@@ -102,14 +169,16 @@ int ctlike::unbinned(void) @@ -102,14 +169,16 @@ int ctlike::unbinned(void)
102 run.roi(roi); 169 run.roi(roi);
103 run.gti()->add(tstart, tstop); 170 run.gti()->add(tstart, tstop);
104 run.ebounds()->append(emin, emax); 171 run.ebounds()->append(emin, emax);
105 - obs.append(run); 172 + m_obs.append(run);
106 173
107 // Setup models for optimizing 174 // Setup models for optimizing
108 - obs.models(m_models); 175 + m_obs.models(m_models);
109 176
110 // Perform LM optimization 177 // Perform LM optimization
111 - obs.optimize(*m_opt);  
112 - std::cout << obs << std::endl; 178 + m_obs.optimize(*m_opt);
  179 +
  180 + // Get models back
  181 + m_models = *(m_obs.models());
113 182
114 // Return 183 // Return
115 return rc; 184 return rc;
@@ -128,21 +197,62 @@ int ctlike::binned(void) @@ -128,21 +197,62 @@ int ctlike::binned(void)
128 std::string cntmap = par("cntmap")->value(); 197 std::string cntmap = par("cntmap")->value();
129 198
130 // Declare observations 199 // Declare observations
131 - GObservations obs;  
132 GCTAObservation run; 200 GCTAObservation run;
133 201
134 // Load binned CTA observation 202 // Load binned CTA observation
135 run.load_binned(cntmap); 203 run.load_binned(cntmap);
136 run.response(m_irf, m_caldb); 204 run.response(m_irf, m_caldb);
137 - obs.append(run); 205 + m_obs.append(run);
138 206
139 // Setup models for optimizing 207 // Setup models for optimizing
140 - obs.models(m_models); 208 + m_obs.models(m_models);
141 209
142 // Perform LM optimization 210 // Perform LM optimization
143 - obs.optimize(*m_opt);  
144 - std::cout << obs << std::endl; 211 + m_obs.optimize(*m_opt);
  212 +
  213 + // Get models back
  214 + m_models = *(m_obs.models());
145 215
146 // Return 216 // Return
147 return rc; 217 return rc;
148 } 218 }
  219 +
  220 +
  221 +/*==========================================================================
  222 + = =
  223 + = Private methods =
  224 + = =
  225 + ==========================================================================*/
  226 +
  227 +/***********************************************************************//**
  228 + * @brief Initialise class members
  229 + ***************************************************************************/
  230 +void ctlike::init_members(void)
  231 +{
  232 + // Initialise members
  233 + m_method.clear();
  234 + m_caldb.clear();
  235 + m_irf.clear();
  236 + m_srcmdl.clear();
  237 + m_outmdl.clear();
  238 + m_models.clear();
  239 + //m_obs.clear(); //!< NOT YET IMPLEMENTED
  240 + m_max_iter = 1000;
  241 + m_opt = NULL;
  242 +
  243 + // Return
  244 + return;
  245 +}
  246 +
  247 +
  248 +/***********************************************************************//**
  249 + * @brief Delete class members
  250 + ***************************************************************************/
  251 +void ctlike::free_members(void)
  252 +{
  253 + // Free members
  254 + delete m_opt;
  255 +
  256 + // Return
  257 + return;
  258 +}
src/ctlike/ctlike.hpp
@@ -37,10 +37,9 @@ @@ -37,10 +37,9 @@
37 class ctlike : public GApplication { 37 class ctlike : public GApplication {
38 public: 38 public:
39 // Constructors and destructors 39 // Constructors and destructors
40 - ctlike(void) :  
41 - GApplication(CTLIKE_NAME, CTLIKE_VERSION) { return; }  
42 - ctlike(int argc, char *argv[]) :  
43 - GApplication(CTLIKE_NAME, CTLIKE_VERSION, argc, argv) { return; } 40 + ctlike(void);
  41 + ctlike(int argc, char *argv[]);
  42 + ~ctlike(void);
44 43
45 // Methods 44 // Methods
46 int run(void); 45 int run(void);
@@ -48,16 +47,22 @@ public: @@ -48,16 +47,22 @@ public:
48 int binned(void); 47 int binned(void);
49 48
50 protected: 49 protected:
  50 + // Protected methods
  51 + void init_members(void);
  52 + void free_members(void);
  53 +
51 // User parameters 54 // 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 55 + std::string m_method; //!< Likelihood fitting method (binned/unbinned)
  56 + std::string m_caldb; //!< Calibration database
  57 + std::string m_irf; //!< Instrument response functions
  58 + std::string m_srcmdl; //!< Source model XML file
  59 + std::string m_outmdl; //!< Source model output XML file
  60 + GModels m_models; //!< Source models
  61 + GObservations m_obs; //!< Observations
57 62
58 // Fixed parameters 63 // Fixed parameters
59 - int m_max_iter; //!< Maximum number of iterations  
60 - GOptimizer* m_opt; //!< Optimizer 64 + int m_max_iter; //!< Maximum number of iterations
  65 + GOptimizer* m_opt; //!< Optimizer
61 }; 66 };
62 67
63 #endif /* CTLIKE_HPP */ 68 #endif /* CTLIKE_HPP */