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 **************************************************************************/
20 ///////////////////////////////////////////////////////////////////////////////
22 // Alice segment manager object //
24 // AliTRDsegmentIDArray object is array of pointers to object derived from //
25 // AliTRDsegmentID object //
26 // AliTRDsegmentID - object in comparison with TObject enhalt //
27 // additional information fSegmentID //
29 ///////////////////////////////////////////////////////////////////////////////
33 #include "TClonesArray.h"
34 #include "TDirectory.h"
35 #include "AliTRDarrayI.h"
39 #include "AliTRDsegmentID.h"
40 #include "AliTRDsegmentArrayBase.h"
42 //_____________________________________________________________________________
43 ClassImp(AliTRDsegmentArrayBase)
45 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase()
57 AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
62 // Create an array of objects of classname. The class must inherit from
63 // AliTRDsegmentID . The second argument adjust number of entries in
71 if (MakeArray(n)==kFALSE){
72 Error("AliTRDsegmentArrayBase", "can't allocate %d segments in memory",n);
77 Bool_t AliTRDsegmentArrayBase:: SetClass(Text_t *classname)
80 //set class of stored object
98 ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase", "ROOT system not initialized");
100 fClass = gROOT->GetClass(classname);
102 Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname);
105 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
106 Error("AliTRDsegmentArrayBase", "%s does not inherit from AliTRDsegmentID", classname);
112 //Bool_t AliTRDsegmentArrayBase::ClassError( )
114 //signalize class error
116 // Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname);
122 AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
128 if (fTree) delete fTree;
129 if (fTreeIndex) delete fTreeIndex;
130 if (fClass!=0) delete fClass;
133 AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
136 //create object according class information
137 if (fClass==0) return 0;
138 AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New();
139 if (segment == 0) return 0;
144 Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
147 // add segment to array
149 if (segment==0) return kFALSE;
150 if (fSegment==0) return kFALSE;
151 if (fClass==0) return kFALSE;
152 if (!(segment->IsA()->InheritsFrom(fClass))){
153 Error("AliTRDsegmentArrayBase", "added class %s is not of proper type ",
154 segment->IsA()->GetName());
157 fSegment->AddAt(segment,segment->GetID());
158 fNSegment = fSegment->GetLast()+1;
162 AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
165 // add segment to array
167 if (fSegment==0) return 0;
168 if (fClass==0) return 0;
169 // AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New();
170 AliTRDsegmentID * segment = NewSegment();
171 if (segment == 0) return 0;
172 fSegment->AddAt(segment,index);
173 segment->SetID(index);
174 fNSegment = fSegment->GetLast()+1;
180 Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
183 //make array of pointers to Segments
189 if (fTreeIndex) delete fTreeIndex;
190 fSegment = new TObjArray(n);
191 fTreeIndex = new AliTRDarrayI;
194 if ( (fSegment) && (fTreeIndex)) return kTRUE;
199 void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
202 //remove segment from active memory
204 if ((*fSegment)[index]){
205 // (*fSegment)[index]->Delete(); //not working for TClonesArray
206 delete (*fSegment)[index]; //because problem with deleting TClonesArray
207 fSegment->RemoveAt(index);
212 void AliTRDsegmentArrayBase::MakeTree()
214 // AliTRDsegmentID segment;
215 AliTRDsegmentID * psegment = NewSegment();
216 if (fTree) delete fTree;
217 fTree = new TTree("Segment Tree","Tree with segments");
218 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
222 Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
224 //connect tree from current directory
230 fTree =(TTree*)gDirectory->Get(treeName);
231 if (fTree == 0) return kFALSE;
232 fBranch = fTree->GetBranch("Segment");
233 if (fBranch==0) return kFALSE;
234 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
238 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
241 //load segment with index to the memory
244 if (fTreeIndex ==0 ) MakeDictionary(3000);
245 //firstly try to load dictionary
246 if (fTreeIndex ==0 ) return 0;
247 if (fBranch==0) return 0;
248 if (index>fTreeIndex->fN) return 0;
249 AliTRDsegmentID *s = (AliTRDsegmentID*)(*fSegment)[index];
250 if (s==0) s= NewSegment();
252 // new AliTRDsegmentID(index);
255 Int_t treeIndex =(*fTreeIndex)[index];
256 if (treeIndex<1) return 0;
257 else treeIndex--; //I don't like it Int table I have index shifted by 1
258 fBranch->SetAddress(&s);
259 fTree->GetEvent(treeIndex);
260 (*fSegment)[index] = (TObject*) s;
265 // AbstractMethod("LoadSegment");
267 AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
270 //load segment at position inex in tree to the memory
273 if (fBranch==0) return 0;
274 if (index>fTree->GetEntries()) return 0;
275 AliTRDsegmentID * s = NewSegment();
278 fBranch->SetAddress(&s);
279 fTree->GetEvent(index);
283 Int_t nindex = s->GetID();
284 ClearSegment(nindex);
285 (*fSegment)[nindex] = (TObject*) s;
287 // AbstractMethod("LoadSegment");
290 void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
293 //make segment persistent
295 const AliTRDsegmentID * segment = (*this)[index];
296 if (segment == 0 ) return;
297 if (fTree==0) MakeTree();
298 fBranch->SetAddress(&segment);
302 Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
305 //create index table for tree
307 if (size<1) return kFALSE;
308 if (fTreeIndex) delete fTreeIndex;
309 fTreeIndex = new AliTRDarrayI();
310 fTreeIndex->Set(size);
312 AliTRDsegmentID segment;
313 AliTRDsegmentID * psegment = &segment;
314 fBranch->SetAddress(&psegment);
315 TBranch * brindix = fTree->GetBranch("fSegmentID");
316 Int_t nevent = (Int_t)fTree->GetEntries();
317 for (Int_t i = 0; i<nevent; i++){
318 brindix->GetEvent(i);
319 Int_t treeIndex=segment.GetID();
320 if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1);
321 // Int_t index = segment.GetID();
322 (*fTreeIndex)[treeIndex]=i+1; // MI 19.5. I'm sorry -index 0 couldn't be use in AliTRDarrayI