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 ///////////////////////////////////////////////////////////////////////////////
24 #include "AliTRDsegmentArrayBase.h"
27 #include <TClonesArray.h>
28 #include <TDirectory.h>
32 #include "AliTRDarrayI.h"
33 #include "AliTRDsegmentID.h"
35 ClassImp(AliTRDsegmentArrayBase)
37 //_____________________________________________________________________________
38 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
41 // AliTRDsegmentArrayBase default constructor
53 //_____________________________________________________________________________
54 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const char *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)
83 // AliTRDsegmentArrayBase copy constructor
86 ((AliTRDsegmentArrayBase &) a).Copy(*this);
90 //_____________________________________________________________________________
91 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
94 // AliTRDsegmentArrayBase destructor
102 //if (fTree) delete fTree;
103 if (fTreeIndex) delete fTreeIndex;
107 //_____________________________________________________________________________
108 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
109 ::operator=(const AliTRDsegmentArrayBase &a)
112 // Assignment operator
115 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
120 //_____________________________________________________________________________
121 void AliTRDsegmentArrayBase::Copy(TObject &a) const
129 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
130 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
131 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
133 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
137 //_____________________________________________________________________________
138 Bool_t AliTRDsegmentArrayBase::SetClass(const char *classname)
141 // Sets the classname of the stored object
157 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
158 ,"ROOT system not initialized");
160 fClass = gROOT->GetClass(classname);
162 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
165 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
166 Error("AliTRDsegmentArrayBase"
167 ,"%s does not inherit from AliTRDsegmentID",classname);
175 //_____________________________________________________________________________
176 AliTRDsegmentID *AliTRDsegmentArrayBase::NewSegment()
179 // Create a new object according to the class information
182 if (fClass == 0) return 0;
184 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
185 if (segment == 0) return 0;
191 //_____________________________________________________________________________
192 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
195 // Add a segment to the array
198 if (segment == 0) return kFALSE;
199 if (fSegment == 0) return kFALSE;
200 if (fClass == 0) return kFALSE;
202 if (!(segment->IsA()->InheritsFrom(fClass))) {
203 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
204 segment->IsA()->GetName());
208 fSegment->AddAt(segment,segment->GetID());
209 fNSegment = fSegment->GetLast() + 1;
215 //_____________________________________________________________________________
216 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
219 // Add a segment to the array
222 if (fSegment == 0) return 0;
223 if (fClass == 0) return 0;
225 AliTRDsegmentID *segment = NewSegment();
226 if (segment == 0) return 0;
228 fSegment->AddAt(segment,index);
229 segment->SetID(index);
230 fNSegment = fSegment->GetLast() + 1;
236 //_____________________________________________________________________________
237 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
240 // Create an array of pointers to the segments
247 if (fTreeIndex) delete fTreeIndex;
249 fSegment = new TObjArray(n);
250 fTreeIndex = new AliTRDarrayI();
253 if ((fSegment) && (fTreeIndex))
260 //_____________________________________________________________________________
261 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
264 // Remove a segment from the active memory
267 //PH if ((*fSegment)[index]){
268 //PH delete (*fSegment)[index]; // because problem with deleting TClonesArray
269 //PH fSegment->RemoveAt(index);
271 if (fSegment->At(index)){
272 delete fSegment->RemoveAt(index);
277 //_____________________________________________________________________________
278 void AliTRDsegmentArrayBase::MakeTree(char *file)
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);
291 fBranch->SetFile(file);
297 //_____________________________________________________________________________
298 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
301 // Connect a tree from current directory
310 fTree = (TTree*) gDirectory->Get(treeName);
311 if (fTree == 0) return kFALSE;
312 fBranch = fTree->GetBranch("Segment");
313 if (fBranch == 0) return kFALSE;
315 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
321 //_____________________________________________________________________________
322 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
325 // Load a segment with index <index> into the memory
328 if (fTreeIndex == 0) MakeDictionary(3000);
330 // First try to load dictionary
331 if (fTreeIndex == 0) return 0;
332 if (fBranch == 0) return 0;
333 if (index > fTreeIndex->fN) return 0;
334 //PH AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
335 AliTRDsegmentID *s = (AliTRDsegmentID*) fSegment->At(index);
336 if (s == 0) s = NewSegment();
340 Int_t treeIndex = (*fTreeIndex)[index];
345 fBranch->SetAddress(&s);
346 fTree->GetEvent(treeIndex);
347 //PH (*fSegment)[index] = (TObject*) s;
348 fSegment->AddAt((TObject*) s, index);
357 //_____________________________________________________________________________
358 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
361 // Load a segment at position <index> in the tree into the memory
364 if (fBranch == 0) return 0;
365 if (index > fTree->GetEntries()) return 0;
367 AliTRDsegmentID *s = NewSegment();
369 fBranch->SetAddress(&s);
370 fTree->GetEvent(index);
375 Int_t nindex = s->GetID();
376 ClearSegment(nindex);
377 //PH (*fSegment)[nindex] = (TObject *) s;
378 fSegment->AddAt((TObject *) s, nindex);
384 //_____________________________________________________________________________
385 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
388 // Make a segment persistent
391 const AliTRDsegmentID *kSegment = (*this)[index];
392 if (kSegment == 0) return;
393 if (fTree == 0) MakeTree();
394 fBranch->SetAddress(&kSegment);
399 //_____________________________________________________________________________
400 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
403 // Create an index table for the tree
406 if (size < 1) return kFALSE;
407 if (fTreeIndex) delete fTreeIndex;
409 fTreeIndex = new AliTRDarrayI();
410 fTreeIndex->Set(size);
412 AliTRDsegmentID segment;
413 AliTRDsegmentID *psegment = &segment;
415 fBranch->SetAddress(&psegment);
416 TBranch *brindix = fTree->GetBranch("fSegmentID");
418 Int_t nevent = (Int_t) fTree->GetEntries();
419 for (Int_t i = 0; i < nevent; i++){
420 brindix->GetEvent(i);
421 Int_t treeIndex = segment.GetID();
422 if (fTreeIndex->fN < treeIndex)
423 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
424 (*fTreeIndex)[treeIndex] = i + 1;
431 //_____________________________________________________________________________
432 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i) const
435 // Returns a segment with the given index <i>
438 if ((i < 0) || (i >= fNSegment)) return 0;
439 return (AliTRDsegmentID *) fSegment->At(i);
443 //_____________________________________________________________________________
444 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
447 // Returns a segment with the given index <i>
450 if ((i < 0) || (i >= fNSegment)) return 0;
451 //PH return (AliTRDsegmentID *)((*fSegment)[i]);
452 return (AliTRDsegmentID *) fSegment->At(i);