]>
Commit | Line | Data |
---|---|---|
a46e9031 | 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 | ||
88cb7938 | 16 | /* $Id$ */ |
a46e9031 | 17 | |
18 | /////////////////////////////////////////////////////////////////////////////// | |
19 | // // | |
20 | // Alice segment manager object // | |
21 | // // | |
22 | /////////////////////////////////////////////////////////////////////////////// | |
70479d0e | 23 | #include <Riostream.h> |
a46e9031 | 24 | |
a46e9031 | 25 | #include <TTree.h> |
88cb7938 | 26 | #include <TROOT.h> |
27 | #include "AliRun.h" | |
28 | ||
a46e9031 | 29 | #include "TClonesArray.h" |
30 | #include "TDirectory.h" | |
e756ece8 | 31 | #include <TArrayI.h> |
a46e9031 | 32 | #include "TError.h" |
33 | #include "TClass.h" | |
1ec4fc51 | 34 | #include "TFile.h" |
a46e9031 | 35 | |
36 | #include "AliSegmentID.h" | |
37 | #include "AliSegmentArray.h" | |
38 | #include "TObjString.h" | |
39 | ||
a11596ad | 40 | using std::endl; |
41 | using std::cout; | |
a46e9031 | 42 | //_____________________________________________________________________________ |
43 | ClassImp(AliSegmentArray) | |
44 | ||
179c6296 | 45 | AliSegmentArray::AliSegmentArray() |
46 | :TNamed(), | |
47 | fSegment(0), | |
48 | fTreeIndex(0), | |
49 | fNSegment(0), | |
50 | fTree(0), | |
51 | fTreeOwner(kFALSE), | |
52 | fBranch(0), | |
53 | fClass(0) | |
a46e9031 | 54 | { |
55 | // | |
179c6296 | 56 | // constructor |
a46e9031 | 57 | // |
179c6296 | 58 | |
a46e9031 | 59 | } |
60 | ||
8e8eae84 | 61 | AliSegmentArray::AliSegmentArray(const char *classname, Int_t n) |
179c6296 | 62 | :TNamed("SegmentArray","SegmentArray"), |
63 | fSegment(0), | |
64 | fTreeIndex(0), | |
65 | fNSegment(0), | |
66 | fTree(0), | |
67 | fTreeOwner(kFALSE), | |
68 | fBranch(0), | |
69 | fClass(0) | |
a46e9031 | 70 | { |
71 | // | |
72 | //constructor which | |
73 | // | |
74 | // Create an array of objects of classname. The class must inherit from | |
75 | // AliSegmentID . The second argument adjust number of entries in | |
76 | // the array. | |
179c6296 | 77 | |
a46e9031 | 78 | |
79 | SetClass(classname); | |
80 | if (MakeArray(n)==kFALSE){ | |
81 | Error("AliSegmentArray", "can't allocate %d segments in memory",n); | |
82 | return; | |
83 | } | |
84 | } | |
85 | ||
179c6296 | 86 | AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment) |
87 | :TNamed(segment), | |
88 | fSegment(0), | |
89 | fTreeIndex(0), | |
90 | fNSegment(0), | |
91 | fTree(0), | |
92 | fTreeOwner(kFALSE), | |
93 | fBranch(0), | |
94 | fClass(0) | |
95 | ||
a46e9031 | 96 | { |
97 | // | |
98 | //copy constructor | |
99 | // to be later implemented | |
100 | } | |
101 | ||
88cb7938 | 102 | AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/) |
a46e9031 | 103 | { |
104 | //assignment operator | |
105 | //to be later implemented | |
106 | return (*this); | |
107 | } | |
108 | ||
109 | AliSegmentArray::~AliSegmentArray() | |
110 | { | |
111 | // | |
112 | // default destructor | |
113 | if (fNSegment>0){ | |
114 | fSegment->Delete(); | |
115 | delete fSegment; | |
116 | } | |
4f57cad7 | 117 | if (fTree) { |
88cb7938 | 118 | if (fTreeOwner) delete fTree; |
4f57cad7 | 119 | else fTree->Reset();} |
88cb7938 | 120 | |
a46e9031 | 121 | if (fTreeIndex) delete fTreeIndex; |
40321246 | 122 | // if (fClass!=0) delete fClass; |
a46e9031 | 123 | } |
124 | ||
125 | ||
8e8eae84 | 126 | Bool_t AliSegmentArray::SetClass(const char *classname) |
a46e9031 | 127 | { |
128 | // | |
129 | //set class of stored object | |
130 | if ( fClass !=0 ) { | |
40321246 | 131 | //delete fClass; not ower of fClass |
a46e9031 | 132 | fClass = 0; |
133 | } | |
134 | if (fTree !=0) { | |
88cb7938 | 135 | if (fTreeOwner) delete fTree; |
136 | else fTree->Reset(); | |
a46e9031 | 137 | fTree = 0; |
138 | fBranch = 0; | |
139 | delete fTreeIndex; | |
140 | fTreeIndex = 0; | |
141 | } | |
88cb7938 | 142 | |
a46e9031 | 143 | if (fSegment != 0) { |
144 | fSegment->Delete(); | |
145 | delete fSegment; | |
146 | fSegment = 0; | |
147 | } | |
88cb7938 | 148 | |
a46e9031 | 149 | if (!gROOT) |
150 | ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized"); | |
151 | ||
152 | fClass = gROOT->GetClass(classname); | |
153 | if (!fClass) { | |
154 | Error("AliSegmentArray", "%s is not a valid class name", classname); | |
155 | return kFALSE; | |
156 | } | |
157 | if (!fClass->InheritsFrom(AliSegmentID::Class())) { | |
158 | Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname); | |
159 | return kFALSE; | |
160 | } | |
161 | return kTRUE; | |
162 | } | |
163 | ||
164 | ||
165 | AliSegmentID * AliSegmentArray::NewSegment() | |
166 | { | |
167 | // | |
168 | //create object according class information | |
169 | if (fClass==0) return 0; | |
170 | AliSegmentID * segment = (AliSegmentID * )fClass->New(); | |
171 | if (segment == 0) return 0; | |
172 | return segment; | |
173 | } | |
174 | ||
175 | ||
176 | Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment) | |
177 | { | |
178 | // | |
179 | // add segment to array | |
180 | // | |
181 | if (segment==0) return kFALSE; | |
182 | if (fSegment==0) return kFALSE; | |
183 | if (fClass==0) return kFALSE; | |
184 | if (!(segment->IsA()->InheritsFrom(fClass))){ | |
185 | Error("AliSegmentArray", "added class %s is not of proper type ", | |
186 | segment->IsA()->GetName()); | |
187 | return kFALSE; | |
188 | } | |
189 | fSegment->AddAt(segment,segment->GetID()); | |
190 | fNSegment = fSegment->GetLast()+1; | |
191 | return kTRUE; | |
192 | } | |
193 | ||
194 | AliSegmentID * AliSegmentArray::AddSegment(Int_t index) | |
195 | { | |
196 | // | |
197 | // add segment to array | |
198 | // | |
199 | if (fSegment==0) return 0; | |
200 | if (fClass==0) return 0; | |
201 | AliSegmentID * segment = NewSegment(); | |
202 | if (segment == 0) return 0; | |
203 | fSegment->AddAt(segment,index); | |
204 | segment->SetID(index); | |
205 | fNSegment = fSegment->GetLast()+1; | |
206 | return segment; | |
207 | } | |
208 | ||
209 | ||
210 | void AliSegmentArray::ClearSegment(Int_t index) | |
211 | { | |
212 | // | |
213 | //remove segment from active memory | |
214 | // | |
01e713b2 | 215 | //PH if ((*fSegment)[index]){ |
216 | if (fSegment->At(index)){ | |
a46e9031 | 217 | // (*fSegment)[index]->Delete(); //not working for TClonesArray |
01e713b2 | 218 | //PH delete (*fSegment)[index]; //because problem with deleting TClonesArray |
219 | //PH fSegment->RemoveAt(index); | |
220 | delete fSegment->RemoveAt(index); | |
a46e9031 | 221 | } |
222 | } | |
223 | ||
224 | ||
225 | Bool_t AliSegmentArray::MakeArray(Int_t n) | |
226 | { | |
227 | // | |
228 | //make array of pointers to Segments | |
229 | // | |
230 | if (fSegment) { | |
231 | fSegment->Delete(); | |
232 | delete fSegment; | |
233 | } | |
234 | fSegment = new TObjArray(n); | |
235 | fNSegment=n; | |
236 | if (fSegment) return kTRUE; | |
237 | else return kFALSE; | |
238 | } | |
88cb7938 | 239 | void AliSegmentArray::MakeTree(TTree* tree) |
240 | { | |
241 | //Make tree with the name | |
242 | AliSegmentID * psegment = NewSegment(); | |
243 | fTree = tree; | |
244 | //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000); | |
245 | fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99); | |
a46e9031 | 246 | |
88cb7938 | 247 | } |
a46e9031 | 248 | |
67ae3681 | 249 | void AliSegmentArray::MakeTree(char *file) |
a46e9031 | 250 | { |
251 | // AliSegmentID segment; | |
252 | AliSegmentID * psegment = NewSegment(); | |
4f57cad7 | 253 | if (fTree) { |
88cb7938 | 254 | if (fTreeOwner) |
255 | { | |
256 | delete fTree; | |
257 | fTree = new TTree("Segment Tree","Tree with segments"); | |
258 | } | |
de0e19ab | 259 | else fTree->Reset(); |
260 | } | |
261 | else { | |
262 | cout << "Tree with segments does not exist"<<endl; | |
263 | return; | |
264 | } | |
88cb7938 | 265 | |
266 | ||
01e713b2 | 267 | //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000); |
21e74529 | 268 | fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99); |
88cb7938 | 269 | |
270 | if (file) { | |
cec8fec2 | 271 | TString outFile = gAlice->GetBaseFile(); |
272 | outFile = outFile + "/" + file; | |
273 | fBranch->SetFile(outFile.Data()); | |
67ae3681 | 274 | TDirectory *wd = gDirectory; |
67ae3681 | 275 | TBranch *b = fBranch; |
276 | TIter next( b->GetListOfBranches()); | |
277 | while ((b=(TBranch*)next())) { | |
cec8fec2 | 278 | b->SetFile(outFile.Data()); |
67ae3681 | 279 | } |
88cb7938 | 280 | cout << "Diverting branch " << "Segment" << " to file " << outFile << endl; |
67ae3681 | 281 | wd->cd(); |
282 | } | |
a46e9031 | 283 | delete psegment; |
284 | } | |
285 | ||
1ec4fc51 | 286 | |
a46e9031 | 287 | Bool_t AliSegmentArray::MakeDictionary(Int_t size) |
288 | { | |
289 | // | |
290 | //create index table for tree | |
291 | // | |
292 | if (size<1) return kFALSE; | |
293 | if (fTreeIndex) delete fTreeIndex; | |
e756ece8 | 294 | fTreeIndex = new TArrayI(); |
a46e9031 | 295 | fTreeIndex->Set(size); |
296 | ||
40321246 | 297 | AliSegmentID * psegment = NewSegment(); //MI change |
a46e9031 | 298 | fBranch->SetAddress(&psegment); |
299 | TBranch * brindix = fTree->GetBranch("fSegmentID"); | |
300 | Int_t nevent = (Int_t)fTree->GetEntries(); | |
301 | for (Int_t i = 0; i<nevent; i++){ | |
302 | brindix->GetEvent(i); | |
40321246 | 303 | Int_t treeIndex=psegment->GetID(); |
e756ece8 | 304 | if (fTreeIndex->fN<treeIndex) fTreeIndex->Set(Int_t(Float_t(treeIndex)*1.5)+1); |
a46e9031 | 305 | // Int_t index = segment.GetID(); |
306 | (*fTreeIndex)[treeIndex]=i+1; // | |
307 | } | |
40321246 | 308 | if (psegment) delete psegment; |
a46e9031 | 309 | return kTRUE; |
310 | } | |
311 | ||
88cb7938 | 312 | Bool_t AliSegmentArray::ConnectTree(TTree* tree) |
313 | { | |
314 | fTree =tree; | |
315 | if (fTree == 0) return kFALSE; | |
316 | fBranch = fTree->GetBranch("Segment"); | |
317 | if (fBranch==0) return kFALSE; | |
318 | MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries()))); | |
319 | MakeArray(fTreeIndex->fN); | |
320 | return kTRUE; | |
321 | } | |
322 | ||
323 | ||
a46e9031 | 324 | Bool_t AliSegmentArray::ConnectTree(const char * treeName) |
325 | { | |
326 | //connect tree from current directory | |
327 | if (fTree){ | |
88cb7938 | 328 | if (fTreeOwner) |
329 | { | |
330 | delete fTree; | |
331 | fTree = 0; | |
332 | } | |
333 | else fTree->Reset(); | |
334 | fBranch = 0; | |
a46e9031 | 335 | } |
336 | fTree =(TTree*)gDirectory->Get(treeName); | |
88cb7938 | 337 | |
a46e9031 | 338 | if (fTree == 0) return kFALSE; |
339 | fBranch = fTree->GetBranch("Segment"); | |
340 | if (fBranch==0) return kFALSE; | |
341 | MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries()))); | |
342 | MakeArray(fTreeIndex->fN); | |
343 | return kTRUE; | |
344 | } | |
345 | ||
346 | AliSegmentID *AliSegmentArray::LoadSegment(Int_t index) | |
347 | { | |
348 | // | |
349 | //load segment with index to the memory | |
350 | // | |
351 | // | |
352 | if (fTreeIndex ==0 ) MakeDictionary(3000); | |
353 | //firstly try to load dictionary | |
354 | if (fTreeIndex ==0 ) return 0; | |
355 | if (fBranch==0) return 0; | |
356 | if (index>fTreeIndex->fN) return 0; | |
01e713b2 | 357 | //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index]; |
358 | AliSegmentID *s = (AliSegmentID*)fSegment->At(index); | |
a46e9031 | 359 | if (s==0) s= NewSegment(); |
de0e19ab | 360 | |
a46e9031 | 361 | |
362 | if (s!=0) { | |
de0e19ab | 363 | s->SetID(index); |
364 | // new AliSegmentID(index); | |
a46e9031 | 365 | Int_t treeIndex =(*fTreeIndex)[index]; |
366 | if (treeIndex<1) return 0; | |
367 | else treeIndex--; //I don't like it Int table I have index shifted by 1 | |
368 | fBranch->SetAddress(&s); | |
369 | fTree->GetEvent(treeIndex); | |
01e713b2 | 370 | //PH (*fSegment)[index] = (TObject*) s; |
371 | fSegment->AddAt((TObject*) s, index); | |
a46e9031 | 372 | } |
373 | else | |
374 | return 0; | |
375 | return s; | |
376 | ||
377 | } | |
378 | AliSegmentID *AliSegmentArray::LoadEntry(Int_t index) | |
379 | { | |
380 | // | |
381 | //load segment at position inex in tree to the memory | |
382 | // | |
383 | // | |
384 | if (fBranch==0) return 0; | |
385 | if (index>fTree->GetEntries()) return 0; | |
386 | AliSegmentID * s = NewSegment(); | |
387 | ||
388 | if (s) { | |
389 | fBranch->SetAddress(&s); | |
390 | fTree->GetEvent(index); | |
391 | } | |
392 | else | |
393 | return 0; | |
394 | Int_t nindex = s->GetID(); | |
395 | ClearSegment(nindex); | |
01e713b2 | 396 | //PH (*fSegment)[nindex] = (TObject*) s; |
397 | fSegment->AddAt((TObject*) s, nindex); | |
a46e9031 | 398 | return s; |
399 | } | |
400 | ||
401 | void AliSegmentArray::StoreSegment(Int_t index) | |
402 | { | |
403 | // | |
404 | //make segment persistent | |
405 | // | |
406 | const AliSegmentID * ksegment = (*this)[index]; | |
407 | if (ksegment == 0 ) return; | |
408 | if (fTree==0) MakeTree(); | |
409 | fBranch->SetAddress(&ksegment); | |
410 | fTree->Fill(); | |
411 | } | |
412 | ||
413 | ||
414 | void AliSegmentArray::Streamer(TBuffer &R__b) | |
415 | { | |
416 | TObjString treeName, * ptreeName=&treeName; | |
417 | if (R__b.IsReading()) { | |
418 | Version_t R__v = R__b.ReadVersion(); if (R__v) { } | |
419 | TNamed::Streamer(R__b); | |
420 | R__b>>ptreeName; | |
88cb7938 | 421 | if (fTree && fTreeOwner) delete fTree; |
a46e9031 | 422 | ConnectTree(ptreeName->String()); |
423 | } else { | |
424 | R__b.WriteVersion(AliSegmentArray::IsA()); | |
425 | TNamed::Streamer(R__b); | |
426 | // char ch[200]; | |
427 | // sprintf(ch,"%s",fTrre->GetTitle()); | |
428 | treeName.String() = fTree->GetTitle(); | |
429 | R__b<<ptreeName; | |
430 | fTree->Write(); | |
431 | } | |
432 | } | |
88cb7938 | 433 |