1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // Alice segment manager object //
22 ///////////////////////////////////////////////////////////////////////////////
23 #include <Riostream.h>
29 #include "TClonesArray.h"
30 #include "TDirectory.h"
36 #include "AliSegmentID.h"
37 #include "AliSegmentArray.h"
38 #include "TObjString.h"
42 //_____________________________________________________________________________
43 ClassImp(AliSegmentArray)
45 AliSegmentArray::AliSegmentArray()
61 AliSegmentArray::AliSegmentArray(const char *classname, Int_t n)
62 :TNamed("SegmentArray","SegmentArray"),
74 // Create an array of objects of classname. The class must inherit from
75 // AliSegmentID . The second argument adjust number of entries in
80 if (MakeArray(n)==kFALSE){
81 Error("AliSegmentArray", "can't allocate %d segments in memory",n);
86 AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
99 // to be later implemented
102 AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/)
104 //assignment operator
105 //to be later implemented
109 AliSegmentArray::~AliSegmentArray()
112 // default destructor
118 if (fTreeOwner) delete fTree;
119 else fTree->Reset();}
121 if (fTreeIndex) delete fTreeIndex;
122 // if (fClass!=0) delete fClass;
126 Bool_t AliSegmentArray::SetClass(const char *classname)
129 //set class of stored object
131 //delete fClass; not ower of fClass
135 if (fTreeOwner) delete fTree;
150 ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
152 fClass = gROOT->GetClass(classname);
154 Error("AliSegmentArray", "%s is not a valid class name", classname);
157 if (!fClass->InheritsFrom(AliSegmentID::Class())) {
158 Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
165 AliSegmentID * AliSegmentArray::NewSegment()
168 //create object according class information
169 if (fClass==0) return 0;
170 AliSegmentID * segment = (AliSegmentID * )fClass->New();
171 if (segment == 0) return 0;
176 Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
179 // add segment to array
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());
189 fSegment->AddAt(segment,segment->GetID());
190 fNSegment = fSegment->GetLast()+1;
194 AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
197 // add segment to array
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;
210 void AliSegmentArray::ClearSegment(Int_t index)
213 //remove segment from active memory
215 //PH if ((*fSegment)[index]){
216 if (fSegment->At(index)){
217 // (*fSegment)[index]->Delete(); //not working for TClonesArray
218 //PH delete (*fSegment)[index]; //because problem with deleting TClonesArray
219 //PH fSegment->RemoveAt(index);
220 delete fSegment->RemoveAt(index);
225 Bool_t AliSegmentArray::MakeArray(Int_t n)
228 //make array of pointers to Segments
234 fSegment = new TObjArray(n);
236 if (fSegment) return kTRUE;
239 void AliSegmentArray::MakeTree(TTree* tree)
241 //Make tree with the name
242 AliSegmentID * psegment = NewSegment();
244 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
245 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
249 void AliSegmentArray::MakeTree(char *file)
251 // AliSegmentID segment;
252 AliSegmentID * psegment = NewSegment();
257 fTree = new TTree("Segment Tree","Tree with segments");
262 cout << "Tree with segments does not exist"<<endl;
267 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
268 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
271 TString outFile = gAlice->GetBaseFile();
272 outFile = outFile + "/" + file;
273 fBranch->SetFile(outFile.Data());
274 TDirectory *wd = gDirectory;
275 TBranch *b = fBranch;
276 TIter next( b->GetListOfBranches());
277 while ((b=(TBranch*)next())) {
278 b->SetFile(outFile.Data());
280 cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;
287 Bool_t AliSegmentArray::MakeDictionary(Int_t size)
290 //create index table for tree
292 if (size<1) return kFALSE;
293 if (fTreeIndex) delete fTreeIndex;
294 fTreeIndex = new TArrayI();
295 fTreeIndex->Set(size);
297 AliSegmentID * psegment = NewSegment(); //MI change
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);
303 Int_t treeIndex=psegment->GetID();
304 if (fTreeIndex->fN<treeIndex) fTreeIndex->Set(Int_t(Float_t(treeIndex)*1.5)+1);
305 // Int_t index = segment.GetID();
306 (*fTreeIndex)[treeIndex]=i+1; //
308 if (psegment) delete psegment;
312 Bool_t AliSegmentArray::ConnectTree(TTree* 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);
324 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
326 //connect tree from current directory
336 fTree =(TTree*)gDirectory->Get(treeName);
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);
346 AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
349 //load segment with index to the memory
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;
357 //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
358 AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
359 if (s==0) s= NewSegment();
364 // new AliSegmentID(index);
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);
370 //PH (*fSegment)[index] = (TObject*) s;
371 fSegment->AddAt((TObject*) s, index);
378 AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
381 //load segment at position inex in tree to the memory
384 if (fBranch==0) return 0;
385 if (index>fTree->GetEntries()) return 0;
386 AliSegmentID * s = NewSegment();
389 fBranch->SetAddress(&s);
390 fTree->GetEvent(index);
394 Int_t nindex = s->GetID();
395 ClearSegment(nindex);
396 //PH (*fSegment)[nindex] = (TObject*) s;
397 fSegment->AddAt((TObject*) s, nindex);
401 void AliSegmentArray::StoreSegment(Int_t index)
404 //make segment persistent
406 const AliSegmentID * ksegment = (*this)[index];
407 if (ksegment == 0 ) return;
408 if (fTree==0) MakeTree();
409 fBranch->SetAddress(&ksegment);
414 void AliSegmentArray::Streamer(TBuffer &R__b)
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);
421 if (fTree && fTreeOwner) delete fTree;
422 ConnectTree(ptreeName->String());
424 R__b.WriteVersion(AliSegmentArray::IsA());
425 TNamed::Streamer(R__b);
427 // sprintf(ch,"%s",fTrre->GetTitle());
428 treeName.String() = fTree->GetTitle();