# Read histogram from TFile while actively writing to it (with example)

**URL:** <https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273>\
**Category:** ROOT\
**Tags:** hist\
**Created:** [March 11, 2018, 10:37pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273 "2018-03-11T22:37:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![svpaulauskas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/svpaulauskas/32/13788_2.png) [@svpaulauskas](https://root-forum.cern.ch/u/svpaulauskas)\
**Post date:** [March 12, 2018, 11:01am UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/4 "2018-03-12T11:01:54Z")

</div>

I modified my script to call `TFile::Open()`, instead of taking a pointer to a TFile. It’s a modification from [this](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/6) post. According to Philippe, this is not the most efficient way to do this, see [here](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/9).

Thus far this is the most robust method I’ve found.

```auto
#include <TFile.h>
#include <TH1.h>

#include <iostream>

void viewOnline(const std::string &filename, const int &id) {
    const char *name = ("h"+std::to_string(id)).c_str();
    TFile *file = TFile::Open(filename.c_str());
    TH1D *hist1;
    file->GetObject(name, hist1);
    hist1->Draw();
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273)._
