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