]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDsegmentArrayBase.cxx
Gsbool and GetMCGeomType added
[u/mrichter/AliRoot.git] / TRD / AliTRDsegmentArrayBase.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.11  2001/11/19 08:44:08  cblume
19 Fix bugs reported by Rene
20
21 Revision 1.10  2001/08/30 09:31:22  hristov
22 The operator[] is replaced by At() or AddAt() in case of TObjArray.
23
24 Revision 1.9  2001/07/27 13:03:15  hristov
25 Default Branch split level set to 99
26
27 Revision 1.8  2001/01/26 19:56:57  hristov
28 Major upgrade of AliRoot code
29
30 Revision 1.7  2000/11/20 08:56:07  cblume
31 Cleanup of data arrays
32
33 Revision 1.6  2000/11/01 14:53:21  cblume
34 Merge with TRD-develop
35
36 Revision 1.1.4.3  2000/10/06 16:49:46  cblume
37 Made Getters const
38
39 Revision 1.1.4.2  2000/10/04 16:34:58  cblume
40 Replace include files by forward declarations
41
42 Revision 1.5  2000/06/09 11:10:07  cblume
43 Compiler warnings and coding conventions, next round
44
45 Revision 1.4  2000/06/08 18:32:58  cblume
46 Make code compliant to coding conventions
47
48 Revision 1.3  2000/06/07 16:27:01  cblume
49 Try to remove compiler warnings on Sun and HP
50
51 Revision 1.2  2000/05/08 16:17:27  cblume
52 Merge TRD-develop
53
54 Revision 1.1.4.1  2000/05/08 14:55:03  cblume
55 Bug fixes
56
57 Revision 1.1  2000/02/28 19:02:56  cblume
58 Add new TRD classes
59
60 */
61
62 ///////////////////////////////////////////////////////////////////////////////
63 //                                                                           //
64 //  Alice segment manager base class                                         //
65 //                                                                           //
66 ///////////////////////////////////////////////////////////////////////////////
67
68 #include <TROOT.h>
69 #include <TTree.h>
70 #include <TClonesArray.h>
71 #include <TDirectory.h>
72 #include <TError.h>
73 #include <TClass.h>
74
75 #include "AliTRDarrayI.h"
76 #include "AliTRDsegmentID.h"
77 #include "AliTRDsegmentArrayBase.h"
78
79 ClassImp(AliTRDsegmentArrayBase)
80   
81 //_____________________________________________________________________________
82 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
83 {
84   //
85   // AliTRDsegmentArrayBase default constructor
86   //
87
88   fNSegment  = 0;
89   fSegment   = 0; 
90   fTreeIndex = 0;
91   fTree      = 0;
92   fClass     = 0;
93   fBranch    = 0;
94
95 }
96
97 //_____________________________________________________________________________
98 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
99 {
100   //
101   //  Create an array of objects of <classname>. The class must inherit from
102   //  AliTRDsegmentID. The second argument sets the number of entries in 
103   //  the array.
104   //
105
106   fNSegment  = 0;
107   fSegment   = 0; 
108   fTreeIndex = 0;
109   fTree      = 0;
110   fClass     = 0;
111   fBranch    = 0;
112
113   SetClass(classname);
114
115   if (MakeArray(n) == kFALSE) {
116     Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
117     return;
118   }
119
120 }
121
122 //_____________________________________________________________________________
123 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
124 {
125   //
126   // AliTRDsegmentArrayBase copy constructor
127   //
128   
129   ((AliTRDsegmentArrayBase &) a).Copy(*this);
130
131 }
132
133 //_____________________________________________________________________________
134 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
135 {
136   //
137   // AliTRDsegmentArrayBase destructor
138   //
139
140   if (fNSegment){
141     fSegment->Delete();
142     delete fSegment;
143   }
144
145   //if (fTree)      delete fTree;
146   if (fTreeIndex) delete fTreeIndex;
147
148 }
149
150 //_____________________________________________________________________________
151 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
152                         ::operator=(const AliTRDsegmentArrayBase &a)
153 {
154   //
155   // Assignment operator
156   //
157
158   if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
159   return *this;
160
161 }
162
163 //_____________________________________________________________________________
164 void AliTRDsegmentArrayBase::Copy(TObject &a)
165 {
166   //
167   // Copy function
168   //
169
170   TNamed::Copy(a);
171
172   fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
173   fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
174   fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
175
176   ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
177
178 }
179
180 //_____________________________________________________________________________
181 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
182 {
183   //
184   // Sets the classname of the stored object
185   //
186
187   if (fTree    != 0) {
188     delete fTree;
189     fTree      = 0;
190     fBranch    = 0;
191     delete fTreeIndex;
192     fTreeIndex = 0;
193   } 
194   if (fSegment != 0) {
195     fSegment->Delete();
196     delete fSegment;
197     fSegment   = 0;
198   }
199
200   if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
201                      ,"ROOT system not initialized");
202    
203    fClass = gROOT->GetClass(classname);
204    if (!fClass) {
205      Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
206      return kFALSE;
207    }
208    if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
209      Error("AliTRDsegmentArrayBase"
210           ,"%s does not inherit from AliTRDsegmentID",classname);
211      return kFALSE;
212    }
213   
214    return kTRUE;
215
216 }
217
218 //_____________________________________________________________________________
219 AliTRDsegmentID *AliTRDsegmentArrayBase::NewSegment()
220 {
221   //
222   // Create a new object according to the class information
223   //
224
225   if (fClass  == 0) return 0;
226
227   AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
228   if (segment == 0) return 0;
229
230   return segment;
231
232 }
233
234 //_____________________________________________________________________________
235 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
236 {
237   //
238   // Add a segment to the array
239   //
240
241   if (segment  == 0) return kFALSE;
242   if (fSegment == 0) return kFALSE;
243   if (fClass   == 0) return kFALSE;
244
245   if (!(segment->IsA()->InheritsFrom(fClass))) {
246     Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
247           segment->IsA()->GetName());
248     return kFALSE;
249   }
250
251   fSegment->AddAt(segment,segment->GetID());
252   fNSegment = fSegment->GetLast() + 1;
253
254   return kTRUE;
255
256 }
257
258 //_____________________________________________________________________________
259 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
260 {
261   //
262   // Add a segment to the array
263   //
264
265   if (fSegment == 0) return 0;
266   if (fClass   == 0) return 0;
267
268   AliTRDsegmentID *segment = NewSegment();
269   if (segment  == 0) return 0;
270
271   fSegment->AddAt(segment,index);
272   segment->SetID(index);
273   fNSegment = fSegment->GetLast() + 1;
274
275   return segment;
276
277 }
278
279 //_____________________________________________________________________________
280 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
281 {
282   //
283   // Create an array of pointers to the segments
284   //
285
286   if (fSegment) {
287     fSegment->Delete();
288     delete fSegment;
289   }
290   if (fTreeIndex) delete fTreeIndex;  
291
292   fSegment   = new TObjArray(n);
293   fTreeIndex = new AliTRDarrayI();
294   fTreeIndex->Set(n);
295   fNSegment  = n;
296   if ((fSegment) && (fTreeIndex)) 
297     return kTRUE;
298   else 
299     return kFALSE;
300                   
301 }
302
303 //_____________________________________________________________________________
304 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
305 {
306   //
307   // Remove a segment from the active memory    
308   //
309
310   //PH  if ((*fSegment)[index]){
311   //PH    delete (*fSegment)[index]; // because problem with deleting TClonesArray
312   //PH    fSegment->RemoveAt(index);
313   //PH  }
314   if (fSegment->At(index)){
315     delete fSegment->RemoveAt(index);
316   }
317
318 }
319
320 //_____________________________________________________________________________
321 void AliTRDsegmentArrayBase::MakeTree(char *file)
322 {
323   //
324   // Create a tree for the segment
325   //
326
327   AliTRDsegmentID *psegment = NewSegment();  
328
329   if (fTree) delete fTree;
330   fTree   = new TTree("Segment Tree","Tree with segments");
331
332   fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
333   if (file) 
334       fBranch->SetFile(file);      
335
336   delete psegment;
337
338 }              
339
340 //_____________________________________________________________________________
341 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
342 {
343   //
344   // Connect a tree from current directory  
345   //
346
347   if (fTree){
348     delete fTree;
349     fTree   = 0;
350     fBranch = 0;
351   }
352
353   fTree   = (TTree*) gDirectory->Get(treeName);
354   if (fTree   == 0) return kFALSE;
355   fBranch = fTree->GetBranch("Segment");
356   if (fBranch == 0) return kFALSE;
357
358   MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
359
360   return kTRUE;
361
362 }
363
364 //_____________________________________________________________________________
365 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
366 {
367   //
368   // Load a segment with index <index> into the memory
369   //
370
371   if (fTreeIndex == 0) MakeDictionary(3000);
372
373   // First try to load dictionary 
374   if (fTreeIndex == 0)        return 0;
375   if (fBranch    == 0)        return 0;
376   if (index > fTreeIndex->fN) return 0;
377   //PH  AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
378   AliTRDsegmentID *s = (AliTRDsegmentID*) fSegment->At(index);
379   if (s == 0) s = NewSegment();
380   s->SetID(index);
381   
382   if (s != 0) {
383     Int_t treeIndex = (*fTreeIndex)[index];
384     if (treeIndex < 1) 
385       return 0;
386     else 
387       treeIndex--;   
388     fBranch->SetAddress(&s);
389     fTree->GetEvent(treeIndex);
390     //PH    (*fSegment)[index] = (TObject*) s;
391     fSegment->AddAt((TObject*) s, index);
392   }
393   else 
394     return 0;
395
396   return s;
397
398 }
399
400 //_____________________________________________________________________________
401 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
402 {
403   //
404   // Load a segment at position <index> in the tree into the memory
405   //
406
407   if (fBranch == 0)                return 0;
408   if (index > fTree->GetEntries()) return 0;
409
410   AliTRDsegmentID *s = NewSegment();  
411   if (s) {
412     fBranch->SetAddress(&s);
413     fTree->GetEvent(index);
414   }
415   else 
416     return 0;
417
418   Int_t nindex = s->GetID();
419   ClearSegment(nindex);
420   //PH  (*fSegment)[nindex] = (TObject *) s;
421   fSegment->AddAt((TObject *) s, nindex);
422
423   return s;
424
425 }
426
427 //_____________________________________________________________________________
428 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
429 {
430   //
431   // Make a segment persistent 
432   //
433
434   const AliTRDsegmentID *kSegment = (*this)[index];
435   if (kSegment == 0) return;
436   if (fTree    == 0) MakeTree();
437   fBranch->SetAddress(&kSegment);
438   fTree->Fill();
439
440 }
441
442 //_____________________________________________________________________________
443 Bool_t  AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
444 {
445   //
446   // Create an index table for the tree
447   //  
448
449   if (size < 1)   return kFALSE;
450   if (fTreeIndex) delete fTreeIndex;
451
452   fTreeIndex = new AliTRDarrayI(); 
453   fTreeIndex->Set(size);
454   
455   AliTRDsegmentID   segment;
456   AliTRDsegmentID *psegment = &segment;
457
458   fBranch->SetAddress(&psegment);
459   TBranch *brindix = fTree->GetBranch("fSegmentID");
460
461   Int_t nevent = (Int_t) fTree->GetEntries();  
462   for (Int_t i = 0; i < nevent; i++){
463     brindix->GetEvent(i);
464     Int_t treeIndex = segment.GetID();
465     if (fTreeIndex->fN < treeIndex) 
466       fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
467     (*fTreeIndex)[treeIndex] = i + 1; 
468   }
469
470   return kTRUE;
471
472 }
473
474 //_____________________________________________________________________________
475 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
476 {
477   //
478   // Returns a segment with the given index <i>
479   //
480
481   if ((i < 0) || (i >= fNSegment)) return 0; 
482   return (AliTRDsegmentID *) fSegment->At(i);
483
484 }
485
486 //_____________________________________________________________________________
487 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
488 {
489   //
490   // Returns a segment with the given index <i>
491   //
492
493   if ((i < 0) || (i >= fNSegment)) return 0; 
494   //PH  return (AliTRDsegmentID *)((*fSegment)[i]);
495   return (AliTRDsegmentID *) fSegment->At(i);
496
497 }