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