f7336fa3 |
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 | */ |
19 | |
20 | /////////////////////////////////////////////////////////////////////////////// |
21 | // // |
22 | // Alice segment manager object // |
23 | // // |
24 | // AliTRDsegmentIDArray object is array of pointers to object derived from // |
25 | // AliTRDsegmentID object // |
26 | // AliTRDsegmentID - object in comparison with TObject enhalt // |
27 | // additional information fSegmentID // |
28 | // // |
29 | /////////////////////////////////////////////////////////////////////////////// |
30 | |
31 | #include <TROOT.h> |
32 | #include <TTree.h> |
33 | #include "TClonesArray.h" |
34 | #include "TDirectory.h" |
35 | #include "AliTRDarrayI.h" |
36 | #include "TError.h" |
37 | #include "TClass.h" |
38 | |
39 | #include "AliTRDsegmentID.h" |
40 | #include "AliTRDsegmentArrayBase.h" |
41 | |
42 | //_____________________________________________________________________________ |
43 | ClassImp(AliTRDsegmentArrayBase) |
44 | |
45 | AliTRDsegmentArrayBase::AliTRDsegmentArrayBase() |
46 | { |
47 | // |
48 | // |
49 | // |
50 | fNSegment=0; |
51 | fSegment =0; |
52 | fTreeIndex = 0; |
53 | fTree = 0; |
54 | fClass = 0; |
55 | } |
56 | |
57 | AliTRDsegmentArrayBase::AliTRDsegmentArrayBase(Text_t *classname, Int_t n) |
58 | { |
59 | // |
60 | //constructor which |
61 | // |
62 | // Create an array of objects of classname. The class must inherit from |
63 | // AliTRDsegmentID . The second argument adjust number of entries in |
64 | // the array. |
65 | fNSegment=0; |
66 | fSegment =0; |
67 | fTreeIndex = 0; |
68 | fTree = 0; |
69 | fClass = 0; |
70 | SetClass(classname); |
71 | if (MakeArray(n)==kFALSE){ |
72 | Error("AliTRDsegmentArrayBase", "can't allocate %d segments in memory",n); |
73 | return; |
74 | } |
75 | } |
76 | |
77 | Bool_t AliTRDsegmentArrayBase:: SetClass(Text_t *classname) |
78 | { |
79 | // |
80 | //set class of stored object |
81 | if ( fClass !=0 ) { |
82 | delete fClass; |
83 | fClass = 0; |
84 | } |
85 | if (fTree !=0) { |
86 | delete fTree; |
87 | fTree = 0; |
88 | fBranch = 0; |
89 | delete fTreeIndex; |
90 | fTreeIndex = 0; |
91 | } |
92 | if (fSegment != 0) { |
93 | fSegment->Delete(); |
94 | delete fSegment; |
95 | fSegment = 0; |
96 | } |
97 | if (!gROOT) |
98 | ::Fatal("AliTRDsegmentArrayBase::AliTRDsegmentArrayBase", "ROOT system not initialized"); |
99 | |
100 | fClass = gROOT->GetClass(classname); |
101 | if (!fClass) { |
102 | Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname); |
103 | return kFALSE; |
104 | } |
105 | if (!fClass->InheritsFrom(AliTRDsegmentID::Class())) { |
106 | Error("AliTRDsegmentArrayBase", "%s does not inherit from AliTRDsegmentID", classname); |
107 | return kFALSE; |
108 | } |
109 | return kTRUE; |
110 | } |
111 | |
112 | //Bool_t AliTRDsegmentArrayBase::ClassError( ) |
113 | //{ |
114 | //signalize class error |
115 | // if (!fClass) { |
116 | // Error("AliTRDsegmentArrayBase", "%s is not a valid class name", classname); |
117 | // return kFALSE; |
118 | // } |
119 | //// return kFALSE; |
120 | //} |
121 | |
122 | AliTRDsegmentArrayBase::~AliTRDsegmentArrayBase() |
123 | { |
124 | if (fNSegment>0){ |
125 | fSegment->Delete(); |
126 | delete fSegment; |
127 | } |
128 | if (fTree) delete fTree; |
129 | if (fTreeIndex) delete fTreeIndex; |
130 | if (fClass!=0) delete fClass; |
131 | } |
132 | |
133 | AliTRDsegmentID * AliTRDsegmentArrayBase::NewSegment() |
134 | { |
135 | // |
136 | //create object according class information |
137 | if (fClass==0) return 0; |
138 | AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New(); |
139 | if (segment == 0) return 0; |
140 | return segment; |
141 | } |
142 | |
143 | |
144 | Bool_t AliTRDsegmentArrayBase::AddSegment(AliTRDsegmentID *segment) |
145 | { |
146 | // |
147 | // add segment to array |
148 | // |
149 | if (segment==0) return kFALSE; |
150 | if (fSegment==0) return kFALSE; |
151 | if (fClass==0) return kFALSE; |
152 | if (!(segment->IsA()->InheritsFrom(fClass))){ |
153 | Error("AliTRDsegmentArrayBase", "added class %s is not of proper type ", |
154 | segment->IsA()->GetName()); |
155 | return kFALSE; |
156 | } |
157 | fSegment->AddAt(segment,segment->GetID()); |
158 | fNSegment = fSegment->GetLast()+1; |
159 | return kTRUE; |
160 | } |
161 | |
162 | AliTRDsegmentID * AliTRDsegmentArrayBase::AddSegment(Int_t index) |
163 | { |
164 | // |
165 | // add segment to array |
166 | // |
167 | if (fSegment==0) return 0; |
168 | if (fClass==0) return 0; |
169 | // AliTRDsegmentID * segment = (AliTRDsegmentID * )fClass->New(); |
170 | AliTRDsegmentID * segment = NewSegment(); |
171 | if (segment == 0) return 0; |
172 | fSegment->AddAt(segment,index); |
173 | segment->SetID(index); |
174 | fNSegment = fSegment->GetLast()+1; |
175 | return segment; |
176 | } |
177 | |
178 | |
179 | |
180 | Bool_t AliTRDsegmentArrayBase::MakeArray(Int_t n) |
181 | { |
182 | // |
183 | //make array of pointers to Segments |
184 | // |
185 | if (fSegment) { |
186 | fSegment->Delete(); |
187 | delete fSegment; |
188 | } |
189 | if (fTreeIndex) delete fTreeIndex; |
190 | fSegment = new TObjArray(n); |
191 | fTreeIndex = new AliTRDarrayI; |
192 | fTreeIndex->Set(n); |
193 | fNSegment=n; |
194 | if ( (fSegment) && (fTreeIndex)) return kTRUE; |
195 | else return kFALSE; |
196 | } |
197 | |
198 | |
199 | void AliTRDsegmentArrayBase::ClearSegment(Int_t index) |
200 | { |
201 | // |
202 | //remove segment from active memory |
203 | // |
204 | if ((*fSegment)[index]){ |
205 | // (*fSegment)[index]->Delete(); //not working for TClonesArray |
206 | delete (*fSegment)[index]; //because problem with deleting TClonesArray |
207 | fSegment->RemoveAt(index); |
208 | } |
209 | } |
210 | |
211 | |
212 | void AliTRDsegmentArrayBase::MakeTree() |
213 | { |
214 | // AliTRDsegmentID segment; |
215 | AliTRDsegmentID * psegment = NewSegment(); |
216 | if (fTree) delete fTree; |
217 | fTree = new TTree("Segment Tree","Tree with segments"); |
218 | fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,1); |
219 | delete psegment; |
220 | } |
221 | |
222 | Bool_t AliTRDsegmentArrayBase::ConnectTree(const char * treeName) |
223 | { |
224 | //connect tree from current directory |
225 | if (fTree){ |
226 | delete fTree; |
227 | fTree = 0; |
228 | fBranch = 0; |
229 | } |
230 | fTree =(TTree*)gDirectory->Get(treeName); |
231 | if (fTree == 0) return kFALSE; |
232 | fBranch = fTree->GetBranch("Segment"); |
233 | if (fBranch==0) return kFALSE; |
234 | MakeDictionary(TMath::Max(fNSegment,Int_t(fTree->GetEntries()))); |
235 | return kTRUE; |
236 | } |
237 | |
238 | AliTRDsegmentID *AliTRDsegmentArrayBase::LoadSegment(Int_t index) |
239 | { |
240 | // |
241 | //load segment with index to the memory |
242 | // |
243 | // |
244 | if (fTreeIndex ==0 ) MakeDictionary(3000); |
245 | //firstly try to load dictionary |
246 | if (fTreeIndex ==0 ) return 0; |
247 | if (fBranch==0) return 0; |
248 | if (index>fTreeIndex->fN) return 0; |
249 | AliTRDsegmentID *s = (AliTRDsegmentID*)(*fSegment)[index]; |
250 | if (s==0) s= NewSegment(); |
251 | s->SetID(index); |
252 | // new AliTRDsegmentID(index); |
253 | |
254 | if (s!=0) { |
255 | Int_t treeIndex =(*fTreeIndex)[index]; |
256 | if (treeIndex<1) return 0; |
257 | else treeIndex--; //I don't like it Int table I have index shifted by 1 |
258 | fBranch->SetAddress(&s); |
259 | fTree->GetEvent(treeIndex); |
260 | (*fSegment)[index] = (TObject*) s; |
261 | } |
262 | else |
263 | return 0; |
264 | return s; |
265 | // AbstractMethod("LoadSegment"); |
266 | } |
267 | AliTRDsegmentID *AliTRDsegmentArrayBase::LoadEntry(Int_t index) |
268 | { |
269 | // |
270 | //load segment at position inex in tree to the memory |
271 | // |
272 | // |
273 | if (fBranch==0) return 0; |
274 | if (index>fTree->GetEntries()) return 0; |
275 | AliTRDsegmentID * s = NewSegment(); |
276 | |
277 | if (s) { |
278 | fBranch->SetAddress(&s); |
279 | fTree->GetEvent(index); |
280 | } |
281 | else |
282 | return 0; |
283 | Int_t nindex = s->GetID(); |
284 | ClearSegment(nindex); |
285 | (*fSegment)[nindex] = (TObject*) s; |
286 | return s; |
287 | // AbstractMethod("LoadSegment"); |
288 | } |
289 | |
290 | void AliTRDsegmentArrayBase::StoreSegment(Int_t index) |
291 | { |
292 | // |
293 | //make segment persistent |
294 | // |
295 | const AliTRDsegmentID * segment = (*this)[index]; |
296 | if (segment == 0 ) return; |
297 | if (fTree==0) MakeTree(); |
298 | fBranch->SetAddress(&segment); |
299 | fTree->Fill(); |
300 | } |
301 | |
302 | Bool_t AliTRDsegmentArrayBase::MakeDictionary(Int_t size) |
303 | { |
304 | // |
305 | //create index table for tree |
306 | // |
307 | if (size<1) return kFALSE; |
308 | if (fTreeIndex) delete fTreeIndex; |
309 | fTreeIndex = new AliTRDarrayI(); |
310 | fTreeIndex->Set(size); |
311 | |
312 | AliTRDsegmentID segment; |
313 | AliTRDsegmentID * psegment = &segment; |
314 | fBranch->SetAddress(&psegment); |
315 | TBranch * brindix = fTree->GetBranch("fSegmentID"); |
316 | Int_t nevent = (Int_t)fTree->GetEntries(); |
317 | for (Int_t i = 0; i<nevent; i++){ |
318 | brindix->GetEvent(i); |
319 | Int_t treeIndex=segment.GetID(); |
320 | if (fTreeIndex->fN<treeIndex) fTreeIndex->Expand(Int_t(Float_t(treeIndex)*1.5)+1); |
321 | // Int_t index = segment.GetID(); |
322 | (*fTreeIndex)[treeIndex]=i+1; // MI 19.5. I'm sorry -index 0 couldn't be use in AliTRDarrayI |
323 | } |
324 | return kTRUE; |
325 | } |