1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // $MpId: AliMpSlatZonePadIterator.cxx,v 1.4 2005/09/19 19:01:31 ivana Exp $
19 #include "AliMpSlatZonePadIterator.h"
23 #include "AliMpSlat.h"
24 #include "AliMpSlatSegmentation.h"
30 ClassImp(AliMpSlatZonePadIterator)
32 // This iterator only works within a region of constant density.
36 const Double_t epsilon = 1E-9; // mm
37 Double_t DMAX(std::numeric_limits<Double_t>::max());
40 //_____________________________________________________________________________
41 AliMpSlatZonePadIterator::AliMpSlatZonePadIterator(const AliMpSlat* slat,
42 const AliMpArea& area)
43 : AliMpVPadIterator(),
45 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
53 // Iteration will be done on the slat, over the crop of (area,slat_area)
57 AliFatal(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
58 area.LeftBorder(),area.DownBorder(),
59 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
64 //_____________________________________________________________________________
65 AliMpSlatZonePadIterator::~AliMpSlatZonePadIterator()
70 delete fSlatSegmentation;
73 //_____________________________________________________________________________
75 AliMpSlatZonePadIterator::CropArea()
78 // Checks the area is correct, and truncate it
79 // if it goes outside the slat.
81 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
82 fArea.LeftBorder(),fArea.DownBorder(),
83 fArea.RightBorder(),fArea.UpBorder()));
85 // Left and right x-limits have to come from first and last pcbs
86 // to deal with short and rounded pcbs cases.
87 AliMpPCB* first = fkSlat->FindPCB(fArea.LeftBorder(),fArea.DownBorder());
88 AliMpPCB* last = fkSlat->FindPCB(fArea.RightBorder()-epsilon,
91 AliDebug(3,Form("First PCB %s Ixmin %2d Last PCB %s Ixmax %2d",
92 first->GetID(),first->Ixmin(),
93 last->GetID(),last->Ixmax()));
95 Double_t xleft = first->ActiveXmin();
96 Double_t xright = last->ActiveXmax() - epsilon;
98 AliDebug(3,Form("xleft,xright=%7.2f,%7.2f",xleft,xright));
100 Double_t xmin = std::max(fArea.LeftBorder(),xleft);
101 Double_t xmax = std::min(fArea.RightBorder(),xright);
102 Double_t ymin = std::max(fArea.DownBorder(),0.0);
103 Double_t ymax = std::min(fArea.UpBorder(),fkSlat->DY()*2.0-epsilon);
105 AliDebug(3,Form("Cropped area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
106 xmin,ymin,xmax,ymax));
108 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
110 // But there's so far no guarantee that it is "filling" an integer number
111 // of pads. The following lines solve this, by expanding the area up to
112 // the bottomLeft and topRight limits of the pads sitting at (xmin,ymin)
116 = fSlatSegmentation->PadByPosition(TVector2(xmin,ymin),kFALSE);
117 if ( bottomLeft.IsValid() )
119 xmin = std::min(xmin,fkSlat->DX() +
120 bottomLeft.Position().X() - bottomLeft.Dimensions().X());
121 ymin = std::min(ymin,fkSlat->DY() +
122 bottomLeft.Position().Y() - bottomLeft.Dimensions().Y());
126 = fSlatSegmentation->PadByPosition(TVector2(xmax,ymax),kFALSE);
127 if ( topRight.IsValid() )
129 xmax = std::max(xmax,fkSlat->DX() +
130 topRight.Position().X() + topRight.Dimensions().X());
131 ymax = std::max(ymax,fkSlat->DY() +
132 topRight.Position().Y() + topRight.Dimensions().Y());
135 fArea = AliMpArea(TVector2((xmin+xmax)/2.0,(ymin+ymax)/2.0),
136 TVector2((xmax-xmin)/2.0,(ymax-ymin)/2.0));
138 AliDebug(3,Form("Paddified cropped area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
139 fArea.LeftBorder(),fArea.DownBorder(),
140 fArea.RightBorder(),fArea.UpBorder()));
142 // Finally set the step sizes equal to the smallest pad sizes (which is
143 // hereby assumed to be that of the first pcb).
144 fStep.Set(first->PadSizeX(),first->PadSizeY());
146 AliDebug(3,Form("Step Sizes (%7.2f,%7.2f)",fStep.X(),fStep.Y()));
148 return fArea.IsValid();
151 //_____________________________________________________________________________
153 AliMpSlatZonePadIterator::CurrentItem() const
156 // Returns the current iteration position (i.e. a pad)
161 //_____________________________________________________________________________
163 AliMpSlatZonePadIterator::GetNextPosition(Double_t& x, Double_t& y)
165 // Get the next iteration position.
166 // On input, fOffset must be a valid position (i.e. within iteration
169 AliDebug(3,Form("input (x,y)=(%7.2f,%7.2f)",x,y));
173 if ( x > fArea.Dimensions().X() )
175 AliDebug(3,"Going back left");
176 // Go back leftmost position...
177 x = -1.0*fArea.Dimensions().X();
181 fOffset.Set(fOffset.X(),y);
182 if ( y > fArea.Dimensions().Y() )
187 AliDebug(3,Form("output (x,y)=(%7.2f,%7.2f",x,y));
191 //_____________________________________________________________________________
193 AliMpSlatZonePadIterator::First()
196 // (re)Starts the iteration.
199 fOffset = fArea.Dimensions()*(-1.0);
201 SetPad(fCurrentPad,fArea.Position()+fOffset);
202 if (!fCurrentPad.IsValid()) Next();
203 AliDebug(3,Form("fOffset after Next=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
204 if ( !fCurrentPad.IsValid() )
206 // did not find any valid pad in there, bailing out.
208 AliError("Could not initiate iterator. Please check the area you gave.");
213 // Reposition to pad center (both in x and y).
214 // Please note that repositionning y is valid here, and only here
215 // (i.e. do *not* do this in Next() for instance).
216 fOffset.Set(fCurrentPad.Position().X()+fkSlat->DX()-fArea.Position().X(),
217 fCurrentPad.Position().Y()+fkSlat->DY()-fArea.Position().Y());
220 AliDebug(3,Form("fOffset repositionned=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
223 //_____________________________________________________________________________
225 AliMpSlatZonePadIterator::Invalidate()
228 // Invalidate the iterator.
231 fOffset = TVector2(DMAX,DMAX);
232 fCurrentPad = AliMpPad::Invalid();
236 //_____________________________________________________________________________
238 AliMpSlatZonePadIterator::IsDone() const
241 // Whether the iteration is finished or not.
246 //_____________________________________________________________________________
248 AliMpSlatZonePadIterator::Next()
250 // This one is the meat of the class.
251 // We're iterating in x-direction mainly, starting from
252 // lower-left of the iteration area, and proceeding right,
253 // until we reach right border, in which case we increment y
254 // and go back to leftmost position.
255 // Put otherwise, here's basically how it should work:
256 // try to do x+=xstep. If outside the area, get back to xmin
257 // and try y+=ystep. If outside of the window end-of-game.
258 // When new x,y known, get the corresponding pad.
259 // If same pad as before (should not happen if step sizes are
260 // well chosen) or not valid pad (might happen for e.g. rounded pcbs),
262 // End of iteration occurs when both x and y are outside the iteration
265 if (IsDone()) return;
267 AliMpPad pad(fCurrentPad);
269 Double_t x(fOffset.X());
270 Double_t y(fOffset.Y());
272 while ( ( pad == fCurrentPad || !pad.IsValid() ) && n<100 )
275 if (GetNextPosition(x,y)==kFALSE)
280 SetPad(pad,fArea.Position()+TVector2(x,y));
285 //_____________________________________________________________________________
287 AliMpSlatZonePadIterator::SetPad(AliMpPad& pad, const TVector2& pos)
290 // Sets the current pad.
292 pad = fSlatSegmentation->PadByPosition(pos,kFALSE);
295 // Reposition fOffset to pad center (only in x-direction).
296 fOffset.Set(pad.Position().X()+fkSlat->DX()-fArea.Position().X(),