Hi,
I have a previously created root file with a lot of histograms. I want to read each histogram into a map indexed by it’s name, then close the file, do some processing on the histograms, and write them back into either the same or some other file. Reading and storing them looks good. But when I try to use them, I get a crash. Here is the code:
int main(int argc, char **argv)
{
TApplication app("root",&argc,argv);
map<string,TH1F*> hists;
{
string const filename = "histograms.root";
TFile file(filename.c_str());
TIter iter(file.GetListOfKeys());
TKey *key;
while (key = (TKey*)iter()) {
TObject *obj = key->ReadObj();
TH1F *h = dynamic_cast<TH1F*>(obj);
if (h) {
hists[h->GetName()] = h;
}
}
}
for (map<string,TH1F*>::iterator it=hists.begin(); it != hists.end(); ++it) {
TH1F *h = it->second;
assert(h);
cout <<h->GetTitle() <<" " <<h->GetEntries() <<endl;
}
}
The crash happens when I use GetTitle() and GetEntries() on the histograms in the second loop. Notice that if I bring the TFile outside the local scope it works. So it seems to me that the stored histograms are somehow linked to the file. However I found no documentation stating that this is the case based on reading references for TFile, TIter, TKey, and TH1. I don’t want to keep the file open, I want to read everything and store it in memory, because I might want to overwrite the same file while still holding on the histogram objects.
Could someone please let me know what I’m doing wrong. I would also appreciate if someone could point me to the appropriate documentation explaining this behavior so I won’t get stumbled on something like this again.
Thanks!