]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSRealignTracks.cxx
Improved treatment of dead areas in track chi2 calculation (A. Dainese)
[u/mrichter/AliRoot.git] / ITS / AliITSRealignTracks.cxx
index 2dfd09db377fbc1477d5fbb708e826e70c690e14..e6bccd7aabe80ee70f455568a7aeb06f614d2bed 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+//---------------------------------
+//Class to perform the realignment if the Inner Tracking System 
+//with an iterative approach based on track to cluster residuals
+// minimization. A chi2 function of the residuals is minimized with
+//respect to alignment parameters. The class allows both single module
+//realignment and set of modules realignment. Tracks are fitted with
+//AliTrackFitter* fitters. AliTrackFitterKalman is more suited for
+//straight line (e.g. cosmic in the absence of magnetic field) but can't
+//work with helixes. AliTrackFitterRieman is suited for helixes. 
+//The minimization is performed by AliTrackResiduals* classes: default
+//one is AliTrackResidualsFast (analytic minimization by inversion). 
+//For numerical minimization using MINUIT, use AliTrackResidualChi2.
+//Methods are present to defined both the set of modules where the tracks
+//are fittef and the set of modules which  are to be realigned
+//The main method is  AlignVolumesITS
+//
+//Class by: A. Rossi, andrea,rossi@ts.infn.it
 
-#include <TArray.h>
 #include <TFile.h>
 #include <TStopwatch.h>
-#include <TArray.h>
 #include <TNtuple.h>
 #include <TClonesArray.h>
 #include <TMath.h>
-#include <TGeoManager.h>
-#include <TSystem.h>
-#include <TGeoMatrix.h>
+#include <TGraph.h>
+#include <TCanvas.h>
+#include <TH1F.h>
 #include "AliITSRealignTracks.h"
-#include "AliAlignmentTracks.h"
 #include "AliAlignObjParams.h"
+#include "AliAlignObj.h"
 #include "AliGeomManager.h"
 #include "AliTrackFitter.h"
 #include "AliTrackFitterKalman.h"
 #include "AliTrackResidualsFast.h"
 #include "AliTrackResidualsChi2.h"
 #include "AliTrackResidualsLinear.h"
+
 #include "AliLog.h"
+#include <TSystem.h>
+#include <TGeoManager.h>
+
+class AliAlignmentTracks;
+class TGeoMatrix;
+class TArray;
+
+
+/* $Id$ */
 
 
 ClassImp(AliITSRealignTracks)
 
-const Int_t referSect=2;
+const Int_t kreferSect=2;
 
 
 AliITSRealignTracks::AliITSRealignTracks(TString minimizer,Int_t fit,Bool_t covUsed,TString fileintro,TString geometryfile,TString misalignmentFile,TString startingfile):
@@ -47,8 +72,27 @@ AliITSRealignTracks::AliITSRealignTracks(TString minimizer,Int_t fit,Bool_t covU
   fSurveyObjs(0),
   fgeomfilename(),
   fmintracks(),
-  fCovIsUsed(covUsed),
-  fUpdateCov(kFALSE)
+  fUpdateCov(kFALSE),
+  fVarySigmaY(kFALSE),
+  fCorrModules(0),
+  fLimitCorr(0),
+  fsigmaY(),
+  fDraw(kFALSE),  
+  fAlignDrawObjs(0), 
+  fCanvPar(0), 
+  fCanvGr(0), 
+  fgrIterMeanX(0), 
+  fgrIterRMSX(0),  
+  fgrIterMeanY(0), 
+  fgrIterRMSY(0),  
+  fgrIterMeanZ(0), 
+  fgrIterRMSZ(0),  
+  fgrIterMeanPsi(0), 
+  fgrIterRMSPsi(0),  
+  fgrIterMeanTheta(0), 
+  fgrIterRMSTheta(0),  
+  fgrIterMeanPhi(0), 
+  fgrIterRMSPhi(0)  
 {
 
   // minimizer="fast"->AliTrackResidualFast minimizer
@@ -78,7 +122,7 @@ AliITSRealignTracks::AliITSRealignTracks(TString minimizer,Int_t fit,Bool_t covU
   if(covUsed)SetCovIsUsed(kTRUE);
   if(!SelectFitter(fit))AliWarning("Incorrect fitter assignment!");
   if(!SelectMinimizer(minimizer))AliWarning("Incorrect minimizer assignment!");
-  
+  fsigmaY=1.;
   fmintracks=1;
   BuildIndex();
   
@@ -89,8 +133,28 @@ AliITSRealignTracks::AliITSRealignTracks(const AliITSRealignTracks &realignTrack
   fSurveyObjs(new AliAlignObj**(*realignTracks.fSurveyObjs)),
   fgeomfilename(realignTracks.fgeomfilename),
   fmintracks(realignTracks.fmintracks),
-  fCovIsUsed(realignTracks.fCovIsUsed),
-  fUpdateCov(realignTracks.fUpdateCov)
+  fUpdateCov(realignTracks.fUpdateCov),
+  fVarySigmaY(realignTracks.fVarySigmaY),
+  fCorrModules(new Double_t *(*realignTracks.fCorrModules)),
+  fLimitCorr(realignTracks.fLimitCorr),
+  fsigmaY(realignTracks.fsigmaY),
+  fDraw(kFALSE),  
+  fAlignDrawObjs(realignTracks.fAlignDrawObjs), 
+  fCanvPar(realignTracks.fCanvPar), 
+  fCanvGr(realignTracks.fCanvGr), 
+  fgrIterMeanX(realignTracks.fgrIterMeanX), 
+  fgrIterRMSX(realignTracks.fgrIterRMSX),  
+  fgrIterMeanY(realignTracks.fgrIterMeanY), 
+  fgrIterRMSY(realignTracks.fgrIterRMSY),  
+  fgrIterMeanZ(realignTracks.fgrIterMeanZ), 
+  fgrIterRMSZ(realignTracks.fgrIterRMSZ),  
+  fgrIterMeanPsi(realignTracks.fgrIterMeanPsi), 
+  fgrIterRMSPsi(realignTracks.fgrIterRMSPsi),  
+  fgrIterMeanTheta(realignTracks.fgrIterMeanTheta), 
+  fgrIterRMSTheta(realignTracks.fgrIterRMSTheta),  
+  fgrIterMeanPhi(realignTracks.fgrIterMeanPhi), 
+  fgrIterRMSPhi(realignTracks.fgrIterRMSPhi)  
+
 {//Copy Constructor
   AliWarning("Can't copy AliAlignmentTracks Data member!");
 }
@@ -108,6 +172,7 @@ AliITSRealignTracks::~AliITSRealignTracks(){
   //destructor
   
   if(fSurveyObjs)   DeleteSurveyObjs();
+  if(fAlignDrawObjs) DeleteDrawHists();
   //delete [] fSurveyObjs; 
   
 } 
@@ -115,6 +180,10 @@ AliITSRealignTracks::~AliITSRealignTracks(){
 
 //_____________________________
 Bool_t AliITSRealignTracks::SelectFitter(Int_t fit,Int_t minTrackPoint){
+  //Method to select the fitter: 0 for AliTrackFitterRieman (use this for helixes)
+  //                             1 for AliTrackFitterKalman
+  //minTrackPoint defines the minimum number of points (not rejected by the fit itself) 
+  //a track should have to fit it
  
   if(fit==1){
      AliTrackFitterKalman *fitter= new AliTrackFitterKalman();
@@ -135,6 +204,21 @@ Bool_t AliITSRealignTracks::SelectFitter(Int_t fit,Int_t minTrackPoint){
 
 
 Bool_t AliITSRealignTracks::SelectMinimizer(TString minimizer,Int_t minpoints,const Bool_t *coord){
+  //Method to select the minimizer: "minuit" for AliTrackFitterChi2 (numerical minimization by MINUIT)
+  //                                "fast" for AliTrackResidualsFast
+  //                                "linear" for AliTrackResidualsLinear
+  //                                "minuitnorot" for AliTrackFitterChi2 by 
+  // coord[6] allows to fix the degrees of freedom in the minimization (e.g. look only for tranlsations, 
+  //       or only for rotations). The coord are: dTx,dTy,dTz,dPsi,dTheta,dPhi where "d" stands for 
+  //       "differential" and "T" for translation. If coord[i] is set to kTRUE then the i coord is fixed
+  //       When a coordinate is fixed the value returnd for it is 0
+  // minnpoints fix the minimum number of residuals to perform the minimization: it's not safe
+  //     to align a module with a small number of track passing through it since the results could be
+  //     not reliable. For single modules the number of residuals and the number of tracks passing 
+  //     through it and accepted bu the fit procedure coincide. This is not the case for sets of modules,
+  //     since a given track can pass through two or more modules in the set (e.g a cosmic track can give
+  //     two cluster on a layer)
+
   AliTrackResiduals *res;
   if(minimizer=="minuit"){
     res = new AliTrackResidualsChi2();
@@ -174,7 +258,27 @@ Bool_t AliITSRealignTracks::SelectMinimizer(TString minimizer,Int_t minpoints,co
   return kTRUE;
 }
 
+//____________________________________
+void AliITSRealignTracks::SetVarySigmaY(Bool_t varysigmay,Double_t sigmaYfixed){
+  //SigmaY is the value of the error along the track direction assigned 
+  //to the AliTrackPoint constructed from the extrapolation of the fit of a track
+  //to the module one is realigning. This error simulate the uncertainty on the
+  //position of the cluster in space due to the misalingment of the module (i.e. 
+  //you don't the real position and orientation of the plane of the desired extrapolation 
+  //but you rely on the fit and consider the point to lie along the track)
+  
+  
+  fVarySigmaY=varysigmay;
+  if(!varysigmay){
+    if(sigmaYfixed>0.)fsigmaY=sigmaYfixed;
+    else {
+      printf("Negative assignment to sigmaY! set it to default value (1 cm) \n");
+      fsigmaY=1.;
+    }
+  }
+}
 
+//_______________________________________
 void AliITSRealignTracks::RealignITSVolIndependent(Int_t iter1,Int_t iterations,Int_t minNtracks,Int_t layer,Int_t minTrackPoint){
   
 
@@ -346,22 +450,22 @@ void AliITSRealignTracks::RealignITStracks(TString minimizer,Int_t fit=0,Int_t i
       for(Int_t siter=0;siter<5;siter++){
        fTrackFitter->SetMinNPoints(2);
        SetCovUpdate(kFALSE);
-       AlignSPDHalfBarrelToSectorRef(referSect,3);
+       AlignSPDHalfBarrelToSectorRef(kreferSect,3);
        //      AlignSPDBarrel(1);
        //      if(siter==0)SetCovUpdate(kFALSE);
        //      AlignSPDHalfBarrel(0,3);
        //      SetCovUpdate(kTRUE);
        AlignSPDHalfBarrelToHalfBarrel(1,3);
-       //      AlignSPDHalfBarrelToSectorRef(referSect,3);
+       //      AlignSPDHalfBarrelToSectorRef(kreferSect,3);
        for(Int_t sector=0;sector<10;sector++){
          SetMinNtracks(100);
-         if(sector==referSect)continue;
+         if(sector==kreferSect)continue;
          AlignSPDSectorWithSectors(sector,1);
        }
 
 
        for(Int_t lay=1;lay<=6;lay++){
-         if(!AlignLayerToSector(lay,referSect,3))AlignLayerToSPDHalfBarrel(lay,0,3);
+         if(!AlignLayerToSector(lay,kreferSect,3))AlignLayerToSPDHalfBarrel(lay,0,3);
        }
        AlignSPDHalfBarrel(0,3);
        
@@ -495,36 +599,51 @@ void AliITSRealignTracks::InitAlignObjs()
 }
 
 //______________________________________________________________________________
-Bool_t AliITSRealignTracks::InitSurveyObjs(Bool_t infinite,Double_t factor,Bool_t fromfile,TString filename,TString arrayName){
+void AliITSRealignTracks::ResetCorrModules(){
+  // Initialize and reset to 0 the array with the information on correlation
+  if(!fCorrModules){
+    Int_t nLayers = AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer;
+    fCorrModules = new Double_t*[nLayers];
+    for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+      fCorrModules[iLayer] = new Double_t[AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer)];
+      for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
+       fCorrModules[iLayer][iModule]=0.;
+      }
+    }
+  }
+  else{
+    for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+      for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
+       fCorrModules[iLayer][iModule]=0.;
+      }
+    }
+  }
+}
 
-  if(filename!=""){
+//______________________________________________________________________________
+Bool_t AliITSRealignTracks::InitSurveyObjs(Bool_t infinite,Double_t factor,TString filename,TString arrayName){
+  //Initialize the Survey Objects. There is the possibility to set them equal to external objects
+  //   stored in file filename. Otherwuse they are set equal to 0 and with default values for the variances
+  //infinite: set the cov matrix to extremly large values, so that the results of a minimization
+  //   are never rejected by the comparison with the survey
+  //factor: multiplication factor for the variances of the cov. matrix of the survey obj.
+
+  if(fSurveyObjs)DeleteSurveyObjs();
+  Bool_t fromfile=kFALSE;
+  TFile *surveyObj;
+  TClonesArray *clnarray;
+  if(!filename.IsNull()){
     //Initialize from file
     if(gSystem->AccessPathName(filename.Data(),kFileExists)){
       printf("Wrong Survey AlignObjs File Name \n");
       return kFALSE;
     } 
-    
-  TFile *surveyObj=TFile::Open(filename.Data());
-  if (!surveyObj || !surveyObj->IsOpen()) {
-    AliError(Form("Could not open SurveyObjs file: file %s !",filename.Data()));
-    return kFALSE;
-  }  
-  printf("Getting TClonesArray \n");
-  TClonesArray *clnarray=(TClonesArray*)surveyObj->Get(arrayName);
-  Int_t size=clnarray->GetSize();
-  UShort_t volid;
-  for(Int_t ivol=0;ivol<size;ivol++){
-    AliAlignObjParams *a=(AliAlignObjParams*)clnarray->At(ivol);
-    volid=a->GetVolUID();
-    Int_t iModule;
-    AliGeomManager::ELayerID iLayer = AliGeomManager::VolUIDToLayer(volid,iModule);
-    printf("Updating volume: %d ,layer: %d module: %d \n",volid,iLayer,iModule);
-    *fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule] *= *a;
-  }
-  delete clnarray;
-  surveyObj->Close();
-  return kTRUE;
+    if(arrayName.IsNull()){
+      printf("Null Survey Object Name! \n");
+      return kFALSE;
+    }
+   
+    fromfile=kTRUE;
   }
 
   // Initialize the alignment objects array with default values
@@ -534,44 +653,148 @@ Bool_t AliITSRealignTracks::InitSurveyObjs(Bool_t infinite,Double_t factor,Bool_
   Double_t cov[21];
   for(Int_t i=0;i<21;i++)cov[i]=0.;
   for(Int_t i=0;i<3;i++)cov[i*(i+1)/2+i]=0.1*0.1*v;//Set Default Error to 1 mm  for Translation 
-  for(Int_t i=3;i<6;i++)cov[i*(i+1)/2+i]=0.01*0.01*180.*180./3.14/3.14*v;//and 10 mrad (~0.5 degrees)for rotations (global ref. sysytem)
+  for(Int_t i=3;i<6;i++)cov[i*(i+1)/2+i]=0.03*0.03*180.*180./3.14/3.14*v;//and 30 mrad (~1.7 degrees)for rotations (global ref. sysytem)
+  
   Int_t nLayers = AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer;
   fSurveyObjs = new AliAlignObj**[nLayers];
   for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
     fSurveyObjs[iLayer] = new AliAlignObj*[AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer)];
+    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
+      fSurveyObjs[iLayer][iModule] = 0x0;
+    }
+  }
+  
+  if(fromfile){
+    surveyObj=TFile::Open(filename.Data());
+    if (!surveyObj || !surveyObj->IsOpen()) {
+      AliError(Form("Could not open SurveyObjs file: %s !",filename.Data()));
+      return kFALSE;
+    }
+    printf("Getting TClonesArray \n");
+    clnarray=(TClonesArray*)surveyObj->Get(arrayName);
+    Int_t size=clnarray->GetSize();
+    UShort_t volid;
+    for(Int_t ivol=0;ivol<size;ivol++){
+      AliAlignObjParams *a=(AliAlignObjParams*)clnarray->At(ivol);
+      volid=a->GetVolUID();
+      Int_t iModule;
+      AliGeomManager::ELayerID iLayer = AliGeomManager::VolUIDToLayer(volid,iModule);
+      if(iLayer<=0)continue;
+      if(a->GetUniqueID()==0)continue;
+      printf("Updating survey for volume: %d ,layer: %d module: %d from file\n",volid,iLayer,iModule);
+      fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule] = new AliAlignObjParams(*a);
+      fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule]->SetUniqueID(a->GetUniqueID());
+      fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule]->Print("");
+    }
+    delete clnarray;
+    surveyObj->Close();
+  }
+  for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
     for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
       UShort_t volid = AliGeomManager::LayerToVolUID(iLayer+ AliGeomManager::kFirstLayer,iModule);
-      if(!fromfile){
+      if(!fSurveyObjs[iLayer][iModule]){
+       printf("Updating survey for volume: %d ,layer: %d module: %d with default values \n",volid,iLayer,iModule);
        fSurveyObjs[iLayer][iModule] = new AliAlignObjParams(AliGeomManager::SymName(volid),volid,0,0,0,0,0,0,kTRUE);
        fSurveyObjs[iLayer][iModule]->SetCorrMatrix(cov);
        fSurveyObjs[iLayer][iModule]->SetUniqueID(0);
       }
+      
     }
   }
+  
   return kTRUE;
 }
 
 
 //______________________________________________________________________________
-void AliITSRealignTracks::ResetAlignObjs()
+Int_t AliITSRealignTracks::CheckWithSurvey(Double_t factor,const TArrayI *volids){
+  
+  // Check the parameters of the alignment objects in volids (or of all objects if volids is null) 
+  // are into the boundaries set by the cov. matrix of the survey objs
+  // Returns the number of objects out of boudaries
+  AliAlignObj *alignObj;       
+  Int_t outofsurv=0;
+  UShort_t volid;
+  Double_t surveycov[21],transl[3],rot[3],survtransl[3],survrot[3];
+  if(volids==0x0){
+    for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+      for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++){
+       volid=AliGeomManager::LayerToVolUIDSafe(iLayer+AliGeomManager::kFirstLayer,iModule);
+       alignObj=GetAlignObj(volid);
+       alignObj->GetPars(transl,rot);
+       fSurveyObjs[iLayer][iModule]->GetCovMatrix(surveycov);
+       fSurveyObjs[iLayer][iModule]->GetPars(survtransl,survrot);
+       if(TMath::Sqrt(TMath::Abs(surveycov[0]))*factor<TMath::Abs(transl[0]-survtransl[0])||TMath::Sqrt(TMath::Abs(surveycov[2]))*factor<TMath::Abs(transl[1]-survtransl[1])||TMath::Sqrt(TMath::Abs(surveycov[5]))*factor<TMath::Abs(transl[2]-survtransl[2])||TMath::Sqrt(TMath::Abs(surveycov[9]))*factor<TMath::Abs(rot[0]-survrot[0])||TMath::Sqrt(TMath::Abs(surveycov[14]))*factor<TMath::Abs(rot[1]-survrot[1])||TMath::Sqrt(TMath::Abs(surveycov[20]))*factor<TMath::Abs(rot[2]-survrot[2])){
+         printf("Results for module %d out of Survey: reinitializing it from survey \n",volid);
+         //      *alignObj = *alignObjSurv;
+         alignObj->SetPars(survtransl[0],survtransl[1],survtransl[2],survrot[0],survrot[1],survrot[2]);
+         alignObj->SetUniqueID(0);
+         if(fUpdateCov)alignObj->SetCorrMatrix(surveycov);
+         outofsurv++;
+       }
+      }
+    }
+  }
+  else{
+    Int_t iLayer;
+    Int_t iModule;
+    for(Int_t j=0;j<volids->GetSize();j++){
+      volid=volids->At(j);
+      alignObj=GetAlignObj(volid);
+      alignObj->GetPars(transl,rot);
+      iLayer=(Int_t)AliGeomManager::VolUIDToLayerSafe(volid,iModule)-(Int_t)AliGeomManager::kFirstLayer;
+      fSurveyObjs[iLayer][iModule]->GetCovMatrix(surveycov);
+      fSurveyObjs[iLayer][iModule]->GetPars(survtransl,survrot);
+      if(TMath::Sqrt(TMath::Abs(surveycov[0]))*factor<TMath::Abs(transl[0]-survtransl[0])||TMath::Sqrt(TMath::Abs(surveycov[2]))*factor<TMath::Abs(transl[1]-survtransl[1])||TMath::Sqrt(TMath::Abs(surveycov[5]))*factor<TMath::Abs(transl[2]-survtransl[2])||TMath::Sqrt(TMath::Abs(surveycov[9]))*factor<TMath::Abs(rot[0]-survrot[0])||TMath::Sqrt(TMath::Abs(surveycov[14]))*factor<TMath::Abs(rot[1]-survrot[1])||TMath::Sqrt(TMath::Abs(surveycov[20]))*factor<TMath::Abs(rot[2]-survrot[2])){
+       printf("Results for module %d out of Survey: reinitializing it from survey \n",volid);
+       //        *alignObj = *alignObjSurv;
+       alignObj->SetPars(survtransl[0],survtransl[1],survtransl[2],survrot[0],survrot[1],survrot[2]);
+       alignObj->SetUniqueID(0);
+       if(fUpdateCov)alignObj->SetCorrMatrix(surveycov);
+       outofsurv++;
+      }
+    }
+  }  
+  return outofsurv;
+}  
+
+//___________________________________________________________________
+
+void AliITSRealignTracks::ResetAlignObjs(Bool_t all,TArrayI *volids)
 {
-  // Reset the alignment objects array
-  for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
-    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++)
-      fAlignObjs[iLayer][iModule]->SetPars(0,0,0,0,0,0);
+  // Reset the alignment objects in volids or all if all=kTRUE
+  if(all){
+    for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+      for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++)
+       fAlignObjs[iLayer][iModule]->SetPars(0,0,0,0,0,0);
+    }
+  }
+  else{
+    Int_t layer;
+    Int_t mod;
+    for(Int_t j=0;j<volids->GetSize();j++){
+      layer=(Int_t)AliGeomManager::VolUIDToLayer(volids->At(j),mod)-(Int_t)AliGeomManager::kFirstLayer;
+      fAlignObjs[layer][mod]->SetPars(0,0,0,0,0,0);
+    }
   }
 }
 
 //______________________________________________-
 void AliITSRealignTracks::DeleteSurveyObjs()
-{
+{//destructor for the survey objs. array
+
+  if(!fSurveyObjs)return;
   // Delete the alignment objects array
   for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
-    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++)
-      if (fSurveyObjs[iLayer][iModule])
-       delete fSurveyObjs[iLayer][iModule];
-    delete [] fSurveyObjs[iLayer];
+    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++){
+      if (fSurveyObjs[iLayer][iModule])        delete fSurveyObjs[iLayer][iModule];
+    }
+    
+    if(fSurveyObjs[iLayer])delete [] fSurveyObjs[iLayer];
   }
+    
   delete [] fSurveyObjs;
   fSurveyObjs = 0;
 }
@@ -614,22 +837,24 @@ Bool_t AliITSRealignTracks::ReadAlignObjs(const char *alignObjFileName, const ch
 }
 
 //_________________________________________
-Bool_t AliITSRealignTracks::FirstAlignmentLayers(Bool_t *layers,Int_t minNtracks,Int_t iterations,TArrayI *volidsSet){
+Bool_t AliITSRealignTracks::FirstAlignmentLayers(const Bool_t *layers,Int_t minNtracks,Int_t iterations,Bool_t fitall,TArrayI *volidsSet){
 
   //Align all modules in the set of layers independently according to a sequence based on the number of tracks passing through a given module
   
   BuildIndex();
-  
+  TString name="DrawFirstAlignment_Layers";
   UShort_t voluid;
   Int_t **lastIndex;
   Int_t laymax = 0;
   Int_t modmax = 0;
-  Int_t maxntr=0,nMod=0;
-  Int_t size=0;
+  Int_t maxntr=0,nMod=0,modAligned=0,size=0;
   for(Int_t i=0;i<6;i++){
-    if(layers[i]==1)size+=AliGeomManager::LayerSize(i+AliGeomManager::kFirstLayer);
+    if(layers[i]==1){
+      size+=AliGeomManager::LayerSize(i+AliGeomManager::kFirstLayer);
+      name+=i+1;
+    }
   }
-  TArrayI *volFit3;
+
   TArrayI *volFit=new TArrayI(size);
   TArrayI *volFit2=new TArrayI(size-1);
   TArrayI *sequence=new TArrayI(size);
@@ -642,7 +867,7 @@ Bool_t AliITSRealignTracks::FirstAlignmentLayers(Bool_t *layers,Int_t minNtracks
     lastIndex[iLayer] = new Int_t[AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer)];
     for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
       lastIndex[iLayer][iModule] =  fLastIndex[iLayer][iModule];
-      if(layers[iLayer]==1){
+      if(iLayer<=(AliGeomManager::kSSD2-AliGeomManager::kFirstLayer)&&layers[iLayer]==1){
        volFit->AddAt(AliGeomManager::LayerToVolUID(iLayer+AliGeomManager::kFirstLayer,iModule),maxntr);
        maxntr++;
       }
@@ -669,31 +894,67 @@ Bool_t AliITSRealignTracks::FirstAlignmentLayers(Bool_t *layers,Int_t minNtracks
       nMod++;
     }
   }
-  
-  Int_t ilayer,imod;
-  for(Int_t iter=0;iter<iterations;iter++){ 
+
+  sequence->Set(nMod);
+
+ Int_t ilayer,imod;
+  for(Int_t iter=0;iter<iterations;iter++){
+    if(iter>0&&fDraw)UpdateDraw(sequence,iter,iter);
+    modAligned=0;
     for(Int_t k=0;k<nMod;k++){
+      TArrayI *volFit3;
       voluid=sequence->At(k);
       ilayer=AliGeomManager::VolUIDToLayer(voluid,imod);
       volIn->AddAt(voluid,0);
       found=0;
-      for(Int_t j=0;j<volFit->GetSize();j++){
-       if(volFit->At(j)!=volIn->At(0))volFit2->AddAt(volFit->At(j),j-found);
-       else found=1;
+      if(!fitall){
+       for(Int_t j=0;j<nMod;j++){
+         if(j==k){
+           found=1;
+           continue;
+         }
+         else volFit2->AddAt(sequence->At(j),j-found);
+       }
+       volFit2->Set(nMod-1);
+      }
+      else{
+       for(Int_t j=0;j<volFit->GetSize();j++){
+         if(volFit->At(j)!=volIn->At(0))volFit2->AddAt(volFit->At(j),j-found);
+         else found=1;
+       }
       }
       
       if(volidsSet){
        volFit3=IntersectVolArray(volidsSet,volFit2);
       }
-      else volFit3=volFit2;  
-      AlignVolumesITS(volIn,volFit3,AliGeomManager::kSPD1,AliGeomManager::kSSD1,2);
+      else volFit3=new TArrayI(*volFit2);
+      
+      
+      if(AlignVolumesITS(volIn,volFit3,AliGeomManager::kSPD1,AliGeomManager::kTPC1,2))modAligned++;
+      delete volFit3;
+    
     }
   }
+  Int_t noutofsurv=CheckWithSurvey(2.,sequence);
+  printf("%d modules into the sequence \n %d modules re-aligned \n %d modules moved far away from survey (-> reset) \n",nMod,modAligned,noutofsurv);
+  name.Append("_iter");
+  name+=iterations;
+  name.Append(".root");
+  if(fDraw)WriteHists(name.Data());
+  delete volFit;
+  delete volFit2;
+  delete sequence;
+
   return kTRUE;
+  
 }
 
 //__________________________________________
-Bool_t AliITSRealignTracks::FirstAlignmentSPD(Int_t minNtracks,Int_t iterations,TArrayI *volidsSet){
+Bool_t AliITSRealignTracks::FirstAlignmentSPD(Int_t minNtracks,Int_t iterations,Bool_t fitall,TArrayI *volidsSet){
+
+  //OBSOLETE METHOD: perform a stand-alone realignment of the SPD modules
+  //                 based on a sequence constructed accordingly to the number of tracks
+  //                 passing through each module
   
   BuildIndex();
    
@@ -701,7 +962,7 @@ Bool_t AliITSRealignTracks::FirstAlignmentSPD(Int_t minNtracks,Int_t iterations,
   Int_t **lastIndex;
   Int_t laymax = 0;
   Int_t modmax = 0;
-  Int_t maxntr=0,nMod=0;
+  Int_t maxntr=0,nMod=0,modAligned=0;
   TArrayI *volFit=new TArrayI(AliGeomManager::LayerSize(1)+AliGeomManager::LayerSize(2));
   TArrayI *volFit2=new TArrayI(AliGeomManager::LayerSize(1)+AliGeomManager::LayerSize(2)-1);
   TArrayI *sequence=new TArrayI(AliGeomManager::LayerSize(1)+AliGeomManager::LayerSize(2));
@@ -741,35 +1002,62 @@ Bool_t AliITSRealignTracks::FirstAlignmentSPD(Int_t minNtracks,Int_t iterations,
       volIn->AddAt(voluid,0);
     }
   }
-
-  TArrayI *volFit3;
+  sequence->Set(nMod);
+  
   Int_t ilayer,imod;
   for(Int_t iter=0;iter<iterations;iter++){ 
+    modAligned=0;
     for(Int_t k=0;k<nMod;k++){
+      TArrayI *volFit3;
       voluid=sequence->At(k);
       ilayer=AliGeomManager::VolUIDToLayer(voluid,imod);
       volIn->AddAt(voluid,0);
       found=0;
-      for(Int_t j=0;j<volFit->GetSize();j++){
-       if(volFit->At(j)!=volIn->At(0))volFit2->AddAt(volFit->At(j),j-found);
-       else found=1;
+      if(!fitall){
+       for(Int_t j=0;j<nMod;j++){
+         if(j==k){
+           found=1;
+           continue;
+         }
+         else volFit2->AddAt(sequence->At(j),j-found);
+       }
+       volFit2->Set(nMod-1);
+      }
+      else{
+       for(Int_t j=0;j<volFit->GetSize();j++){
+         if(volFit->At(j)!=volIn->At(0))volFit2->AddAt(volFit->At(j),j-found);
+         else found=1;
+       }
       }
       
       if(volidsSet){
        volFit3=IntersectVolArray(volidsSet,volFit2);
       }
-      else volFit3=volFit2;
+      else volFit3=new TArrayI(*volFit2);
+      
       
-      AlignVolumesITS(volIn,volFit3,AliGeomManager::kSPD1,AliGeomManager::kSDD1,2);
+      if(AlignVolumesITS(volIn,volFit3,AliGeomManager::kSPD1,AliGeomManager::kSDD1,2))modAligned++;
+      delete volFit3;
+      //      if(volidsSet)delete volFit3;
     }
   }
+  Int_t noutofsurv=CheckWithSurvey(2.,sequence);
+  printf("%d modules into the sequence \n %d modules re-aligned \n %d modules moved far away from survey (-> reset) \n",nMod,modAligned,noutofsurv);
+  delete volFit;
+  delete volFit2;
+  delete sequence;
+
   return kTRUE;
 }
 
 
 //__________________________________
 Bool_t AliITSRealignTracks::SPDmodulesAlignToSSD(Int_t minNtracks,Int_t iterations){
-
+  //Align each SPD module with at least minNtracks passing through it with respect to SSD
+  //The selection based on the minimum number of tracks is a fast one:
+  // the number considere here doesn't coincide with the tracks effectively used then in the
+  // minimization, it's just the total number of tracks in the sample passing through the module
+  // The procedure is iterated "iterations" times
   Int_t volSSD[6]={0,0,0,0,1,1};
   TArrayI *volOuter=GetLayersVolUID(volSSD);
   TArrayI *voluid=new TArrayI(1);
@@ -813,87 +1101,208 @@ Bool_t AliITSRealignTracks::AlignVolumesITS(const TArrayI *volids, const TArrayI
     AliError("Volume IDs array is empty!");
     return kFALSE;
   }
+  Bool_t correlated=kFALSE;
+  Double_t surveycov[21],transl[3],rot[3],survtransl[3],survrot[3];
+  Double_t frac;
 
-  Double_t surveycov[21],transl[3],rot[3];
+  TGeoHMatrix hM;
+  Double_t phiglob,smearing,rotorig[9],normplanevect[3]={0.,0.,0.},normplanevect2[3]={0.,0.,0.};  
+  Double_t *deltarot;
+  TMatrixDSym covmatrx(6);
+  AliAlignObj *modAlign;
 
   // Load only the tracks with at least one
   // space point in the set of volume (volids)
   BuildIndex();
   AliTrackPointArray **points;
   Bool_t failed=kFALSE;
-  Int_t pointsdim=0;
+  Int_t pointsdim=0,skipped=0;
   // Start the iterations
-  while (iterations > 0) {
+  while (iterations > 0){
+    normplanevect2[0]=0.;
+    normplanevect2[1]=0.;
+    normplanevect2[2]=0.;
+    if(fLimitCorr>0.){
+      ResetCorrModules();
+      skipped=0;
+    }
     Int_t nArrays = LoadPoints(volids, points,pointsdim);
-    if (nArrays < fmintracks) {
+    
+    if (nArrays < fmintracks||nArrays<=0){
       failed=kTRUE;
-      printf("Not enough tracks to try minimization \n");
+      printf("Not enough tracks to try minimization: volUID %d and following in volids \n", volids->At(0));
       UnloadPoints(pointsdim, points);
       break;
     }
-
+    frac=1./(Double_t)nArrays;
     AliTrackResiduals *minimizer = CreateMinimizer();
     minimizer->SetNTracks(nArrays);
     minimizer->InitAlignObj();
     AliTrackFitter *fitter = CreateFitter();
+
+    //Here prepare to set the plane for GetPCArot
+                                                       //    if(volids->GetSize()==1){//TEMPORARY: to be improved
+    AliGeomManager::GetOrigRotation(volids->At(0),rotorig);
+    if((Int_t)AliGeomManager::VolUIDToLayer(volids->At(0))==1){//TEMPORARY: to be improved  
+      normplanevect[0]=-rotorig[1];
+      normplanevect[1]=-rotorig[4];
+      normplanevect[2]=0.;
+    }
+    else{
+         normplanevect[0]=rotorig[1];
+         normplanevect[1]=rotorig[4];
+         normplanevect[2]=0.;
+    }
+    
+    phiglob=TMath::ATan2(normplanevect[1],normplanevect[0]);
+    
+    modAlign=GetAlignObj(volids->At(0));
+    modAlign->GetMatrix(hM);
+    deltarot=hM.GetRotationMatrix();
+    for(Int_t j=0;j<3;j++){
+      for(Int_t i=0;i<3;i++){
+       normplanevect2[j]+=deltarot[j*3+i]*normplanevect[i];
+      }
+      // printf("Here the difference: norm1[%d]=%f  norm2[%d]=%f \n",j,normplanevect[j],j,normplanevect2[j]);
+    }
+    
+    if(fVarySigmaY){
+      if(modAlign->GetUniqueID()==0)smearing=fsigmaY;
+      else{
+       modAlign->GetCovMatrix(covmatrx);
+       smearing=5.*5.*(covmatrx(0,0)+covmatrx(1,1)+covmatrx(2,2)+10.*10.*covmatrx(3,3)+10.*10.*covmatrx(4,4)+10.*10.*covmatrx(5,5))/6.; 
+       //This is a sort of average: the trace with the variances of the angles 
+       //weighted with 10 cm divided per 6 and the result multiplied per 25 
+       // (the sqrt would be 5 times a sort of "mean sigma" )
+       //       
+         }
+    }
+    else smearing=fsigmaY;
+    printf("This is the sigmaY value: %f \n",smearing);
+    // the plane will be set into the loop on tracks
+    
+
     for (Int_t iArray = 0; iArray < nArrays; iArray++) {
       if (!points[iArray]) continue;
-      fitter->SetTrackPointArray(points[iArray], kTRUE);
+      points[iArray]->Sort(kTRUE);
+      fitter->SetTrackPointArray(points[iArray], kFALSE);
+      // printf("Here normplane vect: %f \n",normplanevect2[1]); //TO BE REPLACED BY      fitter->SetNormPlaneVect(normplanevect2);
       if (fitter->Fit(volids,volidsfit,layerRangeMin,layerRangeMax) == kFALSE) continue;
+      
+       if(fLimitCorr>0.){
+       correlated=kFALSE;
+       AliTrackPoint p;
+       Int_t layer,module;
+       TArrayI *volparray=new TArrayI(points[iArray]->GetNPoints());
+       for(Int_t point=0;point<points[iArray]->GetNPoints();point++){
+         points[iArray]->GetPoint(p,point);
+         volparray->AddAt(p.GetVolumeID(),point);
+       }
+       TArrayI *volpArray=ExcludeVolidsFromVolidsArray(volids,volparray);
+       for(Int_t point=0;point<volpArray->GetSize();point++){
+         layer=(Int_t)AliGeomManager::VolUIDToLayerSafe(volpArray->At(point),module);  
+         if(fCorrModules[layer-AliGeomManager::kFirstLayer][module]>fLimitCorr){
+           correlated=kTRUE;
+           //      printf("volid %d, iarray = %d : skipping %d for Volume: %d \n",volids->At(0),iArray,skipped,volpArray->At(point));
+           skipped++;
+           break;
+         }
+       }
+       if(!correlated){
+         for(Int_t point=0;point<volpArray->GetSize();point++){
+           layer=(Int_t)AliGeomManager::VolUIDToLayerSafe(volpArray->At(point),module);  
+           //printf("Number of common tracks: %d \n",fCorrModules[layer-AliGeomManager::kFirstLayer][module]);
+           fCorrModules[layer-AliGeomManager::kFirstLayer][module]+=frac;
+           delete volparray;
+           delete volpArray;
+         }
+       }
+               else { 
+         delete volparray;
+         delete volpArray;
+         continue;
+       }
+       }
+       
       AliTrackPointArray *pVolId,*pTrack;
       fitter->GetTrackResiduals(pVolId,pTrack);
       minimizer->AddTrackPointArrays(pVolId,pTrack);
     }
-    if(minimizer->GetNFilledTracks()<=fmintracks){
-      printf("No good tracks found: could not find parameter for volume %d (and following in volids)\n",volids->At(0));
+    
+    printf("Number of tracks considered: %d \n",nArrays);
+    frac=(Double_t)skipped/(Double_t)nArrays;
+    printf("Number of tracks skipped cause of correlation: %d (fraction: %f )\n",skipped,frac);
+    
+    Int_t ntracks=minimizer->GetNFilledTracks();
+    frac=(Double_t)ntracks/(Double_t)nArrays;
+    printf("Number of tracks into the minimizer: %d (fraction: %f )\n",ntracks,frac);
+    if(ntracks<=fmintracks){
+      printf("Not enough good tracks found: could not find parameter for volume %d (and following in volids)\n",volids->At(0));
       UnloadPoints(pointsdim, points);
       failed=kTRUE;
       break;
     }
+    
     failed=(!minimizer->Minimize());
     
     // Update the alignment object(s)
     if (fDoUpdate) for (Int_t iVolId = 0; iVolId < nVolIds; iVolId++) {
       UShort_t volid = (*volids)[iVolId];
-      Int_t iModule;
-      AliGeomManager::ELayerID iLayer = AliGeomManager::VolUIDToLayer(volid,iModule);
-      
-      AliAlignObj *alignObj = fAlignObjs[iLayer-AliGeomManager::kFirstLayer][iModule];  
-      AliAlignObj *alignObjSurv = fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule];      
-      
       if(!failed){
-       if(fUpdateCov)*alignObj *= *minimizer->GetAlignObj();
-       else{
-         TMatrixDSym covmatrx(6);
-         alignObj->GetCovMatrix(covmatrx);
-         *alignObj *= *minimizer->GetAlignObj();
-         alignObj->SetCorrMatrix(covmatrx);
-         alignObj->SetUniqueID(1);
-       }
-       alignObjSurv->GetCovMatrix(surveycov);
-       alignObj->GetPars(transl,rot);
-       
-       if(TMath::Sqrt(TMath::Abs(surveycov[0]))*2<TMath::Abs(transl[0])||TMath::Sqrt(TMath::Abs(surveycov[2]))*2<TMath::Abs(transl[1])||TMath::Sqrt(TMath::Abs(surveycov[5]))*2<TMath::Abs(transl[2])||TMath::Sqrt(TMath::Abs(surveycov[9]))*2<TMath::Abs(rot[0])||TMath::Sqrt(TMath::Abs(surveycov[14]))*2<TMath::Abs(rot[1])||TMath::Sqrt(TMath::Abs(surveycov[20]))*2<TMath::Abs(rot[2])){
-         printf("Results for module %d out of Survey: reinitializing it from survey \n",volid);
-         //      *alignObj = *alignObjSurv;
-         alignObj->SetPars(0.,0.,0.,0.,0.,0.);
-         alignObj->SetUniqueID(0);
-         if(fUpdateCov)alignObj->SetCorrMatrix(surveycov);
+       Int_t iModule;
+       AliGeomManager::ELayerID iLayer = AliGeomManager::VolUIDToLayer(volid,iModule);
+       AliAlignObj *alignObj = fAlignObjs[iLayer-AliGeomManager::kFirstLayer][iModule];  
+
+       //Check the last minimization is not too large
+       minimizer->GetAlignObj()->GetPars(transl,rot);     
+       fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule]->GetCovMatrix(surveycov);
+       fSurveyObjs[iLayer-AliGeomManager::kFirstLayer][iModule]->GetPars(survtransl,survrot);
+       if(TMath::Sqrt(TMath::Abs(surveycov[0]))*2<TMath::Abs(transl[0]-survtransl[0])||TMath::Sqrt(TMath::Abs(surveycov[2]))*2<TMath::Abs(transl[1]-survtransl[1])||TMath::Sqrt(TMath::Abs(surveycov[5]))*2<TMath::Abs(transl[2]-survtransl[2])||TMath::Sqrt(TMath::Abs(surveycov[9]))*2<TMath::Abs(rot[0]-survrot[0])||TMath::Sqrt(TMath::Abs(surveycov[14]))*2<TMath::Abs(rot[1]-survrot[1])||TMath::Sqrt(TMath::Abs(surveycov[20]))*2<TMath::Abs(rot[2]-survrot[2])){
+         printf("Results for module %d too large: can't update them \n",volid);
+         alignObj->SetUniqueID(2);
          if(iterations==1){
            failed=kTRUE;
          }
        }
+       else{
+         if(fUpdateCov){
+           *alignObj *= *minimizer->GetAlignObj();
+           alignObj->SetUniqueID(1);
+         }
+         else{
+           alignObj->GetCovMatrix(covmatrx);
+           *alignObj *= *minimizer->GetAlignObj();
+           alignObj->SetCorrMatrix(covmatrx);
+           alignObj->SetUniqueID(1);
+         }
+         
+         /*alignObj->GetPars(transl,rot);
+         
+         if(TMath::Sqrt(TMath::Abs(surveycov[0]))*20<TMath::Abs(transl[0])||TMath::Sqrt(TMath::Abs(surveycov[2]))*20<TMath::Abs(transl[1])||TMath::Sqrt(TMath::Abs(surveycov[5]))*20<TMath::Abs(transl[2])||TMath::Sqrt(TMath::Abs(surveycov[9]))*20<TMath::Abs(rot[0])||TMath::Sqrt(TMath::Abs(surveycov[14]))*20<TMath::Abs(rot[1])||TMath::Sqrt(TMath::Abs(surveycov[20]))*20<TMath::Abs(rot[2])){
+         printf("Results for module %d out of Survey: reinitializing it from survey \n",volid);
+           //    *alignObj = *alignObjSurv;
+           alignObj->SetPars(0.,0.,0.,0.,0.,0.);
+           alignObj->SetUniqueID(0);
+           if(fUpdateCov)alignObj->SetCorrMatrix(surveycov);
+           if(iterations==1){
+           failed=kTRUE;
+           }
+           }*/
+       }
+       if(iterations==1)alignObj->Print("");
       }
       else {
        printf("Minimization failed: cannot update AlignObj for volume: %d \n",volid);
       }
-      if(iterations==1)alignObj->Print("");
     }
-    
     UnloadPoints(pointsdim,points);
     if(failed)break;
+    minimizer->InitAlignObj();
     iterations--;
   }
+  
+  printf("\n \n");
+
   return (!failed);
 }
 
@@ -901,6 +1310,7 @@ Bool_t AliITSRealignTracks::AlignVolumesITS(const TArrayI *volids, const TArrayI
 
 //______________________________________________
 Bool_t AliITSRealignTracks::AlignSPDBarrel(Int_t iterations){
+  //Align the SPD barrel "iterations" times
   
   Int_t size=0,size2=0;
   Int_t layers[6]={1,1,0,0,0,0};
@@ -931,7 +1341,12 @@ Bool_t AliITSRealignTracks::AlignSPDBarrel(Int_t iterations){
 
 //______________________
 Bool_t AliITSRealignTracks::AlignSPDHalfBarrel(Int_t method,Int_t iterations){
+  //Align a SPD Half barrel "iterations" times
+  //method 0 : align SPDHalfBarrel Up without using the points on SPD Half Barrel down in the fits (only outer layers)
+  //method 1 : align SPDHalfBarrel Down without using the points on SPD Half Barrel up in the fits (only outer layers)
+  //method 10 : align SPDHalfBarrel Up using also the points on SPD Half Barrel down in the fits (and points on outer layers)
+  //method 11 : align SPDHalfBarrel Down using also the points on SPD Half Barrel up in the fits (and points on outer layers)
+
   Int_t size=0,size2=0;
   Int_t layers[6]={0,0,1,1,1,1};
   Int_t sectorsUp[10]={1,1,1,1,1,0,0,0,0,0}; 
@@ -978,6 +1393,7 @@ Bool_t AliITSRealignTracks::AlignSPDHalfBarrel(Int_t method,Int_t iterations){
 
 //______________________________________________________
 Bool_t AliITSRealignTracks::AlignLayer(Int_t layer,Int_t iterations){
+  //Align the layer "layer" iterations times
 
   Int_t size=0,size2=0;
   Int_t layers[6]={0,0,0,0,0,0};
@@ -1008,8 +1424,15 @@ Bool_t AliITSRealignTracks::AlignLayer(Int_t layer,Int_t iterations){
 
 //___________________________________________
 
-Bool_t AliITSRealignTracks::AlignLayersToLayers(Int_t *layer,Int_t iterations){
+Bool_t AliITSRealignTracks::AlignLayersToLayers(const Int_t *layer,Int_t iterations){
 
+  //Align the set of layers A with respect to the set of layers B iterations time.
+  //The two sets A and B are defined into *layer==layer[6] the following way:
+  //   layer[i]=0 the layer is skipped both in the fits than in the minimization
+  //   layer[i]=1 the layer is skipped in the fits and considered in the minimization
+  //   layer[i]=2 the layer is considered in the fits and skipped in the minimization
+  //   layer[i]=3 the layer is considered both in the fits and in the minimization
+  
   UShort_t volid;
   Int_t size=0,size2=0,j=0,k=0;
   Int_t iLayer;
@@ -1069,6 +1492,7 @@ Bool_t AliITSRealignTracks::AlignLayersToLayers(Int_t *layer,Int_t iterations){
 //______________________________________________
 
 Bool_t AliITSRealignTracks::AlignSPDSectorToOuterLayers(Int_t sector,Int_t iterations){
+  //Align the SPD sector "sector" with respect to outer layers iterations times
 
   
   Int_t layers[6]={0,0,1,1,1,1};
@@ -1099,7 +1523,7 @@ Bool_t AliITSRealignTracks::AlignSPDSectorToOuterLayers(Int_t sector,Int_t itera
 
 //______________________________________________
 Bool_t AliITSRealignTracks::AlignSPDSectorWithSectors(Int_t sector,Int_t iterations){
-
+  //Align the SPD sector "sector" with respect to the other SPD sectors iterations times
 
   Int_t sectorsIN[10]={0,0,0,0,0,0,0,0,0,0};
   Int_t sectorsFit[10]={1,1,1,1,1,1,1,1,1,1};
@@ -1122,6 +1546,8 @@ Bool_t AliITSRealignTracks::AlignSPDSectorWithSectors(Int_t sector,Int_t iterati
 
 //___________________________________________________
 Bool_t AliITSRealignTracks::AlignSPDSectorsWithSectors(Int_t *sectorsIN,Int_t *sectorsFit,Int_t iterations){
+  //Align SPD sectors defined in "sectorsIN" with respect to 
+  //SPD sectors defined in "sectorsFit" iterations time
 
   TArrayI *volIDs=GetSPDSectorsVolids(sectorsIN);
   TArrayI *volIDsFit=GetSPDSectorsVolids(sectorsFit);;   
@@ -1129,15 +1555,36 @@ Bool_t AliITSRealignTracks::AlignSPDSectorsWithSectors(Int_t *sectorsIN,Int_t *s
   printf("Aligning SPD sectors: modules: %d \n",volIDs->GetSize());  
   printf("Fitting modules: %d \n",volIDsFit->GetSize());
   
-  AlignVolumesITS(volIDs,volIDsFit,AliGeomManager::kSPD1,AliGeomManager::kSDD1,iterations);
   
-  return kTRUE; 
+  
+  return AlignVolumesITS(volIDs,volIDsFit,AliGeomManager::kSPD1,AliGeomManager::kSDD1,iterations);; 
+}
+
+//___________________________________________________
+Bool_t AliITSRealignTracks::AlignSPDStaves(Int_t *staves,Int_t *sectorsIN,Int_t *sectorsFit,Int_t iterations){
+  //Align SPD staves defined by staves and sectorsIN with respect to sectorsFit volumes iterations times
+
+  TArrayI *volIDs=GetSPDStavesVolids(sectorsIN,staves);
+  TArrayI *volIDsFit=GetSPDSectorsVolids(sectorsFit);   
+  
+  if(volIDs->GetSize()==0){
+    printf("EMPTY ARRAY !! \n");
+    return kFALSE;
+  }
+  printf("Aligning SPD staves: modules: %d \n",volIDs->GetSize());  
+  printf("Fitting modules: %d \n",volIDsFit->GetSize());
+
+  TArrayI *volIDsFit2=ExcludeVolidsFromVolidsArray(volIDs,volIDsFit);
+  return  AlignVolumesITS(volIDs,volIDsFit2,AliGeomManager::kSPD1,AliGeomManager::kSSD1,iterations); 
 }
 
 
 //___________________________________________
 
 Bool_t AliITSRealignTracks::AlignLayerToSPDHalfBarrel(Int_t layer,Int_t updown,Int_t iterations){
+  //Align the layer "layer" with respect to SPD Half Barrel Up (updowon=0) 
+  //or Down (updown=1) iterations times
+  
 
 
   Int_t sectorsDown[10]={0,0,0,0,0,1,1,1,1,1};
@@ -1170,7 +1617,8 @@ Bool_t AliITSRealignTracks::AlignLayerToSPDHalfBarrel(Int_t layer,Int_t updown,I
 //___________________________________________
 
 Bool_t AliITSRealignTracks::AlignLayerToSector(Int_t layer,Int_t sector,Int_t iterations){
-  
+  //Align the layer "layer" with respect to SPD sector "sector" iterations times
+
   if(sector>9){
     printf("Wrong Sector selection! \n");
     return kFALSE;
@@ -1196,6 +1644,8 @@ Bool_t AliITSRealignTracks::AlignLayerToSector(Int_t layer,Int_t sector,Int_t it
 //_______________________________________________
 
 Bool_t AliITSRealignTracks::AlignSPDHalfBarrelToHalfBarrel(Int_t updown,Int_t iterations){
+  //Align the SPD Half Barrel Up[Down] with respect to HB Down[Up] iterations time if
+  //updown=0[1]
 
   
   Int_t sectorsDown[10]={0,0,0,0,0,1,1,1,1,1};
@@ -1225,6 +1675,7 @@ Bool_t AliITSRealignTracks::AlignSPDHalfBarrelToHalfBarrel(Int_t updown,Int_t it
 
 //_______________________
 Bool_t AliITSRealignTracks::AlignSPDHalfBarrelToSectorRef(Int_t sector,Int_t iterations){
+  //Align the SPD Half Barrel Down with respect to sector "sector" iterations times
 
   Int_t sectorsIN[10]={0,0,0,0,0,1,1,1,1,1};
   Int_t sectorsFit[10]={0,0,0,0,0,0,0,0,0,0};
@@ -1245,6 +1696,8 @@ Bool_t AliITSRealignTracks::AlignSPDHalfBarrelToSectorRef(Int_t sector,Int_t ite
 }
 //_________________________________________
 Bool_t AliITSRealignTracks::AlignSPD1SectorRef(Int_t sector,Int_t iterations){
+  //OBSOLETE METHOD: Align the SPD1 modules of sector "sector" with respect 
+  // to the other SPD volumes iterations times
 
   Int_t sectorsIN[10]={0,0,0,0,0,0,0,0,0,0};
   Int_t sectorsFit[10]={1,1,1,1,1,1,1,1,1,1};
@@ -1286,6 +1739,10 @@ Bool_t AliITSRealignTracks::AlignSPD1SectorRef(Int_t sector,Int_t iterations){
 //_____________________________________________
 
 AliAlignObjParams* AliITSRealignTracks::MediateAlignObj(TArrayI *volIDs,Int_t lastVolid){
+  //TEMPORARY METHOD: perform an average of the values of the parameters of the AlignObjs 
+  // defined by the array volIDs up to lastVolid position in this array
+  //The aim of such a method is to look for collective movement of a given set of modules
+
   UShort_t volid;
 
   TGeoHMatrix hm;
@@ -1321,8 +1778,78 @@ AliAlignObjParams* AliITSRealignTracks::MediateAlignObj(TArrayI *volIDs,Int_t la
   
 }
 
+
 //________________________________________________
-TArrayI* AliITSRealignTracks::GetSPDSectorsVolids(Int_t *sectors) 
+TArrayI* AliITSRealignTracks::GetSPDStavesVolids(const Int_t *sectors,const Int_t* staves){
+
+  
+  // This method gets the volID Array for the chosen staves into the 
+  // chosen sectors. You have to pass an array (10 dim) with a 1 for each 
+  // selected sector and an array (6 dim) with a 1 for each chosen stave.    
+  // The staves are numbered in this way: 0,1 for SPD1 and 2,3,4,5 for SPD2
+  // i.e. sectors[10] = {1,1,0,0,0,0,0,0,1,0} -> Sector 0, 1, 9 selected.
+  // staves[6]={0,1,1,0,0,1} -> Staves 1 on SPD1 and 0 and 3 on SPD2 selected
+  
+  Int_t nSect=0,nStaves=0;
+  Int_t last=0;
+
+  for(Int_t co=0;co<10;co++){ //counts the number of sectors chosen
+    if(sectors[co]==1) nSect++;
+  }
+
+  for(Int_t co=0;co<6;co++){ //counts the number of sectors chosen
+    if(staves[co]==1) nStaves++;
+  }
+  
+  if(nSect<1||nStaves<1){ //if no sector chosen -> exit
+    Printf("Error! No Sector/s or staves Selected!");
+    return 0x0;
+  }
+
+  TArrayI *volIDs = new TArrayI(nSect*nStaves*4);
+  TString stave="/Stave",str,symn,laystr;
+  
+  TArrayI *sectvol=GetSPDSectorsVolids(sectors); 
+  //SPD1 
+  laystr="SPD0";
+  for(Int_t k=0;k<2;k++){
+    if(staves[k]==1){
+      str=stave;
+      str+=k;
+      for(Int_t i=0;i<sectvol->GetSize();i++){
+       symn=AliGeomManager::SymName(sectvol->At(i));
+       if(symn.Contains(str)&&symn.Contains(laystr)){
+         //      printf("Adding: %s \n",symn.Data());
+         volIDs->AddAt(sectvol->At(i),last);
+         last++;
+       }
+      }
+    }
+  }
+  //SPD1 
+  laystr="SPD1";
+  for(Int_t k=2;k<6;k++){
+    if(staves[k]==1){
+      str=stave;
+      str+=k-2;
+      for(Int_t i=0;i<sectvol->GetSize();i++){
+       symn=AliGeomManager::SymName(sectvol->At(i));
+       if(symn.Contains(str)&&symn.Contains(laystr)){
+         volIDs->AddAt(sectvol->At(i),last);
+         printf("Adding: %s \n",symn.Data());
+         last++;
+       }
+      }
+    }
+  }
+
+  volIDs->Set(last);
+  return volIDs;
+} 
+
+//________________________________________________
+TArrayI* AliITSRealignTracks::GetSPDSectorsVolids(const Int_t *sectors) 
 {
   //
   // This method gets the volID Array for the chosen sectors.
@@ -1610,7 +2137,10 @@ TArrayI* AliITSRealignTracks::GetSPDSectorsVolids(Int_t *sectors)
 }
 
 //___________________________________
-TArrayI* AliITSRealignTracks::GetLayersVolUID(Int_t *layer){
+TArrayI* AliITSRealignTracks::GetLayersVolUID(const Int_t *layer){
+
+  //return a TArrayI with the volUIDs of the modules into the set of layers
+  //defined by layer[6]
   
   TArrayI *out=new TArrayI(2198);
   Int_t last=0;
@@ -1631,6 +2161,8 @@ TArrayI* AliITSRealignTracks::GetLayersVolUID(Int_t *layer){
 
 //_________________
 TArrayI* AliITSRealignTracks::SelectLayerInVolids(const TArrayI *volidsIN,AliGeomManager::ELayerID layer){
+  //Select between the modules specified by their volUIDs in volidsIN only those
+  // of a given layer "layer"
 
   Int_t size=volidsIN->GetSize();
   Int_t count=0;
@@ -1651,6 +2183,8 @@ TArrayI* AliITSRealignTracks::SelectLayerInVolids(const TArrayI *volidsIN,AliGeo
 //______________________________________________
 
 TArrayI* AliITSRealignTracks::IntersectVolArray(const TArrayI *vol1,const TArrayI *vol2){
+
+  //Perform the intersection between the array vol1 and vol2
   
   Int_t size1=vol1->GetSize();
   Int_t size2=vol2->GetSize();
@@ -1675,7 +2209,7 @@ TArrayI* AliITSRealignTracks::IntersectVolArray(const TArrayI *vol1,const TArray
 //_________________________________________
 
 TArrayI* AliITSRealignTracks::JoinVolArrays(const TArrayI *vol1,const TArrayI *vol2){
-  //!BE CAREFUL: If an index is repeated in vol1 or vol2 will be repeated also in the final array
+  //!BE CAREFUL: If an index is repeated into vol1 or into vol2 will be repeated also in the final array
   
   Int_t size1=vol1->GetSize();
   Int_t size2=vol2->GetSize();
@@ -1689,11 +2223,11 @@ TArrayI* AliITSRealignTracks::JoinVolArrays(const TArrayI *vol1,const TArrayI *v
     volidOut->AddAt(volid,k);
   }
  
-  for(Int_t k=0;k<size1;k++){
+  for(Int_t k=0;k<size2;k++){
     found=kFALSE;
-    volid=vol1->At(k);
-    for(Int_t j=0;j<size2;j++){
-      if(vol2->At(j)==volid)found=kTRUE;
+    volid=vol2->At(k);
+    for(Int_t j=0;j<size1;j++){
+      if(volidOut->At(j)==volid)found=kTRUE;
     }
     if(!found){
       volidOut->AddAt(volid,size1+count);
@@ -1707,6 +2241,7 @@ TArrayI* AliITSRealignTracks::JoinVolArrays(const TArrayI *vol1,const TArrayI *v
 //______________________________________
 
 TArrayI* AliITSRealignTracks::ExcludeVolidsFromVolidsArray(const TArrayI *volidsToExclude,const TArrayI *volStart){
+  //Excludes the modules defined by their volUID in the array volidsToExclude from the array volStart
 
   Int_t size1=volidsToExclude->GetSize();
   Int_t size2=volStart->GetSize();
@@ -1736,7 +2271,9 @@ TArrayI* AliITSRealignTracks::ExcludeVolidsFromVolidsArray(const TArrayI *volids
 
 //________________________________________
 
-TArrayI* AliITSRealignTracks::GetLayerVolumes(Int_t *layer){
+TArrayI* AliITSRealignTracks::GetLayerVolumes(const Int_t *layer){
+  //returns a TArrayI with the volUIDs of the modules of the layers
+  //specified into *layer
   
   TArrayI *out=new TArrayI(2198);
   Int_t last=0;
@@ -1754,3 +2291,326 @@ TArrayI* AliITSRealignTracks::GetLayerVolumes(Int_t *layer){
   out->Set(last);
   return out;
 }
+
+
+
+//______________________________
+TArrayI* AliITSRealignTracks::GetAlignedVolumes(char *filename){
+  //Open the file "filename" which is expected to contain
+  //a TClonesArray named "ITSAlignObjs" with stored a set of AlignObjs
+  //returns an array with the volumes UID of the modules considered realigned 
+  
+  if(gSystem->AccessPathName(filename)){
+    printf("Wrong Realignment file name \n");
+    return 0x0;
+  }
+  TFile *f=TFile::Open(filename,"READ");
+  TClonesArray *array=(TClonesArray*)f->Get("ITSAlignObjs");
+  AliAlignObjParams *a;
+  Int_t last=0;
+  TArrayI *volidOut=new TArrayI(2200);
+  for(Int_t j=0;j<array->GetSize();j++){
+    a=(AliAlignObjParams*)array->At(j);
+    if(a->GetUniqueID()==0)continue;
+    
+    else {
+      volidOut->AddAt(a->GetVolUID(),last);
+      last++;                                                                
+    }
+  }
+  volidOut->Set(last);
+  f->Close();
+  return volidOut;
+}
+
+
+//________________________________________
+void AliITSRealignTracks::SetDraw(Bool_t draw,Bool_t refresh){
+  //TEPMORARY METHOD: method to switch on/off the drawing of histograms
+  // if refresh=kTRUE deletes the old histos and constructs new ones
+  
+  if(refresh){
+    // WriteHists();
+    if(fAlignDrawObjs)DeleteDrawHists();
+    InitDrawHists();
+  }
+  fDraw=draw;
+  return;
+}
+
+void AliITSRealignTracks::DeleteDrawHists(){
+  //Delete the pointers to the histograms
+
+  for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
+      delete fAlignDrawObjs[iLayer][iModule];
+    }
+    if(fAlignDrawObjs[iLayer])delete [] fAlignDrawObjs[iLayer];
+  }
+  
+  delete [] fAlignDrawObjs;
+  fAlignDrawObjs = 0;
+
+  
+  delete fCanvPar;
+  delete fCanvGr; 
+  delete fgrIterMeanX;
+  delete fgrIterRMSX; 
+  delete fgrIterMeanY;
+  delete fgrIterRMSY;
+  delete fgrIterMeanZ;
+  delete fgrIterRMSZ;
+  delete fgrIterMeanPsi;
+  delete fgrIterRMSPsi;
+  delete fgrIterMeanTheta;
+  delete fgrIterRMSTheta;
+  delete fgrIterMeanPhi;
+  delete fgrIterRMSPhi;  
+
+} 
+
+void AliITSRealignTracks::InitDrawHists(){
+  //Initialize the histograms to monitor the results
+
+  Int_t nLayers = AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer;
+  fAlignDrawObjs = new AliAlignObj**[nLayers];
+  for (Int_t iLayer = 0; iLayer < (AliGeomManager::kLastLayer - AliGeomManager::kFirstLayer); iLayer++) {
+    fAlignDrawObjs[iLayer] = new AliAlignObj*[AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer)];
+    for (Int_t iModule = 0; iModule < AliGeomManager::LayerSize(iLayer + AliGeomManager::kFirstLayer); iModule++) {
+      UShort_t volid = AliGeomManager::LayerToVolUID(iLayer+ AliGeomManager::kFirstLayer,iModule);
+      fAlignDrawObjs[iLayer][iModule] = new AliAlignObjParams(AliGeomManager::SymName(volid),volid,0,0,0,0,0,0,kTRUE);
+      fAlignDrawObjs[iLayer][iModule]->SetUniqueID(1);
+    }
+  }
+
+
+  TH1F *hX=new TH1F("hX","hX",1000,-10000.,10000.);
+  TH1F *hY=new TH1F("hY","hY",1000,-10000.,10000.);
+  TH1F *hZ=new TH1F("hZ","hZ",1000,-10000.,10000.);
+  TH1F *hPsi=new TH1F("hPsi","hPsi",1000,-5000.,5000.);
+  TH1F *hTheta=new TH1F("hTheta","hTheta",1000,-5000.,5000.);
+  TH1F *hPhi=new TH1F("hPhi","hPhi",1000,-5000.,5000.);
+
+  fCanvPar=new TCanvas("fCanvPar","Parameters trend during iterations: Convergence \n");
+  fCanvPar->Divide(3,2);
+  fCanvPar->cd(1);
+  hX->Draw();
+  hX->SetXTitle("#mum");
+  fCanvPar->cd(2);
+  hY->Draw();
+  hY->SetXTitle("#mum");
+  fCanvPar->cd(3);
+  hZ->SetXTitle("#mum");
+  hZ->Draw();
+  fCanvPar->cd(4);
+  hPsi->SetXTitle("mdeg");
+  hPsi->Draw();
+  fCanvPar->cd(5);
+  hTheta->SetXTitle("mdeg");
+  hTheta->Draw();
+  fCanvPar->cd(6);
+  hPhi->SetXTitle("mdeg");
+  hPhi->Draw();
+  fCanvPar->Update();
+
+  
+  fCanvGr=new TCanvas("fCanvGr","Parameters trend during iterations: Convergence \n");
+  fCanvGr->Divide(3,2);
+
+  fCanvGr->cd(1);
+  fgrIterMeanX=new TGraph(1);
+  fgrIterRMSX=new TGraph(1);
+  fgrIterRMSX->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSX->SetName("fgrIterRMSX");
+  fgrIterRMSX->SetLineColor(2);
+  fgrIterMeanX->SetName("fgrIterMeanX");
+  fgrIterMeanX->SetTitle("Convergence of #deltaX \n");
+  fgrIterMeanX->GetXaxis()->SetTitle("#mum");
+  fgrIterRMSX->Draw("acp");
+  fgrIterMeanX->Draw("cp");
+
+  fCanvGr->cd(2);
+  fgrIterMeanY=new TGraph(1);
+  fgrIterRMSY=new TGraph(1);
+  fgrIterRMSY->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSY->SetName("fgrIterRMSY");
+  fgrIterRMSY->SetLineColor(2);
+  fgrIterMeanY->SetName("fgrIterMeanY");
+  fgrIterMeanY->SetTitle("Convergence of #deltaY \n");
+  fgrIterMeanY->GetXaxis()->SetTitle("#mum");
+  fgrIterRMSY->Draw("acp");
+  fgrIterMeanY->Draw("cp");
+
+  fCanvGr->cd(3);
+  fgrIterMeanZ=new TGraph(1);
+  fgrIterRMSZ=new TGraph(1);
+  fgrIterRMSZ->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSZ->SetName("fgrIterRMSZ");
+  fgrIterRMSZ->SetLineColor(2);
+  fgrIterMeanZ->SetName("fgrIterMeanZ");
+  fgrIterMeanZ->SetTitle("Convergence of #deltaZ \n");
+  fgrIterMeanZ->GetXaxis()->SetTitle("#mum");
+  fgrIterRMSZ->Draw("acp");
+  fgrIterMeanZ->Draw("cp");
+
+  fCanvGr->cd(4);
+  fgrIterMeanPsi=new TGraph(1);
+  fgrIterRMSPsi=new TGraph(1);
+  fgrIterRMSPsi->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSPsi->SetName("fgrIterRMSPsi");
+  fgrIterRMSPsi->SetLineColor(2);
+  fgrIterMeanPsi->SetName("fgrIterMeanPsi");
+  fgrIterMeanPsi->SetTitle("Convergence of #deltaPsi \n");
+  fgrIterMeanPsi->GetXaxis()->SetTitle("mdeg");
+  fgrIterRMSPsi->Draw("acp");
+  fgrIterMeanPsi->Draw("cp");
+
+  fCanvGr->cd(5);
+  fgrIterMeanTheta=new TGraph(1);
+  fgrIterRMSTheta=new TGraph(1);
+  fgrIterRMSTheta->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSTheta->SetName("fgrIterRMSTheta");
+  fgrIterRMSTheta->SetLineColor(2);
+  fgrIterMeanTheta->SetName("fgrIterMeanTheta");
+  fgrIterMeanTheta->SetTitle("Convergence of #deltaTheta \n");
+  fgrIterMeanTheta->GetXaxis()->SetTitle("mdeg");
+  fgrIterRMSTheta->Draw("acp");
+  fgrIterMeanTheta->Draw("cp");
+
+  fCanvGr->cd(6);
+  fgrIterMeanPhi=new TGraph(1);
+  fgrIterRMSPhi=new TGraph(1);
+  fgrIterRMSPhi->GetYaxis()->SetRangeUser(-1000.,1000.);
+  fgrIterRMSPhi->SetName("fgrIterRMSPhi");
+  fgrIterRMSPhi->SetLineColor(2);
+  fgrIterMeanPhi->SetName("fgrIterMeanPhi");
+  fgrIterMeanPhi->SetTitle("Convergence of #deltaPhi \n");
+  fgrIterMeanPhi->GetXaxis()->SetTitle("mdeg");
+  fgrIterRMSPhi->Draw("acp");
+  fgrIterMeanPhi->Draw("cp");
+
+
+  
+}
+
+void AliITSRealignTracks::UpdateDraw(TArrayI *volids,Int_t iter,Int_t color){
+  //Updates the histograms to monitor the results. Only the histograms
+  //of the volumes specified in *volids will be updated.
+  // iter is just a flag for the names of the histo
+  // color specifies the color of the lines of the histograms for this update
+  
+  TString name="hX_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hX=new TH1F("hX",name.Data(),1000,-10000.,10000.);
+
+  name="hY_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hY=new TH1F("hY",name.Data(),1000,-10000.,10000.);
+
+  name="hZ_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hZ=new TH1F("hZ",name.Data(),1000,-10000.,10000.);
+
+  name="hPsi_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hPsi=new TH1F("hPsi",name.Data(),1000,-5000.,5000.);
+  
+  name="hTheta_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hTheta=new TH1F("hTheta",name.Data(),1000,-5000.,5000.);
+
+  name="hPhi_";
+  name+=iter;
+  name.Append("iter");
+  TH1F *hPhi=new TH1F("hPhi",name.Data(),1000,-5000.,5000.);
+  
+  Int_t layer,mod;
+  Double_t transl[3],rot[3],transldr[3],rotdr[3];
+  
+  for(Int_t i=0;i<volids->GetSize();i++){
+    layer=AliGeomManager::VolUIDToLayer(volids->At(i),mod); 
+    fAlignObjs[layer-AliGeomManager::kFirstLayer][mod]->GetPars(transl,rot);
+    fAlignDrawObjs[layer-AliGeomManager::kFirstLayer][mod]->GetPars(transldr,rotdr);
+    
+    hX->Fill(10000.*(transl[0]-transldr[0]));
+    hY->Fill(10000.*(transl[1]-transldr[1]));
+    hZ->Fill(10000.*(transl[2]-transldr[2]));
+    hPsi->Fill(1000.*(rot[0]-rotdr[0]));
+    hTheta->Fill(1000.*(rot[1]-rotdr[1]));
+    hPhi->Fill(1000.*(rot[1]-rotdr[2]));
+    //Update the pars of the draw object
+    fAlignDrawObjs[layer-AliGeomManager::kFirstLayer][mod]->SetPars(transl[0],transl[1],transl[2],rot[0],rot[1],rot[2]);
+  }
+
+  hX->SetLineColor(color);
+  hY->SetLineColor(color);
+  hZ->SetLineColor(color);
+  hPsi->SetLineColor(color);
+  hTheta->SetLineColor(color);
+  hPhi->SetLineColor(color);
+  
+  
+  fCanvPar->cd(1);
+  hX->Draw("Same");
+  fCanvPar->cd(2);
+  hY->Draw("Same");
+  fCanvPar->cd(3);
+  hZ->Draw("Same");
+  fCanvPar->cd(4);
+  hPsi->Draw("Same");
+  fCanvPar->cd(5);
+  hTheta->Draw("Same");
+  fCanvPar->cd(6);
+  hPhi->Draw("Same");
+  gPad->Modified();
+  fCanvPar->Update();
+  fCanvPar->Modified();
+  
+  fgrIterMeanX->SetPoint(fgrIterMeanX->GetN()+1,iter,hX->GetMean());
+  fgrIterRMSX->SetPoint(fgrIterRMSX->GetN()+1,iter,hX->GetRMS());
+  fgrIterMeanY->SetPoint(fgrIterMeanY->GetN()+1,iter,hY->GetMean());
+  fgrIterRMSY->SetPoint(fgrIterRMSY->GetN()+1,iter,hY->GetRMS());
+  fgrIterMeanZ->SetPoint(fgrIterMeanZ->GetN()+1,iter,hZ->GetMean());
+  fgrIterRMSZ->SetPoint(fgrIterRMSZ->GetN()+1,iter,hZ->GetRMS());
+  fgrIterMeanPsi->SetPoint(fgrIterMeanPsi->GetN()+1,iter,hPsi->GetMean());
+  fgrIterRMSPsi->SetPoint(fgrIterRMSPsi->GetN()+1,iter,hPsi->GetRMS());
+  fgrIterMeanTheta->SetPoint(fgrIterMeanTheta->GetN()+1,iter,hTheta->GetMean());
+  fgrIterRMSTheta->SetPoint(fgrIterRMSTheta->GetN()+1,iter,hTheta->GetRMS());
+  fgrIterMeanPhi->SetPoint(fgrIterMeanPhi->GetN()+1,iter,hPhi->GetMean());
+  fgrIterRMSPhi->SetPoint(fgrIterRMSPhi->GetN()+1,iter,hPhi->GetRMS());
+
+  gPad->Modified();
+  fCanvGr->Update();
+  fCanvGr->Update();
+}
+
+void AliITSRealignTracks::WriteHists(const char *outfile){
+  //Writes the histograms for the monitoring of the results
+  // in a file named "outfile"
+
+  TFile *f=new TFile(outfile,"RECREATE");
+  f->cd();
+  fCanvPar->Write();
+  fCanvGr->Write(); 
+  fgrIterMeanX->Write(); 
+  fgrIterRMSX->Write();  
+  fgrIterMeanY->Write(); 
+  fgrIterRMSY->Write();  
+  fgrIterMeanZ->Write(); 
+  fgrIterRMSZ->Write();  
+  fgrIterMeanPsi->Write(); 
+  fgrIterRMSPsi->Write();  
+  fgrIterMeanTheta->Write(); 
+  fgrIterRMSTheta->Write();  
+  fgrIterMeanPhi->Write();  
+  fgrIterRMSPhi->Write();
+
+  f->Close();
+  return;
+  
+}