Small question about TMacro and Exec function in c++ application

Hello ROOT support,

Let’s assume that i have simple macro and i would like to execute it dynamically in c++ application.

In ROOT interpreter i can do

$ root
root [0] TMacro m("/tmp/mymacro.C")
(TMacro &) Name: mymacro Title: /tmp/mymacro.C
root [1] m.Exec()
Hello macro
(long) 0
root [2] .q

Please notice output (long) 0 in stdout. If i do m.Exec(); (with ‘;’ in the end)

root [1] m.Exec();

the (long) 0 output is hidden. But when i try to use it in my c++ application without root interpreter i see this output. Can you give me hint how to hide it or what am i doing wrong. Example for my code is something like this

Longptr_t ok = fMacro->Exec(TString::Format("(NdmSpc::PointRun*)%p", this));

Thanks

Ciao

Martin

Dear @mvala ,

This is exactly the intended behaviour. The ROOT repl will print to screen the result of executing the line if there is no ; character at the end, whereas filling the line with the ; token, which makes it a proper C++ line of code, will behave like standard C++.

I don’t understand this point, what do you mean exactly? The snippet you are showing afterwards does not show the output. Note that the return type is indeed correctly Longptr_t as you write.

Cheers,
Vincenzo

When do fMacro->Exec(...) from c++ app (program without ROOT interpreter ) i see

Objects stored in 'root://eos.ndmspc.io//eos/ndmspc/scratch/temp/mvala/gitlab/milestone/bins/32/content.root'
Processing 'milestone[32.00,33.00]' ...
Integral = 10.000000 
(bool) true

Note that (bool) true is printed and i want to hide it.

I would send you link with source code but as new user i cannot post links here.

Dear @mvala ,

I understand what you mean. From what I can see, TMacro runs the macro verbatim, including all the output from the macro which includes the return value. You can easily redirect this output for the scope where you are running the macro. There are many ways to do it also with standard C++ (see for example this SO question) and you can do it with the following ROOT helper class as well:

#include <TMacro.h>
#include <TRedirectOutputGuard.h>

int main() {
  {
    TRedirectOutputGuard g{"/dev/null"};
    TMacro m{"macro.C"};
    m.Exec();
  }
}

Cheers,
Vincenzo

Dear @vpadulan,

Thanks for answer, i like this solution. And i assume that there is no way to see macro output and only hide (bool) true. Right?

Ciao

Martin

Dear @mvala ,

Not to my knowledge. I wonder if @pcanal, @couet or @bellenot know anything more about it.

Cheers,
Vincenzo

You can use:

Longptr_t ok = gROOT->ProcessLine(TString::Format("%s((NdmSpc::PointRun*)%p);",
                                  fMacro->GetName(), this));

This is nice, will it be same (gROOT->ProcessLine vs. fMacro->Exec()) from performance point of view?

Yes, It will.

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