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