TNtupla and TGraphErrors

Dear corooters,
there is a simple way to plot data of a TNtuple into a TGraphErrors?
I’ve found different solution for create and plot a TGraph, but nothing for TGraphErrors.
The code below return a blank canvas:

    TNtuple *nt = new TNtuple("nt", "nt", "pre:e_pre:r1:er1:r2:er2:r3:er3");
    nt->ReadFile("range.txt");
    nt->Draw("pre:r1");
    TGraphErrors *un = new TGraphErrors(nt->GetSelectedRows(), nt->GetVal(0), nt->GetVal(2), nt->GetVal(1), nt->GetVal(3));
    un->Draw("ap");

Thanks for your attention.
_ROOT Version:6-26
_Platform:Manjaro Linux
Compiler: Not Provided


You can read directly from a text file into a TGraphErrors.

Thanks for your response. I am well aware of this possibility. But I would like to use TNtuple because I have more possibilities to handle the data.

See the other constructors on the same page. Apart from a file, you can use arrays, vectors, other TGraphErrors or a histogram.

I’m trying to use this constructor:

TGraphErrors::TGraphErrors ( Int_t n,
const Double_t * x,
const Double_t * y,
const Double_t * ex = nullptr,
const Double_t * ey = nullptr
)

In fact it seems that it is the only way to create a TGraphErrors starting from a TNtuple. But it doesn’t seem to work.
I’m just looking for a way to figure out creating a TGraphErrors from a TNtuple

nt->Draw("pre:e_pre:r1:er1"); // "V1:V2:V3:V4"

Thanks for your response, but this command produce a TH3F object:

I’m looking for a TGraphErrors, in 2D

Yes, you abuse this feature (see “How to obtain more info from TTree::Draw” in the TTree::Draw class reference).
But you can then create your desired TGraphErrors out of it.

Thank you very much for your attention, I had already viewed the link you sent me and starting from that I tried to write the code linked above. In particular I found this example:
https://root.cern/doc/master/treegetval_8C.html
But the latter only works for TGraphs and not for TGraphErrors

nt->Draw("pre:e_pre:r1:er1", "", "goff"); // "V1:V2:V3:V4"
TGraphErrors *un = new TGraphErrors(nt->GetSelectedRows(), nt->GetV1(), nt->GetV3(), nt->GetV2(), nt->GetV4());
un->Draw("ap");

Thank you very much, your suggestions worked perfectly. The code appears to work only if this command is invoked:

nt-> Draw ("pre: e_pre: r1: er1", "", "goff");

In fact, if I just insert this command in my code it works perfectly:

    TNtuple *nt = new TNtuple("nt", "nt", "pre:e_pre:r1:er1:r2:er2:r3:er3");
    nt->ReadFile("range.txt");
    nt-> Draw ("pre: e_pre: r1: er1", "", "goff");
    TGraphErrors *un = new TGraphErrors(nt->GetSelectedRows(), nt->GetVal(0), nt->GetVal(2), nt->GetVal(1), nt->GetVal(3));
    un->Draw("ap");

I can’t understand why this command makes the code work.
In the documentation i found that The “goff” option only suppresses generating the graphics!
Thanks again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.