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