+
+//_____________________________________________________________________________
+Bool_t AliTRDclusterizer::FivePadCluster(MaxStruct &ThisMax, MaxStruct &NeighbourMax)
+{
+ //
+ // Look for 5 pad cluster with minimum in the middle
+ // Gives back the ratio
+ //
+
+ if (ThisMax.col >= fColMax - 3) return kFALSE;
+ Float_t gain;
+ if (ThisMax.col < fColMax - 5){
+ gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col+4,ThisMax.row);
+ if (fDigits->GetData(ThisMax.row, ThisMax.col+4, ThisMax.time) - fBaseline >= fSigThresh * gain)
+ return kFALSE;
+ }
+ if (ThisMax.col > 1) {
+ gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col-2,ThisMax.row);
+ if (fDigits->GetData(ThisMax.row, ThisMax.col-2, ThisMax.time) - fBaseline >= fSigThresh * gain)
+ return kFALSE;
+ }
+
+ const Float_t kEpsilon = 0.01;
+ Double_t padSignal[5] = {ThisMax.signals[0], ThisMax.signals[1], ThisMax.signals[2],
+ NeighbourMax.signals[1], NeighbourMax.signals[2]};
+
+ // Unfold the two maxima and set the signal on
+ // the overlapping pad to the ratio
+ Float_t ratio = Unfold(kEpsilon,fLayer,padSignal);
+ ThisMax.signals[2] = (Short_t)(ThisMax.signals[2]*ratio + 0.5f);
+ NeighbourMax.signals[0] = (Short_t)(NeighbourMax.signals[0]*(1-ratio) + 0.5f);
+ ThisMax.fivePad=kTRUE;
+ NeighbourMax.fivePad=kTRUE;
+ return kTRUE;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::CreateCluster(const MaxStruct &Max)
+{
+ //
+ // Creates a cluster at the given position and saves it in fRecPoints
+ //
+
+ Int_t nPadCount = 1;
+ Short_t signals[7] = { 0, 0, Max.signals[0], Max.signals[1], Max.signals[2], 0, 0 };
+ if(!TestBit(kHLT)) CalcAdditionalInfo(Max, signals, nPadCount);
+
+ AliTRDcluster cluster(fDet, ((UChar_t) Max.col), ((UChar_t) Max.row), ((UChar_t) Max.time), signals, fVolid);
+ cluster.SetNPads(nPadCount);
+ if(TestBit(kLUT)) cluster.SetRPhiMethod(AliTRDcluster::kLUT);
+ else if(TestBit(kGAUS)) cluster.SetRPhiMethod(AliTRDcluster::kGAUS);
+ else cluster.SetRPhiMethod(AliTRDcluster::kCOG);
+
+ cluster.SetFivePad(Max.fivePad);
+ // set pads status for the cluster
+ UChar_t maskPosition = GetCorruption(Max.padStatus);
+ if (maskPosition) {
+ cluster.SetPadMaskedPosition(maskPosition);
+ cluster.SetPadMaskedStatus(GetPadStatus(Max.padStatus));
+ }
+
+ // Transform the local cluster coordinates into calibrated
+ // space point positions defined in the local tracking system.
+ // Here the calibration for T0, Vdrift and ExB is applied as well.
+ if(!fTransform->Transform(&cluster)) return;
+ // Temporarily store the Max.Row, column and time bin of the center pad
+ // Used to later on assign the track indices
+ cluster.SetLabel(Max.row, 0);
+ cluster.SetLabel(Max.col, 1);
+ cluster.SetLabel(Max.time,2);
+
+ //needed for HLT reconstruction
+ AddClusterToArray(&cluster);
+
+ // Store the index of the first cluster in the current ROC
+ if (firstClusterROC < 0) firstClusterROC = fNoOfClusters;
+
+ fNoOfClusters++;
+ fClusterROC++;
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::CalcAdditionalInfo(const MaxStruct &Max, Short_t *const signals, Int_t &nPadCount)
+{
+ // Look to the right
+ Int_t ii = 1;
+ while (fDigits->GetData(Max.row, Max.col-ii, Max.time) >= fSigThresh) {
+ nPadCount++;
+ ii++;
+ if (Max.col < ii) break;
+ }
+ // Look to the left
+ ii = 1;
+ while (fDigits->GetData(Max.row, Max.col+ii, Max.time) >= fSigThresh) {
+ nPadCount++;
+ ii++;
+ if (Max.col+ii >= fColMax) break;
+ }
+
+ // Store the amplitudes of the pads in the cluster for later analysis
+ // and check whether one of these pads is masked in the database
+ signals[2]=Max.signals[0];
+ signals[3]=Max.signals[1];
+ signals[4]=Max.signals[2];
+ Float_t gain;
+ for(Int_t i = 0; i<2; i++)
+ {
+ if(Max.col+i >= 3){
+ gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col-3+i,Max.row);
+ signals[i] = (Short_t)((fDigits->GetData(Max.row, Max.col-3+i, Max.time) - fBaseline) / gain + 0.5f);
+ }
+ if(Max.col+3-i < fColMax){
+ gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col+3-i,Max.row);
+ signals[6-i] = (Short_t)((fDigits->GetData(Max.row, Max.col+3-i, Max.time) - fBaseline) / gain + 0.5f);
+ }
+ }
+ /*for (Int_t jPad = Max.Col-3; jPad <= Max.Col+3; jPad++) {
+ if ((jPad >= 0) && (jPad < fColMax))
+ signals[jPad-Max.Col+3] = TMath::Nint(fDigits->GetData(Max.Row,jPad,Max.Time));
+ }*/
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::AddClusterToArray(AliTRDcluster* cluster)
+{
+ //
+ // Add a cluster to the array
+ //
+
+ Int_t n = RecPoints()->GetEntriesFast();
+ if(n!=fNoOfClusters)AliError(Form("fNoOfClusters != RecPoints()->GetEntriesFast %i != %i \n", fNoOfClusters, n));
+ new((*RecPoints())[n]) AliTRDcluster(*cluster);
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::AddTrackletsToArray()
+{
+ //
+ // Add the online tracklets of this chamber to the array
+ //
+
+ UInt_t* trackletword;
+ for(Int_t side=0; side<2; side++)
+ {
+ Int_t trkl=0;
+ trackletword=fTrackletContainer[side];
+ while(trackletword[trkl]>0){
+ Int_t n = TrackletsArray()->GetEntriesFast();
+ AliTRDtrackletWord tmp(trackletword[trkl]);
+ new((*TrackletsArray())[n]) AliTRDcluster(&tmp,fDet,fVolid);
+ trkl++;
+ }
+ }
+}
+
+//_____________________________________________________________________________
+Bool_t AliTRDclusterizer::AddLabels()
+{
+ //
+ // Add the track indices to the found clusters
+ //
+
+ const Int_t kNclus = 3;
+ const Int_t kNdict = AliTRDdigitsManager::kNDict;
+ const Int_t kNtrack = kNdict * kNclus;
+
+ Int_t iClusterROC = 0;
+
+ Int_t row = 0;
+ Int_t col = 0;
+ Int_t time = 0;
+ Int_t iPad = 0;
+
+ // Temporary array to collect the track indices
+ Int_t *idxTracks = new Int_t[kNtrack*fClusterROC];
+
+ // Loop through the dictionary arrays one-by-one
+ // to keep memory consumption low
+ AliTRDarrayDictionary *tracksIn = 0; //mod
+ for (Int_t iDict = 0; iDict < kNdict; iDict++) {
+
+ // tracksIn should be expanded beforehand!
+ tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(fDet,iDict);
+
+ // Loop though the clusters found in this ROC
+ for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
+
+ AliTRDcluster *cluster = (AliTRDcluster *)
+ RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
+ row = cluster->GetLabel(0);
+ col = cluster->GetLabel(1);
+ time = cluster->GetLabel(2);
+
+ for (iPad = 0; iPad < kNclus; iPad++) {
+ Int_t iPadCol = col - 1 + iPad;
+ Int_t index = tracksIn->GetData(row,iPadCol,time); //Modification of -1 in Track
+ idxTracks[3*iPad+iDict + iClusterROC*kNtrack] = index;
+ }
+
+ }
+
+ }
+
+ // Copy the track indices into the cluster
+ // Loop though the clusters found in this ROC
+ for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
+
+ AliTRDcluster *cluster = (AliTRDcluster *)
+ RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
+ cluster->SetLabel(-9999,0);
+ cluster->SetLabel(-9999,1);
+ cluster->SetLabel(-9999,2);
+
+ cluster->AddTrackIndex(&idxTracks[iClusterROC*kNtrack]);
+
+ }
+
+ delete [] idxTracks;
+
+ return kTRUE;
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDclusterizer::Unfold(Double_t eps, Int_t layer, const Double_t *const padSignal) const
+{
+ //
+ // Method to unfold neighbouring maxima.
+ // The charge ratio on the overlapping pad is calculated
+ // until there is no more change within the range given by eps.
+ // The resulting ratio is then returned to the calling method.
+ //
+
+ AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
+ if (!calibration) {
+ AliError("No AliTRDcalibDB instance available\n");
+ return kFALSE;
+ }
+
+ Int_t irc = 0;
+ Int_t itStep = 0; // Count iteration steps
+
+ Double_t ratio = 0.5; // Start value for ratio
+ Double_t prevRatio = 0.0; // Store previous ratio
+
+ Double_t newLeftSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store left cluster signal
+ Double_t newRightSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store right cluster signal
+ Double_t newSignal[3] = { 0.0, 0.0, 0.0 };
+
+ // Start the iteration
+ while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
+
+ itStep++;
+ prevRatio = ratio;
+
+ // Cluster position according to charge ratio
+ Double_t maxLeft = (ratio*padSignal[2] - padSignal[0])
+ / (padSignal[0] + padSignal[1] + ratio * padSignal[2]);
+ Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2])
+ / ((1.0 - ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
+
+ // Set cluster charge ratio
+ irc = calibration->PadResponse(1.0, maxLeft, layer, newSignal);
+ Double_t ampLeft = padSignal[1] / newSignal[1];
+ irc = calibration->PadResponse(1.0, maxRight, layer, newSignal);
+ Double_t ampRight = padSignal[3] / newSignal[1];
+
+ // Apply pad response to parameters
+ irc = calibration->PadResponse(ampLeft ,maxLeft ,layer,newLeftSignal );
+ irc = calibration->PadResponse(ampRight,maxRight,layer,newRightSignal);
+
+ // Calculate new overlapping ratio
+ ratio = TMath::Min((Double_t) 1.0
+ ,newLeftSignal[2] / (newLeftSignal[2] + newRightSignal[0]));
+
+ }
+
+ return ratio;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::TailCancelation()
+{
+ //
+ // Applies the tail cancelation and gain factors:
+ // Transform fDigits to fDigits
+ //
+
+ Int_t iRow = 0;
+ Int_t iCol = 0;
+ Int_t iTime = 0;
+
+ Float_t *arr = new Float_t[fTimeTotal]; // temp array containing the ADC signals
+
+ TTreeSRedirector *fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
+ Bool_t debugStreaming = fReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 7 && fReconstructor->IsDebugStreaming();
+ Int_t nexp = fReconstructor->GetRecoParam()->GetTCnexp();
+ while(fIndexes->NextRCIndex(iRow, iCol))
+ {
+ // if corrupted then don't make the tail cancallation
+ if (fCalPadStatusROC->GetStatus(iCol, iRow)) continue;
+
+ // Save data into the temporary processing array and substract the baseline,
+ // since DeConvExp does not expect a baseline
+ for (iTime = 0; iTime < fTimeTotal; iTime++)
+ arr[iTime] = fDigits->GetData(iRow,iCol,iTime)-fBaseline;
+
+ if(debugStreaming){
+ for (iTime = 0; iTime < fTimeTotal; iTime++)
+ (*fDebugStream) << "TailCancellation"
+ << "col=" << iCol
+ << "row=" << iRow
+ << "time=" << iTime
+ << "arr=" << arr[iTime]
+ << "\n";
+ }
+
+ // Apply the tail cancelation via the digital filter
+ DeConvExp(arr,fTimeTotal,nexp);
+
+ // Save tailcancalled data and add the baseline
+ for(iTime = 0; iTime < fTimeTotal; iTime++)
+ fDigits->SetData(iRow,iCol,iTime,(Short_t)(arr[iTime] + fBaseline + 0.5f));
+
+ } // while irow icol
+
+ delete [] arr;
+
+ return;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::DeConvExp(Float_t *const arr, const Int_t n, const Int_t nexp)
+{
+ //
+ // Tail cancellation by deconvolution for PASA v4 TRF
+ //
+
+ Float_t rates[2];
+ Float_t coefficients[2];
+
+ // Initialization (coefficient = alpha, rates = lambda)
+ Float_t r1 = 1.0;
+ Float_t r2 = 1.0;
+ Float_t c1 = 0.5;
+ Float_t c2 = 0.5;
+
+ if (nexp == 1) { // 1 Exponentials
+ r1 = 1.156;
+ r2 = 0.130;
+ c1 = 0.066;
+ c2 = 0.000;
+ }
+ if (nexp == 2) { // 2 Exponentials
+ Double_t par[4];
+ fReconstructor->GetRecoParam()->GetTCParams(par);
+ r1 = par[0];//1.156;
+ r2 = par[1];//0.130;
+ c1 = par[2];//0.114;
+ c2 = par[3];//0.624;
+ }
+
+ coefficients[0] = c1;
+ coefficients[1] = c2;
+
+ Float_t dt = 0.1;
+
+ rates[0] = TMath::Exp(-dt/(r1));
+ rates[1] = TMath::Exp(-dt/(r2));
+
+ Int_t i = 0;
+ Int_t k = 0;
+
+ Float_t reminder[2];
+ Float_t correction = 0.0;
+ Float_t result = 0.0;
+
+ // Attention: computation order is important
+ for (k = 0; k < nexp; k++) {
+ reminder[k] = 0.0;
+ }
+
+ for (i = 0; i < n; i++) {
+
+ result = (arr[i] - correction); // No rescaling
+ arr[i] = result;
+
+ for (k = 0; k < nexp; k++) {
+ reminder[k] = rates[k] * (reminder[k] + coefficients[k] * result);
+ }
+
+ correction = 0.0;
+ for (k = 0; k < nexp; k++) {
+ correction += reminder[k];
+ }
+
+ }
+
+}
+
+//_____________________________________________________________________________
+void AliTRDclusterizer::ResetRecPoints()
+{
+ //
+ // Resets the list of rec points
+ //
+
+ if (fRecPoints) {
+ fRecPoints->Delete();
+ delete fRecPoints;
+ }
+}
+
+//_____________________________________________________________________________
+TClonesArray *AliTRDclusterizer::RecPoints()
+{
+ //
+ // Returns the list of rec points
+ //
+
+ if (!fRecPoints) {
+ if(!(fRecPoints = AliTRDReconstructor::GetClusters())){
+ // determine number of clusters which has to be allocated
+ Float_t nclusters = fReconstructor->GetRecoParam()->GetNClusters();
+
+ fRecPoints = new TClonesArray("AliTRDcluster", Int_t(nclusters));
+ }
+ //SetClustersOwner(kTRUE);
+ AliTRDReconstructor::SetClusters(0x0);
+ }
+ return fRecPoints;
+
+}
+
+//_____________________________________________________________________________
+TClonesArray *AliTRDclusterizer::TrackletsArray()
+{
+ //
+ // Returns the list of rec points
+ //
+
+ if (!fTracklets && fReconstructor->IsProcessingTracklets()) {
+ fTracklets = new TClonesArray("AliTRDcluster", 2*MAXTRACKLETSPERHC);
+ //SetClustersOwner(kTRUE);
+ //AliTRDReconstructor::SetTracklets(0x0);
+ }
+ return fTracklets;
+
+}
+