TApplication in a function, not in main

Hello,

I am trying to solve the following problem.

Data to be plotted is generated in main(), which I may not modify. The main() function, simplified, loops continuously and produces data in each event:

using namespace std;

#include <fstream>
#include <iostream>
#include <string>
#include <stdlib>

extern void plot(const int N, const double x[], 
                 const double y[]);

//=========================================================
int main()
{
int i = 0;
const int N = 3;
const  double xarray[N]={0.5, 0.7, 2.1};
double yarray[N];     

while(1)
{
 yarray[0]=random() / static_cast<double>(RAND_MAX);
 yarray[1]=random() / static_cast<double>(RAND_MAX);
 yarray[2]=random() / static_cast<double>(RAND_MAX);
 cout << "Data of event # " << i << " generated. "
      << "Now plotting...\n"; 

 plot(N, xarray, yarray);
 i++;
}

return 0;
}

I want to do all the plotting outside of main() by calling the function plot():


using namespace std;

#include <fstream>
#include <iostream>
#include <string>
#include <stdlib>

#include "TROOT.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TGraph.h"


//---------------------------------------------------------
void plot(const int N, const double x[], const  double y[])
{ 
int   dummy_int=0; 
char  dummy_char='d'; 
char* dummy_char_pointer = &dummy_char;
const unsigned int XC1=70, YC1=80, XC2=800+XC1, YC2=600+YC1;

TApplication theApp("A", &dummy_int, &dummy_char_pointer);
gROOT->Reset();

TGraph* gr = new TGraph(N, x, y);

TCanvas* c1 = new TCanvas("c1","Canvas 1", XC1, YC1, XC2, YC2);

gr->Draw("APL");
c1->Update();

theApp.Run();
}

Unfortunately, after plotting the first event, no further running is possible. When I quit the root window with the first graph hoping to re-create the canvas and draw the graph of the second event, the program terminates.

I have looked through examples and forum topics, and in none of them the file containing main() is ROOT-free. Due to large and complicated character of out data analysis files involving Fortran, C, C++, different Makefiles etc., I cannot modify the file with main() easily, but I still want to see events plotted one after another, either in the same canvas, or with canvas re-created.

Is it possible to get something like this running?

Help will be appreciated.

See examplebelow with two files plotmain.C and plot.C.
Compile/link with
g++ -o plot plotmain.C plot.C root-config --vflags --glibs
./plot

to go to the next call to plot double click in the canvas

Rene

[code]//file plotmain.C
using namespace std;

#include
#include
#include

extern void plot(const int N, const double x[],
const double y[]);

//=========================================================
int main() {
int i = 0;
const int N = 3;
const double xarray[N]={0.5, 0.7, 2.1};
double yarray[N];

while(1) {
yarray[0]=random() / static_cast(RAND_MAX);
yarray[1]=random() / static_cast(RAND_MAX);
yarray[2]=random() / static_cast(RAND_MAX);
cout << "Data of event # " << i << " generated. "
<< “Now plotting…\n”;

  plot(N, xarray, yarray);
  i++;

}

return 0;
}
[/code]

[code]//file plot.C
#include “TApplication.h”
#include “TCanvas.h”
#include “TGraph.h”

TApplication theApp(“A”,0,0);

//---------------------------------------------------------
void plot(const int N, const double x[], const double y[]) {
int dummy_int=0;
char dummy_char=‘d’;
char* dummy_char_pointer = &dummy_char;
const unsigned int XC1=70, YC1=80, XC2=800+XC1, YC2=600+YC1;

TGraph* gr = new TGraph(N, x, y);

TCanvas* c1 = new TCanvas(“c1”,“Canvas 1”, XC1, YC1, XC2, YC2);

gr->Draw(“APL”);
c1->Update();
c1->WaitPrimitive(); //double click in canvas to exit
}

[/code]

Rene:

thank you so much; I was trying to accomplish this kind of process monitoring in several ways for some time. I will implement full-scale now.

I slightly modified the plot.C, and post it here for the impatient ones.

// File plot.2.C

using namespace std;

#include "TApplication.h"
#include "TCanvas.h"
#include "TGraph.h"

TApplication theApp("A", 0, 0);

//--------------------------------------------------------------
void plot(const int N, const double x[], const double y[]) 
{
 const unsigned int XC1=70, YC1=80, XC2=400+XC1, YC2=400+YC1;
 TGraph* gr1 = new TGraph(N, x, y);
 TGraph* gr2 = new TGraph(N, y, x);

 static TCanvas* c1 = new TCanvas("c1","Canvas 1", XC1, YC1, XC2, YC2);
 c1->SetFillColor(5);
 static TCanvas* c2 = new TCanvas("c2","Canvas 2", XC1+400, YC1+400, XC2+400, YC2+400);
 c2->SetFillColor(10);

 c1->cd();  gr1->Draw("APL");   c1->Update();
 c2->cd();  gr2->Draw("APL*");  c2->Update();

 usleep(100000);
 c1->Clear();  c2->Clear();
       
 // c2->WaitPrimitive();    // Double click in canvas to exit
}