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