Re: [ROOT] Table output

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Tue Oct 23 2001 - 19:31:21 MEST


Hi Sidoti, 

On Tue, 23 Oct 2001 16:03:44 +0200 (MET DST)
"Sidoti Antonio tel. +39+0461 88 1525" <sidoti@science.unitn.it> wrote
concerning "[ROOT] Table output":
> Hi Rooters,
> I am wondering if there is a function (or a simple way) that generates
> HTML and/or LateX tables filled with arrays calculated in a .C root script?
>
> 
> Something  like:
> 
> Float_t x[n]  = {,,,};
> Float_t y[n]  = {,,,};
> gr = new THtmlTable(n,x,y,...,...);
> gr->Save(file_tex); // or file_html
> file_tex->Write();
> 
> ________________________
> x[1] | x[2] | ... | x[n]|
> y[1] | y[2] | ....| y[n]|
> etc...


I don't think anything like that exists, but it'd be easy to
implement: 

  class TableWriter : public TNamed {
  private:
    Int_t   fNCols; 
    Int_t   fNRows; 
    TArrayD fData;
  public: 
    TableWriter(const Char_t* name, const Char_t* caption, 
                Int_t ncols, Int_t nrows); 
    virtual void  AddRow(Int_t n, Double_t* data); 
    virtual void  WriteToStream(ostream& out) = 0;
    ...
  };

  class LaTeXTableWriter : public TableWriter {
  private:
  public: 
    LaTeXTableWriter(const Char_t* name, const Char_t* caption, 
                     Int_t ncols, Int_t nrows); 
    virtual void  WriteToStream(ostream& out) { 
      // Need this here for M$VC and KCC
      Int_t i, j; 
      out << "\\begin{table}[htbp]" << endl
          << "   \\begin{tabular}{|" << flush; 
      for (i = 0; i < fNCols; i++) {
        out << "l" << flush; 
      out << "|}" << endl << "    \\hline" << endl; 
      for (i = 0; i < fNRows; i++) { 
        for (j = 0; i < fNCols; i++) {
	  out << fData[i * fNCols + j] << flush;
	  if (j != fNRows - 1) 
	    out << " & " << flush; 
	  else 
	    out << "\\\\" << endl;
        }
      } 
      out << "    \\hline" << endl 
          << "  \\end{tabular}" << endl
	  << "  \\caption{" << GetTitle() << "}" << endl
	  << "  \\label{" << GetName() << "}" << endl
          << "\\end{table}" << endl;
    }
  };

  class HtmlTableWriter : public TableWriter {
  private:
  public: 
    HtmlTableWriter(const Char_t* name, const Char_t* caption, 
                     Int_t ncols, Int_t nrows); 
    virtual void  WriteToStream(ostream& out) { 
      // Need this here for M$VC and KCC
      Int_t i, j; 
      out << "<a name=\"" << GetName() << "\"" << endl
	  << "<table>" << endl; 
      for (i = 0; i < fNRows; i++) { 
        out << "<tr>" << flush;
        for (j = 0; i < fNCols; i++) {
	  out << "<td>" << fData[i * fNCols + j] << "</td>" << flush;
        out << "</tr>" << endl;
      } 
      out << "  <caption>" << GetTitle() << "</caption>" << endl;
      out << "</table>" << endl;
    }
  }; 

  ostream file("foo.tex"); 
  if (!file) 
    Bark(); 

  file << "\\documentclass{article}" << endl
       << "\\begin{document}" << endl;  
  LaTeXTableWriter ltxWriter("foo", "Foo is bar"); 
  ...
  ltxWriter.WriteToStream(file); 
  file << "\\end{docuiment}" << endl;
  file.close(); 
  gSystem->Exec("latex foo.tex"); 
  gSystem->Exec("xdvi foo.dvi"); 
  
One can ofcourse do more fancy stuff, adding column labels and so on,
should one feel like it. 

This sort of brings up another thing:  It'd be nice if ROOT had
something parallel to THtml that could write
LaTeX/SGML/DocBook/etc. formatted files

[Ok, I'm throwing out a lot of suggestions today which I
unfortunately do not have the time to do something about myself -
sorry about that - anyway, it's suggestions to put (far down) on the
TODO list if you feel like it.]

Yours, 

Christian Holm Christensen -------------------------------------------
Address: Sankt Hansgade 23, 1. th.           Phone:  (+45) 35 35 96 91 
         DK-2200 Copenhagen N                Cell:   (+45) 28 82 16 23
         Denmark                             Office: (+45) 353  25 305 
Email:   cholm@nbi.dk                        Web:    www.nbi.dk/~cholm



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:04 MET