Question on saving histograms to TFile

If I have to create histograms first before I create TFile, how can I save the histograms to the TFile created later?
Thank you very much!

You can write the list of histograms from any existing directory in memory
to the new file, via
If mydir is the default directory in memory (gROOT), do

You can list what you have in mydir (gROOT) with


Thank you very much! Now I am ablle to save histograms to the new file.
But I have one more question: I think GetList() will get all objects in the memory. I also have some TCutG objects and normal C++ objects in memory. Is it possible to specify the class type so that I can only get histograms? But I don’t want to get histograms one by one because I have too many histograms to get.

If you want to write a subset of the objects in the mydir->GetList(), you can do something like:

TIter next(mydir->GetList()); TObject *obj; while ((obj=next())) { if (obj->InheritsFrom("TGraph")) continue; //skip all TGraph/TCutg obj->Write(); }