ctobssim.cpp 13.7 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
/***************************************************************************
 *                ctobssim - CTA observation simulator tool                *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2011 by Jurgen Knodlseder                                *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/**
 * @file ctobssim.cpp
 * @brief CTA observation simulator tool implementation
 * @author J. Knodlseder
 */

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

/* __ Debug definitions __________________________________________________ */

/* __ Coding definitions _________________________________________________ */


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

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
ctobssim::ctobssim(void) : GApplication(CTOBSSIM_NAME, CTOBSSIM_VERSION)
{
    // Initialise members
    init_members();

    // Write header into logger
    log_header();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Command line constructor
 *
 * @param[in] argc Number of arguments in command line.
 * @param[in] argv Array of command line arguments.
 ***************************************************************************/
ctobssim::ctobssim(int argc, char *argv[]) : 
                    GApplication(CTOBSSIM_NAME, CTOBSSIM_VERSION, argc, argv)
{
    // Initialise members
    init_members();

    // Write header into logger
    log_header();

    // Return
    return;
}


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

    // Return
    return;
}


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

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

/***********************************************************************//**
 * @brief Simulate event data
 ***************************************************************************/
void ctobssim::run(void)
{
    // Switch screen logging on in debug mode
    if (logDebug())
        log.cout(true);

    // Get parameters
    get_parameters();

    // Write parameters into logger
    if (logTerse()) {
        log_parameters();
        log << std::endl;
        log << m_pnt << std::endl;
        log << m_obs << std::endl;
        log << m_models << std::endl;
    }

    // Write header
    if (logTerse()) {
        log << std::endl;
        log.header1("Simulate photons");
    }

    // Simulate photons
    GPhotons photons = simulate_photons();

    // Write header
    if (logTerse()) {
        log << std::endl;
        log.header1("Simulate events");
    }

    // Simulate events
    GCTAEventList* events = simulate_events(photons);

    // Write header
    if (logTerse()) {
        log << std::endl;
        log.header1("Save events in FITS file");
    }

    // Save events
    save_events(events);

    // Release events
    delete events;

    // Write separator into logger
    if (logTerse())
        log << std::endl;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Get application parameters
 *
 * Get all task parameters from parameter file or (if required) by querying
 * the user.
 ***************************************************************************/
void ctobssim::get_parameters(void)
{
    // Get parameters
    m_infile  = par("infile")->value();
    m_outfile = par("outfile")->value();
    m_caldb   = par("caldb")->value();
    m_irf     = par("irf")->value();
    m_seed    = par("seed")->integer();
    m_ra      = par("ra")->real();
    m_dec     = par("dec")->real();
    m_rad     = par("rad")->real();
    m_tmin    = par("tmin")->real();
    m_tmax    = par("tmax")->real();
    m_emin    = par("emin")->real();
    m_emax    = par("emax")->real();

    // Initialise random number generator
    m_ran.seed(m_seed);

    // Set CTA pointing
    GSkyDir dir;
    dir.radec_deg(m_ra, m_dec);
    m_pnt.dir(dir);

    // Set GTI
    GGti  gti;
    GTime tstart;
    GTime tstop;
    tstart.met(m_tmin);
    tstop.met(m_tmax);
    gti.append(tstart, tstop);

    // Set energy boundaries
    GEbounds ebds;
    GEnergy  emin;
    GEnergy  emax;
    emin.TeV(m_emin);
    emax.TeV(m_emax);
    ebds.append(emin, emax);

    // Set observation
    m_obs.ebounds(ebds);
    m_obs.gti(gti);
    m_obs.response(m_irf, m_caldb);
    m_obs.pointing(m_pnt);

    // Load models
    m_models.load(m_infile);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Simulate photons
 ***************************************************************************/
GPhotons ctobssim::simulate_photons(void)
{
    // Allocate MC parameters
    GSkyDir dir;
    GEnergy emin;
    GEnergy emax;
    GTime   tmin;
    GTime   tmax;

    // Define MC parameters
    dir.radec_deg(m_ra, m_dec);
    emin.TeV(m_emin);
    emax.TeV(m_emax);
    tmin.met(m_tmin);
    tmax.met(m_tmax);

    // Allocate photons
    GPhotons photons;

    // Simulate photons for all sky models
    for (int i = 0; i < m_models.size(); ++i) {

        // Get sky model (NULL if not a sky model)
        GModelSky* model = dynamic_cast<GModelSky*>(m_models(i));

        // If we have a sky model then simulate photons
        if (model != NULL) {

            // Get photons
            GPhotons p = model->mc(m_area, dir, m_rad, emin, emax, tmin, tmax, m_ran);

            // Reserve new space for photons
            photons.reserve(photons.size() + p.size());
        
            // Add photons to list
            for (int k = 0; k < p.size(); ++k) {
                p[k].mcid(i);
                photons.push_back(p[k]);
            }

        } // endif: model was a sky model

    } // endfor: looped over models

    // Dump simulation results
    if (logNormal()) {
        log << parformat("Simulated photons") << str(photons.size()) << std::endl;
        log << parformat("Simulation area");
        log << str(m_area) << " cm2" << std::endl;
        log << parformat("Simulation cone");
        log << "RA=" << dir.ra_deg() << " deg";
        log << ", Dec=" << dir.dec_deg() << "deg";
        log << ", r=" << m_rad << " deg" << std::endl;
        log << parformat("Time interval");
        log << tmin << " - " << tmax << std::endl;
        log << parformat("Energy range");
        log << emin << " - " << emax << std::endl;
        log << parformat("Random number seed") << str(m_seed) << std::endl;
    }
    
    // Return photons
    return photons;
}


/***********************************************************************//**
 * @brief Simulate events
 *
 * @param[in] photons Photon list.
 *
 * Simulate events from a photon list using a given response function and
 * a given pointing.
 ***************************************************************************/
GCTAEventList* ctobssim::simulate_events(const GPhotons& photons)
{
    // Allocate CTA event list
    GCTAEventList* events = new GCTAEventList;

    // Reserves space for events
    events->reserve(photons.size());

    // Simulate events from photons
    for (int i = 0; i < photons.size(); ++i) {
        GCTAEventAtom* event = m_obs.response()->mc(m_area, photons[i], m_pnt, m_ran);
        if (event != NULL)
            events->append(*event);
        delete event;
    }

    // Dump simulation results
    if (logNormal()) {
        log << parformat("MC source events");
        log << str(events->size()) << std::endl;
    }

    // Simulate background events
    for (int i = 0; i < m_models.size(); ++i) {

        // Get model (NULL if not a radial acceptance model)
        GCTAModelRadialAcceptance* model = 
                 dynamic_cast<GCTAModelRadialAcceptance*>(m_models(i));

        // If we have a radial acceptance model then simulate events
        if (model != NULL) {

            // Get simulated event list
            GCTAEventList* list = model->mc(m_obs, m_ran);

            // Reserves space for events
            events->reserve(list->size()+events->size());

            // Append events
            for (int k = 0; k < list->size(); k++)
                events->append(*(list->pointer(k)));

            // Dump simulation results
            if (logNormal()) {
                log << parformat("MC background events");
                log << str(list->size()) << std::endl;
            }

            // Free event list
            delete list;

        } // endif: model was valid

    } // endfor: looped over all models

    // Return event list
    return events;
}


/***********************************************************************//**
 * @brief Save events
 *
 * @todo Set header keywords
 ***************************************************************************/
void ctobssim::save_events(const GCTAEventList* events)
{
    // Allocate events file
    GFits file;

    // Write events
    events->write(&file);

    // Set keywords in EVENTS header
    set_events_keywords(&file);

    // Allocate Gti
    GGti gti;

    // Setup single GTI covering the selected time range
    GTime tstart;
    GTime tstop;
    tstart.met(m_tmin);
    tstop.met(m_tmax);
    gti.add(tstart, tstop);

    // Write GTI
    gti.write(&file);

    // Dump FITS file
    if (logExplicit()) {
        log << file << std::endl;
    }

    // Save events file. We need to use to saveto() method to make sure
    // that any existing file is indeed overwritten
    file.saveto(m_outfile, clobber());

    // Close FITS file
    file.close();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Set keywords of EVENTS header
 *
 * @param[in] file FITS file.
 ***************************************************************************/
void ctobssim::set_events_keywords(GFits* file)
{
    // Get EVENTS HDU
    GFitsHDU* hdu = file->hdu("EVENTS");

    // Set observation time information
    hdu->card("TSTART",   m_tmin,        "[s] Mission time of start of observation");
    hdu->card("TSTOP",    m_tmax,        "[s] Mission time of end of observation");
    hdu->card("TELAPSE",  m_tmax-m_tmin, "[s] Mission elapsed time");
    hdu->card("ONTIME",   m_tmax-m_tmin, "[s] Total good time including deadtime");
    hdu->card("LIVETIME", m_tmax-m_tmin, "[s] Total livetime");

    // Set target information
    //hdu->card("RA_OBJ",  m_ra,  "[deg] Target Right Ascension");
    //hdu->card("DEC_OBJ", m_dec, "[deg] Target Declination");

    // Set pointing information
    hdu->card("RA_PNT",  m_ra,  "[deg] Pointing Right Ascension");
    hdu->card("DEC_PNT", m_dec, "[deg] Pointing Declination");

    // Return
    return;
}


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

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void ctobssim::init_members(void)
{
    // Initialise members
    m_infile.clear();
    m_outfile.clear();
    m_caldb.clear();
    m_irf.clear();
    m_obs.clear();
    m_pnt.clear();
    m_seed =   1;
    m_ra   = 0.0;
    m_dec  = 0.0;
    m_rad  = 0.0;
    m_tmin = 0.0;
    m_tmax = 0.0;
    m_emin = 0.0;
    m_emax = 0.0;

    // Set fixed parameters
    m_area = 3200000.0 * 1.0e4; //!< 3200000.0 m^2

    // Set logger properties
    log.date(true);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void ctobssim::free_members(void)
{
    // Return
    return;
}