+AliTPCCalPad* AliTPCCalPad::LocalFit(const char* padName, Int_t rowRadius, Int_t padRadius, AliTPCCalPad* PadOutliers, Bool_t robust, Double_t chi2Threshold, Double_t robustFraction, Bool_t printCurrentSector) const {
+ //
+ // Loops over all AliTPCCalROCs and performs a localFit in each ROC
+ // AliTPCCalPad with fit-data is returned
+ // rowRadius and padRadius specifies a window around a given pad in one sector.
+ // The data of this window are fitted with a parabolic function.
+ // This function is evaluated at the pad's position.
+ // At the edges the window is shifted, so that the specified pad is not anymore in the center of the window.
+ // rowRadius - radius - rows to be used for smoothing
+ // padradius - radius - pads to be used for smoothing
+ // ROCoutlier - map of outliers - pads not to be used for local smoothing
+ // robust - robust method of fitting - (much slower)
+ // chi2Threshold: Threshold for chi2 when EvalRobust is called
+ // robustFraction: Fraction of data that will be used in EvalRobust
+ //
+ //
+ AliTPCCalPad* pad = new AliTPCCalPad(padName, padName);
+ for (Int_t isec = 0; isec < 72; isec++){
+ if (printCurrentSector) std::cout << "LocalFit in sector " << isec << "\r" << std::flush;
+ if (PadOutliers)
+ pad->SetCalROC(GetCalROC(isec)->LocalFit(rowRadius, padRadius, PadOutliers->GetCalROC(isec), robust, chi2Threshold, robustFraction));
+ else
+ pad->SetCalROC(GetCalROC(isec)->LocalFit(rowRadius, padRadius, 0, robust, chi2Threshold, robustFraction));
+ }
+ return pad;
+}
+
+
+AliTPCCalPad* AliTPCCalPad::GlobalFit(const char* padName, AliTPCCalPad* PadOutliers, Bool_t robust, Int_t fitType, Double_t chi2Threshold, Double_t robustFraction, Double_t err, TObjArray *fitParArr, TObjArray *fitCovArr){
+ //
+ // Loops over all AliTPCCalROCs and performs a globalFit in each ROC
+ // AliTPCCalPad with fit-data is returned
+ // chi2Threshold: Threshold for chi2 when EvalRobust is called
+ // robustFraction: Fraction of data that will be used in EvalRobust
+ // chi2Threshold: Threshold for chi2 when EvalRobust is called
+ // robustFraction: Fraction of data that will be used in EvalRobust
+ // err: error of the data points
+ // if fitParArr and/or fitCovArr is given, write fitParameters and/or covariance Matrices into the array
+ //
+ AliTPCCalPad* pad = new AliTPCCalPad(padName, padName);
+ TVectorD fitParam(0);
+ TMatrixD covMatrix(0,0);
+ Float_t chi2 = 0;
+ for (Int_t isec = 0; isec < 72; isec++){
+ if (PadOutliers)
+ GetCalROC(isec)->GlobalFit(PadOutliers->GetCalROC(isec), robust, fitParam, covMatrix, chi2, fitType, chi2Threshold, robustFraction, err);
+ else
+ GetCalROC(isec)->GlobalFit(0, robust, fitParam, covMatrix, chi2, fitType, chi2Threshold, robustFraction, err);
+
+ AliTPCCalROC *roc=AliTPCCalROC::CreateGlobalFitCalROC(fitParam, isec);
+ pad->SetCalROC(roc);
+ delete roc;
+ if ( fitParArr ) fitParArr->AddAtAndExpand(new TVectorD(fitParam), isec);
+ if ( fitCovArr ) fitCovArr->AddAtAndExpand(new TMatrixD(covMatrix), isec);
+ }
+ return pad;
+}
+//_____________________________________________________________________________
+TObjArray* AliTPCCalPad::CreateFormulaArray(const char *fitFormula)
+{
+ //
+ // create an array of TFormulas for the each parameter of the fit function
+ //
+
+ // split fit string in single parameters
+ // find dimension of the fit:
+ TString fitString(fitFormula);
+ fitString.ReplaceAll("++","#");
+ fitString.ReplaceAll(" ","");
+ TObjArray *arrFitParams = fitString.Tokenize("#");
+ Int_t ndim = arrFitParams->GetEntries();
+ //create array of TFormulas to evaluate the parameters
+ TObjArray *arrFitFormulas = new TObjArray(ndim);
+ arrFitFormulas->SetOwner(kTRUE);
+ for (Int_t idim=0;idim<ndim;++idim){
+ TString s=((TObjString*)arrFitParams->At(idim))->GetString();
+ s.ReplaceAll("gx","[0]");
+ s.ReplaceAll("gy","[1]");
+ s.ReplaceAll("lx","[2]");
+ s.ReplaceAll("ly","[3]");
+ s.ReplaceAll("sector","[4]");
+ arrFitFormulas->AddAt(new TFormula(Form("param%02d",idim),s.Data()),idim);
+ }
+ delete arrFitParams;
+
+ return arrFitFormulas;
+}
+//_____________________________________________________________________________
+void AliTPCCalPad::EvalFormulaArray(const TObjArray &arrFitFormulas, TVectorD &results,
+ const Int_t sec, const Int_t row, const Int_t pad)
+{
+ //
+ // evaluate the fit formulas
+ //
+ Int_t ndim=arrFitFormulas.GetEntries();
+ results.ResizeTo(ndim);
+
+ AliTPCROC* tpcROCinstance = AliTPCROC::Instance(); // to calculate the pad's position
+ Float_t localXYZ[3];
+ Float_t globalXYZ[3];
+ tpcROCinstance->GetPositionLocal(sec, row, pad, localXYZ);
+ tpcROCinstance->GetPositionGlobal(sec, row, pad, globalXYZ);
+ //calculate parameter values
+ for (Int_t idim=0;idim<ndim;++idim){
+ TFormula *f=(TFormula*)arrFitFormulas.At(idim);
+ f->SetParameters(globalXYZ[0],globalXYZ[1],localXYZ[0],localXYZ[1],sec);
+ results[idim]=f->Eval(0);
+ }
+}
+//_____________________________________________________________________________
+void AliTPCCalPad::GlobalSidesFit(const AliTPCCalPad* PadOutliers, const char* fitFormula, TVectorD &fitParamSideA, TVectorD &fitParamSideC,TMatrixD &covMatrixSideA, TMatrixD &covMatrixSideC, Float_t & chi2SideA, Float_t & chi2SideC, AliTPCCalPad *pointError, Bool_t robust, Double_t robustFraction){
+ //
+ // Performs a fit on both sides.
+ // Valid information for the fitFormula are the variables
+ // - gx, gy, lx ,ly: meaning global x, global y, local x, local y value of the padName
+ // - sector: the sector number.
+ // eg. a formula might look 'gy' or '(sector<36) ++ gy' or 'gx ++ gy' or 'gx ++ gy ++ lx ++ lx^2' and so on
+ //
+ // PadOutliers - pads with value !=0 are not used in fitting procedure
+ // chi2Threshold: Threshold for chi2 when EvalRobust is called
+ // robustFraction: Fraction of data that will be used in EvalRobust
+ //
+
+ TObjArray* arrFitFormulas=CreateFormulaArray(fitFormula);
+ Int_t ndim = arrFitFormulas->GetEntries();
+ //resize output data arrays
+ fitParamSideA.ResizeTo(ndim+1);
+ fitParamSideC.ResizeTo(ndim+1);
+ covMatrixSideA.ResizeTo(ndim+1,ndim+1);
+ covMatrixSideC.ResizeTo(ndim+1,ndim+1);
+ // create linear fitter for A- and C- Side
+ TLinearFitter* fitterGA = new TLinearFitter(ndim+1,Form("hyp%d",ndim));
+ TLinearFitter* fitterGC = new TLinearFitter(ndim+1,Form("hyp%d",ndim));
+ fitterGA->StoreData(kTRUE);
+ fitterGC->StoreData(kTRUE);
+ //parameter values
+ TVectorD parValues(ndim);
+
+ AliTPCCalROC *rocErr=0x0;
+
+ for (UInt_t isec = 0; isec<kNsec; ++isec){
+ AliTPCCalROC *rocOut=PadOutliers->GetCalROC(isec);
+ AliTPCCalROC *rocData=GetCalROC(isec);
+ if (pointError) rocErr=pointError->GetCalROC(isec);
+ if (!rocData) continue;
+ for (UInt_t irow = 0; irow < GetCalROC(isec)->GetNrows(); irow++) {
+ for (UInt_t ipad = 0; ipad < GetCalROC(isec)->GetNPads(irow); ipad++) {
+ //check for outliers
+ if (rocOut && rocOut->GetValue(irow,ipad)) continue;
+ //calculate parameter values
+ EvalFormulaArray(*arrFitFormulas,parValues,isec,irow,ipad);
+ //get value
+ Float_t value=rocData->GetValue(irow,ipad);
+ //point error
+ Int_t err=1;
+ if (rocErr) {
+ err=TMath::Nint(rocErr->GetValue(irow,ipad));
+ if (err==0) err=1;
+ }
+ //add points to the fitters
+ if (isec/18%2==0){
+ fitterGA->AddPoint(parValues.GetMatrixArray(),value,err);
+ }else{
+ fitterGC->AddPoint(parValues.GetMatrixArray(),value,err);
+ }
+ }
+ }
+ }
+ if (robust){
+ fitterGA->EvalRobust(robustFraction);
+ fitterGC->EvalRobust(robustFraction);
+ } else {
+ fitterGA->Eval();
+ fitterGC->Eval();
+ }
+ chi2SideA=fitterGA->GetChisquare()/(fitterGA->GetNpoints()-(ndim+1));
+ chi2SideC=fitterGC->GetChisquare()/(fitterGC->GetNpoints()-(ndim+1));
+ fitterGA->GetParameters(fitParamSideA);
+ fitterGC->GetParameters(fitParamSideC);
+ fitterGA->GetCovarianceMatrix(covMatrixSideA);
+ fitterGC->GetCovarianceMatrix(covMatrixSideC);
+
+ delete arrFitFormulas;
+ delete fitterGA;
+ delete fitterGC;
+
+}
+//
+AliTPCCalPad *AliTPCCalPad::CreateCalPadFit(const char* fitFormula, const TVectorD &fitParamSideA, const TVectorD &fitParamSideC)
+{
+ //
+ //
+ //
+ TObjArray *arrFitFormulas=CreateFormulaArray(fitFormula);
+ Int_t ndim = arrFitFormulas->GetEntries();
+ //check if dimension of fit formula and fit parameters agree
+ if (ndim!=fitParamSideA.GetNrows()||ndim!=fitParamSideC.GetNrows()){
+ printf("AliTPCCalPad::CreateCalPadFit: Dimensions of fit formula and fit Parameters does not match!");
+ return 0;
+ }
+ //create cal pad
+ AliTPCCalPad *pad=new AliTPCCalPad("fitResultPad",Form("Fit result: %s",fitFormula));
+ //fill cal pad with fit results if requested
+ for (UInt_t isec = 0; isec<kNsec; ++isec){
+ AliTPCCalROC *roc=pad->GetCalROC(isec);
+ for (UInt_t irow = 0; irow < roc->GetNrows(); irow++) {
+ for (UInt_t ipad = 0; ipad < roc->GetNPads(irow); ipad++) {
+ const TVectorD *fitPar=0;
+ TVectorD fitResArray;
+ if (isec/18%2==0){
+ fitPar=&fitParamSideA;
+ }else{
+ fitPar=&fitParamSideC;
+ }
+ EvalFormulaArray(*arrFitFormulas,fitResArray, isec, irow, ipad);
+ for (Int_t idim=0;idim<ndim;++idim)
+ fitResArray(idim)*=(*fitPar)(idim);
+ roc->SetValue(irow,ipad,fitResArray.Sum());
+ }
+ }
+ }
+ delete arrFitFormulas;
+ return pad;
+}