Fixing coding violation
[u/mrichter/AliRoot.git] / TPC / AliTPCCorrection.cxx
index a88eb2a..a1262bf 100644 (file)
 #include <AliCDBId.h>
 #include <AliCDBMetaData.h>
 
+#include  "TRandom.h"
 #include  "AliExternalTrackParam.h"
 #include  "AliTrackPointArray.h"
 #include  "TDatabasePDG.h"
 #include  "AliTrackerBase.h"
 #include  "AliTPCROC.h"
+#include  "THnSparse.h"
 
 
 #include "AliTPCCorrection.h"
@@ -63,7 +65,7 @@
 ClassImp(AliTPCCorrection)
 
 // FIXME: the following values should come from the database
-const Double_t AliTPCCorrection::fgkTPC_Z0   =249.7;     // nominal gating grid position 
+const Double_t AliTPCCorrection::fgkTPCZ0    =249.7;     // nominal gating grid position 
 const Double_t AliTPCCorrection::fgkIFCRadius= 83.06;    // Mean Radius of the Inner Field Cage ( 82.43 min,  83.70 max) (cm)
 const Double_t AliTPCCorrection::fgkOFCRadius=254.5;     // Mean Radius of the Outer Field Cage (252.55 min, 256.45 max) (cm)
 const Double_t AliTPCCorrection::fgkZOffSet  = 0.2;      // Offset from CE: calculate all distortions closer to CE as if at this point
@@ -389,12 +391,11 @@ TH2F* AliTPCCorrection::CreateTH2F(const char *name,const char *title,
 // Simple Interpolation functions: e.g. with bi(tri)cubic interpolations (not yet in TH2 and TH3)
 
 void AliTPCCorrection::Interpolate2DEdistortion( const Int_t order, const Double_t r, const Double_t z, 
-                                                 const Double_t er[kNZ][kNR], Double_t &er_value )
-{
+                                                 const Double_t er[kNZ][kNR], Double_t &erValue ) {
   //
   // Interpolate table - 2D interpolation
   //
-  Double_t save_er[10] ;
+  Double_t saveEr[10] ;
 
   Search( kNZ,   fgkZList,  z,   fJLow   ) ;
   Search( kNR,   fgkRList,  r,   fKLow   ) ;
@@ -404,16 +405,15 @@ void AliTPCCorrection::Interpolate2DEdistortion( const Int_t order, const Double
   if ( fKLow + order  >=    kNR - 1 ) fKLow =   kNR - 1 - order ;
 
   for ( Int_t j = fJLow ; j < fJLow + order + 1 ; j++ ) {
-      save_er[j-fJLow]     = Interpolate( &fgkRList[fKLow], &er[j][fKLow], order, r )   ;
+      saveEr[j-fJLow]     = Interpolate( &fgkRList[fKLow], &er[j][fKLow], order, r )   ;
   }
-  er_value = Interpolate( &fgkZList[fJLow], save_er, order, z )   ;
+  erValue = Interpolate( &fgkZList[fJLow], saveEr, order, z )   ;
 
 }
 
 
 Double_t AliTPCCorrection::Interpolate( const Double_t xArray[], const Double_t yArray[], 
-                                      const Int_t order, const Double_t x )
-{
+                                      const Int_t order, const Double_t x ) {
   //
   // Interpolate function Y(x) using linear (order=1) or quadratic (order=2) interpolation.
   //
@@ -432,8 +432,7 @@ Double_t AliTPCCorrection::Interpolate( const Double_t xArray[], const Double_t
 }
 
 
-void AliTPCCorrection::Search( const Int_t n, const Double_t xArray[], const Double_t x, Int_t &low )
-{
+void AliTPCCorrection::Search( const Int_t n, const Double_t xArray[], const Double_t x, Int_t &low ) {
   //
   // Search an ordered table by starting at the most recently used point
   //
@@ -481,7 +480,7 @@ void AliTPCCorrection::Search( const Int_t n, const Double_t xArray[], const Dou
 }
 
 
-AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackParam & trackIn, Double_t refX, Int_t dir,TTreeSRedirector *pcstream){
+AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackParam & trackIn, Double_t refX, Int_t dir, TTreeSRedirector * const pcstream){
   //
   // Fit the track parameters - without and with distortion
   // 1. Space points in the TPC are simulated along the trajectory  
@@ -515,14 +514,16 @@ AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackPara
   AliTrackPointArray pointArray0(npoints0);
   AliTrackPointArray pointArray1(npoints0);
   Double_t xyz[3];
-  AliTrackerBase::PropagateTrackTo(&track,kRTPC0,kMass,3,kTRUE,kMaxSnp);
+  AliTrackerBase::PropagateTrackToBxByBz(&track,kRTPC0,kMass,3,kTRUE,kMaxSnp);
   //
   // simulate the track
   Int_t npoints=0;
   Float_t covPoint[6]={0,0,0, kSigmaY*kSigmaY,0,kSigmaZ*kSigmaZ};  //covariance at the local frame
   for (Double_t radius=kRTPC0; radius<kRTPC1; radius++){
-    AliTrackerBase::PropagateTrackTo(&track,radius,kMass,3,kTRUE,kMaxSnp);
+    AliTrackerBase::PropagateTrackToBxByBz(&track,radius,kMass,3,kTRUE,kMaxSnp);
     track.GetXYZ(xyz);
+    xyz[0]+=gRandom->Gaus(0,0.001);
+    xyz[1]+=gRandom->Gaus(0,0.001);
     AliTrackPoint pIn0;                               // space point          
     AliTrackPoint pIn1;
     Int_t sector= (xyz[2]>0)? 0:18;
@@ -554,12 +555,12 @@ AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackPara
   AliTrackPoint   point1,point2,point3;
   if (dir==1) {  //make seed inner
     pointArray0.GetPoint(point1,1);
-    pointArray0.GetPoint(point2,10);
-    pointArray0.GetPoint(point3,20);
+    pointArray0.GetPoint(point2,30);
+    pointArray0.GetPoint(point3,60);
   }
   if (dir==-1){ //make seed outer
-    pointArray0.GetPoint(point1,npoints-20);
-    pointArray0.GetPoint(point2,npoints-10);
+    pointArray0.GetPoint(point1,npoints-60);
+    pointArray0.GetPoint(point2,npoints-30);
     pointArray0.GetPoint(point3,npoints-1);
   }  
   track0 = AliTrackerBase::MakeSeed(point1, point2, point3);
@@ -567,7 +568,7 @@ AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackPara
 
 
   for (Int_t jpoint=0; jpoint<npoints; jpoint++){
-    Int_t ipoint= (dir>0) ? ipoint: npoints-1-jpoint;
+    Int_t ipoint= (dir>0) ? jpoint: npoints-1-jpoint;
     //
     AliTrackPoint pIn0;
     AliTrackPoint pIn1;
@@ -576,9 +577,9 @@ AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackPara
     AliTrackPoint prot0 = pIn0.Rotate(track0->GetAlpha());   // rotate to the local frame - non distoted  point
     AliTrackPoint prot1 = pIn1.Rotate(track1->GetAlpha());   // rotate to the local frame -     distorted point
     //
-    AliTrackerBase::PropagateTrackTo(track0,prot0.GetX(),kMass,1,kFALSE,kMaxSnp);
-    AliTrackerBase::PropagateTrackTo(track1,prot1.GetX(),kMass,1,kFALSE,kMaxSnp);
-    track.GetXYZ(xyz);
+    AliTrackerBase::PropagateTrackToBxByBz(track0,prot0.GetX(),kMass,3,kFALSE,kMaxSnp);
+    AliTrackerBase::PropagateTrackToBxByBz(track1,prot0.GetX(),kMass,3,kFALSE,kMaxSnp);
+    track.GetXYZ(xyz);  // distorted track also propagated to the same reference radius
     //
     Double_t pointPos[2]={0,0};
     Double_t pointCov[3]={0,0,0};
@@ -589,15 +590,19 @@ AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackPara
     pointCov[2]=prot0.GetCov()[5];//sigmaz^2
     track0->Update(pointPos,pointCov);
     //
-    pointPos[0]=prot1.GetY();//local y
-    pointPos[1]=prot1.GetZ();//local z
+    Double_t deltaX=prot1.GetX()-prot0.GetX();   // delta X 
+    Double_t deltaYX=deltaX*TMath::Tan(TMath::ASin(track1->GetSnp()));  // deltaY due  delta X
+    Double_t deltaZX=deltaX*track1->GetTgl();                           // deltaZ due  delta X
+
+    pointPos[0]=prot1.GetY()-deltaYX;//local y
+    pointPos[1]=prot1.GetZ()-deltaZX;//local z
     pointCov[0]=prot1.GetCov()[3];//simay^2
     pointCov[1]=prot1.GetCov()[4];//sigmayz
     pointCov[2]=prot1.GetCov()[5];//sigmaz^2
     track1->Update(pointPos,pointCov);
   }
 
-  AliTrackerBase::PropagateTrackTo(track0,refX,kMass,2.,kTRUE,kMaxSnp);
+  AliTrackerBase::PropagateTrackToBxByBz(track0,refX,kMass,2.,kTRUE,kMaxSnp);
   track1->Rotate(track0->GetAlpha());
   track1->PropagateTo(track0->GetX(),AliTrackerBase::GetBz());
 
@@ -680,7 +685,7 @@ TTree* AliTPCCorrection::CreateDistortionTree(Double_t step){
 
 
 
-void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, TObjArray * corrArray, Int_t step, Bool_t debug ){
+void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, const TObjArray * corrArray, Int_t step, Bool_t debug ){
   //
   // Make a fit tree:
   // For each partial correction (specified in array) and given track topology (phi, theta, snp, refX)
@@ -697,6 +702,9 @@ void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t
   // corrArray - array with partial corrections
   // step      - skipe entries  - if 1 all entries processed - it is slow
   // debug     0 if debug on also space points dumped - it is slow
+  const Double_t kMaxSnp = 0.85;  
+  const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
+  //  const Double_t kB2C=-0.299792458e-3;
   const Int_t kMinEntries=50;
   Double_t phi,theta, snp, mean,rms, entries;
   tinput->SetBranchAddress("theta",&theta);
@@ -715,8 +723,8 @@ void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t
   Double_t refX=0;
   Int_t dir=0;
   if (dtype==0) {refX=85; dir=-1;}
-  if (dtype==1) {refX=245; dir=1;}
-  if (dtype==2) {refX=0; dir=-1;}
+  if (dtype==1) {refX=275; dir=1;}
+  if (dtype==2) {refX=85; dir=-1;}
   //
   for (Int_t ientry=0; ientry<nentries; ientry+=step){
     tinput->GetEntry(ientry);
@@ -724,20 +732,13 @@ void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t
     tPar[1]=theta*refX;
     tPar[2]=snp;
     tPar[3]=theta;
-    tPar[4]=0.00001;  // should be calculated - non equal to 0
-    cout<<endl<<endl;
-    cout<<"Entry\n\n"<<ientry<<endl;
-    cout<<"dtype="<<dtype<<   // detector match type
-      "ptype="<<ptype<<   // parameter type
-      "theta="<<theta<<   // theta
-      "phi="<<phi<<       // phi 
-      "snp="<<phi<<       // snp
-      "mean="<<mean<<     // mean dist value
-      "rms="<<rms<<       // rms
-      "entries="<<entries<<endl; // number of entries in bin      
-    
+    tPar[4]=(gRandom->Rndm()-0.5)*0.02;  // should be calculated - non equal to 0
+    Double_t bz=AliTrackerBase::GetBz();
+    if (refX>10. && TMath::Abs(bz)>0.1 )  tPar[4]=snp/(refX*bz*kB2C*2);
+    tPar[4]+=(gRandom->Rndm()-0.5)*0.02;
     if (TMath::Abs(snp)>0.251) continue;
     (*pcstream)<<"fit"<<
+      "bz="<<bz<<         // magnetic filed used
       "dtype="<<dtype<<   // detector match type
       "ptype="<<ptype<<   // parameter type
       "theta="<<theta<<   // theta
@@ -755,6 +756,13 @@ void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t
        AliExternalTrackParam *trackOut = 0;
        if (debug) trackOut=corr->FitDistortedTrack(trackIn, refX, dir,pcstream);
        if (!debug) trackOut=corr->FitDistortedTrack(trackIn, refX, dir,0);
+       if (dtype==0) {refX=85; dir=-1;}
+       if (dtype==1) {refX=275; dir=1;}
+       if (dtype==2) {refX=0; dir=-1;}
+       //
+       AliTrackerBase::PropagateTrackToBxByBz(&trackIn,refX,kMass,3,kTRUE,kMaxSnp);
+       AliTrackerBase::PropagateTrackToBxByBz(trackOut,refX,kMass,3,kTRUE,kMaxSnp);
+       //
        corrections[icorr]= trackOut->GetParameter()[ptype]-trackIn.GetParameter()[ptype];
        delete trackOut;      
       }      
@@ -769,6 +777,78 @@ void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t
 
 
 
+void   AliTPCCorrection::MakeDistortionMap(THnSparse * his0, TTreeSRedirector * const pcstream, const char* hname, Int_t run){
+  //
+  // make a distortion map out ou fthe residual histogram
+  // Results are written to the debug streamer - pcstream
+  // Parameters:
+  //   his0       - input (4D) residual histogram
+  //   pcstream   - file to write the tree
+  //   run        - run number
+  // marian.ivanov@cern.ch
+  const Int_t kMinEntries=50;
+  Int_t nbins1=his0->GetAxis(1)->GetNbins();
+  Int_t first1=his0->GetAxis(1)->GetFirst();
+  Int_t last1 =his0->GetAxis(1)->GetLast();
+  //
+  Double_t bz=AliTrackerBase::GetBz();
+  Int_t idim[4]={0,1,2,3};
+  for (Int_t ibin1=first1; ibin1<last1; ibin1++){   //axis 1 - theta
+    //
+    his0->GetAxis(1)->SetRange(TMath::Max(ibin1,1),TMath::Min(ibin1,nbins1));
+    Double_t       x1= his0->GetAxis(1)->GetBinCenter(ibin1);
+    THnSparse * his1 = his0->Projection(4,idim);  // projected histogram according range1
+    Int_t nbins3     = his1->GetAxis(3)->GetNbins();
+    Int_t first3     = his1->GetAxis(3)->GetFirst();
+    Int_t last3      = his1->GetAxis(3)->GetLast();
+    //
+
+    for (Int_t ibin3=first3-1; ibin3<last3; ibin3+=1){   // axis 3 - local angle
+      his1->GetAxis(3)->SetRange(TMath::Max(ibin3-1,1),TMath::Min(ibin3+1,nbins3));
+      Double_t      x3= his1->GetAxis(3)->GetBinCenter(ibin3);
+      if (ibin3<first3) {
+       his1->GetAxis(3)->SetRangeUser(-1,1);
+       x3=0;
+      }
+      THnSparse * his3= his1->Projection(4,idim);         //projected histogram according selection 3
+      Int_t nbins2    = his3->GetAxis(2)->GetNbins();
+      Int_t first2    = his3->GetAxis(2)->GetFirst();
+      Int_t last2     = his3->GetAxis(2)->GetLast();
+      //
+      for (Int_t ibin2=first2; ibin2<last2; ibin2+=1){
+       his3->GetAxis(2)->SetRange(TMath::Max(ibin2-1,1),TMath::Min(ibin2+1,nbins2));
+       Double_t x2= his3->GetAxis(2)->GetBinCenter(ibin2);
+       TH1 * hisDelta = his3->Projection(0);
+       //
+       Double_t entries = hisDelta->GetEntries();
+       Double_t mean=0, rms=0;
+       if (entries>kMinEntries){
+         mean    = hisDelta->GetMean(); 
+         rms = hisDelta->GetRMS(); 
+       }
+       (*pcstream)<<hname<<
+         "run="<<run<<
+         "bz="<<bz<<
+         "theta="<<x1<<
+         "phi="<<x2<<
+         "snp="<<x3<<
+         "entries="<<entries<<
+         "mean="<<mean<<
+         "rms="<<rms<<
+         "\n";
+       delete hisDelta;
+       printf("%f\t%f\t%f\t%f\t%f\n",x1,x3,x2, entries,mean);
+      }
+      delete his3;
+    }
+    delete his1;
+  }
+}
+
+
+
+
+
 void AliTPCCorrection::StoreInOCDB(Int_t startRun, Int_t endRun, const char *comment){
   //
   // Store object in the OCDB