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 **************************************************************************/
19 #include "AliMpPCBPadIterator.h"
21 #include "AliMpArea.h"
22 #include "AliMpConstants.h"
25 #include "AliMpSlat.h"
26 #include "AliMpSlatSegmentation.h"
28 #include "Riostream.h"
32 /// \class AliMpPCBPadIterator
34 /// Iterates over slat pads within a region of constant pad size.
36 /// \author Laurent Aphecetche
39 ClassImp(AliMpPCBPadIterator)
42 //_____________________________________________________________________________
43 AliMpPCBPadIterator::AliMpPCBPadIterator(const AliMpSlat* slat,
44 const AliMpArea& area)
45 : AliMpVPadIterator(),
47 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
56 // Iteration will be done on the slat, over the crop of (area,slat_area)
60 AliError(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
61 area.LeftBorder(),area.DownBorder(),
62 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
67 //_____________________________________________________________________________
68 AliMpPCBPadIterator::~AliMpPCBPadIterator()
73 delete fSlatSegmentation;
76 //_____________________________________________________________________________
78 AliMpPCBPadIterator::CropArea(const AliMpArea& area)
81 // Checks the area is correct, and truncate it
82 // if it goes outside the slat.
84 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
85 area.LeftBorder(),area.DownBorder(),
86 area.RightBorder(),area.UpBorder()));
88 const Double_t kEpsilon = AliMpConstants::LengthTolerance();
90 // Left and right x-limits have to come from first and last pcbs
91 // to deal with short and rounded pcbs cases.
92 AliMpPCB* first = fkSlat->FindPCB(area.LeftBorder(),area.DownBorder());
93 AliMpPCB* last = fkSlat->FindPCB(area.RightBorder()-kEpsilon,
96 // Check we're indeed dealing with only one pcb
99 AliError("This iterator supposed to work on a single PCB. Please check");
103 AliDebug(3,Form("PCB %s Ixmin %2d Ixmax %2d",
104 first->GetID(),first->Ixmin(),first->Ixmax()));
106 Double_t xleft = first->ActiveXmin();
107 Double_t xright = first->ActiveXmax() - kEpsilon;
109 AliDebug(3,Form("xleft,xright=%e,%e",xleft,xright));
111 Double_t xmin = std::max(area.LeftBorder(),xleft);
112 Double_t xmax = std::min(area.RightBorder(),xright);
113 Double_t ymin = std::max(area.DownBorder(),0.0);
114 Double_t ymax = std::min(area.UpBorder(),first->DY()*2.0-kEpsilon);
116 AliDebug(3,Form("Cropped area (%e,%e)->(%e,%e)",
117 xmin,ymin,xmax,ymax));
119 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
121 // We now try to convert this into a couple of indices pair indicating the
122 // region to iterate over, using integer values, not floating point ones.
123 // For this, we must find out the 4 pads that intersect the (xmin,ymin;xmax,ymax)
126 Int_t ixmin = first->Ixmin() + TMath::FloorNint((xmin-first->ActiveXmin())/first->PadSizeX());
127 Int_t ixmax = first->Ixmin() + TMath::CeilNint((xmax-first->ActiveXmin())/first->PadSizeX()) - 1;
128 Int_t iymin = first->Iymin() + TMath::FloorNint((ymin-first->Ymin())/first->PadSizeY());
129 Int_t iymax = first->Iymin() + TMath::CeilNint((ymax-first->Ymin())/first->PadSizeY()) - 1;
132 fMinIndices.Set(ixmin,iymin);
133 fMaxIndices.Set(ixmax,iymax);
135 AliDebug(3,Form("Paddified cropped area (%d,%d)->(%d,%d) %d,%d ; %d,%d",
136 ixmin,iymin,ixmax,iymax,
137 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
138 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()));
140 return fMinIndices.IsValid() && fMaxIndices.IsValid();
143 //_____________________________________________________________________________
145 AliMpPCBPadIterator::CurrentItem() const
148 // Returns the current iteration position (i.e. a pad)
153 //_____________________________________________________________________________
155 AliMpPCBPadIterator::First()
158 // (re)Starts the iteration.
161 AliDebug(3,Form("area = (%d,%d)->(%d,%d)",
162 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
163 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()));
164 fOffset = fMinIndices;
166 SetPad(fCurrentPad,fOffset);
167 if (!fCurrentPad.IsValid()) Next();
168 if ( !fCurrentPad.IsValid() )
170 // did not find any valid pad in there, bailing out.
172 AliError(Form("Could not initiate iterator for slat %s. "
173 " Please check the area you gave : %d,%d to %d,%d",
175 fMinIndices.GetFirst(),
176 fMinIndices.GetSecond(),
177 fMaxIndices.GetFirst(),
178 fMaxIndices.GetSecond()));
183 //_____________________________________________________________________________
185 AliMpPCBPadIterator::GetNextPosition(Int_t& ix, Int_t& iy)
187 // Get the next iteration position.
188 // On input, fOffset must be a valid position (i.e. within iteration
193 if ( ix > fMaxIndices.GetFirst() )
195 // Go back leftmost position...
196 ix = fMinIndices.GetFirst();
199 if ( iy > fMaxIndices.GetSecond() )
208 //_____________________________________________________________________________
210 AliMpPCBPadIterator::Invalidate()
213 // Invalidate the iterator.
215 fOffset = AliMpIntPair::Invalid();
216 fCurrentPad = AliMpPad::Invalid();
220 //_____________________________________________________________________________
222 AliMpPCBPadIterator::IsDone() const
225 // Whether the iteration is finished or not.
230 //_____________________________________________________________________________
232 AliMpPCBPadIterator::Next()
234 // This one is the meat of the class.
235 // We're iterating in x-direction mainly, starting from
236 // lower-left of the iteration area, and proceeding right,
237 // until we reach right border, in which case we increment y
238 // and go back to leftmost position.
239 // End of iteration occurs when both x and y are outside the iteration
242 if (IsDone()) return;
244 AliMpPad pad(fCurrentPad);
246 Int_t ix(fOffset.GetFirst());
247 Int_t iy(fOffset.GetSecond());
249 while ( ( pad == fCurrentPad || !pad.IsValid() ) && n<100 )
252 if (GetNextPosition(ix,iy)==kFALSE)
257 SetPad(pad,AliMpIntPair(ix,iy));
261 AliFatal("This should not happen!");
266 //_____________________________________________________________________________
268 AliMpPCBPadIterator::Print(Option_t*) const
271 cout << Form("fkSlat=%p fSlatSegmentation=%p (%s)",fkSlat,fSlatSegmentation,
272 fkSlat->GetName()) << endl
273 << Form("minIndices=(%d,%d) maxIndices=(%d,%d)",
274 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
275 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()) << endl
276 << Form("currentOffset=(%d,%d) isdone=%d currentpad=",
277 fOffset.GetFirst(),fOffset.GetSecond(),IsDone()) << endl;
281 //_____________________________________________________________________________
283 AliMpPCBPadIterator::SetPad(AliMpPad& pad, const AliMpIntPair& indices)
286 // Sets the current pad.
288 pad = fSlatSegmentation->PadByIndices(indices,kFALSE);