# 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:** 17\
**Page:** 1

<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 11, 2018, 10:37pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/1 "2018-03-11T22:37:42Z")

</div>

# Context

I have software that processes data from a data file. The software pre-defines histograms. Users can define their own trees and histograms to write to disk. The software asynchronously writes the data to disk. This means that while data is writing to disk histograms are still being filled. I have confirmed that this doesn’t create any issues as all data is written to disk as expected.

# Problem

Now the tricky part. I’ve been trying to create a histogram / tree viewer to go along with the software. I’ve followed the advice listed [here](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608). But keep ending up with

- Error R\_\_unzip\_header: error in header. Values : 
- Segmentation faults
- R\_\_unzip: error -X in inflate (zlib) (where X is either 3 or 5)

While the advice that “zillions” of people are doing this means there’s a solution. I’m at a loss to find any concrete examples of where this is handled.

## Script

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

#include <iostream>

void viewOnline(TFile *file, const char *name) {
    TH1I *hist1 = nullptr;

    while(!hist1) {
        file->ReadKeys();
        delete file->FindObject(name);
        file->GetObject(name, hist1);
        usleep(100000);
    }
    hist1->Draw();
}

```

Compiled using `.L viewOnline.C+` in ROOT and called via `viewOnline(_file0, "histogram")`

# Environment

| Component | Value |
| --- | --- |
| Operating system | ubuntu |
| Kernel Version | 4.4.0-43-Microsoft |
| CMake Version | 3.5.1 |
| GCC Version | gcc (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609 |
| ROOT Version | 6.13/01 |
| GSL Version | 2.4 |

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [March 12, 2018, 7:58am UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/2 "2018-03-12T07:58:17Z")

</div>

Hi,

are you modifying the same file from two different processes (" delete file-\>FindObject(name);")?

Cheers,  
D

---

<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, 10:47am UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/3 "2018-03-12T10:47:24Z")

</div>

That is correct. I am using the method suggested in [this](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/6) forum post. I had to remove the `const` modifier from the TFile argument in `reading` because ReadKeys is not marked as `const` (see error below).

I tried omitting `delete file->FindObject(name);`, but end up with this error

```auto
Error in <TFile::ReadKeys>: reading illegal key, exiting after 1 keys

```

If I use the method described in [this](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/7) response, then I can view histograms as expected.

I should note that I’ve also read the the threads [here](https://root-forum.cern.ch/t/real-time-ttree-reading/18842) and [here](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/6).

### Error when using const TFile

```auto
In file included from input_line_12:9:
././viewOnline.C:11:9: error: member function 'ReadKeys' not viable: 'this' argument has type 'const TFile', but function is not marked const
        file->ReadKeys();
        ^~~~
/opt/root/v6-11-02-1447-g38586de/include/TDirectoryFile.h:105:24: note: 'ReadKeys' declared here
   virtual Int_t ReadKeys(Bool_t forceRead=kTRUE);
                       ^
In file included from input_line_12:9:
././viewOnline.C:13:15: error: no matching member function for call to 'GetObject'
        file->GetObject(name, hist1);
        ~~~~~~^~~~~~~~~
/opt/root/v6-11-02-1447-g38586de/include/TDirectoryFile.h:78:35: note: candidate function not viable: no known conversion from 'const TFile' to 'TDirectoryFile' for object argument
   template <class T> inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for i...

```

---

<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();
}

```

---

<div class="post-metadata">

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

</div>

Hi,

modifying the same file from 2 different processes is not allowed. Reading a file which is being written is supported (it is there to support real time acquisition systems for example) _but_ at the cost of warnings which may be prompted by the system.

Cheers,  
D

---

<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, 12:14pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/6 "2018-03-12T12:14:47Z")

</div>

Then I’m confused by the suggestion made [here](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/6). Why was this even suggested as a solution if that process isn’t allowed? What would be the correct way to handle this?

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [March 12, 2018, 1:14pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/7 "2018-03-12T13:14:03Z")

</div>

> [@Danilo](#):
>
> Reading a file which is being written is supported (it is there to support real time acquisition systems for example) but at the cost of warnings which may be prompted by the system.

Is there an example of this? I’ve never gotten it to work well enough. Is there an alternative output that is better for real time acquisition?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 12, 2018, 5:52pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/8 "2018-03-12T17:52:32Z")

</div>

> [@Danilo](#):
>
> modifying the same file from 2 different processes is not allowed. …

> [@svpaulauskas](#):
>
> Then I’m confused by the suggestion made here. Why was this even suggested as a solution if that process isn’t allowed? What would be the correct way to handle this?

Hi,

What is supported (and as far as I thought described in the link you used) is **writing by a single** process into a file and **reading by multiples** processes. (And for the reader to properly refresh their view on the updated files, they need to call ReadKeys).

Cheers,  
Philippe

---

<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, 6:08pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/9 "2018-03-12T18:08:52Z")

</div>

Just to be clear. I’m only writing to the via a single process, see [here](https://github.com/spaulaus/paass-laughing-conqueror/blob/dev/Analysis/Utkscan/core/source/RootHandler.cpp#L122). The write command is protected by a mutex.

I am reading from the file by

1. Opening it in ROOT: `root file.root`
2. Loading the script : `.L viewOnline.C+;`
3. Attempting to view the histogram: `viewOnline(_file0, 1);`

If I understand your suggestion properly, then if I simply remove

```auto
        delete file->FindObject(name);

```

From my original script, then it should work? I can confirm that removing this line still results in a segfault when trying to call

```auto
        file->GetObject(name, hist1);

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 12, 2018, 6:29pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/10 "2018-03-12T18:29:42Z")

</div>

The crash is a bit surprising. But nonetheless, I would actually expect the code to be like:

```auto
        delete file->FindObject(name);
        file->ReadKeys();

```

If this still fails, I would run the failing process under valgrind (with the option --suppressions=$ROOTSYS/etc/valgrind-root.supp) to get more information on the why of the crash.

Cheers,  
Philippe.

---

<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, 7:12pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/11 "2018-03-12T19:12:13Z")

</div>

I will check this order when I return home. Do you know of any open source examples of reading online files? I have a feeling I’m trying to reinvent the wheel here.

---

<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 13, 2018, 12:49am UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/12 "2018-03-13T00:49:54Z")

</div>

@pcanal : I determined the cause of the segfault was calling `Draw()` on the `TH1I` pointer that was still `NULL`.

I realize now my issue with

```auto
        delete file->FindObject(name);
        file->ReadKeys();

```

These commands only work if you have loaded the ROOT file that is actively being written. This **does not work** if you have opened an old file of the same name. When the file is `RECREATE`d,

> [@pcanal](#):
>
> the inode and location of the physical files changes and the TFile object that are looking at the old version are not informed that there is a ‘new physical file’ they should be looking.

I now have two working methods of reading a file from disk while a process is actively writing to it. Below I post both the example, and steps to run. I will mark the topic as solved.

# Working Example

```c++
///@file example-28273.C
///@brief A program that will generate data and asynchronously write it to disk.
///@author S. V. Paulauskas
///@date March 12, 2018
///@copyright Copyright (c) 2018 S. V. Paulauskas.
///@copyright All rights reserved. Released under the Creative Commons Attribution-ShareAlike 4.0 International License
#include <TFile.h>
#include <TH1D.h>

#include <iostream>
#include <mutex>
#include <thread>

void AsyncFlush(TFile *file, std::mutex *lock, unsigned int *loopId) {
    std::cout << "AsyncFlush - Now calling file->Write" << std::endl;
    file->Write(0, TObject::kWriteDelete);
    std::cout << "AsyncFlush - Now unlocking the file for writing in loop " << *loopId << std::endl;
    lock->unlock();
}

void WriteToDisk(TFile *file, unsigned int *loopID, std::mutex *lock) {
    if (lock->try_lock()) {
        std::cout << "WriteToDisk - we're creating the new thread in loop number " << *loopID << endl;
        std::thread worker0(AsyncFlush, file, lock, loopID);
        worker0.detach();
    }
}

void GenerateHistogram() {
    std::mutex lock;
    TFile *f = new TFile("test.root", "RECREATE");
    TH1D *hist = new TH1D("hist", "", 100, -2, 2);
    unsigned int loopCounter = 0;
    while (loopCounter < 40000) {
        hist->FillRandom("gaus", 10000);
        WriteToDisk(f, &loopCounter, &lock);
        loopCounter++;
    }
    while(!lock.try_lock())
        sleep(1);
    f->Write(nullptr, TObject::kWriteDelete);
    f->Close();
}

// Adapted from
// https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/6
// and follow up comment
// https://root-forum.cern.ch/t/read-histogram-tree-from-tfile-while-actively-writing-to-it/28273/10
void ReadHistogram(TFile *file, const char *name = "hist") {
    TH1D *hist1 = nullptr;
    delete file->FindObject(name);
    file->ReadKeys();
    file->GetObject(name, hist1);
    if(hist1)
       hist1->Draw();
}

// Adapted from
// https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/7
void ReadHistogram() {
    TFile *file = TFile::Open("test.root");
    TH1D *hist = nullptr;
    file->GetObject("hist", hist);
    if(hist)
        hist->Draw();
}

```

## Instructions for Running Script

### Terminal A - The Writer

```auto
user@localhost:> root
root [0] .L example.C+;
root [1] GenerateHistogram();

```

### Terminal B - The Reader

```auto
user@localhost:> root
root [0] .L example_C.so;
root [1] ReadHistogram();
root [2] ReadHistogram();
root [3] ReadHistogram();
...

```

### Terminal C - The Reader (alternate)

Executed after Writing has started in Terminal A

```auto
user@localhost:> root test.root
root [0] .L example_C.so;
root [1] ReadHistogram(_file0);
root [2] ReadHistogram(_file0);
root [3] ReadHistogram(_file0);
...

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 13, 2018, 4:03pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/13 "2018-03-13T16:03:20Z")

</div>

> [@svpaulauskas](#):
>
> This does not work if you have opened an old file of the same name. When the file is RECREATEd, I assume the memory address changes.

I am not sure what you mean here … Are those operation in the same process?

---

<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 13, 2018, 10:19pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/14 "2018-03-13T22:19:44Z")

</div>

@pcanal : They say pictures are worth 1k words. Here’s a drawing of what I mean.

 ![Sketch](https://root-forum.cern.ch/uploads/default/original/2X/e/ed559300ebf4e2647e1220c6b1a7d83f3f97ca7d.png)

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 14, 2018, 7:54pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/15 "2018-03-14T19:54:33Z")

</div>

> When the file is RECREATEd, I assume the memory address changes.

Right, I get it now. [I think you meant "I assumed that the inode and location of the physical files changes and the TFile object that are looking at the old version are not informed that there is a ‘new physical file’ they should be looking.]

Cheers,  
Philippe.

PS. What confused me is that `memory address` is usually refering to a location in the RAM (of a given process) rather than a location on the physical disk.

---

<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 14, 2018, 9:52pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/16 "2018-03-14T21:52:17Z")

</div>

@pcanal : Thanks for clarifying the language! I updated the post marked as the solution with your rephrasing.

I’m still learning a lot of the jargon surrounding memory / inodes / etc. I appreciate all your help!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 28, 2018, 9:52pm UTC](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/17 "2018-03-28T21:52:21Z")

</div>

Closed on March 28, 2018, 9:52pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
