4 // Class AliMpNeighboursPadIterator
5 // --------------------------------
6 // Class, which defines an iterator over the pads surrounding a given pad
7 // Included in AliRoot: 2003/05/02
8 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
12 #include "AliMpNeighboursPadIterator.h"
13 #include "AliMpIntPair.h"
14 #include "AliMpSectorSegmentation.h"
16 #include "AliMpConstants.h"
18 ClassImp(AliMpNeighboursPadIterator)
20 const UInt_t AliMpNeighboursPadIterator::fgkInvalidIndex = 9999;
21 //never so much neighbours...
23 //______________________________________________________________________________
24 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator()
25 : AliMpVPadIterator(),
27 fCenterPad(AliMpPad::Invalid()),
29 fIndex(fgkInvalidIndex)
31 // default constructor, set the current position to "invalid"
34 //______________________________________________________________________________
35 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator(
36 const AliMpSectorSegmentation* segmentation,
37 const AliMpPad& centerPad,
39 : AliMpVPadIterator(),
40 fkSegmentation(segmentation),
41 fCenterPad(centerPad),
42 fIndex(fgkInvalidIndex)
44 // normal constructor, set *this to invalid position
46 FillPadsVector(includeCenter);
49 //______________________________________________________________________________
50 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator(
51 const AliMpNeighboursPadIterator& right)
52 : AliMpVPadIterator(right)
59 //______________________________________________________________________________
60 AliMpNeighboursPadIterator::~AliMpNeighboursPadIterator()
71 //______________________________________________________________________________
72 AliMpNeighboursPadIterator&
73 AliMpNeighboursPadIterator::operator = (const AliMpNeighboursPadIterator& right)
75 // assignement operator
76 // if the right hand iterator isn't of good type
77 // the current operator is invalidated
79 // check assignement to self
80 if (this == &right) return *this;
82 // base class assignement
83 AliMpVPadIterator::operator=(right);
86 fkSegmentation = right.fkSegmentation;
87 fCenterPad = right.fCenterPad;
89 fIndex = right.fIndex;
92 Fatal("operator=", "Not allowed assignment for TObjArray");
100 //______________________________________________________________________________
101 Bool_t AliMpNeighboursPadIterator::IsNeighbours(const AliMpPad& pad) const
103 // true if the pad located by <padIndice> is a neighbours of those
104 // located at <fCenterPad>
107 TVector2 relPos = pad.Position() - fCenterPad.Position();
108 TVector2 bounds = pad.Dimensions() + fCenterPad.Dimensions();
109 return (TMath::Abs(relPos.X())- bounds.X()<AliMpConstants::LengthTolerance()) &&
110 (TMath::Abs(relPos.Y())- bounds.Y()<AliMpConstants::LengthTolerance());
115 //______________________________________________________________________________
116 PadVector AliMpNeighboursPadIterator::PadVectorLine(const AliMpPad& from,
117 const AliMpIntPair& direction) const
119 // Fill a new vector with all pads which have common
120 // parts with the pad located at <fCenterPad>, in a given line
121 // starting from <from> and moving by <direction>
123 AliMpPad current = from;
127 if (IsNeighbours(current))
128 ans.push_back(current);
131 TVector2 nextPos = current.Position() + TVector2(
132 current.Dimensions().X()*(AliMpConstants::LengthStep()+1.)*direction.GetFirst(),
133 current.Dimensions().Y()*(AliMpConstants::LengthStep()+1.)*direction.GetSecond());
134 current = fkSegmentation->PadByPosition(nextPos);
139 //______________________________________________________________________________
140 void AliMpNeighboursPadIterator::UpdateTotalSet(PadSet& setTotal,
141 const PadVector& from) const
143 // Add pads from pad vector to the total set
144 // only if they are not yet included
146 setTotal.insert(from.begin(),from.end());
151 //______________________________________________________________________________
152 PadVector* AliMpNeighboursPadIterator::PadVectorLine(const AliMpPad& from,
153 const AliMpIntPair& direction) const
155 // Fill a new vector with all pads which have common
156 // parts with the pad located at <fCenterPad>, in a given line
157 // starting from <from> and moving by <direction>
159 AliMpPad current = from;
160 PadVector* ans = new PadVector();
163 if (IsNeighbours(current))
164 ans->Add(new AliMpPad(current));
167 TVector2 nextPos = current.Position() + TVector2(
168 current.Dimensions().X()*(AliMpConstants::LengthStep()+1.)*direction.GetFirst(),
169 current.Dimensions().Y()*(AliMpConstants::LengthStep()+1.)*direction.GetSecond());
170 current = fkSegmentation->PadByPosition(nextPos);
175 //______________________________________________________________________________
176 void AliMpNeighboursPadIterator::UpdateTotalSet(PadSet& setTotal,
177 PadVector* from) const
179 // Add pads from pad vector to the total set
180 // only if they are not yet included and deletes the pad vector
182 for (Int_t i=0; i<from->GetEntriesFast(); i++) {
183 AliMpPad* candidate = (AliMpPad*)from->At(i);
185 Bool_t isInSetTotal = false;
186 for (Int_t j=0; j<setTotal.GetEntriesFast(); j++) {
187 AliMpPad* pad = (AliMpPad*)setTotal.At(j);
189 if (pad->GetIndices() == candidate->GetIndices()) {
195 setTotal.Add(candidate);
204 //______________________________________________________________________________
205 void AliMpNeighboursPadIterator::FillPadsVector(Bool_t includeCenter)
207 // Fill the indices vector with all indices of pads which have common
208 // parts with the pad located at <fCenterPad>
210 if (!fkSegmentation || !fCenterPad.IsValid()) return;
214 AliMpIntPair direction;
222 // repare a unique simple associative container
223 // --> no doublons, rapid insersion
226 ///////////// Left side
228 ////////////////// up direction
230 from = fkSegmentation->PadsLeft(fCenterPad).GetFirst();
231 direction = AliMpIntPair(0,1);
232 found = PadVectorLine(from,direction);
233 UpdateTotalSet(setTotal, found);
235 ////////////////// down direction
237 from = fkSegmentation->PadsDown(from).GetFirst(); // the Pad down is already added
238 direction = AliMpIntPair(0,-1);
239 found = PadVectorLine(from,direction);
240 UpdateTotalSet(setTotal, found);
242 ///////////// Up side
244 ////////////////// right direction
246 from = fkSegmentation->PadsUp(fCenterPad).GetFirst();
247 direction = AliMpIntPair(1,0);
248 found = PadVectorLine(from,direction);
249 UpdateTotalSet(setTotal, found);
251 ////////////////// left direction
253 from = fkSegmentation->PadsLeft(from).GetFirst(); // the pad up is already added
254 direction = AliMpIntPair(-1,0);
255 found = PadVectorLine(from,direction);
256 UpdateTotalSet(setTotal, found);
258 ///////////// Right side
260 ////////////////// Up direction
262 from = fkSegmentation->PadsRight(fCenterPad).GetFirst();
263 direction = AliMpIntPair(0,1);
264 found = PadVectorLine(from,direction);
265 UpdateTotalSet(setTotal, found);
267 ////////////////// down direction
269 from = fkSegmentation->PadsDown(from).GetFirst(); // the pad right is already added
270 direction = AliMpIntPair(0,-1);
271 found = PadVectorLine(from,direction);
272 UpdateTotalSet(setTotal, found);
274 ///////////// Down side
276 ////////////////// Right direction
278 from = fkSegmentation->PadsDown(fCenterPad).GetFirst();
279 direction = AliMpIntPair(1,0);
280 found = PadVectorLine(from,direction);
281 UpdateTotalSet(setTotal, found);
283 ////////////////// left direction
285 from = fkSegmentation->PadsLeft(from).GetFirst(); // the pad down is already added
286 direction = AliMpIntPair(-1,0);
287 found = PadVectorLine(from,direction);
288 UpdateTotalSet(setTotal, found);
290 // fill the fIndices vector with the set (-->pass from a rapid insertion,
291 // to rapid and indexed access, for the rest of the job)
295 // include the center pad if requiered
296 if (includeCenter) fPads.push_back(fCenterPad);
297 //fPads.insert(fPads.end(),setTotal.begin(),setTotal.end());
300 for (it = setTotal.begin(); it != setTotal.end(); it++)
301 fPads.push_back((*it));
306 // include the center pad if requiered
307 if (includeCenter) fPads.Add(new AliMpPad(fCenterPad));
309 for (Int_t i = 0; i<setTotal.GetEntriesFast(); i++)
310 fPads.Add(setTotal.At(i));
314 //______________________________________________________________________________
315 Bool_t AliMpNeighboursPadIterator::IsValid() const
317 // Is the iterator in a valid position?
318 return (fkSegmentation!=0 && fIndex!=fgkInvalidIndex);
323 //______________________________________________________________________________
324 void AliMpNeighboursPadIterator::First()
326 // Reset the iterator, so that it points to the first available
330 if ((fkSegmentation != 0) && (fPads.size() != 0))
333 if ((fkSegmentation != 0) && (fPads.GetEntriesFast() != 0))
337 fIndex=fgkInvalidIndex;
341 //______________________________________________________________________________
342 void AliMpNeighboursPadIterator::Next()
344 // pre-increment operator. Should be used by default for iterating over
348 if (!IsValid()) return;
351 if (fIndex < fPads.size()-1)
354 if (Int_t(fIndex) < fPads.GetEntriesFast()-1)
361 //______________________________________________________________________________
362 Bool_t AliMpNeighboursPadIterator::IsDone() const
368 //______________________________________________________________________________
369 AliMpPad AliMpNeighboursPadIterator::CurrentItem() const
371 // dereferencement operator
373 return AliMpPad::Invalid();
376 return fPads[fIndex];
379 return *((AliMpPad*)fPads[fIndex]);
383 //______________________________________________________________________________
384 void AliMpNeighboursPadIterator::Invalidate()
386 // Let the iterator points to the invalid position
387 fIndex=fgkInvalidIndex;