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