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