diff --git a/ChangeLog b/ChangeLog index edfbd95..b3a9154 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,7 @@ * Version 1.2.0 released ======================== - Introduce ctlikelihood base class (#1828) + Introduce ctobservation and ctlikelihood base classes (#1828) Remove ctselect::time_reference() method Add ctool and cscript pars() methods (#1838) Fix ctbin::save() bug for empty observation container (#1835) diff --git a/NEWS b/NEWS index e4b3436..8fade37 100644 --- a/NEWS +++ b/NEWS @@ -24,10 +24,26 @@ all parameters in a dictionary (#1838). Support model ------------- -The ctlikelihood base class was introduced from which all likelihood ctools -now derive (ctlike, cttsmap, cterror, ctulimit). This avoids code duplication -and provides all tools with a standard interface. All tools now have the -obs() and opt() method (#1828). +The ctobservation base class that derives from the ctool base class was +introduced to provide a common base class for all ctools that hold an +observation container. The class implements the following methods: +- obs() Returns reference to observation container +- first_unbinned_observation() First unbinned CTA observation in container +- next_unbinned_observation() Next unbinned CTA observation in container + +The ctlikelihood base class that derives from the ctobservation base class +was introduced to provide a common base class for all ctools that perform +maximum likelihood optimisations. The class holds an optimizer member. The +class implements the following methods: +- opt() Returns pointer to optimizer +- evaluate() Evaluates log-likelihood at a given parameter value +The following ctools derive from the class: +- cterror +- ctlike +- cttsmap +- ctulimit + +The introduction of these base classes avoids code duplication. ctobssim - CTA observation simulation diff --git a/pyext/ctlikelihood.i b/pyext/ctlikelihood.i index 47450ae..6aa3b86 100644 --- a/pyext/ctlikelihood.i +++ b/pyext/ctlikelihood.i @@ -35,7 +35,7 @@ * * @brief Base class for likelihood tools ***************************************************************************/ -class ctlikelihood : public ctool { +class ctlikelihood : public ctobservation { public: // Constructors and destructors @@ -54,8 +54,7 @@ public: virtual void save(void) = 0; // Methods - const GObservations& obs(void) const; - const GOptimizer* opt(void) const; + const GOptimizer* opt(void) const; }; diff --git a/pyext/ctobservation.i b/pyext/ctobservation.i new file mode 100644 index 0000000..64e2a30 --- /dev/null +++ b/pyext/ctobservation.i @@ -0,0 +1,65 @@ +/*************************************************************************** + * ctobservation - Base class for observation tools * + * ----------------------------------------------------------------------- * + * copyright (C) 2016 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * 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 . * + * * + ***************************************************************************/ +/** + * @file ctobservation.i + * @brief Observation tool base class interface definition + * @author Juergen Knoedlseder + */ + +%{ +/* Put headers and other declarations here that are needed for compilation */ +#include "ctobservation.hpp" +%} + + +/***********************************************************************//** + * @class ctobservation + * + * @brief Base class for likelihood tools + ***************************************************************************/ +class ctobservation : public ctool { + +public: + // Constructors and destructors + ctobservation(void); + ctobservation(const std::string& name, const std::string& version); + ctobservation(const std::string& name, const std::string& version, + const GObservations& obs); + ctobservation(const std::string& name, const std::string& version, + int argc, char* argv[]); + ctobservation(const ctobservation& app); + virtual ~ctobservation(void); + + // Pure virtual methods + virtual void clear(void) = 0; + virtual void run(void) = 0; + virtual void save(void) = 0; + + // Methods + const GObservations& obs(void) const; +}; + + +/***********************************************************************//** + * @brief Likelihood tool Python extensions + ***************************************************************************/ +%extend ctobservation { +} diff --git a/pyext/ctools/tools.i b/pyext/ctools/tools.i index 1b3fc82..56b7375 100755 --- a/pyext/ctools/tools.i +++ b/pyext/ctools/tools.i @@ -46,6 +46,7 @@ /* __ ctools base classes ________________________________________________ */ %include "ctool.i" +%include "ctobservation.i" %include "ctlikelihood.i" /* __ ctools _____________________________________________________________ */ diff --git a/src/cterror/cterror.cpp b/src/cterror/cterror.cpp index d7b1dad..8afce1b 100644 --- a/src/cterror/cterror.cpp +++ b/src/cterror/cterror.cpp @@ -190,6 +190,7 @@ void cterror::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) @@ -197,6 +198,7 @@ void cterror::clear(void) // Initialise members this->ctool::init_members(); + this->ctobservation::init_members(); this->ctlikelihood::init_members(); init_members(); diff --git a/src/ctlike/ctlike.cpp b/src/ctlike/ctlike.cpp index e03a43a..1aa18de 100644 --- a/src/ctlike/ctlike.cpp +++ b/src/ctlike/ctlike.cpp @@ -179,6 +179,7 @@ void ctlike::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) @@ -186,6 +187,7 @@ void ctlike::clear(void) // Initialise members this->ctool::init_members(); + this->ctobservation::init_members(); this->ctlikelihood::init_members(); init_members(); diff --git a/src/cttsmap/cttsmap.cpp b/src/cttsmap/cttsmap.cpp index b4e6165..ec8616d 100644 --- a/src/cttsmap/cttsmap.cpp +++ b/src/cttsmap/cttsmap.cpp @@ -182,6 +182,7 @@ 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) @@ -189,6 +190,7 @@ void cttsmap::clear(void) // Initialise members this->ctool::init_members(); + this->ctobservation::init_members(); this->ctlikelihood::init_members(); init_members(); diff --git a/src/ctulimit/ctulimit.cpp b/src/ctulimit/ctulimit.cpp index 0cde83e..4eddd9c 100644 --- a/src/ctulimit/ctulimit.cpp +++ b/src/ctulimit/ctulimit.cpp @@ -192,6 +192,7 @@ void ctulimit::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) @@ -199,6 +200,7 @@ void ctulimit::clear(void) // Initialise members this->ctool::init_members(); + this->ctobservation::init_members(); this->ctlikelihood::init_members(); init_members(); diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 4c00567..b6d8bda 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -1,5 +1,5 @@ ############################################################################# -# Makefile.am for ctool base class # +# Makefile.am for ctool base classes # # ------------------------------------------------------------------------- # # Copyright (C) 2016 Juergen Knoedlseder # # ------------------------------------------------------------------------- # @@ -20,7 +20,9 @@ ############################################################################# # Define sources for this directory -sources = ctool.cpp ctool.hpp ctlikelihood.cpp ctlikelihood.hpp +sources = ctool.cpp ctool.hpp \ + ctobservation.cpp ctobservation.hpp \ + ctlikelihood.cpp ctlikelihood.hpp # Build libtool library noinst_LTLIBRARIES = libsupport.la diff --git a/src/support/ctlikelihood.cpp b/src/support/ctlikelihood.cpp index 78341d3..285386d 100644 --- a/src/support/ctlikelihood.cpp +++ b/src/support/ctlikelihood.cpp @@ -28,15 +28,8 @@ #ifdef HAVE_CONFIG_H #include #endif -#include "GammaLib.hpp" -#include "GCTALib.hpp" #include "ctlikelihood.hpp" -//#include -//#include "ctlikelihood.hpp" -//#include "GTools.hpp" -//#include "GOptimizer.hpp" - /* __ Method name definitions ____________________________________________ */ #define G_EVALUATE "ctlikelihood::evaluate(GModelPar&, double&)" @@ -56,7 +49,7 @@ * * Constructs an empty likelihood tool. ***************************************************************************/ -ctlikelihood::ctlikelihood(void) : ctool() +ctlikelihood::ctlikelihood(void) : ctobservation() { // Initialise members init_members(); @@ -69,14 +62,15 @@ ctlikelihood::ctlikelihood(void) : ctool() /***********************************************************************//** * @brief Name constructor * - * @param[in] name Application name. - * @param[in] version Application version. + * @param[in] name Likelihood tool name. + * @param[in] version Likelihood tool version. * * Constructs a likelihood tool from the @p name and @p version. See the * equivalent ctool constructor for details. ***************************************************************************/ ctlikelihood::ctlikelihood(const std::string& name, - const std::string& version) : ctool(name, version) + const std::string& version) : + ctobservation(name, version) { // Initialise members init_members(); @@ -89,8 +83,8 @@ ctlikelihood::ctlikelihood(const std::string& name, /***********************************************************************//** * @brief Observations constructor * - * @param[in] name Application name. - * @param[in] version Application version. + * @param[in] name Likelihood tool name. + * @param[in] version Likelihood tool version. * param[in] obs Observation container. * * Constructs a likelihood tool from the @p name, @p version and an @@ -98,14 +92,12 @@ ctlikelihood::ctlikelihood(const std::string& name, ***************************************************************************/ ctlikelihood::ctlikelihood(const std::string& name, const std::string& version, - const GObservations& obs) : ctool(name, version) + const GObservations& obs) : + ctobservation(name, version, obs) { // Initialise members init_members(); - // Set observations - m_obs = obs; - // Return return; } @@ -114,8 +106,8 @@ ctlikelihood::ctlikelihood(const std::string& name, /***********************************************************************//** * @brief Command line constructor * - * @param[in] name Application name. - * @param[in] version Application version. + * @param[in] name Likelihood tool name. + * @param[in] version Likelihood tool version. * @param[in] argc Number of arguments in command line. * @param[in] argv Array of command line arguments. * @@ -125,7 +117,8 @@ ctlikelihood::ctlikelihood(const std::string& name, ctlikelihood::ctlikelihood(const std::string& name, const std::string& version, int argc, - char *argv[]) : ctool(name, version, argc, argv) + char *argv[]) : + ctobservation(name, version, argc, argv) { // Initialise members init_members(); @@ -138,12 +131,12 @@ ctlikelihood::ctlikelihood(const std::string& name, /***********************************************************************//** * @brief Copy constructor * - * @param[in] app Application. + * @param[in] app Likelihood tool. * * Constructs an instance of a likelihood tool by copying information from * another likelihood tool. ***************************************************************************/ -ctlikelihood::ctlikelihood(const ctlikelihood& app) : ctool(app) +ctlikelihood::ctlikelihood(const ctlikelihood& app) : ctobservation(app) { // Initialise members init_members(); @@ -159,7 +152,7 @@ ctlikelihood::ctlikelihood(const ctlikelihood& app) : ctool(app) /***********************************************************************//** * @brief Destructor * - * Destructs the ctlikelihood tool. + * Destructs the likelihood tool. ***************************************************************************/ ctlikelihood::~ctlikelihood(void) { @@ -180,8 +173,8 @@ ctlikelihood::~ctlikelihood(void) /***********************************************************************//** * @brief Assignment operator * - * @param[in] app Application. - * @return Application. + * @param[in] app Likelihood tool. + * @return Likelihood tool. * * Assigns a likelihood tool. ***************************************************************************/ @@ -191,7 +184,7 @@ ctlikelihood& ctlikelihood::operator=(const ctlikelihood& app) if (this != &app) { // Copy base class members - this->ctool::operator=(app); + this->ctobservation::operator=(app); // Free members free_members(); @@ -228,7 +221,6 @@ ctlikelihood& ctlikelihood::operator=(const ctlikelihood& app) void ctlikelihood::init_members(void) { // Initialise members - m_obs.clear(); m_opt.clear(); // Return @@ -239,12 +231,11 @@ void ctlikelihood::init_members(void) /***********************************************************************//** * @brief Copy class members * - * @param[in] app Application. + * @param[in] app Likelihood tool. ***************************************************************************/ void ctlikelihood::copy_members(const ctlikelihood& app) { // Copy members - m_obs = app.m_obs; m_opt = app.m_opt; // Return diff --git a/src/support/ctlikelihood.hpp b/src/support/ctlikelihood.hpp index 1e600ae..f899453 100644 --- a/src/support/ctlikelihood.hpp +++ b/src/support/ctlikelihood.hpp @@ -28,7 +28,7 @@ #define CTLIKELIHOOD_HPP /* __ Includes ___________________________________________________________ */ -#include "ctool.hpp" +#include "ctobservation.hpp" /* __Definitions _________________________________________________________ */ @@ -38,7 +38,7 @@ * * @brief Base class for likelihood tools ***************************************************************************/ -class ctlikelihood : public ctool { +class ctlikelihood : public ctobservation { public: // Constructors and destructors @@ -60,8 +60,7 @@ public: virtual void save(void) = 0; // Methods - const GObservations& obs(void) const; - const GOptimizer* opt(void) const; + const GOptimizer* opt(void) const; protected: // Protected methods @@ -71,26 +70,11 @@ protected: double evaluate(GModelPar& par, const double& value); // Protected members - GObservations m_obs; //!< Observation container - GOptimizerLM m_opt; //!< Optimizer + GOptimizerLM m_opt; //!< Optimizer }; /***********************************************************************//** - * @brief Return observation container - * - * @return Reference to observation container. - * - * Returns a reference to the observation container. - ***************************************************************************/ -inline -const GObservations& ctlikelihood::obs(void) const -{ - return m_obs; -} - - -/***********************************************************************//** * @brief Return optimizer * * @return Pointer to optimizer. diff --git a/src/support/ctobservation.cpp b/src/support/ctobservation.cpp new file mode 100644 index 0000000..e922f45 --- /dev/null +++ b/src/support/ctobservation.cpp @@ -0,0 +1,252 @@ +/*************************************************************************** + * ctobservation - Base class for observation tools * + * ----------------------------------------------------------------------- * + * copyright (C) 2016 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * 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 . * + * * + ***************************************************************************/ +/** + * @file ctobservation.cpp + * @brief Observation tool base class implementation + * @author Juergen Knoedlseder + */ + +/* __ Includes ___________________________________________________________ */ +#ifdef HAVE_CONFIG_H +#include +#endif +#include "ctobservation.hpp" + +/* __ Method name definitions ____________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + + +/*========================================================================== + = = + = Constructors/destructors = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Void constructor + * + * Constructs an empty observation tool. + ***************************************************************************/ +ctobservation::ctobservation(void) : ctool() +{ + // Initialise members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Name constructor + * + * @param[in] name Observation tool name. + * @param[in] version Observation tool version. + * + * Constructs a observation tool from the @p name and @p version. See the + * equivalent ctool constructor for details. + ***************************************************************************/ +ctobservation::ctobservation(const std::string& name, + const std::string& version) : ctool(name, version) +{ + // Initialise members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Observations constructor + * + * @param[in] name Observation tool name. + * @param[in] version Observation tool version. + * param[in] obs Observation container. + * + * Constructs a observation tool from the @p name, @p version and an + * observation container. + ***************************************************************************/ +ctobservation::ctobservation(const std::string& name, + const std::string& version, + const GObservations& obs) : ctool(name, version) +{ + // Initialise members + init_members(); + + // Set observations + m_obs = obs; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Command line constructor + * + * @param[in] name Observation tool name. + * @param[in] version Observation tool version. + * @param[in] argc Number of arguments in command line. + * @param[in] argv Array of command line arguments. + * + * Constructs a observation tool from the @p name, @p version and command + * line arguments. See the equivalent ctool constructor for details. + ***************************************************************************/ +ctobservation::ctobservation(const std::string& name, + const std::string& version, + int argc, + char *argv[]) : ctool(name, version, argc, argv) +{ + // Initialise members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy constructor + * + * @param[in] app Observation tool. + * + * Constructs an instance of a observation tool by copying information from + * another observation tool. + ***************************************************************************/ +ctobservation::ctobservation(const ctobservation& app) : ctool(app) +{ + // Initialise members + init_members(); + + // Copy members + copy_members(app); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Destructor + * + * Destructs the observation tool. + ***************************************************************************/ +ctobservation::~ctobservation(void) +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = Operators = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Assignment operator + * + * @param[in] app Observation tool. + * @return Observation tool. + * + * Assigns a observation tool. + ***************************************************************************/ +ctobservation& ctobservation::operator=(const ctobservation& app) +{ + // Execute only if object is not identical + if (this != &app) { + + // Copy base class members + this->ctool::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 = + = = + ==========================================================================*/ + + +/*========================================================================== + = = + = Private methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Initialise class members + ***************************************************************************/ +void ctobservation::init_members(void) +{ + // Initialise members + m_obs.clear(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy class members + * + * @param[in] app Observation tool. + ***************************************************************************/ +void ctobservation::copy_members(const ctobservation& app) +{ + // Copy members + m_obs = app.m_obs; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Delete class members + ***************************************************************************/ +void ctobservation::free_members(void) +{ + // Return + return; +} diff --git a/src/support/ctobservation.hpp b/src/support/ctobservation.hpp new file mode 100644 index 0000000..a63a4f5 --- /dev/null +++ b/src/support/ctobservation.hpp @@ -0,0 +1,92 @@ +/*************************************************************************** + * ctobservation - Base class for observation tools * + * ----------------------------------------------------------------------- * + * copyright (C) 2016 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * 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 . * + * * + ***************************************************************************/ +/** + * @file ctobservation.hpp + * @brief Observation tool base class interface definition + * @author Juergen Knoedlseder + */ + +#ifndef CTOBSERVATION_HPP +#define CTOBSERVATION_HPP + +/* __ Includes ___________________________________________________________ */ +#include "ctool.hpp" + +/* __Definitions _________________________________________________________ */ + + +/***********************************************************************//** + * @class ctobservation + * + * @brief Base class for observation tools + * + * This is the baseclass for observation tools. Observation tools are ctools + * that hold an observation container. + ***************************************************************************/ +class ctobservation : public ctool { + +public: + // Constructors and destructors + ctobservation(void); + ctobservation(const std::string& name, const std::string& version); + ctobservation(const std::string& name, const std::string& version, + const GObservations& obs); + ctobservation(const std::string& name, const std::string& version, + int argc, char* argv[]); + ctobservation(const ctobservation& app); + virtual ~ctobservation(void); + + // Operators + ctobservation& operator=(const ctobservation& app); + + // Pure virtual methods + virtual void clear(void) = 0; + virtual void run(void) = 0; + virtual void save(void) = 0; + + // Methods + const GObservations& obs(void) const; + +protected: + // Protected methods + void init_members(void); + void copy_members(const ctobservation& app); + void free_members(void); + + // Protected members + GObservations m_obs; //!< Observation container +}; + + +/***********************************************************************//** + * @brief Return observation container + * + * @return Reference to observation container. + * + * Returns a reference to the observation container. + ***************************************************************************/ +inline +const GObservations& ctobservation::obs(void) const +{ + return m_obs; +} + +#endif /* CTOBSERVATION_HPP */