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