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