]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliGRPPreprocessor.cxx
Storing the timestamp of the second measurement in case the Machine Mode or the
[u/mrichter/AliRoot.git] / STEER / AliGRPPreprocessor.cxx
index 33f4cfc581d476cd9012b6c168b1750930d99e95..bd4563674276ad76aab8c5c52790bff47814450a 100644 (file)
 #include <TList.h>
 #include <TMap.h>
 #include <TObjString.h>
+#include <TObjArray.h>
 #include <TGraph.h>
+#include <TString.h>
+#include <TFile.h>
 
 #include <float.h>
 
 
 #include "AliCDBMetaData.h"
 #include "AliLog.h"
+#include "AliESDVertex.h"
+#include "AliLHCReader.h"
+#include "AliLHCData.h"
+#include "AliDCSArray.h"
 
 class AliDCSValue;
 class AliShuttleInterface;
@@ -56,8 +63,9 @@ class AliShuttleInterface;
 #include <AliCDBMetaData.h>
 #include <AliCDBId.h>
 #include <AliTriggerConfiguration.h>
+#include <AliCTPTimeParams.h>
 
-const Double_t kFitFraction = 0.7;                 // Fraction of DCS sensor fits required
+const Double_t kFitFraction = -1.;                 // Fraction of DCS sensor fits required
 
 ClassImp(AliGRPPreprocessor)
 
@@ -65,14 +73,14 @@ ClassImp(AliGRPPreprocessor)
 
   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 8; // num parameters in the logbook for PHYSICS runs, when beamType from DAQ logbook == NULL
   const Int_t AliGRPPreprocessor::fgknDAQLbParReduced = 7; // num parameters in the logbook for the other cases
-  const Int_t AliGRPPreprocessor::fgknDCSDP = 50;   // number of dcs dps
+  const Int_t AliGRPPreprocessor::fgknDCSDP = 48;   // number of dcs dps
   const Int_t AliGRPPreprocessor::fgknDCSDPHallProbes = 40;   // number of dcs dps
+  const Int_t AliGRPPreprocessor::fgknLHCDP = 5;   // number of dcs dps from LHC data
+  const Int_t AliGRPPreprocessor::fgkDCSDPHallTopShift = 4;   // shift from the top to get tp the Hall Probes names in the list of DCS DPs
+  const Int_t AliGRPPreprocessor::fgkDCSDPNonWorking = 5; // number of non working DCS DPs
   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
-                   "LHCState",              // missing in DCS
                    "L3Polarity",
                    "DipolePolarity",
-                   "LHCLuminosity",         // missing in DCS
-                   "BeamIntensity",         // missing in DCS
                    "L3Current",
                    "DipoleCurrent",
                   "L3_BSF17_H1",
@@ -117,7 +125,8 @@ ClassImp(AliGRPPreprocessor)
                   "Dipole_Outside_Temperature",
                    "CavernTemperature",
                    "CavernAtmosPressure",
-                   "SurfaceAtmosPressure"
+                   "SurfaceAtmosPressure",
+                   "CavernAtmosPressure2"
                  };
 
   const char* AliGRPPreprocessor::fgkDCSDataPointsHallProbes[AliGRPPreprocessor::fgknDCSDPHallProbes] = {
@@ -160,25 +169,20 @@ ClassImp(AliGRPPreprocessor)
                   "Dipole_Outside_H1",
                   "Dipole_Outside_H2",
                   "Dipole_Outside_H3",
-                  "Dipole_Outside_Temperature",
+                  "Dipole_Outside_Temperature"
                  };
                  
-  const Short_t kSensors = 48; // start index position of sensor in DCS DPs
-  const Short_t kNumSensors = 2; // Number of sensors in DCS DPs
-
-  const char* AliGRPPreprocessor::fgkLHCState[20] = {
-                   "P", "PREPARE",
-                   "J", "PREINJECTION",
-                   "I", "INJECTION",
-                   "F", "FILLING",
-                   "A", "ADJUST",
-                   "U", "UNSTABLE BEAMS",
-                   "S", "STABLE BEAMS",
-                   "D", "BEAM DUMP",
-                   "R", "RECOVER",
-                   "C", "PRECYCLE"
-                 };
+  const Short_t kSensors = 45; // start index position of sensor in DCS DPs
+  const Short_t kNumSensors = 3; // Number of sensors in DCS DPs (CavernAtmosPressure, SurfaceAtmosPressure, CavernAtmosPressure2)
 
+
+  const char* AliGRPPreprocessor::fgkLHCDataPoints[AliGRPPreprocessor::fgknLHCDP] = {
+         "lhcMon_LHCBeam.Energy",
+         "lhcMon_LHCMachineMode.value",
+         "lhcMon_LHCBeamMode.value",
+          "dip/acc/LHC/RunControl/BeamType/Beam1.payload",
+          "dip/acc/LHC/RunControl/BeamType/Beam2.payload"
+  };
   const char* kppError[] = {
                    "",
                    "(DAQ logbook ERROR)",
@@ -186,13 +190,17 @@ ClassImp(AliGRPPreprocessor)
                    "(Trigger Scalers not found in DCS FXS - ERROR)",
                    "(DCS data points ERROR)",
                    "(Trigger Configuration ERROR)",
-                   "(DAQ logbook ERROR determining partition of the run)"
+                   "(DAQ logbook ERROR determining partition of the run)",
+                   "(CTP timing ERROR)",
+                  "(SPD Mean Vertex ERROR)",
+                  "(DCS FXS Error for LHC Data)",
+                  "(LHC Data Error)"
   };
 
 //_______________________________________________________________
 
 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
-       AliPreprocessor("GRP",shuttle),  fPressure(0), fmaxFloat(0), fminFloat(0),fmaxDouble(0), fminDouble(0), fmaxInt(0), fminInt(0), fmaxUInt(0), fminUInt(0)
+       AliPreprocessor("GRP",shuttle),  fPressure(0), fmaxFloat(0), fminFloat(0),fmaxDouble(0), fminDouble(0), fmaxInt(0), fminInt(0), fmaxUInt(0), fminUInt(0),fdaqStartEndTimeOk(kTRUE),ffailedDPs(new TObjArray(fgknDCSDP))
 {
        // constructor - shuttle must be instantiated!
 
@@ -211,6 +219,7 @@ AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
        AddRunType("NOISE");
        AddRunType("PULSER");
         AddRunType("STANDALONE_PULSER");
+        AddRunType("STANDALONE_BC");
 
        fmaxFloat = FLT_MAX;
        fminFloat = -FLT_MAX;
@@ -230,6 +239,7 @@ AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
        AliInfo(Form("Max allowed unsigned integer = %u",(Int_t)fmaxUInt));
        AliInfo(Form("Min allowed unsigned integer = %u",(Int_t)fminUInt));
 
+       ffailedDPs->SetOwner(kTRUE);
 }
 
 //_______________________________________________________________
@@ -239,27 +249,35 @@ AliGRPPreprocessor::~AliGRPPreprocessor()
        //destructor
        
        delete fPressure;
+       delete ffailedDPs;
+
 }
 
 //_______________________________________________________________
 
 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
 {
-  // Initialize preprocessor
+       // Initialize preprocessor
 
-  AliPreprocessor::Initialize(run, startTime, endTime);
+       AliPreprocessor::Initialize(run, startTime, endTime);
+       
+       AliInfo("Initialization of the GRP preprocessor.");
+       AliInfo(Form("Start Time DCS = %d",GetStartTimeDCSQuery()));
+       AliInfo(Form("End Time DCS = %d",GetEndTimeDCSQuery()));
+       TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
+       for(Int_t j = 0; j < kNumSensors; j++) {
+               AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
+               sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
+       }
+       AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
+       
+       fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
 
-  AliInfo("Initialization of the GRP preprocessor.");
-  AliInfo(Form("Start Time DCS = %d",GetStartTimeDCSQuery()));
-  AliInfo(Form("End Time DCS = %d",GetEndTimeDCSQuery()));
-  TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
-  for(Int_t j = 0; j < kNumSensors; j++) {
-    AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
-    sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
-  }
-  AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
+       for (Int_t iDP=0; iDP < fgknDCSDP; iDP++){
+               TObjString* dp = new TObjString(fgkDCSDataPoints[iDP]);
+               ffailedDPs->AddAt(dp,iDP);
+       }
 
-  fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
 }
 
 //_______________________________________________________________
@@ -276,6 +294,7 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
        TString detector = (TString)GetRunParameter("detector");   
 
        AliGRPObject *grpobj = new AliGRPObject();  // object to store data
+       grpobj->SetBeamEnergyIsSqrtSHalfGeV(); // new format
 
        //=================//
        // DAQ logbook     //
@@ -323,11 +342,28 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
        //=================//
        Log(Form("Starting DCS Query at %d and finishing at %d",GetStartTimeDCSQuery(),GetEndTimeDCSQuery()));
        Int_t entries = ProcessDcsDPs( valueMap, grpobj );
-       Log(Form("entries found = %d (should be %d)",entries, fgknDCSDP-4));
-       if( entries < fgknDCSDP-4 ) { // FIXME (!= ) LHState, LHCLuminosity, BeamIntensity, LH3_BSF4_H3 are not working yet...  
-               Log(Form("Problem with the DCS data points!!! Only %d/%d entries found",entries,fgknDCSDP-4));
-               error |= 8;
-       } else  Log(Form("DCS data points, successful!"));
+       Log(Form("entries found = %d (should be %d)",entries, fgknDCSDP-fgkDCSDPNonWorking));
+       if (fdaqStartEndTimeOk){
+               if( entries < fgknDCSDP - fgkDCSDPNonWorking ) { // L3_BSF4_H3, L3_BSF17_H1, L3_BSF17_H2, L3_BSF17_H3, L3_BSF17_Temperature are not working yet...  
+                       Log(Form("Possible problem with the DCS data points!!! Only %d/%d entries found - Please read further for more details",entries,fgknDCSDP-fgkDCSDPNonWorking));
+                       Log(Form("The DPs giving problems were:"));
+                       for (Int_t iDP = 0; iDP < fgknDCSDP; iDP++){
+                               TObjString *dpString = (TObjString*)ffailedDPs->At(iDP);
+                               if (dpString){
+                                       TString name = dpString->String();
+                                       if (name != "L3_BSF4_H3" && name != "L3_BSF17_H1" && name != "L3_BSF17_H2" && name != "L3_BSF17_H3" && name != "L3_BSF17_Temperature" ){
+                                               Log(Form("******** %s ******** not present, but foreseen --> causing an ERROR",name.Data()));
+                                       }
+                                       else {
+                                               Log(Form(" %s is not present, but was not generating any error since it is not ready in DCS - check the other DPs in this list!",name.Data()));
+                                       }
+                               }
+                       }
+                       error |= 8;
+               }
+               else  Log(Form("DCS data points, successful!"));
+       }
+       else Log(Form("Statistical values for DCS DPs could not be computed due to missing DAQ_time_start and DAQ_time_end fields in DAQ logbook")); 
        
        //=======================//
        // Trigger Configuration //
@@ -396,6 +432,112 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
                error |= 32;
        }
 
+        //===========================//
+       // Trigger Timing Parameters //
+        //===========================//
+
+       
+       const char * triggerCTPtiming = GetCTPTimeParams();
+
+       if (partition.IsNull() && !detector.IsNull()){ // standalone partition
+               Log("STANDALONE partition for current run, using CTP timing params dummy value");
+               AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyCTPtime");
+               if (!cdbEntry) {
+                       Log(Form("No dummy CTP timing parameters entry found, going into error..."));
+                       error |= 64;
+               }
+               else{
+                       AliCTPTimeParams *runCTPtiming = (AliCTPTimeParams*)cdbEntry->GetObject();
+                       if (!runCTPtiming){
+                               Log(Form("dummy CTP timing parameters not found in OCDB entry, going into error..."));
+                               error |= 64;
+                       }
+                       else {
+                               TString titleCTPtiming = Form("CTP timing params for run %i from Dummy entry in OCDB",fRun);
+                               runCTPtiming->SetTitle(titleCTPtiming);
+                               AliCDBMetaData metadata;
+                               metadata.SetResponsible("Roman Lietava");
+                               metadata.SetComment("CTP run timing parameters from dummy entry in OCDB");
+                               if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
+                                       Log("Unable to store the dummy CTP timing params object to OCDB!");
+                                       error |= 64;
+                               }
+                       }
+               }
+       }
+
+       else if (!partition.IsNull() && detector.IsNull()){ // global partition
+               Log("GLOBAL partition for current run, using Trigger Timing Parameters from DAQ Logbook");
+               if (triggerCTPtiming!= NULL) {
+                       Log("Found trigger timing params in DAQ logbook");
+                       AliDebug(2,Form("%s",triggerCTPtiming));
+                       AliCTPTimeParams *runCTPtiming = AliCTPTimeParams::LoadCTPTimeParamsFromString(triggerCTPtiming);         
+                       if (!runCTPtiming) {
+                               Log("Bad CTP trigger timing params file from DAQ logbook! The corresponding CDB entry will not be filled!");
+                               error |= 64;
+                       }
+                       else {
+                               TString titleCTPtiming = Form("CTP timing params for run %i from DAQ",fRun);
+                               runCTPtiming->SetTitle(titleCTPtiming);
+                               AliCDBMetaData metadata;
+                               metadata.SetBeamPeriod(0);
+                               metadata.SetResponsible("Roman Lietava");
+                               metadata.SetComment("CTP timing params from DAQ logbook");
+                               if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
+                                       Log("Unable to store the CTP timing params object to OCDB!");
+                                       error |= 64;
+                               }
+                       }
+               }
+
+               else {
+                       Log("Trigger timing params NULL in DAQ logbook");
+                       error |= 64;
+               }
+       }
+
+       else {
+               Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger timing parameters...",partition.Data(),detector.Data()));
+               error |= 32;
+       }
+
+
+       //=================//
+       // LHC Data        //
+       //=================//
+
+       UInt_t iLHCData = ProcessLHCData(grpobj);
+
+       if( iLHCData == 0 ) {
+               Log(Form("LHC Data from DCS FXS, successful!"));
+       } else  if (iLHCData == 1) {
+               Log(Form("LHC Data, problems with DCS FXS!"));
+               error |= 256;
+       } else if (iLHCData ==3){
+               Log(Form("Problems in storing LHC Data - but not going into Error"));
+       } else if (iLHCData ==4){
+               Log(Form("Problems with LHC Data to be put in /GRP/GRP/LHCData - but not going into Error"));
+       } else{
+               Log(Form("LHC Data problems"));
+               error |= 512;
+       }
+       
+       //==================//
+       // SPD Mean Vertex  //
+       //==================//
+       if (runType == "PHYSICS"){
+               UInt_t iSPDMeanVertex = ProcessSPDMeanVertex();
+               if( iSPDMeanVertex == 1 ) {
+                       Log(Form("SPD Mean Vertex, successful!"));
+               } else {
+                       Log(Form("SPD Mean Vertex failed!!!"));
+                       error |= 128; 
+               }
+       }
+       else {
+               Log("SPD Mean Vertex not processed since runType != PHYSICS");
+       }
+
        // storing AliGRPObject in OCDB
 
        AliCDBMetaData md;
@@ -405,23 +547,237 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
        Bool_t result = kTRUE;
        result = Store("GRP", "Data", grpobj, &md); 
        delete grpobj;
-       
+
        if (result && !error ) {
                Log("GRP Preprocessor Success");
                return 0;
        } else {
-               Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s",
+               Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s%s%s%s%s",
                          kppError[(error&1)?1:0],
                          kppError[(error&2)?2:0],
                          kppError[(error&4)?3:0],
                          kppError[(error&8)?4:0],
                          kppError[(error&16)?5:0],
-                         kppError[(error&32)?6:0]
+                         kppError[(error&32)?6:0],
+                         kppError[(error&64)?7:0],
+                         kppError[(error&128)?8:0],
+                         kppError[(error&256)?9:0],
+                         kppError[(error&512)?10:0]
                          ));
                return error;
        }
+
+
 }
 
+//_______________________________________________________________
+
+UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
+{
+       //
+       //Getting the LHC Data from DCS FXS
+       //
+
+       TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
+       TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
+       if (timeStartString.IsNull() || timeEndString.IsNull()){
+               if (timeStartString.IsNull()){ 
+                       AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
+               }
+               else if (timeEndString.IsNull()){
+                       AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
+               }
+               return 2;
+       }  
+
+       Double_t timeStart = timeStartString.Atof();
+       Double_t timeEnd = timeEndString.Atof();
+
+       TString fileName = GetFile(kDCS, "LHCData","");
+       if (fileName.Length()>0){
+               AliInfo("Got The LHC Data file");
+               AliLHCReader lhcReader;
+               TMap* lhcMap = (TMap*)lhcReader.ReadLHCDP(fileName.Data());
+               if (lhcMap) {
+                       Log(Form("LHCData map entries = %d",lhcMap->GetEntries()));
+                       
+                       // Processing data to be put in AliGRPObject
+                       // Energy
+                       TObjArray* energyArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[0]);
+                       if (energyArray){                       
+                               Float_t energy = ProcessEnergy(energyArray,timeStart,timeEnd);
+                               if (energy != -1) {
+                                       grpobj->SetBeamEnergy(energy);
+                                       grpobj->SetBeamEnergyIsSqrtSHalfGeV(kTRUE);
+                               }
+                       }
+                       else {
+                               AliError("Energy not found in LHC Data file!!!");
+                       }       
+                       Double_t timeBeamMode = 0;
+                       Double_t timeMachineMode = 0;
+                       // BeamMode
+                       TObjArray* beamModeArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[2]);
+                       if (beamModeArray){             
+                               if (beamModeArray->GetEntries()==0){
+                                       AliInfo("No Beam Mode found, setting it to UNKNOWN");
+                                       grpobj->SetLHCState("UNKNOWN");
+                               }
+                               else{
+                                       AliDCSArray* beamMode = (AliDCSArray*)beamModeArray->At(0);
+                                       TObjString* beamModeString = beamMode->GetStringArray(0);
+                                       if (beamModeArray->GetEntries()>1){
+                                               AliDCSArray* beamMode1 = (AliDCSArray*)beamModeArray->At(1);
+                                               timeBeamMode = beamMode1->GetTimeStamp();
+                                               AliWarning(Form("The beam mode changed at timestamp %f! Setting it to the first value found and setting MaxTimeLHCValidity",timeBeamMode));
+                                       }
+                                       AliInfo(Form("LHC State (corresponding to BeamMode) = %s",(beamModeString->String()).Data()));
+                                       grpobj->SetLHCState(beamModeString->String());
+                               }
+                               
+                       }
+                       else{
+                               AliError("Beam mode array not found in LHC Data file!!!");
+                       }
+                       // MachineMode
+                       TObjArray* machineModeArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[1]);
+                       if (machineModeArray){
+                               if (machineModeArray->GetEntries()==0){
+                                       AliInfo("No Machine Mode found, setting it to UNKNOWN");
+                                       grpobj->SetMachineMode("UNKNOWN");
+                               }
+                               else{
+                                       AliDCSArray* machineMode = (AliDCSArray*)machineModeArray->At(0);
+                                       TObjString* machineModeString = machineMode->GetStringArray(0);
+                                       if (machineModeArray->GetEntries()>1){
+                                               AliDCSArray* machineMode1 = (AliDCSArray*)machineModeArray->At(1);
+                                               timeMachineMode = machineMode1->GetTimeStamp();
+                                               AliWarning(Form("The Machine Mode changed at timestamp %f! Setting it to the first value found and setting MaxTimeLHCValidity",timeMachineMode));
+                                       }
+                                       AliInfo(Form("Machine Mode = %s",(machineModeString->String()).Data()));
+                                       grpobj->SetMachineMode(machineModeString->String());
+                               }
+                       }
+                       else {
+                               AliError("Machine mode array not found in LHC Data file!!!");
+                       }       
+                       if (timeBeamMode!=0 || timeMachineMode!=0){
+                               Double_t minTimeLHCValidity;
+                               if (timeBeamMode == 0){
+                                       minTimeLHCValidity = timeMachineMode;
+                               }
+                               else if (timeMachineMode == 0){
+                                       minTimeLHCValidity = timeBeamMode;
+                               }
+                               else {
+                                       minTimeLHCValidity= TMath::Min(timeBeamMode,timeMachineMode);
+                               }
+                               AliWarning(Form("Setting MaxTimeLHCValidity to %f",minTimeLHCValidity));
+                               grpobj->SetMaxTimeLHCValidity(minTimeLHCValidity);
+                       }
+                       
+                       // BeamType1 and BeamType2 
+                       TObjArray* beam1Array = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[3]);
+                       if (beam1Array){                        
+                               AliInfo(Form("%d entries for Beam1",beam1Array->GetEntries()));
+                       }
+                       else{
+                               AliError("Beam1 array not found in LHC data file!!!");
+                       }                       
+                       TObjArray* beam2Array = 
+                               (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[4]);
+                       if (beam2Array){
+                               AliInfo(Form("%d entries for Beam2",beam2Array->GetEntries()));
+                       }
+                       else{
+                               AliError("Beam2 array not found in LHC data file!!!");
+                       }       
+                       // Processing data to go to AliLHCData object
+                       AliLHCData* dt = new AliLHCData(lhcMap,timeStart,timeEnd);
+                                                       
+                       // storing AliLHCData in OCDB
+                       if (dt){                        
+                               AliCDBMetaData md;
+                               md.SetResponsible("Ruben Shahoyan");
+                               md.SetComment("LHC data from the GRP preprocessor.");
+                               
+                               Bool_t result = kTRUE;
+                               result = Store("GRP", "LHCData", dt, &md); 
+                               delete dt;
+                               if (result) return 0;
+                               else return 3;
+                       }
+                       else return 4;
+                       delete lhcMap; 
+               }
+               else {
+                       AliError("Cannot read correctly LHCData file");
+                       return 2;
+               }
+       }
+  
+       else {
+               AliError("No LHCData file found in DCS FXS");
+               return 1;
+       }
+
+}
+
+//_______________________________________________________________
+
+UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
+{
+       //
+       //Getting the SPD Mean Vertex
+       //
+
+       TList* list = GetForeignFileSources("SPD", kDAQ, "VertexDiamond");
+       Bool_t storeResult = kTRUE;
+       if (list !=0x0 && list->GetEntries()!=0)
+               {
+                       AliInfo("The following sources produced files with the id VertexDiamond from SPD");
+                       list->Print();
+                       for (Int_t jj=0;jj<list->GetEntries();jj++){
+                               TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
+                               AliInfo(Form("found source %s", str->String().Data()));
+                               TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
+                               if (fileNameRun.Length()>0){
+                                       AliInfo(Form("Got the file %s", fileNameRun.Data()));
+                                       TFile daqFile(fileNameRun.Data(),"READ");
+                                       if (daqFile.IsOpen()) {
+                                               AliESDVertex* meanVtx = dynamic_cast<AliESDVertex*>(daqFile.Get("MeanVertexPos"));
+                                               if (meanVtx){
+                                                       meanVtx->Print();       
+                                                       // storing in the OCDB 
+                                                       AliCDBMetaData md;
+                                                       md.SetResponsible("Cvetan Cheshkov");
+                                                       md.SetComment("SPD Mean Vertex");                                       
+                                                       storeResult = Store("Calib", "MeanVertexSPD", meanVtx, &md, 0, kTRUE); 
+                                               }
+                                               else{
+                                                       AliWarning("No SPD Mean Vertex object found in file");
+                                               } 
+                                       }
+                                       else {
+                                               AliError("Can't open file");
+                                               storeResult = kFALSE;
+                                       }
+                               }
+                               else{
+                                       AliWarning("No file found for current source for SPD Mean Vertex");
+                               }
+                       }
+               }
+       else {
+               AliWarning("No list found for SPD Mean Vertex");
+       }
+
+       if (list) delete list;
+
+       return storeResult;
+}
+
+
 //_______________________________________________________________
 
 Int_t AliGRPPreprocessor::ProcessDaqLB(AliGRPObject* grpObj)
@@ -663,9 +1019,11 @@ UInt_t AliGRPPreprocessor::ProcessDcsFxs(TString partition, TString detector)
                                metaData.SetComment("CTP scalers");
                                if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
                                        Log("Unable to store the CTP scalers object to OCDB!");
+                                       delete scalers;                                 
                                        return 1;
                                }
                        }
+                       delete scalers;
                }
        }
        
@@ -688,18 +1046,17 @@ Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
        //
 
        Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
-       Int_t nLHCEntries = 0;
        Int_t nL3Entries = 0;
        Int_t nDipoleEntries = 0;
        Int_t nEnvEntries = 0;
        Int_t nHallProbesEntries = 0;
-       nLHCEntries = ProcessLHCDPs(valueMap, grpObj);
        nL3Entries = ProcessL3DPs(valueMap, grpObj);
        nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
        nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
        nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
        grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
-       entries = nLHCEntries + nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
+       Log(Form("L3Entries = %d, nDipoleEntries =%d, nEnvEntries = %d, nHallProbesEntries = %d", nL3Entries, nDipoleEntries, nEnvEntries, nHallProbesEntries));
+       entries = nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
        return entries;
 
 }
@@ -714,6 +1071,7 @@ Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpOb
        // L3 info
 
        Int_t nL3Entries = 0;
+
        TObjArray *array = 0x0;
        Int_t indexDP = -1;
        Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
@@ -742,7 +1100,10 @@ Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpOb
                                floatDCS = 0x0;
                        }
                }
-               if (!outOfRange) nL3Entries++;
+               if (!outOfRange) {
+                       nL3Entries++;
+                       ffailedDPs->RemoveAt(indexDP);
+               }
        }
 
        if (array) array = 0x0;
@@ -763,10 +1124,12 @@ Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpOb
                        if (change == kFALSE){
                                grpObj->SetL3Polarity(charDCS);
                                AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
+                               ffailedDPs->RemoveAt(indexDP);
                                nL3Entries++;
                        }
                        else if (isZero){
                                AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
+                               ffailedDPs->RemoveAt(indexDP);
                                nL3Entries++;
                        }
                        else {
@@ -815,7 +1178,10 @@ Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* g
                                floatDCS = 0x0;
                        }
                }
-               if (!outOfRange) nDipoleEntries++;
+               if (!outOfRange) {
+                       nDipoleEntries++;
+                       ffailedDPs->RemoveAt(indexDP);
+               }
        }
 
        if (array) array = 0x0;
@@ -836,10 +1202,12 @@ Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* g
                        if (!change){
                                grpObj->SetDipolePolarity(charDCS);
                                AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
+                               ffailedDPs->RemoveAt(indexDP);
                                nDipoleEntries++;
                        }
                        else if (isZero){
                                AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
+                               ffailedDPs->RemoveAt(indexDP);
                                nDipoleEntries++;
                        }
                        else{
@@ -887,47 +1255,95 @@ Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
                                floatDCS = 0x0;
                        }
                }
-               if (!outOfRange) nEnvEntries++;
+               if (!outOfRange) {
+                       ffailedDPs->RemoveAt(indexDP);
+                       nEnvEntries++;
+               }
        }
 
        if (array) array = 0x0;
 
-       AliInfo(Form("==========AtmosPressures (Cavern + Surface)==========="));
+       AliInfo(Form("========== AtmosPressures (Cavern + Surface + Cavern2) ==========="));
        AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
-       dcsSensorArray->Print();
-       if( fPressure->NumFits()==0 ) {
-               Log("Problem with the pressure sensor values!!!");
+       //dcsSensorArray->Print();
+       if( fPressure->NumFits()<kNumSensors ) {
+               Log(Form("Check the pressure sensor values! Not all the %d pressure sensors have been fit",kNumSensors));
        } 
-       else {
-               AliInfo(Form("==========CavernAtmosPressure==========="));
-               indexDP = kCavernAtmosPressure;
-               AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
-               AliDebug(2,Form("sensorCavernP2 = %p", sensorCavernP2));
-               if( sensorCavernP2->GetFit() ) {
-                       Log(Form("<%s> for run %d: Sensor Fit found",fgkDCSDataPoints[indexDP], fRun));
-                       grpObj->SetCavernAtmosPressure(sensorCavernP2);
-                       nEnvEntries++;
-               } 
-               //if (sensorP2) delete sensorP2;
+       Log(Form("Number of fits performed = %d",fPressure->NumFits()));
+
+       AliInfo(Form("==========CavernAtmosPressure==========="));
+       indexDP = kCavernAtmosPressure;
+       AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
+       TGraph* graph = sensorCavernP2->GetGraph();
+       AliDebug(3,Form("index = %d",indexDP));
+       AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
+       AliDebug(2,Form("graph = %p",graph));
+       AliDebug(3,Form("sensorCavernP2 = %p", sensorCavernP2));
+       if(sensorCavernP2->GetFit() || graph) {
+               if (sensorCavernP2->GetFit()){
+                       Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
+               }
                else {
-                       Log(Form("ERROR Sensor Fit for %s not found ", fgkDCSDataPoints[indexDP] ));
-               }
-               AliInfo(Form("==========SurfaceAtmosPressure==========="));
-               indexDP = kSurfaceAtmosPressure;
-               AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
-               AliDebug(2,Form("sensorP2 = %p", sensorP2));
-               if( sensorP2->GetFit() ) {
-                       Log(Form("<%s> for run %d: Sensor Fit found",fgkDCSDataPoints[indexDP], fRun));
-                       grpObj->SetSurfaceAtmosPressure(sensorP2);
-                       nEnvEntries++;
-               } 
-               //if (sensorP2) delete sensorP2;
+                       Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
+               }
+               grpObj->SetCavernAtmosPressure(sensorCavernP2);
+               ffailedDPs->RemoveAt(indexDP);
+               nEnvEntries++;
+       } 
+       //if (sensorP2) delete sensorP2;
+       else {
+               Log(Form("ERROR!!! Neither graph nor fit found for sensor %s - this will not increase the number of found DCS DPs and will cause an error", fgkDCSDataPoints[indexDP] ));
+       }
+       
+       AliInfo(Form("==========SurfaceAtmosPressure==========="));
+       indexDP = kSurfaceAtmosPressure;
+       AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
+       graph = sensorP2->GetGraph();
+       AliDebug(3,Form("index = %d",indexDP));
+       AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
+       AliDebug(2,Form("graph = %p",graph));   
+       AliDebug(3,Form("sensorP2 = %p", sensorP2));
+       if(sensorP2->GetFit() || graph) {
+               if (sensorP2->GetFit()){
+                       Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
+               }
                else {
-                       Log(Form("ERROR Sensor Fit for %s not found ", fgkDCSDataPoints[indexDP] ));
+                       Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
                }
-               
+               grpObj->SetSurfaceAtmosPressure(sensorP2);
+               ffailedDPs->RemoveAt(indexDP);
+               nEnvEntries++;
+       } 
+       //if (sensorP2) delete sensorP2;
+       else {
+               Log(Form("ERROR!!! Neither graph nor fit found for sensor %s - this will not increase the number of found DCS DPs and will cause an error", fgkDCSDataPoints[indexDP] ));
        }
 
+       AliInfo(Form("==========CavernAtmosPressure2==========="));
+       indexDP = kCavernAtmosPressure2;
+       AliDCSSensor* sensorCavernP22 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
+       graph = sensorCavernP22->GetGraph();
+       AliDebug(3,Form("index = %d",indexDP));
+       AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
+       AliDebug(2,Form("graph = %p",graph));   
+       AliDebug(3,Form("sensorCavernP2_2 = %p", sensorCavernP22));
+       if(sensorCavernP22->GetFit() || graph) {
+               if (sensorCavernP22->GetFit()){
+                       Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
+               }
+               else {
+                       Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
+               }
+               grpObj->SetCavernAtmosPressure2(sensorCavernP22);
+               ffailedDPs->RemoveAt(indexDP);
+               nEnvEntries++;
+       } 
+       //if (sensorP2) delete sensorP2;
+       else {
+               Log(Form("ERROR!!! Neither graph nor fit found for sensor %s - this will not increase the number of found DCS DPs and will cause an error", fgkDCSDataPoints[indexDP] ));
+       }
+       
+       
        return nEnvEntries;
 }
 //_______________________________________________________________
@@ -974,7 +1390,10 @@ Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpOb
                                        floatDCS = 0x0;
                                }
                        }
-                       if (!outOfRange) nHPEntries++;
+                       if (!outOfRange) {
+                               ffailedDPs->RemoveAt(indexDP + fgkDCSDPHallTopShift);  // 7 = shift in the complete list of DPs to get to the Hall Probes
+                               nHPEntries++;
+                       }
                }
        }
                
@@ -982,126 +1401,6 @@ Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpOb
        return nHPEntries;
 }
 
-//_______________________________________________________________
-
-Int_t AliGRPPreprocessor::ProcessLHCDPs(const TMap* valueMap, AliGRPObject* grpObj)
-{
-
-       //
-       // processing of LHC related DCS DPs, i.e.:
-       // LHCState
-       // LHCLuminosity
-       // BeamIntensity
-       //
-
-       Int_t nLHCEntries = 0;
-       TObjArray *array = 0x0;
-       Int_t indexDP = -1;
-
-       AliInfo(Form("==========LHCState==========="));
-       indexDP = kLHCState;
-       array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
-       if(!array) {
-               Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
-       } 
-       else {
-               if (array->GetEntries() == 0){
-                       AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
-               }
-               else {
-                       TString stringDCS = ProcessChar(array);
-                       if (stringDCS.Length()!=0) {
-                               Bool_t found = kFALSE;
-                               for( Int_t i=0; i<20; i+=2 ) {
-                                       if( stringDCS.CompareTo(fgkLHCState[i]) == 0 ) {
-                                               stringDCS = fgkLHCState[i+1];
-                                               found = kTRUE;
-                                               break;
-                                       }
-                               }
-                               if (found){
-                                       Log(Form("<%s> for run %d: %s",fgkDCSDataPoints[indexDP],fRun, stringDCS.Data()));
-                                       grpObj->SetLHCState(stringDCS);
-                               }
-                               else{
-                                       Log(Form("%s values found not valid!",fgkDCSDataPoints[indexDP]));
-                                       grpObj->SetLHCState(AliGRPObject::GetInvalidString());
-                               } 
-                       }
-                       else {
-                               Log(Form("%s not valid (null length), string set as invalid!",fgkDCSDataPoints[indexDP]));
-                               grpObj->SetLHCState(AliGRPObject::GetInvalidString());
-                       }         
-               }
-               nLHCEntries++;
-       }
-       
-       if (array) array = 0x0;
-
-       AliInfo(Form("==========LHCLuminosity==========="));
-       Bool_t outOfRange = kFALSE; // flag to monitor if any value collected by DCS is out of range
-       indexDP = kLHCLuminosity;
-       array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
-       if(!array) {
-               Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
-       } 
-       else {
-               if (array->GetEntries() == 0){
-                       AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
-               }
-               else {
-                       Float_t *floatDCS = ProcessFloatAll(array);
-                       if (floatDCS != NULL){
-                               grpObj->SetLHCLuminosity(floatDCS);
-                               AliSplineFit* splfit = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
-                               grpObj->SetLHCLuminositySplineFit(splfit);
-                       //              delete splfit;
-                       }
-                       else {
-                               outOfRange = kTRUE;
-                       }
-                       if (floatDCS){
-                               delete[] floatDCS;
-                               floatDCS = 0x0;
-                       }
-               }
-               if (!outOfRange) nLHCEntries++;
-       }
-
-       if (array) array = 0x0;
-
-       AliInfo(Form("==========BeamIntensity==========="));
-       if (outOfRange) outOfRange = kFALSE;  // resetting outOfRange if needed
-       indexDP = kBeamIntensity;
-       array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
-       if(!array) {
-               Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
-       } 
-       else {
-               if (array->GetEntries() == 0){
-                       AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
-               }
-               else {
-                       Float_t *floatDCS = ProcessFloatAll(array);
-                       if (floatDCS != NULL){
-                               grpObj->SetBeamIntensity(floatDCS);
-                               AliSplineFit* splfit1 = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
-                               grpObj->SetBeamIntensitySplineFit(splfit1);
-                               //delete splfit;
-                       }
-                       else{
-                               outOfRange = kTRUE;
-                       }
-                       if (floatDCS){
-                               delete[] floatDCS;
-                               floatDCS = 0x0;
-                       }
-               }
-               if (!outOfRange) nLHCEntries++;
-       }
-
-       return nLHCEntries;
-}
 //_________________________________________________________________________
 
 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
@@ -1188,6 +1487,7 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
                else if (timeStartString.IsNull()){
                        AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
                }
+               fdaqStartEndTimeOk = kFALSE;
                return 0;
        }  
 
@@ -1206,6 +1506,8 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
        Float_t* arrayValues = 0x0; 
        Double_t* arrayWeights = 0x0; 
        Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
+       Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
+
        for(Int_t i = 0; i < nCounts; i++) {
                AliDCSValue *v = (AliDCSValue *)array->At(i);
                if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
@@ -1242,10 +1544,10 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
        }
 
        if (timestampBeforeSOR == -1){
-               AliWarning("No value found before SOR!");
+               AliWarning("No value found before SOR");
        }
        if (timestampAfterEOR == -1){
-               AliWarning("No value found after EOR!");
+               AliWarning("No value found after EOR");
        }
 
        AliDebug(2,Form("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries",iCounts,nCounts));
@@ -1305,14 +1607,9 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
                        return parameters;
                }
        }
-       else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
-               if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
-                       if (timestampBeforeSOR == -1){
-                               AliError("Cannot calculate mean, truncated mean, median, SD wrt mean, SD wrt median for current DP - no points during the run collected, and point before SOR missing");
-                       }
-                       if (timestampAfterEOR == -1){
-                               AliError("Cannot calculate mean, truncated mean, median, SD wrt mean, SD wrt median for current DP - no points during the run collected, and point after EOR missing");
-                       }
+       else { // iCountsRun == 0, using only the point immediately before SOR
+               if (timestampBeforeSOR == -1){
+                       AliError("Cannot set mean, truncated mean, median, SD wrt mean, SD wrt median for current DP - no points during the run collected, and point before SOR missing");
                        parameters[0] = AliGRPObject::GetInvalidFloat();
                        parameters[1] = AliGRPObject::GetInvalidFloat();
                        parameters[2] = AliGRPObject::GetInvalidFloat();
@@ -1321,19 +1618,12 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
                        return parameters;
                }
                else {
-                       AliWarning("Using last entry before SOR and first entry after EOR. Truncated mean won't be calculated.");
-                       nentriesUsed = 2;
-                       arrayValues = new Float_t[2];
-                       arrayWeights = new Double_t[2];
-                       arrayValues[0] = valueBeforeSOR;
-                       arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
-                       arrayValues[1] = valueAfterEOR;
-                       arrayWeights[1] = 1.;
-                       AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
-                       AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
-                       parameters[0] = TMath::Mean(1,arrayValues,arrayWeights);
-                       parameters[2] = TMath::Median(1,arrayValues,arrayWeights);
+                       AliWarning("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.");
+                       AliDebug(2,Form("value = %f",valueBeforeSOR)); 
+                       parameters[0] = valueBeforeSOR;
+                       parameters[2] = valueBeforeSOR;
                        truncMeanFlag = kFALSE;
+                       sdFlag = kFALSE;
                }
        }
 
@@ -1346,38 +1636,48 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
 
        // calculating SD wrt Mean and Median
        AliDebug(2,"Calculating SD wrt Mean and SD wrt Median");
-       for (Int_t i =0; i< nentriesUsed; i++){
-               AliInfo(Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
-               temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
-               temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
-               sumweights += arrayWeights[i];
-       }
-       // setting SD wrt Mean 
-       if (sumweights != 0 ){
-               parameters[3] = TMath::Sqrt(temp1/sumweights);
+       if (sdFlag){
+               for (Int_t i =0; i< nentriesUsed; i++){
+                       AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
+                       temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
+                       temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
+                       sumweights += arrayWeights[i];
+               }
+               // setting SD wrt Mean 
+               if (sumweights != 0 ){
+                       parameters[3] = TMath::Sqrt(temp1/sumweights);
+               }
+               else {
+                       AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
+                       parameters[3] = AliGRPObject::GetInvalidFloat();
+               }
+               // setting SD wrt Median
+               if (nentriesUsed != 0){
+                       parameters[4] = TMath::Sqrt(temp/nentriesUsed);
+               }
+               else{
+                       AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
+                       parameters[4] = AliGRPObject::GetInvalidFloat();
+               }
        }
        else {
-               AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
                parameters[3] = AliGRPObject::GetInvalidFloat();
-       }
-       // setting SD wrt Median
-       if (nentriesUsed != 0){
-               parameters[4] = TMath::Sqrt(temp/nentriesUsed);
-       }
-       else{
-               AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
                parameters[4] = AliGRPObject::GetInvalidFloat();
-       }
+       }               
 
        // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
        if (truncMeanFlag){
                AliDebug(2,"Calculating Truncated Mean");
                for (Int_t i =0; i< nentriesUsed; i++){
+                       AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
                        if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
-                               AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
+                               arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
+                               arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
+                               AliDebug(2,Form("For Truncated Mean: Entry %d: value = %f, weight = %f",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]));
                                entriesTruncMean++;                     
-                               arrayValuesTruncMean[i]=arrayValues[i];
-                               arrayWeightsTruncMean[i]=arrayWeights[i];
+                       }
+                       else{
+                               AliDebug(2,"Discarding entry");
                        }
                }
                // setting truncated mean 
@@ -1505,7 +1805,6 @@ Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
        Int_t timeEnd = (Int_t)(timeEndString.Atoi());
        Float_t aDCSArrayMean = 0.0;
        Int_t iCounts = 0;
-       AliDCSValue* v = 0x0;
        Float_t valueBeforeSOR = 0;
        Float_t valueAfterEOR = 0;
        Int_t timestampBeforeSOR = -1;
@@ -1518,7 +1817,7 @@ Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
        Int_t nCounts = array->GetEntries();
 
        for(Int_t i = 0; i < nCounts; i++) {
-               v = (AliDCSValue *)array->At(i);
+               AliDCSValue* v = (AliDCSValue *)array->At(i);
                if ((v->GetInt() < fminInt) || (v->GetInt() > fmaxInt)) {
                        AliError(Form("Error! Int value found in DCS map at %d-th entry is OUT OF RANGE: value = %d",i, v->GetInt()));
                        return AliGRPObject::GetInvalidFloat();
@@ -1661,7 +1960,6 @@ Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
        Int_t timeEnd = (Int_t)(timeEndString.Atoi());
        Float_t aDCSArrayMean = 0.0;
        Int_t iCounts = 0;
-       AliDCSValue* v = 0x0;
        Float_t valueBeforeSOR = 0;
        Float_t valueAfterEOR = 0;
        Int_t timestampBeforeSOR = -1;
@@ -1674,7 +1972,7 @@ Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
        Int_t nCounts = array->GetEntries();
 
        for(Int_t i = 0; i < nCounts; i++) {
-               v = (AliDCSValue *)array->At(i);
+               AliDCSValue* v = (AliDCSValue *)array->At(i);
                if ((v->GetUInt() < fminUInt) || (v->GetUInt() > fmaxUInt)) {
                        AliError(Form("Error! UInt value found in DCS map at %d-th entry is OUT OF RANGE: value = %u",i,v->GetUInt()));
                        return AliGRPObject::GetInvalidFloat();
@@ -1801,10 +2099,11 @@ AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
        
        TMap *map = fPressure->ExtractDCS(dcsAliasMap);
        if (map) {
+               AliDebug(2,Form("Map has %d entries",map->GetEntries()));
                fPressure->MakeSplineFit(map);
                Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
                if (fitFraction > kFitFraction ) {
-                       AliInfo(Form("Pressure values extracted, %d fits performed.", fPressure->NumFits()));
+                       AliInfo(Form("Pressure values extracted, %d fits performed for %d sensors.", fPressure->NumFits(),fPressure->NumSensors()));
                } else { 
                        AliInfo("Too few pressure maps fitted!!!");
                }
@@ -1900,6 +2199,7 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
        grpObj->SetL3Polarity(l3Polarity);
        grpObj->SetDipolePolarity(dipolePolarity);
+       grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
 
        delete row;
        row = 0;
@@ -2060,3 +2360,269 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        
        return lastRun;
 }
+//-----------------------------------------------------------------
+Double_t AliGRPPreprocessor::CalculateMean(TObjArray* array){
+
+       //
+       // Calculating mean over TObjArray from LHC Data
+       //
+
+       TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
+       TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
+       if (timeStartString.IsNull() || timeStartString.IsNull()){
+               if (timeStartString.IsNull()){ 
+                       AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
+               }
+               else if (timeStartString.IsNull()){
+                       AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
+               }
+               return 0;
+       }  
+
+       Int_t timeStart = (Int_t)(timeStartString.Atoi());
+       Int_t timeEnd = (Int_t)(timeEndString.Atoi());
+       timeStart = 1260646960;
+       timeEnd = 1260652740;
+       Double_t* parameters = new Double_t[5];
+       parameters[0] = -1.;
+       parameters[1] = -1.;
+       parameters[2] = -1.;
+       parameters[3] = -1.;
+       parameters[4] = -1.;
+       Int_t iCounts = 0;
+       Int_t iCountsRun = 0;
+       Int_t nCounts = array->GetEntries();
+       printf("ncounts = %d\n",nCounts);
+       Double_t valueBeforeSOR = 0;
+       Double_t valueAfterEOR = 0;
+       Double_t timestampBeforeSOR = -1.;
+       Double_t timestampAfterEOR = -1.;
+       Int_t ientrySOR = -1;
+       Int_t ientryEOR = -1;
+       Double_t* arrayValues = 0x0; 
+       Double_t* arrayWeights = 0x0; 
+       Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
+       Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
+
+       for(Int_t i = 0; i < nCounts; i++) {
+               AliDCSArray *dcs = (AliDCSArray*)array->At(i);
+               if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
+                       AliDebug(2,Form("%d-th entry = %f at timestamp %f\n",i,(Double_t)(dcs->GetInt(0)),dcs->GetTimeStamp()));
+                       iCounts += 1;
+                       // look for the last value before SOR and the first value before EOR
+                       if ((dcs->GetTimeStamp() >= timeStart) && (dcs->GetTimeStamp() < timeStart)) {
+                               timestampBeforeSOR = dcs->GetTimeStamp();
+                               AliDebug(2,Form("timestamp of last value before SOR = %f, with DAQ_time_start = %d\n",timestampBeforeSOR,timeStart));
+                               valueBeforeSOR = (Double_t)(dcs->GetInt(0));
+                       }
+                       else if ((dcs->GetTimeStamp() <= timeEnd) && (dcs->GetTimeStamp() > timeEnd) && timestampAfterEOR == -1){
+                               timestampAfterEOR = dcs->GetTimeStamp();
+                               valueAfterEOR = (Double_t)(dcs->GetInt(0));
+                               AliDebug(2,Form("timestamp of first value after EOR = %f, with DAQ_time_end = %d\n",timestampAfterEOR,timeEnd));
+                       }
+                       // check if there are DPs between DAQ_time_start and DAQ_time_end
+                       if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
+                               if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
+                               if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
+                               AliDebug(2,Form("entry between SOR and EOR\n"));
+                               iCountsRun += 1;
+                       }
+               }
+               else {
+                       printf("DCS values for the parameter outside the queried interval: timestamp = %f\n",dcs->GetTimeStamp());
+               }
+       }
+
+       if (timestampBeforeSOR == -1.){
+               printf("No value found before SOR\n");
+       }
+       if (timestampAfterEOR == -1.){
+               printf("No value found after EOR\n");
+       }
+
+       printf("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries\n",iCounts,nCounts);
+       printf("Last value before DAQ_time_start (SOR) = %f at timestamp = %f\n",valueBeforeSOR,timestampBeforeSOR);
+       printf("First value after DAQ_time_end (EOR)   = %f at timestamp = %f\n",valueAfterEOR,timestampAfterEOR);
+       printf("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n",iCountsRun);
+       printf("Index of first entry after DAQ_time_start (SOR) = %d\n ",ientrySOR);
+       printf("Index of first entry before DAQ_time_end (EOR) = %d\n ",ientryEOR);
+
+       Int_t nentriesUsed = 0;
+       if (iCountsRun > 1){
+               printf("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n");
+               printf("Calculating (weighted) Mean and Median\n" );
+               arrayValues = new Double_t[iCountsRun]; 
+               arrayWeights = new Double_t[iCountsRun]; 
+               nentriesUsed = iCountsRun;
+               for (Int_t i = ientrySOR; i <= ientryEOR; i++){
+                       AliDCSArray *dcs = (AliDCSArray *)array->At(i);
+                       Double_t timestamp2 = 0;
+                       if (i < ientryEOR){
+                               AliDCSArray *dcs1 = (AliDCSArray *)array->At(i+1);
+                               timestamp2 = dcs1->GetTimeStamp();
+                       }
+                       else {
+                               timestamp2 = (Double_t)timeEnd+1;
+                       }
+                       arrayWeights[i-ientrySOR] = (Double_t)((Double_t)timestamp2 - dcs->GetTimeStamp());
+                       arrayValues[i-ientrySOR] = (Double_t)(dcs->GetInt(0));
+                       printf("Entry %d: value = %f, weight = %f\n",i-ientrySOR,arrayValues[i-ientrySOR],arrayWeights[i-ientrySOR]);
+               }
+               parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
+               parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
+       }
+       else if (iCountsRun == 1){
+               AliDCSArray* dcs = (AliDCSArray *)array->At(ientrySOR);
+               nentriesUsed = 2;
+               if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)dcs->GetTimeStamp()){
+                       printf("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR. Truncated mean won't be calculated.\n");
+                       arrayValues = new Double_t[2];
+                       arrayWeights = new Double_t[2];
+                       arrayValues[0] = valueBeforeSOR;
+                       arrayWeights[0] = (Double_t)(dcs->GetTimeStamp()-(Double_t)timestampBeforeSOR);
+                       arrayValues[1] = (Double_t)(dcs->GetInt(0));
+                       arrayWeights[1] = (Double_t)((Double_t)timeEnd+1-dcs->GetTimeStamp());
+                       printf("value0 = %f, with weight = %f\n",arrayValues[0],arrayWeights[0]); 
+                       printf("value1 = %f, with weight = %f\n",arrayValues[1],arrayWeights[1]); 
+                       parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
+                       parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
+                       truncMeanFlag = kFALSE;
+               }
+               else{
+                       printf("Cannot calculate mean, truncated mean, median, SD wrt mean, SD wrt median for current DP - only one value collected during the run, but no value before with which to calculate the statistical quantities\n");
+                       parameters[0] = -1;
+                       parameters[1] = -1;
+                       parameters[2] = -1;
+                       parameters[3] = -1;
+                       parameters[4] = -1;
+                       return parameters[0];
+               }
+       }
+       else { // iCountsRun == 0, using only the point immediately before SOR
+               if (timestampBeforeSOR == -1.){
+                       printf("Cannot set mean, truncated mean, median, SD wrt mean, SD wrt median for current DP - no points during the run collected, and point before SOR missing\n");
+                       parameters[0] = -1;
+                       parameters[1] = -1;
+                       parameters[2] = -1;
+                       parameters[3] = -1;
+                       parameters[4] = -1;
+                       return parameters[0];
+               }
+               else {
+                       printf("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.\n");
+                       printf("value = %f\n",valueBeforeSOR); 
+                       parameters[0] = valueBeforeSOR;
+                       parameters[2] = valueBeforeSOR;
+                       truncMeanFlag = kFALSE;
+                       sdFlag = kFALSE;
+               }
+       }
+
+       Double_t temp = 0;
+       Double_t temp1 = 0;
+       Double_t sumweights = 0; 
+       Int_t entriesTruncMean = 0;
+       Double_t* arrayValuesTruncMean = new Double_t[nentriesUsed]; 
+       Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
+
+       // calculating SD wrt Mean and Median
+       printf("Calculating SD wrt Mean and SD wrt Median\n");
+       if (sdFlag){
+               for (Int_t i =0; i< nentriesUsed; i++){
+                       //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
+                       temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
+                       temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
+                       sumweights += arrayWeights[i];
+               }
+               // setting SD wrt Mean 
+               if (sumweights != 0 ){
+                       parameters[3] = TMath::Sqrt(temp1/sumweights);
+               }
+               else {
+                       printf("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid\n");
+                       parameters[3] = -1;
+               }
+               // setting SD wrt Median
+               if (nentriesUsed != 0){
+                       parameters[4] = TMath::Sqrt(temp/nentriesUsed);
+               }
+               else{
+                       printf("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid\n");
+                       parameters[4] = -1;
+               }
+       }
+       else {
+               parameters[3] = -1;
+               parameters[4] = -1;
+       }               
+
+       // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
+       if (truncMeanFlag){
+               printf("Calculating Truncated Mean\n");
+               for (Int_t i =0; i< nentriesUsed; i++){
+                       //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
+                       if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
+                               arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
+                               arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
+                               printf("For Truncated Mean: Entry %d: value = %f, weight = %f\n",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]);
+                               entriesTruncMean++;                     
+                       }
+                       else{
+                               printf("Discarding entry\n");
+                       }
+               }
+               // setting truncated mean 
+               if (entriesTruncMean >1){
+                       printf("%d entries used for truncated mean\n",entriesTruncMean);
+                       parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
+               }
+               else{   
+                       printf("Too few entries (%d) to calculate truncated mean\n",entriesTruncMean);
+                       parameters[1] = -1;
+               }
+       }
+       else{
+                       parameters[1] = -1;
+       }
+       
+       printf("(weighted) mean = %f \n",parameters[0]);
+       printf("(weighted) truncated mean = %f \n",parameters[1]);
+       printf("median = %f \n",parameters[2]);
+       printf("(weighted) standard deviation with (weighted) mean = %f \n",parameters[3]);
+       printf("standard deviation with median = %f \n",parameters[4]);
+       
+       return (parameters[0]);
+}
+//------------------------------------------------------------------------------------------------------
+Float_t AliGRPPreprocessor::ProcessEnergy(TObjArray* array, Double_t timeStart, Double_t timeEnd){
+
+       //
+       // Method to processo LHC Energy information
+       // Only the first value is returned, provided that it is withing DAQ_time_start and DAQ_time_end
+       //
+
+       Int_t nCounts = array->GetEntries();
+       Float_t energy = -1;
+       Bool_t inRange = kFALSE;
+       AliDebug(2,Form("Energy measurements = %d\n",nCounts));
+       if (nCounts ==0){
+               AliWarning("No Energy values found! Beam Energy remaining invalid!");
+       }
+       else{
+               for(Int_t i = 0; i < nCounts; i++) {
+                       AliDCSArray *dcs = (AliDCSArray*)array->At(i);
+                       if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
+                               energy = (Float_t)(TMath::Nint(((Double_t)(dcs->GetInt(0)))*120/1000)); // sqrt(s)/2 energy in GeV
+                               AliInfo(Form("Energy value found = %d, converting --> sqrt(s)/2 = %f (GeV)", dcs->GetInt(0),energy));
+                               inRange = kTRUE;
+                               break;
+                       }
+               }
+               if (inRange == kFALSE){
+                               AliInfo("No Energy value found between DAQ_time_start and DAQ_time_end - energy will remain invalid!");
+               }
+       
+       }
+
+       return energy;
+}