]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDclusterizerV1.cxx
6f8f68f09fb2a7842ca81f3b1db7ee0b91d29cfa
[u/mrichter/AliRoot.git] / TRD / AliTRDclusterizerV1.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18
19 ///////////////////////////////////////////////////////////////////////////////
20 //                                                                           //
21 // TRD cluster finder                                                        //
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <TF1.h>
26 #include <TTree.h>
27 #include <TH1.h>
28 #include <TFile.h>
29
30 #include "AliRunLoader.h"
31 #include "AliLoader.h"
32 #include "AliRawReader.h"
33 #include "AliLog.h"
34
35 #include "AliTRDclusterizerV1.h"
36 #include "AliTRDgeometry.h"
37 #include "AliTRDdataArrayF.h"
38 #include "AliTRDdataArrayI.h"
39 #include "AliTRDdigitsManager.h"
40 #include "AliTRDpadPlane.h"
41 #include "AliTRDrawData.h"
42 #include "AliTRDcalibDB.h"
43 #include "AliTRDSimParam.h"
44 #include "AliTRDRecParam.h"
45 #include "AliTRDCommonParam.h"
46 #include "AliTRDcluster.h"
47
48 ClassImp(AliTRDclusterizerV1)
49
50 //_____________________________________________________________________________
51 AliTRDclusterizerV1::AliTRDclusterizerV1()
52   :AliTRDclusterizer()
53   ,fDigitsManager(NULL)
54 {
55   //
56   // AliTRDclusterizerV1 default constructor
57   //
58
59 }
60
61 //_____________________________________________________________________________
62 AliTRDclusterizerV1::AliTRDclusterizerV1(const Text_t *name, const Text_t *title)
63   :AliTRDclusterizer(name,title)
64   ,fDigitsManager(new AliTRDdigitsManager())
65 {
66   //
67   // AliTRDclusterizerV1 constructor
68   //
69
70   fDigitsManager->CreateArrays();
71
72 }
73
74 //_____________________________________________________________________________
75 AliTRDclusterizerV1::AliTRDclusterizerV1(const AliTRDclusterizerV1 &c)
76   :AliTRDclusterizer(c)
77   ,fDigitsManager(NULL)
78 {
79   //
80   // AliTRDclusterizerV1 copy constructor
81   //
82
83 }
84
85 //_____________________________________________________________________________
86 AliTRDclusterizerV1::~AliTRDclusterizerV1()
87 {
88   //
89   // AliTRDclusterizerV1 destructor
90   //
91
92   if (fDigitsManager) {
93     delete fDigitsManager;
94     fDigitsManager = NULL;
95   }
96
97 }
98
99 //_____________________________________________________________________________
100 AliTRDclusterizerV1 &AliTRDclusterizerV1::operator=(const AliTRDclusterizerV1 &c)
101 {
102   //
103   // Assignment operator
104   //
105
106   if (this != &c) ((AliTRDclusterizerV1 &) c).Copy(*this);
107   return *this;
108
109 }
110
111 //_____________________________________________________________________________
112 void AliTRDclusterizerV1::Copy(TObject &c) const
113 {
114   //
115   // Copy function
116   //
117
118   ((AliTRDclusterizerV1 &) c).fDigitsManager = 0;
119
120   AliTRDclusterizer::Copy(c);
121
122 }
123
124 //_____________________________________________________________________________
125 Bool_t AliTRDclusterizerV1::ReadDigits()
126 {
127   //
128   // Reads the digits arrays from the input aliroot file
129   //
130
131   if (!fRunLoader) {
132     AliError("No run loader available");
133     return kFALSE;
134   }
135
136   AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
137   if (!loader->TreeD()) {
138     loader->LoadDigits();
139   }
140
141   // Read in the digit arrays
142   return (fDigitsManager->ReadDigits(loader->TreeD()));
143
144 }
145
146 //_____________________________________________________________________________
147 Bool_t AliTRDclusterizerV1::ReadDigits(AliRawReader *rawReader)
148 {
149   //
150   // Reads the digits arrays from the ddl file
151   //
152
153   AliTRDrawData raw;
154   fDigitsManager = raw.Raw2Digits(rawReader);
155
156   return kTRUE;
157
158 }
159
160 //_____________________________________________________________________________
161 Bool_t AliTRDclusterizerV1::MakeClusters()
162 {
163   //
164   // Generates the cluster.
165   //
166
167   Int_t row   = 0;
168   Int_t col   = 0;
169   Int_t time  = 0;
170   Int_t icham = 0;
171   Int_t iplan = 0;
172   Int_t isect = 0;
173   Int_t iPad  = 0;
174     
175   AliTRDdataArrayI *digitsIn;
176   AliTRDdataArrayI *tracksIn;
177
178   // Get the geometry
179   AliTRDgeometry *geo            = AliTRDgeometry::GetGeometry(fRunLoader);  
180
181   AliTRDcalibDB  *calibration    = AliTRDcalibDB::Instance();
182   if (!calibration) {
183     AliError("No AliTRDcalibDB instance available\n");
184     return kFALSE;  
185   }
186   
187   AliTRDSimParam *simParam       = AliTRDSimParam::Instance();
188   if (!simParam) {
189     AliError("No AliTRDSimParam instance available\n");
190     return kFALSE;  
191   }
192   
193   AliTRDRecParam *recParam       = AliTRDRecParam::Instance();
194   if (!recParam) {
195     AliError("No AliTRDRecParam instance available\n");
196     return kFALSE;  
197   }
198   
199   AliTRDCommonParam *commonParam = AliTRDCommonParam::Instance();
200   if (!commonParam) {
201     AliError("Could not get common parameters\n");
202     return kFALSE;
203   }
204
205   // ADC threshols
206   Float_t ADCthreshold   = simParam->GetADCthreshold();
207   // Threshold value for the maximum
208   Float_t maxThresh      = recParam->GetClusMaxThresh();
209   // Threshold value for the digit signal
210   Float_t sigThresh      = recParam->GetClusSigThresh();
211
212   // Iteration limit for unfolding procedure
213   const Float_t kEpsilon = 0.01;             
214   const Int_t   kNclus   = 3;  
215   const Int_t   kNsig    = 5;
216   const Int_t   kNdict   = AliTRDdigitsManager::kNDict;
217   const Int_t   kNtrack  = kNdict * kNclus;
218
219   Int_t    iType         = 0;
220   Int_t    iUnfold       = 0;  
221   Double_t ratioLeft     = 1.0;
222   Double_t ratioRight    = 1.0;
223
224   Int_t    iClusterROC   = 0;
225
226   Double_t padSignal[kNsig];   
227   Double_t clusterSignal[kNclus];
228   Double_t clusterPads[kNclus];   
229
230   Int_t    chamBeg    = 0;
231   Int_t    chamEnd    = AliTRDgeometry::Ncham();
232   Int_t    planBeg    = 0;
233   Int_t    planEnd    = AliTRDgeometry::Nplan();
234   Int_t    sectBeg    = 0;
235   Int_t    sectEnd    = AliTRDgeometry::Nsect();
236   Int_t    nTimeTotal = calibration->GetNumberOfTimeBins();
237
238   Int_t    dummy[9]   = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
239
240   AliDebug(1,Form("Number of Time Bins = %d.\n",nTimeTotal));
241
242   // Start clustering in every chamber
243   for (icham = chamBeg; icham < chamEnd; icham++) {
244     for (iplan = planBeg; iplan < planEnd; iplan++) {
245       for (isect = sectBeg; isect < sectEnd; isect++) {
246
247         Int_t idet = geo->GetDetector(iplan,icham,isect);
248
249         // Get the digits
250         digitsIn = fDigitsManager->GetDigits(idet);
251         // This is to take care of switched off super modules
252         if (digitsIn->GetNtime() == 0) {
253           continue;
254         }
255         digitsIn->Expand();
256         AliTRDdataArrayI *tracksTmp = fDigitsManager->GetDictionary(idet,0);
257         tracksTmp->Expand();
258
259         Int_t nRowMax = commonParam->GetRowMax(iplan,icham,isect);
260         Int_t nColMax = commonParam->GetColMax(iplan);
261
262         AliTRDpadPlane *padPlane = commonParam->GetPadPlane(iplan,icham);
263
264         Int_t nClusters      = 0;
265         Int_t nClusters2pad  = 0;
266         Int_t nClusters3pad  = 0;
267         Int_t nClusters4pad  = 0;
268         Int_t nClusters5pad  = 0;
269         Int_t nClustersLarge = 0;
270
271         // Apply the gain and the tail cancelation via digital filter
272         AliTRDdataArrayF *digitsOut = new AliTRDdataArrayF(digitsIn->GetNrow()
273                                                           ,digitsIn->GetNcol()
274                                                           ,digitsIn->GetNtime());
275         Transform(digitsIn,digitsOut,idet,nRowMax,nColMax,nTimeTotal,ADCthreshold);
276
277         // Input digits are not needed any more
278         digitsIn->Compress(1,0);
279
280         // Loop through the chamber and find the maxima 
281         for ( row = 0;  row <  nRowMax;    row++) {
282           for ( col = 2;  col <  nColMax;    col++) {
283             for (time = 0; time < nTimeTotal; time++) {
284
285               Float_t signalL = TMath::Abs(digitsOut->GetDataUnchecked(row,col  ,time));
286               Float_t signalM = TMath::Abs(digitsOut->GetDataUnchecked(row,col-1,time));
287               Float_t signalR = TMath::Abs(digitsOut->GetDataUnchecked(row,col-2,time));
288  
289               // Look for the maximum
290               if (signalM >= maxThresh) {
291                 if ((TMath::Abs(signalL) <= signalM) && 
292                     (TMath::Abs(signalR) <= signalM)) {
293                   if ((TMath::Abs(signalL) + TMath::Abs(signalR)) > sigThresh) {
294                     // Maximum found, mark the position by a negative signal
295                     digitsOut->SetDataUnchecked(row,col-1,time,-signalM);
296                   }
297                 }
298               }
299
300             }
301           }
302         }
303         tracksTmp->Compress(1,0);
304
305         // The index to the first cluster of a given ROC
306         Int_t firstClusterROC = -1;
307         // The number of cluster in a given ROC
308         Int_t nClusterROC     =  0;
309
310         // Now check the maxima and calculate the cluster position
311         for ( row = 0;  row <  nRowMax  ;  row++) {
312           for (time = 0; time < nTimeTotal; time++) {
313             for ( col = 1;  col <  nColMax-1;  col++) {
314
315               // Maximum found ?             
316               if (digitsOut->GetDataUnchecked(row,col,time) < 0.0) {
317
318                 for (iPad = 0; iPad < kNclus; iPad++) {
319                   Int_t iPadCol = col - 1 + iPad;
320                   clusterSignal[iPad] = 
321                     TMath::Abs(digitsOut->GetDataUnchecked(row,iPadCol,time));
322                 }
323
324                 // Count the number of pads in the cluster
325                 Int_t nPadCount = 0;
326                 Int_t ii;
327                 // Look to the left
328                 ii = 0;
329                 while (TMath::Abs(digitsOut->GetDataUnchecked(row,col-ii  ,time)) >= sigThresh) {
330                   nPadCount++;
331                   ii++;
332                   if (col-ii   <        0) break;
333                 }
334                 // Look to the right
335                 ii = 0;
336                 while (TMath::Abs(digitsOut->GetDataUnchecked(row,col+ii+1,time)) >= sigThresh) {
337                   nPadCount++;
338                   ii++;
339                   if (col+ii+1 >= nColMax) break;
340                 }
341                 nClusters++;
342                 switch (nPadCount) {
343                 case 2:
344                   iType = 0;
345                   nClusters2pad++;
346                   break;
347                 case 3:
348                   iType = 1;
349                   nClusters3pad++;
350                   break;
351                 case 4:
352                   iType = 2;
353                   nClusters4pad++;
354                   break;
355                 case 5:
356                   iType = 3;
357                   nClusters5pad++;
358                   break;
359                 default:
360                   iType = 4;
361                   nClustersLarge++;
362                   break;
363                 };
364
365                 // Look for 5 pad cluster with minimum in the middle
366                 Bool_t fivePadCluster = kFALSE;
367                 if (col < (nColMax - 3)) {
368                   if (digitsOut->GetDataUnchecked(row,col+2,time) < 0) {
369                     fivePadCluster = kTRUE;
370                   }
371                   if ((fivePadCluster) && (col < (nColMax - 5))) {
372                     if (digitsOut->GetDataUnchecked(row,col+4,time) >= sigThresh) {
373                       fivePadCluster = kFALSE;
374                     }
375                   }
376                   if ((fivePadCluster) && (col >             1)) {
377                     if (digitsOut->GetDataUnchecked(row,col-2,time) >= sigThresh) {
378                       fivePadCluster = kFALSE;
379                     }
380                   }
381                 }
382
383                 // 5 pad cluster
384                 // Modify the signal of the overlapping pad for the left part 
385                 // of the cluster which remains from a previous unfolding
386                 if (iUnfold) {
387                   clusterSignal[0] *= ratioLeft;
388                   iType   = 5;
389                   iUnfold = 0;
390                 }
391
392                 // Unfold the 5 pad cluster
393                 if (fivePadCluster) {
394                   for (iPad = 0; iPad < kNsig; iPad++) {
395                     padSignal[iPad] = TMath::Abs(digitsOut->GetDataUnchecked(row
396                                                                             ,col-1+iPad
397                                                                             ,time));
398                   }
399                   // Unfold the two maxima and set the signal on 
400                   // the overlapping pad to the ratio
401                   ratioRight        = Unfold(kEpsilon,iplan,padSignal);
402                   ratioLeft         = 1.0 - ratioRight; 
403                   clusterSignal[2] *= ratioRight;
404                   iType   = 5;
405                   iUnfold = 1;
406                 }
407
408                 Double_t clusterCharge = clusterSignal[0]
409                                        + clusterSignal[1]
410                                        + clusterSignal[2];
411                 
412                 // The position of the cluster
413                 clusterPads[0] =  row + 0.5;
414                 // Take the shift of the additional time bins into account
415                 clusterPads[2] = time + 0.5;
416
417                 if (recParam->LUTOn()) {
418                   // Calculate the position of the cluster by using the
419                   // lookup table method
420                   clusterPads[1] = recParam->LUTposition(iplan,clusterSignal[0]
421                                                               ,clusterSignal[1]
422                                                               ,clusterSignal[2]);
423                 }
424                 else {
425                   // Calculate the position of the cluster by using the
426                   // center of gravity method
427                   for (Int_t i = 0; i < kNsig; i++) {
428                     padSignal[i] = 0.0;
429                   }
430                   padSignal[2] = TMath::Abs(digitsOut->GetDataUnchecked(row,col  ,time)); // Central pad
431                   padSignal[1] = TMath::Abs(digitsOut->GetDataUnchecked(row,col-1,time)); // Left    pad
432                   padSignal[3] = TMath::Abs(digitsOut->GetDataUnchecked(row,col+1,time)); // Right   pad
433                   if ((col >           2) && 
434                       (TMath::Abs(digitsOut->GetDataUnchecked(row,col-2,time)) < padSignal[1])) {
435                     padSignal[0] = TMath::Abs(digitsOut->GetDataUnchecked(row,col-2,time));
436                   }
437                   if ((col < nColMax - 3) &&
438                       (TMath::Abs(digitsOut->GetDataUnchecked(row,col+2,time)) < padSignal[3])) {
439                     padSignal[4] = TMath::Abs(digitsOut->GetDataUnchecked(row,col+2,time));
440                   }               
441                   clusterPads[1] = GetCOG(padSignal);
442                 }
443
444                 Double_t q0 = clusterSignal[0];
445                 Double_t q1 = clusterSignal[1];
446                 Double_t q2 = clusterSignal[2];
447                 Double_t clusterSigmaY2 = (q1 * (q0 + q2) + 4.0 * q0 * q2)
448                                         / (clusterCharge*clusterCharge);
449
450                 //
451                 // Calculate the position and the error
452                 //              
453
454                 // Correct for t0
455                 Int_t    clusterTimeBin = TMath::Nint(time - calibration->GetT0(idet,col,row));
456                 Double_t colSize        = padPlane->GetColSize(col);
457                 Double_t rowSize        = padPlane->GetRowSize(row);
458
459                 Double_t clusterPos[3];
460                 clusterPos[0] = padPlane->GetColPos(col) - (clusterPads[1] + 0.5) * colSize;
461                 clusterPos[1] = padPlane->GetRowPos(row) - 0.5                    * rowSize;
462                 clusterPos[2] = CalcXposFromTimebin(clusterPads[2],idet,col,row);
463                 Double_t clusterSig[2];
464                 clusterSig[0] = (clusterSigmaY2 + 1.0/12.0) * colSize*colSize;
465                 clusterSig[1] = rowSize * rowSize / 12.0;                                       
466                 
467                 // Add the cluster to the output array
468                 // The track indices will be stored later 
469                 AliTRDcluster *cluster = AddCluster(clusterPos
470                                                    ,clusterTimeBin
471                                                    ,idet
472                                                    ,clusterCharge
473                                                    ,dummy
474                                                    ,clusterSig
475                                                    ,iType
476                                                    ,clusterPads[1]);
477
478                 // Store the amplitudes of the pads in the cluster for later analysis
479                 Short_t signals[7] = { 0, 0, 0, 0, 0, 0, 0 };
480                 for (Int_t jPad = col-3; jPad <= col+3; jPad++) {
481                   if ((jPad <          0) || 
482                       (jPad >= nColMax-1)) {
483                     continue;
484                   }
485                   signals[jPad-col+3] = TMath::Nint(TMath::Abs(digitsOut->GetDataUnchecked(row,jPad,time)));
486                 }
487                 cluster->SetSignals(signals);
488
489                 // Temporarily store the row, column and time bin of the center pad
490                 // Used to later on assign the track indices
491                 cluster->SetLabel( row,0);
492                 cluster->SetLabel( col,1);
493                 cluster->SetLabel(time,2);
494
495                 // Store the index of the first cluster in the current ROC
496                 if (firstClusterROC < 0) {
497                   firstClusterROC = RecPoints()->GetEntriesFast() - 1;
498                 }
499                 // Count the number of cluster in the current ROC
500                 nClusterROC++;
501
502               } // if: Maximum found ?
503
504             } // loop: pad columns
505           } // loop: time bins
506         } // loop: pad rows
507
508         delete digitsOut;
509
510         //
511         // Add the track indices to the found clusters
512         //
513
514         // Temporary array to collect the track indices
515         Int_t *idxTracks = new Int_t[kNtrack*nClusterROC];
516
517         // Loop through the dictionary arrays one-by-one
518         // to keep memory consumption low
519         for (Int_t iDict = 0; iDict < kNdict; iDict++) {
520
521           tracksIn = fDigitsManager->GetDictionary(idet,iDict);
522           tracksIn->Expand();
523
524           // Loop though the clusters found in this ROC
525           for (iClusterROC = 0; iClusterROC < nClusterROC; iClusterROC++) {
526  
527             AliTRDcluster *cluster = (AliTRDcluster *)
528                                      RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
529             row  = cluster->GetLabel(0);
530             col  = cluster->GetLabel(1);
531             time = cluster->GetLabel(2);
532
533             for (iPad = 0; iPad < kNclus; iPad++) {
534               Int_t iPadCol = col - 1 + iPad;
535               Int_t index   = tracksIn->GetDataUnchecked(row,iPadCol,time) - 1;
536               idxTracks[3*iPad+iDict + iClusterROC*kNtrack] = index;     
537             }
538
539           }
540
541           // Compress the arrays
542           tracksIn->Compress(1,0);
543
544         }
545
546         // Copy the track indices into the cluster
547         // Loop though the clusters found in this ROC
548         for (iClusterROC = 0; iClusterROC < nClusterROC; iClusterROC++) {
549  
550           AliTRDcluster *cluster = (AliTRDcluster *)
551                                    RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
552           cluster->SetLabel(-9999,0);
553           cluster->SetLabel(-9999,1);
554           cluster->SetLabel(-9999,2);
555   
556           cluster->AddTrackIndex(&idxTracks[iClusterROC*kNtrack]);
557
558         }
559
560         delete [] idxTracks;
561
562         // Write the cluster and reset the array
563         WriteClusters(idet);
564         ResetRecPoints();
565
566       } // loop: Sectors
567     } // loop: Planes
568   } // loop: Chambers
569
570   return kTRUE;
571
572 }
573
574 //_____________________________________________________________________________
575 Double_t AliTRDclusterizerV1::GetCOG(Double_t signal[5])
576 {
577   //
578   // Get COG position
579   // Used for clusters with more than 3 pads - where LUT not applicable
580   //
581
582   Double_t sum = signal[0]
583                + signal[1]
584                + signal[2] 
585                + signal[3]
586                + signal[4];
587
588   Double_t res = (0.0 * (-signal[0] + signal[4])
589                       + (-signal[1] + signal[3])) / sum;
590
591   return res;             
592
593 }
594
595 //_____________________________________________________________________________
596 Double_t AliTRDclusterizerV1::Unfold(Double_t eps, Int_t plane, Double_t *padSignal)
597 {
598   //
599   // Method to unfold neighbouring maxima.
600   // The charge ratio on the overlapping pad is calculated
601   // until there is no more change within the range given by eps.
602   // The resulting ratio is then returned to the calling method.
603   //
604
605   AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
606   if (!calibration) {
607     AliError("No AliTRDcalibDB instance available\n");
608     return kFALSE;  
609   }
610   
611   Int_t   irc                = 0;
612   Int_t   itStep             = 0;                 // Count iteration steps
613
614   Double_t ratio             = 0.5;               // Start value for ratio
615   Double_t prevRatio         = 0.0;               // Store previous ratio
616
617   Double_t newLeftSignal[3]  = { 0.0, 0.0, 0.0 }; // Array to store left cluster signal
618   Double_t newRightSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store right cluster signal
619   Double_t newSignal[3]      = { 0.0, 0.0, 0.0 };
620
621   // Start the iteration
622   while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
623
624     itStep++;
625     prevRatio = ratio;
626
627     // Cluster position according to charge ratio
628     Double_t maxLeft  = (ratio*padSignal[2] - padSignal[0]) 
629                       / (padSignal[0] + padSignal[1] + ratio*padSignal[2]);
630     Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2]) 
631                       / ((1.0 - ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
632
633     // Set cluster charge ratio
634     irc = calibration->PadResponse(1.0,maxLeft ,plane,newSignal);
635     Double_t ampLeft  = padSignal[1] / newSignal[1];
636     irc = calibration->PadResponse(1.0,maxRight,plane,newSignal);
637     Double_t ampRight = padSignal[3] / newSignal[1];
638
639     // Apply pad response to parameters
640     irc = calibration->PadResponse(ampLeft ,maxLeft ,plane,newLeftSignal );
641     irc = calibration->PadResponse(ampRight,maxRight,plane,newRightSignal);
642
643     // Calculate new overlapping ratio
644     ratio = TMath::Min((Double_t)1.0,newLeftSignal[2] / 
645                                     (newLeftSignal[2] + newRightSignal[0]));
646
647   }
648
649   return ratio;
650
651 }
652
653 //_____________________________________________________________________________
654 void AliTRDclusterizerV1::Transform(AliTRDdataArrayI *digitsIn
655                                   , AliTRDdataArrayF *digitsOut
656                                   , Int_t idet, Int_t nRowMax
657                                   , Int_t nColMax, Int_t nTimeTotal
658                                   , Float_t ADCthreshold)
659 {
660   //
661   // Apply gain factor
662   // Apply tail cancelation: Transform digitsIn to digitsOut
663   //
664
665   Int_t iRow  = 0;
666   Int_t iCol  = 0;
667   Int_t iTime = 0;
668
669   AliTRDRecParam *recParam = AliTRDRecParam::Instance();
670   if (!recParam) {
671     AliError("No AliTRDRecParam instance available\n");
672     return;
673   }
674   AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
675   if (!calibration) {
676     AliError("No AliTRDcalibDB instance available\n");
677     return;  
678   }
679
680   Double_t *inADC  = new Double_t[nTimeTotal];  // ADC data before tail cancellation
681   Double_t *outADC = new Double_t[nTimeTotal];  // ADC data after tail cancellation
682
683   AliDebug(1,Form("Tail cancellation (nExp = %d) for detector %d.\n"
684                  ,recParam->GetTCnexp(),idet));
685
686   for (iRow  = 0; iRow  <  nRowMax;   iRow++ ) {
687     for (iCol  = 0; iCol  <  nColMax;   iCol++ ) {
688
689       for (iTime = 0; iTime < nTimeTotal; iTime++) {
690
691         //
692         // Add gain
693         //
694         Double_t gain = calibration->GetGainFactor(idet,iCol,iRow);
695         if (gain == 0.0) {
696           AliError("Not a valid gain\n");
697         }
698         inADC[iTime]   = digitsIn->GetDataUnchecked(iRow,iCol,iTime);
699         inADC[iTime]  /= gain;
700         outADC[iTime]  = inADC[iTime];
701
702       }
703
704       // Apply the tail cancelation via the digital filter
705       if (recParam->TCOn()) {
706         DeConvExp(inADC,outADC,nTimeTotal,recParam->GetTCnexp());
707       }
708
709       for (iTime = 0; iTime < nTimeTotal; iTime++) {
710
711         // Store the amplitude of the digit if above threshold
712         if (outADC[iTime] > ADCthreshold) {
713           digitsOut->SetDataUnchecked(iRow,iCol,iTime,outADC[iTime]);
714         }
715
716       }
717
718     }
719   }
720
721   delete [] inADC;
722   delete [] outADC;
723
724   return;
725
726 }
727
728 //_____________________________________________________________________________
729 void AliTRDclusterizerV1::DeConvExp(Double_t *source, Double_t *target
730                                   , Int_t n, Int_t nexp) 
731 {
732   //
733   // Tail cancellation by deconvolution for PASA v4 TRF
734   //
735
736   Double_t rates[2];
737   Double_t coefficients[2];
738
739   // Initialization (coefficient = alpha, rates = lambda)
740   Double_t R1 = 1.0;
741   Double_t R2 = 1.0;
742   Double_t C1 = 0.5;
743   Double_t C2 = 0.5;
744
745   if (nexp == 1) {   // 1 Exponentials
746     R1 = 1.156;
747     R2 = 0.130;
748     C1 = 0.066;
749     C2 = 0.000;
750   }
751   if (nexp == 2) {   // 2 Exponentials
752     R1 = 1.156;
753     R2 = 0.130;
754     C1 = 0.114;
755     C2 = 0.624;
756   }
757
758   coefficients[0] = C1;
759   coefficients[1] = C2;
760
761   Double_t Dt = 0.1;
762
763   rates[0] = TMath::Exp(-Dt/(R1));
764   rates[1] = TMath::Exp(-Dt/(R2));
765   
766   Int_t i = 0;
767   Int_t k = 0;
768
769   Double_t reminder[2];
770   Double_t correction;
771   Double_t result;
772
773   // Attention: computation order is important
774   correction = 0.0;
775   for (k = 0; k < nexp; k++) {
776     reminder[k] = 0.0;
777   }
778   for (i = 0; i < n; i++) {
779     result    = (source[i] - correction);    // No rescaling
780     target[i] = result;
781
782     for (k = 0; k < nexp; k++) {
783       reminder[k] = rates[k] * (reminder[k] + coefficients[k] * result);
784     }
785     correction = 0.0;
786     for (k = 0; k < nexp; k++) {
787       correction += reminder[k];
788     }
789   }
790
791 }