Hello All,
I am trying to loop through many root files and print some numbers from each file. Here is a pared down version of my macro:
Int_t x,nosingles;
const char filename[100];
TFile *file;
for (x=12;x<25;x=x+2){
sprintf(filename,"R%i.root",x);
file = TFile::Open(filename);
nosingles = Singles->GetEntries("energy>0.0");
nosingles = Singles->GetEntries();
printf ( "%i %s\n",nosingles,filename);
file->Close();
}
However, when I run it I get a “segmentation violation”.
Any idea what I am doing wrong? Thanks in advance.
Karthik.
The code should have been
instead of const char filename[100];
Hello, how did you define the object “Singles”?
Singles is a tree in the root file I am trying to open.
I noticed that if I do not close the file before opening the next one, the macro seems to work ok.
pcanal
March 11, 2011, 4:32pm
5
Hi,
You probably also need to delete the TFile object itself. Int_t x,nosingles;
TString filename;
for (x=12;x<25;x=x+2){
filename.Form("R%i.root",x);
TFile *file = TFile::Open(filename);
nosingles = Singles->GetEntries("energy>0.0");
nosingles = Singles->GetEntries();
printf ( "%i %s\n",nosingles,filename.Data());
delete file;
}
Cheers.
Philippe
Hello Philippe,
That does not work either.
[code]$root -l -b -q test.c
root [0]
Processing test.c…
66584 R12.root
*** Break *** segmentation violation[/code]
Karthik.
[quote=“pcanal”]Hi,
You probably also need to delete the TFile object itself. Int_t x,nosingles;
TString filename;
for (x=12;x<25;x=x+2){
filename.Form("R%i.root",x);
TFile *file = TFile::Open(filename);
nosingles = Singles->GetEntries("energy>0.0");
nosingles = Singles->GetEntries();
printf ( "%i %s\n",nosingles,filename.Data());
delete file;
}
Cheers.
Philippe[/quote]
Int_t x,nosingles;
const char filename[100];
TFile *file;
TTree *Singles;
for (x=12;x<25;x=x+2){
sprintf(filename,"R%i.root",x);
file = TFile::Open(filename);
Singles = (TTree*)file->Get("Singles"); // or 'Singles = (TTree*)file->FindObjectAny("Singles");'
nosingles = Singles->GetEntries("energy>0.0");
printf ( "%i %s\n",nosingles,filename);
delete Singles;
delete file;
}
pcanal
March 11, 2011, 7:07pm
8
Hi,
Was there a stack trace indicating where the crash happened? Did you try running with valgrind:valgrind root.exe -b -l -q test.c
Did you try adding if statement and printing to make sure all the value are as exepected (i.e. file != 0, filename being what you expect, etc.). And as Pepe points out you could retrieve the object explicitly. The following being the recommend way:TFile *file = TFile::Open(filename);
TTree * Singles;
file->GetObject("Singles",Singles);
if (Singles == 0) { cerr << "No TTree object named Singles are in the file " << filename << "\n"; return ; }
Cheers,
Philippe.
Thanks Pepe, deleting the Singles worked.
[quote=“Pepe Le Pew”]
Int_t x,nosingles;
const char filename[100];
TFile *file;
TTree *Singles;
for (x=12;x<25;x=x+2){
sprintf(filename,"R%i.root",x);
file = TFile::Open(filename);
Singles = (TTree*)file->Get("Singles"); // or 'Singles = (TTree*)file->FindObjectAny("Singles");'
nosingles = Singles->GetEntries("energy>0.0");
printf ( "%i %s\n",nosingles,filename);
delete Singles;
delete file;
}
[/quote]