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.