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.14 2006/03/17 11:38:43 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 const Double_t AliMpSectorSegmentation::fgkS1 = 100000.;
53 const Double_t AliMpSectorSegmentation::fgkS2 = 1000.;
56 ClassImp(AliMpSectorSegmentation)
58 //______________________________________________________________________________
59 AliMpSectorSegmentation::AliMpSectorSegmentation(const AliMpSector* sector)
60 : AliMpVSegmentation(),
65 /// Standard constructor
67 fPadBuffer = new AliMpPad(AliMpPad::Invalid());
69 FillPadDimensionsMap();
72 //______________________________________________________________________________
73 AliMpSectorSegmentation::AliMpSectorSegmentation()
74 : AliMpVSegmentation(),
81 /// Default constructor
84 //_____________________________________________________________________________
85 AliMpSectorSegmentation::AliMpSectorSegmentation(
86 const AliMpSectorSegmentation& right)
87 : AliMpVSegmentation(right)
89 /// Protected copy constructor (not provided)
91 Fatal("AliMpSectorSegmentation", "Copy constructor not provided.");
94 //______________________________________________________________________________
95 AliMpSectorSegmentation::~AliMpSectorSegmentation()
106 //_____________________________________________________________________________
107 AliMpSectorSegmentation&
108 AliMpSectorSegmentation::operator=(const AliMpSectorSegmentation& right)
110 /// Protected assignment operator (not provided)
112 // check assignment to self
113 if (this == &right) return *this;
115 Fatal("operator =", "Assignment operator not provided.");
124 //_____________________________________________________________________________
126 AliMpSectorSegmentation::GetAllElectronicCardIDs(TArrayI& ecn) const
128 GetSector()->GetAllMotifPositionsIDs(ecn);
132 //______________________________________________________________________________
133 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
135 /// Convert the two vector to long.
137 return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
140 //______________________________________________________________________________
141 TVector2 AliMpSectorSegmentation::GetVector(Long_t index) const
143 /// Convert the long index to twovector.
145 return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
146 (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
150 //______________________________________________________________________________
151 void AliMpSectorSegmentation::FillPadDimensionsMap()
153 /// Fill the maps between zone ids and pad dimensions.
155 for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
156 AliMpZone* zone = fkSector->GetZone(i+1);
157 Int_t zoneID = zone->GetID();
159 if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
163 fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
166 AliDebug(1,Form("Filling fPadDimensions[%d]=(%e,%e)",
167 zoneID*10,zone->GetPadDimensions().X(),
168 zone->GetPadDimensions().Y()));
169 fPadDimensionsMap.Add((Long_t)(zoneID*10),
170 GetIndex(zone->GetPadDimensions()));
176 for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
177 AliMpSubZone* subZone = zone->GetSubZone(j);
178 AliMpVMotif* motif = subZone->GetMotif();
180 for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
181 Int_t index = zoneID*10 + subIndex++;
183 fPadDimensionsMap[index] = motif->GetPadDimensions(k);
186 AliDebug(1,Form("Filling fPadDimensions[%d]=(%e,%e) motif %s-%d",
188 motif->GetPadDimensions(k).X(),
189 motif->GetPadDimensions(k).Y(),
190 motif->GetID().Data(),k));
192 fPadDimensionsMap.Add((Long_t)(index),
193 GetIndex(motif->GetPadDimensions(k)));
201 //______________________________________________________________________________
203 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
205 /// Find the motif position which contains the given pad indices
206 /// return 0 if not found
208 switch (fkSector->GetDirection()) {
210 // Case where all the pads have the same size along X direction
212 for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
213 AliMpRow* row = fkSector->GetRow(irow);
214 if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
215 row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
217 for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
218 AliMpVRowSegment* seg = row->GetRowSegment(iseg);
219 if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
220 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
222 AliMpMotifPosition* motifPos;
223 for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
225 = fkSector->GetMotifMap()
226 ->FindMotifPosition(seg->GetMotifPositionId(imot));
227 if (motifPos && motifPos->HasPad(indices)) return motifPos;
236 ////////////////////////////////////////////////////////////////////////////////
238 // Case where all the pads have the same size along Y direction
239 // look for the row which contains the indices
242 for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
243 row = fkSector->GetRow(irow);
244 AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
245 if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
246 lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
247 // NOTE : We use the last row segment in order to ensure that
248 // we are not on a special motif
250 if (irow==fkSector->GetNofRows()) return 0;
251 // look for the row segment, in the found row, which contains the indices
252 AliMpVRowSegment* seg=0;
254 for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
255 seg = row->GetRowSegment(iseg);
256 if (seg->HasIndices(indices)) break;
258 if (iseg==row->GetNofRowSegments()) return 0;
260 // look for the motif position which contains the indices
261 AliMpMotifPosition* motifPos=0;
263 for (imot=0;imot<seg->GetNofMotifs();++imot) {
265 = fkSector->GetMotifMap()
266 ->FindMotifPosition(seg->GetMotifPositionId(imot));
267 if (motifPos && motifPos->HasPad(indices)) break;
269 if (imot==seg->GetNofMotifs()) return 0;
277 //______________________________________________________________________________
279 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition,
282 /// Find the first valid pad from starting position in the
283 /// direction of pad lines up to distance dx.
285 // Define step limits
286 Double_t stepX = fkSector->GetMinPadDimensions().X();
288 // Search in X direction
290 TVector2 position(startPosition);
292 pad = PadByPosition(position, false);
293 position += TVector2(stepX, 0.);
295 while ( !pad.IsValid() && position.X() < maxX );
297 // Invalidate pad if it is outside limits
298 if ((pad.Position().X() - pad.Dimensions().X()) > maxX)
299 pad = AliMpPad::Invalid();
304 //______________________________________________________________________________
306 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition,
309 /// Find the first valid pad from starting position in the
310 /// direction of pad columns up to distance dx.
312 // Define step limits
313 Double_t stepY = fkSector->GetMinPadDimensions().Y();
315 // Search in Y direction
317 TVector2 position(startPosition);
319 pad = PadByPosition(position, false);
320 position += TVector2(0., stepY);
322 while ( !pad.IsValid() && position.Y() < maxY );
324 // Invalidate pad if it is outside limits
325 if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY)
326 pad = AliMpPad::Invalid();
335 //______________________________________________________________________________
337 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
339 /// Create the area iterator.
341 switch (fkSector->GetDirection()) {
343 case kX: return new AliMpSectorAreaVPadIterator(this, area);
345 case kY: return new AliMpSectorAreaHPadIterator(this, area);
349 Fatal("CreateIterator", "Incomplete switch on Sector direction");
353 //______________________________________________________________________________
355 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
356 Bool_t includeCenter) const
358 /// Create the neighbours pad iterator.
360 return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
363 //______________________________________________________________________________
365 AliMpSectorSegmentation::Dimensions() const
367 return GetSector()->Dimensions();
370 //______________________________________________________________________________
372 AliMpSectorSegmentation::PlaneType() const
374 return GetSector()->GetPlaneType();
377 //______________________________________________________________________________
379 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location,
380 Bool_t warning) const
382 /// Find the pad which corresponds to the given location
384 if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
386 AliMpMotifPosition* motifPos =
387 fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
389 if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
390 return AliMpPad::Invalid();
393 AliMpVMotif* motif = motifPos->GetMotif();
394 AliMpIntPair localIndices =
395 motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
396 if (! localIndices.IsValid()) {
397 if (warning) Warning("PadByLocation","The pad number doesn't exists");
398 return AliMpPad::Invalid();
400 TVector2 delta = motif->PadPositionLocal(localIndices);
401 return (*fPadBuffer) = AliMpPad(location,
402 motifPos->GlobalIndices(localIndices),
403 motifPos->Position()+delta,
404 motif->GetPadDimensions(localIndices));
407 //______________________________________________________________________________
409 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
410 Bool_t warning ) const
412 /// Find the pad which corresponds to the given indices
414 if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);
416 AliMpMotifPosition* motifPos = FindMotifPosition(indices);
419 Warning("PadByIndices","Pad indices not contained in any motif!");
420 return AliMpPad::Invalid();
423 // retrieve the local indices in the found motif
424 AliMpVMotif* motif = motifPos->GetMotif();
425 AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
427 AliMpConnection* connection=
428 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
431 if (warning) Warning("PadByIndices","No connection with the given indices!");
432 return AliMpPad::Invalid();
435 TVector2 localPos = motif->PadPositionLocal(localIndices);
438 = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
440 motifPos->Position()+localPos,
441 motif->GetPadDimensions(localIndices));
444 //______________________________________________________________________________
446 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
447 Bool_t warning) const
449 /// Find the pad which corresponds to the given position
451 if ((*fPadBuffer).Position().X()==position.X() &&
452 (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);
454 Int_t motifPosID = fkSector->FindMotifPositionId(position);
455 AliMpMotifPosition* motifPos
456 = fkSector->GetMotifMap()
457 ->FindMotifPosition(motifPosID);
460 if (warning) Warning("PadByPosition","Position outside limits");
461 return AliMpPad::Invalid();
464 AliMpVMotif* motif = motifPos->GetMotif();
465 AliMpIntPair localIndices
466 = motif->PadIndicesLocal(position-motifPos->Position());
468 AliMpConnection* connect =
469 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
472 if (warning) Warning("PadByPosition","Position outside motif limits");
473 return AliMpPad::Invalid();
477 = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
478 motifPos->GlobalIndices(localIndices),
479 motifPos->Position()+motif->PadPositionLocal(localIndices),
480 motif->GetPadDimensions(localIndices));
484 //______________________________________________________________________________
486 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition,
487 Double_t distance) const
489 /// Find the first valid pad from starting position in the
490 /// direction of pad lines/columns up to the specified distance.
491 /// Pad lines are the lines of pads in the sector with constant pad y size,
492 /// pad columns are the columns of pads in the sector with constant pad x size.
494 switch (fkSector->GetDirection()) {
496 case kX: return PadByYDirection(startPosition, distance);
498 case kY: return PadByXDirection(startPosition, distance);
502 Fatal("PadByDirection", "Incomplete switch on Sector direction");
503 return AliMpPad::Invalid();
506 //______________________________________________________________________________
507 Int_t AliMpSectorSegmentation::MaxPadIndexX() const
509 /// Return maximum pad index in x
511 return fkSector->GetMaxPadIndices().GetFirst();
514 //______________________________________________________________________________
515 Int_t AliMpSectorSegmentation::MaxPadIndexY() const
517 /// Return maximum pad index in y
519 return fkSector->GetMaxPadIndices().GetSecond();
522 //______________________________________________________________________________
523 Int_t AliMpSectorSegmentation::NofPads() const
525 /// Return number of pads defined in the sector
527 return fkSector->GetNofPads();
530 //______________________________________________________________________________
531 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
533 /// Does the pad specified by <indices> exist ?
535 return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
538 //______________________________________________________________________________
539 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
541 /// Does the motif position specified by motifPositionID exist ?
543 return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
546 //______________________________________________________________________________
547 TVector2 AliMpSectorSegmentation::GetMinPadDimensions() const
549 /// Returne the dimensions of the smallest pad.
551 return fkSector->GetMinPadDimensions();
554 //______________________________________________________________________________
555 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
557 /// Return the zone index of the zone containing the specified pad.
558 /// This zone index is different from the zone ID,
559 /// as it is unique for each pad dimensions.
560 /// It is composed in this way:
561 /// zoneID*10 + specific index
562 /// Specific index is present only for zones containing special motifs.
564 if (!pad.IsValid()) {
565 if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
570 PadDimensionsMapCIterator it;
571 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
572 if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
578 PadDimensionsMapCIterator it(&fPadDimensionsMap);
580 while ( it.Next(key, value) ) {
581 TVector2 dimensions = GetVector(value);
582 if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
586 AliError(Form("fPadDimensionsMap size is %d",fPadDimensionsMap.GetSize()));
590 // Should never happen
592 << "Zone(AliMpPad pad) not found, where pad is: " << pad << endl;
596 //______________________________________________________________________________
598 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
600 /// Return the pad dimensions for the zone with the specified zone index.
603 PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
604 if (it != fPadDimensionsMap.end()) return it->second;
608 Long_t value = fPadDimensionsMap.GetValue(zone);
609 if (value) return GetVector(value);
612 if (warning) Warning("PadDimensions(zone)", "not found");
616 //______________________________________________________________________________
617 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
619 /// Verify that all methods for retrieving pads are consistents between them.
620 /// Return true if the pad with specified indices was found and verified,
623 if (!HasPad(indices)) return false;
625 // Verify the indice->location->position->indice way
626 AliMpIntPair location = PadByIndices(indices).GetLocation();
627 TVector2 position = PadByLocation(location).Position();
628 AliMpIntPair retIndices = PadByPosition(position).GetIndices();
630 if (retIndices != indices) {
631 cout << "Pad " << indices << " lead to inconsistency" << endl;
632 cout << "in indice->location->position->indice way..." << endl;
633 cout << "starting from " << indices << "-->" << location << "-->"
634 << '(' << position.X() << ',' << position.Y() << ')'
635 << " and retIndices: " << retIndices << endl;
639 // Verify the indice->position->location->indice way
640 position = PadByIndices(indices).Position();
641 location = PadByPosition(position).GetLocation();
642 retIndices = PadByLocation(location).GetIndices();
644 if (retIndices != indices) {
645 cout << "Pad " << indices << " lead to inconsistency" << endl;
646 cout << "in indice->position->location->indice way..." <<endl;
647 cout << "starting from " << indices
648 << " and retIndices: " << retIndices << endl;
654 //______________________________________________________________________________
656 AliMpSectorSegmentation::Print(Option_t* opt) const
658 fkSector->Print(opt);
661 //______________________________________________________________________________
662 void AliMpSectorSegmentation::PrintZones() const
664 /// Print all zones and pads dimensions from the map.
666 cout << "Zones: " << endl;
669 PadDimensionsMapCIterator it;
670 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
671 cout << " zone: " << setw(4) << it->first;
672 cout << " pad dimensions: ( "
673 << it->second.X() << ", " << it->second.Y() << ")" << endl;
678 PadDimensionsMapCIterator it(&fPadDimensionsMap);
680 while ( it.Next(key, value) ) {
681 //cout << "Iterating over: " << key << ", " << value << endl;
682 TVector2 dimensions = GetVector(value);
684 cout << " zone: " << setw(4) << key;
685 cout << " pad dimensions: ( "
686 << dimensions.X() << ", " << dimensions.Y() << ")" << endl;