1 // ------------------------------------------------------------------
5 // This class is used to produce tables using latex sintax. It can
6 // than print the table as Latex source code (to be pasted on a paper)
9 // The basic idea is that you have a stack of rows. You add columns
10 // one after the other with the SetNextCol method and than you insert
11 // this row. The SetNextCol method comes in different flavours to alow
12 // the insertion of different type of values (numbers with or without
13 // errors, strings...). Errors are rounded and written in scientific
14 // notation with the desired accuracy.
17 // 1. Make the class drawable
18 // 2. Implement vertical lines in ascii print
19 // 3. Improve output in HTML format
21 // Author: Michele Floris, CERN
22 // ------------------------------------------------------------------
24 #include "AliLatexTable.h"
27 #include "TObjString.h"
28 #include "TObjArray.h"
40 ClassImp(AliLatexTable)
42 AliLatexTable::AliLatexTable() : fNcol(0), fFormat(""), fRows(0), fCols(0), fNcolReady(0){
43 // default constructor
47 fRows = new TObjArray;
48 fCols = new TObjArray;
55 AliLatexTable::AliLatexTable(Int_t ncol, TString format) : fNcol(0), fFormat(""), fRows(0), fCols(0), fNcolReady(0){
56 // constructor, specify number of cols
60 fRows = new TObjArray;
61 fCols = new TObjArray;
68 AliLatexTable::~AliLatexTable() {
71 if (fRows) delete fRows;
72 if (fCols) delete fCols;
76 void AliLatexTable::SetNextCol(Int_t val){
77 // Set next column in current row - integer
79 snprintf(col, 200, " %d ", val);
84 void AliLatexTable::SetNextCol(Int_t val, Int_t err){
86 // Set next column in current row - int +- int
88 snprintf(col, 200, " $%d \\pm %d$ ", val, err);
93 void AliLatexTable::SetNextCol(Double_t val, Int_t scientificNotation, Bool_t rounding){
95 // Set next column in current row - double, specify resolution
99 if(scientificNotation >= 0) {
102 // cout << format << endl;
103 Double_t mantissa, exp;
104 GetMantissaAndExpBase10(val, mantissa, exp);
107 snprintf(format, 100," $%%%d.%df $", scientificNotation, scientificNotation);
108 snprintf(col, 200, format, mantissa);
111 snprintf(format, 100," $%%%d.%df \\cdot 10^{%%0.0f} $", scientificNotation, scientificNotation);
112 snprintf(col, 200,format, mantissa, exp);
122 snprintf(format, 100," $%%%d.%df $", -scientificNotation,-scientificNotation);
123 snprintf(col, 200, format , TMath::Nint(val*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation));
127 snprintf(col,200, " %f ", val);
132 void AliLatexTable::SetNextCol(Double_t val, Double_t err, Int_t scientificNotation, Bool_t rounding){
134 // Set next column in current row - double +- double, specify resolution
136 // scientific notation is used to determine number of
139 //if it is > 0 exp notation is used
144 if(scientificNotation >=0 ) {
146 Double_t mantissa, exp;
147 GetMantissaAndExpBase10(val, mantissa, exp);
149 Double_t mantissa_err, exp_err;
150 GetMantissaAndExpBase10(err, mantissa_err, exp_err);
152 Int_t nSigDigits =TMath::Nint(exp - exp_err);
153 if(scientificNotation != 0) nSigDigits = nSigDigits + scientificNotation - 1;
158 snprintf(format, 100," $%%%d.%df \\pm %%%d.%df $", nSigDigits, nSigDigits, nSigDigits, nSigDigits);
159 snprintf(col, 200,format, mantissa, mantissa_err/TMath::Power(10,exp - exp_err));
162 snprintf(format, 100, " $%%%d.%df \\pm %%%d.%df \\cdot 10^{%%0.0f}$", nSigDigits, nSigDigits, nSigDigits, nSigDigits);
163 snprintf(col, 200, format, mantissa, mantissa_err/TMath::Power(10,exp - exp_err), exp);
167 //cout << format << endl;
173 snprintf(format, 100, " $%%%d.%df \\pm %%%d.%df $", -scientificNotation,-scientificNotation,-scientificNotation,-scientificNotation);
174 snprintf(col, 200, format , TMath::Nint(val*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation), TMath::Nint(err*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation));
179 snprintf(col, 200, " $%f \\pm %f$ ", val, err);
184 void AliLatexTable::SetNextCol(Double_t val, Double_t err, Double_t errSyst, Int_t scientificNotation, Bool_t rounding){
186 // Set next column in current row - double +- double +- double, specify resolution
188 // if scientific notation is != -1 is used to determine number of
191 //if it is > 0 exp notation is used
193 //if it is < -1 number is truncated to the number of digits after
194 //point dictated by scientificNotation,
198 if(scientificNotation >=0 ) {
200 Double_t mantissa, exp;
201 GetMantissaAndExpBase10(val, mantissa, exp);
203 Double_t mantissa_err, exp_err;
204 GetMantissaAndExpBase10(err, mantissa_err, exp_err);
206 Double_t mantissa_errsyst, exp_errsyst;
207 GetMantissaAndExpBase10(errSyst, mantissa_errsyst, exp_errsyst);
209 Int_t nSigDigits =TMath::Nint(exp - exp_err);
210 if(scientificNotation != 0) nSigDigits = nSigDigits + scientificNotation - 1;
215 snprintf(format, 100, " $%%%d.%df \\pm %%%d.%df \\pm %%%d.%df $",
216 nSigDigits, nSigDigits, nSigDigits, nSigDigits, nSigDigits, nSigDigits);
217 snprintf(col, 200, format, mantissa,
218 mantissa_err/TMath::Power(10,exp - exp_err),
219 mantissa_errsyst/TMath::Power(10,exp - exp_errsyst));
222 snprintf(format, 100, " $%%%d.%df \\pm %%%d.%df \\pm %%%d.%df \\cdot 10^{%%0.0f}$",
223 nSigDigits, nSigDigits, nSigDigits, nSigDigits, nSigDigits, nSigDigits);
224 snprintf(col, 200, format, mantissa,
225 mantissa_err/TMath::Power(10,exp - exp_err),
226 mantissa_errsyst/TMath::Power(10,exp - exp_errsyst),
231 //cout << format << endl;
237 snprintf(format, 100, " $%%%d.%df \\pm %%%d.%df \\pm %%%d.%df $", -scientificNotation,-scientificNotation,-scientificNotation,-scientificNotation, -scientificNotation,-scientificNotation);
238 snprintf(col, 200, format ,TMath::Nint(val*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation), TMath::Nint(err*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation), TMath::Nint(errSyst*TMath::Power(10,-scientificNotation))/TMath::Power(10,-scientificNotation));
243 snprintf(col, 200, " $%f \\pm %f \\pm %f$ ", val, err, errSyst);
248 // void AliLatexTable::SetNextColPrintf(const char *va_(fmt), ...){
251 // const Int_t buf_size = 1024; // hope it's long enough
252 // char colBuffer[buf_size];
255 // va_start(ap, va_(fmt));
256 // Int_t n = vsnprintf(colBuffer, buf_size, fmt, ap);
258 // if (n == -1 || n >= buf_size) {
259 // Error("SetNextCol", "vsnprintf error!");
264 // // for(int iobj=0; iobj<num; iobj++){ //Loop until all objects are added
265 // // TH1 * obj = (TH1 *) va_arg(arguments, void *);
266 // // returnValue = returnValue && AddObject(obj,(char*)fOptMany.Data(), EColor(iobj+1));
268 // SetNextCol(colBuffer);
272 void AliLatexTable::SetNextCol(TString val){
274 // Set next column in current row - tstring
276 fCols->Add(new TObjString(val));
281 void AliLatexTable::InsertCustomRow(TString row){
282 // insert a full row from string. Make sure you have all the columns
283 fRows->Add(new TObjString(row));
288 void AliLatexTable::InsertRow(){
290 // Insert the row, based on all the individual columns
292 if ( fNcolReady != fNcol) {
293 Warning("InsertRow", "Wrong number of cols: %d (!= %d)", fNcolReady, fNcol);
297 for(Int_t icol = 0; icol < fNcol; icol++){
298 row = row + ((TObjString*) fCols->At(icol))->String();
299 if(icol != (fNcol-1)) row = row + " & ";
303 fRows->Add(new TObjString(row));
310 void AliLatexTable::InsertHline(){
312 // insert an horizontal line
313 fRows->Add(new TObjString("\\hline"));
318 Int_t * AliLatexTable::GetColWidths() {
320 // Compute the width of columns, for nice ascii printing
322 Int_t * col_widths= new Int_t[fNcol];
323 Int_t nrow = fRows->GetEntriesFast();
325 for(Int_t icol = 0; icol < fNcol; icol++){
326 col_widths[icol] = 0;
330 for(Int_t irow = 0; irow < nrow; irow++){
331 TString row(((TObjString*) fRows->At(irow))->String());
332 if(row.Contains("\\hline"))continue;
334 StripLatex(row, "ASCII");
336 TObjArray * cols = row.Tokenize("&");
337 if(cols->GetEntries() != fNcol) {
338 Error("GetColWidths", "Wrong number of cols in row %s: %d - %d", row.Data(), cols->GetEntries(), fNcol);
340 for(Int_t icol = 0; icol < fNcol; icol++){
341 Int_t w = ((TObjString *) cols->At(icol))->String().Length();
342 if (w>col_widths[icol]) col_widths[icol] = w;
350 void AliLatexTable::PrintTable(Option_t * opt){
352 // Print the table on screen. You can specify the format with opt.
353 // Currently supported:
355 // "ASCII" -> plain ASCII
356 // "HTML" -> HTML, to be improved
357 // "CSV" -> skips hline, usefult for importing in excell
358 // "TWIKI" -> skips hline, usefult for importing in TWIKI
360 if(TString(opt) == "ASCII" || TString(opt)=="HTML" || TString(opt)=="CSV" || TString(opt)=="TWIKI") {
362 Int_t nrow = fRows->GetEntriesFast();
364 Int_t * colWidths = GetColWidths();
366 Int_t total_lenght = 0;
367 for(Int_t icol = 0; icol < fNcol; icol++) total_lenght = total_lenght + colWidths[icol] + 2 ;
370 for(Int_t irow = 0; irow < nrow; irow++){
371 TString row = ((TObjString*) fRows->At(irow))->String();
372 if (row.Contains("\\hline")){
373 if (TString(opt)!="CSV" && TString(opt)!="TWIKI") {
374 for(Int_t il = 0; il < total_lenght; il++) printf("-");
379 StripLatex(row, opt);
380 TObjArray * cols = row.Tokenize("&");
381 if (TString(opt)=="TWIKI") printf(" | ");
382 for(Int_t icol = 0; icol < fNcol; icol++){
383 TString strTmp = ((TObjString *) cols->At(icol))->String();
384 if(TString(opt)=="TWIKI" || TString(opt)=="HTML"){
385 strTmp.ReplaceAll("AMPER","&");
387 const char * colstr = strTmp.Data();
389 if (TString(opt)!="CSV") {
390 snprintf(format, 200, "%%%ds", colWidths[icol] + 2);
392 snprintf(format, 200, "%%s");
394 printf(format, colstr);
395 if (TString(opt)=="CSV") printf(", ");
396 if (TString(opt)=="TWIKI") printf(" | ");
408 cout << "\\begin{tabular}{"<<fFormat<<"}"<<endl;
410 Int_t nrow = fRows->GetEntriesFast();
412 for(Int_t irow = 0; irow < nrow; irow++){
413 cout << ((TObjString*) fRows->At(irow))->String() << endl;
416 cout << "\\end{tabular}" << endl;
421 // void AliLatexTable::ParseExponent(TString &expo){
422 // // TString parseExponent = col;
423 // TRegexp re = "e[+-][0-9][0-9]";
424 // // cout << col << endl;
426 // if(expo.Contains(re)){
427 // Int_t index = expo.Index(re);
428 // TString replacement = "\\cdot 10^{";
429 // replacement = replacement + expo(index+1, 3) +"}";
430 // // cout << replacement <<" --- "<< endl;
431 // replacement.ReplaceAll("+","");
432 // // cout << "B: " << expo << endl;
433 // // cout << "RE " << expo(re) << endl;
435 // expo.ReplaceAll(expo(re), replacement);
436 // // cout << "A: " << expo << endl;
437 // // recursion to parse all exponents
438 // if (expo.Contains(re)) ParseExponent(expo);
440 // else Warning("", "Error parsing exponent");
443 void AliLatexTable::GetMantissaAndExpBase10(Double_t num, Double_t &man, Double_t &exp) {
445 // Helper used to get mantissa and exponent
447 exp = TMath::Floor(TMath::Log10(TMath::Abs(num)));
448 man = num / TMath::Power(10, exp);
450 // cout << "" << endl;
451 // cout << num << " = " << man << " x10^{"<<exp<<"} " << endl;
456 void AliLatexTable::StripLatex(TString &text, TString format) {
458 // Strip latex away for ascii and html printing. Replaces latex
459 // command with corresponding text/tags
461 text.ReplaceAll("\\cdot", "x");
462 text.ReplaceAll("$", "");
463 if (format == "ASCII") {
464 text.ReplaceAll("\\right>", ">");
465 text.ReplaceAll("\\left<", "<");
466 text.ReplaceAll("\\rangle", ">");
467 text.ReplaceAll("\\langle", "<");
468 text.ReplaceAll("\\pm", "+-");
469 } else if (format == "HTML" || format == "TWIKI") {
470 // the & is used to tokenize... Have to cheat here
471 text.ReplaceAll("\\right>", "AMPERrang;");
472 text.ReplaceAll("\\left<", "AMPERlang;");
473 text.ReplaceAll("\\rangle", "AMPERrang;");
474 text.ReplaceAll("\\langle", "AMPERlang;");
475 text.ReplaceAll("\\pm", "AMPERplusmn;");
477 if(text.Contains("multicolumn")) {
478 // cout << "col " << text.Data() << endl;
479 // in case of multicol span, replace first column with content and
481 TObjArray * array = TPRegexp("multicolumn\\{([^}]*)\\}\\{[^}]*\\}\\{([^]]*)\\}").MatchS(text); // Added double \\ because gcc 4 triggers hard error on unknown escape sequence. Hope it still works...
482 const TString content = ((TObjString *)array->At(2))->GetString();
483 Int_t nspan = ((TObjString *)array->At(1))->GetString().Atoi();
485 // cout << "ns " << nspan << ((TObjString *)array->At(1))->GetString() << endl;
486 // cout << "t " << text << endl;
488 for(Int_t ispan = 1; ispan < nspan; ispan++){
491 // cout << "t " << text << endl;
496 text.ReplaceAll("\\mathrm", "");
498 text.ReplaceAll("\\", "");
499 text.Strip(TString::EStripType(1), ' ');
503 void AliLatexTable::LoadTeXFromFileAndPrintASCII(const char * filename) {
505 // opens a file containing only a latex table and prints it on screen as ASCII
507 ifstream file (filename);
508 if (!file.is_open()) {
509 AliError(Form("Cannot open file %s", filename));
512 while (line.ReadLine(file)) {
513 if (line.Contains("begin") && line.Contains("tabular")) {
514 // We need to get and parse the format
515 // TPRegexp re("\\begin\\{tabular\\}\\{([^\\}]*)\\}");
516 TPRegexp re(".*begin{tabular}{(.*)}");
517 TObjArray * arr = re.MatchS(line);
518 if (arr->GetLast() > 0){
519 // cout << "Size: " << arr->GetSize() << " " << arr->GetLast() << endl;
521 TString subStr = ((TObjString *)arr->At(1))->GetString();
522 subStr.ReplaceAll("|","");
523 subStr.ReplaceAll(" ","");
524 subStr.ReplaceAll("\t","");
525 // subStr.ReplaceAll(" ","");
526 // cout << subStr.Data() << " " << subStr.Length()<< endl;
527 fNcol = subStr.Length();
532 // Skip stuff we don't parse
533 if (line.Contains("begin")) continue;
534 if (line.Contains("end")) continue;
535 if (line.Contains("tabular")) continue;
537 InsertCustomRow(line.Data());