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