TCanvas Divide loop

Hi everyone, I’m trying to get a canvas divided in 20 pads containing each one a TH1D; I’m doing it by iteration inside a c++ program.
When I run the program, the result is an undivided canvas with only the last TH1D displayed (I suppose the other ones get overwritten).

These are my ROOT includes and declaration:

#include "TCanvas.h"
#include "TH1D.h"
#include "TPad.h"
#include "TApplication.h"
TApplication app("app",NULL,NULL);

In main() I first create a divided canvas:

    TCanvas *c1 = new TCanvas("c1","Name");
    c1->Divide(5,4);

then I fill the pads in the following loop:

    for(int i=0;i<20;i++){
        test = analysis(name_file[i]+ ".dat", &chi, &dn, &dt);

        //HIST
        c1->cd(i);
        TH1D *h = new TH1D("h", name.c_str(), 2, 0, 1);
        h->Fill(0.0, dn);
        h->Fill(1.0, dt);
        h->Draw("HIST");
        gPad->Update();
    }
app.Run(true);

where analysis() is a routine that calculates the weights of the channels.
Where did I mess up? :slight_smile:
Thank you!


_ROOT Version:_6.16/00
_Platform: Ubuntu on WSL


c1->cd(i + 1);
1 Like

Thank you! Now it works, but I get the same TH1D (the last of the iteration) in all the pads!

Try:

TH1D *h = new TH1D(Form("h_%d", i), name.c_str(), 2, 0., 1.);
1 Like

Thank you again :slight_smile: unfortunately the outcome is the same! :confused:

Try:

h->Fill(h->GetBinCenter(1), dn);
h->Fill(h->GetBinCenter(2), dt);
1 Like

Now it works fine!! Thank you for helping! :slight_smile:

1 Like

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