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