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.6 2006/03/02 16:38:11 ivana Exp $
19 #include "AliMpSlatZonePadIterator.h"
23 #include "AliMpSlat.h"
24 #include "AliMpSlatSegmentation.h"
26 #include "Riostream.h"
31 ClassImp(AliMpSlatZonePadIterator)
33 // This iterator only works within a region of constant density.
37 const Double_t epsilon = 1E-4; // cm
38 Double_t DMAX(std::numeric_limits<Double_t>::max());
41 //_____________________________________________________________________________
42 AliMpSlatZonePadIterator::AliMpSlatZonePadIterator(const AliMpSlat* slat,
43 const AliMpArea& area)
44 : AliMpVPadIterator(),
46 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
54 // Iteration will be done on the slat, over the crop of (area,slat_area)
58 AliError(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
59 area.LeftBorder(),area.DownBorder(),
60 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
65 //_____________________________________________________________________________
66 AliMpSlatZonePadIterator::~AliMpSlatZonePadIterator()
71 delete fSlatSegmentation;
74 //_____________________________________________________________________________
76 AliMpSlatZonePadIterator::CropArea()
79 // Checks the area is correct, and truncate it
80 // if it goes outside the slat.
82 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
83 fArea.LeftBorder(),fArea.DownBorder(),
84 fArea.RightBorder(),fArea.UpBorder()));
86 // Left and right x-limits have to come from first and last pcbs
87 // to deal with short and rounded pcbs cases.
88 AliMpPCB* first = fkSlat->FindPCB(fArea.LeftBorder(),fArea.DownBorder());
89 AliMpPCB* last = fkSlat->FindPCB(fArea.RightBorder()-epsilon,
92 AliDebug(3,Form("First PCB %s Ixmin %2d Last PCB %s Ixmax %2d",
93 first->GetID(),first->Ixmin(),
94 last->GetID(),last->Ixmax()));
96 Double_t xleft = first->ActiveXmin();
97 Double_t xright = last->ActiveXmax() - epsilon;
99 AliDebug(3,Form("xleft,xright=%e,%e",xleft,xright));
101 Double_t xmin = std::max(fArea.LeftBorder(),xleft);
102 Double_t xmax = std::min(fArea.RightBorder(),xright);
103 Double_t ymin = std::max(fArea.DownBorder(),0.0);
104 Double_t ymax = std::min(fArea.UpBorder(),fkSlat->DY()*2.0-epsilon);
106 AliDebug(3,Form("Cropped area (%e,%e)->(%e,%e)",
107 xmin,ymin,xmax,ymax));
109 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
111 // But there's so far no guarantee that it is "filling" an integer number
112 // of pads. The following lines solve this, by expanding the area up to
113 // the bottomLeft and topRight limits of the pads sitting at (xmin,ymin)
117 = fSlatSegmentation->PadByPosition(TVector2(xmin,ymin)-fkSlat->Position(),
121 cout << "bottomLeft=" << endl;
123 cout << bottomLeft.Position().X()+fkSlat->Position().X()
124 << "," << bottomLeft.Position().Y()+fkSlat->Position().Y()
128 if ( bottomLeft.IsValid() )
130 xmin = std::min(xmin,fkSlat->DX() +
131 bottomLeft.Position().X() - bottomLeft.Dimensions().X());
132 ymin = std::min(ymin,fkSlat->DY() +
133 bottomLeft.Position().Y() - bottomLeft.Dimensions().Y());
137 = fSlatSegmentation->PadByPosition(TVector2(xmax,ymax)-fkSlat->Position(),
140 cout << "topRight=" << endl;
142 cout << topRight.Position().X()+fkSlat->Position().X()
143 << "," << topRight.Position().Y()+fkSlat->Position().Y()
148 if ( topRight.IsValid() )
150 xmax = std::max(xmax,fkSlat->DX() +
151 topRight.Position().X() + topRight.Dimensions().X());
152 ymax = std::max(ymax,fkSlat->DY() +
153 topRight.Position().Y() + topRight.Dimensions().Y());
156 fArea = AliMpArea(TVector2((xmin+xmax)/2.0,(ymin+ymax)/2.0),
157 TVector2((xmax-xmin)/2.0,(ymax-ymin)/2.0));
159 AliDebug(3,Form("Paddified cropped area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
160 fArea.LeftBorder(),fArea.DownBorder(),
161 fArea.RightBorder(),fArea.UpBorder()));
163 // Finally set the step sizes equal to the smallest pad sizes (which is
164 // hereby assumed to be that of the first pcb).
165 fStep.Set(first->PadSizeX(),first->PadSizeY());
167 AliDebug(3,Form("Step Sizes (%7.2f,%7.2f)",fStep.X(),fStep.Y()));
169 return fArea.IsValid();
172 //_____________________________________________________________________________
174 AliMpSlatZonePadIterator::CurrentItem() const
177 // Returns the current iteration position (i.e. a pad)
182 //_____________________________________________________________________________
184 AliMpSlatZonePadIterator::GetNextPosition(Double_t& x, Double_t& y)
186 // Get the next iteration position.
187 // On input, fOffset must be a valid position (i.e. within iteration
190 AliDebug(3,Form("input (x,y)=(%7.2f,%7.2f)",x,y));
194 if ( x > fArea.Dimensions().X() )
196 AliDebug(3,"Going back left and one step upper");
197 // Go back leftmost position...
198 x = -1.0*fArea.Dimensions().X();
202 fOffset.Set(fOffset.X(),y);
203 if ( y > fArea.Dimensions().Y() )
208 AliDebug(3,Form("output (x,y)=(%7.2f,%7.2f",x,y));
212 //_____________________________________________________________________________
214 AliMpSlatZonePadIterator::First()
217 // (re)Starts the iteration.
220 fOffset = fArea.Dimensions()*(-1.0);
222 SetPad(fCurrentPad,fArea.Position()+fOffset);
223 if (!fCurrentPad.IsValid()) Next();
224 AliDebug(3,Form("fOffset after Next=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
225 if ( !fCurrentPad.IsValid() )
227 // did not find any valid pad in there, bailing out.
229 AliError(Form("Could not initiate iterator for slat %s. "
230 " Please check the area you gave : %e,%e +- %e,%e",
232 fArea.Position().X(),
233 fArea.Position().Y(),
234 fArea.Dimensions().X(),
235 fArea.Dimensions().Y()));
240 // Reposition to pad center (both in x and y).
241 // Please note that repositionning y is valid here, and only here
242 // (i.e. do *not* do this in Next() for instance).
243 fOffset.Set(fCurrentPad.Position().X()+fkSlat->DX()-fArea.Position().X(),
244 fCurrentPad.Position().Y()+fkSlat->DY()-fArea.Position().Y());
247 AliDebug(3,Form("fOffset repositionned=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
250 //_____________________________________________________________________________
252 AliMpSlatZonePadIterator::Invalidate()
255 // Invalidate the iterator.
258 fOffset = TVector2(DMAX,DMAX);
259 fCurrentPad = AliMpPad::Invalid();
263 //_____________________________________________________________________________
265 AliMpSlatZonePadIterator::IsDone() const
268 // Whether the iteration is finished or not.
273 //_____________________________________________________________________________
275 AliMpSlatZonePadIterator::Next()
277 // This one is the meat of the class.
278 // We're iterating in x-direction mainly, starting from
279 // lower-left of the iteration area, and proceeding right,
280 // until we reach right border, in which case we increment y
281 // and go back to leftmost position.
282 // Put otherwise, here's basically how it should work:
283 // try to do x+=xstep. If outside the area, get back to xmin
284 // and try y+=ystep. If outside of the window end-of-game.
285 // When new x,y known, get the corresponding pad.
286 // If same pad as before (should not happen if step sizes are
287 // well chosen) or not valid pad (might happen for e.g. rounded pcbs),
289 // End of iteration occurs when both x and y are outside the iteration
292 if (IsDone()) return;
294 AliMpPad pad(fCurrentPad);
296 Double_t x(fOffset.X());
297 Double_t y(fOffset.Y());
299 while ( ( pad == fCurrentPad || !pad.IsValid() ) && n<100 )
302 if (GetNextPosition(x,y)==kFALSE)
307 SetPad(pad,fArea.Position()+TVector2(x,y));
312 //_____________________________________________________________________________
314 AliMpSlatZonePadIterator::SetPad(AliMpPad& pad, const TVector2& pos)
317 // Sets the current pad.
319 pad = fSlatSegmentation->PadByPosition(pos-fkSlat->Position(),kFALSE);
322 // Reposition fOffset to pad center (only in x-direction).
323 fOffset.Set(pad.Position().X()+fkSlat->DX()-fArea.Position().X(),
328 AliDebug(3,Form("No pad at pos=%e,%e",pos.X(),pos.Y()));