Re: [Brahms-dev-l] graph utility

From: Christian Holm Christensen <cholm_at_nbi.dk>
Date: Fri, 27 Jul 2007 18:22:25 +0200
Hi Flemming, 

On Mon, 2007-07-23 at 20:43 -0400, Flemming Videbaek wrote:
> Dear All,
>  
...
> Please see brahms_app/fv_app/analysis/Macros/GraphFromFile.C if you
> are interested.

I just did 

        > brahms-cvs co -d fv brahms_app/fv_app/analysis/Macros 
        cvs checkout: Updating fv
        U fv/LoadFilesBase.C
        U fv/README
        U fv/TestTFTp.C
        U fv/TestWrite.C
        
Seems like the macro in question wasn't added and commited. 

How I would code it? Using templates and policies of course: 


        template <typename T> 
        struct AsciiReader 
        {
          AsciiReader(const std::string& file) : _in(file) {} 
          ~AsciiReader() {}
          bool Read(TGraph& g) { 
            size_t i = 0;
            while (!_in.eof()) { 
              T x(g,i);
              _in >> x;
              if (_in.bad()) return false;
            }
            return true;
          }
          std::ifstream _in;
        };
        
        struct x_dx_y_dy_pt_dpt {
          x_dx_y_dy_pt_dpt(TGraph& g, size_t& i) : _g(g), _i(i) {}
          double x, dx, y, dy, pt, dpt;
          TGraph& _g;
          size_t& _i;
        };
        std::istream&
        operator>>(std::istream& i, x_dx_y_dy_pt_dpt& x) {
          TGraphErrors* g = dynamic_cast<TGraphErrors*>(&x._g);
          if (!g) { i.set_state(std::bad); return i; }
          i >> x.x >> x.dx >> x.y >> x.dy >> x.pt >> x.dpt;
          g->SetPoint(x._i, x.x, x.pt);
          g->SetPointError(x._i, x.dx, x.dpt);
          x._i++;
          return i;
        }
      
    void ReadIt(const std::string& f)
    { 
       AsciiReader<x_dx_y_dy_pt_dpt> a(f);
       TGraphErrors g;
       a.Read(g);
    }

> This may have been invented before, but I did not find it. 

Another simple way to read in a file, like the one you mention with
fields x,dx,y,dy,pt,dpt, would be to pass it through a filter: 

        // 4cols2script.cc
    int main()
    {
       TGraphErrors g;
       size_t i = 0;
       while (!std::cin.eof()) { 
	  double x, y, ex, ey;
          std::cin >> x >> y >> ex >> ey;
          if (std::cin.bad()) return 1;
          g.SetPoint(i, x, y);
          g.SetPointError(i, ex, ey);
       }
       std::cout << "{" << std::endl;
       g.SavePrimitive(std::cout);
       std::cout << "}" << std::endl;
       return 0;
    }

        $ g++ `root-config --cflags --libs` 4cols2script.cc -o
        4cols2script
        $ cat input | \
           awk 'BEGIN{FS=" "}{printf "%g %g %g %g\n",$1,$2,$5,$6}' | \
           ./4cols2script > output.C

The power of Unix :-) 

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_at_nbi.dk               Web:    www.nbi.dk/~cholm
 | |

_______________________________________________
Brahms-dev-l mailing list
Brahms-dev-l_at_lists.bnl.gov
https://lists.bnl.gov/mailman/listinfo/brahms-dev-l
Received on Fri Jul 27 2007 - 12:23:12 EDT

This archive was generated by hypermail 2.2.0 : Fri Jul 27 2007 - 12:23:35 EDT