-//-----------------------------------------------------------------
-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]);
-}