Home | News | Documentation | Download

ROOT Fit output on a user created console

Dear ROOTers,

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


Here is what I got printed:

C:\Users\bellenot\rootdev>test_console
this gets printed

****************************************
Minimizer is Minuit / Migrad
Chi2                      =   9.2383e-23
NDf                       =            8
Edm                       =  1.84763e-22
NCalls                    =           30
slope                     =            4   +/-   7.48263e-13
offset                    =            2   +/-   1.99731e-12
this also gets printed

C:\Users\bellenot\rootdev>

Is it what you get? What is missing?

Mmh, no actually this is what I get

this gets printed
this also gets printed

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…

Sure, here you are: https://drive.google.com/file/d/1-bqHg5Ttk9iAzfnI13vNzUNu0ySREGvN/view?usp=sharing

Thanks. I’ll need some time to try and debug it…

Sure, of course, thank you so much!

Here we go, simply add gSystem->RedirectOutput("CONOUT$", "w"); as shown below:

	// Generate a console to display the output
	AllocConsole();
	freopen("CONOUT$", "w", stdout);
	freopen("CONOUT$", "w", stderr);
	gSystem->RedirectOutput("CONOUT$", "w");

sorry for the delay…

1 Like

It works just fine, thank you!

1 Like