]> git.uio.no Git - u/mrichter/AliRoot.git/blob - CONTAINERS/AliSegmentArray.cxx
Removing warnings (Sun)
[u/mrichter/AliRoot.git] / CONTAINERS / 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 "AliArrayI.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
41 //_____________________________________________________________________________
42 ClassImp(AliSegmentArray)
43   
44 AliSegmentArray::AliSegmentArray()
45 {
46   //
47   //
48   //
49   fNSegment=0;
50   fSegment =0; 
51   fTreeIndex = 0;
52   fTree  = 0;
53   fClass = 0;
54   fBranch = 0;
55   fTreeOwner = kFALSE;
56 }
57
58 AliSegmentArray::AliSegmentArray(const char *classname, Int_t n)
59 {
60   //
61   //constructor which 
62   // 
63   //  Create an array of objects of classname. The class must inherit from
64   //  AliSegmentID .  The second argument adjust number of entries in 
65   //  the array.
66   fTreeOwner = kFALSE;
67   fNSegment=0;
68   fSegment =0; 
69   fTreeIndex = 0;
70   fTree  = 0;
71   fClass = 0;
72   SetName("SegmentArray");
73   SetTitle("SegmentArray");
74
75   SetClass(classname);
76   if (MakeArray(n)==kFALSE){
77      Error("AliSegmentArray", "can't allocate %d segments in memory",n);
78      return;
79    }
80 }
81
82 AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment):
83   TNamed(segment)
84 {
85   //
86   //copy constructor
87   // to be later implemented
88 }
89
90 AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & /*segment*/)
91 {
92   //assignment operator
93   //to be later implemented
94   return (*this);
95 }
96
97 AliSegmentArray::~AliSegmentArray()
98 {
99   //
100   // default destructor
101   if (fNSegment>0){
102     fSegment->Delete();
103     delete fSegment;
104   }
105   if (fTree) 
106    if (fTreeOwner) delete fTree;
107    else fTree->Reset();
108
109   if (fTreeIndex) delete fTreeIndex;
110   //  if (fClass!=0) delete fClass;
111 }
112
113
114 Bool_t AliSegmentArray::SetClass(const char *classname)
115 {
116   //
117   //set class of stored object
118   if ( fClass !=0 ) {
119     //delete fClass; not ower of fClass
120     fClass = 0;
121   }
122   if (fTree !=0) {
123     if (fTreeOwner) delete fTree;
124     else fTree->Reset();
125     fTree = 0;
126     fBranch = 0;
127     delete fTreeIndex;
128     fTreeIndex = 0;
129   } 
130   
131   if (fSegment != 0) {
132     fSegment->Delete();
133     delete fSegment;
134     fSegment = 0;
135   }
136   
137   if (!gROOT)
138       ::Fatal("AliSegmentArray::AliSegmentArray", "ROOT system not initialized");
139    
140    fClass = gROOT->GetClass(classname);
141    if (!fClass) {
142       Error("AliSegmentArray", "%s is not a valid class name", classname);
143       return kFALSE;
144    }
145    if (!fClass->InheritsFrom(AliSegmentID::Class())) {
146       Error("AliSegmentArray", "%s does not inherit from AliSegmentID", classname);
147       return kFALSE;
148    }  
149    return kTRUE;
150 }
151
152
153 AliSegmentID * AliSegmentArray::NewSegment()
154 {
155   //
156   //create object according class information
157   if (fClass==0) return 0;
158   AliSegmentID * segment = (AliSegmentID * )fClass->New();
159   if (segment == 0) return 0;
160   return segment;
161 }
162
163
164 Bool_t AliSegmentArray::AddSegment(AliSegmentID *segment)
165 {
166   //
167   // add segment to array
168   //
169   if (segment==0) return kFALSE;
170   if (fSegment==0) return kFALSE;
171   if (fClass==0) return kFALSE;
172   if (!(segment->IsA()->InheritsFrom(fClass))){
173     Error("AliSegmentArray", "added class %s  is not of proper type ",
174           segment->IsA()->GetName());
175       return kFALSE;
176   }
177   fSegment->AddAt(segment,segment->GetID());
178   fNSegment = fSegment->GetLast()+1;
179   return kTRUE;
180 }
181
182 AliSegmentID * AliSegmentArray::AddSegment(Int_t index)
183 {
184   //
185   // add segment to array
186   //
187   if (fSegment==0) return 0;
188   if (fClass==0) return 0;
189   AliSegmentID * segment = NewSegment();
190   if (segment == 0) return 0;
191   fSegment->AddAt(segment,index);
192   segment->SetID(index);
193   fNSegment = fSegment->GetLast()+1;
194   return segment;
195 }
196
197
198 void AliSegmentArray::ClearSegment(Int_t index)
199 {
200   //
201   //remove segment from active memory    
202   //
203   //PH  if ((*fSegment)[index]){
204   if (fSegment->At(index)){
205     //    (*fSegment)[index]->Delete(); //not working for TClonesArray
206     //PH    delete (*fSegment)[index]; //because problem with deleting TClonesArray
207     //PH    fSegment->RemoveAt(index);
208     delete fSegment->RemoveAt(index);
209   }
210 }
211
212
213 Bool_t AliSegmentArray::MakeArray(Int_t n)
214 {
215   //
216   //make array of pointers to Segments
217   //
218   if (fSegment) {
219     fSegment->Delete();
220     delete fSegment;
221   }  
222   fSegment = new TObjArray(n);  
223   fNSegment=n;
224   if (fSegment) return kTRUE;  
225   else return kFALSE;             
226 }
227 void AliSegmentArray::MakeTree(TTree* tree)
228 {
229              //Make tree with the name
230   AliSegmentID * psegment = NewSegment();  
231   fTree = tree;
232   //PH  fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
233   fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
234
235 }
236
237 void AliSegmentArray::MakeTree(char *file)
238 {
239   //  AliSegmentID  segment;
240   AliSegmentID * psegment = NewSegment();  
241   if (fTree) 
242     if (fTreeOwner) 
243      {
244        delete fTree;
245        fTree = new TTree("Segment Tree","Tree with segments");     
246      }
247     else fTree->Reset();
248
249   
250   //PH  fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
251    fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
252  
253   if (file) {
254         TString outFile = gAlice->GetBaseFile();
255         outFile = outFile + "/" + file;
256         fBranch->SetFile(outFile.Data());
257         TDirectory *wd = gDirectory;
258         TBranch *b = fBranch;
259         TIter next( b->GetListOfBranches());
260         while ((b=(TBranch*)next())) {
261            b->SetFile(outFile.Data());
262         }
263         cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;  
264         wd->cd(); 
265     }
266   delete psegment;
267 }              
268
269
270 Bool_t  AliSegmentArray::MakeDictionary(Int_t size)
271 {
272   //
273   //create index table for tree
274   //  
275   if (size<1) return kFALSE;
276   if (fTreeIndex) delete fTreeIndex;
277   fTreeIndex = new AliArrayI(); 
278   fTreeIndex->Set(size);
279   
280   AliSegmentID * psegment = NewSegment(); //MI change
281   fBranch->SetAddress(&psegment);
282   TBranch * brindix = fTree->GetBranch("fSegmentID");
283   Int_t nevent = (Int_t)fTree->GetEntries();  
284   for (Int_t i = 0; i<nevent; i++){
285     brindix->GetEvent(i);
286     Int_t treeIndex=psegment->GetID();
287     if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1);
288     //    Int_t index = segment.GetID(); 
289     (*fTreeIndex)[treeIndex]=i+1; //  
290   }
291   if (psegment) delete psegment;
292   return kTRUE;
293 }
294
295 Bool_t AliSegmentArray::ConnectTree(TTree* tree)
296 {
297   fTree =tree;
298   if (fTree == 0)    return kFALSE;
299   fBranch = fTree->GetBranch("Segment");
300   if (fBranch==0) return kFALSE;
301   MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
302   MakeArray(fTreeIndex->fN);
303   return kTRUE;
304 }
305
306
307 Bool_t AliSegmentArray::ConnectTree(const char * treeName)
308 {
309   //connect tree from current directory  
310   if (fTree){
311    if (fTreeOwner) 
312     {
313      delete fTree;
314      fTree = 0;
315     }
316    else fTree->Reset();
317    fBranch = 0;
318   }
319   fTree =(TTree*)gDirectory->Get(treeName);
320   
321   if (fTree == 0)    return kFALSE;
322   fBranch = fTree->GetBranch("Segment");
323   if (fBranch==0) return kFALSE;
324   MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries())));
325   MakeArray(fTreeIndex->fN);
326   return kTRUE;
327 }
328
329 AliSegmentID *AliSegmentArray::LoadSegment(Int_t index)
330 {
331   //
332   //load segment with index to the memory
333   //
334   //
335   if (fTreeIndex ==0 ) MakeDictionary(3000);
336   //firstly try to load dictionary 
337   if (fTreeIndex ==0 ) return 0;
338   if (fBranch==0) return 0;
339   if (index>fTreeIndex->fN) return 0;
340   //PH  AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
341   AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
342   if (s==0)  s=  NewSegment();
343   s->SetID(index);
344   //  new AliSegmentID(index);
345   
346   if (s!=0) {
347     Int_t treeIndex =(*fTreeIndex)[index];
348     if (treeIndex<1) return 0;
349     else treeIndex--;   //I don't like it Int table I have index shifted by 1                  
350     fBranch->SetAddress(&s);
351     fTree->GetEvent(treeIndex);
352     //PH    (*fSegment)[index] = (TObject*) s;
353     fSegment->AddAt((TObject*) s, index);
354   }
355   else 
356     return 0;
357   return s;
358
359 }
360 AliSegmentID *AliSegmentArray::LoadEntry(Int_t index)
361 {
362   //
363   //load segment at position inex in tree  to the memory
364   //
365   //
366   if (fBranch==0) return 0;
367   if (index>fTree->GetEntries()) return 0;
368   AliSegmentID * s =  NewSegment();
369   
370   if (s) {
371     fBranch->SetAddress(&s);
372     fTree->GetEvent(index);
373   }
374   else 
375     return 0;
376   Int_t nindex = s->GetID();
377   ClearSegment(nindex);
378   //PH  (*fSegment)[nindex] = (TObject*) s;
379   fSegment->AddAt((TObject*) s, nindex);
380   return s;
381 }
382
383 void AliSegmentArray::StoreSegment(Int_t index)
384 {
385   //
386   //make segment persistent 
387   //
388   const AliSegmentID *  ksegment = (*this)[index];
389   if (ksegment == 0 ) return;
390   if (fTree==0) MakeTree();
391   fBranch->SetAddress(&ksegment);
392   fTree->Fill();
393 }
394
395
396 void AliSegmentArray::Streamer(TBuffer &R__b)
397 {
398   TObjString treeName, * ptreeName=&treeName;
399   if (R__b.IsReading()) {
400     Version_t R__v = R__b.ReadVersion(); if (R__v) { }
401     TNamed::Streamer(R__b);
402     R__b>>ptreeName;
403     if (fTree && fTreeOwner) delete fTree;
404     ConnectTree(ptreeName->String());   
405   } else {
406     R__b.WriteVersion(AliSegmentArray::IsA());
407     TNamed::Streamer(R__b);      
408     //  char  ch[200];
409     //  sprintf(ch,"%s",fTrre->GetTitle());
410     treeName.String() = fTree->GetTitle();
411     R__b<<ptreeName;
412     fTree->Write();
413   }
414 }
415