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"
30 ClassImp(AliMpSlatZonePadIterator)
33 /// \class AliMpSlatZonePadIterator
35 /// Iterates over slat pads within a region of constant pad size.
37 /// \author Laurent Aphecetche
39 const Double_t AliMpSlatZonePadIterator::fgkEpsilon = 1E-4; // cm
40 const Double_t AliMpSlatZonePadIterator::fgkDmax = std::numeric_limits<Double_t>::max();
42 //_____________________________________________________________________________
43 AliMpSlatZonePadIterator::AliMpSlatZonePadIterator(const AliMpSlat* slat,
44 const AliMpArea& area)
45 : AliMpVPadIterator(),
47 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
55 // Iteration will be done on the slat, over the crop of (area,slat_area)
59 AliError(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
60 area.LeftBorder(),area.DownBorder(),
61 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
66 //______________________________________________________________________________
67 AliMpSlatZonePadIterator::AliMpSlatZonePadIterator(const AliMpSlatZonePadIterator& right)
68 : AliMpVPadIterator(right)
70 /// Protected copy constructor (not implemented)
72 AliFatal("Copy constructor not provided.");
75 //_____________________________________________________________________________
76 AliMpSlatZonePadIterator::~AliMpSlatZonePadIterator()
81 delete fSlatSegmentation;
84 //______________________________________________________________________________
85 AliMpSlatZonePadIterator&
86 AliMpSlatZonePadIterator::operator=(const AliMpSlatZonePadIterator& right)
88 /// Protected assignement operator (not implemented)
90 // check assignement to self
91 if (this == &right) return *this;
93 AliFatal("Assignement operator not provided.");
98 //_____________________________________________________________________________
100 AliMpSlatZonePadIterator::CropArea()
103 // Checks the area is correct, and truncate it
104 // if it goes outside the slat.
106 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
107 fArea.LeftBorder(),fArea.DownBorder(),
108 fArea.RightBorder(),fArea.UpBorder()));
110 // Left and right x-limits have to come from first and last pcbs
111 // to deal with short and rounded pcbs cases.
112 AliMpPCB* first = fkSlat->FindPCB(fArea.LeftBorder(),fArea.DownBorder());
113 AliMpPCB* last = fkSlat->FindPCB(fArea.RightBorder()-fgkEpsilon,
116 AliDebug(3,Form("First PCB %s Ixmin %2d Last PCB %s Ixmax %2d",
117 first->GetID(),first->Ixmin(),
118 last->GetID(),last->Ixmax()));
120 Double_t xleft = first->ActiveXmin();
121 Double_t xright = last->ActiveXmax() - fgkEpsilon;
123 AliDebug(3,Form("xleft,xright=%e,%e",xleft,xright));
125 Double_t xmin = std::max(fArea.LeftBorder(),xleft);
126 Double_t xmax = std::min(fArea.RightBorder(),xright);
127 Double_t ymin = std::max(fArea.DownBorder(),0.0);
128 Double_t ymax = std::min(fArea.UpBorder(),fkSlat->DY()*2.0-fgkEpsilon);
130 AliDebug(3,Form("Cropped area (%e,%e)->(%e,%e)",
131 xmin,ymin,xmax,ymax));
133 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
135 // But there's so far no guarantee that it is "filling" an integer number
136 // of pads. The following lines solve this, by expanding the area up to
137 // the bottomLeft and topRight limits of the pads sitting at (xmin,ymin)
141 = fSlatSegmentation->PadByPosition(TVector2(xmin,ymin)-fkSlat->Position(),
145 cout << "bottomLeft=" << endl;
147 cout << bottomLeft.Position().X()+fkSlat->Position().X()
148 << "," << bottomLeft.Position().Y()+fkSlat->Position().Y()
152 if ( bottomLeft.IsValid() )
154 xmin = std::min(xmin,fkSlat->DX() +
155 bottomLeft.Position().X() - bottomLeft.Dimensions().X());
156 ymin = std::min(ymin,fkSlat->DY() +
157 bottomLeft.Position().Y() - bottomLeft.Dimensions().Y());
161 = fSlatSegmentation->PadByPosition(TVector2(xmax,ymax)-fkSlat->Position(),
164 cout << "topRight=" << endl;
166 cout << topRight.Position().X()+fkSlat->Position().X()
167 << "," << topRight.Position().Y()+fkSlat->Position().Y()
172 if ( topRight.IsValid() )
174 xmax = std::max(xmax,fkSlat->DX() +
175 topRight.Position().X() + topRight.Dimensions().X());
176 ymax = std::max(ymax,fkSlat->DY() +
177 topRight.Position().Y() + topRight.Dimensions().Y());
180 fArea = AliMpArea(TVector2((xmin+xmax)/2.0,(ymin+ymax)/2.0),
181 TVector2((xmax-xmin)/2.0,(ymax-ymin)/2.0));
183 AliDebug(3,Form("Paddified cropped area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
184 fArea.LeftBorder(),fArea.DownBorder(),
185 fArea.RightBorder(),fArea.UpBorder()));
187 // Finally set the step sizes equal to the smallest pad sizes (which is
188 // hereby assumed to be that of the first pcb).
189 fStep.Set(first->PadSizeX(),first->PadSizeY());
191 AliDebug(3,Form("Step Sizes (%7.2f,%7.2f)",fStep.X(),fStep.Y()));
193 return fArea.IsValid();
196 //_____________________________________________________________________________
198 AliMpSlatZonePadIterator::CurrentItem() const
201 // Returns the current iteration position (i.e. a pad)
206 //_____________________________________________________________________________
208 AliMpSlatZonePadIterator::GetNextPosition(Double_t& x, Double_t& y)
210 // Get the next iteration position.
211 // On input, fOffset must be a valid position (i.e. within iteration
214 AliDebug(3,Form("input (x,y)=(%7.2f,%7.2f)",x,y));
218 if ( x > fArea.Dimensions().X() )
220 AliDebug(3,"Going back left and one step upper");
221 // Go back leftmost position...
222 x = -1.0*fArea.Dimensions().X();
226 fOffset.Set(fOffset.X(),y);
227 if ( y > fArea.Dimensions().Y() )
232 AliDebug(3,Form("output (x,y)=(%7.2f,%7.2f",x,y));
236 //_____________________________________________________________________________
238 AliMpSlatZonePadIterator::First()
241 // (re)Starts the iteration.
244 fOffset = fArea.Dimensions()*(-1.0);
246 SetPad(fCurrentPad,fArea.Position()+fOffset);
247 if (!fCurrentPad.IsValid()) Next();
248 AliDebug(3,Form("fOffset after Next=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
249 if ( !fCurrentPad.IsValid() )
251 // did not find any valid pad in there, bailing out.
253 AliError(Form("Could not initiate iterator for slat %s. "
254 " Please check the area you gave : %e,%e +- %e,%e",
256 fArea.Position().X(),
257 fArea.Position().Y(),
258 fArea.Dimensions().X(),
259 fArea.Dimensions().Y()));
264 // Reposition to pad center (both in x and y).
265 // Please note that repositionning y is valid here, and only here
266 // (i.e. do *not* do this in Next() for instance).
267 fOffset.Set(fCurrentPad.Position().X()+fkSlat->DX()-fArea.Position().X(),
268 fCurrentPad.Position().Y()+fkSlat->DY()-fArea.Position().Y());
271 AliDebug(3,Form("fOffset repositionned=%7.2f,%7.2f",fOffset.X(),fOffset.Y()));
274 //_____________________________________________________________________________
276 AliMpSlatZonePadIterator::Invalidate()
279 // Invalidate the iterator.
282 fOffset = TVector2(fgkDmax,fgkDmax);
283 fCurrentPad = AliMpPad::Invalid();
287 //_____________________________________________________________________________
289 AliMpSlatZonePadIterator::IsDone() const
292 // Whether the iteration is finished or not.
297 //_____________________________________________________________________________
299 AliMpSlatZonePadIterator::Next()
301 // This one is the meat of the class.
302 // We're iterating in x-direction mainly, starting from
303 // lower-left of the iteration area, and proceeding right,
304 // until we reach right border, in which case we increment y
305 // and go back to leftmost position.
306 // Put otherwise, here's basically how it should work:
307 // try to do x+=xstep. If outside the area, get back to xmin
308 // and try y+=ystep. If outside of the window end-of-game.
309 // When new x,y known, get the corresponding pad.
310 // If same pad as before (should not happen if step sizes are
311 // well chosen) or not valid pad (might happen for e.g. rounded pcbs),
313 // End of iteration occurs when both x and y are outside the iteration
316 if (IsDone()) return;
318 AliMpPad pad(fCurrentPad);
320 Double_t x(fOffset.X());
321 Double_t y(fOffset.Y());
323 while ( ( pad == fCurrentPad || !pad.IsValid() ) && n<100 )
326 if (GetNextPosition(x,y)==kFALSE)
331 SetPad(pad,fArea.Position()+TVector2(x,y));
336 //_____________________________________________________________________________
338 AliMpSlatZonePadIterator::SetPad(AliMpPad& pad, const TVector2& pos)
341 // Sets the current pad.
343 pad = fSlatSegmentation->PadByPosition(pos-fkSlat->Position(),kFALSE);
346 // Reposition fOffset to pad center (only in x-direction).
347 fOffset.Set(pad.Position().X()+fkSlat->DX()-fArea.Position().X(),
352 AliDebug(3,Form("No pad at pos=%e,%e",pos.X(),pos.Y()));