Setting the axis range when using TTree::Draw

I am using the following script to loop over a set of files and draw histograms:

plot(TString& formu){
    int counter = 0;
    TCanvas* c = new TCanvas(formu,formu);
    c->Divide(6,4);
    TString cut("sum>8000 && sum<14000");
    for (int y=2;y<6;y++){        
        for (int x=2;x<8;x++){
            TString xs = TString::Format("%d",x);                    
            TString ys = TString::Format("%d",y);
            TString name = "./x_0"+xs+"_y_0"+ys+".root";
            MyTree t(name);
            c->cd(counter+1);
            gStyle->SetHistLineColor(counter);
            t.fChain->Draw(formu,cut);
            counter += 1;
        }
    }
}

It works fine, but when i try to set the range of the histograms via calling the script

plot("myvariable >> (200,0,2)")

Then only the last histogram is shown.

What am I doing wrong?

Also, I would like to know how to change the color of the histogram (in case I want to put the histograms from all files in the same plot), gStyle->SetHistLineColor seems to have no effect.

EDIT: I tried to fix it by drawing a copy of the temporary histogram htemp:

            //...
            t.fChain->Draw(formu,cut,opt);
            TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp"); // 1D
            htemp->DrawCopy();
            std::cout << htemp->GetName() << htemp->GetEntries() << std::endl;
            //...

The behavior of this script is quite strange. When I remove the line “htemp->DrawCopy()” it works and I get the expected output. However, with the line I get a segmentation violation.

EDIT2: It seems like only when i call the script without a range (e.g. plot(“myvariable”)) the Draw uses the temporary TH1F called htemp. When I put a range (e.g. plot(“myvariable >> (200,0,2)”)) htemp is undefined.

When you do:

plot("myvariable >> (200,0,2)")

your macro will do:

TCanvas(“myvariable >> (200,0,2)”,“myvariable >> (200,0,2)”);

thats not valid.

Why is this not valid?
It works without giving errors. My actual problem is the histograms.

try typing

TCanvas(“px>>(200,0,1)”,“px>>(200,0,1)”)

at the root prompt.

I did. No errors.

Anyhow, dont you want to help me with my actual problem? How can I set the range of these histograms?

Ah … ? in my case the TCanvas appears and goes away immediately both with ROOt 6 and root 5.
Any way realise that the canvas name as to be compliant with the C++ variable name conventions or you will not be able to access it. I will look at you macro now… but I will modify that.

EDIT: I do not Have “Mytree”… can you profile something I can execute ?

In that case create a real histogram and you will have the full control ont it and modify it after plotting.

TH1D *h …
t->Draw("…>>h");

TH1D *h = new TH1D(“h”, …

Yes :slight_smile:

Thanks for your efforts, but I still get the same effect. I changed the script to this:

plot(const TString& formu){
    int counter = 0;
    TCanvas* c = new TCanvas("x","x");
    c->Divide(6,4);
    
    //TH1D* h = new TH1D("h","h",200,0,2);  // A

    for (int y=2;y<6;y++){        
        for (int x=2;x<8;x++){
            TString xs = TString::Format("%d",x);                    
            TString ys = TString::Format("%d",y);
            TString name = "./results/x_0"+xs+"_y_0"+ys+".root";
            MyTree t(name.Data());
            c->cd(counter+1);
            TH1D* h = new TH1D("h","h",200,0,2);  // B
            t.fChain->Draw(formu + ">>h");
            counter += 1;
        }
    }
}

and it is still the same: In the end there is only the last histogram shown. I tried to create the histogram outside or inside of the loop (A/B) but it doesnt change.
Next thing I would try is to create one seperate histogram for each loop cycle, but i am not 100% sure how to do that (does “>>h” in draw refer to the variable name “h” or to the name of the object?).

Right before the line"TH1D* h …" add “gROOT->cd();”

Yep, that made it work :smiley: