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