How to plot TGraphErrors from a tree

Dear all,
I have created a tree with 3 branches x, y, yerror and then i want to read the branches and make a plot with errors.I do the following and it doesn’t work:

TBranch *TX=tree->GetBranch("X");
TBranch *TY=tree->GetBranch("Y");
TBranch *TYerror=tree->GetBranch("Yerror");

double x[N],y[N],yerr[N];

TX->SetAddress(&x);
TY->SetAddress(&y);
TYerror->SetAddress(&yerr);
    
   for (int k=0;k<N;k++)
    {
      x[k]=TX->GetEntry(k);
      y[k]=TY->GetEntry(k);
      yerr[k]=TYerror->GetEntry(k);
    }
   
   TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500);
   
   TGraphErrors *gr = new TGraphErrors(N,x,y,yerr);

the branches have the correct values (i can verify this with the TBrowser) but if i plot it like this i get crazy values
thanks
t

You can do:

TBranch *TX=tree->GetBranch("X"); 
TBranch *TY=tree->GetBranch("Y"); 
TBranch *TYerror=tree->GetBranch("Yerror"); 

double x[N],y[N],yerr[N]; 

TX->SetAddress(x); 
TY->SetAddress(y); 
TYerror->SetAddress(yerr); 

for (int k=0;k<N;k++) {
   x[k]=TX->GetEntry(k); 
   y[k]=TY->GetEntry(k); 
   yerr[k]=TYerror->GetEntry(k); 
} 

TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500); 

TGraphErrors *gr = new TGraphErrors(N,x,y,yerr); 

or simply

Long64_t N = tree.Draw("X:Y:Yerror","","goff");
TGraphErrors *gr = new TGraphErrors(N,tree->GetV1(),tree->GetV2(),tree->GetV3()); 

Rene

Hallo rene,
thanks for the quick reply.
The First solution did not work but the second one did.
I cannot keep thinking why the first solution didn’t work.can you please revise it and give me any more ideas?
also is there a way to smooth the lines in the TGraphError directly, i.e. take the mean of each 10 points, or i have to do it before in the trees.
thanks again,
cheers
thodoros

Could you post your exact first version? In particular, did you notice teh difference between my first version and yours, in particular
TX->SetAddress(x); TY->SetAddress(y); TYerror->SetAddress(yerr);
instead of
TX->SetAddress(&x); TY->SetAddress(&y); TYerror->SetAddress(&yerr);
Rene

yes i have noticed and implemented your change but had the same results.
so my exact code is the following:

// creating the tree:

sprintf(treename,"Radon Data",200000);
    sprintf(treetitle,"Tree of the Radon Data");

    TTree *tree = (TTree*)outRf->Get(treename);

    if (tree==NULL)
    {
	tree = new TTree(treename,treetitle);

	tree->Branch("labviewtime", &labviewtime, "labviewtime/D");
	tree->Branch("HumidityRel.", &data[0], "HumidityRel./D");
	tree->Branch("AirPressure", &data[1], "AirPressure/D");
	tree->Branch("Rn222", &data[2], "Rn222/D");
	tree->Branch("Rn222error", &data[3], "Rn222error/D");
	tree->Branch("Temperature", &data[4], "Temperature/D");
    }
    
}
// i fill the Ttree named tree..
}

const int N=nlines-1;
    double x[N],y[N],yerr[N];
    
      for (int k=0;k<N;k++)
      {x[N]=0;y[N]=0;yerr[N]=0;
      }
      
    TBranch *Tlabviewtime=tree->GetBranch("labviewtime");
    TBranch *TRn222=tree->GetBranch("Rn222");
    TBranch *TRn222error=tree->GetBranch("Rn222error");
    Tlabviewtime->SetAddress(x);
    TRn222->SetAddress(y);
    TRn222error->SetAddress(yerr);
    
   for (int k=0;k<N-1;k++)
    {
      x[k]=Tlabviewtime->GetEntry(k);
      cout<<x[k]<<endl;
      y[k]=TRn222->GetEntry(k);
      yerr[k]=TRn222error->GetEntry(k);
    }
    
   
   TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500);

   c1->SetFillColor(42);
   c1->SetGrid();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);

   //const Int_t n = 10;
   
   TGraphErrors *gr = new TGraphErrors(N,x,y,0,yerr);
   
   gr->SetTitle("Radon");
   //gr->SetMarkerColor(4);
   //gr->SetMarkerStyle(21);
   //gr->SetLineColor(4);
   gr->Draw("LPA");
   c1->Update();

hope this helps
cheers

Hi,

TBranch::GetEntry returns the number of bytes read from the disk (after decompression), so doing x[k]=TX->GetEntry(k); is clearly not what you intended. Instead use:

TBranch *TX=tree->GetBranch("X"); 
TBranch *TY=tree->GetBranch("Y"); 
TBranch *TYerror=tree->GetBranch("Yerror"); 

double xval,yval,yerrval
double x[N],y[N],yerr[N]; 

// Here guessing that the branch contains each a single double.
TX->SetAddress(&xval); 
TY->SetAddress(&yval); 
TYerror->SetAddress(&yerr); 

for (int k=0;k<N;k++) { 
   TX->GetEntry(k);
   x[k] = xval;
   TY->GetEntry(k);
   y[k] = yval;
   TYerror->GetEntry(k);
   yerr[k] = yerrval;
} 

TCanvas *c1 = new TCanvas("c1","Radon",200,10,700,500); 

TGraphErrors *gr = new TGraphErrors(N,x,y,yerr);[/code]

Cheers,
Philippe.

yes this was the problem.
thanks a lot!!!