when you do
TFile f("mean.root");
at the end of the function, the object f is automatically deleted, and also all objects associated to this file.
By calling SetDirectory(0) you dissociate the histogram from the file, and then it is your responsibility to manage it (eg avoiding a memory leak)
Rene