]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUON1DArray.cxx
https://savannah.cern.ch/bugs/index.php?98544
[u/mrichter/AliRoot.git] / MUON / AliMUON1DArray.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 #include "AliMUON1DArray.h"
19
20 #include "AliLog.h"
21 #include <TClass.h>
22 #include <TObjArray.h>
23 #include <Riostream.h>
24
25 //-----------------------------------------------------------------------------
26 /// \class AliMUON1DArray
27 /// This class is simply a wrapper to a TObjArray, offering in addition a
28 /// control over the replacement policy when you add
29 /// something to it.
30 ///
31 /// \author Laurent Aphecetche
32 //-----------------------------------------------------------------------------
33
34 /// \cond CLASSIMP
35 ClassImp(AliMUON1DArray)
36 /// \endcond
37
38 //_____________________________________________________________________________
39 AliMUON1DArray::AliMUON1DArray(Int_t theSize)
40 : AliMUONVStore(),
41   fArray(0x0)
42 {
43     /// Default ctor
44
45   if (theSize<=0) theSize=16;
46         
47   fArray = new TObjArray(theSize);
48   fArray->SetOwner(kTRUE);
49 }
50
51 //_____________________________________________________________________________
52 AliMUON1DArray::AliMUON1DArray(const AliMUON1DArray& other)
53 : AliMUONVStore(),
54   fArray(0x0)
55 {
56 /// Copy constructor
57
58     AliDebug(1,Form("this=%p copy ctor",this));
59   other.CopyTo(*this);
60 }
61
62 //_____________________________________________________________________________
63 AliMUON1DArray&
64 AliMUON1DArray::operator=(const AliMUON1DArray& other)
65 {
66 /// Assignment operator
67
68   other.CopyTo(*this);
69   return *this;
70 }
71
72 //_____________________________________________________________________________
73 AliMUON1DArray::~AliMUON1DArray()
74 {
75   /// dtor, we're the owner of our internal array.
76
77   AliDebug(1,Form("this=%p",this));
78   delete fArray;
79 }
80
81 //_____________________________________________________________________________
82 Bool_t
83 AliMUON1DArray::Add(TObject* object)
84 {
85   /// Add an object to this, if its uniqueID is below maxsize
86   if (!object) return kFALSE;
87   
88   Int_t i = (Int_t)object->GetUniqueID();
89   if ( i >= fArray->GetSize() ) 
90   {
91     AliError(Form("Index out of bounds %u (max is %u)",i,fArray->GetSize()));
92     return kFALSE;
93   }
94
95   Set(object->GetUniqueID(),object,kFALSE);
96   return kTRUE;
97 }
98
99 //_____________________________________________________________________________
100 void
101 AliMUON1DArray::Clear(Option_t* opt)
102 {
103   /// Reset
104   fArray->Clear(opt);
105 }
106
107 //_____________________________________________________________________________
108 void
109 AliMUON1DArray::CopyTo(AliMUON1DArray& dest) const
110 {
111 /// Make a deep copy
112
113   delete dest.fArray;
114   dest.fArray = 0;
115   dest.fArray = new TObjArray(fArray->GetSize());
116   dest.fArray->SetOwner(kTRUE);
117   for ( Int_t i = 0; i < fArray->GetLast(); ++i )
118   {
119     dest.fArray->AddAt(fArray->At(i)->Clone(),i);
120   }
121 }
122
123 //_____________________________________________________________________________
124 AliMUON1DArray* 
125 AliMUON1DArray::Create() const 
126 {
127   /// Create an empty clone of this
128   return new AliMUON1DArray(fArray->GetSize());
129 }
130
131 //_____________________________________________________________________________
132 TObject* 
133 AliMUON1DArray::FindObject(UInt_t i) const
134 {
135   /// Get the object located at index i, if it exists, and if i is correct.
136
137   if ( (Int_t)(i) < fArray->GetSize() )
138   {
139     return fArray->At(i);
140   }
141   AliError(Form("Index %d out of bounds (max %d)",i,fArray->GetSize()));
142   return 0x0;
143 }
144
145 //_____________________________________________________________________________
146 TIterator* 
147 AliMUON1DArray::CreateIterator() const
148 {
149   /// Return an iterator on this
150   return fArray->MakeIterator();
151 }
152
153 //_____________________________________________________________________________
154 Bool_t 
155 AliMUON1DArray::Set(Int_t i, TObject* object, Bool_t replace)
156 {
157 /// Set the object located at i
158 /// If replace=kFALSE and there's already an object at location i,
159 /// this method fails and returns kFALSE, otherwise it returns kTRUE
160
161   if ( i >= 0 && i < fArray->GetSize() )
162   {
163     if (((Int_t)(object->GetUniqueID()))!=i)
164     {
165       AliError(Form("object's UniqueID is %d, which is different from the expected %d",
166                     object->GetUniqueID(),i));
167       return kFALSE;
168     }
169     
170     TObject* o = FindObject(i);
171     if ( o && !replace )
172     {
173       AliError(Form("Object %p is already there for i=%d",o,i));
174       return kFALSE;
175     }
176     if ( o && replace ) 
177     {
178       delete o;
179     }
180     fArray->AddAt(object,i);
181     return kTRUE;
182   }
183   AliError(Form("Index %d out of bounds (max %d)",i,fArray->GetSize()));
184   return kFALSE;
185 }
186
187 //_____________________________________________________________________________
188 Int_t 
189 AliMUON1DArray::GetSize() const
190 {
191   /// Return the number of object we hold
192   return fArray->GetEntries();
193 }