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"
41 //_____________________________________________________________________________
42 ClassImp(AliSegmentArray)
44 AliSegmentArray::AliSegmentArray()
60 AliSegmentArray::AliSegmentArray(const char *classname, Int_t n)
61 :TNamed("SegmentArray","SegmentArray"),
73 // Create an array of objects of classname. The class must inherit from
74 // AliSegmentID . The second argument adjust number of entries in
79 if (MakeArray(n)==kFALSE){
80 Error("AliSegmentArray", "can't allocate %d segments in memory",n);
85 AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
98 // to be later implemented
101 AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/)
103 //assignment operator
104 //to be later implemented
108 AliSegmentArray::~AliSegmentArray()
111 // default destructor
117 if (fTreeOwner) delete fTree;
118 else fTree->Reset();}
120 if (fTreeIndex) delete fTreeIndex;
121 // if (fClass!=0) delete fClass;
125 Bool_t AliSegmentArray::SetClass(const char *classname)
128 //set class of stored object
130 //delete fClass; not ower of fClass
134 if (fTreeOwner) delete fTree;
149 ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
151 fClass = gROOT->GetClass(classname);
153 Error("AliSegmentArray", "%s is not a valid class name", classname);
156 if (!fClass->InheritsFrom(AliSegmentID::Class())) {
157 Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
164 AliSegmentID * AliSegmentArray::NewSegment()
167 //create object according class information
168 if (fClass==0) return 0;
169 AliSegmentID * segment = (AliSegmentID * )fClass->New();
170 if (segment == 0) return 0;
175 Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
178 // add segment to array
180 if (segment==0) return kFALSE;
181 if (fSegment==0) return kFALSE;
182 if (fClass==0) return kFALSE;
183 if (!(segment->IsA()->InheritsFrom(fClass))){
184 Error("AliSegmentArray", "added class %s is not of proper type ",
185 segment->IsA()->GetName());
188 fSegment->AddAt(segment,segment->GetID());
189 fNSegment = fSegment->GetLast()+1;
193 AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
196 // add segment to array
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;
209 void AliSegmentArray::ClearSegment(Int_t index)
212 //remove segment from active memory
214 //PH if ((*fSegment)[index]){
215 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 delete fSegment->RemoveAt(index);
224 Bool_t AliSegmentArray::MakeArray(Int_t n)
227 //make array of pointers to Segments
233 fSegment = new TObjArray(n);
235 if (fSegment) return kTRUE;
238 void AliSegmentArray::MakeTree(TTree* tree)
240 //Make tree with the name
241 AliSegmentID * psegment = NewSegment();
243 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
244 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
248 void AliSegmentArray::MakeTree(char *file)
250 // AliSegmentID segment;
251 AliSegmentID * psegment = NewSegment();
256 fTree = new TTree("Segment Tree","Tree with segments");
261 cout << "Tree with segments does not exist"<<endl;
266 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
267 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
270 TString outFile = gAlice->GetBaseFile();
271 outFile = outFile + "/" + file;
272 fBranch->SetFile(outFile.Data());
273 TDirectory *wd = gDirectory;
274 TBranch *b = fBranch;
275 TIter next( b->GetListOfBranches());
276 while ((b=(TBranch*)next())) {
277 b->SetFile(outFile.Data());
279 cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;
286 Bool_t AliSegmentArray::MakeDictionary(Int_t size)
289 //create index table for tree
291 if (size<1) return kFALSE;
292 if (fTreeIndex) delete fTreeIndex;
293 fTreeIndex = new TArrayI();
294 fTreeIndex->Set(size);
296 AliSegmentID * psegment = NewSegment(); //MI change
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);
302 Int_t treeIndex=psegment->GetID();
303 if (fTreeIndex->fN<treeIndex) fTreeIndex->Set(Int_t(Float_t(treeIndex)*1.5)+1);
304 // Int_t index = segment.GetID();
305 (*fTreeIndex)[treeIndex]=i+1; //
307 if (psegment) delete psegment;
311 Bool_t AliSegmentArray::ConnectTree(TTree* 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);
323 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
325 //connect tree from current directory
335 fTree =(TTree*)gDirectory->Get(treeName);
337 if (fTree == 0) return kFALSE;
338 fBranch = fTree->GetBranch("Segment");
339 if (fBranch==0) return kFALSE;
340 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
341 MakeArray(fTreeIndex->fN);
345 AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
348 //load segment with index to the memory
351 if (fTreeIndex ==0 ) MakeDictionary(3000);
352 //firstly try to load dictionary
353 if (fTreeIndex ==0 ) return 0;
354 if (fBranch==0) return 0;
355 if (index>fTreeIndex->fN) return 0;
356 //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
357 AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
358 if (s==0) s= NewSegment();
363 // new AliSegmentID(index);
364 Int_t treeIndex =(*fTreeIndex)[index];
365 if (treeIndex<1) return 0;
366 else treeIndex--; //I don't like it Int table I have index shifted by 1
367 fBranch->SetAddress(&s);
368 fTree->GetEvent(treeIndex);
369 //PH (*fSegment)[index] = (TObject*) s;
370 fSegment->AddAt((TObject*) s, index);
377 AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
380 //load segment at position inex in tree to the memory
383 if (fBranch==0) return 0;
384 if (index>fTree->GetEntries()) return 0;
385 AliSegmentID * s = NewSegment();
388 fBranch->SetAddress(&s);
389 fTree->GetEvent(index);
393 Int_t nindex = s->GetID();
394 ClearSegment(nindex);
395 //PH (*fSegment)[nindex] = (TObject*) s;
396 fSegment->AddAt((TObject*) s, nindex);
400 void AliSegmentArray::StoreSegment(Int_t index)
403 //make segment persistent
405 const AliSegmentID * ksegment = (*this)[index];
406 if (ksegment == 0 ) return;
407 if (fTree==0) MakeTree();
408 fBranch->SetAddress(&ksegment);
413 void AliSegmentArray::Streamer(TBuffer &R__b)
415 TObjString treeName, * ptreeName=&treeName;
416 if (R__b.IsReading()) {
417 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
418 TNamed::Streamer(R__b);
420 if (fTree && fTreeOwner) delete fTree;
421 ConnectTree(ptreeName->String());
423 R__b.WriteVersion(AliSegmentArray::IsA());
424 TNamed::Streamer(R__b);
426 // sprintf(ch,"%s",fTrre->GetTitle());
427 treeName.String() = fTree->GetTitle();