]>
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 | |
7d855b04 | 18 | /// \class AliSegmentArray |
19 | /// | |
20 | /// Alice segment manager object | |
21 | ||
70479d0e | 22 | #include <Riostream.h> |
a46e9031 | 23 | |
a46e9031 | 24 | #include <TTree.h> |
88cb7938 | 25 | #include <TROOT.h> |
26 | #include "AliRun.h" | |
27 | ||
a46e9031 | 28 | #include "TClonesArray.h" |
29 | #include "TDirectory.h" | |
e756ece8 | 30 | #include <TArrayI.h> |
a46e9031 | 31 | #include "TError.h" |
32 | #include "TClass.h" | |
1ec4fc51 | 33 | #include "TFile.h" |
a46e9031 | 34 | |
35 | #include "AliSegmentID.h" | |
36 | #include "AliSegmentArray.h" | |
37 | #include "TObjString.h" | |
38 | ||
a11596ad | 39 | using std::endl; |
40 | using std::cout; | |
a46e9031 | 41 | //_____________________________________________________________________________ |
7d855b04 | 42 | /// \cond CLASSIMP |
a46e9031 | 43 | ClassImp(AliSegmentArray) |
7d855b04 | 44 | /// \endcond |
a46e9031 | 45 | |
179c6296 | 46 | AliSegmentArray::AliSegmentArray() |
47 | :TNamed(), | |
48 | fSegment(0), | |
49 | fTreeIndex(0), | |
50 | fNSegment(0), | |
51 | fTree(0), | |
52 | fTreeOwner(kFALSE), | |
53 | fBranch(0), | |
54 | fClass(0) | |
a46e9031 | 55 | { |
56 | // | |
179c6296 | 57 | // constructor |
a46e9031 | 58 | // |
179c6296 | 59 | |
a46e9031 | 60 | } |
61 | ||
8e8eae84 | 62 | AliSegmentArray::AliSegmentArray(const char *classname, Int_t n) |
179c6296 | 63 | :TNamed("SegmentArray","SegmentArray"), |
64 | fSegment(0), | |
65 | fTreeIndex(0), | |
66 | fNSegment(0), | |
67 | fTree(0), | |
68 | fTreeOwner(kFALSE), | |
69 | fBranch(0), | |
70 | fClass(0) | |
a46e9031 | 71 | { |
7d855b04 | 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. | |
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 | { |
7d855b04 | 97 | /// copy constructor |
98 | /// to be later implemented | |
99 | ||
a46e9031 | 100 | } |
101 | ||
88cb7938 | 102 | AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/) |
a46e9031 | 103 | { |
7d855b04 | 104 | /// assignment operator |
105 | /// to be later implemented | |
106 | ||
a46e9031 | 107 | return (*this); |
108 | } | |
109 | ||
110 | AliSegmentArray::~AliSegmentArray() | |
111 | { | |
7d855b04 | 112 | /// default destructor |
113 | ||
a46e9031 | 114 | if (fNSegment>0){ |
115 | fSegment->Delete(); | |
116 | delete fSegment; | |
117 | } | |
4f57cad7 | 118 | if (fTree) { |
88cb7938 | 119 | if (fTreeOwner) delete fTree; |
4f57cad7 | 120 | else fTree->Reset();} |
88cb7938 | 121 | |
a46e9031 | 122 | if (fTreeIndex) delete fTreeIndex; |
40321246 | 123 | // if (fClass!=0) delete fClass; |
a46e9031 | 124 | } |
125 | ||
126 | ||
8e8eae84 | 127 | Bool_t AliSegmentArray::SetClass(const char *classname) |
a46e9031 | 128 | { |
7d855b04 | 129 | /// set class of stored object |
130 | ||
a46e9031 | 131 | if ( fClass !=0 ) { |
40321246 | 132 | //delete fClass; not ower of fClass |
a46e9031 | 133 | fClass = 0; |
134 | } | |
135 | if (fTree !=0) { | |
88cb7938 | 136 | if (fTreeOwner) delete fTree; |
137 | else fTree->Reset(); | |
a46e9031 | 138 | fTree = 0; |
139 | fBranch = 0; | |
140 | delete fTreeIndex; | |
141 | fTreeIndex = 0; | |
142 | } | |
88cb7938 | 143 | |
a46e9031 | 144 | if (fSegment != 0) { |
145 | fSegment->Delete(); | |
146 | delete fSegment; | |
147 | fSegment = 0; | |
148 | } | |
88cb7938 | 149 | |
a46e9031 | 150 | if (!gROOT) |
151 | ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized"); | |
152 | ||
153 | fClass = gROOT->GetClass(classname); | |
154 | if (!fClass) { | |
155 | Error("AliSegmentArray", "%s is not a valid class name", classname); | |
156 | return kFALSE; | |
157 | } | |
158 | if (!fClass->InheritsFrom(AliSegmentID::Class())) { | |
159 | Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname); | |
160 | return kFALSE; | |
161 | } | |
162 | return kTRUE; | |
163 | } | |
164 | ||
165 | ||
166 | AliSegmentID * AliSegmentArray::NewSegment() | |
167 | { | |
7d855b04 | 168 | /// create object according class information |
169 | ||
a46e9031 | 170 | if (fClass==0) return 0; |
171 | AliSegmentID * segment = (AliSegmentID * )fClass->New(); | |
172 | if (segment == 0) return 0; | |
173 | return segment; | |
174 | } | |
175 | ||
176 | ||
177 | Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment) | |
178 | { | |
7d855b04 | 179 | /// add segment to array |
180 | ||
a46e9031 | 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 | { | |
7d855b04 | 196 | /// add segment to array |
197 | ||
a46e9031 | 198 | if (fSegment==0) return 0; |
199 | if (fClass==0) return 0; | |
200 | AliSegmentID * segment = NewSegment(); | |
201 | if (segment == 0) return 0; | |
202 | fSegment->AddAt(segment,index); | |
203 | segment->SetID(index); | |
204 | fNSegment = fSegment->GetLast()+1; | |
205 | return segment; | |
206 | } | |
207 | ||
208 | ||
209 | void AliSegmentArray::ClearSegment(Int_t index) | |
210 | { | |
7d855b04 | 211 | /// remove segment from active memory |
212 | /// | |
213 | /// PH if ((*fSegment)[index]){ | |
214 | ||
01e713b2 | 215 | if (fSegment->At(index)){ |
a46e9031 | 216 | // (*fSegment)[index]->Delete(); //not working for TClonesArray |
01e713b2 | 217 | //PH delete (*fSegment)[index]; //because problem with deleting TClonesArray |
218 | //PH fSegment->RemoveAt(index); | |
219 | delete fSegment->RemoveAt(index); | |
a46e9031 | 220 | } |
221 | } | |
222 | ||
223 | ||
224 | Bool_t AliSegmentArray::MakeArray(Int_t n) | |
225 | { | |
7d855b04 | 226 | /// make array of pointers to Segments |
227 | ||
a46e9031 | 228 | if (fSegment) { |
229 | fSegment->Delete(); | |
230 | delete fSegment; | |
231 | } | |
232 | fSegment = new TObjArray(n); | |
233 | fNSegment=n; | |
234 | if (fSegment) return kTRUE; | |
235 | else return kFALSE; | |
236 | } | |
88cb7938 | 237 | void AliSegmentArray::MakeTree(TTree* tree) |
238 | { | |
7d855b04 | 239 | /// Make tree with the name |
240 | ||
88cb7938 | 241 | AliSegmentID * psegment = NewSegment(); |
242 | fTree = tree; | |
243 | //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000); | |
244 | fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99); | |
a46e9031 | 245 | |
88cb7938 | 246 | } |
a46e9031 | 247 | |
67ae3681 | 248 | void AliSegmentArray::MakeTree(char *file) |
a46e9031 | 249 | { |
7d855b04 | 250 | /// AliSegmentID segment; |
251 | ||
a46e9031 | 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 | { | |
7d855b04 | 289 | /// create index table for tree |
290 | ||
a46e9031 | 291 | if (size<1) return kFALSE; |
292 | if (fTreeIndex) delete fTreeIndex; | |
e756ece8 | 293 | fTreeIndex = new TArrayI(); |
a46e9031 | 294 | fTreeIndex->Set(size); |
295 | ||
40321246 | 296 | AliSegmentID * psegment = NewSegment(); //MI change |
a46e9031 | 297 | fBranch->SetAddress(&psegment); |
298 | TBranch * brindix = fTree->GetBranch("fSegmentID"); | |
299 | Int_t nevent = (Int_t)fTree->GetEntries(); | |
300 | for (Int_t i = 0; i<nevent; i++){ | |
301 | brindix->GetEvent(i); | |
40321246 | 302 | Int_t treeIndex=psegment->GetID(); |
e756ece8 | 303 | if (fTreeIndex->fN<treeIndex) fTreeIndex->Set(Int_t(Float_t(treeIndex)*1.5)+1); |
a46e9031 | 304 | // Int_t index = segment.GetID(); |
305 | (*fTreeIndex)[treeIndex]=i+1; // | |
306 | } | |
40321246 | 307 | if (psegment) delete psegment; |
a46e9031 | 308 | return kTRUE; |
309 | } | |
310 | ||
88cb7938 | 311 | Bool_t AliSegmentArray::ConnectTree(TTree* tree) |
312 | { | |
313 | fTree =tree; | |
314 | if (fTree == 0) return kFALSE; | |
315 | fBranch = fTree->GetBranch("Segment"); | |
316 | if (fBranch==0) return kFALSE; | |
317 | MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries()))); | |
318 | MakeArray(fTreeIndex->fN); | |
319 | return kTRUE; | |
320 | } | |
321 | ||
322 | ||
a46e9031 | 323 | Bool_t AliSegmentArray::ConnectTree(const char * treeName) |
324 | { | |
7d855b04 | 325 | /// connect tree from current directory |
326 | ||
a46e9031 | 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 | { | |
7d855b04 | 348 | /// load segment with index to the memory |
349 | ||
a46e9031 | 350 | if (fTreeIndex ==0 ) MakeDictionary(3000); |
351 | //firstly try to load dictionary | |
352 | if (fTreeIndex ==0 ) return 0; | |
353 | if (fBranch==0) return 0; | |
354 | if (index>fTreeIndex->fN) return 0; | |
01e713b2 | 355 | //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index]; |
356 | AliSegmentID *s = (AliSegmentID*)fSegment->At(index); | |
a46e9031 | 357 | if (s==0) s= NewSegment(); |
de0e19ab | 358 | |
a46e9031 | 359 | |
360 | if (s!=0) { | |
de0e19ab | 361 | s->SetID(index); |
362 | // new AliSegmentID(index); | |
a46e9031 | 363 | Int_t treeIndex =(*fTreeIndex)[index]; |
364 | if (treeIndex<1) return 0; | |
365 | else treeIndex--; //I don't like it Int table I have index shifted by 1 | |
366 | fBranch->SetAddress(&s); | |
367 | fTree->GetEvent(treeIndex); | |
01e713b2 | 368 | //PH (*fSegment)[index] = (TObject*) s; |
369 | fSegment->AddAt((TObject*) s, index); | |
a46e9031 | 370 | } |
371 | else | |
372 | return 0; | |
373 | return s; | |
374 | ||
375 | } | |
376 | AliSegmentID *AliSegmentArray::LoadEntry(Int_t index) | |
377 | { | |
7d855b04 | 378 | /// load segment at position inex in tree to the memory |
379 | ||
a46e9031 | 380 | if (fBranch==0) return 0; |
381 | if (index>fTree->GetEntries()) return 0; | |
382 | AliSegmentID * s = NewSegment(); | |
383 | ||
384 | if (s) { | |
385 | fBranch->SetAddress(&s); | |
386 | fTree->GetEvent(index); | |
387 | } | |
388 | else | |
389 | return 0; | |
390 | Int_t nindex = s->GetID(); | |
391 | ClearSegment(nindex); | |
01e713b2 | 392 | //PH (*fSegment)[nindex] = (TObject*) s; |
393 | fSegment->AddAt((TObject*) s, nindex); | |
a46e9031 | 394 | return s; |
395 | } | |
396 | ||
397 | void AliSegmentArray::StoreSegment(Int_t index) | |
398 | { | |
7d855b04 | 399 | /// make segment persistent |
400 | ||
a46e9031 | 401 | const AliSegmentID * ksegment = (*this)[index]; |
402 | if (ksegment == 0 ) return; | |
403 | if (fTree==0) MakeTree(); | |
404 | fBranch->SetAddress(&ksegment); | |
405 | fTree->Fill(); | |
406 | } | |
407 | ||
408 | ||
409 | void AliSegmentArray::Streamer(TBuffer &R__b) | |
410 | { | |
411 | TObjString treeName, * ptreeName=&treeName; | |
412 | if (R__b.IsReading()) { | |
413 | Version_t R__v = R__b.ReadVersion(); if (R__v) { } | |
414 | TNamed::Streamer(R__b); | |
415 | R__b>>ptreeName; | |
88cb7938 | 416 | if (fTree && fTreeOwner) delete fTree; |
a46e9031 | 417 | ConnectTree(ptreeName->String()); |
418 | } else { | |
419 | R__b.WriteVersion(AliSegmentArray::IsA()); | |
420 | TNamed::Streamer(R__b); | |
421 | // char ch[200]; | |
422 | // sprintf(ch,"%s",fTrre->GetTitle()); | |
423 | treeName.String() = fTree->GetTitle(); | |
424 | R__b<<ptreeName; | |
425 | fTree->Write(); | |
426 | } | |
427 | } | |
88cb7938 | 428 |