]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TRD/AliTRDdigitizer.cxx
Possibility to change sign of Pz event by event, needed for pA, Ap simulation in
[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//_____________________________________________________________________________
e0d47c25 244void AliTRDdigitizer::Copy(TObject &d) const
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
88cb7938 481 if (!tree)
482 {
483 loader->MakeTree("D");
484 tree = loader->TreeD();
485 }
486 }
487 return MakeBranch(tree);
abaf1f1d 488 }
489 else {
490 return kFALSE;
491 }
793ff80c 492
493}
494
495//_____________________________________________________________________________
496Bool_t AliTRDdigitizer::InitDetector()
497{
498 //
499 // Sets the pointer to the TRD detector and the geometry
500 //
501
dd9a6ee3 502 // Get the pointer to the detector class and check for version 1
4487dad0 503 fTRD = (AliTRD *) gAlice->GetDetector("TRD");
cec4059b 504 if (!fTRD) {
505 printf("<AliTRDdigitizer::InitDetector> ");
506 printf("No TRD module found\n");
507 exit(1);
508 }
dd9a6ee3 509 if (fTRD->IsVersion() != 1) {
e23fbb27 510 printf("<AliTRDdigitizer::InitDetector> ");
dd9a6ee3 511 printf("TRD must be version 1 (slow simulator).\n");
512 exit(1);
513 }
514
515 // Get the geometry
516 fGeo = fTRD->GetGeometry();
e23fbb27 517 if (fDebug > 0) {
518 printf("<AliTRDdigitizer::InitDetector> ");
abaf1f1d 519 printf("Geometry version %d\n",fGeo->IsVersion());
520 }
dd9a6ee3 521
abaf1f1d 522 // Create a digits manager
47b5729a 523 delete fDigitsManager;
abaf1f1d 524 fDigitsManager = new AliTRDdigitsManager();
525 fDigitsManager->SetSDigits(fSDigits);
526 fDigitsManager->CreateArrays();
527 fDigitsManager->SetEvent(fEvent);
e23fbb27 528 fDigitsManager->SetDebug(fDebug);
abaf1f1d 529
530 // The list for the input s-digits manager to be merged
47b5729a 531 if (fSDigitsManagerList) {
532 fSDigitsManagerList->Delete();
533 } else {
534 fSDigitsManagerList = new TList();
535 }
abaf1f1d 536
4487dad0 537 return kTRUE;
f7336fa3 538
539}
540
6244debe 541//_____________________________________________________________________________
88cb7938 542Bool_t AliTRDdigitizer::MakeBranch(TTree* tree) const
6244debe 543{
abaf1f1d 544 //
545 // Create the branches for the digits array
6244debe 546 //
547
88cb7938 548 return fDigitsManager->MakeBranch(tree);
6244debe 549
550}
551
f7336fa3 552//_____________________________________________________________________________
553Bool_t AliTRDdigitizer::MakeDigits()
554{
555 //
872a7aba 556 // Creates digits.
f7336fa3 557 //
558
f7336fa3 559 ///////////////////////////////////////////////////////////////
560 // Parameter
561 ///////////////////////////////////////////////////////////////
562
563 // Converts number of electrons to fC
872a7aba 564 const Double_t kEl2fC = 1.602E-19 * 1.0E15;
f7336fa3 565
566 ///////////////////////////////////////////////////////////////
567
793ff80c 568 // Number of pads included in the pad response
569 const Int_t kNpad = 3;
570
571 // Number of track dictionary arrays
dd56b762 572 const Int_t kNDict = AliTRDdigitsManager::kNDict;
793ff80c 573
872a7aba 574 // Half the width of the amplification region
575 const Float_t kAmWidth = AliTRDgeometry::AmThick() / 2.;
ccb4315c 576 // Width of the drit region
577 const Float_t kDrWidth = AliTRDgeometry::DrThick();
872a7aba 578
c3a4830f 579 Int_t iRow, iCol, iTime, iPad;
71d9fa7b 580 Int_t iDict = 0;
793ff80c 581 Int_t nBytes = 0;
f7336fa3 582
583 Int_t totalSizeDigits = 0;
584 Int_t totalSizeDict0 = 0;
585 Int_t totalSizeDict1 = 0;
586 Int_t totalSizeDict2 = 0;
587
ccb4315c 588 Int_t timeBinTRFend = 1;
872a7aba 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()) {
ccb4315c 615 timeBinTRFend = ((Int_t) ( fPar->GetTRFhi() * fPar->GetSamplingFrequency())) - 1;
e23fbb27 616 if (fDebug > 0) {
617 printf("<AliTRDdigitizer::MakeDigits> ");
ccb4315c 618 printf("Sample the TRF up to bin %d\n",timeBinTRFend);
abaf1f1d 619 }
da581aea 620 }
621
4487dad0 622 Float_t elAttachProp = fPar->GetElAttachProp() / 100.;
793ff80c 623
872a7aba 624 if (!fGeo) {
e23fbb27 625 printf("<AliTRDdigitizer::MakeDigits> ");
872a7aba 626 printf("No geometry defined\n");
627 return kFALSE;
628 }
629
e23fbb27 630 if (fDebug > 0) {
631 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 632 printf("Start creating digits.\n");
633 }
872a7aba 634
88cb7938 635 AliLoader* gimme = fRunLoader->GetLoader("TRDLoader");
636 if (!gimme->TreeH()) gimme->LoadHits();
637 TTree* hitTree = gimme->TreeH();
638 if (hitTree == 0x0)
639 {
640 Error("MakeDigits","Can not get TreeH");
641 return kFALSE;
642 }
643 fTRD->SetTreeAddress();
644
793ff80c 645 // Get the number of entries in the hit tree
646 // (Number of primary particles creating a hit somewhere)
0c24ba98 647 Int_t nTrack = 1;
648 if (!fSimpleSim) {
649 nTrack = (Int_t) hitTree->GetEntries();
650 if (fDebug > 0) {
651 printf("<AliTRDdigitizer::MakeDigits> ");
652 printf("Found %d primary particles\n",nTrack);
653 }
654 }
793ff80c 655
656 Int_t detectorOld = -1;
657 Int_t countHits = 0;
658
a328fff9 659 if (fDebug > 0) {
ccb4315c 660 fPar->PrintDriftVelocity();
661 printf("<AliTRDdigitizer::MakeDigits> Sampling = %.0fMHz\n", fPar->GetSamplingFrequency());
662 printf("<AliTRDdigitizer::MakeDigits> Gain = %d\n",(Int_t)fPar->GetGasGain());
663 printf("<AliTRDdigitizer::MakeDigits> Noise = %d\n",(Int_t)fPar->GetNoise());
a328fff9 664 if (fPar->TimeStructOn()) {
665 printf("<AliTRDdigitizer::MakeDigits> ");
666 printf("Time Structure of drift cells implemented.\n");
a328fff9 667 } else {
668 printf("<AliTRDdigitizer::MakeDigits> ");
669 printf("Constant drift velocity in drift cells.\n");
670 }
671 }
672
793ff80c 673 // Loop through all entries in the tree
674 for (Int_t iTrack = 0; iTrack < nTrack; iTrack++) {
675
0c24ba98 676 if (!fSimpleSim) {
677 gAlice->ResetHits();
678 nBytes += hitTree->GetEvent(iTrack);
679 }
793ff80c 680
e23fbb27 681 // Loop through the TRD hits
682 Int_t iHit = 0;
683 AliTRDhit *hit = (AliTRDhit *) fTRD->FirstHit(-1);
684 while (hit) {
685
793ff80c 686 countHits++;
e23fbb27 687 iHit++;
688
ccb4315c 689 pos[0] = hit->X();
690 pos[1] = hit->Y();
691 pos[2] = hit->Z();
692 Float_t q = hit->GetCharge();
693 Int_t track = hit->Track();
694 Int_t detector = hit->GetDetector();
695 Int_t plane = fGeo->GetPlane(detector);
696 Int_t sector = fGeo->GetSector(detector);
697 Int_t chamber = fGeo->GetChamber(detector);
698 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
699 Int_t nColMax = fPar->GetColMax(plane);
700 Int_t nTimeMax = fPar->GetTimeMax();
701 Int_t nTimeBefore = fPar->GetTimeBefore();
702 Int_t nTimeAfter = fPar->GetTimeAfter();
703 Int_t nTimeTotal = fPar->GetTimeTotal();
704 Float_t row0 = fPar->GetRow0(plane,chamber,sector);
705 Float_t col0 = fPar->GetCol0(plane);
706 Float_t time0 = fPar->GetTime0(plane);
707 Float_t rowPadSize = fPar->GetRowPadSize(plane,chamber,sector);
708 Float_t colPadSize = fPar->GetColPadSize(plane);
709 Float_t driftvelocity = fPar->GetDriftVelocity();
710 Float_t samplingRate = fPar->GetSamplingFrequency();
711 Float_t divideRow = 1.0 / rowPadSize;
712 Float_t divideCol = 1.0 / colPadSize;
793ff80c 713
e23fbb27 714 if (fDebug > 1) {
793ff80c 715 printf("Analyze hit no. %d ",iHit);
716 printf("-----------------------------------------------------------\n");
717 hit->Dump();
718 printf("plane = %d, sector = %d, chamber = %d\n"
719 ,plane,sector,chamber);
720 printf("nRowMax = %d, nColMax = %d, nTimeMax = %d\n"
721 ,nRowMax,nColMax,nTimeMax);
872a7aba 722 printf("nTimeBefore = %d, nTimeAfter = %d, nTimeTotal = %d\n"
723 ,nTimeBefore,nTimeAfter,nTimeTotal);
793ff80c 724 printf("row0 = %f, col0 = %f, time0 = %f\n"
725 ,row0,col0,time0);
ccb4315c 726 printf("rowPadSize = %f, colPadSize = %f, SamplingRate = %f\n"
727 ,rowPadSize,colPadSize,samplingRate);
793ff80c 728 }
729
e23fbb27 730 // Don't analyze test hits and switched off detectors
731 if ((CheckDetector(plane,chamber,sector)) &&
732 (((Int_t) q) != 0)) {
dd56b762 733
e23fbb27 734 if (detector != detectorOld) {
e153aaf6 735
e23fbb27 736 if (fDebug > 1) {
737 printf("<AliTRDdigitizer::MakeDigits> ");
738 printf("Get new container. New det = %d, Old det = %d\n"
739 ,detector,detectorOld);
dd9a6ee3 740 }
e23fbb27 741 // Compress the old one if enabled
742 if ((fCompress) && (detectorOld > -1)) {
743 if (fDebug > 1) {
744 printf("<AliTRDdigitizer::MakeDigits> ");
745 printf("Compress the old container ...");
746 }
747 signals->Compress(1,0);
748 for (iDict = 0; iDict < kNDict; iDict++) {
749 dictionary[iDict]->Compress(1,0);
c1e4b257 750 }
e23fbb27 751 if (fDebug > 1) printf("done\n");
793ff80c 752 }
e23fbb27 753 // Get the new container
754 signals = (AliTRDdataArrayF *) signalsArray->At(detector);
755 if (signals->GetNtime() == 0) {
756 // Allocate a new one if not yet existing
757 if (fDebug > 1) {
758 printf("<AliTRDdigitizer::MakeDigits> ");
759 printf("Allocate a new container ... ");
760 }
761 signals->Allocate(nRowMax,nColMax,nTimeTotal);
793ff80c 762 }
0c24ba98 763 else if (fSimpleSim) {
764 // Clear an old one for the simple simulation
765 if (fDebug > 1) {
766 printf("<AliTRDdigitizer::MakeDigits> ");
767 printf("Clear a old container ... ");
768 }
769 signals->Clear();
770 }
793ff80c 771 else {
e23fbb27 772 // Expand an existing one
773 if (fCompress) {
774 if (fDebug > 1) {
775 printf("<AliTRDdigitizer::MakeDigits> ");
776 printf("Expand an existing container ... ");
777 }
778 signals->Expand();
779 }
793ff80c 780 }
e23fbb27 781 // The same for the dictionary
0c24ba98 782 if (!fSimpleSim) {
783 for (iDict = 0; iDict < kNDict; iDict++) {
784 dictionary[iDict] = fDigitsManager->GetDictionary(detector,iDict);
785 if (dictionary[iDict]->GetNtime() == 0) {
786 dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
787 }
788 else {
789 if (fCompress) dictionary[iDict]->Expand();
790 }
e23fbb27 791 }
792 }
793 if (fDebug > 1) printf("done\n");
794 detectorOld = detector;
793ff80c 795 }
796
e23fbb27 797 // Rotate the sectors on top of each other
0c24ba98 798 if (fSimpleSim) {
799 rot[0] = pos[0];
800 rot[1] = pos[1];
801 rot[2] = pos[2];
802 }
803 else {
804 fGeo->Rotate(detector,pos,rot);
805 }
e23fbb27 806
ccb4315c 807 // The driftlength. It is negative if the hit is between pad plane and anode wires.
e23fbb27 808 Float_t driftlength = time0 - rot[0];
809
ccb4315c 810 // Normalised drift length
811 Float_t absdriftlength = TMath::Abs(driftlength);
812 if (fPar->ExBOn()) absdriftlength /= TMath::Sqrt(fPar->GetLorentzFactor());
e23fbb27 813
814 // Loop over all electrons of this hit
815 // TR photons produce hits with negative charge
816 Int_t nEl = ((Int_t) TMath::Abs(q));
817 for (Int_t iEl = 0; iEl < nEl; iEl++) {
818
819 xyz[0] = rot[0];
820 xyz[1] = rot[1];
821 xyz[2] = rot[2];
822
a328fff9 823 // Stupid patch to take care of TR photons that are absorbed
824 // outside the chamber volume. A real fix would actually need
825 // a more clever implementation of the TR hit generation
81a6930e 826 if (q < 0.0) {
827 Float_t zz = xyz[2] - row0;
828 if ((zz < 0.0) || (zz > rowPadSize*nRowMax)) {
829 if (iEl == 0) {
830 printf("<AliTRDdigitizer::MakeDigits> ");
831 printf("Hit outside of sensitive volume, row (Q = %d)\n",((Int_t) q));
832 //printf("zz=%f, xyz=%f, row0=%f, max=%f\n",zz,xyz[2],row0,rowPadSize*nRowMax);
833 }
834 continue;
a328fff9 835 }
ccb4315c 836 Float_t tt = driftlength + kAmWidth;
837 if (tt < 0.0 || tt > kDrWidth + 2.*kAmWidth) {
81a6930e 838 if (iEl == 0) {
839 printf("<AliTRDdigitizer::MakeDigits> ");
840 printf("Hit outside of sensitive volume, time (Q = %d)\n",((Int_t) q));
ccb4315c 841 /*printf("track %d: tt=%f, xyz=%f, min=%f, max=%f\n",
842 iTrack,tt,xyz[0],time0-kDrWidth-kAmWidth,time0+kAmWidth);*/
81a6930e 843 }
844 continue;
a328fff9 845 }
a328fff9 846 }
847
e23fbb27 848 // Electron attachment
4487dad0 849 if (fPar->ElAttachOn()) {
ccb4315c 850 if (gRandom->Rndm() < (absdriftlength * elAttachProp)) continue;
81a6930e 851 }
e23fbb27 852
853 // Apply the diffusion smearing
4487dad0 854 if (fPar->DiffusionOn()) {
ccb4315c 855 if (!(fPar->Diffusion(absdriftlength,xyz))) continue;
e23fbb27 856 }
f7336fa3 857
e23fbb27 858 // Apply E x B effects (depends on drift direction)
4487dad0 859 if (fPar->ExBOn()) {
ccb4315c 860 if (!(fPar->ExB(driftlength,xyz))) continue;
a328fff9 861 }
862
ccb4315c 863 // The electron position after diffusion and ExB in pad coordinates.
e23fbb27 864 // The pad row (z-direction)
865 Float_t rowDist = xyz[2] - row0;
866 Int_t rowE = ((Int_t) (rowDist * divideRow));
81a6930e 867 if ((rowE < 0) || (rowE >= nRowMax)) continue;
e23fbb27 868 Float_t rowOffset = ((((Float_t) rowE) + 0.5) * rowPadSize) - rowDist;
869
870 // The pad column (rphi-direction)
4487dad0 871 Float_t col0tilt = fPar->Col0Tilted(col0,rowOffset,plane);
e23fbb27 872 Float_t colDist = xyz[1] - col0tilt;
873 Int_t colE = ((Int_t) (colDist * divideCol));
81a6930e 874 if ((colE < 0) || (colE >= nColMax)) continue;
ccb4315c 875 Float_t colOffset = ((((Float_t) colE) + 0.5) * colPadSize) - colDist;
876
877 // Convert the position to drift time, using either constant drift velocity or
878 // time structure of drift cells (non-isochronity, GARFIELD calculation).
879 Float_t drifttime;
880 if (fPar->TimeStructOn()) {
881 // Get z-position with respect to anode wire:
882 Float_t Z = xyz[2] - row0 + fPar->GetAnodeWireOffset();
883 Z -= ((Int_t)(2*Z))/2.;
884 if (Z>0.25) Z = 0.5-Z;
885 // use drift time map (GARFIELD)
886 drifttime = fPar->TimeStruct(time0 - xyz[0] + kAmWidth, Z);
887 } else {
888 // use constant drift velocity
889 drifttime = TMath::Abs(time0 - xyz[0]) / driftvelocity;
e23fbb27 890 }
ccb4315c 891
892 // The time bin (always positive)
893 Int_t timeE = ((Int_t) (drifttime * samplingRate));
894 // The distance of the position to the middle of the timebin
895 Float_t timeOffset = ((((Float_t) timeE) + 0.5) / samplingRate) - drifttime;
872a7aba 896
e23fbb27 897 // Apply the gas gain including fluctuations
898 Float_t ggRndm = 0.0;
899 do {
900 ggRndm = gRandom->Rndm();
901 } while (ggRndm <= 0);
4487dad0 902 Int_t signal = (Int_t) (-fPar->GetGasGain() * TMath::Log(ggRndm));
e23fbb27 903
904 // Apply the pad response
4487dad0 905 if (fPar->PRFOn()) {
e23fbb27 906 // The distance of the electron to the center of the pad
907 // in units of pad width
908 Float_t dist = - colOffset * divideCol;
4487dad0 909 if (!(fPar->PadResponse(signal,dist,plane,padSignal))) continue;
793ff80c 910 }
e23fbb27 911 else {
912 padSignal[0] = 0.0;
913 padSignal[1] = signal;
914 padSignal[2] = 0.0;
915 }
916
917 // Sample the time response inside the drift region
918 // + additional time bins before and after.
919 // The sampling is done always in the middle of the time bin
ccb4315c 920 for (Int_t iTimeBin = TMath::Max(timeE,-nTimeBefore) ;
921 iTimeBin < TMath::Min(timeE+timeBinTRFend,nTimeMax+nTimeAfter ) ;
922 iTimeBin++ ) {
e23fbb27 923
924 // Apply the time response
925 Float_t timeResponse = 1.0;
926 Float_t crossTalk = 0.0;
ccb4315c 927 Float_t time = (iTimeBin - timeE) / samplingRate + timeOffset;
4487dad0 928 if (fPar->TRFOn()) {
929 timeResponse = fPar->TimeResponse(time);
81a6930e 930 //printf("iTimeBin = %d, time = %f, timeResponse = %f\n"
931 // ,iTimeBin,time,timeResponse);
e23fbb27 932 }
4487dad0 933 if (fPar->CTOn()) {
934 crossTalk = fPar->CrossTalk(time);
e23fbb27 935 }
936
937 signalOld[0] = 0.0;
938 signalOld[1] = 0.0;
939 signalOld[2] = 0.0;
f7336fa3 940
e23fbb27 941 for (iPad = 0; iPad < kNpad; iPad++) {
942
943 Int_t colPos = colE + iPad - 1;
944 if (colPos < 0) continue;
945 if (colPos >= nColMax) break;
946
947 // Add the signals
948 // Note: The time bin number is shifted by nTimeBefore to avoid negative
949 // time bins. This has to be subtracted later.
950 Int_t iCurrentTimeBin = iTimeBin + nTimeBefore;
951 signalOld[iPad] = signals->GetDataUnchecked(rowE,colPos,iCurrentTimeBin);
952 if( colPos != colE ) {
953 signalOld[iPad] += padSignal[iPad] * (timeResponse + crossTalk);
954 }
955 else {
956 signalOld[iPad] += padSignal[iPad] * timeResponse;
957 }
958 signals->SetDataUnchecked(rowE,colPos,iCurrentTimeBin,signalOld[iPad]);
959
960 // Store the track index in the dictionary
961 // Note: We store index+1 in order to allow the array to be compressed
0c24ba98 962 if ((signalOld[iPad] > 0) && (!fSimpleSim)) {
e23fbb27 963 for (iDict = 0; iDict < kNDict; iDict++) {
964 Int_t oldTrack = dictionary[iDict]->GetDataUnchecked(rowE
965 ,colPos
966 ,iCurrentTimeBin);
967 if (oldTrack == track+1) break;
968 if (oldTrack == 0) {
969 dictionary[iDict]->SetDataUnchecked(rowE,colPos,iCurrentTimeBin,track+1);
970 break;
971 }
71d9fa7b 972 }
973 }
872a7aba 974
e23fbb27 975 } // Loop: pads
f7336fa3 976
e23fbb27 977 } // Loop: time bins
f7336fa3 978
e23fbb27 979 } // Loop: electrons of a single hit
f7336fa3 980
e23fbb27 981 } // If: detector and test hit
f7336fa3 982
e23fbb27 983 hit = (AliTRDhit *) fTRD->NextHit();
f7336fa3 984
e23fbb27 985 } // Loop: hits of one primary track
986
987 } // Loop: primary tracks
988
989 if (fDebug > 0) {
990 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 991 printf("Finished analyzing %d hits\n",countHits);
992 }
793ff80c 993
0c24ba98 994 // The coupling factor
995 Float_t coupling = fPar->GetPadCoupling()
996 * fPar->GetTimeCoupling();
997
998 // The conversion factor
999 Float_t convert = kEl2fC
1000 * fPar->GetChipGain();
6244debe 1001
793ff80c 1002 // Loop through all chambers to finalize the digits
0c24ba98 1003 Int_t iDetBeg = 0;
1004 Int_t iDetEnd = AliTRDgeometry::Ndet();
1005 if (fSimpleSim) {
1006 iDetBeg = fSimpleDet;
1007 iDetEnd = iDetBeg + 1;
1008 }
1009 for (Int_t iDet = iDetBeg; iDet < iDetEnd; iDet++) {
793ff80c 1010
872a7aba 1011 Int_t plane = fGeo->GetPlane(iDet);
1012 Int_t sector = fGeo->GetSector(iDet);
1013 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 1014 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
1015 Int_t nColMax = fPar->GetColMax(plane);
1016 Int_t nTimeMax = fPar->GetTimeMax();
1017 Int_t nTimeTotal = fPar->GetTimeTotal();
793ff80c 1018
e23fbb27 1019 Double_t *inADC = new Double_t[nTimeTotal];
1020 Double_t *outADC = new Double_t[nTimeTotal];
1021
1022 if (fDebug > 0) {
1023 printf("<AliTRDdigitizer::MakeDigits> ");
793ff80c 1024 printf("Digitization for chamber %d\n",iDet);
1025 }
da581aea 1026
793ff80c 1027 // Add a container for the digits of this detector
abaf1f1d 1028 digits = fDigitsManager->GetDigits(iDet);
793ff80c 1029 // Allocate memory space for the digits buffer
0c24ba98 1030 if (digits->GetNtime() == 0) {
1031 digits->Allocate(nRowMax,nColMax,nTimeTotal);
1032 }
1033 else if (fSimpleSim) {
1034 digits->Clear();
1035 }
1036
793ff80c 1037 // Get the signal container
1038 signals = (AliTRDdataArrayF *) signalsArray->At(iDet);
1039 if (signals->GetNtime() == 0) {
1040 // Create missing containers
872a7aba 1041 signals->Allocate(nRowMax,nColMax,nTimeTotal);
793ff80c 1042 }
1043 else {
1044 // Expand the container if neccessary
1045 if (fCompress) signals->Expand();
1046 }
1047 // Create the missing dictionary containers
0c24ba98 1048 if (!fSimpleSim) {
1049 for (iDict = 0; iDict < kNDict; iDict++) {
1050 dictionary[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
1051 if (dictionary[iDict]->GetNtime() == 0) {
1052 dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
1053 }
1054 }
793ff80c 1055 }
f7336fa3 1056
793ff80c 1057 Int_t nDigits = 0;
1058
6244debe 1059 // Don't create noise in detectors that are switched off
1060 if (CheckDetector(plane,chamber,sector)) {
1061
1062 // Create the digits for this chamber
872a7aba 1063 for (iRow = 0; iRow < nRowMax; iRow++ ) {
1064 for (iCol = 0; iCol < nColMax; iCol++ ) {
6244debe 1065
e23fbb27 1066 // Create summable digits
1067 if (fSDigits) {
6244debe 1068
e23fbb27 1069 for (iTime = 0; iTime < nTimeTotal; iTime++) {
872a7aba 1070 Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
abaf1f1d 1071 signalAmp *= fSDigitsScale;
e23fbb27 1072 signalAmp = TMath::Min(signalAmp,(Float_t) 1.0e9);
abaf1f1d 1073 Int_t adc = (Int_t) signalAmp;
4487dad0 1074 if (adc > 0) nDigits++;
872a7aba 1075 digits->SetDataUnchecked(iRow,iCol,iTime,adc);
c1e4b257 1076 }
6244debe 1077
e23fbb27 1078 }
1079 // Create normal digits
1080 else {
6244debe 1081
e23fbb27 1082 for (iTime = 0; iTime < nTimeTotal; iTime++) {
1083 Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
0c24ba98 1084 // Pad and time coupling
1085 signalAmp *= coupling;
7e582e9f 1086 // Add the noise, starting from minus ADC baseline in electrons
1087 Double_t baselineEl = fPar->GetADCbaseline() * (fPar->GetADCinRange()
1088 / fPar->GetADCoutRange())
1089 / convert;
1090 signalAmp = TMath::Max((Double_t) gRandom->Gaus(signalAmp,fPar->GetNoise())
1091 ,-baselineEl);
6244debe 1092 // Convert to mV
1093 signalAmp *= convert;
7e582e9f 1094 // Add ADC baseline in mV
1095 signalAmp += fPar->GetADCbaseline() * (fPar->GetADCinRange()
1096 / fPar->GetADCoutRange());
1097 // Convert to ADC counts. Set the overflow-bit fADCoutRange if the
6244debe 1098 // signal is larger than fADCinRange
1099 Int_t adc = 0;
4487dad0 1100 if (signalAmp >= fPar->GetADCinRange()) {
1101 adc = ((Int_t) fPar->GetADCoutRange());
6244debe 1102 }
1103 else {
4487dad0 1104 adc = ((Int_t) (signalAmp * (fPar->GetADCoutRange()
1105 / fPar->GetADCinRange())));
6244debe 1106 }
e23fbb27 1107 inADC[iTime] = adc;
1108 outADC[iTime] = adc;
1109 }
6244debe 1110
e23fbb27 1111 // Apply the tail cancelation via the digital filter
4487dad0 1112 if (fPar->TCOn()) {
1113 DeConvExp(inADC,outADC,nTimeTotal,fPar->GetTCnexp());
e23fbb27 1114 }
1115
1116 for (iTime = 0; iTime < nTimeTotal; iTime++) {
6244debe 1117 // Store the amplitude of the digit if above threshold
4487dad0 1118 if (outADC[iTime] > fPar->GetADCthreshold()) {
e23fbb27 1119 if (fDebug > 2) {
1120 printf(" iRow = %d, iCol = %d, iTime = %d, adc = %f\n"
1121 ,iRow,iCol,iTime,outADC[iTime]);
6244debe 1122 }
1123 nDigits++;
3583ac1a 1124 digits->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
6244debe 1125 }
6244debe 1126 }
1127
1128 }
e23fbb27 1129
6244debe 1130 }
793ff80c 1131 }
6244debe 1132
793ff80c 1133 }
1134
1135 // Compress the arrays
0c24ba98 1136 if (!fSimpleSim) {
1137 digits->Compress(1,0);
1138 for (iDict = 0; iDict < kNDict; iDict++) {
1139 dictionary[iDict]->Compress(1,0);
1140 }
f7336fa3 1141
0c24ba98 1142 totalSizeDigits += digits->GetSize();
1143 totalSizeDict0 += dictionary[0]->GetSize();
1144 totalSizeDict1 += dictionary[1]->GetSize();
1145 totalSizeDict2 += dictionary[2]->GetSize();
f7336fa3 1146
0c24ba98 1147 Float_t nPixel = nRowMax * nColMax * nTimeMax;
1148 if (fDebug > 0) {
1149 printf("<AliTRDdigitizer::MakeDigits> ");
1150 printf("Found %d digits in detector %d (%3.0f).\n"
1151 ,nDigits,iDet
1152 ,100.0 * ((Float_t) nDigits) / nPixel);
1153 }
1154
1155 if (fCompress) signals->Compress(1,0);
abaf1f1d 1156
0c24ba98 1157 }
f7336fa3 1158
e23fbb27 1159 delete [] inADC;
1160 delete [] outADC;
1161
f7336fa3 1162 }
1163
0c24ba98 1164 if (signalsArray) {
1165 delete signalsArray;
1166 signalsArray = 0;
1167 }
1168
e23fbb27 1169 if (fDebug > 0) {
1170 printf("<AliTRDdigitizer::MakeDigits> ");
abaf1f1d 1171 printf("Total number of analyzed hits = %d\n",countHits);
0c24ba98 1172 if (!fSimpleSim) {
1173 printf("<AliTRDdigitizer::MakeDigits> ");
1174 printf("Total digits data size = %d, %d, %d, %d\n",totalSizeDigits
1175 ,totalSizeDict0
1176 ,totalSizeDict1
1177 ,totalSizeDict2);
1178 }
abaf1f1d 1179 }
1180
1181 return kTRUE;
1182
1183}
1184
1185//_____________________________________________________________________________
1186void AliTRDdigitizer::AddSDigitsManager(AliTRDdigitsManager *man)
1187{
1188 //
1189 // Add a digits manager for s-digits to the input list.
1190 //
1191
1192 fSDigitsManagerList->Add(man);
1193
1194}
1195
b1113c6b 1196//_____________________________________________________________________________
1197void AliTRDdigitizer::DeleteSDigitsManager()
1198{
1199 //
1200 // Removes digits manager from the input list.
1201 //
1202
1203 fSDigitsManagerList->Delete();
1204
1205}
1206
abaf1f1d 1207//_____________________________________________________________________________
1208Bool_t AliTRDdigitizer::ConvertSDigits()
1209{
1210 //
1211 // Converts s-digits to normal digits
1212 //
1213
1214 // Number of track dictionary arrays
1215 const Int_t kNDict = AliTRDdigitsManager::kNDict;
1216
1217 // Converts number of electrons to fC
1218 const Double_t kEl2fC = 1.602E-19 * 1.0E15;
1219
1220 Int_t iDict = 0;
e23fbb27 1221 Int_t iRow;
1222 Int_t iCol;
1223 Int_t iTime;
abaf1f1d 1224
4487dad0 1225 if (!fPar) {
1226 fPar = new AliTRDparameter("TRDparameter","Standard parameter");
1227 if (fDebug > 0) {
1228 printf("<AliTRDdigitizer::ConvertSDigits> ");
1229 printf("Create the default parameter object\n");
1230 }
abaf1f1d 1231 }
1232
1233 Double_t sDigitsScale = 1.0 / GetSDigitsScale();
4487dad0 1234 Double_t noise = fPar->GetNoise();
1235 Double_t padCoupling = fPar->GetPadCoupling();
1236 Double_t timeCoupling = fPar->GetTimeCoupling();
1237 Double_t chipGain = fPar->GetChipGain();
0c24ba98 1238 Double_t coupling = padCoupling * timeCoupling;
1239 Double_t convert = kEl2fC * chipGain;
4487dad0 1240 Double_t adcInRange = fPar->GetADCinRange();
1241 Double_t adcOutRange = fPar->GetADCoutRange();
1242 Int_t adcThreshold = fPar->GetADCthreshold();
7e582e9f 1243 Int_t adcBaseline = fPar->GetADCbaseline();
abaf1f1d 1244
1245 AliTRDdataArrayI *digitsIn;
1246 AliTRDdataArrayI *digitsOut;
1247 AliTRDdataArrayI *dictionaryIn[kNDict];
1248 AliTRDdataArrayI *dictionaryOut[kNDict];
1249
1250 // Loop through the detectors
1251 for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
da581aea 1252
e23fbb27 1253 if (fDebug > 0) {
1254 printf("<AliTRDdigitizer::ConvertSDigits> ");
abaf1f1d 1255 printf("Convert detector %d to digits.\n",iDet);
1256 }
1257
1258 Int_t plane = fGeo->GetPlane(iDet);
1259 Int_t sector = fGeo->GetSector(iDet);
1260 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 1261 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
1262 Int_t nColMax = fPar->GetColMax(plane);
1263 Int_t nTimeTotal = fPar->GetTimeTotal();
abaf1f1d 1264
e23fbb27 1265 Double_t *inADC = new Double_t[nTimeTotal];
1266 Double_t *outADC = new Double_t[nTimeTotal];
1267
abaf1f1d 1268 digitsIn = fSDigitsManager->GetDigits(iDet);
1269 digitsIn->Expand();
1270 digitsOut = fDigitsManager->GetDigits(iDet);
1271 digitsOut->Allocate(nRowMax,nColMax,nTimeTotal);
1272 for (iDict = 0; iDict < kNDict; iDict++) {
1273 dictionaryIn[iDict] = fSDigitsManager->GetDictionary(iDet,iDict);
1274 dictionaryIn[iDict]->Expand();
1275 dictionaryOut[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
1276 dictionaryOut[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
1277 }
1278
e23fbb27 1279 for (iRow = 0; iRow < nRowMax; iRow++ ) {
1280 for (iCol = 0; iCol < nColMax; iCol++ ) {
abaf1f1d 1281
e23fbb27 1282 for (iTime = 0; iTime < nTimeTotal; iTime++) {
abaf1f1d 1283 Double_t signal = (Double_t) digitsIn->GetDataUnchecked(iRow,iCol,iTime);
1284 signal *= sDigitsScale;
0c24ba98 1285 // Pad and time coupling
1286 signal *= coupling;
7e582e9f 1287 // Add the noise, starting from minus ADC baseline in electrons
1288 Double_t baselineEl = adcBaseline * (adcInRange / adcOutRange) / convert;
1289 signal = TMath::Max((Double_t) gRandom->Gaus(signal,noise),-baselineEl);
abaf1f1d 1290 // Convert to mV
1291 signal *= convert;
7e582e9f 1292 // add ADC baseline in mV
1293 signal += adcBaseline * (adcInRange / adcOutRange);
abaf1f1d 1294 // Convert to ADC counts. Set the overflow-bit adcOutRange if the
1295 // signal is larger than adcInRange
1296 Int_t adc = 0;
1297 if (signal >= adcInRange) {
1298 adc = ((Int_t) adcOutRange);
1299 }
1300 else {
1301 adc = ((Int_t) (signal * (adcOutRange / adcInRange)));
1302 }
e23fbb27 1303 inADC[iTime] = adc;
1304 outADC[iTime] = adc;
1305 }
1306
1307 // Apply the tail cancelation via the digital filter
4487dad0 1308 if (fPar->TCOn()) {
1309 DeConvExp(inADC,outADC,nTimeTotal,fPar->GetTCnexp());
e23fbb27 1310 }
1311
1312 for (iTime = 0; iTime < nTimeTotal; iTime++) {
abaf1f1d 1313 // Store the amplitude of the digit if above threshold
e23fbb27 1314 if (outADC[iTime] > adcThreshold) {
3583ac1a 1315 digitsOut->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
e23fbb27 1316 // Copy the dictionary
1317 for (iDict = 0; iDict < kNDict; iDict++) {
1318 Int_t track = dictionaryIn[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1319 dictionaryOut[iDict]->SetDataUnchecked(iRow,iCol,iTime,track);
1320 }
abaf1f1d 1321 }
abaf1f1d 1322 }
e23fbb27 1323
abaf1f1d 1324 }
1325 }
1326
1327 if (fCompress) {
1328 digitsIn->Compress(1,0);
1329 digitsOut->Compress(1,0);
1330 for (iDict = 0; iDict < kNDict; iDict++) {
1331 dictionaryIn[iDict]->Compress(1,0);
1332 dictionaryOut[iDict]->Compress(1,0);
1333 }
1334 }
1335
e23fbb27 1336 delete [] inADC;
1337 delete [] outADC;
1338
abaf1f1d 1339 }
f7336fa3 1340
1341 return kTRUE;
1342
1343}
1344
16bf9884 1345//_____________________________________________________________________________
abaf1f1d 1346Bool_t AliTRDdigitizer::MergeSDigits()
16bf9884 1347{
1348 //
abaf1f1d 1349 // Merges the input s-digits:
1350 // - The amplitude of the different inputs are summed up.
1351 // - Of the track IDs from the input dictionaries only one is
1352 // kept for each input. This works for maximal 3 different merged inputs.
16bf9884 1353 //
1354
abaf1f1d 1355 // Number of track dictionary arrays
1356 const Int_t kNDict = AliTRDdigitsManager::kNDict;
1357
4487dad0 1358 if (!fPar) {
1359 fPar = new AliTRDparameter("TRDparameter","Standard parameter");
1360 if (fDebug > 0) {
1361 printf("<AliTRDdigitizer::MergeSDigits> ");
1362 printf("Create the default parameter object\n");
1363 }
1364 }
1365
abaf1f1d 1366 Int_t iDict = 0;
e23fbb27 1367 Int_t jDict = 0;
abaf1f1d 1368
1369 AliTRDdataArrayI *digitsA;
1370 AliTRDdataArrayI *digitsB;
1371 AliTRDdataArrayI *dictionaryA[kNDict];
1372 AliTRDdataArrayI *dictionaryB[kNDict];
1373
1374 // Get the first s-digits
1375 fSDigitsManager = (AliTRDdigitsManager *) fSDigitsManagerList->First();
1376 if (!fSDigitsManager) return kFALSE;
1377
1378 // Loop through the other sets of s-digits
1379 AliTRDdigitsManager *mergeSDigitsManager;
1380 mergeSDigitsManager = (AliTRDdigitsManager *)
1381 fSDigitsManagerList->After(fSDigitsManager);
1382
e23fbb27 1383 if (fDebug > 0) {
abaf1f1d 1384 if (mergeSDigitsManager) {
e23fbb27 1385 printf("<AliTRDdigitizer::MergeSDigits> ");
1386 printf("Merge %d input files.\n",fSDigitsManagerList->GetSize());
abaf1f1d 1387 }
1388 else {
e23fbb27 1389 printf("<AliTRDdigitizer::MergeSDigits> ");
abaf1f1d 1390 printf("Only one input file.\n");
1391 }
1392 }
1393
1394 Int_t iMerge = 0;
1395 while (mergeSDigitsManager) {
1396
1397 iMerge++;
1398
1399 // Loop through the detectors
1400 for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
1401
1402 Int_t plane = fGeo->GetPlane(iDet);
1403 Int_t sector = fGeo->GetSector(iDet);
1404 Int_t chamber = fGeo->GetChamber(iDet);
4487dad0 1405 Int_t nRowMax = fPar->GetRowMax(plane,chamber,sector);
1406 Int_t nColMax = fPar->GetColMax(plane);
1407 Int_t nTimeTotal = fPar->GetTimeTotal();
abaf1f1d 1408
1409 // Loop through the pixels of one detector and add the signals
1410 digitsA = fSDigitsManager->GetDigits(iDet);
1411 digitsB = mergeSDigitsManager->GetDigits(iDet);
1412 digitsA->Expand();
1413 digitsB->Expand();
1414 for (iDict = 0; iDict < kNDict; iDict++) {
1415 dictionaryA[iDict] = fSDigitsManager->GetDictionary(iDet,iDict);
1416 dictionaryB[iDict] = mergeSDigitsManager->GetDictionary(iDet,iDict);
1417 dictionaryA[iDict]->Expand();
1418 dictionaryB[iDict]->Expand();
1419 }
1420
4487dad0 1421 // Merge only detectors that contain a signal
1422 Bool_t doMerge = kTRUE;
1423 if (fMergeSignalOnly) {
1424 if (digitsA->GetOverThreshold(0) == 0) {
1425 doMerge = kFALSE;
1426 }
abaf1f1d 1427 }
1428
4487dad0 1429 if (doMerge) {
abaf1f1d 1430
4487dad0 1431 if (fDebug > 0) {
1432 printf("<AliTRDdigitizer::MergeSDigits> ");
1433 printf("Merge detector %d of input no.%d\n",iDet,iMerge+1);
1434 }
abaf1f1d 1435
4487dad0 1436 for (Int_t iRow = 0; iRow < nRowMax; iRow++ ) {
1437 for (Int_t iCol = 0; iCol < nColMax; iCol++ ) {
1438 for (Int_t iTime = 0; iTime < nTimeTotal; iTime++) {
1439
1440 // Add the amplitudes of the summable digits
1441 Int_t ampA = digitsA->GetDataUnchecked(iRow,iCol,iTime);
1442 Int_t ampB = digitsB->GetDataUnchecked(iRow,iCol,iTime);
1443 ampA += ampB;
1444 digitsA->SetDataUnchecked(iRow,iCol,iTime,ampA);
1445
1446 // Add the mask to the track id if defined.
1447 for (iDict = 0; iDict < kNDict; iDict++) {
1448 Int_t trackB = dictionaryB[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1449 if ((fMasks) && (trackB > 0)) {
1450 for (jDict = 0; jDict < kNDict; jDict++) {
1451 Int_t trackA = dictionaryA[iDict]->GetDataUnchecked(iRow,iCol,iTime);
1452 if (trackA == 0) {
1453 trackA = trackB + fMasks[iMerge];
1454 dictionaryA[iDict]->SetDataUnchecked(iRow,iCol,iTime,trackA);
1455 }
1456 }
1457 }
e23fbb27 1458 }
abaf1f1d 1459
4487dad0 1460 }
abaf1f1d 1461 }
4487dad0 1462 }
1463
abaf1f1d 1464 }
1465
1466 if (fCompress) {
1467 digitsA->Compress(1,0);
1468 digitsB->Compress(1,0);
1469 for (iDict = 0; iDict < kNDict; iDict++) {
1470 dictionaryA[iDict]->Compress(1,0);
1471 dictionaryB[iDict]->Compress(1,0);
1472 }
1473 }
1474
1475 }
1476
1477 // The next set of s-digits
1478 mergeSDigitsManager = (AliTRDdigitsManager *)
1479 fSDigitsManagerList->After(mergeSDigitsManager);
1480
1481 }
1482
16bf9884 1483 return kTRUE;
1484
1485}
1486
abaf1f1d 1487//_____________________________________________________________________________
1488Bool_t AliTRDdigitizer::SDigits2Digits()
1489{
1490 //
1491 // Merges the input s-digits and converts them to normal digits
1492 //
1493
1494 if (!MergeSDigits()) return kFALSE;
1495
1496 return ConvertSDigits();
1497
1498}
1499
793ff80c 1500//_____________________________________________________________________________
1501Bool_t AliTRDdigitizer::CheckDetector(Int_t plane, Int_t chamber, Int_t sector)
1502{
1503 //
1504 // Checks whether a detector is enabled
1505 //
1506
0c24ba98 1507 if (fSimpleSim) return kTRUE;
1508
793ff80c 1509 if ((fTRD->GetSensChamber() >= 0) &&
1510 (fTRD->GetSensChamber() != chamber)) return kFALSE;
1511 if ((fTRD->GetSensPlane() >= 0) &&
c1e4b257 1512 (fTRD->GetSensPlane() != plane)) return kFALSE;
793ff80c 1513 if ( fTRD->GetSensSector() >= 0) {
1514 Int_t sens1 = fTRD->GetSensSector();
1515 Int_t sens2 = sens1 + fTRD->GetSensSectorRange();
1516 sens2 -= ((Int_t) (sens2 / AliTRDgeometry::Nsect()))
1517 * AliTRDgeometry::Nsect();
1518 if (sens1 < sens2) {
1519 if ((sector < sens1) || (sector >= sens2)) return kFALSE;
1520 }
1521 else {
1522 if ((sector < sens1) && (sector >= sens2)) return kFALSE;
1523 }
1524 }
1525
1526 return kTRUE;
1527
1528}
1529
f7336fa3 1530//_____________________________________________________________________________
0a29d0f1 1531Bool_t AliTRDdigitizer::WriteDigits() const
f7336fa3 1532{
1533 //
1534 // Writes out the TRD-digits and the dictionaries
1535 //
1536
bdbb05bb 1537 //Write parameters
1538 fRunLoader->CdGAFile();
1539 if (!gFile->Get("TRDparameter")) GetParameter()->Write();
1540
da581aea 1541 // Store the digits and the dictionary in the tree
abaf1f1d 1542 return fDigitsManager->WriteDigits();
f7336fa3 1543
1544}
793ff80c 1545
e23fbb27 1546//_____________________________________________________________________________
1547void AliTRDdigitizer::DeConvExp(Double_t *source, Double_t *target
1548 , Int_t n, Int_t nexp)
1549{
1550 //
1551 // Does the deconvolution by the digital filter.
1552 //
1553 // Author: Marcus Gutfleisch, KIP Heidelberg
1554 // Optimized for: New TRF from Venelin Angelov, simulated with CADENCE
1555 // Pad-ground capacitance = 25 pF
1556 // Pad-pad cross talk capacitance = 6 pF
1557 // For 10 MHz digitization, corresponding to 20 time bins
1558 // in the drift region
1559 //
1560
1561 Double_t rates[2];
1562 Double_t coefficients[2];
81a6930e 1563 Double_t Dt = 0.100; // time bin width [mus] for 10 MHz sampling frequence
e23fbb27 1564
1565 /* initialize (coefficient = alpha, rates = lambda) */
1566
81a6930e 1567 rates[0] = 0.7680;
1568 rates[1] = 0.0995;
1569
1570 rates[0] = TMath::Exp(-Dt/(rates[0]));
1571 rates[1] = TMath::Exp(-Dt/(rates[1]));
1572
1573 // dummy initialization
1574 coefficients[0] = 0.0000;
1575 coefficients[1] = 0.0000;
1576
e23fbb27 1577 if( nexp == 1 ) {
81a6930e 1578 coefficients[0] = 0.0844;
1579 coefficients[1] = 0.0000;
e23fbb27 1580 }
1581 if( nexp == 2 ) {
81a6930e 1582 coefficients[0] = 0.1445;
1583 coefficients[1] = 0.7524;
e23fbb27 1584 }
1585
1586 Int_t i, k;
1587 Double_t reminder[2];
1588 Double_t correction, result;
1589
1590 /* attention: computation order is important */
1591 correction=0.0;
1592 for ( k = 0; k < nexp; k++ ) reminder[k]=0.0;
1593
1594 for ( i = 0; i < n; i++ ) {
1595 result = ( source[i] - correction ); /* no rescaling */
1596 target[i] = result;
1597
1598 for ( k = 0; k < nexp; k++ ) reminder[k] = rates[k]
1599 * ( reminder[k] + coefficients[k] * result);
1600
1601 correction=0.0;
1602 for ( k = 0; k < nexp; k++ ) correction += reminder[k];
1603 }
1604
1605}
1606
8e64dd77 1607//_____________________________________________________________________________
88cb7938 1608void AliTRDdigitizer::InitOutput(Int_t iEvent)
8e64dd77 1609{
1610 //
1611 // Initializes the output branches
1612 //
1613
1614 fEvent = iEvent;
88cb7938 1615
1616 if (!fRunLoader)
1617 {
1618 Error("InitOutput","Run Loader is NULL");
1619 return;
1620 }
1621 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
1622 if (!loader)
1623 {
1624 Error("Open","Can not get TRD loader from Run Loader");
1625 return;
1626 }
1627
1628 TTree* tree = 0;
1629
1630 if (fSDigits)
1631 {
1632 //if we produce SDigits
1633 tree = loader->TreeS();
1634 if (!tree)
1635 {
1636 loader->MakeTree("S");
1637 tree = loader->TreeS();
1638 }
1639 }
1640 else
1641 {//if we produce Digits
1642 tree = loader->TreeD();
1643 if (!tree)
1644 {
1645 loader->MakeTree("D");
1646 tree = loader->TreeD();
1647 }
1648 }
1649 fDigitsManager->SetEvent(iEvent);
1650 fDigitsManager->MakeBranch(tree);
8e64dd77 1651
1652}