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