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