]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpSectorSegmentation.cxx
03a9a881c9b6a1af01d04e6e360c0b41b205ef3b
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpSectorSegmentation.cxx
1 // $Id$
2 // Category: sector
3 //
4 // Class AliMpSectorSegmentation
5 // -----------------------------
6 // Class describing the segmentation of the sector.        
7 // Provides methods related to pads:
8 // conversion between pad indices, pad location, pad position;
9 // finding pad neighbour.
10 //
11 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
12
13 #include <Riostream.h>
14 #include <TMath.h>
15 #include <TError.h>
16
17 #include "AliMpSectorSegmentation.h"
18 #include "AliMpSector.h"
19 #include "AliMpZone.h"
20 #include "AliMpSubZone.h"
21 #include "AliMpRow.h"
22 #include "AliMpVRowSegment.h"
23 #include "AliMpMotifMap.h"
24 #include "AliMpVMotif.h"
25 #include "AliMpMotifPosition.h"
26 #include "AliMpConnection.h"
27 #include "AliMpNeighboursPadIterator.h"
28 #include "AliMpSectorAreaHPadIterator.h"
29 #include "AliMpSectorAreaVPadIterator.h"
30 #include "AliMpIntPair.h"
31 #include "AliMpArea.h"
32 #include "AliMpConstants.h"
33
34 ClassImp(AliMpSectorSegmentation)
35
36 #ifdef WITH_ROOT
37 const Double_t AliMpSectorSegmentation::fgkS1 = 10000.;
38 const Double_t AliMpSectorSegmentation::fgkS2 = 100.;
39 #endif
40
41 //______________________________________________________________________________
42 AliMpSectorSegmentation::AliMpSectorSegmentation(const AliMpSector* sector) 
43   : AliMpVSegmentation(),
44     fkSector(sector),
45     fMaxIndexInX(0),
46     fMaxIndexInY(0)
47 {
48 //
49   fPadBuffer = new AliMpPad(AliMpPad::Invalid());
50   
51   FillPadDimensionsMap();
52 }
53
54 //______________________________________________________________________________
55 AliMpSectorSegmentation::AliMpSectorSegmentation() 
56   : AliMpVSegmentation(),
57     fkSector(0),
58     fPadBuffer(0),
59     fPadDimensionsMap(),      
60     fMaxIndexInX(0),
61     fMaxIndexInY(0)
62 {
63 //
64 }
65
66 //_____________________________________________________________________________
67 AliMpSectorSegmentation::AliMpSectorSegmentation(
68                                     const AliMpSectorSegmentation& right) 
69   : AliMpVSegmentation(right) {
70 // 
71   Fatal("AliMpSectorSegmentation", "Copy constructor not provided.");
72 }
73
74 //______________________________________________________________________________
75 AliMpSectorSegmentation::~AliMpSectorSegmentation() {
76 // 
77   delete fPadBuffer;
78 }
79
80 //
81 // operators
82 //
83
84 //_____________________________________________________________________________
85 AliMpSectorSegmentation& 
86 AliMpSectorSegmentation::operator=(const AliMpSectorSegmentation& right)
87 {
88   // check assignement to self
89   if (this == &right) return *this;
90
91   Fatal("operator =", "Assignement operator not provided.");
92     
93   return *this;  
94 }    
95
96 //
97 // private methods
98 //
99
100 #ifdef WITH_ROOT
101 //______________________________________________________________________________
102 Long_t AliMpSectorSegmentation::GetIndex(const TVector2& vector2) const
103 {
104 // Converts the two vector to long.
105 // ---
106
107   return Long_t(TMath::Floor((vector2.X()*fgkS1 + vector2.Y())*fgkS2));
108 }  
109
110 //______________________________________________________________________________
111 TVector2  AliMpSectorSegmentation::GetVector(Long_t index) const
112 {
113 // Converts the long index to twovector.
114 // ---
115
116   return TVector2( TMath::Floor(index/fgkS1)/fgkS2,
117                    (index - TMath::Floor(index/fgkS1)*fgkS1)/fgkS2 );
118 }  
119 #endif
120
121 //______________________________________________________________________________
122 void AliMpSectorSegmentation::FillPadDimensionsMap()
123 {
124 // Fills the maps between zone ids and pad dimensions.
125 // ---
126
127   for (Int_t i=0; i<fkSector->GetNofZones(); i++) {
128     AliMpZone* zone   = fkSector->GetZone(i+1);
129     Int_t  zoneID = zone->GetID();
130     
131     if (!AliMpConstants::IsEqual(zone->GetPadDimensions(), TVector2())) {
132
133       // regular zone
134 #ifdef WITH_STL
135       fPadDimensionsMap[zoneID*10] = zone->GetPadDimensions();
136 #endif
137 #ifdef WITH_ROOT
138      fPadDimensionsMap.Add((Long_t)(zoneID*10), 
139                             GetIndex(zone->GetPadDimensions()));
140 #endif
141     }
142     else {
143       // special zone
144       Int_t subIndex = 0;
145       for (Int_t j=0; j<zone->GetNofSubZones(); j++) {
146         AliMpSubZone* subZone = zone->GetSubZone(j);
147         AliMpVMotif*  motif = subZone->GetMotif();
148         
149         for (Int_t k=0; k<motif->GetNofPadDimensions(); k++) {
150           Int_t index = zoneID*10 +  subIndex++;
151 #ifdef WITH_STL
152           fPadDimensionsMap[index] = motif->GetPadDimensions(k);
153 #endif
154 #ifdef WITH_ROOT
155           fPadDimensionsMap.Add((Long_t)(index), 
156                             GetIndex(motif->GetPadDimensions(k)));
157 #endif
158         }
159       }   
160     }     
161   }      
162 }
163
164 //______________________________________________________________________________
165 AliMpMotifPosition* 
166 AliMpSectorSegmentation::FindMotifPosition(const AliMpIntPair& indices) const
167 {
168 // Find the motif position which contains the given pad indices
169 // return 0 if not found
170 // ---
171
172   switch (fkSector->GetDirection()) {
173     case kX : {
174     // Case where all the pads have the same size along X direction
175
176       for (Int_t irow=0; irow<fkSector->GetNofRows(); ++irow) {
177         AliMpRow* row = fkSector->GetRow(irow);
178         if (row->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
179             row->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
180             
181            for (Int_t iseg=0;iseg<row->GetNofRowSegments();++iseg){
182             AliMpVRowSegment* seg = row->GetRowSegment(iseg);
183             if (seg->GetLowIndicesLimit().GetFirst()<=indices.GetFirst() &&
184                 seg->GetHighIndicesLimit().GetFirst()>=indices.GetFirst()) {
185
186               AliMpMotifPosition* motifPos;
187               for (Int_t imot=0;imot<seg->GetNofMotifs();++imot) {
188                 motifPos 
189                   = fkSector->GetMotifMap()
190                     ->FindMotifPosition(seg->GetMotifPositionId(imot));
191                 if (motifPos && motifPos->HasPad(indices)) return motifPos;
192               }
193             }
194           }
195         }
196       }
197       return 0;
198     }
199     ////////////////////////////////////////////////////////////////////////////////
200     case kY : {
201       // Case where all the pads have the same size along Y direction   
202       // look for the row which contains the indices
203       AliMpRow* row=0;
204       Int_t irow;
205       for (irow=0; irow<fkSector->GetNofRows(); ++irow) {
206         row = fkSector->GetRow(irow);
207         AliMpVRowSegment* lastSeg = row->GetRowSegment(row->GetNofRowSegments()-1);
208         if (lastSeg->GetLowIndicesLimit().GetSecond()<=indices.GetSecond() &&
209             lastSeg->GetHighIndicesLimit().GetSecond()>=indices.GetSecond()) break;
210         // NOTE : We use the last row segment in order to ensure that
211         // we are not on a special motif
212       }
213       if (irow==fkSector->GetNofRows()) return 0;
214       // look for the row segment, in the found row, which contains the indices
215       AliMpVRowSegment* seg=0;
216       Int_t iseg;
217       for (iseg=0;iseg<row->GetNofRowSegments();++iseg){
218         seg = row->GetRowSegment(iseg);
219         if (seg->HasIndices(indices)) break;
220       }
221       if (iseg==row->GetNofRowSegments()) return 0;
222   
223       // look for the motif position which contains the indices
224       AliMpMotifPosition* motifPos=0;
225       Int_t imot=0;
226       for (imot=0;imot<seg->GetNofMotifs();++imot) {
227         motifPos 
228           = fkSector->GetMotifMap()
229             ->FindMotifPosition(seg->GetMotifPositionId(imot));
230         if (motifPos && motifPos->HasPad(indices)) break;
231       }      
232       if (imot==seg->GetNofMotifs()) return 0;
233    
234       return motifPos;      
235     }
236     default: return 0;
237   }
238 }
239
240 //______________________________________________________________________________
241 AliMpPad 
242 AliMpSectorSegmentation::PadByXDirection(const TVector2& startPosition, 
243                                          Double_t maxX) const
244 {
245 // Find the first valid pad from starting position in the
246 // direction of pad lines up to distance dx.
247 // ---
248
249   // Define step limits
250   Double_t  stepX = fkSector->GetMinPadDimensions().X();
251  
252   // Search in X direction
253   AliMpPad pad;
254   TVector2 position(startPosition);    
255   do {
256     pad = PadByPosition(position, false);
257     position += TVector2(stepX, 0.);
258   }   
259   while ( !pad.IsValid() && position.X() < maxX ); 
260   
261   // Invalidate pad if it is outside limits
262   if ((pad.Position().X() - pad.Dimensions().X()) > maxX) 
263     pad = AliMpPad::Invalid();
264
265   return pad;
266 }
267
268 //______________________________________________________________________________
269 AliMpPad 
270 AliMpSectorSegmentation::PadByYDirection(const TVector2& startPosition, 
271                                          Double_t maxY) const
272 {
273 // Find the first valid pad from starting position in the
274 // direction of pad columns up to distance dx.
275 // ---
276   
277   // Define step limits
278   Double_t stepY = fkSector->GetMinPadDimensions().Y();
279  
280   // Search in Y direction
281   AliMpPad pad;
282   TVector2 position(startPosition);    
283   do {
284     pad = PadByPosition(position, false);
285     position += TVector2(0., stepY);
286   }   
287   while ( !pad.IsValid() && position.Y() < maxY ); 
288   
289   // Invalidate pad if it is outside limits
290   if ((pad.Position().Y() - pad.Dimensions().Y()) > maxY) 
291     pad = AliMpPad::Invalid();
292
293   return pad;
294 }
295
296 //______________________________________________________________________________
297 AliMpVPadIterator* AliMpSectorSegmentation::CreateIterator() const
298 {
299 // The inherited method cannot be used
300
301   Fatal("CreateIterator", "Center pad has to be specified.");
302   return 0;
303 }
304   
305
306 //
307 // public methods
308 //
309
310 //______________________________________________________________________________
311 AliMpVPadIterator* 
312 AliMpSectorSegmentation::CreateIterator(const AliMpArea& area) const
313 {
314 // Creates the are iterator. 
315 // (The inherited method cannot be used)
316 // ---
317
318   switch (fkSector->GetDirection()) {
319   
320     case kX: return new AliMpSectorAreaVPadIterator(this, area);
321              ;;
322     case kY: return new AliMpSectorAreaHPadIterator(this, area);
323              ;;
324   }
325   
326   Fatal("CreateIterator", "Incomplete switch on Sector direction");
327   return 0;  
328 }   
329   
330 //______________________________________________________________________________
331 AliMpVPadIterator* 
332 AliMpSectorSegmentation::CreateIterator(const AliMpPad& centerPad,
333                                         Bool_t includeCenter) const
334 {
335 // Creates the neighbours pad iterator.
336 // (The inherited method cannot be used)
337
338   return new AliMpNeighboursPadIterator(this, centerPad, includeCenter);
339 }   
340   
341 //______________________________________________________________________________
342 AliMpPad 
343 AliMpSectorSegmentation::PadByLocation(const AliMpIntPair& location, 
344                                        Bool_t warning) const
345 {
346 // Find the pad which corresponds to the given location
347   
348   if ((*fPadBuffer).GetLocation()==location) return (*fPadBuffer);
349   
350   AliMpMotifPosition* motifPos = 
351     fkSector->GetMotifMap()->FindMotifPosition(location.GetFirst());
352   if (!motifPos){
353     if (warning) Warning("PadByLocation","The pad motif position ID doesn't exists");
354     return AliMpPad::Invalid();
355   }
356   
357   AliMpVMotif* motif = motifPos->GetMotif();
358   AliMpIntPair localIndices = 
359     motif->GetMotifType()->FindLocalIndicesByGassiNum(location.GetSecond());
360   if (! localIndices.IsValid()) {
361     if (warning) Warning("PadByLocation","The pad number doesn't exists");
362     return AliMpPad::Invalid();
363   }
364   TVector2 delta = motif->PadPositionLocal(localIndices);
365   return (*fPadBuffer) = AliMpPad(location,
366               motifPos->GlobalIndices(localIndices),
367               motifPos->Position()+delta,
368               motif->GetPadDimensions(localIndices));
369
370 }
371 //______________________________________________________________________________
372 AliMpPad 
373 AliMpSectorSegmentation::PadByIndices(const AliMpIntPair& indices,
374                                       Bool_t warning ) const
375 {
376 // Find the pad which corresponds to the given indices  
377
378   if ((*fPadBuffer).GetIndices()==indices) return (*fPadBuffer);    
379    
380   AliMpMotifPosition* motifPos = FindMotifPosition(indices);
381   if (!motifPos) {    
382     if (warning) {
383       cout << "indices " <<  indices << endl;
384       Warning("PadByIndices","Pad indices not contained in any motif!");
385     }  
386     return AliMpPad::Invalid();
387   }
388   
389   // retrieve the local indices in the found motif
390   AliMpVMotif* motif = motifPos->GetMotif();
391   AliMpIntPair localIndices = indices - motifPos->GetLowIndicesLimit();
392   
393   AliMpConnection* connection=
394     motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
395     
396   if (!connection){
397     if (warning) Warning("PadByIndices","No connection with the given indices!");
398     return AliMpPad::Invalid();
399   }
400
401   TVector2 localPos = motif->PadPositionLocal(localIndices);
402
403   return (*fPadBuffer) 
404     = AliMpPad(AliMpIntPair(motifPos->GetID(),connection->GetGassiNum()),
405                indices,
406                motifPos->Position()+localPos,
407                motif->GetPadDimensions(localIndices)); 
408
409 }
410 //______________________________________________________________________________
411 AliMpPad 
412 AliMpSectorSegmentation::PadByPosition(const TVector2& position,
413                                        Bool_t warning) const
414 {
415 // Find the pad which corresponds to the given position
416
417   if ((*fPadBuffer).Position().X()==position.X() && 
418       (*fPadBuffer).Position().Y()==position.Y()) return (*fPadBuffer);  
419
420   Int_t motifPosID = fkSector->FindMotifPositionId(position);
421   AliMpMotifPosition* motifPos 
422     = fkSector->GetMotifMap()
423         ->FindMotifPosition(motifPosID);
424     
425   if (!motifPos){
426     if (warning) Warning("PadByPosition","Position outside limits");
427     return AliMpPad::Invalid();
428   }
429
430   AliMpVMotif* motif =  motifPos->GetMotif();  
431   AliMpIntPair localIndices 
432     = motif->PadIndicesLocal(position-motifPos->Position());
433     
434   AliMpConnection* connect = 
435     motif->GetMotifType()->FindConnectionByLocalIndices(localIndices);
436
437    if (!connect){
438     if (warning) Warning("PadByPosition","Position outside motif limits");
439     return AliMpPad::Invalid();
440   }
441   
442   return (*fPadBuffer)
443     = AliMpPad(AliMpIntPair(motifPosID,connect->GetGassiNum()),
444                motifPos->GlobalIndices(localIndices),
445                motifPos->Position()+motif->PadPositionLocal(localIndices),
446                motif->GetPadDimensions(localIndices));
447
448 }
449
450 //______________________________________________________________________________
451 AliMpPad 
452 AliMpSectorSegmentation::PadByDirection(const TVector2& startPosition, 
453                                         Double_t distance) const
454 {
455 // Find the first valid pad from starting position in the
456 // direction of pad lines/columns up to the specified distance.
457 // Pad lines are the lines of pads in the sector with constant pad y size,
458 // pad columns are the columns of pads in the sector with constant pad x size. 
459 // ---
460
461   switch (fkSector->GetDirection()) {
462   
463     case kX: return PadByYDirection(startPosition, distance);
464              ;;
465     case kY: return PadByXDirection(startPosition, distance);
466              ;;
467   }
468   
469   Fatal("PadByDirection", "Incomplete switch on Sector direction");
470   return AliMpPad::Invalid();  
471 }
472
473 //______________________________________________________________________________
474 Int_t  AliMpSectorSegmentation::MaxPadIndexX()
475 {
476 // Return maximum pad index in x
477
478   if (fMaxIndexInX) return fMaxIndexInX;
479   
480   for (Int_t i=0; i<fkSector->GetNofRows(); i++) {
481     Int_t ixh = fkSector->GetRow(i)->GetHighIndicesLimit().GetFirst();
482     if ( ixh > fMaxIndexInX ) fMaxIndexInX = ixh;
483   }  
484   return fMaxIndexInX;
485 }
486
487 //______________________________________________________________________________
488 Int_t  AliMpSectorSegmentation::MaxPadIndexY()
489 {
490 // Return maximum pad index in y
491
492   if (fMaxIndexInY) return fMaxIndexInY;
493   
494   for (Int_t i=0; i<fkSector->GetNofRows(); i++) {
495     Int_t iyh = fkSector->GetRow(i)->GetHighIndicesLimit().GetSecond();
496     if ( iyh > fMaxIndexInY ) fMaxIndexInY = iyh;
497   }  
498   return fMaxIndexInY;
499 }
500
501 //______________________________________________________________________________
502 Bool_t AliMpSectorSegmentation::HasPad(const AliMpIntPair& indices) const
503 {
504 // Does the pad specified by <indices> exist ?
505 // ---
506
507   return PadByIndices(indices,kFALSE) != AliMpPad::Invalid();
508 }
509
510 //______________________________________________________________________________
511 Bool_t AliMpSectorSegmentation::HasMotifPosition(Int_t motifPositionID) const
512 {
513 // Does the motif position specified by motifPositionID exist ?
514 // ---
515
516   return (fkSector->GetMotifMap()->FindMotifPosition(motifPositionID) != 0);
517 }
518
519 //______________________________________________________________________________
520 TVector2  AliMpSectorSegmentation::GetMinPadDimensions() const
521 {
522 // Returnes the dimensions of the smallest pad.
523 // ---
524
525   return fkSector->GetMinPadDimensions();
526 }  
527
528 //______________________________________________________________________________
529 Int_t AliMpSectorSegmentation::Zone(const AliMpPad& pad, Bool_t warning) const
530 {
531 // Returns the zone index of the zone containing the specified pad.
532 // This zone index is different from the zone ID,
533 // as it is unique for each pad dimensions.
534 // It is composed in this way:
535 //   zoneID*10 + specific index 
536 // Specific index is present only for zones containing special motifs.
537 // ---
538
539   if (!pad.IsValid()) {
540     if (warning) Warning("Zone(AliMpPad)", "Invalid pad");
541     return 0;
542   }  
543
544 #ifdef WITH_STL
545   PadDimensionsMapCIterator it;
546   for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
547     if (AliMpConstants::IsEqual(it->second, pad.Dimensions()))
548       return it->first;
549   }
550 #endif
551
552 #ifdef WITH_ROOT
553   PadDimensionsMapCIterator it(&fPadDimensionsMap);
554   Long_t key, value;
555   while ( it.Next(key, value) ) {
556     TVector2 dimensions =  GetVector(value);
557     if (AliMpConstants::IsEqual(dimensions, pad.Dimensions()))
558       return (Int_t)key;
559   } 
560 #endif
561
562   // Should never happen
563   Error("Zone(AliMpPad)", "not found");
564   cerr << pad << endl;
565   return 0;
566 }  
567
568 //______________________________________________________________________________
569 TVector2 
570 AliMpSectorSegmentation::PadDimensions(Int_t zone, Bool_t warning) const
571 {
572 // Returns the pad dimensions for the zone with the specified zone index.
573 // ---
574
575 #ifdef WITH_STL
576   PadDimensionsMapCIterator it = fPadDimensionsMap.find(zone);
577   if (it != fPadDimensionsMap.end()) return it->second;
578 #endif
579
580 #ifdef WITH_ROOT
581   Long_t value = fPadDimensionsMap.GetValue(zone);
582   if (value) return GetVector(value);
583 #endif
584
585   if (warning) Warning("PadDimensions(zone)", "not found");
586   return TVector2();
587 }  
588
589 //______________________________________________________________________________
590 Bool_t AliMpSectorSegmentation::CircleTest(const AliMpIntPair& indices) const
591 {
592 // Verifies that all methods for retrieving pads are consistents between them.
593 // Returns true if the pad with specified indices was found and verified,
594 // false otherwise.
595 // ---
596
597   if (!HasPad(indices)) return false;
598
599   // Verify the indice->location->position->indice way
600   AliMpIntPair location = PadByIndices(indices).GetLocation();
601   TVector2 position = PadByLocation(location).Position();
602   AliMpIntPair retIndices = PadByPosition(position).GetIndices();
603     
604   if (retIndices != indices) {
605     cout << "Pad " << indices << " lead to inconsistency" << endl;
606     cout << "in indice->location->position->indice way..." << endl;
607     cout << "starting from " << indices << "-->" << location << "-->" 
608          << '(' << position.X() << ',' << position.Y() << ')'
609          << " and retIndices: " << retIndices << endl;
610   }
611     
612     
613   // Verify the indice->position->location->indice way    
614   position = PadByIndices(indices).Position();
615   location = PadByPosition(position).GetLocation();
616   retIndices = PadByLocation(location).GetIndices();
617
618   if (retIndices != indices) {
619     cout << "Pad " << indices << " lead to inconsistency" << endl;
620     cout << "in indice->position->location->indice way..." <<endl;
621     cout << "starting from " << indices 
622          << " and retIndices: " << retIndices << endl;
623   }
624   
625   return true;
626 }
627
628 //______________________________________________________________________________
629 void AliMpSectorSegmentation::PrintZones() const
630 {
631 // Prints all zones and pads dimensions from the map.
632 // ---
633
634   cout << "Zones: " << endl;
635
636 #ifdef WITH_STL
637   PadDimensionsMapCIterator it;
638   for (it = fPadDimensionsMap.begin(); it != fPadDimensionsMap.end(); ++it) {
639     cout << "    zone: " <<   setw(4) << it->first;
640     cout << "    pad dimensions: ( " 
641          << it->second.X() << ", " << it->second.Y() << ")" << endl; 
642   }
643 #endif
644
645 #ifdef WITH_ROOT
646   PadDimensionsMapCIterator it(&fPadDimensionsMap);
647   Long_t key, value;
648   while ( it.Next(key, value) ) {
649     //cout << "Iterating over: " << key << ", " << value << endl;
650     TVector2 dimensions =  GetVector(value);
651
652     cout << "    zone: " <<   setw(4) << key;
653     cout << "    pad dimensions: ( " 
654          << dimensions.X() << ", " << dimensions.Y() << ")" << endl; 
655   }
656 #endif
657 }
658