]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCCorrection.cxx
M AliTPCTransform.cxx - use also radial map
[u/mrichter/AliRoot.git] / TPC / AliTPCCorrection.cxx
index a68bdd39be59a3b394a09586af36cbd887be9d78..496bd995decd0401d4143269fe7cf447cc10819f 100644 (file)
 // date: 27/04/2010                                                           //
 // Authors: Magnus Mager, Stefan Rossegger, Jim Thomas                        //
 ////////////////////////////////////////////////////////////////////////////////
+#include "Riostream.h"
 
 #include <TH2F.h>
 #include <TMath.h>
 #include <TROOT.h>
+#include <TTreeStream.h>
+#include <TTree.h>
+#include <TFile.h>
+#include <TTimeStamp.h>
+#include <AliCDBStorage.h>
+#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"
 
+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::fgkIFCRadius= 83.06;    // Mean Radius of the Inner Field Cage ( 82.43 min,  83.70 max) (cm)
@@ -99,7 +118,7 @@ const Double_t AliTPCCorrection::fgkZList[AliTPCCorrection::kNZ]     =   {
 
 
 AliTPCCorrection::AliTPCCorrection() 
-  : TNamed("correction_unity","unity"),fJLow(0),fKLow(0)
+  : TNamed("correction_unity","unity"),fJLow(0),fKLow(0), fT1(1), fT2(1)
 {
   //
   // default constructor
@@ -107,7 +126,7 @@ AliTPCCorrection::AliTPCCorrection()
 }
 
 AliTPCCorrection::AliTPCCorrection(const char *name,const char *title)
-  : TNamed(name,title),fJLow(0),fKLow(0)
+: TNamed(name,title),fJLow(0),fKLow(0), fT1(1), fT2(1)
 {
   //
   // default constructor, that set the name and title
@@ -188,6 +207,12 @@ void AliTPCCorrection::Init() {
   //
 }
 
+void AliTPCCorrection::Update(const TTimeStamp &/*timeStamp*/) {
+  //
+  // Update function 
+  //
+}
+
 void AliTPCCorrection::Print(Option_t* /*option*/) const {
   //
   // Print function to check which correction classes are used 
@@ -197,13 +222,15 @@ void AliTPCCorrection::Print(Option_t* /*option*/) const {
   printf("TPC spacepoint correction: \"%s\"\n",GetTitle());
 }
 
-void AliTPCCorrection:: SetOmegaTauT1T2(Float_t /*omegaTau*/,Float_t /*t1*/,Float_t /*t2*/) {
+void AliTPCCorrection:: SetOmegaTauT1T2(Float_t /*omegaTau*/,Float_t t1,Float_t t2) {
   //
   // Virtual funtion to pass the wt values (might become event dependent) to the inherited classes
   // t1 and t2 represent the "effective omegaTau" corrections and were measured in a dedicated
   // calibration run
   //
-  // SetOmegaTauT1T2(omegaTau, t1, t2);
+  fT1=t1;
+  fT2=t2;
+  //SetOmegaTauT1T2(omegaTau, t1, t2);
 }
 
 TH2F* AliTPCCorrection::CreateHistoDRinXY(Float_t z,Int_t nx,Int_t ny) {
@@ -455,4 +482,397 @@ void AliTPCCorrection::Search( const Int_t n, const Double_t xArray[], const Dou
   
 }
 
-ClassImp(AliTPCCorrection)
+
+AliExternalTrackParam * AliTPCCorrection::FitDistortedTrack(AliExternalTrackParam & trackIn, Double_t refX, Int_t dir,TTreeSRedirector *pcstream){
+  //
+  // Fit the track parameters - without and with distortion
+  // 1. Space points in the TPC are simulated along the trajectory  
+  // 2. Space points distorted
+  // 3. Fits  the non distorted and distroted track to the reference plane at refX
+  // 4. For visualization and debugging  purposes the space points and tracks can be stored  in the tree - using the TTreeSRedirector functionality 
+  //
+  // trackIn   - input track parameters
+  // refX     - reference X to fit the track
+  // dir      - direction - out=1 or in=-1
+  // pcstream -  debug streamer to check the results
+  //
+  // see AliExternalTrackParam.h documentation:
+  // track1.fP[0] - local y (rphi)
+  // track1.fP[1] - z 
+  // track1.fP[2] - sinus of local inclination angle
+  // track1.fP[3] - tangent of deep angle
+  // track1.fP[4] - 1/pt
+  AliTPCROC * roc = AliTPCROC::Instance();
+  const Int_t    npoints0=roc->GetNRows(0)+roc->GetNRows(36);
+  const Double_t kRTPC0  =roc->GetPadRowRadii(0,0);
+  const Double_t kRTPC1  =roc->GetPadRowRadii(36,roc->GetNRows(36)-1);
+    
+  const Double_t kMaxSnp = 0.85;  
+  const Double_t kSigmaY=0.1;
+  const Double_t kSigmaZ=0.1;
+  const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
+
+  AliExternalTrackParam  track(trackIn); // 
+  // generate points
+  AliTrackPointArray pointArray0(npoints0);
+  AliTrackPointArray pointArray1(npoints0);
+  Double_t xyz[3];
+  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::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;
+    pointArray0.GetPoint(pIn0,npoints);
+    pointArray1.GetPoint(pIn1,npoints);
+    Double_t alpha = TMath::ATan2(xyz[1],xyz[0]);
+    Float_t distPoint[3]={xyz[0],xyz[1],xyz[2]};
+    DistortPoint(distPoint, sector);
+    pIn0.SetXYZ(xyz[0], xyz[1],xyz[2]);
+    pIn1.SetXYZ(distPoint[0], distPoint[1],distPoint[2]);
+    //
+    track.Rotate(alpha);
+    AliTrackPoint prot0 = pIn0.Rotate(alpha);   // rotate to the local frame - non distoted  point
+    AliTrackPoint prot1 = pIn1.Rotate(alpha);   // rotate to the local frame -     distorted point
+    prot0.SetXYZ(prot0.GetX(),prot0.GetY(), prot0.GetZ(),covPoint);
+    prot1.SetXYZ(prot1.GetX(),prot1.GetY(), prot1.GetZ(),covPoint);
+    pIn0=prot0.Rotate(-alpha);                       // rotate back to global frame
+    pIn1=prot1.Rotate(-alpha);                       // rotate back to global frame
+    pointArray0.AddPoint(npoints, &pIn0);      
+    pointArray1.AddPoint(npoints, &pIn1);
+    npoints++;
+    if (npoints>=npoints0) break;
+  }
+  //
+  // refit track
+  //
+  AliExternalTrackParam *track0=0;
+  AliExternalTrackParam *track1=0;
+  AliTrackPoint   point1,point2,point3;
+  if (dir==1) {  //make seed inner
+    pointArray0.GetPoint(point1,1);
+    pointArray0.GetPoint(point2,30);
+    pointArray0.GetPoint(point3,60);
+  }
+  if (dir==-1){ //make seed outer
+    pointArray0.GetPoint(point1,npoints-60);
+    pointArray0.GetPoint(point2,npoints-30);
+    pointArray0.GetPoint(point3,npoints-1);
+  }  
+  track0 = AliTrackerBase::MakeSeed(point1, point2, point3);
+  track1 = AliTrackerBase::MakeSeed(point1, point2, point3);
+
+
+  for (Int_t jpoint=0; jpoint<npoints; jpoint++){
+    Int_t ipoint= (dir>0) ? jpoint: npoints-1-jpoint;
+    //
+    AliTrackPoint pIn0;
+    AliTrackPoint pIn1;
+    pointArray0.GetPoint(pIn0,ipoint);
+    pointArray1.GetPoint(pIn1,ipoint);
+    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::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};
+    pointPos[0]=prot0.GetY();//local y
+    pointPos[1]=prot0.GetZ();//local z
+    pointCov[0]=prot0.GetCov()[3];//simay^2
+    pointCov[1]=prot0.GetCov()[4];//sigmayz
+    pointCov[2]=prot0.GetCov()[5];//sigmaz^2
+    track0->Update(pointPos,pointCov);
+    //
+    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::PropagateTrackToBxByBz(track0,refX,kMass,2.,kTRUE,kMaxSnp);
+  track1->Rotate(track0->GetAlpha());
+  track1->PropagateTo(track0->GetX(),AliTrackerBase::GetBz());
+
+  if (pcstream) (*pcstream)<<Form("fitDistort%s",GetName())<<
+    "point0.="<<&pointArray0<<   //  points
+    "point1.="<<&pointArray1<<   //  distorted points
+    "trackIn.="<<&track<<       //  original track
+    "track0.="<<track0<<         //  fitted track
+    "track1.="<<track1<<         //  fitted distorted track
+    "\n";
+  new(&trackIn) AliExternalTrackParam(*track0);
+  delete track0;
+  return track1;
+}
+
+
+
+
+
+TTree* AliTPCCorrection::CreateDistortionTree(Double_t step){
+  //
+  // create the distortion tree on a mesh with granularity given by step
+  // return the tree with distortions at given position 
+  // Map is created on the mesh with given step size
+  //
+  TTreeSRedirector *pcstream = new TTreeSRedirector(Form("correction%s.root",GetName()));
+  Float_t xyz[3];
+  for (Double_t x= -250; x<250; x+=step){
+    for (Double_t y= -250; y<250; y+=step){
+      Double_t r    = TMath::Sqrt(x*x+y*y);
+      if (r<80) continue;
+      if (r>250) continue;      
+      for (Double_t z= -250; z<250; z+=step){
+       Int_t roc=(z>0)?0:18;
+       xyz[0]=x;
+       xyz[1]=y;
+       xyz[2]=z;
+       Double_t phi  = TMath::ATan2(y,x);
+       DistortPoint(xyz,roc);
+       Double_t r1    = TMath::Sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]);
+       Double_t phi1  = TMath::ATan2(xyz[1],xyz[0]);
+       if ((phi1-phi)>TMath::Pi()) phi1-=TMath::Pi();
+       if ((phi1-phi)<-TMath::Pi()) phi1+=TMath::Pi();
+       Double_t dx = xyz[0]-x;
+       Double_t dy = xyz[1]-y;
+       Double_t dz = xyz[2]-z;
+       Double_t dr=r1-r;
+       Double_t drphi=(phi1-phi)*r;
+       (*pcstream)<<"distortion"<<
+         "x="<<x<<           // original position        
+         "y="<<y<<
+         "z="<<z<<
+         "r="<<r<<
+         "phi="<<phi<<   
+         "x1="<<xyz[0]<<      // distorted position
+         "y1="<<xyz[1]<<
+         "z1="<<xyz[2]<<
+         "r1="<<r1<<
+         "phi1="<<phi1<<
+         //
+         "dx="<<dx<<          // delta position
+         "dy="<<dy<<
+         "dz="<<dz<<
+         "dr="<<dr<<
+         "drphi="<<drphi<<
+         "\n";
+      }
+    }  
+  }
+  delete pcstream;
+  TFile f(Form("correction%s.root",GetName()));
+  TTree * tree = (TTree*)f.Get("distortion");
+  TTree * tree2= tree->CopyTree("1");
+  tree2->SetName(Form("dist%s",GetName()));
+  tree2->SetDirectory(0);
+  delete tree;
+  return tree2;
+}
+
+
+
+
+void AliTPCCorrection::MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, 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)
+  // calculates partial distortions
+  // Partial distortion is stored in the resulting tree
+  // Output is storred in the file distortion_<dettype>_<partype>.root
+  // Partial  distortion is stored with the name given by correction name
+  //
+  //
+  // Parameters of function:
+  // input     - input tree
+  // dtype     - distortion type 0 - ITSTPC,  1 -TPCTRD, 2 - TPCvertex 
+  // ppype     - parameter type
+  // 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);
+  tinput->SetBranchAddress("phi", &phi);
+  tinput->SetBranchAddress("snp",&snp);
+  tinput->SetBranchAddress("mean",&mean);
+  tinput->SetBranchAddress("rms",&rms);
+  tinput->SetBranchAddress("entries",&entries);
+  TTreeSRedirector *pcstream = new TTreeSRedirector(Form("distortion%d_%d.root",dtype,ptype));
+  //
+  Int_t nentries=tinput->GetEntries();
+  Int_t ncorr=corrArray->GetEntries();
+  Double_t corrections[100]; //
+  Double_t tPar[5];
+  Double_t cov[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+  Double_t refX=0;
+  Int_t dir=0;
+  if (dtype==0) {refX=85; 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);
+    tPar[0]=0;
+    tPar[1]=theta*refX;
+    tPar[2]=snp;
+    tPar[3]=theta;
+    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
+      "phi="<<phi<<       // phi 
+      "snp="<<snp<<       // snp
+      "mean="<<mean<<     // mean dist value
+      "rms="<<rms<<       // rms
+      "entries="<<entries;// number of entries in bin
+    //
+    for (Int_t icorr=0; icorr<ncorr; icorr++) {
+      AliTPCCorrection *corr = (AliTPCCorrection*)corrArray->At(icorr);
+      corrections[icorr]=0;
+      if (entries>kMinEntries){
+       AliExternalTrackParam trackIn(refX,phi,tPar,cov);
+       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;      
+      }      
+      (*pcstream)<<"fit"<<
+       Form("%s=",corr->GetName())<<corrections[icorr];   // dump correction value
+    }
+    (*pcstream)<<"fit"<<"\n";
+  }
+  delete pcstream;
+}
+
+
+
+
+void   AliTPCCorrection::MakeDistortionMap(THnSparse * his0, TTreeSRedirector *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
+  // By default the object is stored in the current directory 
+  // default comment consit of user name and the date
+  //
+  TString ocdbStorage="";
+  ocdbStorage+="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
+  AliCDBMetaData *metaData= new AliCDBMetaData();
+  metaData->SetObjectClassName("AliTPCCorrection");
+  metaData->SetResponsible("Marian Ivanov");
+  metaData->SetBeamPeriod(1);
+  metaData->SetAliRootVersion("05-25-01"); //root version
+  TString userName=gSystem->GetFromPipe("echo $USER");
+  TString date=gSystem->GetFromPipe("date");
+
+  if (!comment) metaData->SetComment(Form("Space point distortion calibration\n User: %s\n Data%s",userName.Data(),date.Data()));
+  if (comment) metaData->SetComment(comment);
+  AliCDBId* id1=NULL;
+  id1=new AliCDBId("TPC/Calib/Correction", startRun, endRun);
+  AliCDBStorage* gStorage = AliCDBManager::Instance()->GetStorage(ocdbStorage);
+  gStorage->Put(this, (*id1), metaData);
+}
+