15 #include "libxml/parser.h" 16 #include "libxml/xmlmemory.h" 21 #include <TSQLServer.h> 38 using namespace genie;
60 string fmt = (is_xml) ?
"XML" :
"ASCII";
63 <<
"Constructing spline from data in " << fmt <<
" file: " <<
filename;
76 LOG(
"Spline",
pDEBUG) <<
"Constructing spline from data in a TNtuple";
85 LOG(
"Spline",
pDEBUG) <<
"Constructing spline from data in a TTree";
94 LOG(
"Spline",
pDEBUG) <<
"Constructing spline from data in a MySQL server";
104 <<
"Constructing spline from the arrays passed to the ctor";
114 <<
"Constructing spline from the arrays passed to the ctor";
116 double * dblx =
new double[
nentries];
117 double * dbly =
new double[
nentries];
120 dblx[i] = double( x[i] );
121 dbly[i] = double( y[i] );
134 LOG(
"Spline",
pDEBUG) <<
"Spline copy constructor";
143 <<
"Constructing spline from the input TSpline3 object";
157 xmlDocPtr xml_doc = xmlParseFile(filename.c_str());
161 <<
"XML file could not be parsed! [filename: " << filename <<
"]";
165 xmlNodePtr xmlCur = xmlDocGetRootElement(xml_doc);
169 <<
"XML doc. has null root element! [filename: " << filename <<
"]";
172 if( xmlStrcmp(xmlCur->name, (
const xmlChar *)
"spline") ) {
174 <<
"XML doc. has invalid root element! [filename: " << filename <<
"]";
182 int nknots = atoi( snkn.c_str() );
185 <<
"Parsing XML spline: " << name <<
", nknots = " << nknots;
188 double * vx =
new double[nknots];
189 double * vy =
new double[nknots];
191 xmlNodePtr xmlSplChild = xmlCur->xmlChildrenNode;
194 while (xmlSplChild != NULL) {
196 <<
"Got <spline> children node: " << xmlSplChild->name;
199 if( (!xmlStrcmp(xmlSplChild->name, (
const xmlChar *)
"knot")) ) {
201 xmlNodePtr xmlKnotChild = xmlSplChild->xmlChildrenNode;
204 while (xmlKnotChild != NULL) {
206 <<
"Got <knot> children node: " << xmlKnotChild->name;
209 const xmlChar *
tag = xmlKnotChild->name;
210 bool is_xtag = ! xmlStrcmp(tag,(
const xmlChar *) xtag.c_str());
211 bool is_ytag = ! xmlStrcmp(tag,(
const xmlChar *) ytag.c_str());
212 if (is_xtag || is_ytag) {
213 xmlNodePtr xmlValTagChild = xmlKnotChild->xmlChildrenNode;
215 xmlNodeListGetString(xml_doc, xmlValTagChild, 1));
217 if (is_xtag) vx[iknot] = atof(val.c_str());
218 if (is_ytag) vy[iknot] = atof(val.c_str());
220 xmlFree(xmlValTagChild);
221 LOG(
"Spline",
pDEBUG) <<
"tag: " << tag <<
", value: " <<
val;
224 xmlKnotChild = xmlKnotChild->next;
226 xmlFree(xmlKnotChild);
229 xmlSplChild = xmlSplChild->next;
231 xmlFree(xmlSplChild);
244 TNtupleD nt(
"ntuple",
"",
"x:y");
245 nt.ReadFile(filename.c_str());
253 if(!nt)
return false;
255 TTree *
tree =
dynamic_cast<TTree *
> (nt);
261 LOG(
"Spline",
pDEBUG) <<
"Retrieving data from tree: " << tree->GetName();
263 if(!cut.size()) tree->Draw(var.c_str(),
"",
"GOFF");
264 else tree->Draw(var.c_str(), cut.c_str(),
"GOFF");
266 TH2F * hst = (TH2F*)gROOT->FindObject(
"htemp");
267 if(hst) { hst->SetDirectory(0);
delete hst; }
274 int n = tree->GetSelectedRows();
276 int * idx =
new int[
n];
277 double *
x =
new double[
n];
278 double *
y =
new double[
n];
280 TMath::Sort(n,tree->GetV1(),idx,
false);
282 for(
int i=0; i<
n; i++) {
284 x[i] = (tree->GetV1())[ii];
285 y[i] = (tree->GetV2())[ii];
299 LOG(
"Spline",
pDEBUG) <<
"Retrieving data from data-base: ";
309 double cx = 0,
cy = 0;
312 spline.GetKnot(i, cx,
cy);
327 LOG(
"Spline",
pWARN) <<
"Spline has not been built yet!";
336 LOG(
"Spline",
pWARN) <<
"Spline has not been built yet!";
347 LOG(
"Spline",
pWARN) <<
"Spline has not been built yet!";
357 bool is_in_range = (
fXMin <= x && x <=
fXMax);
363 LOG(
"Spline",
pDEBUG) <<
"Evaluating spline at point x = " <<
x;
364 assert(!TMath::IsNaN(x));
376 LOG(
"Spline",
pDEBUG) <<
"Point is between non-zero knots";
382 LOG(
"Spline",
pDEBUG) <<
"Point is between zero knots";
387 <<
"Point has zero" << (is0n ?
" left " :
" right ") <<
"knot";
388 double xpknot=0, ypknot=0, xnknot=0, ynknot=0;
391 if(is0n) y = ypknot * (x-xnknot)/(xpknot-xnknot);
392 else y = ynknot * (x-xnknot)/(xpknot-xnknot);
398 <<
" is not within spline range [" <<
fXMin <<
", " <<
fXMax <<
"]";
402 LOG(
"Spline",
pINFO) <<
"Negative y (" << y <<
")";
407 LOG(
"Spline",
pDEBUG) <<
"Spline(x = " << x <<
") = " <<
y;
413 string filename,
string xtag,
string ytag,
string name)
const 415 ofstream outxml(filename.c_str());
416 if(!outxml.is_open()) {
420 string spline_name = (name.size()>0 ? name :
fName);
422 this->
SaveAsXml(outxml, xtag, ytag, spline_name);
429 ofstream & ofs,
string xtag,
string ytag,
string name)
const 431 string spline_name = (name.size()>0 ? name :
fName);
434 int nknots = this->
NKnots();
435 string padding =
" ";
436 ofs << padding <<
"<spline name=\"" << spline_name
437 <<
"\" nknots=\"" << nknots <<
"\">" <<
endl;
441 for(
int iknot = 0; iknot < nknots; iknot++)
447 <<
" <" << xtag <<
"> " <<
setfill(
' ')
448 <<
setw(10) << x <<
" </" << xtag <<
">";
450 ofs <<
" <" << ytag <<
"> " <<
setfill(
' ')
451 <<
setw(10) <<
y <<
" </" << ytag <<
">" 452 <<
" </knot>" <<
endl;
454 ofs << padding <<
"</spline>" <<
endl;
459 ofstream outtxt(filename.c_str());
460 if(!outtxt.is_open()) {
464 int nknots = this->
NKnots();
465 outtxt << nknots <<
endl;
468 for(
int iknot = 0; iknot < nknots; iknot++) {
471 sprintf(line,format.c_str(),
x,
y);
472 outtxt << line <<
endl;
480 string spline_name = (name.size()>0 ? name :
fName);
482 string opt = ( (recreate) ?
"RECREATE" :
"UPDATE" );
484 TFile
f(filename.c_str(), opt.c_str());
490 int np,
bool scale_with_x,
bool in_log,
double fx,
double fy)
const 495 np = TMath::Max(np,2);
497 bool use_log = in_log && (
fXMin>0) && (
fXMax>0);
500 xmin = TMath::Log10(xmin);
501 xmax = TMath::Log10(xmax);
504 double dx = (xmax - xmin) / (np-1);
506 double *
x =
new double[np];
507 double *
y =
new double[np];
509 for(
int i=0; i<np; i++) {
510 x[i] = ( (use_log) ? TMath::Power(10, xmin+i*dx) : xmin + i*dx );
514 if (scale_with_x) y[i] /= x[i];
521 TGraph *
graph =
new TGraph(np, x, y);
528 double x,
double & xknot,
double & yknot, Option_t *
opt)
const 532 bool pos = (option.find(
"+") != string::npos);
533 bool neg = (option.find(
"-") != string::npos);
535 if(!pos && !neg)
return;
539 double xp=0, yp=0, xn=0, yn=0;
543 bool p = (TMath::Abs(x-xp) < TMath::Abs(x-xn));
546 if(p) { xknot = xp; yknot = yp; }
547 else { xknot = xn; yknot = yn; }
549 if(pos) { xknot = xp; yknot = yp; }
550 if(neg) { xknot = xn; yknot = yn; }
556 double xknot = 0, yknot = 0;
564 int nknots = this->
NKnots();
565 double xmin = this->
XMin();
566 double xmax = this->
XMax();
569 stream <<
"** Spline: " << this->
Name() <<
endl;
570 stream <<
"Has " << nknots
571 <<
" knots in the [" << xmin <<
", " << xmax <<
"] range" <<
endl;
573 for(
int i=0; i<nknots; i++) {
575 stream <<
"-- knot : " << i
576 <<
" -> (x = " << x <<
", y = " << y <<
")" <<
endl;
583 double xmin = this->
XMin();
584 double xmax = this->
XMax();
587 LOG(
"Spline",
pERROR) <<
"** Can't add splines. X-range mismatch!";
591 int nknots = this->
NKnots();
592 double *
x =
new double[nknots];
593 double *
y =
new double[nknots];
595 for(
int i=0; i<nknots; i++) {
608 double xmin = this->
XMin();
609 double xmax = this->
XMax();
612 LOG(
"Spline",
pERROR) <<
"** Can't multiply splines. X-range mismatch!";
616 int nknots = this->
NKnots();
617 double *
x =
new double[nknots];
618 double *
y =
new double[nknots];
620 for(
int i=0; i<nknots; i++) {
633 double xmin = this->
XMin();
634 double xmax = this->
XMax();
637 LOG(
"Spline",
pERROR) <<
"** Can't divide splines. X-range mismatch!";
641 int nknots = this->
NKnots();
642 double *
x =
new double[nknots];
643 double *
y =
new double[nknots];
645 for(
int i=0; i<nknots; i++) {
647 double denom = c * spl.
Evaluate(x[i]);
648 bool denom_is_zero = TMath::Abs(denom) < DBL_EPSILON;
650 LOG(
"Spline",
pERROR) <<
"** Refusing to divide spline knot by 0";
665 int nknots = this->
NKnots();
666 double *
x =
new double[nknots];
667 double *
y =
new double[nknots];
669 for(
int i=0; i<nknots; i++) {
681 int nknots = this->
NKnots();
682 double *
x =
new double[nknots];
683 double *
y =
new double[nknots];
685 for(
int i=0; i<nknots; i++) {
697 bool a_is_zero = TMath::Abs(a) < DBL_EPSILON;
699 LOG(
"Spline",
pERROR) <<
"** Refusing to divide spline by 0";
702 int nknots = this->
NKnots();
703 double *
x =
new double[nknots];
704 double *
y =
new double[nknots];
706 for(
int i=0; i<nknots; i++) {
718 LOG(
"Spline",
pDEBUG) <<
"Initializing spline...";
720 fName =
"genie-spline";
729 LOG(
"Spline",
pDEBUG) <<
"...done initializing spline";
740 LOG(
"Spline",
pDEBUG) <<
"Building spline...";
743 double xmax = x[nentries-1];
748 fYMax = y[ TMath::LocMax(nentries, y) ];
754 LOG(
"Spline",
pDEBUG) <<
"...done building spline";
TGraph * GetAsTGraph(int np=500, bool xscaling=false, bool inlog=false, double fx=1., double fy=1.) const
THE MAIN GENIE PROJECT NAMESPACE
bool AreEqual(double x1, double x2)
string TrimSpaces(xmlChar *xmls)
A numeric analysis tool class for interpolating 1-D functions.
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
void Print(ostream &stream) const
def graph(desc, maker=maker)
bool LoadFromAsciiFile(string filename)
void BuildSpline(int nentries, double x[], double y[])
double Evaluate(double x) const
bool ClosestKnotValueIsZero(double x, Option_t *opt="-+") const
Q_EXPORT QTSManip setprecision(int p)
bool IsWithinValidRange(double x) const
TSpline3 * GetAsTSpline(void) const
bool LoadFromXmlFile(string filename, string xtag, string ytag)
double GetKnotX(int iknot) const
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
ClassImp(Spline) namespace genie
void SaveAsROOT(string filename, string name="", bool recreate=false) const
void Add(const Spline &spl, double c=1)
void Multiply(const Spline &spl, double c=1)
void GetKnot(int iknot, double &x, double &y) const
Q_EXPORT QTSManip setw(int w)
string TrimSpaces(string input)
double GetKnotY(int iknot) const
bool LoadFromTSpline3(const TSpline3 &spline, int nknots)
void SaveAsText(string filename, string format="%10.6f\t%10.6f") const
void SaveAsXml(string filename, string xtag, string ytag, string name="") const
bool LoadFromNtuple(TNtupleD *nt, string xy, string cut="")
bool LoadFromDBase(TSQLServer *db, string query)
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
void line(double t, double *p, double &x, double &y, double &z)
bool LoadFromTree(TTree *tr, string xy, string cut="")
void FindClosestKnot(double x, double &xknot, double &yknot, Option_t *opt="-+") const
query_result< Args... > query(sqlite3 *db, std::string const &ddl)
string GetAttribute(xmlNodePtr xml_cur, string attr_name)
Q_EXPORT QTSManip setfill(int f)
void Divide(const Spline &spl, double c=1)
QTextStream & endl(QTextStream &s)