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.6 2000/11/01 14:53:21 cblume
19 Merge with TRD-develop
21 Revision 1.1.4.3 2000/10/06 16:49:46 cblume
24 Revision 1.1.4.2 2000/10/04 16:34:58 cblume
25 Replace include files by forward declarations
27 Revision 1.5 2000/06/09 11:10:07 cblume
28 Compiler warnings and coding conventions, next round
30 Revision 1.4 2000/06/08 18:32:58 cblume
31 Make code compliant to coding conventions
33 Revision 1.3 2000/06/07 16:27:01 cblume
34 Try to remove compiler warnings on Sun and HP
36 Revision 1.2 2000/05/08 16:17:27 cblume
39 Revision 1.1.4.1 2000/05/08 14:55:03 cblume
42 Revision 1.1 2000/02/28 19:02:56 cblume
47 ///////////////////////////////////////////////////////////////////////////////
49 // Alice segment manager base class //
51 ///////////////////////////////////////////////////////////////////////////////
55 #include <TClonesArray.h>
56 #include <TDirectory.h>
60 #include "AliTRDarrayI.h"
61 #include "AliTRDsegmentID.h"
62 #include "AliTRDsegmentArrayBase.h"
64 ClassImp(AliTRDsegmentArrayBase)
66 //_____________________________________________________________________________
67 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed()
70 // AliTRDsegmentArrayBase default constructor
82 //_____________________________________________________________________________
83 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
86 // Create an array of objects of <classname>. The class must inherit from
87 // AliTRDsegmentID. The second argument sets the number of entries in
100 if (MakeArray(n) == kFALSE) {
101 Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n);
107 //_____________________________________________________________________________
108 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(const AliTRDsegmentArrayBase &a)
111 // AliTRDsegmentArrayBase copy constructor
114 ((AliTRDsegmentArrayBase &) a).Copy(*this);
118 //_____________________________________________________________________________
119 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
122 // AliTRDsegmentArrayBase destructor
130 if (fTree) delete fTree;
131 if (fTreeIndex) delete fTreeIndex;
132 if (fClass) delete fClass;
136 //_____________________________________________________________________________
137 AliTRDsegmentArrayBase &AliTRDsegmentArrayBase
138 ::operator=(const AliTRDsegmentArrayBase &a)
141 // Assignment operator
144 if (this != &a) ((AliTRDsegmentArrayBase &) a).Copy(*this);
149 //_____________________________________________________________________________
150 void AliTRDsegmentArrayBase::Copy(TObject &a)
158 fSegment->Copy(*((AliTRDsegmentArrayBase &) a).fSegment);
159 fTreeIndex->Copy(*((AliTRDsegmentArrayBase &) a).fTreeIndex);
160 fClass->Copy(*((AliTRDsegmentArrayBase &) a).fClass);
162 ((AliTRDsegmentArrayBase &) a).fNSegment = fNSegment;
166 //_____________________________________________________________________________
167 Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname)
170 // Sets the classname of the stored object
190 if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase"
191 ,"ROOT system not initialized");
193 fClass = gROOT->GetClass(classname);
195 Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname);
198 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
199 Error("AliTRDsegmentArrayBase"
200 ,"%s does not inherit from AliTRDsegmentID",classname);
208 //_____________________________________________________________________________
209 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
212 // Create a new object according to the class information
215 if (fClass == 0) return 0;
217 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
218 if (segment == 0) return 0;
224 //_____________________________________________________________________________
225 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
228 // Add a segment to the array
231 if (segment == 0) return kFALSE;
232 if (fSegment == 0) return kFALSE;
233 if (fClass == 0) return kFALSE;
235 if (!(segment->IsA()->InheritsFrom(fClass))) {
236 Error("AliTRDsegmentArrayBase","added class %s is not of proper type",
237 segment->IsA()->GetName());
241 fSegment->AddAt(segment,segment->GetID());
242 fNSegment = fSegment->GetLast() + 1;
248 //_____________________________________________________________________________
249 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
252 // Add a segment to the array
255 if (fSegment == 0) return 0;
256 if (fClass == 0) return 0;
258 AliTRDsegmentID *segment = NewSegment();
259 if (segment == 0) return 0;
261 fSegment->AddAt(segment,index);
262 segment->SetID(index);
263 fNSegment = fSegment->GetLast() + 1;
269 //_____________________________________________________________________________
270 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
273 // Create an array of pointers to the segments
280 if (fTreeIndex) delete fTreeIndex;
282 fSegment = new TObjArray(n);
283 fTreeIndex = new AliTRDarrayI();
286 if ((fSegment) && (fTreeIndex))
293 //_____________________________________________________________________________
294 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
297 // Remove a segment from the active memory
300 if ((*fSegment)[index]){
301 delete (*fSegment)[index]; // because problem with deleting TClonesArray
302 fSegment->RemoveAt(index);
307 //_____________________________________________________________________________
308 void AliTRDsegmentArrayBase::MakeTree()
311 // Create a tree for the segment
314 AliTRDsegmentID *psegment = NewSegment();
316 if (fTree) delete fTree;
317 fTree = new TTree("Segment Tree","Tree with segments");
319 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
325 //_____________________________________________________________________________
326 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
329 // Connect a tree from current directory
338 fTree = (TTree*) gDirectory->Get(treeName);
339 if (fTree == 0) return kFALSE;
340 fBranch = fTree->GetBranch("Segment");
341 if (fBranch == 0) return kFALSE;
343 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
349 //_____________________________________________________________________________
350 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
353 // Load a segment with index <index> into the memory
356 if (fTreeIndex == 0) MakeDictionary(3000);
358 // First try to load dictionary
359 if (fTreeIndex == 0) return 0;
360 if (fBranch == 0) return 0;
361 if (index > fTreeIndex->fN) return 0;
362 AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index];
363 if (s == 0) s = NewSegment();
367 Int_t treeIndex = (*fTreeIndex)[index];
372 fBranch->SetAddress(&s);
373 fTree->GetEvent(treeIndex);
374 (*fSegment)[index] = (TObject*) s;
383 //_____________________________________________________________________________
384 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
387 // Load a segment at position <index> in the tree into the memory
390 if (fBranch == 0) return 0;
391 if (index > fTree->GetEntries()) return 0;
393 AliTRDsegmentID *s = NewSegment();
395 fBranch->SetAddress(&s);
396 fTree->GetEvent(index);
401 Int_t nindex = s->GetID();
402 ClearSegment(nindex);
403 (*fSegment)[nindex] = (TObject *) s;
409 //_____________________________________________________________________________
410 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
413 // Make a segment persistent
416 const AliTRDsegmentID *kSegment = (*this)[index];
417 if (kSegment == 0) return;
418 if (fTree == 0) MakeTree();
419 fBranch->SetAddress(&kSegment);
424 //_____________________________________________________________________________
425 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
428 // Create an index table for the tree
431 if (size < 1) return kFALSE;
432 if (fTreeIndex) delete fTreeIndex;
434 fTreeIndex = new AliTRDarrayI();
435 fTreeIndex->Set(size);
437 AliTRDsegmentID segment;
438 AliTRDsegmentID *psegment = &segment;
440 fBranch->SetAddress(&psegment);
441 TBranch *brindix = fTree->GetBranch("fSegmentID");
443 Int_t nevent = (Int_t) fTree->GetEntries();
444 for (Int_t i = 0; i < nevent; i++){
445 brindix->GetEvent(i);
446 Int_t treeIndex = segment.GetID();
447 if (fTreeIndex->fN < treeIndex)
448 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
449 (*fTreeIndex)[treeIndex] = i + 1;
456 //_____________________________________________________________________________
457 const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i)
460 // Returns a segment with the given index <i>
463 if ((i < 0) || (i >= fNSegment)) return 0;
464 return (AliTRDsegmentID *) fSegment->At(i);
468 //_____________________________________________________________________________
469 const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) const
472 // Returns a segment with the given index <i>
475 if ((i < 0) || (i >= fNSegment)) return 0;
476 return (AliTRDsegmentID *)((*fSegment)[i]);