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