]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONClusterStoreV2.cxx
from Y. Schutz
[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 Bool_t 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 kFALSE;
136   }
137   
138   // check that there is no cluster with the same Id
139   if (FindObject(cluster->GetUniqueID())) {
140     AliError("cluster store already contains a cluster with the same ID --> add() aborted");
141     return kFALSE;
142   }
143   
144   // add new cluster
145   AliMUONVCluster *c = new((*fClusters)[fClusters->GetLast()+1]) AliMUONRawClusterV2(*cluster);
146   
147   if (c) UpdateMap(*c);
148   
149   return kTRUE;
150 }
151
152 //_____________________________________________________________________________
153 AliMUONVCluster* AliMUONClusterStoreV2::Add(Int_t chamberId, Int_t detElemId, Int_t clusterIndex)
154 {
155   /// Add an empty cluster with an unique ID to this store
156   
157   // check that there is no cluster with the same Id
158   AliMUONVCluster *c = FindObject(AliMUONVCluster::BuildUniqueID(chamberId, detElemId, clusterIndex));
159   if (c) {
160     AliError("cluster store already contains a cluster with the same ID --> add() aborted:");
161     c->Print("FULL");
162     return 0x0;
163   }
164   
165   // add new cluster
166   c = new((*fClusters)[fClusters->GetLast()+1]) AliMUONRawClusterV2(chamberId, detElemId, clusterIndex);
167   
168   if (c) UpdateMap(*c);
169   
170   return c;
171 }
172
173 //_____________________________________________________________________________
174 AliMUONVCluster* AliMUONClusterStoreV2::Remove(AliMUONVCluster& cluster)
175 {
176   /// Remove a cluster
177   AliMUONVCluster* c = static_cast<AliMUONVCluster*>(fClusters->Remove(&cluster));
178   
179   if (c) {
180     fClusters->Compress();
181     fMapped = kFALSE;
182   }
183   
184   return c;
185 }
186
187 //_____________________________________________________________________________
188 void AliMUONClusterStoreV2::ReMap()
189 {
190   /// Recompute the fMap, which map (ch) to an index within the fClusters array
191   fMapped = kTRUE;
192   
193   // Create (or clear) the TClonesArray of map
194   Int_t nChamber = AliMpConstants::NofTrackingChambers();
195   if (!fMap) fMap = new TClonesArray("AliMpExMap",nChamber);
196   else fMap->Clear("C");
197   
198   // Create one map per chamber
199   AliMpExMap *map;
200   for (Int_t chamber=0; chamber<nChamber; chamber++) {
201     map = new((*fMap)[chamber]) AliMpExMap(kTRUE);
202     map->SetOwner(kFALSE);
203   }
204   
205   // Fill the maps
206   TIter next(fClusters);
207   AliMUONVCluster* cluster;
208   while ( (cluster = static_cast<AliMUONVCluster*>(next())) ) UpdateMap(*cluster);
209 }
210
211 //_____________________________________________________________________________
212 void AliMUONClusterStoreV2::UpdateMap(AliMUONVCluster& cluster)
213 {
214   /// Update the internal index given this new cluster
215   if (fMapped) static_cast<AliMpExMap*>(fMap->UncheckedAt(cluster.GetChamberId()))->Add(cluster.GetUniqueID(),&cluster);
216   else ReMap();
217 }
218
219 //_____________________________________________________________________________
220 AliMUONVCluster* AliMUONClusterStoreV2::FindObject(const TObject* object) const
221 {
222   /// Find an object, if of AliMUONVCluster type.
223   const AliMUONVCluster* cluster = dynamic_cast<const AliMUONVCluster*>(object);
224   if (cluster) return FindObject(cluster->GetUniqueID());
225   return 0x0;
226 }
227
228 //_____________________________________________________________________________
229 AliMUONVCluster* AliMUONClusterStoreV2::FindObject(UInt_t uniqueID) const
230 {
231   /// Find a cluster by its UniqueID
232   if (!fMapped) (const_cast<AliMUONClusterStoreV2*>(this))->ReMap();
233   AliMpExMap* map = static_cast<AliMpExMap*>(fMap->UncheckedAt(AliMUONVCluster::GetChamberId(uniqueID)));
234   return static_cast<AliMUONVCluster*>(map->GetValue(uniqueID));
235 }
236
237 //_____________________________________________________________________________
238 TIterator* AliMUONClusterStoreV2::CreateIterator() const
239 {
240   /// Return an iterator to loop over all clusters
241   return fClusters->MakeIterator();
242 }
243
244 //_____________________________________________________________________________
245 TIterator* AliMUONClusterStoreV2::CreateChamberIterator(Int_t firstChamber, Int_t lastChamber) const
246 {
247   /// Return an iterator to loop over clusters in the chambers within the given range
248   
249   // check validity of given chamber IDs
250   if (firstChamber < 0 || firstChamber >= AliMpConstants::NofTrackingChambers()) {
251     AliError(Form("First chamber out of boundaries [0,%d[", AliMpConstants::NofTrackingChambers()));
252     return 0x0;
253   }
254   if (lastChamber < 0 || lastChamber >= AliMpConstants::NofTrackingChambers()) {
255     AliError(Form("Last chamber out of boundaries [0,%d[", AliMpConstants::NofTrackingChambers()));
256     return 0x0;
257   }
258   
259   if (!fMapped) (const_cast<AliMUONClusterStoreV2*>(this))->ReMap();
260   return new AliMUONClusterStoreV2Iterator(this,firstChamber,lastChamber);
261 }