cttsmap.cpp 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
/***************************************************************************
 *                    cttsmap - TS map calculation tool                    *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2014-2016 by Michael Mayer                               *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file cttsmap.hpp
 * @brief TS map calculation tool interface implementation
 * @author Michael Mayer
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdio>
#include "cttsmap.hpp"
#include "GTools.hpp"

/* __ Method name definitions ____________________________________________ */
#define G_GET_PARAMETERS                          "cttsmap::get_parameters()"

/* __ Debug definitions __________________________________________________ */

/* __ Coding definitions _________________________________________________ */


/*==========================================================================
 =                                                                         =
 =                        Constructors/destructors                         =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 *
 * Constructs an empty cttsmap tool.
 ***************************************************************************/
cttsmap::cttsmap(void) : ctlikelihood(CTTSMAP_NAME, CTTSMAP_VERSION)
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Observations constructor
 *
 * param[in] obs Observation container.
 *
 * Constructs cttsmap tool from an observations container.
 ***************************************************************************/
cttsmap::cttsmap(const GObservations& obs) :
         ctlikelihood(CTTSMAP_NAME, CTTSMAP_VERSION, obs)
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Command line constructor
 *
 * @param[in] argc Number of arguments in command line.
 * @param[in] argv Array of command line arguments.
 *
 * Constructs an instance of the cttsmap tool that will parse user parameters
 * that are provided as command line arguments.
 ***************************************************************************/
cttsmap::cttsmap(int argc, char *argv[]) :
         ctlikelihood(CTTSMAP_NAME, CTTSMAP_VERSION, argc, argv)
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] app Application.
 ***************************************************************************/
cttsmap::cttsmap(const cttsmap& app) : ctlikelihood(app)
{
    // Initialise members
    init_members();

    // Copy members
    copy_members(app);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 ***************************************************************************/
cttsmap::~cttsmap(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                               Operators                                 =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] app Application.
 * @return Application.
 ***************************************************************************/
cttsmap& cttsmap::operator=(const cttsmap& app)
{
    // Execute only if object is not identical
    if (this != &app) {

        // Copy base class members
        this->ctlikelihood::operator=(app);

        // Free members
        free_members();

        // Initialise members
        init_members();

        // Copy members
        copy_members(app);

    } // endif: object was not identical

    // Return this object
    return *this;
}


/*==========================================================================
 =                                                                         =
 =                            Public methods                               =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Clear instance
 ***************************************************************************/
void cttsmap::clear(void)
{
    // Free members
    free_members();
    this->ctlikelihood::free_members();
    this->ctobservation::free_members();
    this->ctool::free_members();

    // Clear base class (needed to conserve tool name and version)
    this->GApplication::clear();

    // Initialise members
    this->ctool::init_members();
    this->ctobservation::init_members();
    this->ctlikelihood::init_members();
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Computes the TS maps
 *
 * This method moves a source along a grid of coordinates and refits the
 * given models including this additional source. The fit parameters of the
 * source and its TS values are filled into respective position in the map.
 ***************************************************************************/
void cttsmap::run(void)
{
    // If we're in debug mode then all output is also dumped on the screen
    if (logDebug()) {
        log.cout(true);
    }

    // Get task parameters
    get_parameters();

    // Set energy dispersion flags of all CTA observations and save old
    // values in save_edisp vector
    std::vector<bool> save_edisp = set_edisp(m_obs, m_apply_edisp);

    // Write input observation container into logger
    log_observations(NORMAL, m_obs, "Input observation");

    // Write header
    log_header1(TERSE, "Initialise TS map");

    // Get bins to be computed
    // To split the computation on several jobs,
    // the user is able to provide a subset of bins
    // which should be computed
    int binmin = (m_binmin == -1) ? 0 : m_binmin;
    int binmax = (m_binmax == -1) ? m_tsmap.npix() : m_binmax;

    // Set optimizer logger
    if (logExplicit()) {
        static_cast<GOptimizerLM*>(&m_opt)->logger(&log);
    }
    else {
        static_cast<GOptimizerLM*>(&m_opt)->logger(NULL);
    }

    // Store initial models
    GModels models_orig = m_obs.models();

    // Get model instance for further computations
    GModels models = m_obs.models();

    // Remove test source
    models.remove(m_srcname);

    // Get likelihood of null hypothesis if not given before
    if (m_logL0 == 0.0) {

        // Write header
        log_header1(TERSE, "Compute NULL Hypothesis for TS computation");

        // Compute likelihood without the test source
        m_obs.models(models);
        m_obs.optimize(m_opt);
        m_logL0 = -(m_opt.value());

	}

    // Write header
    log_header1(TERSE, "Generate TS map");

    // Loop over grid positions
    for (int i = binmin; i < binmax; ++i) {

    	// Get the coordinate of current bin
    	GSkyDir bincentre = m_tsmap.inx2dir(i);

    	// Header for verbose logging
        log_header2(EXPLICIT, "Computing TS for bin number "+gammalib::str(i)+
                    " at "+bincentre.print());

    	// Add test source at current bin position
        (*m_testsource)["RA"].value(bincentre.ra_deg());
        (*m_testsource)["DEC"].value(bincentre.dec_deg());
        models.append(*m_testsource);

    	// Assign models to observations
    	m_obs.models(models);

    	// Optimize observation container
    	m_obs.optimize(m_opt);

    	// Compute errors if necessary
    	if (m_errors) {
    		m_obs.errors(m_opt);
    	}

    	// Get status of optimization
    	int status = m_opt.status();

    	// Retrieve the Likelihood value
    	double logL1 = -(m_opt.value());

    	// Compute TS value
    	double ts = 2.0 * (logL1 - m_logL0);

    	// Log information
    	if (logExplicit()) {
            log_value(EXPLICIT, "TS value", ts);
    	}
        else if (logNormal()) {
            log_value(NORMAL, "TS value (bin "+gammalib::str(i)+")",
                      gammalib::str(ts)+" ("+bincentre.print()+")");
        }

    	// Get test source model instance
    	GModels best_fit_model = m_obs.models();
    	GModel* testsource     = best_fit_model[m_srcname];

    	// Assign values to the maps
    	m_tsmap(i) = ts;

    	// Extract fitted test source parameters
        for (int j = 0; j < m_mapnames.size(); ++j) {

            // If map name start with "e_" then set the parameter error ...
            if (m_mapnames[j].substr(0,2) == "e_") {

                // Get parameter name by removing the error prefix
            	std::string parname = m_mapnames[j].substr(2, m_mapnames[j].size());

                // Get parameter error
           		m_maps[j](i) = (*testsource)[parname].error();

            }

            // ... otherwise set the parameter value
            else {
                m_maps[j](i) = (*testsource)[m_mapnames[j]].value();
            }

        } // endfor: looped over all maps

        // Set Fit status of the bin
        m_statusmap(i) = status;

    	// Remove model from container
    	models.remove(m_srcname);

    } // endfor: looped over grid positions

    // Bring models to initial state
    m_obs.models(models_orig);

    // Restore energy dispersion flags of all CTA observations
    restore_edisp(m_obs, save_edisp);

    // Optionally publish TS map
    if (m_publish) {
        publish();
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save maps
 *
 * This method saves the TS map and additional maps into a FITS file.
 ***************************************************************************/
void cttsmap::save(void)
{
    // Write header
    log_header1(TERSE, "Save TS map");

    // Get TS map filename
    m_outmap = (*this)["outmap"].filename();

    // Save only if filename is non-empty
    if (!m_outmap.is_empty() && !m_tsmap.is_empty()) {

        // Create fits file
        GFits fits;

        // Write the sky maps to the FITS file
        m_tsmap.write(fits);
        for (int i = 0; i < m_mapnames.size(); i++) {
            m_maps[i].write(fits);
        }

        // Set extension name for all maps
        for (int i = 0; i < m_mapnames.size(); i++) {
            fits[i+1]->extname(m_mapnames[i]);
        }

        // Write Fit status map
        m_statusmap.write(fits);
        fits[fits.size()-1]->extname("STATUS MAP");

        // Save FITS file
        fits.saveto(m_outmap, clobber());

    } // endif: TS map filename was valid

    // Write into logger what has been done
    std::string fname = (m_outmap.is_empty()) ? "NONE" : m_outmap.url();
    if (m_tsmap.is_empty()) {
        fname.append(" (TS map is empty, no file created)");
    }
    log_value(NORMAL, "TS map file", fname);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Publish TS map
 *
 * @param[in] name TS map name.
 ***************************************************************************/
void cttsmap::publish(const std::string& name)
{
    // Write header into logger
    log_header1(TERSE, "Publish TS map");

    // Set default name is user name is empty
    std::string user_name(name);
    if (user_name.empty()) {
        user_name = CTTSMAP_NAME;
    }

    // Write TS map name into logger
    log_value(NORMAL, "TS map name", user_name);

    // Publish TS map
    m_tsmap.publish(user_name);

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                             Private methods                             =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void cttsmap::init_members(void)
{
    // Initialise members
    m_srcname.clear();
    m_outmap.clear();
    m_apply_edisp = false;
    m_publish     = false;
    m_errors      = false;

    // Initialise protected members
    m_binmin     = -1;
    m_binmax     = -1;
    m_logL0      = 0.0;
    m_tsmap.clear();
    m_statusmap.clear();
    m_mapnames.clear();
    m_maps.clear();
    m_testsource = NULL;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] app Application.
 ***************************************************************************/
void cttsmap::copy_members(const cttsmap& app)
{
    // Copy attributes
    m_srcname     = app.m_srcname;
    m_outmap      = app.m_outmap;
    m_apply_edisp = app.m_apply_edisp;
    m_publish     = app.m_publish;
    m_errors      = app.m_errors;

    // Copy protected members
    m_binmin    = app.m_binmin;
    m_binmax    = app.m_binmax;
    m_logL0     = app.m_logL0;
    m_tsmap     = app.m_tsmap;
    m_statusmap = app.m_statusmap;
    m_mapnames  = app.m_mapnames;
    m_maps      = app.m_maps;

    // Clone protected members
    m_testsource = (app.m_testsource != NULL) ? app.m_testsource->clone()
                                              : NULL;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void cttsmap::free_members(void)
{
    // Free test source
    if (m_testsource != NULL) {
        delete m_testsource;
        m_testsource = NULL;
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Get application parameters
 *
 * @exception GException::invalid_value
 *            Test source has no RA/DEC parameters.
 *
 * Get all task parameters from parameter file or (if required) by querying
 * the user. Most parameters are only required if no observation exists so
 * far in the observation container. In this case, a single CTA observation
 * will be added to the container, using the definition provided in the
 * parameter file.
 ***************************************************************************/
void cttsmap::get_parameters(void)
{
    // Setup observations from "inobs" parameter
    setup_observations(m_obs);

    // Setup models from "inmodel" parameter
    setup_models(m_obs, (*this)["srcname"].string());

    // Get name of test source
    m_srcname = (*this)["srcname"].string();

    // Get model and store it as protected member. Note that the clone
    // method will never return a NULL pointer, hence we do not need
    // to check the pointer.
    m_testsource = m_obs.models()[m_srcname]->clone();

    // Check if model has RA and DEC parameters which are necessary to
    // continue
    if (!m_testsource->has_par("RA") || !m_testsource->has_par("DEC")) {
        std::string msg = "Source \""+m_srcname+"\" has no \"RA\" and "
                          "\"DEC\" parameters. Only sources with \"RA\" "
                          "and \"DEC\" parameters can be used as test "
                          "sources.";
        throw GException::invalid_value(G_GET_PARAMETERS, msg);
    }

    // Fix the spatial parameters of the test source
    (*m_testsource)["RA"].fix();
    (*m_testsource)["DEC"].fix();

    // Create sky map based on task parameters
    GSkyMap map = create_map(m_obs);

    // Check if errors should be computed
    m_errors  = (*this)["errors"].boolean();

    // Initialise maps from user parameters
    init_maps(map);

    // Read energy dispersion flag
    m_apply_edisp = (*this)["edisp"].boolean();

    // Get optional splitting parameters
    m_binmin  = (*this)["binmin"].integer();
    m_binmax  = (*this)["binmax"].integer();
    m_logL0   = (*this)["logL0"].real();
    m_publish = (*this)["publish"].boolean();

    // Optionally read ahead parameters so that they get correctly
    // dumped into the log file
    if (read_ahead()) {
        m_outmap = (*this)["outmap"].filename();
    }

    // Write parameters into logger
    log_parameters(TERSE);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Initialise skymaps
 *
 * Initialises skymaps that will contain map information.
 ***************************************************************************/
void cttsmap::init_maps(const GSkyMap& map)
{
    // Initialise map information
    m_tsmap.clear();

    // Create skymap
    m_tsmap = GSkyMap(map);

    // Initialise map information
	m_statusmap.clear();

	// Create status map and progress map
	m_statusmap = GSkyMap(map);

	// Set status map to invalid value -1 to signal that bin wasn't computed yet
	m_statusmap = -1.0;

    // Loop over all model parameters
    for (int i = 0; i < m_testsource->size(); i++) {

        // Skip fixed parameters
        if ((*m_testsource)[i].is_fixed()) {
            continue;
        }

        // Add sky map for free parameters. Note that push_back will
        // create a copy.
        m_maps.push_back(map);

        // Store parameter name
        m_mapnames.push_back((*m_testsource)[i].name());

        // Add parameter errors if requested
        if (m_errors) {

            // Add sky map for fit error of the free parameter
            m_maps.push_back(map);

            // Store name of parameter error
            m_mapnames.push_back("e_" + (*m_testsource)[i].name());

        } // endif: error calculation was requested
        
    } // endfor: looped over all model parameters

    // Return
    return;
}