# Fail to retrieve histogram from file

**URL:** <https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429>\
**Category:** Newbie\
**Created:** [March 7, 2024, 11:57am UTC](https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429 "2024-03-07T11:57:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mykhailo\_Ieresko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mykhailo_ieresko/32/35205_2.png) [@Mykhailo\_Ieresko](https://root-forum.cern.ch/u/Mykhailo_Ieresko)\
**Post date:** [March 7, 2024, 11:57am UTC](https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429/1 "2024-03-07T11:57:37Z")

</div>

Good afternoon everyone!

I have a very naive question regarding retrieving a histogram from ROOT file.

I create a root file, generate the histogram, writing it to file and then trying to read it back to plot. The  
file is generated, I can access the histogram inside, but I can not plot it. However, if i’m using exactly  
the same commands in the terminal, the histogram magically appears. What am I doing wrong?

Please find below the relevant code.

```auto
string fileName = "demo.root";
string histoName = "genericHistoName";

void createFile(){

    std::unique_ptr<TFile> myFile(TFile::Open(fileName.c_str(), "recreate"));

}

void writeHisto(){

    unique_ptr<TFile> myFile(TFile::Open(fileName.c_str(), "update"));
    if(!myFile || myFile->IsZombie()) cout << "HOUSTON WE HAVE A PROBLEM" << endl;

    TH1D *h = new TH1D(histoName.c_str(), histoName.c_str(), 100, -4, 4);
    h->FillRandom("gaus",20000);

    myFile->WriteObject(h, h->GetName());
    delete h;

}

void readHisto(){

    std::unique_ptr<TFile> myFile(TFile::Open(fileName.c_str(), "read"));
    if(!myFile || myFile->IsZombie()) cout << "HOUSTON WE HAVE A PROBLEM" << endl;

    TH1D *h = myFile->Get<TH1D>(histoName.c_str());
    if(!h || h->IsZombie()) cout << "HOUSTON WE HAVE ANOTHER PROBLEM" << endl;
    h->Draw();

}

int main(){

    createFile();
    writeHisto();
    readHisto();

    return 0;

}

```

ROOT Version: 6.22/06  
Platform: Ubuntu 18.04

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 7, 2024, 12:14pm UTC](https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429/2 "2024-03-07T12:14:40Z")

</div>

In readHisto:

```auto
    // ...
    h->SetDirectory(0);
    h->Draw();
    // ...

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [March 7, 2024, 12:21pm UTC](https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429/3 "2024-03-07T12:21:03Z")

</div>

And if you create a standalone application:

```auto
void readHisto()
{
    std::unique_ptr<TFile> myFile(TFile::Open(fileName.c_str(), "read"));
    if(!myFile || myFile->IsZombie()) std::cout << "HOUSTON WE HAVE A PROBLEM" << std::endl;

    TH1D *h = myFile->Get<TH1D>(histoName.c_str());
    if(!h || h->IsZombie()) std::cout << "HOUSTON WE HAVE ANOTHER PROBLEM" << std::endl;
    h->SetDirectory(nullptr); // << --- to detach the histogram from the file
    h->Draw();
}

int main(int argc, char *argv[])
{
    TApplication theApp("App",&argc,argv);
    createFile();
    writeHisto();
    readHisto();
    theApp.Run();
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Mykhailo\_Ieresko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mykhailo_ieresko/32/35205_2.png) [@Mykhailo\_Ieresko](https://root-forum.cern.ch/u/Mykhailo_Ieresko)\
**Post date:** [March 7, 2024, 1:45pm UTC](https://root-forum.cern.ch/t/fail-to-retrieve-histogram-from-file/58429/4 "2024-03-07T13:45:00Z")

</div>

Thank you both for such quick replies! Your suggestions indeed fix my problem 🙂
