Home | News | Documentation | Download

Fit Parameters from an array of Histogram on text file


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18
Platform: Ubuntu
Compiler: Not Provided


Good Afternoon,

I need some help with Fit Parameters. I have an array of histogram and I’m fitting them with the same function and now I want to write the fit parameters on text file for each histogram.
I tried to use a cicle

ofstream myoutput;
myoutput.open(“datafit.txt”);
if (myoutput.fail()){
cout << “Non posso aprire il file” << endl;
exit(1);
}

[…]

for (Int_t j=0; jGetNpar()&&jGetNpar(); j++){
float Temp = mw->GetParameter(j);
float Temp1 = mw1->GetParameter(j);
}

myoutput << Temp << “\t\t” << Temp1 << “\n” << endl;

[…]

but it doesn’t work. How can I solve this?

I guess @moneta can help you.

Hi @Elisabetta_Di_Franco,
What exactly is the error you get?

I’m not sure about the condition you are setting with jGetNpar(), is it possibly a typo?
Also keep in mind that the variables you define in the for loop have local scope so they won’t be declared when the loop is finished.
Cheers,
Vincenzo

for (Int_t j=0; j<GetNpar()&&j<GetNpar(); j++){ …

Sorry, I didn’ t paste the entire code…

I still think only one j<GetNPar() should be enough. You can try retrieving the array of parameters outside the loop and then write each of them to the file in the loop like this:

// Open file and fit function
[...]

// Retrieve the vector of fit parameters
auto pars = myTF1obj->GetParameters();

// Write the values to the file
for(Int_t j = 0; j < GetNPar(); j++){
    myoutputfile << pars[j]   << "\n";
}