How to plot from a C++ program

Please, take alook at my source code at … rDensity.C

It’s a very simple code that averages a physicsal quantity over 5 different components corresponding to 5 different input 2-D histograms.
My goal is to generate a bar chart representing the relative magnitude of the 5 calculated averages.
To this purpose I store each one of the 5 values into a 1-D histogram and then invoke Draw(bar3) to draw the bar chart.
I was recently shown this method interactively ad we had no problem.
But reproducng the same behaviour (that is plotting) from the program … is a challenge that I have already experienced. Basically my program dies after printing out taht a canvas has been created.
I was told Root is not given the time to draw as the program comes to an end abruptly.
Why isn’t the program waiting for the plot completion ?
Shall I save the calculated values to an external file and then plot from there i
nteractively ?

Thank you in advance,

  1. Do not call exit(0) in C++. Local objects with non-trivial destructors will not be destroyed correctly. It can work in C, but not in C++.
  2. You need some form of message loop - check how it’s done, for example, in $ROOTSYS/test/RootShower/RootShower.cxx, in main function.