# Real-time TTree reading

**URL:** <https://root-forum.cern.ch/t/real-time-ttree-reading/18842>\
**Category:** ROOT\
**Created:** [February 16, 2015, 11:37am UTC](https://root-forum.cern.ch/t/real-time-ttree-reading/18842 "2015-02-16T11:37:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [February 16, 2015, 11:37am UTC](https://root-forum.cern.ch/t/real-time-ttree-reading/18842/1 "2015-02-16T11:37:47Z")

</div>

Dear ROOTers,

I guess it must have been discussed somewhere, but I have troubles finding. How to access TTrees in a file that are still being written? I guess I can make a loop which will be periodically calling GetEntries() and checking if the number has changed (still, I don’t know if it can change, or do I have to close and reopen the TTree?), but maybe there are better methods, including check if the TFile was closed or not, etc.

---

<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:** [February 17, 2015, 4:46pm UTC](https://root-forum.cern.ch/t/real-time-ttree-reading/18842/2 "2015-02-17T16:46:08Z")

</div>

From the TTree doc

```auto
   // How to write a Tree in one process and view it from another process
   // ===================================================================
   // The following two scripts illustrate how to do this.
   // The script treew.C is executed by process1, treer.C by process2
   //
   // ----- script treew.C
   // void treew() {
   // TFile f("test.root","recreate");
   // TNtuple *ntuple = new TNtuple("ntuple","Demo","px:py:pz:random:i");
   // Float_t px, py, pz;
   // for ( Int_t i=0; i<10000000; i++) {
   // gRandom->Rannor(px,py);
   // pz = px*px + py*py;
   // Float_t random = gRandom->Rndm(1);
   // ntuple->Fill(px,py,pz,random,i);
   // if (i%1000 == 1) ntuple->AutoSave("SaveSelf");
   // }
   // }
   //
   // ----- script treer.C
   // void treer() {
   // TFile f("test.root");
   // TTree *ntuple = (TTree*)f.Get("ntuple");
   // TCanvas c1;
   // Int_t first = 0;
   // while(1) {
   // if (first == 0) ntuple->Draw("px>>hpx", "","",10000000,first);
   // else ntuple->Draw("px>>+hpx","","",10000000,first);
   // first = (Int_t)ntuple->GetEntries();
   // c1.Update();
   // gSystem->Sleep(1000); //sleep 1 second
   // ntuple->Refresh();
   // }
   // }
```

Cheers,  
Philippe.

---

<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:** [February 17, 2015, 4:50pm UTC](https://root-forum.cern.ch/t/real-time-ttree-reading/18842/3 "2015-02-17T16:50:29Z")

</div>

See also [[Read a tfile while writing with another process](https://root-forum.cern.ch/t/read-a-tfile-while-writing-with-another-process/18608/1)

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [February 18, 2015, 2:38pm UTC](https://root-forum.cern.ch/t/real-time-ttree-reading/18842/4 "2015-02-18T14:38:22Z")

</div>

Thanks! That was what I needed.
