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 //-----------------------------------------------------------------------------
21 // Class AliMpSectorSegmentation
22 // -----------------------------
23 // Class describing the segmentation of the sector.
24 // Provides methods related to pads:
25 // conversion between pad indices, pad location, pad position;
26 // finding pad neighbour.
28 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
29 //-----------------------------------------------------------------------------
31 #include "AliMpSectorSegmentation.h"
32 #include "AliMpSector.h"
33 #include "AliMpZone.h"
34 #include "AliMpSubZone.h"
36 #include "AliMpVRowSegment.h"
37 #include "AliMpMotifMap.h"
38 #include "AliMpVMotif.h"
39 #include "AliMpMotifPosition.h"
40 #include "AliMpConnection.h"
41 #include "AliMpNeighboursPadIterator.h"
42 #include "AliMpSectorAreaHPadIterator.h"
43 #include "AliMpSectorAreaVPadIterator.h"
44 #include "AliMpSectorPadIterator.h"
45 #include "AliMpIntPair.h"
46 #include "AliMpArea.h"
47 #include "AliMpConstants.h"
51 #include <Riostream.h>
55 ClassImp(AliMpSectorSegmentation)
59 const Double_t AliMpSectorSegmentation::fgkS1 = 100000.;
60 const Double_t AliMpSectorSegmentation::fgkS2 = 1000.;
63 //______________________________________________________________________________
64 AliMpSectorSegmentation::AliMpSectorSegmentation(
65 const AliMpSector* sector, Bool_t own)
66 : AliMpVSegmentation(),
74 /// Standard constructor
76 AliDebugStream(1) << "this = " << this << endl;
78 fPadBuffer = new AliMpPad(AliMpPad::Invalid());
80 FillPadDimensionsMap();
83 //______________________________________________________________________________
84 AliMpSectorSegmentation::AliMpSectorSegmentation()
85 : AliMpVSegmentation(),
93 /// Default constructor
95 AliDebugStream(1) << "this = " << this << endl;
98 //______________________________________________________________________________
99 AliMpSectorSegmentation::~AliMpSectorSegmentation()
103 AliDebugStream(1) << "this = " << this << endl;
105 if ( fIsOwner ) delete fkSector;
115 //_____________________________________________________________________________
117 AliMpSectorSegmentation::GetAllElectronicCardIDs(TArrayI& ecn) const
119 /// Fill the array ecn with all manuIds
121 GetSector()->GetAllMotifPositionsIDs(ecn);
125 //______________________________________________________________________________
126 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
128 /// Convert the two vector to long.
130 return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
133 //______________________________________________________________________________
134 TVector2 AliMpSectorSegmentation::GetVector(Long_t index) const
136 /// Convert the long index to twovector.
138 return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
139 (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
143 //______________________________________________________________________________
144 void AliMpSectorSegmentation::FillPadDimensionsMap()
146 /// Fill the maps between zone ids and pad dimensions.
148 for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
149 AliMpZone* zone = fkSector->GetZone(i+1);
150 Int_t zoneID = zone->GetID();
152 if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
156 fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
160 << "Filling fPadDimensions[" << zoneID*10 << "] = ("
161 << zone->GetPadDimensions().X() << ", "
162 << zone->GetPadDimensions().Y() << ")" << endl;
164 fPadDimensionsMap.Add((Long_t)(zoneID*10),
165 GetIndex(zone->GetPadDimensions()));
171 for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
172 AliMpSubZone* subZone = zone->GetSubZone(j);
173 AliMpVMotif* motif = subZone->GetMotif();
175 for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
176 Int_t index = zoneID*10 + subIndex++;
178 fPadDimensionsMap[index] = motif->GetPadDimensions(k);
182 << "Filling fPadDimensions[" << index << "] = ("
183 << motif->GetPadDimensions(k).X() << ", "
184 << motif->GetPadDimensions(k).Y() << ") motif "
185 << motif->GetID().Data() << "-" << k << endl;
187 fPadDimensionsMap.Add((Long_t)(index),
188 GetIndex(motif->GetPadDimensions(k)));
196 //______________________________________________________________________________
198 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
200 /// Find the motif position which contains the given pad indices
201 /// return 0 if not found
203 switch (fkSector->GetDirection()) {
205 // Case where all the pads have the same size along X direction
207 for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
208 AliMpRow* row = fkSector->GetRow(irow);
209 if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
210 row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
212 for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
213 AliMpVRowSegment* seg = row->GetRowSegment(iseg);
214 if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
215 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
217 AliMpMotifPosition* motifPos;
218 for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
220 = fkSector->GetMotifMap()
221 ->FindMotifPosition(seg->GetMotifPositionId(imot));
222 if (motifPos && motifPos->HasPad(indices)) return motifPos;
231 ////////////////////////////////////////////////////////////////////////////////
233 // Case where all the pads have the same size along Y direction
234 // look for the row which contains the indices
237 for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
238 row = fkSector->GetRow(irow);
239 AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
240 if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
241 lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
242 // NOTE : We use the last row segment in order to ensure that
243 // we are not on a special motif
245 if (irow==fkSector->GetNofRows()) return 0;
246 // look for the row segment, in the found row, which contains the indices
247 AliMpVRowSegment* seg=0;
249 for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
250 seg = row->GetRowSegment(iseg);
251 if (seg->HasIndices(indices)) break;
253 if (iseg==row->GetNofRowSegments()) return 0;
255 // look for the motif position which contains the indices
256 AliMpMotifPosition* motifPos=0;
258 for (imot=0;imot<seg->GetNofMotifs();++imot) {
260 = fkSector->GetMotifMap()
261 ->FindMotifPosition(seg->GetMotifPositionId(imot));
262 if (motifPos && motifPos->HasPad(indices)) break;
264 if (imot==seg->GetNofMotifs()) return 0;
272 //______________________________________________________________________________
274 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition,
277 /// Find the first valid pad from starting position in the
278 /// direction of pad lines up to distance dx.
280 // Define step limits
281 Double_t stepX = fkSector->GetMinPadDimensions().X();
283 // Search in X direction
285 TVector2 position(startPosition);
287 pad = PadByPosition(position, false);
288 position += TVector2(stepX, 0.);
290 while ( !pad.IsValid() &&
291 position.X() - fkSector->GetMaxPadDimensions().X() < maxX );
293 // Invalidate pad if it is outside limits
294 if ( (pad.Position().X() - pad.Dimensions().X()) > maxX )
295 pad = AliMpPad::Invalid();
300 //______________________________________________________________________________
302 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition,
305 /// Find the first valid pad from starting position in the
306 /// direction of pad columns up to distance dx.
308 // Define step limits
309 Double_t stepY = fkSector->GetMinPadDimensions().Y();
311 // Search in Y direction
313 TVector2 position(startPosition);
315 pad = PadByPosition(position, false);
316 position += TVector2(0., stepY);
318 while ( !pad.IsValid() &&
319 position.Y() - fkSector->GetMaxPadDimensions().Y()< maxY );
321 // Invalidate pad if it is outside limits
322 if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY)
323 pad = AliMpPad::Invalid();
332 //______________________________________________________________________________
334 AliMpSectorSegmentation::CreateIterator() const
336 /// Create the sector iterator
338 return new AliMpSectorPadIterator(fkSector);
341 //______________________________________________________________________________
343 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
345 /// Create the area iterator.
347 switch (fkSector->GetDirection()) {
349 case AliMp::kX: return new AliMpSectorAreaVPadIterator(this, area);
351 case AliMp::kY: return new AliMpSectorAreaHPadIterator(this, area);
355 Fatal("CreateIterator", "Incomplete switch on Sector direction");
359 //______________________________________________________________________________
361 AliMpSectorSegmentation::GetNeighbours(const AliMpPad& pad, TObjArray& neighbours,
363 Bool_t includeVoid) const
365 /// Uses default implementation
366 return AliMpVSegmentation::GetNeighbours(pad,neighbours,includeSelf,includeVoid);
369 //______________________________________________________________________________
371 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
372 Bool_t includeCenter) const
374 /// Create the neighbours pad iterator.
376 return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
379 //______________________________________________________________________________
381 AliMpSectorSegmentation::Dimensions() const
383 return GetSector()->Dimensions();
386 //______________________________________________________________________________
388 AliMpSectorSegmentation::PlaneType() const
390 return GetSector()->GetPlaneType();
393 //______________________________________________________________________________
395 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location,
396 Bool_t warning) const
398 /// Find the pad which corresponds to the given location
400 if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
402 AliMpMotifPosition* motifPos =
403 fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
405 if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
406 return AliMpPad::Invalid();
409 AliMpVMotif* motif = motifPos->GetMotif();
410 AliMpIntPair localIndices =
411 motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
412 if (! localIndices.IsValid()) {
413 if (warning) Warning("PadByLocation","The pad number doesn't exists");
414 return AliMpPad::Invalid();
416 TVector2 delta = motif->PadPositionLocal(localIndices);
417 return (*fPadBuffer) = AliMpPad(location,
418 motifPos->GlobalIndices(localIndices),
419 motifPos->Position()+delta,
420 motif->GetPadDimensions(localIndices));
423 //______________________________________________________________________________
425 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
426 Bool_t warning ) const
428 /// Find the pad which corresponds to the given indices
430 if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);
432 AliMpMotifPosition* motifPos = FindMotifPosition(indices);
435 Warning("PadByIndices","Pad indices not contained in any motif!");
436 return AliMpPad::Invalid();
439 // retrieve the local indices in the found motif
440 AliMpVMotif* motif = motifPos->GetMotif();
441 AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
443 AliMpConnection* connection=
444 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
447 if (warning) Warning("PadByIndices","No connection with the given indices!");
448 return AliMpPad::Invalid();
451 TVector2 localPos = motif->PadPositionLocal(localIndices);
454 = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
456 motifPos->Position()+localPos,
457 motif->GetPadDimensions(localIndices));
460 //______________________________________________________________________________
462 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
463 Bool_t warning) const
465 /// Find the pad which corresponds to the given position
467 if ((*fPadBuffer).Position().X()==position.X() &&
468 (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);
470 Int_t motifPosID = fkSector->FindMotifPositionId(position);
471 AliMpMotifPosition* motifPos
472 = fkSector->GetMotifMap()
473 ->FindMotifPosition(motifPosID);
476 if (warning) Warning("PadByPosition","Position outside limits");
477 return AliMpPad::Invalid();
480 AliMpVMotif* motif = motifPos->GetMotif();
481 AliMpIntPair localIndices
482 = motif->PadIndicesLocal(position-motifPos->Position());
484 AliMpConnection* connect =
485 motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
488 if (warning) Warning("PadByPosition","Position outside motif limits");
489 return AliMpPad::Invalid();
493 = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
494 motifPos->GlobalIndices(localIndices),
495 motifPos->Position()+motif->PadPositionLocal(localIndices),
496 motif->GetPadDimensions(localIndices));
500 //______________________________________________________________________________
502 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition,
503 Double_t distance) const
505 /// Find the first valid pad from starting position in the
506 /// direction of pad lines/columns up to the specified distance.
507 /// Pad lines are the lines of pads in the sector with constant pad y size,
508 /// pad columns are the columns of pads in the sector with constant pad x size.
510 switch (fkSector->GetDirection()) {
512 case AliMp::kX: return PadByYDirection(startPosition, distance);
514 case AliMp::kY: return PadByXDirection(startPosition, distance);
518 Fatal("PadByDirection", "Incomplete switch on Sector direction");
519 return AliMpPad::Invalid();
522 //______________________________________________________________________________
523 Int_t AliMpSectorSegmentation::MaxPadIndexX() const
525 /// Return maximum pad index in x
527 return fkSector->GetMaxPadIndices().GetFirst();
530 //______________________________________________________________________________
531 Int_t AliMpSectorSegmentation::MaxPadIndexY() const
533 /// Return maximum pad index in y
535 return fkSector->GetMaxPadIndices().GetSecond();
538 //______________________________________________________________________________
539 Int_t AliMpSectorSegmentation::NofPads() const
541 /// Return number of pads defined in the sector
543 return fkSector->GetNofPads();
546 //______________________________________________________________________________
547 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
549 /// Does the pad specified by \a indices exist ?
551 return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
554 //______________________________________________________________________________
555 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
557 /// Does the motif position specified by motifPositionID exist ?
559 return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
562 //______________________________________________________________________________
563 TVector2 AliMpSectorSegmentation::GetMinPadDimensions() const
565 /// Returne the dimensions of the smallest pad.
567 return fkSector->GetMinPadDimensions();
570 //______________________________________________________________________________
571 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
573 /// Return the zone index of the zone containing the specified pad.
574 /// This zone index is different from the zone ID,
575 /// as it is unique for each pad dimensions.
576 /// It is composed in this way:
577 /// zoneID*10 + specific index
578 /// Specific index is present only for zones containing special motifs.
580 if (!pad.IsValid()) {
581 if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
586 PadDimensionsMapCIterator it;
587 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
588 if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
594 PadDimensionsMapCIterator it(&fPadDimensionsMap);
596 while ( it.Next(key, value) ) {
597 TVector2 dimensions = GetVector(value);
598 if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
602 AliError(Form("fPadDimensionsMap size is %d",fPadDimensionsMap.GetSize()));
606 // Should never happen
608 << "Zone(AliMpPad pad) not found, where pad is: " << pad << endl;
612 //______________________________________________________________________________
614 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
616 /// Return the pad dimensions for the zone with the specified zone index.
619 PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
620 if (it != fPadDimensionsMap.end()) return it->second;
624 Long_t value = fPadDimensionsMap.GetValue(zone);
625 if (value) return GetVector(value);
628 if (warning) Warning("PadDimensions(zone)", "not found");
632 //______________________________________________________________________________
633 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
635 /// Verify that all methods for retrieving pads are consistents between them.
636 /// Return true if the pad with specified indices was found and verified,
639 if (!HasPad(indices)) return false;
641 // Verify the indice->location->position->indice way
642 AliMpIntPair location = PadByIndices(indices).GetLocation();
643 TVector2 position = PadByLocation(location).Position();
644 AliMpIntPair retIndices = PadByPosition(position).GetIndices();
646 if (retIndices != indices) {
647 cout << "Pad " << indices << " lead to inconsistency" << endl;
648 cout << "in indice->location->position->indice way..." << endl;
649 cout << "starting from " << indices << "-->" << location << "-->"
650 << '(' << position.X() << ',' << position.Y() << ')'
651 << " and retIndices: " << retIndices << endl;
655 // Verify the indice->position->location->indice way
656 position = PadByIndices(indices).Position();
657 location = PadByPosition(position).GetLocation();
658 retIndices = PadByLocation(location).GetIndices();
660 if (retIndices != indices) {
661 cout << "Pad " << indices << " lead to inconsistency" << endl;
662 cout << "in indice->position->location->indice way..." <<endl;
663 cout << "starting from " << indices
664 << " and retIndices: " << retIndices << endl;
670 //______________________________________________________________________________
672 AliMpSectorSegmentation::Print(Option_t* opt) const
676 fkSector->Print(opt);
679 //______________________________________________________________________________
680 void AliMpSectorSegmentation::PrintZones() const
682 /// Print all zones and pads dimensions from the map.
684 cout << "Zones: " << endl;
687 PadDimensionsMapCIterator it;
688 for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
689 cout << " zone: " << setw(4) << it->first;
690 cout << " pad dimensions: ( "
691 << it->second.X() << ", " << it->second.Y() << ")" << endl;
696 PadDimensionsMapCIterator it(&fPadDimensionsMap);
698 while ( it.Next(key, value) ) {
699 //cout << "Iterating over: " << key << ", " << value << endl;
700 TVector2 dimensions = GetVector(value);
702 cout << " zone: " << setw(4) << key;
703 cout << " pad dimensions: ( "
704 << dimensions.X() << ", " << dimensions.Y() << ")" << endl;