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