Bug #1367

Fix python bindings to make fitting example FitAlongSlices working

Added by pospelov over 4 years ago. Updated over 4 years ago.

Status:ArchivedStart date:07 Mar 2016
Priority:NormalDue date:
Assignee:-% Done:


Target version:Sprint 31


The issue is small, but it contains some philosophic aspect.

This is all about

IHistogram *FitSuite::getSimulationData()



In FitAlongSlices example getSimulationData() is used to get intensity data from FitSuite and then make an 1D histogram out of it. The problem is that returned object is of IHistogram type, while projectionX() belongs to Histogram2D object. In C++ we would use dynamic_cast to convert IHistogram to Histogram2D. In Python there is no dynamic cast. With old bindings everything was working nobody knows why. With new bindings, SWIG doesn't allow to call projectionX() on IHistogram object (as it in principle should be).

We need to find a way how to make an example working. Possible options
  • Make FitSUite return Histogram2D type
  • Make projectionX to be the virtual member of IHistogram type (and make it throw an exception for Histogram1D objects)
  • Introduce SWIG fake C++ code which will add projectionX to IHistogram objects on Python side.


#1 Updated by jmfisher over 4 years ago

Note that the example uses other methods from Histogram2D, e.g. projectionY(). So the problem is not just isolated to a single method.

Another option is to use Swig to export an explicit dynamic cast, e.g. export a static method such as
Histogram2D* Histogram2D::cast(IHistogram hist) { return dynamic_cast<Histogram2D>(hist); }
Then on the Python side, replace getIntensityData() with Histogram2D.cast(getIntensityData()) and everything works as intended.

My vote is either to make FitSuite return Histogram2D, or to make the dynamic cast explicit. The other solutions spoil the C++ side and/or make the Swig interface more difficult to maintain.

#2 Updated by jmfisher over 4 years ago

I have added a static methods Histogram2D::dynamicCast() and Histogram1D::dynamicCast() to the swig interface which allow dynamic casting from IHistogram to Histogram2D/Histogram1D in Python, and I have added a modified example 'FitAlongSlices_new.py' which demonstrates usage.

#3 Updated by jmfisher over 4 years ago

  • Status changed from Sprint to Resolved

Decision was made to use explicit cast as the solution. FitAlongSlices.py now works with new bindings.

#4 Updated by herck over 4 years ago

  • Status changed from Resolved to Archived

Also available in: Atom PDF