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