AliRoot Core  a565103 (a565103)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliSegmentArray.cxx
Go to the documentation of this file.
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 
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 //_____________________________________________________________________________
45 
47  :TNamed(),
48  fSegment(0),
49  fTreeIndex(0),
50  fNSegment(0),
51  fTree(0),
52  fTreeOwner(kFALSE),
53  fBranch(0),
54  fClass(0)
55 {
56  //
57  // constructor
58  //
59 
60 }
61 
62 AliSegmentArray::AliSegmentArray(const char *classname, Int_t n)
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)
71 {
77 
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 
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 
96 {
99 
100 }
101 
103 {
106 
107  return (*this);
108 }
109 
111 {
113 
114  if (fNSegment>0){
115  fSegment->Delete();
116  delete fSegment;
117  }
118  if (fTree) {
119  if (fTreeOwner) delete fTree;
120  else fTree->Reset();}
121 
122  if (fTreeIndex) delete fTreeIndex;
123  // if (fClass!=0) delete fClass;
124 }
125 
126 
127 Bool_t AliSegmentArray::SetClass(const char *classname)
128 {
130 
131  if ( fClass !=0 ) {
132  //delete fClass; not ower of fClass
133  fClass = 0;
134  }
135  if (fTree !=0) {
136  if (fTreeOwner) delete fTree;
137  else fTree->Reset();
138  fTree = 0;
139  fBranch = 0;
140  delete fTreeIndex;
141  fTreeIndex = 0;
142  }
143 
144  if (fSegment != 0) {
145  fSegment->Delete();
146  delete fSegment;
147  fSegment = 0;
148  }
149 
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 
167 {
169 
170  if (fClass==0) return 0;
171  AliSegmentID * segment = (AliSegmentID * )fClass->New();
172  if (segment == 0) return 0;
173  return segment;
174 }
175 
176 
178 {
180 
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 
195 {
197 
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 
210 {
214 
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);
220  }
221 }
222 
223 
225 {
227 
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 }
238 {
240 
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);
245 
246 }
247 
249 {
251 
252  AliSegmentID * psegment = NewSegment();
253  if (fTree) {
254  if (fTreeOwner)
255  {
256  delete fTree;
257  fTree = new TTree("Segment Tree","Tree with segments");
258  }
259  else fTree->Reset();
260  }
261  else {
262  cout << "Tree with segments does not exist"<<endl;
263  return;
264  }
265 
266 
267  //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
268  fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
269 
270  if (file) {
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());
279  }
280  cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;
281  wd->cd();
282  }
283  delete psegment;
284 }
285 
286 
288 {
290 
291  if (size<1) return kFALSE;
292  if (fTreeIndex) delete fTreeIndex;
293  fTreeIndex = new TArrayI();
294  fTreeIndex->Set(size);
295 
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; //
306  }
307  if (psegment) delete psegment;
308  return kTRUE;
309 }
310 
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 
323 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
324 {
326 
327  if (fTree){
328  if (fTreeOwner)
329  {
330  delete fTree;
331  fTree = 0;
332  }
333  else fTree->Reset();
334  fBranch = 0;
335  }
336  fTree =(TTree*)gDirectory->Get(treeName);
337 
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 
347 {
349 
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;
355  //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
356  AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
357  if (s==0) s= NewSegment();
358 
359 
360  if (s!=0) {
361  s->SetID(index);
362  // new AliSegmentID(index);
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);
368  //PH (*fSegment)[index] = (TObject*) s;
369  fSegment->AddAt((TObject*) s, index);
370  }
371  else
372  return 0;
373  return s;
374 
375 }
377 {
379 
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);
392  //PH (*fSegment)[nindex] = (TObject*) s;
393  fSegment->AddAt((TObject*) s, nindex);
394  return s;
395 }
396 
398 {
400 
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;
416  if (fTree && fTreeOwner) delete fTree;
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 }
428 
TTree * fTree
Definition: MakeTreeStat.C:55
AliSegmentID object.
Definition: AliSegmentID.h:15
Bool_t AddSegment(AliSegmentID *segment)
void ClearSegment(Int_t index)
#define TObjArray
TROOT * gROOT
virtual AliSegmentID * LoadSegment(Int_t index)
AliSegmentArray & operator=(const AliSegmentArray &segment)
Bool_t MakeArray(Int_t n)
Int_t fNSegment
number of alocated segments
void SetID(Int_t index)
Definition: AliSegmentID.h:20
TObjArray * fSegment
! pointer to array of pointers to segment
TTree * tree
ClassImp(TPCGenInfo)
Definition: AliTPCCmpNG.C:254
virtual void StoreSegment(Int_t index)
virtual void MakeTree(char *file=0)
virtual Bool_t ConnectTree(const char *treeName)
Bool_t fTreeOwner
flag determing the ownership of the fTree
virtual ~AliSegmentArray()
Int_t GetID()
Definition: AliSegmentID.h:19
virtual AliSegmentID * LoadEntry(Int_t index)
AliRun * gAlice
Bool_t SetClass(const char *classname)
virtual AliSegmentID * NewSegment()
TBranch * fBranch
! total branch
TTree * fTree
! tree with segment objects
TArrayI * fTreeIndex
! pointers(index) table in tree
Bool_t MakeDictionary(Int_t size)
TClass * fClass
! class type of included objects