]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TRD/AliTRDdigitizer.cxx
Particle history saved
[u/mrichter/AliRoot.git] / TRD / AliTRDdigitizer.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// Creates and handles digits from TRD hits //
abaf1f1d 21// Author: C. Blume (C.Blume@gsi.de) //
f7336fa3 22// //
23// The following effects are included: //
24// - Diffusion //
25// - ExB effects //
26// - Gas gain including fluctuations //
27// - Pad-response (simple Gaussian approximation) //
abaf1f1d 28// - Time-response //
f7336fa3 29// - Electronics noise //
30// - Electronics gain //
31// - Digitization //
32// - ADC threshold //
33// The corresponding parameter can be adjusted via the various //
34// Set-functions. If these parameters are not explicitly set, default //
35// values are used (see Init-function). //
abaf1f1d 36// As an example on how to use this class to produce digits from hits //
37// have a look at the macro hits2digits.C //
38// The production of summable digits is demonstrated in hits2sdigits.C //
39// and the subsequent conversion of the s-digits into normal digits is //
40// explained in sdigits2digits.C. //
f7336fa3 41// //
42///////////////////////////////////////////////////////////////////////////////
43
6798b56e 44#include <stdlib.h>
45
f7336fa3 46#include <TMath.h>
47#include <TVector.h>
48#include <TRandom.h>
94de3818 49#include <TROOT.h>
50#include <TTree.h>
793ff80c 51#include <TFile.h>
52#include <TF1.h>
abaf1f1d 53#include <TList.h>
85cbec76 54#include <TTask.h>
793ff80c 55
56#include "AliRun.h"
88cb7938 57#include "AliRunLoader.h"
58#include "AliLoader.h"
59#include "AliConfig.h"
db30bf0f 60#include "AliMagF.h"
85cbec76 61#include "AliRunDigitizer.h"
88cb7938 62#include "AliRunLoader.h"
63#include "AliLoader.h"
f7336fa3 64
65#include "AliTRD.h"
793ff80c 66#include "AliTRDhit.h"
f7336fa3 67#include "AliTRDdigitizer.h"
da581aea 68#include "AliTRDdataArrayI.h"
69#include "AliTRDdataArrayF.h"
793ff80c 70#include "AliTRDsegmentArray.h"
da581aea 71#include "AliTRDdigitsManager.h"
793ff80c 72#include "AliTRDgeometry.h"
4487dad0 73#include "AliTRDparameter.h"
f7336fa3 74
75ClassImp(AliTRDdigitizer)
76
77//_____________________________________________________________________________
85cbec76 78AliTRDdigitizer::AliTRDdigitizer()
f7336fa3 79{
80 //
81 // AliTRDdigitizer default constructor
82 //
83
85a5290f 84 fRunLoader = 0;
4487dad0 85 fDigitsManager = 0;
4487dad0 86 fSDigitsManager = 0;
85a5290f 87 fSDigitsManagerList = 0;
4487dad0 88 fTRD = 0;
89 fGeo = 0;
90 fPar = 0;
abaf1f1d 91 fEvent = 0;
85a5290f 92 fMasks = 0;
abaf1f1d 93 fCompress = kTRUE;
e23fbb27 94 fDebug = 0;
abaf1f1d 95 fSDigits = kFALSE;
96 fSDigitsScale = 0.0;
4487dad0 97 fMergeSignalOnly = kFALSE;
0c24ba98 98 fSimpleSim = kFALSE;
99 fSimpleDet = 0;
100
f7336fa3 101}
102
103//_____________________________________________________________________________
104AliTRDdigitizer::AliTRDdigitizer(const Text_t *name, const Text_t *title)
85cbec76 105 :AliDigitizer(name,title)
f7336fa3 106{
107 //
85cbec76 108 // AliTRDdigitizer constructor
109 //
110
85a5290f 111 fRunLoader = 0;
88cb7938 112
113 //NewIO: These data members probably are not needed anymore
4487dad0 114 fDigitsManager = 0;
4487dad0 115 fSDigitsManager = 0;
85a5290f 116 fSDigitsManagerList = 0;
4487dad0 117 fTRD = 0;
118 fGeo = 0;
119 fPar = 0;
88cb7938 120 //End NewIO comment
85cbec76 121 fEvent = 0;
85a5290f 122 fMasks = 0;
85cbec76 123 fCompress = kTRUE;
e23fbb27 124 fDebug = 0;
85cbec76 125 fSDigits = kFALSE;
85a5290f 126 fSDigitsScale = 100.; // For the summable digits
4487dad0 127 fMergeSignalOnly = kFALSE;
0c24ba98 128 fSimpleSim = kFALSE;
129 fSimpleDet = 0;
130
85cbec76 131
132}
133
134//_____________________________________________________________________________
135AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager
136 , const Text_t *name, const Text_t *title)
137 :AliDigitizer(manager,name,title)
138{
139 //
140 // AliTRDdigitizer constructor
f7336fa3 141 //
142
85a5290f 143 fRunLoader = 0;
4487dad0 144 fDigitsManager = 0;
4487dad0 145 fSDigitsManager = 0;
85a5290f 146 fSDigitsManagerList = 0;
4487dad0 147 fTRD = 0;
148 fGeo = 0;
149 fPar = 0;
bfc40adc 150 fEvent = 0;
85a5290f 151 fMasks = 0;
bfc40adc 152 fCompress = kTRUE;
153 fDebug = 0;
154 fSDigits = kFALSE;
85a5290f 155 fSDigitsScale = 100.; // For the summable digits
4487dad0 156 fMergeSignalOnly = kFALSE;
0c24ba98 157 fSimpleSim = kFALSE;
158 fSimpleDet = 0;
159
bfc40adc 160
161}
162
163//_____________________________________________________________________________
164AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager)
165 :AliDigitizer(manager,"AliTRDdigitizer","TRD digitizer")
166{
167 //
168 // AliTRDdigitizer constructor
169 //
170
88cb7938 171
85a5290f 172 fRunLoader = 0;
4487dad0 173 fDigitsManager = 0;
4487dad0 174 fSDigitsManager = 0;
85a5290f 175 fSDigitsManagerList = 0;
4487dad0 176 fTRD = 0;
177 fGeo = 0;
178 fPar = 0;
abaf1f1d 179 fEvent = 0;
85a5290f 180 fMasks = 0;
abaf1f1d 181 fCompress = kTRUE;
e23fbb27 182 fDebug = 0;
abaf1f1d 183 fSDigits = kFALSE;
85a5290f 184 fSDigitsScale = 100.; // For the summable digits
4487dad0 185 fMergeSignalOnly = kFALSE;
0c24ba98 186 fSimpleSim = kFALSE;
187 fSimpleDet = 0;
793ff80c 188
f7336fa3 189
190}
191
8230f242 192//_____________________________________________________________________________
73ae7b59 193AliTRDdigitizer::AliTRDdigitizer(const AliTRDdigitizer &d):AliDigitizer(d)
8230f242 194{
195 //
196 // AliTRDdigitizer copy constructor
197 //
198
dd9a6ee3 199 ((AliTRDdigitizer &) d).Copy(*this);
8230f242 200
201}
202
f7336fa3 203//_____________________________________________________________________________
204AliTRDdigitizer::~AliTRDdigitizer()
205{
8230f242 206 //
207 // AliTRDdigitizer destructor
208 //
f7336fa3 209
abaf1f1d 210
211 if (fDigitsManager) {
212 delete fDigitsManager;
4487dad0 213 fDigitsManager = 0;
f7336fa3 214 }
215
47b5729a 216 fSDigitsManager = 0;
f7336fa3 217
abaf1f1d 218 if (fSDigitsManagerList) {
47b5729a 219 fSDigitsManagerList->Delete();
abaf1f1d 220 delete fSDigitsManagerList;
4487dad0 221 fSDigitsManagerList = 0;
e23fbb27 222 }
223
224 if (fMasks) {
225 delete [] fMasks;
226 fMasks = 0;
227 }
228
f7336fa3 229}
230
8230f242 231//_____________________________________________________________________________
dd9a6ee3 232AliTRDdigitizer &AliTRDdigitizer::operator=(const AliTRDdigitizer &d)
233{
234 //
235 // Assignment operator
236 //
237
238 if (this != &d) ((AliTRDdigitizer &) d).Copy(*this);
239 return *this;
240
241}
242
243//_____________________________________________________________________________
244void AliTRDdigitizer::Copy(TObject &d)
8230f242 245{
246 //
247 // Copy function
248 //
249
85a5290f 250 ((AliTRDdigitizer &) d).fRunLoader = 0;
4487dad0 251 ((AliTRDdigitizer &) d).fDigitsManager = 0;
85a5290f 252 ((AliTRDdigitizer &) d).fSDigitsManager = 0;
253 ((AliTRDdigitizer &) d).fSDigitsManagerList = 0;
4487dad0 254 ((AliTRDdigitizer &) d).fTRD = 0;
255 ((AliTRDdigitizer &) d).fGeo = 0;
4487dad0 256 ((AliTRDdigitizer &) d).fPar = 0;
85a5290f 257 ((AliTRDdigitizer &) d).fEvent = 0;
258 ((AliTRDdigitizer &) d).fMasks = 0;
abaf1f1d 259 ((AliTRDdigitizer &) d).fCompress = fCompress;
e23fbb27 260 ((AliTRDdigitizer &) d).fDebug = fDebug ;
abaf1f1d 261 ((AliTRDdigitizer &) d).fSDigits = fSDigits;
262 ((AliTRDdigitizer &) d).fSDigitsScale = fSDigitsScale;
4487dad0 263 ((AliTRDdigitizer &) d).fMergeSignalOnly = fMergeSignalOnly;
0c24ba98 264 ((AliTRDdigitizer &) d).fSimpleSim = fSimpleSim;
265 ((AliTRDdigitizer &) d).fSimpleDet = fSimpleDet;
266
e23fbb27 267}
268
e23fbb27 269//_____________________________________________________________________________
270void AliTRDdigitizer::Exec(Option_t* option)
271{
272 //
273 // Executes the merging
274 //
275
276 Int_t iInput;
277
278 AliTRDdigitsManager *sdigitsManager;
279
280 TString optionString = option;
281 if (optionString.Contains("deb")) {
282 fDebug = 1;
283 if (optionString.Contains("2")) {
284 fDebug = 2;
285 }
286 printf("<AliTRDdigitizer::Exec> ");
287 printf("Called with debug option %d\n",fDebug);
288 }
289
cec4059b 290 // The AliRoot file is already connected by the manager
88cb7938 291 AliRunLoader* inrl;
292
293 if (gAlice)
294 {
4487dad0 295 if (fDebug > 0) {
296 printf("<AliTRDdigitizer::Exec> ");
297 printf("AliRun object found on file.\n");
298 }
88cb7938 299 }
4487dad0 300 else {
88cb7938 301 inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
302 inrl->LoadgAlice();
303 gAlice = inrl->GetAliRun();
304 if (!gAlice)
305 {
306 printf("<AliTRDdigitizer::Exec> ");
307 printf("Could not find AliRun object.\n");
308 return;
309 }
4487dad0 310 }
cec4059b 311
e23fbb27 312 Int_t nInput = fManager->GetNinputs();
313 fMasks = new Int_t[nInput];
314 for (iInput = 0; iInput < nInput; iInput++) {
315 fMasks[iInput] = fManager->GetMask(iInput);
316 }
e23fbb27 317
318 // Initialization
e23fbb27 319
67c67368 320 AliRunLoader* orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
321 if (InitDetector()) {
322 AliLoader* ogime = orl->GetLoader("TRDLoader");
323
324 TTree* tree = 0;
325 if (fSDigits)
326 {
327 //if we produce SDigits
328 tree = ogime->TreeS();
329 if (!tree)
330 {
331 ogime->MakeTree("S");
332 tree = ogime->TreeS();
333 }
334 }
335 else
336 {//if we produce Digits
337 tree = ogime->TreeD();
338 if (!tree)
339 {
340 ogime->MakeTree("D");
341 tree = ogime->TreeD();
342 }
343 }
344 MakeBranch(tree);
345 }
346
e23fbb27 347 for (iInput = 0; iInput < nInput; iInput++) {
348
349 if (fDebug > 0) {
350 printf("<AliTRDdigitizer::Exec> ");
351 printf("Add input stream %d\n",iInput);
352 }
353
c57e2264 354 // check if the input tree exists
88cb7938 355 inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
356 AliLoader* gime = inrl->GetLoader("TRDLoader");
357
358 TTree * treees = gime->TreeS();
359 if (treees == 0x0)
360 {
361 if (gime->LoadSDigits())
362 {
363 Error("Exec","Error Occured while loading S. Digits for input %d.",iInput);
364 return;
365 }
366 treees = gime->TreeS();
367 }
368
369 if (treees == 0x0) {
c57e2264 370 printf("<AliTRDdigitizer::Exec> ");
371 printf("Input stream %d does not exist\n",iInput);
372 return;
373 }
374
e23fbb27 375 // Read the s-digits via digits manager
376 sdigitsManager = new AliTRDdigitsManager();
377 sdigitsManager->SetDebug(fDebug);
378 sdigitsManager->SetSDigits(kTRUE);
88cb7938 379
380 AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
381 AliLoader* gimme = rl->GetLoader("TRDLoader");
382 if (!gimme->TreeS()) gimme->LoadSDigits();
383 sdigitsManager->ReadDigits(gimme->TreeS());
e23fbb27 384
385 // Add the s-digits to the input list
386 AddSDigitsManager(sdigitsManager);
387
388 }
389
390 // Convert the s-digits to normal digits
391 if (fDebug > 0) {
392 printf("<AliTRDdigitizer::Exec> ");
393 printf("Do the conversion\n");
394 }
395 SDigits2Digits();
396
397 // Store the digits
398 if (fDebug > 0) {
399 printf("<AliTRDdigitizer::Exec> ");
400 printf("Write the digits\n");
401 }
88cb7938 402
e23fbb27 403 fDigitsManager->WriteDigits();
88cb7938 404
67c67368 405 //Write parameters
406 orl->CdGAFile();
bdbb05bb 407 if (!gFile->Get("TRDparameter")) GetParameter()->Write();
67c67368 408
e23fbb27 409 if (fDebug > 0) {
410 printf("<AliTRDdigitizer::Exec> ");
411 printf("Done\n");
412 }
413
b1113c6b 414 DeleteSDigitsManager();
415
e23fbb27 416}
417
f7336fa3 418//_____________________________________________________________________________
e23fbb27 419Bool_t AliTRDdigitizer::Open(const Char_t *file, Int_t nEvent)
f7336fa3 420{
421 //
422 // Opens a ROOT-file with TRD-hits and reads in the hit-tree
423 //
424
425 // Connect the AliRoot file containing Geometry, Kine, and Hits
88cb7938 426
f540341d 427
e191bb57 428 TString evfoldname = AliConfig::GetDefaultEventFolderName();
f540341d 429 fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
430 if (!fRunLoader)
e191bb57 431 fRunLoader = AliRunLoader::Open(file,AliConfig::GetDefaultEventFolderName(),
f540341d 432 "UPDATE");
88cb7938 433
434 if (!fRunLoader)
435 {
436 Error("Open","Can not open session for file %s.",file);
437 return kFALSE;
438 }
439
f540341d 440 if (!fRunLoader->GetAliRun()) fRunLoader->LoadgAlice();
88cb7938 441 gAlice = fRunLoader->GetAliRun();
442
da581aea 443 if (gAlice) {
e23fbb27 444 if (fDebug > 0) {
445 printf("<AliTRDdigitizer::Open> ");
abaf1f1d 446 printf("AliRun object found on file.\n");
447 }
da581aea 448 }
449 else {
e23fbb27 450 printf("<AliTRDdigitizer::Open> ");
da581aea 451 printf("Could not find AliRun object.\n");
452 return kFALSE;
453 }
f7336fa3 454
455 fEvent = nEvent;
456
457 // Import the Trees for the event nEvent in the file
88cb7938 458 fRunLoader->GetEvent(fEvent);
459
460 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
461 if (!loader)
462 {
463 Error("Open","Can not get TRD loader from Run Loader");
464 return kFALSE;
465 }
466
abaf1f1d 467 if (InitDetector()) {
88cb7938 468 TTree* tree = 0;
469 if (fSDigits)
470 {
471 //if we produce SDigits
472 tree = loader->TreeS();
473 if (!tree)
474 {
475 loader->MakeTree("S");
476 tree = loader->TreeS();
477 }
478 }
479 else
480 {//if we produce Digits
481 tree = loader->TreeD();
482 if (!tree)
483 {
484 loader->MakeTree("D");
485 tree = loader->TreeD();
486 }
487 }
488 return MakeBranch(tree);
abaf1f1d 489 }
490 else {
491 return kFALSE;
492 }
793ff80c 493
494}
495
496//_____________________________________________________________________________
497Bool_t AliTRDdigitizer::InitDetector()
498{
499 //
500 // Sets the pointer to the TRD detector and the geometry
501 //
502
dd9a6ee3 503 // Get the pointer to the detector class and check for version 1
4487dad0 504 fTRD = (AliTRD *) gAlice->GetDetector("TRD");
cec4059b 505 if (!fTRD) {
506 printf("<AliTRDdigitizer::InitDetector> ");
507 printf("No TRD module found\n");
508 exit(1);
509 }
dd9a6ee3 510 if (fTRD->IsVersion() != 1) {
e23fbb27 511 printf("<AliTRDdigitizer::InitDetector> ");
dd9a6ee3 512 printf("TRD must be version 1 (slow simulator).\n");
513 exit(1);
514 }
515
516 // Get the geometry
517 fGeo = fTRD->GetGeometry();
e23fbb27 518 if (fDebug > 0) {
519 printf("<AliTRDdigitizer::InitDetector> ");
abaf1f1d 520 printf("Geometry version %d\n",fGeo->IsVersion());
521 }
dd9a6ee3 522
abaf1f1d 523 // Create a digits manager
47b5729a 524 delete fDigitsManager;
abaf1f1d 525 fDigitsManager = new AliTRDdigitsManager();
526 fDigitsManager->SetSDigits(fSDigits);
527 fDigitsManager->CreateArrays();
528 fDigitsManager->SetEvent(fEvent);
e23fbb27 529 fDigitsManager->SetDebug(fDebug);
abaf1f1d 530
531 // The list for the input s-digits manager to be merged
47b5729a 532 if (fSDigitsManagerList) {
533 fSDigitsManagerList->Delete();
534 } else {
535 fSDigitsManagerList = new TList();
536 }
abaf1f1d 537
4487dad0 538 return kTRUE;
f7336fa3 539
540}
541
6244debe 542//_____________________________________________________________________________
88cb7938 543Bool_t AliTRDdigitizer::MakeBranch(TTree* tree) const
6244debe 544{
abaf1f1d 545 //
546 // Create the branches for the digits array
6244debe 547 //
548
88cb7938 549 return fDigitsManager->MakeBranch(tree);
6244debe 550
551}
552
f7336fa3 553//_____________________________________________________________________________
554Bool_t AliTRDdigitizer::MakeDigits()
555{
556 //
872a7aba 557 // Creates digits.
f7336fa3 558 //
559
f7336fa3 560 ///////////////////////////////////////////////////////////////
561 // Parameter
562 ///////////////////////////////////////////////////////////////
563
564 // Converts number of electrons to fC
872a7aba 565 const Double_t kEl2fC = 1.602E-19 * 1.0E15;
f7336fa3 566
567 ///////////////////////////////////////////////////////////////
568
793ff80c 569 // Number of pads included in the pad response
570 const Int_t kNpad = 3;
571
572 // Number of track dictionary arrays
dd56b762 573 const Int_t kNDict = AliTRDdigitsManager::kNDict;
793ff80c 574
872a7aba 575 // Half the width of the amplification region
576 const Float_t kAmWidth = AliTRDgeometry::AmThick() / 2.;
577
c3a4830f 578 Int_t iRow, iCol, iTime, iPad;
71d9fa7b 579 Int_t iDict = 0;
793ff80c 580 Int_t nBytes = 0;
f7336fa3 581
582 Int_t totalSizeDigits = 0;
583 Int_t totalSizeDict0 = 0;
584 Int_t totalSizeDict1 = 0;
585 Int_t totalSizeDict2 = 0;
586
872a7aba 587 Int_t timeTRDbeg = 0;
588 Int_t timeTRDend = 1;
589
590 Float_t pos[3];
591 Float_t rot[3];
592 Float_t xyz[3];
593 Float_t padSignal[kNpad];
594 Float_t signalOld[kNpad];
595
793ff80c 596 AliTRDdataArrayF *signals = 0;
597 AliTRDdataArrayI *digits = 0;
8230f242 598 AliTRDdataArrayI *dictionary[kNDict];
da581aea 599
4487dad0 600 // Create a default parameter class if none is defined
601 if (!fPar) {
602 fPar = new AliTRDparameter("TRDparameter","Standard TRD parameter");
603 if (fDebug > 0) {
604 printf("<AliTRDdigitizer::MakeDigits> ");
605 printf("Create the default parameter object\n");
606 }
607 }
608
793ff80c 609 // Create a container for the amplitudes
610 AliTRDsegmentArray *signalsArray
e23fbb27 611 = new AliTRDsegmentArray("AliTRDdataArrayF"
612 ,AliTRDgeometry::Ndet());
793ff80c 613
4487dad0 614 if (fPar->TRFOn()) {
615 timeTRDbeg = ((Int_t) (-fPar->GetTRFlo() / fPar->GetTimeBinSize())) - 1;
616 timeTRDend = ((Int_t) ( fPar->GetTRFhi() / fPar->GetTimeBinSize())) - 1;
e23fbb27 617 if (fDebug > 0) {
618 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 619 printf("Sample the TRF between -%d and %d\n",timeTRDbeg,timeTRDend);
620 }
da581aea 621 }
622
4487dad0 623 Float_t elAttachProp = fPar->GetElAttachProp() / 100.;
793ff80c 624
872a7aba 625 if (!fGeo) {
e23fbb27 626 printf("<AliTRDdigitizer::MakeDigits> ");
872a7aba 627 printf("No geometry defined\n");
628 return kFALSE;
629 }
630
e23fbb27 631 if (fDebug > 0) {
632 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 633 printf("Start creating digits.\n");
634 }
872a7aba 635
88cb7938 636 AliLoader* gimme = fRunLoader->GetLoader("TRDLoader");
637 if (!gimme->TreeH()) gimme->LoadHits();
638 TTree* hitTree = gimme->TreeH();
639 if (hitTree == 0x0)
640 {
641 Error("MakeDigits","Can not get TreeH");
642 return kFALSE;
643 }
644 fTRD->SetTreeAddress();
645
793ff80c 646 // Get the number of entries in the hit tree
647 // (Number of primary particles creating a hit somewhere)
0c24ba98 648 Int_t nTrack = 1;
649 if (!fSimpleSim) {
650 nTrack = (Int_t) hitTree->GetEntries();
651 if (fDebug > 0) {
652 printf("<AliTRDdigitizer::MakeDigits> ");
653 printf("Found %d primary particles\n",nTrack);
654 }
655 }
793ff80c 656
657 Int_t detectorOld = -1;
658 Int_t countHits = 0;
659
660 // Loop through all entries in the tree
661 for (Int_t iTrack = 0; iTrack < nTrack; iTrack++) {
662
0c24ba98 663 if (!fSimpleSim) {
664 gAlice->ResetHits();
665 nBytes += hitTree->GetEvent(iTrack);
666 }
793ff80c 667
e23fbb27 668 // Loop through the TRD hits
669 Int_t iHit = 0;
670 AliTRDhit *hit = (AliTRDhit *) fTRD->FirstHit(-1);
671 while (hit) {
672
793ff80c 673 countHits++;
e23fbb27 674 iHit++;
675
676 pos[0] = hit->X();
677 pos[1] = hit->Y();
678 pos[2] = hit->Z();
679 Float_t q = hit->GetCharge();
680 Int_t track = hit->Track();
681 Int_t detector = hit->GetDetector();
682 Int_t plane = fGeo->GetPlane(detector);
683 Int_t sector = fGeo->GetSector(detector);
684 Int_t chamber = fGeo->GetChamber(detector);
4487dad0 685 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
686 Int_t nColMax = fPar->GetColMax(plane);
687 Int_t nTimeMax = fPar->GetTimeMax();
688 Int_t nTimeBefore = fPar->GetTimeBefore();
689 Int_t nTimeAfter = fPar->GetTimeAfter();
690 Int_t nTimeTotal = fPar->GetTimeTotal();
691 Float_t row0 = fPar->GetRow0(plane,chamber,sector);
692 Float_t col0 = fPar->GetCol0(plane);
693 Float_t time0 = fPar->GetTime0(plane);
694 Float_t rowPadSize = fPar->GetRowPadSize(plane,chamber,sector);
695 Float_t colPadSize = fPar->GetColPadSize(plane);
696 Float_t timeBinSize = fPar->GetTimeBinSize();
872a7aba 697 Float_t divideRow = 1.0 / rowPadSize;
698 Float_t divideCol = 1.0 / colPadSize;
699 Float_t divideTime = 1.0 / timeBinSize;
793ff80c 700
e23fbb27 701 if (fDebug > 1) {
793ff80c 702 printf("Analyze hit no. %d ",iHit);
703 printf("-----------------------------------------------------------\n");
704 hit->Dump();
705 printf("plane = %d, sector = %d, chamber = %d\n"
706 ,plane,sector,chamber);
707 printf("nRowMax = %d, nColMax = %d, nTimeMax = %d\n"
708 ,nRowMax,nColMax,nTimeMax);
872a7aba 709 printf("nTimeBefore = %d, nTimeAfter = %d, nTimeTotal = %d\n"
710 ,nTimeBefore,nTimeAfter,nTimeTotal);
793ff80c 711 printf("row0 = %f, col0 = %f, time0 = %f\n"
712 ,row0,col0,time0);
c1e4b257 713 printf("rowPadSize = %f, colPadSize = %f, timeBinSize = %f\n"
714 ,rowPadSize,colPadSize,timeBinSize);
793ff80c 715 }
716
e23fbb27 717 // Don't analyze test hits and switched off detectors
718 if ((CheckDetector(plane,chamber,sector)) &&
719 (((Int_t) q) != 0)) {
dd56b762 720
e23fbb27 721 if (detector != detectorOld) {
e153aaf6 722
e23fbb27 723 if (fDebug > 1) {
724 printf("<AliTRDdigitizer::MakeDigits> ");
725 printf("Get new container. New det = %d, Old det = %d\n"
726 ,detector,detectorOld);
dd9a6ee3 727 }
e23fbb27 728 // Compress the old one if enabled
729 if ((fCompress) && (detectorOld > -1)) {
730 if (fDebug > 1) {
731 printf("<AliTRDdigitizer::MakeDigits> ");
732 printf("Compress the old container ...");
733 }
734 signals->Compress(1,0);
735 for (iDict = 0; iDict < kNDict; iDict++) {
736 dictionary[iDict]->Compress(1,0);
c1e4b257 737 }
e23fbb27 738 if (fDebug > 1) printf("done\n");
793ff80c 739 }
e23fbb27 740 // Get the new container
741 signals = (AliTRDdataArrayF *) signalsArray->At(detector);
742 if (signals->GetNtime() == 0) {
743 // Allocate a new one if not yet existing
744 if (fDebug > 1) {
745 printf("<AliTRDdigitizer::MakeDigits> ");
746 printf("Allocate a new container ... ");
747 }
748 signals->Allocate(nRowMax,nColMax,nTimeTotal);
793ff80c 749 }
0c24ba98 750 else if (fSimpleSim) {
751 // Clear an old one for the simple simulation
752 if (fDebug > 1) {
753 printf("<AliTRDdigitizer::MakeDigits> ");
754 printf("Clear a old container ... ");
755 }
756 signals->Clear();
757 }
793ff80c 758 else {
e23fbb27 759 // Expand an existing one
760 if (fCompress) {
761 if (fDebug > 1) {
762 printf("<AliTRDdigitizer::MakeDigits> ");
763 printf("Expand an existing container ... ");
764 }
765 signals->Expand();
766 }
793ff80c 767 }
e23fbb27 768 // The same for the dictionary
0c24ba98 769 if (!fSimpleSim) {
770 for (iDict = 0; iDict < kNDict; iDict++) {
771 dictionary[iDict] = fDigitsManager->GetDictionary(detector,iDict);
772 if (dictionary[iDict]->GetNtime() == 0) {
773 dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
774 }
775 else {
776 if (fCompress) dictionary[iDict]->Expand();
777 }
e23fbb27 778 }
779 }
780 if (fDebug > 1) printf("done\n");
781 detectorOld = detector;
793ff80c 782 }
783
e23fbb27 784 // Rotate the sectors on top of each other
0c24ba98 785 if (fSimpleSim) {
786 rot[0] = pos[0];
787 rot[1] = pos[1];
788 rot[2] = pos[2];
789 }
790 else {
791 fGeo->Rotate(detector,pos,rot);
792 }
e23fbb27 793
794 // The driftlength. It is negative if the hit is in the
795 // amplification region.
796 Float_t driftlength = time0 - rot[0];
797
798 // Take also the drift in the amplification region into account
799 // The drift length is at the moment still the same, regardless of
800 // the position relativ to the wire. This non-isochronity needs still
801 // to be implemented.
802 Float_t driftlengthL = TMath::Abs(driftlength + kAmWidth);
4487dad0 803 if (fPar->ExBOn()) driftlengthL /= TMath::Sqrt(fPar->GetLorentzFactor());
e23fbb27 804
805 // Loop over all electrons of this hit
806 // TR photons produce hits with negative charge
807 Int_t nEl = ((Int_t) TMath::Abs(q));
808 for (Int_t iEl = 0; iEl < nEl; iEl++) {
809
810 xyz[0] = rot[0];
811 xyz[1] = rot[1];
812 xyz[2] = rot[2];
813
814 // Electron attachment
4487dad0 815 if (fPar->ElAttachOn()) {
e23fbb27 816 if (gRandom->Rndm() < (driftlengthL * elAttachProp))
817 continue;
818 }
819
820 // Apply the diffusion smearing
4487dad0 821 if (fPar->DiffusionOn()) {
822 if (!(fPar->Diffusion(driftlengthL,xyz))) continue;
e23fbb27 823 }
f7336fa3 824
e23fbb27 825 // Apply E x B effects (depends on drift direction)
4487dad0 826 if (fPar->ExBOn()) {
827 if (!(fPar->ExB(driftlength+kAmWidth,xyz))) continue;
e23fbb27 828 }
793ff80c 829
e23fbb27 830 // The electron position after diffusion and ExB in pad coordinates
831 // The pad row (z-direction)
832 Float_t rowDist = xyz[2] - row0;
833 Int_t rowE = ((Int_t) (rowDist * divideRow));
834 if ((rowE < 0) || (rowE >= nRowMax)) continue;
835 Float_t rowOffset = ((((Float_t) rowE) + 0.5) * rowPadSize) - rowDist;
836
837 // The pad column (rphi-direction)
4487dad0 838 Float_t col0tilt = fPar->Col0Tilted(col0,rowOffset,plane);
e23fbb27 839 Float_t colDist = xyz[1] - col0tilt;
840 Int_t colE = ((Int_t) (colDist * divideCol));
841 if ((colE < 0) || (colE >= nColMax)) continue;
842 Float_t colOffset = ((((Float_t) colE) + 0.5) * colPadSize) - colDist;
843
844 // The time bin (negative for hits in the amplification region)
845 // In the amplification region the electrons drift from both sides
846 // to the middle (anode wire plane)
847 Float_t timeDist = time0 - xyz[0];
848 Float_t timeOffset = 0;
849 Int_t timeE = 0;
850 if (timeDist > 0) {
851 // The time bin
852 timeE = ((Int_t) (timeDist * divideTime));
853 // The distance of the position to the middle of the timebin
854 timeOffset = ((((Float_t) timeE) + 0.5) * timeBinSize) - timeDist;
855 }
856 else {
857 // Difference between half of the amplification gap width and
858 // the distance to the anode wire
859 Float_t anodeDist = kAmWidth - TMath::Abs(timeDist + kAmWidth);
860 // The time bin
861 timeE = -1 * (((Int_t ) (anodeDist * divideTime)) + 1);
862 // The distance of the position to the middle of the timebin
863 timeOffset = ((((Float_t) timeE) + 0.5) * timeBinSize) + anodeDist;
864 }
872a7aba 865
e23fbb27 866 // Apply the gas gain including fluctuations
867 Float_t ggRndm = 0.0;
868 do {
869 ggRndm = gRandom->Rndm();
870 } while (ggRndm <= 0);
4487dad0 871 Int_t signal = (Int_t) (-fPar->GetGasGain() * TMath::Log(ggRndm));
e23fbb27 872
873 // Apply the pad response
4487dad0 874 if (fPar->PRFOn()) {
e23fbb27 875 // The distance of the electron to the center of the pad
876 // in units of pad width
877 Float_t dist = - colOffset * divideCol;
4487dad0 878 if (!(fPar->PadResponse(signal,dist,plane,padSignal))) continue;
793ff80c 879 }
e23fbb27 880 else {
881 padSignal[0] = 0.0;
882 padSignal[1] = signal;
883 padSignal[2] = 0.0;
884 }
885
886 // Sample the time response inside the drift region
887 // + additional time bins before and after.
888 // The sampling is done always in the middle of the time bin
889 for (Int_t iTimeBin = TMath::Max(timeE-timeTRDbeg, -nTimeBefore)
890 ;iTimeBin < TMath::Min(timeE+timeTRDend,nTimeMax+nTimeAfter )
891 ;iTimeBin++) {
892
893 // Apply the time response
894 Float_t timeResponse = 1.0;
895 Float_t crossTalk = 0.0;
896 Float_t time = (iTimeBin - timeE) * timeBinSize + timeOffset;
4487dad0 897 if (fPar->TRFOn()) {
898 timeResponse = fPar->TimeResponse(time);
e23fbb27 899 }
4487dad0 900 if (fPar->CTOn()) {
901 crossTalk = fPar->CrossTalk(time);
e23fbb27 902 }
903
904 signalOld[0] = 0.0;
905 signalOld[1] = 0.0;
906 signalOld[2] = 0.0;
f7336fa3 907
e23fbb27 908 for (iPad = 0; iPad < kNpad; iPad++) {
909
910 Int_t colPos = colE + iPad - 1;
911 if (colPos < 0) continue;
912 if (colPos >= nColMax) break;
913
914 // Add the signals
915 // Note: The time bin number is shifted by nTimeBefore to avoid negative
916 // time bins. This has to be subtracted later.
917 Int_t iCurrentTimeBin = iTimeBin + nTimeBefore;
918 signalOld[iPad] = signals->GetDataUnchecked(rowE,colPos,iCurrentTimeBin);
919 if( colPos != colE ) {
920 signalOld[iPad] += padSignal[iPad] * (timeResponse + crossTalk);
921 }
922 else {
923 signalOld[iPad] += padSignal[iPad] * timeResponse;
924 }
925 signals->SetDataUnchecked(rowE,colPos,iCurrentTimeBin,signalOld[iPad]);
926
927 // Store the track index in the dictionary
928 // Note: We store index+1 in order to allow the array to be compressed
0c24ba98 929 if ((signalOld[iPad] > 0) && (!fSimpleSim)) {
e23fbb27 930 for (iDict = 0; iDict < kNDict; iDict++) {
931 Int_t oldTrack = dictionary[iDict]->GetDataUnchecked(rowE
932 ,colPos
933 ,iCurrentTimeBin);
934 if (oldTrack == track+1) break;
935 if (oldTrack == 0) {
936 dictionary[iDict]->SetDataUnchecked(rowE,colPos,iCurrentTimeBin,track+1);
937 break;
938 }
71d9fa7b 939 }
940 }
872a7aba 941
e23fbb27 942 } // Loop: pads
f7336fa3 943
e23fbb27 944 } // Loop: time bins
f7336fa3 945
e23fbb27 946 } // Loop: electrons of a single hit
f7336fa3 947
e23fbb27 948 } // If: detector and test hit
f7336fa3 949
e23fbb27 950 hit = (AliTRDhit *) fTRD->NextHit();
f7336fa3 951
e23fbb27 952 } // Loop: hits of one primary track
953
954 } // Loop: primary tracks
955
956 if (fDebug > 0) {
957 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 958 printf("Finished analyzing %d hits\n",countHits);
959 }
793ff80c 960
0c24ba98 961 // The coupling factor
962 Float_t coupling = fPar->GetPadCoupling()
963 * fPar->GetTimeCoupling();
964
965 // The conversion factor
966 Float_t convert = kEl2fC
967 * fPar->GetChipGain();
6244debe 968
793ff80c 969 // Loop through all chambers to finalize the digits
0c24ba98 970 Int_t iDetBeg = 0;
971 Int_t iDetEnd = AliTRDgeometry::Ndet();
972 if (fSimpleSim) {
973 iDetBeg = fSimpleDet;
974 iDetEnd = iDetBeg + 1;
975 }
976 for (Int_t iDet = iDetBeg; iDet < iDetEnd; iDet++) {
793ff80c 977
872a7aba 978 Int_t plane = fGeo->GetPlane(iDet);
979 Int_t sector = fGeo->GetSector(iDet);
980 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 981 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
982 Int_t nColMax = fPar->GetColMax(plane);
983 Int_t nTimeMax = fPar->GetTimeMax();
984 Int_t nTimeTotal = fPar->GetTimeTotal();
793ff80c 985
e23fbb27 986 Double_t *inADC = new Double_t[nTimeTotal];
987 Double_t *outADC = new Double_t[nTimeTotal];
988
989 if (fDebug > 0) {
990 printf("<AliTRDdigitizer::MakeDigits> ");
793ff80c 991 printf("Digitization for chamber %d\n",iDet);
992 }
da581aea 993
793ff80c 994 // Add a container for the digits of this detector
abaf1f1d 995 digits = fDigitsManager->GetDigits(iDet);
793ff80c 996 // Allocate memory space for the digits buffer
0c24ba98 997 if (digits->GetNtime() == 0) {
998 digits->Allocate(nRowMax,nColMax,nTimeTotal);
999 }
1000 else if (fSimpleSim) {
1001 digits->Clear();
1002 }
1003
793ff80c 1004 // Get the signal container
1005 signals = (AliTRDdataArrayF *) signalsArray->At(iDet);
1006 if (signals->GetNtime() == 0) {
1007 // Create missing containers
872a7aba 1008 signals->Allocate(nRowMax,nColMax,nTimeTotal);
793ff80c 1009 }
1010 else {
1011 // Expand the container if neccessary
1012 if (fCompress) signals->Expand();
1013 }
1014 // Create the missing dictionary containers
0c24ba98 1015 if (!fSimpleSim) {
1016 for (iDict = 0; iDict < kNDict; iDict++) {
1017 dictionary[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
1018 if (dictionary[iDict]->GetNtime() == 0) {
1019 dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
1020 }
1021 }
793ff80c 1022 }
f7336fa3 1023
793ff80c 1024 Int_t nDigits = 0;
1025
6244debe 1026 // Don't create noise in detectors that are switched off
1027 if (CheckDetector(plane,chamber,sector)) {
1028
1029 // Create the digits for this chamber
872a7aba 1030 for (iRow = 0; iRow < nRowMax; iRow++ ) {
1031 for (iCol = 0; iCol < nColMax; iCol++ ) {
6244debe 1032
e23fbb27 1033 // Create summable digits
1034 if (fSDigits) {
6244debe 1035
e23fbb27 1036 for (iTime = 0; iTime < nTimeTotal; iTime++) {
872a7aba 1037 Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
abaf1f1d 1038 signalAmp *= fSDigitsScale;
e23fbb27 1039 signalAmp = TMath::Min(signalAmp,(Float_t) 1.0e9);
abaf1f1d 1040 Int_t adc = (Int_t) signalAmp;
4487dad0 1041 if (adc > 0) nDigits++;
872a7aba 1042 digits->SetDataUnchecked(iRow,iCol,iTime,adc);
c1e4b257 1043 }
6244debe 1044
e23fbb27 1045 }
1046 // Create normal digits
1047 else {
6244debe 1048
e23fbb27 1049 for (iTime = 0; iTime < nTimeTotal; iTime++) {
1050 Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
0c24ba98 1051 // Pad and time coupling
1052 signalAmp *= coupling;
7e582e9f 1053 // Add the noise, starting from minus ADC baseline in electrons
1054 Double_t baselineEl = fPar->GetADCbaseline() * (fPar->GetADCinRange()
1055 / fPar->GetADCoutRange())
1056 / convert;
1057 signalAmp = TMath::Max((Double_t) gRandom->Gaus(signalAmp,fPar->GetNoise())
1058 ,-baselineEl);
6244debe 1059 // Convert to mV
1060 signalAmp *= convert;
7e582e9f 1061 // Add ADC baseline in mV
1062 signalAmp += fPar->GetADCbaseline() * (fPar->GetADCinRange()
1063 / fPar->GetADCoutRange());
1064 // Convert to ADC counts. Set the overflow-bit fADCoutRange if the
6244debe 1065 // signal is larger than fADCinRange
1066 Int_t adc = 0;
4487dad0 1067 if (signalAmp >= fPar->GetADCinRange()) {
1068 adc = ((Int_t) fPar->GetADCoutRange());
6244debe 1069 }
1070 else {
4487dad0 1071 adc = ((Int_t) (signalAmp * (fPar->GetADCoutRange()
1072 / fPar->GetADCinRange())));
6244debe 1073 }
e23fbb27 1074 inADC[iTime] = adc;
1075 outADC[iTime] = adc;
1076 }
6244debe 1077
e23fbb27 1078 // Apply the tail cancelation via the digital filter
4487dad0 1079 if (fPar->TCOn()) {
1080 DeConvExp(inADC,outADC,nTimeTotal,fPar->GetTCnexp());
e23fbb27 1081 }
1082
1083 for (iTime = 0; iTime < nTimeTotal; iTime++) {
6244debe 1084 // Store the amplitude of the digit if above threshold
4487dad0 1085 if (outADC[iTime] > fPar->GetADCthreshold()) {
e23fbb27 1086 if (fDebug > 2) {
1087 printf(" iRow = %d, iCol = %d, iTime = %d, adc = %f\n"
1088 ,iRow,iCol,iTime,outADC[iTime]);
6244debe 1089 }
1090 nDigits++;
3583ac1a 1091 digits->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
6244debe 1092 }
6244debe 1093 }
1094
1095 }
e23fbb27 1096
6244debe 1097 }
793ff80c 1098 }
6244debe 1099
793ff80c 1100 }
1101
1102 // Compress the arrays
0c24ba98 1103 if (!fSimpleSim) {
1104 digits->Compress(1,0);
1105 for (iDict = 0; iDict < kNDict; iDict++) {
1106 dictionary[iDict]->Compress(1,0);
1107 }
f7336fa3 1108
0c24ba98 1109 totalSizeDigits += digits->GetSize();
1110 totalSizeDict0 += dictionary[0]->GetSize();
1111 totalSizeDict1 += dictionary[1]->GetSize();
1112 totalSizeDict2 += dictionary[2]->GetSize();
f7336fa3 1113
0c24ba98 1114 Float_t nPixel = nRowMax * nColMax * nTimeMax;
1115 if (fDebug > 0) {
1116 printf("<AliTRDdigitizer::MakeDigits> ");
1117 printf("Found %d digits in detector %d (%3.0f).\n"
1118 ,nDigits,iDet
1119 ,100.0 * ((Float_t) nDigits) / nPixel);
1120 }
1121
1122 if (fCompress) signals->Compress(1,0);
abaf1f1d 1123
0c24ba98 1124 }
f7336fa3 1125
e23fbb27 1126 delete [] inADC;
1127 delete [] outADC;
1128
f7336fa3 1129 }
1130
0c24ba98 1131 if (signalsArray) {
1132 delete signalsArray;
1133 signalsArray = 0;
1134 }
1135
e23fbb27 1136 if (fDebug > 0) {
1137 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 1138 printf("Total number of analyzed hits = %d\n",countHits);
0c24ba98 1139 if (!fSimpleSim) {
1140 printf("<AliTRDdigitizer::MakeDigits> ");
1141 printf("Total digits data size = %d, %d, %d, %d\n",totalSizeDigits
1142 ,totalSizeDict0
1143 ,totalSizeDict1
1144 ,totalSizeDict2);
1145 }
abaf1f1d 1146 }
1147
1148 return kTRUE;
1149
1150}
1151
1152//_____________________________________________________________________________
1153void AliTRDdigitizer::AddSDigitsManager(AliTRDdigitsManager *man)
1154{
1155 //
1156 // Add a digits manager for s-digits to the input list.
1157 //
1158
1159 fSDigitsManagerList->Add(man);
1160
1161}
1162
b1113c6b 1163//_____________________________________________________________________________
1164void AliTRDdigitizer::DeleteSDigitsManager()
1165{
1166 //
1167 // Removes digits manager from the input list.
1168 //
1169
1170 fSDigitsManagerList->Delete();
1171
1172}
1173
abaf1f1d 1174//_____________________________________________________________________________
1175Bool_t AliTRDdigitizer::ConvertSDigits()
1176{
1177 //
1178 // Converts s-digits to normal digits
1179 //
1180
1181 // Number of track dictionary arrays
1182 const Int_t kNDict = AliTRDdigitsManager::kNDict;
1183
1184 // Converts number of electrons to fC
1185 const Double_t kEl2fC = 1.602E-19 * 1.0E15;
1186
1187 Int_t iDict = 0;
e23fbb27 1188 Int_t iRow;
1189 Int_t iCol;
1190 Int_t iTime;
abaf1f1d 1191
4487dad0 1192 if (!fPar) {
1193 fPar = new AliTRDparameter("TRDparameter","Standard parameter");
1194 if (fDebug > 0) {
1195 printf("<AliTRDdigitizer::ConvertSDigits> ");
1196 printf("Create the default parameter object\n");
1197 }
abaf1f1d 1198 }
1199
1200 Double_t sDigitsScale = 1.0 / GetSDigitsScale();
4487dad0 1201 Double_t noise = fPar->GetNoise();
1202 Double_t padCoupling = fPar->GetPadCoupling();
1203 Double_t timeCoupling = fPar->GetTimeCoupling();
1204 Double_t chipGain = fPar->GetChipGain();
0c24ba98 1205 Double_t coupling = padCoupling * timeCoupling;
1206 Double_t convert = kEl2fC * chipGain;
4487dad0 1207 Double_t adcInRange = fPar->GetADCinRange();
1208 Double_t adcOutRange = fPar->GetADCoutRange();
1209 Int_t adcThreshold = fPar->GetADCthreshold();
7e582e9f 1210 Int_t adcBaseline = fPar->GetADCbaseline();
abaf1f1d 1211
1212 AliTRDdataArrayI *digitsIn;
1213 AliTRDdataArrayI *digitsOut;
1214 AliTRDdataArrayI *dictionaryIn[kNDict];
1215 AliTRDdataArrayI *dictionaryOut[kNDict];
1216
1217 // Loop through the detectors
1218 for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
da581aea 1219
e23fbb27 1220 if (fDebug > 0) {
1221 printf("<AliTRDdigitizer::ConvertSDigits> ");
abaf1f1d 1222 printf("Convert detector %d to digits.\n",iDet);
1223 }
1224
1225 Int_t plane = fGeo->GetPlane(iDet);
1226 Int_t sector = fGeo->GetSector(iDet);
1227 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 1228 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
1229 Int_t nColMax = fPar->GetColMax(plane);
1230 Int_t nTimeTotal = fPar->GetTimeTotal();
abaf1f1d 1231
e23fbb27 1232 Double_t *inADC = new Double_t[nTimeTotal];
1233 Double_t *outADC = new Double_t[nTimeTotal];
1234
abaf1f1d 1235 digitsIn = fSDigitsManager->GetDigits(iDet);
1236 digitsIn->Expand();
1237 digitsOut = fDigitsManager->GetDigits(iDet);
1238 digitsOut->Allocate(nRowMax,nColMax,nTimeTotal);
1239 for (iDict = 0; iDict < kNDict; iDict++) {
1240 dictionaryIn[iDict] = fSDigitsManager->GetDictionary(iDet,iDict);
1241 dictionaryIn[iDict]->Expand();
1242 dictionaryOut[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
1243 dictionaryOut[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
1244 }
1245
e23fbb27 1246 for (iRow = 0; iRow < nRowMax; iRow++ ) {
1247 for (iCol = 0; iCol < nColMax; iCol++ ) {
abaf1f1d 1248
e23fbb27 1249 for (iTime = 0; iTime < nTimeTotal; iTime++) {
abaf1f1d 1250 Double_t signal = (Double_t) digitsIn->GetDataUnchecked(iRow,iCol,iTime);
1251 signal *= sDigitsScale;
0c24ba98 1252 // Pad and time coupling
1253 signal *= coupling;
7e582e9f 1254 // Add the noise, starting from minus ADC baseline in electrons
1255 Double_t baselineEl = adcBaseline * (adcInRange / adcOutRange) / convert;
1256 signal = TMath::Max((Double_t) gRandom->Gaus(signal,noise),-baselineEl);
abaf1f1d 1257 // Convert to mV
1258 signal *= convert;
7e582e9f 1259 // add ADC baseline in mV
1260 signal += adcBaseline * (adcInRange / adcOutRange);
abaf1f1d 1261 // Convert to ADC counts. Set the overflow-bit adcOutRange if the
1262 // signal is larger than adcInRange
1263 Int_t adc = 0;
1264 if (signal >= adcInRange) {
1265 adc = ((Int_t) adcOutRange);
1266 }
1267 else {
1268 adc = ((Int_t) (signal * (adcOutRange / adcInRange)));
1269 }
e23fbb27 1270 inADC[iTime] = adc;
1271 outADC[iTime] = adc;
1272 }
1273
1274 // Apply the tail cancelation via the digital filter
4487dad0 1275 if (fPar->TCOn()) {
1276 DeConvExp(inADC,outADC,nTimeTotal,fPar->GetTCnexp());
e23fbb27 1277 }
1278
1279 for (iTime = 0; iTime < nTimeTotal; iTime++) {
abaf1f1d 1280 // Store the amplitude of the digit if above threshold
e23fbb27 1281 if (outADC[iTime] > adcThreshold) {
3583ac1a 1282 digitsOut->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
e23fbb27 1283 // Copy the dictionary
1284 for (iDict = 0; iDict < kNDict; iDict++) {
1285 Int_t track = dictionaryIn[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1286 dictionaryOut[iDict]->SetDataUnchecked(iRow,iCol,iTime,track);
1287 }
abaf1f1d 1288 }
abaf1f1d 1289 }
e23fbb27 1290
abaf1f1d 1291 }
1292 }
1293
1294 if (fCompress) {
1295 digitsIn->Compress(1,0);
1296 digitsOut->Compress(1,0);
1297 for (iDict = 0; iDict < kNDict; iDict++) {
1298 dictionaryIn[iDict]->Compress(1,0);
1299 dictionaryOut[iDict]->Compress(1,0);
1300 }
1301 }
1302
e23fbb27 1303 delete [] inADC;
1304 delete [] outADC;
1305
abaf1f1d 1306 }
f7336fa3 1307
1308 return kTRUE;
1309
1310}
1311
16bf9884 1312//_____________________________________________________________________________
abaf1f1d 1313Bool_t AliTRDdigitizer::MergeSDigits()
16bf9884 1314{
1315 //
abaf1f1d 1316 // Merges the input s-digits:
1317 // - The amplitude of the different inputs are summed up.
1318 // - Of the track IDs from the input dictionaries only one is
1319 // kept for each input. This works for maximal 3 different merged inputs.
16bf9884 1320 //
1321
abaf1f1d 1322 // Number of track dictionary arrays
1323 const Int_t kNDict = AliTRDdigitsManager::kNDict;
1324
4487dad0 1325 if (!fPar) {
1326 fPar = new AliTRDparameter("TRDparameter","Standard parameter");
1327 if (fDebug > 0) {
1328 printf("<AliTRDdigitizer::MergeSDigits> ");
1329 printf("Create the default parameter object\n");
1330 }
1331 }
1332
abaf1f1d 1333 Int_t iDict = 0;
e23fbb27 1334 Int_t jDict = 0;
abaf1f1d 1335
1336 AliTRDdataArrayI *digitsA;
1337 AliTRDdataArrayI *digitsB;
1338 AliTRDdataArrayI *dictionaryA[kNDict];
1339 AliTRDdataArrayI *dictionaryB[kNDict];
1340
1341 // Get the first s-digits
1342 fSDigitsManager = (AliTRDdigitsManager *) fSDigitsManagerList->First();
1343 if (!fSDigitsManager) return kFALSE;
1344
1345 // Loop through the other sets of s-digits
1346 AliTRDdigitsManager *mergeSDigitsManager;
1347 mergeSDigitsManager = (AliTRDdigitsManager *)
1348 fSDigitsManagerList->After(fSDigitsManager);
1349
e23fbb27 1350 if (fDebug > 0) {
abaf1f1d 1351 if (mergeSDigitsManager) {
e23fbb27 1352 printf("<AliTRDdigitizer::MergeSDigits> ");
1353 printf("Merge %d input files.\n",fSDigitsManagerList->GetSize());
abaf1f1d 1354 }
1355 else {
e23fbb27 1356 printf("<AliTRDdigitizer::MergeSDigits> ");
abaf1f1d 1357 printf("Only one input file.\n");
1358 }
1359 }
1360
1361 Int_t iMerge = 0;
1362 while (mergeSDigitsManager) {
1363
1364 iMerge++;
1365
1366 // Loop through the detectors
1367 for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
1368
1369 Int_t plane = fGeo->GetPlane(iDet);
1370 Int_t sector = fGeo->GetSector(iDet);
1371 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 1372 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
1373 Int_t nColMax = fPar->GetColMax(plane);
1374 Int_t nTimeTotal = fPar->GetTimeTotal();
abaf1f1d 1375
1376 // Loop through the pixels of one detector and add the signals
1377 digitsA = fSDigitsManager->GetDigits(iDet);
1378 digitsB = mergeSDigitsManager->GetDigits(iDet);
1379 digitsA->Expand();
1380 digitsB->Expand();
1381 for (iDict = 0; iDict < kNDict; iDict++) {
1382 dictionaryA[iDict] = fSDigitsManager->GetDictionary(iDet,iDict);
1383 dictionaryB[iDict] = mergeSDigitsManager->GetDictionary(iDet,iDict);
1384 dictionaryA[iDict]->Expand();
1385 dictionaryB[iDict]->Expand();
1386 }
1387
4487dad0 1388 // Merge only detectors that contain a signal
1389 Bool_t doMerge = kTRUE;
1390 if (fMergeSignalOnly) {
1391 if (digitsA->GetOverThreshold(0) == 0) {
1392 doMerge = kFALSE;
1393 }
abaf1f1d 1394 }
1395
4487dad0 1396 if (doMerge) {
abaf1f1d 1397
4487dad0 1398 if (fDebug > 0) {
1399 printf("<AliTRDdigitizer::MergeSDigits> ");
1400 printf("Merge detector %d of input no.%d\n",iDet,iMerge+1);
1401 }
abaf1f1d 1402
4487dad0 1403 for (Int_t iRow = 0; iRow < nRowMax; iRow++ ) {
1404 for (Int_t iCol = 0; iCol < nColMax; iCol++ ) {
1405 for (Int_t iTime = 0; iTime < nTimeTotal; iTime++) {
1406
1407 // Add the amplitudes of the summable digits
1408 Int_t ampA = digitsA->GetDataUnchecked(iRow,iCol,iTime);
1409 Int_t ampB = digitsB->GetDataUnchecked(iRow,iCol,iTime);
1410 ampA += ampB;
1411 digitsA->SetDataUnchecked(iRow,iCol,iTime,ampA);
1412
1413 // Add the mask to the track id if defined.
1414 for (iDict = 0; iDict < kNDict; iDict++) {
1415 Int_t trackB = dictionaryB[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1416 if ((fMasks) && (trackB > 0)) {
1417 for (jDict = 0; jDict < kNDict; jDict++) {
1418 Int_t trackA = dictionaryA[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1419 if (trackA == 0) {
1420 trackA = trackB + fMasks[iMerge];
1421 dictionaryA[iDict]->SetDataUnchecked(iRow,iCol,iTime,trackA);
1422 }
1423 }
1424 }
e23fbb27 1425 }
abaf1f1d 1426
4487dad0 1427 }
abaf1f1d 1428 }
4487dad0 1429 }
1430
abaf1f1d 1431 }
1432
1433 if (fCompress) {
1434 digitsA->Compress(1,0);
1435 digitsB->Compress(1,0);
1436 for (iDict = 0; iDict < kNDict; iDict++) {
1437 dictionaryA[iDict]->Compress(1,0);
1438 dictionaryB[iDict]->Compress(1,0);
1439 }
1440 }
1441
1442 }
1443
1444 // The next set of s-digits
1445 mergeSDigitsManager = (AliTRDdigitsManager *)
1446 fSDigitsManagerList->After(mergeSDigitsManager);
1447
1448 }
1449
16bf9884 1450 return kTRUE;
1451
1452}
1453
abaf1f1d 1454//_____________________________________________________________________________
1455Bool_t AliTRDdigitizer::SDigits2Digits()
1456{
1457 //
1458 // Merges the input s-digits and converts them to normal digits
1459 //
1460
1461 if (!MergeSDigits()) return kFALSE;
1462
1463 return ConvertSDigits();
1464
1465}
1466
793ff80c 1467//_____________________________________________________________________________
1468Bool_t AliTRDdigitizer::CheckDetector(Int_t plane, Int_t chamber, Int_t sector)
1469{
1470 //
1471 // Checks whether a detector is enabled
1472 //
1473
0c24ba98 1474 if (fSimpleSim) return kTRUE;
1475
793ff80c 1476 if ((fTRD->GetSensChamber() >= 0) &&
1477 (fTRD->GetSensChamber() != chamber)) return kFALSE;
1478 if ((fTRD->GetSensPlane() >= 0) &&
c1e4b257 1479 (fTRD->GetSensPlane() != plane)) return kFALSE;
793ff80c 1480 if ( fTRD->GetSensSector() >= 0) {
1481 Int_t sens1 = fTRD->GetSensSector();
1482 Int_t sens2 = sens1 + fTRD->GetSensSectorRange();
1483 sens2 -= ((Int_t) (sens2 / AliTRDgeometry::Nsect()))
1484 * AliTRDgeometry::Nsect();
1485 if (sens1 < sens2) {
1486 if ((sector < sens1) || (sector >= sens2)) return kFALSE;
1487 }
1488 else {
1489 if ((sector < sens1) && (sector >= sens2)) return kFALSE;
1490 }
1491 }
1492
1493 return kTRUE;
1494
1495}
1496
f7336fa3 1497//_____________________________________________________________________________
0a29d0f1 1498Bool_t AliTRDdigitizer::WriteDigits() const
f7336fa3 1499{
1500 //
1501 // Writes out the TRD-digits and the dictionaries
1502 //
1503
bdbb05bb 1504 //Write parameters
1505 fRunLoader->CdGAFile();
1506 if (!gFile->Get("TRDparameter")) GetParameter()->Write();
1507
da581aea 1508 // Store the digits and the dictionary in the tree
abaf1f1d 1509 return fDigitsManager->WriteDigits();
f7336fa3 1510
1511}
793ff80c 1512
e23fbb27 1513//_____________________________________________________________________________
1514void AliTRDdigitizer::DeConvExp(Double_t *source, Double_t *target
1515 , Int_t n, Int_t nexp)
1516{
1517 //
1518 // Does the deconvolution by the digital filter.
1519 //
1520 // Author: Marcus Gutfleisch, KIP Heidelberg
1521 // Optimized for: New TRF from Venelin Angelov, simulated with CADENCE
1522 // Pad-ground capacitance = 25 pF
1523 // Pad-pad cross talk capacitance = 6 pF
1524 // For 10 MHz digitization, corresponding to 20 time bins
1525 // in the drift region
1526 //
1527
1528 Double_t rates[2];
1529 Double_t coefficients[2];
1530
1531 /* initialize (coefficient = alpha, rates = lambda) */
1532
1533 if( nexp == 1 ) {
1534 rates[0] = 0.466998;
1535 /* no rescaling */
1536 coefficients[0] = 1.0;
1537 }
1538 if( nexp == 2 ) {
1539 rates[0] = 0.8988162;
1540 coefficients[0] = 0.11392069;
1541 rates[1] = 0.3745688;
1542 coefficients[1] = 0.8860793;
1543 /* no rescaling */
1544 Float_t sumc = coefficients[0]+coefficients[1];
1545 coefficients[0] /= sumc;
1546 coefficients[1] /= sumc;
1547 }
1548
1549 Int_t i, k;
1550 Double_t reminder[2];
1551 Double_t correction, result;
1552
1553 /* attention: computation order is important */
1554 correction=0.0;
1555 for ( k = 0; k < nexp; k++ ) reminder[k]=0.0;
1556
1557 for ( i = 0; i < n; i++ ) {
1558 result = ( source[i] - correction ); /* no rescaling */
1559 target[i] = result;
1560
1561 for ( k = 0; k < nexp; k++ ) reminder[k] = rates[k]
1562 * ( reminder[k] + coefficients[k] * result);
1563
1564 correction=0.0;
1565 for ( k = 0; k < nexp; k++ ) correction += reminder[k];
1566 }
1567
1568}
1569
8e64dd77 1570//_____________________________________________________________________________
88cb7938 1571void AliTRDdigitizer::InitOutput(Int_t iEvent)
8e64dd77 1572{
1573 //
1574 // Initializes the output branches
1575 //
1576
1577 fEvent = iEvent;
88cb7938 1578
1579 if (!fRunLoader)
1580 {
1581 Error("InitOutput","Run Loader is NULL");
1582 return;
1583 }
1584 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
1585 if (!loader)
1586 {
1587 Error("Open","Can not get TRD loader from Run Loader");
1588 return;
1589 }
1590
1591 TTree* tree = 0;
1592
1593 if (fSDigits)
1594 {
1595 //if we produce SDigits
1596 tree = loader->TreeS();
1597 if (!tree)
1598 {
1599 loader->MakeTree("S");
1600 tree = loader->TreeS();
1601 }
1602 }
1603 else
1604 {//if we produce Digits
1605 tree = loader->TreeD();
1606 if (!tree)
1607 {
1608 loader->MakeTree("D");
1609 tree = loader->TreeD();
1610 }
1611 }
1612 fDigitsManager->SetEvent(iEvent);
1613 fDigitsManager->MakeBranch(tree);
8e64dd77 1614
1615}