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