]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCbase/AliSegmentArray.cxx
.so cleanup: removed from gSystem->Load()
[u/mrichter/AliRoot.git] / TPC / TPCbase / AliSegmentArray.cxx
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 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Alice segment manager object                                             //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23 #include <Riostream.h>
24
25 #include <TTree.h>
26 #include <TROOT.h>
27 #include "AliRun.h"
28
29 #include "TClonesArray.h"
30 #include "TDirectory.h"
31 #include <TArrayI.h>
32 #include "TError.h"
33 #include "TClass.h"
34 #include "TFile.h"
35
36 #include "AliSegmentID.h"
37 #include "AliSegmentArray.h"
38 #include "TObjString.h"
39
40 using std::endl;
41 using std::cout;
42 //_____________________________________________________________________________
43 ClassImp(AliSegmentArray)
44   
45   AliSegmentArray::AliSegmentArray()
46                   :TNamed(),
47                    fSegment(0),
48                    fTreeIndex(0),
49                    fNSegment(0),
50                    fTree(0),
51                    fTreeOwner(kFALSE),
52                    fBranch(0),
53                    fClass(0)                                      
54 {
55   //
56   // constructor
57   //
58
59 }
60
61 AliSegmentArray::AliSegmentArray(const char *classname, Int_t n)
62                 :TNamed("SegmentArray","SegmentArray"),
63                  fSegment(0),
64                  fTreeIndex(0),
65                  fNSegment(0),
66                  fTree(0),
67                  fTreeOwner(kFALSE),
68                  fBranch(0),
69                  fClass(0) 
70 {
71   //
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   SetClass(classname);
80   if (MakeArray(n)==kFALSE){
81      Error("AliSegmentArray", "can't allocate %d segments in memory",n);
82      return;
83    }
84 }
85
86 AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
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 {
97   //
98   //copy constructor
99   // to be later implemented
100 }
101
102 AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/)
103 {
104   //assignment operator
105   //to be later implemented
106   return (*this);
107 }
108
109 AliSegmentArray::~AliSegmentArray()
110 {
111   //
112   // default destructor
113   if (fNSegment>0){
114     fSegment->Delete();
115     delete fSegment;
116   }
117   if (fTree) { 
118    if (fTreeOwner) delete fTree;
119    else fTree->Reset();}
120
121   if (fTreeIndex) delete fTreeIndex;
122   //  if (fClass!=0) delete fClass;
123 }
124
125
126 Bool_t AliSegmentArray::SetClass(const char *classname)
127 {
128   //
129   //set class of stored object
130   if ( fClass !=0 ) {
131     //delete fClass; not ower of fClass
132     fClass = 0;
133   }
134   if (fTree !=0) {
135     if (fTreeOwner) delete fTree;
136     else fTree->Reset();
137     fTree = 0;
138     fBranch = 0;
139     delete fTreeIndex;
140     fTreeIndex = 0;
141   } 
142   
143   if (fSegment != 0) {
144     fSegment->Delete();
145     delete fSegment;
146     fSegment = 0;
147   }
148   
149   if (!gROOT)
150       ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
151    
152    fClass = gROOT->GetClass(classname);
153    if (!fClass) {
154       Error("AliSegmentArray", "%s is not a valid class name", classname);
155       return kFALSE;
156    }
157    if (!fClass->InheritsFrom(AliSegmentID::Class())) {
158       Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
159       return kFALSE;
160    }  
161    return kTRUE;
162 }
163
164
165 AliSegmentID * AliSegmentArray::NewSegment()
166 {
167   //
168   //create object according class information
169   if (fClass==0) return 0;
170   AliSegmentID * segment = (AliSegmentID * )fClass->New();
171   if (segment == 0) return 0;
172   return segment;
173 }
174
175
176 Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
177 {
178   //
179   // add segment to array
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
194 AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
195 {
196   //
197   // add segment to array
198   //
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;
206   return segment;
207 }
208
209
210 void AliSegmentArray::ClearSegment(Int_t index)
211 {
212   //
213   //remove segment from active memory    
214   //
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);
221   }
222 }
223
224
225 Bool_t AliSegmentArray::MakeArray(Int_t n)
226 {
227   //
228   //make array of pointers to Segments
229   //
230   if (fSegment) {
231     fSegment->Delete();
232     delete fSegment;
233   }  
234   fSegment = new TObjArray(n);  
235   fNSegment=n;
236   if (fSegment) return kTRUE;  
237   else return kFALSE;             
238 }
239 void AliSegmentArray::MakeTree(TTree* tree)
240 {
241              //Make tree with the name
242   AliSegmentID * psegment = NewSegment();  
243   fTree = tree;
244   //PH  fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
245   fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
246
247 }
248
249 void AliSegmentArray::MakeTree(char *file)
250 {
251   //  AliSegmentID  segment;
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
287 Bool_t  AliSegmentArray::MakeDictionary(Int_t size)
288 {
289   //
290   //create index table for tree
291   //  
292   if (size<1) return kFALSE;
293   if (fTreeIndex) delete fTreeIndex;
294   fTreeIndex = new TArrayI(); 
295   fTreeIndex->Set(size);
296   
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; //  
307   }
308   if (psegment) delete psegment;
309   return kTRUE;
310 }
311
312 Bool_t AliSegmentArray::ConnectTree(TTree* tree)
313 {
314   fTree =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);
320   return kTRUE;
321 }
322
323
324 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
325 {
326   //connect tree from current directory  
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
346 AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
347 {
348   //
349   //load segment with index to the memory
350   //
351   //
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();
360
361   
362   if (s!=0) {
363     s->SetID(index);
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);
372   }
373   else 
374     return 0;
375   return s;
376
377 }
378 AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
379 {
380   //
381   //load segment at position inex in tree  to the memory
382   //
383   //
384   if (fBranch==0) return 0;
385   if (index>fTree->GetEntries()) return 0;
386   AliSegmentID * s =  NewSegment();
387   
388   if (s) {
389     fBranch->SetAddress(&s);
390     fTree->GetEvent(index);
391   }
392   else 
393     return 0;
394   Int_t nindex = s->GetID();
395   ClearSegment(nindex);
396   //PH  (*fSegment)[nindex] = (TObject*) s;
397   fSegment->AddAt((TObject*) s, nindex);
398   return s;
399 }
400
401 void AliSegmentArray::StoreSegment(Int_t index)
402 {
403   //
404   //make segment persistent 
405   //
406   const AliSegmentID *  ksegment = (*this)[index];
407   if (ksegment == 0 ) return;
408   if (fTree==0) MakeTree();
409   fBranch->SetAddress(&ksegment);
410   fTree->Fill();
411 }
412
413
414 void AliSegmentArray::Streamer(TBuffer &R__b)
415 {
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);
420     R__b>>ptreeName;
421     if (fTree && fTreeOwner) delete fTree;
422     ConnectTree(ptreeName->String());   
423   } else {
424     R__b.WriteVersion(AliSegmentArray::IsA());
425     TNamed::Streamer(R__b);      
426     //  char  ch[200];
427     //  sprintf(ch,"%s",fTrre->GetTitle());
428     treeName.String() = fTree->GetTitle();
429     R__b<<ptreeName;
430     fTree->Write();
431   }
432 }
433