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