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 **************************************************************************/
16 //-------------------------------------------------------------------------
17 // Implementation of the AliSplineFit class
18 // The class performs a spline fit on an incoming TGraph. The graph is
19 // divided into several parts (identified by knots between each part).
20 // Spline fits are performed on each part. According to user parameters,
21 // the function, first and second derivative are requested to be continuous
23 // Origin: Marian Ivanov, CERN, Marian.Ivanov@cern.ch
24 // Adjustments by Haavard Helstrup, Haavard.Helstrup@cern.ch
25 //-------------------------------------------------------------------------
28 #include "AliSplineFit.h"
30 ClassImp(AliSplineFit);
32 TLinearFitter AliSplineFit::fitterStatic = TLinearFitter(4,"pol3","");
34 AliSplineFit::AliSplineFit() :
51 // Default constructor
57 AliSplineFit::AliSplineFit(const AliSplineFit& source) :
59 fBDump (source.fBDump),
60 fGraph (source.fGraph),
62 fSigma (source.fSigma),
63 fMaxDelta (source.fMaxDelta),
71 fIndex = new Int_t[fN0];
72 fParams = new TClonesArray("TVectorD",fN0);
73 fCovars = new TClonesArray("TMatrixD",fN0);
74 fParams = (TClonesArray*)source.fParams->Clone();
75 fCovars = (TClonesArray*)source.fCovars->Clone();
76 for (Int_t i=0; i<fN0; i++) fIndex[i] = source.fIndex[i];
78 fX = new Double_t[fN];
79 fY0 = new Double_t[fN];
80 fY1 = new Double_t[fN];
81 fChi2I = new Double_t[fN];
82 for (Int_t i=0; i<fN; i++){
84 fY0[i] = source.fY0[i];
85 fY1[i] = source.fY1[i];
88 AliSplineFit& AliSplineFit::operator=(const AliSplineFit& source){
90 // assignment operator
92 if (&source == this) return *this;
95 // reassign memory as previous fit could have a different size
98 if ( fN0 != source.fN0) {
105 fIndex = new Int_t[fN0];
106 fParams = new TClonesArray("TVectorD",fN0);
107 fCovars = new TClonesArray("TMatrixD",fN0);
109 if ( fN != source.fN) {
116 fX = new Double_t[fN];
117 fY0 = new Double_t[fN];
118 fY1 = new Double_t[fN];
119 fChi2I = new Double_t[fN];
122 // use copy constructor (without reassigning memory) to copy values
124 new (this) AliSplineFit(source);
130 AliSplineFit::~AliSplineFit(){
132 // destructor. Don't delete fGraph, as this normally comes as input parameter
143 Double_t AliSplineFit::Eval(Double_t x, Int_t deriv) const{
145 // evaluate value at x
146 // deriv = 0: function value
147 // = 1: first derivative
148 // = 2: 2nd derivative
149 // = 3: 3rd derivative
151 // a2 = -(3*a0 -3*b0 + 2*a1*dx +b1*dx)/(dx*dx)
152 // a3 = -(-2*a0+2*b0 - a1*dx - b1*dx)/(dx*dx*dx)
154 Int_t index = TMath::BinarySearch(fN,fX,x);
155 if (index<0) index =0;
156 if (index>fN-2) index =fN-2;
158 Double_t dx = x-fX[index];
159 Double_t dxc = fX[index+1]-fX[index];
160 Double_t y0 = fY0[index];
161 Double_t y1 = fY1[index];
162 Double_t y01 = fY0[index+1];
163 Double_t y11 = fY1[index+1];
164 Double_t y2 = -(3.*y0-3.*y01+2*y1*dxc+y11*dxc)/(dxc*dxc);
165 Double_t y3 = -(-2.* y0 + 2*y01 - y1*dxc - y11*dxc) /(dxc*dxc*dxc);
166 Double_t val = y0+y1*dx+y2*dx*dx+y3*dx*dx*dx;
167 if (deriv==1) val = y1+2.*y2*dx+3.*y3*dx*dx;
168 if (deriv==2) val = 2.*y2+6.*y3*dx;
169 if (deriv==3) val = 6*y3;
174 TGraph * AliSplineFit::GenerGraph(Int_t npoints, Double_t fraction, Double_t s1, Double_t s2, Double_t s3, Int_t der){
176 // generate random graph
179 // s1, s2, s3 - sigma of derivative
182 Double_t *value = new Double_t[npoints];
183 Double_t *time = new Double_t[npoints];
184 Double_t d0=0, d1=0,d2=0,d3=0;
187 for(Int_t i=1; i<npoints; i++){
188 Double_t dtime = 1./npoints;
189 Double_t dd1 = dtime;
190 Double_t dd2 = dd1*dd1;
191 Double_t dd3 = dd2*dd1;
192 d0 += d1*dd1 + d2*dd2/2. + d3*dd3/6.;
193 d1 += d2*dd1 +d3*dd2/2;
196 time[i] = time[i-1]+dtime;
197 d1 =(1.-fraction)*d1+fraction*(gRandom->Exp(s1))*(gRandom->Rndm()-0.5);
198 d2 =(1.-fraction)*d2+fraction*(gRandom->Exp(s2))*(gRandom->Rndm()-0.5);
199 d3 =(1.-fraction)*d3+fraction*(gRandom->Exp(s3))*(gRandom->Rndm()-0.5);
200 if (gRandom->Rndm()<fraction) d3 =(1.-fraction)*d3+fraction*(gRandom->BreitWigner(0,s3));
202 Double_t dmean = (value[npoints-1]-value[0])/(time[npoints-1]-time[0]);
203 Double_t min = value[0];
204 Double_t max = value[0];
205 for (Int_t i=0; i<npoints; i++){
206 value[i] = value[i]-dmean*(time[i]-time[0]);
207 if (value[i]<min) min=value[i];
208 if (value[i]>max) max=value[i];
211 for (Int_t i=0; i<npoints; i++){
212 value[i] = (value[i]-min)/(max-min);
214 if (der==1) for (Int_t i=1; i<npoints; i++){
215 value[i-1] = (value[i]-value[i-1])/(time[i]-time[i-1]);
218 TGraph * graph = new TGraph(npoints,time,value);
226 TGraph * AliSplineFit::GenerNoise(TGraph * graph0, Double_t sigma0){
228 // add noise to graph
231 Int_t npoints=graph0->GetN();
232 Double_t *value = new Double_t[npoints];
233 Double_t *time = new Double_t[npoints];
234 for(Int_t i=0; i<npoints; i++){
235 time[i] = graph0->GetX()[i];
236 value[i] = graph0->GetY()[i]+gRandom->Gaus(0,sigma0);
238 TGraph * graph = new TGraph(npoints,time,value);
246 TGraph * AliSplineFit::MakeGraph(Double_t xmin, Double_t xmax, Int_t npoints, Int_t deriv) const {
248 // if npoints<=0 draw derivative
253 if (deriv<=0) return new TGraph(fN,fX,fY0);
254 if (deriv==1) return new TGraph(fN,fX,fY1);
255 if (deriv>2) return new TGraph(fN-1,fX,fChi2I);
257 Double_t * x = new Double_t[npoints+1];
258 Double_t * y = new Double_t[npoints+1];
259 for (Int_t ip=0; ip<=npoints; ip++){
260 x[ip] = xmin+ (xmax-xmin)*(Double_t(ip)/Double_t(npoints));
261 y[ip] = Eval(x[ip],deriv);
264 graph = new TGraph(npoints,x,y);
270 TGraph * AliSplineFit::MakeDiff(TGraph * graph0) const {
272 // Make graph of difference to reference graph
275 Int_t npoints=graph0->GetN();
277 Double_t * x = new Double_t[npoints];
278 Double_t * y = new Double_t[npoints];
279 for (Int_t ip=0; ip<npoints; ip++){
280 x[ip] = graph0->GetX()[ip];
281 y[ip] = Eval(x[ip],0)-graph0->GetY()[ip];
283 graph = new TGraph(npoints,x,y);
290 TH1F * AliSplineFit::MakeDiffHisto(TGraph * graph0) const {
292 // Make histogram of difference to reference graph
295 Int_t npoints=graph0->GetN();
296 Float_t min=1e+39,max=-1e+39;
297 for (Int_t ip=0; ip<npoints; ip++){
298 Double_t x = graph0->GetX()[ip];
299 Double_t y = Eval(x,0)-graph0->GetY()[ip];
309 TH1F *his = new TH1F("hdiff","hdiff", 100, min, max);
310 for (Int_t ip=0; ip<npoints; ip++){
311 Double_t x = graph0->GetX()[ip];
312 Double_t y = Eval(x,0)-graph0->GetY()[ip];
321 void AliSplineFit::InitKnots(TGraph * graph, Int_t min, Int_t iter, Double_t maxDelta){
323 // initialize knots + estimate sigma of noise + make initial parameters
327 const Double_t kEpsilon = 1.e-7;
330 fMaxDelta = maxDelta;
331 Int_t npoints = fGraph->GetN();
332 fN0 = (npoints/fNmin)+1;
333 Float_t delta = Double_t(npoints)/Double_t(fN0-1);
335 fParams = new TClonesArray("TVectorD",fN0);
336 fCovars = new TClonesArray("TMatrixD",fN0);
337 fIndex = new Int_t[fN0];
338 TLinearFitter fitterLocal(4,"pol3"); // local fitter
342 Double_t yMin=graph->GetY()[0];
343 Double_t yMax=graph->GetY()[0];
345 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
346 Int_t index0 = TMath::Nint(Double_t(iKnot)*Double_t(delta));
347 Int_t index1 = TMath::Min(TMath::Nint(Double_t(iKnot+1)*Double_t(delta)),npoints-1);
348 Int_t indexM = (iKnot>0) ? fIndex[iKnot-1]:index0;
349 fIndex[iKnot]=TMath::Min(index0, npoints-1);
350 Float_t startX =graph->GetX()[fIndex[iKnot]];
352 for (Int_t ipoint=indexM; ipoint<index1; ipoint++){
353 Double_t dxl =graph->GetX()[ipoint]-startX;
354 Double_t y = graph->GetY()[ipoint];
357 fitterLocal.AddPoint(&dxl,y,1);
361 sigma2 += fitterLocal.GetChisquare()/Double_t((index1-indexM)-4.);
362 TMatrixD * covar = new ((*fCovars)[iKnot]) TMatrixD(4,4);
363 TVectorD * param = new ((*fParams)[iKnot]) TVectorD(4);
364 fitterLocal.GetParameters(*param);
365 fitterLocal.GetCovarianceMatrix(*covar);
366 fitterLocal.ClearPoints();
368 fSigma =TMath::Sqrt(sigma2/Double_t(fN0)); // mean sigma
369 Double_t tDiff = ((yMax-yMin)+TMath::Abs(yMax)+TMath::Abs(yMin))*kEpsilon;
370 fSigma += tDiff+fMaxDelta/TMath::Sqrt(npoints);
372 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
373 TMatrixD & cov = *((TMatrixD*)fCovars->At(iKnot));
379 for (Int_t iKnot=0; iKnot<fN0; iKnot++) if (fIndex[iKnot]>=0) fN++;
380 fX = new Double_t[fN];
381 fY0 = new Double_t[fN];
382 fY1 = new Double_t[fN];
383 fChi2I = new Double_t[fN];
385 for (Int_t i=0; i<fN0; i++){
386 if (fIndex[i]<0) continue;
388 printf("AliSplineFit::InitKnots: Knot number > Max knot number\n");
391 TVectorD * param = (TVectorD*) fParams->At(i);
392 fX[iKnot] = fGraph->GetX()[fIndex[i]];
393 fY0[iKnot] = (*param)(0);
394 fY1[iKnot] = (*param)(1);
401 Int_t AliSplineFit::OptimizeKnots(Int_t nIter){
405 const Double_t kMaxChi2= 5;
407 TTreeSRedirector cstream("SplineIter.root");
408 for (Int_t iIter=0; iIter<nIter; iIter++){
409 if (fBDump) cstream<<"Fit"<<
414 for (Int_t iKnot=1; iKnot<fN0-1; iKnot++){
415 if (fIndex[iKnot]<0) continue; //disabled knot
416 Double_t chi2 = CheckKnot(iKnot);
417 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
419 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
420 TVectorD * param = (TVectorD*)fParams->At(iKnot);
430 if (chi2>kMaxChi2) { nKnots++;continue;}
432 Int_t iPrevious=iKnot-1;
433 Int_t iNext =iKnot+1;
434 while (fIndex[iPrevious]<0) iPrevious--;
435 while (fIndex[iNext]<0) iNext++;
436 RefitKnot(iPrevious);
439 while (iKnot<fN0-1&& fIndex[iKnot]<0) iKnot++;
446 Bool_t AliSplineFit::RefitKnot(Int_t iKnot){
451 Int_t iPrevious=(iKnot>0) ?iKnot-1: 0;
452 Int_t iNext =(iKnot<fN0)?iKnot+1: fN0-1;
453 while (iPrevious>0&&fIndex[iPrevious]<0) iPrevious--;
454 while (iNext<fN0&&fIndex[iNext]<0) iNext++;
455 if (iPrevious<0) iPrevious=0;
456 if (iNext>=fN0) iNext=fN0-1;
458 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
459 AliSplineFit::fitterStatic.ClearPoints();
460 Int_t indPrev = fIndex[iPrevious];
461 Int_t indNext = fIndex[iNext];
462 Double_t *graphX = fGraph->GetX();
463 Double_t *graphY = fGraph->GetY();
465 // make arrays for points to fit (to save time)
467 Int_t nPoints = indNext-indPrev;
468 Double_t *xPoint = new Double_t[3*nPoints];
469 Double_t *yPoint = &xPoint[nPoints];
470 Double_t *ePoint = &xPoint[2*nPoints];
472 for (Int_t iPoint=indPrev; iPoint<indNext; iPoint++, indVec++){
473 Double_t dxl = graphX[iPoint]-startX;
474 Double_t y = graphY[iPoint];
475 xPoint[indVec] = dxl;
477 ePoint[indVec] = fSigma;
479 AliSplineFit::fitterStatic.AssignData(nPoints,1,xPoint,yPoint,ePoint);
480 AliSplineFit::fitterStatic.Eval();
482 // delete temporary arrays
486 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
487 TVectorD * param = (TVectorD*)fParams->At(iKnot);
488 AliSplineFit::fitterStatic.GetParameters(*param);
489 AliSplineFit::fitterStatic.GetCovarianceMatrix(*covar);
494 Float_t AliSplineFit::CheckKnot(Int_t iKnot){
499 Int_t iPrevious=iKnot-1;
500 Int_t iNext =iKnot+1;
501 while (fIndex[iPrevious]<0) iPrevious--;
502 while (fIndex[iNext]<0) iNext++;
503 TVectorD &pPrevious = *((TVectorD*)fParams->At(iPrevious));
504 TVectorD &pNext = *((TVectorD*)fParams->At(iNext));
505 TVectorD &pKnot = *((TVectorD*)fParams->At(iKnot));
506 TMatrixD &cPrevious = *((TMatrixD*)fCovars->At(iPrevious));
507 TMatrixD &cNext = *((TMatrixD*)fCovars->At(iNext));
508 TMatrixD &cKnot = *((TMatrixD*)fCovars->At(iKnot));
509 Double_t xPrevious = fGraph->GetX()[fIndex[iPrevious]];
510 Double_t xNext = fGraph->GetX()[fIndex[iNext]];
511 Double_t xKnot = fGraph->GetX()[fIndex[iKnot]];
513 // extra variables introduced to save processing time
515 Double_t dxc = xNext-xPrevious;
516 Double_t invDxc = 1./dxc;
517 Double_t invDxc2 = invDxc*invDxc;
518 TMatrixD tPrevious(4,4);
521 tPrevious(0,0) = 1; tPrevious(1,1) = 1;
522 tPrevious(2,0) = -3.*invDxc2;
523 tPrevious(2,1) = -2.*invDxc;
524 tPrevious(3,0) = 2.*invDxc2*invDxc;
525 tPrevious(3,1) = 1.*invDxc2;
526 tNext(2,0) = 3.*invDxc2; tNext(2,1) = -1*invDxc;
527 tNext(3,0) = -2.*invDxc2*invDxc; tNext(3,1) = 1.*invDxc2;
528 TMatrixD tpKnot(4,4);
529 TMatrixD tpNext(4,4);
530 Double_t dx = xKnot-xPrevious;
531 tpKnot(0,0) = 1; tpKnot(1,1) = 1; tpKnot(2,2) = 1; tpKnot(3,3) = 1;
532 tpKnot(0,1) = dx; tpKnot(0,2) = dx*dx; tpKnot(0,3) = dx*dx*dx;
533 tpKnot(1,2) = 2.*dx; tpKnot(1,3) = 3.*dx*dx;
535 Double_t dxn = xNext-xPrevious;
536 tpNext(0,0) = 1; tpNext(1,1) = 1; tpNext(2,2) = 1; tpNext(3,3) = 1;
537 tpNext(0,1) = dxn; tpNext(0,2) = dxn*dxn; tpNext(0,3) = dxn*dxn*dxn;
538 tpNext(1,2) = 2.*dxn; tpNext(1,3) = 3.*dxn*dxn;
539 tpNext(2,3) = 3.*dxn;
542 // matrix and vector at previous
545 TVectorD sPrevious = tPrevious*pPrevious+tNext*pNext;
546 TVectorD sKnot = tpKnot*sPrevious;
547 TVectorD sNext = tpNext*sPrevious;
549 TMatrixD csPrevious00(tPrevious, TMatrixD::kMult,cPrevious);
550 csPrevious00 *= tPrevious.T();
551 TMatrixD csPrevious01(tNext,TMatrixD::kMult,cNext);
552 csPrevious01*=tNext.T();
553 TMatrixD csPrevious(csPrevious00,TMatrixD::kPlus,csPrevious01);
554 TMatrixD csKnot(tpKnot,TMatrixD::kMult,csPrevious);
556 TMatrixD csNext(tpNext,TMatrixD::kMult,csPrevious);
559 TVectorD dPrevious = pPrevious-sPrevious;
560 TVectorD dKnot = pKnot-sKnot;
561 TVectorD dNext = pNext-sNext;
565 prec(0,0) = (fMaxDelta*fMaxDelta);
566 prec(1,1) = prec(0,0)*invDxc2;
567 prec(2,2) = prec(1,1)*invDxc2;
568 prec(3,3) = prec(2,2)*invDxc2;
570 // prec(0,0) = (fMaxDelta*fMaxDelta);
571 // prec(1,1) = (fMaxDelta*fMaxDelta)/(dxc*dxc);
572 // prec(2,2) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc);
573 // prec(3,3) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc*dxc*dxc);
575 csPrevious+=cPrevious;
578 Double_t chi2P = dPrevious*(csPrevious*dPrevious);
583 Double_t chi2K = dKnot*(csKnot*dKnot);
588 Double_t chi2N = dNext*(csNext*dNext);
590 return (chi2P+chi2K+chi2N)/8.;
595 void AliSplineFit::SplineFit(Int_t nder){
597 // Cubic spline fit of graph
600 // nder<0 - no continuity requirement
601 // =0 - continous 0 derivative
602 // =1 - continous 1 derivative
603 // >1 - continous 2 derivative
606 TGraph * graph = fGraph;
609 Int_t npoints = graph->GetN();
613 // each knot 4 parameters
615 TMatrixD *pmatrix = 0;
616 TVectorD *pvalues = 0;
618 pmatrix = new TMatrixD(4*(nknots-1)+3*(nknots-2), 4*(nknots-1)+3*(nknots-2));
619 pvalues = new TVectorD(4*(nknots-1)+3*(nknots-2));
622 pmatrix = new TMatrixD(4*(nknots-1)+2*(nknots-2), 4*(nknots-1)+2*(nknots-2));
623 pvalues = new TVectorD(4*(nknots-1)+2*(nknots-2));
626 pmatrix = new TMatrixD(4*(nknots-1)+1*(nknots-2), 4*(nknots-1)+1*(nknots-2));
627 pvalues = new TVectorD(4*(nknots-1)+1*(nknots-2));
630 pmatrix = new TMatrixD(4*(nknots-1)+0*(nknots-2), 4*(nknots-1)+0*(nknots-2));
631 pvalues = new TVectorD(4*(nknots-1)+0*(nknots-2));
635 TMatrixD &matrix = *pmatrix;
636 TVectorD &values = *pvalues;
639 // defined extra variables (current4 etc.) to save processing time.
640 // fill normal matrices, then copy to sparse matrix.
642 Double_t *graphX = graph->GetX();
643 Double_t *graphY = graph->GetY();
644 for (Int_t ip=0;ip<npoints;ip++){
645 if (current<nknots-2&&graphX[ip]>fX[current+1]) current++;
646 Double_t xmiddle = (fX[current+1]+fX[current])*0.5;
647 Double_t x1 = graphX[ip]- xmiddle;
653 Double_t y = graphY[ip];
654 Int_t current4 = 4*current;
656 matrix(current4 , current4 )+=1;
657 matrix(current4 , current4+1)+=x1;
658 matrix(current4 , current4+2)+=x2;
659 matrix(current4 , current4+3)+=x3;
661 matrix(current4+1, current4 )+=x1;
662 matrix(current4+1, current4+1)+=x2;
663 matrix(current4+1, current4+2)+=x3;
664 matrix(current4+1, current4+3)+=x4;
666 matrix(current4+2, current4 )+=x2;
667 matrix(current4+2, current4+1)+=x3;
668 matrix(current4+2, current4+2)+=x4;
669 matrix(current4+2, current4+3)+=x5;
671 matrix(current4+3, current4 )+=x3;
672 matrix(current4+3, current4+1)+=x4;
673 matrix(current4+3, current4+2)+=x5;
674 matrix(current4+3, current4+3)+=x6;
676 values(current4 ) += y;
677 values(current4+1) += y*x1;
678 values(current4+2) += y*x2;
679 values(current4+3) += y*x3;
684 Int_t offset =4*(nknots-1)-1;
685 if (nder>=0) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
687 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
688 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
689 Double_t dxm2 = dxm*dxm;
690 Double_t dxp2 = dxp*dxp;
691 Double_t dxm3 = dxm2*dxm;
692 Double_t dxp3 = dxp2*dxp;
693 Int_t iknot4 = 4*iknot;
694 Int_t iknot41 = 4*(iknot-1);
695 Int_t offsKnot = offset+iknot;
699 // a0[i] = a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3
700 // a0[i] = a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3
701 // (a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3) -
702 // (a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3) = 0
704 matrix(offsKnot, iknot41 )=1;
705 matrix(offsKnot, iknot4 )=-1;
707 matrix(offsKnot, iknot41+1)=dxm;
708 matrix(offsKnot, iknot4 +1)=-dxp;
710 matrix(offsKnot, iknot41+2)=dxm2;
711 matrix(offsKnot, iknot4 +2)=-dxp2;
713 matrix(offsKnot, iknot41+3)=dxm3;
714 matrix(offsKnot, iknot4 +3)=-dxp3;
716 matrix(iknot41 , offsKnot)=1;
717 matrix(iknot41+1, offsKnot)=dxm;
718 matrix(iknot41+2, offsKnot)=dxm2;
719 matrix(iknot41+3, offsKnot)=dxm3;
720 matrix(iknot4 , offsKnot)=-1;
721 matrix(iknot4+1, offsKnot)=-dxp;
722 matrix(iknot4+2, offsKnot)=-dxp2;
723 matrix(iknot4+3, offsKnot)=-dxp3;
728 offset =4*(nknots-1)-1+(nknots-2);
729 if (nder>=1)for (Int_t iknot = 1; iknot<nknots-1; iknot++){
731 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
732 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
733 Double_t dxm2 = dxm*dxm;
734 Double_t dxp2 = dxp*dxp;
735 Int_t iknot4 = 4*iknot;
736 Int_t iknot41 = 4*(iknot-1);
737 Int_t offsKnot = offset+iknot;
739 // condition on knot derivation
741 // a0d[i] = a1m[i-1] + 2*a2m[i-1]*dxm + 3*a3m[i-1]*dxm^2
742 // a0d[i] = a1m[i-0] + 2*a2m[i-0]*dxp + 3*a3m[i-0]*dxp^2
745 matrix(offsKnot, iknot41+1)= 1;
746 matrix(offsKnot, iknot4 +1)=-1;
748 matrix(offsKnot, iknot41+2)= 2.*dxm;
749 matrix(offsKnot, iknot4 +2)=-2.*dxp;
751 matrix(offsKnot, iknot41+3)= 3.*dxm2;
752 matrix(offsKnot, iknot4 +3)=-3.*dxp2;
754 matrix(iknot41+1, offsKnot)=1;
755 matrix(iknot41+2, offsKnot)=2.*dxm;
756 matrix(iknot41+3, offsKnot)=3.*dxm2;
758 matrix(iknot4+1, offsKnot)=-1.;
759 matrix(iknot4+2, offsKnot)=-2.*dxp;
760 matrix(iknot4+3, offsKnot)=-3.*dxp2;
765 offset =4*(nknots-1)-1+2*(nknots-2);
766 if (nder>=2) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
768 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
769 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
770 Int_t iknot4 = 4*iknot;
771 Int_t iknot41 = 4*(iknot-1);
772 Int_t offsKnot = offset+iknot;
774 // condition on knot second derivative
776 // a0dd[i] = 2*a2m[i-1] + 6*a3m[i-1]*dxm
777 // a0dd[i] = 2*a2m[i-0] + 6*a3m[i-0]*dxp
780 matrix(offsKnot, iknot41+2)= 2.;
781 matrix(offsKnot, iknot4 +2)=-2.;
783 matrix(offsKnot, iknot41+3)= 6.*dxm;
784 matrix(offsKnot, iknot4 +3)=-6.*dxp;
786 matrix(iknot41+2, offsKnot)=2.;
787 matrix(iknot41+3, offsKnot)=6.*dxm;
789 matrix(iknot4+2, offsKnot)=-2.;
790 matrix(iknot4+3, offsKnot)=-6.*dxp;
793 // sparse matrix to do fit
795 TMatrixDSparse smatrix(matrix);
796 TDecompSparse svd(smatrix,0);
798 const TVectorD results = svd.Solve(values,ok);
800 for (Int_t iknot = 0; iknot<nknots-1; iknot++){
802 Double_t dxm = -(fX[iknot+1]-fX[iknot])*0.5;
804 fY0[iknot] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
805 results(4*iknot+3)*dxm*dxm*dxm;
807 fY1[iknot] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
808 3*results(4*iknot+3)*dxm*dxm;
810 Int_t iknot2= nknots-1;
811 Int_t iknot = nknots-2;
812 Double_t dxm = (fX[iknot2]-fX[iknot2-1])*0.5;
814 fY0[iknot2] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
815 results(4*iknot+3)*dxm*dxm*dxm;
817 fY1[iknot2] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
818 3*results(4*iknot+3)*dxm*dxm;
829 void AliSplineFit::MakeKnots0(TGraph * graph, Double_t maxdelta, Int_t minpoints){
831 // make knots - restriction max distance and minimum points
834 Int_t npoints = graph->GetN();
835 Double_t *xknots = new Double_t[npoints];
841 for (Int_t ip=0;ip<npoints;ip++){
842 if (graph->GetX()[ip]-xknots[nknots-1]>maxdelta && ipoints>minpoints){
843 xknots[nknots] = graph->GetX()[ip];
849 if (npoints-ipoints>minpoints){
850 xknots[nknots] = graph->GetX()[npoints-1];
853 xknots[nknots-1] = graph->GetX()[npoints-1];
857 fX = new Double_t[nknots];
858 fY0 = new Double_t[nknots];
859 fY1 = new Double_t[nknots];
860 fChi2I= new Double_t[nknots];
861 for (Int_t i=0; i<nknots; i++) fX[i]= xknots[i];
868 void AliSplineFit::MakeSmooth(TGraph * graph, Float_t ratio, char * type){
870 // Interface to GraphSmooth
874 Int_t npoints2 = TMath::Nint(graph->GetN()*ratio);
875 TGraph * graphT0 = smooth.SmoothKern(graph,type,ratio);
876 if (!graphT0) return;
877 TGraph graphT1(npoints2);
878 for (Int_t ipoint=0; ipoint<npoints2; ipoint++){
879 Int_t pointS = TMath::Nint(ipoint/ratio);
880 if (ipoint==npoints2-1) pointS=graph->GetN()-1;
881 graphT1.SetPoint(ipoint, graphT0->GetX()[pointS] , graphT0->GetY()[pointS]);
883 TSpline3 spline2("spline", &graphT1);
884 Update(&spline2, npoints2);
888 void AliSplineFit::Update(TSpline3 *spline, Int_t nknots){
894 fX = new Double_t[nknots];
895 fY0 = new Double_t[nknots];
896 fY1 = new Double_t[nknots];
899 for (Int_t i=0; i<nknots; i++) {
900 spline->GetCoeff(i,fX[i],fY0[i], fY1[i],d0,d1);
907 void AliSplineFit::Test(Int_t npoints, Int_t ntracks, Float_t snoise){
917 TTreeSRedirector *pcstream = new TTreeSRedirector("TestSmooth.root");
918 for (Int_t i=0; i<ntracks; i++){
919 graph0 = AliSplineFit::GenerGraph(npoints,0.05,0,0,1,0);
920 graph1 = AliSplineFit::GenerNoise(graph0,snoise);
921 fit.InitKnots(graph1, 10,10, 0.00);
922 TGraph *d0 = fit.MakeDiff(graph0);
923 TGraph *g0 = fit.MakeGraph(0,1,1000,0);
925 TH1F * h2 = fit.MakeDiffHisto(graph0);
926 TGraph *d2 = fit.MakeDiff(graph0);
927 TGraph *g2 = fit.MakeGraph(0,1,1000,0);
929 TH1F * h1 = fit.MakeDiffHisto(graph0);
930 TGraph *d1 = fit.MakeDiff(graph0);
931 TGraph *g1 = fit.MakeGraph(0,1,1000,0);
933 Float_t ratio = Float_t(fit.fN)/Float_t(npoints);
934 fitS.MakeSmooth(graph1,ratio,"box");
935 TGraph *dS = fitS.MakeDiff(graph0);
936 TGraph *gS = fit.MakeGraph(0,1,1000,0);
938 TH1F * hS = fitS.MakeDiffHisto(graph0);
939 Double_t mean2 = h2->GetMean();
940 Double_t sigma2 = h2->GetRMS();
941 Double_t mean1 = h1->GetMean();
942 Double_t sigma1 = h1->GetRMS();
943 Double_t meanS = hS->GetMean();
944 Double_t sigmaS = hS->GetRMS();
947 sprintf(fname,"pol%d",fit.fN);
949 sprintf(fname,"pol%d",19);
951 TF1 fpol("fpol",fname);
953 TGraph dpol(*graph1);
954 TGraph gpol(*graph1);
955 for (Int_t ipoint=0; ipoint<graph1->GetN(); ipoint++){
956 dpol.GetY()[ipoint]= graph0->GetY()[ipoint]-
957 fpol.Eval(graph0->GetX()[ipoint]);
958 gpol.GetY()[ipoint]= fpol.Eval(graph0->GetX()[ipoint]);
960 (*pcstream)<<"Test"<<