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.8 2001/01/26 19:56:57 hristov
19 Major upgrade of AliRoot code
21 Revision 1.7 2000/11/20 08:56:07 cblume
22 Cleanup of data arrays
24 Revision 1.6 2000/11/01 14:53:21 cblume
25 Merge with TRD-develop
27 Revision 1.1.4.3 2000/10/06 16:49:46 cblume
30 Revision 1.1.4.2 2000/10/04 16:34:58 cblume
31 Replace include files by forward declarations
33 Revision 1.5 2000/06/09 11:10:07 cblume
34 Compiler warnings and coding conventions, next round
36 Revision 1.4 2000/06/08 18:32:58 cblume
37 Make code compliant to coding conventions
39 Revision 1.3 2000/06/07 16:27:01 cblume
40 Try to remove compiler warnings on Sun and HP
42 Revision 1.2 2000/05/08 16:17:27 cblume
45 Revision 1.1.4.1 2000/05/08 14:55:03 cblume
48 Revision 1.1 2000/02/28 19:02:56 cblume
53 ///////////////////////////////////////////////////////////////////////////////
55 // Alice segment manager base class //
57 ///////////////////////////////////////////////////////////////////////////////
61 #include <TClonesArray.h>
62 #include <TDirectory.h>
66 #include "AliTRDarrayI.h"
67 #include "AliTRDsegmentID.h"
68 #include "AliTRDsegmentArrayBase.h"
70 ClassImp(AliTRDsegmentArrayBase)
72 //_____________________________________________________________________________
73 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
76 // AliTRDsegmentArrayBase default constructor
88 //_____________________________________________________________________________
89 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
92 // Create an array of objects of <classname>. The class must inherit from
93 // AliTRDsegmentID. The second argument sets the number of entries in
106 if (MakeArray(n) == kFALSE) {
107 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
113 //_____________________________________________________________________________
114 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
117 // AliTRDsegmentArrayBase copy constructor
120 ((AliTRDsegmentArrayBase &) a).Copy(*this);
124 //_____________________________________________________________________________
125 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
128 // AliTRDsegmentArrayBase destructor
136 if (fTree) delete fTree;
137 if (fTreeIndex) delete fTreeIndex;
138 if (fClass) delete fClass;
142 //_____________________________________________________________________________
143 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
144 ::operator=(const AliTRDsegmentArrayBase &a)
147 // Assignment operator
150 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
155 //_____________________________________________________________________________
156 void AliTRDsegmentArrayBase::Copy(TObject &a)
164 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
165 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
166 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
168 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
172 //_____________________________________________________________________________
173 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
176 // Sets the classname of the stored object
196 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
197 ,"ROOT system not initialized");
199 fClass = gROOT->GetClass(classname);
201 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
204 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
205 Error("AliTRDsegmentArrayBase"
206 ,"%s does not inherit from AliTRDsegmentID",classname);
214 //_____________________________________________________________________________
215 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
218 // Create a new object according to the class information
221 if (fClass == 0) return 0;
223 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
224 if (segment == 0) return 0;
230 //_____________________________________________________________________________
231 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
234 // Add a segment to the array
237 if (segment == 0) return kFALSE;
238 if (fSegment == 0) return kFALSE;
239 if (fClass == 0) return kFALSE;
241 if (!(segment->IsA()->InheritsFrom(fClass))) {
242 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
243 segment->IsA()->GetName());
247 fSegment->AddAt(segment,segment->GetID());
248 fNSegment = fSegment->GetLast() + 1;
254 //_____________________________________________________________________________
255 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
258 // Add a segment to the array
261 if (fSegment == 0) return 0;
262 if (fClass == 0) return 0;
264 AliTRDsegmentID *segment = NewSegment();
265 if (segment == 0) return 0;
267 fSegment->AddAt(segment,index);
268 segment->SetID(index);
269 fNSegment = fSegment->GetLast() + 1;
275 //_____________________________________________________________________________
276 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
279 // Create an array of pointers to the segments
286 if (fTreeIndex) delete fTreeIndex;
288 fSegment = new TObjArray(n);
289 fTreeIndex = new AliTRDarrayI();
292 if ((fSegment) && (fTreeIndex))
299 //_____________________________________________________________________________
300 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
303 // Remove a segment from the active memory
306 if ((*fSegment)[index]){
307 delete (*fSegment)[index]; // because problem with deleting TClonesArray
308 fSegment->RemoveAt(index);
313 //_____________________________________________________________________________
314 void AliTRDsegmentArrayBase::MakeTree(char *file)
317 // Create a tree for the segment
320 AliTRDsegmentID *psegment = NewSegment();
322 if (fTree) delete fTree;
323 fTree = new TTree("Segment Tree","Tree with segments");
325 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
327 fBranch->SetFile(file);
333 //_____________________________________________________________________________
334 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
337 // Connect a tree from current directory
346 fTree = (TTree*) gDirectory->Get(treeName);
347 if (fTree == 0) return kFALSE;
348 fBranch = fTree->GetBranch("Segment");
349 if (fBranch == 0) return kFALSE;
351 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
357 //_____________________________________________________________________________
358 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
361 // Load a segment with index <index> into the memory
364 if (fTreeIndex == 0) MakeDictionary(3000);
366 // First try to load dictionary
367 if (fTreeIndex == 0) return 0;
368 if (fBranch == 0) return 0;
369 if (index > fTreeIndex->fN) return 0;
370 AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
371 if (s == 0) s = NewSegment();
375 Int_t treeIndex = (*fTreeIndex)[index];
380 fBranch->SetAddress(&s);
381 fTree->GetEvent(treeIndex);
382 (*fSegment)[index] = (TObject*) s;
391 //_____________________________________________________________________________
392 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
395 // Load a segment at position <index> in the tree into the memory
398 if (fBranch == 0) return 0;
399 if (index > fTree->GetEntries()) return 0;
401 AliTRDsegmentID *s = NewSegment();
403 fBranch->SetAddress(&s);
404 fTree->GetEvent(index);
409 Int_t nindex = s->GetID();
410 ClearSegment(nindex);
411 (*fSegment)[nindex] = (TObject *) s;
417 //_____________________________________________________________________________
418 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
421 // Make a segment persistent
424 const AliTRDsegmentID *kSegment = (*this)[index];
425 if (kSegment == 0) return;
426 if (fTree == 0) MakeTree();
427 fBranch->SetAddress(&kSegment);
432 //_____________________________________________________________________________
433 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
436 // Create an index table for the tree
439 if (size < 1) return kFALSE;
440 if (fTreeIndex) delete fTreeIndex;
442 fTreeIndex = new AliTRDarrayI();
443 fTreeIndex->Set(size);
445 AliTRDsegmentID segment;
446 AliTRDsegmentID *psegment = &segment;
448 fBranch->SetAddress(&psegment);
449 TBranch *brindix = fTree->GetBranch("fSegmentID");
451 Int_t nevent = (Int_t) fTree->GetEntries();
452 for (Int_t i = 0; i < nevent; i++){
453 brindix->GetEvent(i);
454 Int_t treeIndex = segment.GetID();
455 if (fTreeIndex->fN < treeIndex)
456 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
457 (*fTreeIndex)[treeIndex] = i + 1;
464 //_____________________________________________________________________________
465 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
468 // Returns a segment with the given index <i>
471 if ((i < 0) || (i >= fNSegment)) return 0;
472 return (AliTRDsegmentID *) fSegment->At(i);
476 //_____________________________________________________________________________
477 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
480 // Returns a segment with the given index <i>
483 if ((i < 0) || (i >= fNSegment)) return 0;
484 return (AliTRDsegmentID *)((*fSegment)[i]);