Pads disappearing...?

Hello everyone,

So I was trying to do something that I thought was gonna be easy (ain’t it always the case…?). I was trying to draw my plot and its legend in two separate pads.

I thought I’d draw two unequal pads and put them in each. The funny thing is that, for some reason, if I initiate the canvas and pads outside of the my main macro, it doesn’t draw properly. But if I run the whole thing as an unnamed macro, it draws just fine.

Can anyone tell me why that is? Any help would be greatly appreciated. :slight_smile:

Cheers,
Atyab

P.S.: Attached a script with a few lines to show what I mean.
test2.C (938 Bytes)

It is not clear, from the file you posted, what you are doing exactly …

do you simply type .x test2.C ?

In that file you have some executable code outside any macro (named or unnamed).
This is really weird… Do you execute this code from the ROOT prompt ?

Variable in unnamed macro can be seen on the ROOT prompt after execution but variable in
a named macro are local.

It is always a good idea to run your code using Aclic (extension .c++) to make sure you
are writing legal C++ code.

Hmm, yeah, I do simply type .x test2.C. I execute the code from the ROOT prompt, yes.

And I have some executable code outside of the root macro because I have several ‘functions’ within the script that I call from the ‘main’ macro. I had to put them outside any macro as I need to access them in several functions.

If it’s easier, I can post my whole code so that you can have an idea of what I’m doing if that’s easier?

No need to post any code. I think the following micro example tells all:

File named.C:

Int_t outside = 5678;

void named() {
   Int_t inside = 1234;
}

Root session:

root [0] .x named.C
root [1] inside
Error: Symbol inside is not defined in current scope  (tmpfile):1:
*** Interpreter error recovered ***
root [2] outside
(Int_t)5678
root [3] 

[quote=“azou191”]Hmm, yeah, I do simply type .x test2.C. I execute the code from the ROOT prompt, yes.

And I have some executable code outside of the root macro because I have several ‘functions’ within the script that I call from the ‘main’ macro. I had to put them outside any macro as I need to access them in several functions.

If it’s easier, I can post my whole code so that you can have an idea of what I’m doing if that’s easier?[/quote]

No, please, do some work and send us the smallest working code sample reproducing exactly your problem.
In general it’s not a good idea to use global variables, but this has nothing to do with ROOT and solutions can be different. Of course, a macro can contain global variables, after all if you have to solve your problem at any price … why not.

Now, back to your original macro. The simplest way to fix it:

TCanvas *canvas = new TCanvas("canvas", "results", 50, 10, 1000, 600);
TPad *LHSPad = new TPad("LHSPad", "LHSPad", 0.02, 0.02, 0.79, 0.98);
TPad *RHSPad = new TPad("RHSPad", "RHSPad", 0.81, 0.02, 0.98, 0.98);

void test3()
{
    TH1F * hist = new TH1F("a", "b", 10, -2., 2.);
    LHSPad->cd();
    hist->Draw("lego");
}

void test2()
{
    LHSPad->SetFillColor(kRed);
    LHSPad->Draw();
    RHSPad->SetFillColor(kGreen);
    RHSPad->Draw();
    
    test3();
}

Hi couet and tpochep,

@ couet: Thanks for the help. :slight_smile:

@ tpochep: THAT solved all my troubles away! ^^ However, I still have a few questions.

  1. Like you requested, I tried to upload the smallest working code example that reproduces my problem, while still making sense. With the way the code is right now, it works but why does it not draw the pads correctly if I draw them before the main function?

  2. Also, why is it not a good idea to use global variables? :S

Thanks a lot for your help!

Cheers,
Atyab
test2.C (2.39 KB)

In C++ you can have ONLY declarations at the “top level” (let’s say, by the “top level” I mean “the scope outside of any function body or class definition”). In CINT’s interactive session you can have expressions and statements also, since CINT’s “top-level” is not the same thing as a C++ translation unit.
When you have an unnamed block { … } in your macro it seems to work as if you type these commands in a CINT’s interactive session. What CINT does with other global non-declarations constructs outside of { } … - we have to ask CINT’s developers.

I’m pretty sure you can find tons of articles/books/literature etc. about programming discussing this feature, topic too long to discuss here (and too many letters for me to type). You can start, for example, here:
http://en.wikipedia.org/wiki/Global_variable

It might be any macro error. You may try reinstalling the macros once. I used to face Macro errors very common and re-installing the macros mostly fixes those errors. So you may please try that to fix this.
medical billing and coding schools