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