Hi,
I am not sure if this is a documentation problem or if I am doing something stupid, I will assume the latter for now.
On both pages 24 and 101 of the User Guide, my reading of the text leads me to believe that the functions TH1::SetDirectory(0) (when used on a particular histogram) and TH1::AddDirectory(kFALSE) (used as a global switch) do essentially the same thing. I appear to have found a case where they don’t.
I have a class, DiObjectSpectrum, that inherits from TSelector. Within its Begin() function, I open a file as follows:
After checking if it’s a Zombie, I then call a function (ClusterEff is a std::map<TString,TH1F*>, or inputtype in the code below)
This then calls yet more functions:
void DiObjectSpectrum::InputMap(TFile& input, inputtype& inputmap, TString type, TString offline)
{
FillMapFromFile(input, inputmap, "LJets"+type+offline+"_Efficiency", "OL"+type+"LJets");
//... more of the same
}
and finally the function FillMapFromFile retrieves the histograms:
void DiObjectSpectrum::FillMapFromFile(TFile& inputfile, inputtype& inputmap, TString histname, TString maptag)
{
TH1F* hist = NULL;
inputfile.GetObject(histname.Data(),hist);
if (hist)
{
hist->SetDirectory(0);
inputmap.insert(make_pair(maptag,hist));
}
else cout << "Didn't find histogram named " << histname << endl;
}
To my mind, from reading the Users Guide, calling hist->SetDirectory(0)
should “detach” it from the file, meaning the function-call-from-a-function-call-from-a-function-call is not a problem. But it is, and this code seg faults.
The solution I found is to call TH1::AddDirectory(kFALSE);
before any of the code I’ve pasted. Seems to work. In which case, AddDirectory and SetDirectory seem to do different things in this case. Who is right, me or the documentation?
Cheers,
Mike