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"
27 #include "AliMpEncodePair.h"
29 #include "Riostream.h"
33 //-----------------------------------------------------------------------------
34 /// \class AliMpPCBPadIterator
36 /// Iterates over slat pads within a region of constant pad size.
38 /// \author Laurent Aphecetche
39 //-----------------------------------------------------------------------------
42 ClassImp(AliMpPCBPadIterator)
45 //_____________________________________________________________________________
46 AliMpPCBPadIterator::AliMpPCBPadIterator(const AliMpSlat* slat,
47 const AliMpArea& area)
48 : AliMpVPadIterator(),
50 fSlatSegmentation(new AliMpSlatSegmentation(slat)),
59 /// Iteration will be done on the slat, over the crop of (area,slat_area)
63 AliError(Form("Could not crop area : (x,y)min=(%e,%e) ; max=(%e,%e) for slat %s",
64 area.LeftBorder(),area.DownBorder(),
65 area.RightBorder(),area.UpBorder(),fkSlat->GetID()));
70 //_____________________________________________________________________________
71 AliMpPCBPadIterator::~AliMpPCBPadIterator()
76 delete fSlatSegmentation;
79 //_____________________________________________________________________________
81 AliMpPCBPadIterator::CropArea(const AliMpArea& area)
84 /// Checks the area is correct, and truncate it
85 /// if it goes outside the slat.
87 AliDebug(3,Form("Input area (%7.2f,%7.2f)->(%7.2f,%7.2f)",
88 area.LeftBorder(),area.DownBorder(),
89 area.RightBorder(),area.UpBorder()));
91 const Double_t kEpsilon = AliMpConstants::LengthTolerance();
93 // Left and right x-limits have to come from first and last pcbs
94 // to deal with short and rounded pcbs cases.
95 AliMpPCB* first = fkSlat->FindPCB(area.LeftBorder(),area.DownBorder());
96 AliMpPCB* last = fkSlat->FindPCB(area.RightBorder()-kEpsilon,
99 // Check we're indeed dealing with only one pcb
102 AliError("This iterator supposed to work on a single PCB. Please check");
106 AliDebug(3,Form("PCB %s Ixmin %2d Ixmax %2d",
107 first->GetID(),first->Ixmin(),first->Ixmax()));
109 Double_t xleft = first->ActiveXmin();
110 Double_t xright = first->ActiveXmax() - kEpsilon;
112 AliDebug(3,Form("xleft,xright=%e,%e",xleft,xright));
114 Double_t xmin = TMath::Max(area.LeftBorder(),xleft);
115 Double_t xmax = TMath::Min(area.RightBorder(),xright);
116 Double_t ymin = TMath::Max(area.DownBorder(),0.0);
117 Double_t ymax = TMath::Min(area.UpBorder(),first->DY()*2.0-kEpsilon);
119 AliDebug(3,Form("Cropped area (%e,%e)->(%e,%e)",
120 xmin,ymin,xmax,ymax));
122 // At this point (xmin,ymin)->(xmax,ymax) should be a zone completely included
124 // We now try to convert this into a couple of indices pair indicating the
125 // region to iterate over, using integer values, not floating point ones.
126 // For this, we must find out the 4 pads that intersect the (xmin,ymin;xmax,ymax)
129 Int_t ixmin = first->Ixmin() + TMath::FloorNint((xmin-first->ActiveXmin())/first->PadSizeX());
130 Int_t ixmax = first->Ixmin() + TMath::CeilNint((xmax-first->ActiveXmin())/first->PadSizeX()) - 1;
131 Int_t iymin = first->Iymin() + TMath::FloorNint((ymin-first->Ymin())/first->PadSizeY());
132 Int_t iymax = first->Iymin() + TMath::CeilNint((ymax-first->Ymin())/first->PadSizeY()) - 1;
135 fMinIndices = AliMp::Pair(ixmin,iymin);
136 fMaxIndices = AliMp::Pair(ixmax,iymax);
138 AliDebug(3,Form("Paddified cropped area (%d,%d)->(%d,%d) %d,%d ; %d,%d",
139 ixmin,iymin,ixmax,iymax,
140 AliMp::PairFirst(fMinIndices),AliMp::PairSecond(fMinIndices),
141 AliMp::PairFirst(fMaxIndices),AliMp::PairSecond(fMaxIndices)));
143 return fMinIndices >= 0 && fMaxIndices >= 0;
146 //_____________________________________________________________________________
148 AliMpPCBPadIterator::CurrentItem() const
151 /// Returns the current iteration position (i.e. a pad)
156 //_____________________________________________________________________________
158 AliMpPCBPadIterator::First()
161 /// (re)Starts the iteration.
164 AliDebug(3,Form("area = (%d,%d)->(%d,%d)",
165 AliMp::PairFirst(fMinIndices),AliMp::PairSecond(fMinIndices),
166 AliMp::PairFirst(fMaxIndices),AliMp::PairSecond(fMaxIndices)));
167 fOffset = fMinIndices;
169 SetPad(fCurrentPad,AliMp::PairFirst(fOffset),AliMp::PairSecond(fOffset));
170 if ( ! fCurrentPad.IsValid() ) Next();
171 if ( ! fCurrentPad.IsValid() )
173 // did not find any valid pad in there, bailing out.
175 AliError(Form("Could not initiate iterator for slat %s. "
176 " Please check the area you gave : %d,%d to %d,%d",
178 AliMp::PairFirst(fMinIndices),AliMp::PairSecond(fMinIndices),
179 AliMp::PairFirst(fMaxIndices),AliMp::PairSecond(fMaxIndices)));
184 //_____________________________________________________________________________
186 AliMpPCBPadIterator::GetNextPosition(Int_t& ix, Int_t& iy) const
188 /// Get the next iteration position.
189 /// On input, fOffset must be a valid position (i.e. within iteration
194 if ( ix > AliMp::PairFirst(fMaxIndices) )
196 // Go back leftmost position...
197 ix = AliMp::PairFirst(fMinIndices);
200 if ( iy > AliMp::PairSecond(fMaxIndices) )
209 //_____________________________________________________________________________
211 AliMpPCBPadIterator::Invalidate()
214 /// Invalidate the iterator.
217 fCurrentPad = AliMpPad::Invalid();
221 //_____________________________________________________________________________
223 AliMpPCBPadIterator::IsDone() const
226 /// Whether the iteration is finished or not.
231 //_____________________________________________________________________________
233 AliMpPCBPadIterator::Next()
235 /// This one is the meat of the class.
236 /// We're iterating in x-direction mainly, starting from
237 /// lower-left of the iteration area, and proceeding right,
238 /// until we reach right border, in which case we increment y
239 /// and go back to leftmost position.
240 /// End of iteration occurs when both x and y are outside the iteration
243 if (IsDone()) return;
245 AliMpPad pad(fCurrentPad);
247 Int_t ix(AliMp::PairFirst(fOffset));
248 Int_t iy(AliMp::PairSecond(fOffset));
250 while ( ( pad == fCurrentPad || ! pad.IsValid() ) && n<100 )
253 if (GetNextPosition(ix,iy)==kFALSE)
262 AliFatal("This should not happen!");
267 //_____________________________________________________________________________
269 AliMpPCBPadIterator::Print(Option_t*) const
272 cout << Form("fkSlat=%p fSlatSegmentation=%p (%s)",fkSlat,fSlatSegmentation,
273 fkSlat->GetName()) << endl
274 << Form("minIndices=(%d,%d) maxIndices=(%d,%d)",
275 AliMp::PairFirst(fMinIndices),AliMp::PairSecond(fMinIndices),
276 AliMp::PairFirst(fMaxIndices),AliMp::PairSecond(fMaxIndices)) << endl
277 << Form("currentOffset=(%d,%d) isdone=%d currentpad=",
278 AliMp::PairFirst(fOffset),AliMp::PairSecond(fOffset),fIsDone) << endl;
282 //_____________________________________________________________________________
284 AliMpPCBPadIterator::SetPad(AliMpPad& pad, Int_t ix, Int_t iy)
287 /// Sets the current pad.
289 pad = fSlatSegmentation->PadByIndices(ix, iy,kFALSE);
292 fOffset = AliMp::Pair(ix,iy);