+void AliTPCCorrection::MakeSectorDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, const TObjArray * corrArray, Int_t step, Int_t offset, 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 10 - IROC-OROC
+ // 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.8;
+ const Int_t kMinEntries=200;
+ // AliTPCROC *tpcRoc =AliTPCROC::Instance();
+ //
+ const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
+ // const Double_t kB2C=-0.299792458e-3;
+ Double_t phi,theta, snp, mean,rms, entries,sector,dsec,globalZ;
+ Int_t isec1, isec0;
+ Double_t refXD;
+ Float_t refX;
+ Int_t run;
+ tinput->SetBranchAddress("run",&run);
+ tinput->SetBranchAddress("theta",&theta);
+ tinput->SetBranchAddress("phi", &phi);
+ tinput->SetBranchAddress("snp",&snp);
+ tinput->SetBranchAddress("mean",&mean);
+ tinput->SetBranchAddress("rms",&rms);
+ tinput->SetBranchAddress("entries",&entries);
+ tinput->SetBranchAddress("sector",§or);
+ tinput->SetBranchAddress("dsec",&dsec);
+ tinput->SetBranchAddress("refX",&refXD);
+ tinput->SetBranchAddress("z",&globalZ);
+ tinput->SetBranchAddress("isec0",&isec0);
+ tinput->SetBranchAddress("isec1",&isec1);
+ TTreeSRedirector *pcstream = new TTreeSRedirector(Form("distortionSector%d_%d_%d.root",dtype,ptype,offset));
+ //
+ Int_t nentries=tinput->GetEntries();
+ Int_t ncorr=corrArray->GetEntries();
+ Double_t corrections[100]={0}; //
+ Double_t tPar[5];
+ Double_t cov[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ Int_t dir=0;
+ //
+ for (Int_t ientry=offset; ientry<nentries; ientry+=step){
+ tinput->GetEntry(ientry);
+ refX=refXD;
+ Int_t id=-1;
+ if (TMath::Abs(TMath::Abs(isec0%18)-TMath::Abs(isec1%18))==0) id=1; // IROC-OROC - opposite side
+ if (TMath::Abs(TMath::Abs(isec0%36)-TMath::Abs(isec1%36))==0) id=2; // IROC-OROC - same side
+ if (dtype==10 && id==-1) continue;
+ //
+ dir=-1;
+ tPar[0]=0;
+ tPar[1]=globalZ;
+ tPar[2]=snp;
+ tPar[3]=theta;
+ tPar[4]=(gRandom->Rndm()-0.1)*0.2; //
+ Double_t pt=1./tPar[4];
+ //
+ printf("%f\t%f\t%f\t%f\t%f\t%f\n",entries, sector,theta,snp, mean,rms);
+ Double_t bz=AliTrackerBase::GetBz();
+ AliExternalTrackParam track(refX,phi,tPar,cov);
+ Double_t xyz[3],xyzIn[3],xyzOut[3];
+ track.GetXYZ(xyz);
+ track.GetXYZAt(85,bz,xyzIn);
+ track.GetXYZAt(245,bz,xyzOut);
+ Double_t phiIn = TMath::ATan2(xyzIn[1],xyzIn[0]);
+ Double_t phiOut = TMath::ATan2(xyzOut[1],xyzOut[0]);
+ Double_t phiRef = TMath::ATan2(xyz[1],xyz[0]);
+ Int_t sectorRef = TMath::Nint(9.*phiRef/TMath::Pi()-0.5);
+ Int_t sectorIn = TMath::Nint(9.*phiIn/TMath::Pi()-0.5);
+ Int_t sectorOut = TMath::Nint(9.*phiOut/TMath::Pi()-0.5);
+ //
+ Bool_t isOK=kTRUE;
+ if (sectorIn!=sectorOut) isOK=kFALSE; // requironment - cluster in the same sector
+ if (sectorIn!=sectorRef) isOK=kFALSE; // requironment - cluster in the same sector
+ if (entries<kMinEntries/(1+TMath::Abs(globalZ/100.))) isOK=kFALSE; // requironment - minimal amount of tracks in bin
+ // Do downscale
+ if (TMath::Abs(theta)>1) isOK=kFALSE;
+ //
+ Double_t dRrec=0; // dummy value - needed for points - e.g for laser
+ //
+ (*pcstream)<<"fit"<<
+ "run="<<run<< //run
+ "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
+ "sector="<<sector<<
+ "dsec="<<dsec<<
+ "refX="<<refXD<< // referece X
+ "gx="<<xyz[0]<< // global position at reference
+ "gy="<<xyz[1]<< // global position at reference
+ "gz="<<xyz[2]<< // global position at reference
+ "dRrec="<<dRrec<< // delta Radius in reconstruction
+ "pt="<<pt<< //pt
+ "id="<<id<< // track id
+ "entries="<<entries;// number of entries in bin
+ //
+ AliExternalTrackParam *trackOut0 = 0;
+ AliExternalTrackParam *trackOut1 = 0;
+ AliExternalTrackParam *ptrackIn0 = 0;
+ AliExternalTrackParam *ptrackIn1 = 0;
+
+ for (Int_t icorr=0; icorr<ncorr; icorr++) {
+ //
+ // special case of the TPC tracks crossing the CE
+ //
+ AliTPCCorrection *corr = (AliTPCCorrection*)corrArray->At(icorr);
+ corrections[icorr]=0;
+ if (entries>kMinEntries &&isOK){
+ AliExternalTrackParam trackIn0(refX,phi,tPar,cov);
+ AliExternalTrackParam trackIn1(refX,phi,tPar,cov);
+ ptrackIn1=&trackIn0;
+ ptrackIn0=&trackIn1;
+ //
+ if (debug) trackOut0=corr->FitDistortedTrack(trackIn0, refX, dir,pcstream);
+ if (!debug) trackOut0=corr->FitDistortedTrack(trackIn0, refX, dir,0);
+ if (debug) trackOut1=corr->FitDistortedTrack(trackIn1, refX, -dir,pcstream);
+ if (!debug) trackOut1=corr->FitDistortedTrack(trackIn1, refX, -dir,0);
+ //
+ if (trackOut0 && trackOut1){
+ //
+ if (!AliTrackerBase::PropagateTrackTo(&trackIn0,refX,kMass,1,kTRUE,kMaxSnp)) isOK=kFALSE;
+ if (!AliTrackerBase::PropagateTrackTo(&trackIn0,refX,kMass,1,kFALSE,kMaxSnp)) isOK=kFALSE;
+ // rotate all tracks to the same frame
+ if (!trackOut0->Rotate(trackIn0.GetAlpha())) isOK=kFALSE;
+ if (!trackIn1.Rotate(trackIn0.GetAlpha())) isOK=kFALSE;
+ if (!trackOut1->Rotate(trackIn0.GetAlpha())) isOK=kFALSE;
+ //
+ if (!AliTrackerBase::PropagateTrackTo(trackOut0,refX,kMass,1,kFALSE,kMaxSnp)) isOK=kFALSE;
+ if (!AliTrackerBase::PropagateTrackTo(&trackIn1,refX,kMass,1,kFALSE,kMaxSnp)) isOK=kFALSE;
+ if (!AliTrackerBase::PropagateTrackTo(trackOut1,refX,kMass,1,kFALSE,kMaxSnp)) isOK=kFALSE;
+ //
+ corrections[icorr] = (trackOut0->GetParameter()[ptype]-trackIn0.GetParameter()[ptype]);
+ corrections[icorr]-= (trackOut1->GetParameter()[ptype]-trackIn1.GetParameter()[ptype]);
+ (*pcstream)<<"fitDebug"<< // just to debug the correction
+ "mean="<<mean<<
+ "pIn0.="<<ptrackIn0<<
+ "pIn1.="<<ptrackIn1<<
+ "pOut0.="<<trackOut0<<
+ "pOut1.="<<trackOut1<<
+ "refX="<<refXD<<
+ "\n";
+ delete trackOut0;
+ delete trackOut1;
+ }else{
+ corrections[icorr]=0;
+ isOK=kFALSE;
+ }
+ }
+ (*pcstream)<<"fit"<<
+ Form("%s=",corr->GetName())<<corrections[icorr]; // dump correction value
+ }
+ //
+ (*pcstream)<<"fit"<<"isOK="<<isOK<<"\n";
+ }
+ delete pcstream;
+}
+
+
+
+void AliTPCCorrection::MakeLaserDistortionTreeOld(TTree* tree, TObjArray *corrArray, Int_t itype){