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