From aa447ebf3f48995224e89e7594a99cbad359d072 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (l)" <j.wuttke@fz-juelich.de>
Date: Wed, 16 Sep 2020 11:18:59 +0200
Subject: [PATCH] Core: unify local variable names detector / det

---
 Core/Export/SimulationToPython.cpp     | 77 +++++++++++++-------------
 Core/Instrument/Instrument.cpp         |  6 --
 Core/Instrument/Instrument.h           |  1 -
 Core/Simulation/OffSpecSimulation.cpp  | 19 +++----
 Core/Simulation/Simulation2D.cpp       |  6 +-
 Core/Simulation/SpecularSimulation.cpp |  8 +--
 Core/Simulation/UnitConverterUtils.cpp | 10 ++--
 auto/Wrap/doxygen_core.i               |  3 -
 auto/Wrap/libBornAgainCore.py          |  5 +-
 auto/Wrap/libBornAgainCore_wrap.cpp    | 66 ++--------------------
 10 files changed, 61 insertions(+), 140 deletions(-)

diff --git a/Core/Export/SimulationToPython.cpp b/Core/Export/SimulationToPython.cpp
index 1c1e7e031da..bbb10bc6487 100644
--- a/Core/Export/SimulationToPython.cpp
+++ b/Core/Export/SimulationToPython.cpp
@@ -135,62 +135,59 @@ std::string SimulationToPython::defineSpecularSimulation(const SpecularSimulatio
 
 std::string SimulationToPython::defineDetector(const Simulation* simulation) const
 {
-    const IDetector* iDetector = simulation->getInstrument().getDetector();
-
-    if (iDetector->dimension() != 2)
+    const IDetector* const detector = simulation->getInstrument().getDetector();
+    if (detector->dimension() != 2)
         throw Exceptions::RuntimeErrorException("SimulationToPython::defineDetector: "
                                                 "detector must be two-dimensional for GISAS");
     std::ostringstream result;
     result << std::setprecision(12);
 
-    if (auto detector = dynamic_cast<const SphericalDetector*>(iDetector)) {
+    if (const auto* const det = dynamic_cast<const SphericalDetector*>(detector)) {
         result << pyfmt::indent() << "simulation.setDetectorParameters(";
-        for (size_t index = 0; index < detector->dimension(); ++index) {
+        for (size_t index = 0; index < det->dimension(); ++index) {
             if (index != 0)
                 result << ", ";
-            result << detector->getAxis(index).size() << ", "
-                   << pyfmt::printDegrees(detector->getAxis(index).getMin()) << ", "
-                   << pyfmt::printDegrees(detector->getAxis(index).getMax());
+            result << det->getAxis(index).size() << ", "
+                   << pyfmt::printDegrees(det->getAxis(index).getMin()) << ", "
+                   << pyfmt::printDegrees(det->getAxis(index).getMax());
         }
         result << ")\n";
-    } else if (auto detector = dynamic_cast<const RectangularDetector*>(iDetector)) {
+    } else if (const auto* const det = dynamic_cast<const RectangularDetector*>(detector)) {
         result << pyfmt::indent() << "\n";
-        result << pyfmt::indent() << "detector = ba.RectangularDetector(" << detector->getNbinsX()
-               << ", " << pyfmt::printDouble(detector->getWidth()) << ", " << detector->getNbinsY()
-               << ", " << pyfmt::printDouble(detector->getHeight()) << ")\n";
-        if (detector->getDetectorArrangment() == RectangularDetector::GENERIC) {
+        result << pyfmt::indent() << "detector = ba.RectangularDetector(" << det->getNbinsX()
+               << ", " << pyfmt::printDouble(det->getWidth()) << ", " << det->getNbinsY() << ", "
+               << pyfmt::printDouble(det->getHeight()) << ")\n";
+        if (det->getDetectorArrangment() == RectangularDetector::GENERIC) {
             result << pyfmt::indent() << "detector.setPosition("
-                   << pyfmt::printKvector(detector->getNormalVector()) << ", "
-                   << pyfmt::printDouble(detector->getU0()) << ", "
-                   << pyfmt::printDouble(detector->getV0());
-            if (!pyfmt::isDefaultDirection(detector->getDirectionVector()))
-                result << ", " << pyfmt::printKvector(detector->getDirectionVector());
+                   << pyfmt::printKvector(det->getNormalVector()) << ", "
+                   << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0());
+            if (!pyfmt::isDefaultDirection(det->getDirectionVector()))
+                result << ", " << pyfmt::printKvector(det->getDirectionVector());
             result << ")\n";
-        } else if (detector->getDetectorArrangment()
-                   == RectangularDetector::PERPENDICULAR_TO_SAMPLE) {
+        } else if (det->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_SAMPLE) {
             result << pyfmt::indent() << "detector.setPerpendicularToSampleX("
-                   << pyfmt::printDouble(detector->getDistance()) << ", "
-                   << pyfmt::printDouble(detector->getU0()) << ", "
-                   << pyfmt::printDouble(detector->getV0()) << ")\n";
-        } else if (detector->getDetectorArrangment()
+                   << pyfmt::printDouble(det->getDistance()) << ", "
+                   << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
+                   << ")\n";
+        } else if (det->getDetectorArrangment()
                    == RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM) {
             result << pyfmt::indent() << "detector.setPerpendicularToDirectBeam("
-                   << pyfmt::printDouble(detector->getDistance()) << ", "
-                   << pyfmt::printDouble(detector->getU0()) << ", "
-                   << pyfmt::printDouble(detector->getV0()) << ")\n";
-        } else if (detector->getDetectorArrangment()
+                   << pyfmt::printDouble(det->getDistance()) << ", "
+                   << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
+                   << ")\n";
+        } else if (det->getDetectorArrangment()
                    == RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM) {
             result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam("
-                   << pyfmt::printDouble(detector->getDistance()) << ", "
-                   << pyfmt::printDouble(detector->getU0()) << ", "
-                   << pyfmt::printDouble(detector->getV0()) << ")\n";
-        } else if (detector->getDetectorArrangment()
+                   << pyfmt::printDouble(det->getDistance()) << ", "
+                   << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
+                   << ")\n";
+        } else if (det->getDetectorArrangment()
                    == RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS) {
             result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam("
-                   << pyfmt::printDouble(detector->getDistance()) << ")\n";
+                   << pyfmt::printDouble(det->getDistance()) << ")\n";
             result << pyfmt::indent() << "detector.setDirectBeamPosition("
-                   << pyfmt::printDouble(detector->getDirectBeamU0()) << ", "
-                   << pyfmt::printDouble(detector->getDirectBeamV0()) << ")\n";
+                   << pyfmt::printDouble(det->getDirectBeamU0()) << ", "
+                   << pyfmt::printDouble(det->getDirectBeamV0()) << ")\n";
         } else
             throw Exceptions::RuntimeErrorException(
                 "SimulationToPython::defineDetector() -> Error. Unknown alignment.");
@@ -199,12 +196,12 @@ std::string SimulationToPython::defineDetector(const Simulation* simulation) con
     } else
         throw Exceptions::RuntimeErrorException("SimulationToPython::defineDetector() -> Error. "
                                                 "Unknown detector");
-    if (iDetector->regionOfInterest()) {
+    if (detector->regionOfInterest()) {
         result << pyfmt::indent() << "simulation.setRegionOfInterest("
-               << printFunc(iDetector)(iDetector->regionOfInterest()->getXlow()) << ", "
-               << printFunc(iDetector)(iDetector->regionOfInterest()->getYlow()) << ", "
-               << printFunc(iDetector)(iDetector->regionOfInterest()->getXup()) << ", "
-               << printFunc(iDetector)(iDetector->regionOfInterest()->getYup()) << ")\n";
+               << printFunc(detector)(detector->regionOfInterest()->getXlow()) << ", "
+               << printFunc(detector)(detector->regionOfInterest()->getYlow()) << ", "
+               << printFunc(detector)(detector->regionOfInterest()->getXup()) << ", "
+               << printFunc(detector)(detector->regionOfInterest()->getYup()) << ")\n";
     }
     result << pyfmt::indent() << "\n";
     return result.str();
diff --git a/Core/Instrument/Instrument.cpp b/Core/Instrument/Instrument.cpp
index 5a2c35ea771..2745674be65 100644
--- a/Core/Instrument/Instrument.cpp
+++ b/Core/Instrument/Instrument.cpp
@@ -133,12 +133,6 @@ const IDetector* Instrument::getDetector() const
     return mP_detector.get();
 }
 
-IDetector* Instrument::getDetector()
-{
-    ASSERT(mP_detector);
-    return mP_detector.get();
-}
-
 const IDetector& Instrument::detector() const
 {
     ASSERT(mP_detector);
diff --git a/Core/Instrument/Instrument.h b/Core/Instrument/Instrument.h
index f7cbd4cf7a1..11ebc136f5b 100644
--- a/Core/Instrument/Instrument.h
+++ b/Core/Instrument/Instrument.h
@@ -56,7 +56,6 @@ public:
 
     double getBeamIntensity() const;
 
-    IDetector* getDetector();
     const IDetector* getDetector() const;
     IDetector& detector();
     const IDetector& detector() const;
diff --git a/Core/Simulation/OffSpecSimulation.cpp b/Core/Simulation/OffSpecSimulation.cpp
index edff8d2c684..9103490f1be 100644
--- a/Core/Simulation/OffSpecSimulation.cpp
+++ b/Core/Simulation/OffSpecSimulation.cpp
@@ -54,13 +54,9 @@ size_t OffSpecSimulation::numberOfSimulationElements() const
 SimulationResult OffSpecSimulation::result() const
 {
     auto data = std::unique_ptr<OutputData<double>>(m_intensity_map.clone());
-    auto p_det = dynamic_cast<const IDetector2D*>(getInstrument().getDetector());
-    if (p_det) {
-        OffSpecularConverter converter(*p_det, getInstrument().getBeam(), *mP_alpha_i_axis);
-        return SimulationResult(*data, converter);
-    }
-    throw std::runtime_error("Error in OffSpecSimulation::result: "
-                             "wrong or absent detector type");
+    OffSpecularConverter converter(getInstrument().detector2D(), getInstrument().getBeam(),
+                                   *mP_alpha_i_axis);
+    return SimulationResult(*data, converter);
 }
 
 void OffSpecSimulation::setBeamParameters(double wavelength, const IAxis& alpha_axis, double phi_i)
@@ -81,12 +77,11 @@ const IAxis* OffSpecSimulation::beamAxis() const
 
 std::unique_ptr<IUnitConverter> OffSpecSimulation::createUnitConverter() const
 {
-    const IDetector* detector = getInstrument().getDetector();
     const IAxis* axis = beamAxis();
-    if (!detector || !axis)
-        throw std::runtime_error("Error in OffSpecSimulation::createUnitConverter: missing detector"
-                                 "or inclination angle axis");
-    return std::make_unique<OffSpecularConverter>(static_cast<const IDetector2D&>(*detector),
+    if (!axis)
+        throw std::runtime_error("Error in OffSpecSimulation::createUnitConverter:"
+                                 " missing inclination angle axis");
+    return std::make_unique<OffSpecularConverter>(getInstrument().detector2D(),
                                                   getInstrument().getBeam(), *axis);
 }
 
diff --git a/Core/Simulation/Simulation2D.cpp b/Core/Simulation/Simulation2D.cpp
index 9726d631d01..ed39765f208 100644
--- a/Core/Simulation/Simulation2D.cpp
+++ b/Core/Simulation/Simulation2D.cpp
@@ -93,18 +93,16 @@ std::unique_ptr<IComputation> Simulation2D::generateSingleThreadedComputation(si
 
 std::vector<SimulationElement> Simulation2D::generateSimulationElements(const Beam& beam)
 {
-    std::vector<SimulationElement> result;
-
     const double wavelength = beam.getWavelength();
     const double alpha_i = -beam.getAlpha(); // Defined to be always positive in Beam
     const double phi_i = beam.getPhi();
+    const Eigen::Matrix2cd beam_polarization = beam.getPolarization();
 
     const IDetector2D& detector = m_instrument.detector2D();
-
-    const Eigen::Matrix2cd beam_polarization = beam.getPolarization();
     const Eigen::Matrix2cd analyzer_operator = detector.detectionProperties().analyzerOperator();
     const size_t spec_index = detector.getIndexOfSpecular(beam);
 
+    std::vector<SimulationElement> result;
     result.reserve(detector_context->numberOfSimulationElements());
     for (size_t element_index = 0; element_index < detector_context->numberOfSimulationElements();
          ++element_index) {
diff --git a/Core/Simulation/SpecularSimulation.cpp b/Core/Simulation/SpecularSimulation.cpp
index 1946b040855..577ededa41b 100644
--- a/Core/Simulation/SpecularSimulation.cpp
+++ b/Core/Simulation/SpecularSimulation.cpp
@@ -150,7 +150,7 @@ SpecularSimulation::generateSimulationElements(const Beam& beam)
 
     // add polarization and analyzer operators
     const auto& polarization = beam.getPolarization();
-    const auto& analyzer = m_instrument.getDetector()->detectionProperties().analyzerOperator();
+    const auto& analyzer = m_instrument.detector().detectionProperties().analyzerOperator();
 
     for (auto& elem : elements)
         elem.setPolarizationHandler({polarization, analyzer});
@@ -271,9 +271,8 @@ std::vector<double> SpecularSimulation::rawResults() const
 {
     std::vector<double> result;
     result.resize(m_sim_elements.size());
-    for (unsigned i = 0; i < m_sim_elements.size(); ++i) {
+    for (unsigned i = 0; i < m_sim_elements.size(); ++i)
         result[i] = m_sim_elements[i].getIntensity();
-    }
     return result;
 }
 
@@ -283,9 +282,8 @@ void SpecularSimulation::setRawResults(const std::vector<double>& raw_data)
     if (raw_data.size() != m_sim_elements.size())
         throw std::runtime_error("SpecularSimulation::setRawResults: size of vector passed as "
                                  "argument doesn't match number of elements in this simulation");
-    for (unsigned i = 0; i < raw_data.size(); i++) {
+    for (unsigned i = 0; i < raw_data.size(); i++)
         m_sim_elements[i].setIntensity(raw_data[i]);
-    }
     transferResultsToIntensityMap();
 }
 
diff --git a/Core/Simulation/UnitConverterUtils.cpp b/Core/Simulation/UnitConverterUtils.cpp
index 903cfbc478e..bddc3647ce7 100644
--- a/Core/Simulation/UnitConverterUtils.cpp
+++ b/Core/Simulation/UnitConverterUtils.cpp
@@ -37,12 +37,12 @@ UnitConverterUtils::createOutputData(const IUnitConverter& converter, AxesUnits
 std::unique_ptr<IUnitConverter>
 UnitConverterUtils::createConverterForGISAS(const Instrument& instrument)
 {
-    const IDetector* detector = instrument.getDetector();
+    const IDetector* const detector = instrument.getDetector();
 
-    if (const auto spher_detector = dynamic_cast<const SphericalDetector*>(detector))
-        return std::make_unique<SphericalConverter>(*spher_detector, instrument.getBeam());
-    else if (const auto rect_detector = dynamic_cast<const RectangularDetector*>(detector))
-        return std::make_unique<RectangularConverter>(*rect_detector, instrument.getBeam());
+    if (const auto* const det = dynamic_cast<const SphericalDetector*>(detector))
+        return std::make_unique<SphericalConverter>(*det, instrument.getBeam());
+    else if (const auto* const det = dynamic_cast<const RectangularDetector*>(detector))
+        return std::make_unique<RectangularConverter>(*det, instrument.getBeam());
 
     throw std::runtime_error("Error in createConverterForGISAS: wrong or absent detector type");
 }
diff --git a/auto/Wrap/doxygen_core.i b/auto/Wrap/doxygen_core.i
index 46bab960200..e9e6547fd7d 100644
--- a/auto/Wrap/doxygen_core.i
+++ b/auto/Wrap/doxygen_core.i
@@ -8277,9 +8277,6 @@ Sets the beam's polarization according to the given Bloch vector.
 %feature("docstring")  Instrument::getBeamIntensity "double Instrument::getBeamIntensity() const
 ";
 
-%feature("docstring")  Instrument::getDetector "IDetector * Instrument::getDetector()
-";
-
 %feature("docstring")  Instrument::getDetector "const IDetector * Instrument::getDetector() const
 ";
 
diff --git a/auto/Wrap/libBornAgainCore.py b/auto/Wrap/libBornAgainCore.py
index d4d2c031e99..232d34aa7b3 100644
--- a/auto/Wrap/libBornAgainCore.py
+++ b/auto/Wrap/libBornAgainCore.py
@@ -20978,14 +20978,13 @@ class Instrument(INode):
         """
         return _libBornAgainCore.Instrument_getBeamIntensity(self)
 
-    def getDetector(self, *args):
+    def getDetector(self):
         r"""
         getDetector(Instrument self) -> IDetector
-        getDetector(Instrument self) -> IDetector
         const IDetector * Instrument::getDetector() const
 
         """
-        return _libBornAgainCore.Instrument_getDetector(self, *args)
+        return _libBornAgainCore.Instrument_getDetector(self)
 
     def detector(self, *args):
         r"""
diff --git a/auto/Wrap/libBornAgainCore_wrap.cpp b/auto/Wrap/libBornAgainCore_wrap.cpp
index 7ae1bfeab2d..435472f2352 100644
--- a/auto/Wrap/libBornAgainCore_wrap.cpp
+++ b/auto/Wrap/libBornAgainCore_wrap.cpp
@@ -115662,35 +115662,16 @@ fail:
 }
 
 
-SWIGINTERN PyObject *_wrap_Instrument_getDetector__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
-  PyObject *resultobj = 0;
-  Instrument *arg1 = (Instrument *) 0 ;
-  void *argp1 = 0 ;
-  int res1 = 0 ;
-  IDetector *result = 0 ;
-  
-  if ((nobjs < 1) || (nobjs > 1)) SWIG_fail;
-  res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_Instrument, 0 |  0 );
-  if (!SWIG_IsOK(res1)) {
-    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Instrument_getDetector" "', argument " "1"" of type '" "Instrument *""'"); 
-  }
-  arg1 = reinterpret_cast< Instrument * >(argp1);
-  result = (IDetector *)(arg1)->getDetector();
-  resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_IDetector, 0 |  0 );
-  return resultobj;
-fail:
-  return NULL;
-}
-
-
-SWIGINTERN PyObject *_wrap_Instrument_getDetector__SWIG_1(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
+SWIGINTERN PyObject *_wrap_Instrument_getDetector(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
   PyObject *resultobj = 0;
   Instrument *arg1 = (Instrument *) 0 ;
   void *argp1 = 0 ;
   int res1 = 0 ;
+  PyObject *swig_obj[1] ;
   IDetector *result = 0 ;
   
-  if ((nobjs < 1) || (nobjs > 1)) SWIG_fail;
+  if (!args) SWIG_fail;
+  swig_obj[0] = args;
   res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_Instrument, 0 |  0 );
   if (!SWIG_IsOK(res1)) {
     SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Instrument_getDetector" "', argument " "1"" of type '" "Instrument const *""'"); 
@@ -115704,42 +115685,6 @@ fail:
 }
 
 
-SWIGINTERN PyObject *_wrap_Instrument_getDetector(PyObject *self, PyObject *args) {
-  Py_ssize_t argc;
-  PyObject *argv[2] = {
-    0
-  };
-  
-  if (!(argc = SWIG_Python_UnpackTuple(args, "Instrument_getDetector", 0, 1, argv))) SWIG_fail;
-  --argc;
-  if (argc == 1) {
-    int _v;
-    void *vptr = 0;
-    int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_Instrument, 0);
-    _v = SWIG_CheckState(res);
-    if (_v) {
-      return _wrap_Instrument_getDetector__SWIG_0(self, argc, argv);
-    }
-  }
-  if (argc == 1) {
-    int _v;
-    void *vptr = 0;
-    int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_Instrument, 0);
-    _v = SWIG_CheckState(res);
-    if (_v) {
-      return _wrap_Instrument_getDetector__SWIG_1(self, argc, argv);
-    }
-  }
-  
-fail:
-  SWIG_Python_RaiseOrModifyTypeError("Wrong number or type of arguments for overloaded function 'Instrument_getDetector'.\n"
-    "  Possible C/C++ prototypes are:\n"
-    "    Instrument::getDetector()\n"
-    "    Instrument::getDetector() const\n");
-  return 0;
-}
-
-
 SWIGINTERN PyObject *_wrap_Instrument_detector__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
   PyObject *resultobj = 0;
   Instrument *arg1 = (Instrument *) 0 ;
@@ -133748,8 +133693,7 @@ static PyMethodDef SwigMethods[] = {
 		"double Instrument::getBeamIntensity() const\n"
 		"\n"
 		""},
-	 { "Instrument_getDetector", _wrap_Instrument_getDetector, METH_VARARGS, "\n"
-		"Instrument_getDetector(Instrument self) -> IDetector\n"
+	 { "Instrument_getDetector", _wrap_Instrument_getDetector, METH_O, "\n"
 		"Instrument_getDetector(Instrument self) -> IDetector\n"
 		"const IDetector * Instrument::getDetector() const\n"
 		"\n"
-- 
GitLab