Home | News | Documentation | Download

How to set different line color in TTree::Draw()

The code in $ROOTSYS/tutorials/tree/tree0.C

tree->Draw("a.e");  //energy of det a
tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same");

Now , we have two histograms in the same pad.But,how to let the different line with different color in code?

ROOT Version: ROOT 6.14
Platform: Ubuntu
Compiler: default

tree->Draw("a.e");  //energy of det a
tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same");

There is no “themp” pointer in the code .And although you use the code “tree->SetLineColor(kRed);”,we just change one line.

I usually do:

tree->Draw("a.e");  //energy of det a
tree->Draw("a.e>>histo","3*(-.2<b.e && b.e<.2)","same");
TH1F* h = (TH1F*) gROOT->FindObject("histo");

It works for me:

root [1] ntuple->Draw("px") 
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [2] htemp->SetLineColor(kRed)

Thanks a lot! I tried and it works.

1 Like

Thanks a lot!But the htemp is not declared in the code,Maybe the code is different.

In a macro:

   TH1F *htemp = (TH1F *)gPad->GetPrimitive("htemp");

I tried and it works in only one line.How can we change the other lines’ color in this way?Why can we find an object “themp” in the code ?

what do you mean ? can you post an example ?

As is shown in fifth reply,one line is green and the other is red.

ok, so what is wrong ? the screen dump is correct according to the commands you displayed on the left.

If we have


doing .ls shows that both histos are called htemp, and gPad->GetPrimitive("htemp") only retrieves one of the histos (the first one); the question is how to get the second one in this case (when the Draw line didn’t explicitly create named histos).

root [1] ntuple->Draw("px"); htemp->SetName("h1"); h1->SetLineColor(2);
root [2] ntuple->Draw("py","","same"); htemp->SetLineColor(3);