TFile and Segmentation violation

Could someone explain me why following code is giving segmentation violation:

[code]void processSample(vector<TH1D *> & histograms, const sample & samp, const vector & vec, bool (*cut)(const event &), const char * prefix) {

TFile file(samp.path.c_str());
TTree * tree = (TTree *) file.Get("HZZ4LeptonsAnalysis");

for (vector<plot>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
	string name = (*it).name + samp.name;
	string title = prefix + (*it).title;
	histograms.push_back(new TH1D(name.c_str(), title.c_str(), (*it).nbins, (*it).lbin, (*it).hbin));
}
cout << "Processing sample: " << samp.name << endl;
makeHistos(histograms, tree, vec, cut);

cout << histograms.at(0)->GetEntries() << endl; //WORKS UP TO HERE EVERY TIME
tree->Delete();
file->Close();
cout << histograms.at(0)->GetEntries() << endl; //WORKS ONLY WHEN PREVIOUS LINE IS COMM.

}[/code]

but when I comment line “file->Close();” everything works. There must be some strange mechanism behind which I do not understand.

Hi,

The following should work better:[code]oid processSample(vector<TH1D *> & histograms, const sample & samp, const vector & vec, bool (*cut)(const event &), const char * prefix) {

TFile file(samp.path.c_str());
TTree * tree = (TTree *) file.Get(“HZZ4LeptonsAnalysis”);

for (vector::const_iterator it = vec.begin(); it != vec.end(); ++it) {
string name = (*it).name + samp.name;
string title = prefix + (*it).title;
TH1D *h = new TH1D(name.c_str(), title.c_str(), (*it).nbins, (*it).lbin, (*it).hbin);
h->SetDirectory(0); // => Detach the histogram from the TFile otherwise they will be delete when the file is closed.
histograms.push_back(h);
}
cout << "Processing sample: " << samp.name << endl;
makeHistos(histograms, tree, vec, cut);
}[/code]Note that since you created the TFile on the stack, it will be automatically deleted (and hence closed) once the routine ends. Do not class ‘tree->Delete();’ you actually meant ‘delete tree;’ However it is unnecessary as the TTree object ‘belongs’ to the TFile and will be deleted when the TFile is closed. See the User’s Guide chapter on object ownership for more details.

Cheers,
Philippe.

Dear Philippe,
I think the User Manual should have a more clear explanation of the ownership property of the objects one reads from TFile.

In the current version (for 6.10) you have:

  1. Histograms, trees, and event lists created by the user are owned by current directory
  2. Objects that are members of these TROOT collections are owned by gROOT
  3. Objects created by another object, for example the function object (e.g.TF1) created by the TH1::Fitmethod is owned by the histogram.
  4. An object created by DrawCopy methods, is owned by the pad it is drawn in.

The only option that one could think applies is number 3, but it is very unclear in the text what does it mean to be created by something and the histogram example makes it even less clear. A File get function doesn’t typically “create” something the same way an histogram function creates a TF1.

IMHO the explanation should be less ambiguous

Cheers,
Stefano