make_counts_off in gammapy/makers/background/reflected.py to use the aforementioned function in makers/utils
@@ -8,7 +8,7 @@ from regions import PixCoord, PointSkyRegion, CircleSkyRegion
from gammapy.datasets import SpectrumDatasetOnOff
from gammapy.maps import RegionGeom, RegionNDMap, WcsGeom, WcsNDMap
from ..core import Maker
-from ..utils import get_rad_max_vs_energy
+from ..utils import make_counts_off_rad_max
__all__ = ["ReflectedRegionsFinder", "ReflectedRegionsBackgroundMaker"]
@@ -293,7 +293,21 @@ class ReflectedRegionsBackgroundMaker(Maker):
counts_off : `~gammapy.maps.RegionNDMap`
Counts vs estimated energy extracted from the OFF regions.
"""
- if isinstance(dataset.counts.geom.region, CircleSkyRegion):
+ if dataset.counts.geom.is_region and isinstance(
+ dataset.counts.geom.region, PointSkyRegion
+ ):
+ counts_off, acceptance_off = make_counts_off_rad_max(
+ geom=dataset.counts.geom,
+ observation=observation,
+ binsz=self.binsz,
+ exclusion_mask=self.exclusion_mask,
+ min_distance=self.min_distance,
+ min_distance_input=self.min_distance_input,
+ max_region_number=self.max_region_number,
+ angle_increment=self.angle_increment,
+ )
+
+ else:
finder = self._get_finder(dataset, observation)
regions = finder.run()
@@ -316,57 +330,6 @@ class ReflectedRegionsBackgroundMaker(Maker):
counts_off = None
acceptance_off = RegionNDMap.from_geom(geom=dataset._geom, data=0)
- if isinstance(dataset.counts.geom.region, PointSkyRegion):
- rad_max = get_rad_max_vs_energy(dataset.counts.geom, observation)
-
- counts_off_list = []
- acceptance_off_list = []
- events = observation.events
-
- # we have to define an ON region and a finder for each energy bin
- for i, rad in enumerate(rad_max):
-
- on_region = CircleSkyRegion(center=dataset.counts.geom.center_skydir, radius=rad)
- energy_range = dataset.counts.geom.axes["energy"].slice(i)
-
- finder = ReflectedRegionsFinder(
- binsz=self.binsz,
- exclusion_mask=self.exclusion_mask,
- center=observation.pointing_radec,
- region=on_region,
- min_distance=self.min_distance,
- min_distance_input=self.min_distance_input,
- max_region_number=self.max_region_number,
- angle_increment=self.angle_increment,
- )
- regions = finder.run()
-
- if len(regions) > 0:
- geom = RegionGeom.from_regions(
- regions=regions, axes=[energy_range], wcs=finder.geom_ref.wcs
- )
- counts_off = RegionNDMap.from_geom(geom=geom)
- counts_off.fill_events(events)
- counts_off_list.append(counts_off.data[0])
-
- else:
- log.warning(
- f"ReflectedRegionsBackgroundMaker failed in estimated energy bin {i}."
- )
- counts_off_list.append(0)
-
- acceptance_off_list.append(len(regions))
-
- # create the final arrays with the OFF counts and the acceptance
- counts_off = RegionNDMap.from_geom(
- geom=dataset.counts.geom,
- data=np.asarray(counts_off_list)
- )
- acceptance_off = RegionNDMap.from_geom(
- geom=dataset.counts.geom,
- data=np.asarray(acceptance_off_list)
- )
-
return counts_off, acceptance_off
def run(self, dataset, observation):
