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>
33 #include "AliTRDsegmentArrayBase.h"
34 #include "AliTRDarrayI.h"
35 #include "AliTRDsegmentID.h"
37 ClassImp(AliTRDsegmentArrayBase)
39 //_____________________________________________________________________________
40 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase()
50 // AliTRDsegmentArrayBase default constructor
55 //_____________________________________________________________________________
56 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const char *classname, Int_t n)
66 // Create an array of objects of <classname>. The class must inherit from
67 // AliTRDsegmentID. The second argument sets the number of entries in
73 if (MakeArray(n) == kFALSE) {
74 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
80 //_____________________________________________________________________________
81 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
84 ,fTreeIndex(a.fTreeIndex)
85 ,fNSegment(a.fNSegment)
91 // AliTRDsegmentArrayBase copy constructor
96 //_____________________________________________________________________________
97 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
100 // AliTRDsegmentArrayBase destructor
114 //_____________________________________________________________________________
115 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
116 ::operator=(const AliTRDsegmentArrayBase &a)
119 // Assignment operator
122 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
127 //_____________________________________________________________________________
128 void AliTRDsegmentArrayBase::Copy(TObject &a) const
136 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
137 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
138 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
140 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
144 //_____________________________________________________________________________
145 Bool_t AliTRDsegmentArrayBase::SetClass(const char *classname)
148 // Sets the classname of the stored object
165 AliFatal("ROOT system not initialized");
169 fClass = gROOT->GetClass(classname);
171 AliError(Form("%s is not a valid class name",classname));
174 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
175 AliError(Form("%s does not inherit from AliTRDsegmentID",classname));
183 //_____________________________________________________________________________
184 AliTRDsegmentID *AliTRDsegmentArrayBase::NewSegment()
187 // Create a new object according to the class information
194 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
205 //_____________________________________________________________________________
206 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
209 // Add a segment to the array
222 if (!(segment->IsA()->InheritsFrom(fClass))) {
223 AliError(Form("added class %s is not of proper type"
224 ,segment->IsA()->GetName()));
228 fSegment->AddAt(segment,segment->GetID());
229 fNSegment = fSegment->GetLast() + 1;
235 //_____________________________________________________________________________
236 AliTRDsegmentID *AliTRDsegmentArrayBase::AddSegment(Int_t index)
239 // Add a segment to the array
249 AliTRDsegmentID *segment = NewSegment();
254 fSegment->AddAt(segment,index);
255 segment->SetID(index);
256 fNSegment = fSegment->GetLast() + 1;
262 //_____________________________________________________________________________
263 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
266 // Create an array of pointers to the segments
273 if (fTreeIndex) delete fTreeIndex;
275 fSegment = new TObjArray(n);
276 fTreeIndex = new AliTRDarrayI();
279 if ((fSegment) && (fTreeIndex)) {
288 //_____________________________________________________________________________
289 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
292 // Remove a segment from the active memory
295 if (fSegment->At(index)) {
296 delete fSegment->RemoveAt(index);
301 //_____________________________________________________________________________
302 void AliTRDsegmentArrayBase::MakeTree(char *file)
305 // Create a tree for the segment
308 AliTRDsegmentID *psegment = NewSegment();
314 fTree = new TTree("Segment Tree","Tree with segments");
315 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
318 fBranch->SetFile(file);
325 //_____________________________________________________________________________
326 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char *treeName)
329 // Connect a tree from current directory
338 fTree = (TTree *) gDirectory->Get(treeName);
342 fBranch = fTree->GetBranch("Segment");
347 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
353 //_____________________________________________________________________________
354 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
357 // Load a segment with index <index> into the memory
360 if (fTreeIndex == 0) {
361 MakeDictionary(3000);
364 // First try to load dictionary
365 if (fTreeIndex == 0) {
371 if (index > fTreeIndex->fN) {
375 AliTRDsegmentID *s = (AliTRDsegmentID *) fSegment->At(index);
382 Int_t treeIndex = (*fTreeIndex)[index];
389 fBranch->SetAddress(&s);
390 fTree->GetEvent(treeIndex);
391 fSegment->AddAt((TObject*) s, index);
401 //_____________________________________________________________________________
402 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
405 // Load a segment at position <index> in the tree into the memory
411 if (index > fTree->GetEntries()) {
415 AliTRDsegmentID *s = NewSegment();
417 fBranch->SetAddress(&s);
418 fTree->GetEvent(index);
424 Int_t nindex = s->GetID();
425 ClearSegment(nindex);
426 fSegment->AddAt((TObject *) s, nindex);
432 //_____________________________________________________________________________
433 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
436 // Make a segment persistent
439 const AliTRDsegmentID *kSegment = (*this)[index];
446 fBranch->SetAddress(&kSegment);
451 //_____________________________________________________________________________
452 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
455 // Create an index table for the tree
465 fTreeIndex = new AliTRDarrayI();
466 fTreeIndex->Set(size);
468 AliTRDsegmentID segment;
469 AliTRDsegmentID *psegment = &segment;
471 fBranch->SetAddress(&psegment);
472 TBranch *brindix = fTree->GetBranch("fSegmentID");
474 Int_t nevent = (Int_t) fTree->GetEntries();
475 for (Int_t i = 0; i < nevent; i++) {
476 brindix->GetEvent(i);
477 Int_t treeIndex = segment.GetID();
478 if (fTreeIndex->fN < treeIndex) {
479 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
481 (*fTreeIndex)[treeIndex] = i + 1;
488 //_____________________________________________________________________________
489 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i) const
492 // Returns a segment with the given index <i>
500 return (AliTRDsegmentID *) fSegment->At(i);
504 //_____________________________________________________________________________
505 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
508 // Returns a segment with the given index <i>
516 return (AliTRDsegmentID *) fSegment->At(i);