1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////
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.
28 // AliObjMatrix* matrix=new AliObjMatrix();
29 // matrix->SetOwner();
30 // matrix->SetSwapMode();
41 // s->SetPosition(pos,"car");
42 // matrix->EnterObject(6,21,s);
45 // s->SetSignal(25.84);
49 // s->SetPosition(pos,"car");
50 // matrix->EnterObject(8,13,s);
53 // s->SetSignal(87.25);
57 // s->SetPosition(pos,"car");
58 // matrix->EnterObject(64,3,s);
60 // Int_t nrows=matrix->GetMaxRow();
61 // Int_t ncols=matrix->GetMaxColumn();
63 // cout << " Maxrow : " << nrows << " Maxcol : " << ncols
64 // << " Nobjects : " << matrix->GetNobjects() << endl;
66 // for (Int_t i=1; i<=nrows; i++)
68 // for (Int_t j=1; j<=ncols; j++)
70 // s=(AliSignal*)matrix->GetObject(i,j);
71 // if (s) cout << " At (" << i << "," << j << ") Signal : " << s->GetSignal() << endl;
75 //--- Author: Nick van Eijndhoven 23-jan-2003 Utrecht University
76 //- Modified: NvE $Date$ Utrecht University
77 ///////////////////////////////////////////////////////////////////////////
79 #include "AliObjMatrix.h"
80 #include "Riostream.h"
82 ClassImp(AliObjMatrix) // Class implementation to enable ROOT I/O
84 AliObjMatrix::AliObjMatrix() : TObject()
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
97 ///////////////////////////////////////////////////////////////////////////
98 AliObjMatrix::~AliObjMatrix()
100 // Default destructor.
112 ///////////////////////////////////////////////////////////////////////////
113 void AliObjMatrix::Reset()
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().
133 ///////////////////////////////////////////////////////////////////////////
134 void AliObjMatrix::SetOwner(Int_t own)
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
139 // At invokation of this memberfunction the default argument is own=1.
145 for (Int_t irow=0; irow<fRows->GetSize(); irow++)
147 TObjArray* mrow=(TObjArray*)fRows->At(irow);
152 mrow->SetOwner(kTRUE);
156 mrow->SetOwner(kFALSE);
161 ///////////////////////////////////////////////////////////////////////////
162 Int_t AliObjMatrix::GetOwner()
164 // Provide the owner flag for the stored objects.
167 ///////////////////////////////////////////////////////////////////////////
168 void AliObjMatrix::SetSwapMode(Int_t swap)
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.
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).
188 cout << " *AliObjMatrix::SetSwapMode* Matrix not empty ==> No action." << endl;
191 ///////////////////////////////////////////////////////////////////////////
192 Int_t AliObjMatrix::GetSwapMode()
194 // Provide the swap mode flag for this matrix.
197 ///////////////////////////////////////////////////////////////////////////
198 void AliObjMatrix::EnterObject(Int_t row,Int_t col,TObject* obj)
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.
203 // According to the status of the owner flag (see the SetOwner() function)
204 // the existing object will also be deleted.
205 // Note : The first location in the matrix is indicated as (1,1).
208 cout << " *AliObjMatrix::AddObject* Invalid argument(s) (row,col) : ("
209 << row << "," << col << ")" << endl;
213 if (row>fMaxrow) fMaxrow=row;
214 if (col>fMaxcol) fMaxcol=col;
223 fRows=new TObjArray(rowx);
228 if (rowx > fRows->GetSize()) fRows->Expand(rowx);
231 TObjArray* mrow=(TObjArray*)fRows->At(rowx-1);
235 TObjArray* columns=new TObjArray(colx);
236 if (fOwn) columns->SetOwner();
237 fRows->AddAt(columns,rowx-1);
242 if (colx > mrow->GetSize()) mrow->Expand(colx);
245 TObject* old=(TObject*)mrow->At(colx-1);
248 fObjects->Remove(old);
249 fObjects->Compress();
250 if (fOwn) delete old;
253 mrow->AddAt(obj,colx-1);
255 if (!fObjects) fObjects=new TObjArray();
258 ///////////////////////////////////////////////////////////////////////////
259 void AliObjMatrix::RemoveObject(Int_t row,Int_t col)
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.
264 // Note : The first location in the matrix is indicated as (1,1).
268 if (!fRows || row<1 || col<1) return;
277 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
281 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
285 fObjects->Remove(obj);
286 fObjects->Compress();
288 if (fOwn) delete obj;
291 ///////////////////////////////////////////////////////////////////////////
292 void AliObjMatrix::RemoveObject(TObject* obj,Int_t row,Int_t col)
294 // Remove an object from the matrix according to user specified selections.
295 // In case the object was owned by the matrix, it will be deleted.
297 // An object is only removed from the matrix if the stored reference matches
298 // the argument "obj".
299 // In case obj=0 no check on the matching of the stored reference is performed
300 // and the stored object is always removed in accordance with the other
301 // selection criteria.
303 // In case the argument "row" is specified, only the object references from
304 // that matrix row will be deleted.
305 // In case row=0 (default) no checking on the row index is performed.
307 // In case the argument "col" is specified, only the object references from
308 // that matrix column will be deleted.
309 // In case col=0 (default) no checking on the column index is performed.
311 // So, invokation of RemoveObject(obj) will remove all references to the
312 // object "obj" from the total matrix, whereas RemoveObject(obj,0,col)
313 // will remove all references to the object "obj" only from column "col".
317 // The first location in the matrix is indicated as (1,1).
319 // Invokation of RemoveObject(0,row,col) is equivalent to invoking the
320 // memberfunction RemoveObject(row,col).
321 // Invoking the latter directly is slightly faster.
323 // Invokation of RemoveObject(0) is equivalent to invoking Reset().
324 // Invoking the latter directly is slightly faster.
334 RemoveObject(row,col);
338 TObject* objx=GetObject(row,col);
339 if (objx==obj) RemoveObject(row,col);
353 nrefs=GetIndices(obj,rows,cols);
357 if (row && !col) nrefs=GetIndices(obj,row,cols);
358 if (!row && col) nrefs=GetIndices(obj,rows,col);
360 // Remove the selected objects based on the obtained row and column indices
362 for (Int_t i=0; i<nrefs; i++)
365 if (!irow) irow=rows.At(i);
367 if (!icol) icol=cols.At(i);
368 RemoveObject(irow,icol);
371 ///////////////////////////////////////////////////////////////////////////
372 TObject* AliObjMatrix::GetObject(Int_t row,Int_t col)
374 // Provide a pointer to the object stored at the matrix location (row,col).
375 // In case no object was stored at the indicated location or the location
376 // would reside outside the matrix boundaries, a value 0 will be returned.
377 // Note : The first location in the matrix is indicated as (1,1).
381 if (!fRows || row<1 || col<1) return obj;
390 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
392 if (!mrow) return obj;
394 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
398 ///////////////////////////////////////////////////////////////////////////
399 TObject* AliObjMatrix::GetObject(Int_t j)
401 // Provide a pointer to the j-th stored object.
402 // In case the index j is invalid, a value 0 will be returned.
403 // The first stored object is indicated as j=1.
405 // Note : Do NOT delete the object.
406 // To remove an object, the memberfunction RemoveObject()
411 if (fObjects) nobj=fObjects->GetSize();
413 if (j>0 && j<=nobj) obj=(TObject*)fObjects->At(j-1);
417 ///////////////////////////////////////////////////////////////////////////
418 TObjArray* AliObjMatrix::GetObjects()
420 // Provide references to all the stored objects.
421 // In case no objects are present, a value 0 will be returned.
423 // Note : Do NOT make any changes to the reference array apart from
424 // changing the order of the pointers of the various objects.
425 // For addition or removal of objects, the memberfunctions
426 // EnterObject() and RemoveObject() should be used.
430 ///////////////////////////////////////////////////////////////////////////
431 Int_t AliObjMatrix::GetMaxRow()
433 // Provide the maximum row number index.
436 ///////////////////////////////////////////////////////////////////////////
437 Int_t AliObjMatrix::GetMaxColumn()
439 // Provide the maximum column number index.
442 ///////////////////////////////////////////////////////////////////////////
443 Int_t AliObjMatrix::GetNobjects()
445 // Provide the number of stored objects.
447 if (fObjects) nobj=fObjects->GetEntries();
451 ///////////////////////////////////////////////////////////////////////////
452 Int_t AliObjMatrix::GetNrefs(TObject* obj)
454 // Provide the number of stored references to the specified object.
455 // If obj=0 the total number of stored references for all objects is returned.
456 Int_t nobjs=GetNobjects();
458 if (!obj) return nobjs;
461 for (Int_t i=1; i<=nobjs; i++)
463 TObject* objx=GetObject(i);
464 if (objx==obj) nrefs++;
468 ///////////////////////////////////////////////////////////////////////////
469 Int_t AliObjMatrix::GetIndices(TObject* obj,TArrayI& rows,TArrayI& cols)
471 // Provide the (row,col) indices of all the storage locations of the
473 // The row and column indices are returned in the two separate TArrayI arrays
474 // from which the (row,col) pairs can be obtained from the corresponding
475 // array indices like (row,col)=(rows.At(j),cols.At(j)).
476 // The integer return argument represents the number of (row,col) pairs which
477 // were encountered for the specified object.
479 // If obj=0 no object selection is performed and all (row,col) indices
480 // of the stored references for all objects are returned.
484 // As usual the convention is that row and column numbering starts at 1.
486 // This memberfunction always resets the two TArrayI arrays at the start.
488 // This memberfunction can only be used to obtain the (row,col) indices
489 // of the object as stored via the EnterObject() memberfunction.
490 // This means that in case the user has entered a TObjArray as object
491 // (to increase the dimension of the resulting structure), the (row,col)
492 // indices of that TObjArray are obtained and NOT the indices of the
493 // actual objects contained in that TObjArray structure.
495 Int_t nrefs=GetNrefs(obj);
500 if (!nrefs) return 0;
504 for (Int_t i=0; i<fRows->GetSize(); i++)
506 TObjArray* columns=(TObjArray*)fRows->At(i);
507 if (!columns) continue;
509 for (Int_t j=0; j<columns->GetSize(); j++)
511 TObject* objx=(TObject*)columns->At(j);
512 if (objx && (objx==obj || !obj))
518 rows.AddAt(irow,jref);
519 cols.AddAt(icol,jref);
522 // All references found ==> Done
523 if (jref==nrefs) break;
525 // All references found ==> Done
526 if (jref==nrefs) break;
530 ///////////////////////////////////////////////////////////////////////////
531 Int_t AliObjMatrix::GetIndices(TObject* obj,Int_t row,TArrayI& cols)
533 // Provide the column indices of all the storage locations of the
534 // specified object in the specified row of the matrix.
535 // The column indices are returned in the TArrayI array.
536 // The integer return argument represents the number of storage locations which
537 // were encountered for the specified object in the specified matrix row.
539 // If obj=0 no object selection is performed and all column indices
540 // of the stored references for all objects are returned.
544 // As usual the convention is that row and column numbering starts at 1.
546 // This memberfunction always resets the TArrayI array at the start.
548 // This memberfunction can only be used to obtain the column indices
549 // of the object as stored via the EnterObject() memberfunction.
550 // This means that in case the user has entered a TObjArray as object
551 // (to increase the dimension of the resulting structure), the column
552 // indices of that TObjArray are obtained and NOT the indices of the
553 // actual objects contained in that TObjArray structure.
555 if (row<1 || row>GetMaxRow()) return 0;
557 Int_t nrefs=GetNrefs(obj);
560 if (!nrefs) return 0;
564 for (Int_t i=0; i<fRows->GetSize(); i++)
566 TObjArray* columns=(TObjArray*)fRows->At(i);
567 if (!columns) continue;
569 for (Int_t j=0; j<columns->GetSize(); j++)
571 TObject* objx=(TObject*)columns->At(j);
572 if (objx && (objx==obj || !obj))
580 cols.AddAt(icol,jref);
584 // All references found ==> Done
585 if (jref==nrefs) break;
587 // All references found ==> Done
588 if (jref==nrefs) break;
590 // Set the array size to the actual number of found occurrences
595 ///////////////////////////////////////////////////////////////////////////
596 Int_t AliObjMatrix::GetIndices(TObject* obj,TArrayI& rows,Int_t col)
598 // Provide the row indices of all the storage locations of the
599 // specified object in the specified column of the matrix.
600 // The row indices are returned in the TArrayI array.
601 // The integer return argument represents the number of storage locations which
602 // were encountered for the specified object in the specified matrix column.
604 // If obj=0 no object selection is performed and all row indices
605 // of the stored references for all objects are returned.
609 // As usual the convention is that row and column numbering starts at 1.
611 // This memberfunction always resets the TArrayI array at the start.
613 // This memberfunction can only be used to obtain the row indices
614 // of the object as stored via the EnterObject() memberfunction.
615 // This means that in case the user has entered a TObjArray as object
616 // (to increase the dimension of the resulting structure), the row
617 // indices of that TObjArray are obtained and NOT the indices of the
618 // actual objects contained in that TObjArray structure.
620 if (col<1 || col>GetMaxColumn()) return 0;
622 Int_t nrefs=GetNrefs(obj);
625 if (!nrefs) return 0;
629 for (Int_t i=0; i<fRows->GetSize(); i++)
631 TObjArray* columns=(TObjArray*)fRows->At(i);
632 if (!columns) continue;
634 for (Int_t j=0; j<columns->GetSize(); j++)
636 TObject* objx=(TObject*)columns->At(j);
637 if (objx && (objx==obj || !obj))
645 rows.AddAt(irow,jref);
649 // All references found ==> Done
650 if (jref==nrefs) break;
652 // All references found ==> Done
653 if (jref==nrefs) break;
655 // Set the array size to the actual number of found occurrences
660 ///////////////////////////////////////////////////////////////////////////