Make some calculations optional for HLT
[u/mrichter/AliRoot.git] / TRD / AliTRDsegmentArray.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
88cb7938 16/* $Id$ */
f7336fa3 17
04eeac11 18////////////////////////////////////////////////////////////////////////////
19// //
20// Alice segment manager class //
21// //
22////////////////////////////////////////////////////////////////////////////
f7336fa3 23
b09247a2 24#include <cstdlib>
625f5260 25#include <TROOT.h>
94de3818 26#include <TTree.h>
625f5260 27#include <TClonesArray.h>
28#include <TDirectory.h>
29#include <TError.h>
30#include <TClass.h>
94de3818 31
c965eab1 32#include "AliLog.h"
793ff80c 33
f7336fa3 34#include "AliTRDgeometry.h"
35#include "AliTRDsegmentArray.h"
625f5260 36#include "AliTRDsegmentID.h"
6244debe 37#include "AliTRDdataArray.h"
625f5260 38#include "AliTRDarrayI.h"
f7336fa3 39
40ClassImp(AliTRDsegmentArray)
41
42//_____________________________________________________________________________
04eeac11 43AliTRDsegmentArray::AliTRDsegmentArray()
625f5260 44 :TNamed()
45 ,fSegment(0)
46 ,fTreeIndex(0)
47 ,fNSegment(0)
48 ,fTree(0)
49 ,fBranch(0)
50 ,fClass(0)
f7336fa3 51{
52 //
c10bf383 53 // Default constructor
f7336fa3 54 //
55
56}
57
58//_____________________________________________________________________________
8e8eae84 59AliTRDsegmentArray::AliTRDsegmentArray(const char *classname, Int_t n)
625f5260 60 :TNamed()
61 ,fSegment(0)
62 ,fTreeIndex(0)
63 ,fNSegment(0)
64 ,fTree(0)
65 ,fBranch(0)
66 ,fClass(0)
f7336fa3 67{
68 //
625f5260 69 // Create an array of objects of <classname>. The class must inherit from
70 // AliTRDsegmentID. The second argument sets the number of entries in
71 // the array.
f7336fa3 72 //
73
8230f242 74 AliTRDdataArray *dataArray;
f7336fa3 75
625f5260 76 SetClass(classname);
77
78 if (MakeArray(n) == kFALSE) {
79 AliError(Form("Cannot allocate %d segments in memory",n));
80 return;
81 }
82
f7336fa3 83 for (Int_t i = 0; i < n; i++) {
8230f242 84 dataArray = (AliTRDdataArray *) AddSegment(i);
f7336fa3 85 }
86
87}
88
89//_____________________________________________________________________________
8230f242 90AliTRDsegmentArray::AliTRDsegmentArray(AliTRDsegmentArray &a)
625f5260 91 :TNamed(a)
92 ,fSegment(a.fSegment)
93 ,fTreeIndex(a.fTreeIndex)
94 ,fNSegment(a.fNSegment)
95 ,fTree(a.fTree)
96 ,fBranch(a.fBranch)
97 ,fClass(a.fClass)
8230f242 98{
99 //
100 // AliTRDsegmentArray copy constructor
101 //
102
103 a.Copy(*this);
104
105}
106
107//_____________________________________________________________________________
108AliTRDsegmentArray::~AliTRDsegmentArray()
109{
110 //
111 // AliTRDsegmentArray destructor
112 //
17eee78a 113
c10bf383 114 // Needed ????
115 //Delete();
17eee78a 116
625f5260 117 if (fNSegment) {
118 fSegment->Delete();
119 delete fSegment;
120 }
121
122 if (fTreeIndex) {
123 delete fTreeIndex;
124 }
125
126}
127
128//_____________________________________________________________________________
129AliTRDsegmentArray &AliTRDsegmentArray::operator=(const AliTRDsegmentArray &a)
130{
131 //
132 // Assignment operator
133 //
134
135 if (this != &a) ((AliTRDsegmentArray &) a).Copy(*this);
136 return *this;
137
8230f242 138}
139
140//_____________________________________________________________________________
e0d47c25 141void AliTRDsegmentArray::Copy(TObject &a) const
8230f242 142{
143 //
144 // Copy function
145 //
146
625f5260 147 TNamed::Copy(a);
148
149 fSegment->Copy(*((AliTRDsegmentArray &) a).fSegment);
150 fTreeIndex->Copy(*((AliTRDsegmentArray &) a).fTreeIndex);
151 fClass->Copy(*((AliTRDsegmentArray &) a).fClass);
152
153 ((AliTRDsegmentArray &) a).fNSegment = fNSegment;
154
155}
156
157//_____________________________________________________________________________
158Bool_t AliTRDsegmentArray::SetClass(const Char_t *classname)
159{
160 //
161 // Sets the classname of the stored object
162 //
163
164 if (fTree != 0) {
165 delete fTree;
166 fTree = 0;
167 fBranch = 0;
168 delete fTreeIndex;
169 fTreeIndex = 0;
170 }
171 if (fSegment != 0) {
172 fSegment->Delete();
173 delete fSegment;
174 fSegment = 0;
175 }
176
177 if (!gROOT) {
178 AliFatal("ROOT system not initialized");
179 exit(1);
180 }
181
182 fClass = gROOT->GetClass(classname);
183 if (!fClass) {
184 AliError(Form("%s is not a valid class name",classname));
185 return kFALSE;
186 }
187 if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) {
188 AliError(Form("%s does not inherit from AliTRDsegmentID",classname));
189 return kFALSE;
190 }
191
192 return kTRUE;
193
194}
195
196//_____________________________________________________________________________
197AliTRDsegmentID *AliTRDsegmentArray::NewSegment()
198{
199 //
200 // Create a new object according to the class information
201 //
202
203 if (fClass == 0) {
204 return 0;
205 }
206
207 AliTRDsegmentID *segment = (AliTRDsegmentID *) fClass->New();
208
209 if (segment == 0) {
210 return 0;
211 }
212 else {
213 return segment;
214 }
215
216}
217
218//_____________________________________________________________________________
219Bool_t AliTRDsegmentArray::AddSegment(AliTRDsegmentID *segment)
220{
221 //
222 // Add a segment to the array
223 //
224
225 if (segment == 0) {
226 return kFALSE;
227 }
228 if (fSegment == 0) {
229 return kFALSE;
230 }
231 if (fClass == 0) {
232 return kFALSE;
233 }
234
235 if (!(segment->IsA()->InheritsFrom(fClass))) {
236 AliError(Form("added class %s is not of proper type"
237 ,segment->IsA()->GetName()));
238 return kFALSE;
239 }
240
241 fSegment->AddAt(segment,segment->GetID());
242 fNSegment = fSegment->GetLast() + 1;
243
244 return kTRUE;
245
246}
247
248//_____________________________________________________________________________
249AliTRDsegmentID *AliTRDsegmentArray::AddSegment(Int_t index)
250{
251 //
252 // Add a segment to the array
253 //
254
255 if (fSegment == 0) {
256 return 0;
257 }
258 if (fClass == 0) {
259 return 0;
260 }
261
262 AliTRDsegmentID *segment = NewSegment();
263 if (segment == 0) {
264 return 0;
265 }
266
267 fSegment->AddAt(segment,index);
268 segment->SetID(index);
269 fNSegment = fSegment->GetLast() + 1;
270
271 return segment;
272
273}
274
275//_____________________________________________________________________________
276Bool_t AliTRDsegmentArray::MakeArray(Int_t n)
277{
278 //
279 // Create an array of pointers to the segments
280 //
281
282 if (fSegment) {
283 fSegment->Delete();
284 delete fSegment;
285 }
286 if (fTreeIndex) delete fTreeIndex;
287
288 fSegment = new TObjArray(n);
289 fTreeIndex = new AliTRDarrayI();
290 fTreeIndex->Set(n);
291 fNSegment = n;
292 if ((fSegment) && (fTreeIndex)) {
293 return kTRUE;
294 }
295 else {
296 return kFALSE;
297 }
298
299}
300
301//_____________________________________________________________________________
302void AliTRDsegmentArray::ClearSegment(Int_t index)
303{
304 //
305 // Remove a segment from the active memory
306 //
307
308 if (fSegment->At(index)) {
c10bf383 309 fClass->Destructor(fSegment->RemoveAt(index));
625f5260 310 }
311
312}
313
314//_____________________________________________________________________________
315void AliTRDsegmentArray::MakeTree(char *file)
316{
317 //
318 // Create a tree for the segment
319 //
320
321 AliTRDsegmentID *psegment = NewSegment();
322
323 if (fTree) {
324 delete fTree;
325 }
326
327 fTree = new TTree("Segment Tree","Tree with segments");
328 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
329
330 if (file) {
331 fBranch->SetFile(file);
332 }
333
334 delete psegment;
335
336}
337
338//_____________________________________________________________________________
339Bool_t AliTRDsegmentArray::ConnectTree(const char *treeName)
340{
341 //
342 // Connect a tree from current directory
343 //
344
345 if (fTree) {
346 delete fTree;
347 fTree = 0;
348 fBranch = 0;
349 }
350
351 fTree = (TTree *) gDirectory->Get(treeName);
352 if (fTree == 0) {
353 return kFALSE;
354 }
355 fBranch = fTree->GetBranch("Segment");
356 if (fBranch == 0) {
357 return kFALSE;
358 }
359
360 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
361
362 return kTRUE;
363
364}
365
366//_____________________________________________________________________________
367AliTRDsegmentID *AliTRDsegmentArray::LoadSegment(Int_t index)
368{
369 //
370 // Load a segment with index <index> into the memory
371 //
372
373 if (fTreeIndex == 0) {
374 MakeDictionary(3000);
375 }
376
377 // First try to load dictionary
378 if (fTreeIndex == 0) {
379 return 0;
380 }
381 if (fBranch == 0) {
382 return 0;
383 }
384 if (index > fTreeIndex->fN) {
385 return 0;
386 }
387
388 AliTRDsegmentID *s = (AliTRDsegmentID *) fSegment->At(index);
389 if (s == 0) {
390 s = NewSegment();
391 }
392 s->SetID(index);
393
394 if (s != 0) {
395 Int_t treeIndex = (*fTreeIndex)[index];
396 if (treeIndex < 1) {
397 return 0;
398 }
399 else {
400 treeIndex--;
401 }
402 fBranch->SetAddress(&s);
403 fTree->GetEvent(treeIndex);
404 fSegment->AddAt((TObject*) s, index);
405 }
406 else {
407 return 0;
408 }
409
410 return s;
411
412}
413
414//_____________________________________________________________________________
415AliTRDsegmentID *AliTRDsegmentArray::LoadEntry(Int_t index)
416{
417 //
418 // Load a segment at position <index> in the tree into the memory
419 //
420
421 if (fBranch == 0) {
422 return 0;
423 }
424 if (index > fTree->GetEntries()) {
425 return 0;
426 }
427
428 AliTRDsegmentID *s = NewSegment();
429 if (s) {
430 fBranch->SetAddress(&s);
431 fTree->GetEvent(index);
432 }
433 else {
434 return 0;
435 }
436
437 Int_t nindex = s->GetID();
438 ClearSegment(nindex);
439 fSegment->AddAt((TObject *) s, nindex);
440
441 return s;
442
443}
444
445//_____________________________________________________________________________
446void AliTRDsegmentArray::StoreSegment(Int_t index)
447{
448 //
449 // Make a segment persistent
450 //
451
452 const AliTRDsegmentID *kSegment = (*this)[index];
453 if (kSegment == 0) {
454 return;
455 }
456 if (fTree == 0) {
457 MakeTree();
458 }
459 fBranch->SetAddress(&kSegment);
460 fTree->Fill();
461
462}
463
464//_____________________________________________________________________________
465Bool_t AliTRDsegmentArray::MakeDictionary(Int_t size)
466{
467 //
468 // Create an index table for the tree
469 //
470
471 if (size < 1) {
472 return kFALSE;
473 }
474 if (fTreeIndex) {
475 delete fTreeIndex;
476 }
477
478 fTreeIndex = new AliTRDarrayI();
479 fTreeIndex->Set(size);
480
481 AliTRDsegmentID segment;
482 AliTRDsegmentID *psegment = &segment;
483
484 fBranch->SetAddress(&psegment);
485 TBranch *brindix = fTree->GetBranch("fSegmentID");
486
487 Int_t nevent = (Int_t) fTree->GetEntries();
488 for (Int_t i = 0; i < nevent; i++) {
489 brindix->GetEvent(i);
490 Int_t treeIndex = segment.GetID();
491 if (fTreeIndex->fN < treeIndex) {
492 fTreeIndex->Expand(Int_t (Float_t(treeIndex) * 1.5) + 1);
493 }
494 (*fTreeIndex)[treeIndex] = i + 1;
495 }
496
497 return kTRUE;
498
499}
500
501//_____________________________________________________________________________
502const AliTRDsegmentID * AliTRDsegmentArray::operator[](Int_t i) const
503{
504 //
505 // Returns a segment with the given index <i>
506 //
507
508 if ((i < 0) ||
509 (i >= fNSegment)) {
510 return 0;
511 }
512
513 return (AliTRDsegmentID *) fSegment->At(i);
514
515}
516
517//_____________________________________________________________________________
518const AliTRDsegmentID *AliTRDsegmentArray::At(Int_t i) const
519{
520 //
521 // Returns a segment with the given index <i>
522 //
523
524 if ((i < 0) ||
525 (i >= fNSegment)) {
526 return 0;
527 }
528
529 return (AliTRDsegmentID *) fSegment->At(i);
8230f242 530
531}
532
533//_____________________________________________________________________________
f7336fa3 534void AliTRDsegmentArray::Delete()
535{
536 //
537 // Deletes all detector segments from the array
538 //
539
540 for (Int_t iDet = 0; iDet < fNSegment; iDet++) {
541 ClearSegment(iDet);
542 }
543
544}
545
546//_____________________________________________________________________________
abaf1f1d 547Bool_t AliTRDsegmentArray::LoadArray(const Char_t *branchname, TTree *tree)
f7336fa3 548{
549 //
550 // Loads all segments of the array from the branch <branchname> of
abaf1f1d 551 // the digits tree <tree>
f7336fa3 552 //
553
abaf1f1d 554 fTree = tree;
555
abaf1f1d 556 if (!fTree) {
c965eab1 557 AliError("Digits tree is not defined\n");
558 return kFALSE;
abaf1f1d 559 }
f7336fa3 560
561 // Get the branch
562 fBranch = fTree->GetBranch(branchname);
04eeac11 563 if (!fBranch) {
c965eab1 564 AliError(Form("Branch %s is not defined\n",branchname));
04eeac11 565 return kFALSE;
566 }
f7336fa3 567
568 // Loop through all segments and read them from the tree
569 Bool_t status = kTRUE;
570 for (Int_t iSegment = 0; iSegment < fNSegment; iSegment++) {
8230f242 571 AliTRDdataArray *dataArray = (AliTRDdataArray *) fSegment->At(iSegment);
572 if (!dataArray) {
f7336fa3 573 status = kFALSE;
574 break;
575 }
8230f242 576 fBranch->SetAddress(&dataArray);
f7336fa3 577 fBranch->GetEntry(iSegment);
578 }
579
580 return status;
581
582}
583
584//_____________________________________________________________________________
abaf1f1d 585Bool_t AliTRDsegmentArray::StoreArray(const Char_t *branchname, TTree *tree)
f7336fa3 586{
587 //
588 // Stores all segments of the array in the branch <branchname> of
abaf1f1d 589 // the digits tree <tree>
f7336fa3 590 //
591
abaf1f1d 592 fTree = tree;
593
abaf1f1d 594 if (!fTree) {
c965eab1 595 AliError("Digits tree is not defined\n");
596 return kFALSE;
abaf1f1d 597 }
f7336fa3 598
599 // Get the branch
600 fBranch = fTree->GetBranch(branchname);
04eeac11 601 if (!fBranch) {
c965eab1 602 AliError(Form("Branch %s is not defined\n",branchname));
04eeac11 603 return kFALSE;
604 }
f7336fa3 605
606 // Loop through all segments and fill them into the tree
607 Bool_t status = kTRUE;
608 for (Int_t iSegment = 0; iSegment < fNSegment; iSegment++) {
8230f242 609 const AliTRDdataArray *kDataArray =
625f5260 610 (AliTRDdataArray *) AliTRDsegmentArray::At(iSegment);
8230f242 611 if (!kDataArray) {
f7336fa3 612 status = kFALSE;
613 break;
614 }
8230f242 615 fBranch->SetAddress(&kDataArray);
f7336fa3 616 fBranch->Fill();
617 }
618
619 return status;
620
621}
622
623//_____________________________________________________________________________
793ff80c 624AliTRDdataArray *AliTRDsegmentArray::GetDataArray(Int_t det) const
f7336fa3 625{
626 //
627 // Returns the data array for a given detector
628 //
629
625f5260 630 return ((AliTRDdataArray *) AliTRDsegmentArray::At(det));
f7336fa3 631
632}
633
634//_____________________________________________________________________________
793ff80c 635AliTRDdataArray *AliTRDsegmentArray::GetDataArray(Int_t pla
04eeac11 636 , Int_t cha
637 , Int_t sec) const
f7336fa3 638{
639 //
640 // Returns the data array for a given detector
641 //
642
c965eab1 643 Int_t det = AliTRDgeometry::GetDetector(pla,cha,sec);
644 return GetDataArray(det);
f7336fa3 645
646}