Permission Problems when plotting from .txt file

For some reasons, I have to use root under vista, insead of ubuntu 12.04.
I installed root and everything seems to be fine.

I have a two column .txt file(1st column x, 2nd column y) and I would like to plot y=f(x).
I used the relevant example from http://root.cern.ch/root/html/tutorials/tree/basic.C.html

My code is

[code]#include "Riostream.h"
void basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// see a variant of this macro in basic2.C
//Author: Rene Brun

// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic.c”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sout.txt",dir.Data()));

Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“basic.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”, 5000, 1, 5000);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);

while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f\n”,x);
h1->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();

TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(4);
c->SetFrameFillColor(10);
}
[/code]

The previous macro is saved as basic.c and it’s run by using .x basic.c. My two column out.txt file is the following

500.00 1.98 550.00 2.13 600.00 2.27 650.00 2.42 700.00 2.57 800.00 2.88 900.00 3.19 1000 3.51 1100 3.84 1200 4.18 1300 4.53 1400 4.89 1500 5.26 1600 5.65 1700 6.04 1800 6.44 2000 7.27 2250 8.37 2500 9.53 2750 10.75 3000 12.04 3250 13.38 3500 14.78 3750 16.24 4000 17.76 4500 20.96 5000 24.38

The first weird thing is that there is no plot(although 27 points found). The y values on the first run are all 0

x=500.00, y=0.000000 x=550.00, y=0.000000 x=600.00, y=0.000000 x=650.00, y=0.000000 x=700.00, y=0.000000 found 27 pints

If I rerun the code again I always get a weird value on y and no plot again with a weird permission error.

SysError in <TFile::TFile>: could not delete basic.root(errno: 13)(Permission denied) x=550.00, y=0.000000 x=600.00, y=3244368.000000 x=650.00, y=3244368.000000 x=700.00, y=3244368.000000 found 27 points Warning in <TFile::Write>: file basic.root not opened in write mode

Any idea on what might be wrong?

Hi,

On Windows, you cannot open a file which is in use (already open). Adding delete f; after f->Write();

f->Write(); delete f; should solve the problem.

And for the weird values of y, you should simply correct your code, and add the missing ‘y’ argument to printf():

if (nlines < 5) printf("x=%8f, y=%8f\n",x,y);
Cheers, Bertrand.

Thank you very much for your answer!
If I do that for a root file it doesn’t seem to work…

[code]#include
#include
#include

include “TROOT.h”

include “TGraphErrors.h”

include “TStyle.h”

include “TMultiGraph.h”

include “TF1.h”

include “TLegend.h”

include “TPaveStats.h”

include “TArrow.h”

include “TLatex.h”

include “TPaveText.h”

include “TText.h”

include “TPavesText.h”

#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”

void Fpp0pa0() {
//*********************************************************
//Read data from an ascii file *
//and create a root file with an histogram and an ntuple. *
// read file $ROOTSYS/tutorials/tree/basic.dat$ *
// this file has 2 columns of float data *
// *
//Prints the .mca spectrum in a .pdf file *
//Performs fitting on the specified data range *
//Prints fitting parameter *
// *
//Authors : Brun Rene and Stamatopoulos N. Athanasios *
//*********************************************************

//!!!
//note that first you have to get rid of !
//the data info contained in .mca file !
//then make .mca file a two column file !
//with channel # on the first column !
//and .mca data on the 2nd and place the modified !
//ascii file in the same directory as the root micro !
//!!!

TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“Fpp0pa0.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sFpp0pa0",dir.Data()));//Reads 100kOhm.mca ascii file
Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“Fpp0pa0.root”,“RECREATE”);
TH1F *h2 = new TH1F(“h2”,“Fpp0pa0”, 8000, 1, 8000);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);

while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h2->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();
delete f;

gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);

TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(kYellow);
c->SetFrameFillColor(10);

h2->Draw();
h2->SetFillColor(kRed);
//h2->SetFillStyle();
h2->SetAxisRange(2,2000,“Y”);
//h2->SetFillStyle(3444);
//h1->Fit(“gaus”,“W”,NULL,350,800);
//h1->GetFunction(“gaus”)->SetLineColor(kRed);

//TPaveStats st = ((TPaveStats)(h1->GetListOfFunctions()->FindObject(“stats”)));

c->SetLogy(1);
c->SaveAs(“Fpp0pa0.pdf”);
}[/code]

I get the error

[quote]SysError in TFile::TFile: could not delete Fpp0pa0.root (errno: 13) (Permission denied
Warning in TROOT::Append: Replacing existing TH1: h2 (Potential memory leak).
Warning in TFile::Write: file Fpp0pa0.root not opened in write mode [/quote]

Hi,

Weird… Are you sure you run from a directory where you have r/w permission?

Cheers, Bertrand.

Yes I am sure!
It’s just a directory on MyDocuments and besides I am using administrator privilleges!

Could you try to run from a path without blank characters? (i.e. not “My Documents”) This may potentially cause problems on Windows (and if ROOT is installed in a location containing blank characters, this may cause problems as well)

Cheers, Bertrand.