Copying object from tfiles

Hello all,
I have a problem with TFile handling.
I created a Class which should serve as a container, its members are pointer to histograms. The purpose of such class is to get many histograms from many files, each containing few of those histograms. The point is that everything works if i do something like

TFile* digianalysis = new TFile(TString((*i).Path) + "/analysis.root");
std::ostringstream o;
o << (*i).HistogramNumber;
TString hn;
hn=o.str();
//--- analysis
Digi_Mask_Mod = new TH2D((*(((TH2D*)digianalysis->Get("Mask_Mod_"+hn)))));

the Histogram is loaded correctly in the classs, and I can use all of it but as soon as I add a

Everything stop working since it seems that the object is not present anymore, I understand if I used a pointer to the object, but making a new Th2D I thought could solve the problem.
I also tried using Clone() and Copy() methods, helping myself with temporary TH2D but it did not solve the problem.

So question is, is there a way to copy in the memory the object stored in a TFile so that when closing it I can still access the aforementioned object?

Try:
Digi_Mask_Mod->SetDirectory(0);

It seems to solve the problem, but could you be a little bit more descriptive on how this should solve the problem?, so I can learn from this error :wink:

Thanks a lot

http://root.cern.ch/root/html/TDirectory.html#TDirectory:AddDirectory
http://root.cern.ch/root/html/TH1.html#TH1:AddDirectory
http://root.cern.ch/root/html/TH1.html#TH1:SetDirectory
Is this enough “descriptive” :question: :mrgreen:

BTW. I think you can also try:
Digi_Mask_Mod->SetDirectory(gROOT);