Further development on Mapping and Segmentation. Waiting for a critical review
[u/mrichter/AliRoot.git] / MUON / AliMUONSegmentationDetectionElement.cxx
index ee10b58375ae8e94b9bac97adb75663fba7430f1..2892a515119bb12e9b165a76e82b0ff486d21cee 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "TClonesArray.h"
 #include "TMap.h"
+#include "TMath.h"
 
 #include "AliMUONSegmentationDetectionElement.h"
 #include "AliMUONSegmentManuIndex.h"
@@ -56,6 +57,8 @@ const TString AliMUONSegmentationDetectionElement::fgkNonBendingExt = ".NonBendi
 AliMUONSegmentationDetectionElement::AliMUONSegmentationDetectionElement() : TObject()
 {
   //Default constructor
+  fWireD  = 0.25; // wire pitch in cm
+  fWireX0 = 0.;  // X0 position of the 1st wire
   fMapManuIndexIndex= 0x0;
   fMapIndexManuIndex= 0x0;
   fMapIndexPosition= 0x0;
@@ -90,6 +93,56 @@ AliMUONSegmentationDetectionElement::~AliMUONSegmentationDetectionElement(){
   fMapPositionIndex->Clear();
 }
 
+//__________________________________________________
+AliMUONSegmentIndex *  AliMUONSegmentationDetectionElement::FindIndexFromPosition(Float_t x, Float_t y, Int_t cathode)
+{
+  // Finding x_wire corresponding to x
+  Float_t x_wire = GetAnod(x);
+
+  //Finding pad corresponding to the position (x_wire, y) in a zone of size 3cm x 10cm or 10cm x 3cm depending on cathode plane
+  // 
+  Int_t ix_max = (cathode==0) ? TMath::Nint(5./AliMUONSegmentPosition::GetUnit())  :  TMath::Nint(1.5/AliMUONSegmentPosition::GetUnit());
+  Int_t iy_max = (cathode==0) ? TMath::Nint(1.5/AliMUONSegmentPosition::GetUnit()) :  TMath::Nint(5./AliMUONSegmentPosition::GetUnit());
+
+  AliMUONSegmentIndex * segmentindex =0x0;
+  AliMUONSegmentIndex * foundsegmentindex =0x0;
+  AliMUONSegmentPosition * segmentposition=0x0;
+  Int_t   ix, iy;
+  Float_t xt,yt;
+  Float_t distance = 99999999.;
+  //printf("%d %d \n",ix_max, iy_max);
+
+  for(ix=-ix_max; ix<ix_max; ix++) {
+    xt = x_wire + ((Float_t)ix)*AliMUONSegmentPosition::GetUnit();
+    for(iy=-iy_max; iy<iy_max; iy++) {
+      yt = y + ((Float_t)iy)*AliMUONSegmentPosition::GetUnit();
+      segmentindex = GetIndexFromPosition( xt, yt, cathode);
+      if ( segmentindex ) {
+       // segmentindex->Print();
+       segmentposition = GetPosition(segmentindex->GetName());
+       if ( segmentposition->Distance(x_wire, y) < distance ) {     
+         //printf("adfafa xt %f yt %f distance %f \n", xt, yt, segmentposition->Distance(xt,yt) );     
+         distance = segmentposition->Distance(x_wire,y);
+         foundsegmentindex = segmentindex;
+       }
+      }
+    }
+  }
+ if (!foundsegmentindex) {
+   Warning("FindIndexFromPosition","Not found Index for position x=%5.2f y=%5.2f \n",x,y);
+ }    
+ return foundsegmentindex;
+}
+
+//____________________________________________________-
+Float_t AliMUONSegmentationDetectionElement::GetAnod(Float_t xhit) const
+{
+  // Returns for a hit position xhit the position of the nearest anode wire    
+  Float_t wire= ( (xhit- fWireX0)>0 ) ? 
+    Int_t( (xhit- fWireX0)/fWireD )+0.5 : 
+    Int_t( (xhit- fWireX0)/fWireD )-0.5;
+  return fWireD*wire+fWireX0; 
+}
 //_________________________________________________
 AliMUONSegmentIndex * AliMUONSegmentationDetectionElement::GetIndex(Int_t manu, Int_t channel) const
 {
@@ -124,6 +177,26 @@ AliMUONSegmentManuIndex * AliMUONSegmentationDetectionElement::GetManuIndex( con
     return 0x0;
   }
 }
+//__________________________________________________
+void  AliMUONSegmentationDetectionElement::GetPadC(Int_t ix, Int_t iy, Int_t cathode, Float_t &x, Float_t &y )
+{
+  x = GetPosition(ix,iy,cathode)->GetXlocal();
+  y = GetPosition(ix,iy,cathode)->GetYlocal();
+}
+//__________________________________________________
+void  AliMUONSegmentationDetectionElement::GetPadI(Float_t x, Float_t y, Int_t cathode, Int_t &padx, Int_t &pady)
+{
+
+  AliMUONSegmentIndex * segmentindex = FindIndexFromPosition(x,y,cathode);
+
+  if (segmentindex) {
+    padx = segmentindex->GetPadX();
+    pady = segmentindex->GetPadX();
+  }
+  else {
+    Warning("GetPadI","Not found Index for position x=%5.2f y=%5.2f \n",x,y);
+  }    
+}
 //_________________________________________________
 AliMUONSegmentPosition  * AliMUONSegmentationDetectionElement::GetPosition(Int_t padx, Int_t pady, Int_t cathode ) const
 {
@@ -145,7 +218,7 @@ AliMUONSegmentPosition  * AliMUONSegmentationDetectionElement::GetPosition( cons
 AliMUONSegmentIndex * AliMUONSegmentationDetectionElement::GetIndexFromPosition(Float_t x, Float_t y, Int_t cathode) const
 {
   // Getting Index from position if position is a center pad position
-  return GetIndexFromPosition( AliMUONSegmentPosition::Name(x,y, cathode).Data() );
+  return GetIndexFromPosition( AliMUONSegmentPosition::Name(x, y, cathode).Data() );
 }
 //_________________________________________________
 AliMUONSegmentIndex * AliMUONSegmentationDetectionElement::GetIndexFromPosition( const char * PositionName) const
@@ -183,63 +256,11 @@ AliMUONSegmentIndex * AliMUONSegmentationDetectionElement::FindIndex(const char*
   }
 }
 //_________________________________________________
-AliMUONSegmentIndex * AliMUONSegmentationDetectionElement::FindIndexFromPosition(Float_t /*x*/, Float_t /*y*/, Int_t /*cathode*/ ) const
-{
-
- //  char * name = AliMUONSegmentPosition::Name(x,y);
-
-//   if (GetIndexFromPosition( AliMUONSegmentPosition::Name(x,y)) ) 
-//     return GetIndexFromPosition( AliMUONSegmentPosition::Name(x,y) );
-  
-//   Float_t xl= ((Int_t) x*10 )/10.;
-//   Float_t yl= ((Int_t) y*10 )/10.;
-//   Int_t ix,iy, ixp;
-  
-//   for(ix=1; ix<4; ix++) {
-//     xl = ((Int_t) 0.5+10.*(x +  ((Float_t) ix )*0.1))/10.; 
-//     for(iy=-ix; iy<ix+1; iy++) {
-//       printf("A %d and %d and %f and %f \n",ix,iy,xl,yl);
-//       yl = ((Int_t) 10.*(y +  ((Float_t) iy )*0.1))/10. ;
-//       sprintf(name,"%5.1f-%5.1f",xl, yl);
-//       if (GetIndexFromPosition(name)) break;
-//     }
-//     if (GetIndexFromPosition(name)) break;
-    
-
-//     for(ixp=ix-1; ixp>-ix-1; ixp--) {
-//       xl = ((Int_t) 0.5+10.*(x +  ((Float_t) ixp )*0.1))/10. ;
-//       printf("B %d and %d and %f and %f \n",ixp,ix, xl, yl);
-//       sprintf(name,"%5.1f-%5.1f",xl, yl);
-//       if (GetIndexFromPosition(name)) break;
-//     }
-//     if (GetIndexFromPosition(name)) break;
-    
-//     for(iy=ix-1; iy>-ix-1; iy--) {
-//       yl = ((Int_t) 0.5+10.*(y +  ((Float_t) iy )*0.1))/10. ;
-//       printf("C %d and %d and %f and %f \n",-ix,iy,xl,yl);
-//       sprintf(name,"%5.1f-%5.1f",xl, yl);
-//       if (GetIndexFromPosition(name)) break;
-//     }
-//     if (GetIndexFromPosition(name)) break;
-    
-//     for(ixp=-ix+1; ixp<ix+1; ixp++) {
-//       xl = ((Int_t) 0.5+10.*(x +  ((Float_t) ixp )*0.1))/10. ;
-//       printf("D %d and %d and %f and %f \n",ixp,-ix,xl,yl);
-//       sprintf(name,"%5.1f-%5.1f",xl, yl);
-//       if (GetIndexFromPosition(name)) break;
-//     }
-//     if (GetIndexFromPosition(name)) break;
-//   }
-//   return GetIndexFromPosition(name);
-  return 0x0;
-}
-//_________________________________________________
 void    AliMUONSegmentationDetectionElement::Init(const char * DetectionElementType)
 {
   TString elementType(DetectionElementType);
   fSegmentationMappingFileBending = fgkDefaultTop+fgkStationDir+"345"
     +fgkBendingDir+"/"+elementType+fgkBendingExt+fgkFileExt;
-  printf("file is %s\n", fSegmentationMappingFileBending.Data());
   fSegmentationMappingFileNonBending = fgkDefaultTop+fgkStationDir+"345"
     +fgkNonBendingDir+"/"+elementType+fgkNonBendingExt+fgkFileExt;
 
@@ -264,9 +285,11 @@ void    AliMUONSegmentationDetectionElement::Init(const char * DetectionElementT
   Int_t icathode;
   //Bendingplane
   icathode=0;
+  Info("ReadingSegmentationMappingFile","%s", fSegmentationMappingFileBending.Data());
   ReadingSegmentationMappingFile(fSegmentationMappingFileBending ,icathode);
   //NonBendingplane
   icathode=1;
+  Info("Init","Reading mapping file is %s\n", fSegmentationMappingFileNonBending.Data());
   ReadingSegmentationMappingFile(fSegmentationMappingFileNonBending,icathode);
   
 }
@@ -275,28 +298,29 @@ void AliMUONSegmentationDetectionElement::ReadingSegmentationMappingFile(TString
 { 
   ifstream in( infile,  ios::in);
   if (!in) {
-    //    Error("ReadingSegmentationMappingFile", "File not found.");
+    Error("ReadingSegmentationMappingFile", "File not found.");
   }
   else {
     Int_t id, ix, iy, idmanu, idchannel;
     Float_t x, y;
-    while ( !in.eof()) {
+    do {
       in >> id >> ix >> iy >> x >> y >> idmanu >> idchannel;
-      printf("id=%d ix=%d iy=%d x=%f y=%f idmanu=%d and idchannel=%d\n",id, ix, iy,  x, y,idmanu, idchannel);
+      //     printf("id=%d ix=%d iy=%d x=%f y=%f idmanu=%d and idchannel=%d\n",id, ix, iy,  x, y,idmanu, idchannel);
       
       fListOfIndexes->AddAt(     new AliMUONSegmentIndex(id,ix,iy,cathode), id);
       fListOfManuIndexes->AddAt( new AliMUONSegmentManuIndex(id,idmanu,0,idchannel), id);;
       fListOfPositions->AddAt(   new AliMUONSegmentPosition(id, x, y,cathode), id);;
 
-      ( (AliMUONSegmentIndex* )fListOfIndexes->At(id))->Print();
-      ( (AliMUONSegmentManuIndex*)fListOfManuIndexes->At(id))->Print();
-      ( (AliMUONSegmentPosition*)fListOfPositions->At(id))->Print();
+      //( (AliMUONSegmentIndex* )fListOfIndexes->At(id))->Print();
+      //( (AliMUONSegmentManuIndex*)fListOfManuIndexes->At(id))->Print();
+      //( (AliMUONSegmentPosition*)fListOfPositions->At(id))->Print();
      
       fMapManuIndexIndex->Add( fListOfManuIndexes->At(id), fListOfIndexes->At(id));
       fMapIndexManuIndex->Add( fListOfIndexes->At(id),     fListOfManuIndexes->At(id));
       fMapIndexPosition->Add(  fListOfIndexes->At(id),     fListOfPositions->At(id));
-      fMapPositionIndex->Add(  fListOfPositions->At(id), fListOfIndexes->At(id) );
-    } 
+      fMapPositionIndex->Add(  fListOfPositions->At(id),   fListOfIndexes->At(id) );
+    }  
+    while ( !in.eof()); 
   }
   in.close();
 }