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.11 2006/03/02 16:35:12 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>
53 const Double_t AliMpSectorSegmentation::fgkS1 = 100000.;
54 const Double_t AliMpSectorSegmentation::fgkS2 = 1000.;
57 ClassImp(AliMpSectorSegmentation)
59 //______________________________________________________________________________
60 AliMpSectorSegmentation::AliMpSectorSegmentation(const AliMpSector* sector)
61 : AliMpVSegmentation(),
66 /// Standard constructor
68 fPadBuffer = new AliMpPad(AliMpPad::Invalid());
70 FillPadDimensionsMap();
73 //______________________________________________________________________________
74 AliMpSectorSegmentation::AliMpSectorSegmentation()
75 : AliMpVSegmentation(),
82 /// Default constructor
85 //_____________________________________________________________________________
86 AliMpSectorSegmentation::AliMpSectorSegmentation(
87 const AliMpSectorSegmentation& right)
88 : AliMpVSegmentation(right)
90 /// Protected copy constructor (not provided)
92 Fatal("AliMpSectorSegmentation", "Copy constructor not provided.");
95 //______________________________________________________________________________
96 AliMpSectorSegmentation::~AliMpSectorSegmentation()
107 //_____________________________________________________________________________
108 AliMpSectorSegmentation&
109 AliMpSectorSegmentation::operator=(const AliMpSectorSegmentation& right)
111 /// Protected assignment operator (not provided)
113 // check assignment to self
114 if (this == &right) return *this;
116 Fatal("operator =", "Assignment operator not provided.");
125 //_____________________________________________________________________________
127 AliMpSectorSegmentation::GetAllElectronicCardIDs(TArrayI& ecn) const
129 GetSector()->GetAllMotifPositionsIDs(ecn);
133 //______________________________________________________________________________
134 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
136 /// Convert the two vector to long.
138 return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
141 //______________________________________________________________________________
142 TVector2 AliMpSectorSegmentation::GetVector(Long_t index) const
144 /// Convert the long index to twovector.
146 return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
147 (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
151 //______________________________________________________________________________
152 void AliMpSectorSegmentation::FillPadDimensionsMap()
154 /// Fill the maps between zone ids and pad dimensions.
156 for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
157 AliMpZone* zone = fkSector->GetZone(i+1);
158 Int_t zoneID = zone->GetID();
160 if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
164 fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
167 AliDebug(1,Form("Filling fPadDimensions[%d]=(%e,%e)",
168 zoneID*10,zone->GetPadDimensions().X(),
169 zone->GetPadDimensions().Y()));
170 fPadDimensionsMap.Add((Long_t)(zoneID*10),
171 GetIndex(zone->GetPadDimensions()));
177 for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
178 AliMpSubZone* subZone = zone->GetSubZone(j);
179 AliMpVMotif* motif = subZone->GetMotif();
181 for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
182 Int_t index = zoneID*10 + subIndex++;
184 fPadDimensionsMap[index] = motif->GetPadDimensions(k);
187 AliDebug(1,Form("Filling fPadDimensions[%d]=(%e,%e) motif %s-%d",
189 motif->GetPadDimensions(k).X(),
190 motif->GetPadDimensions(k).Y(),
191 motif->GetID().Data(),k));
193 fPadDimensionsMap.Add((Long_t)(index),
194 GetIndex(motif->GetPadDimensions(k)));
202 //______________________________________________________________________________
204 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
206 /// Find the motif position which contains the given pad indices
207 /// return 0 if not found
209 switch (fkSector->GetDirection()) {
211 // Case where all the pads have the same size along X direction
213 for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
214 AliMpRow* row = fkSector->GetRow(irow);
215 if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
216 row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
218 for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
219 AliMpVRowSegment* seg = row->GetRowSegment(iseg);
220 if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
221 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
223 AliMpMotifPosition* motifPos;
224 for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
226 = fkSector->GetMotifMap()
227 ->FindMotifPosition(seg->GetMotifPositionId(imot));
228 if (motifPos && motifPos->HasPad(indices)) return motifPos;
237 ////////////////////////////////////////////////////////////////////////////////
239 // Case where all the pads have the same size along Y direction
240 // look for the row which contains the indices
243 for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
244 row = fkSector->GetRow(irow);
245 AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
246 if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
247 lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
248 // NOTE : We use the last row segment in order to ensure that
249 // we are not on a special motif
251 if (irow==fkSector->GetNofRows()) return 0;
252 // look for the row segment, in the found row, which contains the indices
253 AliMpVRowSegment* seg=0;
255 for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
256 seg = row->GetRowSegment(iseg);
257 if (seg->HasIndices(indices)) break;
259 if (iseg==row->GetNofRowSegments()) return 0;
261 // look for the motif position which contains the indices
262 AliMpMotifPosition* motifPos=0;
264 for (imot=0;imot<seg->GetNofMotifs();++imot) {
266 = fkSector->GetMotifMap()
267 ->FindMotifPosition(seg->GetMotifPositionId(imot));
268 if (motifPos && motifPos->HasPad(indices)) break;
270 if (imot==seg->GetNofMotifs()) return 0;
278 //______________________________________________________________________________
280 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition,
283 /// Find the first valid pad from starting position in the
284 /// direction of pad lines up to distance dx.
286 // Define step limits
287 Double_t stepX = fkSector->GetMinPadDimensions().X();
289 // Search in X direction
291 TVector2 position(startPosition);
293 pad = PadByPosition(position, false);
294 position += TVector2(stepX, 0.);
296 while ( !pad.IsValid() && position.X() < maxX );
298 // Invalidate pad if it is outside limits
299 if ((pad.Position().X() - pad.Dimensions().X()) > maxX)
300 pad = AliMpPad::Invalid();
305 //______________________________________________________________________________
307 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition,
310 /// Find the first valid pad from starting position in the
311 /// direction of pad columns up to distance dx.
313 // Define step limits
314 Double_t stepY = fkSector->GetMinPadDimensions().Y();
316 // Search in Y direction
318 TVector2 position(startPosition);
320 pad = PadByPosition(position, false);
321 position += TVector2(0., stepY);
323 while ( !pad.IsValid() && position.Y() < maxY );
325 // Invalidate pad if it is outside limits
326 if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY)
327 pad = AliMpPad::Invalid();
336 //______________________________________________________________________________
338 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
340 /// Create the area iterator.
342 switch (fkSector->GetDirection()) {
344 case kX: return new AliMpSectorAreaVPadIterator(this, area);
346 case kY: return new AliMpSectorAreaHPadIterator(this, area);
350 Fatal("CreateIterator", "Incomplete switch on Sector direction");
354 //______________________________________________________________________________
356 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
357 Bool_t includeCenter) const
359 /// Create the neighbours pad iterator.
361 return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
364 //______________________________________________________________________________
366 AliMpSectorSegmentation::Dimensions() const
368 return GetSector()->Dimensions();
371 //______________________________________________________________________________
373 AliMpSectorSegmentation::PlaneType() const
375 return GetSector()->PlaneType();
378 //______________________________________________________________________________
380 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location,
381 Bool_t warning) const
383 /// Find the pad which corresponds to the given location
385 if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
387 AliMpMotifPosition* motifPos =
388 fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
390 if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
391 return AliMpPad::Invalid();
394 AliMpVMotif* motif = motifPos->GetMotif();
395 AliMpIntPair localIndices =
396 motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
397 if (! localIndices.IsValid()) {
398 if (warning) Warning("PadByLocation","The pad number doesn't exists");
399 return AliMpPad::Invalid();
401 TVector2 delta = motif->PadPositionLocal(localIndices);
402 return (*fPadBuffer) = AliMpPad(location,
403 motifPos->GlobalIndices(localIndices),
404 motifPos->Position()+delta,
405 motif->GetPadDimensions(localIndices));
408 //______________________________________________________________________________
410 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
411 Bool_t warning ) const
413 /// Find the pad which corresponds to the given indices
415 if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);
417 AliMpMotifPosition* motifPos = FindMotifPosition(indices);
420 cout << "indices " << indices << endl;
421 Warning("PadByIndices","Pad indices not contained in any motif!");
423 return AliMpPad::Invalid();
426 // retrieve the local indices in the found motif
427 AliMpVMotif* motif = motifPos->GetMotif();
428 AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
430 AliMpConnection* connection=
431 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
434 if (warning) Warning("PadByIndices","No connection with the given indices!");
435 return AliMpPad::Invalid();
438 TVector2 localPos = motif->PadPositionLocal(localIndices);
441 = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
443 motifPos->Position()+localPos,
444 motif->GetPadDimensions(localIndices));
447 //______________________________________________________________________________
449 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
450 Bool_t warning) const
452 /// Find the pad which corresponds to the given position
454 if ((*fPadBuffer).Position().X()==position.X() &&
455 (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);
457 Int_t motifPosID = fkSector->FindMotifPositionId(position);
458 AliMpMotifPosition* motifPos
459 = fkSector->GetMotifMap()
460 ->FindMotifPosition(motifPosID);
463 if (warning) Warning("PadByPosition","Position outside limits");
464 return AliMpPad::Invalid();
467 AliMpVMotif* motif = motifPos->GetMotif();
468 AliMpIntPair localIndices
469 = motif->PadIndicesLocal(position-motifPos->Position());
471 AliMpConnection* connect =
472 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
475 if (warning) Warning("PadByPosition","Position outside motif limits");
476 return AliMpPad::Invalid();
480 = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
481 motifPos->GlobalIndices(localIndices),
482 motifPos->Position()+motif->PadPositionLocal(localIndices),
483 motif->GetPadDimensions(localIndices));
487 //______________________________________________________________________________
489 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition,
490 Double_t distance) const
492 /// Find the first valid pad from starting position in the
493 /// direction of pad lines/columns up to the specified distance.
494 /// Pad lines are the lines of pads in the sector with constant pad y size,
495 /// pad columns are the columns of pads in the sector with constant pad x size.
497 switch (fkSector->GetDirection()) {
499 case kX: return PadByYDirection(startPosition, distance);
501 case kY: return PadByXDirection(startPosition, distance);
505 Fatal("PadByDirection", "Incomplete switch on Sector direction");
506 return AliMpPad::Invalid();
509 //______________________________________________________________________________
510 Int_t AliMpSectorSegmentation::MaxPadIndexX()
512 /// Return maximum pad index in x
514 if (fMaxIndexInX) return fMaxIndexInX;
516 for (Int_t i=0; i<fkSector->GetNofRows(); i++) {
517 Int_t ixh = fkSector->GetRow(i)->GetHighIndicesLimit().GetFirst();
518 if ( ixh > fMaxIndexInX ) fMaxIndexInX = ixh;
523 //______________________________________________________________________________
524 Int_t AliMpSectorSegmentation::MaxPadIndexY()
526 /// Return maximum pad index in y
528 if (fMaxIndexInY) return fMaxIndexInY;
530 for (Int_t i=0; i<fkSector->GetNofRows(); i++) {
531 Int_t iyh = fkSector->GetRow(i)->GetHighIndicesLimit().GetSecond();
532 if ( iyh > fMaxIndexInY ) fMaxIndexInY = iyh;
537 //______________________________________________________________________________
538 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
540 /// Does the pad specified by <indices> exist ?
542 return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
545 //______________________________________________________________________________
546 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
548 /// Does the motif position specified by motifPositionID exist ?
550 return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
553 //______________________________________________________________________________
554 TVector2 AliMpSectorSegmentation::GetMinPadDimensions() const
556 /// Returne the dimensions of the smallest pad.
558 return fkSector->GetMinPadDimensions();
561 //______________________________________________________________________________
562 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
564 /// Return the zone index of the zone containing the specified pad.
565 /// This zone index is different from the zone ID,
566 /// as it is unique for each pad dimensions.
567 /// It is composed in this way:
568 /// zoneID*10 + specific index
569 /// Specific index is present only for zones containing special motifs.
571 if (!pad.IsValid()) {
572 if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
577 PadDimensionsMapCIterator it;
578 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
579 if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
585 PadDimensionsMapCIterator it(&fPadDimensionsMap);
587 while ( it.Next(key, value) ) {
588 TVector2 dimensions = GetVector(value);
589 if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
593 AliError(Form("fPadDimensionsMap size is %d",fPadDimensionsMap.GetSize()));
597 // Should never happen
598 AliError("Zone(AliMpPad pad) not found, where pad is :");
599 StderrToAliError(cerr << pad << endl;);
603 //______________________________________________________________________________
605 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
607 /// Return the pad dimensions for the zone with the specified zone index.
610 PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
611 if (it != fPadDimensionsMap.end()) return it->second;
615 Long_t value = fPadDimensionsMap.GetValue(zone);
616 if (value) return GetVector(value);
619 if (warning) Warning("PadDimensions(zone)", "not found");
623 //______________________________________________________________________________
624 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
626 /// Verify that all methods for retrieving pads are consistents between them.
627 /// Return true if the pad with specified indices was found and verified,
630 if (!HasPad(indices)) return false;
632 // Verify the indice->location->position->indice way
633 AliMpIntPair location = PadByIndices(indices).GetLocation();
634 TVector2 position = PadByLocation(location).Position();
635 AliMpIntPair retIndices = PadByPosition(position).GetIndices();
637 if (retIndices != indices) {
638 cout << "Pad " << indices << " lead to inconsistency" << endl;
639 cout << "in indice->location->position->indice way..." << endl;
640 cout << "starting from " << indices << "-->" << location << "-->"
641 << '(' << position.X() << ',' << position.Y() << ')'
642 << " and retIndices: " << retIndices << endl;
646 // Verify the indice->position->location->indice way
647 position = PadByIndices(indices).Position();
648 location = PadByPosition(position).GetLocation();
649 retIndices = PadByLocation(location).GetIndices();
651 if (retIndices != indices) {
652 cout << "Pad " << indices << " lead to inconsistency" << endl;
653 cout << "in indice->position->location->indice way..." <<endl;
654 cout << "starting from " << indices
655 << " and retIndices: " << retIndices << endl;
661 //______________________________________________________________________________
663 AliMpSectorSegmentation::Print(Option_t* opt) const
665 fkSector->Print(opt);
668 //______________________________________________________________________________
669 void AliMpSectorSegmentation::PrintZones() const
671 /// Print all zones and pads dimensions from the map.
673 cout << "Zones: " << endl;
676 PadDimensionsMapCIterator it;
677 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
678 cout << " zone: " << setw(4) << it->first;
679 cout << " pad dimensions: ( "
680 << it->second.X() << ", " << it->second.Y() << ")" << endl;
685 PadDimensionsMapCIterator it(&fPadDimensionsMap);
687 while ( it.Next(key, value) ) {
688 //cout << "Iterating over: " << key << ", " << value << endl;
689 TVector2 dimensions = GetVector(value);
691 cout << " zone: " << setw(4) << key;
692 cout << " pad dimensions: ( "
693 << dimensions.X() << ", " << dimensions.Y() << ")" << endl;