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