Update for Mean Vertex calibration in CPass - possibility to re-run the calibration
authorzampolli <zampolli@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 1 Aug 2013 13:57:11 +0000 (13:57 +0000)
committerzampolli <zampolli@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 1 Aug 2013 13:57:11 +0000 (13:57 +0000)
without needing to mask the existing OCDB objects (D. Caffarri).

PWGPP/ITS/AliMeanVertexCalibTask.cxx
PWGPP/ITS/AliMeanVertexPreprocessorOffline.cxx

index d7ce90f..76202f5 100644 (file)
@@ -88,10 +88,16 @@ void AliMeanVertexCalibTask::UserCreateOutputObjects()
   fOutput->Add(hTRKVertexY);
   TH1F* hTRKVertexZ = new TH1F("hTRKVertexZ","TRKVertex z; z vertex [cm]; events",200,-20,20);
   fOutput->Add(hTRKVertexZ);
+
+  TH2F *hTRKVertexXvsMult = new TH2F("hTRKVertexXvsMult", "TRKVertex X vs mult", 200, -1, 1, 300, 0, 3000);
+  fOutput->Add(hTRKVertexXvsMult);
+  
+  TH2F *hTRKVertexYvsMult = new TH2F("hTRKVertexYvsMult", "TRKVertex Y vs mult", 200, -1, 1, 300, 0, 3000);
+  fOutput->Add(hTRKVertexYvsMult);
   
   TH2F *hTRKVertexXZ = new TH2F("hTRKVertexXZ", "TRKVertex XZ corr", 200, -1, 1, 200, -20, 20);
   fOutput->Add(hTRKVertexXZ);
-
+  
   TH2F *hTRKVertexYZ = new TH2F("hTRKVertexYZ", "TRKVertex YZ corr", 200, -1, 1, 200, -20, 20);
   fOutput->Add(hTRKVertexYZ);
   
@@ -100,12 +106,11 @@ void AliMeanVertexCalibTask::UserCreateOutputObjects()
   TH1F* hTRKVertexYdefMult = new TH1F("hTRKVertexYdefMult","TRKVertex y Mult; y vertex [cm] 30<Mult<45; events",500,-1,1);
   fOutput->Add(hTRKVertexYdefMult);
        
-       TH1F* hTRKVertexXHighMult = new TH1F("hTRKVertexXHighMult","TRKVertex x High Mult; x vertex [cm] Mult>1500; events",500,-0.5,0.5);  
-       fOutput->Add(hTRKVertexXHighMult);
-       TH1F* hTRKVertexYHighMult = new TH1F("hTRKVertexYHighMult","TRKVertex y High Mult; y vertex [cm] Mult>1500; events",500,-0.5,0.5);
-       fOutput->Add(hTRKVertexYHighMult);      
-       
-
+  TH1F* hTRKVertexXHighMult = new TH1F("hTRKVertexXHighMult","TRKVertex x High Mult; x vertex [cm] Mult>1500; events",500,-0.5,0.5);  
+  fOutput->Add(hTRKVertexXHighMult);
+  TH1F* hTRKVertexYHighMult = new TH1F("hTRKVertexYHighMult","TRKVertex y High Mult; y vertex [cm] Mult>1500; events",500,-0.5,0.5);
+  fOutput->Add(hTRKVertexYHighMult);   
+  
   TH1F* hITSSAVertexX = new TH1F("hITSSAVertexX","ITSSAVertex x; x vertex [cm]; events",200,-1,1);
   fOutput->Add(hITSSAVertexX);
   TH1F* hITSSAVertexY = new TH1F("hITSSAVertexY","ITSSAVertex y; y vertex [cm]; events",200,-1,1);
@@ -118,20 +123,24 @@ void AliMeanVertexCalibTask::UserCreateOutputObjects()
 
   TH2F *hITSSAVertexYZ = new TH2F("hITSSAVertexYZ", "ITSSAVertex YZ corr", 200, -1, 1, 200, -20, 20);
   fOutput->Add(hITSSAVertexYZ);
+
+  TH2F *hITSSAVertexXvsMult = new TH2F("hITSSAVertexXvsMult", "ITSSAVertex X vs mult", 200, -1, 1, 300, 0, 3000);
+  fOutput->Add(hITSSAVertexXvsMult);
+  
+  TH2F *hITSSAVertexYvsMult = new TH2F("hITSSAVertexYvsMult", "ITSSAVertex Y vs mult", 200, -1, 1, 300, 0, 3000);
+  fOutput->Add(hITSSAVertexYvsMult);
   
   TH1F* hITSSAVertexXdefMult = new TH1F("hITSSAVertexXdefMult","ITSSAVertex x Mult; x vertex [cm] 30<Mult<45; events",500,-1,1);  
   fOutput->Add(hITSSAVertexXdefMult);
   TH1F* hITSSAVertexYdefMult = new TH1F("hITSSAVertexYdefMult","ITSSAVertex y Mult; y vertex [cm] 30<Mult<45; events",500,-1,1);
   fOutput->Add(hITSSAVertexYdefMult);
 
-       
-   TH1F* hITSSAVertexXHighMult = new TH1F("hITSSAVertexXHighMult","ITSSAVertex x High Mult; x vertex [cm] Mult>1500; events",500,-0.5,0.5);  
-   fOutput->Add(hITSSAVertexXHighMult);
-   TH1F* hITSSAVertexYHighMult = new TH1F("hITSSAVertexYHighMult","ITSSAVertex y High Mult; y vertex [cm] Mult>1500; events",500,-0.5,0.5);
-   fOutput->Add(hITSSAVertexYHighMult);
-       
-       
-
+  
+  TH1F* hITSSAVertexXHighMult = new TH1F("hITSSAVertexXHighMult","ITSSAVertex x High Mult; x vertex [cm] Mult>1500; events",500,-0.5,0.5);  
+  fOutput->Add(hITSSAVertexXHighMult);
+  TH1F* hITSSAVertexYHighMult = new TH1F("hITSSAVertexYHighMult","ITSSAVertex y High Mult; y vertex [cm] Mult>1500; events",500,-0.5,0.5);
+  fOutput->Add(hITSSAVertexYHighMult);
+  
   PostData(1, fOutput);
   
   return;
@@ -172,20 +181,26 @@ void AliMeanVertexCalibTask::UserExec(Option_t *)
   AliCDBManager* man = AliCDBManager::Instance();
   //man->SetDefaultStorage("raw://");
   Int_t runNb = esdE->GetRunNumber();
-  man->SetRun(runNb);
-  Printf("runNb = %d", runNb);
+  if (runNb > 0) {
+    man->SetRun(runNb);
+    Printf("runNb = %d", runNb);
+  }
+  
   AliCDBEntry *entry = (AliCDBEntry*)man->Get("GRP/Calib/RecoParam/");
   Printf("entry = %p", entry);
-
-  TObjArray *arrayRecoParam = (TObjArray*)entry->GetObject();
-  Printf("arrayRecoParam = %p", arrayRecoParam);
-  
+  TObjArray *arrayRecoParam=0x0;
+  if (entry) {
+    arrayRecoParam = (TObjArray*)entry->GetObject();
+    Printf("arrayRecoParam = %p", arrayRecoParam);
+  }
+  else { 
+    Printf("CDBEntry not found");
+    return;
+  }
   AliGRPRecoParam *grpRecoParam=0x0;
   if (kLowFlux) grpRecoParam= (AliGRPRecoParam*)arrayRecoParam->At(1);
   else if (kHighFlux) grpRecoParam = (AliGRPRecoParam*)arrayRecoParam->At(2);
   
-  grpRecoParam->Dump();
-
   AliVertexerTracks *vertexer= new AliVertexerTracks(esdE->GetMagneticField());
   vertexer->SetITSMode();
   vertexer->SetConstraintOff();
@@ -229,6 +244,9 @@ void AliMeanVertexCalibTask::UserExec(Option_t *)
       ((TH1F*)fOutput->FindObject("hTRKVertexX"))->Fill(trkv->GetXv());
       ((TH1F*)fOutput->FindObject("hTRKVertexY"))->Fill(trkv->GetYv());
       ((TH1F*)fOutput->FindObject("hTRKVertexZ"))->Fill(trkv->GetZv());
+
+      ((TH2F*)fOutput->FindObject("hTRKVertexXvsMult"))->Fill(trkv->GetXv(), ntrklets);
+      ((TH2F*)fOutput->FindObject("hTRKVertexYvsMult"))->Fill(trkv->GetYv(), ntrklets);
       
       if (ntrklets>30 && ntrklets<45){
        ((TH1F*)fOutput->FindObject("hTRKVertexXdefMult"))->Fill(trkv->GetXv());
@@ -252,6 +270,9 @@ void AliMeanVertexCalibTask::UserExec(Option_t *)
       ((TH1F*)fOutput->FindObject("hITSSAVertexX"))->Fill(itsSAv->GetXv());
       ((TH1F*)fOutput->FindObject("hITSSAVertexY"))->Fill(itsSAv->GetYv());
       ((TH1F*)fOutput->FindObject("hITSSAVertexZ"))->Fill(itsSAv->GetZv());
+
+      ((TH2F*)fOutput->FindObject("hITSSAVertexXvsMult"))->Fill(itsSAv->GetXv(), ntrklets);
+      ((TH2F*)fOutput->FindObject("hITSSAVertexYvsMult"))->Fill(itsSAv->GetYv(), ntrklets);
       
       if (ntrklets>30 && ntrklets<45){
        ((TH1F*)fOutput->FindObject("hITSSAVertexXdefMult"))->Fill(itsSAv->GetXv());
index ff5a8f4..bcdb58d 100644 (file)
@@ -35,6 +35,7 @@
 #include <TObjString.h>
 #include <TNamed.h>
 #include "TClass.h"
+#include <TCanvas.h>
 
 #include "AliESDVertex.h"
 #include "TH1F.h"
@@ -110,550 +111,614 @@ void AliMeanVertexPreprocessorOffline::ProcessOutput(const char *filename, AliCD
        
     if (!list) {
                
-               histTRKvtxX = (TH1F*)file->Get("hTRKVertexX");
-               histTRKvtxY = (TH1F*)file->Get("hTRKVertexY");
-               histTRKvtxZ = (TH1F*)file->Get("hTRKVertexZ");
-               
-               histSPDvtxX = (TH1F*)file->Get("hSPDVertexX");
-               histSPDvtxY = (TH1F*)file->Get("hSPDVertexY");
-               histSPDvtxZ = (TH1F*)file->Get("hSPDVertexZ");
-               
-               if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
-                       
-                       useTRKvtx = kFALSE;
-                       useITSSAvtx = kTRUE;
-                       
-                       histTRKvtxX = (TH1F*)file->FindObject("hITSSAVertexX");
-                       histTRKvtxY = (TH1F*)file->FindObject("hITSSAVertexY");
-                       histTRKvtxZ = (TH1F*)file->FindObject("hITSSAVertexZ");
-               
-                       if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
-                               
-                               useITSSAvtx=kFALSE;
-                               useSPDvtx=kTRUE;
-                               
-                               if (!histSPDvtxX || !histSPDvtxY || !histSPDvtxZ){
-                                       AliError(Form("cannot find any histograms available from file"));
-                                       fStatus=kInputError;
-                                       return;
-                               }       
-                       }
-               }
-       }       
-       
-       else{
-               
-               histTRKvtxX = (TH1F*)list->FindObject("hTRKVertexX");
-               histTRKvtxY = (TH1F*)list->FindObject("hTRKVertexY");
-               histTRKvtxZ = (TH1F*)list->FindObject("hTRKVertexZ");
-               
-               histSPDvtxX = (TH1F*)list->FindObject("hSPDVertexX");
-               histSPDvtxY = (TH1F*)list->FindObject("hSPDVertexY");
-               histSPDvtxZ = (TH1F*)list->FindObject("hSPDVertexZ");
-       
-               if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
-                       
-                       useTRKvtx = kFALSE;
-                       useITSSAvtx = kTRUE;
-                       
-                       histTRKvtxX = (TH1F*)list->FindObject("hITSSAVertexX");
-                       histTRKvtxY = (TH1F*)list->FindObject("hITSSAVertexY");
-                       histTRKvtxZ = (TH1F*)list->FindObject("hITSSAVertexZ");
-                       
-                       if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
-                               
-                               useITSSAvtx=kFALSE;
-                               useSPDvtx=kTRUE;
-                               
-                               if (!histSPDvtxX || !histSPDvtxY || !histSPDvtxZ){
-                                       AliError(Form("cannot find any histograms available from list"));
-                                       fStatus=kInputError;
-                                       return;
-                               }       
-                       }                                                
-                       
-               }
-       }
-       
-                       
-       if (useTRKvtx){
+      histTRKvtxX = (TH1F*)file->Get("hTRKVertexX");
+      histTRKvtxY = (TH1F*)file->Get("hTRKVertexY");
+      histTRKvtxZ = (TH1F*)file->Get("hTRKVertexZ");
+      
+      histSPDvtxX = (TH1F*)file->Get("hSPDVertexX");
+      histSPDvtxY = (TH1F*)file->Get("hSPDVertexY");
+      histSPDvtxZ = (TH1F*)file->Get("hSPDVertexZ");
+      
+      if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
+       
+       useTRKvtx = kFALSE;
+       useITSSAvtx = kTRUE;
+       
+       histTRKvtxX = (TH1F*)file->FindObject("hITSSAVertexX");
+       histTRKvtxY = (TH1F*)file->FindObject("hITSSAVertexY");
+       histTRKvtxZ = (TH1F*)file->FindObject("hITSSAVertexZ");
+       
+       if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
          
-               Float_t nEntriesX = histTRKvtxX->GetEffectiveEntries();                                          
-               Float_t nEntriesY = histTRKvtxY->GetEffectiveEntries();                          
-               Float_t nEntriesZ = histTRKvtxZ->GetEffectiveEntries();                          
-       
-               if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
-                       AliError(Form("TRK vertex histograms have too few entries for fitting"));
-                       useTRKvtx=kFALSE;
-                       useSPDvtx = kTRUE;      
-               }
-       }
-       if (useITSSAvtx){
+         useITSSAvtx=kFALSE;
+         useSPDvtx=kTRUE;
          
-               Float_t nEntriesX = histTRKvtxX->GetEffectiveEntries();                                          
-               Float_t nEntriesY = histTRKvtxY->GetEffectiveEntries();                          
-               Float_t nEntriesZ = histTRKvtxZ->GetEffectiveEntries();                          
-       
-               if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
-                       AliError(Form("ITSSA vertex histograms have too few entries for fitting"));
-                       useITSSAvtx=kFALSE;
-                       useSPDvtx=kTRUE;
-               }
+         if (!histSPDvtxX || !histSPDvtxY || !histSPDvtxZ){
+           AliError(Form("cannot find any histograms available from file"));
+           fStatus=kInputError;
+           return;
+         }     
        }
+      }
+    }  
+    
+    else{
                
-       Float_t nEntriesX = histSPDvtxX->GetEffectiveEntries();                                          
-       Float_t nEntriesY = histSPDvtxY->GetEffectiveEntries();                          
-       Float_t nEntriesZ = histSPDvtxZ->GetEffectiveEntries(); 
-
-       if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
-               spdAvailable = kFALSE;
-               if ((useTRKvtx==kFALSE) && (useITSSAvtx==kFALSE)){
-                       AliError(Form("Also SPD vertex histograms have too few entries for fitting, return"));
-                       fStatus=kLowStatistics;
-                       return;          
-               }
-       }
-       
-       if((nEntriesX == 0.)&&(nEntriesY==0.) && (nEntriesZ>0.)){
-               vertexerSPD3Doff = kTRUE;
-               AliWarning("Vertexer SPD 3D off");
-       }
-       
-       Double_t xMeanVtx=0., yMeanVtx=0., zMeanVtx=0.;
-       Double_t xSigmaVtx=0., ySigmaVtx=0., zSigmaVtx=0.;
-       
-       
-       TF1 *fitVtxX, *fitVtxY, *fitVtxZ;
-       
-       if (useTRKvtx || useITSSAvtx){
-         histTRKvtxX ->Fit("gaus", "M");
-         fitVtxX = histTRKvtxX -> GetFunction("gaus");
-         xMeanVtx = fitVtxX -> GetParameter(1);
-         if (TMath::Abs(xMeanVtx) > 2.) {
-           xMeanVtx = 0.;
-           writeMeanVertexSPD=kTRUE;
-           fStatus=kWriteMeanVertexSPD;
-         }     
+      histTRKvtxX = (TH1F*)list->FindObject("hTRKVertexX");
+      histTRKvtxY = (TH1F*)list->FindObject("hTRKVertexY");
+      histTRKvtxZ = (TH1F*)list->FindObject("hTRKVertexZ");
+      
+      histSPDvtxX = (TH1F*)list->FindObject("hSPDVertexX");
+      histSPDvtxY = (TH1F*)list->FindObject("hSPDVertexY");
+      histSPDvtxZ = (TH1F*)list->FindObject("hSPDVertexZ");
+      
+      if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
+       
+       useTRKvtx = kFALSE;
+       useITSSAvtx = kTRUE;
+       
+       histTRKvtxX = (TH1F*)list->FindObject("hITSSAVertexX");
+       histTRKvtxY = (TH1F*)list->FindObject("hITSSAVertexY");
+       histTRKvtxZ = (TH1F*)list->FindObject("hITSSAVertexZ");
+       
+       if (!histTRKvtxX || !histTRKvtxY || !histTRKvtxZ){
          
-         histTRKvtxY ->Fit("gaus", "M");
-         fitVtxY = histTRKvtxY -> GetFunction("gaus");
-         yMeanVtx = fitVtxY -> GetParameter(1);
-         if (TMath::Abs(yMeanVtx) > 2.) {
-           yMeanVtx = 0.;
-           writeMeanVertexSPD=kTRUE;
-           fStatus=kWriteMeanVertexSPD;
-         }     
+         useITSSAvtx=kFALSE;
+         useSPDvtx=kTRUE;
          
-         histTRKvtxZ ->Fit("gaus", "M", "", -12, 12);
-         fitVtxZ = histTRKvtxZ -> GetFunction("gaus");
-         zMeanVtx = fitVtxZ -> GetParameter(1);
-         zSigmaVtx = fitVtxZ -> GetParameter(2);
-         if ((TMath::Abs(zMeanVtx) > 20.) || (zSigmaVtx>12.)) {
-           zMeanVtx = histTRKvtxZ->GetMean();
-           zSigmaVtx = histTRKvtxZ->GetRMS();
-           writeMeanVertexSPD=kTRUE;
-           fStatus=kWriteMeanVertexSPD;
+         if (!histSPDvtxX || !histSPDvtxY || !histSPDvtxZ){
+           AliError(Form("cannot find any histograms available from list"));
+           fStatus=kInputError;
+           return;
          }     
-         
-       }
-       
-       
-       //check fits: compare histo mean with fit mean value 
-       Double_t xHistoMean, yHistoMean, zHistoMean;
-       Double_t xHistoRMS, yHistoRMS, zHistoRMS;
-       
-       if (useTRKvtx || useITSSAvtx){
-         xHistoMean = histTRKvtxX -> GetMean();        
-         xHistoRMS = histTRKvtxX ->GetRMS();
-         
-         if ((TMath::Abs(xHistoMean-xMeanVtx) > 0.5)){
-           AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
-            useTRKvtx = kFALSE;
-           useITSSAvtx = kFALSE;
-           useSPDvtx = kTRUE;
-           fStatus=kUseOfflineSPDvtx;
-         }
-         
-         yHistoMean = histTRKvtxY ->GetMean(); 
-         yHistoRMS = histTRKvtxY ->GetRMS();
-         
-         if ((TMath::Abs(yHistoMean-yMeanVtx) > 0.5)){
-           AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
-            useTRKvtx = kFALSE;
-           useITSSAvtx = kFALSE;
-           useSPDvtx = kTRUE;
-           fStatus=kUseOfflineSPDvtx;
-         }
-         
-         zHistoMean = histTRKvtxZ -> GetMean();        
-         zHistoRMS = histTRKvtxZ ->GetRMS();
-         
-         if ((TMath::Abs(zHistoMean-zMeanVtx) > 1.)){
-           AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
-            useTRKvtx = kFALSE;
-           useITSSAvtx = kFALSE;
-           useSPDvtx = kTRUE;
-           fStatus=kUseOfflineSPDvtx;
-         }
-       }
+       }                                                
        
+      }
+    }
+    
+    
+    if (useTRKvtx){
+      
+      Float_t nEntriesX = histTRKvtxX->GetEffectiveEntries();                                   
+      Float_t nEntriesY = histTRKvtxY->GetEffectiveEntries();                   
+      Float_t nEntriesZ = histTRKvtxZ->GetEffectiveEntries();                   
+      
+      if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
+       AliError(Form("TRK vertex histograms have too few entries for fitting"));
+       useTRKvtx=kFALSE;
+       useSPDvtx = kTRUE;      
+      }
+    }
+    if (useITSSAvtx){
+      
+      Float_t nEntriesX = histTRKvtxX->GetEffectiveEntries();                                   
+      Float_t nEntriesY = histTRKvtxY->GetEffectiveEntries();                   
+      Float_t nEntriesZ = histTRKvtxZ->GetEffectiveEntries();                   
+      
+      if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
+       AliError(Form("ITSSA vertex histograms have too few entries for fitting"));
+       useITSSAvtx=kFALSE;
+       useSPDvtx=kTRUE;
+      }
+    }
+    
+    Float_t nEntriesX = histSPDvtxX->GetEffectiveEntries();                                     
+    Float_t nEntriesY = histSPDvtxY->GetEffectiveEntries();                     
+    Float_t nEntriesZ = histSPDvtxZ->GetEffectiveEntries(); 
+    
+    if (nEntriesX < 50. || nEntriesY<50. || nEntriesZ<50.) {
+      spdAvailable = kFALSE;
+      if ((useTRKvtx==kFALSE) && (useITSSAvtx==kFALSE)){
+       AliError(Form("Also SPD vertex histograms have too few entries for fitting, return"));
+       fStatus=kLowStatistics;
+                       return;          
+      }
+    }
        
-       if ((useSPDvtx) && (spdAvailable) && (!vertexerSPD3Doff)){
-         
-         histSPDvtxX ->Fit("gaus", "M");
-         fitVtxX = histSPDvtxX -> GetFunction("gaus");
-         xMeanVtx = fitVtxX -> GetParameter(1);
-         if (TMath::Abs(xMeanVtx) > 2.) {
-                       xMeanVtx = 0.;
-                       writeMeanVertexSPD=kTRUE;
-         }
+    if((nEntriesX == 0.)&&(nEntriesY==0.) && (nEntriesZ>0.)){
+      vertexerSPD3Doff = kTRUE;
+      AliWarning("Vertexer SPD 3D off");
+    }
+    
+    Double_t xMeanVtx=0., yMeanVtx=0., zMeanVtx=0.;
+    Double_t xSigmaVtx=0., ySigmaVtx=0., zSigmaVtx=0.;
+    
+    
+    TF1 *fitVtxX, *fitVtxY, *fitVtxZ;
+    
+    if (useTRKvtx || useITSSAvtx){
+      histTRKvtxX ->Fit("gaus", "M");
+      fitVtxX = histTRKvtxX -> GetFunction("gaus");
+      xMeanVtx = fitVtxX -> GetParameter(1);
+      if (TMath::Abs(xMeanVtx) > 2.) {
+       xMeanVtx = 0.;
+       writeMeanVertexSPD=kTRUE;
+       fStatus=kWriteMeanVertexSPD;
+      }        
+      
+      histTRKvtxY ->Fit("gaus", "M");
+      fitVtxY = histTRKvtxY -> GetFunction("gaus");
+      yMeanVtx = fitVtxY -> GetParameter(1);
+      if (TMath::Abs(yMeanVtx) > 2.) {
+       yMeanVtx = 0.;
+       writeMeanVertexSPD=kTRUE;
+       fStatus=kWriteMeanVertexSPD;
+      }        
+      
+      histTRKvtxZ ->Fit("gaus", "M", "", -12, 12);
+      fitVtxZ = histTRKvtxZ -> GetFunction("gaus");
+      zMeanVtx = fitVtxZ -> GetParameter(1);
+      zSigmaVtx = fitVtxZ -> GetParameter(2);
+      if ((TMath::Abs(zMeanVtx) > 20.) || (zSigmaVtx>12.)) {
+       zMeanVtx = histTRKvtxZ->GetMean();
+       zSigmaVtx = histTRKvtxZ->GetRMS();
+       writeMeanVertexSPD=kTRUE;
+       fStatus=kWriteMeanVertexSPD;
+      }        
+      
+    }
+    
+    
+    //check fits: compare histo mean with fit mean value 
+    Double_t xHistoMean, yHistoMean, zHistoMean;
+    Double_t xHistoRMS, yHistoRMS, zHistoRMS;
+    
+    if (useTRKvtx || useITSSAvtx){
+      xHistoMean = histTRKvtxX -> GetMean();   
+      xHistoRMS = histTRKvtxX ->GetRMS();
+      
+      if ((TMath::Abs(xHistoMean-xMeanVtx) > 0.5)){
+       AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
+       useTRKvtx = kFALSE;
+       useITSSAvtx = kFALSE;
+       useSPDvtx = kTRUE;
+       fStatus=kUseOfflineSPDvtx;
+      }
+      
+      yHistoMean = histTRKvtxY ->GetMean();    
+      yHistoRMS = histTRKvtxY ->GetRMS();
+      
+      if ((TMath::Abs(yHistoMean-yMeanVtx) > 0.5)){
+       AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
+       useTRKvtx = kFALSE;
+       useITSSAvtx = kFALSE;
+       useSPDvtx = kTRUE;
+       fStatus=kUseOfflineSPDvtx;
+      }
+      
+      zHistoMean = histTRKvtxZ -> GetMean();   
+      zHistoRMS = histTRKvtxZ ->GetRMS();
+      
+      if ((TMath::Abs(zHistoMean-zMeanVtx) > 1.)){
+       AliWarning(Form("Possible problems with the fit mean very different from histo mean... using SPD vertex"));
+       useTRKvtx = kFALSE;
+       useITSSAvtx = kFALSE;
+       useSPDvtx = kTRUE;
+       fStatus=kUseOfflineSPDvtx;
+      }
+    }
+    
+    
+    if ((useSPDvtx) && (spdAvailable) && (!vertexerSPD3Doff)){
+      
+      histSPDvtxX ->Fit("gaus", "M");
+      fitVtxX = histSPDvtxX -> GetFunction("gaus");
+      xMeanVtx = fitVtxX -> GetParameter(1);
+      if (TMath::Abs(xMeanVtx) > 2.) {
+       xMeanVtx = 0.;
+       writeMeanVertexSPD=kTRUE;
+      }
+      
+      histSPDvtxY ->Fit("gaus", "M");
+      fitVtxY = histSPDvtxY -> GetFunction("gaus");
+      yMeanVtx = fitVtxY -> GetParameter(1);
+      if (TMath::Abs(yMeanVtx) > 2.) {
+       yMeanVtx = 0.;
+       writeMeanVertexSPD=kTRUE;
+      }        
+      
+      histSPDvtxZ ->Fit("gaus", "M", "", -12, 12);
+      fitVtxZ = histSPDvtxZ -> GetFunction("gaus");
+      zMeanVtx = fitVtxZ -> GetParameter(1);
+      zSigmaVtx = fitVtxZ -> GetParameter(2);
+      if ((TMath::Abs(zMeanVtx) > 20.) || (zSigmaVtx>12.)) {
+       zMeanVtx = histSPDvtxZ ->GetMean();
+       zSigmaVtx = histSPDvtxZ->GetRMS();
+       writeMeanVertexSPD = kTRUE;
+      }        
+      
+    }
+    else if ((useSPDvtx) && (!spdAvailable)){
+      AliError(Form("Difference between trkVtx and online one, SPD histos not enough entry or SPD 3D vertex off. Writing Mean Vertex SPD"));
+      writeMeanVertexSPD = kTRUE;      
+    }
+    
+    
+    //check with online position
+    
+    Double_t posOnline[3], sigmaOnline[3];
+    
+    if (useTRKvtx || useITSSAvtx || writeMeanVertexSPD){
+      AliCDBManager *manCheck = AliCDBManager::Instance();
+      manCheck->SetDefaultStorage("raw://");
+      manCheck->SetRun(runNb);
                
-         histSPDvtxY ->Fit("gaus", "M");
-         fitVtxY = histSPDvtxY -> GetFunction("gaus");
-         yMeanVtx = fitVtxY -> GetParameter(1);
-         if (TMath::Abs(yMeanVtx) > 2.) {
-           yMeanVtx = 0.;
-           writeMeanVertexSPD=kTRUE;
-         }     
-         
-         histSPDvtxZ ->Fit("gaus", "M", "", -12, 12);
-         fitVtxZ = histSPDvtxZ -> GetFunction("gaus");
-         zMeanVtx = fitVtxZ -> GetParameter(1);
-         zSigmaVtx = fitVtxZ -> GetParameter(2);
-         if ((TMath::Abs(zMeanVtx) > 20.) || (zSigmaVtx>12.)) {
-           zMeanVtx = histSPDvtxZ ->GetMean();
-           zSigmaVtx = histSPDvtxZ->GetRMS();
-           writeMeanVertexSPD = kTRUE;
-         }     
-         
-       }
-       else if ((useSPDvtx) && (!spdAvailable)){
-         AliError(Form("Difference between trkVtx and online one, SPD histos not enough entry or SPD 3D vertex off. Writing Mean Vertex SPD"));
-         writeMeanVertexSPD = kTRUE;   
-       }
+      AliCDBEntry *entr = manCheck->Get("GRP/Calib/MeanVertexSPD");
+      if(entr) {
+       AliESDVertex *vtxOnline = (AliESDVertex*)entr->GetObject();
        
+       posOnline[0] = vtxOnline->GetX();
+       posOnline[1] = vtxOnline->GetY();
+       posOnline[2] = vtxOnline->GetZ();
        
-       //check with online position
+       sigmaOnline[0] = vtxOnline->GetXRes();
+       sigmaOnline[1] = vtxOnline->GetYRes();
+       sigmaOnline[2] = vtxOnline->GetZRes();
        
-        Double_t posOnline[3], sigmaOnline[3];
+       //vtxOnline->GetSigmaXYZ(sigmaOnline);
        
-       if (useTRKvtx || useITSSAvtx || writeMeanVertexSPD){
-               AliCDBManager *manCheck = AliCDBManager::Instance();
-               manCheck->SetDefaultStorage("raw://");
-               manCheck->SetRun(runNb);
-               
-               AliCDBEntry *entr = manCheck->Get("GRP/Calib/MeanVertexSPD");
-               if(entr) {
-                 AliESDVertex *vtxOnline = (AliESDVertex*)entr->GetObject();
-               
-                 posOnline[0] = vtxOnline->GetX();
-                 posOnline[1] = vtxOnline->GetY();
-                 posOnline[2] = vtxOnline->GetZ();
-               
-                 sigmaOnline[0] = vtxOnline->GetXRes();
-                 sigmaOnline[1] = vtxOnline->GetYRes();
-                 sigmaOnline[2] = vtxOnline->GetZRes();
-               
-                 //vtxOnline->GetSigmaXYZ(sigmaOnline);
-               
-                 if ((TMath::Abs(posOnline[0]-xMeanVtx) > 0.1) || (TMath::Abs(posOnline[1]-yMeanVtx) > 0.1) || (TMath::Abs(posOnline[2]-zMeanVtx) > 1.)){
-                   AliWarning(Form("vertex offline far from the online one"));
-                 }
-               }
+       if ((TMath::Abs(posOnline[0]-xMeanVtx) > 0.1) || (TMath::Abs(posOnline[1]-yMeanVtx) > 0.1) || (TMath::Abs(posOnline[2]-zMeanVtx) > 1.)){
+         AliWarning(Form("vertex offline far from the online one"));
        }
-       
-       
-       
-       if (writeMeanVertexSPD){
-               
-               AliWarning(Form("Writing Mean Vertex SPD, Mean Vertex not available"));
-                                  
-               Double_t sigma[3]={0.0150, 0.0150, zSigmaVtx};
-                        
-               AliESDVertex  *vertex =  new AliESDVertex(posOnline, sigma, "vertex");
-                               
-               AliCDBId id("GRP/Calib/MeanVertex", runNb, runNb);
-               
-               AliCDBMetaData metaData;
-               metaData.SetBeamPeriod(0); //check!!!!
-               metaData.SetResponsible("Davide Caffarri");
-               metaData.SetComment("Mean Vertex object used in reconstruction");
-               
-               if (!db->Put(vertex, id, &metaData)) {
-                 AliError(Form("Error while putting object in storage %s", db->GetURI().Data()));
-               }
-               
-               delete vertex;
-               return; 
+      }
+    }
+    
+    
+    
+    if (writeMeanVertexSPD){
+      
+      AliWarning(Form("Writing Mean Vertex SPD, Mean Vertex not available"));
+      
+      Double_t sigma[3]={0.0150, 0.0150, zSigmaVtx};
+      
+      AliESDVertex  *vertex =  new AliESDVertex(posOnline, sigma, "vertex");
+      
+      AliCDBId id("GRP/Calib/MeanVertex", runNb, runNb);
+      
+      AliCDBMetaData metaData;
+      metaData.SetBeamPeriod(0); //check!!!!
+      metaData.SetResponsible("Davide Caffarri");
+      metaData.SetComment("Mean Vertex object used in reconstruction");
+      
+      if (!db->Put(vertex, id, &metaData)) {
+       AliError(Form("Error while putting object in storage %s", db->GetURI().Data()));
+      }
+      
+      delete vertex;
+      return;  
+    }
+    
+    
+    Float_t meanMult = 40.;
+    Float_t p2 = 1.4;
+    Float_t resolVtx = 0.05;
+    
+    Double_t xSigmaMult, ySigmaMult, corrXZ, corrYZ, lumiRegSquaredX, lumiRegSquaredY;
+    Double_t covarXZ=0., covarYZ=0.;
+    
+    Bool_t highMultEnvironment = kFALSE;
+    Bool_t highMultppEnvironment = kFALSE;
+    Bool_t lowMultppEnvironment = kFALSE;
+    
+    TF1 *sigmaFitX, *sigmaFitY, *corrFit;
+    
+    TH1F *histTRKdefMultX=0;
+    TH1F *histTRKdefMultY=0;
+    TH1F *histTRKHighMultX=0;
+    TH1F *histTRKHighMultY=0;
+    TH2F *histTRKVertexXZ=0;
+    TH2F *histTRKVertexYZ=0;
+    
+    TH2F *histTRKvsMultX=0x0;
+    TH2F *histTRKvsMultY=0x0;
+    
+    if (useTRKvtx){
+      if (list){
+       histTRKdefMultX = (TH1F*)list->FindObject("hTRKVertexXdefMult");
+       histTRKdefMultY = (TH1F*)list->FindObject("hTRKVertexYdefMult");
+       histTRKHighMultX = (TH1F*)list->FindObject("hTRKVertexXHighMult");
+       histTRKHighMultY = (TH1F*)list->FindObject("hTRKVertexYHighMult");
+       
+       histTRKvsMultX = (TH2F*)list->FindObject("hTRKVertexXvsMult");
+       histTRKvsMultY = (TH2F*)list->FindObject("hTRKVertexYvsMult");
+       
+       histTRKVertexXZ = (TH2F*)list->FindObject("hTRKVertexXZ");
+       histTRKVertexYZ = (TH2F*)list->FindObject("hTRKVertexYZ");
+      }
+      
+      else {
+       histTRKdefMultX = (TH1F*)file->Get("hTRKVertexXdefMult");
+       histTRKdefMultY = (TH1F*)file->Get("hTRKVertexYdefMult");
+       histTRKHighMultX = (TH1F*)file->Get("hTRKVertexXHighMult");
+       histTRKHighMultY = (TH1F*)file->Get("hTRKVertexYHighMult");
+       
+       histTRKvsMultX = (TH2F*)file->FindObject("hTRKVertexXvsMult");
+       histTRKvsMultY = (TH2F*)file->FindObject("hTRKVertexYvsMult");
+           
+       histTRKVertexXZ = (TH2F*)file->Get("hTRKVertexXZ");
+       histTRKVertexYZ = (TH2F*)file->Get("hTRKVertexYZ");
+      }
+      
+    }  
+    
+    if (useITSSAvtx){
+      if (list){
+       histTRKdefMultX = (TH1F*)list->FindObject("hITSSAVertexXdefMult");
+       histTRKdefMultY = (TH1F*)list->FindObject("hITSSAVertexYdefMult");
+       histTRKHighMultX = (TH1F*)list->FindObject("hITSSAVertexXHighMult");
+       histTRKHighMultY = (TH1F*)list->FindObject("hITSSAVertexYHighMult");
+       
+       histTRKvsMultX = (TH2F*)file->FindObject("hITSSAVertexXvsMult");
+       histTRKvsMultY = (TH2F*)file->FindObject("hITSSAVertexYvsMult");
+       
+       histTRKVertexXZ = (TH2F*)list->FindObject("hITSSAVertexXZ");
+       histTRKVertexYZ = (TH2F*)list->FindObject("hITSSAVertexYZ");
+      }
+      
+      else {
+       histTRKdefMultX = (TH1F*)file->Get("hITSSAVertexXdefMult");
+       histTRKdefMultY = (TH1F*)file->Get("hITSSAVertexYdefMult");
+       histTRKHighMultX = (TH1F*)file->Get("hITSSAVertexXHighMult");
+       histTRKHighMultY = (TH1F*)file->Get("hITSSAVertexYHighMult");
+       
+       histTRKvsMultX = (TH2F*)file->FindObject("hITSSAVertexXvsMult");
+       histTRKvsMultY = (TH2F*)file->FindObject("hITSSAVertexYvsMult");
+       
+       histTRKVertexXZ = (TH2F*)file->Get("hITSSAVertexXZ");
+       histTRKVertexYZ = (TH2F*)file->Get("hITSSAVertexYZ");
+      }
+    }
+    
+    
+    TH1D *projXvsMult;
+    TH1D *projYvsMult;
+    
+    Float_t nEntriesMultX=0, nEntriesMultY=0.;
+
+    if ((histTRKHighMultX) && (histTRKHighMultY)){
+    
+      projXvsMult = (TH1D*)histTRKvsMultX->ProjectionX("projXHighMultPbPb", 150, 300);
+      projYvsMult = (TH1D*)histTRKvsMultY->ProjectionX("projYHighMultPbPb", 150, 300);
+      
+      nEntriesMultX = projXvsMult->GetEffectiveEntries();
+      nEntriesMultY = projYvsMult->GetEffectiveEntries();
+      
+      if ((nEntriesMultX >100) && (nEntriesMultY>100)) {
+       AliWarning(Form("Setting High Mulitplicity environment"));      
+       highMultEnvironment = kTRUE;
+      }
+      else {
+       
+       projXvsMult = (TH1D*)histTRKvsMultX->ProjectionX("projXHighMultPbPb", 10, 30);
+       projYvsMult = (TH1D*)histTRKvsMultY->ProjectionX("projYHighMultPbPb", 10, 30);
+       
+       nEntriesMultX = projXvsMult->GetEffectiveEntries();
+       nEntriesMultY = projYvsMult->GetEffectiveEntries();
+       
+       if ((nEntriesMultX >100) && (nEntriesMultY>100)) {
+         AliWarning(Form("Setting high pp Mulitplicity environment or p-A high multiplicity"));
+         highMultppEnvironment=kTRUE;                  
        }
-       
-       
-       Float_t meanMult = 40.;
-       Float_t p2 = 1.4;
-       Float_t resolVtx = 0.05;
-       
-       Double_t xSigmaMult, ySigmaMult, corrXZ, corrYZ, lumiRegSquaredX, lumiRegSquaredY;
-       Double_t covarXZ=0., covarYZ=0.;
-       
-       Bool_t highMultEnvironment = kTRUE;
-       
-       TF1 *sigmaFitX, *sigmaFitY, *corrFit;
-               
-       TH1F *histTRKdefMultX=0;
-       TH1F *histTRKdefMultY=0;
-       TH1F *histTRKHighMultX=0;
-       TH1F *histTRKHighMultY=0;
-       TH2F *histTRKVertexXZ=0;
-       TH2F *histTRKVertexYZ=0;
-       
-       
-       if (useTRKvtx){
-         if (list){
-                 histTRKdefMultX = (TH1F*)list->FindObject("hTRKVertexXdefMult");
-                 histTRKdefMultY = (TH1F*)list->FindObject("hTRKVertexYdefMult");
-                 histTRKHighMultX = (TH1F*)list->FindObject("hTRKVertexXHighMult");
-                 histTRKHighMultY = (TH1F*)list->FindObject("hTRKVertexYHighMult");
-                 histTRKVertexXZ = (TH2F*)list->FindObject("hTRKVertexXZ");
-                 histTRKVertexYZ = (TH2F*)list->FindObject("hTRKVertexYZ");
-         }
-               
-         else {
-                 histTRKdefMultX = (TH1F*)file->Get("hTRKVertexXdefMult");
-                 histTRKdefMultY = (TH1F*)file->Get("hTRKVertexYdefMult");
-                 histTRKHighMultX = (TH1F*)file->Get("hTRKVertexXHighMult");
-                 histTRKHighMultY = (TH1F*)file->Get("hTRKVertexYHighMult");
-                 histTRKVertexXZ = (TH2F*)file->Get("hTRKVertexXZ");
-                 histTRKVertexYZ = (TH2F*)file->Get("hTRKVertexYZ");
-         }
-               
-       }       
-       
-       if (useITSSAvtx){
-         if (list){
-                 histTRKdefMultX = (TH1F*)list->FindObject("hITSSAVertexXdefMult");
-                 histTRKdefMultY = (TH1F*)list->FindObject("hITSSAVertexYdefMult");
-                 histTRKHighMultX = (TH1F*)list->FindObject("hITSSAVertexXHighMult");
-                 histTRKHighMultY = (TH1F*)list->FindObject("hITSSAVertexYHighMult");
-                 histTRKVertexXZ = (TH2F*)list->FindObject("hITSSAVertexXZ");
-                 histTRKVertexYZ = (TH2F*)list->FindObject("hITSSAVertexYZ");
-         }
+       else {
          
-         else {
-                 histTRKdefMultX = (TH1F*)file->Get("hITSSAVertexXdefMult");
-                 histTRKdefMultY = (TH1F*)file->Get("hITSSAVertexYdefMult");
-                 histTRKHighMultX = (TH1F*)file->Get("hITSSAVertexXHighMult");
-                 histTRKHighMultY = (TH1F*)file->Get("hITSSAVertexYHighMult");
-                 histTRKVertexXZ = (TH2F*)file->Get("hITSSAVertexXZ");
-                 histTRKVertexYZ = (TH2F*)file->Get("hITSSAVertexYZ");
+         projXvsMult = (TH1D*)histTRKvsMultX->ProjectionX("projXHighMultPbPb", 3, 5);
+         projYvsMult = (TH1D*)histTRKvsMultY->ProjectionX("projYHighMultPbPb", 3, 5);
+         
+         nEntriesMultX = projXvsMult->GetEffectiveEntries();
+         nEntriesMultY = projYvsMult->GetEffectiveEntries();
+         
+         if ((nEntriesMultX >100) && (nEntriesMultY>100)) {
+           AliWarning(Form("Setting low pp Mulitplicity environment"));
+           lowMultppEnvironment=kTRUE;                 
          }
-       }
-               
        
-       if ((histTRKHighMultX) && (histTRKHighMultY)){
-               
-               Float_t nEntriesHighMultX = histTRKHighMultX->GetEffectiveEntries();
-               Float_t nEntriesHighMultY = histTRKHighMultY->GetEffectiveEntries();
-               
-               if ((nEntriesHighMultX >100) && (nEntriesHighMultY>100)) {
-                       AliWarning(Form("Setting High Mulitplicity environment"));      
-                       highMultEnvironment = kTRUE;
-               }
-               else {
-                 AliWarning(Form("Setting Low Mulitplicity environment"));
-                 highMultEnvironment=kFALSE;                   
-               }
        }
        
-       else{
-         AliWarning(Form("No histos found, setting Low Mulitplicity environment"));
-         highMultEnvironment=kFALSE;
-         
-       }
+      }
+    
+       
+      if (lowMultppEnvironment==kTRUE) {
        
-       if (highMultEnvironment==kFALSE){
+       if ((projXvsMult->GetEntries() < 40.) || (projYvsMult->GetEntries() < 40.)){
+         AliWarning(Form("histos for lumi reg calculation not found, default value set"));
+         xSigmaVtx=0.0120;
+         ySigmaVtx=0.0120;
+         fStatus=kLumiRegCovMatrixProblem;
+       } else {
          
-         if ((!histTRKdefMultX) || (!histTRKdefMultY) || (histTRKdefMultX->GetEntries() < 40.) || (histTRKdefMultY->GetEntries() < 40.)){
-           AliWarning(Form("histos for lumi reg calculation not found, default value setted"));
-           xSigmaVtx=0.0120;
-           ySigmaVtx=0.0120;
+         projXvsMult -> Fit("gaus", "M", "", -0.4, 0.4);
+         sigmaFitX = projXvsMult -> GetFunction("gaus");
+         xSigmaMult = sigmaFitX->GetParameter(2);
+         
+         lumiRegSquaredX = (xSigmaMult*xSigmaMult - ((resolVtx*resolVtx)/TMath::Power(meanMult, p2)));
+         if (lumiRegSquaredX < 0 || lumiRegSquaredX < 1E-5) {
+           AliWarning(Form("Difficult luminous region determination X, keep convoluted sigma"));
+           xSigmaVtx = xSigmaMult;
            fStatus=kLumiRegCovMatrixProblem;
-         } else {
-           
-           histTRKdefMultX -> Fit("gaus", "M", "", -0.4, 0.4);
-           sigmaFitX = histTRKdefMultX -> GetFunction("gaus");
-           xSigmaMult = sigmaFitX->GetParameter(2);
-           
-           lumiRegSquaredX = (xSigmaMult*xSigmaMult - ((resolVtx*resolVtx)/TMath::Power(meanMult, p2)));
-           if (lumiRegSquaredX < 0) {
-             AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
-             xSigmaMult = 0.;
-             xSigmaVtx = 0.0120;
-             fStatus=kLumiRegCovMatrixProblem;
-           }
-           
+         }
+         
+         else {
            if (lumiRegSquaredX > 0 && lumiRegSquaredX < 0.0005){
              xSigmaVtx = TMath::Sqrt(lumiRegSquaredX);
-             xSigmaVtx = xSigmaVtx*1.2;
-           }
-           
-           histTRKdefMultY -> Fit("gaus", "M", "", -0.2, 0.6);
-           sigmaFitY = histTRKdefMultY -> GetFunction("gaus");
-           ySigmaMult = sigmaFitY->GetParameter(2);
-           
-           lumiRegSquaredY= (ySigmaMult*ySigmaMult - ((resolVtx*resolVtx)/TMath::Power(meanMult, p2)));
-           if (lumiRegSquaredY < 0) {
-             AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
-             ySigmaMult = 0.;
-             ySigmaVtx = 0.0120;
-             fStatus=kLumiRegCovMatrixProblem;
-           }
-           
-           if (lumiRegSquaredY > 0 && lumiRegSquaredY < 0.0005){
-             ySigmaVtx = TMath::Sqrt(lumiRegSquaredY);
-             ySigmaVtx = ySigmaVtx*1.2;
-           }
-           
-           TProfile *htrkXZ = histTRKVertexXZ ->ProfileY();
-           htrkXZ -> Fit("pol1", "M", "", -10., 10.);
-           corrFit = htrkXZ->GetFunction("pol1");
-           corrXZ = corrFit->GetParameter(1);
-           
-           if (TMath::Abs(corrXZ) > 0.01) {
-             AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
-             corrXZ =0.;
-             fStatus=kLumiRegCovMatrixProblem;
-           }
-           else{
-             covarXZ = corrXZ * zSigmaVtx*zSigmaVtx;
-             
+             xSigmaVtx = xSigmaVtx*1.1;
            }
            
-           TProfile *htrkYZ = histTRKVertexYZ ->ProfileY();
-           htrkYZ -> Fit("pol1", "M", "", -10., 10.);
-           corrFit = htrkYZ->GetFunction("pol1");
-           corrYZ = corrFit->GetParameter(1);
-                       
-           if (TMath::Abs(corrYZ) > 0.01) {
-             AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
-             corrYZ =0.;
+           else{ 
+             AliWarning(Form("Not possible to define a luminous region X. Default values set"));
+             xSigmaVtx = 0.0120;
              fStatus=kLumiRegCovMatrixProblem;
            }
-           else{
-             covarYZ = corrYZ*zSigmaVtx*zSigmaVtx;
-           }
-           
-         }
-       }
-       
-       if (highMultEnvironment==kTRUE){
-         
-         histTRKHighMultX -> Fit("gaus", "M", "", -0.4, 0.4);
-         sigmaFitX = histTRKHighMultX -> GetFunction("gaus");
-         xSigmaMult = sigmaFitX->GetParameter(2);
-         
-         if ((xSigmaMult <0) || (xSigmaMult>0.03)){
-           AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
-           xSigmaMult = 0.;
-           xSigmaVtx = 0.0120;
-           fStatus=kLumiRegCovMatrixProblem;
-         }
-         else{
-           xSigmaVtx = xSigmaMult;
-           xSigmaVtx = xSigmaVtx*1.2;
          }
          
-         histTRKHighMultY -> Fit("gaus", "M", "", -0.2, 0.5);
-         sigmaFitY = histTRKHighMultY -> GetFunction("gaus");
+         projYvsMult -> Fit("gaus", "M", "", -0.2, 0.6);
+         sigmaFitY = projYvsMult -> GetFunction("gaus");
          ySigmaMult = sigmaFitY->GetParameter(2);
          
-         if ((ySigmaMult <0) || (ySigmaMult>0.03)){
-           AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
-           ySigmaMult = 0.;
-           ySigmaVtx = 0.0120;
-           fStatus=kLumiRegCovMatrixProblem;
-         }
-         else{
-           ySigmaVtx = ySigmaMult;
-           ySigmaVtx = ySigmaVtx*1.2;
-         }
-         
-         TProfile *htrkXZ = histTRKVertexXZ ->ProfileY();
-         htrkXZ -> Fit("pol1", "M", "", -10., 10.);
-         corrFit = htrkXZ->GetFunction("pol1");
-         corrXZ = corrFit->GetParameter(1);
+         lumiRegSquaredY= (ySigmaMult*ySigmaMult - ((resolVtx*resolVtx)/TMath::Power(meanMult, p2)));
          
-         if (TMath::Abs(corrXZ) > 0.01) {
-           AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
-           corrXZ =0.;
+         if (lumiRegSquaredY < 0 || lumiRegSquaredY < 1E-5) {
+           AliWarning(Form("Difficult luminous region determination Y, keep convoluted sigma"));
+           ySigmaVtx = ySigmaMult;
            fStatus=kLumiRegCovMatrixProblem;
          }
-         else{
-           covarXZ = corrXZ * zSigmaVtx*zSigmaVtx;
-           
-         }
          
-         TProfile *htrkYZ = histTRKVertexYZ ->ProfileY();
-         htrkYZ -> Fit("pol1", "M", "", -10., 10.);
-         corrFit = htrkYZ->GetFunction("pol1");
-         corrYZ = corrFit->GetParameter(1);
-         
-         if (TMath::Abs(corrYZ) > 0.01) {
-           AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
-           corrYZ =0.;
-           fStatus=kLumiRegCovMatrixProblem;
-         }
-         else{
-           covarYZ = corrYZ*zSigmaVtx*zSigmaVtx;
+         else{ 
+           if (lumiRegSquaredY > 0 && lumiRegSquaredY < 0.0005){
+             ySigmaVtx = TMath::Sqrt(lumiRegSquaredY);
+             ySigmaVtx = ySigmaVtx*1.1;
+           }
+           else{ 
+             AliWarning(Form("Not possible to define a luminous region Y. Default values set"));
+             ySigmaVtx = 0.0120;
+             fStatus=kLumiRegCovMatrixProblem;
+           }
          }
-         
        }
        
-       Double_t position[3], covMatrix[6];
-       Double_t chi2=1.; 
-       Int_t nContr=1; 
-       
-       position[0] = xMeanVtx;
-       position[1] = yMeanVtx;
-       position[2] = zMeanVtx;
-       
-       covMatrix[0] = xSigmaVtx*xSigmaVtx;
-       covMatrix[1] = 0.; //xy
-       covMatrix[2] = ySigmaVtx*ySigmaVtx;
-       covMatrix[3] = covarXZ;
-       covMatrix[4] = covarYZ;
-       covMatrix[5] = zSigmaVtx*zSigmaVtx;
-       
-       
-       //Printf ("%f, %f, %f, %f", xSigmaVtx, ySigmaVtx, covarXZ, covarYZ);
-       
-       AliESDVertex  *vertex =  new AliESDVertex(position, covMatrix, chi2, nContr, "vertex");
+       TProfile *htrkXZ = histTRKVertexXZ ->ProfileY();
+       htrkXZ -> Fit("pol1", "M", "", -10., 10.);
+       corrFit = htrkXZ->GetFunction("pol1");
+       corrXZ = corrFit->GetParameter(1);
        
-       AliCDBId id("GRP/Calib/MeanVertex", runNb, runNb);
-       
-       AliCDBMetaData metaData;
-       metaData.SetBeamPeriod(0); //check!!!!
-       metaData.SetResponsible("Davide Caffarri");
-       metaData.SetComment("Mean Vertex object used in reconstruction");
-       
-       if (!db->Put(vertex, id, &metaData)) {
-         AliError(Form("Error while putting object in storage %s", db->GetURI().Data()));
-         fStatus=kStoreError;
+       if (TMath::Abs(corrXZ) > 0.01) {
+         AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
+         corrXZ =0.;
+         fStatus=kLumiRegCovMatrixProblem;
+       }
+       else{
+         covarXZ = corrXZ * zSigmaVtx*zSigmaVtx;
+         
        }
        
-       delete vertex;
+       TProfile *htrkYZ = histTRKVertexYZ ->ProfileY();
+       htrkYZ -> Fit("pol1", "M", "", -10., 10.);
+       corrFit = htrkYZ->GetFunction("pol1");
+       corrYZ = corrFit->GetParameter(1);
        
-               Int_t status=GetStatus();
-       if (status == 0) {
-         AliInfo(Form("MeanVertex calibration successful: %s (status=%d)", fgkStatusCodeName[fStatus], status));
-       }
-       else if (status > 0) {
-         AliInfo(Form("MeanVertex calibration failed: %s (status=%d)", fgkStatusCodeName[fStatus], status));
+       if (TMath::Abs(corrYZ) > 0.01) {
+         AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
+         corrYZ =0.;
+         fStatus=kLumiRegCovMatrixProblem;
        }
-       else if (status < 0) {
-         AliInfo(Form("MeanVertex calibration but not fatal error: %s (status=%d)", fgkStatusCodeName[fStatus], status));
+       else{
+         covarYZ = corrYZ*zSigmaVtx*zSigmaVtx;
        }
        
-       
+      }
+    }
+    
+     if (highMultEnvironment==kTRUE || highMultppEnvironment==kTRUE){
+    
+    projXvsMult -> Fit("gaus", "M", "", -0.4, 0.4);
+    sigmaFitX = projXvsMult -> GetFunction("gaus");
+    xSigmaMult = sigmaFitX->GetParameter(2);
+         
+      if ((xSigmaMult <0) || (xSigmaMult>0.03)){
+       AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
+       xSigmaMult = 0.;
+       xSigmaVtx = 0.0120;
+       fStatus=kLumiRegCovMatrixProblem;
+      }
+      else{
+       xSigmaVtx = xSigmaMult;
+       xSigmaVtx = xSigmaVtx*1.1;
+      }
+         
+      projYvsMult -> Fit("gaus", "M", "", -0.2, 0.5);
+      TCanvas *c = new TCanvas("nwC", "nwC");
+      c->cd();
+      projYvsMult->Draw();
+      sigmaFitY = projYvsMult -> GetFunction("gaus");
+      ySigmaMult = sigmaFitY->GetParameter(2);
+         
+      if ((ySigmaMult <0) || (ySigmaMult>0.03)){
+       AliWarning(Form("Problems with luminosiy region determination, update of the postion only"));
+       ySigmaMult = 0.;
+       ySigmaVtx = 0.0120;
+       fStatus=kLumiRegCovMatrixProblem;
+      }
+      else{
+       ySigmaVtx = ySigmaMult;
+       ySigmaVtx = ySigmaVtx*1.1;
+      }
+         
+      TProfile *htrkXZ = histTRKVertexXZ ->ProfileY();
+      htrkXZ -> Fit("pol1", "M", "", -10., 10.);
+      corrFit = htrkXZ->GetFunction("pol1");
+      corrXZ = corrFit->GetParameter(1);
+         
+      if (TMath::Abs(corrXZ) > 0.01) {
+       AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
+       corrXZ =0.;
+       fStatus=kLumiRegCovMatrixProblem;
+      }
+      else{
+       covarXZ = corrXZ * zSigmaVtx*zSigmaVtx;
+           
+      }
+         
+      TProfile *htrkYZ = histTRKVertexYZ ->ProfileY();
+      htrkYZ -> Fit("pol1", "M", "", -10., 10.);
+      corrFit = htrkYZ->GetFunction("pol1");
+      corrYZ = corrFit->GetParameter(1);
+         
+      if (TMath::Abs(corrYZ) > 0.01) {
+       AliWarning(Form("Problems in the correlation fitting, not update the covariance matrix"));
+       corrYZ =0.;
+       fStatus=kLumiRegCovMatrixProblem;
+      }
+      else{
+       covarYZ = corrYZ*zSigmaVtx*zSigmaVtx;
+      }
+         
+     }
+       
+     Double_t position[3], covMatrix[6];
+     Double_t chi2=1.; 
+     Int_t nContr=1;   
+     
+     position[0] = xMeanVtx;
+     position[1] = yMeanVtx;
+     position[2] = zMeanVtx;
+     
+     covMatrix[0] = xSigmaVtx*xSigmaVtx;
+     covMatrix[1] = 0.; //xy
+     covMatrix[2] = ySigmaVtx*ySigmaVtx;
+     covMatrix[3] = covarXZ;
+     covMatrix[4] = covarYZ;
+     covMatrix[5] = zSigmaVtx*zSigmaVtx;
+     
+     
+     //Printf ("%f, %f, %f, %f", xSigmaVtx, ySigmaVtx, covarXZ, covarYZ);
+     
+     AliESDVertex  *vertex =  new AliESDVertex(position, covMatrix, chi2, nContr, "vertex");
+     
+     AliCDBId id("GRP/Calib/MeanVertex", runNb, runNb);
+     
+     AliCDBMetaData metaData;
+     metaData.SetBeamPeriod(0); //check!!!!
+     metaData.SetResponsible("Davide Caffarri");
+     metaData.SetComment("Mean Vertex object used in reconstruction");
+     
+     if (!db->Put(vertex, id, &metaData)) {
+       AliError(Form("Error while putting object in storage %s", db->GetURI().Data()));
+       fStatus=kStoreError;
+     }
+     
+     delete vertex;
+     
+     Int_t status=GetStatus();
+     if (status == 0) {
+       AliInfo(Form("MeanVertex calibration successful: %s (status=%d)", fgkStatusCodeName[fStatus], status));
+     }
+     else if (status > 0) {
+       AliInfo(Form("MeanVertex calibration failed: %s (status=%d)", fgkStatusCodeName[fStatus], status));
+     }
+     else if (status < 0) {
+       AliInfo(Form("MeanVertex calibration but not fatal error: %s (status=%d)", fgkStatusCodeName[fStatus], status));
+     }
+     
+     
 }
 
 //__________________________________________________________________________