# Take histo from file and overwrite it!

**URL:** <https://root-forum.cern.ch/t/take-histo-from-file-and-overwrite-it/35375>\
**Category:** Newbie\
**Tags:** root\
**Created:** [July 28, 2019, 11:48am UTC](https://root-forum.cern.ch/t/take-histo-from-file-and-overwrite-it/35375 "2019-07-28T11:48:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![siragoni](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@siragoni](https://root-forum.cern.ch/u/siragoni)\
**Post date:** [July 28, 2019, 11:48am UTC](https://root-forum.cern.ch/t/take-histo-from-file-and-overwrite-it/35375/1 "2019-07-28T11:48:27Z")

</div>

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:

```auto
  // 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");
  fileSyst->cd();
  fileSyst->Close();
  TFile* fileSyst2 = new TFile("pngResults/Systematics2.root", "recreate");
  fileSyst2->cd();
  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!!

Thanks,  
Simone

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 28, 2019, 12:28pm UTC](https://root-forum.cern.ch/t/take-histo-from-file-and-overwrite-it/35375/2 "2019-07-28T12:28:02Z")

</div>

```auto
  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
  Systematics->SetDirectory(gROOT);
  TH1F *Errors;
  fileSyst->GetObject("Errors", Errors);
  if (!Errors) { delete fileSyst; return; } // just a precaution
  Errors->SetDirectory(gROOT);
  delete fileSyst;

```

---

<div class="post-metadata">

**Author:** ![siragoni](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@siragoni](https://root-forum.cern.ch/u/siragoni)\
**Post date:** [July 28, 2019, 2:04pm UTC](https://root-forum.cern.ch/t/take-histo-from-file-and-overwrite-it/35375/3 "2019-07-28T14:04:15Z")

</div>

Thanks!  
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…

Thanks,

Simone
