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 //-------------------------------------------------------------------------
29 #include "AliSplineFit.h"
31 ClassImp(AliSplineFit)
34 AliSplineFit::fitterStatic()
36 static TLinearFitter* fit = new TLinearFitter(4,"pol3","");
40 AliSplineFit::AliSplineFit() :
57 // Default constructor
63 AliSplineFit::AliSplineFit(const AliSplineFit& source) :
65 fBDump (source.fBDump),
66 fGraph (source.fGraph),
68 fSigma (source.fSigma),
69 fMaxDelta (source.fMaxDelta),
77 fIndex = new Int_t[fN0];
78 fParams = new TClonesArray("TVectorD",fN0);
79 fCovars = new TClonesArray("TMatrixD",fN0);
80 fParams = (TClonesArray*)source.fParams->Clone();
81 fCovars = (TClonesArray*)source.fCovars->Clone();
82 for (Int_t i=0; i<fN0; i++) fIndex[i] = source.fIndex[i];
84 fX = new Double_t[fN];
85 fY0 = new Double_t[fN];
86 fY1 = new Double_t[fN];
87 fChi2I = new Double_t[fN];
88 for (Int_t i=0; i<fN; i++){
90 fY0[i] = source.fY0[i];
91 fY1[i] = source.fY1[i];
92 fChi2I[i] = source.fChi2I[i];
95 AliSplineFit& AliSplineFit::operator=(const AliSplineFit& source){
97 // assignment operator
99 if (&source == this) return *this;
102 // reassign memory as previous fit could have a different size
105 if ( fN0 != source.fN0) {
112 fIndex = new Int_t[fN0];
113 fParams = new TClonesArray("TVectorD",fN0);
114 fCovars = new TClonesArray("TMatrixD",fN0);
116 if ( fN != source.fN) {
123 fX = new Double_t[fN];
124 fY0 = new Double_t[fN];
125 fY1 = new Double_t[fN];
126 fChi2I = new Double_t[fN];
129 // use copy constructor (without reassigning memory) to copy values
131 new (this) AliSplineFit(source);
137 AliSplineFit::~AliSplineFit(){
139 // destructor. Don't delete fGraph, as this normally comes as input parameter
150 Double_t AliSplineFit::Eval(Double_t x, Int_t deriv) const{
152 // evaluate value at x
153 // deriv = 0: function value
154 // = 1: first derivative
155 // = 2: 2nd derivative
156 // = 3: 3rd derivative
158 // a2 = -(3*a0 -3*b0 + 2*a1*dx +b1*dx)/(dx*dx)
159 // a3 = -(-2*a0+2*b0 - a1*dx - b1*dx)/(dx*dx*dx)
161 Int_t index = TMath::BinarySearch(fN,fX,x);
162 if (index<0) index =0;
163 if (index>fN-2) index =fN-2;
165 Double_t dx = x-fX[index];
166 Double_t dxc = fX[index+1]-fX[index];
167 Double_t y0 = fY0[index];
168 Double_t y1 = fY1[index];
169 Double_t y01 = fY0[index+1];
170 Double_t y11 = fY1[index+1];
171 Double_t y2 = -(3.*y0-3.*y01+2*y1*dxc+y11*dxc)/(dxc*dxc);
172 Double_t y3 = -(-2.* y0 + 2*y01 - y1*dxc - y11*dxc) /(dxc*dxc*dxc);
173 Double_t val = y0+y1*dx+y2*dx*dx+y3*dx*dx*dx;
174 if (deriv==1) val = y1+2.*y2*dx+3.*y3*dx*dx;
175 if (deriv==2) val = 2.*y2+6.*y3*dx;
176 if (deriv==3) val = 6*y3;
181 TGraph * AliSplineFit::GenerGraph(Int_t npoints, Double_t fraction, Double_t s1, Double_t s2, Double_t s3, Int_t der){
183 // generate random graph
186 // s1, s2, s3 - sigma of derivative
189 Double_t *value = new Double_t[npoints];
190 Double_t *time = new Double_t[npoints];
191 Double_t d0=0, d1=0,d2=0,d3=0;
194 for(Int_t i=1; i<npoints; i++){
195 Double_t dtime = 1./npoints;
196 Double_t dd1 = dtime;
197 Double_t dd2 = dd1*dd1;
198 Double_t dd3 = dd2*dd1;
199 d0 += d1*dd1 + d2*dd2/2. + d3*dd3/6.;
200 d1 += d2*dd1 +d3*dd2/2;
203 time[i] = time[i-1]+dtime;
204 d1 =(1.-fraction)*d1+fraction*(gRandom->Exp(s1))*(gRandom->Rndm()-0.5);
205 d2 =(1.-fraction)*d2+fraction*(gRandom->Exp(s2))*(gRandom->Rndm()-0.5);
206 d3 =(1.-fraction)*d3+fraction*(gRandom->Exp(s3))*(gRandom->Rndm()-0.5);
207 if (gRandom->Rndm()<fraction) d3 =(1.-fraction)*d3+fraction*(gRandom->BreitWigner(0,s3));
209 Double_t dmean = (value[npoints-1]-value[0])/(time[npoints-1]-time[0]);
210 Double_t min = value[0];
211 Double_t max = value[0];
212 for (Int_t i=0; i<npoints; i++){
213 value[i] = value[i]-dmean*(time[i]-time[0]);
214 if (value[i]<min) min=value[i];
215 if (value[i]>max) max=value[i];
218 for (Int_t i=0; i<npoints; i++){
219 value[i] = (value[i]-min)/(max-min);
221 if (der==1) for (Int_t i=1; i<npoints; i++){
222 value[i-1] = (value[i]-value[i-1])/(time[i]-time[i-1]);
225 TGraph * graph = new TGraph(npoints,time,value);
233 TGraph * AliSplineFit::GenerNoise(TGraph * graph0, Double_t sigma0){
235 // add noise to graph
238 Int_t npoints=graph0->GetN();
239 Double_t *value = new Double_t[npoints];
240 Double_t *time = new Double_t[npoints];
241 for(Int_t i=0; i<npoints; i++){
242 time[i] = graph0->GetX()[i];
243 value[i] = graph0->GetY()[i]+gRandom->Gaus(0,sigma0);
245 TGraph * graph = new TGraph(npoints,time,value);
253 TGraph * AliSplineFit::MakeGraph(Double_t xmin, Double_t xmax, Int_t npoints, Int_t deriv) const {
255 // if npoints<=0 draw derivative
260 if (deriv<=0) return new TGraph(fN,fX,fY0);
261 if (deriv==1) return new TGraph(fN,fX,fY1);
262 if (deriv>2) return new TGraph(fN-1,fX,fChi2I);
264 Double_t * x = new Double_t[npoints+1];
265 Double_t * y = new Double_t[npoints+1];
266 for (Int_t ip=0; ip<=npoints; ip++){
267 x[ip] = xmin+ (xmax-xmin)*(Double_t(ip)/Double_t(npoints));
268 y[ip] = Eval(x[ip],deriv);
271 graph = new TGraph(npoints,x,y);
277 TGraph * AliSplineFit::MakeDiff(TGraph * graph0) const {
279 // Make graph of difference to reference graph
282 Int_t npoints=graph0->GetN();
284 Double_t * x = new Double_t[npoints];
285 Double_t * y = new Double_t[npoints];
286 for (Int_t ip=0; ip<npoints; ip++){
287 x[ip] = graph0->GetX()[ip];
288 y[ip] = Eval(x[ip],0)-graph0->GetY()[ip];
290 graph = new TGraph(npoints,x,y);
297 TH1F * AliSplineFit::MakeDiffHisto(TGraph * graph0) const {
299 // Make histogram of difference to reference graph
302 Int_t npoints=graph0->GetN();
303 Float_t min=1e+33,max=-1e+33;
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];
316 TH1F *his = new TH1F("hdiff","hdiff", 100, min, max);
317 for (Int_t ip=0; ip<npoints; ip++){
318 Double_t x = graph0->GetX()[ip];
319 Double_t y = Eval(x,0)-graph0->GetY()[ip];
328 void AliSplineFit::InitKnots(TGraph * graph, Int_t min, Int_t iter, Double_t maxDelta){
330 // initialize knots + estimate sigma of noise + make initial parameters
334 const Double_t kEpsilon = 1.e-7;
337 fMaxDelta = maxDelta;
338 Int_t npoints = fGraph->GetN();
339 fN0 = (npoints/fNmin)+1;
340 Float_t delta = Double_t(npoints)/Double_t(fN0-1);
342 fParams = new TClonesArray("TVectorD",fN0);
343 fCovars = new TClonesArray("TMatrixD",fN0);
344 fIndex = new Int_t[fN0];
345 TLinearFitter fitterLocal(4,"pol3"); // local fitter
349 Double_t yMin=graph->GetY()[0];
350 Double_t yMax=graph->GetY()[0];
352 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
353 Int_t index0 = TMath::Nint(Double_t(iKnot)*Double_t(delta));
354 Int_t index1 = TMath::Min(TMath::Nint(Double_t(iKnot+1)*Double_t(delta)),npoints-1);
355 Int_t indexM = (iKnot>0) ? fIndex[iKnot-1]:index0;
356 fIndex[iKnot]=TMath::Min(index0, npoints-1);
357 Float_t startX =graph->GetX()[fIndex[iKnot]];
359 for (Int_t ipoint=indexM; ipoint<index1; ipoint++){
360 Double_t dxl =graph->GetX()[ipoint]-startX;
361 Double_t y = graph->GetY()[ipoint];
364 fitterLocal.AddPoint(&dxl,y,1);
368 sigma2 += fitterLocal.GetChisquare()/Double_t((index1-indexM)-4.);
369 TMatrixD * covar = new ((*fCovars)[iKnot]) TMatrixD(4,4);
370 TVectorD * param = new ((*fParams)[iKnot]) TVectorD(4);
371 fitterLocal.GetParameters(*param);
372 fitterLocal.GetCovarianceMatrix(*covar);
373 fitterLocal.ClearPoints();
375 fSigma =TMath::Sqrt(sigma2/Double_t(fN0)); // mean sigma
376 Double_t tDiff = ((yMax-yMin)+TMath::Abs(yMax)+TMath::Abs(yMin))*kEpsilon;
377 fSigma += tDiff+fMaxDelta/TMath::Sqrt(npoints);
379 for (Int_t iKnot=0; iKnot<fN0; iKnot++){
380 TMatrixD & cov = *((TMatrixD*)fCovars->At(iKnot));
386 for (Int_t iKnot=0; iKnot<fN0; iKnot++) if (fIndex[iKnot]>=0) fN++;
387 fX = new Double_t[fN];
388 fY0 = new Double_t[fN];
389 fY1 = new Double_t[fN];
390 fChi2I = new Double_t[fN];
392 for (Int_t i=0; i<fN0; i++){
393 if (fIndex[i]<0) continue;
395 printf("AliSplineFit::InitKnots: Knot number > Max knot number\n");
398 TVectorD * param = (TVectorD*) fParams->At(i);
399 fX[iKnot] = fGraph->GetX()[fIndex[i]];
400 fY0[iKnot] = (*param)(0);
401 fY1[iKnot] = (*param)(1);
408 Int_t AliSplineFit::OptimizeKnots(Int_t nIter){
412 const Double_t kMaxChi2= 5;
414 TTreeSRedirector cstream("SplineIter.root");
415 for (Int_t iIter=0; iIter<nIter; iIter++){
416 if (fBDump) cstream<<"Fit"<<
421 for (Int_t iKnot=1; iKnot<fN0-1; iKnot++){
422 if (fIndex[iKnot]<0) continue; //disabled knot
423 Double_t chi2 = CheckKnot(iKnot);
424 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
426 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
427 TVectorD * param = (TVectorD*)fParams->At(iKnot);
437 if (chi2>kMaxChi2) { nKnots++;continue;}
439 Int_t iPrevious=iKnot-1;
440 Int_t iNext =iKnot+1;
441 while (fIndex[iPrevious]<0) iPrevious--;
442 while (fIndex[iNext]<0) iNext++;
443 RefitKnot(iPrevious);
446 while (iKnot<fN0-1&& fIndex[iKnot]<0) iKnot++;
453 Bool_t AliSplineFit::RefitKnot(Int_t iKnot){
458 Int_t iPrevious=(iKnot>0) ?iKnot-1: 0;
459 Int_t iNext =(iKnot<fN0)?iKnot+1: fN0-1;
460 while (iPrevious>0&&fIndex[iPrevious]<0) iPrevious--;
461 while (iNext<fN0&&fIndex[iNext]<0) iNext++;
462 if (iPrevious<0) iPrevious=0;
463 if (iNext>=fN0) iNext=fN0-1;
465 Double_t startX = fGraph->GetX()[fIndex[iKnot]];
466 AliSplineFit::fitterStatic()->ClearPoints();
467 Int_t indPrev = fIndex[iPrevious];
468 Int_t indNext = fIndex[iNext];
469 Double_t *graphX = fGraph->GetX();
470 Double_t *graphY = fGraph->GetY();
472 // make arrays for points to fit (to save time)
474 Int_t nPoints = indNext-indPrev;
475 Double_t *xPoint = new Double_t[3*nPoints];
476 Double_t *yPoint = &xPoint[nPoints];
477 Double_t *ePoint = &xPoint[2*nPoints];
479 for (Int_t iPoint=indPrev; iPoint<indNext; iPoint++, indVec++){
480 Double_t dxl = graphX[iPoint]-startX;
481 Double_t y = graphY[iPoint];
482 xPoint[indVec] = dxl;
484 ePoint[indVec] = fSigma;
485 // ePoint[indVec] = fSigma+TMath::Abs(y)*kEpsilon;
486 // AliSplineFit::fitterStatic.AddPoint(&dxl,y,fSigma+TMath::Abs(y)*kEpsilon);
488 AliSplineFit::fitterStatic()->AssignData(nPoints,1,xPoint,yPoint,ePoint);
489 AliSplineFit::fitterStatic()->Eval();
491 // delete temporary arrays
495 TMatrixD * covar = (TMatrixD*)fCovars->At(iKnot);
496 TVectorD * param = (TVectorD*)fParams->At(iKnot);
497 AliSplineFit::fitterStatic()->GetParameters(*param);
498 AliSplineFit::fitterStatic()->GetCovarianceMatrix(*covar);
503 Float_t AliSplineFit::CheckKnot(Int_t iKnot){
508 Int_t iPrevious=iKnot-1;
509 Int_t iNext =iKnot+1;
510 while (fIndex[iPrevious]<0) iPrevious--;
511 while (fIndex[iNext]<0) iNext++;
512 TVectorD &pPrevious = *((TVectorD*)fParams->At(iPrevious));
513 TVectorD &pNext = *((TVectorD*)fParams->At(iNext));
514 TVectorD &pKnot = *((TVectorD*)fParams->At(iKnot));
515 TMatrixD &cPrevious = *((TMatrixD*)fCovars->At(iPrevious));
516 TMatrixD &cNext = *((TMatrixD*)fCovars->At(iNext));
517 TMatrixD &cKnot = *((TMatrixD*)fCovars->At(iKnot));
518 Double_t xPrevious = fGraph->GetX()[fIndex[iPrevious]];
519 Double_t xNext = fGraph->GetX()[fIndex[iNext]];
520 Double_t xKnot = fGraph->GetX()[fIndex[iKnot]];
522 // extra variables introduced to save processing time
524 Double_t dxc = xNext-xPrevious;
525 Double_t invDxc = 1./dxc;
526 Double_t invDxc2 = invDxc*invDxc;
527 TMatrixD tPrevious(4,4);
530 tPrevious(0,0) = 1; tPrevious(1,1) = 1;
531 tPrevious(2,0) = -3.*invDxc2;
532 tPrevious(2,1) = -2.*invDxc;
533 tPrevious(3,0) = 2.*invDxc2*invDxc;
534 tPrevious(3,1) = 1.*invDxc2;
535 tNext(2,0) = 3.*invDxc2; tNext(2,1) = -1*invDxc;
536 tNext(3,0) = -2.*invDxc2*invDxc; tNext(3,1) = 1.*invDxc2;
537 TMatrixD tpKnot(4,4);
538 TMatrixD tpNext(4,4);
539 Double_t dx = xKnot-xPrevious;
540 tpKnot(0,0) = 1; tpKnot(1,1) = 1; tpKnot(2,2) = 1; tpKnot(3,3) = 1;
541 tpKnot(0,1) = dx; tpKnot(0,2) = dx*dx; tpKnot(0,3) = dx*dx*dx;
542 tpKnot(1,2) = 2.*dx; tpKnot(1,3) = 3.*dx*dx;
544 Double_t dxn = xNext-xPrevious;
545 tpNext(0,0) = 1; tpNext(1,1) = 1; tpNext(2,2) = 1; tpNext(3,3) = 1;
546 tpNext(0,1) = dxn; tpNext(0,2) = dxn*dxn; tpNext(0,3) = dxn*dxn*dxn;
547 tpNext(1,2) = 2.*dxn; tpNext(1,3) = 3.*dxn*dxn;
548 tpNext(2,3) = 3.*dxn;
551 // matrix and vector at previous
554 TVectorD sPrevious = tPrevious*pPrevious+tNext*pNext;
555 TVectorD sKnot = tpKnot*sPrevious;
556 TVectorD sNext = tpNext*sPrevious;
558 TMatrixD csPrevious00(tPrevious, TMatrixD::kMult,cPrevious);
559 csPrevious00 *= tPrevious.T();
560 TMatrixD csPrevious01(tNext,TMatrixD::kMult,cNext);
561 csPrevious01*=tNext.T();
562 TMatrixD csPrevious(csPrevious00,TMatrixD::kPlus,csPrevious01);
563 TMatrixD csKnot(tpKnot,TMatrixD::kMult,csPrevious);
565 TMatrixD csNext(tpNext,TMatrixD::kMult,csPrevious);
568 TVectorD dPrevious = pPrevious-sPrevious;
569 TVectorD dKnot = pKnot-sKnot;
570 TVectorD dNext = pNext-sNext;
574 prec(0,0) = (fMaxDelta*fMaxDelta);
575 prec(1,1) = prec(0,0)*invDxc2;
576 prec(2,2) = prec(1,1)*invDxc2;
577 prec(3,3) = prec(2,2)*invDxc2;
579 // prec(0,0) = (fMaxDelta*fMaxDelta);
580 // prec(1,1) = (fMaxDelta*fMaxDelta)/(dxc*dxc);
581 // prec(2,2) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc);
582 // prec(3,3) = (fMaxDelta*fMaxDelta)/(dxc*dxc*dxc*dxc*dxc*dxc);
584 csPrevious+=cPrevious;
587 Double_t chi2P = dPrevious*(csPrevious*dPrevious);
592 Double_t chi2K = dKnot*(csKnot*dKnot);
597 Double_t chi2N = dNext*(csNext*dNext);
599 return (chi2P+chi2K+chi2N)/8.;
604 void AliSplineFit::SplineFit(Int_t nder){
606 // Cubic spline fit of graph
609 // nder<0 - no continuity requirement
610 // =0 - continous 0 derivative
611 // =1 - continous 1 derivative
612 // >1 - continous 2 derivative
615 TGraph * graph = fGraph;
618 Int_t npoints = graph->GetN();
622 // each knot 4 parameters
624 TMatrixD *pmatrix = 0;
625 TVectorD *pvalues = 0;
627 pmatrix = new TMatrixD(4*(nknots-1)+3*(nknots-2), 4*(nknots-1)+3*(nknots-2));
628 pvalues = new TVectorD(4*(nknots-1)+3*(nknots-2));
631 pmatrix = new TMatrixD(4*(nknots-1)+2*(nknots-2), 4*(nknots-1)+2*(nknots-2));
632 pvalues = new TVectorD(4*(nknots-1)+2*(nknots-2));
635 pmatrix = new TMatrixD(4*(nknots-1)+1*(nknots-2), 4*(nknots-1)+1*(nknots-2));
636 pvalues = new TVectorD(4*(nknots-1)+1*(nknots-2));
639 pmatrix = new TMatrixD(4*(nknots-1)+0*(nknots-2), 4*(nknots-1)+0*(nknots-2));
640 pvalues = new TVectorD(4*(nknots-1)+0*(nknots-2));
644 TMatrixD &matrix = *pmatrix;
645 TVectorD &values = *pvalues;
648 // defined extra variables (current4 etc.) to save processing time.
649 // fill normal matrices, then copy to sparse matrix.
651 Double_t *graphX = graph->GetX();
652 Double_t *graphY = graph->GetY();
653 for (Int_t ip=0;ip<npoints;ip++){
654 if (current<nknots-2&&graphX[ip]>fX[current+1]) current++;
655 Double_t xmiddle = (fX[current+1]+fX[current])*0.5;
656 Double_t x1 = graphX[ip]- xmiddle;
662 Double_t y = graphY[ip];
663 Int_t current4 = 4*current;
665 matrix(current4 , current4 )+=1;
666 matrix(current4 , current4+1)+=x1;
667 matrix(current4 , current4+2)+=x2;
668 matrix(current4 , current4+3)+=x3;
670 matrix(current4+1, current4 )+=x1;
671 matrix(current4+1, current4+1)+=x2;
672 matrix(current4+1, current4+2)+=x3;
673 matrix(current4+1, current4+3)+=x4;
675 matrix(current4+2, current4 )+=x2;
676 matrix(current4+2, current4+1)+=x3;
677 matrix(current4+2, current4+2)+=x4;
678 matrix(current4+2, current4+3)+=x5;
680 matrix(current4+3, current4 )+=x3;
681 matrix(current4+3, current4+1)+=x4;
682 matrix(current4+3, current4+2)+=x5;
683 matrix(current4+3, current4+3)+=x6;
685 values(current4 ) += y;
686 values(current4+1) += y*x1;
687 values(current4+2) += y*x2;
688 values(current4+3) += y*x3;
693 Int_t offset =4*(nknots-1)-1;
694 if (nder>=0) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
696 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
697 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
698 Double_t dxm2 = dxm*dxm;
699 Double_t dxp2 = dxp*dxp;
700 Double_t dxm3 = dxm2*dxm;
701 Double_t dxp3 = dxp2*dxp;
702 Int_t iknot4 = 4*iknot;
703 Int_t iknot41 = 4*(iknot-1);
704 Int_t offsKnot = offset+iknot;
708 // a0[i] = a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3
709 // a0[i] = a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3
710 // (a0m[i-1] + a1m[i-1]*dxm + a2m[i-1]*dxm^2 + a3m[i-1]*dxm^3) -
711 // (a0m[i-0] + a1m[i-0]*dxp + a2m[i-0]*dxp^2 + a3m[i-0]*dxp^3) = 0
713 matrix(offsKnot, iknot41 )=1;
714 matrix(offsKnot, iknot4 )=-1;
716 matrix(offsKnot, iknot41+1)=dxm;
717 matrix(offsKnot, iknot4 +1)=-dxp;
719 matrix(offsKnot, iknot41+2)=dxm2;
720 matrix(offsKnot, iknot4 +2)=-dxp2;
722 matrix(offsKnot, iknot41+3)=dxm3;
723 matrix(offsKnot, iknot4 +3)=-dxp3;
725 matrix(iknot41 , offsKnot)=1;
726 matrix(iknot41+1, offsKnot)=dxm;
727 matrix(iknot41+2, offsKnot)=dxm2;
728 matrix(iknot41+3, offsKnot)=dxm3;
729 matrix(iknot4 , offsKnot)=-1;
730 matrix(iknot4+1, offsKnot)=-dxp;
731 matrix(iknot4+2, offsKnot)=-dxp2;
732 matrix(iknot4+3, offsKnot)=-dxp3;
737 offset =4*(nknots-1)-1+(nknots-2);
738 if (nder>=1)for (Int_t iknot = 1; iknot<nknots-1; iknot++){
740 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
741 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
742 Double_t dxm2 = dxm*dxm;
743 Double_t dxp2 = dxp*dxp;
744 Int_t iknot4 = 4*iknot;
745 Int_t iknot41 = 4*(iknot-1);
746 Int_t offsKnot = offset+iknot;
748 // condition on knot derivation
750 // a0d[i] = a1m[i-1] + 2*a2m[i-1]*dxm + 3*a3m[i-1]*dxm^2
751 // a0d[i] = a1m[i-0] + 2*a2m[i-0]*dxp + 3*a3m[i-0]*dxp^2
754 matrix(offsKnot, iknot41+1)= 1;
755 matrix(offsKnot, iknot4 +1)=-1;
757 matrix(offsKnot, iknot41+2)= 2.*dxm;
758 matrix(offsKnot, iknot4 +2)=-2.*dxp;
760 matrix(offsKnot, iknot41+3)= 3.*dxm2;
761 matrix(offsKnot, iknot4 +3)=-3.*dxp2;
763 matrix(iknot41+1, offsKnot)=1;
764 matrix(iknot41+2, offsKnot)=2.*dxm;
765 matrix(iknot41+3, offsKnot)=3.*dxm2;
767 matrix(iknot4+1, offsKnot)=-1.;
768 matrix(iknot4+2, offsKnot)=-2.*dxp;
769 matrix(iknot4+3, offsKnot)=-3.*dxp2;
774 offset =4*(nknots-1)-1+2*(nknots-2);
775 if (nder>=2) for (Int_t iknot = 1; iknot<nknots-1; iknot++){
777 Double_t dxm = (fX[iknot]-fX[iknot-1])*0.5;
778 Double_t dxp = -(fX[iknot+1]-fX[iknot])*0.5;
779 Int_t iknot4 = 4*iknot;
780 Int_t iknot41 = 4*(iknot-1);
781 Int_t offsKnot = offset+iknot;
783 // condition on knot second derivative
785 // a0dd[i] = 2*a2m[i-1] + 6*a3m[i-1]*dxm
786 // a0dd[i] = 2*a2m[i-0] + 6*a3m[i-0]*dxp
789 matrix(offsKnot, iknot41+2)= 2.;
790 matrix(offsKnot, iknot4 +2)=-2.;
792 matrix(offsKnot, iknot41+3)= 6.*dxm;
793 matrix(offsKnot, iknot4 +3)=-6.*dxp;
795 matrix(iknot41+2, offsKnot)=2.;
796 matrix(iknot41+3, offsKnot)=6.*dxm;
798 matrix(iknot4+2, offsKnot)=-2.;
799 matrix(iknot4+3, offsKnot)=-6.*dxp;
802 // sparse matrix to do fit
804 TMatrixDSparse smatrix(matrix);
805 TDecompSparse svd(smatrix,0);
807 const TVectorD results = svd.Solve(values,ok);
809 for (Int_t iknot = 0; iknot<nknots-1; iknot++){
811 Double_t dxm = -(fX[iknot+1]-fX[iknot])*0.5;
813 fY0[iknot] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
814 results(4*iknot+3)*dxm*dxm*dxm;
816 fY1[iknot] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
817 3*results(4*iknot+3)*dxm*dxm;
819 Int_t iknot2= nknots-1;
820 Int_t iknot = nknots-2;
821 Double_t dxm = (fX[iknot2]-fX[iknot2-1])*0.5;
823 fY0[iknot2] = results(4*iknot)+ results(4*iknot+1)*dxm+results(4*iknot+2)*dxm*dxm+
824 results(4*iknot+3)*dxm*dxm*dxm;
826 fY1[iknot2] = results(4*iknot+1)+2.*results(4*iknot+2)*dxm+
827 3*results(4*iknot+3)*dxm*dxm;
838 void AliSplineFit::MakeKnots0(TGraph * graph, Double_t maxdelta, Int_t minpoints){
840 // make knots - restriction max distance and minimum points
843 Int_t npoints = graph->GetN();
844 Double_t *xknots = new Double_t[npoints];
850 for (Int_t ip=0;ip<npoints;ip++){
851 if (graph->GetX()[ip]-xknots[nknots-1]>maxdelta && ipoints>minpoints){
852 xknots[nknots] = graph->GetX()[ip];
858 if (npoints-ipoints>minpoints){
859 xknots[nknots] = graph->GetX()[npoints-1];
862 xknots[nknots-1] = graph->GetX()[npoints-1];
866 fX = new Double_t[nknots];
867 fY0 = new Double_t[nknots];
868 fY1 = new Double_t[nknots];
869 fChi2I= new Double_t[nknots];
870 for (Int_t i=0; i<nknots; i++) fX[i]= xknots[i];
877 void AliSplineFit::MakeSmooth(TGraph * graph, Float_t ratio, char * type){
879 // Interface to GraphSmooth
883 Int_t npoints2 = TMath::Nint(graph->GetN()*ratio);
884 TGraph * graphT0 = smooth.SmoothKern(graph,type,ratio);
885 if (!graphT0) return;
886 TGraph graphT1(npoints2);
887 for (Int_t ipoint=0; ipoint<npoints2; ipoint++){
888 Int_t pointS = TMath::Nint(ipoint/ratio);
889 if (ipoint==npoints2-1) pointS=graph->GetN()-1;
890 graphT1.SetPoint(ipoint, graphT0->GetX()[pointS] , graphT0->GetY()[pointS]);
892 TSpline3 spline2("spline", &graphT1);
893 Update(&spline2, npoints2);
897 void AliSplineFit::Update(TSpline3 *spline, Int_t nknots){
903 fX = new Double_t[nknots];
904 fY0 = new Double_t[nknots];
905 fY1 = new Double_t[nknots];
908 for (Int_t i=0; i<nknots; i++) {
909 spline->GetCoeff(i,fX[i],fY0[i], fY1[i],d0,d1);
916 void AliSplineFit::Test(Int_t npoints, Int_t ntracks, Float_t snoise){
926 TTreeSRedirector *pcstream = new TTreeSRedirector("TestSmooth.root");
927 for (Int_t i=0; i<ntracks; i++){
928 graph0 = AliSplineFit::GenerGraph(npoints,0.05,0,0,1,0);
929 graph1 = AliSplineFit::GenerNoise(graph0,snoise);
930 fit.InitKnots(graph1, 10,10, 0.00);
931 TGraph *d0 = fit.MakeDiff(graph0);
932 TGraph *g0 = fit.MakeGraph(0,1,1000,0);
934 TH1F * h2 = fit.MakeDiffHisto(graph0);
935 TGraph *d2 = fit.MakeDiff(graph0);
936 TGraph *g2 = fit.MakeGraph(0,1,1000,0);
938 TH1F * h1 = fit.MakeDiffHisto(graph0);
939 TGraph *d1 = fit.MakeDiff(graph0);
940 TGraph *g1 = fit.MakeGraph(0,1,1000,0);
942 Float_t ratio = Float_t(fit.fN)/Float_t(npoints);
943 fitS.MakeSmooth(graph1,ratio,"box");
944 TGraph *dS = fitS.MakeDiff(graph0);
945 TGraph *gS = fit.MakeGraph(0,1,1000,0);
947 TH1F * hS = fitS.MakeDiffHisto(graph0);
948 Double_t mean2 = h2->GetMean();
949 Double_t sigma2 = h2->GetRMS();
950 Double_t mean1 = h1->GetMean();
951 Double_t sigma1 = h1->GetRMS();
952 Double_t meanS = hS->GetMean();
953 Double_t sigmaS = hS->GetRMS();
956 sprintf(fname,"pol%d",fit.fN);
958 sprintf(fname,"pol%d",19);
960 TF1 fpol("fpol",fname);
962 TGraph dpol(*graph1);
963 TGraph gpol(*graph1);
964 for (Int_t ipoint=0; ipoint<graph1->GetN(); ipoint++){
965 dpol.GetY()[ipoint]= graph0->GetY()[ipoint]-
966 fpol.Eval(graph0->GetX()[ipoint]);
967 gpol.GetY()[ipoint]= fpol.Eval(graph0->GetX()[ipoint]);
969 (*pcstream)<<"Test"<<
1004 void AliSplineFit::Cleanup(){
1006 // deletes extra information to reduce amount of information stored on the data
1009 delete fGraph; fGraph=0;
1010 delete fParams; fParams=0;
1011 delete fCovars; fCovars=0;
1012 delete [] fIndex; fIndex=0;
1013 delete [] fChi2I; fChi2I=0;