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