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: AliMpSectorSegmentation.cxx,v 1.15 2006/05/24 13:58:46 ivana Exp $
20 // Class AliMpSectorSegmentation
21 // -----------------------------
22 // Class describing the segmentation of the sector.
23 // Provides methods related to pads:
24 // conversion between pad indices, pad location, pad position;
25 // finding pad neighbour.
27 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
29 #include "AliMpSectorSegmentation.h"
30 #include "AliMpSector.h"
31 #include "AliMpZone.h"
32 #include "AliMpSubZone.h"
34 #include "AliMpVRowSegment.h"
35 #include "AliMpMotifMap.h"
36 #include "AliMpVMotif.h"
37 #include "AliMpMotifPosition.h"
38 #include "AliMpConnection.h"
39 #include "AliMpNeighboursPadIterator.h"
40 #include "AliMpSectorAreaHPadIterator.h"
41 #include "AliMpSectorAreaVPadIterator.h"
42 #include "AliMpIntPair.h"
43 #include "AliMpArea.h"
44 #include "AliMpConstants.h"
48 #include <Riostream.h>
52 ClassImp(AliMpSectorSegmentation)
56 const Double_t AliMpSectorSegmentation::fgkS1 = 100000.;
57 const Double_t AliMpSectorSegmentation::fgkS2 = 1000.;
60 //______________________________________________________________________________
61 AliMpSectorSegmentation::AliMpSectorSegmentation(
62 const AliMpSector* sector, Bool_t own)
63 : AliMpVSegmentation(),
71 /// Standard constructor
73 AliDebugStream(1) << "this = " << this << endl;
75 fPadBuffer = new AliMpPad(AliMpPad::Invalid());
77 FillPadDimensionsMap();
80 //______________________________________________________________________________
81 AliMpSectorSegmentation::AliMpSectorSegmentation()
82 : AliMpVSegmentation(),
90 /// Default constructor
92 AliDebugStream(1) << "this = " << this << endl;
95 //______________________________________________________________________________
96 AliMpSectorSegmentation::~AliMpSectorSegmentation()
100 AliDebugStream(1) << "this = " << this << endl;
102 if ( fIsOwner ) delete fkSector;
112 //_____________________________________________________________________________
114 AliMpSectorSegmentation::GetAllElectronicCardIDs(TArrayI& ecn) const
116 GetSector()->GetAllMotifPositionsIDs(ecn);
120 //______________________________________________________________________________
121 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
123 /// Convert the two vector to long.
125 return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
128 //______________________________________________________________________________
129 TVector2 AliMpSectorSegmentation::GetVector(Long_t index) const
131 /// Convert the long index to twovector.
133 return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
134 (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
138 //______________________________________________________________________________
139 void AliMpSectorSegmentation::FillPadDimensionsMap()
141 /// Fill the maps between zone ids and pad dimensions.
143 for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
144 AliMpZone* zone = fkSector->GetZone(i+1);
145 Int_t zoneID = zone->GetID();
147 if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
151 fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
155 << "Filling fPadDimensions[" << zoneID*10 << "] = ("
156 << zone->GetPadDimensions().X() << ", "
157 << zone->GetPadDimensions().Y() << ")" << endl;
159 fPadDimensionsMap.Add((Long_t)(zoneID*10),
160 GetIndex(zone->GetPadDimensions()));
166 for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
167 AliMpSubZone* subZone = zone->GetSubZone(j);
168 AliMpVMotif* motif = subZone->GetMotif();
170 for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
171 Int_t index = zoneID*10 + subIndex++;
173 fPadDimensionsMap[index] = motif->GetPadDimensions(k);
177 << "Filling fPadDimensions[" << index << "] = ("
178 << motif->GetPadDimensions(k).X() << ", "
179 << motif->GetPadDimensions(k).Y() << ") motif "
180 << motif->GetID().Data() << "-" << k << endl;
182 fPadDimensionsMap.Add((Long_t)(index),
183 GetIndex(motif->GetPadDimensions(k)));
191 //______________________________________________________________________________
193 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
195 /// Find the motif position which contains the given pad indices
196 /// return 0 if not found
198 switch (fkSector->GetDirection()) {
200 // Case where all the pads have the same size along X direction
202 for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
203 AliMpRow* row = fkSector->GetRow(irow);
204 if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
205 row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
207 for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
208 AliMpVRowSegment* seg = row->GetRowSegment(iseg);
209 if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
210 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
212 AliMpMotifPosition* motifPos;
213 for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
215 = fkSector->GetMotifMap()
216 ->FindMotifPosition(seg->GetMotifPositionId(imot));
217 if (motifPos && motifPos->HasPad(indices)) return motifPos;
226 ////////////////////////////////////////////////////////////////////////////////
228 // Case where all the pads have the same size along Y direction
229 // look for the row which contains the indices
232 for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
233 row = fkSector->GetRow(irow);
234 AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
235 if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
236 lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
237 // NOTE : We use the last row segment in order to ensure that
238 // we are not on a special motif
240 if (irow==fkSector->GetNofRows()) return 0;
241 // look for the row segment, in the found row, which contains the indices
242 AliMpVRowSegment* seg=0;
244 for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
245 seg = row->GetRowSegment(iseg);
246 if (seg->HasIndices(indices)) break;
248 if (iseg==row->GetNofRowSegments()) return 0;
250 // look for the motif position which contains the indices
251 AliMpMotifPosition* motifPos=0;
253 for (imot=0;imot<seg->GetNofMotifs();++imot) {
255 = fkSector->GetMotifMap()
256 ->FindMotifPosition(seg->GetMotifPositionId(imot));
257 if (motifPos && motifPos->HasPad(indices)) break;
259 if (imot==seg->GetNofMotifs()) return 0;
267 //______________________________________________________________________________
269 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition,
272 /// Find the first valid pad from starting position in the
273 /// direction of pad lines up to distance dx.
275 // Define step limits
276 Double_t stepX = fkSector->GetMinPadDimensions().X();
278 // Search in X direction
280 TVector2 position(startPosition);
282 pad = PadByPosition(position, false);
283 position += TVector2(stepX, 0.);
285 while ( !pad.IsValid() && position.X() < maxX );
287 // Invalidate pad if it is outside limits
288 if ((pad.Position().X() - pad.Dimensions().X()) > maxX)
289 pad = AliMpPad::Invalid();
294 //______________________________________________________________________________
296 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition,
299 /// Find the first valid pad from starting position in the
300 /// direction of pad columns up to distance dx.
302 // Define step limits
303 Double_t stepY = fkSector->GetMinPadDimensions().Y();
305 // Search in Y direction
307 TVector2 position(startPosition);
309 pad = PadByPosition(position, false);
310 position += TVector2(0., stepY);
312 while ( !pad.IsValid() && position.Y() < maxY );
314 // Invalidate pad if it is outside limits
315 if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY)
316 pad = AliMpPad::Invalid();
325 //______________________________________________________________________________
327 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
329 /// Create the area iterator.
331 switch (fkSector->GetDirection()) {
333 case AliMp::kX: return new AliMpSectorAreaVPadIterator(this, area);
335 case AliMp::kY: return new AliMpSectorAreaHPadIterator(this, area);
339 Fatal("CreateIterator", "Incomplete switch on Sector direction");
343 //______________________________________________________________________________
345 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
346 Bool_t includeCenter) const
348 /// Create the neighbours pad iterator.
350 return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
353 //______________________________________________________________________________
355 AliMpSectorSegmentation::Dimensions() const
357 return GetSector()->Dimensions();
360 //______________________________________________________________________________
362 AliMpSectorSegmentation::PlaneType() const
364 return GetSector()->GetPlaneType();
367 //______________________________________________________________________________
369 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location,
370 Bool_t warning) const
372 /// Find the pad which corresponds to the given location
374 if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
376 AliMpMotifPosition* motifPos =
377 fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
379 if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
380 return AliMpPad::Invalid();
383 AliMpVMotif* motif = motifPos->GetMotif();
384 AliMpIntPair localIndices =
385 motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
386 if (! localIndices.IsValid()) {
387 if (warning) Warning("PadByLocation","The pad number doesn't exists");
388 return AliMpPad::Invalid();
390 TVector2 delta = motif->PadPositionLocal(localIndices);
391 return (*fPadBuffer) = AliMpPad(location,
392 motifPos->GlobalIndices(localIndices),
393 motifPos->Position()+delta,
394 motif->GetPadDimensions(localIndices));
397 //______________________________________________________________________________
399 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
400 Bool_t warning ) const
402 /// Find the pad which corresponds to the given indices
404 if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);
406 AliMpMotifPosition* motifPos = FindMotifPosition(indices);
409 Warning("PadByIndices","Pad indices not contained in any motif!");
410 return AliMpPad::Invalid();
413 // retrieve the local indices in the found motif
414 AliMpVMotif* motif = motifPos->GetMotif();
415 AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
417 AliMpConnection* connection=
418 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
421 if (warning) Warning("PadByIndices","No connection with the given indices!");
422 return AliMpPad::Invalid();
425 TVector2 localPos = motif->PadPositionLocal(localIndices);
428 = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
430 motifPos->Position()+localPos,
431 motif->GetPadDimensions(localIndices));
434 //______________________________________________________________________________
436 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
437 Bool_t warning) const
439 /// Find the pad which corresponds to the given position
441 if ((*fPadBuffer).Position().X()==position.X() &&
442 (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);
444 Int_t motifPosID = fkSector->FindMotifPositionId(position);
445 AliMpMotifPosition* motifPos
446 = fkSector->GetMotifMap()
447 ->FindMotifPosition(motifPosID);
450 if (warning) Warning("PadByPosition","Position outside limits");
451 return AliMpPad::Invalid();
454 AliMpVMotif* motif = motifPos->GetMotif();
455 AliMpIntPair localIndices
456 = motif->PadIndicesLocal(position-motifPos->Position());
458 AliMpConnection* connect =
459 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
462 if (warning) Warning("PadByPosition","Position outside motif limits");
463 return AliMpPad::Invalid();
467 = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
468 motifPos->GlobalIndices(localIndices),
469 motifPos->Position()+motif->PadPositionLocal(localIndices),
470 motif->GetPadDimensions(localIndices));
474 //______________________________________________________________________________
476 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition,
477 Double_t distance) const
479 /// Find the first valid pad from starting position in the
480 /// direction of pad lines/columns up to the specified distance.
481 /// Pad lines are the lines of pads in the sector with constant pad y size,
482 /// pad columns are the columns of pads in the sector with constant pad x size.
484 switch (fkSector->GetDirection()) {
486 case AliMp::kX: return PadByYDirection(startPosition, distance);
488 case AliMp::kY: return PadByXDirection(startPosition, distance);
492 Fatal("PadByDirection", "Incomplete switch on Sector direction");
493 return AliMpPad::Invalid();
496 //______________________________________________________________________________
497 Int_t AliMpSectorSegmentation::MaxPadIndexX() const
499 /// Return maximum pad index in x
501 return fkSector->GetMaxPadIndices().GetFirst();
504 //______________________________________________________________________________
505 Int_t AliMpSectorSegmentation::MaxPadIndexY() const
507 /// Return maximum pad index in y
509 return fkSector->GetMaxPadIndices().GetSecond();
512 //______________________________________________________________________________
513 Int_t AliMpSectorSegmentation::NofPads() const
515 /// Return number of pads defined in the sector
517 return fkSector->GetNofPads();
520 //______________________________________________________________________________
521 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
523 /// Does the pad specified by \a indices exist ?
525 return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
528 //______________________________________________________________________________
529 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
531 /// Does the motif position specified by motifPositionID exist ?
533 return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
536 //______________________________________________________________________________
537 TVector2 AliMpSectorSegmentation::GetMinPadDimensions() const
539 /// Returne the dimensions of the smallest pad.
541 return fkSector->GetMinPadDimensions();
544 //______________________________________________________________________________
545 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
547 /// Return the zone index of the zone containing the specified pad.
548 /// This zone index is different from the zone ID,
549 /// as it is unique for each pad dimensions.
550 /// It is composed in this way:
551 /// zoneID*10 + specific index
552 /// Specific index is present only for zones containing special motifs.
554 if (!pad.IsValid()) {
555 if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
560 PadDimensionsMapCIterator it;
561 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
562 if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
568 PadDimensionsMapCIterator it(&fPadDimensionsMap);
570 while ( it.Next(key, value) ) {
571 TVector2 dimensions = GetVector(value);
572 if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
576 AliError(Form("fPadDimensionsMap size is %d",fPadDimensionsMap.GetSize()));
580 // Should never happen
582 << "Zone(AliMpPad pad) not found, where pad is: " << pad << endl;
586 //______________________________________________________________________________
588 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
590 /// Return the pad dimensions for the zone with the specified zone index.
593 PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
594 if (it != fPadDimensionsMap.end()) return it->second;
598 Long_t value = fPadDimensionsMap.GetValue(zone);
599 if (value) return GetVector(value);
602 if (warning) Warning("PadDimensions(zone)", "not found");
606 //______________________________________________________________________________
607 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
609 /// Verify that all methods for retrieving pads are consistents between them.
610 /// Return true if the pad with specified indices was found and verified,
613 if (!HasPad(indices)) return false;
615 // Verify the indice->location->position->indice way
616 AliMpIntPair location = PadByIndices(indices).GetLocation();
617 TVector2 position = PadByLocation(location).Position();
618 AliMpIntPair retIndices = PadByPosition(position).GetIndices();
620 if (retIndices != indices) {
621 cout << "Pad " << indices << " lead to inconsistency" << endl;
622 cout << "in indice->location->position->indice way..." << endl;
623 cout << "starting from " << indices << "-->" << location << "-->"
624 << '(' << position.X() << ',' << position.Y() << ')'
625 << " and retIndices: " << retIndices << endl;
629 // Verify the indice->position->location->indice way
630 position = PadByIndices(indices).Position();
631 location = PadByPosition(position).GetLocation();
632 retIndices = PadByLocation(location).GetIndices();
634 if (retIndices != indices) {
635 cout << "Pad " << indices << " lead to inconsistency" << endl;
636 cout << "in indice->position->location->indice way..." <<endl;
637 cout << "starting from " << indices
638 << " and retIndices: " << retIndices << endl;
644 //______________________________________________________________________________
646 AliMpSectorSegmentation::Print(Option_t* opt) const
648 fkSector->Print(opt);
651 //______________________________________________________________________________
652 void AliMpSectorSegmentation::PrintZones() const
654 /// Print all zones and pads dimensions from the map.
656 cout << "Zones: " << endl;
659 PadDimensionsMapCIterator it;
660 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
661 cout << " zone: " << setw(4) << it->first;
662 cout << " pad dimensions: ( "
663 << it->second.X() << ", " << it->second.Y() << ")" << endl;
668 PadDimensionsMapCIterator it(&fPadDimensionsMap);
670 while ( it.Next(key, value) ) {
671 //cout << "Iterating over: " << key << ", " << value << endl;
672 TVector2 dimensions = GetVector(value);
674 cout << " zone: " << setw(4) << key;
675 cout << " pad dimensions: ( "
676 << dimensions.X() << ", " << dimensions.Y() << ")" << endl;