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