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