]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFArray.cxx
Coverity fixes.
[u/mrichter/AliRoot.git] / TOF / AliTOFArray.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 /* $Id$ */
16
17 // 
18 // Class to hold variable size arrays of Float_t
19 //
20
21 #include <TObject.h>
22 #include <TArrayF.h>
23 #include <TCollection.h>
24 #include "AliTOFArray.h"
25 //#include "AliLog.h"
26
27 ClassImp(AliTOFArray)
28
29 //-------------------------------------------------------------------
30 AliTOFArray::AliTOFArray(Int_t size):
31         TObject(),
32         fSize(size),
33         fArray(new TArrayF*[size]){
34
35         // constructor
36
37         for (Int_t i=0;i<size;i++){
38                 fArray[i] = NULL;
39         }
40 }
41 //-------------------------------------------------------------------
42 AliTOFArray::AliTOFArray(const AliTOFArray & source):
43         TObject(),fSize(0),fArray(0x0){ 
44
45         //
46         // copy constructor
47         //
48
49         this->fSize= source.fSize;
50         this->fArray= source.fArray;
51 }
52
53 //-------------------------------------------------------------------
54 AliTOFArray& AliTOFArray::operator=(const AliTOFArray & source) { 
55
56         //
57         // assignment operator
58         //
59
60         this->fSize= source.fSize;
61         this->fArray= source.fArray;
62         return *this;
63 }
64
65 //------------------------------------------------------------------
66 AliTOFArray::~AliTOFArray(){
67
68         //
69         // Destructor
70         //
71
72         delete [] fArray;
73 }
74
75 //-------------------------------------------------------------------
76 void AliTOFArray::SetArray(Int_t pos, Int_t size) {
77
78         //
79         // adding an array of Float_t with size=size
80         //
81         
82         if (pos>-1 && pos < fSize){
83                 if (!fArray[pos]) {
84                         //                      printf("Creating array\n");
85                         fArray[pos] = new TArrayF();                    
86                 }
87                 fArray[pos]->Reset();
88                 fArray[pos]->Set(size);
89         }
90         else printf("Position out of bounds, returning\n");
91         return;
92 }
93
94 //-------------------------------------------------------------------
95 void AliTOFArray::SetAt(Int_t pos, Int_t nelements, Float_t* content) {
96
97         // pos = index of the array to be modified
98         // nelements = n. of elements to be modified in array 
99         // content = values to be set in array
100
101         if (pos>-1 && pos < fSize){
102                 if (fArray[pos]){
103                         Int_t size = fArray[pos]->GetSize();
104                         if (nelements <= size){
105                                 for (Int_t i=0;i<nelements;i++){
106                                         fArray[pos]->AddAt(content[i],i);
107                                 }
108                         }
109                         else printf("Too many elements to be added, returning without adding any\n");
110                 }
111                 else printf("Non-existing array, returning\n");
112         }
113         else printf("Position out of bounds, returning\n");
114         return;
115 }
116
117 //-------------------------------------------------------------------
118 void AliTOFArray::SetAt(Int_t pos, Int_t ielement, Float_t content) {
119
120         // pos = index of the array to be modified
121         // ielement = index of the element to be modified in array 
122         // content = value to be set in array
123
124         if (pos>-1 && pos < fSize){
125                 if (fArray[pos]){
126                         Int_t size = fArray[pos]->GetSize();
127                         if (ielement < size){
128                                 //printf("Adding %f content in position %d to array %d \n",content, ielement, pos);
129                                 fArray[pos]->AddAt(content,ielement);
130                         }
131                         else if (ielement == size) {
132                                 printf ("Increasing the size by 1 and adding a new element to the array\n");
133                                 fArray[pos]->Set(size+1);
134                                 fArray[pos]->AddAt(content,ielement);
135                         }
136                         else printf("Not possible to add element %d, size of the array too small, and this would not be the next entry!\n",ielement);
137                 }
138                 else printf("Non-existing array, returning\n");
139         }       
140         else printf("Position out of bounds, returning\n");
141         return;
142 }
143
144 //-------------------------------------------------------------------
145 void AliTOFArray::RemoveArray(Int_t pos) {
146
147         //
148         // removing the array at position pos
149         //
150
151         if (fArray[pos]) fArray[pos]->Reset();
152         else printf("Not possible to remove array, array does not exist\n");
153         return;
154 }
155
156 //-------------------------------------------------------------------
157 Float_t* AliTOFArray::GetArray(Int_t pos) {
158
159         //
160         // Getting back array at position pos
161         //
162
163         if  (pos>-1 && pos < fSize){
164                 if (fArray[pos]){
165                         return fArray[pos]->GetArray();
166                 }
167                 else printf("Non-existing array, returning\n");
168         }
169         else printf("Position out of bounds, returning\n");
170         return 0;
171 }
172
173 //-------------------------------------------------------------------
174 Float_t AliTOFArray::GetArrayAt(Int_t pos, Int_t ielement) {
175
176         //
177         // Getting back ielement of array at position pos
178         //
179
180         if  (pos>-1 && pos < fSize){
181                 if (fArray[pos]){
182                         if (ielement<fArray[pos]->GetSize()){
183                                 return fArray[pos]->GetAt(ielement);
184                         }
185                         else printf("Element in array out of bounds, returning\n");
186                 }
187                 else printf("Non-existing array, returning\n");
188         }
189         else printf("Position out of bounds, returning\n");
190         return 0;
191 }
192
193 //-------------------------------------------------------------------
194 void AliTOFArray::ReSetArraySize(Int_t pos, Int_t size) {
195
196         //
197         // Changing size of array at position pos, using TArrayF::Set method
198         // (without loosing what is already there)
199         //
200
201         if  (pos>-1 && pos < fSize){
202                 if (fArray[pos]){
203                         fArray[pos]->Set(size);
204                 }
205                 else printf("Non-existing array, returning\n");
206         }
207         else printf("Position out of bounds, returning\n");
208         return;
209 }
210
211 //-------------------------------------------------------------------
212 Int_t AliTOFArray::GetArraySize(Int_t pos) {
213
214         //
215         // Getting back size of array at position pos
216         //
217
218         if  (pos>-1 && pos < fSize){
219                 if (fArray[pos]){
220                         return fArray[pos]->GetSize();
221                 }
222                 else printf("Non-existing array, returning\n");
223         }
224         else printf("Position out of bounds, returning\n");
225         return -1;
226 }
227
228 //-------------------------------------------------------------------
229 Long64_t AliTOFArray::Merge(TCollection *list){
230
231         //
232         // Merging method
233         //
234         
235         if (!list) return 0;
236         if (list->IsEmpty()) return 1;
237         printf("Merging %d AliTOFArrays %s\n", list->GetSize()+1, GetName());
238         
239         // iterating over the entries in the TList
240         TIter next(list);
241         AliTOFArray *tofArray;
242         Int_t count = 0; // object counter
243         while ((tofArray=(AliTOFArray*)next())) {
244                 //              printf("Count = %d \n",count);
245                 if (!tofArray) continue;
246                 if (tofArray->GetSize() != fSize){
247                         printf("Merging with current entry in list not possible, AliTOFArray in the list has size different from the current one\n");
248                         continue;
249                 }
250                 for (Int_t i = 0; i<fSize; i++){
251                         Float_t* tempArray = tofArray->GetArray(i);
252                         Int_t tempSize = tofArray->GetArraySize(i);
253                         Int_t currentSize = GetArraySize(i);
254                         Int_t mergeSize = currentSize+tempSize;
255                         fArray[i]->Set(mergeSize);
256                         if (tempSize !=0){
257                                 for (Int_t j = currentSize; j<mergeSize; j++){
258                                         SetAt(i,j,tempArray[j-currentSize]);
259                                 }
260                         }                       
261                 }
262                 count++;
263                 printf("Count = %d \n",count);
264                 
265         }
266         return count+1;
267         
268 }
269