1 /**************************************************************************
2 * Copyright(c) 2006-07, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 #include "AliSplineFit.h"
19 ClassImp(AliSplineFit);
22 AliSplineFit::fitterStatic()
24 static TLinearFitter* fit = new TLinearFitter(4,"pol3","");
28 AliSplineFit::AliSplineFit() :
45 // Default constructor
51 AliSplineFit::AliSplineFit(const AliSplineFit& source) :
53 fBDump (source.fBDump),
54 fGraph (source.fGraph),
56 fSigma (source.fSigma),
57 fMaxDelta (source.fMaxDelta),
65 fIndex = new Int_t[fN0];
66 fParams = new TClonesArray("TVectorD",fN0);
67 fCovars = new TClonesArray("TMatrixD",fN0);
68 fParams = (TClonesArray*)source.fParams->Clone();
69 fCovars = (TClonesArray*)source.fCovars->Clone();
70 for (Int_t i=0; i<fN0; i++) fIndex[i] = source.fIndex[i];
72 fX = new Double_t[fN];
73 fY0 = new Double_t[fN];
74 fY1 = new Double_t[fN];
75 fChi2I = new Double_t[fN];
76 for (Int_t i=0; i<fN; i++){
78 fY0[i] = source.fY0[i];
79 fY1[i] = source.fY1[i];
82 AliSplineFit& AliSplineFit::operator=(const AliSplineFit& source){
84 // assignment operator
86 if (&source == this) return *this;
89 // reassign memory as previous fit could have a different size
92 if ( fN0 != source.fN0) {
99 fIndex = new Int_t[fN0];
100 fParams = new TClonesArray("TVectorD",fN0);
101 fCovars = new TClonesArray("TMatrixD",fN0);
103 if ( fN != source.fN) {
110 fX = new Double_t[fN];
111 fY0 = new Double_t[fN];
112 fY1 = new Double_t[fN];
113 fChi2I = new Double_t[fN];
116 // use copy constructor (without reassigning memory) to copy values
118 new (this) AliSplineFit(source);
124 AliSplineFit::~AliSplineFit(){
126 // destructor. Don't delete fGraph, as this normally comes as input parameter
137 Double_t AliSplineFit::Eval(Double_t x, Int_t deriv) const{
139 // evaluate value at x
140 // deriv = 0: function value
141 // = 1: first derivative
142 // = 2: 2nd derivative
143 // = 3: 3rd derivative
145 // a2 = -(3*a0 -3*b0 + 2*a1*dx +b1*dx)/(dx*dx)
146 // a3 = -(-2*a0+2*b0 - a1*dx - b1*dx)/(dx*dx*dx)
148 Int_t index = TMath::BinarySearch(fN,fX,x);
149 if (index<0) index =0;
150 if (index>fN-2) index =fN-2;
152 Double_t dx = x-fX[index];
153 Double_t dxc = fX[index+1]-fX[index];
154 Double_t y0 = fY0[index];
155 Double_t y1 = fY1[index];
156 Double_t y01 = fY0[index+1];
157 Double_t y11 = fY1[index+1];
158 Double_t y2 = -(3.*y0-3.*y01+2*y1*dxc+y11*dxc)/(dxc*dxc);
159 Double_t y3 = -(-2.* y0 + 2*y01 - y1*dxc - y11*dxc) /(dxc*dxc*dxc);
160 Double_t val = y0+y1*dx+y2*dx*dx+y3*dx*dx*dx;
161 if (deriv==1) val = y1+2.*y2*dx+3.*y3*dx*dx;
162 if (deriv==2) val = 2.*y2+6.*y3*dx;
163 if (deriv==3) val = 6*y3;
168 TGraph * AliSplineFit::GenerGraph(Int_t npoints, Double_t fraction, Double_t s1, Double_t s2, Double_t s3, Int_t der){
170 // generate random graph
173 // s1, s2, s3 - sigma of derivative
176 Double_t *value = new Double_t[npoints];
177 Double_t *time = new Double_t[npoints];
178 Double_t d0=0, d1=0,d2=0,d3=0;
181 for(Int_t i=1; i<npoints; i++){
182 Double_t dtime = 1./npoints;
183 Double_t dd1 = dtime;
184 Double_t dd2 = dd1*dd1;
185 Double_t dd3 = dd2*dd1;
186 d0 += d1*dd1 + d2*dd2/2. + d3*dd3/6.;
187 d1 += d2*dd1 +d3*dd2/2;
190 time[i] = time[i-1]+dtime;
191 d1 =(1.-fraction)*d1+fraction*(gRandom->Exp(s1))*(gRandom->Rndm()-0.5);
192 d2 =(1.-fraction)*d2+fraction*(gRandom->Exp(s2))*(gRandom->Rndm()-0.5);
193 d3 =(1.-fraction)*d3+fraction*(gRandom->Exp(s3))*(gRandom->Rndm()-0.5);
194 if (gRandom->Rndm()<fraction) d3 =(1.-fraction)*d3+fraction*(gRandom->BreitWigner(0,s3));
196 Double_t dmean = (value[npoints-1]-value[0])/(time[npoints-1]-time[0]);
197 Double_t min = value[0];
198 Double_t max = value[0];
199 for (Int_t i=0; i<npoints; i++){
200 value[i] = value[i]-dmean*(time[i]-time[0]);
201 if (value[i]<min) min=value[i];
202 if (value[i]>max) max=value[i];
205 for (Int_t i=0; i<npoints; i++){
206 value[i] = (value[i]-min)/(max-min);
208 if (der==1) for (Int_t i=1; i<npoints; i++){
209 value[i-1] = (value[i]-value[i-1])/(time[i]-time[i-1]);
212 TGraph * graph = new TGraph(npoints,time,value);
220 TGraph * AliSplineFit::GenerNoise(TGraph * graph0, Double_t sigma0){
222 // add noise to graph
225 Int_t npoints=graph0->GetN();
226 Double_t *value = new Double_t[npoints];
227 Double_t *time = new Double_t[npoints];
228 for(Int_t i=0; i<npoints; i++){
229 time[i] = graph0->GetX()[i];
230 value[i] = graph0->GetY()[i]+gRandom->Gaus(0,sigma0);
232 TGraph * graph = new TGraph(npoints,time,value);
240 TGraph * AliSplineFit::MakeGraph(Double_t xmin, Double_t xmax, Int_t npoints, Int_t deriv) const {
242 // if npoints<=0 draw derivative
247 if (deriv<=0) return new TGraph(fN,fX,fY0);
248 if (deriv==1) return new TGraph(fN,fX,fY1);
249 if (deriv>2) return new TGraph(fN-1,fX,fChi2I);
251 Double_t * x = new Double_t[npoints+1];
252 Double_t * y = new Double_t[npoints+1];
253 for (Int_t ip=0; ip<=npoints; ip++){
254 x[ip] = xmin+ (xmax-xmin)*(Double_t(ip)/Double_t(npoints));
255 y[ip] = Eval(x[ip],deriv);
258 graph = new TGraph(npoints,x,y);
264 TGraph * AliSplineFit::MakeDiff(TGraph * graph0) const {
266 // Make graph of difference to reference graph
269 Int_t npoints=graph0->GetN();
271 Double_t * x = new Double_t[npoints];
272 Double_t * y = new Double_t[npoints];
273 for (Int_t ip=0; ip<npoints; ip++){
274 x[ip] = graph0->GetX()[ip];
275 y[ip] = Eval(x[ip],0)-graph0->GetY()[ip];
277 graph = new TGraph(npoints,x,y);
284 TH1F * AliSplineFit::MakeDiffHisto(TGraph * graph0) const {
286 // Make histogram of difference to reference graph
289 Int_t npoints=graph0->GetN();
290 Float_t min=1e+39,max=-1e+39;
291 for (Int_t ip=0; ip<npoints; ip++){
292 Double_t x = graph0->GetX()[ip];
293 Double_t y = Eval(x,0)-graph0->GetY()[ip];
303 TH1F *his = new TH1F("hdiff","hdiff", 100, min, max);
304 for (Int_t ip=0; ip<npoints; ip++){
305 Double_t x = graph0->GetX()[ip];
306 Double_t y = Eval(x,0)-graph0->GetY()[ip];
315 void AliSplineFit::InitKnots(TGraph * graph, Int_t min, Int_t iter, Double_t maxDelta){
317 // initialize knots + estimate sigma of noise + make initial parameters
321 const Double_t kEpsilon = 1.e-7;
324 fMaxDelta = maxDelta;
325 Int_t npoints = fGraph->GetN();
326 fN0 = (npoints/fNmin)+1;
327 Float_t delta = Double_t(npoints)/Double_t(fN0-1);
329 fParams = new TClonesArray("TVectorD",fN0);
330 fCovars = new TClonesArray("TMatrixD",fN0);
331 fIndex = new Int_t[fN0];
332 TLinearFitter fitterLocal(4,"pol3"); // local fitter
336 Double_t yMin=graph->GetY()[0];
337 Double_t yMax=graph->GetY()[0];
339 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
340 Int_t index0 = TMath::Nint(Double_t(iKnot)*Double_t(delta));
341 Int_t index1 = TMath::Min(TMath::Nint(Double_t(iKnot+1)*Double_t(delta)),npoints-1);
342 Int_t indexM = (iKnot>0) ? fIndex[iKnot-1]:index0;
343 fIndex[iKnot]=TMath::Min(index0, npoints-1);
344 Float_t startX =graph->GetX()[fIndex[iKnot]];
346 for (Int_t ipoint=indexM; ipoint<index1; ipoint++){
347 Double_t dxl =graph->GetX()[ipoint]-startX;
348 Double_t y = graph->GetY()[ipoint];
351 fitterLocal.AddPoint(&dxl,y,1);
355 sigma2 += fitterLocal.GetChisquare()/Double_t((index1-indexM)-4.);
356 TMatrixD * covar = new ((*fCovars)[iKnot]) TMatrixD(4,4);
357 TVectorD * param = new ((*fParams)[iKnot]) TVectorD(4);
358 fitterLocal.GetParameters(*param);
359 fitterLocal.GetCovarianceMatrix(*covar);
360 fitterLocal.ClearPoints();
362 fSigma =TMath::Sqrt(sigma2/Double_t(fN0)); // mean sigma
363 Double_t tDiff = ((yMax-yMin)+TMath::Abs(yMax)+TMath::Abs(yMin))*kEpsilon;
364 fSigma += tDiff+fMaxDelta/TMath::Sqrt(npoints);
366 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
367 TMatrixD & cov = *((TMatrixD*)fCovars->At(iKnot));
373 for (Int_t iKnot=0; iKnot<fN0; iKnot++) if (fIndex[iKnot]>=0) fN++;
374 fX = new Double_t[fN];
375 fY0 = new Double_t[fN];
376 fY1 = new Double_t[fN];
377 fChi2I = new Double_t[fN];
379 for (Int_t i=0; i<fN0; i++){
380 if (fIndex[i]<0) continue;
382 printf("AliSplineFit::InitKnots: Knot number > Max knot number\n");
385 TVectorD * param = (TVectorD*) fParams->At(i);
386 fX[iKnot] = fGraph->GetX()[fIndex[i]];
387 fY0[iKnot] = (*param)(0);
388 fY1[iKnot] = (*param)(1);
395 Int_t AliSplineFit::OptimizeKnots(Int_t nIter){
399 const Double_t kMaxChi2= 5;
401 TTreeSRedirector cstream("SplineIter.root");
402 for (Int_t iIter=0; iIter<nIter; iIter++){
403 if (fBDump) cstream<<"Fit"<<
408 for (Int_t iKnot=1; iKnot<fN0-1; iKnot++){
409 if (fIndex[iKnot]<0) continue; //disabled knot
410 Double_t chi2 = CheckKnot(iKnot);
411 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
413 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
414 TVectorD * param = (TVectorD*)fParams->At(iKnot);
424 if (chi2>kMaxChi2) { nKnots++;continue;}
426 Int_t iPrevious=iKnot-1;
427 Int_t iNext =iKnot+1;
428 while (fIndex[iPrevious]<0) iPrevious--;
429 while (fIndex[iNext]<0) iNext++;
430 RefitKnot(iPrevious);
433 while (iKnot<fN0-1&& fIndex[iKnot]<0) iKnot++;
440 Bool_t AliSplineFit::RefitKnot(Int_t iKnot){
444 const Double_t kEpsilon = 1.e-7;
446 Int_t iPrevious=(iKnot>0) ?iKnot-1: 0;
447 Int_t iNext =(iKnot<fN0)?iKnot+1: fN0-1;
448 while (iPrevious>0&&fIndex[iPrevious]<0) iPrevious--;
449 while (iNext<fN0&&fIndex[iNext]<0) iNext++;
450 if (iPrevious<0) iPrevious=0;
451 if (iNext>=fN0) iNext=fN0-1;
453 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
454 AliSplineFit::fitterStatic()->ClearPoints();
455 Int_t indPrev = fIndex[iPrevious];
456 Int_t indNext = fIndex[iNext];
457 Double_t *graphX = fGraph->GetX();
458 Double_t *graphY = fGraph->GetY();
460 // make arrays for points to fit (to save time)
462 Int_t nPoints = indNext-indPrev;
463 Double_t *xPoint = new Double_t[3*nPoints];
464 Double_t *yPoint = &xPoint[nPoints];
465 Double_t *ePoint = &xPoint[2*nPoints];
467 for (Int_t iPoint=indPrev; iPoint<indNext; iPoint++, indVec++){
468 Double_t dxl = graphX[iPoint]-startX;
469 Double_t y = graphY[iPoint];
470 xPoint[indVec] = dxl;
472 ePoint[indVec] = fSigma;
473 // ePoint[indVec] = fSigma+TMath::Abs(y)*kEpsilon;
474 // AliSplineFit::fitterStatic.AddPoint(&dxl,y,fSigma+TMath::Abs(y)*kEpsilon);
476 AliSplineFit::fitterStatic()->AssignData(nPoints,1,xPoint,yPoint,ePoint);
477 AliSplineFit::fitterStatic()->Eval();
479 // delete temporary arrays
483 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
484 TVectorD * param = (TVectorD*)fParams->At(iKnot);
485 AliSplineFit::fitterStatic()->GetParameters(*param);
486 AliSplineFit::fitterStatic()->GetCovarianceMatrix(*covar);
491 Float_t AliSplineFit::CheckKnot(Int_t iKnot){
496 Int_t iPrevious=iKnot-1;
497 Int_t iNext =iKnot+1;
498 while (fIndex[iPrevious]<0) iPrevious--;
499 while (fIndex[iNext]<0) iNext++;
500 TVectorD &pPrevious = *((TVectorD*)fParams->At(iPrevious));
501 TVectorD &pNext = *((TVectorD*)fParams->At(iNext));
502 TVectorD &pKnot = *((TVectorD*)fParams->At(iKnot));
503 TMatrixD &cPrevious = *((TMatrixD*)fCovars->At(iPrevious));
504 TMatrixD &cNext = *((TMatrixD*)fCovars->At(iNext));
505 TMatrixD &cKnot = *((TMatrixD*)fCovars->At(iKnot));
506 Double_t xPrevious = fGraph->GetX()[fIndex[iPrevious]];
507 Double_t xNext = fGraph->GetX()[fIndex[iNext]];
508 Double_t xKnot = fGraph->GetX()[fIndex[iKnot]];
510 // extra variables introduced to save processing time
512 Double_t dxc = xNext-xPrevious;
513 Double_t invDxc = 1./dxc;
514 Double_t invDxc2 = invDxc*invDxc;
515 TMatrixD tPrevious(4,4);
518 tPrevious(0,0) = 1; tPrevious(1,1) = 1;
519 tPrevious(2,0) = -3.*invDxc2;
520 tPrevious(2,1) = -2.*invDxc;
521 tPrevious(3,0) = 2.*invDxc2*invDxc;
522 tPrevious(3,1) = 1.*invDxc2;
523 tNext(2,0) = 3.*invDxc2; tNext(2,1) = -1*invDxc;
524 tNext(3,0) = -2.*invDxc2*invDxc; tNext(3,1) = 1.*invDxc2;
525 TMatrixD tpKnot(4,4);
526 TMatrixD tpNext(4,4);
527 Double_t dx = xKnot-xPrevious;
528 tpKnot(0,0) = 1; tpKnot(1,1) = 1; tpKnot(2,2) = 1; tpKnot(3,3) = 1;
529 tpKnot(0,1) = dx; tpKnot(0,2) = dx*dx; tpKnot(0,3) = dx*dx*dx;
530 tpKnot(1,2) = 2.*dx; tpKnot(1,3) = 3.*dx*dx;
532 Double_t dxn = xNext-xPrevious;
533 tpNext(0,0) = 1; tpNext(1,1) = 1; tpNext(2,2) = 1; tpNext(3,3) = 1;
534 tpNext(0,1) = dxn; tpNext(0,2) = dxn*dxn; tpNext(0,3) = dxn*dxn*dxn;
535 tpNext(1,2) = 2.*dxn; tpNext(1,3) = 3.*dxn*dxn;
536 tpNext(2,3) = 3.*dxn;
539 // matrix and vector at previous
542 TVectorD sPrevious = tPrevious*pPrevious+tNext*pNext;
543 TVectorD sKnot = tpKnot*sPrevious;
544 TVectorD sNext = tpNext*sPrevious;
546 TMatrixD csPrevious00(tPrevious, TMatrixD::kMult,cPrevious);
547 csPrevious00 *= tPrevious.T();
548 TMatrixD csPrevious01(tNext,TMatrixD::kMult,cNext);
549 csPrevious01*=tNext.T();
550 TMatrixD csPrevious(csPrevious00,TMatrixD::kPlus,csPrevious01);
551 TMatrixD csKnot(tpKnot,TMatrixD::kMult,csPrevious);
553 TMatrixD csNext(tpNext,TMatrixD::kMult,csPrevious);
556 TVectorD dPrevious = pPrevious-sPrevious;
557 TVectorD dKnot = pKnot-sKnot;
558 TVectorD dNext = pNext-sNext;
562 prec(0,0) = (fMaxDelta*fMaxDelta);
563 prec(1,1) = prec(0,0)*invDxc2;
564 prec(2,2) = prec(1,1)*invDxc2;
565 prec(3,3) = prec(2,2)*invDxc2;
567 // prec(0,0) = (fMaxDelta*fMaxDelta);
568 // prec(1,1) = (fMaxDelta*fMaxDelta)/(dxc*dxc);
569 // prec(2,2) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc);
570 // prec(3,3) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc*dxc*dxc);
572 csPrevious+=cPrevious;
575 Double_t chi2P = dPrevious*(csPrevious*dPrevious);
580 Double_t chi2K = dKnot*(csKnot*dKnot);
585 Double_t chi2N = dNext*(csNext*dNext);
587 return (chi2P+chi2K+chi2N)/8.;
592 void AliSplineFit::SplineFit(Int_t nder){
594 // Cubic spline fit of graph
597 // nder<0 - no continuity requirement
598 // =0 - continous 0 derivative
599 // =1 - continous 1 derivative
600 // >1 - continous 2 derivative
603 TGraph * graph = fGraph;
606 Int_t npoints = graph->GetN();
610 // each knot 4 parameters
612 TMatrixD *pmatrix = 0;
613 TVectorD *pvalues = 0;
615 pmatrix = new TMatrixD(4*(nknots-1)+3*(nknots-2), 4*(nknots-1)+3*(nknots-2));
616 pvalues = new TVectorD(4*(nknots-1)+3*(nknots-2));
619 pmatrix = new TMatrixD(4*(nknots-1)+2*(nknots-2), 4*(nknots-1)+2*(nknots-2));
620 pvalues = new TVectorD(4*(nknots-1)+2*(nknots-2));
623 pmatrix = new TMatrixD(4*(nknots-1)+1*(nknots-2), 4*(nknots-1)+1*(nknots-2));
624 pvalues = new TVectorD(4*(nknots-1)+1*(nknots-2));
627 pmatrix = new TMatrixD(4*(nknots-1)+0*(nknots-2), 4*(nknots-1)+0*(nknots-2));
628 pvalues = new TVectorD(4*(nknots-1)+0*(nknots-2));
632 TMatrixD &matrix = *pmatrix;
633 TVectorD &values = *pvalues;
636 // defined extra variables (current4 etc.) to save processing time.
637 // fill normal matrices, then copy to sparse matrix.
639 Double_t *graphX = graph->GetX();
640 Double_t *graphY = graph->GetY();
641 for (Int_t ip=0;ip<npoints;ip++){
642 if (current<nknots-2&&graphX[ip]>fX[current+1]) current++;
643 Double_t xmiddle = (fX[current+1]+fX[current])*0.5;
644 Double_t x1 = graphX[ip]- xmiddle;
650 Double_t y = graphY[ip];
651 Int_t current4 = 4*current;
653 matrix(current4 , current4 )+=1;
654 matrix(current4 , current4+1)+=x1;
655 matrix(current4 , current4+2)+=x2;
656 matrix(current4 , current4+3)+=x3;
658 matrix(current4+1, current4 )+=x1;
659 matrix(current4+1, current4+1)+=x2;
660 matrix(current4+1, current4+2)+=x3;
661 matrix(current4+1, current4+3)+=x4;
663 matrix(current4+2, current4 )+=x2;
664 matrix(current4+2, current4+1)+=x3;
665 matrix(current4+2, current4+2)+=x4;
666 matrix(current4+2, current4+3)+=x5;
668 matrix(current4+3, current4 )+=x3;
669 matrix(current4+3, current4+1)+=x4;
670 matrix(current4+3, current4+2)+=x5;
671 matrix(current4+3, current4+3)+=x6;
673 values(current4 ) += y;
674 values(current4+1) += y*x1;
675 values(current4+2) += y*x2;
676 values(current4+3) += y*x3;
681 Int_t offset =4*(nknots-1)-1;
682 if (nder>=0) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
684 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
685 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
686 Double_t dxm2 = dxm*dxm;
687 Double_t dxp2 = dxp*dxp;
688 Double_t dxm3 = dxm2*dxm;
689 Double_t dxp3 = dxp2*dxp;
690 Int_t iknot4 = 4*iknot;
691 Int_t iknot41 = 4*(iknot-1);
692 Int_t offsKnot = offset+iknot;
696 // a0[i] = a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3
697 // a0[i] = a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3
698 // (a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3) -
699 // (a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3) = 0
701 matrix(offsKnot, iknot41 )=1;
702 matrix(offsKnot, iknot4 )=-1;
704 matrix(offsKnot, iknot41+1)=dxm;
705 matrix(offsKnot, iknot4 +1)=-dxp;
707 matrix(offsKnot, iknot41+2)=dxm2;
708 matrix(offsKnot, iknot4 +2)=-dxp2;
710 matrix(offsKnot, iknot41+3)=dxm3;
711 matrix(offsKnot, iknot4 +3)=-dxp3;
713 matrix(iknot41 , offsKnot)=1;
714 matrix(iknot41+1, offsKnot)=dxm;
715 matrix(iknot41+2, offsKnot)=dxm2;
716 matrix(iknot41+3, offsKnot)=dxm3;
717 matrix(iknot4 , offsKnot)=-1;
718 matrix(iknot4+1, offsKnot)=-dxp;
719 matrix(iknot4+2, offsKnot)=-dxp2;
720 matrix(iknot4+3, offsKnot)=-dxp3;
725 offset =4*(nknots-1)-1+(nknots-2);
726 if (nder>=1)for (Int_t iknot = 1; iknot<nknots-1; iknot++){
728 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
729 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
730 Double_t dxm2 = dxm*dxm;
731 Double_t dxp2 = dxp*dxp;
732 Int_t iknot4 = 4*iknot;
733 Int_t iknot41 = 4*(iknot-1);
734 Int_t offsKnot = offset+iknot;
736 // condition on knot derivation
738 // a0d[i] = a1m[i-1] + 2*a2m[i-1]*dxm + 3*a3m[i-1]*dxm^2
739 // a0d[i] = a1m[i-0] + 2*a2m[i-0]*dxp + 3*a3m[i-0]*dxp^2
742 matrix(offsKnot, iknot41+1)= 1;
743 matrix(offsKnot, iknot4 +1)=-1;
745 matrix(offsKnot, iknot41+2)= 2.*dxm;
746 matrix(offsKnot, iknot4 +2)=-2.*dxp;
748 matrix(offsKnot, iknot41+3)= 3.*dxm2;
749 matrix(offsKnot, iknot4 +3)=-3.*dxp2;
751 matrix(iknot41+1, offsKnot)=1;
752 matrix(iknot41+2, offsKnot)=2.*dxm;
753 matrix(iknot41+3, offsKnot)=3.*dxm2;
755 matrix(iknot4+1, offsKnot)=-1.;
756 matrix(iknot4+2, offsKnot)=-2.*dxp;
757 matrix(iknot4+3, offsKnot)=-3.*dxp2;
762 offset =4*(nknots-1)-1+2*(nknots-2);
763 if (nder>=2) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
765 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
766 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
767 Int_t iknot4 = 4*iknot;
768 Int_t iknot41 = 4*(iknot-1);
769 Int_t offsKnot = offset+iknot;
771 // condition on knot second derivative
773 // a0dd[i] = 2*a2m[i-1] + 6*a3m[i-1]*dxm
774 // a0dd[i] = 2*a2m[i-0] + 6*a3m[i-0]*dxp
777 matrix(offsKnot, iknot41+2)= 2.;
778 matrix(offsKnot, iknot4 +2)=-2.;
780 matrix(offsKnot, iknot41+3)= 6.*dxm;
781 matrix(offsKnot, iknot4 +3)=-6.*dxp;
783 matrix(iknot41+2, offsKnot)=2.;
784 matrix(iknot41+3, offsKnot)=6.*dxm;
786 matrix(iknot4+2, offsKnot)=-2.;
787 matrix(iknot4+3, offsKnot)=-6.*dxp;
790 // sparse matrix to do fit
792 TMatrixDSparse smatrix(matrix);
793 TDecompSparse svd(smatrix,0);
795 const TVectorD results = svd.Solve(values,ok);
797 for (Int_t iknot = 0; iknot<nknots-1; iknot++){
799 Double_t dxm = -(fX[iknot+1]-fX[iknot])*0.5;
801 fY0[iknot] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
802 results(4*iknot+3)*dxm*dxm*dxm;
804 fY1[iknot] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
805 3*results(4*iknot+3)*dxm*dxm;
807 Int_t iknot2= nknots-1;
808 Int_t iknot = nknots-2;
809 Double_t dxm = (fX[iknot2]-fX[iknot2-1])*0.5;
811 fY0[iknot2] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
812 results(4*iknot+3)*dxm*dxm*dxm;
814 fY1[iknot2] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
815 3*results(4*iknot+3)*dxm*dxm;
826 void AliSplineFit::MakeKnots0(TGraph * graph, Double_t maxdelta, Int_t minpoints){
828 // make knots - restriction max distance and minimum points
831 Int_t npoints = graph->GetN();
832 Double_t *xknots = new Double_t[npoints];
838 for (Int_t ip=0;ip<npoints;ip++){
839 if (graph->GetX()[ip]-xknots[nknots-1]>maxdelta && ipoints>minpoints){
840 xknots[nknots] = graph->GetX()[ip];
846 if (npoints-ipoints>minpoints){
847 xknots[nknots] = graph->GetX()[npoints-1];
850 xknots[nknots-1] = graph->GetX()[npoints-1];
854 fX = new Double_t[nknots];
855 fY0 = new Double_t[nknots];
856 fY1 = new Double_t[nknots];
857 fChi2I= new Double_t[nknots];
858 for (Int_t i=0; i<nknots; i++) fX[i]= xknots[i];
865 void AliSplineFit::MakeSmooth(TGraph * graph, Float_t ratio, char * type){
867 // Interface to GraphSmooth
871 Int_t npoints2 = TMath::Nint(graph->GetN()*ratio);
872 TGraph * graphT0 = smooth.SmoothKern(graph,type,ratio);
873 if (!graphT0) return;
874 TGraph graphT1(npoints2);
875 for (Int_t ipoint=0; ipoint<npoints2; ipoint++){
876 Int_t pointS = TMath::Nint(ipoint/ratio);
877 if (ipoint==npoints2-1) pointS=graph->GetN()-1;
878 graphT1.SetPoint(ipoint, graphT0->GetX()[pointS] , graphT0->GetY()[pointS]);
880 TSpline3 spline2("spline", &graphT1);
881 Update(&spline2, npoints2);
885 void AliSplineFit::Update(TSpline3 *spline, Int_t nknots){
891 fX = new Double_t[nknots];
892 fY0 = new Double_t[nknots];
893 fY1 = new Double_t[nknots];
896 for (Int_t i=0; i<nknots; i++) {
897 spline->GetCoeff(i,fX[i],fY0[i], fY1[i],d0,d1);
904 void AliSplineFit::Test(Int_t npoints, Int_t ntracks, Float_t snoise){
914 TTreeSRedirector *pcstream = new TTreeSRedirector("TestSmooth.root");
915 for (Int_t i=0; i<ntracks; i++){
916 graph0 = AliSplineFit::GenerGraph(npoints,0.05,0,0,1,0);
917 graph1 = AliSplineFit::GenerNoise(graph0,snoise);
918 fit.InitKnots(graph1, 10,10, 0.00);
919 TGraph *d0 = fit.MakeDiff(graph0);
920 TGraph *g0 = fit.MakeGraph(0,1,1000,0);
922 TH1F * h2 = fit.MakeDiffHisto(graph0);
923 TGraph *d2 = fit.MakeDiff(graph0);
924 TGraph *g2 = fit.MakeGraph(0,1,1000,0);
926 TH1F * h1 = fit.MakeDiffHisto(graph0);
927 TGraph *d1 = fit.MakeDiff(graph0);
928 TGraph *g1 = fit.MakeGraph(0,1,1000,0);
930 Float_t ratio = Float_t(fit.fN)/Float_t(npoints);
931 fitS.MakeSmooth(graph1,ratio,"box");
932 TGraph *dS = fitS.MakeDiff(graph0);
933 TGraph *gS = fit.MakeGraph(0,1,1000,0);
935 TH1F * hS = fitS.MakeDiffHisto(graph0);
936 Double_t mean2 = h2->GetMean();
937 Double_t sigma2 = h2->GetRMS();
938 Double_t mean1 = h1->GetMean();
939 Double_t sigma1 = h1->GetRMS();
940 Double_t meanS = hS->GetMean();
941 Double_t sigmaS = hS->GetRMS();
944 sprintf(fname,"pol%d",fit.fN);
946 sprintf(fname,"pol%d",19);
948 TF1 fpol("fpol",fname);
950 TGraph dpol(*graph1);
951 TGraph gpol(*graph1);
952 for (Int_t ipoint=0; ipoint<graph1->GetN(); ipoint++){
953 dpol.GetY()[ipoint]= graph0->GetY()[ipoint]-
954 fpol.Eval(graph0->GetX()[ipoint]);
955 gpol.GetY()[ipoint]= fpol.Eval(graph0->GetX()[ipoint]);
957 (*pcstream)<<"Test"<<