Commit 1553215eb641b34352dc0dd2490cc3301717ef38

Authored by knodlseder
1 parent 312e28db

update to support standardized Python interface

pyext/ctbin.i
... ... @@ -12,7 +12,7 @@
12 12 ***************************************************************************/
13 13 /**
14 14 * @file ctbin.i
15   - * @brief CTA data binning tool SWIG definition
  15 + * @brief CTA data binning tool Python interface definition
16 16 * @author J. Knodlseder
17 17 */
18 18 %{
... ... @@ -25,19 +25,24 @@
25 25 /***********************************************************************//**
26 26 * @class ctbin
27 27 *
28   - * @brief CTA data binning tool SWIG interface defintion.
  28 + * @brief CTA data binning tool Python interface defintion
29 29 ***************************************************************************/
30 30 class ctbin : public GApplication {
31 31 public:
32 32 // Constructors and destructors
33 33 ctbin(void);
  34 + explicit ctbin(GObservations obs);
34 35 ctbin(int argc, char *argv[]);
35   - ~ctbin(void);
  36 + virtual ~ctbin(void);
36 37  
37 38 // Methods
38   - void run(void);
39   - void get_parameters(void);
40   - void bin(void);
  39 + void clear(void);
  40 + void execute(void);
  41 + void run(void);
  42 + void save(void);
  43 + GObservations& obs(void) { return m_obs; }
  44 + void get_parameters(void);
  45 + void bin_events(GCTAObservation* obs);
41 46 };
42 47  
43 48  
... ...
src/ctbin/ctbin.cpp
... ... @@ -24,6 +24,9 @@
24 24 #include "ctbin.hpp"
25 25 #include "GTools.hpp"
26 26  
  27 +/* __ Method name definitions ____________________________________________ */
  28 +#define G_BIN_EVENTS "ctbin::bin_events(GCTAObservation*)"
  29 +
27 30 /* __ Debug definitions __________________________________________________ */
28 31  
29 32 /* __ Coding definitions _________________________________________________ */
... ... @@ -52,6 +55,29 @@ ctbin::ctbin(void) : GApplication(CTBIN_NAME, CTBIN_VERSION)
52 55  
53 56  
54 57 /***********************************************************************//**
  58 + * @brief Observations constructor
  59 + *
  60 + * This method creates an instance of the class by copying an existing
  61 + * observations container.
  62 + ***************************************************************************/
  63 +ctbin::ctbin(GObservations obs) : GApplication(CTBIN_NAME, CTBIN_VERSION)
  64 +{
  65 + // Initialise members
  66 + init_members();
  67 +
  68 + // Set observations
  69 + m_obs = obs;
  70 +
  71 + // Write header into logger
  72 + log_header();
  73 +
  74 + // Return
  75 + return;
  76 +}
  77 +
  78 +
  79 +
  80 +/***********************************************************************//**
55 81 * @brief Command line constructor
56 82 *
57 83 * @param[in] argc Number of arguments in command line.
... ... @@ -97,7 +123,48 @@ ctbin::~ctbin(void)
97 123 ==========================================================================*/
98 124  
99 125 /***********************************************************************//**
100   - * @brief Run gtbin application
  126 + * @brief Clear instance
  127 + ***************************************************************************/
  128 +void ctbin::clear(void)
  129 +{
  130 + // Free members
  131 + free_members();
  132 + this->GApplication::free_members();
  133 +
  134 + // Initialise members
  135 + this->GApplication::init_members();
  136 + init_members();
  137 +
  138 + // Return
  139 + return;
  140 +}
  141 +
  142 +
  143 +/***********************************************************************//**
  144 + * @brief Execute application
  145 + *
  146 + * This method bins the events data into a counts maps and saves the counts
  147 + * map into a FITS file.
  148 + ***************************************************************************/
  149 +void ctbin::execute(void)
  150 +{
  151 + // Read ahead output filename so that it gets dumped correctly in the
  152 + // parameters log
  153 + m_outfile = par("outfile")->value();
  154 +
  155 + // Bin the event data
  156 + run();
  157 +
  158 + // Save the counts map into FITS file
  159 + save();
  160 +
  161 + // Return
  162 + return;
  163 +}
  164 +
  165 +
  166 +/***********************************************************************//**
  167 + * @brief Bin the event data
101 168 ***************************************************************************/
102 169 void ctbin::run(void)
103 170 {
... ... @@ -114,12 +181,95 @@ void ctbin::run(void)
114 181 log << std::endl;
115 182 }
116 183  
117   - // Bin data
118   - bin();
  184 + // Write observation(s) into logger
  185 + if (logTerse()) {
  186 + log << std::endl;
  187 + if (m_obs.size() > 1)
  188 + log.header1("Observations");
  189 + else
  190 + log.header1("Observation");
  191 + log << m_obs << std::endl;
  192 + }
119 193  
120   - // Write separator into logger
121   - if (logTerse())
  194 + // Write header
  195 + if (logTerse()) {
122 196 log << std::endl;
  197 + if (m_obs.size() > 1)
  198 + log.header1("Bin observations");
  199 + else
  200 + log.header1("Bin observation");
  201 + }
  202 +
  203 + // Loop over all observation in the container
  204 + for (int i = 0; i < m_obs.size(); ++i) {
  205 +
  206 + // Get CTA observation
  207 + GCTAObservation* obs = dynamic_cast<GCTAObservation*>(m_obs(i));
  208 +
  209 + // Continue only if observation is a CTA observation
  210 + if (obs != NULL) {
  211 +
  212 + // Write header for observation
  213 + if (logTerse()) {
  214 + if (obs->name().length() > 1)
  215 + log.header3("Observation "+obs->name());
  216 + else
  217 + log.header3("Observation");
  218 + }
  219 +
  220 + // Bin events into counts map
  221 + bin_events(obs);
  222 +
  223 + } // endif: CTA observation found
  224 +
  225 + } // endfor: looped over observations
  226 +
  227 + // Return
  228 + return;
  229 +}
  230 +
  231 +
  232 +/***********************************************************************//**
  233 + * @brief Save observation
  234 + *
  235 + * This method saves the counts map(s) into (a) FITS file(s).
  236 + ***************************************************************************/
  237 +void ctbin::save(void)
  238 +{
  239 + // Write header
  240 + if (logTerse()) {
  241 + log << std::endl;
  242 + if (m_obs.size() > 1)
  243 + log.header1("Save observations");
  244 + else
  245 + log.header1("Save observation");
  246 + }
  247 +
  248 + // Get output filename
  249 + m_outfile = par("outfile")->value();
  250 +
  251 + // Loop over all observations in the container
  252 + int file_num = 0;
  253 + for (int i = 0; i < m_obs.size(); ++i) {
  254 +
  255 + // Get CTA observation
  256 + GCTAObservation* obs = dynamic_cast<GCTAObservation*>(m_obs(i));
  257 +
  258 + // Save only if observation is a CTA observation
  259 + if (obs != NULL) {
  260 +
  261 + // Set filename. If more than one file will be created an
  262 + // index "_xxx" will be appended.
  263 + std::string filename = m_outfile;
  264 + if (file_num > 0)
  265 + filename += "_"+str(file_num);
  266 +
  267 + // Save file
  268 + obs->save(filename, clobber());
  269 +
  270 + } // endif: observation was a CTA observation
  271 +
  272 + } // endfor: looped over files
123 273  
124 274 // Return
125 275 return;
... ... @@ -130,13 +280,30 @@ void ctbin::run(void)
130 280 * @brief Get application parameters
131 281 *
132 282 * Get all task parameters from parameter file or (if required) by querying
133   - * the user.
  283 + * the user. Most parameters are only required if no observation exists so
  284 + * far in the observation container. In this case, a single CTA observation
  285 + * will be added to the container, using the definition provided in the
  286 + * parameter file.
134 287 ***************************************************************************/
135 288 void ctbin::get_parameters(void)
136 289 {
137   - // Get parameters
138   - m_evfile = par("evfile")->value();
139   - m_outfile = par("outfile")->value();
  290 + // If there are no observations in container then add a single CTA
  291 + // observation using the parameters from the parameter file
  292 + if (m_obs.size() == 0) {
  293 +
  294 + // Get name of CTA events file
  295 + m_evfile = par("evfile")->value();
  296 +
  297 + // Load unbinned CTA observation
  298 + GCTAObservation obs;
  299 + obs.load_unbinned(m_evfile);
  300 +
  301 + // Append CTA observation to container
  302 + m_obs.append(obs);
  303 +
  304 + } // endif: there was no observation in the container
  305 +
  306 + // Get remaining parameters
140 307 m_emin = par("emin")->real();
141 308 m_emax = par("emax")->real();
142 309 m_enumbins = par("enumbins")->integer();
... ... @@ -154,98 +321,104 @@ void ctbin::get_parameters(void)
154 321  
155 322  
156 323 /***********************************************************************//**
157   - * @brief Bin the data
  324 + * @brief Bin events into a counts map
  325 + *
  326 + * @param[in] obs CTA observation.
  327 + *
  328 + * @exception GException::no_list
  329 + * No event list found in observation.
  330 + *
  331 + * This method bins the events found in a CTA events list into a counts map
  332 + * and replaces the event list by the counts map in the observation. The
  333 + * energy boundaries of the counts map are also stored in the observation's
  334 + * energy boundary member.
158 335 ***************************************************************************/
159   -void ctbin::bin(void)
  336 +void ctbin::bin_events(GCTAObservation* obs)
160 337 {
161   - // Load events as unbinned data
162   - GCTAObservation obs;
163   - obs.load_unbinned(m_evfile);
164   -
165   - // Setup energy range covered by data
166   - GEnergy emin;
167   - GEnergy emax;
168   - GEbounds ebds;
169   - emin.TeV(m_emin);
170   - emax.TeV(m_emax);
171   - ebds.setlog(emin, emax, m_enumbins);
172   - obs.ebounds(ebds);
173   -
174   - // Log observation
175   - if (logTerse()) {
176   - log << std::endl;
177   - log.header1("Observation");
178   - log << obs << std::endl;
179   - }
180   -
181   - // Create skymap
182   - GSkymap map = GSkymap(m_proj, m_coordsys,
183   - m_xref, m_yref, m_binsz, m_binsz,
184   - m_nxpix, m_nypix, m_enumbins);
185   -
186   - // Initialise binning statistics
187   - int num_outside_map = 0;
188   - int num_outside_ebds = 0;
189   - int num_in_map = 0;
  338 + // Continue only if observation pointer is valid
  339 + if (obs != NULL) {
  340 +
  341 + // Make sure that the observation holds a CTA event list. If this
  342 + // is not the case then throw an exception.
  343 + if (dynamic_cast<const GCTAEventList*>(obs->events()) == NULL)
  344 + throw GException::no_list(G_BIN_EVENTS);
  345 +
  346 + // Create skymap
  347 + GSkymap map = GSkymap(m_proj, m_coordsys,
  348 + m_xref, m_yref, m_binsz, m_binsz,
  349 + m_nxpix, m_nypix, m_enumbins);
  350 +
  351 + // Initialise binning statistics
  352 + int num_outside_map = 0;
  353 + int num_outside_ebds = 0;
  354 + int num_in_map = 0;
190 355  
191   - // Fill sky map
192   - GCTAEventList* events = (GCTAEventList*)obs.events();
193   - for (GCTAEventList::iterator event = events->begin(); event != events->end(); ++event) {
194   -
195   - // Determine sky pixel
196   - GCTAInstDir* inst = (GCTAInstDir*)&(event->dir());
197   - GSkyDir dir = inst->skydir();
198   - GSkyPixel pixel = map.dir2xy(dir);
199   -
200   - // Skip if pixel is out of range
201   - if (pixel.x() < -0.5 || pixel.x() > (m_nxpix-0.5) ||
202   - pixel.y() < -0.5 || pixel.y() > (m_nypix-0.5)) {
203   - num_outside_map++;
204   - continue;
  356 + // Fill sky map
  357 + GCTAEventList* events = (GCTAEventList*)obs->events();
  358 + for (GCTAEventList::iterator event = events->begin(); event != events->end(); ++event) {
  359 +
  360 + // Determine sky pixel
  361 + GCTAInstDir* inst = (GCTAInstDir*)&(event->dir());
  362 + GSkyDir dir = inst->skydir();
  363 + GSkyPixel pixel = map.dir2xy(dir);
  364 +
  365 + // Skip if pixel is out of range
  366 + if (pixel.x() < -0.5 || pixel.x() > (m_nxpix-0.5) ||
  367 + pixel.y() < -0.5 || pixel.y() > (m_nypix-0.5)) {
  368 + num_outside_map++;
  369 + continue;
  370 + }
  371 +
  372 + // Determine energy bin. Skip if we are outside the energy range
  373 + int index = obs->ebounds().index(event->energy());
  374 + if (index == -1) {
  375 + num_outside_ebds++;
  376 + continue;
  377 + }
  378 +
  379 + // Fill event in skymap
  380 + map(pixel, index) += 1.0;
  381 + num_in_map++;
  382 +
  383 + } // endfor: looped over all events
  384 +
  385 + // Log binning results
  386 + if (logTerse()) {
  387 + log << std::endl;
  388 + log.header1("Binning");
  389 + log << parformat("Events in list");
  390 + log << obs->events()->size() << std::endl;
  391 + log << parformat("Events in map");
  392 + log << num_in_map << std::endl;
  393 + log << parformat("Events outside map area");
  394 + log << num_outside_map << std::endl;
  395 + log << parformat("Events outside energy bins");
  396 + log << num_outside_ebds << std::endl;
205 397 }
206 398  
207   - // Determine energy bin. Skip if we are outside the energy range
208   - int index = obs.ebounds().index(event->energy());
209   - if (index == -1) {
210   - num_outside_ebds++;
211   - continue;
  399 + // Log map
  400 + if (logTerse()) {
  401 + log << std::endl;
  402 + log.header1("Counts map");
  403 + log << map << std::endl;
212 404 }
213 405  
214   - // Fill event in skymap
215   - map(pixel, index) += 1.0;
216   - num_in_map++;
217   -
218   - } // endfor: looped over all events
  406 + // Create events cube from sky map
  407 + GCTAEventCube cube(map);
219 408  
220   - // Log binning results
221   - if (logTerse()) {
222   - log << std::endl;
223   - log.header1("Binning");
224   - log << parformat("Events in list");
225   - log << obs.events()->size() << std::endl;
226   - log << parformat("Events in map");
227   - log << num_in_map << std::endl;
228   - log << parformat("Events outside map area");
229   - log << num_outside_map << std::endl;
230   - log << parformat("Events outside energy bins");
231   - log << num_outside_ebds << std::endl;
232   - }
  409 + // Replace event list by event cube in observation
  410 + obs->events(&cube);
233 411  
234   - // Log map
235   - if (logTerse()) {
236   - log << std::endl;
237   - log.header1("Counts map");
238   - log << map << std::endl;
239   - }
240   -
241   - // Create events cube from sky map
242   - GCTAEventCube cube(map);
  412 + // Setup energy range covered by data
  413 + GEnergy emin;
  414 + GEnergy emax;
  415 + GEbounds ebds;
  416 + emin.TeV(m_emin);
  417 + emax.TeV(m_emax);
  418 + ebds.setlog(emin, emax, m_enumbins);
  419 + obs->ebounds(ebds);
243 420  
244   - // Replace event list by event cube in observation
245   - obs.events(&cube);
246   -
247   - // Save observation
248   - obs.save(m_outfile, clobber());
  421 + } // endif: observation was valid
249 422  
250 423 // Return
251 424 return;
... ... @@ -266,16 +439,17 @@ void ctbin::init_members(void)
266 439 // Initialise members
267 440 m_evfile.clear();
268 441 m_outfile.clear();
  442 + m_proj.clear();
  443 + m_coordsys.clear();
  444 + m_obs.clear();
269 445 m_emin = 0.0;
270 446 m_emax = 0.0;
271 447 m_enumbins = 0;
272   - m_proj.clear();
273   - m_coordsys.clear();
274   - m_xref = 0.0;
275   - m_yref = 0.0;
276   - m_binsz = 0.0;
277   - m_nxpix = 0;
278   - m_nypix = 0;
  448 + m_xref = 0.0;
  449 + m_yref = 0.0;
  450 + m_binsz = 0.0;
  451 + m_nxpix = 0;
  452 + m_nypix = 0;
279 453  
280 454 // Set logger properties
281 455 log.date(true);
... ... @@ -290,7 +464,9 @@ void ctbin::init_members(void)
290 464 ***************************************************************************/
291 465 void ctbin::free_members(void)
292 466 {
293   - // Free members
  467 + // Write separator into logger
  468 + if (logTerse())
  469 + log << std::endl;
294 470  
295 471 // Return
296 472 return;
... ...
src/ctbin/ctbin.hpp
... ... @@ -25,25 +25,32 @@
25 25  
26 26 /* __Definitions _________________________________________________________ */
27 27 #define CTBIN_NAME "ctbin"
28   -#define CTBIN_VERSION "00-01-00"
  28 +#define CTBIN_VERSION "00-02-00"
29 29  
30 30  
31 31 /***********************************************************************//**
32 32 * @class ctbin
33 33 *
34   - * @brief CTA data binning tool interface defintion.
  34 + * @brief CTA data binning tool interface defintion
  35 + *
  36 + * This class bins CTA events into a counts map.
35 37 ***************************************************************************/
36 38 class ctbin : public GApplication {
37 39 public:
38 40 // Constructors and destructors
39 41 ctbin(void);
  42 + explicit ctbin(GObservations obs);
40 43 ctbin(int argc, char *argv[]);
41   - ~ctbin(void);
  44 + virtual ~ctbin(void);
42 45  
43 46 // Methods
44   - void run(void);
45   - void get_parameters(void);
46   - void bin(void);
  47 + void clear(void);
  48 + void execute(void);
  49 + void run(void);
  50 + void save(void);
  51 + GObservations& obs(void) { return m_obs; }
  52 + void get_parameters(void);
  53 + void bin_events(GCTAObservation* obs);
47 54  
48 55 protected:
49 56 // Protected methods
... ... @@ -63,6 +70,9 @@ protected:
63 70 double m_binsz; //!< Pixel size
64 71 int m_nxpix; //!< Number of pixels in longitude
65 72 int m_nypix; //!< Number of pixels in latitude
  73 +
  74 + // Protected members
  75 + GObservations m_obs; //!< Observation container
66 76 };
67 77  
68 78 #endif /* CTBIN_HPP */
... ...
src/ctbin/main.cpp
... ... @@ -20,7 +20,7 @@
20 20 #ifdef HAVE_CONFIG_H
21 21 #include <config.h>
22 22 #endif
23   -#include <stdio.h>
  23 +#include <cstdio>
24 24 #include "ctbin.hpp"
25 25  
26 26  
... ... @@ -43,7 +43,10 @@ int main (int argc, char *argv[])
43 43  
44 44 // Run application
45 45 try {
46   - application.run();
  46 + // Execute application
  47 + application.execute();
  48 +
  49 + // Signal success
47 50 rc = 0;
48 51 }
49 52 catch (std::exception &e) {
... ... @@ -51,7 +54,7 @@ int main (int argc, char *argv[])
51 54 // Extract error message
52 55 std::string message = e.what();
53 56 std::string signal = "*** ERROR encounterted in the execution of"
54   - " ctbin. Run aborted ...";
  57 + " ctbin. Run aborted ...";
55 58  
56 59 // Write error in logger
57 60 application.log << signal << std::endl;
... ...