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