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