I’m currently working on a C++ windows form that uses a ROOT script, in such script some TGraph::Fit are performed.
I’ve allocated to the windows form a console, where I would like to have all the outputs of my code, as well as error notifications. This works fine with all the cout that I write, both in the main file and in headers, but I cannot figure out how to print ROOT fit output on the console.
Here’s a simplified version that describes my code.
void main(array<String^>^ args)
{
// Generate a console to display the output
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
std::cout << "this gets printed" << std::endl;
auto g = new TGraph();
double x, y;
//define data
for (int i = 0; i < 10; ++i) {
x = 0.5 * i;
y = 4 * x + 2;
g->SetPoint(i, x, y);
}
//Define fit function
//I could have written "pol1", or pol+the grade of the function
//Check SetPar + tab on ROOT command line for more options on initial fit values
auto f = new TF1("f", "[0]*x + [1]", 0, 5);
f->SetParNames("slope", "offset");
// auto f = new TF1("f","pol1", 0, 5);
// auto f = new TF1("f","gaus", 0, 5);
g->Fit(f);
std::cout << "this also gets printed" << std::endl;
}
On the console I get only the 2 cout as output, the usual output shown with ROOT fits is not displayed.
Is there a way to make TGraph::Fit use my console as output?
Thank you so much in advance,
Filippo
ROOT Version: 6.22.02 Platform: Windows 10 Compiler: Visual Studio 2019
The full code I’m running for this debug is the following:
Code
// basic C++ includes
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <corecrt_math_defines.h>
#include <msclr\marshal_cppstd.h>
// ROOT includes
#include <TROOT.h>
#include <TApplication.h>
#include <TSystem.h>
#include <TF1.h>
#include <TH1.h>
#include <TCanvas.h>
#include <TFrame.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TBrowser.h>
#include "RootFitScript.h"
// include form
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
//void Main(array<String^>^ args)
void main(array<String^>^ args)
{
// Generate a console to display the output
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
std::cout << "this gets printed" << std::endl;
auto g = new TGraph();
double x, y;
//define data
for (int i = 0; i < 10; ++i) {
x = 0.5 * i;
y = 4 * x + 2;
g->SetPoint(i, x, y);
}
//Define fit function
//I could have written "pol1", or pol+the grade of the function
//Check SetPar + tab on ROOT command line for more options on initial fit values
auto f = new TF1("f", "[0]*x + [1]", 0, 5);
f->SetParNames("slope", "offset");
// auto f = new TF1("f","pol1", 0, 5);
// auto f = new TF1("f","gaus", 0, 5);
g->Fit(f);
std::cout << "this also gets printed" << std::endl;
/*Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
GUI::MyForm form;
Application::Run(% form);*/
return;
}
But if you can get the full output I guess that’s, again, an issue with Visual Studio project options.
Edit: form.create() was just a copy - paste error, it is not present in the debug code
Oh, OK, I see. I didn’t create such application. This might be due to ROOT allocating its own console. But I would need the complete project in order to try it…