]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RALICE/AliObjMatrix.cxx
Protecting if the events are not there
[u/mrichter/AliRoot.git] / RALICE / AliObjMatrix.cxx
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");
42 // m->EnterObject(6,21,s);
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");
50 // m->EnterObject(8,13,s);
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");
58 // m->EnterObject(64,3,s);
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"
80  
81 ClassImp(AliObjMatrix) // Class implementation to enable ROOT I/O
82  
83 AliObjMatrix::AliObjMatrix()
84 {
85 // Default constructor.
86 // Note : The owner and swap mode flags will be initialised to 0.
87 //        See the memberfunctions SetOwner() and SetSwapMode() for further
88 //        details. 
89  fRows=0;
90  fOwn=0;
91  fSwap=0;
92  fMaxrow=0;
93  fMaxcol=0;
94  fNobjects=0;
95 }
96 ///////////////////////////////////////////////////////////////////////////
97 AliObjMatrix::~AliObjMatrix()
98 {
99 // Default destructor.
100  if (fRows)
101  {
102   delete fRows;
103   fRows=0;
104  }
105 }
106 ///////////////////////////////////////////////////////////////////////////
107 void AliObjMatrix::Reset()
108 {
109 // Reset the whole matrix structure.
110 // Note : The values of the owner and swap mode flags will not be modified.
111 //        To modify the ownership, use the memberfunction SetOwner(). 
112 //        To modify the swap mode, use the memberfunction SetSwapMode(). 
113  if (fRows)
114  {
115   delete fRows;
116   fRows=0;
117  }
118
119  fMaxrow=0;
120  fMaxcol=0;
121  fNobjects=0;
122 }
123 ///////////////////////////////////////////////////////////////////////////
124 void AliObjMatrix::SetOwner(Int_t own)
125 {
126 // Set the owner flag (0/1) for the stored objects.
127 // When the owner flag is set to 1, all entered objects are owned by the
128 // matrix structure.
129 // At invokation of this memberfunction the default argument is own=1.
130 //
131  fOwn=own;
132
133  if (!fRows) return;
134
135  for (Int_t irow=0; irow<fRows->GetSize(); irow++)
136  {
137   TObjArray* mrow=(TObjArray*)fRows->At(irow);
138   if (mrow)
139   {
140    if (own)
141    {
142     mrow->SetOwner(kTRUE);
143    }
144    else
145    {
146     mrow->SetOwner(kFALSE);
147    }
148   }
149  }
150 }
151 ///////////////////////////////////////////////////////////////////////////
152 Int_t AliObjMatrix::GetOwner()
153 {
154 // Provide the owner flag for the stored objects.
155  return fOwn;
156 }
157 ///////////////////////////////////////////////////////////////////////////
158 void AliObjMatrix::SetSwapMode(Int_t swap)
159 {
160 // Set the swap mode flag (0/1) for the internal matrix storage.
161 // In case the number of rows differs considerably from the number of columns,
162 // it might be more efficient (w.r.t. memory usage and/or output file size)
163 // to internally store the matrix with the rows and colums swapped.
164 // This swapping is only related with the internal storage and as such
165 // is completely hidden for the user.
166 // At invokation of this memberfunction the default argument is swap=1.
167 //
168 // Note : The swap mode can only be set as long as no objects have
169 //        been stored in the matrix structure (i.e. a new instance
170 //        of AliObjMatrix or after invokation of the Reset() function). 
171 //
172  if (!fRows)
173  {
174   fSwap=swap;
175  }
176  else
177  {
178   cout << " *AliObjMatrix::SetSwapMode* Matrix not empty ==> No action." << endl;
179  }
180 }
181 ///////////////////////////////////////////////////////////////////////////
182 Int_t AliObjMatrix::GetSwapMode()
183 {
184 // Provide the swap mode flag for this matrix.
185  return fSwap;
186 }
187 ///////////////////////////////////////////////////////////////////////////
188 void AliObjMatrix::EnterObject(Int_t row,Int_t col,TObject* obj)
189 {
190 // Enter an object to the matrix structure at location (row,col).
191 // In case the location already contained an object, the existing object
192 // will first be removed before the new object is stored.
193 // According to the status of the copy flag (see the SetCopy() function)
194 // the existing object will also be deleted.
195 // Note : The first location in the matrix is indicated as (1,1).
196  if (row<1 || col<1)
197  {
198   cout << " *AliObjMatrix::AddObject* Invalid argument(s) (row,col) : ("
199        << row << "," << col << ")" << endl;
200   return;
201  }
202
203  if (row>fMaxrow) fMaxrow=row;
204  if (col>fMaxcol) fMaxcol=col;
205
206  Int_t rowx=row;
207  if (fSwap) rowx=col;
208  Int_t colx=col;
209  if (fSwap) colx=row;
210
211  if (!fRows)
212  {
213   fRows=new TObjArray(rowx);
214   fRows->SetOwner();
215  }
216  else
217  {
218   if (rowx > fRows->GetSize()) fRows->Expand(rowx);
219  }
220
221  TObjArray* mrow=(TObjArray*)fRows->At(rowx-1);
222
223  if (!mrow)
224  {
225   TObjArray* columns=new TObjArray(colx);
226   if (fOwn) columns->SetOwner();
227   fRows->AddAt(columns,rowx-1);
228   mrow=columns;
229  }
230  else
231  {
232   if (colx > mrow->GetSize()) mrow->Expand(colx);
233  }
234
235  TObject* old=(TObject*)mrow->At(colx-1);
236  if (!old) fNobjects++;
237  if (old && fOwn) delete old;
238
239  mrow->AddAt(obj,colx-1);
240 }
241 ///////////////////////////////////////////////////////////////////////////
242 TObject* AliObjMatrix::GetObject(Int_t row,Int_t col)
243 {
244 // Provide a pointer to the object stored at the matrix location (row,col).
245 // In case no object was stored at the indicated location or the location
246 // would reside outside the matrix boundaries, a value 0 will be returned.
247 // Note : The first location in the matrix is indicated as (1,1).
248
249  TObject* obj=0;
250
251  if (!fRows || row<1 || col<1) return obj;
252
253
254  Int_t rowx=row;
255  if (fSwap) rowx=col;
256  Int_t colx=col;
257  if (fSwap) colx=row;
258
259  TObjArray* mrow=0;
260  if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
261
262  if (!mrow) return obj;
263
264  if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
265
266  return obj;
267 }
268 ///////////////////////////////////////////////////////////////////////////
269 Int_t AliObjMatrix::GetMaxRow()
270 {
271 // Provide the maximum row number index.
272  return fMaxrow;
273 }
274 ///////////////////////////////////////////////////////////////////////////
275 Int_t AliObjMatrix::GetMaxColumn()
276 {
277 // Provide the maximum column number index.
278  return fMaxcol;
279 }
280 ///////////////////////////////////////////////////////////////////////////
281 Int_t AliObjMatrix::GetNobjects()
282 {
283 // Provide the number of stored objects.
284  return fNobjects;
285 }
286 ///////////////////////////////////////////////////////////////////////////