Hi @Javier_Galan ,
Sorry for the latency. Maybe I don’t understand your question completely, but doesn’t something like this work for you?
import ROOT
ROOT.gInterpreter.Declare("""
struct A {};
struct B: public A {
int methodB(A *a) { return 42; }
};
""")
b = ROOT.B()
a = ROOT.A()
print(b.methodB(a))
I cannot create an instance of A because it is abstract, I believe that is the main difference with your example.
Perhaps it would be more like:
import ROOT
ROOT.gInterpreter.Declare("""
struct C {
int methodC( A *a ) { return 42; }
};
struct A {};
struct B: public A {
};
""")
c = ROOT.C()
b = ROOT.B()
print(c.methodC(b))
Ok, glad the examples work! For your last case, the error is due to the fact that changing to class makes the methodC private in that class. You need to make it public.
Cheers,
Vincenzo
I find it weird that it works for code injected into the interpreter, but not for compiled classes. Could you please attach a minimal reproducer so that we can better diagnose (or debug) the issue?
As a (hacky) workaround, maybe you could define an inline member function on the C++ side that returns a pointer to the base object, as in
class TRestRawSignalEvent : public TRestEvent {
public:
// ...
TRestEvent *AsTRestEvent() { return static_cast<TRestEvent *>(this); }
};
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: TRestEvent* TRestRawSignalEvent::AsTRestEvent() =>
TypeError: unbound method TRestRawSignalEvent::AsTRestEvent must be called with a TRestRawSignalEvent instance as first argument
This snippet should be equivalent to what you are doing (based on your previous examples, corrected class names and also adding the latest AsTRestEvent method). But all the classes feature their methods in the public section in your own repository, so I still don’t understand what was your previous issue with using the private methods.
import ROOT
ROOT.gInterpreter.Declare("""
class TRestEvent {
public:
virtual void Initialize() = 0;
};
class TRestRawSignalEvent: public TRestEvent {
public:
void Initialize() final {}
TRestEvent *AsTRestEvent() { return static_cast<TRestEvent *>(this); }
};
class TRestRun{
public:
void SetInputEvent(TRestEvent *) {}
};
""")
rn = ROOT.TRestRun()
rawEv = ROOT.TRestRawSignalEvent()
rn.SetInputEvent(rawEv)
rn.SetInputEvent(rawEv.AsTRestEvent())
This fails as it is, what are the requirements to build the project? I’ve hit a couple issues, first I didn’t have a ROOT installation before running the cmake command and it complained. Then I sourced ROOT and now it complains that it can’t find TEveArrow.h.
I cannot build on ArchLinux due to notable changes in the GNU mpfr headers (using version 4.1.1). Could you provide us additional information about your build environment?
make -j install
[...]
[ 17%] generating: /home/vpadulan/Programs/framework/build/rootdict/CINT_TRestTask.cxx with /home/vpadulan/Programs/framework/source/framework/core/inc/TRestTask.h;/home/vpadulan/Programs/framework/build/rootdict/TRestTask_Linkdef.h
In file included from input_line_9:3:
In file included from /home/vpadulan/Programs/framework/source/framework/tools/inc/TRestComplex.h:27:
/home/vpadulan/Programs/framework/source/framework/tools/inc/mpreal.h:121:10: fatal error: 'mpfr.h' file not found
#include <mpfr.h>
^~~~~~~~