Different/Unwanted results when executing macro repeatedly

Suppose I have two root files, both containing a histogram hist. If I wanted to plot both of them into a single figure, I would use something like this:

{
    canvas = new TCanvas("canvas", "My Canvas");
    canvas->SetFillColor(10);

    gStyle->SetOptStat(0);

    f = new TFile("file1.root", "READONLY");
    hist->SetNormFactor(1.0);
    hist->SetLineColor(4);
    hist->Draw();

    f = new TFile("file2.root", "READONLY");
    hist->SetNormFactor(1.0);
    hist->SetLineColor(2);
    hist->Draw("same");
}

If I run the above code in a fresh root session with .x plot.c (where plot.c is the name of the program above), I get the expected result. However, if I repeat the command .x plot.c, I only get a figure displaying one of the two histograms.
What am I missing?

Thank you very much in advance.

Did you compile the macro before you execute it?

In a root session, What’s the output of :

.L plot.c

?

Also, if I were you, I’d use two different variables for the two different root files:

f1= new TFile("file1.root", "READONLY");
f2= new TFile("file2.root", "READONLY");

No, I didn’t compile the macro.
Also, there isn’t any output of .L plot.c.

While we’re at it, do you know how to ‘switch’ between the files, meaning making one or the other the ‘active’, to which variables (such as hist) refer to? Or is it necessary to re-read the whole file if I’d want to make it the active one?

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