1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 Revision 1.7 2000/11/20 08:56:07 cblume
19 Cleanup of data arrays
21 Revision 1.6 2000/11/01 14:53:21 cblume
22 Merge with TRD-develop
24 Revision 1.1.4.3 2000/10/06 16:49:46 cblume
27 Revision 1.1.4.2 2000/10/04 16:34:58 cblume
28 Replace include files by forward declarations
30 Revision 1.5 2000/06/09 11:10:07 cblume
31 Compiler warnings and coding conventions, next round
33 Revision 1.4 2000/06/08 18:32:58 cblume
34 Make code compliant to coding conventions
36 Revision 1.3 2000/06/07 16:27:01 cblume
37 Try to remove compiler warnings on Sun and HP
39 Revision 1.2 2000/05/08 16:17:27 cblume
42 Revision 1.1.4.1 2000/05/08 14:55:03 cblume
45 Revision 1.1 2000/02/28 19:02:56 cblume
50 ///////////////////////////////////////////////////////////////////////////////
52 // Alice segment manager base class //
54 ///////////////////////////////////////////////////////////////////////////////
58 #include <TClonesArray.h>
59 #include <TDirectory.h>
63 #include "AliTRDarrayI.h"
64 #include "AliTRDsegmentID.h"
65 #include "AliTRDsegmentArrayBase.h"
67 ClassImp(AliTRDsegmentArrayBase)
69 //_____________________________________________________________________________
70 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
73 // AliTRDsegmentArrayBase default constructor
85 //_____________________________________________________________________________
86 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
89 // Create an array of objects of <classname>. The class must inherit from
90 // AliTRDsegmentID. The second argument sets the number of entries in
103 if (MakeArray(n) == kFALSE) {
104 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
110 //_____________________________________________________________________________
111 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
114 // AliTRDsegmentArrayBase copy constructor
117 ((AliTRDsegmentArrayBase &) a).Copy(*this);
121 //_____________________________________________________________________________
122 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
125 // AliTRDsegmentArrayBase destructor
133 if (fTree) delete fTree;
134 if (fTreeIndex) delete fTreeIndex;
135 if (fClass) delete fClass;
139 //_____________________________________________________________________________
140 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
141 ::operator=(const AliTRDsegmentArrayBase &a)
144 // Assignment operator
147 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
152 //_____________________________________________________________________________
153 void AliTRDsegmentArrayBase::Copy(TObject &a)
161 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
162 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
163 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
165 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
169 //_____________________________________________________________________________
170 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
173 // Sets the classname of the stored object
193 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
194 ,"ROOT system not initialized");
196 fClass = gROOT->GetClass(classname);
198 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
201 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
202 Error("AliTRDsegmentArrayBase"
203 ,"%s does not inherit from AliTRDsegmentID",classname);
211 //_____________________________________________________________________________
212 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
215 // Create a new object according to the class information
218 if (fClass == 0) return 0;
220 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
221 if (segment == 0) return 0;
227 //_____________________________________________________________________________
228 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
231 // Add a segment to the array
234 if (segment == 0) return kFALSE;
235 if (fSegment == 0) return kFALSE;
236 if (fClass == 0) return kFALSE;
238 if (!(segment->IsA()->InheritsFrom(fClass))) {
239 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
240 segment->IsA()->GetName());
244 fSegment->AddAt(segment,segment->GetID());
245 fNSegment = fSegment->GetLast() + 1;
251 //_____________________________________________________________________________
252 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
255 // Add a segment to the array
258 if (fSegment == 0) return 0;
259 if (fClass == 0) return 0;
261 AliTRDsegmentID *segment = NewSegment();
262 if (segment == 0) return 0;
264 fSegment->AddAt(segment,index);
265 segment->SetID(index);
266 fNSegment = fSegment->GetLast() + 1;
272 //_____________________________________________________________________________
273 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
276 // Create an array of pointers to the segments
283 if (fTreeIndex) delete fTreeIndex;
285 fSegment = new TObjArray(n);
286 fTreeIndex = new AliTRDarrayI();
289 if ((fSegment) && (fTreeIndex))
296 //_____________________________________________________________________________
297 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
300 // Remove a segment from the active memory
303 if ((*fSegment)[index]){
304 delete (*fSegment)[index]; // because problem with deleting TClonesArray
305 fSegment->RemoveAt(index);
310 //_____________________________________________________________________________
311 void AliTRDsegmentArrayBase::MakeTree(char *file)
314 // Create a tree for the segment
317 AliTRDsegmentID *psegment = NewSegment();
319 if (fTree) delete fTree;
320 fTree = new TTree("Segment Tree","Tree with segments");
322 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
324 fBranch->SetFile(file);
330 //_____________________________________________________________________________
331 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
334 // Connect a tree from current directory
343 fTree = (TTree*) gDirectory->Get(treeName);
344 if (fTree == 0) return kFALSE;
345 fBranch = fTree->GetBranch("Segment");
346 if (fBranch == 0) return kFALSE;
348 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
354 //_____________________________________________________________________________
355 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
358 // Load a segment with index <index> into the memory
361 if (fTreeIndex == 0) MakeDictionary(3000);
363 // First try to load dictionary
364 if (fTreeIndex == 0) return 0;
365 if (fBranch == 0) return 0;
366 if (index > fTreeIndex->fN) return 0;
367 AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
368 if (s == 0) s = NewSegment();
372 Int_t treeIndex = (*fTreeIndex)[index];
377 fBranch->SetAddress(&s);
378 fTree->GetEvent(treeIndex);
379 (*fSegment)[index] = (TObject*) s;
388 //_____________________________________________________________________________
389 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
392 // Load a segment at position <index> in the tree into the memory
395 if (fBranch == 0) return 0;
396 if (index > fTree->GetEntries()) return 0;
398 AliTRDsegmentID *s = NewSegment();
400 fBranch->SetAddress(&s);
401 fTree->GetEvent(index);
406 Int_t nindex = s->GetID();
407 ClearSegment(nindex);
408 (*fSegment)[nindex] = (TObject *) s;
414 //_____________________________________________________________________________
415 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
418 // Make a segment persistent
421 const AliTRDsegmentID *kSegment = (*this)[index];
422 if (kSegment == 0) return;
423 if (fTree == 0) MakeTree();
424 fBranch->SetAddress(&kSegment);
429 //_____________________________________________________________________________
430 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
433 // Create an index table for the tree
436 if (size < 1) return kFALSE;
437 if (fTreeIndex) delete fTreeIndex;
439 fTreeIndex = new AliTRDarrayI();
440 fTreeIndex->Set(size);
442 AliTRDsegmentID segment;
443 AliTRDsegmentID *psegment = &segment;
445 fBranch->SetAddress(&psegment);
446 TBranch *brindix = fTree->GetBranch("fSegmentID");
448 Int_t nevent = (Int_t) fTree->GetEntries();
449 for (Int_t i = 0; i < nevent; i++){
450 brindix->GetEvent(i);
451 Int_t treeIndex = segment.GetID();
452 if (fTreeIndex->fN < treeIndex)
453 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
454 (*fTreeIndex)[treeIndex] = i + 1;
461 //_____________________________________________________________________________
462 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
465 // Returns a segment with the given index <i>
468 if ((i < 0) || (i >= fNSegment)) return 0;
469 return (AliTRDsegmentID *) fSegment->At(i);
473 //_____________________________________________________________________________
474 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
477 // Returns a segment with the given index <i>
480 if ((i < 0) || (i >= fNSegment)) return 0;
481 return (AliTRDsegmentID *)((*fSegment)[i]);