Flush roofit log streams in jupyter

Hi,

my jupyter cells contain RooFit code like this:

c = TCanvas()
c.Divide(3,3)
tframe_true = obsTime.frame()
print "plot all B0"
data_both.plotOn(tframe_true,Cut("prodFlav==prodFlav::B0"))
print "plot all B0bar"
data_both.plotOn(tframe_true,Cut("prodFlav==prodFlav::B0bar"))
c.cd(1)
tframe_true.Draw()

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

sys.stdout.flush()

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

Thanks in advance,
Paul

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

Cheers,
Danilo

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

Cheers,
Danilo

fetched, cherry-picked, rebuilt, works!