Adaption to new fluka common blocks (E. Futo)
[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$
70479d0e 18Revision 1.9 2002/10/14 14:55:34 hristov
19Merging the VirtualMC branch to the main development branch (HEAD)
20
b9d0a01d 21Revision 1.7.6.1 2002/06/10 15:00:53 hristov
22Merged with v3-08-02
23
24Revision 1.8 2002/04/04 16:38:52 kowal2
25Digits and Sdigits go to separate files
26
1ec4fc51 27Revision 1.7 2001/10/21 18:22:54 hristov
28BranchOld replaced by Branch. It works correctly with Root 2.02.xx
29
21e74529 30Revision 1.6 2001/08/30 09:25:24 hristov
31The operator[] is replaced by At() or AddAt() in case of TObjArray. A temporary replacement of Branch with BranchOld is introduced
32
01e713b2 33Revision 1.5 2001/07/27 13:03:11 hristov
34Default Branch split level set to 99
35
d0f40f23 36Revision 1.4 2001/06/07 18:23:52 buncic
37TPC branches now correctly diverted into Digits.TPS.root file
38
cec8fec2 39Revision 1.3 2001/02/05 14:34:54 hristov
40Avoid deleting of Root class dictionary (R.Brun, M.Ivanov)
41
40321246 42Revision 1.2 2001/01/26 20:29:00 hristov
43Major upgrade of AliRoot code
44
67ae3681 45Revision 1.1 2000/11/01 15:57:13 kowal2
46Moved from the TPC directory
47
a46e9031 48Revision 1.3 2000/06/30 12:07:49 kowal2
49Updated from the TPC-PreRelease branch
50
51Revision 1.2.4.1 2000/06/25 08:38:41 kowal2
52Splitted from AliTPCtracking
53
54Revision 1.2 2000/04/17 09:37:33 kowal2
55removed obsolete AliTPCDigitsDisplay.C
56
57Revision 1.1.4.2 2000/04/10 11:39:36 kowal2
58
59New data structure handling
60
61*/
62
63///////////////////////////////////////////////////////////////////////////////
64// //
65// Alice segment manager object //
66// //
67///////////////////////////////////////////////////////////////////////////////
70479d0e 68#include <Riostream.h>
a46e9031 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"
1ec4fc51 77#include "TFile.h"
a46e9031 78
67ae3681 79#include "AliRun.h"
a46e9031 80#include "AliSegmentID.h"
81#include "AliSegmentArray.h"
82#include "TObjString.h"
83
84
85//_____________________________________________________________________________
86ClassImp(AliSegmentArray)
87
88AliSegmentArray::AliSegmentArray()
89{
90 //
91 //
92 //
93 fNSegment=0;
94 fSegment =0;
95 fTreeIndex = 0;
96 fTree = 0;
97 fClass = 0;
21e74529 98 fBranch = 0;
a46e9031 99}
100
101AliSegmentArray::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
124AliSegmentArray::AliSegmentArray(const AliSegmentArray &segment)
125{
126 //
127 //copy constructor
128 // to be later implemented
129}
130
131AliSegmentArray &AliSegmentArray::operator = (const AliSegmentArray & segment)
132{
133 //assignment operator
134 //to be later implemented
135 return (*this);
136}
137
138AliSegmentArray::~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;
40321246 148 // if (fClass!=0) delete fClass;
a46e9031 149}
150
151
152Bool_t AliSegmentArray::SetClass(Text_t *classname)
153{
154 //
155 //set class of stored object
156 if ( fClass !=0 ) {
40321246 157 //delete fClass; not ower of fClass
a46e9031 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
188AliSegmentID * 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
199Bool_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
217AliSegmentID * 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
233void AliSegmentArray::ClearSegment(Int_t index)
234{
235 //
236 //remove segment from active memory
237 //
01e713b2 238 //PH if ((*fSegment)[index]){
239 if (fSegment->At(index)){
a46e9031 240 // (*fSegment)[index]->Delete(); //not working for TClonesArray
01e713b2 241 //PH delete (*fSegment)[index]; //because problem with deleting TClonesArray
242 //PH fSegment->RemoveAt(index);
243 delete fSegment->RemoveAt(index);
a46e9031 244 }
245}
246
247
248Bool_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
67ae3681 264void AliSegmentArray::MakeTree(char *file)
a46e9031 265{
266 // AliSegmentID segment;
267 AliSegmentID * psegment = NewSegment();
268 if (fTree) delete fTree;
269 fTree = new TTree("Segment Tree","Tree with segments");
01e713b2 270 //PH fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000);
21e74529 271 fBranch = fTree->Branch("Segment",psegment->IsA()->GetName(),&psegment,64000,99);
01e713b2 272 if (file) {
cec8fec2 273 TString outFile = gAlice->GetBaseFile();
274 outFile = outFile + "/" + file;
275 fBranch->SetFile(outFile.Data());
67ae3681 276 TDirectory *wd = gDirectory;
67ae3681 277 TBranch *b = fBranch;
278 TIter next( b->GetListOfBranches());
279 while ((b=(TBranch*)next())) {
cec8fec2 280 b->SetFile(outFile.Data());
67ae3681 281 }
cec8fec2 282 cout << "Diverting branch " << "Segment" << " to file " << outFile << endl;
67ae3681 283 wd->cd();
284 }
a46e9031 285 delete psegment;
286}
287
1ec4fc51 288////////////////////////////////////////////////////////////////////////
289TTree* 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
a46e9031 306Bool_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
40321246 316 AliSegmentID * psegment = NewSegment(); //MI change
a46e9031 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);
40321246 322 Int_t treeIndex=psegment->GetID();
a46e9031 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 }
40321246 327 if (psegment) delete psegment;
a46e9031 328 return kTRUE;
329}
330
331Bool_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
348AliSegmentID *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;
01e713b2 359 //PH AliSegmentID *s = (AliSegmentID*)(*fSegment)[index];
360 AliSegmentID *s = (AliSegmentID*)fSegment->At(index);
a46e9031 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);
01e713b2 371 //PH (*fSegment)[index] = (TObject*) s;
372 fSegment->AddAt((TObject*) s, index);
a46e9031 373 }
374 else
375 return 0;
376 return s;
377
378}
379AliSegmentID *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);
01e713b2 397 //PH (*fSegment)[nindex] = (TObject*) s;
398 fSegment->AddAt((TObject*) s, nindex);
a46e9031 399 return s;
400}
401
402void 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
415void 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}