]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDdigitsManager.cxx
4614cc93ecf9eeb6973b70431faaa611f6ab034b
[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 /*
17 $Log$
18 Revision 1.14  2001/11/14 10:50:46  cblume
19 Changes in digits IO. Add merging of summable digits
20
21 Revision 1.13  2001/11/06 17:19:41  cblume
22 Add detailed geometry and simple simulator
23
24 Revision 1.12  2001/05/16 14:57:28  alibrary
25 New files for folders and Stack
26
27 Revision 1.11  2001/03/13 09:30:35  cblume
28 Update of digitization. Moved digit branch definition to AliTRD
29
30 Revision 1.10  2001/01/26 19:56:57  hristov
31 Major upgrade of AliRoot code
32
33 Revision 1.9  2000/11/02 09:25:53  cblume
34 Change also the dictionary to AliTRDdataArray
35
36 Revision 1.8  2000/11/01 15:20:13  cblume
37 Change AliTRDdataArrayI to AliTRDdataArray in MakeBranch()
38
39 Revision 1.7  2000/11/01 14:53:20  cblume
40 Merge with TRD-develop
41
42 Revision 1.1.2.5  2000/10/17 02:27:34  cblume
43 Get rid of global constants
44
45 Revision 1.1.2.4  2000/10/15 23:40:01  cblume
46 Remove AliTRDconst
47
48 Revision 1.1.2.3  2000/10/06 16:49:46  cblume
49 Made Getters const
50
51 Revision 1.1.2.2  2000/10/04 16:34:58  cblume
52 Replace include files by forward declarations
53
54 Revision 1.5  2000/06/09 11:10:07  cblume
55 Compiler warnings and coding conventions, next round
56
57 Revision 1.4  2000/06/08 18:32:58  cblume
58 Make code compliant to coding conventions
59
60 Revision 1.3  2000/06/07 16:27:01  cblume
61 Try to remove compiler warnings on Sun and HP
62
63 Revision 1.2  2000/05/08 16:17:27  cblume
64 Merge TRD-develop
65
66 Revision 1.1.2.1  2000/05/08 14:44:01  cblume
67 Add new class AliTRDdigitsManager
68
69 */
70
71 ///////////////////////////////////////////////////////////////////////////////
72 //                                                                           //
73 //  Manages the digits and the track dictionary in the form of               //
74 //  AliTRDdataArray objects.                                                 //
75 //                                                                           //
76 ///////////////////////////////////////////////////////////////////////////////
77
78 #include <iostream.h>
79  
80 #include <TROOT.h>
81 #include <TTree.h>                                                              
82 #include <TFile.h>
83
84 #include "AliRun.h"
85
86 #include "AliTRDdigitsManager.h"
87 #include "AliTRDsegmentArray.h"
88 #include "AliTRDdataArrayI.h"
89 #include "AliTRDdigit.h"
90 #include "AliTRDgeometry.h"
91 #include "AliTRD.h"
92
93 ClassImp(AliTRDdigitsManager)
94
95 //_____________________________________________________________________________
96
97   // Number of track dictionary arrays
98   const Int_t AliTRDdigitsManager::fgkNDict = kNDict;
99
100 //_____________________________________________________________________________
101 AliTRDdigitsManager::AliTRDdigitsManager():TObject()
102 {
103   //
104   // Default constructor
105   //
106
107   fIsRaw   = kFALSE;
108   fEvent   = 0;
109   fDebug   = 0;
110   fSDigits = 0;
111
112   fFile    = NULL;
113   fTree    = NULL;
114   fDigits  = NULL;
115   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
116     fDictionary[iDict] = NULL;
117   }
118
119 }
120
121 //_____________________________________________________________________________
122 AliTRDdigitsManager::AliTRDdigitsManager(const AliTRDdigitsManager &m)
123 {
124   //
125   // AliTRDdigitsManager copy constructor
126   //
127
128   ((AliTRDdigitsManager &) m).Copy(*this);
129
130 }
131
132 //_____________________________________________________________________________
133 AliTRDdigitsManager::~AliTRDdigitsManager()
134 {
135   //
136   // AliTRDdigitsManager destructor
137   //
138
139   if (fFile) {
140     fFile->Close();
141     delete fFile;
142     fFile = NULL;
143   }
144
145   if (fTree) {
146     delete fTree;
147     fTree = NULL;
148   }
149
150   if (fDigits) {
151     fDigits->Delete();
152     delete fDigits;
153     fDigits = NULL;
154   }
155
156   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
157     fDictionary[iDict]->Delete();
158     delete fDictionary[iDict];
159     fDictionary[iDict] = NULL;
160   }
161
162 }
163
164 //_____________________________________________________________________________
165 void AliTRDdigitsManager::Copy(TObject &m)
166 {
167   //
168   // Copy function
169   //
170
171   ((AliTRDdigitsManager &) m).fIsRaw   = fIsRaw;
172   ((AliTRDdigitsManager &) m).fEvent   = fEvent;
173   ((AliTRDdigitsManager &) m).fDebug   = fDebug;
174   ((AliTRDdigitsManager &) m).fSDigits = fSDigits;
175
176   TObject::Copy(m);
177
178 }
179
180 //_____________________________________________________________________________
181 void AliTRDdigitsManager::CreateArrays()
182 {
183   //
184   // Create the data arrays
185   //
186
187   fDigits = new AliTRDsegmentArray("AliTRDdataArrayI",AliTRDgeometry::Ndet());
188
189   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
190     fDictionary[iDict] = new AliTRDsegmentArray("AliTRDdataArrayI"
191                                                ,AliTRDgeometry::Ndet());
192   }
193
194 }
195
196 //_____________________________________________________________________________
197 void AliTRDdigitsManager::SetRaw()
198 {
199
200   fIsRaw = kTRUE;
201
202   fDigits->SetBit(AliTRDdigit::RawDigit());
203   
204 }
205
206 //_____________________________________________________________________________
207 Short_t AliTRDdigitsManager::GetDigitAmp(Int_t row, Int_t col,Int_t time
208                                        , Int_t det) const
209 {
210   //
211   // Returns the amplitude of a digit
212   //
213
214   return ((Short_t) GetDigits(det)->GetData(row,col,time));
215
216 }
217  
218 //_____________________________________________________________________________
219 Bool_t AliTRDdigitsManager::Open(const Char_t *file)
220 {
221   //
222   // Opens the file for the TRD digits
223   //
224
225   fFile = (TFile*) gROOT->GetListOfFiles()->FindObject(file);
226   if (!fFile) {
227     if (fDebug > 0) {
228       printf("<AliTRDdigitsManager::Open> ");
229       printf("Open the AliROOT-file %s.\n",file);
230     }
231     fFile = new TFile(file,"UPDATE");
232     if (!fFile) return kFALSE;
233   }
234   else {
235     if (fDebug > 0) {
236       printf("<AliTRDdigitsManager::Open> ");
237       printf("%s is already open.\n",file);
238     }
239   }
240
241   return kTRUE;
242
243 }
244
245 //_____________________________________________________________________________
246 Bool_t AliTRDdigitsManager::MakeBranch(const Char_t *file)
247 {
248   //
249   // Creates the tree and branches for the digits and the dictionary
250   //
251
252   // Create the TRD digits tree
253   TTree *tree;
254   Char_t treeName[12];
255   if (fSDigits) {
256     sprintf(treeName,"TreeS%d_TRD",fEvent);
257     tree = new TTree(treeName,"TRD SDigits");
258   }
259   else {
260     sprintf(treeName,"TreeD%d_TRD",fEvent);
261     tree = new TTree(treeName,"TRD Digits");
262   }
263
264   if (fDebug > 0) {
265     printf("<AliTRDdigitsManager::MakeBranch> ");
266     printf("Creating tree %s\n",treeName);
267   }
268
269   return MakeBranch(tree,file);
270
271 }
272
273 //_____________________________________________________________________________
274 Bool_t AliTRDdigitsManager::MakeBranch(TTree *tree, const Char_t *file)
275 {
276   //
277   // Creates the tree and branches for the digits and the dictionary
278   //
279
280   Int_t buffersize = 64000;
281
282   Bool_t status = kTRUE;
283
284   AliTRD *trd = (AliTRD *) gAlice->GetDetector("TRD") ;
285
286   if (tree) {
287     fTree = tree;
288   }
289
290   // Make the branch for the digits
291   if (fDigits) {
292     const AliTRDdataArray *kDigits = (AliTRDdataArray *) fDigits->At(0);
293     if (kDigits) {
294       trd->MakeBranchInTree(fTree,"TRDdigits",kDigits->IsA()->GetName()
295                                  ,&kDigits,buffersize,99,file);
296       if (fDebug > 0) {
297         printf("<AliTRDdigitsManager::MakeBranch> ");
298         printf("Making branch TRDdigits\n");
299       }
300     }
301     else {
302       status = kFALSE;
303     }
304   }
305   else {
306     status = kFALSE;
307   }
308
309   // Make the branches for the dictionaries
310   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
311     Char_t branchname[15];
312     sprintf(branchname,"TRDdictionary%d",iDict);
313     if (fDictionary[iDict]) {
314       const AliTRDdataArray *kDictionary = 
315               (AliTRDdataArray *) fDictionary[iDict]->At(0);
316       if (kDictionary) {
317         trd->MakeBranchInTree(fTree,branchname,kDictionary->IsA()->GetName()
318                              ,&kDictionary,buffersize,99,file);
319         if (fDebug > 0) {
320           printf("<AliTRDdigitsManager::MakeBranch> ");
321           printf("Making branch %s\n",branchname);
322         }
323       }
324       else {
325         status = kFALSE;
326       }
327     }
328     else {
329       status = kFALSE;
330     }
331   }
332
333   return status;
334
335 }
336
337 //_____________________________________________________________________________
338 Bool_t AliTRDdigitsManager::ReadDigits(TTree *tree)
339 {
340   //
341   // Reads the digit information from the input file
342   //
343
344   Bool_t status = kTRUE;
345
346   if (tree) {
347
348     fTree = tree;
349
350   }
351   else {
352
353     // Get the digits tree
354     Char_t treeName[12];
355     if (fSDigits) {
356       sprintf(treeName,"TreeS%d_TRD",fEvent);
357     }
358     else {
359       sprintf(treeName,"TreeD%d_TRD",fEvent);
360     }
361     if (fFile) {
362       fTree = (TTree *) fFile->Get(treeName);
363     }
364     else {
365       fTree = (TTree *) gDirectory->Get(treeName);
366     }
367
368     if (!fTree) {
369       if (fDebug > 0) {
370         printf("<AliTRDdigitsManager::ReadDigits> ");
371         printf("Could not find tree %s.\n",treeName);
372       }
373       return kFALSE;
374     }
375
376   }
377
378   if (!fDigits) {
379     if (fDebug > 0) {
380       printf("<AliTRDdigitsManager::ReadDigits> ");
381       printf("Create the data arrays.\n");
382     }
383     CreateArrays();
384   }
385
386   status = fDigits->LoadArray("TRDdigits",fTree);
387
388   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
389     Char_t branchname[15];
390     sprintf(branchname,"TRDdictionary%d",iDict);
391     status = fDictionary[iDict]->LoadArray(branchname,fTree);
392   }  
393
394   if (fDigits->TestBit(AliTRDdigit::RawDigit())) {
395     fIsRaw = kTRUE;
396   }
397   else {
398     fIsRaw = kFALSE;
399   }
400
401   return kTRUE;
402
403 }
404
405 //_____________________________________________________________________________
406 Bool_t AliTRDdigitsManager::WriteDigits()
407 {
408   //
409   // Writes out the TRD-digits and the dictionaries
410   //
411
412   // Store the contents of the segment array in the tree
413   if (!fDigits->StoreArray("TRDdigits",fTree)) {
414     printf("<AliTRDdigitsManager::WriteDigits> ");
415     printf("Error while storing digits in branch TRDdigits\n");
416     return kFALSE;
417   }
418   for (Int_t iDict = 0; iDict < kNDict; iDict++) {
419     Char_t branchname[15];
420     sprintf(branchname,"TRDdictionary%d",iDict);
421     if (!fDictionary[iDict]->StoreArray(branchname,fTree)) {
422       printf("<AliTRDdigitsManager::WriteDigits> ");
423       printf("Error while storing dictionary in branch %s\n",branchname);
424       return kFALSE;
425     }
426   }
427
428   // Write the new tree to the output file
429   fTree->Write();
430
431   return kTRUE;
432
433 }
434
435 //_____________________________________________________________________________
436 AliTRDdigit *AliTRDdigitsManager::GetDigit(Int_t row, Int_t col
437                                          , Int_t time, Int_t det) const
438 {
439   // 
440   // Creates a single digit object 
441   //
442
443   Int_t digits[4];
444   Int_t amp[1];
445
446   digits[0] = det;
447   digits[1] = row;
448   digits[2] = col;
449   digits[3] = time;
450
451   amp[0]    = GetDigits(det)->GetData(row,col,time);
452   
453   return (new AliTRDdigit(fIsRaw,digits,amp));
454
455 }
456
457 //_____________________________________________________________________________
458 Int_t AliTRDdigitsManager::GetTrack(Int_t track
459                                   , Int_t row, Int_t col, Int_t time
460                                   , Int_t det) const
461 {
462   // 
463   // Returns the MC-track numbers from the dictionary.
464   //
465
466   if ((track < 0) || (track >= kNDict)) {
467     TObject::Error("GetTracks"
468                   ,"track %d out of bounds (size: %d, this: 0x%08x)"
469                   ,track,kNDict,this);
470     return -1;
471   }
472
473   // Array contains index+1 to allow data compression
474   return (GetDictionary(det,track)->GetData(row,col,time) - 1);
475
476 }
477
478 //_____________________________________________________________________________
479 AliTRDdataArrayI *AliTRDdigitsManager::GetDigits(Int_t det) const
480 {
481   //
482   // Returns the digits array for one detector
483   //
484
485   return (AliTRDdataArrayI *) fDigits->At(det);
486
487 }
488
489 //_____________________________________________________________________________
490 AliTRDdataArrayI *AliTRDdigitsManager::GetDictionary(Int_t det, Int_t i) const
491 {
492   //
493   // Returns the dictionary for one detector
494   //
495
496   return (AliTRDdataArrayI *) fDictionary[i]->At(det);
497
498 }
499
500 //_____________________________________________________________________________
501 Int_t AliTRDdigitsManager::GetTrack(Int_t track, AliTRDdigit *Digit) const
502 {
503   // 
504   // Returns the MC-track numbers from the dictionary for a given digit
505   //
506
507   Int_t row  = Digit->GetRow();
508   Int_t col  = Digit->GetCol();
509   Int_t time = Digit->GetTime();
510   Int_t det  = Digit->GetDetector();
511
512   return GetTrack(track,row,col,time,det);
513
514 }
515
516 //_____________________________________________________________________________
517 AliTRDdigitsManager &AliTRDdigitsManager::operator=(const AliTRDdigitsManager &m)
518 {
519   //
520   // Assignment operator
521   //
522
523   if (this != &m) ((AliTRDdigitsManager &) m).Copy(*this);
524   return *this;
525
526 }