Update of tracking code provided by Sergei
[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 /*
17 $Log$
18 Revision 1.16  2002/03/25 20:01:30  cblume
19 Introduce parameter class
20
21 Revision 1.15  2001/11/14 12:09:11  cblume
22 Use correct name for digitizer
23
24 Revision 1.14  2001/11/14 10:50:45  cblume
25 Changes in digits IO. Add merging of summable digits
26
27 Revision 1.13  2001/05/28 17:07:58  hristov
28 Last minute changes; ExB correction in AliTRDclusterizerV1; taking into account of material in G10 TEC frames and material between TEC planes (C.Blume,S.Sedykh)
29
30 Revision 1.12  2001/05/21 17:42:58  hristov
31 Constant casted to avoid the ambiguity
32
33 Revision 1.11  2001/05/21 16:45:47  hristov
34 Last minute changes (C.Blume)
35
36 Revision 1.10  2001/05/07 08:06:44  cblume
37 Speedup of the code. Create only AliTRDcluster
38
39 Revision 1.9  2000/11/01 14:53:20  cblume
40 Merge with TRD-develop
41
42 Revision 1.1.4.5  2000/10/15 23:40:01  cblume
43 Remove AliTRDconst
44
45 Revision 1.1.4.4  2000/10/06 16:49:46  cblume
46 Made Getters const
47
48 Revision 1.1.4.3  2000/10/04 16:34:58  cblume
49 Replace include files by forward declarations
50
51 Revision 1.1.4.2  2000/09/22 14:49:49  cblume
52 Adapted to tracking code
53
54 Revision 1.8  2000/10/02 21:28:19  fca
55 Removal of useless dependecies via forward declarations
56
57 Revision 1.7  2000/06/27 13:08:50  cblume
58 Changed to Copy(TObject &A) to appease the HP-compiler
59
60 Revision 1.6  2000/06/09 11:10:07  cblume
61 Compiler warnings and coding conventions, next round
62
63 Revision 1.5  2000/06/08 18:32:58  cblume
64 Make code compliant to coding conventions
65
66 Revision 1.4  2000/06/07 16:27:01  cblume
67 Try to remove compiler warnings on Sun and HP
68
69 Revision 1.3  2000/05/08 16:17:27  cblume
70 Merge TRD-develop
71
72 Revision 1.1.4.1  2000/05/08 15:09:01  cblume
73 Introduce AliTRDdigitsManager
74
75 Revision 1.1  2000/02/28 18:58:54  cblume
76 Add new TRD classes
77
78 */
79
80 ///////////////////////////////////////////////////////////////////////////////
81 //                                                                           //
82 // TRD cluster finder for the slow simulator. 
83 //                                                                           //
84 ///////////////////////////////////////////////////////////////////////////////
85
86 #include <TF1.h>
87 #include <TTree.h>
88 #include <TH1.h>
89 #include <TFile.h>
90
91 #include "AliRun.h"
92
93 #include "AliTRD.h"
94 #include "AliTRDclusterizerV1.h"
95 #include "AliTRDmatrix.h"
96 #include "AliTRDgeometry.h"
97 #include "AliTRDdigitizer.h"
98 #include "AliTRDdataArrayF.h"
99 #include "AliTRDdataArrayI.h"
100 #include "AliTRDdigitsManager.h"
101 #include "AliTRDparameter.h"
102
103 ClassImp(AliTRDclusterizerV1)
104
105 //_____________________________________________________________________________
106 AliTRDclusterizerV1::AliTRDclusterizerV1():AliTRDclusterizer()
107 {
108   //
109   // AliTRDclusterizerV1 default constructor
110   //
111
112   fDigitsManager = 0;
113
114 }
115
116 //_____________________________________________________________________________
117 AliTRDclusterizerV1::AliTRDclusterizerV1(const Text_t* name, const Text_t* title)
118                     :AliTRDclusterizer(name,title)
119 {
120   //
121   // AliTRDclusterizerV1 default constructor
122   //
123
124   fDigitsManager = new AliTRDdigitsManager();
125   fDigitsManager->CreateArrays();
126
127 }
128
129 //_____________________________________________________________________________
130 AliTRDclusterizerV1::AliTRDclusterizerV1(const AliTRDclusterizerV1 &c)
131 {
132   //
133   // AliTRDclusterizerV1 copy constructor
134   //
135
136   ((AliTRDclusterizerV1 &) c).Copy(*this);
137
138 }
139
140 //_____________________________________________________________________________
141 AliTRDclusterizerV1::~AliTRDclusterizerV1()
142 {
143   //
144   // AliTRDclusterizerV1 destructor
145   //
146
147   if (fDigitsManager) {
148     delete fDigitsManager;
149     fDigitsManager = NULL;
150   }
151
152 }
153
154 //_____________________________________________________________________________
155 AliTRDclusterizerV1 &AliTRDclusterizerV1::operator=(const AliTRDclusterizerV1 &c)
156 {
157   //
158   // Assignment operator
159   //
160
161   if (this != &c) ((AliTRDclusterizerV1 &) c).Copy(*this);
162   return *this;
163
164 }
165
166 //_____________________________________________________________________________
167 void AliTRDclusterizerV1::Copy(TObject &c)
168 {
169   //
170   // Copy function
171   //
172
173   ((AliTRDclusterizerV1 &) c).fDigitsManager = 0;
174
175   AliTRDclusterizer::Copy(c);
176
177 }
178
179 //_____________________________________________________________________________
180 Bool_t AliTRDclusterizerV1::ReadDigits()
181 {
182   //
183   // Reads the digits arrays from the input aliroot file
184   //
185
186   if (!fInputFile) {
187     printf("<AliTRDclusterizerV1::ReadDigits> ");
188     printf("No input file open\n");
189     return kFALSE;
190   }
191
192   fDigitsManager->Open(fInputFile->GetName());
193
194   // Read in the digit arrays
195   return (fDigitsManager->ReadDigits());  
196
197 }
198
199 //_____________________________________________________________________________
200 Bool_t AliTRDclusterizerV1::MakeClusters()
201 {
202   //
203   // Generates the cluster.
204   //
205
206   Int_t row, col, time;
207
208   if (fTRD->IsVersion() != 1) {
209     printf("<AliTRDclusterizerV1::MakeCluster> ");
210     printf("TRD must be version 1 (slow simulator).\n");
211     return kFALSE; 
212   }
213
214   // Get the geometry
215   AliTRDgeometry *geo = fTRD->GetGeometry();
216
217   // Create a default parameter class if none is defined
218   if (!fPar) {
219     fPar = new AliTRDparameter("TRDparameter","Standard TRD parameter");
220     printf("<AliTRDclusterizerV1::MakeCluster> ");
221     printf("Create the default parameter object.\n");
222   }
223
224   Float_t timeBinSize = fPar->GetTimeBinSize();
225   // Half of ampl.region
226   const Float_t kAmWidth = AliTRDgeometry::AmThick()/2.; 
227
228   Float_t omegaTau = fPar->GetOmegaTau();
229   if (fVerbose > 0) {
230     printf("<AliTRDclusterizerV1::MakeCluster> ");
231     printf("OmegaTau = %f \n",omegaTau);
232     printf("<AliTRDclusterizerV1::MakeCluster> ");
233     printf("Start creating clusters.\n");
234   } 
235
236   AliTRDdataArrayI *digits;
237   AliTRDdataArrayI *track0;
238   AliTRDdataArrayI *track1;
239   AliTRDdataArrayI *track2; 
240
241   // Threshold value for the maximum
242   Int_t maxThresh = fPar->GetClusMaxThresh();   
243   // Threshold value for the digit signal
244   Int_t sigThresh = fPar->GetClusSigThresh();   
245
246   // Iteration limit for unfolding procedure
247   const Float_t kEpsilon = 0.01;             
248
249   const Int_t   kNclus   = 3;  
250   const Int_t   kNsig    = 5;
251   const Int_t   kNtrack  = 3 * kNclus;
252
253   Int_t   iType          = 0;
254   Int_t   iUnfold        = 0;
255
256   Float_t ratioLeft      = 1.0;
257   Float_t ratioRight     = 1.0;
258
259   Float_t padSignal[kNsig];   
260   Float_t clusterSignal[kNclus];
261   Float_t clusterPads[kNclus];   
262   Int_t   clusterDigit[kNclus];
263   Int_t   clusterTracks[kNtrack];   
264
265   Int_t chamBeg = 0;
266   Int_t chamEnd = AliTRDgeometry::Ncham();
267   if (fTRD->GetSensChamber()  >= 0) {
268     chamBeg = fTRD->GetSensChamber();
269     chamEnd = chamBeg + 1;
270   }
271   Int_t planBeg = 0;
272   Int_t planEnd = AliTRDgeometry::Nplan();
273   if (fTRD->GetSensPlane()    >= 0) {
274     planBeg = fTRD->GetSensPlane();
275     planEnd = planBeg + 1;
276   }
277   Int_t sectBeg = 0;
278   Int_t sectEnd = AliTRDgeometry::Nsect();
279
280   // Start clustering in every chamber
281   for (Int_t icham = chamBeg; icham < chamEnd; icham++) {
282     for (Int_t iplan = planBeg; iplan < planEnd; iplan++) {
283       for (Int_t isect = sectBeg; isect < sectEnd; isect++) {
284
285         if (fTRD->GetSensSector() >= 0) {
286           Int_t sens1 = fTRD->GetSensSector();
287           Int_t sens2 = sens1 + fTRD->GetSensSectorRange();
288           sens2 -= ((Int_t) (sens2 / AliTRDgeometry::Nsect())) 
289                  * AliTRDgeometry::Nsect();
290           if (sens1 < sens2) {
291             if ((isect < sens1) || (isect >= sens2)) continue;
292           }
293           else {
294             if ((isect < sens1) && (isect >= sens2)) continue;
295           }
296         }
297
298         Int_t idet = geo->GetDetector(iplan,icham,isect);
299
300         Int_t nClusters      = 0;
301         Int_t nClusters2pad  = 0;
302         Int_t nClusters3pad  = 0;
303         Int_t nClusters4pad  = 0;
304         Int_t nClusters5pad  = 0;
305         Int_t nClustersLarge = 0;
306
307         if (fVerbose > 0) {
308           printf("<AliTRDclusterizerV1::MakeCluster> ");
309           printf("Analyzing chamber %d, plane %d, sector %d.\n"
310                 ,icham,iplan,isect);
311         }
312
313         Int_t   nRowMax     = fPar->GetRowMax(iplan,icham,isect);
314         Int_t   nColMax     = fPar->GetColMax(iplan);
315         Int_t   nTimeBefore = fPar->GetTimeBefore();
316         Int_t   nTimeTotal  = fPar->GetTimeTotal();  
317
318         Float_t row0        = fPar->GetRow0(iplan,icham,isect);
319         Float_t col0        = fPar->GetCol0(iplan);
320         Float_t rowSize     = fPar->GetRowPadSize(iplan,icham,isect);
321         Float_t colSize     = fPar->GetColPadSize(iplan);
322
323         // Get the digits
324         digits = fDigitsManager->GetDigits(idet);
325         digits->Expand();
326         track0 = fDigitsManager->GetDictionary(idet,0);
327         track0->Expand();
328         track1 = fDigitsManager->GetDictionary(idet,1);
329         track1->Expand();
330         track2 = fDigitsManager->GetDictionary(idet,2); 
331         track2->Expand();
332
333         // Loop through the chamber and find the maxima 
334         for ( row = 0;  row <  nRowMax;    row++) {
335           for ( col = 2;  col <  nColMax;    col++) {
336             for (time = 0; time < nTimeTotal; time++) {
337
338               Int_t signalL = TMath::Abs(digits->GetDataUnchecked(row,col  ,time));
339               Int_t signalM = TMath::Abs(digits->GetDataUnchecked(row,col-1,time));
340               Int_t signalR = TMath::Abs(digits->GetDataUnchecked(row,col-2,time));
341  
342               // Look for the maximum
343               if (signalM >= maxThresh) {
344                 if (((signalL >= sigThresh) &&
345                      (signalL <  signalM))  ||
346                     ((signalR >= sigThresh) &&
347                      (signalR <  signalM))) {
348                   // Maximum found, mark the position by a negative signal
349                   digits->SetDataUnchecked(row,col-1,time,-signalM);
350                 }
351               }
352
353             }  
354           }    
355         }      
356
357         // Now check the maxima and calculate the cluster position
358         for ( row = 0;  row <  nRowMax  ;  row++) {
359           for (time = 0; time < nTimeTotal; time++) {
360             for ( col = 1;  col <  nColMax-1;  col++) {
361
362               // Maximum found ?             
363               if (digits->GetDataUnchecked(row,col,time) < 0) {
364
365                 Int_t iPad;
366                 for (iPad = 0; iPad < kNclus; iPad++) {
367                   Int_t iPadCol = col - 1 + iPad;
368                   clusterSignal[iPad]     = TMath::Abs(digits->GetDataUnchecked(row
369                                                                                ,iPadCol
370                                                                                ,time));
371                   clusterDigit[iPad]      = digits->GetIndexUnchecked(row,iPadCol,time);
372                   clusterTracks[3*iPad  ] = track0->GetDataUnchecked(row,iPadCol,time) - 1;
373                   clusterTracks[3*iPad+1] = track1->GetDataUnchecked(row,iPadCol,time) - 1;
374                   clusterTracks[3*iPad+2] = track2->GetDataUnchecked(row,iPadCol,time) - 1;
375                 }
376
377                 // Count the number of pads in the cluster
378                 Int_t nPadCount = 0;
379                 Int_t ii        = 0;
380                 while (TMath::Abs(digits->GetDataUnchecked(row,col-ii  ,time))
381                                                                   >= sigThresh) {
382                   nPadCount++;
383                   ii++;
384                   if (col-ii   <        0) break;
385                 }
386                 ii = 0;
387                 while (TMath::Abs(digits->GetDataUnchecked(row,col+ii+1,time))
388                                                                   >= sigThresh) {
389                   nPadCount++;
390                   ii++;
391                   if (col+ii+1 >= nColMax) break;
392                 }
393
394                 nClusters++;
395                 switch (nPadCount) {
396                 case 2:
397                   iType = 0;
398                   nClusters2pad++;
399                   break;
400                 case 3:
401                   iType = 1;
402                   nClusters3pad++;
403                   break;
404                 case 4:
405                   iType = 2;
406                   nClusters4pad++;
407                   break;
408                 case 5:
409                   iType = 3;
410                   nClusters5pad++;
411                   break;
412                 default:
413                   iType = 4;
414                   nClustersLarge++;
415                   break;
416                 };
417
418                 // Don't analyze large clusters
419                 //if (iType == 4) continue;
420
421                 // Look for 5 pad cluster with minimum in the middle
422                 Bool_t fivePadCluster = kFALSE;
423                 if (col < nColMax-3) {
424                   if (digits->GetDataUnchecked(row,col+2,time) < 0) {
425                     fivePadCluster = kTRUE;
426                   }
427                   if ((fivePadCluster) && (col < nColMax-5)) {
428                     if (digits->GetDataUnchecked(row,col+4,time) >= sigThresh) {
429                       fivePadCluster = kFALSE;
430                     }
431                   }
432                   if ((fivePadCluster) && (col >         1)) {
433                     if (digits->GetDataUnchecked(row,col-2,time) >= sigThresh) {
434                       fivePadCluster = kFALSE;
435                     }
436                   }
437                 }
438
439                 // 5 pad cluster
440                 // Modify the signal of the overlapping pad for the left part 
441                 // of the cluster which remains from a previous unfolding
442                 if (iUnfold) {
443                   clusterSignal[0] *= ratioLeft;
444                   iType   = 3;
445                   iUnfold = 0;
446                 }
447
448                 // Unfold the 5 pad cluster
449                 if (fivePadCluster) {
450                   for (iPad = 0; iPad < kNsig; iPad++) {
451                     padSignal[iPad] = TMath::Abs(digits->GetDataUnchecked(row
452                                                                          ,col-1+iPad
453                                                                          ,time));
454                   }
455                   // Unfold the two maxima and set the signal on 
456                   // the overlapping pad to the ratio
457                   ratioRight        = Unfold(kEpsilon,iplan,padSignal);
458                   ratioLeft         = 1.0 - ratioRight; 
459                   clusterSignal[2] *= ratioRight;
460                   iType   = 3;
461                   iUnfold = 1;
462                 }
463
464                 Float_t clusterCharge = clusterSignal[0]
465                                       + clusterSignal[1]
466                                       + clusterSignal[2];
467                 
468                 // The position of the cluster
469                 clusterPads[0] = row + 0.5;
470                 // Take the shift of the additional time bins into account
471                 clusterPads[2] = time - nTimeBefore + 0.5;
472
473                 if (fPar->LUTOn()) {
474
475                   // Calculate the position of the cluster by using the
476                   // lookup table method
477                   clusterPads[1] = col + 0.5
478                                  + fPar->LUTposition(iplan,clusterSignal[0]
479                                                           ,clusterSignal[1]
480                                                           ,clusterSignal[2]);
481
482                 }
483                 else {
484
485                   // Calculate the position of the cluster by using the
486                   // center of gravity method
487                   clusterPads[1] = col + 0.5 
488                                  + (clusterSignal[2] - clusterSignal[0]) 
489                                  / clusterCharge;
490
491                 }
492
493                 Float_t q0 = clusterSignal[0];
494                 Float_t q1 = clusterSignal[1];
495                 Float_t q2 = clusterSignal[2];
496                 Float_t clusterSigmaY2 = (q1*(q0+q2)+4*q0*q2) /
497                                          (clusterCharge*clusterCharge);
498
499                 // Correct for ExB displacement
500                 if (fPar->ExBOn()) { 
501                   Int_t   local_time_bin = (Int_t) clusterPads[2];
502                   Float_t driftLength    = local_time_bin * timeBinSize + kAmWidth;
503                   Float_t colSize        = fPar->GetColPadSize(iplan);
504                   Float_t deltaY         = omegaTau*driftLength/colSize;
505                   clusterPads[1]         = clusterPads[1] - deltaY;
506                 }
507                                        
508                 if (fVerbose > 1) {
509                   printf("-----------------------------------------------------------\n");
510                   printf("Create cluster no. %d\n",nClusters);
511                   printf("Position: row = %f, col = %f, time = %f\n",clusterPads[0]
512                                                                     ,clusterPads[1]
513                                                                     ,clusterPads[2]);
514                   printf("Indices: %d, %d, %d\n",clusterDigit[0]
515                                                 ,clusterDigit[1]
516                                                 ,clusterDigit[2]);
517                   printf("Total charge = %f\n",clusterCharge);
518                   printf("Tracks: pad0 %d, %d, %d\n",clusterTracks[0]
519                                                     ,clusterTracks[1]
520                                                     ,clusterTracks[2]);
521                   printf("        pad1 %d, %d, %d\n",clusterTracks[3]
522                                                     ,clusterTracks[4]
523                                                     ,clusterTracks[5]);
524                   printf("        pad2 %d, %d, %d\n",clusterTracks[6]
525                                                     ,clusterTracks[7]
526                                                     ,clusterTracks[8]);
527                   printf("Type = %d, Number of pads = %d\n",iType,nPadCount);
528                 }
529
530                 // Calculate the position and the error
531                 Float_t clusterPos[3];
532                 clusterPos[0] = clusterPads[1] * colSize + col0;
533                 clusterPos[1] = clusterPads[0] * rowSize + row0;
534                 clusterPos[2] = clusterPads[2];
535                 Float_t clusterSig[2];
536                 clusterSig[0] = (clusterSigmaY2 + 1./12.) * colSize*colSize;
537                 clusterSig[1] = rowSize * rowSize / 12.;
538
539                 // Add the cluster to the output array 
540                 fTRD->AddCluster(clusterPos
541                                 ,idet
542                                 ,clusterCharge
543                                 ,clusterTracks
544                                 ,clusterSig
545                                 ,iType);
546
547               }
548             } 
549           }   
550         }     
551
552         // Compress the arrays
553         digits->Compress(1,0);
554         track0->Compress(1,0);
555         track1->Compress(1,0);
556         track2->Compress(1,0);
557
558         // Write the cluster and reset the array
559         WriteClusters(idet);
560         fTRD->ResetRecPoints();
561
562         if (fVerbose > 0) {
563           printf("<AliTRDclusterizerV1::MakeCluster> ");
564           printf("Found %d clusters in total.\n"
565                 ,nClusters);
566           printf("                                    2pad:  %d\n",nClusters2pad);
567           printf("                                    3pad:  %d\n",nClusters3pad);
568           printf("                                    4pad:  %d\n",nClusters4pad);
569           printf("                                    5pad:  %d\n",nClusters5pad);
570           printf("                                    Large: %d\n",nClustersLarge);
571         }
572
573       }    
574     }      
575   }        
576
577   if (fVerbose > 0) {
578     printf("<AliTRDclusterizerV1::MakeCluster> ");
579     printf("Done.\n");
580   }
581
582   return kTRUE;
583
584 }
585
586 //_____________________________________________________________________________
587 Float_t AliTRDclusterizerV1::Unfold(Float_t eps, Int_t plane, Float_t* padSignal)
588 {
589   //
590   // Method to unfold neighbouring maxima.
591   // The charge ratio on the overlapping pad is calculated
592   // until there is no more change within the range given by eps.
593   // The resulting ratio is then returned to the calling method.
594   //
595
596   Int_t   irc               = 0;
597   Int_t   itStep            = 0;      // Count iteration steps
598
599   Float_t ratio             = 0.5;    // Start value for ratio
600   Float_t prevRatio         = 0;      // Store previous ratio
601
602   Float_t newLeftSignal[3]  = {0};    // Array to store left cluster signal
603   Float_t newRightSignal[3] = {0};    // Array to store right cluster signal
604   Float_t newSignal[3]      = {0};
605
606   // Start the iteration
607   while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
608
609     itStep++;
610     prevRatio = ratio;
611
612     // Cluster position according to charge ratio
613     Float_t maxLeft  = (ratio*padSignal[2] - padSignal[0]) 
614                      / (padSignal[0] + padSignal[1] + ratio*padSignal[2]);
615     Float_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2]) 
616                      / ((1-ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
617
618     // Set cluster charge ratio
619     irc = fPar->PadResponse(1.0,maxLeft ,plane,newSignal);
620     Float_t ampLeft  = padSignal[1] / newSignal[1];
621     irc = fPar->PadResponse(1.0,maxRight,plane,newSignal);
622     Float_t ampRight = padSignal[3] / newSignal[1];
623
624     // Apply pad response to parameters
625     irc = fPar->PadResponse(ampLeft ,maxLeft ,plane,newLeftSignal );
626     irc = fPar->PadResponse(ampRight,maxRight,plane,newRightSignal);
627
628     // Calculate new overlapping ratio
629     ratio = TMath::Min((Float_t)1.0,newLeftSignal[2] / 
630                           (newLeftSignal[2] + newRightSignal[0]));
631
632   }
633
634   return ratio;
635
636 }
637