Multiple TLegend entries from the same ntuple?


I’m creating plots with multiple graphs all drawn from the data in a single ntuple. Each is plotted in a different color.

The problem I’m having is getting TLegend to use these different colors. No matter where and how often i put Draw() commands (i.e., before each color change) all the entries have the same color marker… Does anyone have a fix for this?

Script attached - the root file is very large, so let me know if you really need it. I think this is a pretty generic problem though…

Thank you!
bfield.C (961 Bytes)


Root isn’t going to know which one of the draw statement is supposed to correspond to which color (you put all of the legend commands at the bottom).

I’m not a root developer, but it could work if you put AddEntries after the draw commands.

What will work is for you to draw into histograms (Draw(“bla>>hist1”)and use those histograms in the AddLegend command.

Good luck,

yes, before this version I tried the following:

… setup…
field->Draw(“bz:z”, “x==…”);
leg->AddEntry(field, “x==…”,“p”);

Even with AddEntry() and Draw() commands inside the scope of a color they all show up Orange (the last color I use) on the plot.

I’m not sure what you mean by drawing into a histogram, but I understand how that would fix the problem. Could you give me a couple lines of example code or point me to a documentation page?


I was thinking of something like

field->Draw("bz:z>>hist1", "x==....");
leg->AddEntry(hist1, "x==...","p"); 

This won’t work in non-compiled code (you’ll either have to declare hist1 first and then fill it or use gDirectory->Get() to get it), but should work in Cint).


thanks, problem solved :slight_smile: