Best way to fill, write and draw a TTree

I’m writing a data monitoring program which should process data files, fill the branches of a TTree with a set of monitoring variables, plot some of these variables according to user’s input from a GUI and then save the tree on a Root file for later use. I thought about implementing this sequence:
[li]create branches[/li]
[li]loop over all events and fill branches[/li]
[li]wait for the user to define which variable to plot, possibly with selection criteria, and then call TTree::Draw[/li]
[li]eventually repeat step 3 for other variables[/li]
[li]write the tree on file if the user asks to[/li][/ol]

I have some concerns about this workflow:
[li]what happens if during the loop in step 2 the size of the tree exceeds fMaxVirtualSize? There is still no Root file opened, so I’d guess that branches cannot be flushed to disk…[/li]
[li]if I swap steps 5 and 3 (e.g. save the tree on file before drawing anything) then only the last Fill value is plotted: for example, if I fill a branch 100 times and the last fill value is 10, then after writing the tree on the file TTree::Draw gives a histogram with 100 entries all with value 10 (mean 10, RMS 0). I think that this may have something to do with the flushing of baskets. Do I have to re-read the TTree from the file at this point, before drawing anything??[/li][/ul]

Maybe my workflow and the objects I use are not the best for achieving my goal (a monitoring program with the ability to do plots on the fly with selection criteria, like a TTree does, and which can save the monitoring data for later use). Any help/suggestion on the above topics is welcome. Thanks.

When you open a tree a file, linked to that tree, should be open if you want to avoid any memory size problem.

you mean you see only the last event ?

[quote=“couet”]When you open a tree a file, linked to that tree, should be open if you want to avoid any memory size problem.
Understood, thank you.

you mean you see only the last event ?[/quote]

I mean that calling TTree::Draw I get a histogram with 100 entries (correct) but every entry seems to be equal to the value used for the last call of TTree::Fill, since the histogram has mean equal to that value and RMS = 0.

I have put together a simple example showing the issue:

void test() {

  TFile *file = TFile::Open("test.root", "RECREATE");
  TTree *t1 = new TTree("aTree", "A tree");
  float f;

  t1->Branch("aBranch", &f);
  for (int j = 0; j < 10; j++) {
    f = j+1;


  delete file;


By running it I get:

*    Row   *   aBranch *
*        0 *         1 *
*        1 *         2 *
*        2 *         3 *
*        3 *         4 *
*        4 *         5 *
*        5 *         6 *
*        6 *         7 *
*        7 *         8 *
*        8 *         9 *
*        9 *        10 *
*    Row   *   aBranch *
*        0 *         1 *
*        1 *         1 *
*        2 *         1 *
*        3 *         1 *
*        4 *         1 *
*        5 *         1 *
*        6 *         1 *
*        7 *         1 *
*        8 *         1 *
*        9 *         1 *

After writing the tree on the file Scan returns wrong values (in this case it is always the first fill value, not exactly what I reported in my original post but nevertheless a wrong behaviour). Am I doing something wrong or is this a bug?


I am not sure I understand your description. I finished your routine by adding 'file->Write();'
then exited and executed:[code]$ root.exe -b -l test.root
root [0]
Attaching file test.root as _file0…
(class TFile *) 0x7fadd295f7e0
root [1] aTree->Scan()

  • Row * aBranch *

  •    0 *         1 *
  •    1 *         2 *
  •    2 *         3 *
  •    3 *         4 *
  •    4 *         5 *
  •    5 *         6 *
  •    6 *         7 *
  •    7 *         8 *
  •    8 *         9 *
  •    9 *        10 *

(Long64_t) 10
[/code]which seems fine to me. What are you doing/seeing differently?


Hi Philippe, what I want to do is to fill a TTree, save it to disk and then keep it in memory to let the user draw some histograms. My simple test script just shows that Scan prints different things before and after the call to TTree::Write, and also histograms drawn with TTree::Draw are different when drawn before or after TTree::Write.

Hi Nicola,

I see, I somehow missed the bottom part of your output (the 2nd output and the 2nd call to Scan)!

I can reproduce the problem, I am looking into it.


Hi Nicola,

You can work around the problem by adding: t1->SetBranchAddress("aBranch",0);right after the call to file->Write()[/code].

The problem is also fixed in the v5.34/00 and v6.02/00 patches branches and the master branch.


Thank you Philippe, your solution works. I had to tweak it a bit because t1->SetBranchAddress(“aBranch”,0) gives me this error:

so I used:


right after t1->Write. This makes my small example work.

However, the data monitoring program where the bug showed up the first time still did not work after implementing the fix. I noticed that accessing the variables of the TTree just after resetting the addresses (for example with Scan or Draw) somehow fixed the residual problem. So I put GetEntry(0) just after ResetBranchAddress and this magically fixed everything. I don’t have any idea about why it works, and I have not been able to reproduce this in the example, but now it works so I can move on.
Thanks again!