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