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