4 // Class AliMpNeighboursPadIterator
5 // --------------------------------
6 // Class, which defines an iterator over the pads surrounding a given pad
8 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
14 #include "AliMpNeighboursPadIterator.h"
15 #include "AliMpIntPair.h"
16 #include "AliMpSectorSegmentation.h"
18 #include "AliMpConstants.h"
20 ClassImp(AliMpNeighboursPadIterator)
22 const UInt_t AliMpNeighboursPadIterator::fgkInvalidIndex = 9999;
23 //never so much neighbours...
25 //______________________________________________________________________________
26 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator()
27 : AliMpVPadIterator(),
29 fCenterPad(AliMpPad::Invalid()),
31 fIndex(fgkInvalidIndex)
33 // default constructor, set the current position to "invalid"
36 //______________________________________________________________________________
37 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator(
38 const AliMpSectorSegmentation* segmentation,
39 const AliMpPad& centerPad,
41 : AliMpVPadIterator(),
42 fkSegmentation(segmentation),
43 fCenterPad(centerPad),
44 fIndex(fgkInvalidIndex)
46 // normal constructor, set *this to invalid position
48 FillPadsVector(includeCenter);
51 //______________________________________________________________________________
52 AliMpNeighboursPadIterator::AliMpNeighboursPadIterator(
53 const AliMpNeighboursPadIterator& right)
54 : AliMpVPadIterator(right)
61 //______________________________________________________________________________
62 AliMpNeighboursPadIterator::~AliMpNeighboursPadIterator()
69 //______________________________________________________________________________
70 AliMpNeighboursPadIterator&
71 AliMpNeighboursPadIterator::operator = (const AliMpNeighboursPadIterator& right)
73 // assignement operator
74 // if the right hand iterator isn't of good type
75 // the current operator is invalidated
77 // check assignement to self
78 if (this == &right) return *this;
80 // base class assignement
81 AliMpVPadIterator::operator=(right);
83 fkSegmentation = right.fkSegmentation;
84 fCenterPad = right.fCenterPad;
86 fIndex = right.fIndex;
94 //______________________________________________________________________________
95 Bool_t AliMpNeighboursPadIterator::IsNeighbours(const AliMpPad& pad) const
97 // true if the pad located by <padIndice> is a neighbours of those
98 // located at <fCenterPad>
101 TVector2 relPos = pad.Position() - fCenterPad.Position();
102 TVector2 bounds = pad.Dimensions() + fCenterPad.Dimensions();
103 return (TMath::Abs(relPos.X())- bounds.X()<AliMpConstants::LengthTolerance()) &&
104 (TMath::Abs(relPos.Y())- bounds.Y()<AliMpConstants::LengthTolerance());
108 //______________________________________________________________________________
109 PadVector AliMpNeighboursPadIterator::PadVectorLine(const AliMpPad& from,
110 const AliMpIntPair& direction) const
112 // Fill a new vector with all pads which have common
113 // parts with the pad located at <fCenterPad>, in a given line
114 // starting from <from> and moving by <direction>
116 AliMpPad current = from;
120 if (IsNeighbours(current))
121 ans.push_back(current);
124 TVector2 nextPos = current.Position() + TVector2(
125 current.Dimensions().X()*(AliMpConstants::LengthStep()+1.)*direction.GetFirst(),
126 current.Dimensions().Y()*(AliMpConstants::LengthStep()+1.)*direction.GetSecond());
127 current = fkSegmentation->PadByPosition(nextPos);
132 //______________________________________________________________________________
133 void AliMpNeighboursPadIterator::FillPadsVector(Bool_t includeCenter)
135 // Fill the indices vector with all indices of pads which have common
136 // parts with the pad located at <fCenterPad>
138 if (!fkSegmentation || !fCenterPad.IsValid()) return;
142 AliMpIntPair direction;
145 // repare a unique simple associative container
146 // --> no doublons, rapid insersion
149 ///////////// Left side
151 ////////////////// up direction
153 from = fkSegmentation->PadsLeft(fCenterPad).GetFirst();
154 direction = AliMpIntPair(0,1);
155 found = PadVectorLine(from,direction);
156 setTotal.insert(found.begin(),found.end());
159 ////////////////// down direction
161 from = fkSegmentation->PadsDown(from).GetFirst(); // the Pad down is already added
162 direction = AliMpIntPair(0,-1);
163 found = PadVectorLine(from,direction);
164 setTotal.insert(found.begin(),found.end());
166 ///////////// Up side
168 ////////////////// right direction
170 from = fkSegmentation->PadsUp(fCenterPad).GetFirst();
171 direction = AliMpIntPair(1,0);
172 found = PadVectorLine(from,direction);
173 setTotal.insert(found.begin(),found.end());
175 ////////////////// left direction
177 from = fkSegmentation->PadsLeft(from).GetFirst(); // the pad up is already added
178 direction = AliMpIntPair(-1,0);
179 found = PadVectorLine(from,direction);
180 setTotal.insert(found.begin(),found.end());
182 ///////////// Right side
184 ////////////////// Up direction
186 from = fkSegmentation->PadsRight(fCenterPad).GetFirst();
187 direction = AliMpIntPair(0,1);
188 found = PadVectorLine(from,direction);
189 setTotal.insert(found.begin(),found.end());
191 ////////////////// down direction
193 from = fkSegmentation->PadsDown(from).GetFirst(); // the pad right is already added
194 direction = AliMpIntPair(0,-1);
195 found = PadVectorLine(from,direction);
196 setTotal.insert(found.begin(),found.end());
198 ///////////// Down side
200 ////////////////// Right direction
202 from = fkSegmentation->PadsDown(fCenterPad).GetFirst();
203 direction = AliMpIntPair(1,0);
204 found = PadVectorLine(from,direction);
205 setTotal.insert(found.begin(),found.end());
207 ////////////////// left direction
209 from = fkSegmentation->PadsLeft(from).GetFirst(); // the pad down is already added
210 direction = AliMpIntPair(-1,0);
211 found = PadVectorLine(from,direction);
212 setTotal.insert(found.begin(),found.end());
215 // fill the fIndices vector with the set (-->pass from a rapid insertion,
216 // to rapid and indexed access, for the rest of the job)
219 // include the center pad if requiered
220 if (includeCenter) fPads.push_back(fCenterPad);
221 //fPads.insert(fPads.end(),setTotal.begin(),setTotal.end());
224 for (it = setTotal.begin(); it != setTotal.end(); it++)
225 fPads.push_back((*it));
228 //______________________________________________________________________________
229 Bool_t AliMpNeighboursPadIterator::IsValid() const
231 // Is the iterator in a valid position?
232 return (fkSegmentation!=0 && fIndex!=fgkInvalidIndex);
237 //______________________________________________________________________________
238 void AliMpNeighboursPadIterator::First()
240 // Reset the iterator, so that it points to the first available
243 if ((fkSegmentation != 0) && (fPads.size() != 0))
246 fIndex=fgkInvalidIndex;
250 //______________________________________________________________________________
251 void AliMpNeighboursPadIterator::Next()
253 // pre-increment operator. Should be used by default for iterating over
257 if (!IsValid()) return;
259 if (fIndex < fPads.size()-1)
265 //______________________________________________________________________________
266 Bool_t AliMpNeighboursPadIterator::IsDone() const
272 //______________________________________________________________________________
273 AliMpPad AliMpNeighboursPadIterator::CurrentItem() const
275 // dereferencement operator
277 return AliMpPad::Invalid();
279 return fPads[fIndex];
282 //______________________________________________________________________________
283 void AliMpNeighboursPadIterator::Invalidate()
285 // Let the iterator points to the invalid position
286 fIndex=fgkInvalidIndex;