]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONClusterStoreV2.cxx
Memory leaks
[u/mrichter/AliRoot.git] / MUON / AliMUONClusterStoreV2.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 AliMUONClusterStoreV2
20 ///
21 /// Implementation of VClusterStore.
22 ///
23 /// Note that clusters are identified by their UniqueID, so it MUST be correctly set
24 ///
25 /// \author Philippe Pillot, Subatech
26 ///
27 //-----------------------------------------------------------------------------
28
29 #include "AliMUONClusterStoreV2.h"
30
31 #include "AliMUONRawClusterV2.h"
32 #include "AliMUONClusterStoreV2Iterator.h"
33 #include "AliMUONTreeManager.h"
34 #include "AliMpConstants.h"
35 #include "AliMpExMap.h"
36
37 #include "AliLog.h"
38
39 #include <TTree.h>
40
41 #include <Riostream.h>
42
43 /// \cond CLASSIMP
44 ClassImp(AliMUONClusterStoreV2)
45 /// \endcond
46
47 //_____________________________________________________________________________
48 AliMUONClusterStoreV2::AliMUONClusterStoreV2() 
49 : AliMUONVClusterStore(), 
50   fClusters(new TClonesArray("AliMUONRawClusterV2",100)),
51   fMap(0x0),
52   fMapped(kFALSE)
53 {
54   /// Constructor
55 }
56
57 //_____________________________________________________________________________
58 AliMUONClusterStoreV2::AliMUONClusterStoreV2(const AliMUONClusterStoreV2& store)
59 : AliMUONVClusterStore(), 
60   fClusters(new TClonesArray(*(store.fClusters))),
61   fMap(0x0),
62   fMapped(kFALSE)
63 {
64   /// Copy constructor
65   if (store.fMapped) ReMap();
66 }
67
68 //_____________________________________________________________________________
69 AliMUONClusterStoreV2& AliMUONClusterStoreV2::operator=(const AliMUONClusterStoreV2& store)
70 {
71   /// Assignment operator
72   fClusters = new TClonesArray(*(store.fClusters));
73   fMap = 0x0;
74   fMapped = kFALSE;
75   if (store.fMapped) ReMap();
76   return *this;
77 }
78
79 //_____________________________________________________________________________
80 AliMUONClusterStoreV2::~AliMUONClusterStoreV2()
81 {
82   /// Destructor
83   delete fClusters;
84   delete fMap;
85 }
86
87 //_____________________________________________________________________________
88 void AliMUONClusterStoreV2::Clear(Option_t*)
89 {
90   /// Clear the internal cluster array AND the index
91   fClusters->Clear("C");
92   if (fMap) {
93     fMap->Clear("C");
94     fMapped = kFALSE;
95   }
96 }
97
98 //_____________________________________________________________________________
99 Bool_t AliMUONClusterStoreV2::Connect(TTree& tree, Bool_t alone) const
100 {
101   /// Connect this to the tree, i.e. make the branches or set their addresses.
102   
103   AliMUONTreeManager tman;
104   
105   if (tree.GetBranch("MUONRawClusters")) {
106     
107     if (alone) tman.UpdateBranchStatuses(tree,"MUONRawClusters");
108     
109     return tman.SetAddress(tree,"MUONRawClusters", 
110                          const_cast<TClonesArray**>(&fClusters));
111   } else {
112     
113     return tman.MakeBranch(tree,ClassName(),"TClonesArray", "MUONRawClusters",
114                          const_cast<TClonesArray**>(&fClusters));
115   }
116     
117 }
118
119 //_____________________________________________________________________________
120 AliMUONVCluster* AliMUONClusterStoreV2::CreateCluster(Int_t chamberId, Int_t detElemId, Int_t clusterIndex) const
121 {
122   /// Create a cluster
123   return new AliMUONRawClusterV2(chamberId, detElemId, clusterIndex);
124 }
125
126 //_____________________________________________________________________________
127 AliMUONVCluster* AliMUONClusterStoreV2::Add(const AliMUONVCluster& vCluster)
128 {
129   /// Add a cluster to this store
130   const AliMUONRawClusterV2* cluster = dynamic_cast<const AliMUONRawClusterV2*>(&vCluster);
131   
132   if (!cluster) {
133     AliError(Form("Cluster is not of the expected type (%s vs AliMUONRawClusterV2)",
134                   vCluster.ClassName()));
135     return 0x0;
136   }
137   
138   // check chamberId
139   Int_t chamberId = cluster->GetChamberId();
140   if (chamberId < 0 || chamberId >= AliMpConstants::NofTrackingChambers()) {
141     AliError(Form("ChamberId (%d) out of boundaries [0,%d[",chamberId,AliMpConstants::NofTrackingChambers()));
142     return 0x0;
143   }
144   
145   // check that there is no cluster with the same Id
146   AliMUONVCluster *c = FindObject(cluster->GetUniqueID());
147   if (c) {
148     AliError("cluster store already contains a cluster with the same ID --> add() aborted:");
149     c->Print("FULL");
150     return 0x0;
151   }
152   
153   // add new cluster
154   c = new((*fClusters)[fClusters->GetLast()+1]) AliMUONRawClusterV2(*cluster);
155   
156   if (c) UpdateMap(*c);
157   
158   return c;
159 }
160
161 //_____________________________________________________________________________
162 AliMUONVCluster* AliMUONClusterStoreV2::Add(Int_t chamberId, Int_t detElemId, Int_t clusterIndex)
163 {
164   /// Add an empty cluster with an unique ID to this store
165   
166   // check chamberId
167   if (chamberId < 0 || chamberId >= AliMpConstants::NofTrackingChambers()) {
168     AliError(Form("ChamberId (%d) out of boundaries [0,%d[",chamberId,AliMpConstants::NofTrackingChambers()));
169     return 0x0;
170   }
171   
172   // check that there is no cluster with the same Id
173   AliMUONVCluster *c = FindObject(AliMUONVCluster::BuildUniqueID(chamberId, detElemId, clusterIndex));
174   if (c) {
175     AliError("cluster store already contains a cluster with the same ID --> add() aborted:");
176     c->Print("FULL");
177     return 0x0;
178   }
179   
180   // add new cluster
181   c = new((*fClusters)[fClusters->GetLast()+1]) AliMUONRawClusterV2(chamberId, detElemId, clusterIndex);
182   
183   if (c) UpdateMap(*c);
184   
185   return c;
186 }
187
188 //_____________________________________________________________________________
189 AliMUONVCluster* AliMUONClusterStoreV2::Remove(AliMUONVCluster& cluster)
190 {
191   /// Remove a cluster
192   AliMUONVCluster* c = static_cast<AliMUONVCluster*>(fClusters->Remove(&cluster));
193   
194   if (c) 
195   {
196     fClusters->Compress();
197     fMapped = kFALSE;
198   }
199   else
200   {
201     AliError("Could not remove cluster from array");
202   }
203   
204   return c;
205 }
206
207 //_____________________________________________________________________________
208 void AliMUONClusterStoreV2::ReMap()
209 {
210   /// Recompute the fMap, which map (ch) to an index within the fClusters array
211   fMapped = kTRUE;
212   
213   // Create (or clear) the TClonesArray of map
214   Int_t nChamber = AliMpConstants::NofTrackingChambers();
215   
216   if (!fMap) {
217     fMap = new TClonesArray("AliMpExMap",nChamber);
218     
219     // Create one map per chamber
220     AliMpExMap *map;
221     for (Int_t chamber=0; chamber<nChamber; chamber++) {
222       map = new((*fMap)[chamber]) AliMpExMap(kTRUE);
223       map->SetOwner(kFALSE);
224     }
225   }
226   else {
227     for (Int_t chamber=0; chamber<nChamber; chamber++) {
228       AliMpExMap *map = static_cast<AliMpExMap *>(fMap->At(chamber));
229       map->Clear("C");
230     }
231   }  
232
233   // Fill the maps
234   TIter next(fClusters);
235   AliMUONVCluster* cluster;
236   while ( (cluster = static_cast<AliMUONVCluster*>(next())) ) UpdateMap(*cluster);
237 }
238
239 //_____________________________________________________________________________
240 void AliMUONClusterStoreV2::UpdateMap(AliMUONVCluster& cluster)
241 {
242   /// Update the internal index given this new cluster
243   if (fMapped) static_cast<AliMpExMap*>(fMap->UncheckedAt(cluster.GetChamberId()))->Add(cluster.GetUniqueID(),&cluster);
244   else ReMap();
245 }
246
247 //_____________________________________________________________________________
248 AliMUONVCluster* AliMUONClusterStoreV2::FindObject(const TObject* object) const
249 {
250   /// Find an object, if of AliMUONVCluster type.
251   const AliMUONVCluster* cluster = dynamic_cast<const AliMUONVCluster*>(object);
252   if (cluster) return FindObject(cluster->GetUniqueID());
253   return 0x0;
254 }
255
256 //_____________________________________________________________________________
257 AliMUONVCluster* AliMUONClusterStoreV2::FindObject(UInt_t uniqueID) const
258 {
259   /// Find a cluster by its UniqueID
260   if (!fMapped) (const_cast<AliMUONClusterStoreV2*>(this))->ReMap();
261   AliMpExMap* map = static_cast<AliMpExMap*>(fMap->UncheckedAt(AliMUONVCluster::GetChamberId(uniqueID)));
262   return static_cast<AliMUONVCluster*>(map->GetValue(uniqueID));
263 }
264
265 //_____________________________________________________________________________
266 TIterator* AliMUONClusterStoreV2::CreateIterator() const
267 {
268   /// Return an iterator to loop over all clusters
269   return fClusters->MakeIterator();
270 }
271
272 //_____________________________________________________________________________
273 TIterator* AliMUONClusterStoreV2::CreateChamberIterator(Int_t firstChamber, Int_t lastChamber) const
274 {
275   /// Return an iterator to loop over clusters in the chambers within the given range
276   
277   // check validity of given chamber IDs
278   if (firstChamber < 0 || firstChamber >= AliMpConstants::NofTrackingChambers()) {
279     AliError(Form("First chamber out of boundaries [0,%d[", AliMpConstants::NofTrackingChambers()));
280     return 0x0;
281   }
282   if (lastChamber < 0 || lastChamber >= AliMpConstants::NofTrackingChambers()) {
283     AliError(Form("Last chamber out of boundaries [0,%d[", AliMpConstants::NofTrackingChambers()));
284     return 0x0;
285   }
286   
287   if (!fMapped) (const_cast<AliMUONClusterStoreV2*>(this))->ReMap();
288   return new AliMUONClusterStoreV2Iterator(this,firstChamber,lastChamber);
289 }