Reading Multiple root files and plotting one histogram

HOW to read multiple root files and overlay a particular histogram from all these files into one histogram. I m confused pls help…

You can get the histograms you need from the various files and group them into a THStack in order to plot them with axis’ scales automatically computed.
Something like:

auto f1 = new TFile("file1.root");
TH1F *h1 = (TH1F*)f1->Get("hist1");

… etc… for the other files. Then:

auto hs = new THStack();
hs->Add(h1);
hs->Add(h2);

etc … . Then:

hs->Draw();

2 Likes

can i do the same for TEfficiency plots?
and I m using c++

void overlayhist(){

    string fileName = "first.root";

    TFile *f1 = TFile::Open(fileName.c_str());




    string histName1 = "ABC/ABC/eTimingEfficiency";


    TEfficiency* h1 = static_cast<TEfficiency*>(f1->Get(histName1.c_str()));
    TCanvas* c1 = new TCanvas("c1","c1",1000,600);
    c1->cd(1);
    h1->SetTitle("Timing Efficiency");

    h1->Draw();
    gStyle->SetOptStat(0);
    TLegend *leg = new TLegend(0.6,0.7,0.9,0.9);
    leg->AddEntry(h1,"Number: 32");
    leg->Draw();

}

I m using this function to and it successfully plots one file how to add more histograms from other root files as well overlaying it

THStack works with histograms. may be you can get the histogram from TEfficiency ?
I guess @moneta can tell.

Hi,
TEfficiency will drow TGraphAsymErrors objects. You might use for this instead of THStack TMultiGraph.

Lorenzo

can you help me with some dummy code that I have posted above ? To bring more clarity

I guess you can retrieve the graphs using GetPaintedGraph.

Something like this should work

void overlayEfficiencies(){

   TMultiGraph * mg = new TMultiGraph(); 

// loop  input files 
{
    string fileName = "first.root";
  
    TFile *f1 = TFile::Open(fileName.c_str());




    string histName1 = "ABC/ABC/eTimingEfficiency";


    TEfficiency* h1 = static_cast<TEfficiency*>(f1->Get(histName1.c_str()));
    TCanvas* c1 = new TCanvas("c1","c1",1000,600);
    c1->cd(1);
    h1->SetTitle("Timing Efficiency");

    h1->Draw();
    auto g1 = h1->GetPaintedGraph(); 
    mg->Add(g1);
} 

// end loop 

// draw all the graphs 
mg->Draw();  

}

1 Like