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 //-----------------------------------------------------------------------------
33 /// \class AliMpPCBPadIterator
35 /// Iterates over slat pads within a region of constant pad size.
37 /// \author Laurent Aphecetche
38 //-----------------------------------------------------------------------------
41 ClassImp(AliMpPCBPadIterator)
44 //_____________________________________________________________________________
45 AliMpPCBPadIterator::AliMpPCBPadIterator(const AliMpSlat* slat,
46 const AliMpArea& area)
47 : AliMpVPadIterator(),
49 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
58 /// Iteration will be done on the slat, over the crop of (area,slat_area)
62 AliError(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
63 area.LeftBorder(),area.DownBorder(),
64 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
69 //_____________________________________________________________________________
70 AliMpPCBPadIterator::~AliMpPCBPadIterator()
75 delete fSlatSegmentation;
78 //_____________________________________________________________________________
80 AliMpPCBPadIterator::CropArea(const AliMpArea& area)
83 /// Checks the area is correct, and truncate it
84 /// if it goes outside the slat.
86 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
87 area.LeftBorder(),area.DownBorder(),
88 area.RightBorder(),area.UpBorder()));
90 const Double_t kEpsilon = AliMpConstants::LengthTolerance();
92 // Left and right x-limits have to come from first and last pcbs
93 // to deal with short and rounded pcbs cases.
94 AliMpPCB* first = fkSlat->FindPCB(area.LeftBorder(),area.DownBorder());
95 AliMpPCB* last = fkSlat->FindPCB(area.RightBorder()-kEpsilon,
98 // Check we're indeed dealing with only one pcb
101 AliError("This iterator supposed to work on a single PCB. Please check");
105 AliDebug(3,Form("PCB %s Ixmin %2d Ixmax %2d",
106 first->GetID(),first->Ixmin(),first->Ixmax()));
108 Double_t xleft = first->ActiveXmin();
109 Double_t xright = first->ActiveXmax() - kEpsilon;
111 AliDebug(3,Form("xleft,xright=%e,%e",xleft,xright));
113 Double_t xmin = TMath::Max(area.LeftBorder(),xleft);
114 Double_t xmax = TMath::Min(area.RightBorder(),xright);
115 Double_t ymin = TMath::Max(area.DownBorder(),0.0);
116 Double_t ymax = TMath::Min(area.UpBorder(),first->DY()*2.0-kEpsilon);
118 AliDebug(3,Form("Cropped area (%e,%e)->(%e,%e)",
119 xmin,ymin,xmax,ymax));
121 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
123 // We now try to convert this into a couple of indices pair indicating the
124 // region to iterate over, using integer values, not floating point ones.
125 // For this, we must find out the 4 pads that intersect the (xmin,ymin;xmax,ymax)
128 Int_t ixmin = first->Ixmin() + TMath::FloorNint((xmin-first->ActiveXmin())/first->PadSizeX());
129 Int_t ixmax = first->Ixmin() + TMath::CeilNint((xmax-first->ActiveXmin())/first->PadSizeX()) - 1;
130 Int_t iymin = first->Iymin() + TMath::FloorNint((ymin-first->Ymin())/first->PadSizeY());
131 Int_t iymax = first->Iymin() + TMath::CeilNint((ymax-first->Ymin())/first->PadSizeY()) - 1;
134 fMinIndices.Set(ixmin,iymin);
135 fMaxIndices.Set(ixmax,iymax);
137 AliDebug(3,Form("Paddified cropped area (%d,%d)->(%d,%d) %d,%d ; %d,%d",
138 ixmin,iymin,ixmax,iymax,
139 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
140 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()));
142 return fMinIndices.IsValid() && fMaxIndices.IsValid();
145 //_____________________________________________________________________________
147 AliMpPCBPadIterator::CurrentItem() const
150 /// Returns the current iteration position (i.e. a pad)
155 //_____________________________________________________________________________
157 AliMpPCBPadIterator::First()
160 /// (re)Starts the iteration.
163 AliDebug(3,Form("area = (%d,%d)->(%d,%d)",
164 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
165 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()));
166 fOffset = fMinIndices;
168 SetPad(fCurrentPad,fOffset);
169 if (!fCurrentPad.IsValid()) Next();
170 if ( !fCurrentPad.IsValid() )
172 // did not find any valid pad in there, bailing out.
174 AliError(Form("Could not initiate iterator for slat %s. "
175 " Please check the area you gave : %d,%d to %d,%d",
177 fMinIndices.GetFirst(),
178 fMinIndices.GetSecond(),
179 fMaxIndices.GetFirst(),
180 fMaxIndices.GetSecond()));
185 //_____________________________________________________________________________
187 AliMpPCBPadIterator::GetNextPosition(Int_t& ix, Int_t& iy)
189 /// Get the next iteration position.
190 /// On input, fOffset must be a valid position (i.e. within iteration
195 if ( ix > fMaxIndices.GetFirst() )
197 // Go back leftmost position...
198 ix = fMinIndices.GetFirst();
201 if ( iy > fMaxIndices.GetSecond() )
210 //_____________________________________________________________________________
212 AliMpPCBPadIterator::Invalidate()
215 /// Invalidate the iterator.
217 fOffset = AliMpIntPair::Invalid();
218 fCurrentPad = AliMpPad::Invalid();
222 //_____________________________________________________________________________
224 AliMpPCBPadIterator::IsDone() const
227 /// Whether the iteration is finished or not.
232 //_____________________________________________________________________________
234 AliMpPCBPadIterator::Next()
236 /// This one is the meat of the class.
237 /// We're iterating in x-direction mainly, starting from
238 /// lower-left of the iteration area, and proceeding right,
239 /// until we reach right border, in which case we increment y
240 /// and go back to leftmost position.
241 /// End of iteration occurs when both x and y are outside the iteration
244 if (IsDone()) return;
246 AliMpPad pad(fCurrentPad);
248 Int_t ix(fOffset.GetFirst());
249 Int_t iy(fOffset.GetSecond());
251 while ( ( pad == fCurrentPad || !pad.IsValid() ) && n<100 )
254 if (GetNextPosition(ix,iy)==kFALSE)
259 SetPad(pad,AliMpIntPair(ix,iy));
263 AliFatal("This should not happen!");
268 //_____________________________________________________________________________
270 AliMpPCBPadIterator::Print(Option_t*) const
273 cout << Form("fkSlat=%p fSlatSegmentation=%p (%s)",fkSlat,fSlatSegmentation,
274 fkSlat->GetName()) << endl
275 << Form("minIndices=(%d,%d) maxIndices=(%d,%d)",
276 fMinIndices.GetFirst(),fMinIndices.GetSecond(),
277 fMaxIndices.GetFirst(),fMaxIndices.GetSecond()) << endl
278 << Form("currentOffset=(%d,%d) isdone=%d currentpad=",
279 fOffset.GetFirst(),fOffset.GetSecond(),IsDone()) << endl;
283 //_____________________________________________________________________________
285 AliMpPCBPadIterator::SetPad(AliMpPad& pad, const AliMpIntPair& indices)
288 /// Sets the current pad.
290 pad = fSlatSegmentation->PadByIndices(indices,kFALSE);