Grounding foil converted to an assembly to avoid overlaps with residual misalignment...
[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
625f5260 18////////////////////////////////////////////////////////////////////////////
19// //
20// Creates and handles digits from TRD hits //
21// //
22// Authors: C. Blume (blume@ikf.uni-frankfurt.de) //
23// C. Lippmann //
24// B. Vulpescu //
25// //
26// The following effects are included: //
27// - Diffusion //
28// - ExB effects //
29// - Gas gain including fluctuations //
30// - Pad-response (simple Gaussian approximation) //
31// - Time-response //
32// - Electronics noise //
33// - Electronics gain //
34// - Digitization //
b65e5048 35// - Zero suppression //
625f5260 36// //
37////////////////////////////////////////////////////////////////////////////
6798b56e 38
793ff80c 39#include <TF1.h>
f7a1cc68 40#include <TFile.h>
41#include <TGeoGlobalMagField.h>
42#include <TGeoManager.h>
abaf1f1d 43#include <TList.h>
f7a1cc68 44#include <TMath.h>
45#include <TROOT.h>
46#include <TRandom.h>
85cbec76 47#include <TTask.h>
f7a1cc68 48#include <TTree.h>
49#include <TVector.h>
26d3c914 50
793ff80c 51#include "AliRun.h"
3e2e3ece 52#include "AliMC.h"
88cb7938 53#include "AliRunLoader.h"
54#include "AliLoader.h"
55#include "AliConfig.h"
db30bf0f 56#include "AliMagF.h"
85cbec76 57#include "AliRunDigitizer.h"
88cb7938 58#include "AliRunLoader.h"
59#include "AliLoader.h"
ad2cc20b 60#include "AliLog.h"
f7336fa3 61#include "AliTRD.h"
793ff80c 62#include "AliTRDhit.h"
f7336fa3 63#include "AliTRDdigitizer.h"
b65e5048 64
65#include "AliTRDarrayDictionary.h"
66#include "AliTRDarrayADC.h"
67#include "AliTRDarraySignal.h"
da581aea 68#include "AliTRDdigitsManager.h"
793ff80c 69#include "AliTRDgeometry.h"
a5cadd36 70#include "AliTRDpadPlane.h"
3551db50 71#include "AliTRDcalibDB.h"
72#include "AliTRDSimParam.h"
73#include "AliTRDCommonParam.h"
ecf39416 74#include "AliTRDfeeParam.h"
b65e5048 75#include "AliTRDmcmSim.h"
4329977a 76#include "Cal/AliTRDCalROC.h"
77#include "Cal/AliTRDCalDet.h"
78
f7336fa3 79ClassImp(AliTRDdigitizer)
80
81//_____________________________________________________________________________
85cbec76 82AliTRDdigitizer::AliTRDdigitizer()
4d18a639 83 :AliDigitizer()
84 ,fRunLoader(0)
85 ,fDigitsManager(0)
86 ,fSDigitsManager(0)
87 ,fSDigitsManagerList(0)
88 ,fTRD(0)
89 ,fGeo(0)
90 ,fEvent(0)
91 ,fMasks(0)
92 ,fCompress(kTRUE)
93 ,fSDigits(kFALSE)
4d18a639 94 ,fMergeSignalOnly(kFALSE)
f7336fa3 95{
96 //
97 // AliTRDdigitizer default constructor
98 //
3551db50 99
100 Init();
3becff3c 101
f7336fa3 102}
103
104//_____________________________________________________________________________
4d18a639 105AliTRDdigitizer::AliTRDdigitizer(const Text_t *name, const Text_t *title)
106 :AliDigitizer(name,title)
107 ,fRunLoader(0)
108 ,fDigitsManager(0)
109 ,fSDigitsManager(0)
110 ,fSDigitsManagerList(0)
111 ,fTRD(0)
112 ,fGeo(0)
113 ,fEvent(0)
114 ,fMasks(0)
115 ,fCompress(kTRUE)
116 ,fSDigits(kFALSE)
4d18a639 117 ,fMergeSignalOnly(kFALSE)
f7336fa3 118{
119 //
85cbec76 120 // AliTRDdigitizer constructor
121 //
122
3551db50 123 Init();
3becff3c 124
85cbec76 125}
126
127//_____________________________________________________________________________
128AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager
4d18a639 129 , const Text_t *name, const Text_t *title)
130 :AliDigitizer(manager,name,title)
131 ,fRunLoader(0)
132 ,fDigitsManager(0)
133 ,fSDigitsManager(0)
134 ,fSDigitsManagerList(0)
135 ,fTRD(0)
136 ,fGeo(0)
137 ,fEvent(0)
138 ,fMasks(0)
139 ,fCompress(kTRUE)
140 ,fSDigits(kFALSE)
4d18a639 141 ,fMergeSignalOnly(kFALSE)
85cbec76 142{
143 //
144 // AliTRDdigitizer constructor
f7336fa3 145 //
146
3551db50 147 Init();
3becff3c 148
bfc40adc 149}
150
151//_____________________________________________________________________________
152AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager)
4d18a639 153 :AliDigitizer(manager,"AliTRDdigitizer","TRD digitizer")
154 ,fRunLoader(0)
155 ,fDigitsManager(0)
156 ,fSDigitsManager(0)
157 ,fSDigitsManagerList(0)
158 ,fTRD(0)
159 ,fGeo(0)
160 ,fEvent(0)
161 ,fMasks(0)
162 ,fCompress(kTRUE)
163 ,fSDigits(kFALSE)
4d18a639 164 ,fMergeSignalOnly(kFALSE)
bfc40adc 165{
166 //
167 // AliTRDdigitizer constructor
168 //
169
3551db50 170 Init();
3becff3c 171
3551db50 172}
88cb7938 173
3551db50 174//_____________________________________________________________________________
175Bool_t AliTRDdigitizer::Init()
176{
3becff3c 177 //
178 // Initialize the digitizer with default values
179 //
180
85a5290f 181 fRunLoader = 0;
4487dad0 182 fDigitsManager = 0;
4487dad0 183 fSDigitsManager = 0;
85a5290f 184 fSDigitsManagerList = 0;
4487dad0 185 fTRD = 0;
186 fGeo = 0;
3becff3c 187
abaf1f1d 188 fEvent = 0;
85a5290f 189 fMasks = 0;
abaf1f1d 190 fCompress = kTRUE;
abaf1f1d 191 fSDigits = kFALSE;
4487dad0 192 fMergeSignalOnly = kFALSE;
3551db50 193
3551db50 194 return AliDigitizer::Init();
3becff3c 195
f7336fa3 196}
197
198//_____________________________________________________________________________
4d18a639 199AliTRDdigitizer::AliTRDdigitizer(const AliTRDdigitizer &d)
200 :AliDigitizer(d)
201 ,fRunLoader(0)
202 ,fDigitsManager(0)
203 ,fSDigitsManager(0)
204 ,fSDigitsManagerList(0)
205 ,fTRD(0)
206 ,fGeo(0)
207 ,fEvent(0)
208 ,fMasks(0)
209 ,fCompress(d.fCompress)
210 ,fSDigits(d.fSDigits)
4d18a639 211 ,fMergeSignalOnly(d.fMergeSignalOnly)
8230f242 212{
213 //
214 // AliTRDdigitizer copy constructor
215 //
216
8230f242 217}
218
219//_____________________________________________________________________________
f7336fa3 220AliTRDdigitizer::~AliTRDdigitizer()
221{
8230f242 222 //
223 // AliTRDdigitizer destructor
224 //
f7336fa3 225
abaf1f1d 226 if (fDigitsManager) {
227 delete fDigitsManager;
4d18a639 228 fDigitsManager = 0;
f7336fa3 229 }
230
b65e5048 231 if (fDigitsManager) { //typo? fSDigitsManager?
4d18a639 232 delete fSDigitsManager;
233 fSDigitsManager = 0;
234 }
f7336fa3 235
abaf1f1d 236 if (fSDigitsManagerList) {
47b5729a 237 fSDigitsManagerList->Delete();
abaf1f1d 238 delete fSDigitsManagerList;
4487dad0 239 fSDigitsManagerList = 0;
e23fbb27 240 }
241
242 if (fMasks) {
243 delete [] fMasks;
4329977a 244 fMasks = 0;
e23fbb27 245 }
246
f162af62 247 if (fGeo) {
248 delete fGeo;
249 fGeo = 0;
250 }
251
f7336fa3 252}
253
254//_____________________________________________________________________________
dd9a6ee3 255AliTRDdigitizer &AliTRDdigitizer::operator=(const AliTRDdigitizer &d)
256{
257 //
258 // Assignment operator
259 //
260
625f5260 261 if (this != &d) {
262 ((AliTRDdigitizer &) d).Copy(*this);
263 }
4d18a639 264
dd9a6ee3 265 return *this;
266
267}
268
269//_____________________________________________________________________________
e0d47c25 270void AliTRDdigitizer::Copy(TObject &d) const
8230f242 271{
272 //
273 // Copy function
274 //
275
85a5290f 276 ((AliTRDdigitizer &) d).fRunLoader = 0;
4487dad0 277 ((AliTRDdigitizer &) d).fDigitsManager = 0;
85a5290f 278 ((AliTRDdigitizer &) d).fSDigitsManager = 0;
279 ((AliTRDdigitizer &) d).fSDigitsManagerList = 0;
4487dad0 280 ((AliTRDdigitizer &) d).fTRD = 0;
281 ((AliTRDdigitizer &) d).fGeo = 0;
85a5290f 282 ((AliTRDdigitizer &) d).fEvent = 0;
283 ((AliTRDdigitizer &) d).fMasks = 0;
abaf1f1d 284 ((AliTRDdigitizer &) d).fCompress = fCompress;
abaf1f1d 285 ((AliTRDdigitizer &) d).fSDigits = fSDigits;
4487dad0 286 ((AliTRDdigitizer &) d).fMergeSignalOnly = fMergeSignalOnly;
3becff3c 287
e23fbb27 288}
289
290//_____________________________________________________________________________
4d18a639 291void AliTRDdigitizer::Exec(Option_t *option)
e23fbb27 292{
293 //
294 // Executes the merging
295 //
296
297 Int_t iInput;
298
299 AliTRDdigitsManager *sdigitsManager;
300
301 TString optionString = option;
302 if (optionString.Contains("deb")) {
9afbd7de 303 AliLog::SetClassDebugLevel("AliTRDdigitizer",1);
7925de54 304 AliInfo("Called with debug option");
e23fbb27 305 }
306
cec4059b 307 // The AliRoot file is already connected by the manager
625f5260 308 AliRunLoader *inrl = 0x0;
88cb7938 309
9afbd7de 310 if (gAlice) {
7925de54 311 AliDebug(1,"AliRun object found on file.");
9afbd7de 312 }
4487dad0 313 else {
88cb7938 314 inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
315 inrl->LoadgAlice();
316 gAlice = inrl->GetAliRun();
9afbd7de 317 if (!gAlice) {
7925de54 318 AliError("Could not find AliRun object.")
9afbd7de 319 return;
320 }
4487dad0 321 }
cec4059b 322
e23fbb27 323 Int_t nInput = fManager->GetNinputs();
4d18a639 324 fMasks = new Int_t[nInput];
e23fbb27 325 for (iInput = 0; iInput < nInput; iInput++) {
326 fMasks[iInput] = fManager->GetMask(iInput);
327 }
e23fbb27 328
9afbd7de 329 //
e23fbb27 330 // Initialization
9afbd7de 331 //
e23fbb27 332
95867fd1 333 AliRunLoader *orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
334
67c67368 335 if (InitDetector()) {
9afbd7de 336
95867fd1 337 AliLoader *ogime = orl->GetLoader("TRDLoader");
67c67368 338
95867fd1 339 TTree *tree = 0;
9afbd7de 340 if (fSDigits) {
341 // If we produce SDigits
342 tree = ogime->TreeS();
343 if (!tree) {
344 ogime->MakeTree("S");
67c67368 345 tree = ogime->TreeS();
67c67368 346 }
9afbd7de 347 }
348 else {
349 // If we produce Digits
350 tree = ogime->TreeD();
351 if (!tree) {
352 ogime->MakeTree("D");
353 tree = ogime->TreeD();
67c67368 354 }
9afbd7de 355 }
356
67c67368 357 MakeBranch(tree);
9afbd7de 358
67c67368 359 }
360
e23fbb27 361 for (iInput = 0; iInput < nInput; iInput++) {
362
7925de54 363 AliDebug(1,Form("Add input stream %d",iInput));
e23fbb27 364
9afbd7de 365 // Check if the input tree exists
88cb7938 366 inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
4d18a639 367 AliLoader *gime = inrl->GetLoader("TRDLoader");
88cb7938 368
4d18a639 369 TTree *treees = gime->TreeS();
9afbd7de 370 if (treees == 0x0) {
371 if (gime->LoadSDigits()) {
372 AliError(Form("Error Occured while loading S. Digits for input %d.",iInput));
373 return;
374 }
4d18a639 375 treees = gime->TreeS();
9afbd7de 376 }
88cb7938 377
378 if (treees == 0x0) {
7925de54 379 AliError(Form("Input stream %d does not exist",iInput));
c57e2264 380 return;
381 }
382
e23fbb27 383 // Read the s-digits via digits manager
384 sdigitsManager = new AliTRDdigitsManager();
e23fbb27 385 sdigitsManager->SetSDigits(kTRUE);
88cb7938 386
4d18a639 387 AliRunLoader *rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
388 AliLoader *gimme = rl->GetLoader("TRDLoader");
b65e5048 389 if (!gimme->TreeS())
390 {
391 gimme->LoadSDigits();
392 }
e23fbb27 393
b65e5048 394 sdigitsManager->ReadDigits(gimme->TreeS());
395
e23fbb27 396 // Add the s-digits to the input list
397 AddSDigitsManager(sdigitsManager);
398
399 }
400
401 // Convert the s-digits to normal digits
7925de54 402 AliDebug(1,"Do the conversion");
e23fbb27 403 SDigits2Digits();
404
405 // Store the digits
7925de54 406 AliDebug(1,"Write the digits");
e23fbb27 407 fDigitsManager->WriteDigits();
88cb7938 408
9afbd7de 409 // Write parameters
67c67368 410 orl->CdGAFile();
67c67368 411
26d3c914 412 // Clean up
b1113c6b 413 DeleteSDigitsManager();
414
26d3c914 415 AliDebug(1,"Done");
416
e23fbb27 417}
418
419//_____________________________________________________________________________
e23fbb27 420Bool_t AliTRDdigitizer::Open(const Char_t *file, Int_t nEvent)
f7336fa3 421{
422 //
423 // Opens a ROOT-file with TRD-hits and reads in the hit-tree
424 //
f7336fa3 425 // Connect the AliRoot file containing Geometry, Kine, and Hits
3becff3c 426 //
f540341d 427
e191bb57 428 TString evfoldname = AliConfig::GetDefaultEventFolderName();
9afbd7de 429
f540341d 430 fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
9afbd7de 431 if (!fRunLoader) {
432 fRunLoader = AliRunLoader::Open(file,evfoldname,"UPDATE");
433 }
9afbd7de 434 if (!fRunLoader) {
95867fd1 435 AliError(Form("Can not open session for file %s.",file));
436 return kFALSE;
437 }
438
439 if (!fRunLoader->GetAliRun()) {
440 fRunLoader->LoadgAlice();
441 }
442 gAlice = fRunLoader->GetAliRun();
443
444 if (gAlice) {
7925de54 445 AliDebug(1,"AliRun object found on file.");
95867fd1 446 }
447 else {
7925de54 448 AliError("Could not find AliRun object.");
95867fd1 449 return kFALSE;
450 }
451
452 fEvent = nEvent;
453
454 AliLoader *loader = fRunLoader->GetLoader("TRDLoader");
455 if (!loader) {
456 AliError("Can not get TRD loader from Run Loader");
457 return kFALSE;
458 }
459
460 if (InitDetector()) {
461 TTree *tree = 0;
462 if (fSDigits) {
463 // If we produce SDigits
464 tree = loader->TreeS();
465 if (!tree) {
466 loader->MakeTree("S");
467 tree = loader->TreeS();
468 }
469 }
470 else {
471 // If we produce Digits
472 tree = loader->TreeD();
473 if (!tree) {
474 loader->MakeTree("D");
475 tree = loader->TreeD();
476 }
477 }
478 return MakeBranch(tree);
479 }
480 else {
481 return kFALSE;
482 }
483
484}
485
486//_____________________________________________________________________________
487Bool_t AliTRDdigitizer::Open(AliRunLoader *runLoader, Int_t nEvent)
488{
489 //
490 // Opens a ROOT-file with TRD-hits and reads in the hit-tree
491 //
492 // Connect the AliRoot file containing Geometry, Kine, and Hits
493 //
494
495 fRunLoader = runLoader;
496 if (!fRunLoader) {
497 AliError("RunLoader does not exist");
498 return kFALSE;
4d18a639 499 }
88cb7938 500
9afbd7de 501 if (!fRunLoader->GetAliRun()) {
502 fRunLoader->LoadgAlice();
503 }
88cb7938 504 gAlice = fRunLoader->GetAliRun();
505
da581aea 506 if (gAlice) {
7925de54 507 AliDebug(1,"AliRun object found on file.");
da581aea 508 }
509 else {
7925de54 510 AliError("Could not find AliRun object.");
da581aea 511 return kFALSE;
512 }
f7336fa3 513
514 fEvent = nEvent;
515
4d18a639 516 AliLoader *loader = fRunLoader->GetLoader("TRDLoader");
9afbd7de 517 if (!loader) {
518 AliError("Can not get TRD loader from Run Loader");
519 return kFALSE;
520 }
88cb7938 521
abaf1f1d 522 if (InitDetector()) {
4d18a639 523 TTree *tree = 0;
9afbd7de 524 if (fSDigits) {
525 // If we produce SDigits
526 tree = loader->TreeS();
527 if (!tree) {
528 loader->MakeTree("S");
529 tree = loader->TreeS();
4d18a639 530 }
9afbd7de 531 }
532 else {
533 // If we produce Digits
95867fd1 534 tree = loader->TreeD();
9afbd7de 535 if (!tree) {
536 loader->MakeTree("D");
537 tree = loader->TreeD();
4d18a639 538 }
9afbd7de 539 }
88cb7938 540 return MakeBranch(tree);
abaf1f1d 541 }
542 else {
543 return kFALSE;
544 }
793ff80c 545
546}
547
548//_____________________________________________________________________________
549Bool_t AliTRDdigitizer::InitDetector()
550{
551 //
552 // Sets the pointer to the TRD detector and the geometry
553 //
554
dd9a6ee3 555 // Get the pointer to the detector class and check for version 1
4487dad0 556 fTRD = (AliTRD *) gAlice->GetDetector("TRD");
cec4059b 557 if (!fTRD) {
7925de54 558 AliFatal("No TRD module found");
cec4059b 559 exit(1);
560 }
dd9a6ee3 561 if (fTRD->IsVersion() != 1) {
7925de54 562 AliFatal("TRD must be version 1 (slow simulator)");
dd9a6ee3 563 exit(1);
564 }
565
566 // Get the geometry
f162af62 567 fGeo = new AliTRDgeometry();
dd9a6ee3 568
abaf1f1d 569 // Create a digits manager
625f5260 570 if (fDigitsManager) {
571 delete fDigitsManager;
572 }
abaf1f1d 573 fDigitsManager = new AliTRDdigitsManager();
574 fDigitsManager->SetSDigits(fSDigits);
575 fDigitsManager->CreateArrays();
576 fDigitsManager->SetEvent(fEvent);
abaf1f1d 577
578 // The list for the input s-digits manager to be merged
47b5729a 579 if (fSDigitsManagerList) {
580 fSDigitsManagerList->Delete();
3becff3c 581 }
582 else {
47b5729a 583 fSDigitsManagerList = new TList();
584 }
abaf1f1d 585
4487dad0 586 return kTRUE;
f7336fa3 587
588}
f7336fa3 589//_____________________________________________________________________________
95867fd1 590Bool_t AliTRDdigitizer::MakeBranch(TTree *tree) const
6244debe 591{
abaf1f1d 592 //
593 // Create the branches for the digits array
6244debe 594 //
595
88cb7938 596 return fDigitsManager->MakeBranch(tree);
6244debe 597
598}
599
600//_____________________________________________________________________________
625f5260 601void AliTRDdigitizer::AddSDigitsManager(AliTRDdigitsManager *man)
602{
603 //
604 // Add a digits manager for s-digits to the input list.
605 //
606
607 fSDigitsManagerList->Add(man);
608
609}
610
611//_____________________________________________________________________________
612void AliTRDdigitizer::DeleteSDigitsManager()
613{
614 //
615 // Removes digits manager from the input list.
616 //
617
618 fSDigitsManagerList->Delete();
619
620}
621
622//_____________________________________________________________________________
f7336fa3 623Bool_t AliTRDdigitizer::MakeDigits()
624{
625 //
872a7aba 626 // Creates digits.
f7336fa3 627 //
628
625f5260 629 AliDebug(1,"Start creating digits");
630
631 if (!fGeo) {
632 AliError("No geometry defined");
633 return kFALSE;
634 }
f7336fa3 635
625f5260 636 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
637 if (!calibration) {
638 AliFatal("Could not get calibration object");
639 return kFALSE;
640 }
641
642 const Int_t kNdet = AliTRDgeometry::Ndet();
643
644 Float_t **hits = new Float_t*[kNdet];
645 Int_t *nhit = new Int_t[kNdet];
646
b65e5048 647 AliTRDarraySignal *signals = 0x0;
625f5260 648
649 // Sort all hits according to detector number
650 if (!SortHits(hits,nhit)) {
651 AliError("Sorting hits failed");
652 return kFALSE;
653 }
654
655 // Loop through all detectors
656 for (Int_t det = 0; det < kNdet; det++) {
657
658 // Detectors that are switched off, not installed, etc.
659 if (( calibration->IsChamberInstalled(det)) &&
b65e5048 660 (!calibration->IsChamberMasked(det)) &&
625f5260 661 ( fGeo->ChamberInGeometry(det)) &&
662 (nhit[det] > 0)) {
663
b65e5048 664 signals = new AliTRDarraySignal();
665
625f5260 666 // Convert the hits of the current detector to detector signals
667 if (!ConvertHits(det,hits[det],nhit[det],signals)) {
b65e5048 668 AliError(Form("Conversion of hits failed for detector=%d",det));
625f5260 669 return kFALSE;
670 }
671 // Convert the detector signals to digits or s-digits
672 if (!ConvertSignals(det,signals)) {
b65e5048 673 AliError(Form("Conversion of signals failed for detector=%d",det));
674 return kFALSE;
625f5260 675 }
676
677 // Delete the signals array
678 delete signals;
679 signals = 0x0;
680
681 } // if: detector status
682
27cea06a 683 delete [] hits[det];
684
625f5260 685 } // for: detector
686
687 delete [] hits;
688 delete [] nhit;
689
690 return kTRUE;
691
692}
693
694//_____________________________________________________________________________
695Bool_t AliTRDdigitizer::SortHits(Float_t **hits, Int_t *nhit)
696{
697 //
698 // Read all the hits and sorts them according to detector number
699 // in the output array <hits>.
700 //
701
702 AliDebug(1,"Start sorting hits");
703
704 const Int_t kNdet = AliTRDgeometry::Ndet();
705 // Size of the hit vector
706 const Int_t kNhit = 6;
707
708 Float_t *xyz = 0;
709 Int_t nhitTrk = 0;
710
711 Int_t *lhit = new Int_t[kNdet];
712
713 for (Int_t det = 0; det < kNdet; det++) {
714 lhit[det] = 0;
715 nhit[det] = 0;
716 hits[det] = 0;
717 }
718
719 AliLoader *gimme = fRunLoader->GetLoader("TRDLoader");
720 if (!gimme->TreeH()) {
721 gimme->LoadHits();
722 }
723 TTree *hitTree = gimme->TreeH();
724 if (hitTree == 0x0) {
725 AliError("Can not get TreeH");
726 return kFALSE;
727 }
728 fTRD->SetTreeAddress();
729
730 // Get the number of entries in the hit tree
731 // (Number of primary particles creating a hit somewhere)
732 Int_t nTrk = (Int_t) hitTree->GetEntries();
733 AliDebug(1,Form("Found %d tracks",nTrk));
734
735 // Loop through all the tracks in the tree
736 for (Int_t iTrk = 0; iTrk < nTrk; iTrk++) {
737
3e2e3ece 738 gAlice->GetMCApp()->ResetHits();
625f5260 739 hitTree->GetEvent(iTrk);
740
741 if (!fTRD->Hits()) {
742 AliError(Form("No hits array for track = %d",iTrk));
743 continue;
744 }
745
746 // Number of hits for this track
747 nhitTrk = fTRD->Hits()->GetEntriesFast();
748
749 Int_t hitCnt = 0;
750 // Loop through the TRD hits
751 AliTRDhit *hit = (AliTRDhit *) fTRD->FirstHit(-1);
752 while (hit) {
753
754 hitCnt++;
755
756 // Don't analyze test hits
757 if (((Int_t) hit->GetCharge()) != 0) {
758
759 Int_t trk = hit->Track();
760 Int_t det = hit->GetDetector();
761 Int_t q = hit->GetCharge();
762 Float_t x = hit->X();
763 Float_t y = hit->Y();
764 Float_t z = hit->Z();
765 Float_t time = hit->GetTime();
766
767 if (nhit[det] == lhit[det]) {
768 // Inititialization of new detector
769 xyz = new Float_t[kNhit*(nhitTrk+lhit[det])];
770 if (hits[det]) {
771 memcpy(xyz,hits[det],sizeof(Float_t)*kNhit*lhit[det]);
772 delete [] hits[det];
773 }
774 lhit[det] += nhitTrk;
775 hits[det] = xyz;
776 }
777 else {
778 xyz = hits[det];
779 }
780 xyz[nhit[det]*kNhit+0] = x;
781 xyz[nhit[det]*kNhit+1] = y;
782 xyz[nhit[det]*kNhit+2] = z;
783 xyz[nhit[det]*kNhit+3] = q;
784 xyz[nhit[det]*kNhit+4] = trk;
785 xyz[nhit[det]*kNhit+5] = time;
786 nhit[det]++;
787
788 } // if: charge != 0
789
790 hit = (AliTRDhit *) fTRD->NextHit();
791
792 } // for: hits of one track
793
794 } // for: tracks
795
796 delete [] lhit;
797
798 return kTRUE;
799
800}
f7336fa3 801
625f5260 802//_____________________________________________________________________________
803Bool_t AliTRDdigitizer::ConvertHits(Int_t det, Float_t *hits, Int_t nhit
b65e5048 804 , AliTRDarraySignal *signals)
625f5260 805{
806 //
807 // Converts the detectorwise sorted hits to detector signals
808 //
809
810 AliDebug(1,Form("Start converting hits for detector=%d (nhits=%d)",det,nhit));
f7336fa3 811
793ff80c 812 // Number of pads included in the pad response
a5cadd36 813 const Int_t kNpad = 3;
793ff80c 814 // Number of track dictionary arrays
625f5260 815 const Int_t kNdict = AliTRDdigitsManager::kNDict;
816 // Size of the hit vector
817 const Int_t kNhit = 6;
793ff80c 818
4329977a 819 // Width of the amplification region
820 const Float_t kAmWidth = AliTRDgeometry::AmThick();
3becff3c 821 // Width of the drift region
ccb4315c 822 const Float_t kDrWidth = AliTRDgeometry::DrThick();
4329977a 823 // Drift + amplification region
824 const Float_t kDrMin = - 0.5 * kAmWidth;
825 const Float_t kDrMax = kDrWidth + 0.5 * kAmWidth;
3551db50 826
625f5260 827 Int_t iPad = 0;
828 Int_t dict = 0;
829 Int_t timeBinTRFend = 1;
872a7aba 830
a5cadd36 831 Double_t pos[3];
4329977a 832 Double_t loc[3];
a5cadd36 833 Double_t padSignal[kNpad];
834 Double_t signalOld[kNpad];
872a7aba 835
b65e5048 836 AliTRDarrayDictionary *dictionary[kNdict];
4329977a 837
9afbd7de 838 AliTRDSimParam *simParam = AliTRDSimParam::Instance();
9afbd7de 839 AliTRDCommonParam *commonParam = AliTRDCommonParam::Instance();
625f5260 840 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
841
3becff3c 842 if (!commonParam) {
4329977a 843 AliFatal("Could not get common parameterss");
3551db50 844 return kFALSE;
845 }
625f5260 846 if (!simParam) {
847 AliFatal("Could not get simulation parameters");
848 return kFALSE;
849 }
3becff3c 850 if (!calibration) {
4329977a 851 AliFatal("Could not get calibration object");
3551db50 852 return kFALSE;
853 }
854
625f5260 855 // Get the detector wise calibration objects
856 AliTRDCalROC *calVdriftROC = 0;
857 Float_t calVdriftDetValue = 0.0;
858 const AliTRDCalDet *calVdriftDet = calibration->GetVdriftDet();
859 AliTRDCalROC *calT0ROC = 0;
860 Float_t calT0DetValue = 0.0;
861 const AliTRDCalDet *calT0Det = calibration->GetT0Det();
4329977a 862
4329977a 863 if (simParam->TRFOn()) {
864 timeBinTRFend = ((Int_t) (simParam->GetTRFhi()
865 * commonParam->GetSamplingFrequency())) - 1;
4329977a 866 }
867
3becff3c 868 Int_t nTimeTotal = calibration->GetNumberOfTimeBins();
b43a3e17 869 Float_t samplingRate = commonParam->GetSamplingFrequency();
4329977a 870 Float_t elAttachProp = simParam->GetElAttachProp() / 100.0;
a328fff9 871
625f5260 872 AliTRDpadPlane *padPlane = fGeo->GetPadPlane(det);
b65e5048 873 Int_t layer = fGeo->GetLayer(det); //update
625f5260 874 Float_t row0 = padPlane->GetRow0ROC();
875 Int_t nRowMax = padPlane->GetNrows();
876 Int_t nColMax = padPlane->GetNcols();
877
878 // Create a new array for the signals
879 signals->Allocate(nRowMax,nColMax,nTimeTotal);
880
881 // Create a new array for the dictionary
882 for (dict = 0; dict < kNdict; dict++) {
b65e5048 883 dictionary[dict] = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(det,dict);
625f5260 884 dictionary[dict]->Allocate(nRowMax,nColMax,nTimeTotal);
885 }
886
887 // Loop through the hits in this detector
888 for (Int_t hit = 0; hit < nhit; hit++) {
889
890 pos[0] = hits[hit*kNhit+0];
891 pos[1] = hits[hit*kNhit+1];
892 pos[2] = hits[hit*kNhit+2];
893 Float_t q = hits[hit*kNhit+3];
894 Float_t hittime = hits[hit*kNhit+5];
895 Int_t track = ((Int_t) hits[hit*kNhit+4]);
896
897 Int_t inDrift = 1;
898
899 // Find the current volume with the geo manager
900 gGeoManager->SetCurrentPoint(pos);
901 gGeoManager->FindNode();
902 if (strstr(gGeoManager->GetPath(),"/UK")) {
903 inDrift = 0;
904 }
793ff80c 905
625f5260 906 // Get the calibration objects
907 calVdriftROC = calibration->GetVdriftROC(det);
908 calVdriftDetValue = calVdriftDet->GetValue(det);
909 calT0ROC = calibration->GetT0ROC(det);
910 calT0DetValue = calT0Det->GetValue(det);
911
912 // Go to the local coordinate system:
913 // loc[0] - col direction in amplification or driftvolume
914 // loc[1] - row direction in amplification or driftvolume
915 // loc[2] - time direction in amplification or driftvolume
916 gGeoManager->MasterToLocal(pos,loc);
917 if (inDrift) {
918 // Relative to middle of amplification region
919 loc[2] = loc[2] - kDrWidth/2.0 - kAmWidth/2.0;
920 }
793ff80c 921
625f5260 922 // The driftlength [cm] (w/o diffusion yet !).
923 // It is negative if the hit is between pad plane and anode wires.
924 Double_t driftlength = -1.0 * loc[2];
e23fbb27 925
625f5260 926 // Stupid patch to take care of TR photons that are absorbed
927 // outside the chamber volume. A real fix would actually need
928 // a more clever implementation of the TR hit generation
929 if (q < 0.0) {
930 if ((loc[1] < padPlane->GetRowEndROC()) ||
931 (loc[1] > padPlane->GetRow0ROC())) {
f881dc35 932 continue;
933 }
625f5260 934 if ((driftlength < kDrMin) ||
935 (driftlength > kDrMax)) {
936 continue;
4329977a 937 }
625f5260 938 }
f881dc35 939
625f5260 940 // Get row and col of unsmeared electron to retrieve drift velocity
941 // The pad row (z-direction)
942 Int_t rowE = padPlane->GetPadRowNumberROC(loc[1]);
943 if (rowE < 0) {
944 continue;
945 }
946 Double_t rowOffset = padPlane->GetPadRowOffsetROC(rowE,loc[1]);
947 // The pad column (rphi-direction)
948 Double_t offsetTilt = padPlane->GetTiltOffset(rowOffset);
949 Int_t colE = padPlane->GetPadColNumber(loc[0]+offsetTilt);
950 if (colE < 0) {
951 continue;
952 }
0a17cc30 953 Double_t colOffset = 0.0;
f881dc35 954
625f5260 955 // Normalized drift length
956 Float_t driftvelocity = calVdriftDetValue * calVdriftROC->GetValue(colE,rowE);
957 Double_t absdriftlength = TMath::Abs(driftlength);
958 if (commonParam->ExBOn()) {
959 absdriftlength /= TMath::Sqrt(GetLorentzFactor(driftvelocity));
960 }
961
962 // Loop over all electrons of this hit
963 // TR photons produce hits with negative charge
964 Int_t nEl = ((Int_t) TMath::Abs(q));
965 for (Int_t iEl = 0; iEl < nEl; iEl++) {
966
967 // Now the real local coordinate system of the ROC
968 // column direction: locC
969 // row direction: locR
970 // time direction: locT
971 // locR and locC are identical to the coordinates of the corresponding
972 // volumina of the drift or amplification region.
973 // locT is defined relative to the wire plane (i.e. middle of amplification
26d3c914 974 // region), meaning locT = 0, and is negative for hits coming from the
625f5260 975 // drift region.
976 Double_t locC = loc[0];
977 Double_t locR = loc[1];
978 Double_t locT = loc[2];
979
980 // Electron attachment
981 if (simParam->ElAttachOn()) {
982 if (gRandom->Rndm() < (absdriftlength * elAttachProp)) {
983 continue;
f881dc35 984 }
625f5260 985 }
986
987 // Apply the diffusion smearing
988 if (simParam->DiffusionOn()) {
989 if (!(Diffusion(driftvelocity,absdriftlength,locR,locC,locT))) {
990 continue;
f881dc35 991 }
f881dc35 992 }
793ff80c 993
625f5260 994 // Apply E x B effects (depends on drift direction)
995 if (commonParam->ExBOn()) {
996 if (!(ExB(driftvelocity,driftlength,locC))) {
4329977a 997 continue;
998 }
729161c5 999 }
1000
625f5260 1001 // The electron position after diffusion and ExB in pad coordinates.
4329977a 1002 // The pad row (z-direction)
625f5260 1003 rowE = padPlane->GetPadRowNumberROC(locR);
1004 if (rowE < 0) continue;
1005 rowOffset = padPlane->GetPadRowOffsetROC(rowE,locR);
4329977a 1006
1007 // The pad column (rphi-direction)
625f5260 1008 offsetTilt = padPlane->GetTiltOffset(rowOffset);
1009 colE = padPlane->GetPadColNumber(locC+offsetTilt);
1010 if (colE < 0) continue;
1011 colOffset = padPlane->GetPadColOffset(colE,locC+offsetTilt);
b65e5048 1012
625f5260 1013 // Also re-retrieve drift velocity because col and row may have changed
1014 driftvelocity = calVdriftDetValue * calVdriftROC->GetValue(colE,rowE);
1015 Float_t t0 = calT0DetValue + calT0ROC->GetValue(colE,rowE);
1016
1017 // Convert the position to drift time [mus], using either constant drift velocity or
1018 // time structure of drift cells (non-isochronity, GARFIELD calculation).
1019 // Also add absolute time of hits to take pile-up events into account properly
1020 Double_t drifttime;
1021 if (simParam->TimeStructOn()) {
1022 // Get z-position with respect to anode wire
58897a75 1023 Double_t zz = row0 - locR + padPlane->GetAnodeWireOffset();
625f5260 1024 zz -= ((Int_t)(2 * zz)) / 2.0;
1025 if (zz > 0.25) {
1026 zz = 0.5 - zz;
1027 }
1028 // Use drift time map (GARFIELD)
a076fc2f 1029 drifttime = commonParam->TimeStruct(driftvelocity,0.5*kAmWidth-1.0*locT,zz)
625f5260 1030 + hittime;
1031 }
1032 else {
1033 // Use constant drift velocity
26d3c914 1034 drifttime = TMath::Abs(locT) / driftvelocity
625f5260 1035 + hittime;
4329977a 1036 }
625f5260 1037
1038 // Apply the gas gain including fluctuations
1039 Double_t ggRndm = 0.0;
1040 do {
1041 ggRndm = gRandom->Rndm();
1042 } while (ggRndm <= 0);
eb52b657 1043 Double_t signal = -(simParam->GetGasGain()) * TMath::Log(ggRndm);
625f5260 1044
1045 // Apply the pad response
1046 if (simParam->PRFOn()) {
1047 // The distance of the electron to the center of the pad
1048 // in units of pad width
1049 Double_t dist = (colOffset - 0.5*padPlane->GetColSize(colE))
1050 / padPlane->GetColSize(colE);
1051 // This is a fixed parametrization, i.e. not dependent on
1052 // calibration values !
053767a4 1053 if (!(calibration->PadResponse(signal,dist,layer,padSignal))) continue;
625f5260 1054 }
1055 else {
1056 padSignal[0] = 0.0;
1057 padSignal[1] = signal;
1058 padSignal[2] = 0.0;
4329977a 1059 }
f881dc35 1060
625f5260 1061 // The time bin (always positive), with t0 distortion
1062 Double_t timeBinIdeal = drifttime * samplingRate + t0;
1063 // Protection
1064 if (TMath::Abs(timeBinIdeal) > 2*nTimeTotal) {
1065 timeBinIdeal = 2 * nTimeTotal;
1066 }
1067 Int_t timeBinTruncated = ((Int_t) timeBinIdeal);
1068 // The distance of the position to the middle of the timebin
1069 Double_t timeOffset = ((Float_t) timeBinTruncated
1070 + 0.5 - timeBinIdeal) / samplingRate;
3551db50 1071
625f5260 1072 // Sample the time response inside the drift region
1073 // + additional time bins before and after.
1074 // The sampling is done always in the middle of the time bin
1075 for (Int_t iTimeBin = TMath::Max(timeBinTruncated,0)
1076 ;iTimeBin < TMath::Min(timeBinTruncated+timeBinTRFend,nTimeTotal)
1077 ;iTimeBin++) {
1078
1079 // Apply the time response
1080 Double_t timeResponse = 1.0;
1081 Double_t crossTalk = 0.0;
1082 Double_t time = (iTimeBin - timeBinTruncated) / samplingRate + timeOffset;
58897a75 1083
625f5260 1084 if (simParam->TRFOn()) {
1085 timeResponse = simParam->TimeResponse(time);
f881dc35 1086 }
625f5260 1087 if (simParam->CTOn()) {
1088 crossTalk = simParam->CrossTalk(time);
f881dc35 1089 }
e23fbb27 1090
625f5260 1091 signalOld[0] = 0.0;
1092 signalOld[1] = 0.0;
1093 signalOld[2] = 0.0;
e23fbb27 1094
625f5260 1095 for (iPad = 0; iPad < kNpad; iPad++) {
f7336fa3 1096
625f5260 1097 Int_t colPos = colE + iPad - 1;
1098 if (colPos < 0) continue;
1099 if (colPos >= nColMax) break;
e23fbb27 1100
625f5260 1101 // Add the signals
b65e5048 1102 signalOld[iPad] = signals->GetData(rowE,colPos,iTimeBin);
e23fbb27 1103
625f5260 1104 if (colPos != colE) {
1105 // Cross talk added to non-central pads
1106 signalOld[iPad] += padSignal[iPad]
1107 * (timeResponse + crossTalk);
1108 }
1109 else {
1110 // W/o cross talk at central pad
1111 signalOld[iPad] += padSignal[iPad]
1112 * timeResponse;
1113 }
f881dc35 1114
b65e5048 1115 signals->SetData(rowE,colPos,iTimeBin,signalOld[iPad]);
625f5260 1116
1117 // Store the track index in the dictionary
1118 // Note: We store index+1 in order to allow the array to be compressed
b65e5048 1119 // Note2: Taking out the +1 in track
625f5260 1120 if (signalOld[iPad] > 0.0) {
1121 for (dict = 0; dict < kNdict; dict++) {
b65e5048 1122 Int_t oldTrack = dictionary[dict]->GetData(rowE,colPos,iTimeBin);
1123 if (oldTrack == track) break;
1124 if (oldTrack == -1 ) {
1125 dictionary[dict]->SetData(rowE,colPos,iTimeBin,track);
1126 break;
1127 }
f881dc35 1128 }
625f5260 1129 }
872a7aba 1130
625f5260 1131 } // Loop: pads
f7336fa3 1132
625f5260 1133 } // Loop: time bins
f7336fa3 1134
625f5260 1135 } // Loop: electrons of a single hit
f7336fa3 1136
625f5260 1137 } // Loop: hits
f7336fa3 1138
625f5260 1139 AliDebug(2,Form("Finished analyzing %d hits",nhit));
e23fbb27 1140
625f5260 1141 return kTRUE;
e23fbb27 1142
625f5260 1143}
793ff80c 1144
625f5260 1145//_____________________________________________________________________________
b65e5048 1146Bool_t AliTRDdigitizer::ConvertSignals(Int_t det, AliTRDarraySignal *signals)
625f5260 1147{
4329977a 1148 //
625f5260 1149 // Convert signals to digits
4329977a 1150 //
1151
625f5260 1152 AliDebug(1,Form("Start converting the signals for detector %d",det));
1153
1154 if (fSDigits) {
1155 // Convert the signal array to s-digits
1156 if (!Signal2SDigits(det,signals)) {
1157 return kFALSE;
793ff80c 1158 }
625f5260 1159 }
1160 else {
1161 // Convert the signal array to digits
1162 if (!Signal2ADC(det,signals)) {
1163 return kFALSE;
793ff80c 1164 }
625f5260 1165 }
f7336fa3 1166
625f5260 1167 // Compress the arrays
b65e5048 1168 CompressOutputArrays(det);
6244debe 1169
625f5260 1170 return kTRUE;
6244debe 1171
625f5260 1172}
4329977a 1173
625f5260 1174//_____________________________________________________________________________
b65e5048 1175Bool_t AliTRDdigitizer::Signal2ADC(Int_t det, AliTRDarraySignal *signals)
625f5260 1176{
1177 //
1178 // Converts the sampled electron signals to ADC values for a given chamber
1179 //
6244debe 1180
625f5260 1181 AliDebug(1,Form("Start converting signals to ADC values for detector=%d",det));
e23fbb27 1182
625f5260 1183 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
1184 if (!calibration) {
1185 AliFatal("Could not get calibration object");
1186 return kFALSE;
1187 }
793ff80c 1188
625f5260 1189 AliTRDSimParam *simParam = AliTRDSimParam::Instance();
1190 if (!simParam) {
1191 AliFatal("Could not get simulation parameters");
1192 return kFALSE;
1193 }
f7336fa3 1194
625f5260 1195 // Converts number of electrons to fC
1196 const Double_t kEl2fC = 1.602e-19 * 1.0e15;
0c24ba98 1197
625f5260 1198 // Coupling factor
1199 Double_t coupling = simParam->GetPadCoupling()
1200 * simParam->GetTimeCoupling();
1201 // Electronics conversion factor
1202 Double_t convert = kEl2fC
1203 * simParam->GetChipGain();
1204 // ADC conversion factor
1205 Double_t adcConvert = simParam->GetADCoutRange()
1206 / simParam->GetADCinRange();
1207 // The electronics baseline in mV
1208 Double_t baseline = simParam->GetADCbaseline()
1209 / adcConvert;
1210 // The electronics baseline in electrons
1211 Double_t baselineEl = baseline
1212 / convert;
1213
1214 Int_t row = 0;
1215 Int_t col = 0;
1216 Int_t time = 0;
1217
1218 Int_t nRowMax = fGeo->GetPadPlane(det)->GetNrows();
1219 Int_t nColMax = fGeo->GetPadPlane(det)->GetNcols();
1220 Int_t nTimeTotal = calibration->GetNumberOfTimeBins();
abaf1f1d 1221
625f5260 1222 // The gainfactor calibration objects
1223 const AliTRDCalDet *calGainFactorDet = calibration->GetGainFactorDet();
1224 AliTRDCalROC *calGainFactorROC = 0;
1225 Float_t calGainFactorDetValue = 0.0;
f7336fa3 1226
b65e5048 1227 AliTRDarrayADC *digits = 0x0;
e23fbb27 1228
625f5260 1229 if (!signals) {
1230 AliError(Form("Signals array for detector %d does not exist\n",det));
1231 return kFALSE;
1232 }
1233 if (signals->HasData()) {
1234 // Expand the container if neccessary
b65e5048 1235 signals->Expand();
625f5260 1236 }
1237 else {
1238 // Create missing containers
1239 signals->Allocate(nRowMax,nColMax,nTimeTotal);
f7336fa3 1240 }
1241
625f5260 1242 // Get the container for the digits of this detector
1243 if (fDigitsManager->HasSDigits()) {
1244 AliError("Digits manager has s-digits");
1245 return kFALSE;
1246 }
b65e5048 1247
1248 digits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det);
625f5260 1249 // Allocate memory space for the digits buffer
1250 if (!digits->HasData()) {
1251 digits->Allocate(nRowMax,nColMax,nTimeTotal);
0c24ba98 1252 }
1253
625f5260 1254 // Get the calibration objects
1255 calGainFactorROC = calibration->GetGainFactorROC(det);
1256 calGainFactorDetValue = calGainFactorDet->GetValue(det);
abaf1f1d 1257
625f5260 1258 // Create the digits for this chamber
1259 for (row = 0; row < nRowMax; row++ ) {
1260 for (col = 0; col < nColMax; col++ ) {
abaf1f1d 1261
625f5260 1262 // Check whether pad is masked
1263 // Bridged pads are not considered yet!!!
1264 if (calibration->IsPadMasked(det,col,row)) {
1265 continue;
1266 }
abaf1f1d 1267
625f5260 1268 // The gain factors
1269 Float_t padgain = calGainFactorDetValue
1270 * calGainFactorROC->GetValue(col,row);
1271 if (padgain <= 0) {
1272 AliError(Form("Not a valid gain %f, %d %d %d",padgain,det,col,row));
1273 }
abaf1f1d 1274
625f5260 1275 for (time = 0; time < nTimeTotal; time++) {
1276
1277 // Get the signal amplitude
b65e5048 1278 Float_t signalAmp = signals->GetData(row,col,time);
625f5260 1279 // Pad and time coupling
1280 signalAmp *= coupling;
1281 // Gain factors
1282 signalAmp *= padgain;
1283
1284 // Add the noise, starting from minus ADC baseline in electrons
1285 signalAmp = TMath::Max((Double_t) gRandom->Gaus(signalAmp,simParam->GetNoise())
1286 ,-baselineEl);
1287
1288 // Convert to mV
1289 signalAmp *= convert;
1290 // Add ADC baseline in mV
1291 signalAmp += baseline;
1292
1293 // Convert to ADC counts. Set the overflow-bit fADCoutRange if the
1294 // signal is larger than fADCinRange
1295 Short_t adc = 0;
1296 if (signalAmp >= simParam->GetADCinRange()) {
1297 adc = ((Short_t) simParam->GetADCoutRange());
1298 }
1299 else {
1300 adc = TMath::Nint(signalAmp * adcConvert);
1301 }
abaf1f1d 1302
b65e5048 1303 // Saving all digits
1304 digits->SetData(row,col,time,adc);
abaf1f1d 1305
625f5260 1306 } // for: time
b65e5048 1307
625f5260 1308 } // for: col
1309 } // for: row
b1113c6b 1310
b0a41e80 1311 // Run the digital processing in the MCM
1312 RunDigitalProcessing(digits, det);
b65e5048 1313
625f5260 1314 return kTRUE;
b1113c6b 1315
1316}
1317
1318//_____________________________________________________________________________
b65e5048 1319Bool_t AliTRDdigitizer::Signal2SDigits(Int_t det, AliTRDarraySignal *signals)
abaf1f1d 1320{
1321 //
625f5260 1322 // Converts the sampled electron signals to s-digits
abaf1f1d 1323 //
1324
625f5260 1325 AliDebug(1,Form("Start converting signals to s-digits for detector=%d",det));
abaf1f1d 1326
4329977a 1327 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
9afbd7de 1328 if (!calibration) {
4329977a 1329 AliFatal("Could not get calibration object");
3551db50 1330 return kFALSE;
1331 }
6bf5f0ce 1332
625f5260 1333 Int_t row = 0;
1334 Int_t col = 0;
1335 Int_t time = 0;
9afbd7de 1336
625f5260 1337 Int_t nRowMax = fGeo->GetPadPlane(det)->GetNrows();
1338 Int_t nColMax = fGeo->GetPadPlane(det)->GetNcols();
1339 Int_t nTimeTotal = calibration->GetNumberOfTimeBins();
e23fbb27 1340
625f5260 1341 // Get the container for the digits of this detector
b65e5048 1342
625f5260 1343 if (!fDigitsManager->HasSDigits()) {
1344 AliError("Digits manager has no s-digits");
1345 return kFALSE;
1346 }
b65e5048 1347
1348 AliTRDarraySignal *digits = (AliTRDarraySignal *) fDigitsManager->GetSDigits(det);
625f5260 1349 // Allocate memory space for the digits buffer
1350 if (!digits->HasData()) {
1351 digits->Allocate(nRowMax,nColMax,nTimeTotal);
1352 }
e23fbb27 1353
625f5260 1354 // Create the sdigits for this chamber
1355 for (row = 0; row < nRowMax; row++ ) {
1356 for (col = 0; col < nColMax; col++ ) {
1357 for (time = 0; time < nTimeTotal; time++) {
b65e5048 1358 digits->SetData(row,col,time,signals->GetData(row,col,time));
625f5260 1359 } // for: time
1360 } // for: col
1361 } // for: row
b65e5048 1362
625f5260 1363 return kTRUE;
abaf1f1d 1364
625f5260 1365}
abaf1f1d 1366
625f5260 1367//_____________________________________________________________________________
1368Bool_t AliTRDdigitizer::SDigits2Digits()
1369{
1370 //
1371 // Merges the input s-digits and converts them to normal digits
1372 //
e23fbb27 1373
625f5260 1374 if (!MergeSDigits()) {
1375 return kFALSE;
1376 }
f7336fa3 1377
625f5260 1378 return ConvertSDigits();
f7336fa3 1379
1380}
1381
1382//_____________________________________________________________________________
abaf1f1d 1383Bool_t AliTRDdigitizer::MergeSDigits()
16bf9884 1384{
1385 //
abaf1f1d 1386 // Merges the input s-digits:
1387 // - The amplitude of the different inputs are summed up.
1388 // - Of the track IDs from the input dictionaries only one is
1389 // kept for each input. This works for maximal 3 different merged inputs.
16bf9884 1390 //
1391
abaf1f1d 1392 // Number of track dictionary arrays
1393 const Int_t kNDict = AliTRDdigitsManager::kNDict;
1394
4d18a639 1395 AliTRDSimParam *simParam = AliTRDSimParam::Instance();
9afbd7de 1396 if (!simParam) {
4329977a 1397 AliFatal("Could not get simulation parameters");
3551db50 1398 return kFALSE;
1399 }
1400
4d18a639 1401 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
9afbd7de 1402 if (!calibration) {
4329977a 1403 AliFatal("Could not get calibration object");
3551db50 1404 return kFALSE;
1405 }
1406
abaf1f1d 1407 Int_t iDict = 0;
e23fbb27 1408 Int_t jDict = 0;
abaf1f1d 1409
b65e5048 1410 AliTRDarraySignal *digitsA;
1411 AliTRDarraySignal *digitsB;
1412 AliTRDarrayDictionary *dictionaryA[kNDict];
1413 AliTRDarrayDictionary *dictionaryB[kNDict];
abaf1f1d 1414
b65e5048 1415 AliTRDdigitsManager *mergeSDigitsManager = 0x0;
abaf1f1d 1416 // Get the first s-digits
1417 fSDigitsManager = (AliTRDdigitsManager *) fSDigitsManagerList->First();
9afbd7de 1418 if (!fSDigitsManager) {
7925de54 1419 AliError("No SDigits manager");
9afbd7de 1420 return kFALSE;
1421 }
abaf1f1d 1422
1423 // Loop through the other sets of s-digits
b65e5048 1424 mergeSDigitsManager = (AliTRDdigitsManager *) fSDigitsManagerList->After(fSDigitsManager);
abaf1f1d 1425
9afbd7de 1426 if (mergeSDigitsManager) {
7925de54 1427 AliDebug(1,Form("Merge %d input files.",fSDigitsManagerList->GetSize()));
9afbd7de 1428 }
1429 else {
7925de54 1430 AliDebug(1,"Only one input file.");
abaf1f1d 1431 }
3551db50 1432
b65e5048 1433 Int_t nTimeTotal = calibration->GetNumberOfTimeBins();
abaf1f1d 1434 Int_t iMerge = 0;
b65e5048 1435
abaf1f1d 1436 while (mergeSDigitsManager) {
1437
1438 iMerge++;
b65e5048 1439
abaf1f1d 1440 // Loop through the detectors
1441 for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
1442
625f5260 1443 Int_t nRowMax = fGeo->GetPadPlane(iDet)->GetNrows();
1444 Int_t nColMax = fGeo->GetPadPlane(iDet)->GetNcols();
b65e5048 1445
abaf1f1d 1446 // Loop through the pixels of one detector and add the signals
b65e5048 1447 digitsA = (AliTRDarraySignal *) fSDigitsManager->GetSDigits(iDet);
1448 digitsB = (AliTRDarraySignal *) mergeSDigitsManager->GetSDigits(iDet);
1449 digitsA->Expand();
1450 if (!digitsA->HasData()) continue;
1451 digitsB->Expand();
1452 if (!digitsB->HasData()) continue;
1453
abaf1f1d 1454 for (iDict = 0; iDict < kNDict; iDict++) {
b65e5048 1455 dictionaryA[iDict] = (AliTRDarrayDictionary *) fSDigitsManager->GetDictionary(iDet,iDict);
1456 dictionaryB[iDict] = (AliTRDarrayDictionary *) mergeSDigitsManager->GetDictionary(iDet,iDict);
1457 dictionaryA[iDict]->Expand();
abaf1f1d 1458 dictionaryB[iDict]->Expand();
1459 }
1460
4487dad0 1461 // Merge only detectors that contain a signal
1462 Bool_t doMerge = kTRUE;
1463 if (fMergeSignalOnly) {
b65e5048 1464 if (digitsA->GetOverThreshold(0) == 0) {
1465 doMerge = kFALSE;
4487dad0 1466 }
abaf1f1d 1467 }
b65e5048 1468
4487dad0 1469 if (doMerge) {
b65e5048 1470
1471 AliDebug(1,Form("Merge detector %d of input no.%d",iDet,iMerge+1));
1472
1473 for (Int_t iRow = 0; iRow < nRowMax; iRow++ ) {
1474 for (Int_t iCol = 0; iCol < nColMax; iCol++ ) {
1475 for (Int_t iTime = 0; iTime < nTimeTotal; iTime++) {
1476
4487dad0 1477 // Add the amplitudes of the summable digits
b65e5048 1478 Float_t ampA = digitsA->GetData(iRow,iCol,iTime);
1479 Float_t ampB = digitsB->GetData(iRow,iCol,iTime);
1480 ampA += ampB;
1481 digitsA->SetData(iRow,iCol,iTime,ampA);
1482
1483 // Add the mask to the track id if defined.
1484 for (iDict = 0; iDict < kNDict; iDict++) {
1485 Int_t trackB = dictionaryB[iDict]->GetData(iRow,iCol,iTime);
1486 if ((fMasks) && (trackB > 0)) {
1487 for (jDict = 0; jDict < kNDict; jDict++) {
1488 Int_t trackA = dictionaryA[iDict]->GetData(iRow,iCol,iTime);
1489 if (trackA == 0) {
1490 trackA = trackB + fMasks[iMerge];
1491 dictionaryA[iDict]->SetData(iRow,iCol,iTime,trackA);
625f5260 1492 } // if: track A == 0
b65e5048 1493 } // for: jDict
1494 } // if: fMasks and trackB > 0
625f5260 1495 } // for: iDict
abaf1f1d 1496
625f5260 1497 } // for: iTime
1498 } // for: iCol
1499 } // for: iRow
4487dad0 1500
625f5260 1501 } // if: doMerge
1502
1503 mergeSDigitsManager->RemoveDigits(iDet);
1504 mergeSDigitsManager->RemoveDictionaries(iDet);
b65e5048 1505
abaf1f1d 1506 if (fCompress) {
b65e5048 1507 digitsA->Compress(0);
1508 for (iDict = 0; iDict < kNDict; iDict++) {
1509 dictionaryA[iDict]->Compress();
abaf1f1d 1510 }
1511 }
b65e5048 1512
625f5260 1513 } // for: detectors
b65e5048 1514
abaf1f1d 1515 // The next set of s-digits
b65e5048 1516 mergeSDigitsManager = (AliTRDdigitsManager *) fSDigitsManagerList->After(mergeSDigitsManager);
1517
625f5260 1518 } // while: mergeDigitsManagers
b65e5048 1519
625f5260 1520 return kTRUE;
1521
1522}
1523
1524//_____________________________________________________________________________
1525Bool_t AliTRDdigitizer::ConvertSDigits()
1526{
1527 //
1528 // Converts s-digits to normal digits
1529 //
1530
b65e5048 1531 AliTRDarraySignal *digitsIn = 0x0;
625f5260 1532
1533 if (!fSDigitsManager->HasSDigits()) {
1534 AliError("No s-digits in digits manager");
1535 return kFALSE;
abaf1f1d 1536 }
1537
625f5260 1538 // Loop through the detectors
1539 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
1540
1541 // Get the merged s-digits (signals)
b65e5048 1542 digitsIn = (AliTRDarraySignal *) fSDigitsManager->GetSDigits(det);
625f5260 1543 if (!digitsIn->HasData()) {
1544 AliDebug(2,Form("No digits for det=%d",det));
1545 continue;
1546 }
b0a41e80 1547
625f5260 1548 // Convert the merged sdigits to digits
1549 if (!Signal2ADC(det,digitsIn)) {
1550 continue;
1551 }
1552
1553 // Copy the dictionary information to the output array
1554 if (!CopyDictionary(det)) {
1555 continue;
1556 }
1557
1558 // Delete
1559 fSDigitsManager->RemoveDigits(det);
1560 fSDigitsManager->RemoveDictionaries(det);
1561
1562 // Compress the arrays
1563 CompressOutputArrays(det);
1564
1565 } // for: detector numbers
1566
b0a41e80 1567
16bf9884 1568 return kTRUE;
1569
1570}
1571
1572//_____________________________________________________________________________
625f5260 1573Bool_t AliTRDdigitizer::CopyDictionary(Int_t det)
abaf1f1d 1574{
1575 //
625f5260 1576 // Copies the dictionary information from the s-digits arrays
1577 // to the output arrays
abaf1f1d 1578 //
1579
625f5260 1580 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
1581 if (!calibration) {
1582 AliFatal("Could not get calibration object");
4d18a639 1583 return kFALSE;
1584 }
abaf1f1d 1585
625f5260 1586 AliDebug(1,Form("Start copying dictionaries for detector=%d",det));
1587
1588 const Int_t kNDict = AliTRDdigitsManager::kNDict;
b65e5048 1589 AliTRDarrayDictionary *dictionaryIn[kNDict];
1590 AliTRDarrayDictionary *dictionaryOut[kNDict];
625f5260 1591
1592 Int_t nRowMax = fGeo->GetPadPlane(det)->GetNrows();
1593 Int_t nColMax = fGeo->GetPadPlane(det)->GetNcols();
1594 Int_t nTimeTotal = calibration->GetNumberOfTimeBins();
1595
1596 Int_t row = 0;
1597 Int_t col = 0;
1598 Int_t time = 0;
1599 Int_t dict = 0;
1600
1601 for (dict = 0; dict < kNDict; dict++) {
1602
b65e5048 1603 dictionaryIn[dict] = (AliTRDarrayDictionary *) fSDigitsManager->GetDictionary(det,dict);
625f5260 1604 dictionaryIn[dict]->Expand();
b65e5048 1605 dictionaryOut[dict] = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(det,dict);
625f5260 1606 dictionaryOut[dict]->Allocate(nRowMax,nColMax,nTimeTotal);
1607
1608 for (row = 0; row < nRowMax; row++) {
1609 for (col = 0; col < nColMax; col++) {
1610 for (time = 0; time < nTimeTotal; time++) {
b65e5048 1611 Int_t track = dictionaryIn[dict]->GetData(row,col,time);
1612 dictionaryOut[dict]->SetData(row,col,time,track);
625f5260 1613 } // for: time
1614 } // for: col
1615 } // for: row
b65e5048 1616
625f5260 1617 } // for: dictionaries
b65e5048 1618
625f5260 1619 return kTRUE;
1620
1621}
1622
1623//_____________________________________________________________________________
1624void AliTRDdigitizer::CompressOutputArrays(Int_t det)
1625{
1626 //
1627 // Compress the output arrays
1628 //
1629
1630 const Int_t kNDict = AliTRDdigitsManager::kNDict;
b65e5048 1631 AliTRDarrayDictionary *dictionary = 0x0;
625f5260 1632
1633 if (fCompress) {
1634
b65e5048 1635 if (!fSDigits) {
1636 AliTRDarrayADC *digits = 0x0;
1637 digits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det);
1638 digits->Compress();
1639 }
1640
1641 if (fSDigits) {
1642 AliTRDarraySignal *digits = 0x0;
1643 digits = (AliTRDarraySignal *) fDigitsManager->GetSDigits(det);
1644 digits->Compress(0);
1645 }
1646
625f5260 1647 for (Int_t dict = 0; dict < kNDict; dict++) {
b65e5048 1648 dictionary = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(det,dict);
1649 dictionary->Compress();
625f5260 1650 }
1651
1652 }
abaf1f1d 1653
1654}
1655
1656//_____________________________________________________________________________
0a29d0f1 1657Bool_t AliTRDdigitizer::WriteDigits() const
f7336fa3 1658{
1659 //
1660 // Writes out the TRD-digits and the dictionaries
1661 //
1662
4d18a639 1663 // Write parameters
bdbb05bb 1664 fRunLoader->CdGAFile();
bdbb05bb 1665
da581aea 1666 // Store the digits and the dictionary in the tree
abaf1f1d 1667 return fDigitsManager->WriteDigits();
f7336fa3 1668
1669}
793ff80c 1670
1671//_____________________________________________________________________________
88cb7938 1672void AliTRDdigitizer::InitOutput(Int_t iEvent)
8e64dd77 1673{
1674 //
1675 // Initializes the output branches
1676 //
1677
1678 fEvent = iEvent;
88cb7938 1679
9afbd7de 1680 if (!fRunLoader) {
1681 AliError("Run Loader is NULL");
1682 return;
1683 }
1684
4329977a 1685 AliLoader *loader = fRunLoader->GetLoader("TRDLoader");
9afbd7de 1686 if (!loader) {
1687 AliError("Can not get TRD loader from Run Loader");
1688 return;
1689 }
88cb7938 1690
4d18a639 1691 TTree *tree = 0;
88cb7938 1692
9afbd7de 1693 if (fSDigits) {
1694 // If we produce SDigits
88cb7938 1695 tree = loader->TreeS();
9afbd7de 1696 if (!tree) {
88cb7938 1697 loader->MakeTree("S");
1698 tree = loader->TreeS();
9afbd7de 1699 }
1700 }
1701 else {
1702 // If we produce Digits
1703 tree = loader->TreeD();
1704 if (!tree) {
1705 loader->MakeTree("D");
1706 tree = loader->TreeD();
1707 }
1708 }
88cb7938 1709 fDigitsManager->SetEvent(iEvent);
1710 fDigitsManager->MakeBranch(tree);
8e64dd77 1711
1712}
3551db50 1713
3551db50 1714//_____________________________________________________________________________
a076fc2f 1715Int_t AliTRDdigitizer::Diffusion(Float_t vdrift, Double_t absdriftlength
1716 , Double_t &lRow, Double_t &lCol, Double_t &lTime)
3551db50 1717{
1718 //
a076fc2f 1719 // Applies the diffusion smearing to the position of a single electron.
1720 // Depends on absolute drift length.
3551db50 1721 //
1722
a076fc2f 1723 Float_t diffL = 0.0;
1724 Float_t diffT = 0.0;
4d18a639 1725
a076fc2f 1726 if (AliTRDCommonParam::Instance()->GetDiffCoeff(diffL,diffT,vdrift)) {
3551db50 1727
a076fc2f 1728 Float_t driftSqrt = TMath::Sqrt(absdriftlength);
1729 Float_t sigmaT = driftSqrt * diffT;
1730 Float_t sigmaL = driftSqrt * diffL;
1731 lRow = gRandom->Gaus(lRow ,sigmaT);
1732 lCol = gRandom->Gaus(lCol ,sigmaT * GetLorentzFactor(vdrift));
1733 lTime = gRandom->Gaus(lTime,sigmaL * GetLorentzFactor(vdrift));
4d18a639 1734
a076fc2f 1735 return 1;
7754cd1f 1736
3551db50 1737 }
a076fc2f 1738 else {
3551db50 1739
a076fc2f 1740 return 0;
1b95a37b 1741
3551db50 1742 }
9afbd7de 1743
3551db50 1744}
3551db50 1745
1746//_____________________________________________________________________________
1747Float_t AliTRDdigitizer::GetLorentzFactor(Float_t vd)
1748{
9afbd7de 1749 //
a076fc2f 1750 // Returns the Lorentz factor
9afbd7de 1751 //
1752
a076fc2f 1753 Double_t omegaTau = AliTRDCommonParam::Instance()->GetOmegaTau(vd);
1754 Double_t lorentzFactor = 1.0;
1755 if (AliTRDCommonParam::Instance()->ExBOn()) {
1756 lorentzFactor = 1.0 / (1.0 + omegaTau*omegaTau);
1757 }
4d18a639 1758
a076fc2f 1759 return lorentzFactor;
9afbd7de 1760
3551db50 1761}
1762
1763//_____________________________________________________________________________
4329977a 1764Int_t AliTRDdigitizer::ExB(Float_t vdrift, Double_t driftlength, Double_t &lCol)
3551db50 1765{
1766 //
4329977a 1767 // Applies E x B effects to the position of a single electron.
1768 // Depends on signed drift length.
3551db50 1769 //
4329977a 1770
a076fc2f 1771 lCol = lCol
1772 + AliTRDCommonParam::Instance()->GetOmegaTau(vdrift)
1773 * driftlength;
3551db50 1774
1775 return 1;
9afbd7de 1776
3551db50 1777}
b65e5048 1778
1779//_____________________________________________________________________________
b0a41e80 1780void AliTRDdigitizer::RunDigitalProcessing(AliTRDarrayADC *digits, Int_t det)
b65e5048 1781{
1782 //
b0a41e80 1783 // Run the digital processing in the TRAP
b65e5048 1784 //
1785
b0a41e80 1786 AliTRDfeeParam *feeParam = AliTRDfeeParam::Instance();
b65e5048 1787
1788 //Create and initialize the mcm object
b0a41e80 1789 AliTRDmcmSim* mcmfast = new AliTRDmcmSim();
b65e5048 1790
1791 //Call the methods in the mcm class using the temporary array as input
b0a41e80 1792 for(Int_t rob = 0; rob < digits->GetNrow() / 4; rob++)
1793 {
1794 for(Int_t mcm = 0; mcm < 16; mcm++)
b65e5048 1795 {
b0a41e80 1796 mcmfast->Init(det, rob, mcm);
1797 mcmfast->SetData(digits);
b65e5048 1798 mcmfast->Filter();
b0a41e80 1799 if (feeParam->GetTracklet())
1800 mcmfast->Tracklet();
b65e5048 1801 mcmfast->ZSMapping();
b0a41e80 1802 mcmfast->WriteData(digits);
b65e5048 1803 }
b0a41e80 1804 }
b65e5048 1805
b65e5048 1806 delete mcmfast;
b65e5048 1807}