Reseting root

I am currently tweaking a macro which I execute repeatedly in root.
However after executing it 2 or 3 times in the same instance of root, it throws a seg fault.

Is there a way to delete all pointers and clear the memory after each run of the macro?

I have been told not to use gROOT->Reset() in a macro.



Indeed gROOT->Reset() is the call you are looking for and indeed you should not use within a function (but you can used in an unnamed macro). For example:root [] .x mymacro.C root [] ... root [] gROOT->Reset(); root [] .L mymacro.C


I think this is the right place to share my experience concerning “resetting” ROOT - I started using ROOT only half a year ago but have been somewhat unsatisfied with what I perceived ROOT’s inability to fully reset itself to a “blank” state without exiting the program. However, I think I found a solution to this problem that I would like to share with ROOT users’ community on this forum.

Very often one needs to execute the same script again and again and again and again… Usually this happens when one is working on improving how the plot looks etc. For obvious reasons, I assume that ROOT script in question is “unnamed”, i.e. it starts with just “{”, no name, no arguments.

In such cases it is very important to know how to reset ROOT completely.

However, executing gROOT->Reset(); in the beginning of such script is not enough, because ROOT would not free memory allocated on heap when commands such as TH1F* my_hist = new TH1F(whatever) are executed (pointers are freed but the actual memory is not). Memory leak is not the only problem in this case, as ROOT actually refuses to execute some commands in a reproducible way from one instance of such unnamed script execution to another, if not restarted.

An elegant solution to this “problem” seems to be to use the advantage of “context”, something I stumbled upon after thoroughly reading respective page of ROOT’s User’s Guide, where it is discussed that gROOT->Reset() is not enough.

In my experience, in order to execute the same unnamed script as many times as you need in the same session of ROOT, what solves the problem is the introduction of one more context to make ROOT “forget” everything and free memory allocated from heap automatically, i.e.



// the body of your script with as many “new” statements as needed


assuming this source code is in the file limbo.C, one can now do

.x limbo.C

as many times as needed.

I was wondering if there is a better solution to this “ROOT reset” problem.

BTW, of course, one can write a script that locates all dynamically allocated objects and then deletes them all (so not only the pointers but, also, the actual memory are both freed), but this additional pair of {} seems to solve the problem completely.

The best solution is to use named scripts to avoid local variables in the global name space.