# Problem with Reading Root File

**URL:** <https://root-forum.cern.ch/t/problem-with-reading-root-file/12164>\
**Category:** ROOT\
**Created:** [March 29, 2011, 12:04pm UTC](https://root-forum.cern.ch/t/problem-with-reading-root-file/12164 "2011-03-29T12:04:33Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [March 29, 2011, 1:43pm UTC](https://root-forum.cern.ch/t/problem-with-reading-root-file/12164/2 "2011-03-29T13:43:18Z")

</div>

An expert’s advice is needed, why there is a “segmentation violation” in the code below. ![8-[](https://root-forum.cern.ch/uploads/default/original/1X/5d59c8d2c8bc4ec0349fdd9dd8b35e3713880a92.gif "Anxious")

```auto
{
  TFile *TPCTOFEfffile = TFile::Open("EFFTPCTOF.root", "read");
  
#if 1 /* 0 or 1 */
  // This method works well.
  TH1F *TPCTOFEFFDeltapp = (TH1F*)(TPCTOFEfffile->FindObjectAny("hEff;1"));
  TH1F *TPCTOFEFFDeltamm = (TH1F*)(TPCTOFEfffile->FindObjectAny("hEff;2"));
  TH1F *TPCTOFEFFAverage = (TH1F*)(TPCTOFEfffile->FindObjectAny("hEff;3"));
#else /* 0 or 1 */
  // This method should also work, I get these pointers initialized, but
  // then I get a "segmentation violation" in the "Draw" command below.
  // Always only the last call to "GetObject" gives a "valid" object,
  // pointers initialized before become "invalid".
  TH1F *TPCTOFEFFDeltapp, *TPCTOFEFFDeltamm, *TPCTOFEFFAverage;
  TPCTOFEfffile->GetObject("hEff;1", TPCTOFEFFDeltapp);
  TPCTOFEfffile->GetObject("hEff;2", TPCTOFEFFDeltamm);
  TPCTOFEfffile->GetObject("hEff;3", TPCTOFEFFAverage);
#endif /* 0 or 1 */
  
  TCanvas *d23 = new TCanvas("d23", "efficiency*acc", 0, 0, 950, 700);
  
  TPCTOFEFFDeltapp->Draw();
  TPCTOFEFFDeltamm->Draw("same");
  TPCTOFEFFAverage->Draw("same");
  
  d23->SaveAs("TPC_TOF_EFF_ACC.eps");
}
```

---

_[View the full topic](https://root-forum.cern.ch/t/problem-with-reading-root-file/12164)._
