]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TPC/AliSegmentArray.cxx
New class replacing AliCluster
[u/mrichter/AliRoot.git] / TPC / AliSegmentArray.cxx
CommitLineData
cc80f89e 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$
73042f01 18Revision 1.2.4.1 2000/06/25 08:38:41 kowal2
19Splitted from AliTPCtracking
20
21Revision 1.2 2000/04/17 09:37:33 kowal2
22removed obsolete AliTPCDigitsDisplay.C
23
cc80f89e 24Revision 1.1.4.2 2000/04/10 11:39:36 kowal2
25
26New data structure handling
27
28*/
29
30///////////////////////////////////////////////////////////////////////////////
31// //
32// Alice segment manager object //
33//
34// AliSegmentIDArray object is array of pointers to object derived from
35// AliSegmentID object //
36// AliSegmentID - object in comparison with TObject enhalt
37// additional information fSegmentID
38//
39// //
40// //
41///////////////////////////////////////////////////////////////////////////////
42
43#include <TROOT.h>
44#include <TTree.h>
45#include "TClonesArray.h"
46#include "TDirectory.h"
47#include "AliArrayI.h"
48#include "TError.h"
49#include "TClass.h"
50
51#include "AliSegmentID.h"
52#include "AliSegmentArray.h"
53#include "TObjString.h"
54
55
56//_____________________________________________________________________________
57ClassImp(AliSegmentArray)
58
59AliSegmentArray::AliSegmentArray()
60{
61 //
62 //
63 //
64 fNSegment=0;
65 fSegment =0;
66 fTreeIndex = 0;
67 fTree = 0;
68 fClass = 0;
69}
70
71AliSegmentArray::AliSegmentArray(Text_t *classname, Int_t n)
72{
73 //
74 //constructor which
75 //
76 // Create an array of objects of classname. The class must inherit from
77 // AliSegmentID . The second argument adjust number of entries in
78 // the array.
79 fNSegment=0;
80 fSegment =0;
81 fTreeIndex = 0;
82 fTree = 0;
83 fClass = 0;
84 SetName("SegmentArray");
85 SetTitle("SegmentArray");
86
87 SetClass(classname);
88 if (MakeArray(n)==kFALSE){
89 Error("AliSegmentArray", "can't allocate %d segments in memory",n);
90 return;
91 }
92}
93
73042f01 94AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
95{
96 //
97 //copy constructor
98 // to be later implemented
99}
100
101AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & segment)
102{
103 //assignment operator
104 //to be later implemented
105 return (*this);
106}
107
108AliSegmentArray::~AliSegmentArray()
109{
110 //
111 // default destructor
112 if (fNSegment>0){
113 fSegment->Delete();
114 delete fSegment;
115 }
116 if (fTree) delete fTree;
117 if (fTreeIndex) delete fTreeIndex;
118 if (fClass!=0) delete fClass;
119}
120
121
cc80f89e 122Bool_t AliSegmentArray::SetClass(Text_t *classname)
123{
124 //
125 //set class of stored object
126 if ( fClass !=0 ) {
127 delete fClass;
128 fClass = 0;
129 }
130 if (fTree !=0) {
131 delete fTree;
132 fTree = 0;
133 fBranch = 0;
134 delete fTreeIndex;
135 fTreeIndex = 0;
136 }
137 if (fSegment != 0) {
138 fSegment->Delete();
139 delete fSegment;
140 fSegment = 0;
141 }
142 if (!gROOT)
143 ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
144
145 fClass = gROOT->GetClass(classname);
146 if (!fClass) {
147 Error("AliSegmentArray", "%s is not a valid class name", classname);
148 return kFALSE;
149 }
150 if (!fClass->InheritsFrom(AliSegmentID::Class())) {
151 Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
152 return kFALSE;
153 }
154 return kTRUE;
155}
156
cc80f89e 157
158AliSegmentID * AliSegmentArray::NewSegment()
159{
160 //
161 //create object according class information
162 if (fClass==0) return 0;
163 AliSegmentID * segment = (AliSegmentID * )fClass->New();
164 if (segment == 0) return 0;
165 return segment;
166}
167
168
169Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
170{
171 //
172 // add segment to array
173 //
174 if (segment==0) return kFALSE;
175 if (fSegment==0) return kFALSE;
176 if (fClass==0) return kFALSE;
177 if (!(segment->IsA()->InheritsFrom(fClass))){
178 Error("AliSegmentArray", "added class %s is not of proper type ",
179 segment->IsA()->GetName());
180 return kFALSE;
181 }
182 fSegment->AddAt(segment,segment->GetID());
183 fNSegment = fSegment->GetLast()+1;
184 return kTRUE;
185}
186
187AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
188{
189 //
190 // add segment to array
191 //
192 if (fSegment==0) return 0;
193 if (fClass==0) return 0;
194 AliSegmentID * segment = NewSegment();
195 if (segment == 0) return 0;
196 fSegment->AddAt(segment,index);
197 segment->SetID(index);
198 fNSegment = fSegment->GetLast()+1;
199 return segment;
200}
201
202
203void AliSegmentArray::ClearSegment(Int_t index)
204{
205 //
206 //remove segment from active memory
207 //
208 if ((*fSegment)[index]){
209 // (*fSegment)[index]->Delete(); //not working for TClonesArray
210 delete (*fSegment)[index]; //because problem with deleting TClonesArray
211 fSegment->RemoveAt(index);
212 }
213}
214
215
216Bool_t AliSegmentArray::MakeArray(Int_t n)
217{
218 //
219 //make array of pointers to Segments
220 //
221 if (fSegment) {
222 fSegment->Delete();
223 delete fSegment;
224 }
225 fSegment = new TObjArray(n);
226 fNSegment=n;
227 if (fSegment) return kTRUE;
228 else return kFALSE;
229}
230
231
232void AliSegmentArray::MakeTree()
233{
234 // AliSegmentID segment;
235 AliSegmentID * psegment = NewSegment();
236 if (fTree) delete fTree;
237 fTree = new TTree("Segment Tree","Tree with segments");
238 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1);
239 delete psegment;
240}
241
242Bool_t AliSegmentArray::MakeDictionary(Int_t size)
243{
244 //
245 //create index table for tree
246 //
247 if (size<1) return kFALSE;
248 if (fTreeIndex) delete fTreeIndex;
249 fTreeIndex = new AliArrayI();
250 fTreeIndex->Set(size);
251
252 AliSegmentID segment;
253 AliSegmentID * psegment = &segment;
254 fBranch->SetAddress(&psegment);
255 TBranch * brindix = fTree->GetBranch("fSegmentID");
256 Int_t nevent = (Int_t)fTree->GetEntries();
257 for (Int_t i = 0; i<nevent; i++){
258 brindix->GetEvent(i);
259 Int_t treeIndex=segment.GetID();
260 if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1);
261 // Int_t index = segment.GetID();
262 (*fTreeIndex)[treeIndex]=i+1; //
263 }
264 return kTRUE;
265}
266
267Bool_t AliSegmentArray::ConnectTree(const char * treeName)
268{
269 //connect tree from current directory
270 if (fTree){
271 delete fTree;
272 fTree = 0;
273 fBranch = 0;
274 }
275 fTree =(TTree*)gDirectory->Get(treeName);
276 if (fTree == 0) return kFALSE;
277 fBranch = fTree->GetBranch("Segment");
278 if (fBranch==0) return kFALSE;
279 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
280 MakeArray(fTreeIndex->fN);
281 return kTRUE;
282}
283
284AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
285{
286 //
287 //load segment with index to the memory
288 //
289 //
290 if (fTreeIndex ==0 ) MakeDictionary(3000);
291 //firstly try to load dictionary
292 if (fTreeIndex ==0 ) return 0;
293 if (fBranch==0) return 0;
294 if (index>fTreeIndex->fN) return 0;
295 AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
296 if (s==0) s= NewSegment();
297 s->SetID(index);
298 // new AliSegmentID(index);
299
300 if (s!=0) {
301 Int_t treeIndex =(*fTreeIndex)[index];
302 if (treeIndex<1) return 0;
303 else treeIndex--; //I don't like it Int table I have index shifted by 1
304 fBranch->SetAddress(&s);
305 fTree->GetEvent(treeIndex);
306 (*fSegment)[index] = (TObject*) s;
307 }
308 else
309 return 0;
310 return s;
311
312}
313AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
314{
315 //
316 //load segment at position inex in tree to the memory
317 //
318 //
319 if (fBranch==0) return 0;
320 if (index>fTree->GetEntries()) return 0;
321 AliSegmentID * s = NewSegment();
322
323 if (s) {
324 fBranch->SetAddress(&s);
325 fTree->GetEvent(index);
326 }
327 else
328 return 0;
329 Int_t nindex = s->GetID();
330 ClearSegment(nindex);
331 (*fSegment)[nindex] = (TObject*) s;
332 return s;
333}
334
335void AliSegmentArray::StoreSegment(Int_t index)
336{
337 //
338 //make segment persistent
339 //
73042f01 340 const AliSegmentID * ksegment = (*this)[index];
341 if (ksegment == 0 ) return;
cc80f89e 342 if (fTree==0) MakeTree();
73042f01 343 fBranch->SetAddress(&ksegment);
cc80f89e 344 fTree->Fill();
345}
346
347
348void AliSegmentArray::Streamer(TBuffer &R__b)
349{
350 TObjString treeName, * ptreeName=&treeName;
351 if (R__b.IsReading()) {
352 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
353 TNamed::Streamer(R__b);
354 R__b>>ptreeName;
355 if (fTree) delete fTree;
356 ConnectTree(ptreeName->String());
357 } else {
358 R__b.WriteVersion(AliSegmentArray::IsA());
359 TNamed::Streamer(R__b);
360 // char ch[200];
361 // sprintf(ch,"%s",fTrre->GetTitle());
362 treeName.String() = fTree->GetTitle();
363 R__b<<ptreeName;
364 fTree->Write();
365 }
366}