TTree Draw() and dump to TGraph

Hello root people,

I wish to use the TTree->Draw() fucntion to produce a TGraph. I what to use this function along with the “gOff” option.

How do I do it?

TGraph *mygraph = (TGraph*)gDirectory->Get("mygraph");

Is giving me funny behaviour later on when I wish to plot the graph, with axis not appearing, but a stats box appearing. Right now the following code is doing what I want…but I do not like the need to have a pad appear just to be a temporary holder.

TGraph temp;
temp = (TGraph*)gPad->GetPrimitive("Graph");
TGraph *mygraph = (TGraph*)temp->Clone("mygraph");

I recommend the following procedure

int n = tree->Draw("y:x","z>0","goff"); TGraph *g = new TGraph(n,tree->GetV1(),tree->GetV2()); g->Draw("ap");
for more details, see section "How to obtain more info from TTree::Draw"
at : … DrawSelect


Hi, is it possible to do this with only one variable? Ie I want to dump just Draw(“x”) onto a Tgraph

int n = tree->Draw("x","","",3950,0);
TGraph *graph = new TGraph(n, tree->GetV1(), tree->GetV2());

The issue is that the GetV2 does not reference anything so I get an error.

Many thanks

Try (see the TTree::Draw method description for more details): int n = tree->Draw("Entry$:x", "", "goff", 3950, 0);

I tried the solutions and I think that it draws a graph of entry number vs x value rather than a histogram of x values which is what I want?


If you want the histogram of the x variable simply do:


I want to take the histogram from Draw(x) and dump it onto something that I can then manipulate so that I can combine several of them. I’m having some trouble doing it with TH1F so I was hoping I could use TGraph as a workaround.

I’ve done the same by using eg Draw(y:x), dumping to a TGraph and then using TMultiGraph to construct graphs that are superimposed, I wanted to do the same but for a couple of 1D histograms.

The equivalent of TMultiGraph for histograms is called: THStack.

I am trying to implement TStack using the following code:

using namespace std;

void drawcompiledgraph(){

TFile *file250 = new TFile("250integrallist.root");
TTree *tree250 =(TTree*)file250->Get("tree250");
TH1F *histo250 = (TH1F*)gPad->GetPrimitive("htemp");

TFile *file275 = new TFile("275integrallist.root");
TTree *tree275 =(TTree*)file275->Get("tree275");
TH1F *histo275 = (TH1F*)gPad->GetPrimitive("htemp");

THStack *hs = new THStack("hs","Stacked");

However I get the following error

*** Break *** segmentation violation

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#5 0x0000000000000000 in ?? ()
#6 0x00002af8456e43e6 in THStack::Add(TH1*, char const*) ()
from /usr/local/root-v5.26.00-gcc3.4/lib/
#7 0x00002af847a3e951 in drawcompiledgraph() ()
from /home/labs/Linearity/FinalTreesMeanBaseline/./

Not sure how to fix?


TH1F *histo250 = (TH1F*)gPad->GetPrimitive("htemp")->Clone("histo250"); histo250->SetDirectory(0); // (0) or (gROOT) // ... TH1F *histo275 = (TH1F*)gPad->GetPrimitive("htemp")->Clone("histo275"); histo275->SetDirectory(0); // (0) or (gROOT)