# Saving Fit Parameters to a Latex Table and/or file

I have a fit function of the following form:

Double_t fitval = par[0] + par[1]*TMath::Cos(2.0*x[0]) + par[2]*TMath::Cos(4.0*x[0]);


I fit 8 histograms in a loop with this fitting function. I’d like to be able to save the parameters for each histogram once fitted to ideally a latex table, but if not a text file that I can then easily use to create a latex table.

Some starting point:

FILE *f = fopen("table.tex", "w");
fprintf(f,"%s\n",Form("%d", somefitpar));
fclose(f);


what am I to replace somefitpar with?

With the fit parameter value you want to put in your latex table.

Hey,

I managed to figure it out several days ago, and came up with the following:

 FILE *Fit_Statistics_29 = fopen("Fit_Statistics_29_version2.tex", "w");
fprintf(Fit_Statistics_29, "\\begin{tabular} { |l|| r | r | r| } \n");
fprintf(Fit_Statistics_29, "\\multicolumn{4}{c}{Fit Statistics Variable 29} \\\\ \n");
fprintf(Fit_Statistics_29, "\\hline");
fprintf(Fit_Statistics_29, "Histogram Name & $p_0$ & $p_1$ & p_2$\\\\ \n"); fprintf(Fit_Statistics_29, "\\hline");  The inside my main loop: if(Fit_Statistics_29!=NULL){ fprintf(Fit_Statistics_29, Form("%s", histogram_names_29[i].c_str()) ); for (int par=0 ; par< f1->GetNpar(); par++){ Float_t value = f1->GetParameter(par); Float_t value_error = f1->GetParError(par); fprintf(Fit_Statistics_29, " &$ %f \\pm %f \$", value, value_error);
}
fprintf(Fit_Statistics_29, " \\\\"); //end of row
fprintf (Fit_Statistics_29, "\n");
}


Outside of the main loop:

fprintf(Fit_Statistics_29, "\\hline");
fprintf(Fit_Statistics_29, "\n\\end{tabular}\n");


Putting it here incase anyone else finds it useful! I wasn’t aware that latex commands need an extra ‘\’ in front of them, and other newbies may not either.

Yes that’s how C++ works: https://stackoverflow.com/questions/10220401/rules-for-c-string-literals-escape-character

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.