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 Revision 1.9 2002/10/14 14:55:34 hristov
19 Merging the VirtualMC branch to the main development branch (HEAD)
21 Revision 1.7.6.1 2002/06/10 15:00:53 hristov
24 Revision 1.8 2002/04/04 16:38:52 kowal2
25 Digits and Sdigits go to separate files
27 Revision 1.7 2001/10/21 18:22:54 hristov
28 BranchOld replaced by Branch. It works correctly with Root 2.02.xx
30 Revision 1.6 2001/08/30 09:25:24 hristov
31 The operator[] is replaced by At() or AddAt() in case of TObjArray. A temporary replacement of Branch with BranchOld is introduced
33 Revision 1.5 2001/07/27 13:03:11 hristov
34 Default Branch split level set to 99
36 Revision 1.4 2001/06/07 18:23:52 buncic
37 TPC branches now correctly diverted into Digits.TPS.root file
39 Revision 1.3 2001/02/05 14:34:54 hristov
40 Avoid deleting of Root class dictionary (R.Brun, M.Ivanov)
42 Revision 1.2 2001/01/26 20:29:00 hristov
43 Major upgrade of AliRoot code
45 Revision 1.1 2000/11/01 15:57:13 kowal2
46 Moved from the TPC directory
48 Revision 1.3 2000/06/30 12:07:49 kowal2
49 Updated from the TPC-PreRelease branch
51 Revision 1.2.4.1 2000/06/25 08:38:41 kowal2
52 Splitted from AliTPCtracking
54 Revision 1.2 2000/04/17 09:37:33 kowal2
55 removed obsolete AliTPCDigitsDisplay.C
57 Revision 1.1.4.2 2000/04/10 11:39:36 kowal2
59 New data structure handling
63 ///////////////////////////////////////////////////////////////////////////////
65 // Alice segment manager object //
67 ///////////////////////////////////////////////////////////////////////////////
68 #include <Riostream.h>
72 #include "TClonesArray.h"
73 #include "TDirectory.h"
74 #include "AliArrayI.h"
80 #include "AliSegmentID.h"
81 #include "AliSegmentArray.h"
82 #include "TObjString.h"
85 //_____________________________________________________________________________
86 ClassImp(AliSegmentArray)
88 AliSegmentArray::AliSegmentArray()
101 AliSegmentArray::AliSegmentArray(Text_t *classname, Int_t n)
106 // Create an array of objects of classname. The class must inherit from
107 // AliSegmentID . The second argument adjust number of entries in
114 SetName("SegmentArray");
115 SetTitle("SegmentArray");
118 if (MakeArray(n)==kFALSE){
119 Error("AliSegmentArray", "can't allocate %d segments in memory",n);
124 AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
128 // to be later implemented
131 AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & segment)
133 //assignment operator
134 //to be later implemented
138 AliSegmentArray::~AliSegmentArray()
141 // default destructor
146 if (fTree) delete fTree;
147 if (fTreeIndex) delete fTreeIndex;
148 // if (fClass!=0) delete fClass;
152 Bool_t AliSegmentArray::SetClass(Text_t *classname)
155 //set class of stored object
157 //delete fClass; not ower of fClass
173 ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
175 fClass = gROOT->GetClass(classname);
177 Error("AliSegmentArray", "%s is not a valid class name", classname);
180 if (!fClass->InheritsFrom(AliSegmentID::Class())) {
181 Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
188 AliSegmentID * AliSegmentArray::NewSegment()
191 //create object according class information
192 if (fClass==0) return 0;
193 AliSegmentID * segment = (AliSegmentID * )fClass->New();
194 if (segment == 0) return 0;
199 Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
202 // add segment to array
204 if (segment==0) return kFALSE;
205 if (fSegment==0) return kFALSE;
206 if (fClass==0) return kFALSE;
207 if (!(segment->IsA()->InheritsFrom(fClass))){
208 Error("AliSegmentArray", "added class %s is not of proper type ",
209 segment->IsA()->GetName());
212 fSegment->AddAt(segment,segment->GetID());
213 fNSegment = fSegment->GetLast()+1;
217 AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
220 // add segment to array
222 if (fSegment==0) return 0;
223 if (fClass==0) return 0;
224 AliSegmentID * segment = NewSegment();
225 if (segment == 0) return 0;
226 fSegment->AddAt(segment,index);
227 segment->SetID(index);
228 fNSegment = fSegment->GetLast()+1;
233 void AliSegmentArray::ClearSegment(Int_t index)
236 //remove segment from active memory
238 //PH if ((*fSegment)[index]){
239 if (fSegment->At(index)){
240 // (*fSegment)[index]->Delete(); //not working for TClonesArray
241 //PH delete (*fSegment)[index]; //because problem with deleting TClonesArray
242 //PH fSegment->RemoveAt(index);
243 delete fSegment->RemoveAt(index);
248 Bool_t AliSegmentArray::MakeArray(Int_t n)
251 //make array of pointers to Segments
257 fSegment = new TObjArray(n);
259 if (fSegment) return kTRUE;
264 void AliSegmentArray::MakeTree(char *file)
266 // AliSegmentID segment;
267 AliSegmentID * psegment = NewSegment();
268 if (fTree) delete fTree;
269 fTree = new TTree("Segment Tree","Tree with segments");
270 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
271 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
273 TString outFile = gAlice->GetBaseFile();
274 outFile = outFile + "/" + file;
275 fBranch->SetFile(outFile.Data());
276 TDirectory *wd = gDirectory;
277 TBranch *b = fBranch;
278 TIter next( b->GetListOfBranches());
279 while ((b=(TBranch*)next())) {
280 b->SetFile(outFile.Data());
282 cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;
288 ////////////////////////////////////////////////////////////////////////
289 TTree* AliSegmentArray::MakeTree(TFile *file)
292 // create the whole tree in the file file
294 AliSegmentID * psegment = NewSegment();
295 if (fTree) delete fTree;
296 TDirectory *wd = gDirectory;
298 fTree = new TTree("Segment Tree","Tree with segments");
299 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
304 ////////////////////////////////////////////////////////////////////////
306 Bool_t AliSegmentArray::MakeDictionary(Int_t size)
309 //create index table for tree
311 if (size<1) return kFALSE;
312 if (fTreeIndex) delete fTreeIndex;
313 fTreeIndex = new AliArrayI();
314 fTreeIndex->Set(size);
316 AliSegmentID * psegment = NewSegment(); //MI change
317 fBranch->SetAddress(&psegment);
318 TBranch * brindix = fTree->GetBranch("fSegmentID");
319 Int_t nevent = (Int_t)fTree->GetEntries();
320 for (Int_t i = 0; i<nevent; i++){
321 brindix->GetEvent(i);
322 Int_t treeIndex=psegment->GetID();
323 if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1);
324 // Int_t index = segment.GetID();
325 (*fTreeIndex)[treeIndex]=i+1; //
327 if (psegment) delete psegment;
331 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
333 //connect tree from current directory
339 fTree =(TTree*)gDirectory->Get(treeName);
340 if (fTree == 0) return kFALSE;
341 fBranch = fTree->GetBranch("Segment");
342 if (fBranch==0) return kFALSE;
343 MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
344 MakeArray(fTreeIndex->fN);
348 AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
351 //load segment with index to the memory
354 if (fTreeIndex ==0 ) MakeDictionary(3000);
355 //firstly try to load dictionary
356 if (fTreeIndex ==0 ) return 0;
357 if (fBranch==0) return 0;
358 if (index>fTreeIndex->fN) return 0;
359 //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
360 AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
361 if (s==0) s= NewSegment();
363 // new AliSegmentID(index);
366 Int_t treeIndex =(*fTreeIndex)[index];
367 if (treeIndex<1) return 0;
368 else treeIndex--; //I don't like it Int table I have index shifted by 1
369 fBranch->SetAddress(&s);
370 fTree->GetEvent(treeIndex);
371 //PH (*fSegment)[index] = (TObject*) s;
372 fSegment->AddAt((TObject*) s, index);
379 AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
382 //load segment at position inex in tree to the memory
385 if (fBranch==0) return 0;
386 if (index>fTree->GetEntries()) return 0;
387 AliSegmentID * s = NewSegment();
390 fBranch->SetAddress(&s);
391 fTree->GetEvent(index);
395 Int_t nindex = s->GetID();
396 ClearSegment(nindex);
397 //PH (*fSegment)[nindex] = (TObject*) s;
398 fSegment->AddAt((TObject*) s, nindex);
402 void AliSegmentArray::StoreSegment(Int_t index)
405 //make segment persistent
407 const AliSegmentID * ksegment = (*this)[index];
408 if (ksegment == 0 ) return;
409 if (fTree==0) MakeTree();
410 fBranch->SetAddress(&ksegment);
415 void AliSegmentArray::Streamer(TBuffer &R__b)
417 TObjString treeName, * ptreeName=&treeName;
418 if (R__b.IsReading()) {
419 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
420 TNamed::Streamer(R__b);
422 if (fTree) delete fTree;
423 ConnectTree(ptreeName->String());
425 R__b.WriteVersion(AliSegmentArray::IsA());
426 TNamed::Streamer(R__b);
428 // sprintf(ch,"%s",fTrre->GetTitle());
429 treeName.String() = fTree->GetTitle();