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