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.3 2000/06/07 16:27:01 cblume
19 Try to remove compiler warnings on Sun and HP
21 Revision 1.2 2000/05/08 16:17:27 cblume
24 Revision 1.1.4.1 2000/05/08 14:55:03 cblume
27 Revision 1.1 2000/02/28 19:02:56 cblume
32 ///////////////////////////////////////////////////////////////////////////////
34 // Alice segment manager base class //
36 ///////////////////////////////////////////////////////////////////////////////
40 #include "TClonesArray.h"
41 #include "TDirectory.h"
42 #include "AliTRDarrayI.h"
46 #include "AliTRDsegmentID.h"
47 #include "AliTRDsegmentArrayBase.h"
49 ClassImp(AliTRDsegmentArrayBase)
51 //_____________________________________________________________________________
52 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
55 // AliTRDsegmentArrayBase default constructor
66 //_____________________________________________________________________________
67 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
70 // Create an array of objects of <classname>. The class must inherit from
71 // AliTRDsegmentID. The second argument sets the number of entries in
83 if (MakeArray(n) == kFALSE) {
84 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
90 //_____________________________________________________________________________
91 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(AliTRDsegmentArrayBase &a)
94 // AliTRDsegmentArrayBase copy constructor
101 //_____________________________________________________________________________
102 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
105 // AliTRDsegmentArrayBase destructor
113 if (fTree) delete fTree;
114 if (fTreeIndex) delete fTreeIndex;
115 if (fClass) delete fClass;
119 //_____________________________________________________________________________
120 void AliTRDsegmentArrayBase::Copy(AliTRDsegmentArrayBase &a)
128 fSegment->Copy(*a.fSegment);
129 fTreeIndex->Copy(*a.fTreeIndex);
130 fClass->Copy(*a.fClass);
132 a.fNSegment = fNSegment;
136 //_____________________________________________________________________________
137 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
140 // Sets the classname of the stored object
160 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
161 ,"ROOT system not initialized");
163 fClass = gROOT->GetClass(classname);
165 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
168 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
169 Error("AliTRDsegmentArrayBase"
170 ,"%s does not inherit from AliTRDsegmentID",classname);
178 //_____________________________________________________________________________
179 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
182 // Create a new object according to the class information
185 if (fClass == 0) return 0;
187 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
188 if (segment == 0) return 0;
194 //_____________________________________________________________________________
195 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
198 // Add a segment to the array
201 if (segment == 0) return kFALSE;
202 if (fSegment == 0) return kFALSE;
203 if (fClass == 0) return kFALSE;
205 if (!(segment->IsA()->InheritsFrom(fClass))) {
206 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
207 segment->IsA()->GetName());
211 fSegment->AddAt(segment,segment->GetID());
212 fNSegment = fSegment->GetLast() + 1;
218 //_____________________________________________________________________________
219 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
222 // Add a segment to the array
225 if (fSegment == 0) return 0;
226 if (fClass == 0) return 0;
228 AliTRDsegmentID *segment = NewSegment();
229 if (segment == 0) return 0;
231 fSegment->AddAt(segment,index);
232 segment->SetID(index);
233 fNSegment = fSegment->GetLast() + 1;
239 //_____________________________________________________________________________
240 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
243 // Create an array of pointers to the segments
250 if (fTreeIndex) delete fTreeIndex;
252 fSegment = new TObjArray(n);
253 fTreeIndex = new AliTRDarrayI;
256 if ((fSegment) && (fTreeIndex))
263 //_____________________________________________________________________________
264 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
267 // Remove a segment from the active memory
270 if ((*fSegment)[index]){
271 delete (*fSegment)[index]; // because problem with deleting TClonesArray
272 fSegment->RemoveAt(index);
277 //_____________________________________________________________________________
278 void AliTRDsegmentArrayBase::MakeTree()
281 // Create a tree for the segment
284 AliTRDsegmentID *psegment = NewSegment();
286 if (fTree) delete fTree;
287 fTree = new TTree("Segment Tree","Tree with segments");
289 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
295 //_____________________________________________________________________________
296 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
299 // Connect a tree from current directory
308 fTree = (TTree*) gDirectory->Get(treeName);
309 if (fTree == 0) return kFALSE;
310 fBranch = fTree->GetBranch("Segment");
311 if (fBranch == 0) return kFALSE;
313 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
319 //_____________________________________________________________________________
320 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
323 // Load a segment with index <index> into the memory
326 if (fTreeIndex == 0) MakeDictionary(3000);
328 // First try to load dictionary
329 if (fTreeIndex == 0) return 0;
330 if (fBranch == 0) return 0;
331 if (index > fTreeIndex->fN) return 0;
332 AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
333 if (s == 0) s = NewSegment();
337 Int_t treeIndex = (*fTreeIndex)[index];
342 fBranch->SetAddress(&s);
343 fTree->GetEvent(treeIndex);
344 (*fSegment)[index] = (TObject*) s;
353 //_____________________________________________________________________________
354 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
357 // Load a segment at position <index> in the tree into the memory
360 if (fBranch == 0) return 0;
361 if (index > fTree->GetEntries()) return 0;
363 AliTRDsegmentID *s = NewSegment();
365 fBranch->SetAddress(&s);
366 fTree->GetEvent(index);
371 Int_t nindex = s->GetID();
372 ClearSegment(nindex);
373 (*fSegment)[nindex] = (TObject *) s;
379 //_____________________________________________________________________________
380 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
383 // Make a segment persistent
386 const AliTRDsegmentID *kSegment = (*this)[index];
387 if (kSegment == 0) return;
388 if (fTree == 0) MakeTree();
389 fBranch->SetAddress(&kSegment);
394 //_____________________________________________________________________________
395 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
398 // Create an index table for the tree
401 if (size < 1) return kFALSE;
402 if (fTreeIndex) delete fTreeIndex;
404 fTreeIndex = new AliTRDarrayI();
405 fTreeIndex->Set(size);
407 AliTRDsegmentID segment;
408 AliTRDsegmentID *psegment = &segment;
410 fBranch->SetAddress(&psegment);
411 TBranch *brindix = fTree->GetBranch("fSegmentID");
413 Int_t nevent = (Int_t) fTree->GetEntries();
414 for (Int_t i = 0; i < nevent; i++){
415 brindix->GetEvent(i);
416 Int_t treeIndex = segment.GetID();
417 if (fTreeIndex->fN < treeIndex)
418 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
419 (*fTreeIndex)[treeIndex] = i + 1;
426 //_____________________________________________________________________________
427 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
430 // Returns a segment with the given index <i>
433 if ((i < 0) || (i >= fNSegment)) return 0;
434 return (AliTRDsegmentID *) fSegment->At(i);
438 //_____________________________________________________________________________
439 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i)
442 // Returns a segment with the given index <i>
445 if ((i < 0) || (i >= fNSegment)) return 0;
446 return (AliTRDsegmentID *)((*fSegment)[i]);