Custom class in ROOT6

Hey,
I changed from v5 to v6 and cannot get my usual environment to run. I have a variety of log-file-readers/analyzers all having the same structure.

  1. header file with class definition
  2. .cpp file with class implementation
  3. _rootexec.C loading class and doing the analysis

I attached the most simple example here.
mytry.h (88 Bytes)
mytry.cpp (147 Bytes)
mytry_rootexec.C (126 Bytes)

For each analysis I have a separate _rootexec.C which I execute from the terminal with
$ root mytry_rootexec.C

The problem in ROOT6 is that way rootexec.C does not run because the class is not known in advance, which as I understood was not needed in ROOT5. I tried using .rootmap and LinkDef.h files but I didn’t really understood how to use/generate them the right way.

How would I get this simple example to work?
Thanks
Sebastian

Hi Sebastian,

the reason is that in ROOT6, the interpreter is not a simple C++ “parser” but rather a veritable compiler.
The code of the macros is “seen” by the interpreter as a whole and not digested line by line. It is therefore not possible to make symbols known at runtime with processLine or loading libraries.

But don’t worry, there are at least two ways to achieve what you want:

  1. Include the class code in your macro. Here the example crafted starting from your files:
    mytry_rootexec.C
#include "mytry.cpp"
void mytry_rootexec() {
   mytry *t = new mytry();   
}

mytry.cpp (Note that ClassImp is useless in ROOT6)

#include "mytry.h"
#include <iostream>

mytry::mytry(){
   std::cout << "Hallo Welt" << std::endl;
}
mytry::~mytry(){}

mytry.h Note that ClassDef is necessary if you want to do I/O of objects of a certain class and not for interactivity only.

#ifndef __mytry__
#define __mytry__
class mytry {
   public:
      mytry();
      ~mytry();
};
#endif
  1. A bit more evolved. Prepare a dictionary and a shared library with the user code:
genreflex mytry.h --rootmap my.rootmap --rootmap-lib libmytry.so
g++ -o libmytry.so mytry_rflx.cpp mytry.cpp -shared -fPIC `root-config --cflags --libs`
root -b -q  mytry_rootexec.C

Cheers,
D

Thanks a lot!!

The second suggestion works perfectly for me! In the end this is what I tried to accomplish. However I didn’t include the _rflx.cpp when compiling the library so it wouldn’t run.

Thanks again
Sebastian

Hi Sebastian,

glad it works. Indeed the second option is the cleaner one. In some sense I still find the first one interesting as it shows it’s possible to interactively use custom C++ classes in macros and at the ROOT prompt.

Cheers,
D