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