Monitor a histogram while running

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,

{
        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,

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?

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.

Try sth like:

//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.

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.

Yes, just take a look at this Read a tfile while writing with another process

A simple (untested) implementation would be:

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