How to conditionally load and then use a macro in ROOT6

Hello,

I am trying to use a function in a macro that I load only upon its existence. You can see from the macros that I transcribe below (I cannot attach 3 files…) that:

  • “mainMacro.C” loads “conditionaMacro.C” if it exists;
  • “mainMacro.C” then runs “testMacro.C”
  • in “testMacro.C” the function defined in “conditionalMacro.C” is called only if this macro exists.

If “conditionalMacro.C” exists, everything works, but if it doesn’t, there is an error of “undeclared identifier” in “testMacro.C”, of course. Can one overcome this problem?

Thanks for any help, and regards,

Chiara

========= mainMacro.C:

void mainMacro(){

  if (gSystem->AccessPathName("conditionalMacro.C", kFileExists)==0) {        
    gROOT->LoadMacro("conditionalMacro.C");
    Printf("loaded");
  }

  gROOT->Macro("testMacro.C");

  return;

}

========= testMacro.C

void testMacro(){

  Printf("In testMacro");
  if (gSystem->AccessPathName("condionalMacro.C", kFileExists)==0) {
    conditionalMacro();
  }
  return;
}

========= conditionalMacro.C

void conditionalMacro(){

  Printf("In conditionalMacro");

  return;

}
if (gSystem->AccessPathName("conditionalMacro.C", kFileExists)==0) {
  gROOT->ProcessLine("conditionalMacro();"); // try this ...
  // gInterpreter->ProcessLine("conditionalMacro();"); // ... or that
}

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Dear Wile,

Thanks for the suggestion, but for me it does not work. I tried both options (I guess you meant to use these lines in testMacro.C, right?), and even if I get no error, the conditionalMacro is not executed (I have no output from it). Did it work for you?

Thanks also for the comment about the format, I will follow it next time.

Regards,

Chiara

if (gSystem->AccessPathName("conditionalMacro.C", kFileExists)==0) { // NOT "condionalMacro.C"

Of course! Sorry for the typo in the example :slight_smile:

I have then a question: what is the difference between the three:

conditionalMacro(); // not working
gROOT->ProcessLine("conditionalMacro();"); // try this ...
  // gInterpreter->ProcessLine("conditionalMacro();"); // ... or that

Thanks again,

Chiara

The main difference is that, the first one is “not working” with ROOT 6, while the two later ones work fine.

BTW. Your version, which directly calls “conditionalMacro();”, works fine with ROOT 5.

Hello Wile,

Yes, the first does not work but the others do, I would be curious to know why :slight_smile:
And I know that my first version works in ROOT5, this is what I started from - I am porting my code from ROOT5 to ROOT6.

Thanks again!

Chiara

It’s a top secret confidential knowledge. :-#
Only for The Initiated. :mrgreen:

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