Updating a ROOT object in file creates multiple copies

Take the following script:

#include "TFile.h"
#include "TH1F.h"
int main()
  TFile * file = TFile::Open("file.root", "UPDATE");
  file -> ls();
  TH1 * h = (TH1*) file -> Get("h");
  h -> Print("all");
  h -> Fill(1.0);
  h -> Write();
  file -> Close();
  return 0;

After successive iterations of the script I see there are multiple copies of the object in the file:

TFile**		file.root	
 TFile*		file.root	
  KEY: TH1F	h;8	h
  KEY: TH1F	h;7	h
  KEY: TH1F	h;6	h
  KEY: TH1F	h;5	h
  KEY: TH1F	h;4	h
  KEY: TH1F	h;3	h
  KEY: TH1F	h;2	h
  KEY: TH1F	h;1	h

I feel uncomfortable with that. What I intend is to replace the existing version of the object in the file with a new version of it. How to do it?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

Try to use the kOverwrite option

Could you please provide a complete example?
Particularly, I see this problem:

error: ‘kOverwrite’ was not declared in this scope
   h -> Write(h -> GetName(), kOverwrite);

h->Write(0, TObject::kOverwrite);

Thanks, it works also for nameless objects

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.