ROOT logo
#include "KImplant2D.h"
#include "TMath.h"

ClassImp(KImplant2D)
//////////////////////////////////////////////////////////////////////////
//                                                                      //
// KImplant2D                                                             //
// A mesh generator for the non-equividistant bins                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////


Double_t KImplant2D::ImplEdge(Double_t *x,Double_t *par)
{
  // x  = coordinate x
  // par[0]= curvature in X;
  // par[1]= curvature in Y;

  if(x[0]>par[0]) return -1;  
  return par[1]*TMath::Power(1-TMath::Power(TMath::Abs(x[0]/par[0]),par[2]), 1/par[2] );  
}

Double_t KImplant2D::Derivative(Double_t *x,Double_t *par)
{
  // x  = coordinate x
  // par[0]= width in X;
  // par[1]= width in Y;
  // par[2]= curvature
  if(x[0]>par[0]) return 1;  
  return (-par[1]/TMath::Power(par[0],par[2])*TMath::Power(1-TMath::Power(x[0]/par[0],par[2]), 1/par[2]-1 )*TMath::Power(x[0],par[2]-1));  
}

Double_t KImplant2D::Distance(Double_t *x,Double_t *par, Double_t *X)
{
  // par[0]= width in X;
  // par[1]= width in Y;
  // par[2]= curvature in XY;


 Double_t yl,yr,ym;
 Double_t xl=0,xr=par[0],xm;
 Double_t y0,dist;
 Int_t d=0;


 xm=(xr+xl)/2.;
 yl=TMath::Tan(TMath::ATan(Derivative(&xl,par))+TMath::Pi()/2)*(x[0]-xl)+ImplEdge(&xl,par)-x[1];
 yr=TMath::Tan(TMath::ATan(Derivative(&xr,par))+TMath::Pi()/2)*(x[0]-xr)+ImplEdge(&xr,par)-x[1];
 ym=TMath::Tan(TMath::ATan(Derivative(&xm,par))+TMath::Pi()/2)*(x[0]-xm)+ImplEdge(&xm,par)-x[1];

 while ((TMath::Abs(ym)>1e-5 && TMath::Abs(xl-xr)>1e-15) && d<50 )
   {
     if(yl*ym<0) xr=xm; else xl=xm;
     xm=(xl+xr)/2;
     
     yl=TMath::Tan(TMath::ATan(Derivative(&xl,par))+TMath::Pi()/2)*(x[0]-xl)+ImplEdge(&xl,par)-x[1];
     yr=TMath::Tan(TMath::ATan(Derivative(&xr,par))+TMath::Pi()/2)*(x[0]-xr)+ImplEdge(&xr,par)-x[1];
     ym=TMath::Tan(TMath::ATan(Derivative(&xm,par))+TMath::Pi()/2)*(x[0]-xm)+ImplEdge(&xm,par)-x[1];

     //     printf("%d::  %f %f (%f %f, %f %f)\n",d,xm,ym, xl,yl,xr,yr);
     d++;
   }

 y0=ImplEdge(&xm,par);
 dist=TMath::Sqrt(TMath::Power(xm-x[0],2)+TMath::Power(y0-x[1],2)); 

 if(X!=NULL)
   {
 X[0]=xm;
 X[1]=y0;
   }

 if(xm>x[0]) dist=-dist;
 return dist;
}

Double_t KImplant2D::Distance1(Double_t *x,Double_t *par, Double_t *X)
{
  // par[0]= curvature in Y;
  // par[1]= curvature in X;
  // par[2]= Point to which the distance is measured X1;
  // par[3]= Point to which the distance is measured Y1;

 Double_t yl[2],yr[2];
 Double_t vl,vr;
 Double_t dist;
 Int_t d=0;

 yl[0]=0; yr[0]=par[0];
 yl[1]=ImplEdge(yl[0],par); yr[1]=ImplEdge(yr[0],par); 
 vl=PDistance(x,yl); vr=PDistance(x,yr); 
 printf("%d :: %f %f %f %f , %f %f\n",d,yl[0],yr[0],yl[1],yr[1],vl,vr);
 while ((TMath::Abs(vl-vr)>1e-10 && TMath::Abs(yl[0]-yr[0])>1e-15) && d<50 )
   {
     if(vl<vr) 
       { yr[0]=(yl[0]+yr[0])*0.5;  yr[1]=ImplEdge(yr[0],par); vr=PDistance(x,yr); }
     else 
       { yl[0]=(yl[0]+yr[0])*0.5;  yl[1]=ImplEdge(yl[0],par); vl=PDistance(x,yl); }
 printf("%d :: %f %f %f %f , %f %f\n",d,yl[0],yr[0],yl[1],yr[1],vl,vr);

     d++;
   }

 dist=PDistance(x,yl);

 if(X!=NULL)
   {
 X[0]=yl[0];
 X[1]=yl[1];
   }

 if(yl[0]>x[0]) dist=-dist;
 return dist;
}



Double_t KImplant2D::Conc(Double_t *x, Double_t Thresh)
{
  // x[0]  = coordinate x
  // x[1]  = coordinate y 

  Double_t dist=Distance(x,Dim); 
  if(dist<Thresh) return 0; else
  return fConc->Eval(dist);
  
}


KImplant2D::KImplant2D(Double_t *x, Double_t Sigma, Double_t Nimpl)
{
  // x[0]  = coordinate x
  // x[1]  = coordinate y 

  for(Int_t i=0;i<3;i++) Dim[i]=x[i];
  fConc=new TF1("fConc","TMath::Erfc((x-[0])/[1])*[2]+[3]",-20,20);
  fConc->SetParameter(3,0);
  fConc->SetParameter(2,Nimpl);
  fConc->SetParameter(1,Sigma);
  fConc->SetParameter(0,0);

}
 KImplant2D.cxx:1
 KImplant2D.cxx:2
 KImplant2D.cxx:3
 KImplant2D.cxx:4
 KImplant2D.cxx:5
 KImplant2D.cxx:6
 KImplant2D.cxx:7
 KImplant2D.cxx:8
 KImplant2D.cxx:9
 KImplant2D.cxx:10
 KImplant2D.cxx:11
 KImplant2D.cxx:12
 KImplant2D.cxx:13
 KImplant2D.cxx:14
 KImplant2D.cxx:15
 KImplant2D.cxx:16
 KImplant2D.cxx:17
 KImplant2D.cxx:18
 KImplant2D.cxx:19
 KImplant2D.cxx:20
 KImplant2D.cxx:21
 KImplant2D.cxx:22
 KImplant2D.cxx:23
 KImplant2D.cxx:24
 KImplant2D.cxx:25
 KImplant2D.cxx:26
 KImplant2D.cxx:27
 KImplant2D.cxx:28
 KImplant2D.cxx:29
 KImplant2D.cxx:30
 KImplant2D.cxx:31
 KImplant2D.cxx:32
 KImplant2D.cxx:33
 KImplant2D.cxx:34
 KImplant2D.cxx:35
 KImplant2D.cxx:36
 KImplant2D.cxx:37
 KImplant2D.cxx:38
 KImplant2D.cxx:39
 KImplant2D.cxx:40
 KImplant2D.cxx:41
 KImplant2D.cxx:42
 KImplant2D.cxx:43
 KImplant2D.cxx:44
 KImplant2D.cxx:45
 KImplant2D.cxx:46
 KImplant2D.cxx:47
 KImplant2D.cxx:48
 KImplant2D.cxx:49
 KImplant2D.cxx:50
 KImplant2D.cxx:51
 KImplant2D.cxx:52
 KImplant2D.cxx:53
 KImplant2D.cxx:54
 KImplant2D.cxx:55
 KImplant2D.cxx:56
 KImplant2D.cxx:57
 KImplant2D.cxx:58
 KImplant2D.cxx:59
 KImplant2D.cxx:60
 KImplant2D.cxx:61
 KImplant2D.cxx:62
 KImplant2D.cxx:63
 KImplant2D.cxx:64
 KImplant2D.cxx:65
 KImplant2D.cxx:66
 KImplant2D.cxx:67
 KImplant2D.cxx:68
 KImplant2D.cxx:69
 KImplant2D.cxx:70
 KImplant2D.cxx:71
 KImplant2D.cxx:72
 KImplant2D.cxx:73
 KImplant2D.cxx:74
 KImplant2D.cxx:75
 KImplant2D.cxx:76
 KImplant2D.cxx:77
 KImplant2D.cxx:78
 KImplant2D.cxx:79
 KImplant2D.cxx:80
 KImplant2D.cxx:81
 KImplant2D.cxx:82
 KImplant2D.cxx:83
 KImplant2D.cxx:84
 KImplant2D.cxx:85
 KImplant2D.cxx:86
 KImplant2D.cxx:87
 KImplant2D.cxx:88
 KImplant2D.cxx:89
 KImplant2D.cxx:90
 KImplant2D.cxx:91
 KImplant2D.cxx:92
 KImplant2D.cxx:93
 KImplant2D.cxx:94
 KImplant2D.cxx:95
 KImplant2D.cxx:96
 KImplant2D.cxx:97
 KImplant2D.cxx:98
 KImplant2D.cxx:99
 KImplant2D.cxx:100
 KImplant2D.cxx:101
 KImplant2D.cxx:102
 KImplant2D.cxx:103
 KImplant2D.cxx:104
 KImplant2D.cxx:105
 KImplant2D.cxx:106
 KImplant2D.cxx:107
 KImplant2D.cxx:108
 KImplant2D.cxx:109
 KImplant2D.cxx:110
 KImplant2D.cxx:111
 KImplant2D.cxx:112
 KImplant2D.cxx:113
 KImplant2D.cxx:114
 KImplant2D.cxx:115
 KImplant2D.cxx:116
 KImplant2D.cxx:117
 KImplant2D.cxx:118
 KImplant2D.cxx:119
 KImplant2D.cxx:120
 KImplant2D.cxx:121
 KImplant2D.cxx:122
 KImplant2D.cxx:123
 KImplant2D.cxx:124
 KImplant2D.cxx:125
 KImplant2D.cxx:126
 KImplant2D.cxx:127
 KImplant2D.cxx:128
 KImplant2D.cxx:129
 KImplant2D.cxx:130
 KImplant2D.cxx:131
 KImplant2D.cxx:132
 KImplant2D.cxx:133
 KImplant2D.cxx:134
 KImplant2D.cxx:135
 KImplant2D.cxx:136
 KImplant2D.cxx:137
 KImplant2D.cxx:138
 KImplant2D.cxx:139
 KImplant2D.cxx:140
 KImplant2D.cxx:141
 KImplant2D.cxx:142