]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDdigitsManager.cxx
All known overlaps removed (M. Sitta)
[u/mrichter/AliRoot.git] / TRD / AliTRDdigitsManager.cxx
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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Manages the digits and the track dictionary in the form of               //
21 //  AliTRDdataArray objects.                                                 //
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <Riostream.h>
26  
27 #include <TROOT.h>
28 #include <TTree.h>                                                              
29 #include <TFile.h>
30
31 #include "AliRun.h"
32 #include "AliLog.h"
33
34 #include "AliTRDdigitsManager.h"
35 #include "AliTRDsegmentArray.h"
36 #include "AliTRDdataArray.h"
37 #include "AliTRDdataArrayI.h"
38 #include "AliTRDdataArrayS.h"
39 #include "AliTRDdataArrayDigits.h"
40 #include "AliTRDdigit.h"
41 #include "AliTRDgeometry.h"
42
43 #include "AliTRDSignalIndex.h"
44
45 ClassImp(AliTRDdigitsManager)
46
47 //_____________________________________________________________________________
48
49   // Number of track dictionary arrays
50   const Int_t AliTRDdigitsManager::fgkNDict = kNDict;
51
52 //_____________________________________________________________________________
53 AliTRDdigitsManager::AliTRDdigitsManager()
54   :TObject()
55   ,fEvent(0)
56   ,fTree(0)
57   ,fDigits(0)
58   ,fHasSDigits(0)
59   ,fSignalIndexes(NULL)
60   ,fUseDictionaries(kTRUE)
61 {
62   //
63   // Default constructor
64   //
65
66   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
67     fDictionary[iDict] = NULL;
68   }
69   
70   fSignalIndexes = new TObjArray(AliTRDgeometry::Ndet());
71   
72 }
73
74 //_____________________________________________________________________________
75 AliTRDdigitsManager::AliTRDdigitsManager(const AliTRDdigitsManager &m)
76   :TObject(m)
77   ,fEvent(m.fEvent)
78   ,fTree(0)
79   ,fDigits(0)
80   ,fHasSDigits(m.fHasSDigits)
81   ,fSignalIndexes(NULL)
82   ,fUseDictionaries(kTRUE)
83 {
84   //
85   // AliTRDdigitsManager copy constructor
86   //
87
88 }
89
90 //_____________________________________________________________________________
91 AliTRDdigitsManager::~AliTRDdigitsManager()
92 {
93   //
94   // AliTRDdigitsManager destructor
95   //
96
97   if (fDigits) {
98     fDigits->Delete();
99     delete fDigits;
100     fDigits            = NULL;
101   }
102
103   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
104     fDictionary[iDict]->Delete();
105     delete fDictionary[iDict];
106     fDictionary[iDict] = NULL;
107   }
108
109   if (fSignalIndexes) {
110     fSignalIndexes->Delete();
111     delete fSignalIndexes;
112     fSignalIndexes     = NULL;
113   }
114
115 }
116
117 //_____________________________________________________________________________
118 AliTRDdigitsManager &AliTRDdigitsManager::operator=(const AliTRDdigitsManager &m)
119 {
120   //
121   // Assignment operator
122   //
123
124   if (this != &m) {
125     ((AliTRDdigitsManager &) m).Copy(*this);
126   }
127
128   return *this;
129
130 }
131
132 //_____________________________________________________________________________
133 void AliTRDdigitsManager::Copy(TObject &m) const
134 {
135   //
136   // Copy function
137   //
138
139   ((AliTRDdigitsManager &) m).fEvent           = fEvent;
140   ((AliTRDdigitsManager &) m).fHasSDigits      = fHasSDigits;
141   
142   ((AliTRDdigitsManager &) m).fSignalIndexes   = fSignalIndexes;
143   ((AliTRDdigitsManager &) m).fUseDictionaries = fUseDictionaries;
144
145   TObject::Copy(m);
146
147 }
148
149 //_____________________________________________________________________________
150 void AliTRDdigitsManager::CreateArrays()
151 {
152   //
153   // Create the data arrays
154   //
155
156   if (fHasSDigits) {
157     fDigits = new AliTRDsegmentArray("AliTRDdataArrayF",AliTRDgeometry::Ndet());
158   }
159   else {
160     fDigits = new AliTRDsegmentArray("AliTRDdataArrayDigits",AliTRDgeometry::Ndet());
161   }
162
163   if (fUseDictionaries) {
164     for (Int_t iDict = 0; iDict < kNDict; iDict++) {
165       fDictionary[iDict] = new AliTRDsegmentArray("AliTRDdataArrayI"
166                                                  ,AliTRDgeometry::Ndet());
167     }
168   }
169
170   for (Int_t i = 0; i < AliTRDgeometry::Ndet(); i++) {
171     fSignalIndexes->AddLast(new AliTRDSignalIndex());
172   }
173
174 }
175
176 //_____________________________________________________________________________
177 void AliTRDdigitsManager::ResetArrays()
178 {
179   //
180   // Reset the data arrays
181   //
182
183   if (fDigits) {
184     fDigits->Delete();
185     delete fDigits;
186   }
187   if (fHasSDigits) {
188     fDigits = new AliTRDsegmentArray("AliTRDdataArrayF",AliTRDgeometry::Ndet());
189   }
190   else {
191     fDigits = new AliTRDsegmentArray("AliTRDdataArrayS",AliTRDgeometry::Ndet());
192   }
193
194   if (fUseDictionaries) {
195     for (Int_t iDict = 0; iDict < kNDict; iDict++) {
196       if (fDictionary[iDict]) { 
197         fDictionary[iDict]->Delete();
198         delete fDictionary[iDict];
199       }
200       fDictionary[iDict] = new AliTRDsegmentArray("AliTRDdataArrayI"
201                                                  ,AliTRDgeometry::Ndet());
202     }
203   }
204
205   for (Int_t i = 0; i < AliTRDgeometry::Ndet(); i++) {
206     AliTRDSignalIndex *idx = (AliTRDSignalIndex *)fSignalIndexes->At(i);
207     if (idx) idx->Reset();
208   }
209
210 }
211
212 //_____________________________________________________________________________
213 Short_t AliTRDdigitsManager::GetDigitAmp(Int_t row, Int_t col,Int_t time
214                                        , Int_t det) const
215 {
216   //
217   // Returns the amplitude of a digit
218   //
219
220   if (!GetDigits(det)) {
221     return 0;
222   }
223
224   return ((Short_t) ((AliTRDdataArrayDigits *) GetDigits(det))->GetData(row,col,time));
225
226 }
227  
228 //_____________________________________________________________________________
229 UChar_t AliTRDdigitsManager::GetPadStatus(Int_t row, Int_t col, Int_t time
230                                              , Int_t det) const
231 {
232   //
233   // Returns the pad status for the requested pad
234   //
235         
236   if (!GetDigits(det)) {
237     return 0;
238   }
239
240   return ((UChar_t) ((AliTRDdataArrayDigits *) GetDigits(det))->GetPadStatus(row,col,time));
241
242 }
243
244 //_____________________________________________________________________________
245 Bool_t AliTRDdigitsManager::MakeBranch(TTree *tree)
246 {
247   //
248   // Creates the tree and branches for the digits and the dictionary
249   //
250
251   Int_t buffersize = 64000;
252
253   Bool_t status = kTRUE;
254
255   if (tree) {
256     fTree = tree;
257   }
258
259   // Make the branch for the digits
260   if (fDigits) {
261     const AliTRDdataArray *kDigits = (AliTRDdataArray *) fDigits->At(0);
262     if (kDigits) {
263       if (!fTree) return kFALSE;
264       TBranch* branch = fTree->GetBranch("TRDdigits");
265       if (!branch) fTree->Branch("TRDdigits",kDigits->IsA()->GetName()
266                                 ,&kDigits,buffersize,99);
267       AliDebug(1,"Making branch TRDdigits\n");
268     }
269     else {
270       status = kFALSE;
271     }
272   }
273   else {
274     status = kFALSE;
275   }
276
277   if (fUseDictionaries) {
278     // Make the branches for the dictionaries
279     for (Int_t iDict = 0; iDict < kNDict; iDict++) {
280       Char_t branchname[15];
281       sprintf(branchname,"TRDdictionary%d",iDict);
282       if (fDictionary[iDict]) {
283         const AliTRDdataArray *kDictionary = 
284                                (AliTRDdataArray *) fDictionary[iDict]->At(0);
285         if (kDictionary) {
286           if (!fTree) return kFALSE;
287           TBranch* branch = fTree->GetBranch(branchname);
288           if (!branch) fTree->Branch(branchname,kDictionary->IsA()->GetName()
289                                     ,&kDictionary,buffersize,99);
290           AliDebug(1,Form("Making branch %s\n",branchname));
291         }
292         else {
293           status = kFALSE;
294         }
295       }
296       else {
297         status = kFALSE;
298       }
299     }
300   }
301
302   return status;
303
304 }
305
306 //_____________________________________________________________________________
307 Bool_t AliTRDdigitsManager::ReadDigits(TTree *tree)
308 {
309   //
310   // Reads the digit information from the input file
311   //
312
313   Bool_t status = kTRUE;
314
315   if (tree) {
316     fTree = tree;
317   }
318
319   if (!fDigits) {
320     AliDebug(1,"Create the data arrays.\n");
321     CreateArrays();
322   }
323
324   status = fDigits->LoadArray("TRDdigits",fTree);
325
326   if (fUseDictionaries) {
327     for (Int_t iDict = 0; iDict < kNDict; iDict++) {
328       Char_t branchname[15];
329       sprintf(branchname,"TRDdictionary%d",iDict);
330       status = fDictionary[iDict]->LoadArray(branchname,fTree);
331       if (status == kFALSE) {
332         fUseDictionaries = kFALSE;
333         AliWarning("Unable to load dict arrays. Will not use them.\n");
334         break;
335       }
336     }  
337   }
338
339   return kTRUE;
340
341 }
342
343 //_____________________________________________________________________________
344 Bool_t AliTRDdigitsManager::WriteDigits()
345 {
346   //
347   // Writes out the TRD-digits and the dictionaries
348   //
349
350   // Store the contents of the segment array in the tree
351   if (!fDigits->StoreArray("TRDdigits",fTree)) {
352     AliError("Error while storing digits in branch TRDdigits\n");
353     return kFALSE;
354   }
355
356   if (fUseDictionaries) {
357     for (Int_t iDict = 0; iDict < kNDict; iDict++) {
358       Char_t branchname[15];
359       sprintf(branchname,"TRDdictionary%d",iDict);
360       if (!fDictionary[iDict]->StoreArray(branchname,fTree)) {
361         AliError(Form("Error while storing dictionary in branch %s\n",branchname));
362         return kFALSE;
363       }
364     }
365   }
366
367   // Write the new tree to the output file
368   fTree->AutoSave();  // Modification by Jiri
369
370   return kTRUE;
371
372 }
373
374 //_____________________________________________________________________________
375 AliTRDdigit *AliTRDdigitsManager::GetDigit(Int_t row, Int_t col
376                                          , Int_t time, Int_t det) const
377 {
378   // 
379   // Creates a single digit object 
380   //
381
382   Int_t digits[4];
383   Int_t amp[1];
384
385   digits[0] = det;
386   digits[1] = row;
387   digits[2] = col;
388   digits[3] = time;
389
390   amp[0]    = ((AliTRDdataArrayDigits *) GetDigits(det))->GetData(row,col,time);
391   
392   return (new AliTRDdigit(digits,amp));
393
394 }
395
396 //_____________________________________________________________________________
397 Int_t AliTRDdigitsManager::GetTrack(Int_t track
398                                   , Int_t row, Int_t col, Int_t time
399                                   , Int_t det) const
400 {
401   // 
402   // Returns the MC-track numbers from the dictionary.
403   //
404
405   if ((track < 0) || (track >= kNDict)) {
406     AliError(Form("track %d out of bounds (size: %d, this: 0x%08x)"
407                  ,track,kNDict,this));
408     return -1;
409   }
410
411   if (fUseDictionaries == kFALSE) {
412     return -1;
413   }
414
415   // Array contains index+1 to allow data compression
416   return (((AliTRDdataArrayI *) GetDictionary(det,track))->GetData(row,col,time) - 1);
417
418 }
419
420 //_____________________________________________________________________________
421 AliTRDdataArrayDigits *AliTRDdigitsManager::GetDigits(Int_t det) const
422 {
423   //
424   // Returns the digits array for one detector
425   //
426
427   if (!fDigits) {
428     return 0x0;
429   }
430
431   return (AliTRDdataArrayDigits *) fDigits->At(det);
432
433 }
434
435 //_____________________________________________________________________________
436 AliTRDdataArray *AliTRDdigitsManager::GetDictionary(Int_t det, Int_t i) const
437 {
438   //
439   // Returns the dictionary for one detector
440   //
441
442   if (fUseDictionaries == kFALSE) {
443     return 0x0;
444   }
445
446   return (AliTRDdataArray *) fDictionary[i]->At(det);
447
448 }
449
450 //_____________________________________________________________________________
451 Int_t AliTRDdigitsManager::GetTrack(Int_t track, AliTRDdigit *Digit) const
452 {
453   // 
454   // Returns the MC-track numbers from the dictionary for a given digit
455   //
456
457   Int_t row  = Digit->GetRow();
458   Int_t col  = Digit->GetCol();
459   Int_t time = Digit->GetTime();
460   Int_t det  = Digit->GetDetector();
461
462   return GetTrack(track,row,col,time,det);
463
464 }
465
466 //_____________________________________________________________________________
467 AliTRDSignalIndex *AliTRDdigitsManager::GetIndexes(Int_t det) 
468 {
469   // 
470   // Returns indexes of active pads
471   //
472
473   return (AliTRDSignalIndex *) fSignalIndexes->At(det);
474
475 }
476
477 //_____________________________________________________________________________
478 void AliTRDdigitsManager::RemoveDigits(Int_t det) 
479 {
480   // 
481   // Clear memory
482   //
483
484   fDigits->ClearSegment(det);
485
486 }
487
488 //_____________________________________________________________________________
489 void AliTRDdigitsManager::RemoveDictionaries(Int_t det) 
490 {
491   // 
492   // Clear memory
493   //
494
495   if (fUseDictionaries == kFALSE) {
496     return;
497   }
498
499   for (Int_t i = 0; i < kNDict; i++) {
500     fDictionary[i]->ClearSegment(det);
501   }
502
503 }
504
505 //_____________________________________________________________________________
506 void AliTRDdigitsManager::ClearIndexes(Int_t det) 
507 {
508   // 
509   // Clear memory
510   //
511
512   ((AliTRDSignalIndex *) fSignalIndexes->At(det))->ClearAll();  
513
514 }
515
516 //_____________________________________________________________________________
517 Bool_t AliTRDdigitsManager::BuildIndexes(Int_t det)
518 {
519   //
520   // Build the list of indices
521   //
522
523   Int_t nRows = 0;
524   Int_t nCols = 0;
525   Int_t nTbins = 0;
526
527   AliTRDgeometry    geom;
528   AliTRDdataArrayDigits *digits = 0x0;
529
530   if (fHasSDigits) {
531     return kFALSE;
532   }
533   else {
534     digits = (AliTRDdataArrayDigits *) GetDigits(det);
535   }
536
537   //digits should be expanded by now!!!
538   if (digits->GetNtime() > 0) {
539
540     digits->Expand();
541     nRows  = digits->GetNrow();
542     nCols  = digits->GetNcol();
543     nTbins = digits->GetNtime();
544
545     AliTRDSignalIndex *indexes = GetIndexes(det);
546     indexes->SetSM(geom.GetSector(det));
547     indexes->SetStack(geom.GetStack(det));
548     indexes->SetLayer(geom.GetLayer(det));
549     indexes->SetDetNumber(det);
550
551     if (indexes->IsAllocated() == kFALSE) {
552       indexes->Allocate(nRows,nCols,nTbins);
553     }
554
555     for (Int_t ir = 0; ir < nRows; ir++) {
556       for (Int_t ic = 0; ic < nCols; ic++) {
557         for (Int_t it = 0; it < nTbins; it++) {   
558   
559           Int_t isig = digits->GetDataUnchecked(ir,ic,it);
560           if (isig > 0) {
561             indexes->AddIndexTBin(ir,ic,it);        
562           }
563         } // tbins
564       } // cols
565     } // rows
566
567   } // if GetNtime
568   else {
569     return kFALSE;
570   }
571
572   return kTRUE;
573
574 }