Flush roofit log streams in jupyter


my jupyter cells contain RooFit code like this:

c = TCanvas()
tframe_true = obsTime.frame()
print "plot all B0"
print "plot all B0bar"

the printout in the cell’s output looks like this:

not so much of a problem for two lines only, but extremely annoying for the 18 plots i have in the real example.
Is there a way to flush the roofit log streams?
I tried

NB: in “normal” python, the messages appear in the right order.

Thanks in advance,

Hi Paul,

thanks for reporting about this aspect of the output capturing in notebooks. What happens in ROOTbooks, is that the output which is printed from compiled libraries, like the RooFit one, is captured and “pasted” in the streams the notebook after the execution of the cell.
As a result, the output coming from Python (e.g. “plot all B0”) and the one from C++ (e.g. “[#1] INFO:Plotting – RooTreeData::”) are not mixed one with each other.
Said that, we are aware that this is less then optimal and in fact a major refactoring of the output capturing is almost completed: this upgrade fixes this and other issues and will be ready for the next 6.07 release of ROOT.
I can backport even today the fix for this particular problem.
For an immediate usage I can also propose a workaround: I hope it can work for you.

from ctypes import *
libc = CDLL("libc.so.6") 

import ROOT
import sys

# Here we play with the Jupyter internals to interleave Python adn C++ output
tmpStream = sys.stdout
sys.stdout = sys.__stdout__

print "Py1"
libc.printf("From C\n")
print "Py2"

# here we remove the traces of our trick
sys.stdout = tmpStream


Hi Paul,

for your information, a fix to produce the behaviour you describe has been pushed to the repository: github.com/root-mirror/root/com … 65d92a7101


fetched, cherry-picked, rebuilt, works!