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