cssens.py 41.3 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 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
#! /usr/bin/env python
# ==========================================================================
# Computes the array sensitivity using the Test Statistic for a test source
#
# Copyright (C) 2011-2021 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 <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import sys
import math
import gammalib
import ctools
from cscripts import obsutils
from cscripts import modutils
from cscripts import mputils


# ============ #
# cssens class #
# ============ #
class cssens(ctools.csobservation):
    """
    Computes the CTA sensitivity

    This class computes the CTA sensitivity for a number of energy bins using
    ctlike. Spectra are fitted in narrow energy bins to simulated data,
    and the flux level is determined that leads to a particular significance.

    The significance is determined using the Test Statistic, defined as twice
    the likelihood difference between fitting with and without the test source.
    """

    # Constructor
    def __init__(self, *argv):
        """
        Constructor
        """
        # Initialise application by calling the appropriate class constructor
        self._init_csobservation(self.__class__.__name__, ctools.__version__, argv)

        # Initialise class members
        self._ebounds     = gammalib.GEbounds()
        self._obs_ebounds = []
        self._fits        = None
        self._srcname     = ''
        self._ra          = None
        self._dec         = None
        self._log_clients = False
        self._models      = gammalib.GModels()
        self._seed        = 1
        self._nthreads    = 0

        # Return
        return

    # State methods por pickling
    def __getstate__(self):
        """
        Extend ctools.csobservation getstate method to include some members

        Returns
        -------
        state : dict
            Pickled instance
        """
        # Set pickled dictionary
        state = {'base'         : ctools.csobservation.__getstate__(self),
                 'ebounds'      : self._ebounds,
                 'obs_ebounds'  : self._obs_ebounds,
                 'fits'         : self._fits,
                 'srcname'      : self._srcname,
                 'ra'           : self._ra,
                 'dec'          : self._dec,
                 'log_clients'  : self._log_clients,
                 'models'       : self._models,
                 'seed'         : self._seed,
                 'nthreads'     : self._nthreads}

        # Return pickled dictionary
        return state

    def __setstate__(self, state):
        """
        Extend ctools.csobservation setstate method to include some members

        Parameters
        ----------
        state : dict
            Pickled instance
        """
        ctools.csobservation.__setstate__(self, state['base'])
        self._ebounds     = state['ebounds']
        self._obs_ebounds = state['obs_ebounds']
        self._fits        = state['fits']
        self._srcname     = state['srcname']
        self._ra          = state['ra']
        self._dec         = state['dec']
        self._log_clients = state['log_clients']
        self._models      = state['models']
        self._seed        = state['seed']
        self._nthreads    = state['nthreads']

        # Return
        return


    # Private methods
    def _get_parameters(self):
        """
        Get user parameters from parfile
        """
        # Set observation if not done before
        if self.obs().size() == 0:
            self.obs(self._set_obs(self['emin'].real(), self['emax'].real()))

        # Set observation statistic
        self._set_obs_statistic(gammalib.toupper(self['statistic'].string()))

        # Set models if we have none
        if self.obs().models().size() == 0:
            self.obs().models(self['inmodel'].filename())

        # Set source name and position
        self._set_source()

        # Read further parameters
        emin = self['emin'].real()
        emax = self['emax'].real()
        bins = self['bins'].integer()

        # Query parameters for binned if requested
        enumbins = self['enumbins'].integer()
        if not enumbins == 0:
            self['npix'].integer()
            self['binsz'].real()

        # Query input parameters
        self['sigma'].real()
        self['max_iter'].integer()
        self['type'].string()
        self['edisp'].boolean()
        self['debug'].boolean()
        self['mincounts'].integer()

        # Read seed
        self._seed = self['seed'].integer()

        # Derive some parameters
        self._ebounds = gammalib.GEbounds(bins,
                                          gammalib.GEnergy(emin, 'TeV'),
                                          gammalib.GEnergy(emax, 'TeV'))

        # Read ahead output parameters
        if self._read_ahead():
            self['outfile'].filename()

        #  Write input parameters into logger
        self._log_parameters(gammalib.TERSE)

        # Set number of processes for multiprocessing
        self._nthreads = mputils.nthreads(self)

        # Return
        return

    def _median(self, array):
        """
        Compute median value for an array

        Parameters
        ----------
        array : list of floats
            Array

        Returns
        -------
        median : float
            Median value of array
        """
        # Initialise median
        median = 0.0

        # Get number of elements in array
        n = len(array)

        # Continue only if there are elements in the array
        if n > 0:

            # Sort the array
            wrk_array = sorted(array)

            # Get median
            if n % 2 != 0:
                median = wrk_array[int(n/2)]
            else:
                median = (wrk_array[int((n-1)/2)] + wrk_array[int(n/2)]) / 2.0

        # Return median
        return median

    def _set_source(self):
        """
        Set source name and position
        """
        # Set source name
        self._srcname = self['srcname'].string()

        # Set source position. If the test source has no position then set the
        # source position to (RA,Dec)=(0,0)
        source = self.obs().models()[self._srcname]
        if source.has_par('RA') and source.has_par('DEC'):
            self._ra  = source['RA'].value()
            self._dec = source['DEC'].value()
        elif source.has_par('GLON') and source.has_par('GLAT'):
            glon      = source['GLON'].value()
            glat      = source['GLAT'].value()
            srcdir    = gammalib.GSkyDir()
            srcdir.lb_deg(glon, glat)
            self._ra  = srcdir.ra_deg()
            self._dec = srcdir.dec_deg()
        else:
            self._ra  = 0.0
            self._dec = 0.0

        # Return
        return

    def _set_obs(self, emin, emax):
        """
        Set an observation container

        Parameters
        ----------
        emin : float
            Minimum energy (TeV)
        emax : float
            Maximum energy (TeV)

        Returns
        -------
        obs : `~gammalib.GObservations`
            Observation container
        """
        # If an observation was provided on input then load it from XML file
        if self['inobs'].is_valid():
            obs = self._get_observations()

        # ... otherwise allocate a single observation using the test source
        # position as pointing direction, optionally offset by a certain
        # amount
        else:

            # Load models
            models = gammalib.GModels(self['inmodel'].filename())

            # Get test source
            source = models[self['srcname'].string()]

            # Set pointing direction to test source position. If test source
            # has no position then set the pointing to (RA,Dec)=(0,0)
            pntdir = gammalib.GSkyDir()
            if source.has_par('RA') and source.has_par('DEC'):
                pntdir.radec_deg(source['RA'].value(), source['DEC'].value())
            elif source.has_par('GLON') and source.has_par('GLAT'):
                pntdir.lb_deg(source['GLON'].value(), source['GLAT'].value())
            else:
                pntdir.radec_deg(0.0, 0.0)

            # Read other relevant user parameters
            caldb    = self['caldb'].string()
            irf      = self['irf'].string()
            deadc    = self['deadc'].real()
            duration = self['duration'].real()
            rad      = self['rad'].real()
            offset   = self['offset'].real()

            # Add offset to pointing direction
            pntdir.rotate_deg(0.0, offset)

            # Allocate observation container
            obs = gammalib.GObservations()

            # Create CTA observation
            run = obsutils.set_obs(pntdir, caldb=caldb, irf=irf,
                                   duration=duration, deadc=deadc,
                                   emin=emin, emax=emax, rad=rad)

            # Append observation to container
            obs.append(run)

        # Return observation container
        return obs

    def _set_obs_ebounds(self, emin, emax):
        """
        Set energy boundaries for all observations in container

        Parameters
        ----------
        emin : `~gammalib.GEnergy`
            Minimum energy
        emax : `~gammalib.GEnergy`
            Maximum energy
        """
        # Loop over all observations in container
        for i, obs in enumerate(self.obs()):

            # Get energy boundaries of the observation
            obs_ebounds = self._obs_ebounds[i]

            # Get minimum and maximum energy of the observation
            obs_emin = obs_ebounds.emin()
            obs_emax = obs_ebounds.emax()

            # If [emin,emax] is fully contained in the observation energy range
            # the use [emin,emax] as energy boundaries
            if obs_emin <= emin and obs_emax >= emax:
                ebounds = gammalib.GEbounds(emin, emax)

            # ... otherwise, if [emin,emax] is completely outside the
            # observation energy range then set the energy boundaries to the
            # zero-width interval [0,0]
            elif emax < obs_emin or emin > obs_emax:
                e0      = gammalib.GEnergy(0.0, 'TeV')
                ebounds = gammalib.GEbounds(e0, e0)

            # ... otherwise, if [emin,emax] overlaps partially with the
            # observation energy range then set the energy boundaries to the
            # overlapping part
            else:
                # Set overlapping energy range
                set_emin = max(emin, obs_emin)
                set_emax = min(emax, obs_emax)

                # Set energy boundaries
                ebounds = gammalib.GEbounds(set_emin, set_emax)

            # Set the energy boundaries as the boundaries of the observation
            obs.events().ebounds(ebounds)

        # Return
        return

    def _get_crab_flux(self, emin, emax):
        """
        Return Crab photon flux in a given energy interval

        Parameters
        ----------
        emin : `~gammalib.GEnergy`
            Minimum energy
        emax : `~gammalib.GEnergy`
            Maximum energy

        Returns
        -------
        flux : float
            Crab photon flux in specified energy interval (ph/cm2/s)
        """
        # Set Crab TeV spectral model based on a power law
        crab = gammalib.GModelSpectralPlaw(5.7e-16, -2.48,
                                           gammalib.GEnergy(0.3, 'TeV'))

        # Determine photon flux
        flux = crab.flux(emin, emax)

        # Return photon flux
        return flux

    def _get_sensitivity(self, emin, emax, test_model):
        """
        Determine sensitivity for given observations

        Parameters
        ----------
        emin : `~gammalib.GEnergy`
            Minimum energy for fitting and flux computation
        emax : `~gammalib.GEnergy`
            Maximum energy for fitting and flux computation
        test_model : `~gammalib.GModels`
            Test source model

        Returns
        -------
        result : dict
            Result dictionary
        """
        # Set TeV->erg conversion factor
        tev2erg = 1.6021764

        # Set parameters
        ts_thres = self['sigma'].real() * self['sigma'].real()
        max_iter = self['max_iter'].integer()
        enumbins = self['enumbins'].integer()
        if not enumbins == 0:
            npix  = self['npix'].integer()
            binsz = self['binsz'].real()
        else:
            npix  = 200
            binsz = 0.05

        # Keep track of the original value of edisp and switch-off energy
        # dispersion for the first iterations to speed-up the computations
        edisp_orig    = self['edisp'].boolean()
        self['edisp'] = False

        # Set flux ratio precision required for convergence to 5%
        ratio_precision = 0.05

        # Set energy boundaries
        self._set_obs_ebounds(emin, emax)

        # Determine mean energy for energy boundary
        e_mean   = math.sqrt(emin.TeV()*emax.TeV())
        loge     = math.log10(e_mean)
        erg_mean = e_mean * tev2erg

        # Compute Crab unit. This is the factor with which the Prefactor needs
        # to be multiplied to get 1 Crab.
        crab_flux = self._get_crab_flux(emin, emax)
        src_flux  = test_model[self._srcname].spectral().flux(emin, emax)
        crab_unit = crab_flux/src_flux

        # Initialise regression coefficient
        regcoeff = 0.0

        # Write header for energy bin
        self._log_string(gammalib.TERSE, '')
        self._log_header2(gammalib.TERSE, 'Energies: '+str(emin)+' - '+str(emax))

        # Write initial parameters
        self._log_header3(gammalib.TERSE, 'Initial parameters')
        self._log_value(gammalib.TERSE, 'Crab flux', str(crab_flux)+' ph/cm2/s')
        self._log_value(gammalib.TERSE, 'Source model flux', str(src_flux)+' ph/cm2/s')
        self._log_value(gammalib.TERSE, 'Crab unit factor', crab_unit)

        # Initialise loop
        results        = []
        iterations     = 0
        test_crab_flux = 0.1 # Initial test flux in Crab units (100 mCrab)

        # Write header for iterations for terse chatter level
        if self._logTerse():
            self._log_header3(gammalib.TERSE, 'Iterations')

        # Loop until we break
        while True:

            # Update iteration counter
            iterations += 1

            # Increment the seed value
            self._seed += 1

            # Write header for iteration into logger
            self._log_header2(gammalib.EXPLICIT, 'Iteration '+str(iterations))

            crab_prefactor = self._set_src_prefactor(test_model, crab_unit, test_crab_flux)

            # Simulate events for the models. "sim" holds an observation
            # container with observations containing the simulated events.
            sim = obsutils.sim(self.obs(), nbins=enumbins, seed=self._seed,
                               binsz=binsz, npix=npix,
                               log=self._log_clients,
                               debug=self['debug'].boolean(),
                               edisp=self['edisp'].boolean(),
                               nthreads=1)

            # Determine number of events in simulation
            nevents = sim.nobserved()

            # Write simulation results into logger
            self._log_header3(gammalib.EXPLICIT, 'Simulation')
            self._log_value(gammalib.EXPLICIT, 'Number of simulated events', nevents)

            # Fit test source to the simulated events in the observation
            # container
            fit = ctools.ctlike(sim)
            fit['edisp']    = self['edisp'].boolean()
            fit['nthreads'] = 1  # Avoids OpenMP conflict
            fit['debug']    = self['debug'].boolean()
            fit['chatter']  = self['chatter'].integer()
            fit.run()

            # Get model fitting results
            logL   = fit.opt().value()
            npred  = fit.obs().npred()
            models = fit.obs().models()
            source = models[self._srcname]
            ts     = source.ts()

            # Get fitted Crab, photon and energy fluxes
            prefactor   = modutils.normalisation_parameter(source)
            crab_flux   = prefactor.value() / crab_prefactor
            photon_flux = source.spectral().flux(emin, emax)
            energy_flux = source.spectral().eflux(emin, emax)

            # Compute differential sensitivity in unit erg/cm2/s by evaluating
            # the spectral model at the "e_mean" energy and by multipling the
            # result with the energy squared. Since the "eval()" method returns
            # an intensity in units of ph/cm2/s/MeV we multiply by 1.0e6 to
            # convert into ph/cm2/s/TeV, by "e_mean" to convert into ph/cm2/s,
            # and finally by "erg_mean" to convert to erg/cm2/s.
            energy      = gammalib.GEnergy(e_mean, 'TeV')
            sensitivity = source.spectral().eval(energy) * e_mean*erg_mean*1.0e6

            # Write fit results into logger
            name  = 'Iteration %d' % iterations
            value = ('TS=%10.4f  Sim=%9.4f mCrab  Fit=%9.4f mCrab  '
                     'Sens=%e erg/cm2/s' %
                     (ts, test_crab_flux*1000.0, crab_flux*1000.0, sensitivity))
            self._log_value(gammalib.TERSE, name, value)

            # If TS was non-positive then increase the test flux and start over
            if ts <= 0.0:

                # If the number of iterations was exceeded then stop
                if (iterations >= max_iter):
                    self._log_string(gammalib.TERSE,
                         ' Test ended after %d iterations.' % max_iter)
                    break

                # Increase test flux
                test_crab_flux *= 3.0

                # Signal start we start over
                self._log_string(gammalib.EXPLICIT,
                     'Non positive TS, increase test flux and start over.')

                # ... and start over
                continue

            # Append result entry to result list
            result = {'ts': ts, 'crab_flux': crab_flux,
                                'photon_flux': photon_flux,
                                'energy_flux': energy_flux}
            results.append(result)

            # Predict Crab flux at threshold TS using a linear regression of
            # the log(TS) and log(crab_flux) values that have so far been
            # computed. If not enough results are available then use a simple
            # TS scaling relation.
            correct = math.sqrt(ts_thres / ts)
            if len(results) > 1:
                try:
                    pred_crab_flux, regcoeff = self._predict_flux(results, ts_thres)
                    correct                  = pred_crab_flux / crab_flux
                except:
                    self._log_value(gammalib.TERSE, 'Skipping failed regression',
                                    'Retain simple TS scaling relation')

            # Compute extrapolated fluxes based on the flux correction factor
            crab_flux   = correct * crab_flux
            photon_flux = correct * photon_flux
            energy_flux = correct * energy_flux
            sensitivity = correct * sensitivity

            # If we have at least 3 results then check if the flux determination
            # at the TS threshold has converged
            if len(results) > 3:
                if test_crab_flux > 0:

                    # Compute fractional change in the Crab flux between two
                    # iterations
                    ratio = crab_flux/test_crab_flux

                    # If fractional change is smaller than the required position
                    # the iterations are stopped
                    if ratio > 1.0-ratio_precision and \
                       ratio < 1.0+ratio_precision:
                        value = ('TS=%10.4f  Sim=%9.4f mCrab                  '
                                 '     Sens=%e erg/cm2/s' %
                                 (ts, crab_flux*1000.0, sensitivity))
                        self._log_value(gammalib.TERSE, 'Converged result', value)
                        self._log_value(gammalib.TERSE, 'Converged flux ratio', ratio)
                        self._log_value(gammalib.TERSE, 'Regression coefficient',
                                        regcoeff)

                        # If the flux has converged then check if the original
                        # value of edisp was set, and is not what has fo far
                        # been used
                        if edisp_orig and not self['edisp'].boolean():

                            # Set edisp on and continue the calculation
                            self['edisp'] = True

                            # Log action
                            self._log_value(gammalib.TERSE, 'Converged result',
                                            'Now use energy dispersion after '
                                            'initial convergence without it')

                        # ... otherwise finish the calculation after convergence
                        else:
                            break

                # ... otherwise break with a zero flux
                else:
                    self._log_value(gammalib.TERSE, 'Not converged', 'Flux is zero')
                    break

            # Set test flux for next iteration
            test_crab_flux = crab_flux

            # Exit loop if number of trials exhausted
            if (iterations >= max_iter):
                self._log_string(gammalib.TERSE,
                                 ' Test ended after %d iterations.' % max_iter)
                break

        # Write header for iterations for terse chatter level
        if self._logTerse():
            self._log_header3(gammalib.TERSE, 'Iterations for source counts cuts')

        # Recover original energy dispersion setting
        self['edisp'] = edisp_orig

        # Take into account the excess event cuts
        n_bck_evts = None
        for iterations in range(max_iter):

            # Simulate event excess
            pass_evt_cut, n_bck_evts = self._sim_evt_excess(enumbins=enumbins,
                                                            binsz=binsz,
                                                            npix=npix,
                                                            n_bck_evts=n_bck_evts)

            # Write results into logger
            name  = 'Iteration %d' % iterations
            value = 'Fit=%9.4f mCrab  Sens=%e erg/cm2/s' % (crab_flux*1000.0, sensitivity)
            self._log_value(gammalib.TERSE, name, value)

            # If event excess cuts are passed then break now
            if pass_evt_cut:
                break

            # ... otherwise increase the test flux
            correct     = 1.0 + ratio_precision
            crab_flux   = correct * crab_flux
            photon_flux = correct * photon_flux
            energy_flux = correct * energy_flux
            sensitivity = correct * sensitivity

            # ...
            _ = self._set_src_prefactor(test_model, crab_unit, crab_flux)

        # Write fit results into logger
        self._log_header3(gammalib.TERSE, 'Fit results')
        self._log_value(gammalib.TERSE, 'Photon flux',
                        str(photon_flux)+' ph/cm2/s')
        self._log_value(gammalib.TERSE, 'Energy flux',
                        str(energy_flux)+' erg/cm2/s')
        self._log_value(gammalib.TERSE, 'Crab flux',
                        str(crab_flux*1000.0)+' mCrab')
        self._log_value(gammalib.TERSE, 'Differential sensitivity',
                        str(sensitivity)+' erg/cm2/s')
        self._log_value(gammalib.TERSE, 'Number of simulated events', nevents)
        self._log_header3(gammalib.TERSE, 'Test source model fitting')
        self._log_value(gammalib.TERSE, 'log likelihood', logL)
        self._log_value(gammalib.TERSE, 'Number of predicted events', npred)
        for model in models:
            self._log_value(gammalib.TERSE, 'Model', model.name())
            for par in model:
                self._log_string(gammalib.TERSE, str(par))

        # Restore energy boundaries of observation container
        for i, obs in enumerate(self.obs()):
            obs.events().ebounds(self._obs_ebounds[i])

        # Store result
        result = {'loge': loge, 'emin': emin.TeV(), 'emax': emax.TeV(), \
                  'crab_flux': crab_flux, 'photon_flux': photon_flux, \
                  'energy_flux': energy_flux, \
                  'sensitivity': sensitivity, 'regcoeff': regcoeff, \
                  'nevents': nevents, 'npred': npred}

        # Return result
        return result

    def _set_src_prefactor(self, test_model, crab_unit, test_crab_flux):
        """
        Create a copy of the test models, set the normalisation parameter
        of the test source in the models, and append the models to the observation.

        Parameters
        ----------
        test_model : `~gammalib.GModels`
            Test source model
        crab_unit : float
            Crab unit factor
        test_crab_flux : float
            Test flux in Crab units (100 mCrab)

        Returns
        -------
        crab_prefactor : float
            the prefactor that corresponds to a flux of 1 Crab.
        """
        # Initialise variables
        models         = test_model.copy()
        prefactor      = modutils.normalisation_parameter(models[self._srcname])
        crab_prefactor = prefactor.value() * crab_unit
        val_margin     = 0.01
        min_pref       = prefactor.min() * (1.0 + val_margin)
        max_pref       = prefactor.max() * (1.0 - val_margin)
        val_now        = crab_prefactor * test_crab_flux

        # Check whether prefactor is in valid range
        if val_now < min_pref or val_now > max_pref:

            # Store old prefactor value for logging
            val_old = val_now

            # Set new prefactor value
            val_now        = max(val_now, min_pref)
            val_now        = min(val_now, max_pref)
            crab_prefactor = val_now / test_crab_flux

            # Log prefactor modification
            self._log_value(gammalib.EXPLICIT, 'Prefactor range',
                            '['+str(min_pref)+','+str(max_pref)+']')
            self._log_value(gammalib.EXPLICIT, 'Initial Prefactor', val_old)
            self._log_value(gammalib.EXPLICIT, 'Updated Prefactor', val_now)

        # Store prefactor value
        prefactor.value(val_now)

        # Update the models
        self.obs().models(models)

        # Return Prefactor
        return crab_prefactor

    def _simulate_events(self, obs, rad, enumbins, binsz, npix):
        """
        Simulate events

        Parameters
        ----------
        obs : `~gammalib.GObservations`
            Observation container
        rad : float
            Simulation selection radius (degrees)
        enumbins : integer
            Number of energy bins
        binsz : float
            Pixel size
        npix : integer
            Number of pixels
        """
        # Initialise list of number of simulated events
        n_sim_evts = []

        # Simulate events
        for n_sim in range(15):

            # Increment the seed value
            self._seed += 1

            # Simulate observations
            sim = obsutils.sim(obs, nbins=enumbins, seed=self._seed, binsz=binsz,
                               npix=npix, log=self._log_clients,
                               debug=self['debug'].boolean(),
                               edisp=self['edisp'].boolean(), nthreads=1)

            # If a selection radius is given then select only the events within
            # that selection radius
            if rad > 0.0:

                # Run event selection tool
                select = ctools.ctselect(sim)
                select['rad']  = rad
                select['emin'] = 'INDEF'
                select['tmin'] = 'INDEF'
                select.run()

                # Store number of events
                n_sim_evts.append(select.obs().nobserved())

            # ... otherwise we simply store the number of simulated events
            else:
                n_sim_evts.append(sim.nobserved())

        # Determine median number of events
        median = self._median(n_sim_evts)

        # Log results
        if rad > 0.0:
            name = 'Median source events'
        else:
            name = 'Median background events'
        self._log_value(gammalib.EXPLICIT, name, median)

        # Return
        return median

    def _sim_evt_excess(self, enumbins, binsz, npix, n_bck_evts):
        """
        Return the number of excess events for the source model, compared
        to all other models

        Parameters
        ----------
        enumbins : int
            Number of bins for the observation simulation
        binsz : float
            Bin size for the observation simulation
        npix : int
            Pixel size for the observation simulation
        n_bck_evts : int
            Number of background counts from previous call

        Returns
        -------
        pass_evt_cut : bool
            Signals that the source passes the minimal excess criteria
        n_bck_evts : int
            Number of background counts
        """
        # Initialise results
        pass_evt_cut = True
        n_bck_evts   = 0

        # Get user parameters
        mincounts = self['mincounts'].integer()
        bkgexcess = self['bkgexcess'].real()
        bkgrad    = self['bkgrad'].real()

        # Continue only if cuts were specified
        if mincounts > 0 or bkgexcess > 0.0:

            # Get models and split them into source and background
            models     = self.obs().models()
            src_model  = gammalib.GModels()
            bck_models = gammalib.GModels()
            for model in models:
                if model.name() == self._srcname:
                    src_model.append(model)
                else:
                    bck_models.append(model)

            # Create observations for source and background
            src_obs = self.obs().copy()
            bck_obs = self.obs().copy()
            src_obs.models(src_model)
            bck_obs.models(bck_models)

            # Simulate source events
            n_src_evts = self._simulate_events(src_obs, 0.0, enumbins, binsz, npix)

            # Simulate background events in case that a background fraction cut
            # is applied and if the number of background events was not yet
            # estimated
            if bkgexcess > 0.0 and n_bck_evts == None:
                n_bck_evts = self._simulate_events(bck_obs, bkgrad, enumbins, binsz, npix)

            # Set cut results
            min_bkg_events = n_bck_evts * bkgexcess
            has_min_evts   = n_src_evts >= mincounts
            is_above_bck   = n_src_evts >= min_bkg_events
            pass_evt_cut   = has_min_evts and is_above_bck

            # Log results
            self._log_value(gammalib.EXPLICIT, 'Pass minimum counts cut', has_min_evts)
            self._log_value(gammalib.EXPLICIT, 'Pass background cut', is_above_bck)
            self._log_value(gammalib.EXPLICIT, 'Pass event cut', pass_evt_cut)
            self._log_value(gammalib.EXPLICIT, 'Minimum counts threshold', mincounts)
            self._log_value(gammalib.EXPLICIT, 'Background threshold', min_bkg_events)
            self._log_value(gammalib.EXPLICIT, 'Source events', n_src_evts)
            self._log_value(gammalib.EXPLICIT, 'Background events', n_bck_evts)

        # Return passing flag and number of background events
        return pass_evt_cut, n_bck_evts

    def _predict_flux(self, results, ts):
        """
        Predict Crab flux for a given TS value

        The Crab flux at a given Test Statistic value is predicted by doing a
        linear regression of the log(TS) and log(crab_flux) values in a results
        list.

        See https://en.wikipedia.org/wiki/Simple_linear_regression

        Parameters
        ----------
        results : list of dict
            List of results
        ts : float
            Test Statistic value

        Returns
        -------
        crab_flux_prediction, regcoeff : tuple of float
            Predicted Crab flux and regression coefficient
        """
        # Compute means and regression coefficient
        mean_x  = 0.0
        mean_y  = 0.0
        mean_xy = 0.0
        mean_xx = 0.0
        mean_yy = 0.0
        for result in results:
            x        = math.log(float(result['ts']))
            y        = math.log(float(result['crab_flux']))
            mean_x  += x
            mean_y  += y
            mean_xy += x * y
            mean_xx += x * x
            mean_yy += y * y
        norm     = 1.0 / float(len(results))
        mean_x  *= norm
        mean_y  *= norm
        mean_xy *= norm
        mean_xx *= norm
        mean_yy *= norm

        # Compute regression coefficient
        rxy_norm = (mean_xx - mean_x * mean_x) * (mean_yy - mean_y * mean_y)
        if rxy_norm < 1e-10:
            rxy_norm = 1
        else:
            rxy_norm = math.sqrt(rxy_norm)
        rxy      = (mean_xy - mean_x * mean_y) / rxy_norm
        regcoeff = rxy*rxy

        # Compute regression line slope
        beta_nom   = 0.0
        beta_denom = 0.0
        for result in results:
            x           = math.log(float(result['ts']))
            y           = math.log(float(result['crab_flux']))
            beta_nom   += (x - mean_x) * (y - mean_y)
            beta_denom += (x - mean_x) * (x - mean_x)
        beta = beta_nom / beta_denom

        # Compute regression line offset
        alpha = mean_y - beta * mean_x

        # Predict Crab flux at TS threshold
        log_ts_thres         = math.log(ts)
        crab_flux_prediction = math.exp(alpha + beta * log_ts_thres)

        # Return
        return (crab_flux_prediction, regcoeff)

    def _e_bin(self, ieng):
        """
        Determines sensivity in energy bin

        Parameters
        ----------
        ieng : int
            Energy bin number

        Returns
        -------
        result : dict
            Result dictionary
        """
        # Get sensitivity type
        sensitivity_type = self['type'].string()

        # Set energies
        if sensitivity_type == 'Differential':
            emin = self._ebounds.emin(ieng)
            emax = self._ebounds.emax(ieng)
        elif sensitivity_type == 'Integral':
            emin = self._ebounds.emin(ieng)
            emax = self._ebounds.emax()
        else:
            msg = ('Invalid sensitivity type "%s" encountered. Either '
                   'specify "Differential" or "Integral".' %
                   sensitivity_type)
            raise RuntimeError(msg)

        # Determine sensitivity
        result = self._get_sensitivity(emin, emax, self._models)

        # Return results
        return result

    def _create_fits(self, results):
        """
        Create FITS file from results

        Parameters
        ----------
        results : list of dict
            List of result dictionaries
        """
        # Create FITS table columns
        nrows        = len(results)
        e_mean       = gammalib.GFitsTableDoubleCol('E_MEAN', nrows)
        e_min        = gammalib.GFitsTableDoubleCol('E_MIN', nrows)
        e_max        = gammalib.GFitsTableDoubleCol('E_MAX', nrows)
        flux_crab    = gammalib.GFitsTableDoubleCol('FLUX_CRAB', nrows)
        flux_photon  = gammalib.GFitsTableDoubleCol('FLUX_PHOTON', nrows)
        flux_energy  = gammalib.GFitsTableDoubleCol('FLUX_ENERGY', nrows)
        sensitivity  = gammalib.GFitsTableDoubleCol('SENSITIVITY', nrows)
        regcoeff     = gammalib.GFitsTableDoubleCol('REGRESSION_COEFF', nrows)
        nevents      = gammalib.GFitsTableDoubleCol('NEVENTS', nrows)
        npred        = gammalib.GFitsTableDoubleCol('NPRED', nrows)
        e_mean.unit('TeV')
        e_min.unit('TeV')
        e_max.unit('TeV')
        flux_crab.unit('')
        flux_photon.unit('ph/cm2/s')
        flux_energy.unit('erg/cm2/s')
        sensitivity.unit('erg/cm2/s')
        regcoeff.unit('')
        nevents.unit('counts')
        npred.unit('counts')

        # Fill FITS table columns
        for i, result in enumerate(results):
            e_mean[i]      = math.pow(10.0, result['loge'])
            e_min[i]       = result['emin']
            e_max[i]       = result['emax']
            flux_crab[i]   = result['crab_flux']
            flux_photon[i] = result['photon_flux']
            flux_energy[i] = result['energy_flux']
            sensitivity[i] = result['sensitivity']
            regcoeff[i]    = result['regcoeff']
            nevents[i]     = result['nevents']
            npred[i]       = result['npred']

        # Initialise FITS Table with extension "SENSITIVITY"
        table = gammalib.GFitsBinTable(nrows)
        table.extname('SENSITIVITY')

        # Add keywors for compatibility with gammalib.GMWLSpectrum
        table.card('INSTRUME', 'CTA', 'Name of Instrument')
        table.card('TELESCOP', 'CTA', 'Name of Telescope')

        # Stamp header
        self._stamp(table)

        # Add script keywords
        table.card('OBJECT',   self._srcname, 'Source for which sensitivity was estimated')
        table.card('TYPE',     self['type'].string(), 'Sensitivity type')
        table.card('SIGMA',    self['sigma'].real(), '[sigma] Sensitivity threshold')
        table.card('MAX_ITER', self['max_iter'].integer(), 'Maximum number of iterations')
        table.card('STAT',     self['statistic'].string(), 'Optimization statistic')
        table.card('MINCOUNT', self['mincounts'].integer(), 'Minimum number of source counts')
        table.card('BKGEXCES', self['bkgexcess'].real(), 'Background uncertainty fraction')
        table.card('BKGRAD',   self['bkgrad'].real(), '[deg] Background radius')
        table.card('SEED',     self['seed'].integer(), 'Seed value for random numbers')

        # Append filled columns to fits table
        table.append(e_mean)
        table.append(e_min)
        table.append(e_max)
        table.append(flux_crab)
        table.append(flux_photon)
        table.append(flux_energy)
        table.append(sensitivity)
        table.append(regcoeff)
        table.append(nevents)
        table.append(npred)

        # Create the FITS file now
        self._fits = gammalib.GFits()
        self._fits.append(table)

        # Return
        return


    # Public methods
    def run(self):
        """
        Run the script
        """
        # Switch screen logging on in debug mode
        if self._logDebug():
            self._log.cout(True)

        # Get parameters
        self._get_parameters()

        # Loop over observations and store a deep copy of the energy
        # boundaries for later use
        for obs in self.obs():
            self._obs_ebounds.append(obs.events().ebounds().copy())

        # Initialise results
        results  = []

        # Set test source model for this observation
        self._models = modutils.test_source(self.obs().models(), self._srcname,
                                            ra=self._ra, dec=self._dec)

        # Write observation into logger
        self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation')

        # Write models into logger
        self._log_models(gammalib.NORMAL, self._models, 'Input model')

        # Write header
        self._log_header1(gammalib.TERSE, 'Sensitivity determination')
        self._log_value(gammalib.TERSE, 'Type', self['type'].string())

        # If using multiprocessing
        if self._nthreads > 1 and self._ebounds.size() > 1:

            # Compute energy bins
            args        = [(self, '_e_bin', i)
                           for i in range(self._ebounds.size())]
            poolresults = mputils.process(self._nthreads, mputils.mpfunc, args)

            # Construct results
            for ieng in range(self._ebounds.size()):
                results.append(poolresults[ieng][0])
                self._log_string(gammalib.TERSE, poolresults[ieng][1]['log'], False)

        # Otherwise, loop over energy bins
        else:
            for ieng in range(self._ebounds.size()):

                # Run analysis in energy bin
                result = self._e_bin(ieng)

                # Append results
                results.append(result)

        # Create FITS file
        self._create_fits(results)

        # Return
        return

    def save(self):
        """
        Save sensitivity FITS file
        """
        # Write header
        self._log_header1(gammalib.TERSE, 'Save sensitivity curve')

        # Continue only if FITS file is valid
        if self._fits != None:

            # Get outmap parameter
            outfile = self['outfile'].filename()

            # Log file name
            self._log_value(gammalib.NORMAL, 'Sensitivity file', outfile.url())

            # Save sensitivity
            self._fits.saveto(outfile, self['clobber'].boolean())

        # Return
        return

    def sensitivity(self):
        """
        Return sensitivity FITS file

        Returns:
            FITS file containing sensitivity curve
        """
        # Return
        return self._fits


# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':

    # Create instance of application
    app = cssens(sys.argv)

    # Run application
    app.execute()