Delete only existent objects.
[u/mrichter/AliRoot.git] / TRD / AliTRDsegmentArrayBase.cxx
CommitLineData
f7336fa3 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/*
17$Log$
6f1e466d 18Revision 1.1.4.1 2000/05/08 14:55:03 cblume
19Bug fixes
20
21Revision 1.1 2000/02/28 19:02:56 cblume
22Add new TRD classes
23
f7336fa3 24*/
25
26///////////////////////////////////////////////////////////////////////////////
27// //
28// Alice segment manager object //
29// //
30// AliTRDsegmentIDArray object is array of pointers to object derived from //
31// AliTRDsegmentID object //
32// AliTRDsegmentID - object in comparison with TObject enhalt //
33// additional information fSegmentID //
34// //
35///////////////////////////////////////////////////////////////////////////////
36
37#include <TROOT.h>
38#include <TTree.h>
39#include "TClonesArray.h"
40#include "TDirectory.h"
41#include "AliTRDarrayI.h"
42#include "TError.h"
43#include "TClass.h"
44
45#include "AliTRDsegmentID.h"
46#include "AliTRDsegmentArrayBase.h"
47
f7336fa3 48ClassImp(AliTRDsegmentArrayBase)
49
6f1e466d 50//_____________________________________________________________________________
f7336fa3 51AliTRDsegmentArrayBase::AliTRDsegmentArrayBase()
52{
53 //
54 //
55 //
6f1e466d 56
57 fNSegment = 0;
58 fSegment = 0;
f7336fa3 59 fTreeIndex = 0;
6f1e466d 60 fTree = 0;
61 fClass = 0;
62
f7336fa3 63}
64
6f1e466d 65//_____________________________________________________________________________
f7336fa3 66AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n)
67{
68 //
69 //constructor which
70 //
71 // Create an array of objects of classname. The class must inherit from
72 // AliTRDsegmentID . The second argument adjust number of entries in
73 // the array.
74 fNSegment=0;
75 fSegment =0;
76 fTreeIndex = 0;
77 fTree = 0;
78 fClass = 0;
79 SetClass(classname);
80 if (MakeArray(n)==kFALSE){
81 Error("AliTRDsegmentArrayBase", "can't allocate %d segments in memory",n);
82 return;
83 }
84}
85
6f1e466d 86//_____________________________________________________________________________
f7336fa3 87Bool_t AliTRDsegmentArrayBase:: SetClass(Text_t *classname)
88{
89 //
90 //set class of stored object
91 if ( fClass !=0 ) {
92 delete fClass;
93 fClass = 0;
94 }
95 if (fTree !=0) {
96 delete fTree;
97 fTree = 0;
98 fBranch = 0;
99 delete fTreeIndex;
100 fTreeIndex = 0;
101 }
102 if (fSegment != 0) {
103 fSegment->Delete();
104 delete fSegment;
105 fSegment = 0;
106 }
107 if (!gROOT)
108 ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase", "ROOT system not initialized");
109
110 fClass = gROOT->GetClass(classname);
111 if (!fClass) {
112 Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname);
113 return kFALSE;
114 }
115 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
116 Error("AliTRDsegmentArrayBase", "%s does not inherit from AliTRDsegmentID", classname);
117 return kFALSE;
118 }
119 return kTRUE;
120}
121
6f1e466d 122//_____________________________________________________________________________
f7336fa3 123//Bool_t AliTRDsegmentArrayBase::ClassError( )
124//{
125 //signalize class error
126 // if (!fClass) {
127 // Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname);
128 // return kFALSE;
129 // }
130//// return kFALSE;
131//}
132
6f1e466d 133//_____________________________________________________________________________
f7336fa3 134AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase()
135{
136 if (fNSegment>0){
137 fSegment->Delete();
138 delete fSegment;
139 }
140 if (fTree) delete fTree;
141 if (fTreeIndex) delete fTreeIndex;
142 if (fClass!=0) delete fClass;
143}
144
6f1e466d 145//_____________________________________________________________________________
f7336fa3 146AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment()
147{
148 //
149 //create object according class information
150 if (fClass==0) return 0;
151 AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New();
152 if (segment == 0) return 0;
153 return segment;
154}
155
6f1e466d 156//_____________________________________________________________________________
f7336fa3 157Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment)
158{
159 //
160 // add segment to array
161 //
162 if (segment==0) return kFALSE;
163 if (fSegment==0) return kFALSE;
164 if (fClass==0) return kFALSE;
165 if (!(segment->IsA()->InheritsFrom(fClass))){
166 Error("AliTRDsegmentArrayBase", "added class %s is not of proper type ",
167 segment->IsA()->GetName());
168 return kFALSE;
169 }
170 fSegment->AddAt(segment,segment->GetID());
171 fNSegment = fSegment->GetLast()+1;
172 return kTRUE;
173}
174
6f1e466d 175//_____________________________________________________________________________
f7336fa3 176AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index)
177{
178 //
179 // add segment to array
180 //
181 if (fSegment==0) return 0;
182 if (fClass==0) return 0;
183 // AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New();
184 AliTRDsegmentID * segment = NewSegment();
185 if (segment == 0) return 0;
186 fSegment->AddAt(segment,index);
187 segment->SetID(index);
188 fNSegment = fSegment->GetLast()+1;
189 return segment;
190}
191
6f1e466d 192//_____________________________________________________________________________
f7336fa3 193Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n)
194{
195 //
196 //make array of pointers to Segments
197 //
198 if (fSegment) {
199 fSegment->Delete();
200 delete fSegment;
201 }
202 if (fTreeIndex) delete fTreeIndex;
203 fSegment = new TObjArray(n);
204 fTreeIndex = new AliTRDarrayI;
205 fTreeIndex->Set(n);
206 fNSegment=n;
207 if ( (fSegment) && (fTreeIndex)) return kTRUE;
208 else return kFALSE;
209}
210
6f1e466d 211//_____________________________________________________________________________
f7336fa3 212void AliTRDsegmentArrayBase::ClearSegment(Int_t index)
213{
214 //
215 //remove segment from active memory
216 //
217 if ((*fSegment)[index]){
218 // (*fSegment)[index]->Delete(); //not working for TClonesArray
219 delete (*fSegment)[index]; //because problem with deleting TClonesArray
220 fSegment->RemoveAt(index);
221 }
222}
223
6f1e466d 224//_____________________________________________________________________________
f7336fa3 225void AliTRDsegmentArrayBase::MakeTree()
226{
227 // AliTRDsegmentID segment;
228 AliTRDsegmentID * psegment = NewSegment();
229 if (fTree) delete fTree;
230 fTree = new TTree("Segment Tree","Tree with segments");
231 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
232 delete psegment;
233}
234
6f1e466d 235//_____________________________________________________________________________
f7336fa3 236Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName)
237{
238 //connect tree from current directory
239 if (fTree){
240 delete fTree;
241 fTree = 0;
242 fBranch = 0;
243 }
244 fTree =(TTree*)gDirectory->Get(treeName);
245 if (fTree == 0) return kFALSE;
246 fBranch = fTree->GetBranch("Segment");
247 if (fBranch==0) return kFALSE;
248 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
249 return kTRUE;
250}
251
6f1e466d 252//_____________________________________________________________________________
f7336fa3 253AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index)
254{
255 //
256 //load segment with index to the memory
257 //
258 //
259 if (fTreeIndex ==0 ) MakeDictionary(3000);
260 //firstly try to load dictionary
261 if (fTreeIndex ==0 ) return 0;
262 if (fBranch==0) return 0;
263 if (index>fTreeIndex->fN) return 0;
264 AliTRDsegmentID *s = (AliTRDsegmentID*)(*fSegment)[index];
265 if (s==0) s= NewSegment();
266 s->SetID(index);
267 // new AliTRDsegmentID(index);
268
269 if (s!=0) {
270 Int_t treeIndex =(*fTreeIndex)[index];
271 if (treeIndex<1) return 0;
272 else treeIndex--; //I don't like it Int table I have index shifted by 1
273 fBranch->SetAddress(&s);
274 fTree->GetEvent(treeIndex);
275 (*fSegment)[index] = (TObject*) s;
276 }
277 else
278 return 0;
279 return s;
280 // AbstractMethod("LoadSegment");
281}
6f1e466d 282
283//_____________________________________________________________________________
f7336fa3 284AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index)
285{
286 //
287 //load segment at position inex in tree to the memory
288 //
289 //
290 if (fBranch==0) return 0;
291 if (index>fTree->GetEntries()) return 0;
292 AliTRDsegmentID * s = NewSegment();
293
294 if (s) {
295 fBranch->SetAddress(&s);
296 fTree->GetEvent(index);
297 }
298 else
299 return 0;
300 Int_t nindex = s->GetID();
301 ClearSegment(nindex);
302 (*fSegment)[nindex] = (TObject*) s;
303 return s;
304 // AbstractMethod("LoadSegment");
305}
306
307void AliTRDsegmentArrayBase::StoreSegment(Int_t index)
308{
309 //
310 //make segment persistent
311 //
312 const AliTRDsegmentID * segment = (*this)[index];
313 if (segment == 0 ) return;
314 if (fTree==0) MakeTree();
315 fBranch->SetAddress(&segment);
316 fTree->Fill();
317}
318
6f1e466d 319//_____________________________________________________________________________
f7336fa3 320Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size)
321{
322 //
323 //create index table for tree
324 //
325 if (size<1) return kFALSE;
326 if (fTreeIndex) delete fTreeIndex;
327 fTreeIndex = new AliTRDarrayI();
328 fTreeIndex->Set(size);
329
330 AliTRDsegmentID segment;
331 AliTRDsegmentID * psegment = &segment;
332 fBranch->SetAddress(&psegment);
333 TBranch * brindix = fTree->GetBranch("fSegmentID");
334 Int_t nevent = (Int_t)fTree->GetEntries();
335 for (Int_t i = 0; i<nevent; i++){
336 brindix->GetEvent(i);
337 Int_t treeIndex=segment.GetID();
338 if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1);
339 // Int_t index = segment.GetID();
340 (*fTreeIndex)[treeIndex]=i+1; // MI 19.5. I'm sorry -index 0 couldn't be use in AliTRDarrayI
341 }
342 return kTRUE;
343}