[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ROOT/gnuplot



Hi all,

n Thu, 2006-06-08 at 20:01 -0500, Ricky Egeland wrote: 
> On Jun 8, 2006, at 7:48 PM, Christian Holm Christensen wrote:
> >
> > Hmm, writing a GNUPlot-like front-end for ROOT, is about the sillyst
> > thing I've heard in a long time.  ... [cut] ...
> 
> FYI, my motivation for doing this is to interface with a PHP web  
> app.  I want to open a named pipe to the gnuplot-like program and  
> write data to it from PHP.  The output will be a plot (PNG format)  
> and ROOT file which is cached and displayed on the web site.

There's a separate project called `Carrot' which, with an Apache server'
allows you to execute ROOT stuff from the browser.   I'm not very
familiar with that project, so I cannot tell you a lot about it.   I
think the web-page is at http://carrot.cern.ch 

On Fri, 2006-06-09 at 09:31 +0200, Yannick Patois wrote: 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi,
> 
> Christian Holm Christensen wrote:
> > Hmm, writing a GNUPlot-like front-end for ROOT, is about the sillyst
> > thing I've heard in a long time.
> > In essence, I think you'd be wasting your time pursuing a GNUPlot-like
> > interface to ROOT.  Time much better spent learning C++ (or teaching
> > others the basics of C++), writing a GUI, or do scientific work :-)
>
> When i thought to this idea, I wrote in a minut a 10 lines program that
> instancied a TRint object and called it "rootplot"... Then I wondered
> where to go and I stopped there. So basically I understand your position.

If you're thinking about making a GUI, why not try the relatively new
GUI builder? 

> But anyway, I belive that adding a few class for quicker interactions
> could be useful. Eg, when you take this example:
> "TNtuple nt("X:Y:EX:EY");
> nt.ReadFile("data.dat", 0);
> nt.Draw("X","Y");"
> 
> Why not replace it by something like:
> TPlot("data.dat","1:2");

Right, why not?   That's what I meant when I said `... convenience
functions, scripts, GUIs, what-not, can easily be added to ROOT.' 

Perhaps what you're looking for, is to make some sort of class with the
most `common' plotting functions, a la GNUPlot, say 

  struct TGNUPlot
  {
    static Plot(const char* file, const char* descriptor);
    static Plot(const char* funct);
    ...
  };

which could be put in a script that you compile on the fly with 

  Root> .L TGNUPlot.C+
  Root> TGNUPlot::Plot("data.dat", "%f %f");
  ...

> I'm sure TPlot() is not a good class name, 

There's currently no such class in ROOT.   BTW, if you do make something
like above, why not submit it to ROOT?  normally, they are very happy to
receive contributions that have a general interest. 

> and maybe "1:2" not the best
> way to say "plot first column against second".

Well, yes and no.  What you really want to do, is to use a
`std::operator>>(std::istream&,T&)' call.  However, the above doesn't
lent itself well to do this.   Also, a string argument isn't always the
best.  Perhaps a bit pattern would be better. 

  TNtuple* ReadDataFile(const char* name, size_t mask, const char* format) 
  {
     // Figure out the last column to read. 
     size_t maxCol = 0;
     for (size_t i = 0; i < sizeof(size_t); i++) 
       if ((mask >> i) & 0x1) maxCol = i

     // Figure out the format of the columns 
     size_t nArg     = 0;
     size_t nNeeded  = 10;
     int*   argTypes = 0;
     do {
       if (nNeeded >= nArg) {
          if (argTypes) delete [] argTypes;
          argTypes = new int[nNeeded];
          nArg     = nNeeded;
       }
       nNeeded = parse_printf_format(format, nArg, argTypes);
     } while (nNeeded > nArg);

     // Make return 
     std::string form;
     std::ostringstream fs(form)[
     size_t j = 0;
     for (size_t i = 0; i < nArg; i++)  { 
       if (! ((mask >> i) & 0x1) continue;
       if (j != 0) fs << ":";
       switch (argTypes[i] & ~PA_FLAG_MASK) {
          case PA_INT:     fs << "COL"  <<  j << "/I" ; break;
          case PA_CHAR:    fs << "COL"  <<  j << "/C" ; break;
          case PA_FLOAT:   fs << "COL"  <<  j << "/F" ; break;
          case PA_DOUBLE:  fs << "COL"  <<  j << "/D" ; break;
          case PA_STRING:
             std::cerr  << "String columns not supported!" << std::endl;
             return 0;
          default: 
             std::cerr << "uknown column type: " 
                       << argTypes[i] & ~PA_FLAG_MASK << std::endl;
             return 0;
          }
       }  
       j++;
     }
     TNtuple* ret = new TNtuple(name, form.c_str();


     // Read the file 
     std::ifstream file(name);
     double tmp;
     double x[10]; 
     int ii;
     char ic;
     float if;
     double id;
     std::string is;
     while (!file.eof()) {
        size_t j = 0;
        if (file.peek() != int('#')) {
          for (size_t i = 0; i < maxCol; i++) {
            switch (argTypes[i] & ~PA_FLAG_MASK) {
            case PA_INT:     file >> ii; tmp = ii; break;
	    case PA_CHAR:    file >> ic; tmp = ic; break;
            case PA_FLOAT:   file >> if; tmp = if; break; 
            case PA_DOUBLE:  file >> id; tmp = id; break;
            case PA_STRING:  file >> is; break;
            }
            if ((mask >> i) & 0x1) x[j] = tmp;
            j++;
          }
        }
        std::getline(istr, file);
        ret->Fill(x);
     }
     return ret;
   }

This could be used like 

   TNtuple* nt = ReadDataFile("data.dat", 0x3, "%f %f");    // col 1:2
   TNtuple* nt = ReadDataFile("data.dat", 0x5, "%f %f %d"); // col 1:3
   TNtuple* nt = ReadDataFile("data.dat", 0x6, "%f %d %f"); // col 2:3
   TNtuple* nt = ReadDataFile("data.dat", 0x5, "%f %s %f"); // col 1:3
   

>  But that's exactly the
> points that have to be discussed in such a project...

Right. Good luck.

Yours,

-- 
 ___  |  Christian Holm Christensen 
  |_| |  -------------------------------------------------------------
    | |  Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|           DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|            Denmark                    Office: (+45) 353  25 404
 ____|   Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |



Reply to: