Multiple histograms / TPad - TLegend issue


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.16/00
Platform: Ubuntu 18/04 LTS
Compiler: g++ 4:7.4.0-1ubuntu2.3


I want to produce 3 different plots all containing 4 histograms drawn on the same pad. I will also add a ratio plot at the bottom of the histograms. I wrote a macro which does close to what I want but has some problems.

On the 2nd plot, which is saved as Plot2.png, the histograms of the previous plot are still shown. Same issue with the Plot3.png. Histogram titles are not updated as Plot-2 and Plot-3 on the 2nd and 3rd plots.
Legends and ratio plots look ok in this version.

If I comment out else if statement and modify the condition in the if statement as if(i==0)
then I get only 4 histograms shown on the plot as I want. Ratios also look ok. Plot titles are ok. But the legends are messed up this time. This version is closer to what I want.

I tried so many variations hopelessly but did not come up with the exact outcome as I wished. The problem lies I guess in the last part of the code, inside the last two for loops. Do you have an idea ?
I am attaching the desired outcome as an image

Blockquote

void testmacro(){

    const int m = 4;
    const int n = 3;
    const char *histname[m] ={"hist 1", "hist 2", "hist 3", "hist 4"};

    gStyle->SetOptStat(0);

    TCanvas *c1 = new TCanvas("c1","",200,10,1000,1000);
    c1->cd();

    TPad *pad[2];

    pad[0] = new TPad("pad1","histogram pad",0.025,0.225,0.975,0.975);
    pad[1] = new TPad("pad2","ratio pad",0.025,0.025,0.975,0.225);

    pad[0]->Draw();
    pad[1]->Draw();

    TH1F *hist[m][n];
    TH1F *hist_ratio[m][n];

    TLegend *leg=new TLegend(0.75,0.55,0.85,0.88);
    leg->SetBorderSize(0);
    leg->SetTextSize(0.03);
    leg->SetFillColor(0);
    leg->SetTextFont(42);

    for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                    pad[0]->cd();
                    pad[0]->SetBottomMargin(0);

                    hist[i][j]  = new TH1F(TString::Format("Hist-%i-%i",i,j),TString::Format("Plot-%i",j+1),10,0,1);
                    hist[i][j]->FillRandom("landau");
                    hist[i][j]->SetLineColor(i+1);
                    hist[i][j]->GetYaxis()->SetTitle("Event count");

            }
    }

    for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++) {
                    pad[1]->cd();
                    pad[1]->SetTopMargin(0);
                    pad[1]->SetBottomMargin(0.3);

                    hist_ratio[i][j] = (TH1F*)hist[i][j]->Clone("");
                    hist_ratio[i][j]->Sumw2();
                    hist_ratio[i][j]->Divide(hist[0][j]);
                    hist_ratio[i][j]->GetYaxis()->SetNdivisions(505);
                    hist_ratio[i][j]->SetMinimum(0.7);
                    hist_ratio[i][j]->SetMaximum(1.3);
                    hist_ratio[i][j]->SetTitle("");
                    hist_ratio[i][j]->SetFillColor(i+1);
                    hist_ratio[i][j]->SetFillStyle(3004);
                    hist_ratio[i][j]->GetYaxis()->SetLabelSize(0.15);
                    hist_ratio[i][j]->GetXaxis()->SetLabelSize(0.15);
                    hist_ratio[i][j]->GetYaxis()->SetTitle("Ratio");
                    hist_ratio[i][j]->GetYaxis()->SetTitleSize(0.1);
                    hist_ratio[i][j]->GetYaxis()->SetTitleOffset(0.4);
                    hist_ratio[i][j]->GetYaxis()->CenterTitle();

            }
    }


    for (int j=0; j<n; j++) {
            for (int i=0; i<m; i++) {
                    pad[0]->cd();
                    pad[0]->SetBottomMargin(0);

                    if(i==0 && j==0){
                            hist[i][j]->Draw("hist");
                            leg->AddEntry(hist[i][j],Form("#color[%i]{%s}",i+1,histname[i]),"");
                    }

                    else if(i!=0 && j==0){
                            hist[i][j]->Draw("hist same");
                            leg->AddEntry(hist[i][j],Form("#color[%i]{%s}",i+1,histname[i]),"");
                    }


                    else { hist[i][j]->Draw("hist same");
                    }

                    leg->Draw();

                    pad[1]->cd();
                    pad[1]->SetTopMargin(0);
                    pad[1]->SetBottomMargin(0.3);

                    if(i==0){hist_ratio[i][j]->Draw("E2");}
                    else{hist_ratio[i][j]->Draw("E2 same");}


                    TLine *line1=new TLine(0,1,1,1);
                    line1->SetLineColor(kBlack);
                    line1->SetLineStyle(1);
                    line1->Draw();

            }

            c1->SaveAs(Form("Plot%i%s",j+1,".png"));
            c1->Modified();
            c1->Update();

    }
   }

I think clearing the TLegend object after each plot which is saved does the job. c1->Modified(); and c1->Update(); seem abundant because I get the same results without them. So the final version of the last loops

Blockquote

    for (int j=0; j<n; j++) {
            for (int i=0; i<m; i++) {
                    pad[0]->cd();
                    pad[0]->SetBottomMargin(0);

                    if(i==0){
                            hist[i][j]->Draw("hist");
                            leg->AddEntry(hist[i][j],Form("#color[%i]{%s}",i+1,histname[i]),"");

                    }

                    else { hist[i][j]->Draw("hist same");
                            leg->AddEntry(hist[i][j],Form("#color[%i]{%s}",i+1,histname[i]),"");

                    }

                    leg->Draw();

                    pad[1]->cd();
                    pad[1]->SetTopMargin(0);
                    pad[1]->SetBottomMargin(0.3);

                    if(i==0){hist_ratio[i][j]->Draw("E2");}
                    else{hist_ratio[i][j]->Draw("E2 same");}


                    TLine *line1=new TLine(0,1,1,1);
                    line1->SetLineColor(kBlack);
                    line1->SetLineStyle(1);
                    line1->Draw();

            }

            c1->SaveAs(Form("Plot%i%s",j+1,".png"));
            leg->Clear();
    }

You should create different canvases not different pads otherwise your plots will mix.

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