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