File close is very slow

I have an output class that is essentially a histogram container with methods for filling the histograms and then writing them to a file. I have one file for each systematic variation in my analysis and > 100 histograms per file, so there are a great deal of histograms generated overall. The code that generates and fills the histograms runs rapidly, but the saving takes quite some time. The save function is contains:

file->cd();
map<Str, TH1D*>::iterator it = histos.begin();
for(; it != histos.end(); it++) {
it->second->Write();
}
for(uint ih = 0; ih < datahisto.size(); ih++) {
  datahisto.at(ih)->Write();
}
event_tree->Write();
cout<<"Writing file: "<<file->GetName()<<endl;
delete file;

The writing of the individual histograms and the event_tree are done rapidly, but after the cout statement the program sits for some minutes closing or deleting the file (the behavior is the same if I do f->Close()). Is the time taken to close the file simply a function of the number of histograms? If I remove the delete (or f->Close() ) line, the files are written rapidly but then the code stops after the writing of all the files, as though it is performing the same cleanup operation at the end of running. Is there a way to speed up this process?

Thanks

[quote] Is the time taken to close the file simply a function of the number of histograms?[/quote]Yes.

[quote]if I remove the delete (or f->Close() ) line, the files are written rapidly but then the code stops after the writing of all the files, as though it is performing the same cleanup operation at the end of running.[/quote]Yes. If you do not explicitly close the file, they will be closed for you toward the end of the process.

Cheers,
Philippe.