Get histograms from two different files and plot on same canvas

I am 100% sure I am making an extremely stupid mistake here. I have a macro that is supposed to get one histogram from each file and plot them on the same canvas. what it actually does is plot them separately:


void compare(){

TFile *f1 = TFile::Open("hfile.root");

TFile *f2 = TFile::Open("PythiaWithTrackSystematicsCombined.root");

TFile *output = new TFile("output.root","Recreate");

gROOT -> cd();

output->cd();

TCanvas *c = new TCanvas("c");

c->cd();

TH1F *pTNominal = new TH1F("pTNominal","pT Nominal",100,50,7000);

if(f1){pTNominal = (TH1F*)f1->Get("h");

}

TH1F *pTSystematics1 = new TH1F("pTSystematics1","pT Systematics Variation 1",100,50,7000);

pTSystematics1 = (TH1F*)f2->Get("h1_central_rjet_pt_TRACKING1");

if(pTNominal){pTNominal->Draw();

std::cout<<"histogram exists"<<endl;}

pTNominal->SetLineColor(kBlue);

std::cout<<"in the loop"<<endl;

pTNominal->Write();

pTSystematics1->Draw("Same");

pTSystematics1->SetLineColor(kRed);

pTSystematics1->Write();

std::cout<<"in second loop"<<endl;

c->Update();
output->Write();
output->Close();

The output is two empty histograms with the name pTSystematics1 and pTNominal, and two filled histograms with the names they had in the original file, i.e. h and h1_central_rjet_pt_TRACKING1. What am I missing? Thanks for any help!


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,

here is how I would do this:

TCanvas *c = new TCanvas("c");

TFile *f1 = TFile::Open("hfile.root");
if (!f1 || f1->IsZombie())
{
    printf("Can't open f1!\n");
    return;
}
TH1F* pTNominal = static_cast<TH1F*>(f1->Get("h"));
if (!pTNominal)
{
    printf("Can't find 'h' in f1!\n");
    return;
}
pTNominal->SetDirectory(gROOT);
pTNominal->SetLineColor(kBlue);
pTNominal->Draw("hist"); // without "hist" the points will be drawn, and you seem to want a line, otherwise why set line color?
f1->Close();

TFile *f2 = TFile::Open("PythiaWithTrackSystematicsCombined.root");
if (!f2 || f2->IsZombie())
{
    printf("Can't open f2!\n");
    return;
}
TH1F* pTSystematics1 = static_cast<TH1F*>(f2->Get("h1_central_rjet_pt_TRACKING1"));
if (!pTSystematics1)
{
    printf("Can't find 'h1_central_rjet_pt_TRACKING1' in f2!\n");
    return;
}
pTSystematics1->SetDirectory(gROOT);
pTSystematics1->SetLineColor(kRed);
pTSystematics1->Draw("hist same");
f2->Close();

TFile *output = new TFile("output.root", "Recreate");
pTNominal->Write();
pTSystematics1->Write();
output->Write();
output->Close();
return;

But I don’t have your root files so I can’t test this…

1 Like

Hi! Thank you, that removes the empty histograms! They still plot in separate histograms though :frowning:

What do you mean by separate histograms? Can you post a screenshot maybe?

Contents of the file and one of the two histograms. It’s plotted, but I’d like both on the same axes in the same plot.

If you want to have two superimposed histograms stored in the file you need a different approach. Sorry, I thought you wanted to have these superimposed histograms plotted on screen and to write two separate histograms in the file. You probably want to take a look at the THStack class in this case.