Exemple #1
0
def getPointSource(like):
    '''This function creats a test source object for use in the likelihood
    analysis at a specific pixel.'''

    try:
        test_src = pyLike.PointSource(0, 0, like.observation.observation)
    except AttributeError:
        test_src = pyLike.PointSource(0, 0, like.binnedData.observation)
    pl = pyLike.SourceFactory_funcFactory().create('PowerLaw')
    pl.setParamValues((1, -2, 100))
    indexPar = pl.getParam('Index')
    indexPar.setBounds(-3.5, -1)
    pl.setParam(indexPar)
    prefactor = pl.getParam('Prefactor')
    prefactor.setBounds(1e-10, 1e3)
    prefactor.setScale(1e-9)
    pl.setParam(prefactor)
    test_src.setSpectrum(pl)
    return test_src
    def __init__(self, pylike_object, test_source=None, target="GRB"):
        """

        :param pylike_object: a UnbinnedAnalysis.UnbinnedAnalysis object containing a source in the center
        """

        self._pylike_object = pylike_object

        logLike = self._pylike_object.logLike

        # Remove target source (will be replaced with the test source)
        self._target_source = logLike.deleteSource(target)

        ra = self._pylike_object.model[target]['Position']['RA']
        dec = self._pylike_object.model[target]['Position']['DEC']

        # Generate a test source (and compute the exposure, since we provide the observation in the constructor)
        if test_source is None:

            # This is quite slow, so if you can, provide the test source already

            self._test_source = pyLikelihood.PointSource(
                ra, dec, logLike.observation())
            self._test_source.setSpectrum(
                self._target_source.spectrum().clone())
            self._test_source.setName("_test_source")

        else:

            self._test_source = test_source

        # compute the value for the likelihood without the point source

        # Optimize (i.e., fit) the model without the point source
        self._pylike_object.optimize(verbosity=0)
        logLike0 = logLike.value()

        # Store it
        self._logLike0 = float(logLike0)

        # Save the values for the free parameters in the model without the point source
        self._nullhyp_best_fit_param = pyLikelihood.DoubleVector()
        # This is a C++ call-by-reference, so self._nullhyp_best_fit_param will be changed
        logLike.getFreeParamValues(self._nullhyp_best_fit_param)
    def process_ft1s(self, ft1s, ra_center, dec_center):

        # Get the TSs
        tss = np.zeros(len(ft1s))

        log.info("Computing TSs...")

        start_time = time.time()

        # Create a test source outside of the loop, so the exposure is computed only here (and not for every
        # ft1 file). This can be done because all ft1s are assumed to be a simulation of the
        # same ROI in the same interval, so they share the same livetime cube and exposure map

        test_source = pyLike.PointSource(
            ra_center, dec_center, self._orig_log_like.logLike.observation())
        test_source.setSpectrum(
            self._orig_log_like[self._target].spectrum().clone())
        test_source.setName("_test_source")

        for i, ft1 in enumerate(ft1s):

            tss[i] = self.get_TS(ft1,
                                 ra_center,
                                 dec_center,
                                 test_source=test_source)

            if (i + 1) % int(math.ceil(len(ft1s) / 100.0)) == 0:

                this_time = time.time()
                elapsed_time = (this_time - start_time)
                time_per_ts = elapsed_time / (i + 1)
                remaining_time = (len(ft1s) - (i + 1)) * time_per_ts

                log.info("Processed %i out of %i" % (i + 1, len(ft1s)))
                log.info("Elapsed time: %s, remaining time: %s" %
                         (datetime.timedelta(seconds=elapsed_time),
                          datetime.timedelta(seconds=remaining_time)))

        log.info("done")

        return tss