I have a question about the AccessPathName command and how to know if a file exists.
I tried this command on the terminal: root [0] gSystem->AccessPathName(“nomefile”)
If the file exists, the terminal says: (Bool_t)0
If it doesn’t exist, it says: (Bool_t)1
This is what I was expecting to have… but I want to do a similar thing inside a program that I want to launch through .x name_of_program.C.
I want to do this check:
if (“commands to know if the file exists”) {
do some things…
}
but I don’t know how to use AccessPathName in this case.
Does anyone know the exact commands to put inside the program?
I need this because I must open many file in a loop (each one different only for a number, like file1, file2, file3 and so on) and so I need to index the file names.
Ok… but this not solve my problem with many files in a cycle… probably I miss something.
I tried this:
ifstream myfile1;
string filename1;
filename1 = "file5.txt";
myfile1.open (filename1.c_str());
if(gSystem->AccessPathName(filename1.c_str())){
cout << "The file doesn't exists\n";
} else {
cout << "The file exists\n";
for (int i=0; i<65; i++) {
if (i==0) {getline(myfile1,line1);}
else {myfile1 >> read >> what >> is >> inside;
cout << write << "\t" << what << "\t" << is << "\t" << inside << "\n";
}
}
}
and it worked perfectly.
But I need to do the same for many files to open and control inside a cycle… and I can’t do this in the same way (I can’t use: nomefile1, nomefile2…) then I tried this:
for(int j=0; j<34; j++){
ifstream myfile;
string filename;
sprintf(filename, "file%i.txt", j);
myfile.open (filename.c_str());
if(gSystem->AccessPathName(filename.c_str())){
cout << "The file doesn't exist\n";
} else {
cout << "The file exists\n";
for (int i=0; i<65; i++) {
if (i==0) {getline(myfile,line1);}
else {myfile >> read >> what >> is >> inside;
cout << write << "\t" << what << "\t" << is << "\t" << inside << "\n";
}
}
}
}
and it didn’t work.
The terminal gave me:
Error: sprintf parameter mismatch param[0] C u C:\path\of\program(92)
*** Interpreter error recovered ***
and the line 92 is the one with the command sprintf
Another time thank you (I was thinking on it from 3 days :D); sorry, but I’m not very expert with root and c++… I’m learning while I’m writing programs.
I need to plot an histogram for each file I opened.
When I made the program for each file I used:
TH1D *h5 = new TH1D("h5","Histogram for file 5",53,-26.5,26.5);
TH1D *h6 = new TH1D("h6","Histogram for file 6",53,-26.5,26.5);
TH1D *h7 = new TH1D("h7","Histogram for file 7",53,-26.5,26.5);
...
at the beginning and then, for each file, I wrote:
h5->Fill (variable);
mean = h5->GetMean();
rms = h5->GetRMS();
outputfile << "\t\t" << mean << "\t\t" << rms << " \n";
and then, at the end:
h5->Draw("");
Double_t par[9];
TF1 *g5 = new TF1("g5","gaus",-26.5,26.5);
g5->SetLineColor(2);
h5->Fit(g5,"R");
new TCanvas();
Now I need to modify all the fields with numbers (5,6,7,…); if is it possible to use a method like the one used to open all the files, how could I write it?
thank you
Thank you Wile_E_Coyote.
It solved my problem: I didn’t find the correct expression of Format command before your answer.
I have only one last problem, regarding TCanvas:
after I put the command h->Fit(g, "R");, if I don’t use new TCanvas();, obviously the program plots only the last histogram;
instead, if I use new TCanvas();, the program give me to me many Warning:
Warning in <TCanvas::ResizePad>: c1_n3 width changed from 0 to 10
Warning in <TCanvas::ResizePad>: c1_n3 height changed from 0 to 10
Warning in <TCanvas::ResizePad>: c1_n4 width changed from 0 to 10
Warning in <TCanvas::ResizePad>: c1_n4 height changed from 0 to 10
The last time I saw such errors was when the “system.rootrc” file was missing in the subdirectory returned by root-config --etcdir (i.e. often ${ROOTSYS}/etc). Actually, in that time, the whole subdirectory itself was missing due to some ROOT building / installing misconfiguration.
Unfortunately nothing changes adding that command.
In my opinion it’s strange because in the program I did before, I used many new TCanvas(); lines and all was ok… now that the same line is inside the loop, it gives me the warnings.
Answer for couet:
??? I made a program that creates the txt files… all works… the only problem is the plotting of histograms in new TCanvas.
Answer for Wile_E_Coyote:
Thank you very much! Your code works perfectly!
I have only a question: why the program plots the histograms, even if it lacks of Draw commands?
It’s only curiosity to better understand all
Is it due to #include commands?