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 ///////////////////////////////////////////////////////////////////////////////
20 // Alice segment manager base class //
22 ///////////////////////////////////////////////////////////////////////////////
26 #include <TClonesArray.h>
27 #include <TDirectory.h>
31 #include "AliTRDarrayI.h"
32 #include "AliTRDsegmentID.h"
33 #include "AliTRDsegmentArrayBase.h"
35 ClassImp(AliTRDsegmentArrayBase)
37 //_____________________________________________________________________________
38 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
41 // AliTRDsegmentArrayBase default constructor
53 //_____________________________________________________________________________
54 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
57 // Create an array of objects of <classname>. The class must inherit from
58 // AliTRDsegmentID. The second argument sets the number of entries in
71 if (MakeArray(n) == kFALSE) {
72 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
78 //_____________________________________________________________________________
79 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
82 // AliTRDsegmentArrayBase copy constructor
85 ((AliTRDsegmentArrayBase &) a).Copy(*this);
89 //_____________________________________________________________________________
90 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
93 // AliTRDsegmentArrayBase destructor
101 //if (fTree) delete fTree;
102 if (fTreeIndex) delete fTreeIndex;
106 //_____________________________________________________________________________
107 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
108 ::operator=(const AliTRDsegmentArrayBase &a)
111 // Assignment operator
114 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
119 //_____________________________________________________________________________
120 void AliTRDsegmentArrayBase::Copy(TObject &a)
128 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
129 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
130 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
132 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
136 //_____________________________________________________________________________
137 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
140 // Sets the classname of the stored object
156 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
157 ,"ROOT system not initialized");
159 fClass = gROOT->GetClass(classname);
161 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
164 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
165 Error("AliTRDsegmentArrayBase"
166 ,"%s does not inherit from AliTRDsegmentID",classname);
174 //_____________________________________________________________________________
175 AliTRDsegmentID *AliTRDsegmentArrayBase::NewSegment()
178 // Create a new object according to the class information
181 if (fClass == 0) return 0;
183 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
184 if (segment == 0) return 0;
190 //_____________________________________________________________________________
191 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
194 // Add a segment to the array
197 if (segment == 0) return kFALSE;
198 if (fSegment == 0) return kFALSE;
199 if (fClass == 0) return kFALSE;
201 if (!(segment->IsA()->InheritsFrom(fClass))) {
202 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
203 segment->IsA()->GetName());
207 fSegment->AddAt(segment,segment->GetID());
208 fNSegment = fSegment->GetLast() + 1;
214 //_____________________________________________________________________________
215 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
218 // Add a segment to the array
221 if (fSegment == 0) return 0;
222 if (fClass == 0) return 0;
224 AliTRDsegmentID *segment = NewSegment();
225 if (segment == 0) return 0;
227 fSegment->AddAt(segment,index);
228 segment->SetID(index);
229 fNSegment = fSegment->GetLast() + 1;
235 //_____________________________________________________________________________
236 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
239 // Create an array of pointers to the segments
246 if (fTreeIndex) delete fTreeIndex;
248 fSegment = new TObjArray(n);
249 fTreeIndex = new AliTRDarrayI();
252 if ((fSegment) && (fTreeIndex))
259 //_____________________________________________________________________________
260 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
263 // Remove a segment from the active memory
266 //PH if ((*fSegment)[index]){
267 //PH delete (*fSegment)[index]; // because problem with deleting TClonesArray
268 //PH fSegment->RemoveAt(index);
270 if (fSegment->At(index)){
271 delete fSegment->RemoveAt(index);
276 //_____________________________________________________________________________
277 void AliTRDsegmentArrayBase::MakeTree(char *file)
280 // Create a tree for the segment
283 AliTRDsegmentID *psegment = NewSegment();
285 if (fTree) delete fTree;
286 fTree = new TTree("Segment Tree","Tree with segments");
288 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
290 fBranch->SetFile(file);
296 //_____________________________________________________________________________
297 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
300 // Connect a tree from current directory
309 fTree = (TTree*) gDirectory->Get(treeName);
310 if (fTree == 0) return kFALSE;
311 fBranch = fTree->GetBranch("Segment");
312 if (fBranch == 0) return kFALSE;
314 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
320 //_____________________________________________________________________________
321 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
324 // Load a segment with index <index> into the memory
327 if (fTreeIndex == 0) MakeDictionary(3000);
329 // First try to load dictionary
330 if (fTreeIndex == 0) return 0;
331 if (fBranch == 0) return 0;
332 if (index > fTreeIndex->fN) return 0;
333 //PH AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
334 AliTRDsegmentID *s = (AliTRDsegmentID*) fSegment->At(index);
335 if (s == 0) s = NewSegment();
339 Int_t treeIndex = (*fTreeIndex)[index];
344 fBranch->SetAddress(&s);
345 fTree->GetEvent(treeIndex);
346 //PH (*fSegment)[index] = (TObject*) s;
347 fSegment->AddAt((TObject*) s, index);
356 //_____________________________________________________________________________
357 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
360 // Load a segment at position <index> in the tree into the memory
363 if (fBranch == 0) return 0;
364 if (index > fTree->GetEntries()) return 0;
366 AliTRDsegmentID *s = NewSegment();
368 fBranch->SetAddress(&s);
369 fTree->GetEvent(index);
374 Int_t nindex = s->GetID();
375 ClearSegment(nindex);
376 //PH (*fSegment)[nindex] = (TObject *) s;
377 fSegment->AddAt((TObject *) s, nindex);
383 //_____________________________________________________________________________
384 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
387 // Make a segment persistent
390 const AliTRDsegmentID *kSegment = (*this)[index];
391 if (kSegment == 0) return;
392 if (fTree == 0) MakeTree();
393 fBranch->SetAddress(&kSegment);
398 //_____________________________________________________________________________
399 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
402 // Create an index table for the tree
405 if (size < 1) return kFALSE;
406 if (fTreeIndex) delete fTreeIndex;
408 fTreeIndex = new AliTRDarrayI();
409 fTreeIndex->Set(size);
411 AliTRDsegmentID segment;
412 AliTRDsegmentID *psegment = &segment;
414 fBranch->SetAddress(&psegment);
415 TBranch *brindix = fTree->GetBranch("fSegmentID");
417 Int_t nevent = (Int_t) fTree->GetEntries();
418 for (Int_t i = 0; i < nevent; i++){
419 brindix->GetEvent(i);
420 Int_t treeIndex = segment.GetID();
421 if (fTreeIndex->fN < treeIndex)
422 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
423 (*fTreeIndex)[treeIndex] = i + 1;
430 //_____________________________________________________________________________
431 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i) const
434 // Returns a segment with the given index <i>
437 if ((i < 0) || (i >= fNSegment)) return 0;
438 return (AliTRDsegmentID *) fSegment->At(i);
442 //_____________________________________________________________________________
443 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
446 // Returns a segment with the given index <i>
449 if ((i < 0) || (i >= fNSegment)) return 0;
450 //PH return (AliTRDsegmentID *)((*fSegment)[i]);
451 return (AliTRDsegmentID *) fSegment->At(i);