]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODCaloCells.cxx
Fix for coverity 18322 (hopefully)
[u/mrichter/AliRoot.git] / STEER / AOD / AliAODCaloCells.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, 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 //     AOD class to store calorimeter cell data
20 //     Author: Markus Oldenburg, CERN
21 //-------------------------------------------------------------------------
22
23 #include "AliAODCaloCells.h"
24
25 ClassImp(AliAODCaloCells)
26
27 AliAODCaloCells::AliAODCaloCells() : AliVCaloCells(), fNCells(0), fCellNumber(0), fAmplitude(0), fIsSorted(kTRUE), fType(kUndef)
28 {
29   // default constructor
30 }
31
32 AliAODCaloCells::AliAODCaloCells(const char* name, const char* title, VCells_t ttype) :
33     AliVCaloCells(name, title), fNCells(0), fCellNumber(0), fAmplitude(0), fIsSorted(kTRUE), fType(ttype)
34 {
35   //constructor
36 }
37
38 AliAODCaloCells::AliAODCaloCells(const AliAODCaloCells& cells) :
39     AliVCaloCells(cells),
40     fNCells(cells.fNCells),
41     fCellNumber(0),
42     fAmplitude(0),
43     fIsSorted(cells.fIsSorted),
44     fType(cells.fType)
45 {
46 // Copy constructor
47   fCellNumber = new Short_t[fNCells];
48   fAmplitude  = new Double32_t[fNCells]; 
49   
50   for (Int_t i = 0; i < fNCells; i++) {
51     fCellNumber[i]    = cells.fCellNumber[i];
52     fAmplitude[i]     = cells.fAmplitude[i];
53   }
54 }
55
56 AliAODCaloCells& AliAODCaloCells::operator=(const AliAODCaloCells& source)
57 {
58     // Assignment operator
59   if(this != &source) {
60     AliVCaloCells::operator=(source);
61     
62     if(fNCells != source.fNCells) {
63       delete [] fCellNumber;
64       delete [] fAmplitude;
65       fNCells     = source.fNCells;
66       fCellNumber = new Short_t[fNCells];
67       fAmplitude  = new Double32_t[fNCells];
68     }
69     memcpy(fCellNumber,source.fCellNumber, fNCells*sizeof(Short_t));
70     memcpy(fAmplitude, source.fAmplitude,  fNCells*sizeof(Double32_t));
71     fIsSorted = source.fIsSorted;
72     fType     = source.fType;
73   }
74
75   return *this;
76   
77 }
78
79 AliAODCaloCells::~AliAODCaloCells()
80 {
81   // destructor
82
83   DeleteContainer();
84 }
85
86 void AliAODCaloCells::Clear(const Option_t*)
87 {
88   // clear
89   
90   DeleteContainer();
91 }
92
93 void AliAODCaloCells::Copy(TObject &obj) const {
94   
95   // this overwrites the virtual TOBject::Copy()
96   // to allow run time copying without casting
97   // in AliESDEvent
98   
99   if(this==&obj)return;
100   AliAODCaloCells *robj = dynamic_cast<AliAODCaloCells*>(&obj);
101   if(!robj)return; // not an AliAODCaloCells
102   *robj = *this;
103   
104 }
105
106 AliVCaloCells *AliAODCaloCells::CopyCaloCells(Bool_t all = kTRUE) const {
107   
108   // copy the calo cells into a new object. If option all=FALSE, just the object type, 
109   // for mixing
110   
111   AliVCaloCells *obj =  new AliAODCaloCells();
112   
113   if(all){
114     obj->SetName (GetName()) ; 
115     obj->SetTitle(GetTitle()) ; 
116     obj->SetType (GetType()) ; 
117   
118     obj->SetNumberOfCells(fNCells);
119     for (Short_t i = 0; i < fNCells; i++) 
120       obj->SetCell(i,fCellNumber[i],fAmplitude[i],-1);
121   }
122   
123   return obj;
124   
125 }
126
127 void AliAODCaloCells::CreateContainer(Short_t nCells)
128 {
129   // function that creates container to store calorimeter cell data
130
131   DeleteContainer();
132   
133   if (nCells <= 0) {
134     fNCells = 0;
135     return;
136   }
137
138   fNCells = nCells;
139
140   fCellNumber = new Short_t[fNCells];
141   fAmplitude  = new Double32_t[fNCells];
142
143   // set to zero
144   for(int i = 0;i<fNCells;++i){
145     fAmplitude[i] = fCellNumber[i] = 0 ;
146   }
147 }
148
149 void AliAODCaloCells::DeleteContainer()
150 {
151   // deletes allocated memory
152
153   if (fCellNumber)
154   {
155     delete[] fCellNumber;
156     fCellNumber = NULL;
157   }
158
159   if (fAmplitude)
160   {
161     delete[] fAmplitude;
162     fAmplitude = NULL;
163   }
164   
165   fNCells = 0;
166   fIsSorted = kFALSE;
167 }
168
169 void AliAODCaloCells::Sort() 
170 {
171   // sort the cell array by cell number
172   
173   Int_t *idxArray = new Int_t[fNCells];
174   TMath::Sort(fNCells,fCellNumber,idxArray,kFALSE);
175   
176   Short_t    *newIndex     = new Short_t[fNCells];
177   Double32_t *newAmplitude = new Double32_t[fNCells];
178   for (Int_t i=0; i < fNCells; i++) {
179     newIndex[i]     = fCellNumber[idxArray[i]];
180     newAmplitude[i] = fAmplitude[idxArray[i]];
181   }
182   delete [] fCellNumber;
183   delete [] fAmplitude;
184   fCellNumber = newIndex;
185   fAmplitude  = newAmplitude;
186   
187   delete [] idxArray;
188   
189   fIsSorted = kTRUE;
190
191
192 Bool_t AliAODCaloCells::SetCell(Short_t pos, Short_t cellNumber, Double32_t amplitude, Double32_t /*time*/)
193 {
194   // Sets a cell at the given position
195
196   if (pos>=0 && pos < fNCells) {
197     fCellNumber[pos] = cellNumber;
198     fAmplitude[pos]  = amplitude;
199     fIsSorted = kFALSE;
200     return kTRUE;
201   } else {
202     return kFALSE;
203   }
204 }