Raw data reconstruction (K.Shileev)
[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
8230f242 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
f7336fa3 80//_____________________________________________________________________________
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
dd9a6ee3 94//_____________________________________________________________________________
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
8230f242 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
f7336fa3 119//_____________________________________________________________________________
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
7ad19338 167 //Float_t timeBinSize = fPar->GetDriftVelocity()
168 // / fPar->GetSamplingFrequency();
a819a5f7 169 // Half of ampl.region
7ad19338 170 // const Float_t kAmWidth = AliTRDgeometry::AmThick()/2.;
a819a5f7 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 // Iteration limit for unfolding procedure
8230f242 190 const Float_t kEpsilon = 0.01;
f7336fa3 191
8230f242 192 const Int_t kNclus = 3;
193 const Int_t kNsig = 5;
3e1a3ad8 194 const Int_t kNtrack = 3 * kNclus;
195
a5cadd36 196 Int_t iType = 0;
7ad19338 197 Int_t iUnfold = 0;
a5cadd36 198 Double_t ratioLeft = 1.0;
199 Double_t ratioRight = 1.0;
db30bf0f 200
7ad19338 201 //
a5cadd36 202 Double_t padSignal[kNsig];
203 Double_t clusterSignal[kNclus];
204 Double_t clusterPads[kNclus];
205 Int_t clusterDigit[kNclus];
206 Int_t clusterTracks[kNtrack];
f7336fa3 207
a5cadd36 208 Int_t chamBeg = 0;
209 Int_t chamEnd = AliTRDgeometry::Ncham();
210 Int_t planBeg = 0;
211 Int_t planEnd = AliTRDgeometry::Nplan();
212 Int_t sectBeg = 0;
213 Int_t sectEnd = AliTRDgeometry::Nsect();
f7336fa3 214
3e1a3ad8 215 // Start clustering in every chamber
f7336fa3 216 for (Int_t icham = chamBeg; icham < chamEnd; icham++) {
217 for (Int_t iplan = planBeg; iplan < planEnd; iplan++) {
218 for (Int_t isect = sectBeg; isect < sectEnd; isect++) {
219
8230f242 220 Int_t idet = geo->GetDetector(iplan,icham,isect);
f7336fa3 221
db30bf0f 222 Int_t nClusters = 0;
223 Int_t nClusters2pad = 0;
224 Int_t nClusters3pad = 0;
225 Int_t nClusters4pad = 0;
226 Int_t nClusters5pad = 0;
227 Int_t nClustersLarge = 0;
3e1a3ad8 228
47517f42 229 if (fVerbose > 0) {
17b26de4 230 printf("<AliTRDclusterizerV1::MakeCluster> ");
47517f42 231 printf("Analyzing chamber %d, plane %d, sector %d.\n"
232 ,icham,iplan,isect);
233 }
f7336fa3 234
a5cadd36 235 Int_t nRowMax = fPar->GetRowMax(iplan,icham,isect);
236 Int_t nColMax = fPar->GetColMax(iplan);
237 Int_t nTimeBefore = fPar->GetTimeBefore();
238 Int_t nTimeTotal = fPar->GetTimeTotal();
5443e65e 239
a5cadd36 240 AliTRDpadPlane *padPlane = fPar->GetPadPlane(iplan,icham);
f7336fa3 241
3e1a3ad8 242 // Get the digits
8230f242 243 digits = fDigitsManager->GetDigits(idet);
3e1a3ad8 244 digits->Expand();
793ff80c 245 track0 = fDigitsManager->GetDictionary(idet,0);
3e1a3ad8 246 track0->Expand();
793ff80c 247 track1 = fDigitsManager->GetDictionary(idet,1);
3e1a3ad8 248 track1->Expand();
793ff80c 249 track2 = fDigitsManager->GetDictionary(idet,2);
3e1a3ad8 250 track2->Expand();
251
252 // Loop through the chamber and find the maxima
253 for ( row = 0; row < nRowMax; row++) {
de4b10e5 254 for ( col = 2; col < nColMax; col++) {
255 //for ( col = 4; col < nColMax-2; col++) {
3e1a3ad8 256 for (time = 0; time < nTimeTotal; time++) {
257
a819a5f7 258 Int_t signalL = TMath::Abs(digits->GetDataUnchecked(row,col ,time));
259 Int_t signalM = TMath::Abs(digits->GetDataUnchecked(row,col-1,time));
260 Int_t signalR = TMath::Abs(digits->GetDataUnchecked(row,col-2,time));
3e1a3ad8 261
7ad19338 262// // Look for the maximum
263// if (signalM >= maxThresh) {
264// if (((signalL >= sigThresh) &&
265// (signalL < signalM)) ||
266// ((signalR >= sigThresh) &&
267// (signalR < signalM))) {
268// // Maximum found, mark the position by a negative signal
269// digits->SetDataUnchecked(row,col-1,time,-signalM);
270// }
271// }
3e1a3ad8 272 // Look for the maximum
db30bf0f 273 if (signalM >= maxThresh) {
de4b10e5 274 if ( (TMath::Abs(signalL)<=signalM) && (TMath::Abs(signalR)<=signalM) &&
275 (TMath::Abs(signalL)+TMath::Abs(signalR))>sigThresh ) {
3e1a3ad8 276 // Maximum found, mark the position by a negative signal
277 digits->SetDataUnchecked(row,col-1,time,-signalM);
278 }
279 }
280
281 }
282 }
283 }
284
285 // Now check the maxima and calculate the cluster position
286 for ( row = 0; row < nRowMax ; row++) {
db30bf0f 287 for (time = 0; time < nTimeTotal; time++) {
288 for ( col = 1; col < nColMax-1; col++) {
3e1a3ad8 289
290 // Maximum found ?
291 if (digits->GetDataUnchecked(row,col,time) < 0) {
f7336fa3 292
9d0b222b 293 Int_t iPad;
8230f242 294 for (iPad = 0; iPad < kNclus; iPad++) {
3e1a3ad8 295 Int_t iPadCol = col - 1 + iPad;
296 clusterSignal[iPad] = TMath::Abs(digits->GetDataUnchecked(row
297 ,iPadCol
298 ,time));
299 clusterDigit[iPad] = digits->GetIndexUnchecked(row,iPadCol,time);
300 clusterTracks[3*iPad ] = track0->GetDataUnchecked(row,iPadCol,time) - 1;
301 clusterTracks[3*iPad+1] = track1->GetDataUnchecked(row,iPadCol,time) - 1;
302 clusterTracks[3*iPad+2] = track2->GetDataUnchecked(row,iPadCol,time) - 1;
f7336fa3 303 }
304
db30bf0f 305 // Count the number of pads in the cluster
306 Int_t nPadCount = 0;
307 Int_t ii = 0;
308 while (TMath::Abs(digits->GetDataUnchecked(row,col-ii ,time))
309 >= sigThresh) {
310 nPadCount++;
311 ii++;
312 if (col-ii < 0) break;
313 }
314 ii = 0;
315 while (TMath::Abs(digits->GetDataUnchecked(row,col+ii+1,time))
316 >= sigThresh) {
317 nPadCount++;
318 ii++;
319 if (col+ii+1 >= nColMax) break;
320 }
321
322 nClusters++;
323 switch (nPadCount) {
324 case 2:
325 iType = 0;
326 nClusters2pad++;
327 break;
328 case 3:
329 iType = 1;
330 nClusters3pad++;
331 break;
332 case 4:
333 iType = 2;
334 nClusters4pad++;
335 break;
336 case 5:
337 iType = 3;
338 nClusters5pad++;
339 break;
340 default:
341 iType = 4;
342 nClustersLarge++;
343 break;
344 };
345
7ad19338 346 // Look for 5 pad cluster with minimum in the middle
db30bf0f 347 Bool_t fivePadCluster = kFALSE;
3e1a3ad8 348 if (col < nColMax-3) {
349 if (digits->GetDataUnchecked(row,col+2,time) < 0) {
db30bf0f 350 fivePadCluster = kTRUE;
351 }
352 if ((fivePadCluster) && (col < nColMax-5)) {
353 if (digits->GetDataUnchecked(row,col+4,time) >= sigThresh) {
354 fivePadCluster = kFALSE;
355 }
356 }
357 if ((fivePadCluster) && (col > 1)) {
358 if (digits->GetDataUnchecked(row,col-2,time) >= sigThresh) {
359 fivePadCluster = kFALSE;
360 }
361 }
362 }
363
364 // 5 pad cluster
365 // Modify the signal of the overlapping pad for the left part
366 // of the cluster which remains from a previous unfolding
367 if (iUnfold) {
368 clusterSignal[0] *= ratioLeft;
7ad19338 369 iType = 5;
db30bf0f 370 iUnfold = 0;
371 }
372
373 // Unfold the 5 pad cluster
374 if (fivePadCluster) {
375 for (iPad = 0; iPad < kNsig; iPad++) {
376 padSignal[iPad] = TMath::Abs(digits->GetDataUnchecked(row
377 ,col-1+iPad
378 ,time));
f7336fa3 379 }
db30bf0f 380 // Unfold the two maxima and set the signal on
381 // the overlapping pad to the ratio
17b26de4 382 ratioRight = Unfold(kEpsilon,iplan,padSignal);
db30bf0f 383 ratioLeft = 1.0 - ratioRight;
384 clusterSignal[2] *= ratioRight;
7ad19338 385 iType = 5;
db30bf0f 386 iUnfold = 1;
f7336fa3 387 }
f7336fa3 388
7ad19338 389
a5cadd36 390 Double_t clusterCharge = clusterSignal[0]
391 + clusterSignal[1]
392 + clusterSignal[2];
3e1a3ad8 393
db30bf0f 394 // The position of the cluster
3e1a3ad8 395 clusterPads[0] = row + 0.5;
3e1a3ad8 396 // Take the shift of the additional time bins into account
397 clusterPads[2] = time - nTimeBefore + 0.5;
398
17b26de4 399 if (fPar->LUTOn()) {
db30bf0f 400 // Calculate the position of the cluster by using the
401 // lookup table method
7ad19338 402 clusterPads[1] =
403 fPar->LUTposition(iplan,clusterSignal[0]
17b26de4 404 ,clusterSignal[1]
405 ,clusterSignal[2]);
db30bf0f 406 }
407 else {
db30bf0f 408 // Calculate the position of the cluster by using the
409 // center of gravity method
7ad19338 410 for (Int_t i=0;i<5;i++) padSignal[i]=0;
411 padSignal[2] = TMath::Abs(digits->GetDataUnchecked(row,col,time)); // central pad
412 padSignal[1] = TMath::Abs(digits->GetDataUnchecked(row,col-1,time)); // left pad
413 padSignal[3] = TMath::Abs(digits->GetDataUnchecked(row,col+1,time)); // right pad
414 if (col>2 &&TMath::Abs(digits->GetDataUnchecked(row,col-2,time)<padSignal[1])){
415 padSignal[0] = TMath::Abs(digits->GetDataUnchecked(row,col-2,time));
416 }
417 if (col<nColMax-3 &&TMath::Abs(digits->GetDataUnchecked(row,col+2,time)<padSignal[3])){
418 padSignal[4] = TMath::Abs(digits->GetDataUnchecked(row,col+2,time));
419 }
420 clusterPads[1] = GetCOG(padSignal);
421 Double_t check = fPar->LUTposition(iplan,clusterSignal[0]
422 ,clusterSignal[1]
423 ,clusterSignal[2]);
de4b10e5 424 // Float_t diff = clusterPads[1] - check;
db30bf0f 425
426 }
427
a5cadd36 428 Double_t q0 = clusterSignal[0];
429 Double_t q1 = clusterSignal[1];
430 Double_t q2 = clusterSignal[2];
431 Double_t clusterSigmaY2 = (q1*(q0+q2)+4*q0*q2) /
432 (clusterCharge*clusterCharge);
a819a5f7 433
5443e65e 434 // Calculate the position and the error
7ad19338 435 Double_t colSize = padPlane->GetColSize(col);
436 Double_t rowSize = padPlane->GetRowSize(row);
a5cadd36 437 Double_t clusterPos[3];
7ad19338 438 clusterPos[0] = padPlane->GetColPos(col) + (clusterPads[1]-0.5)*colSize; // MI change
439 clusterPos[1] = padPlane->GetRowPos(row) -0.5*rowSize; //MI change
5443e65e 440 clusterPos[2] = clusterPads[2];
a5cadd36 441 Double_t clusterSig[2];
5443e65e 442 clusterSig[0] = (clusterSigmaY2 + 1./12.) * colSize*colSize;
7ad19338 443 clusterSig[1] = rowSize * rowSize / 12.;
f7336fa3 444 // Add the cluster to the output array
bdbb05bb 445 AddCluster(clusterPos
a5cadd36 446 ,idet
447 ,clusterCharge
448 ,clusterTracks
449 ,clusterSig
de4b10e5 450 ,iType,clusterPads[1]);
f7336fa3 451
452 }
3e1a3ad8 453 }
454 }
455 }
f7336fa3 456
3e1a3ad8 457 // Compress the arrays
458 digits->Compress(1,0);
459 track0->Compress(1,0);
7ad19338 460 track1->Compress(1,0);
3e1a3ad8 461 track2->Compress(1,0);
f7336fa3 462
3e1a3ad8 463 // Write the cluster and reset the array
793ff80c 464 WriteClusters(idet);
bdbb05bb 465 ResetRecPoints();
3e1a3ad8 466 }
467 }
468 }
f7336fa3 469
47517f42 470 if (fVerbose > 0) {
17b26de4 471 printf("<AliTRDclusterizerV1::MakeCluster> ");
47517f42 472 printf("Done.\n");
473 }
f7336fa3 474
475 return kTRUE;
476
477}
478
7ad19338 479Double_t AliTRDclusterizerV1::GetCOG(Double_t signal[5])
480{
481 //
482 // get COG position
483 // used for clusters with more than 3 pads - where LUT not applicable
484 Double_t sum = signal[0]+signal[1]+signal[2]+signal[3]+signal[4];
485 Double_t res = (0.0*(-signal[0]+signal[4])+(-signal[1]+signal[3]))/sum;
486 return res;
487}
488
489
490
f7336fa3 491//_____________________________________________________________________________
a5cadd36 492Double_t AliTRDclusterizerV1::Unfold(Double_t eps, Int_t plane, Double_t* padSignal)
f7336fa3 493{
494 //
495 // Method to unfold neighbouring maxima.
496 // The charge ratio on the overlapping pad is calculated
497 // until there is no more change within the range given by eps.
498 // The resulting ratio is then returned to the calling method.
499 //
500
a5cadd36 501 Int_t irc = 0;
502 Int_t itStep = 0; // Count iteration steps
f7336fa3 503
a5cadd36 504 Double_t ratio = 0.5; // Start value for ratio
505 Double_t prevRatio = 0; // Store previous ratio
f7336fa3 506
a5cadd36 507 Double_t newLeftSignal[3] = {0}; // Array to store left cluster signal
508 Double_t newRightSignal[3] = {0}; // Array to store right cluster signal
509 Double_t newSignal[3] = {0};
f7336fa3 510
3e1a3ad8 511 // Start the iteration
f7336fa3 512 while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
513
514 itStep++;
515 prevRatio = ratio;
516
3e1a3ad8 517 // Cluster position according to charge ratio
a5cadd36 518 Double_t maxLeft = (ratio*padSignal[2] - padSignal[0])
519 / (padSignal[0] + padSignal[1] + ratio*padSignal[2]);
520 Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2])
521 / ((1-ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
f7336fa3 522
3e1a3ad8 523 // Set cluster charge ratio
17b26de4 524 irc = fPar->PadResponse(1.0,maxLeft ,plane,newSignal);
a5cadd36 525 Double_t ampLeft = padSignal[1] / newSignal[1];
17b26de4 526 irc = fPar->PadResponse(1.0,maxRight,plane,newSignal);
a5cadd36 527 Double_t ampRight = padSignal[3] / newSignal[1];
f7336fa3 528
3e1a3ad8 529 // Apply pad response to parameters
17b26de4 530 irc = fPar->PadResponse(ampLeft ,maxLeft ,plane,newLeftSignal );
531 irc = fPar->PadResponse(ampRight,maxRight,plane,newRightSignal);
f7336fa3 532
3e1a3ad8 533 // Calculate new overlapping ratio
a5cadd36 534 ratio = TMath::Min((Double_t)1.0,newLeftSignal[2] /
db30bf0f 535 (newLeftSignal[2] + newRightSignal[0]));
f7336fa3 536
537 }
538
539 return ratio;
540
541}
542