How plot only fit result of the TGraph?

Long64_t graphN=0;
graphN=t4->Draw("y:x","z>0","goff");
g=new TGraph(graphN,t4->GetV2(),t4->GetV1());
g->Fit("pol1","QW","");
g->Draw("p same");

but this code is not printing the stat box on canvas.

how can I add stats of the fit?

Hi

We need a minimal working macro to inderstand your problem

Lorenzo

{ TFile *f=new TFile("c:\\test.root","read"); TTree *t=(TTree*)f->Get("TREE"); Long64_t graphN=0; TCanvas *c1=new TCanvas("c1","Graph",800,600); t->SetMarkerStyle(24); t->Draw("y:x:z","","colz"); gStyle->SetOptFit(1111); graphN=t->Draw("y:x","z>0","goff"); TGraph *g=new TGraph(graphN,t->GetV2(),t->GetV1()); g->Fit("pol1","QW",""); g->Draw("p"); c1->Update(); f->Close(); }
I’ve attached the test data and canvas exported in pdf. As is visible there are black points inside the circles that were used for the fit.
And it behaves strange with different set of data.
c2.pdf (20.4 KB)
test2.root (7.34 KB)
c1.pdf (18.6 KB)
test.root (6.7 KB)

Use the option “sames”

[quote=“couet”][quote]
but this code is not printing the stat box on canvas.
[/quote]

Use the option “sames”[/quote]
to what draw should I pass sames option?

if I do g->Draw("") it prints the lines;
I’ve also tried to do g->FindObject(“pol1”)->Draw(); it plots the fit line on the canvas and with
g->FindObject(“pol1”)->Draw(“same”); or “sames” option it plots the fit with on colored background without the statistics.
Actually the it is also strange that it fills the background of the plot as it is in c2.pdf.

The option col applies on 2d histogram. in you case you have a 3D one

do

t->Draw(“y:x”,“z”,“col”);

[quote=“couet”]The option col applies on 2d histogram. in you case you have a 3D one

do

t->Draw(“y:x”,“z”,“col”);[/quote]
As you suggested it plots opaque squares instead of open circles. But this part of the code works perfect:

t->SetMarkerStyle(24); t->Draw("y:x:z","","colz");

The problem is in the following code. It plots also the markers of the graph if I put Draw(“p”) or lines if option is “”. Is there any way to get rid of them.

gStyle->SetOptFit(1111);
graphN=t->Draw("y:x","z>0","goff");
TGraph *g=new TGraph(graphN,t->GetV2(),t->GetV1());
g->Fit("pol1","QW","");
g->Draw("p");

Also I’ve tried this way which plots without the markers, but I can’t figure out how to print out on canvas the statistics of the fit:

graphN=t->Draw("y:x","z>0","goff");
TGraph *g=new TGraph(graphN,t->GetV2(),t->GetV1());
g->Fit("pol1","QW","");
g->FindObject("pol1")->Draw("same");

Up to you…but that’s wrong … I my view it should be protected and not allowed.

I am a bit lost … if you to Draw(“p”) you get the markers … of course … that is what you are asking for with this command.

[quote=“couet”][quote]
As you suggested it plots opaque squares instead of open circles. But this part of the code works perfect:
t->SetMarkerStyle(24);
t->Draw(“y:x:z”,"",“colz”);
[/quote]
Up to you…but that’s wrong … I my view it should be protected and not allowed.

I am a bit lost … if you to Draw(“p”) you get the markers … of course … that is what you are asking for with this command.[/quote]
yes I know, but this is the only way not to plot lines of the plot, if I do t->Draw("") it plots the lines. But what I want is to plot only the fitted function with the statistics.

that code is a modification of what I found here:
root.cern.ch/root/html532/TTree.html#TTree:Draw%1

If option COL is specified when varexp has three fields:
tree.Draw(“e1:e2:e3”,"",“col”);
a 2D scatter is produced with e1 vs e2, and e3 is mapped on the color table.

simply I modified “col” with “colz” to print the colormap.

I think you want his:

{
   TFile *f=new TFile("test2.root","read");
   TTree *t=(TTree*)f->Get("TREE");
   Long64_t graphN=0;
   TCanvas *c1=new TCanvas("c1","Graph",800,600);
   t->SetMarkerStyle(24);
   t->Draw("y:x:z","","colz");
   gStyle->SetOptFit(1111);
   graphN=t->Draw("y:x","z>0","goff");
   TGraph *g=new TGraph(graphN,t->GetV2(),t->GetV1());
   g->Fit("pol1","QW","");
   TF1 *f1 = g->GetListOfFunctions()->FindObject("pol1");
   f1->Draw("same");
}

yes its close what I want, but there is no statistics.
And this code:
TPaveStats p1 = (TPaveStats) gPad->GetListOfFunctions()->FindObject(“stats”);
or
TPaveStats p1 = (TPaveStats) gROOT->GetListOfFunctions()->FindObject(“stats”);
and other combinations return null pointer.

could it be that I use Windows version?

also with the code you have provided I get colored background.

{
   TFile *f=new TFile("test2.root","read");
   TTree *t=(TTree*)f->Get("TREE");
   Long64_t graphN=0;
   TCanvas *c1=new TCanvas("c1","Graph",800,600);
   t->SetMarkerStyle(24);
   gStyle->SetOptFit(1111);
   graphN=t->Draw("y:x","z>0","goff");
   TGraph *g=new TGraph(graphN,t->GetV2(),t->GetV1());
   g->Fit("pol1","QW","");
   g->Draw();
   gPad->Update();
   TPaveStats *p = g->GetListOfFunctions()->FindObject("stats");
   t->Draw("y:x:z","","colz");
   TF1 *f1 = g->GetListOfFunctions()->FindObject("pol1");
   f1->Draw("same");
   p->Draw();
}

Thanks finally its doing what I wanted :slight_smile:

But what I’ve noticed if one is not passing a selection on 3rd value of the tree:
t->Draw(“y:x:z”,"",“colz”);
the background is colored with color of the z=0. Am I right?

that’s where the colz option in wrong in that context

Even with the col option it is the same. So is it wrong in TTree Draw description?
Also it is the same with your suggested way t->Draw(“y:x”,“z”,“col”);

I do not see this problem with others trees I have. For instance the one produce by hsimple.C in the root tutorials.
Something to be checked.

I see. That’s because you have negative content. See:

root.cern.ch/root/html/THistPainter.html#HP14

I will check