//testing script for the spline stuff - needs to be compiled //Felix #include "tpc/spacecharge/CubSpline.h" #include "tpc/spacecharge/CubSplineFitter.h" #include "tpc/spacecharge/BSpline.h" #include "tpc/spacecharge/SplineTF1Interface.h" #include #include #include "TF1.h" #include "TH1D.h" #include "TCanvas.h" #include "TRandom.h" #include "TGraph.h" #include "TArrayD.h" #include "TMatrixD.h" #include "TVectorD.h" #include "TStopwatch.h" // // helper class ------------------------------------------------ // class Function { // public: // void setCSP(CubSpline* csp) { // _csp = csp; // _knots = _csp->getKnots(); // _length = _knots->size(); // _coeffs = _csp->getCoeffs(); // std::cout<<"ncoeffs="<<_coeffs->size()<setCoeffsByArray(p); // WATCH n_par of TF1 !!! // double result = _csp->eval(x[0]); // return result; // } // private: // CubSpline* _csp; // const std::vector* _knots; // std::vector* _coeffs; // int _length; // }; // // ------------------------------------------------------------- void testCubSpline() { int Nknots = 18; int Nlamda = Nknots+8; std::vector* knots = new std::vector; std::vector* coeff = new std::vector; double lL = 0; double uL = 15; double step = (uL - lL)/Nknots; double k; for(int i=0; ipush_back(k); if(ipush_back(i%4); //alternating 1's and 0's } std::cout<<"\n\nThe knots:"<size(); k++) std::cout<at(k)<<" "; std::cout<print(); for(int j=0; j<5; j++) { std::cout<<"\nValue at x = "<<(knots->at(10)) - j*step<<": " <eval(knots->at(10) - j*step); } std::cout<<"\nInitialized BSpline\n"<at(i); // build the histogram ------------------------------------------ TH1D* hist = new TH1D("blub","blub",100,0,14); for ( Int_t i = 0; i<1000; i++ ) hist->Fill(gRandom->Gaus(6,1)); for ( Int_t i = 0; i<1000; i++ ) hist->Fill(gRandom->Gaus(9,1)); for (int i = 0; i<100; i++) hist->AddBinContent(i); //WATCH NUMBER OF COEFFICIENTS!!! TF1* f1 =new TF1("f1",func,&SplineTF1Interface::eval, 0,14,Nlamda-4); f1->SetNpx(1000); f1->SetParameters(c_A.GetArray()); std::cout<<"\nInitialized TF1 f1"<Divide(1,2); c1->cd(1); f1->Draw(); c1->cd(2); TF1* f2 =new TF1("f2",func2,&SplineTF1Interface::eval, 0,14,Nlamda-4); TArrayD c_A2 = TArrayD(Nlamda-4); for(int i=0; iSetNpx(1000); f2->SetParameters(c_A2.fArray); //starting params don't matter //build data ----------------------------------------------------------- std::cout<<"\nBuilding data vector...."<*> data; int nbin = hist->GetNbinsX(); TVectorD data_vec = TVectorD(nbin); for(int i=0; i(2)); data[i]->at(0) = (double) i * 14/nbin; //x coordinate data[i]->at(1) = hist->GetBinContent(i+1); data_vec[i] = hist->GetBinContent(i+1); //} } std::cout<<"\nData point coordinates: "<at(0)<<" "; // --------------------------------------------------------------------- // std::cout<<"\nfirst entries of the data vector... "<at(0)<<" val: "<at(1)<getSpMatrix(); //matrix->Print(); fitter->decompose(); TVectorD solution = fitter->solve(data_vec); timer.Stop(); //TO DO: expand solution by zeros for each removed column //(see CubSplineFitter....) TArrayD sol_arr = TArrayD(Nknots+4); std::cout<<"\n\nNew solution coefficient vector:"<SetParameters(temp); hist->Draw(); // hist->Fit("f2"); //c1->cd(3); f2->Draw("same"); Double_t rtime = timer.RealTime(); Double_t ctime = timer.CpuTime(); printf("RealTime=%f seconds, CpuTime=%f seconds\n",rtime,ctime); }