[ROOT] Saving ROOT functions in C code?

From: Tomasz Bold (bold@fatcat.ftj.agh.edu.pl)
Date: Mon Apr 26 2004 - 11:34:50 MEST


Dear Rooters,

	I have posted mail some time ago asking about possiblity to
save/export splies (and possibly other functions) from ROOT to C code.

I have written the simple function which does the job but asking whether
there is any general method?


// saves spline as the C code
// function returns the string with valid C code of with function body.
// the string can be saved in file and direcly compilled.
// The code is inaccurate (give invalid results) at ends.
//
TString CCode (TSpline3 &spline, const char *fname, double oret=-999. )
{
  Int_t i; // inedx
  Int_t N = spline.GetNpx();
  TString *out = new TString();
  // prepare function header
  *out += "// the code generated automatically by root \n double ";
  *out += fname;
  *out += "( double x )\n{\n";
  *out += "const int N =";
  *out += N;
  *out += ";\n";
  *out += "int i;\n";
  *out += "double coef[";
  *out += N;
  *out +="][5]= {\n";


  double x,y,b,c,d;
  for ( i = 0 ; i < N ; i++ )
    {
      spline.GetCoeff(i, x, y, b, c, d);
      *out += "\t\t{";
      *out += x; *out += ", ";
      *out += y; *out += ", ";
      *out += b; *out += ", ";
      *out += c; *out += ", ";
      *out += d; *out += " ";
      *out += "}";
      if (i != N-1)
        *out += ",\n";
    }
  *out += "};\n";


  *out += "if (x < coef[0][0] || x >= coef[N-1][0])\n\t return ";
  *out += oret;
  *out += ";\n";

  *out += "
    i = 0;
    do {
        // found section
        if (coef[i][0] <= x && coef[i+1][0] > x ) {
           double dx;
           dx = x - coef[i][0];
           return coef[i][1] +dx*(coef[i][2] + dx*(coef[i][3] +
dx*coef[i][3]));
         }
        i++;
      } while (i < N-1);\n";
    *out += "return -1;\n";

    *out += "}\n";

    return *out;

}



Any hints for general solution? I suppose that this would be usefull for
Analysis --->
production code cycle for HEP experiments as the HQUAD.F was in PAW since
many times the PDFs are used for reconstruction alogtithms (and others).
I need it for ATLAS analysis.

Best reagrds,
	Tomasz Bold



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:07 MET