]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliGRPPreprocessor.cxx
AliPIDResponse related
[u/mrichter/AliRoot.git] / STEER / AliGRPPreprocessor.cxx
index 6da314d44157a211097702c377bab5661349b3c2..becdb0c455442e4ea936416354ef707515f218af 100644 (file)
 
 #include "AliTriggerConfiguration.h"
 #include "AliTriggerRunScalers.h"
+#include "AliTriggerInput.h"
 
 #include "AliCDBMetaData.h"
 #include "AliESDVertex.h"
 #include "AliLHCReader.h"
 #include "AliLHCData.h"
 #include "AliDCSArray.h"
+#include "AliDAQ.h"
+#include "AliLTUConfig.h"
 
 class AliLog;
 class AliDCSValue;
@@ -75,7 +78,7 @@ ClassImp(AliGRPPreprocessor)
   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 6; // num parameters in the logbook used to fill the GRP object
   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 = 4;   // number of dcs dps from LHC data
+  const Int_t AliGRPPreprocessor::fgknLHCDP = 6;   // 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] = {
@@ -180,7 +183,9 @@ ClassImp(AliGRPPreprocessor)
          "LHC_Beam_Energy",
          "LHC_MachineMode",
          "LHC_BeamMode",
-          "LHC_Beams_Particle_Type"
+          "LHC_Beams_Particle_Type",
+         "BPTX_Phase_Shift_B1",
+         "BPTX_Phase_Shift_B2"
   };
 
   const char* kppError[] = {
@@ -194,7 +199,9 @@ ClassImp(AliGRPPreprocessor)
                    "(CTP timing ERROR)",
                   "(SPD Mean Vertex ERROR)",
                   "(DCS FXS Error for LHC Data)",
-                  "(LHC Data Error)"
+                  "(LHC Data Error)",
+                  "(LHC Clock Phase Error (from LHC Data))",
+                  "(LTU Configuration Error)"
   };
 
 //_______________________________________________________________
@@ -515,6 +522,90 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
                error |= 32;
        }
 
+        //===========================//
+       // LTU Configuration         //
+        //===========================//
+
+       Log("*************** Processing LTU Configuration");
+       
+       if (partition.IsNull() && !detector.IsNull()){ // standalone partition
+               Log("STANDALONE partition for current run, using LTU configuration dummy value");
+               AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyLTUConfig");
+               if (!cdbEntry) {
+                       Log(Form("No dummy LTU Config entry found, going into error..."));
+                       error |= 2048;
+               }
+               else{
+                       TObjArray *ltuConfig = (TObjArray*)cdbEntry->GetObject();
+                       if (!ltuConfig){
+                               Log(Form("dummy LTU Config not found in OCDB entry, going into error..."));
+                               error |= 2048;
+                       }
+                       else {
+                               AliCDBMetaData metadata;
+                               metadata.SetResponsible("Roman Lietava");
+                               metadata.SetComment("LTU Config from dummy entry in OCDB");
+                               if (!Store("CTP","LTUConfig", ltuConfig, &metadata, 0, 0)) {
+                                       Log("Unable to store the dummy LTU Config object to OCDB!");
+                                       error |= 2048;
+                               }
+                       }
+               }
+       }
+
+       else if (!partition.IsNull() && detector.IsNull()){ // global partition
+       
+               Log("GLOBAL partition for current run, getting LTU Config from DAQ Logbook (logbook_detectors table)");
+               UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
+               Printf ("detectormask = %d",detectorMask);
+               TObjArray * ltuarray = new TObjArray();
+               ltuarray->SetOwner(1);
+               Bool_t isLTUok = kTRUE;
+               for(Int_t i = 0; i<AliDAQ::kNDetectors-2; i++){
+                       if ((detectorMask >> i) & 0x1) {
+                               TString det = AliDAQ::OfflineModuleName(i);
+                               TString detCTPName = AliTriggerInput::fgkCTPDetectorName[i];
+                               if (detCTPName == "CTP") {
+                                       detCTPName="TRG"; // converting according to what is found in DAQ logbook_detectors                                     
+                                       Printf("Processing CTP (CTP Detector name %s) --> SKIPPING, CTP does not have any LTU!!!!!!",detCTPName.Data());
+                                       continue;
+                               }                               
+                               Printf("Processing detector %s (CTP Detector name %s)",det.Data(),detCTPName.Data());
+                               TString* ltu = GetLTUConfig(detCTPName.Data());
+                               if (!ltu){
+                                       Log(Form("No LTU Configuration from DAQ logbook for detector %s (BUT it was expected)! The corresponding CDB entry will not be filled!",detCTPName.Data()));
+                                       error |= 2048;
+                                       isLTUok = kFALSE;
+                                       break;
+                               }
+                               else{
+                                       Float_t ltuFineDelay1 = ltu[0].Atof();
+                                       Float_t ltuFineDelay2 = ltu[1].Atof();
+                                       Float_t ltuBCDelayAdd = ltu[2].Atof();
+                                       const char* name = AliDAQ::DetectorName(i);
+                                       AliLTUConfig* ltuConfig = new AliLTUConfig((UChar_t)AliDAQ::DetectorID(name),ltuFineDelay1,ltuFineDelay2,ltuBCDelayAdd);
+                                       ltuarray->AddAtAndExpand(ltuConfig,i);
+                               }                               
+                       }
+               }
+               if (isLTUok){
+                       AliCDBMetaData metadata;
+                       metadata.SetBeamPeriod(0);
+                       metadata.SetResponsible("Roman Lietava");
+                       metadata.SetComment("LTU Configuration for current run");
+                       if (!Store("CTP","LTUConfig", ltuarray, &metadata, 0, 0)) {
+                               Log("Unable to store the LTU Config object to OCDB!");
+                               error |= 2048;
+                       }               
+               }
+               delete ltuarray;
+       }
+
+       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        //
@@ -530,10 +621,15 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
                } else  if (iLHCData == 1) {
                        Log(Form("LHC Data, problems with DCS FXS!"));
                        error |= 256;
+               } else  if (iLHCData == 2) {
+                       Log(Form("LHC Data, problems with DAQ_time_start/DAQ_time_end!"));
+                       error |= 512;
                } else if (iLHCData ==3){
-                       Log(Form("Problems in storing LHC Data - but not going into Error"));
+                       Log(Form("Problems in storing LHC Phase - going into Error"));
+                       error |= 1024;
                } else if (iLHCData ==4){
-                       Log(Form("Problems with LHC Data to be put in /GRP/GRP/LHCData - but not going into Error"));
+                       Log(Form("Problems with LHC Phase - going into Error"));
+                       error |= 1024;
                } else{
                        Log(Form("LHC Data problems"));
                        error |= 512;
@@ -574,7 +670,7 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
                Log("GRP Preprocessor Success");
                return 0;
        } else {
-               Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s%s%s%s%s",
+               Log( Form("GRP Preprocessor FAILS!!! %s%s%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],
@@ -584,7 +680,9 @@ UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
                          kppError[(error&64)?7:0],
                          kppError[(error&128)?8:0],
                          kppError[(error&256)?9:0],
-                         kppError[(error&512)?10:0]
+                         kppError[(error&512)?10:0],
+                         kppError[(error&1024)?11:0],
+                         kppError[(error&2048)?12:0]
                          ));
                return error;
        }
@@ -794,7 +892,7 @@ UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
                                        }
                                }
                                if (!foundBeamStart){
-                                       AliInfo("No value for the Machine Mode found before start of run, the Machine Mode will remain empty");
+                                       AliInfo("No value for the Beam Type found before start of run, the Machine Mode will remain empty");
                                }
                                else {
                                        AliDCSArray* beam = (AliDCSArray*)beamArray->At(indexBeam);
@@ -805,13 +903,19 @@ UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
                                                AliInfo("Setting beam type to p-p");
                                                grpobj->SetBeamType("p-p");
                                        }
-                                       else if (beamType.CompareTo("LEAD82",TString::kIgnoreCase) == 0){
+                                       else { // if there is no PROTON beam, we suppose it is Pb, and we put A-A
+                                               AliInfo("Setting beam type to A-A");
+                                               grpobj->SetBeamType("A-A");
+                                       }
+                                       /*
+                                         else if (beamType.CompareTo("LEAD82",TString::kIgnoreCase) == 0){
                                                AliInfo("Setting beam type to Pb-Pb");
                                                grpobj->SetBeamType("Pb-Pb");
                                        }
                                        else{
                                                AliError("Beam Type not known, leaving it empty");
                                        }
+                                       */
                                        if (indexBeam < nBeam-1){
                                                AliDCSArray* beam1 = (AliDCSArray*)beamArray->At(indexBeam+1);
                                                if (beam1){
@@ -877,9 +981,31 @@ UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
                  Bool_t result = kTRUE;
                  result = Store("GRP", "LHCData", dt, &md); 
                  delete dt;
-                 if (result) return 0;
-                 else return 3;
+                 if (!result){
+                       Log(Form("Problems in storing LHC Data - but not going into Error"));
+                 }
+               }
+
+               // processing LHC Phase
+
+               TObjArray *beam1phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[4]);
+               TObjArray *beam2phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[5]);
+               if (beam1phase == 0x0 || beam2phase == 0x0){
+                       Log(Form("Problems in retrieving LHC Clock data from LHC file"));
+                       return 4;
+               }                       
+               AliLHCClockPhase *phaseObj = ProcessLHCClockPhase(beam1phase,beam2phase,timeEnd);
+               if (phaseObj){
+                       AliInfo(Form("LHC Phase found"));
+                       AliCDBMetaData mdPhase;
+                       mdPhase.SetResponsible("Cvetan Cheshkov");
+                       mdPhase.SetComment("LHC Clock Phase");
+                       Bool_t result = kTRUE;
+                       result = Store("Calib", "LHCClockPhase", phaseObj, &mdPhase); 
+                       delete phaseObj;
+                       if (!result) return 3;
                }
+               else return 4;
        }
        
        else {
@@ -905,7 +1031,11 @@ UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
                        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));
+                               TObjString * str = dynamic_cast<TObjString*> (list->At(jj)); 
+                               if (!str){
+                                       AliError(Form("Expecting a TObjString in the list for the %d-th source, but something else was found.",jj));
+                                       continue;
+                               }
                                AliInfo(Form("found source %s", str->String().Data()));
                                TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
                                if (fileNameRun.Length()>0){
@@ -1039,6 +1169,7 @@ UInt_t AliGRPPreprocessor::ProcessDaqFxs()
        //======DAQ FXS======//
        
        AliRawEventHeaderV3_9::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_9 - temporary fix 
+       AliRawEventHeaderV3_11::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_11 - temporary fix 
        TList* list = GetFileSources(kDAQ);  
        if (!list) {
                Log("No raw data tag list: connection problems with DAQ FXS logbook!");
@@ -1583,10 +1714,6 @@ AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TSt
        fit->InitKnots(gr,10,10,0.0);
        fit->SplineFit(2);
        fit->Cleanup();
-       if (!fit) {
-               AliWarning(Form("%s: no fit performed",stringID.Data()));
-               return NULL;
-       } 
        return fit;
 }
 
@@ -1667,6 +1794,7 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
                        AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
                        if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
                        if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
+                       delete [] parameters;
                        return NULL;
                }
                if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
@@ -1847,6 +1975,15 @@ Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
                        parameters[1] = AliGRPObject::GetInvalidFloat();
        }
 
+       if (arrayValues){
+               delete [] arrayValues;
+       } 
+       if (arrayWeights){
+               delete [] arrayWeights;
+       } 
+       delete [] arrayValuesTruncMean;
+       delete [] arrayWeightsTruncMean;
+
        AliInfo(Form("(weighted) mean = %f ",parameters[0]));
        AliInfo(Form("(weighted) truncated mean = %f ",parameters[1]));
        AliInfo(Form("median = %f ",parameters[2]));
@@ -2037,6 +2174,8 @@ Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
                        arrayValues[i-ientrySOR] = (Float_t)v->GetInt();
                }
                aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
+               delete [] arrayValues;
+               delete [] arrayWeights;
        }
        else if (iCountsRun == 1){
                AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
@@ -2052,6 +2191,8 @@ Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
                        AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
                        AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
                        aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
+                       delete [] arrayValues;
+                       delete [] arrayWeights;
                }
                else{
                        AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
@@ -2080,6 +2221,8 @@ Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
                        AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
                        AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
                        aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
+                       delete [] arrayValues;
+                       delete [] arrayWeights;
                }
        }
 
@@ -2191,6 +2334,8 @@ Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
                        arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
                        arrayValues[i-ientrySOR] = (Float_t)v->GetUInt();
                }
+               delete [] arrayValues;
+               delete [] arrayWeights;
                aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
        }
        else if (iCountsRun == 1){
@@ -2207,6 +2352,8 @@ Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
                        AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
                        AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
                        aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
+                       delete [] arrayValues;
+                       delete [] arrayWeights;
                }
                else{
                        AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
@@ -2235,6 +2382,8 @@ Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
                        AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
                        AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
                        aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
+                       delete [] arrayValues;
+                       delete [] arrayWeights;
                }
        }
 
@@ -2309,7 +2458,7 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        
        // main logbook
        TString sqlQuery;
-       sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent FROM logbook WHERE run = %d", run);
+       sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent,beamType FROM logbook WHERE run = %d", run);
        TSQLResult* result = server->Query(sqlQuery);
        if (!result)
                {
@@ -2337,12 +2486,16 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        TString detectorMaskString(row->GetField(2));
        TString l3CurrentString(row->GetField(3));
        TString dipoleCurrentString(row->GetField(4));
+       TString beamTypeString(row->GetField(5));
        time_t timeStart = (time_t)(timeStartString.Atoi());
        UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
        Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
        Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
        Char_t l3Polarity = (l3CurrentString.Atof() < 0) ? 1 : 0;
        Char_t dipolePolarity = (dipoleCurrentString.Atof() < 0) ? 1 : 0;
+       if (beamTypeString.CompareTo("Pb-Pb",TString::kIgnoreCase) == 0){
+               beamTypeString="A-A";
+       }
        
        AliGRPObject * grpObj = new AliGRPObject();
        grpObj->SetTimeStart(timeStart); 
@@ -2353,6 +2506,7 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        grpObj->SetL3Polarity(l3Polarity);
        grpObj->SetDipolePolarity(dipolePolarity);
        grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
+       grpObj->SetBeamType(beamTypeString);
 
        delete row;
        row = 0;
@@ -2513,239 +2667,6 @@ Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* db
        
        return lastRun;
 }
-//-----------------------------------------------------------------
-Double_t AliGRPPreprocessor::CalculateMean(TObjArray* const 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* const array, Double_t timeStart){
 
@@ -2791,7 +2712,7 @@ Float_t AliGRPPreprocessor::ProcessEnergy(TObjArray* const array, Double_t timeS
        return energy;
 }
 //------------------------------------------------------------------------------------------------------
-AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase,TObjArray *beam2phase, Double_t timeStart, Double_t timeEnd)
+AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase,TObjArray *beam2phase, Double_t timeEnd)
 {
   //
   // Method to process LHC-Clock Phase data
@@ -2800,6 +2721,10 @@ AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase
   AliLHCClockPhase *phaseObj = new AliLHCClockPhase;
 
   Bool_t foundBeam1Phase = kFALSE, foundBeam2Phase = kFALSE;
+  const Float_t threshold = 0.050; // we store the measurement only in case they differ with more 50ps from the previous one 
+
+  TString timeCreatedStr = GetRunParameter("time_created");
+  Double_t timeCreated = timeCreatedStr.Atof();
 
   Int_t nCounts = beam1phase->GetEntries();
   AliDebug(2,Form("Beam1 phase measurements = %d\n",nCounts));
@@ -2809,14 +2734,21 @@ AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase
     return NULL;
   }
   else{
+    Double_t prevPhase = 0;
     for (Int_t i = 0; i < nCounts; i++){
       AliDCSArray *dcs = (AliDCSArray*)beam1phase->At(i);
       if (dcs){
-       if (dcs->GetTimeStamp()>=timeStart && dcs->GetTimeStamp()<=timeEnd) {
-         foundBeam1Phase = kTRUE;
-         AliInfo(Form("Beam1 Clock Phase = %f at timestamp = %u",
-                      (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
-         phaseObj->AddPhaseB1DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
+             //if (dcs->GetTimeStamp()>=timeStart && dcs->GetTimeStamp()<=timeEnd) {
+             if (dcs->GetTimeStamp()>=timeCreated && dcs->GetTimeStamp()<=timeEnd) {
+         if ((i == 0) || (i == (nCounts-1)) ||
+             !foundBeam1Phase ||
+             (TMath::Abs(dcs->GetDouble(0)-prevPhase) > threshold)) {
+           prevPhase = dcs->GetDouble(0);
+           foundBeam1Phase = kTRUE;
+           AliInfo(Form("B1 Clk Phase = %f at TS = %f",
+                        (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
+           phaseObj->AddPhaseB1DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
+         }
        }
       }
     }
@@ -2835,14 +2767,20 @@ AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase
     return NULL;
   }
   else{
+    Double_t prevPhase = 0;
     for (Int_t i = 0; i < nCounts; i++){
       AliDCSArray *dcs = (AliDCSArray*)beam2phase->At(i);
       if (dcs){
-       if (dcs->GetTimeStamp()>=timeStart && dcs->GetTimeStamp()<=timeEnd) {
-         foundBeam2Phase = kTRUE;
-         AliInfo(Form("Beam2 Clock Phase = %f at timestamp = %u",
-                      (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
-         phaseObj->AddPhaseB2DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
+       if (dcs->GetTimeStamp()>=timeCreated && dcs->GetTimeStamp()<=timeEnd) {
+         if ((i == 0) || (i == (nCounts-1)) ||
+             !foundBeam2Phase ||
+             (TMath::Abs(dcs->GetDouble(0)-prevPhase) > threshold)) {
+           prevPhase = dcs->GetDouble(0);
+           foundBeam2Phase = kTRUE;
+           AliInfo(Form("B2 Clk Phase = %f at TS = %f",
+                        (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
+           phaseObj->AddPhaseB2DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
+         }
        }
       }
     }