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.4 2000/06/08 18:32:58 cblume
19 Make code compliant to coding conventions
21 Revision 1.3 2000/06/07 16:27:01 cblume
22 Try to remove compiler warnings on Sun and HP
24 Revision 1.2 2000/05/08 16:17:27 cblume
27 Revision 1.1.4.1 2000/05/08 14:55:03 cblume
30 Revision 1.1 2000/02/28 19:02:56 cblume
35 ///////////////////////////////////////////////////////////////////////////////
37 // Alice segment manager base class //
39 ///////////////////////////////////////////////////////////////////////////////
43 #include "TClonesArray.h"
44 #include "TDirectory.h"
45 #include "AliTRDarrayI.h"
49 #include "AliTRDsegmentID.h"
50 #include "AliTRDsegmentArrayBase.h"
52 ClassImp(AliTRDsegmentArrayBase)
54 //_____________________________________________________________________________
55 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
58 // AliTRDsegmentArrayBase default constructor
69 //_____________________________________________________________________________
70 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
73 // Create an array of objects of <classname>. The class must inherit from
74 // AliTRDsegmentID. The second argument sets the number of entries in
86 if (MakeArray(n) == kFALSE) {
87 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
93 //_____________________________________________________________________________
94 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
97 // AliTRDsegmentArrayBase copy constructor
100 ((AliTRDsegmentArrayBase &) a).Copy(*this);
104 //_____________________________________________________________________________
105 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
108 // AliTRDsegmentArrayBase destructor
116 if (fTree) delete fTree;
117 if (fTreeIndex) delete fTreeIndex;
118 if (fClass) delete fClass;
122 //_____________________________________________________________________________
123 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
124 ::operator=(const AliTRDsegmentArrayBase &a)
127 // Assignment operator
130 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
135 //_____________________________________________________________________________
136 void AliTRDsegmentArrayBase::Copy(TObject &a)
144 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
145 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
146 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
148 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
152 //_____________________________________________________________________________
153 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
156 // Sets the classname of the stored object
176 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
177 ,"ROOT system not initialized");
179 fClass = gROOT->GetClass(classname);
181 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
184 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
185 Error("AliTRDsegmentArrayBase"
186 ,"%s does not inherit from AliTRDsegmentID",classname);
194 //_____________________________________________________________________________
195 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
198 // Create a new object according to the class information
201 if (fClass == 0) return 0;
203 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
204 if (segment == 0) return 0;
210 //_____________________________________________________________________________
211 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
214 // Add a segment to the array
217 if (segment == 0) return kFALSE;
218 if (fSegment == 0) return kFALSE;
219 if (fClass == 0) return kFALSE;
221 if (!(segment->IsA()->InheritsFrom(fClass))) {
222 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
223 segment->IsA()->GetName());
227 fSegment->AddAt(segment,segment->GetID());
228 fNSegment = fSegment->GetLast() + 1;
234 //_____________________________________________________________________________
235 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
238 // Add a segment to the array
241 if (fSegment == 0) return 0;
242 if (fClass == 0) return 0;
244 AliTRDsegmentID *segment = NewSegment();
245 if (segment == 0) return 0;
247 fSegment->AddAt(segment,index);
248 segment->SetID(index);
249 fNSegment = fSegment->GetLast() + 1;
255 //_____________________________________________________________________________
256 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
259 // Create an array of pointers to the segments
266 if (fTreeIndex) delete fTreeIndex;
268 fSegment = new TObjArray(n);
269 fTreeIndex = new AliTRDarrayI;
272 if ((fSegment) && (fTreeIndex))
279 //_____________________________________________________________________________
280 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
283 // Remove a segment from the active memory
286 if ((*fSegment)[index]){
287 delete (*fSegment)[index]; // because problem with deleting TClonesArray
288 fSegment->RemoveAt(index);
293 //_____________________________________________________________________________
294 void AliTRDsegmentArrayBase::MakeTree()
297 // Create a tree for the segment
300 AliTRDsegmentID *psegment = NewSegment();
302 if (fTree) delete fTree;
303 fTree = new TTree("Segment Tree","Tree with segments");
305 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
311 //_____________________________________________________________________________
312 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
315 // Connect a tree from current directory
324 fTree = (TTree*) gDirectory->Get(treeName);
325 if (fTree == 0) return kFALSE;
326 fBranch = fTree->GetBranch("Segment");
327 if (fBranch == 0) return kFALSE;
329 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
335 //_____________________________________________________________________________
336 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
339 // Load a segment with index <index> into the memory
342 if (fTreeIndex == 0) MakeDictionary(3000);
344 // First try to load dictionary
345 if (fTreeIndex == 0) return 0;
346 if (fBranch == 0) return 0;
347 if (index > fTreeIndex->fN) return 0;
348 AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
349 if (s == 0) s = NewSegment();
353 Int_t treeIndex = (*fTreeIndex)[index];
358 fBranch->SetAddress(&s);
359 fTree->GetEvent(treeIndex);
360 (*fSegment)[index] = (TObject*) s;
369 //_____________________________________________________________________________
370 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
373 // Load a segment at position <index> in the tree into the memory
376 if (fBranch == 0) return 0;
377 if (index > fTree->GetEntries()) return 0;
379 AliTRDsegmentID *s = NewSegment();
381 fBranch->SetAddress(&s);
382 fTree->GetEvent(index);
387 Int_t nindex = s->GetID();
388 ClearSegment(nindex);
389 (*fSegment)[nindex] = (TObject *) s;
395 //_____________________________________________________________________________
396 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
399 // Make a segment persistent
402 const AliTRDsegmentID *kSegment = (*this)[index];
403 if (kSegment == 0) return;
404 if (fTree == 0) MakeTree();
405 fBranch->SetAddress(&kSegment);
410 //_____________________________________________________________________________
411 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
414 // Create an index table for the tree
417 if (size < 1) return kFALSE;
418 if (fTreeIndex) delete fTreeIndex;
420 fTreeIndex = new AliTRDarrayI();
421 fTreeIndex->Set(size);
423 AliTRDsegmentID segment;
424 AliTRDsegmentID *psegment = &segment;
426 fBranch->SetAddress(&psegment);
427 TBranch *brindix = fTree->GetBranch("fSegmentID");
429 Int_t nevent = (Int_t) fTree->GetEntries();
430 for (Int_t i = 0; i < nevent; i++){
431 brindix->GetEvent(i);
432 Int_t treeIndex = segment.GetID();
433 if (fTreeIndex->fN < treeIndex)
434 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
435 (*fTreeIndex)[treeIndex] = i + 1;
442 //_____________________________________________________________________________
443 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
446 // Returns a segment with the given index <i>
449 if ((i < 0) || (i >= fNSegment)) return 0;
450 return (AliTRDsegmentID *) fSegment->At(i);
454 //_____________________________________________________________________________
455 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i)
458 // Returns a segment with the given index <i>
461 if ((i < 0) || (i >= fNSegment)) return 0;
462 return (AliTRDsegmentID *)((*fSegment)[i]);