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$ |
8230f242 |
18 | Revision 1.3 2000/06/07 16:27:01 cblume |
19 | Try to remove compiler warnings on Sun and HP |
20 | |
9d0b222b |
21 | Revision 1.2 2000/05/08 16:17:27 cblume |
22 | Merge TRD-develop |
23 | |
6f1e466d |
24 | Revision 1.1.4.1 2000/05/08 14:55:03 cblume |
25 | Bug fixes |
26 | |
27 | Revision 1.1 2000/02/28 19:02:56 cblume |
28 | Add new TRD classes |
29 | |
f7336fa3 |
30 | */ |
31 | |
32 | /////////////////////////////////////////////////////////////////////////////// |
33 | // // |
8230f242 |
34 | // Alice segment manager base class // |
f7336fa3 |
35 | // // |
36 | /////////////////////////////////////////////////////////////////////////////// |
37 | |
38 | #include <TROOT.h> |
39 | #include <TTree.h> |
40 | #include "TClonesArray.h" |
41 | #include "TDirectory.h" |
42 | #include "AliTRDarrayI.h" |
43 | #include "TError.h" |
44 | #include "TClass.h" |
45 | |
46 | #include "AliTRDsegmentID.h" |
47 | #include "AliTRDsegmentArrayBase.h" |
48 | |
f7336fa3 |
49 | ClassImp(AliTRDsegmentArrayBase) |
50 | |
6f1e466d |
51 | //_____________________________________________________________________________ |
8230f242 |
52 | AliTRDsegmentArrayBase::AliTRDsegmentArrayBase():TNamed() |
f7336fa3 |
53 | { |
54 | // |
8230f242 |
55 | // AliTRDsegmentArrayBase default constructor |
f7336fa3 |
56 | // |
6f1e466d |
57 | |
58 | fNSegment = 0; |
59 | fSegment = 0; |
f7336fa3 |
60 | fTreeIndex = 0; |
6f1e466d |
61 | fTree = 0; |
62 | fClass = 0; |
63 | |
f7336fa3 |
64 | } |
65 | |
6f1e466d |
66 | //_____________________________________________________________________________ |
f7336fa3 |
67 | AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n) |
68 | { |
69 | // |
8230f242 |
70 | // Create an array of objects of <classname>. The class must inherit from |
71 | // AliTRDsegmentID. The second argument sets the number of entries in |
f7336fa3 |
72 | // the array. |
8230f242 |
73 | // |
74 | |
75 | fNSegment = 0; |
76 | fSegment = 0; |
f7336fa3 |
77 | fTreeIndex = 0; |
8230f242 |
78 | fTree = 0; |
79 | fClass = 0; |
80 | |
f7336fa3 |
81 | SetClass(classname); |
8230f242 |
82 | |
83 | if (MakeArray(n) == kFALSE) { |
84 | Error("AliTRDsegmentArrayBase","Cannot allocate %d segments in memory",n); |
f7336fa3 |
85 | return; |
8230f242 |
86 | } |
87 | |
88 | } |
89 | |
90 | //_____________________________________________________________________________ |
91 | AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(AliTRDsegmentArrayBase &a) |
92 | { |
93 | // |
94 | // AliTRDsegmentArrayBase copy constructor |
95 | // |
96 | |
97 | a.Copy(*this); |
98 | |
99 | } |
100 | |
101 | //_____________________________________________________________________________ |
102 | AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase() |
103 | { |
104 | // |
105 | // AliTRDsegmentArrayBase destructor |
106 | // |
107 | |
108 | if (fNSegment){ |
109 | fSegment->Delete(); |
110 | delete fSegment; |
111 | } |
112 | |
113 | if (fTree) delete fTree; |
114 | if (fTreeIndex) delete fTreeIndex; |
115 | if (fClass) delete fClass; |
116 | |
117 | } |
118 | |
119 | //_____________________________________________________________________________ |
120 | void AliTRDsegmentArrayBase::Copy(AliTRDsegmentArrayBase &a) |
121 | { |
122 | // |
123 | // Copy function |
124 | // |
125 | |
126 | TNamed::Copy(a); |
127 | |
128 | fSegment->Copy(*a.fSegment); |
129 | fTreeIndex->Copy(*a.fTreeIndex); |
130 | fClass->Copy(*a.fClass); |
131 | |
132 | a.fNSegment = fNSegment; |
133 | |
f7336fa3 |
134 | } |
135 | |
6f1e466d |
136 | //_____________________________________________________________________________ |
8230f242 |
137 | Bool_t AliTRDsegmentArrayBase::SetClass(Text_t *classname) |
f7336fa3 |
138 | { |
139 | // |
8230f242 |
140 | // Sets the classname of the stored object |
141 | // |
142 | |
143 | if (fClass != 0) { |
f7336fa3 |
144 | delete fClass; |
145 | fClass = 0; |
146 | } |
8230f242 |
147 | if (fTree != 0) { |
f7336fa3 |
148 | delete fTree; |
8230f242 |
149 | fTree = 0; |
150 | fBranch = 0; |
f7336fa3 |
151 | delete fTreeIndex; |
152 | fTreeIndex = 0; |
153 | } |
154 | if (fSegment != 0) { |
155 | fSegment->Delete(); |
156 | delete fSegment; |
8230f242 |
157 | fSegment = 0; |
f7336fa3 |
158 | } |
8230f242 |
159 | |
160 | if (!gROOT) ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase" |
161 | ,"ROOT system not initialized"); |
f7336fa3 |
162 | |
163 | fClass = gROOT->GetClass(classname); |
164 | if (!fClass) { |
8230f242 |
165 | Error("AliTRDsegmentArrayBase","%s is not a valid class name",classname); |
166 | return kFALSE; |
f7336fa3 |
167 | } |
168 | if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) { |
8230f242 |
169 | Error("AliTRDsegmentArrayBase" |
170 | ,"%s does not inherit from AliTRDsegmentID",classname); |
171 | return kFALSE; |
172 | } |
173 | |
f7336fa3 |
174 | return kTRUE; |
f7336fa3 |
175 | |
f7336fa3 |
176 | } |
177 | |
6f1e466d |
178 | //_____________________________________________________________________________ |
f7336fa3 |
179 | AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment() |
180 | { |
181 | // |
8230f242 |
182 | // Create a new object according to the class information |
183 | // |
184 | |
185 | if (fClass == 0) return 0; |
186 | |
187 | AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New(); |
f7336fa3 |
188 | if (segment == 0) return 0; |
8230f242 |
189 | |
f7336fa3 |
190 | return segment; |
8230f242 |
191 | |
f7336fa3 |
192 | } |
193 | |
6f1e466d |
194 | //_____________________________________________________________________________ |
f7336fa3 |
195 | Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment) |
196 | { |
197 | // |
8230f242 |
198 | // Add a segment to the array |
f7336fa3 |
199 | // |
8230f242 |
200 | |
201 | if (segment == 0) return kFALSE; |
202 | if (fSegment == 0) return kFALSE; |
203 | if (fClass == 0) return kFALSE; |
204 | |
205 | if (!(segment->IsA()->InheritsFrom(fClass))) { |
206 | Error("AliTRDsegmentArrayBase","added class %s is not of proper type", |
f7336fa3 |
207 | segment->IsA()->GetName()); |
8230f242 |
208 | return kFALSE; |
f7336fa3 |
209 | } |
8230f242 |
210 | |
f7336fa3 |
211 | fSegment->AddAt(segment,segment->GetID()); |
8230f242 |
212 | fNSegment = fSegment->GetLast() + 1; |
213 | |
f7336fa3 |
214 | return kTRUE; |
8230f242 |
215 | |
f7336fa3 |
216 | } |
217 | |
6f1e466d |
218 | //_____________________________________________________________________________ |
f7336fa3 |
219 | AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index) |
220 | { |
221 | // |
8230f242 |
222 | // Add a segment to the array |
f7336fa3 |
223 | // |
8230f242 |
224 | |
225 | if (fSegment == 0) return 0; |
226 | if (fClass == 0) return 0; |
227 | |
228 | AliTRDsegmentID *segment = NewSegment(); |
229 | if (segment == 0) return 0; |
230 | |
f7336fa3 |
231 | fSegment->AddAt(segment,index); |
232 | segment->SetID(index); |
8230f242 |
233 | fNSegment = fSegment->GetLast() + 1; |
234 | |
f7336fa3 |
235 | return segment; |
8230f242 |
236 | |
f7336fa3 |
237 | } |
238 | |
6f1e466d |
239 | //_____________________________________________________________________________ |
f7336fa3 |
240 | Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n) |
241 | { |
242 | // |
8230f242 |
243 | // Create an array of pointers to the segments |
f7336fa3 |
244 | // |
8230f242 |
245 | |
f7336fa3 |
246 | if (fSegment) { |
247 | fSegment->Delete(); |
248 | delete fSegment; |
249 | } |
8230f242 |
250 | if (fTreeIndex) delete fTreeIndex; |
251 | |
252 | fSegment = new TObjArray(n); |
f7336fa3 |
253 | fTreeIndex = new AliTRDarrayI; |
254 | fTreeIndex->Set(n); |
8230f242 |
255 | fNSegment = n; |
256 | if ((fSegment) && (fTreeIndex)) |
257 | return kTRUE; |
258 | else |
259 | return kFALSE; |
260 | |
f7336fa3 |
261 | } |
262 | |
6f1e466d |
263 | //_____________________________________________________________________________ |
f7336fa3 |
264 | void AliTRDsegmentArrayBase::ClearSegment(Int_t index) |
265 | { |
266 | // |
8230f242 |
267 | // Remove a segment from the active memory |
f7336fa3 |
268 | // |
8230f242 |
269 | |
f7336fa3 |
270 | if ((*fSegment)[index]){ |
8230f242 |
271 | delete (*fSegment)[index]; // because problem with deleting TClonesArray |
f7336fa3 |
272 | fSegment->RemoveAt(index); |
273 | } |
8230f242 |
274 | |
f7336fa3 |
275 | } |
276 | |
6f1e466d |
277 | //_____________________________________________________________________________ |
f7336fa3 |
278 | void AliTRDsegmentArrayBase::MakeTree() |
279 | { |
8230f242 |
280 | // |
281 | // Create a tree for the segment |
282 | // |
283 | |
284 | AliTRDsegmentID *psegment = NewSegment(); |
285 | |
f7336fa3 |
286 | if (fTree) delete fTree; |
8230f242 |
287 | fTree = new TTree("Segment Tree","Tree with segments"); |
288 | |
f7336fa3 |
289 | fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1); |
8230f242 |
290 | |
f7336fa3 |
291 | delete psegment; |
8230f242 |
292 | |
f7336fa3 |
293 | } |
294 | |
6f1e466d |
295 | //_____________________________________________________________________________ |
f7336fa3 |
296 | Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName) |
297 | { |
8230f242 |
298 | // |
299 | // Connect a tree from current directory |
300 | // |
301 | |
f7336fa3 |
302 | if (fTree){ |
303 | delete fTree; |
8230f242 |
304 | fTree = 0; |
f7336fa3 |
305 | fBranch = 0; |
306 | } |
8230f242 |
307 | |
308 | fTree = (TTree*) gDirectory->Get(treeName); |
309 | if (fTree == 0) return kFALSE; |
f7336fa3 |
310 | fBranch = fTree->GetBranch("Segment"); |
8230f242 |
311 | if (fBranch == 0) return kFALSE; |
312 | |
f7336fa3 |
313 | MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries()))); |
8230f242 |
314 | |
f7336fa3 |
315 | return kTRUE; |
8230f242 |
316 | |
f7336fa3 |
317 | } |
318 | |
6f1e466d |
319 | //_____________________________________________________________________________ |
f7336fa3 |
320 | AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index) |
321 | { |
322 | // |
8230f242 |
323 | // Load a segment with index <index> into the memory |
f7336fa3 |
324 | // |
8230f242 |
325 | |
326 | if (fTreeIndex == 0) MakeDictionary(3000); |
327 | |
328 | // First try to load dictionary |
329 | if (fTreeIndex == 0) return 0; |
330 | if (fBranch == 0) return 0; |
331 | if (index > fTreeIndex->fN) return 0; |
332 | AliTRDsegmentID *s = (AliTRDsegmentID*) (*fSegment)[index]; |
333 | if (s == 0) s = NewSegment(); |
f7336fa3 |
334 | s->SetID(index); |
f7336fa3 |
335 | |
8230f242 |
336 | if (s != 0) { |
337 | Int_t treeIndex = (*fTreeIndex)[index]; |
338 | if (treeIndex < 1) |
339 | return 0; |
340 | else |
341 | treeIndex--; |
f7336fa3 |
342 | fBranch->SetAddress(&s); |
343 | fTree->GetEvent(treeIndex); |
344 | (*fSegment)[index] = (TObject*) s; |
345 | } |
346 | else |
347 | return 0; |
8230f242 |
348 | |
f7336fa3 |
349 | return s; |
8230f242 |
350 | |
f7336fa3 |
351 | } |
6f1e466d |
352 | |
353 | //_____________________________________________________________________________ |
f7336fa3 |
354 | AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index) |
355 | { |
356 | // |
8230f242 |
357 | // Load a segment at position <index> in the tree into the memory |
f7336fa3 |
358 | // |
8230f242 |
359 | |
360 | if (fBranch == 0) return 0; |
361 | if (index > fTree->GetEntries()) return 0; |
362 | |
363 | AliTRDsegmentID *s = NewSegment(); |
f7336fa3 |
364 | if (s) { |
365 | fBranch->SetAddress(&s); |
366 | fTree->GetEvent(index); |
367 | } |
368 | else |
369 | return 0; |
8230f242 |
370 | |
f7336fa3 |
371 | Int_t nindex = s->GetID(); |
372 | ClearSegment(nindex); |
8230f242 |
373 | (*fSegment)[nindex] = (TObject *) s; |
374 | |
f7336fa3 |
375 | return s; |
8230f242 |
376 | |
f7336fa3 |
377 | } |
378 | |
8230f242 |
379 | //_____________________________________________________________________________ |
f7336fa3 |
380 | void AliTRDsegmentArrayBase::StoreSegment(Int_t index) |
381 | { |
382 | // |
8230f242 |
383 | // Make a segment persistent |
f7336fa3 |
384 | // |
8230f242 |
385 | |
386 | const AliTRDsegmentID *kSegment = (*this)[index]; |
387 | if (kSegment == 0) return; |
388 | if (fTree == 0) MakeTree(); |
389 | fBranch->SetAddress(&kSegment); |
f7336fa3 |
390 | fTree->Fill(); |
8230f242 |
391 | |
f7336fa3 |
392 | } |
393 | |
6f1e466d |
394 | //_____________________________________________________________________________ |
f7336fa3 |
395 | Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size) |
396 | { |
397 | // |
8230f242 |
398 | // Create an index table for the tree |
f7336fa3 |
399 | // |
8230f242 |
400 | |
401 | if (size < 1) return kFALSE; |
f7336fa3 |
402 | if (fTreeIndex) delete fTreeIndex; |
8230f242 |
403 | |
f7336fa3 |
404 | fTreeIndex = new AliTRDarrayI(); |
405 | fTreeIndex->Set(size); |
406 | |
8230f242 |
407 | AliTRDsegmentID segment; |
408 | AliTRDsegmentID *psegment = &segment; |
409 | |
f7336fa3 |
410 | fBranch->SetAddress(&psegment); |
8230f242 |
411 | TBranch *brindix = fTree->GetBranch("fSegmentID"); |
412 | |
413 | Int_t nevent = (Int_t) fTree->GetEntries(); |
414 | for (Int_t i = 0; i < nevent; i++){ |
f7336fa3 |
415 | brindix->GetEvent(i); |
8230f242 |
416 | Int_t treeIndex = segment.GetID(); |
417 | if (fTreeIndex->fN < treeIndex) |
418 | fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1); |
419 | (*fTreeIndex)[treeIndex] = i + 1; |
f7336fa3 |
420 | } |
8230f242 |
421 | |
f7336fa3 |
422 | return kTRUE; |
8230f242 |
423 | |
f7336fa3 |
424 | } |
9d0b222b |
425 | |
426 | //_____________________________________________________________________________ |
8230f242 |
427 | const AliTRDsegmentID * AliTRDsegmentArrayBase::operator[](Int_t i) |
9d0b222b |
428 | { |
429 | // |
8230f242 |
430 | // Returns a segment with the given index <i> |
9d0b222b |
431 | // |
8230f242 |
432 | |
433 | if ((i < 0) || (i >= fNSegment)) return 0; |
434 | return (AliTRDsegmentID *) fSegment->At(i); |
435 | |
9d0b222b |
436 | } |
437 | |
438 | //_____________________________________________________________________________ |
8230f242 |
439 | const AliTRDsegmentID *AliTRDsegmentArrayBase::At(Int_t i) |
9d0b222b |
440 | { |
441 | // |
8230f242 |
442 | // Returns a segment with the given index <i> |
9d0b222b |
443 | // |
8230f242 |
444 | |
445 | if ((i < 0) || (i >= fNSegment)) return 0; |
9d0b222b |
446 | return (AliTRDsegmentID *)((*fSegment)[i]); |
8230f242 |
447 | |
9d0b222b |
448 | } |