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 "AliMpSectorPadIterator.h"
43 #include "AliMpIntPair.h"
44 #include "AliMpArea.h"
45 #include "AliMpConstants.h"
49 #include <Riostream.h>
53 ClassImp(AliMpSectorSegmentation)
57 const Double_t AliMpSectorSegmentation::fgkS1 = 100000.;
58 const Double_t AliMpSectorSegmentation::fgkS2 = 1000.;
61 //______________________________________________________________________________
62 AliMpSectorSegmentation::AliMpSectorSegmentation(
63 const AliMpSector* sector, Bool_t own)
64 : AliMpVSegmentation(),
72 /// Standard constructor
74 AliDebugStream(1) << "this = " << this << endl;
76 fPadBuffer = new AliMpPad(AliMpPad::Invalid());
78 FillPadDimensionsMap();
81 //______________________________________________________________________________
82 AliMpSectorSegmentation::AliMpSectorSegmentation()
83 : AliMpVSegmentation(),
91 /// Default constructor
93 AliDebugStream(1) << "this = " << this << endl;
96 //______________________________________________________________________________
97 AliMpSectorSegmentation::~AliMpSectorSegmentation()
101 AliDebugStream(1) << "this = " << this << endl;
103 if ( fIsOwner ) delete fkSector;
113 //_____________________________________________________________________________
115 AliMpSectorSegmentation::GetAllElectronicCardIDs(TArrayI& ecn) const
117 /// Fill the array ecn with all manuIds
119 GetSector()->GetAllMotifPositionsIDs(ecn);
123 //______________________________________________________________________________
124 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
126 /// Convert the two vector to long.
128 return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
131 //______________________________________________________________________________
132 TVector2 AliMpSectorSegmentation::GetVector(Long_t index) const
134 /// Convert the long index to twovector.
136 return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
137 (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
141 //______________________________________________________________________________
142 void AliMpSectorSegmentation::FillPadDimensionsMap()
144 /// Fill the maps between zone ids and pad dimensions.
146 for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
147 AliMpZone* zone = fkSector->GetZone(i+1);
148 Int_t zoneID = zone->GetID();
150 if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
154 fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
158 << "Filling fPadDimensions[" << zoneID*10 << "] = ("
159 << zone->GetPadDimensions().X() << ", "
160 << zone->GetPadDimensions().Y() << ")" << endl;
162 fPadDimensionsMap.Add((Long_t)(zoneID*10),
163 GetIndex(zone->GetPadDimensions()));
169 for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
170 AliMpSubZone* subZone = zone->GetSubZone(j);
171 AliMpVMotif* motif = subZone->GetMotif();
173 for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
174 Int_t index = zoneID*10 + subIndex++;
176 fPadDimensionsMap[index] = motif->GetPadDimensions(k);
180 << "Filling fPadDimensions[" << index << "] = ("
181 << motif->GetPadDimensions(k).X() << ", "
182 << motif->GetPadDimensions(k).Y() << ") motif "
183 << motif->GetID().Data() << "-" << k << endl;
185 fPadDimensionsMap.Add((Long_t)(index),
186 GetIndex(motif->GetPadDimensions(k)));
194 //______________________________________________________________________________
196 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
198 /// Find the motif position which contains the given pad indices
199 /// return 0 if not found
201 switch (fkSector->GetDirection()) {
203 // Case where all the pads have the same size along X direction
205 for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
206 AliMpRow* row = fkSector->GetRow(irow);
207 if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
208 row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
210 for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
211 AliMpVRowSegment* seg = row->GetRowSegment(iseg);
212 if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
213 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
215 AliMpMotifPosition* motifPos;
216 for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
218 = fkSector->GetMotifMap()
219 ->FindMotifPosition(seg->GetMotifPositionId(imot));
220 if (motifPos && motifPos->HasPad(indices)) return motifPos;
229 ////////////////////////////////////////////////////////////////////////////////
231 // Case where all the pads have the same size along Y direction
232 // look for the row which contains the indices
235 for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
236 row = fkSector->GetRow(irow);
237 AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
238 if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
239 lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
240 // NOTE : We use the last row segment in order to ensure that
241 // we are not on a special motif
243 if (irow==fkSector->GetNofRows()) return 0;
244 // look for the row segment, in the found row, which contains the indices
245 AliMpVRowSegment* seg=0;
247 for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
248 seg = row->GetRowSegment(iseg);
249 if (seg->HasIndices(indices)) break;
251 if (iseg==row->GetNofRowSegments()) return 0;
253 // look for the motif position which contains the indices
254 AliMpMotifPosition* motifPos=0;
256 for (imot=0;imot<seg->GetNofMotifs();++imot) {
258 = fkSector->GetMotifMap()
259 ->FindMotifPosition(seg->GetMotifPositionId(imot));
260 if (motifPos && motifPos->HasPad(indices)) break;
262 if (imot==seg->GetNofMotifs()) return 0;
270 //______________________________________________________________________________
272 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition,
275 /// Find the first valid pad from starting position in the
276 /// direction of pad lines up to distance dx.
278 // Define step limits
279 Double_t stepX = fkSector->GetMinPadDimensions().X();
281 // Search in X direction
283 TVector2 position(startPosition);
285 pad = PadByPosition(position, false);
286 position += TVector2(stepX, 0.);
288 while ( !pad.IsValid() &&
289 position.X() - fkSector->GetMaxPadDimensions().X() < maxX );
291 // Invalidate pad if it is outside limits
292 if ( (pad.Position().X() - pad.Dimensions().X()) > maxX )
293 pad = AliMpPad::Invalid();
298 //______________________________________________________________________________
300 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition,
303 /// Find the first valid pad from starting position in the
304 /// direction of pad columns up to distance dx.
306 // Define step limits
307 Double_t stepY = fkSector->GetMinPadDimensions().Y();
309 // Search in Y direction
311 TVector2 position(startPosition);
313 pad = PadByPosition(position, false);
314 position += TVector2(0., stepY);
316 while ( !pad.IsValid() &&
317 position.Y() - fkSector->GetMaxPadDimensions().Y()< maxY );
319 // Invalidate pad if it is outside limits
320 if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY)
321 pad = AliMpPad::Invalid();
330 //______________________________________________________________________________
332 AliMpSectorSegmentation::CreateIterator() const
334 /// Create the sector iterator
336 return new AliMpSectorPadIterator(fkSector);
339 //______________________________________________________________________________
341 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
343 /// Create the area iterator.
345 switch (fkSector->GetDirection()) {
347 case AliMp::kX: return new AliMpSectorAreaVPadIterator(this, area);
349 case AliMp::kY: return new AliMpSectorAreaHPadIterator(this, area);
353 Fatal("CreateIterator", "Incomplete switch on Sector direction");
357 //______________________________________________________________________________
359 AliMpSectorSegmentation::GetNeighbours(const AliMpPad& pad, TObjArray& neighbours,
361 Bool_t includeVoid) const
363 /// Uses default implementation
364 return AliMpVSegmentation::GetNeighbours(pad,neighbours,includeSelf,includeVoid);
367 //______________________________________________________________________________
369 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
370 Bool_t includeCenter) const
372 /// Create the neighbours pad iterator.
374 return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
377 //______________________________________________________________________________
379 AliMpSectorSegmentation::Dimensions() const
381 return GetSector()->Dimensions();
384 //______________________________________________________________________________
386 AliMpSectorSegmentation::PlaneType() const
388 return GetSector()->GetPlaneType();
391 //______________________________________________________________________________
393 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location,
394 Bool_t warning) const
396 /// Find the pad which corresponds to the given location
398 if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
400 AliMpMotifPosition* motifPos =
401 fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
403 if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
404 return AliMpPad::Invalid();
407 AliMpVMotif* motif = motifPos->GetMotif();
408 AliMpIntPair localIndices =
409 motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
410 if (! localIndices.IsValid()) {
411 if (warning) Warning("PadByLocation","The pad number doesn't exists");
412 return AliMpPad::Invalid();
414 TVector2 delta = motif->PadPositionLocal(localIndices);
415 return (*fPadBuffer) = AliMpPad(location,
416 motifPos->GlobalIndices(localIndices),
417 motifPos->Position()+delta,
418 motif->GetPadDimensions(localIndices));
421 //______________________________________________________________________________
423 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
424 Bool_t warning ) const
426 /// Find the pad which corresponds to the given indices
428 if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);
430 AliMpMotifPosition* motifPos = FindMotifPosition(indices);
433 Warning("PadByIndices","Pad indices not contained in any motif!");
434 return AliMpPad::Invalid();
437 // retrieve the local indices in the found motif
438 AliMpVMotif* motif = motifPos->GetMotif();
439 AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
441 AliMpConnection* connection=
442 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
445 if (warning) Warning("PadByIndices","No connection with the given indices!");
446 return AliMpPad::Invalid();
449 TVector2 localPos = motif->PadPositionLocal(localIndices);
452 = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
454 motifPos->Position()+localPos,
455 motif->GetPadDimensions(localIndices));
458 //______________________________________________________________________________
460 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
461 Bool_t warning) const
463 /// Find the pad which corresponds to the given position
465 if ((*fPadBuffer).Position().X()==position.X() &&
466 (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);
468 Int_t motifPosID = fkSector->FindMotifPositionId(position);
469 AliMpMotifPosition* motifPos
470 = fkSector->GetMotifMap()
471 ->FindMotifPosition(motifPosID);
474 if (warning) Warning("PadByPosition","Position outside limits");
475 return AliMpPad::Invalid();
478 AliMpVMotif* motif = motifPos->GetMotif();
479 AliMpIntPair localIndices
480 = motif->PadIndicesLocal(position-motifPos->Position());
482 AliMpConnection* connect =
483 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
486 if (warning) Warning("PadByPosition","Position outside motif limits");
487 return AliMpPad::Invalid();
491 = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
492 motifPos->GlobalIndices(localIndices),
493 motifPos->Position()+motif->PadPositionLocal(localIndices),
494 motif->GetPadDimensions(localIndices));
498 //______________________________________________________________________________
500 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition,
501 Double_t distance) const
503 /// Find the first valid pad from starting position in the
504 /// direction of pad lines/columns up to the specified distance.
505 /// Pad lines are the lines of pads in the sector with constant pad y size,
506 /// pad columns are the columns of pads in the sector with constant pad x size.
508 switch (fkSector->GetDirection()) {
510 case AliMp::kX: return PadByYDirection(startPosition, distance);
512 case AliMp::kY: return PadByXDirection(startPosition, distance);
516 Fatal("PadByDirection", "Incomplete switch on Sector direction");
517 return AliMpPad::Invalid();
520 //______________________________________________________________________________
521 Int_t AliMpSectorSegmentation::MaxPadIndexX() const
523 /// Return maximum pad index in x
525 return fkSector->GetMaxPadIndices().GetFirst();
528 //______________________________________________________________________________
529 Int_t AliMpSectorSegmentation::MaxPadIndexY() const
531 /// Return maximum pad index in y
533 return fkSector->GetMaxPadIndices().GetSecond();
536 //______________________________________________________________________________
537 Int_t AliMpSectorSegmentation::NofPads() const
539 /// Return number of pads defined in the sector
541 return fkSector->GetNofPads();
544 //______________________________________________________________________________
545 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
547 /// Does the pad specified by \a indices exist ?
549 return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
552 //______________________________________________________________________________
553 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
555 /// Does the motif position specified by motifPositionID exist ?
557 return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
560 //______________________________________________________________________________
561 TVector2 AliMpSectorSegmentation::GetMinPadDimensions() const
563 /// Returne the dimensions of the smallest pad.
565 return fkSector->GetMinPadDimensions();
568 //______________________________________________________________________________
569 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
571 /// Return the zone index of the zone containing the specified pad.
572 /// This zone index is different from the zone ID,
573 /// as it is unique for each pad dimensions.
574 /// It is composed in this way:
575 /// zoneID*10 + specific index
576 /// Specific index is present only for zones containing special motifs.
578 if (!pad.IsValid()) {
579 if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
584 PadDimensionsMapCIterator it;
585 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
586 if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
592 PadDimensionsMapCIterator it(&fPadDimensionsMap);
594 while ( it.Next(key, value) ) {
595 TVector2 dimensions = GetVector(value);
596 if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
600 AliError(Form("fPadDimensionsMap size is %d",fPadDimensionsMap.GetSize()));
604 // Should never happen
606 << "Zone(AliMpPad pad) not found, where pad is: " << pad << endl;
610 //______________________________________________________________________________
612 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
614 /// Return the pad dimensions for the zone with the specified zone index.
617 PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
618 if (it != fPadDimensionsMap.end()) return it->second;
622 Long_t value = fPadDimensionsMap.GetValue(zone);
623 if (value) return GetVector(value);
626 if (warning) Warning("PadDimensions(zone)", "not found");
630 //______________________________________________________________________________
631 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
633 /// Verify that all methods for retrieving pads are consistents between them.
634 /// Return true if the pad with specified indices was found and verified,
637 if (!HasPad(indices)) return false;
639 // Verify the indice->location->position->indice way
640 AliMpIntPair location = PadByIndices(indices).GetLocation();
641 TVector2 position = PadByLocation(location).Position();
642 AliMpIntPair retIndices = PadByPosition(position).GetIndices();
644 if (retIndices != indices) {
645 cout << "Pad " << indices << " lead to inconsistency" << endl;
646 cout << "in indice->location->position->indice way..." << endl;
647 cout << "starting from " << indices << "-->" << location << "-->"
648 << '(' << position.X() << ',' << position.Y() << ')'
649 << " and retIndices: " << retIndices << endl;
653 // Verify the indice->position->location->indice way
654 position = PadByIndices(indices).Position();
655 location = PadByPosition(position).GetLocation();
656 retIndices = PadByLocation(location).GetIndices();
658 if (retIndices != indices) {
659 cout << "Pad " << indices << " lead to inconsistency" << endl;
660 cout << "in indice->position->location->indice way..." <<endl;
661 cout << "starting from " << indices
662 << " and retIndices: " << retIndices << endl;
668 //______________________________________________________________________________
670 AliMpSectorSegmentation::Print(Option_t* opt) const
674 fkSector->Print(opt);
677 //______________________________________________________________________________
678 void AliMpSectorSegmentation::PrintZones() const
680 /// Print all zones and pads dimensions from the map.
682 cout << "Zones: " << endl;
685 PadDimensionsMapCIterator it;
686 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
687 cout << " zone: " << setw(4) << it->first;
688 cout << " pad dimensions: ( "
689 << it->second.X() << ", " << it->second.Y() << ")" << endl;
694 PadDimensionsMapCIterator it(&fPadDimensionsMap);
696 while ( it.Next(key, value) ) {
697 //cout << "Iterating over: " << key << ", " << value << endl;
698 TVector2 dimensions = GetVector(value);
700 cout << " zone: " << setw(4) << key;
701 cout << " pad dimensions: ( "
702 << dimensions.X() << ", " << dimensions.Y() << ")" << endl;