Running a Macro from a compiled c++ program

Hi ROOTers!

I would like to execute a macro from a compiled c++ program, but I don’t know how.

In fact I have a .C macro with many graphical settings and functions definitions and I would like to execute it before other commands.

I saw there’s a method TCint::ExecuteMacro(), but I’m not sure how to use it.

Thanks for your help!



gROOT->ProcessLine(".x myscript.C");

Thanks Rene for your answer, but it doesn’t work properly.

In fact I have to execute the Atlas template ROOt file (the “rootlogon.C” which I copied to “atlas.C”) ant then use objects defined there in my compiled c++ code.

If I do the following at the ROOT prompt (interactevely), it works well:

root [0] .x ~/rootmacros/atlas.C

Welcome to the ATLAS rootlogon.C
root [1] atlasStyle->SetTitleSize(0.06,“Y”);
root [2]

But if I do the following on my c++ compiled code, I get errors; it seems that the macro is not executed:

**** my code *******
void RPlot::PlotCSC(TString histoname, TString linlog, TString savename, Int_t rebin, TString xlabel, TString ylabel, TString title)
gROOT->ProcessLine(".X ~/rootmacros/atlas.C");



******** Errors I get: *************
~/trunk/src/RPlot.C: In member function ‘void RPlot::PlotCSC(TString, TString, TString, Int_t, TString, TString, TString)’:
~/trunk/src/RPlot.C:1790: error: ‘atlasStyle’ was not declared in this scope
~/trunk/src/RPlot.C:1940: error: ‘myMarkerText’ was not declared in this scope
make: *** [/localscratch/COMPILE_CUSTOM/objs/RPlot.o] Error 1
[rbianchi@hefr34] ~/trunk $

You can find the “rootlogon.C” ATLAS file (that I copied to "atlas.C’), I want to execute within my code, at:

Thanks again for your precious help! :slight_smile:


I tried both with “.X macro.C” and “.x macro.C”; I’m not sure if it changes something, but it doesn’t work anyway.


I don’t have an answer to your question, but here’s a solution we use. We make sure that whatever macro we use to setup our root environment ( in our case) works both interactively and compiled. So we just compile the .cc file with the rest of our executable and call it (you’ll want an void corporate_id(); somewhere to let the compiler know it exists before you call it) before we make any plots.

Good luck,

As I already said, there is no problem in executing your script from compiled code. Now your problem is different, you want to access an object (eg atlasStyle named “ATLAS”) created by the interpreter in your compiled member function.
For that, you should regsiter this object in some collection, eg
in your script atlas.C (at the end) add the line

gROOT->GetListOfSpecials()->Add(atlasStyle); and in your member function retrieve this object with:

TStyle *atlasStyle = (TStyle*)gROOT->GetListOfSpecials()->FindObject("ATLAS");

Thank you very much! It works fine!

Thanks for your help :slight_smile: