fbb10a0f3c8e80876945c6c25a174b3f9cb3bdf6
[u/mrichter/AliRoot.git] / TRD / AliTRDclusterizerV1.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // TRD cluster finder for the slow simulator. 
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <TF1.h>
25 #include <TTree.h>
26 #include <TH1.h>
27 #include <TFile.h>
28
29 #include "AliRun.h"
30 #include "AliRunLoader.h"
31 #include "AliLoader.h"
32
33 #include "AliTRDclusterizerV1.h"
34 #include "AliTRDmatrix.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDdataArrayF.h"
37 #include "AliTRDdataArrayI.h"
38 #include "AliTRDdigitsManager.h"
39 #include "AliTRDparameter.h"
40 #include "AliTRDpadPlane.h"
41
42 ClassImp(AliTRDclusterizerV1)
43
44 //_____________________________________________________________________________
45 AliTRDclusterizerV1::AliTRDclusterizerV1():AliTRDclusterizer()
46 {
47   //
48   // AliTRDclusterizerV1 default constructor
49   //
50
51   fDigitsManager = 0;
52
53 }
54
55 //_____________________________________________________________________________
56 AliTRDclusterizerV1::AliTRDclusterizerV1(const Text_t* name, const Text_t* title)
57                     :AliTRDclusterizer(name,title)
58 {
59   //
60   // AliTRDclusterizerV1 default constructor
61   //
62
63   fDigitsManager = new AliTRDdigitsManager();
64   fDigitsManager->CreateArrays();
65
66 }
67
68 //_____________________________________________________________________________
69 AliTRDclusterizerV1::AliTRDclusterizerV1(const AliTRDclusterizerV1 &c)
70 :AliTRDclusterizer(c)
71 {
72   //
73   // AliTRDclusterizerV1 copy constructor
74   //
75
76   ((AliTRDclusterizerV1 &) c).Copy(*this);
77
78 }
79
80 //_____________________________________________________________________________
81 AliTRDclusterizerV1::~AliTRDclusterizerV1()
82 {
83   //
84   // AliTRDclusterizerV1 destructor
85   //
86
87   if (fDigitsManager) {
88     delete fDigitsManager;
89     fDigitsManager = NULL;
90   }
91
92 }
93
94 //_____________________________________________________________________________
95 AliTRDclusterizerV1 &AliTRDclusterizerV1::operator=(const AliTRDclusterizerV1 &c)
96 {
97   //
98   // Assignment operator
99   //
100
101   if (this != &c) ((AliTRDclusterizerV1 &) c).Copy(*this);
102   return *this;
103
104 }
105
106 //_____________________________________________________________________________
107 void AliTRDclusterizerV1::Copy(TObject &c) const
108 {
109   //
110   // Copy function
111   //
112
113   ((AliTRDclusterizerV1 &) c).fDigitsManager = 0;
114
115   AliTRDclusterizer::Copy(c);
116
117 }
118
119 //_____________________________________________________________________________
120 Bool_t AliTRDclusterizerV1::ReadDigits()
121 {
122   //
123   // Reads the digits arrays from the input aliroot file
124   //
125
126   if (!fRunLoader) {
127     printf("<AliTRDclusterizerV1::ReadDigits> ");
128     printf("No input file open\n");
129     return kFALSE;
130   }
131   AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
132   if (!loader->TreeD()) loader->LoadDigits();
133
134   // Read in the digit arrays
135   return (fDigitsManager->ReadDigits(loader->TreeD()));
136
137 }
138
139 //_____________________________________________________________________________
140 Bool_t AliTRDclusterizerV1::MakeClusters()
141 {
142   //
143   // Generates the cluster.
144   //
145
146   Int_t row, col, time;
147
148   /*
149   if (fTRD->IsVersion() != 1) {
150     printf("<AliTRDclusterizerV1::MakeCluster> ");
151     printf("TRD must be version 1 (slow simulator).\n");
152     return kFALSE; 
153   }
154   */
155
156   // Get the geometry
157   AliTRDgeometry *geo = AliTRDgeometry::GetGeometry(fRunLoader);
158
159   // Create a default parameter class if none is defined
160   if (!fPar) {
161     fPar = new AliTRDparameter("TRDparameter","Standard TRD parameter");
162     printf("<AliTRDclusterizerV1::MakeCluster> ");
163     printf("Create the default parameter object.\n");
164   }
165
166   Float_t timeBinSize = fPar->GetDriftVelocity()
167                       / fPar->GetSamplingFrequency();
168   // Half of ampl.region
169   const Float_t kAmWidth = AliTRDgeometry::AmThick()/2.; 
170
171   Float_t omegaTau = fPar->GetOmegaTau();
172   if (fVerbose > 0) {
173     printf("<AliTRDclusterizerV1::MakeCluster> ");
174     printf("OmegaTau = %f \n",omegaTau);
175     printf("<AliTRDclusterizerV1::MakeCluster> ");
176     printf("Start creating clusters.\n");
177   } 
178
179   AliTRDdataArrayI *digits;
180   AliTRDdataArrayI *track0;
181   AliTRDdataArrayI *track1;
182   AliTRDdataArrayI *track2; 
183
184   // Threshold value for the maximum
185   Int_t maxThresh = fPar->GetClusMaxThresh();   
186   // Threshold value for the digit signal
187   Int_t sigThresh = fPar->GetClusSigThresh();   
188
189   // Iteration limit for unfolding procedure
190   const Float_t kEpsilon = 0.01;             
191
192   const Int_t   kNclus   = 3;  
193   const Int_t   kNsig    = 5;
194   const Int_t   kNtrack  = 3 * kNclus;
195
196   Int_t    iType         = 0;
197   Int_t    iUnfold       = 0;
198
199   Double_t ratioLeft     = 1.0;
200   Double_t ratioRight    = 1.0;
201
202   Double_t padSignal[kNsig];   
203   Double_t clusterSignal[kNclus];
204   Double_t clusterPads[kNclus];   
205   Int_t    clusterDigit[kNclus];
206   Int_t    clusterTracks[kNtrack];   
207
208   Int_t    chamBeg = 0;
209   Int_t    chamEnd = AliTRDgeometry::Ncham();
210   Int_t    planBeg = 0;
211   Int_t    planEnd = AliTRDgeometry::Nplan();
212   Int_t    sectBeg = 0;
213   Int_t    sectEnd = AliTRDgeometry::Nsect();
214
215   // Start clustering in every chamber
216   for (Int_t icham = chamBeg; icham < chamEnd; icham++) {
217     for (Int_t iplan = planBeg; iplan < planEnd; iplan++) {
218       for (Int_t isect = sectBeg; isect < sectEnd; isect++) {
219
220         Int_t idet = geo->GetDetector(iplan,icham,isect);
221
222         Int_t nClusters      = 0;
223         Int_t nClusters2pad  = 0;
224         Int_t nClusters3pad  = 0;
225         Int_t nClusters4pad  = 0;
226         Int_t nClusters5pad  = 0;
227         Int_t nClustersLarge = 0;
228
229         if (fVerbose > 0) {
230           printf("<AliTRDclusterizerV1::MakeCluster> ");
231           printf("Analyzing chamber %d, plane %d, sector %d.\n"
232                 ,icham,iplan,isect);
233         }
234
235         Int_t    nRowMax     = fPar->GetRowMax(iplan,icham,isect);
236         Int_t    nColMax     = fPar->GetColMax(iplan);
237         Int_t    nTimeBefore = fPar->GetTimeBefore();
238         Int_t    nTimeTotal  = fPar->GetTimeTotal();  
239
240         AliTRDpadPlane *padPlane = fPar->GetPadPlane(iplan,icham);
241
242         // Get the digits
243         digits = fDigitsManager->GetDigits(idet);
244         digits->Expand();
245         track0 = fDigitsManager->GetDictionary(idet,0);
246         track0->Expand();
247         track1 = fDigitsManager->GetDictionary(idet,1);
248         track1->Expand();
249         track2 = fDigitsManager->GetDictionary(idet,2); 
250         track2->Expand();
251
252         // Loop through the chamber and find the maxima 
253         for ( row = 0;  row <  nRowMax;    row++) {
254           //          for ( col = 2;  col <  nColMax;    col++) {
255           for ( col = 4;  col <  nColMax-2;    col++) {
256             for (time = 0; time < nTimeTotal; time++) {
257
258               Int_t signalL = TMath::Abs(digits->GetDataUnchecked(row,col  ,time));
259               Int_t signalM = TMath::Abs(digits->GetDataUnchecked(row,col-1,time));
260               Int_t signalR = TMath::Abs(digits->GetDataUnchecked(row,col-2,time));
261  
262               // Look for the maximum
263               if (signalM >= maxThresh) {
264                 if (((signalL >= sigThresh) &&
265                      (signalL <  signalM))  ||
266                     ((signalR >= sigThresh) &&
267                      (signalR <  signalM))) {
268                   // Maximum found, mark the position by a negative signal
269                   digits->SetDataUnchecked(row,col-1,time,-signalM);
270                 }
271               }
272
273             }  
274           }    
275         }      
276
277         // Now check the maxima and calculate the cluster position
278         for ( row = 0;  row <  nRowMax  ;  row++) {
279           for (time = 0; time < nTimeTotal; time++) {
280             for ( col = 1;  col <  nColMax-1;  col++) {
281
282               // Maximum found ?             
283               if (digits->GetDataUnchecked(row,col,time) < 0) {
284
285                 Int_t iPad;
286                 for (iPad = 0; iPad < kNclus; iPad++) {
287                   Int_t iPadCol = col - 1 + iPad;
288                   clusterSignal[iPad]     = TMath::Abs(digits->GetDataUnchecked(row
289                                                                                ,iPadCol
290                                                                                ,time));
291                   clusterDigit[iPad]      = digits->GetIndexUnchecked(row,iPadCol,time);
292                   clusterTracks[3*iPad  ] = track0->GetDataUnchecked(row,iPadCol,time) - 1;
293                   clusterTracks[3*iPad+1] = track1->GetDataUnchecked(row,iPadCol,time) - 1;
294                   clusterTracks[3*iPad+2] = track2->GetDataUnchecked(row,iPadCol,time) - 1;
295                 }
296
297                 // Count the number of pads in the cluster
298                 Int_t nPadCount = 0;
299                 Int_t ii        = 0;
300                 while (TMath::Abs(digits->GetDataUnchecked(row,col-ii  ,time))
301                                                                   >= sigThresh) {
302                   nPadCount++;
303                   ii++;
304                   if (col-ii   <        0) break;
305                 }
306                 ii = 0;
307                 while (TMath::Abs(digits->GetDataUnchecked(row,col+ii+1,time))
308                                                                   >= sigThresh) {
309                   nPadCount++;
310                   ii++;
311                   if (col+ii+1 >= nColMax) break;
312                 }
313
314                 nClusters++;
315                 switch (nPadCount) {
316                 case 2:
317                   iType = 0;
318                   nClusters2pad++;
319                   break;
320                 case 3:
321                   iType = 1;
322                   nClusters3pad++;
323                   break;
324                 case 4:
325                   iType = 2;
326                   nClusters4pad++;
327                   break;
328                 case 5:
329                   iType = 3;
330                   nClusters5pad++;
331                   break;
332                 default:
333                   iType = 4;
334                   nClustersLarge++;
335                   break;
336                 };
337
338                 // Don't analyze large clusters
339                 //if (iType == 4) continue;
340
341                 // Look for 5 pad cluster with minimum in the middle
342                 Bool_t fivePadCluster = kFALSE;
343                 if (col < nColMax-3) {
344                   if (digits->GetDataUnchecked(row,col+2,time) < 0) {
345                     fivePadCluster = kTRUE;
346                   }
347                   if ((fivePadCluster) && (col < nColMax-5)) {
348                     if (digits->GetDataUnchecked(row,col+4,time) >= sigThresh) {
349                       fivePadCluster = kFALSE;
350                     }
351                   }
352                   if ((fivePadCluster) && (col >         1)) {
353                     if (digits->GetDataUnchecked(row,col-2,time) >= sigThresh) {
354                       fivePadCluster = kFALSE;
355                     }
356                   }
357                 }
358
359                 // 5 pad cluster
360                 // Modify the signal of the overlapping pad for the left part 
361                 // of the cluster which remains from a previous unfolding
362                 if (iUnfold) {
363                   clusterSignal[0] *= ratioLeft;
364                   iType   = 3;
365                   iUnfold = 0;
366                 }
367
368                 // Unfold the 5 pad cluster
369                 if (fivePadCluster) {
370                   for (iPad = 0; iPad < kNsig; iPad++) {
371                     padSignal[iPad] = TMath::Abs(digits->GetDataUnchecked(row
372                                                                          ,col-1+iPad
373                                                                          ,time));
374                   }
375                   // Unfold the two maxima and set the signal on 
376                   // the overlapping pad to the ratio
377                   ratioRight        = Unfold(kEpsilon,iplan,padSignal);
378                   ratioLeft         = 1.0 - ratioRight; 
379                   clusterSignal[2] *= ratioRight;
380                   iType   = 3;
381                   iUnfold = 1;
382                 }
383
384                 Double_t clusterCharge = clusterSignal[0]
385                                        + clusterSignal[1]
386                                        + clusterSignal[2];
387                 
388                 // The position of the cluster
389                 clusterPads[0] = row + 0.5;
390                 // Take the shift of the additional time bins into account
391                 clusterPads[2] = time - nTimeBefore + 0.5;
392
393                 if (fPar->LUTOn()) {
394
395                   // Calculate the position of the cluster by using the
396                   // lookup table method
397 //                   clusterPads[1] = col + 0.5
398 //                                  + fPar->LUTposition(iplan,clusterSignal[0]
399 //                                                           ,clusterSignal[1]
400 //                                                        ,clusterSignal[2]);
401                   clusterPads[1] = 0.5
402                                  + fPar->LUTposition(iplan,clusterSignal[0]
403                                                           ,clusterSignal[1]
404                                                           ,clusterSignal[2]);
405
406                 }
407                 else {
408
409                   // Calculate the position of the cluster by using the
410                   // center of gravity method
411 //                   clusterPads[1] = col + 0.5 
412 //                                  + (clusterSignal[2] - clusterSignal[0]) 
413 //                               / clusterCharge;
414                   clusterPads[1] = 0.5 
415                                  + (clusterSignal[2] - clusterSignal[0]) 
416                                  / clusterCharge;
417
418                 }
419
420                 Double_t q0 = clusterSignal[0];
421                 Double_t q1 = clusterSignal[1];
422                 Double_t q2 = clusterSignal[2];
423                 Double_t clusterSigmaY2 = (q1*(q0+q2)+4*q0*q2) /
424                                           (clusterCharge*clusterCharge);
425
426                 if (fVerbose > 1) {
427                   printf("-----------------------------------------------------------\n");
428                   printf("Create cluster no. %d\n",nClusters);
429                   printf("Position: row = %f, col = %f, time = %f\n",clusterPads[0]
430                                                                     ,clusterPads[1]
431                                                                     ,clusterPads[2]);
432                   printf("Indices: %d, %d, %d\n",clusterDigit[0]
433                                                 ,clusterDigit[1]
434                                                 ,clusterDigit[2]);
435                   printf("Total charge = %f\n",clusterCharge);
436                   printf("Tracks: pad0 %d, %d, %d\n",clusterTracks[0]
437                                                     ,clusterTracks[1]
438                                                     ,clusterTracks[2]);
439                   printf("        pad1 %d, %d, %d\n",clusterTracks[3]
440                                                     ,clusterTracks[4]
441                                                     ,clusterTracks[5]);
442                   printf("        pad2 %d, %d, %d\n",clusterTracks[6]
443                                                     ,clusterTracks[7]
444                                                     ,clusterTracks[8]);
445                   printf("Type = %d, Number of pads = %d\n",iType,nPadCount);
446                 }
447
448                 // Calculate the position and the error
449                 Double_t clusterPos[3];
450 //                 clusterPos[0] = clusterPads[1] * colSize + col0;
451 //                 clusterPos[1] = clusterPads[0] * rowSize + row0;
452                 clusterPos[0] = padPlane->GetColPos(col) - clusterPads[1];
453                 clusterPos[1] = padPlane->GetRowPos(row) - clusterPads[0];
454                 clusterPos[2] = clusterPads[2];
455                 Double_t clusterSig[2];
456                 Double_t colSize = padPlane->GetColSize(col);
457                 Double_t rowSize = padPlane->GetRowSize(row);
458                 clusterSig[0] = (clusterSigmaY2 + 1./12.) * colSize*colSize;
459                 clusterSig[1] = rowSize * rowSize / 12.;
460
461                 // Correct for ExB displacement
462                 if (fPar->ExBOn()) { 
463                   Int_t    local_time_bin = (Int_t) clusterPads[2];
464                   Double_t driftLength    = local_time_bin * timeBinSize + kAmWidth;
465                   Double_t deltaY         = omegaTau * driftLength;
466                   clusterPos[1]           = clusterPos[1] - deltaY;
467                 }
468                                        
469                 // Add the cluster to the output array 
470                 AddCluster(clusterPos
471                           ,idet
472                           ,clusterCharge
473                           ,clusterTracks
474                           ,clusterSig
475                           ,iType);
476
477               }
478             } 
479           }   
480         }     
481
482         // Compress the arrays
483         digits->Compress(1,0);
484         track0->Compress(1,0);
485  track1->Compress(1,0);
486         track2->Compress(1,0);
487
488         // Write the cluster and reset the array
489         WriteClusters(idet);
490         ResetRecPoints();
491
492         if (fVerbose > 0) {
493           printf("<AliTRDclusterizerV1::MakeCluster> ");
494           printf("Found %d clusters in total.\n"
495                 ,nClusters);
496           printf("                                    2pad:  %d\n",nClusters2pad);
497           printf("                                    3pad:  %d\n",nClusters3pad);
498           printf("                                    4pad:  %d\n",nClusters4pad);
499           printf("                                    5pad:  %d\n",nClusters5pad);
500           printf("                                    Large: %d\n",nClustersLarge);
501         }
502
503       }    
504     }      
505   }        
506
507   if (fVerbose > 0) {
508     printf("<AliTRDclusterizerV1::MakeCluster> ");
509     printf("Done.\n");
510   }
511
512   return kTRUE;
513
514 }
515
516 //_____________________________________________________________________________
517 Double_t AliTRDclusterizerV1::Unfold(Double_t eps, Int_t plane, Double_t* padSignal)
518 {
519   //
520   // Method to unfold neighbouring maxima.
521   // The charge ratio on the overlapping pad is calculated
522   // until there is no more change within the range given by eps.
523   // The resulting ratio is then returned to the calling method.
524   //
525
526   Int_t   irc                = 0;
527   Int_t   itStep             = 0;      // Count iteration steps
528
529   Double_t ratio             = 0.5;    // Start value for ratio
530   Double_t prevRatio         = 0;      // Store previous ratio
531
532   Double_t newLeftSignal[3]  = {0};    // Array to store left cluster signal
533   Double_t newRightSignal[3] = {0};    // Array to store right cluster signal
534   Double_t newSignal[3]      = {0};
535
536   // Start the iteration
537   while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
538
539     itStep++;
540     prevRatio = ratio;
541
542     // Cluster position according to charge ratio
543     Double_t maxLeft  = (ratio*padSignal[2] - padSignal[0]) 
544                       / (padSignal[0] + padSignal[1] + ratio*padSignal[2]);
545     Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2]) 
546                       / ((1-ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
547
548     // Set cluster charge ratio
549     irc = fPar->PadResponse(1.0,maxLeft ,plane,newSignal);
550     Double_t ampLeft  = padSignal[1] / newSignal[1];
551     irc = fPar->PadResponse(1.0,maxRight,plane,newSignal);
552     Double_t ampRight = padSignal[3] / newSignal[1];
553
554     // Apply pad response to parameters
555     irc = fPar->PadResponse(ampLeft ,maxLeft ,plane,newLeftSignal );
556     irc = fPar->PadResponse(ampRight,maxRight,plane,newRightSignal);
557
558     // Calculate new overlapping ratio
559     ratio = TMath::Min((Double_t)1.0,newLeftSignal[2] / 
560                           (newLeftSignal[2] + newRightSignal[0]));
561
562   }
563
564   return ratio;
565
566 }
567