Repository: gammapy/gammapy

Issue:
Raise an error when passing an energy dependent exclusion map to `ReflectedRegionsBackgroundMaker`
If one pass an energy dependent exclusion map to the `ReflectedRegionsBackgroundMaker`, the error that is raised is really unclear (see picture below). We should add a check on the maker class to check whether the exclusion map contains an energy axis (or any other axis actually), and raise an explicit error to the user. 

![Capture d’écran 2024-03-29 à 14 05 28](https://github.com/gammapy/gammapy/assets/108189156/8abdb0db-f5ce-4181-98ec-0fbf3909325e)



Discussion:

Resolving patch:
diff --git a/gammapy/makers/background/reflected.py b/gammapy/makers/background/reflected.py
--- a/gammapy/makers/background/reflected.py
+++ b/gammapy/makers/background/reflected.py
@@ -436,7 +436,8 @@ class ReflectedRegionsBackgroundMaker(Maker):
         If not given, a `ReflectedRegionsFinder` will be created and
         any of the ``**kwargs`` will be forwarded to the `ReflectedRegionsFinder`.
     exclusion_mask : `~gammapy.maps.WcsNDMap`, optional
-        Exclusion mask.
+        Exclusion mask. The map must contain at max one non-spatial dimension, and this
+        dimension must be one bin.
     """
 
     tag = "ReflectedRegionsBackgroundMaker"
@@ -451,6 +452,13 @@ def __init__(
         if exclusion_mask and not exclusion_mask.is_mask:
             raise ValueError("Exclusion mask must contain boolean values")
 
+        if exclusion_mask and not exclusion_mask.geom.is_flat:
+            raise ValueError("Exclusion mask must only contain spatial dimension")
+
+        if exclusion_mask:
+            exclusion_mask = exclusion_mask.sum_over_axes(keepdims=False)
+            exclusion_mask.data = exclusion_mask.data.astype("bool")
+
         self.exclusion_mask = exclusion_mask
 
         if region_finder is None:


Test patch:
diff --git a/gammapy/makers/background/tests/test_reflected.py b/gammapy/makers/background/tests/test_reflected.py
--- a/gammapy/makers/background/tests/test_reflected.py
+++ b/gammapy/makers/background/tests/test_reflected.py
@@ -21,7 +21,7 @@
     SpectrumDatasetMaker,
     WobbleRegionsFinder,
 )
-from gammapy.maps import MapAxis, RegionGeom, WcsGeom
+from gammapy.maps import Map, MapAxis, RegionGeom, WcsGeom
 from gammapy.utils.regions import compound_region_to_regions
 from gammapy.utils.testing import assert_quantity_allclose, requires_data
 
@@ -456,3 +456,24 @@ def test_reflected_bkg_maker_fixed_rad_max_bad(
     dataset = maker.run(dataset_empty, obs)
     with pytest.raises(TypeError):
         reflected_bkg_maker.run(dataset, obs)
+
+
+def test_reflected_bkg_exclsion_error(exclusion_mask):
+
+    energy = MapAxis.from_energy_bounds(1 * u.TeV, 2 * u.TeV, nbin=1, name="energy")
+
+    exclusion_cube = exclusion_mask.to_cube([energy])
+    ReflectedRegionsBackgroundMaker(exclusion_mask=exclusion_cube)
+
+    energy = MapAxis.from_energy_bounds(2 * u.TeV, 3 * u.TeV, nbin=1, name="energy")
+    exclusion_cube_2 = exclusion_mask.to_cube([energy])
+
+    exclusion_stack = Map.from_stack([exclusion_cube, exclusion_cube_2])
+
+    with pytest.raises(ValueError):
+        ReflectedRegionsBackgroundMaker(exclusion_mask=exclusion_stack)
+
+    exclusion_mask.data = exclusion_mask.data.astype("int")
+
+    with pytest.raises(ValueError):
+        ReflectedRegionsBackgroundMaker(exclusion_mask=exclusion_mask)