newly added. Not for productive used yetsvn diff | grep Index:
This commit is contained in:
32
src/tests/fio_test/fio_test.cpp
Normal file
32
src/tests/fio_test/fio_test.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
ofstream f;
|
||||
|
||||
// open mlog-file
|
||||
f.open("fio_test.txt", iostream::out);
|
||||
if (!f.is_open()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const int prec=6;
|
||||
double val = 0.258466548916354835498465;
|
||||
|
||||
f.width(9);
|
||||
f.precision(prec);
|
||||
f << left << val;
|
||||
f << " ";
|
||||
f.width(9);
|
||||
f.precision(prec);
|
||||
f << left << val;
|
||||
f << " ";
|
||||
f.width(9);
|
||||
f.precision(prec);
|
||||
f << left << val;
|
||||
|
||||
f.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
src/tests/fio_test/fio_test.pro
Normal file
17
src/tests/fio_test/fio_test.pro
Normal file
@@ -0,0 +1,17 @@
|
||||
#------------------------------------------------------
|
||||
# fio_test.pro
|
||||
# qmake file for fio_test
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
SOURCES = fio_test.cpp
|
||||
|
||||
TARGET=fio_test
|
||||
1
src/tests/fio_test/fio_test.txt
Normal file
1
src/tests/fio_test/fio_test.txt
Normal file
@@ -0,0 +1 @@
|
||||
0.258467 0.258467 0.258467
|
||||
84
src/tests/minuit2example/PGlobalChiSquare.cpp
Normal file
84
src/tests/minuit2example/PGlobalChiSquare.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/***************************************************************************
|
||||
|
||||
PGlobalChiSquare.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "PGlobalChiSquare.h"
|
||||
|
||||
namespace ROOT {
|
||||
namespace Minuit2 {
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PGlobalChiSquare::PGlobalChiSquare(PRunList &runList)
|
||||
{
|
||||
fRunList = runList;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PGlobalChiSquare::~PGlobalChiSquare()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param par
|
||||
*/
|
||||
double PGlobalChiSquare::operator()(const std::vector<double>& par) const
|
||||
{
|
||||
double chi2 = 0.0;
|
||||
double funcValue;
|
||||
double diff;
|
||||
|
||||
// calculate chi2 for the given model
|
||||
for (unsigned int i=0; i<fRunList.size(); i++) { // run loop
|
||||
for (unsigned int j=0; j<fRunList[i].fTime.size(); j++) { // data loop
|
||||
funcValue = par[i]*exp(-par[2]*fRunList[i].fTime[j]); // par[i] since par[0] = ampl of run 0, etc.
|
||||
diff = funcValue - fRunList[i].fValue[j];
|
||||
chi2 += diff*diff/(fRunList[i].fError[j]*fRunList[i].fError[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return chi2;
|
||||
}
|
||||
|
||||
} // namespace Minuit2
|
||||
} // namespace ROOT
|
||||
|
||||
62
src/tests/minuit2example/PGlobalChiSquare.h
Normal file
62
src/tests/minuit2example/PGlobalChiSquare.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/***************************************************************************
|
||||
|
||||
PGlobalChiSquare.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PGLOBALCHISQUARE_H_
|
||||
#define _PGLOBALCHISQUARE_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Minuit2/FCNBase.h"
|
||||
|
||||
#include "mn2test.h"
|
||||
|
||||
namespace ROOT {
|
||||
namespace Minuit2 {
|
||||
|
||||
class PGlobalChiSquare : public FCNBase
|
||||
{
|
||||
|
||||
public:
|
||||
PGlobalChiSquare(PRunList &runList);
|
||||
~PGlobalChiSquare();
|
||||
|
||||
double Up() const { return 1.0; }
|
||||
double operator()(const std::vector<double>&) const;
|
||||
|
||||
private:
|
||||
PRunList fRunList;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Minuit2
|
||||
} // namespace ROOT
|
||||
|
||||
#endif // _PGLOBALCHISQUARE_H_
|
||||
157
src/tests/minuit2example/mn2test.cpp
Normal file
157
src/tests/minuit2example/mn2test.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/***************************************************************************
|
||||
|
||||
mn2test.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "Minuit2/FunctionMinimum.h"
|
||||
#include "Minuit2/MnUserParameters.h"
|
||||
#include "Minuit2/MnMinimize.h"
|
||||
#include "Minuit2/MnMinos.h"
|
||||
|
||||
#include "PGlobalChiSquare.h"
|
||||
|
||||
#include "mn2test.h"
|
||||
|
||||
using namespace ROOT::Minuit2;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>generates data for testing
|
||||
* model = ampl * exp(-lambda * t), where ampl is depending on the run, but
|
||||
* not lambda
|
||||
*/
|
||||
void generateData(PRunList &data)
|
||||
{
|
||||
PRun run;
|
||||
const unsigned int length = 100;
|
||||
double value;
|
||||
|
||||
for (unsigned int i=0; i<2; i++) { // run loop
|
||||
for (unsigned int j=0; j<length; j++) { // data loop
|
||||
run.fTime.push_back((double)j/(double)length);
|
||||
value = (2*i+10)*exp(-1.7*(double)j/(double)length);
|
||||
value += 2.0*sqrt(value)*((double)random()/(double)RAND_MAX-0.5);
|
||||
run.fValue.push_back(value);
|
||||
run.fError.push_back(sqrt(value/4.0));
|
||||
}
|
||||
data.push_back(run);
|
||||
|
||||
// clean up
|
||||
run.fTime.clear();
|
||||
run.fValue.clear();
|
||||
run.fError.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc || argv);
|
||||
|
||||
int success = 1;
|
||||
|
||||
PRunList runs;
|
||||
generateData(runs);
|
||||
|
||||
unsigned int max = 25;
|
||||
if (runs[0].fTime.size() < max)
|
||||
max = runs[0].fTime.size();
|
||||
for (unsigned int i=0; i<runs.size(); i++) { // run loop
|
||||
std::cout << std::endl << "run " << i;
|
||||
for (unsigned int j=0; j<max; j++) { // data loop
|
||||
std::cout << std::endl << runs[i].fTime[j] << ", " << runs[i].fValue[j] << ", " << runs[i].fError[j];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
PGlobalChiSquare chi2(runs); // chi2 object derived from ROOT::Minuit2::FCNBase
|
||||
|
||||
// create parameters ---------------------------------
|
||||
MnUserParameters upar;
|
||||
|
||||
// model = ampl_i * exp(-lambda * t), i = run no.
|
||||
upar.Add("ampl1", 10.0, 1.0); // ampl1
|
||||
upar.Add("ampl2", 12.0, 1.0); // ampl2
|
||||
upar.Add("lambda", 0.1, 0.01); // lambda
|
||||
|
||||
// create minimizer ---------------------------------
|
||||
MnMinimize minimize(chi2, upar);
|
||||
|
||||
// minimize
|
||||
FunctionMinimum min = minimize();
|
||||
|
||||
if (min.IsValid()) {
|
||||
std::cout << std::endl << "minimize (MIGRAD/SIMPLEX) found minimum ...";
|
||||
} else {
|
||||
std::cout << std::endl << "minimize (MIGRAD/SIMPLEX) failed to find minimum ...";
|
||||
}
|
||||
std::cout << std::endl << "Chi2 = " << min.Fval()
|
||||
<< ", NDF = " << 2*runs[0].fTime.size()-3
|
||||
<< ", Chi2r = " << min.Fval()/((double)(2*runs[0].fTime.size()-3)) << std::endl;
|
||||
|
||||
// make minos analysis
|
||||
MnMinos minos(chi2, min);
|
||||
|
||||
// 1-sigma MINOS errors
|
||||
std::pair<double, double> e0 = minos(0);
|
||||
std::pair<double, double> e1 = minos(1);
|
||||
std::pair<double, double> e2 = minos(2);
|
||||
|
||||
// print out parameters
|
||||
unsigned int i=0;
|
||||
std::cout << std::endl << "1-sigma minos errors:" << std::endl;
|
||||
i=0;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e0.first << " "
|
||||
<< e0.second;
|
||||
i++;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e1.first << " "
|
||||
<< e1.second;
|
||||
i++;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e2.first << " "
|
||||
<< e2.second;
|
||||
|
||||
std::cout << std::endl << std::endl;
|
||||
|
||||
// clean up
|
||||
runs.clear();
|
||||
|
||||
return success;
|
||||
}
|
||||
45
src/tests/minuit2example/mn2test.h
Normal file
45
src/tests/minuit2example/mn2test.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/***************************************************************************
|
||||
|
||||
mn2test.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _MN2TEST_H_
|
||||
#define _MN2TEST_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
typedef struct {
|
||||
std::vector<double> fTime;
|
||||
std::vector<double> fValue;
|
||||
std::vector<double> fError;
|
||||
} PRun;
|
||||
|
||||
typedef std::vector<PRun> PRunList;
|
||||
|
||||
#endif // _MN2TEST_H_
|
||||
26
src/tests/minuit2example/mn2test.pro
Normal file
26
src/tests/minuit2example/mn2test.pro
Normal file
@@ -0,0 +1,26 @@
|
||||
#------------------------------------------------------
|
||||
# mn2test.pro
|
||||
# qmake file for mn2test
|
||||
#
|
||||
# Andreas Suter, 2007/10/23
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile.mn2test
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PGlobalChiSquare.h
|
||||
|
||||
SOURCES = mn2test.cpp \
|
||||
PGlobalChiSquare.cpp
|
||||
|
||||
INCLUDEPATH += /usr/local/include
|
||||
INCLUDEPATH += ./
|
||||
|
||||
unix:LIBS += -L/usr/local/lib -lMinuit2Base -lbsd -lm -ldl -lutil
|
||||
|
||||
TARGET=mn2test
|
||||
|
||||
22
src/tests/nanTest/nanTest.cpp
Normal file
22
src/tests/nanTest/nanTest.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <math.h>
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
double value;
|
||||
|
||||
value = nan("NAN");
|
||||
|
||||
if (isnan(value))
|
||||
cout << endl << "value is not a number";
|
||||
else
|
||||
cout << endl << "value is a number";
|
||||
|
||||
cout << endl << "value = " << value;
|
||||
|
||||
cout << endl << "done ..." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
src/tests/nanTest/nanTest.pro
Normal file
17
src/tests/nanTest/nanTest.pro
Normal file
@@ -0,0 +1,17 @@
|
||||
#------------------------------------------------------
|
||||
# nanTest.pro
|
||||
# qmake file for nanTest
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
SOURCES = nanTest.cpp
|
||||
|
||||
TARGET=nanTest
|
||||
110
src/tests/nemuRootFileRead/nemuRootFileRead.cpp
Normal file
110
src/tests/nemuRootFileRead/nemuRootFileRead.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <TROOT.h>
|
||||
#include <TSystem.h>
|
||||
#include <TFile.h>
|
||||
#include <TFolder.h>
|
||||
#include <TH1F.h>
|
||||
|
||||
#include "TLemRunHeader.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
cout << endl;
|
||||
cout << "SYNTAX: nemuRootFileRead PathFileName";
|
||||
cout << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
TROOT root("nemuRootFileRead", "nemuRootFileRead", 0);
|
||||
|
||||
// get file name
|
||||
char *str = gSystem->ExpandPathName(argv[1]);
|
||||
|
||||
// check that the file name exists
|
||||
if (gSystem->AccessPathName(str)==true) {
|
||||
cout << endl << "file " << str << " doesn't exist :-(" << endl;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// define file
|
||||
TFile f(str);
|
||||
if (f.IsZombie()) {
|
||||
cout << endl << "couldn't open file " << str << endl;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cout << endl << "opened file " << str;
|
||||
|
||||
// get RunInfo Folder
|
||||
TFolder *folder = 0;
|
||||
f.GetObject("RunInfo", folder);
|
||||
|
||||
// check if RunInfo Folder is valid
|
||||
if (!folder) {
|
||||
cout << endl << "Couldn't obtain RunInfo folder from ROOT file " << str;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get run header
|
||||
TLemRunHeader *runHeader = dynamic_cast<TLemRunHeader*>(folder->FindObjectAny("TLemRunHeader"));
|
||||
|
||||
// check if run header is valid
|
||||
if (!runHeader) {
|
||||
cout << endl << "Couldn't obtain run header info from ROOT file " << str;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cout << endl << "Run No : " << runHeader->GetRunNumber();
|
||||
cout << endl << "Run Title: " << runHeader->GetRunTitle().GetString().Data();
|
||||
|
||||
// get histos folder
|
||||
f.GetObject("histos", folder);
|
||||
|
||||
// check if histos Folder is valid
|
||||
if (!folder) {
|
||||
cout << endl << "Couldn't obtain histos folder from ROOT file " << str;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get histo hDecay00
|
||||
TH1F *histo = dynamic_cast<TH1F*>(folder->FindObject("DecayAnaModule/hDecay00"));
|
||||
|
||||
// check if histo hDecay00 was found
|
||||
if (!histo) {
|
||||
cout << endl << "Couldn't obtain hDecay00 histo from ROOT file " << str;
|
||||
cout << endl << "will quit now ..." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// print out first 10 values of hDecay00 and some other info
|
||||
cout << endl << "hDecay00: No of Bins: " << histo->GetNbinsX();
|
||||
int max;
|
||||
if (max > histo->GetNbinsX())
|
||||
max = histo->GetNbinsX();
|
||||
else
|
||||
max = 10;
|
||||
cout << endl << "hDecay00: Data:";
|
||||
for (int i=0; i<max; i++) {
|
||||
cout << endl << i << ": " << histo->GetBinContent(i+1);
|
||||
}
|
||||
|
||||
if (f.IsOpen())
|
||||
f.Close();
|
||||
|
||||
cout << endl << "closed file .." << endl;
|
||||
|
||||
if (str)
|
||||
delete str;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
src/tests/nemuRootFileRead/nemuRootFileRead.pro
Normal file
23
src/tests/nemuRootFileRead/nemuRootFileRead.pro
Normal file
@@ -0,0 +1,23 @@
|
||||
#------------------------------------------------------
|
||||
# nemuRootFileRead.pro
|
||||
# qmake file for rootSystemTest
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
SOURCES = nemuRootFileRead.cpp \
|
||||
|
||||
INCLUDEPATH += $$(ROOTSYS)/include
|
||||
|
||||
ROOTLIBS = -lCore -lCint -lRIO -lGraf -lGraf3d -lGpad -lHist -lMatrix -lMinuit -lGui
|
||||
NEMULIBS = -lTLemRunHeader
|
||||
unix:LIBS += -L$$(ROOTSYS)/lib $${ROOTLIBS} $${NEMULIBS} -lbsd -lm -ldl -lutil
|
||||
|
||||
TARGET=nemuRootFileRead
|
||||
24
src/tests/root2DHisto/root2DHisto.C
Normal file
24
src/tests/root2DHisto/root2DHisto.C
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
gStyle->SetOptStat(000000000);
|
||||
|
||||
TCanvas *cov = new TCanvas("cov", "Minuit2 Output Covariance Matrix", 500, 500);
|
||||
|
||||
TH2D *f = new TH2D("f", "Minuit2 Output Covariance Matrix", 16, 0.0, 16.0, 16, 0.0, 16.0);
|
||||
|
||||
double x, y, z;
|
||||
|
||||
for (unsigned int i=0; i<16; i++) {
|
||||
for (unsigned int j=0; j<16; j++) {
|
||||
x = (double)i;
|
||||
y = (double)j;
|
||||
z = x/16.0-y/16.0+(x-8.0)*(y-8.0)/(8.0*8.0);
|
||||
f->Fill(x,y,z);
|
||||
}
|
||||
}
|
||||
|
||||
TFile ff("test.root", "recreate");
|
||||
cov->Draw();
|
||||
f->Draw("COLZ");
|
||||
cov->Write();
|
||||
ff.Close();
|
||||
}
|
||||
30
src/tests/rootFiles/histoWithErrors.C
Normal file
30
src/tests/rootFiles/histoWithErrors.C
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
TFile f("test.root", "recreate");
|
||||
|
||||
// make a histo with errors
|
||||
TH1F *h = new TH1F("h", "histo test", 20, -0.05, 1.05);
|
||||
TH1F *hc = new TH1F("hc", "curve", 20, -0.05, 1.05);
|
||||
for (unsigned int i=0; i<20; i++) {
|
||||
x = (double)i/20.0;
|
||||
value = TMath::Sqrt(1.5*x+0.1*TMath::Power(x,3.0)-0.4*TMath::Power(x,5.0));
|
||||
hc->SetBinContent(i, value+0.1*TMath::Sqrt(value)-value*value);
|
||||
h->SetBinContent(i, value);
|
||||
h->SetBinError(i, TMath::Power(value, 5.0));
|
||||
}
|
||||
|
||||
TCanvas *c1 = new TCanvas("c1", "my Canvas c1", 10, 10, 800, 600);
|
||||
|
||||
h->SetMarkerStyle(20);
|
||||
h->Draw("*H HIST E1");
|
||||
|
||||
hc->SetLineColor(2);
|
||||
hc->SetLineWidth(3);
|
||||
hc->Draw("C SAME");
|
||||
|
||||
TCanvas *c2 = new TCanvas("c2", "my Canvas c2", 10, 10, 800, 600);
|
||||
h->SetMarkerStyle(23);
|
||||
h->Draw("*H HIST E1");
|
||||
|
||||
f.WriteTObject(c1);
|
||||
f.WriteTObject(c2);
|
||||
}
|
||||
105
src/tests/rootMinuit2Test/minuit2test.C
Normal file
105
src/tests/rootMinuit2Test/minuit2test.C
Normal file
@@ -0,0 +1,105 @@
|
||||
/*******************************************************************
|
||||
minuit2test.C
|
||||
|
||||
author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
********************************************************************/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <TF1.h>
|
||||
#include <TH1.h>
|
||||
#include <TVirtualFitter.h>
|
||||
#include <TCanvas.h>
|
||||
#include <TMath.h>
|
||||
|
||||
const double gGammaMu = TMath::TwoPi()*0.01355;
|
||||
const double gTauMu = 2.19705; // in (us)
|
||||
|
||||
TF1 *gFitFcn;
|
||||
|
||||
//------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Background (flat for the moment)
|
||||
*
|
||||
* \param x
|
||||
* \param par par[0] is the background
|
||||
*/
|
||||
Double_t Background(Double_t *x, Double_t *par)
|
||||
{
|
||||
return par[0];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Asymmetry
|
||||
*
|
||||
* \param x
|
||||
* \param par par[0] = asymmetry
|
||||
* par[1] = lambda
|
||||
* par[2] = B
|
||||
* par[3] = phase
|
||||
*/
|
||||
Double_t Asymmetry(Double_t *x, Double_t *par)
|
||||
{
|
||||
return par[0]*TMath::Exp(-par[1]*x[0])*TMath::Cos(gGammaMu*par[2]*x[0]+par[3]/180.0*TMath::Pi());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Spectrum
|
||||
*
|
||||
* \param x
|
||||
* \param par
|
||||
*/
|
||||
Double_t Spectrum(Double_t *x, Double_t *par)
|
||||
{
|
||||
return par[0]*TMath::Exp(-x[0]/gTauMu)*(1.0 + Asymmetry(x,&par[1])) + Background(x,&par[5]);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
/**
|
||||
* <p>minuit2test
|
||||
*
|
||||
* \param x
|
||||
* \param par
|
||||
*/
|
||||
void minuit2test()
|
||||
{
|
||||
TH1::AddDirectory(kFALSE);
|
||||
TCanvas *c1 = new TCanvas("c1","Fitting Test",10,10,500,500);
|
||||
|
||||
// create a TF1 with the range from 0.0 to 12.0 and 6 parameters
|
||||
gFitFcn = new TF1("gFitFcn", Spectrum, 0.0, 12.0, 6);
|
||||
gFitFcn->SetNpx(1200);
|
||||
|
||||
// set parameters
|
||||
gFitFcn->SetParNames("N0", "asym", "lambda", "B", "phase", "Bkg");
|
||||
gFitFcn->SetParameter(0, 30.0); // N0
|
||||
// gFitFcn->SetParLimits(0, 0.0, 1.0e6);
|
||||
gFitFcn->SetParameter(1, 0.11); // asym
|
||||
// gFitFcn->SetParLimits(1, 0.0, 0.33);
|
||||
gFitFcn->SetParameter(2, 0.3); // lambda
|
||||
// gFitFcn->SetParLimits(2, 0.0, 100.0);
|
||||
gFitFcn->SetParameter(3, 100.0); // B
|
||||
// gFitFcn->SetParLimits(3, 0.0, 1000.0);
|
||||
gFitFcn->SetParameter(4, 0.0); // phase
|
||||
// gFitFcn->SetParLimits(4, -90.0, 90.0);
|
||||
gFitFcn->SetParameter(5, 5.0); // Bkg
|
||||
// gFitFcn->SetParLimits(5, 0.0, 1000.0);
|
||||
|
||||
cout << endl << "gFitFcn->Integral(0.0, 12.0) = " << gFitFcn->Integral(0.0, 12.0);
|
||||
cout << endl;
|
||||
|
||||
// fill histo
|
||||
TH1F *histo = new TH1F("histo","Minuit2 Test",1200,0.0,12.0);
|
||||
histo->FillRandom("gFitFcn", 100*(int)gFitFcn->Integral(0.0, 12.0));
|
||||
histo->Rebin(5);
|
||||
|
||||
histo->Draw();
|
||||
|
||||
TVirtualFitter::SetDefaultFitter("Minuit2");
|
||||
histo->Fit("gFitFcn", "LE"); // L->likleyhood, E->minos
|
||||
}
|
||||
0
src/tests/rootSystemTest/.kdbgrc.rootSystemTest
Executable file
0
src/tests/rootSystemTest/.kdbgrc.rootSystemTest
Executable file
35
src/tests/rootSystemTest/rootSystemTest.cpp
Normal file
35
src/tests/rootSystemTest/rootSystemTest.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <TROOT.h>
|
||||
#include <TSystem.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
cout << endl;
|
||||
cout << "SYNTAX: rootSystemTest PathFileName";
|
||||
cout << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
TROOT root("rootSystemTest", "rootSystemTest", 0);
|
||||
|
||||
cout << endl;
|
||||
cout << "Path name: " << argv[1] << endl;
|
||||
char *str = gSystem->ExpandPathName(argv[1]);
|
||||
cout << "Path name expanded: " << str << endl;
|
||||
|
||||
if (gSystem->AccessPathName(str)==true) {
|
||||
cout << endl << "file " << str << " doesn't exist :-(" << endl;
|
||||
} else {
|
||||
cout << endl << "file " << str << " exists :-)" << endl;
|
||||
}
|
||||
|
||||
cout << "done ..." << endl;
|
||||
|
||||
if (str)
|
||||
delete str;
|
||||
|
||||
return 0;
|
||||
}
|
||||
22
src/tests/rootSystemTest/rootSystemTest.pro
Normal file
22
src/tests/rootSystemTest/rootSystemTest.pro
Normal file
@@ -0,0 +1,22 @@
|
||||
#------------------------------------------------------
|
||||
# rootSystemTest.pro
|
||||
# qmake file for rootSystemTest
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
SOURCES = rootSystemTest.cpp \
|
||||
|
||||
INCLUDEPATH += $$(ROOTSYS)/include
|
||||
|
||||
ROOTLIBS = -lCore -lCint
|
||||
unix:LIBS += -L$$(ROOTSYS)/lib $${ROOTLIBS} -lbsd -lm -ldl -lutil
|
||||
|
||||
TARGET=rootSystemTest
|
||||
0
src/tests/spirit/.kdbgrc.spirit_fcn_test
Executable file
0
src/tests/spirit/.kdbgrc.spirit_fcn_test
Executable file
620
src/tests/spirit/PFunction.cpp
Normal file
620
src/tests/spirit/PFunction.cpp
Normal file
@@ -0,0 +1,620 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunction.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include<cmath>
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PFunction.h"
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Constructor
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* info is an abstract syntax tree (AST) generate by the spirit parse library
|
||||
* (see http://spirit.sourceforge.net/distrib/spirit_1_8_5/libs/spirit/doc/trees.html).
|
||||
* It contains a single parsed msr-function in an ascii representation.
|
||||
* Here it takes the from
|
||||
* assignment (root node)
|
||||
* |_ 'FUNx'
|
||||
* |_ '='
|
||||
* |_ expression
|
||||
* |_ ...
|
||||
*
|
||||
* Since it would be inefficient to evaluate this AST directly it is transferred to
|
||||
* a more efficient tree fFuncs here in the constructor.
|
||||
*
|
||||
* \param info AST parse tree holding a single parsed msr-function in an ascii representation
|
||||
* \param param the parameter vector
|
||||
* \param map the map vector
|
||||
*/
|
||||
PFunction::PFunction(tree_parse_info<> info, vector<double> param, vector<int> map)
|
||||
{
|
||||
cout << endl << "in PFunction ...";
|
||||
|
||||
fInfo = info;
|
||||
fParam = param;
|
||||
fMap = map;
|
||||
|
||||
// init class variables
|
||||
fValid = true;
|
||||
fFuncNo = -1;
|
||||
|
||||
// check parameter and map range
|
||||
if (!CheckParameterAndMapRange()) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// generate function evaluation tree
|
||||
if (!GenerateFuncEvalTree()) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
EvaluateTree(info);
|
||||
|
||||
cout << endl << "fFuncString: '" << fFuncString << "'";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Destructor
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PFunction::~PFunction()
|
||||
{
|
||||
// cout << endl << "in ~PFunction ...";
|
||||
fParam.clear();
|
||||
fMap.clear();
|
||||
|
||||
CleanupFuncEvalTree();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CheckParameterRange (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunction::CheckParameterAndMapRange()
|
||||
{
|
||||
return CheckParameterAndMapInTree(fInfo.trees.begin());
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CheckParameterAndMapInTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p> walk through the tree and check if the parameter found
|
||||
* are within a valid range given by the size of fParam.
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
bool PFunction::CheckParameterAndMapInTree(iter_t const& i)
|
||||
{
|
||||
bool success = true;
|
||||
int value;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::funLabelID) {
|
||||
assert(i->children.size() == 0);
|
||||
SetFuncNo(i);
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) {
|
||||
assert(i->children.size() == 0);
|
||||
string str(i->value.begin(), i->value.end());
|
||||
cout << endl << "parameterID: value = " << str << endl;
|
||||
sscanf(str.c_str(), "PAR%d", &value);
|
||||
//cout << endl << ">> value = " << value << ", fParam.size() = " << fParam.size();
|
||||
if (value > (int)fParam.size()) { // parameter number found > number of parameters
|
||||
cout << endl << "**ERROR**: found parameter " << str << " with only " << fParam.size() << " parameters present?!?";
|
||||
fValid = false;
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) {
|
||||
assert(i->children.size() == 0);
|
||||
string str(i->value.begin(), i->value.end());
|
||||
cout << endl << "mapID: value = " << str << endl;
|
||||
sscanf(str.c_str(), "MAP%d", &value);
|
||||
//cout << endl << ">> value = " << value << ", fParam.size() = " << fParam.size();
|
||||
if (value > (int)fMap.size()) { // parameter number found > number of parameters
|
||||
cout << endl << "**ERROR**: found map " << str << " with only " << fMap.size() << " maps present?!?";
|
||||
fValid = false;
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) {
|
||||
cout << endl << "children = " << i->children.size() << endl;
|
||||
assert(i->children.size() == 4);
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+2); // thats the real stuff
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
// i: real | parameter | map | function | expression
|
||||
assert(i->children.size() == 1);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
// '*'/'/' i: lhs, rhs
|
||||
assert(i->children.size() == 2);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+1);
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) {
|
||||
// '+'/'-' i: lhs, rhs
|
||||
assert(i->children.size() == 2);
|
||||
success = CheckParameterAndMapInTree(i->children.begin());
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+1);
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
// i: 'FUNx', '=', 'expression'
|
||||
assert(i->children.size() == 3);
|
||||
success = CheckParameterAndMapInTree(i->children.begin()); // FUNx
|
||||
success = CheckParameterAndMapInTree(i->children.begin()+2); // this is the real stuff
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// SetFuncNo (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
bool PFunction::SetFuncNo(iter_t const& i)
|
||||
{
|
||||
int funNo = -1;
|
||||
int status;
|
||||
bool success = true;
|
||||
|
||||
// get string from tree
|
||||
string str(i->value.begin(), i->value.end());
|
||||
|
||||
// extract function number from string
|
||||
status = sscanf(str.c_str(), "FUN%d", &funNo);
|
||||
//cout << endl << "SetFuncNo: status = " << status << ", funNo = " << funNo;
|
||||
if (status == 1) { // found 1 int
|
||||
fFuncNo = funNo;
|
||||
} else { // wrong string
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GenerateFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunction::GenerateFuncEvalTree()
|
||||
{
|
||||
FillFuncEvalTree(fInfo.trees.begin(), fFunc);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// FillFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
void PFunction::FillFuncEvalTree(iter_t const& i, PFuncTreeNode &node)
|
||||
{
|
||||
double dvalue;
|
||||
int ivalue;
|
||||
int status;
|
||||
string str;
|
||||
PFuncTreeNode child;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::realID) { // handle number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "%lf", &dvalue); // convert string to double
|
||||
node.fID = PFunctionGrammar::realID; // keep the ID
|
||||
node.fDvalue = dvalue; // keep the value
|
||||
// cout << endl << ">> realID: value = " << dvalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) { // handle parameter number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "PAR%d", &ivalue); // convert string to parameter number
|
||||
node.fID = PFunctionGrammar::parameterID; // keep the ID
|
||||
node.fIvalue = ivalue; // keep the value
|
||||
// cout << endl << ">> parameterID: value = " << ivalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) { // handle map number
|
||||
str = string(i->value.begin(), i->value.end()); // get string
|
||||
status = sscanf(str.c_str(), "MAP%d", &ivalue); // convert string to map number
|
||||
node.fID = PFunctionGrammar::mapID; // keep the ID
|
||||
node.fIvalue = ivalue; // keep the value
|
||||
// cout << endl << ">> mapID: value = " << ivalue;
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) { // handle function like cos ...
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::functionID;
|
||||
// keep function tag
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
iter_t it = i->children.begin();
|
||||
str = string(it->value.begin(), it->value.end()); // get string
|
||||
// cout << endl << ">> functionID: value = " << str;
|
||||
if (!strcmp(str.c_str(), "COS"))
|
||||
node.fFunctionTag = FUN_COS;
|
||||
else if (!strcmp(str.c_str(), "SIN"))
|
||||
node.fFunctionTag = FUN_SIN;
|
||||
else if (!strcmp(str.c_str(), "TAN"))
|
||||
node.fFunctionTag = FUN_TAN;
|
||||
else if (!strcmp(str.c_str(), "COSH"))
|
||||
node.fFunctionTag = FUN_COSH;
|
||||
else if (!strcmp(str.c_str(), "SINH"))
|
||||
node.fFunctionTag = FUN_SINH;
|
||||
else if (!strcmp(str.c_str(), "TANH"))
|
||||
node.fFunctionTag = FUN_TANH;
|
||||
else if (!strcmp(str.c_str(), "ACOS"))
|
||||
node.fFunctionTag = FUN_ACOS;
|
||||
else if (!strcmp(str.c_str(), "ASIN"))
|
||||
node.fFunctionTag = FUN_ASIN;
|
||||
else if (!strcmp(str.c_str(), "ATAN"))
|
||||
node.fFunctionTag = FUN_ATAN;
|
||||
else if (!strcmp(str.c_str(), "ACOSH"))
|
||||
node.fFunctionTag = FUN_ACOSH;
|
||||
else if (!strcmp(str.c_str(), "ASINH"))
|
||||
node.fFunctionTag = FUN_ASINH;
|
||||
else if (!strcmp(str.c_str(), "ATANH"))
|
||||
node.fFunctionTag = FUN_ATANH;
|
||||
else if (!strcmp(str.c_str(), "LOG"))
|
||||
node.fFunctionTag = FUN_LOG;
|
||||
else if (!strcmp(str.c_str(), "LN"))
|
||||
node.fFunctionTag = FUN_LN;
|
||||
else if (!strcmp(str.c_str(), "EXP"))
|
||||
node.fFunctionTag = FUN_EXP;
|
||||
else {
|
||||
cout << endl << "**PANIC ERROR**: function " << str << " doesn't exist, but you never should have reached this point!";
|
||||
assert(0);
|
||||
}
|
||||
// add node
|
||||
node.children.push_back(child);
|
||||
// i: 'funcName', '(', 'expression', ')'
|
||||
FillFuncEvalTree(i->children.begin()+2, node.children[0]);
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
// cout << endl << ">> factorID";
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::factorID;
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::termID;
|
||||
// keep operator tag
|
||||
if (*i->value.begin() == '*')
|
||||
node.fOperatorTag = OP_MUL;
|
||||
else
|
||||
node.fOperatorTag = OP_DIV;
|
||||
/*
|
||||
if (node.fOperatorTag == OP_MUL)
|
||||
cout << endl << ">> termID: value = *";
|
||||
else
|
||||
cout << endl << ">> termID: value = /";
|
||||
*/
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
// add child rhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin()+1, node.children[1]);
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) { // handle expression
|
||||
// keep the id
|
||||
node.fID = PFunctionGrammar::expressionID;
|
||||
// keep operator tag
|
||||
if (*i->value.begin() == '+')
|
||||
node.fOperatorTag = OP_ADD;
|
||||
else
|
||||
node.fOperatorTag = OP_SUB;
|
||||
/*
|
||||
if (node.fOperatorTag == OP_ADD)
|
||||
cout << endl << ">> expressionID: value = +";
|
||||
else
|
||||
cout << endl << ">> expressionID: value = -";
|
||||
*/
|
||||
// add child lhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin(), node.children[0]);
|
||||
// add child rhs
|
||||
node.children.push_back(child);
|
||||
FillFuncEvalTree(i->children.begin()+1, node.children[1]);
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
// nothing to be done except to pass the next element in the ast
|
||||
// i: 'funx', '=', 'expression'
|
||||
FillFuncEvalTree(i->children.begin()+2, node);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Eval (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
double PFunction::Eval()
|
||||
{
|
||||
return EvalNode(fFunc);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvalNode (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param node
|
||||
*/
|
||||
double PFunction::EvalNode(PFuncTreeNode &node)
|
||||
{
|
||||
if (node.fID == PFunctionGrammar::realID) {
|
||||
return node.fDvalue;
|
||||
} else if (node.fID == PFunctionGrammar::parameterID) {
|
||||
return fParam[node.fIvalue-1];
|
||||
} else if (node.fID == PFunctionGrammar::mapID) {
|
||||
return fParam[fMap[node.fIvalue-1]-1];
|
||||
} else if (node.fID == PFunctionGrammar::functionID) {
|
||||
if (node.fFunctionTag == FUN_COS) {
|
||||
return cos(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_SIN) {
|
||||
return sin(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_TAN) {
|
||||
return tan(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_COSH) {
|
||||
return cosh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_SINH) {
|
||||
return sinh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_TANH) {
|
||||
return tanh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ACOS) {
|
||||
return acos(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ASIN) {
|
||||
return asin(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ATAN) {
|
||||
return atan(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ACOSH) {
|
||||
return acosh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ASINH) {
|
||||
return asinh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_ATANH) {
|
||||
return atanh(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_LOG) {
|
||||
return log(EvalNode(node.children[0]))/log(10);
|
||||
} else if (node.fFunctionTag == FUN_LN) {
|
||||
return log(EvalNode(node.children[0]));
|
||||
} else if (node.fFunctionTag == FUN_EXP) {
|
||||
return exp(EvalNode(node.children[0]));
|
||||
} else {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: node.fID == PFunctionGrammar::functionID: you never should have reached this point!" << endl;
|
||||
assert(0);
|
||||
}
|
||||
} else if (node.fID == PFunctionGrammar::factorID) {
|
||||
return EvalNode(node.children[0]);
|
||||
} else if (node.fID == PFunctionGrammar::termID) {
|
||||
if (node.fOperatorTag == OP_MUL) {
|
||||
return EvalNode(node.children[0]) * EvalNode(node.children[1]);
|
||||
} else {
|
||||
double denominator = EvalNode(node.children[1]);
|
||||
if (denominator == 0.0) {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: division by 0.0" << endl;
|
||||
assert(0);
|
||||
}
|
||||
return EvalNode(node.children[0]) / denominator;
|
||||
}
|
||||
} else if (node.fID == PFunctionGrammar::expressionID) {
|
||||
if (node.fOperatorTag == OP_ADD) {
|
||||
return EvalNode(node.children[0]) + EvalNode(node.children[1]);
|
||||
} else {
|
||||
return EvalNode(node.children[0]) - EvalNode(node.children[1]);
|
||||
}
|
||||
} else {
|
||||
cout << endl << "**PANIC ERROR**: PFunction::EvalNode: you never should have reached this point!" << endl;
|
||||
assert(0);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CleanupFuncEvalTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
void PFunction::CleanupFuncEvalTree()
|
||||
{
|
||||
// clean up all children
|
||||
CleanupNode(fFunc);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// CleanupNode (protected)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param node
|
||||
*/
|
||||
void PFunction::CleanupNode(PFuncTreeNode &node)
|
||||
{
|
||||
if (node.children.size() != 0) {
|
||||
for (unsigned int i=0; i<node.children.size(); i++) {
|
||||
CleanupNode(node.children[i]);
|
||||
}
|
||||
node.children.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvaluateTree (protected)
|
||||
//-------------------------------------------------------------
|
||||
long PFunction::EvaluateTree(tree_parse_info<> info)
|
||||
{
|
||||
return EvalTreeExpression(info.trees.begin());
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// EvalTreeExpression (protected)
|
||||
//-------------------------------------------------------------
|
||||
long PFunction::EvalTreeExpression(iter_t const& i)
|
||||
{
|
||||
static int counter = 0;
|
||||
static int termOp = 0;
|
||||
|
||||
cout << endl << counter <<": in EvalExpression. i->value = '" <<
|
||||
string(i->value.begin(), i->value.end()) <<
|
||||
"' i->children.size() = " << i->children.size() << endl;
|
||||
|
||||
if (i->value.id() == PFunctionGrammar::realID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "realID: children = " << i->children.size();
|
||||
cout << endl << "realID: " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
if (*i->value.begin() == '-')
|
||||
fFuncString += "(";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
if (*i->value.begin() == '-')
|
||||
fFuncString += ")";
|
||||
} else if (i->value.id() == PFunctionGrammar::funLabelID) {
|
||||
assert(i->children.size() == 0);
|
||||
//SetFuncNo(i);
|
||||
cout << endl << "funLabelID: children = " << i->children.size();
|
||||
cout << endl << "funLabelID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end()); // funx
|
||||
} else if (i->value.id() == PFunctionGrammar::parameterID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "parameterID: children = " << i->children.size();
|
||||
cout << endl << "parameterID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
} else if (i->value.id() == PFunctionGrammar::mapID) {
|
||||
assert(i->children.size() == 0);
|
||||
cout << endl << "mapID: children = " << i->children.size();
|
||||
cout << endl << "mapID: value = " << string(i->value.begin(), i->value.end());
|
||||
cout << endl << "-----";
|
||||
fFuncString += string(i->value.begin(), i->value.end());
|
||||
} else if (i->value.id() == PFunctionGrammar::functionID) {
|
||||
assert(i->children.size() == 4);
|
||||
cout << endl << "functionID: children = " << i->children.size();
|
||||
iter_t it = i->children.begin();
|
||||
cout << endl << "functionID: value = " << string(it->value.begin(), it->value.end());
|
||||
cout << endl << "-----";
|
||||
// funcName, '(', expression, ')'
|
||||
counter++;
|
||||
fFuncString += string(it->value.begin(), it->value.end());
|
||||
if (termOp == 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin()+2); // the real stuff
|
||||
if (termOp == 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else if (i->value.id() == PFunctionGrammar::factorID) {
|
||||
cout << endl << "factorID: children = " << i->children.size();
|
||||
counter++;
|
||||
return EvalTreeExpression(i->children.begin());
|
||||
counter--;
|
||||
} else if (i->value.id() == PFunctionGrammar::termID) {
|
||||
cout << endl << "termID: children = " << i->children.size();
|
||||
if (*i->value.begin() == '*') {
|
||||
cout << endl << "termID: '*'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
termOp++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " * ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
termOp--;
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '/') {
|
||||
cout << endl << "termID: '/'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
termOp++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " / ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
termOp--;
|
||||
counter--;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::expressionID) {
|
||||
cout << endl << "expressionID: children = " << i->children.size();
|
||||
if (*i->value.begin() == '+') {
|
||||
cout << endl << "expressionID: '+'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
if (termOp > 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " + ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
if (termOp > 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '-') {
|
||||
cout << endl << "expressionID: '-'";
|
||||
assert(i->children.size() == 2);
|
||||
counter++;
|
||||
if (termOp > 0)
|
||||
fFuncString += "(";
|
||||
EvalTreeExpression(i->children.begin());
|
||||
fFuncString += " - ";
|
||||
EvalTreeExpression(i->children.begin()+1);
|
||||
if (termOp > 0)
|
||||
fFuncString += ")";
|
||||
counter--;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
|
||||
cout << endl << "assignmentID: children = " << i->children.size();
|
||||
assert(i->children.size() == 3);
|
||||
counter++;
|
||||
EvalTreeExpression(i->children.begin());
|
||||
EvalTreeExpression(i->children.begin()+1); // this is the string "="
|
||||
EvalTreeExpression(i->children.begin()+2); // this is the real stuff
|
||||
counter--;
|
||||
} else if (*i->value.begin() == '=') {
|
||||
cout << endl << "'=' in assignment";
|
||||
fFuncString += " = ";
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
111
src/tests/spirit/PFunction.h
Normal file
111
src/tests/spirit/PFunction.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunction.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTION_H_
|
||||
#define _PFUNCTION_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <boost/spirit/tree/ast.hpp>
|
||||
using namespace boost::spirit;
|
||||
|
||||
#include "PFunctionGrammar.h"
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
#define OP_ADD 0
|
||||
#define OP_SUB 1
|
||||
#define OP_MUL 2
|
||||
#define OP_DIV 3
|
||||
|
||||
#define FUN_COS 0
|
||||
#define FUN_SIN 1
|
||||
#define FUN_TAN 2
|
||||
#define FUN_COSH 3
|
||||
#define FUN_SINH 4
|
||||
#define FUN_TANH 5
|
||||
#define FUN_ACOS 6
|
||||
#define FUN_ASIN 7
|
||||
#define FUN_ATAN 8
|
||||
#define FUN_ACOSH 9
|
||||
#define FUN_ASINH 10
|
||||
#define FUN_ATANH 11
|
||||
#define FUN_LOG 12
|
||||
#define FUN_LN 13
|
||||
#define FUN_EXP 14
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
typedef struct func_tree_node {
|
||||
int fID; ///< tag showing what tree element this is
|
||||
int fOperatorTag; ///< tag for '+', '-', '*', '/'
|
||||
int fFunctionTag; ///< tag got "cos", "sin", ...
|
||||
int fIvalue; ///< for parameter numbers and maps
|
||||
double fDvalue; ///< for numbers
|
||||
vector<func_tree_node> children; ///< holding sub-tree
|
||||
} PFuncTreeNode;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
class PFunction {
|
||||
public:
|
||||
PFunction(tree_parse_info<> info, vector<double> param, vector<int> map);
|
||||
virtual ~PFunction();
|
||||
|
||||
virtual bool IsValid() { return fValid; }
|
||||
virtual int GetFuncNo() { return fFuncNo; }
|
||||
virtual double Eval();
|
||||
|
||||
protected:
|
||||
virtual bool CheckParameterAndMapRange();
|
||||
virtual bool CheckParameterAndMapInTree(iter_t const& i);
|
||||
virtual bool SetFuncNo(iter_t const& i);
|
||||
|
||||
virtual bool GenerateFuncEvalTree();
|
||||
virtual void FillFuncEvalTree(iter_t const& i, PFuncTreeNode &node);
|
||||
virtual double EvalNode(PFuncTreeNode &node);
|
||||
virtual void CleanupFuncEvalTree();
|
||||
virtual void CleanupNode(PFuncTreeNode &node);
|
||||
|
||||
virtual long EvaluateTree(tree_parse_info<> info);
|
||||
virtual long EvalTreeExpression(iter_t const& i);
|
||||
|
||||
private:
|
||||
tree_parse_info<> fInfo;
|
||||
vector<double> fParam;
|
||||
vector<int> fMap;
|
||||
PFuncTreeNode fFunc;
|
||||
|
||||
bool fValid; ///< flag showing if the function is valid
|
||||
int fFuncNo; ///< function number, i.e. FUNx with x the function number
|
||||
|
||||
string fFuncString;
|
||||
};
|
||||
|
||||
#endif // _PFUNCTION_H_
|
||||
142
src/tests/spirit/PFunctionGrammar.h
Normal file
142
src/tests/spirit/PFunctionGrammar.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionGrammer.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTIONGRAMMAR_H_
|
||||
#define _PFUNCTIONGRAMMAR_H_
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
//#define BOOST_SPIRIT_DEBUG
|
||||
|
||||
#include <boost/spirit/core.hpp>
|
||||
#include <boost/spirit/tree/ast.hpp>
|
||||
using namespace boost::spirit;
|
||||
|
||||
typedef char const* iterator_t;
|
||||
typedef tree_match<iterator_t> parse_tree_match_t;
|
||||
typedef parse_tree_match_t::tree_iterator iter_t;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
*
|
||||
*/
|
||||
struct PFunctionGrammar : public grammar<PFunctionGrammar>
|
||||
{
|
||||
static const int realID = 1;
|
||||
static const int funLabelID = 2;
|
||||
static const int parameterID = 3;
|
||||
static const int mapID = 4;
|
||||
static const int functionID = 5;
|
||||
static const int factorID = 6;
|
||||
static const int termID = 7;
|
||||
static const int expressionID = 8;
|
||||
static const int assignmentID = 9;
|
||||
|
||||
template <typename ScannerT>
|
||||
struct definition
|
||||
{
|
||||
definition(PFunctionGrammar const& /*self*/)
|
||||
{
|
||||
// Start grammar definition
|
||||
real = leaf_node_d[ real_p ];
|
||||
|
||||
fun_label = leaf_node_d[ ( lexeme_d[ "FUN" >> +digit_p ] ) ];
|
||||
|
||||
parameter = leaf_node_d[ ( lexeme_d[ "PAR" >> +digit_p ] ) ];
|
||||
|
||||
map = leaf_node_d[ ( lexeme_d[ "MAP" >> +digit_p ] ) ];
|
||||
|
||||
function = str_p("COS") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("SIN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("TAN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("COSH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("SINH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("TANH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ACOS") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ASIN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ATAN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ACOSH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ASINH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("ATANH") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("LOG") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("LN") >> ch_p('(') >> expression >> ch_p(')')
|
||||
| str_p("EXP") >> ch_p('(') >> expression >> ch_p(')')
|
||||
;
|
||||
|
||||
factor = real
|
||||
| parameter
|
||||
| map
|
||||
| function
|
||||
| inner_node_d[ch_p('(') >> expression >> ch_p(')')]
|
||||
;
|
||||
|
||||
term = factor >>
|
||||
*( (root_node_d[ch_p('*')] >> factor)
|
||||
| (root_node_d[ch_p('/')] >> factor)
|
||||
);
|
||||
|
||||
expression = term >>
|
||||
*( (root_node_d[ch_p('+')] >> term)
|
||||
| (root_node_d[ch_p('-')] >> term)
|
||||
);
|
||||
|
||||
assignment = (fun_label >> ch_p('=') >> expression);
|
||||
// End grammar definition
|
||||
|
||||
// turn on the debugging info.
|
||||
BOOST_SPIRIT_DEBUG_RULE(real);
|
||||
BOOST_SPIRIT_DEBUG_RULE(fun_label);
|
||||
BOOST_SPIRIT_DEBUG_RULE(parameter);
|
||||
BOOST_SPIRIT_DEBUG_RULE(map);
|
||||
BOOST_SPIRIT_DEBUG_RULE(function);
|
||||
BOOST_SPIRIT_DEBUG_RULE(factor);
|
||||
BOOST_SPIRIT_DEBUG_RULE(term);
|
||||
BOOST_SPIRIT_DEBUG_RULE(expression);
|
||||
BOOST_SPIRIT_DEBUG_RULE(assignment);
|
||||
}
|
||||
|
||||
rule<ScannerT, parser_context<>, parser_tag<assignmentID> > assignment;
|
||||
rule<ScannerT, parser_context<>, parser_tag<expressionID> > expression;
|
||||
rule<ScannerT, parser_context<>, parser_tag<termID> > term;
|
||||
rule<ScannerT, parser_context<>, parser_tag<factorID> > factor;
|
||||
rule<ScannerT, parser_context<>, parser_tag<functionID> > function;
|
||||
rule<ScannerT, parser_context<>, parser_tag<mapID> > map;
|
||||
rule<ScannerT, parser_context<>, parser_tag<parameterID> > parameter;
|
||||
rule<ScannerT, parser_context<>, parser_tag<funLabelID> > fun_label;
|
||||
rule<ScannerT, parser_context<>, parser_tag<realID> > real;
|
||||
|
||||
rule<ScannerT, parser_context<>, parser_tag<assignmentID> > const&
|
||||
start() const { return assignment; }
|
||||
};
|
||||
};
|
||||
|
||||
#endif // _PFUNCTIONGRAMMAR_H_
|
||||
417
src/tests/spirit/PFunctionHandler.cpp
Normal file
417
src/tests/spirit/PFunctionHandler.cpp
Normal file
@@ -0,0 +1,417 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionHandler.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
#include <qfile.h>
|
||||
#include <qtextstream.h>
|
||||
#include <qstring.h>
|
||||
|
||||
#include "PFunctionHandler.h"
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Constructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param fln
|
||||
*/
|
||||
PFunctionHandler::PFunctionHandler(char *fln)
|
||||
{
|
||||
fValid = true;
|
||||
fFileName = QString(fln);
|
||||
|
||||
cout << endl << "in PFunctionHandler(char *fln)";
|
||||
cout << endl << "fFileName = " << fFileName.latin1();
|
||||
|
||||
fValid = ReadFile();
|
||||
if (fValid)
|
||||
fValid = MapsAreValid();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Constructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param lines
|
||||
*/
|
||||
PFunctionHandler::PFunctionHandler(vector<QString> lines)
|
||||
{
|
||||
fValid = true;
|
||||
fFileName = "";
|
||||
|
||||
cout << endl << "in PFunctionHandler(vector<QString> lines)";
|
||||
|
||||
if (lines.size() == 0) {
|
||||
fValid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// analyze input
|
||||
bool done = false;
|
||||
int status;
|
||||
int val[10];
|
||||
double dval[10];
|
||||
bool inFcnBlock = false;
|
||||
for (unsigned int i=0; i<lines.size(); i++) {
|
||||
if (lines[i].startsWith("#")) // comment hence ignore
|
||||
continue;
|
||||
lines[i] = lines[i].upper();
|
||||
if (lines[i].startsWith("PAR")) {
|
||||
cout << endl << "this is a parameter line ...";
|
||||
status = sscanf(lines[i].latin1(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
|
||||
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
|
||||
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
fValid = false;
|
||||
cout << endl << "invalid PAR line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "PAR line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fParam.push_back(dval[i]);
|
||||
}
|
||||
} else if (lines[i].startsWith("MAP")) {
|
||||
cout << endl << "this is a map line ...";
|
||||
status = sscanf(lines[i].latin1(), "MAP %d %d %d %d %d %d %d %d %d %d",
|
||||
&val[0], &val[1], &val[2], &val[3], &val[4],
|
||||
&val[5], &val[6], &val[7], &val[8], &val[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
fValid = false;
|
||||
cout << endl << "invalid MAP line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "MAP line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fMap.push_back(val[i]);
|
||||
}
|
||||
} else if (lines[i].startsWith("FUNCTIONS")) {
|
||||
cout << endl << "the functions block start ...";
|
||||
inFcnBlock = true;
|
||||
} else if (lines[i].startsWith("END")) {
|
||||
cout << endl << "end tag found; rest will be ignored";
|
||||
done = true;
|
||||
} else if (inFcnBlock) {
|
||||
fLines.push_back(lines[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// check if all blocks are given
|
||||
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
|
||||
if (fMap.size() == 0)
|
||||
cout << endl << "MAP block is missing ...";
|
||||
if (fParam.size() == 0)
|
||||
cout << endl << "PAR block is missing ...";
|
||||
if (fLines.size() == 0)
|
||||
cout << endl << "FUNCTION block is missing ...";
|
||||
fValid = false;
|
||||
}
|
||||
|
||||
fValid = MapsAreValid();
|
||||
|
||||
if (fValid) {
|
||||
cout << endl << "Functions: ";
|
||||
for (unsigned int i=0; i<fLines.size(); i++)
|
||||
cout << endl << fLines[i].latin1();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Destructor
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
PFunctionHandler::~PFunctionHandler()
|
||||
{
|
||||
cout << endl << "in ~PFunctionHandler()" << endl << endl;
|
||||
fParam.clear();
|
||||
fMap.clear();
|
||||
fLines.clear();
|
||||
|
||||
fFuncs.clear();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// DoParse (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::DoParse()
|
||||
{
|
||||
cout << endl << "in PFunctionHandler::DoParse() ...";
|
||||
|
||||
bool success = true;
|
||||
PFunctionGrammar function;
|
||||
|
||||
for (unsigned int i=0; i<fLines.size(); i++) {
|
||||
cout << endl << "fLines[" << i << "] = '" << fLines[i].latin1() << "'";
|
||||
|
||||
tree_parse_info<> info = ast_parse(fLines[i].latin1(), function, space_p);
|
||||
|
||||
if (info.full) {
|
||||
cout << endl << "parse successfull ..." << endl;
|
||||
PFunction func(info, fParam, fMap);
|
||||
fFuncs.push_back(func);
|
||||
} else {
|
||||
cout << endl << "parse failed ... (" << i << ")" << endl;
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check that all functions are valid. It could be that parsing was fine but
|
||||
// the parameter index, or map index was out of range
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
if (!fFuncs[i].IsValid()) {
|
||||
cout << endl << "**ERROR**: function fun" << fFuncs[i].GetFuncNo();
|
||||
cout << " has a problem with either parameter or map out of range!";
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check that the function numbers are unique
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
for (unsigned int j=i+1; j<fFuncs.size(); j++) {
|
||||
if (fFuncs[i].GetFuncNo() == fFuncs[j].GetFuncNo()) {
|
||||
cout << endl << "**ERROR**: function number " << fFuncs[i].GetFuncNo();
|
||||
cout << " is at least twice present! Fix this first.";
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (success) {
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++)
|
||||
cout << endl << "func number = " << fFuncs[i].GetFuncNo();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Eval (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
double PFunctionHandler::Eval(int i)
|
||||
{
|
||||
if (GetFuncIndex(i) == -1) {
|
||||
cout << endl << "**ERROR**: Couldn't find FUN" << i << " for evaluation";
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
cout << endl << "PFunctionHandler::Eval: GetFuncIndex("<<i<<") = " << GetFuncIndex(i);
|
||||
cout << endl;
|
||||
|
||||
return fFuncs[GetFuncIndex(i)].Eval();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GetFuncNo (public)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param i
|
||||
*/
|
||||
unsigned int PFunctionHandler::GetFuncNo(unsigned int i)
|
||||
{
|
||||
if (i > fFuncs.size())
|
||||
return -1;
|
||||
|
||||
return fFuncs[i].GetFuncNo();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// ReadFile (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::ReadFile()
|
||||
{
|
||||
cout << endl << "in ~PFunctionHandler::ReadFile()";
|
||||
|
||||
if (fFileName.isEmpty()) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " no file name given :-(. Will quit";
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile f(fFileName);
|
||||
if (!f.exists()) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " File '" << fFileName.latin1() << "' does not exist.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!f.open(IO_ReadOnly)) {
|
||||
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
|
||||
cout << endl << " File '" << fFileName.latin1() << "' couldn't being opened.";
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream stream(&f);
|
||||
QString line;
|
||||
bool done = false;
|
||||
bool success = true;
|
||||
int status;
|
||||
int val[10];
|
||||
double dval[10];
|
||||
bool inFcnBlock = false;
|
||||
while ( !stream.atEnd() && !done) {
|
||||
line = stream.readLine(); // line of text excluding '\n'
|
||||
if (line.startsWith("#")) // comment hence ignore
|
||||
continue;
|
||||
line = line.upper();
|
||||
if (line.startsWith("PAR")) {
|
||||
cout << endl << "this is a parameter line ...";
|
||||
status = sscanf(line.latin1(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
|
||||
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
|
||||
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
success = false;
|
||||
cout << endl << "invalid PAR line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "PAR line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fParam.push_back(dval[i]);
|
||||
}
|
||||
} else if (line.startsWith("MAP")) {
|
||||
cout << endl << "this is a map line ...";
|
||||
status = sscanf(line.latin1(), "MAP %d %d %d %d %d %d %d %d %d %d",
|
||||
&val[0], &val[1], &val[2], &val[3], &val[4],
|
||||
&val[5], &val[6], &val[7], &val[8], &val[9]);
|
||||
if (status < 0) {
|
||||
done = true;
|
||||
success = false;
|
||||
cout << endl << "invalid MAP line, sorry ...";
|
||||
} else { // fill map
|
||||
cout << endl << "MAP line, status = " << status;
|
||||
for (int i=0; i<status; i++)
|
||||
fMap.push_back(val[i]);
|
||||
}
|
||||
} else if (line.startsWith("FUNCTIONS")) {
|
||||
cout << endl << "the functions block start ...";
|
||||
inFcnBlock = true;
|
||||
} else if (line.startsWith("END")) {
|
||||
cout << endl << "end tag found; rest will be ignored";
|
||||
done = true;
|
||||
} else if (inFcnBlock) {
|
||||
fLines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
f.close();
|
||||
|
||||
// check if all blocks are given
|
||||
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
|
||||
if (fMap.size() == 0)
|
||||
cout << endl << "MAP block is missing ...";
|
||||
if (fParam.size() == 0)
|
||||
cout << endl << "PAR block is missing ...";
|
||||
if (fLines.size() == 0)
|
||||
cout << endl << "FUNCTION block is missing ...";
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
cout << endl << "Functions: ";
|
||||
for (unsigned int i=0; i<fLines.size(); i++)
|
||||
cout << endl << fLines[i].latin1();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// MapsAreValid (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
*/
|
||||
bool PFunctionHandler::MapsAreValid()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
int maxParam = fParam.size();
|
||||
for (unsigned int i=0; i<fMap.size(); i++)
|
||||
if (fMap[i] > maxParam)
|
||||
success = false;
|
||||
|
||||
if (!success)
|
||||
cout << endl << "invalid MAP found ...";
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// GetFuncIndex (private)
|
||||
//-------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param funcNo
|
||||
*/
|
||||
int PFunctionHandler::GetFuncIndex(int funcNo)
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
for (unsigned int i=0; i<fFuncs.size(); i++) {
|
||||
if (fFuncs[i].GetFuncNo() == funcNo) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
86
src/tests/spirit/PFunctionHandler.h
Normal file
86
src/tests/spirit/PFunctionHandler.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/***************************************************************************
|
||||
|
||||
PFunctionHandler.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.c *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PFUNCTIONHANDLER_H_
|
||||
#define _PFUNCTIONHANDLER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
#include <qstring.h>
|
||||
|
||||
#include "PFunctionGrammar.h"
|
||||
#include "PFunction.h"
|
||||
|
||||
class PFunctionHandler
|
||||
{
|
||||
public:
|
||||
PFunctionHandler(char *fln);
|
||||
PFunctionHandler(vector<QString> lines);
|
||||
virtual ~PFunctionHandler();
|
||||
|
||||
virtual bool IsValid() { return fValid; }
|
||||
virtual bool DoParse();
|
||||
virtual double Eval(int i);
|
||||
virtual unsigned int GetFuncNo(unsigned int i);
|
||||
virtual unsigned int GetNoOfFuncs() { return fFuncs.size(); }
|
||||
|
||||
private:
|
||||
bool fValid;
|
||||
|
||||
QString fFileName;
|
||||
|
||||
vector<double> fParam;
|
||||
vector<int> fMap;
|
||||
vector<QString> fLines;
|
||||
|
||||
vector<PFunction> fFuncs;
|
||||
|
||||
virtual bool ReadFile();
|
||||
virtual bool MapsAreValid();
|
||||
virtual int GetFuncIndex(int funcNo);
|
||||
};
|
||||
|
||||
// cint dictionary stuff --------------------------------------
|
||||
#ifdef __CINT__
|
||||
|
||||
#pragma link off all globals;
|
||||
#pragma link off all classes;
|
||||
#pragma link off all functions;
|
||||
|
||||
#pragma link C++ class PFunctionHandler+;
|
||||
|
||||
#endif // end __CINT__
|
||||
//-------------------------------------------------------------
|
||||
|
||||
#endif // _PFUNCTIONHANDLER_H_
|
||||
|
||||
28
src/tests/spirit/fcnInput.txt
Normal file
28
src/tests/spirit/fcnInput.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
#----------------------------------------------
|
||||
# function input file
|
||||
#----------------------------------------------
|
||||
#
|
||||
# '#' are comment lines
|
||||
#
|
||||
# The file has the following structure:
|
||||
#
|
||||
# PAR <par1> <par2> ... <parN>
|
||||
# MAP <map1> <map2> ... <mapM>
|
||||
# FUNCTIONS
|
||||
# fun1 = <function>
|
||||
# fun2 = <function>
|
||||
# funX = <function>
|
||||
# END
|
||||
#----------------------------------------------
|
||||
PAR 1.0 2.1 3.5 -0.87 0.87
|
||||
MAP 2 1 4 5
|
||||
FUNCTIONS
|
||||
fun0 = sin(par3/(par1+map2))
|
||||
#fun0 = par1 + map3 * cos(cos(par2 - map1))
|
||||
#fun8 = log(sin(par1)) + exp(-1.0*map2)
|
||||
#fun1 = par1 + map1 * (0.01355+par1*(2.1 - (-2.3 / 3.4)))
|
||||
#fun2 = par1 * par2 - map3
|
||||
#fun3 = -3.2 + (par2-par1)/(map2+map3)
|
||||
#fun7 = 1.2
|
||||
END
|
||||
#----------------------------------------------
|
||||
BIN
src/tests/spirit/spirit.pdf
Normal file
BIN
src/tests/spirit/spirit.pdf
Normal file
Binary file not shown.
100
src/tests/spirit/spirit_fcn_test.cpp
Normal file
100
src/tests/spirit/spirit_fcn_test.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PFunctionHandler.h"
|
||||
|
||||
//-----------------------------------------------------
|
||||
void syntax()
|
||||
{
|
||||
cout << endl << "spirit_fcn_test [--file <filename>] | [--help]";
|
||||
cout << endl << " without arguments: interactive mode";
|
||||
cout << endl << " --file <filename>: function block etc. from file";
|
||||
cout << endl << " --help: this help";
|
||||
cout << endl << endl;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
void handle_input(vector<QString> &lines)
|
||||
{
|
||||
cout << endl << "will handle input ...";
|
||||
cout << endl << "you should provide a PAR, a MAP, and a FUNCTION block";
|
||||
cout << endl << " Map block:";
|
||||
cout << endl << " MAP <map1> <map2> ... <mapM>";
|
||||
cout << endl << " Parameter block:";
|
||||
cout << endl << " PAR <par1> <par2> ... <parN>";
|
||||
cout << endl << " Function Block:";
|
||||
cout << endl << " FUNCTION";
|
||||
cout << endl << " fun1 = <function1>";
|
||||
cout << endl << " fun2 = <function2>";
|
||||
cout << endl << " ...";
|
||||
cout << endl << " funX = <functionX>";
|
||||
cout << endl << " END";
|
||||
cout << endl << "to get out of the input handle type '.q'";
|
||||
cout << endl;
|
||||
bool done = false;
|
||||
char str[128];
|
||||
do {
|
||||
cout << ">> ";
|
||||
cin.getline(str, sizeof(str));
|
||||
if (!strcmp(str, ".q"))
|
||||
done = true;
|
||||
else
|
||||
lines.push_back(str);
|
||||
} while (!done);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool inputFile = false;
|
||||
|
||||
if (argc > 3) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else if (argc == 2) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else if (argc == 3) {
|
||||
if (strcmp(argv[1], "--file")) {
|
||||
syntax();
|
||||
return 0;
|
||||
} else {
|
||||
inputFile = true;
|
||||
}
|
||||
}
|
||||
|
||||
PFunctionHandler *fcnHandler = 0;
|
||||
|
||||
if (inputFile) {
|
||||
fcnHandler = new PFunctionHandler(argv[2]);
|
||||
} else {
|
||||
vector<QString> lines;
|
||||
handle_input(lines);
|
||||
cout << endl << "lines.size() = " << lines.size();
|
||||
fcnHandler = new PFunctionHandler(lines);
|
||||
}
|
||||
|
||||
if (fcnHandler == 0) {
|
||||
cout << endl << "Couldn't invoke function handler, sorry ..." << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool go_on = fcnHandler->IsValid();
|
||||
|
||||
if (go_on) {
|
||||
cout << endl << "will do the parsing ...";
|
||||
if (fcnHandler->DoParse()) {
|
||||
cout << endl << "will do the evaluation ...";
|
||||
for (unsigned int i=0; i<fcnHandler->GetNoOfFuncs(); i++)
|
||||
cout << endl << "FUN" << fcnHandler->GetFuncNo(i) << " = " << fcnHandler->Eval(fcnHandler->GetFuncNo(i));
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
if (fcnHandler) {
|
||||
delete fcnHandler;
|
||||
fcnHandler = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
24
src/tests/spirit/spirit_fcn_test.pro
Normal file
24
src/tests/spirit/spirit_fcn_test.pro
Normal file
@@ -0,0 +1,24 @@
|
||||
#------------------------------------------------------
|
||||
# spirit_fcn_test.pro
|
||||
# qmake file for spirit_fcn_test
|
||||
#
|
||||
# Andreas Suter, 2007/12/10
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PFunctionGrammar.h \
|
||||
PFunction.h \
|
||||
PFunctionHandler.h
|
||||
|
||||
SOURCES = spirit_fcn_test.cpp \
|
||||
PFunction.cpp \
|
||||
PFunctionHandler.cpp
|
||||
|
||||
TARGET=spirit_fcn_test
|
||||
|
||||
0
src/tests/stl_check/.kdbgrc.stl_check
Executable file
0
src/tests/stl_check/.kdbgrc.stl_check
Executable file
37
src/tests/stl_check/PPointObj.cpp
Normal file
37
src/tests/stl_check/PPointObj.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PPointObj.h"
|
||||
|
||||
PPointObj::PPointObj(unsigned int counter) : fCounter(counter)
|
||||
{
|
||||
fTest = new int[5];
|
||||
|
||||
for (unsigned int i=0; i<5; i++)
|
||||
fTest[i] = 10*counter+i;
|
||||
|
||||
cout << endl << "in PPointObj() " << fCounter << ": fTest = " << fTest;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
PPointObj::~PPointObj()
|
||||
{
|
||||
cout << endl << "in ~PPointObj() " << fCounter << ": fTest = " << fTest;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void PPointObj::PrintTest()
|
||||
{
|
||||
cout << endl << fCounter << ": ";
|
||||
for (unsigned int i=0; i<5; i++)
|
||||
cout << fTest[i] << ", ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void PPointObj::CleanUp()
|
||||
{
|
||||
if (fTest) {
|
||||
delete [] fTest;
|
||||
fTest = 0;
|
||||
}
|
||||
}
|
||||
17
src/tests/stl_check/PPointObj.h
Normal file
17
src/tests/stl_check/PPointObj.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _PPOINTOBJ_H_
|
||||
#define _PPOINTOBJ_H_
|
||||
|
||||
class PPointObj {
|
||||
public:
|
||||
PPointObj(unsigned int counter);
|
||||
~PPointObj();
|
||||
|
||||
void PrintTest();
|
||||
void CleanUp();
|
||||
|
||||
private:
|
||||
int fCounter;
|
||||
int *fTest;
|
||||
};
|
||||
|
||||
#endif // _PPOINTOBJ_H_
|
||||
10
src/tests/stl_check/PStlCheck.cpp
Normal file
10
src/tests/stl_check/PStlCheck.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "PStlCheck.h"
|
||||
|
||||
PStlCheck::PStlCheck()
|
||||
{
|
||||
}
|
||||
|
||||
PStlCheck::~PStlCheck()
|
||||
{
|
||||
fPpo.clear();
|
||||
}
|
||||
17
src/tests/stl_check/PStlCheck.h
Normal file
17
src/tests/stl_check/PStlCheck.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _PSTLCHECK_H_
|
||||
#define _PSTLCHECK_H_
|
||||
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
#include "PPointObj.h"
|
||||
|
||||
class PStlCheck {
|
||||
public:
|
||||
PStlCheck();
|
||||
~PStlCheck();
|
||||
|
||||
list<PPointObj> fPpo;
|
||||
};
|
||||
|
||||
#endif // _PSTLCHECK_H_
|
||||
28
src/tests/stl_check/stl_check.cpp
Normal file
28
src/tests/stl_check/stl_check.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
#include "PStlCheck.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
PStlCheck check;
|
||||
unsigned int counter = 0;
|
||||
|
||||
for (unsigned int i=0; i<2; i++) {
|
||||
check.fPpo.push_back(PPointObj(counter++));
|
||||
cout << endl << "----------";
|
||||
}
|
||||
|
||||
cout << endl << "size = " << check.fPpo.size();
|
||||
cout << endl;
|
||||
|
||||
list<PPointObj>::iterator iter;
|
||||
for (iter=check.fPpo.begin(); iter!=check.fPpo.end(); ++iter)
|
||||
iter->PrintTest();
|
||||
|
||||
for (iter=check.fPpo.begin(); iter!=check.fPpo.end(); ++iter)
|
||||
iter->CleanUp();
|
||||
|
||||
return 0;
|
||||
}
|
||||
22
src/tests/stl_check/stl_check.pro
Normal file
22
src/tests/stl_check/stl_check.pro
Normal file
@@ -0,0 +1,22 @@
|
||||
#------------------------------------------------------
|
||||
# stl_check.pro
|
||||
# qmake file for stl_check
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile.stl_check
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PStlCheck.h \
|
||||
PPointObj.h
|
||||
|
||||
SOURCES = stl_check.cpp \
|
||||
PStlCheck.cpp \
|
||||
PPointObj.cpp
|
||||
|
||||
TARGET=stl_check
|
||||
36
src/tests/stl_check_2/PPointObj.cpp
Normal file
36
src/tests/stl_check_2/PPointObj.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "PPointObj.h"
|
||||
|
||||
#define PPO_SIZE 100
|
||||
|
||||
PPointObj::PPointObj(unsigned int counter) : fCounter(counter)
|
||||
{
|
||||
fTest = new int[PPO_SIZE];
|
||||
|
||||
for (unsigned int i=0; i<PPO_SIZE; i++)
|
||||
fTest[i] = 2*PPO_SIZE*counter+i;
|
||||
|
||||
cout << endl << "in PPointObj() " << fCounter << ": fTest = " << fTest;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
PPointObj::~PPointObj()
|
||||
{
|
||||
if (fTest) {
|
||||
delete [] fTest;
|
||||
fTest = 0;
|
||||
}
|
||||
|
||||
cout << endl << "in ~PPointObj() " << fCounter << ": fTest = " << fTest;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void PPointObj::PrintTest()
|
||||
{
|
||||
cout << endl << fCounter << ": ";
|
||||
for (unsigned int i=0; i<PPO_SIZE; i++)
|
||||
cout << fTest[i] << ", ";
|
||||
cout << endl;
|
||||
}
|
||||
16
src/tests/stl_check_2/PPointObj.h
Normal file
16
src/tests/stl_check_2/PPointObj.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _PPOINTOBJ_H_
|
||||
#define _PPOINTOBJ_H_
|
||||
|
||||
class PPointObj {
|
||||
public:
|
||||
PPointObj(unsigned int counter);
|
||||
~PPointObj();
|
||||
|
||||
void PrintTest();
|
||||
|
||||
private:
|
||||
int fCounter;
|
||||
int *fTest;
|
||||
};
|
||||
|
||||
#endif // _PPOINTOBJ_H_
|
||||
29
src/tests/stl_check_2/PStlCheck.cpp
Normal file
29
src/tests/stl_check_2/PStlCheck.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "PStlCheck.h"
|
||||
|
||||
PStlCheck::PStlCheck()
|
||||
{
|
||||
}
|
||||
|
||||
PStlCheck::~PStlCheck()
|
||||
{
|
||||
fPpo.clear();
|
||||
}
|
||||
|
||||
void PStlCheck::Add(unsigned int count)
|
||||
{
|
||||
fPpo.push_back(new PPointObj(count));
|
||||
}
|
||||
|
||||
void PStlCheck::CleanUp()
|
||||
{
|
||||
for (unsigned int i=0; i<fPpo.size(); i++) {
|
||||
fPpo[i]->~PPointObj();
|
||||
}
|
||||
}
|
||||
|
||||
void PStlCheck::PrintTest()
|
||||
{
|
||||
for (unsigned int i=0; i<fPpo.size(); i++)
|
||||
fPpo[i]->PrintTest();
|
||||
}
|
||||
|
||||
22
src/tests/stl_check_2/PStlCheck.h
Normal file
22
src/tests/stl_check_2/PStlCheck.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef _PSTLCHECK_H_
|
||||
#define _PSTLCHECK_H_
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
#include "PPointObj.h"
|
||||
|
||||
class PStlCheck {
|
||||
public:
|
||||
PStlCheck();
|
||||
~PStlCheck();
|
||||
|
||||
void Add(unsigned int count);
|
||||
void CleanUp();
|
||||
void PrintTest();
|
||||
|
||||
private:
|
||||
vector<PPointObj*> fPpo;
|
||||
};
|
||||
|
||||
#endif // _PSTLCHECK_H_
|
||||
21
src/tests/stl_check_2/stl_check_2.cpp
Normal file
21
src/tests/stl_check_2/stl_check_2.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
#include "PStlCheck.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
PStlCheck check;
|
||||
|
||||
for (unsigned int i=0; i<2; i++) {
|
||||
check.Add(i);
|
||||
cout << endl << "----------";
|
||||
}
|
||||
|
||||
check.PrintTest();
|
||||
|
||||
check.CleanUp();
|
||||
|
||||
return 0;
|
||||
}
|
||||
22
src/tests/stl_check_2/stl_check_2.pro
Normal file
22
src/tests/stl_check_2/stl_check_2.pro
Normal file
@@ -0,0 +1,22 @@
|
||||
#------------------------------------------------------
|
||||
# stl_check_2.pro
|
||||
# qmake file for stl_check_2
|
||||
#
|
||||
# Andreas Suter, 2007/05/14
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile.stl_check_2
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PStlCheck.h \
|
||||
PPointObj.h
|
||||
|
||||
SOURCES = stl_check_2.cpp \
|
||||
PStlCheck.cpp \
|
||||
PPointObj.cpp
|
||||
|
||||
TARGET=stl_check_2
|
||||
Reference in New Issue
Block a user