ROOT scripts and user-defined classes

Dear ROOT experts,

I would like to create an interface (C++ abstract base class and daughter classes, derived from the base class) and implement it into ROOT script. How can I do this?? Some basic templates of the .h and .cxx files are below:

BaseClass.h

#pragma once

class BaseClass{
public:
   BaseClass(){};
   ~BaseClass(){};
   
   virtual void Create() = 0;
   virtual void Fill() = 0;
   virtual void Write() = 0;

protected:
   TString name;
};

DerivedClass.h

#pragma once

#include "BaseClass.h"

class DerivedClass: public BaseClass{
public:
   DerivedClass():BaseClass(){};
   ~DerivedClass(){};
   
   void Create() override;
   void Fill() override;
   void Write() override;
};

DerivedClass.cxx

#include "DerivedClass.h"
   
void DerivedClass::Create() {cout << "Create\n";};
void DerivedClass::Fill() {cout << "Fill\n";};
void DerivedClass::Write() {cout << "Write\n";};

testscript.cxx

#include "DerivedClass.h"

void testscript(){

   DerivedClass obj;

   obj.Create();
   obj.Fill();
   obj.Write();

}

That’s more a C++ question than a ROOT one. You can have a look at the many C++ tutorials available on the web. That said, what you wrote seems correct.

Nevertheless, it doesn’t work:

vlad ~/HEP/root_classes_exmple % root -l testscript.cxx
root [0] 
Processing testscript.cxx...
IncrementalExecutor::executeFunction: symbol '_ZN12DerivedClass5WriteEv' unresolved while linking [cling interface function]!
You are probably missing the definition of DerivedClass::Write()
Maybe you need to load the corresponding shared library?
IncrementalExecutor::executeFunction: symbol '_ZN12DerivedClass6CreateEv' unresolved while linking [cling interface function]!
You are probably missing the definition of DerivedClass::Create()
Maybe you need to load the corresponding shared library?
IncrementalExecutor::executeFunction: symbol '_ZN12DerivedClass4FillEv' unresolved while linking [cling interface function]!
You are probably missing the definition of DerivedClass::Fill()
Maybe you need to load the corresponding shared library?
IncrementalExecutor::executeFunction: symbol '_ZTV12DerivedClass' unresolved while linking [cling interface function]!
You are probably missing the definition of vtable for DerivedClass
Maybe you need to load the corresponding shared library?
root [1] 

Try to put the definition of the derived methods in the include file. Not in a separated .cxx file.

1 Like

You have to load (“compile”) DerivedClass before you can use it:

root [0] .L DerivedClass.cxx+
root [1] .L testscript.cxx+
root [2] testscript()

Cheers,
Enrico

1 Like

couet, eguirad,

both of your solutions are work. First one is the simplest to realize, but it can be messy in case of some sophisticated classes and projects. In this case the second variant can be a solution (to compile shared libraries).

Thank you both!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.