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