Why this plot not working

Hi,

I got the following error message by running a very simple macro

void Plotx()
{
  TFile *f = new TFile("$HOME/Desktop/test.root");
  TTree * T = (TTree*) f->Get("T");
  
  Double_t Close;
  T->SetBranchAddress("Close",&Close);
  
  Int_t* timeidx;
  Double_t* x;
  Int_t nentry = (Int_t)T->GetEntries();
  timeidx = new Int_t[nentry];
  x = new Double_t[nentry];
  
  for (Int_t i=0; i<nentry;i++)
  {
   T->GetEntry(i); 
   x[i]=Close;
   timeidx[i]= i;
  }
  
  
  TGraph * gr = new TGraph(nentry,timeidx,x);
  
  gr->Draw("test");
  
  
  delete f;
  
}

I know TGraph’s signature is const Double_t, but it should work for this case. And my example is no different from the examples in the manual. Thank for your helps.

regards,

gma

C++ cannot promote a Int_t* to a Double_t*. Change the type of timeidx from Int_t to Double_t

Rene

:blush: Many thanks, Brun. I was focused on the first argument.

regards,

Gang Ma

Hi, Brun

My code generates a blank graph and I don’t know why this is the case. Attached please find my root file. (I checked it to and looks good to me).

void Plotx()
{
  TFile *f = new TFile("$HOME/Desktop/test.root");
  TTree * T = (TTree*) f->Get("T");
  
  Double_t Close;
  T->SetBranchAddress("Close",&Close);
  
  Double_t* timeidx;
  Double_t* x;
  Int_t nentry = (Int_t)T->GetEntries();
  timeidx = new Int_t[nentry];
  x = new Double_t[nentry];
  
  for (Int_t i=0; i<nentry;i++)
  {
   T->GetEntry(i); 
   //cout<<Close<<endl;
   x[i]=Close;
   //cout<<"x_i is "<<x[i]<<endl;
   timeidx[i]= (Double_t)i;
  }
  
  //cout << x[10]<<endl;
  //cout << timeidx[10]<<endl;
  TGraph * gr = new TGraph(nentry,timeidx,x);
  
  //TCanvas *c1 = new TCanvas("c1","Graph options",200,10,600,400);
  gr->Draw("test");
  
  
  delete f;
 // delete [] timeidx;
 // delete [] x;
 // timeidx =0;
 // x = 0;
}

Many thanks.

regards,

gma
test.root (351 KB)

Replace the statement

gr->Draw("test"); by

gr->Draw("al");
Please read the doc TGraph::Draw

Rene

Hi,

And if you declare a pointer on an array of Double_t:

You must create your array like this (with Double_t instead of Int_t):

Cheers,
Bertrand.

many thanks for your helps. Still got a blank figure. I just read the user manual. Will take a look at the reference.

regards,

gma

Now it works. :smiley: