Commit d8ee9991f826f5d81d04be9e53954b0e9fc5135b

Authored by knodlseder
1 parent d4952241

add init_map and write out sky map

src/ctskymap/ctskymap.cpp
@@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
33 #include "GTools.hpp" 33 #include "GTools.hpp"
34 34
35 /* __ Method name definitions ____________________________________________ */ 35 /* __ Method name definitions ____________________________________________ */
  36 +#define G_INIT_MAP "ctskymap::init_map(GCTAObservation* obs)"
36 #define G_BIN_EVENTS "ctskymap::bin_events(GCTAObservation*)" 37 #define G_BIN_EVENTS "ctskymap::bin_events(GCTAObservation*)"
37 38
38 /* __ Debug definitions __________________________________________________ */ 39 /* __ Debug definitions __________________________________________________ */
@@ -198,8 +199,7 @@ void ctskymap::clear(void) @@ -198,8 +199,7 @@ void ctskymap::clear(void)
198 /***********************************************************************//** 199 /***********************************************************************//**
199 * @brief Execute application 200 * @brief Execute application
200 * 201 *
201 - * This method bins the events data into a counts maps and saves the counts  
202 - * map into a FITS file. 202 + * This method creates the sky map and saves the map into a FITS file.
203 ***************************************************************************/ 203 ***************************************************************************/
204 void ctskymap::execute(void) 204 void ctskymap::execute(void)
205 { 205 {
@@ -207,10 +207,10 @@ void ctskymap::execute(void) @@ -207,10 +207,10 @@ void ctskymap::execute(void)
207 // parameters log 207 // parameters log
208 m_outfile = (*this)["outfile"].filename(); 208 m_outfile = (*this)["outfile"].filename();
209 209
210 - // Bin the event data 210 + // Create the sky map
211 run(); 211 run();
212 212
213 - // Save the counts map into FITS file 213 + // Save the sky map into FITS file
214 save(); 214 save();
215 215
216 // Return 216 // Return
@@ -219,13 +219,22 @@ void ctskymap::execute(void) @@ -219,13 +219,22 @@ void ctskymap::execute(void)
219 219
220 220
221 /***********************************************************************//** 221 /***********************************************************************//**
222 - * @brief Bin the event data 222 + * @brief Creates sky maps from data
  223 + *
  224 + * This method is the main code. It
  225 + * (1) reads task parameters from the par file
  226 + * (2) initialises the sky maps
  227 + * (3) loops over all observations to add its events to the sky map
223 ***************************************************************************/ 228 ***************************************************************************/
224 void ctskymap::run(void) 229 void ctskymap::run(void)
225 { 230 {
  231 + // Initialise statistics
  232 + int num_obs = 0;
  233 +
226 // Switch screen logging on in debug mode 234 // Switch screen logging on in debug mode
227 - if (logDebug()) 235 + if (logDebug()) {
228 log.cout(true); 236 log.cout(true);
  237 + }
229 238
230 // Get parameters 239 // Get parameters
231 get_parameters(); 240 get_parameters();
@@ -239,20 +248,24 @@ void ctskymap::run(void) @@ -239,20 +248,24 @@ void ctskymap::run(void)
239 // Write observation(s) into logger 248 // Write observation(s) into logger
240 if (logTerse()) { 249 if (logTerse()) {
241 log << std::endl; 250 log << std::endl;
242 - if (m_obs.size() > 1) 251 + if (m_obs.size() > 1) {
243 log.header1("Observations"); 252 log.header1("Observations");
244 - else 253 + }
  254 + else {
245 log.header1("Observation"); 255 log.header1("Observation");
  256 + }
246 log << m_obs << std::endl; 257 log << m_obs << std::endl;
247 } 258 }
248 259
249 // Write header 260 // Write header
250 if (logTerse()) { 261 if (logTerse()) {
251 log << std::endl; 262 log << std::endl;
252 - if (m_obs.size() > 1)  
253 - log.header1("Bin observations");  
254 - else  
255 - log.header1("Bin observation"); 263 + if (m_obs.size() > 1) {
  264 + log.header1("Map observations");
  265 + }
  266 + else {
  267 + log.header1("Map observation");
  268 + }
256 } 269 }
257 270
258 // Loop over all observation in the container 271 // Loop over all observation in the container
@@ -264,30 +277,45 @@ void ctskymap::run(void) @@ -264,30 +277,45 @@ void ctskymap::run(void)
264 // Continue only if observation is a CTA observation 277 // Continue only if observation is a CTA observation
265 if (obs != NULL) { 278 if (obs != NULL) {
266 279
267 - // Write header for observation 280 + // Write header for current observation
268 if (logTerse()) { 281 if (logTerse()) {
269 - if (obs->name().length() > 1) 282 + if (obs->name().length() > 1) {
270 log.header3("Observation "+obs->name()); 283 log.header3("Observation "+obs->name());
271 - else 284 + }
  285 + else {
272 log.header3("Observation"); 286 log.header3("Observation");
  287 + }
  288 + }
  289 +
  290 + // If this is the first valid observation we're working on
  291 + // then initialise the sky maps
  292 + if (num_obs == 0) {
  293 + init_map(obs);
273 } 294 }
274 295
275 // Map events into sky map 296 // Map events into sky map
276 map_events(obs); 297 map_events(obs);
  298 +
  299 + // Increment observation counter
  300 + num_obs++;
277 301
278 } // endif: CTA observation found 302 } // endif: CTA observation found
279 303
280 } // endfor: looped over observations 304 } // endfor: looped over observations
281 305
282 // Write observation(s) into logger 306 // Write observation(s) into logger
  307 +/*
283 if (logTerse()) { 308 if (logTerse()) {
284 log << std::endl; 309 log << std::endl;
285 - if (m_obs.size() > 1)  
286 - log.header1("Binned observations");  
287 - else  
288 - log.header1("Binned observation"); 310 + if (m_obs.size() > 1) {
  311 + log.header1("Map observations");
  312 + }
  313 + else {
  314 + log.header1("Map observation");
  315 + }
289 log << m_obs << std::endl; 316 log << m_obs << std::endl;
290 } 317 }
  318 +*/
291 319
292 // Return 320 // Return
293 return; 321 return;
@@ -301,45 +329,18 @@ void ctskymap::run(void) @@ -301,45 +329,18 @@ void ctskymap::run(void)
301 ***************************************************************************/ 329 ***************************************************************************/
302 void ctskymap::save(void) 330 void ctskymap::save(void)
303 { 331 {
304 -/*  
305 // Write header 332 // Write header
306 if (logTerse()) { 333 if (logTerse()) {
307 log << std::endl; 334 log << std::endl;
308 - if (m_obs.size() > 1)  
309 - log.header1("Save observations");  
310 - else  
311 - log.header1("Save observation"); 335 + log.header1("Save sky map");
312 } 336 }
313 337
314 // Get output filename 338 // Get output filename
315 m_outfile = (*this)["outfile"].filename(); 339 m_outfile = (*this)["outfile"].filename();
316 340
317 - // Loop over all observations in the container  
318 - int file_num = 0;  
319 - for (int i = 0; i < m_obs.size(); ++i) {  
320 -  
321 - // Get CTA observation  
322 - GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]);  
323 -  
324 - // Save only if observation is a CTA observation  
325 - if (obs != NULL) { 341 + // Save sky map
  342 + m_skymap.save(m_outfile, clobber());
326 343
327 - // Set filename. If more than one file will be created an  
328 - // index "_xxx" will be appended.  
329 - std::string filename = m_outfile;  
330 - if (file_num > 0)  
331 - filename += "_"+str(file_num);  
332 -  
333 - // Save file  
334 - obs->save(filename, clobber());  
335 -  
336 - // Increment file number  
337 - file_num++;  
338 -  
339 - } // endif: observation was a CTA observation  
340 -  
341 - } // endfor: looped over files  
342 -*/  
343 // Return 344 // Return
344 return; 345 return;
345 } 346 }
@@ -392,6 +393,60 @@ void ctskymap::get_parameters(void) @@ -392,6 +393,60 @@ void ctskymap::get_parameters(void)
392 393
393 394
394 /***********************************************************************//** 395 /***********************************************************************//**
  396 + * @brief Initialise sky map
  397 + *
  398 + * @param[in] obs CTA observation (no NULL pointer allowed).
  399 + *
  400 + * @exception GCTAException::no_pointing
  401 + * No valid CTA pointing found.
  402 + *
  403 + * This method initialises the sky map.
  404 + ***************************************************************************/
  405 +void ctskymap::init_map(GCTAObservation* obs)
  406 +{
  407 + // Clear any existing sky map
  408 + m_skymap.clear();
  409 +
  410 + // Get map centre. If no centre is specified then extract the map centre
  411 + // from the pointing direction. This obviously only works for
  412 + double xref;
  413 + double yref;
  414 + if (m_xref != 9999.0 && m_yref != 9999.0) {
  415 + xref = m_xref;
  416 + yref = m_yref;
  417 + }
  418 + else {
  419 +
  420 +
  421 + // Get pointer on CTA pointing
  422 + const GTime srcTime; // Dummy time
  423 + const GCTAPointing *pnt = obs->pointing(srcTime);
  424 + if (pnt == NULL) {
  425 + throw GCTAException::no_pointing(G_INIT_MAP);
  426 + }
  427 +
  428 + // Set reference point to pointing
  429 + if (toupper(m_coordsys) == "GAL") {
  430 + xref = pnt->dir().l_deg();
  431 + yref = pnt->dir().b_deg();
  432 + }
  433 + else {
  434 + xref = pnt->dir().ra_deg();
  435 + yref = pnt->dir().dec_deg();
  436 + }
  437 +
  438 + } // endelse: map centre set to pointing
  439 +
  440 + // Create skymap
  441 + m_skymap = GSkymap(m_proj, m_coordsys, xref, yref, m_binsz, m_binsz,
  442 + m_nxpix, m_nypix, 1);
  443 +
  444 + // Return
  445 + return;
  446 +}
  447 +
  448 +
  449 +/***********************************************************************//**
395 * @brief Map events into a sky map 450 * @brief Map events into a sky map
396 * 451 *
397 * @param[in] obs CTA observation. 452 * @param[in] obs CTA observation.
@@ -421,38 +476,6 @@ void ctskymap::map_events(GCTAObservation* obs) @@ -421,38 +476,6 @@ void ctskymap::map_events(GCTAObservation* obs)
421 emin.TeV(m_emin); 476 emin.TeV(m_emin);
422 emax.TeV(m_emax); 477 emax.TeV(m_emax);
423 478
424 - // Get map centre  
425 - double xref;  
426 - double yref;  
427 - if (m_xref != 9999.0 && m_yref != 9999.0) {  
428 - xref = m_xref;  
429 - yref = m_yref;  
430 - }  
431 - else {  
432 -  
433 - // Get pointer on CTA pointing  
434 - const GTime srcTime; // Dummy time  
435 - const GCTAPointing *pnt = obs->pointing(srcTime);  
436 - if (pnt == NULL)  
437 - throw GCTAException::no_pointing(G_BIN_EVENTS);  
438 -  
439 - // Set reference point to pointing  
440 - if (toupper(m_coordsys) == "GAL") {  
441 - xref = pnt->dir().l_deg();  
442 - yref = pnt->dir().b_deg();  
443 - }  
444 - else {  
445 - xref = pnt->dir().ra_deg();  
446 - yref = pnt->dir().dec_deg();  
447 - }  
448 -  
449 - } // endelse: map centre set to pointing  
450 -  
451 - // Create skymap  
452 - GSkymap map = GSkymap(m_proj, m_coordsys,  
453 - xref, yref, m_binsz, m_binsz,  
454 - m_nxpix, m_nypix, 1);  
455 -  
456 // Initialise binning statistics 479 // Initialise binning statistics
457 int num_outside_map = 0; 480 int num_outside_map = 0;
458 int num_outside_erange = 0; 481 int num_outside_erange = 0;
@@ -471,7 +494,7 @@ void ctskymap::map_events(GCTAObservation* obs) @@ -471,7 +494,7 @@ void ctskymap::map_events(GCTAObservation* obs)
471 // Determine sky pixel 494 // Determine sky pixel
472 GCTAInstDir* inst = (GCTAInstDir*)&(event->dir()); 495 GCTAInstDir* inst = (GCTAInstDir*)&(event->dir());
473 GSkyDir dir = inst->skydir(); 496 GSkyDir dir = inst->skydir();
474 - GSkyPixel pixel = map.dir2xy(dir); 497 + GSkyPixel pixel = m_skymap.dir2xy(dir);
475 498
476 // Skip if pixel is out of range 499 // Skip if pixel is out of range
477 if (pixel.x() < -0.5 || pixel.x() > (m_nxpix-0.5) || 500 if (pixel.x() < -0.5 || pixel.x() > (m_nxpix-0.5) ||
@@ -481,7 +504,7 @@ void ctskymap::map_events(GCTAObservation* obs) @@ -481,7 +504,7 @@ void ctskymap::map_events(GCTAObservation* obs)
481 } 504 }
482 505
483 // Fill event in skymap 506 // Fill event in skymap
484 - map(pixel) += 1.0; 507 + m_skymap(pixel, 0) += 1.0;
485 num_in_map++; 508 num_in_map++;
486 509
487 } // endfor: looped over all events 510 } // endfor: looped over all events
@@ -504,7 +527,7 @@ void ctskymap::map_events(GCTAObservation* obs) @@ -504,7 +527,7 @@ void ctskymap::map_events(GCTAObservation* obs)
504 if (logTerse()) { 527 if (logTerse()) {
505 log << std::endl; 528 log << std::endl;
506 log.header1("Sky map"); 529 log.header1("Sky map");
507 - log << map << std::endl; 530 + log << m_skymap << std::endl;
508 } 531 }
509 532
510 } // endif: observation was valid 533 } // endif: observation was valid
@@ -531,6 +554,7 @@ void ctskymap::init_members(void) @@ -531,6 +554,7 @@ void ctskymap::init_members(void)
531 m_proj.clear(); 554 m_proj.clear();
532 m_coordsys.clear(); 555 m_coordsys.clear();
533 m_obs.clear(); 556 m_obs.clear();
  557 + m_skymap.clear();
534 m_emin = 0.0; 558 m_emin = 0.0;
535 m_emax = 0.0; 559 m_emax = 0.0;
536 m_xref = 9999.0; // Flags unset (use pointing direction) 560 m_xref = 9999.0; // Flags unset (use pointing direction)
@@ -560,6 +584,7 @@ void ctskymap::copy_members(const ctskymap&amp; app) @@ -560,6 +584,7 @@ void ctskymap::copy_members(const ctskymap&amp; app)
560 m_proj = app.m_proj; 584 m_proj = app.m_proj;
561 m_coordsys = app.m_coordsys; 585 m_coordsys = app.m_coordsys;
562 m_obs = app.m_obs; 586 m_obs = app.m_obs;
  587 + m_skymap = app.m_skymap;
563 m_emin = app.m_emin; 588 m_emin = app.m_emin;
564 m_emax = app.m_emax; 589 m_emax = app.m_emax;
565 m_xref = app.m_xref; 590 m_xref = app.m_xref;
src/ctskymap/ctskymap.hpp
@@ -62,6 +62,7 @@ public: @@ -62,6 +62,7 @@ public:
62 void save(void); 62 void save(void);
63 GObservations& obs(void) { return m_obs; } 63 GObservations& obs(void) { return m_obs; }
64 void get_parameters(void); 64 void get_parameters(void);
  65 + void init_map(GCTAObservation* obs);
65 void map_events(GCTAObservation* obs); 66 void map_events(GCTAObservation* obs);
66 67
67 protected: 68 protected:
@@ -85,6 +86,7 @@ protected: @@ -85,6 +86,7 @@ protected:
85 86
86 // Protected members 87 // Protected members
87 GObservations m_obs; //!< Observation container 88 GObservations m_obs; //!< Observation container
  89 + GSkymap m_skymap; //!< Sky map
88 }; 90 };
89 91
90 #endif /* CTSKYMAP_HPP */ 92 #endif /* CTSKYMAP_HPP */