Esempio n. 1
0
    def set_model(self, likelihood_model_instance):
        """
        Set the model to be used in the joint minimization. Must be a LikelihoodModel instance.
        """

        # Instance the python - C++ bridge

        self._model = likelihood_model_instance

        self._pymodel = pyToCppModelInterfaceCache()

        # Set boundaries for extended source
        # NOTE: we assume that these boundaries do not change during the fit

        for id in range(self._model.get_number_of_extended_sources()):

            (
                lon_min,
                lon_max,
                lat_min,
                lat_max,
            ) = self._model.get_extended_source_boundaries(id)

            self._pymodel.setExtSourceBoundaries(id, lon_min, lon_max, lat_min, lat_max)

        # Set positions for point source
        # NOTE: this should not change so much that the response is not valid anymore

        n_point_sources = self._model.get_number_of_point_sources()

        for id in range(n_point_sources):

            this_ra, this_dec = self._model.get_point_source_position(id)

            self._pymodel.setPtsSourcePosition(id, this_ra, this_dec)

        # Now init the HAWC LIFF software

        try:

            # Load all sky
            # (ROI will be defined later)

            if self._n_transits is None:

                self._theLikeHAWC = liff_3ML.LikeHAWC(
                    self._maptree,
                    self._response,
                    self._pymodel,
                    self._bin_list,
                    self._fullsky,
                )

            else:

                self._theLikeHAWC = liff_3ML.LikeHAWC(
                    self._maptree,
                    self._n_transits,
                    self._response,
                    self._pymodel,
                    self._bin_list,
                    self._fullsky,
                )

        except:

            print(
                "Could not instance the LikeHAWC class from LIFF. "
                + "Check that HAWC software is working"
            )

            raise

        else:

            self._instanced = True

        # If fullsky=True, the user *must* use one of the set_ROI methods

        if self._fullsky:

            if self._roi_ra is None and self._roi_fits is None:

                raise RuntimeError("You have to define a ROI with the setROI method")

        # Now if an ROI is set, try to use it

        if self._roi_ra is not None:

            if not isinstance(self._roi_ra, list):

                self._theLikeHAWC.SetROI(
                    self._roi_ra,
                    self._roi_dec,
                    self._roi_radius,
                    self._fixed_ROI,
                    self._roi_galactic,
                )

            elif len(self._roi_ra) == 2:

                self._theLikeHAWC.SetROI(
                    self._roi_ra[0],
                    self._roi_ra[1],
                    self._roi_dec[0],
                    self._roi_dec[1],
                    self._fixed_ROI,
                    self._roi_galactic,
                )

            elif len(self._roi_ra) > 2:

                self._theLikeHAWC.SetROI(
                    self._roi_ra, self._roi_dec, self._fixed_ROI, self._roi_galactic
                )

            else:

                raise RuntimeError(
                    "Only one point is found, use set_ROI(float ra, float dec, float radius, bool fixedROI, bool galactic)."
                )

        elif self._roi_fits is not None:

            self._theLikeHAWC.SetROI(
                self._roi_fits, self._roi_threshold, self._fixed_ROI
            )

        # Now set a callback in the CommonNorm parameter, so that if the user or the fit
        # engine or the Bayesian sampler change the CommonNorm value, the change will be
        # propagated to the LikeHAWC instance

        list(self._nuisance_parameters.values())[0].add_callback(
            self._CommonNormCallback
        )

        # Update to start the computation of positions and energies inside LiFF

        self._theLikeHAWC.UpdateSources()

        # Get the energies needed by LiFF (the same for all sources)
        # (note that the output is in MeV, while we need keV)

        self._energies = np.array(self._theLikeHAWC.GetEnergies(False)) * 1000.0

        # make sure the model maps etc. get filled
        self.get_log_like()
Esempio n. 2
0
    def set_model(self, likelihood_model_instance):
        """
        Set the model to be used in the joint minimization. Must be a LikelihoodModel instance.
        """

        # Instance the python - C++ bridge

        self._model = likelihood_model_instance

        self._pymodel = pyToCppModelInterfaceCache()

        # Set boundaries for extended source
        # NOTE: we assume that these boundaries do not change during the fit

        for id in range(self._model.get_number_of_extended_sources()):

            lon_min, lon_max, lat_min, lat_max = self._model.get_extended_source_boundaries(id)

            self._pymodel.setExtSourceBoundaries(id, lon_min, lon_max, lat_min, lat_max)

        # Set positions for point source
        # NOTE: this should not change so much that the response is not valid anymore

        n_point_sources = self._model.get_number_of_point_sources()

        for id in range(n_point_sources):

            this_ra, this_dec = self._model.get_point_source_position(id)

            self._pymodel.setPtsSourcePosition(id, this_ra, this_dec)

        # Now init the HAWC LIFF software

        try:

            # Load all sky
            # (ROI will be defined later)

            if self._n_transits is None:

                self._theLikeHAWC = liff_3ML.LikeHAWC(self._maptree,
                                                      self._response,
                                                      self._pymodel,
                                                      self._bin_list,
                                                      self._fullsky)

            else:

                self._theLikeHAWC = liff_3ML.LikeHAWC(self._maptree,
                                                      self._n_transits,
                                                      self._response,
                                                      self._pymodel,
                                                      self._bin_list,
                                                      self._fullsky)

        except:

            print("Could not instance the LikeHAWC class from LIFF. " +
                  "Check that HAWC software is working")

            raise

        else:

            self._instanced = True

        # If fullsky=True, the user *must* use one of the set_ROI methods

        if self._fullsky:

            if self._roi_ra is None and self._roi_fits is None:

                raise RuntimeError("You have to define a ROI with the setROI method")

        # Now if an ROI is set, try to use it

        if self._roi_ra is not None:

            if not isinstance(self._roi_ra, list):

                self._theLikeHAWC.SetROI(self._roi_ra, self._roi_dec, self._roi_radius, self._fixed_ROI,
                                         self._roi_galactic)

            elif len(self._roi_ra) == 2:

                self._theLikeHAWC.SetROI(self._roi_ra[0], self._roi_ra[1], self._roi_dec[0], self._roi_dec[1],
                                         self._fixed_ROI, self._roi_galactic)

            elif len(self._roi_ra) > 2:

                self._theLikeHAWC.SetROI(self._roi_ra, self._roi_dec, self._fixed_ROI, self._roi_galactic)

            else:

                raise RuntimeError(
                    "Only one point is found, use set_ROI(float ra, float dec, float radius, bool fixedROI, bool galactic).")

        elif self._roi_fits is not None:

            self._theLikeHAWC.SetROI(self._roi_fits, self._roi_threshold, self._fixed_ROI)

        # Now set a callback in the CommonNorm parameter, so that if the user or the fit
        # engine or the Bayesian sampler change the CommonNorm value, the change will be
        # propagated to the LikeHAWC instance

        self._nuisance_parameters.values()[0].add_callback(self._CommonNormCallback)

        # Update to start the computation of positions and energies inside LiFF

        self._theLikeHAWC.UpdateSources()

        # Get the energies needed by LiFF (the same for all sources)
        # (note that the output is in MeV, while we need keV)

        self._energies = np.array(self._theLikeHAWC.GetEnergies(False)) * 1000.0
        
        #make sure the model maps etc. get filled
        self.get_log_like()