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