+// newpep
+Int_t AliITSAlignMille::ApplyToGeometry() {
+ /// apply starting realignment to ideal geometry
+ if(!AliGeomManager::GetGeometry()) return -1;
+
+ TFile *pref = new TFile(fPreAlignmentFileName.Data());
+ if (!pref->IsOpen()) return -2;
+ TClonesArray *prea=(TClonesArray*)pref->Get("ITSAlignObjs");
+ if (!prea) return -3;
+ Int_t nprea=prea->GetEntriesFast();
+ AliInfo(Form("Array of input misalignments with %d entries",nprea));
+
+ AliGeomManager::ApplyAlignObjsToGeom(*prea); // apply all levels of objs
+
+ // set prealignment factor if defined...
+ for (int ix=0; ix<nprea; ix++) {
+ AliAlignObjParams *preo=(AliAlignObjParams*) prea->UncheckedAt(ix);
+ Int_t index=AliITSAlignMilleModule::GetIndexFromVolumeID(preo->GetVolUID());
+ if (index>=0) {
+ fPreAlignQF[index] = (int) preo->GetUniqueID();
+ //printf("index=%d QF=%d\n",index,preo->GetUniqueID());
+ }
+ //if (!preo->ApplyToGeometry()) return -4;
+ }
+ pref->Close();
+ delete pref;
+
+ fUsePreAlignment = kTRUE;
+ return 0;
+}
+// endnewpep
+
+Int_t AliITSAlignMille::GetPreAlignmentQualityFactor(Int_t index) const {
+ /// works for sensitive volumes
+ if (!fUsePreAlignment || index<0 || index>2197) return -1;
+ return fPreAlignQF[index];
+}
+
+AliTrackPointArray *AliITSAlignMille::PrepareTrack(const AliTrackPointArray *atp) {
+ /// create a new AliTrackPointArray keeping only defined modules
+ /// move points according to a given prealignment, if any
+ /// sort alitrackpoints w.r.t. global Y direction, if selected
+
+ AliTrackPointArray *atps=NULL;
+ Int_t idx[20];
+ Int_t npts=atp->GetNPoints();
+
+ /// checks if AliTrackPoints belong to defined modules
+ Int_t ngoodpts=0;
+ Int_t intidx[20];
+
+ for (int j=0; j<npts; j++) {
+ intidx[j] = IsContained(atp->GetVolumeID()[j]);
+ if (intidx[j]>=0) ngoodpts++;
+ }
+ AliDebug(3,Form("Number of points in defined modules: %d",ngoodpts));
+
+ // reject track if not enough points are left
+ if (ngoodpts<fMinNPtsPerTrack) {
+ AliInfo("Track with not enough points!");
+ return NULL;
+ }
+
+ AliTrackPoint p;
+ // check points in specific places
+ if (fRequirePoints) {
+ Int_t nlayup[6],nlaydown[6],nlay[6];
+ Int_t ndetup[3],ndetdown[3],ndet[3];
+ for (Int_t j=0; j<6; j++) {nlayup[j]=0; nlaydown[j]=0; nlay[j]=0;}
+ for (Int_t j=0; j<3; j++) {ndetup[j]=0; ndetdown[j]=0; ndet[j]=0;}
+
+ for (int i=0; i<npts; i++) {
+ // skip not defined points
+ if (intidx[i]<0) continue;
+ Float_t xx=atp->GetX()[i];
+ Float_t yy=atp->GetY()[i];
+ Float_t r=TMath::Sqrt(xx*xx + yy*yy);
+ int lay=-1;
+ if (r<5) lay=0;
+ else if (r>5 && r<10) lay=1;
+ else if (r>10 && r<18) lay=2;
+ else if (r>18 && r<30) lay=3;
+ else if (r>30 && r<40) lay=4;
+ else if (r>40) lay=5;
+ if (lay<0) continue;
+ int det=lay/2;
+ //printf("Point %d - x=%f y=%f R=%f lay=%d det=%d\n",i,xx,yy,r,lay,det);
+
+ if (yy>=0.0) { // UP point
+ nlayup[lay]++;
+ nlay[lay]++;
+ ndetup[det]++;
+ ndet[det]++;
+ }
+ else {
+ nlaydown[lay]++;
+ nlay[lay]++;
+ ndetdown[det]++;
+ ndet[det]++;
+ }
+ }
+
+ // checks minimum values
+ Bool_t isok=kTRUE;
+ for (Int_t j=0; j<6; j++) {
+ if (nlayup[j]<fNReqLayUp[j]) isok=kFALSE;
+ if (nlaydown[j]<fNReqLayDown[j]) isok=kFALSE;
+ if (nlay[j]<fNReqLay[j]) isok=kFALSE;
+ }
+ for (Int_t j=0; j<3; j++) {
+ if (ndetup[j]<fNReqDetUp[j]) isok=kFALSE;
+ if (ndetdown[j]<fNReqDetDown[j]) isok=kFALSE;
+ if (ndet[j]<fNReqDet[j]) isok=kFALSE;
+ }
+ if (!isok) {
+ AliDebug(2,Form("Track does not meet all location point requirements!"));
+ return NULL;
+ }
+ }
+
+ // build a new track with (sorted) (prealigned) good points
+ atps=new AliTrackPointArray(ngoodpts);
+
+ for (int i=0; i<npts; i++) idx[i]=i;
+ // sort track if required
+ if (fUseSortedTracks) TMath::Sort(npts,atp->GetY(),idx); // sort descending...
+
+ Int_t npto=0;
+ for (int i=0; i<npts; i++) {
+ // skip not defined points
+ if (intidx[idx[i]]<0) continue;
+ atp->GetPoint(p,idx[i]);
+
+ // prealign point if required
+ // get IDEAL matrix
+ TGeoHMatrix *svOrigMatrix = fMilleModule[intidx[idx[i]]]->GetSensitiveVolumeOrigGlobalMatrix(p.GetVolumeID());
+ // get back real local coordinates: use OriginalGlobalMatrix because AliTrackPoints were written
+ // with idel geometry
+ Double_t pg[3],pl[3];
+ pg[0]=p.GetX();
+ pg[1]=p.GetY();
+ pg[2]=p.GetZ();
+ AliDebug(3,Form("Global coordinates of measured point : X=%f Y=%f Z=%f \n",pg[0],pg[1],pg[2]));
+ svOrigMatrix->MasterToLocal(pg,pl);
+
+ AliDebug(3,Form("Local coordinates of measured point : X=%f Y=%f Z=%f \n",pl[0],pl[1],pl[2]));
+
+ // update covariance matrix
+ TGeoHMatrix hcov;
+ Double_t hcovel[9];
+ hcovel[0]=double(p.GetCov()[0]);
+ hcovel[1]=double(p.GetCov()[1]);
+ hcovel[2]=double(p.GetCov()[2]);
+ hcovel[3]=double(p.GetCov()[1]);
+ hcovel[4]=double(p.GetCov()[3]);
+ hcovel[5]=double(p.GetCov()[4]);
+ hcovel[6]=double(p.GetCov()[2]);
+ hcovel[7]=double(p.GetCov()[4]);
+ hcovel[8]=double(p.GetCov()[5]);
+ hcov.SetRotation(hcovel);
+ // now rotate in local system
+ hcov.Multiply(svOrigMatrix);
+ hcov.MultiplyLeft(&svOrigMatrix->Inverse());
+ // now hcov is LOCAL COVARIANCE MATRIX
+
+
+ // pepopepo
+ if (fBug==1) {
+ // correzione bug LAYER 5 SSD temporanea..
+ int ssdidx=AliITSAlignMilleModule::GetIndexFromVolumeID(p.GetVolumeID());
+ if (ssdidx>=500 && ssdidx<1248) {
+ int ladder=(ssdidx-500)%22;
+ if (ladder==18) p.SetVolumeID(AliITSAlignMilleModule::GetVolumeIDFromIndex(ssdidx+1));
+ if (ladder==19) p.SetVolumeID(AliITSAlignMilleModule::GetVolumeIDFromIndex(ssdidx-1));
+ }
+ }
+
+ /// get (evenctually prealigned) matrix of sens. vol.
+ TGeoHMatrix *svMatrix = fMilleModule[intidx[idx[i]]]->GetSensitiveVolumeMatrix(p.GetVolumeID());
+ // modify global coordinates according with pre-aligment
+ svMatrix->LocalToMaster(pl,pg);
+ // now rotate in local system
+ hcov.Multiply(&svMatrix->Inverse());
+ hcov.MultiplyLeft(svMatrix);
+ // hcov is back in GLOBAL RF
+ Float_t pcov[6];
+ pcov[0]=hcov.GetRotationMatrix()[0];
+ pcov[1]=hcov.GetRotationMatrix()[1];
+ pcov[2]=hcov.GetRotationMatrix()[2];
+ pcov[3]=hcov.GetRotationMatrix()[4];
+ pcov[4]=hcov.GetRotationMatrix()[5];
+ pcov[5]=hcov.GetRotationMatrix()[8];
+
+ p.SetXYZ(pg[0],pg[1],pg[2],pcov);
+ AliDebug(3,Form("New global coordinates of measured point : X=%f Y=%f Z=%f \n",pg[0],pg[1],pg[2]));
+ atps->AddPoint(npto,&p);
+ AliDebug(2,Form("Adding point[%d] = ( %f , %f , %f ) volid = %d",npto,atps->GetX()[npto],atps->GetY()[npto],atps->GetZ()[npto],atps->GetVolumeID()[npto] ));
+
+ npto++;
+ }
+
+ return atps;
+}
+
+
+
+AliTrackPointArray *AliITSAlignMille::SortTrack(const AliTrackPointArray *atp) {
+ /// sort alitrackpoints w.r.t. global Y direction
+ AliTrackPointArray *atps=NULL;
+ Int_t idx[20];
+ Int_t npts=atp->GetNPoints();
+ AliTrackPoint p;
+ atps=new AliTrackPointArray(npts);
+
+ TMath::Sort(npts,atp->GetY(),idx);
+
+ for (int i=0; i<npts; i++) {
+ atp->GetPoint(p,idx[i]);
+ atps->AddPoint(i,&p);
+ AliDebug(2,Form("Point[%d] = ( %f , %f , %f ) volid = %d",i,atps->GetX()[i],atps->GetY()[i],atps->GetZ()[i],atps->GetVolumeID()[i] ));
+ }
+ return atps;
+}
+
+