How to update an existing Tree?

Please read tips for efficient and successful posting and posting code

_ROOT Version: 6/20.02
_Platform: Mac OS Catalina
_Compiler: V 10.15.4


I ran the tutorial tree1.C (see
It created a Tree tree1.root with 10k events.
Next time I wanted to add to tree1.root another 10k events, so I ran again tree1.C.
I see that no new events have been added, instead it created the same Tree as before.
I have then changed the line TFile f(“tree1.root”,“recreate”); to TFile f(“tree1.root”,“update”); but the result remained the same. What is the modification I need to do in order to update my tree, i.e. to add to it new events? I couldn’t find an example of this in the tutorial.

Modify the “tree1r” routine:

   // ...
   TFile *f = new TFile("tree1.root", "UPDATE"); // open in "update" mode
   // ...

   // add events to the tree
   Int_t i0 = t1->GetEntries();
   for (Int_t i = i0; i < (i0 + 10000); i++) {
     gRandom->Rannor(px, py);
     pz = px*px + py*py;
     random = gRandom->Rndm();
     ev = i;
   // f->cd(); // where "t1" resides
   t1->Write(); // flush it to the file
   gROOT->cd(); // newly created histograms should go here

   //create two histograms
   // ...

Many thanks! Elemer

Just another question:
I see that on the updated root file the old tree remained. So, after the update I have 2 trees, the original and the updated one. How can I remove the original and keep only the updated tree?

Aux;1 aux;2 aux3;

The link you sent me for the answer contains unfortunately too much information to choose easily the right solution. So let me reformulate my question: could you please send me the line(s) I have to insert in the modified tree1.C macro to suppress from the tree1.root file the original tree before update, or if I need to do it outside the macro, the command to apply.
I intend to update my tree rather frequently with a small amount of new events, so the file will grow very rapidly if it will contain all the former and no more useful trees (old cycles). Thanks in advance,

“Cycles” are normal when trees with many entries are created so you should not care about them (it’s ROOT which manages them automatically so that you will get the very last one, unless you explicitly ask for some specific one).

Yes indeed: adding the new cycles the size of the tree remains almost the same except the first time when it doubled. This triggered my concern but I see now there is no reason for this concern. Many thanks again for your help!

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