Help? Cannot open TGraph in the current scope

I keep getting the error where it says:

  1. could not delete seasondependence.root
  2. Error cannot call TGraph

A detailed error message is in the attached picture. Can someone please help? I am a newbie. Thanks!

#include “Riostream.h”

void seasondependence() {

TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“seasondependence.C”,"");
dir.ReplaceAll("/./","/");
ifstream file1;
file1.open(Form("%seasondependence.txt",dir.Data()));

Double_t date1,charge1, error1;
Int_t nlines = 0;
TFile *f = new TFile(“seasondependence.root”,“RECREATE”);
TNtuple *ntuple1 = new TNtuple(“ntuple”,“data from ascii file”,“date1:y:z”);

TCanvas *c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);
c1->SetFillColor(kWhite);
c1->SetGrid();

  // TCanvas::Update() draws the frame, after which one can change it

c1->Update();
c1->GetFrame()->SetFillColor(kWhite);
c1->GetFrame()->SetBorderSize(12);
c1->Modified();

while (!file1.eof( )) {
file1 >> date1 >> charge1 >> error1;
if (!file1.good()) break;
if (nlines < 5) printf(“DateofYear1=%8f, charge(fC)=%8f, error=%8f\n”,date1,charge1,error1);
gr1 = new TGraph(date1,charge1,error1);
gr1->SetLineColor(kRed);
gr1->SetLineWidth(4);
gr1->SetMarkerColor(kRed);
gr1->SetMarkerStyle(21);
gr1->SetTitle(“a simple graph”);
gr1->GetXaxis()->SetTitle(“Days”);
gr1->GetYaxis()->SetTitle(“Charge Values (fC)”);
gr1->Draw(“ACP”);
ntuple->Fill(date1,charge1,error1);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();
f->delete();
}


It looks like a simple C++ mistake.

gr1 = new TGraph(date1,charge1,error1);

should be:

TGraph * gr1 = new TGraph(date1,charge1,error1);

There may be other problems, but that’s what I noticed.

Jean-François

[code]#include “TFile.h”
#include “TTree.h”
#include “TGraphErrors.h”
#include “TCanvas.h”

void seasondependence(void)
{
const char *data_file = “seasondependence.txt”;
const char *root_file = “seasondependence.root”;

TFile *f = TFile::Open(root_file, “RECREATE”);

TTree *ntuple1 = new TTree(“ntuple1”, “data from ascii file”);
ntuple1->ReadFile(data_file, “date1/D:y:z”); // “date1/D:y:z” or "date1:y:z"
ntuple1->Write();

TGraphErrors *gr1 = new TGraphErrors(data_file, “%lg %lg %lg”);
gr1->SetNameTitle(“gr1”, “a simple graph;Days;Charge Values (fC)”);
gr1->SetLineColor(kRed);
gr1->SetLineWidth(4);
gr1->SetMarkerColor(kRed);
gr1->SetMarkerStyle(21);
gr1->Write();

delete f; // automatically deletes “ntuple1”, too

TCanvas *c1 = new TCanvas(“c1”, “A Simple Graph Example”, 200, 10, 700, 500);
c1->SetGrid();
gr1->Draw(“ACP”);
c1->Modified(); c1->Update();
}[/code]
BTW. If you get an error “could not delete seasondependence.root” that means that this file (and/or the subdirectory in which you work) is read-only for you.

Thanks. It works!!