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