01-sep-2003 NvE Explicit initialisation of TObject() introduced in default constructo...
[u/mrichter/AliRoot.git] / RALICE / AliObjMatrix.cxx
CommitLineData
84bb7c66 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// $Id$
17
18///////////////////////////////////////////////////////////////////////////
19// Class AliObjMatrix
20// Handling of a matrix structure of objects.
21// All objects which are derived from TObject may be entered into the matrix
22// structure. This means that also TObjArray objects can be entered,
23// which implies an increase of the dimension of the resulting structure.
24//
25// Example :
26// =========
27//
28// AliObjMatrix* matrix=new AliObjMatrix();
29// matrix->SetOwner();
30// matrix->SetSwapMode();
31//
32// Float_t pos[3];
33//
34// AliSignal* s=0;
35//
36// s=new AliSignal();
37// s->SetSignal(135);
38// pos[0]=-120.4
39// pos[1]=78.25
40// pos[3]=12.93
41// s->SetPosition(pos,"car");
c72198f1 42// matrix->EnterObject(6,21,s);
84bb7c66 43//
44// s=new AliSignal();
45// s->SetSignal(25.84);
46// pos[0]=68.7
47// pos[1]=-53.88
48// pos[3]=22.69
49// s->SetPosition(pos,"car");
c72198f1 50// matrix->EnterObject(8,13,s);
84bb7c66 51//
52// s=new AliSignal();
53// s->SetSignal(87.25);
54// pos[0]=154.8
55// pos[1]=932.576
56// pos[3]=-1382.754
57// s->SetPosition(pos,"car");
c72198f1 58// matrix->EnterObject(64,3,s);
84bb7c66 59//
60// Int_t nrows=matrix->GetMaxRow();
61// Int_t ncols=matrix->GetMaxColumn();
62//
63// cout << " Maxrow : " << nrows << " Maxcol : " << ncols
64// << " Nobjects : " << matrix->GetNobjects() << endl;
65//
66// for (Int_t i=1; i<=nrows; i++)
67// {
68// for (Int_t j=1; j<=ncols; j++)
69// {
70// s=(AliSignal*)matrix->GetObject(i,j);
71// if (s) cout << " At (" << i << "," << j << ") Signal : " << s->GetSignal() << endl;
72// }
73// }
74//
75//--- Author: Nick van Eijndhoven 23-jan-2003 Utrecht University
76//- Modified: NvE $Date$ Utrecht University
77///////////////////////////////////////////////////////////////////////////
78
79#include "AliObjMatrix.h"
c72198f1 80#include "Riostream.h"
84bb7c66 81
82ClassImp(AliObjMatrix) // Class implementation to enable ROOT I/O
83
1fbffa23 84AliObjMatrix::AliObjMatrix() : TObject()
84bb7c66 85{
86// Default constructor.
87// Note : The owner and swap mode flags will be initialised to 0.
88// See the memberfunctions SetOwner() and SetSwapMode() for further
89// details.
90 fRows=0;
91 fOwn=0;
92 fSwap=0;
93 fMaxrow=0;
94 fMaxcol=0;
c72198f1 95 fObjects=0;
84bb7c66 96}
97///////////////////////////////////////////////////////////////////////////
98AliObjMatrix::~AliObjMatrix()
99{
100// Default destructor.
101 if (fRows)
102 {
103 delete fRows;
104 fRows=0;
105 }
c72198f1 106 if (fObjects)
107 {
108 delete fObjects;
109 fObjects=0;
110 }
84bb7c66 111}
112///////////////////////////////////////////////////////////////////////////
113void AliObjMatrix::Reset()
114{
115// Reset the whole matrix structure.
116// Note : The values of the owner and swap mode flags will not be modified.
117// To modify the ownership, use the memberfunction SetOwner().
118// To modify the swap mode, use the memberfunction SetSwapMode().
119 if (fRows)
120 {
121 delete fRows;
122 fRows=0;
123 }
c72198f1 124 if (fObjects)
125 {
126 delete fObjects;
127 fObjects=0;
128 }
84bb7c66 129
130 fMaxrow=0;
131 fMaxcol=0;
84bb7c66 132}
133///////////////////////////////////////////////////////////////////////////
134void AliObjMatrix::SetOwner(Int_t own)
135{
136// Set the owner flag (0/1) for the stored objects.
137// When the owner flag is set to 1, all entered objects are owned by the
138// matrix structure.
139// At invokation of this memberfunction the default argument is own=1.
140//
141 fOwn=own;
142
143 if (!fRows) return;
144
145 for (Int_t irow=0; irow<fRows->GetSize(); irow++)
146 {
147 TObjArray* mrow=(TObjArray*)fRows->At(irow);
148 if (mrow)
149 {
150 if (own)
151 {
152 mrow->SetOwner(kTRUE);
153 }
154 else
155 {
156 mrow->SetOwner(kFALSE);
157 }
158 }
159 }
160}
161///////////////////////////////////////////////////////////////////////////
162Int_t AliObjMatrix::GetOwner()
163{
164// Provide the owner flag for the stored objects.
165 return fOwn;
166}
167///////////////////////////////////////////////////////////////////////////
168void AliObjMatrix::SetSwapMode(Int_t swap)
169{
170// Set the swap mode flag (0/1) for the internal matrix storage.
171// In case the number of rows differs considerably from the number of columns,
172// it might be more efficient (w.r.t. memory usage and/or output file size)
173// to internally store the matrix with the rows and colums swapped.
174// This swapping is only related with the internal storage and as such
175// is completely hidden for the user.
176// At invokation of this memberfunction the default argument is swap=1.
177//
178// Note : The swap mode can only be set as long as no objects have
179// been stored in the matrix structure (i.e. a new instance
180// of AliObjMatrix or after invokation of the Reset() function).
181//
182 if (!fRows)
183 {
184 fSwap=swap;
185 }
186 else
187 {
188 cout << " *AliObjMatrix::SetSwapMode* Matrix not empty ==> No action." << endl;
189 }
190}
191///////////////////////////////////////////////////////////////////////////
192Int_t AliObjMatrix::GetSwapMode()
193{
194// Provide the swap mode flag for this matrix.
195 return fSwap;
196}
197///////////////////////////////////////////////////////////////////////////
198void AliObjMatrix::EnterObject(Int_t row,Int_t col,TObject* obj)
199{
200// Enter an object to the matrix structure at location (row,col).
201// In case the location already contained an object, the existing object
202// will first be removed before the new object is stored.
c72198f1 203// According to the status of the owner flag (see the SetOwner() function)
84bb7c66 204// the existing object will also be deleted.
205// Note : The first location in the matrix is indicated as (1,1).
206 if (row<1 || col<1)
207 {
208 cout << " *AliObjMatrix::AddObject* Invalid argument(s) (row,col) : ("
209 << row << "," << col << ")" << endl;
210 return;
211 }
212
213 if (row>fMaxrow) fMaxrow=row;
214 if (col>fMaxcol) fMaxcol=col;
215
216 Int_t rowx=row;
217 if (fSwap) rowx=col;
218 Int_t colx=col;
219 if (fSwap) colx=row;
220
221 if (!fRows)
222 {
223 fRows=new TObjArray(rowx);
224 fRows->SetOwner();
225 }
226 else
227 {
228 if (rowx > fRows->GetSize()) fRows->Expand(rowx);
229 }
230
231 TObjArray* mrow=(TObjArray*)fRows->At(rowx-1);
232
233 if (!mrow)
234 {
235 TObjArray* columns=new TObjArray(colx);
236 if (fOwn) columns->SetOwner();
237 fRows->AddAt(columns,rowx-1);
238 mrow=columns;
239 }
240 else
241 {
242 if (colx > mrow->GetSize()) mrow->Expand(colx);
243 }
244
245 TObject* old=(TObject*)mrow->At(colx-1);
c72198f1 246 if (old)
247 {
248 fObjects->Remove(old);
249 fObjects->Compress();
250 if (fOwn) delete old;
251 }
84bb7c66 252
253 mrow->AddAt(obj,colx-1);
c72198f1 254
255 if (!fObjects) fObjects=new TObjArray();
256 fObjects->Add(obj);
257}
258///////////////////////////////////////////////////////////////////////////
259void AliObjMatrix::RemoveObject(Int_t row,Int_t col)
260{
261// Remove the object stored at the matrix location (row,col).
262// In case the object was owned by the matrix, it will be deleted.
263//
264// Note : The first location in the matrix is indicated as (1,1).
265
266 TObject* obj=0;
267
268 if (!fRows || row<1 || col<1) return;
269
270
271 Int_t rowx=row;
272 if (fSwap) rowx=col;
273 Int_t colx=col;
274 if (fSwap) colx=row;
275
276 TObjArray* mrow=0;
277 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
278
279 if (!mrow) return;
280
281 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
282
283 if (obj)
284 {
285 fObjects->Remove(obj);
286 fObjects->Compress();
287 mrow->Remove(obj);
288 if (fOwn) delete obj;
289 }
84bb7c66 290}
291///////////////////////////////////////////////////////////////////////////
292TObject* AliObjMatrix::GetObject(Int_t row,Int_t col)
293{
294// Provide a pointer to the object stored at the matrix location (row,col).
295// In case no object was stored at the indicated location or the location
296// would reside outside the matrix boundaries, a value 0 will be returned.
297// Note : The first location in the matrix is indicated as (1,1).
298
299 TObject* obj=0;
300
301 if (!fRows || row<1 || col<1) return obj;
302
303
304 Int_t rowx=row;
305 if (fSwap) rowx=col;
306 Int_t colx=col;
307 if (fSwap) colx=row;
308
309 TObjArray* mrow=0;
310 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
311
312 if (!mrow) return obj;
313
314 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
315
316 return obj;
317}
318///////////////////////////////////////////////////////////////////////////
c72198f1 319TObject* AliObjMatrix::GetObject(Int_t j)
320{
321// Provide a pointer to the j-th stored object.
322// In case the index j is invalid, a value 0 will be returned.
323// The first stored object is indicated as j=1.
324//
325// Note : Do NOT delete the object.
326// To remove an object, the memberfunction RemoveObject()
327// should be used.
328
329 TObject* obj=0;
330 Int_t nobj=0;
331 if (fObjects) nobj=fObjects->GetSize();
332
333 if (j>0 && j<=nobj) obj=(TObject*)fObjects->At(j-1);
334
335 return obj;
336}
337///////////////////////////////////////////////////////////////////////////
338TObjArray* AliObjMatrix::GetObjects()
339{
340// Provide references to all the stored objects.
341// In case no objects are present, a value 0 will be returned.
342//
343// Note : Do NOT make any changes to the reference array apart from
344// changing the order of the pointers of the various objects.
345// For addition or removal of objects, the memberfunctions
346// EnterObject() and RemoveObject() should be used.
347
348 return fObjects;
349}
350///////////////////////////////////////////////////////////////////////////
84bb7c66 351Int_t AliObjMatrix::GetMaxRow()
352{
353// Provide the maximum row number index.
354 return fMaxrow;
355}
356///////////////////////////////////////////////////////////////////////////
357Int_t AliObjMatrix::GetMaxColumn()
358{
359// Provide the maximum column number index.
360 return fMaxcol;
361}
362///////////////////////////////////////////////////////////////////////////
363Int_t AliObjMatrix::GetNobjects()
364{
365// Provide the number of stored objects.
c72198f1 366 Int_t nobj=0;
367 if (fObjects) nobj=fObjects->GetEntries();
368
369 return nobj;
84bb7c66 370}
371///////////////////////////////////////////////////////////////////////////