Take histo from file and overwrite it!

Dear All,
I am facing this apparently very simple problem.
I have even googled on the Root Forum but to no avail…
The incriminated piece of code is the following:

  // TFile* fileSyst = new TFile("pngResults/Systematics.root", "recreate");
  // TH1F* Systematics = new TH1F("Systematics", "Systematics", 1000, -0.5, 999.5);
  // TH1F* Errors = new TH1F("Errors", "Errors", 1000, -0.5, 999.5);
  // Systematics->Fill(0);
  // Errors->Fill(0);
  // Systematics->Write(Systematics->GetName(), TObject::kOverwrite);
  // Errors->Write(Errors->GetName(), TObject::kOverwrite);
  // fileSyst->Close();

  TFile* fileSyst = new TFile("pngResults/Systematics.root");
  Systematics = (TH1F*) fileSyst->Get("Systematics");
  Errors      = (TH1F*) fileSyst->Get("Errors");
  TFile* fileSyst2 = new TFile("pngResults/Systematics2.root", "recreate");
  Systematics->Fill( selectionFlag+selectionFlag2, numberOfTotalJPsi    );
  Errors     ->Fill( selectionFlag+selectionFlag2, numberOfTotalJPsiErr );
  Systematics->Write(Systematics->GetName(), TObject::kOverwrite);
  Errors     ->Write(Errors     ->GetName(), TObject::kOverwrite);
  // Systematics->Write();
  // Errors     ->Write();
  fileSyst2  ->Close();

Basically, what I am doing is:

  1. open a file;
  2. take two histograms out of it;
  3. filling them;
  4. then overwriting them for bookkeeping;

Please, note that the commented code at the beginning is just a snippet to recreate the source tfiles…

The best thing would be to write on the same tfile, but this is not possible (not writable?), so I had to go on this roundabout way… But it doesn’t work the same and gives segmentation fault.

Any help is highly appreciated!!


  TFile *fileSyst = TFile::Open("pngResults/Systematics.root");
  if ((!fileSyst) || fileSyst->IsZombie()) { delete fileSyst; return; } // just a precaution
  TH1F *Systematics;
  fileSyst->GetObject("Systematics", Systematics);
  if (!Systematics) { delete fileSyst; return; } // just a precaution
  TH1F *Errors;
  fileSyst->GetObject("Errors", Errors);
  if (!Errors) { delete fileSyst; return; } // just a precaution
  delete fileSyst;

It turns out that the problem was that you cannot use the histograms if you have closed the file… Even if you have cloned it. Or at least, this is what I have gathered. Now it seems to be working properly at least…