# Monitor a histogram while running

**URL:** <https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287>\
**Category:** ROOT\
**Created:** [November 30, 2016, 4:25pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287 "2016-11-30T16:25:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![betterwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/betterwang/32/11724_2.png) [@betterwang](https://root-forum.cern.ch/u/betterwang)\
**Post date:** [November 30, 2016, 4:25pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/1 "2016-11-30T16:25:28Z")

</div>

Dear Experts,

Is there a way to monitor a histogram while running a script?  
For example, while I run the following code in a terminal,

```auto
{
        TH1D * h = new TH1D("h", "h", 10, 0, 10);
        for ( int i = 0; i < 1000000; i++ ) {
                sleep(5);
                h->Fill(2);
        }
}
```

Can I draw “h” from another terminal?  
I can definitely write it to a file, but it will generate 1000000 histograms, which I don’t want.

Is there an elegant way to know the content of a histogram while running?

Thanks,

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [November 30, 2016, 5:23pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/2 "2016-11-30T17:23:27Z")

</div>

Are you looking for the following?

`{
        auto c = new TCanvas;
        TH1D * h = new TH1D("h", "h", 10, 0, 10);
        for ( int i = 0; i < 1000000; i++ ) {
                sleep(1);
                h->Fill(2);
                h->Draw();
                c->Update();
        }
}
`  
What do you mean with “another terminal”? Do you want to add some IPC code? If so, why?

---

<div class="post-metadata">

**Author:** ![betterwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/betterwang/32/11724_2.png) [@betterwang](https://root-forum.cern.ch/u/betterwang)\
**Post date:** [November 30, 2016, 6:11pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/3 "2016-11-30T18:11:41Z")

</div>

No, I don’t want to monitor the histogram all the time.  
Because that will significantly reduce the processing speed.  
I would like to monitor it whenever I want without slowing down the processing loop.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [November 30, 2016, 6:58pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/4 "2016-11-30T18:58:04Z")

</div>

Try sth like:

```auto
//Save code as test.cpp file and run as root -l test.cpp+
#include <TCanvas.h>
#include <TH1D.h>
#include <TThread.h>
#include <TRandom.h>

void *filler(void *hist) {
	TH1D* h = (TH1D*)hist;
	for ( int i = 0; i < 1000000; i++ ) {
		usleep(10000);
		h->Fill(gRandom->Gaus(5,1));
	}
	return NULL;
}

void test()
{
	TCanvas* c = new TCanvas();
	c->SetLogy();
	TH1D * h = new TH1D("h", "h", 100, 0, 10);
	h->Draw();

	TThread *filler_t=new TThread("filler_thread",filler,(void*)h);
	filler_t->Run();
}
```

Just click with the mouse on the canvas whenever you want to update.

---

<div class="post-metadata">

**Author:** ![betterwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/betterwang/32/11724_2.png) [@betterwang](https://root-forum.cern.ch/u/betterwang)\
**Post date:** [December 1, 2016, 2:58pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/5 "2016-12-01T14:58:35Z")

</div>

Not exactly what I want because you have to run the plotting in the same session.  
I know there are web pages, like CMS DQM, showing realtime root histograms to monitor the detector performance etc during the run.  
I wonder if I can do that in a light weight framework.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [December 1, 2016, 3:59pm UTC](https://root-forum.cern.ch/t/monitor-a-histogram-while-running/22287/6 "2016-12-01T15:59:01Z")

</div>

Yes, just take a look at this [Read a tfile while writing with another process](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/1)

A simple (untested) implementation would be:

```auto
//Program 1
void filler() {
   TFile* f = new TFile("test.root","RECREATE");
   TH1D* h = new TH1D("h", "h", 100, 0, 10);
   for ( int i = 0; i < 1000000; i++ ) {
      usleep(10000);
      h->Fill(gRandom->Gaus(5,1));
      if(i%1000==0)
      {
          h->Write("",TObject::kWriteDelete);
          f->SaveSelf();
      }
   }
   f->Close();
   delete f;
   return;
}

//Program 2 in another terminal
void readnow() {
    TFile* f = TFile::Open("test.root");
    new TCanvas();
    TH1D *h; f->GetObject("h",h);
    h->Draw();
}

```
