How to update an Histogram in a file?

Hello,
I have some problems doing a rather simple task…
In a compiled c++ program I want to open a rootfile, fill something to a histogram, and close it again.

//i create a file, in someother program:
TH1F * outputhisto;
TFile * output2;
output2 = new TFile("output.root","RECREATE");
outputhisto = new TH1F("histo","histo"10,10,20);
outputhisto->Write();
output2->Close();
//
//and i want to run this, to update the histogram:
output2 = new TFile("output.root","UPDATE");
//TH1F * outputhisto;
outputhisto = ((TH1F*) output2->Get("histo"));
outputhisto->Fill(2);
output2->Write();

Then i get a file with many histograms “histo”… does get give me a new Copy of the histogram?

Thx for hints…
Thomas

Hi,

Those are backup copies of the histograms (See User’s Guide for details). To avoid those backups copies, call output2->Write("",TObject::kOverwrite);

Cheers,
Philippe.

Thx for the fast and helpful answer… now it works :slight_smile:

pcanal, is there any way to make this the default for a given file, or globally?

Hi,

No you can not set a default value.

Cheers,
Philippe.

Does this delete the object in question?

I’ve been doing this with TTree’s, and it seems to have removed the tree’s key, but not the physical data, since my TFile keeps growing in size every time I do this.

If I do MyFile.ls() and then tree.GetZipBytes() for each tree in my file, I get nowhere near the size of the file on disk.

Is there any way to recover (‘defrag’? ‘cleanup’?) my ROOT files? Or at least is there a quick way to copy the ‘good’ objects out into another file?

Thanks,

  • Peter

[quote]Does this delete the object in question? [/quote]Technically it add to space used by the TKey to the list of free block of the TFile. For almost all objects except TTree, this means that the ‘old version’ of the object is no longer available from the file and that the disk space might be re-used by other object being written later one. For TTree this is more complex since it is decomposed into one TKey for the meta data (the TTree object itself) and many TBasket (which inherits from TKey) from the branches (i.e. you event data). The overwrite only ‘free up’ the TKey for the meta data.

[quote]If I do MyFile.ls() and then tree.GetZipBytes() for each tree in my file, I get nowhere near the size of the file on disk. [/quote]Humm … do you call TTree::Reset? Note that GetZipBytes only count the data from the baskets (i.e. minus the TTree meta data).

[quote]Is there any way to recover (‘defrag’? ‘cleanup’?) my ROOT files? Or at least is there a quick way to copy the ‘good’ objects out into another file? [/quote]Not directly. The only way is copy all the data from this file to a new file (which you can later rename using the old name). If you file contains only Histograms and Trees, you can use ‘hadd’ or TFileMerge to do this copying.

Cheers,
Philippe.