]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliGRPPreprocessor.cxx
Fix for report #64208 Missing OCDB (GRP/Calib/MeanVertex) object in GRP
[u/mrichter/AliRoot.git] / STEER / AliGRPPreprocessor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //                          Class AliGRPPreprocessor
20 //                  Global Run Parameters (GRP) preprocessor
21 //    Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
22 //    Modified: Ernesto.Lopez.Torres@cern.ch  CEADEN-CERN
23 //    Modified: Chiara.Zampolli@cern.ch  CERN
24 //-------------------------------------------------------------------------
25
26 #include <TChain.h>
27 #include <TList.h>
28 #include <TMap.h>
29 #include <TObjString.h>
30 #include <TObjArray.h>
31 #include <TGraph.h>
32 #include <TString.h>
33 #include <TFile.h>
34
35 #include <float.h>
36
37 #include "AliGRPPreprocessor.h"
38 #include "AliGRPObject.h"
39 #include "AliDCSSensor.h"
40 #include "AliSplineFit.h"
41 #include "AliDCSSensorArray.h"
42 #include "AliRawEventHeaderVersions.h"
43
44 #include "AliTriggerConfiguration.h"
45 #include "AliTriggerRunScalers.h"
46
47 #include "AliCDBMetaData.h"
48 #include "AliLog.h"
49 #include "AliESDVertex.h"
50 #include "AliLHCReader.h"
51 #include "AliLHCData.h"
52 #include "AliDCSArray.h"
53
54 class AliDCSValue;
55 class AliShuttleInterface;
56
57 // needed for ReceivePromptRecoParameters
58
59 #include <TSQLServer.h>
60 #include <TSQLResult.h>
61 #include <TSQLRow.h>
62 #include <AliCDBManager.h>
63 #include <AliCDBMetaData.h>
64 #include <AliCDBId.h>
65 #include <AliTriggerConfiguration.h>
66 #include <AliCTPTimeParams.h>
67
68 const Double_t kFitFraction = -1.;                 // Fraction of DCS sensor fits required
69
70 ClassImp(AliGRPPreprocessor)
71
72 //_______________________________________________________________
73
74   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 8; // num parameters in the logbook for PHYSICS runs, when beamType from DAQ logbook == NULL
75   const Int_t AliGRPPreprocessor::fgknDAQLbParReduced = 7; // num parameters in the logbook for the other cases
76   const Int_t AliGRPPreprocessor::fgknDCSDP = 48;   // number of dcs dps
77   const Int_t AliGRPPreprocessor::fgknDCSDPHallProbes = 40;   // number of dcs dps
78   const Int_t AliGRPPreprocessor::fgknLHCDP = 5;   // number of dcs dps from LHC data
79   const Int_t AliGRPPreprocessor::fgkDCSDPHallTopShift = 4;   // shift from the top to get tp the Hall Probes names in the list of DCS DPs
80   const Int_t AliGRPPreprocessor::fgkDCSDPNonWorking = 5; // number of non working DCS DPs
81   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
82                    "L3Polarity",
83                    "DipolePolarity",
84                    "L3Current",
85                    "DipoleCurrent",
86                    "L3_BSF17_H1",
87                    "L3_BSF17_H2",
88                    "L3_BSF17_H3",
89                    "L3_BSF17_Temperature",
90                    "L3_BSF4_H1",
91                    "L3_BSF4_H2",
92                    "L3_BSF4_H3",
93                    "L3_BSF4_Temperature",
94                    "L3_BKF17_H1",
95                    "L3_BKF17_H2",
96                    "L3_BKF17_H3",
97                    "L3_BKF17_Temperature",
98                    "L3_BKF4_H1",
99                    "L3_BKF4_H2",
100                    "L3_BKF4_H3",
101                    "L3_BKF4_Temperature",
102                    "L3_BSF13_H1",
103                    "L3_BSF13_H2",
104                    "L3_BSF13_H3",
105                    "L3_BSF13_Temperature",
106                    "L3_BSF8_H1",
107                    "L3_BSF8_H2",
108                    "L3_BSF8_H3",
109                    "L3_BSF8_Temperature",
110                    "L3_BKF13_H1",
111                    "L3_BKF13_H2",
112                    "L3_BKF13_H3",
113                    "L3_BKF13_Temperature",
114                    "L3_BKF8_H1",
115                    "L3_BKF8_H2",
116                    "L3_BKF8_H3",
117                    "L3_BKF8_Temperature",
118                    "Dipole_Inside_H1",
119                    "Dipole_Inside_H2",
120                    "Dipole_Inside_H3",
121                    "Dipole_Inside_Temperature",
122                    "Dipole_Outside_H1",
123                    "Dipole_Outside_H2",
124                    "Dipole_Outside_H3",
125                    "Dipole_Outside_Temperature",
126                    "CavernTemperature",
127                    "CavernAtmosPressure",
128                    "SurfaceAtmosPressure",
129                    "CavernAtmosPressure2"
130                  };
131
132   const char* AliGRPPreprocessor::fgkDCSDataPointsHallProbes[AliGRPPreprocessor::fgknDCSDPHallProbes] = {
133                    "L3_BSF17_H1",
134                    "L3_BSF17_H2",
135                    "L3_BSF17_H3",
136                    "L3_BSF17_Temperature",
137                    "L3_BSF4_H1",
138                    "L3_BSF4_H2",
139                    "L3_BSF4_H3",
140                    "L3_BSF4_Temperature",
141                    "L3_BKF17_H1",
142                    "L3_BKF17_H2",
143                    "L3_BKF17_H3",
144                    "L3_BKF17_Temperature",
145                    "L3_BKF4_H1",
146                    "L3_BKF4_H2",
147                    "L3_BKF4_H3",
148                    "L3_BKF4_Temperature",
149                    "L3_BSF13_H1",
150                    "L3_BSF13_H2",
151                    "L3_BSF13_H3",
152                    "L3_BSF13_Temperature",
153                    "L3_BSF8_H1",
154                    "L3_BSF8_H2",
155                    "L3_BSF8_H3",
156                    "L3_BSF8_Temperature",
157                    "L3_BKF13_H1",
158                    "L3_BKF13_H2",
159                    "L3_BKF13_H3",
160                    "L3_BKF13_Temperature",
161                    "L3_BKF8_H1",
162                    "L3_BKF8_H2",
163                    "L3_BKF8_H3",
164                    "L3_BKF8_Temperature",
165                    "Dipole_Inside_H1",
166                    "Dipole_Inside_H2",
167                    "Dipole_Inside_H3",
168                    "Dipole_Inside_Temperature",
169                    "Dipole_Outside_H1",
170                    "Dipole_Outside_H2",
171                    "Dipole_Outside_H3",
172                    "Dipole_Outside_Temperature"
173                  };
174                  
175   const Short_t kSensors = 45; // start index position of sensor in DCS DPs
176   const Short_t kNumSensors = 3; // Number of sensors in DCS DPs (CavernAtmosPressure, SurfaceAtmosPressure, CavernAtmosPressure2)
177
178
179   const char* AliGRPPreprocessor::fgkLHCDataPoints[AliGRPPreprocessor::fgknLHCDP] = {
180           "LHC_Beam_Energy",
181           "LHC_MachineMode",
182           "LHC_BeamMode",
183           "dip/acc/LHC/RunControl/BeamType/Beam1.payload",
184           "dip/acc/LHC/RunControl/BeamType/Beam2.payload"
185   };
186   const char* kppError[] = {
187                    "",
188                    "(DAQ logbook ERROR)",
189                    "(DAQ FXS ERROR)",
190                    "(Trigger Scalers not found in DCS FXS - ERROR)",
191                    "(DCS data points ERROR)",
192                    "(Trigger Configuration ERROR)",
193                    "(DAQ logbook ERROR determining partition of the run)",
194                    "(CTP timing ERROR)",
195                    "(SPD Mean Vertex ERROR)",
196                    "(DCS FXS Error for LHC Data)",
197                    "(LHC Data Error)"
198   };
199
200 //_______________________________________________________________
201
202 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
203         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))
204 {
205         // constructor - shuttle must be instantiated!
206
207         AddRunType("COSMIC");
208         AddRunType("LASER");
209         AddRunType("PHYSICS");
210         AddRunType("CALIBRATION_BC");
211         AddRunType("CALIBRATION_CENTRAL");
212         AddRunType("CALIBRATION_EMD");
213         AddRunType("CALIBRATION_MB");
214         AddRunType("CALIBRATION_SEMICENTRAL");
215         AddRunType("CALIBRATION");
216         AddRunType("PEDESTAL");
217         AddRunType("STANDALONE");
218         AddRunType("GAIN");
219         AddRunType("NOISE");
220         AddRunType("PULSER");
221         AddRunType("STANDALONE_PULSER");
222         AddRunType("STANDALONE_BC");
223
224         fmaxFloat = FLT_MAX;
225         fminFloat = -FLT_MAX;
226         fmaxDouble = DBL_MAX;
227         fminDouble = -DBL_MAX;
228         fmaxInt = kMaxInt;
229         fminInt = kMinInt;
230         fmaxUInt = kMaxUInt;
231         fminUInt = 0;
232
233         AliInfo(Form("Max allowed float = %6.5e",fmaxFloat));
234         AliInfo(Form("Min allowed float = %6.5e",fminFloat));
235         AliInfo(Form("Max allowed double = %6.5e",fmaxDouble));
236         AliInfo(Form("Min allowed double = %6.5e",fminDouble));
237         AliInfo(Form("Max allowed integer = %d",fmaxInt));
238         AliInfo(Form("Min allowed integer = %d",fminInt));
239         AliInfo(Form("Max allowed unsigned integer = %u",(Int_t)fmaxUInt));
240         AliInfo(Form("Min allowed unsigned integer = %u",(Int_t)fminUInt));
241
242         ffailedDPs->SetOwner(kTRUE);
243 }
244
245 //_______________________________________________________________
246
247 AliGRPPreprocessor::~AliGRPPreprocessor()
248 {
249         //destructor
250         
251         delete fPressure;
252         delete ffailedDPs;
253
254 }
255
256 //_______________________________________________________________
257
258 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
259 {
260         // Initialize preprocessor
261
262         AliPreprocessor::Initialize(run, startTime, endTime);
263         
264         AliInfo("Initialization of the GRP preprocessor.");
265         AliInfo(Form("Start Time DCS = %d",GetStartTimeDCSQuery()));
266         AliInfo(Form("End Time DCS = %d",GetEndTimeDCSQuery()));
267         TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
268         for(Int_t j = 0; j < kNumSensors; j++) {
269                 AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
270                 sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
271         }
272         AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
273         
274         fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
275
276         for (Int_t iDP=0; iDP < fgknDCSDP; iDP++){
277                 TObjString* dp = new TObjString(fgkDCSDataPoints[iDP]);
278                 ffailedDPs->AddAt(dp,iDP);
279         }
280
281 }
282
283 //_______________________________________________________________
284
285 UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
286 {
287         // process data retrieved by the Shuttle
288         
289         // retrieving "partition" and "detector" fields from DAQ logbook to 
290         // determine the partition in which the run was taken
291         // the partition is used to decide how to react in case of errors for CTP
292
293         TString partition = (TString)GetRunParameter("partition");  
294         TString detector = (TString)GetRunParameter("detector");   
295
296         AliGRPObject *grpobj = new AliGRPObject();  // object to store data
297         grpobj->SetBeamEnergyIsSqrtSHalfGeV(); // new format
298
299         //=================//
300         // DAQ logbook     //
301         //=================//
302         UInt_t error = 0;
303         
304         Int_t iDaqLB = ProcessDaqLB(grpobj);
305         TString runType = (TString)GetRunType();
306         TString beamType = (TString)GetRunParameter("beamType");
307         //if((runType == "PHYSICS" && iDaqLB == fgknDAQLbPar && beamType!="Cosmics") ||  (runType == "PHYSICS" && iDaqLB == fgknDAQLbParReduced && beamType=="Cosmics") || (runType != "PHYSICS" && iDaqLB == fgknDAQLbParReduced)) {
308         if((runType == "PHYSICS" && iDaqLB == fgknDAQLbPar && !beamType.IsNull()) ||  (runType == "PHYSICS" && iDaqLB == fgknDAQLbParReduced && beamType.IsNull()) || (runType != "PHYSICS" && iDaqLB == fgknDAQLbParReduced)) {
309                 Log(Form("DAQ Logbook, successful!"));
310         } else {
311                 Log(Form("DAQ Logbook, could not get all expected entries!!!"));
312                 error |= 1;
313         }
314
315         //=================//
316         // DAQ FXS         //
317         //=================//
318         UInt_t iDaqFxs = ProcessDaqFxs();
319         if( iDaqFxs == 0 ) {
320                 Log(Form("DAQ FXS, successful!"));
321         } else {
322                 Log(Form("DAQ FXS, could not store run raw tag file!!!"));
323                 error |= 2;
324         }
325         
326         //=================//
327         // DCS FXS         //
328         //=================//
329         UInt_t iDcsFxs = ProcessDcsFxs(partition, detector);
330         if( iDcsFxs == 0 ) {
331                 Log(Form("DCS FXS, successful!"));
332         } else  if (iDcsFxs ==1) {
333                 Log(Form("DCS FXS, Could not store CTP scalers!!!"));
334                 error |= 4;
335         } else{
336                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without CTP scalers...",partition.Data(),detector.Data()));
337                 error |= 32;
338         }
339         
340         //=================//
341         // DCS data points //
342         //=================//
343         Log(Form("Starting DCS Query at %d and finishing at %d",GetStartTimeDCSQuery(),GetEndTimeDCSQuery()));
344         Int_t entries = ProcessDcsDPs( valueMap, grpobj );
345         Log(Form("entries found = %d (should be %d)",entries, fgknDCSDP-fgkDCSDPNonWorking));
346         if (fdaqStartEndTimeOk){
347                 if( entries < fgknDCSDP - fgkDCSDPNonWorking ) { // L3_BSF4_H3, L3_BSF17_H1, L3_BSF17_H2, L3_BSF17_H3, L3_BSF17_Temperature are not working yet...  
348                         Log(Form("Possible problem with the DCS data points!!! Only %d/%d entries found - Please read further for more details",entries,fgknDCSDP-fgkDCSDPNonWorking));
349                         Log(Form("The DPs giving problems were:"));
350                         for (Int_t iDP = 0; iDP < fgknDCSDP; iDP++){
351                                 TObjString *dpString = (TObjString*)ffailedDPs->At(iDP);
352                                 if (dpString){
353                                         TString name = dpString->String();
354                                         if (name != "L3_BSF4_H3" && name != "L3_BSF17_H1" && name != "L3_BSF17_H2" && name != "L3_BSF17_H3" && name != "L3_BSF17_Temperature" ){
355                                                 Log(Form("******** %s ******** not present, but foreseen --> causing an ERROR",name.Data()));
356                                         }
357                                         else {
358                                                 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()));
359                                         }
360                                 }
361                         }
362                         error |= 8;
363                 }
364                 else  Log(Form("DCS data points, successful!"));
365         }
366         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")); 
367         
368         //=======================//
369         // Trigger Configuration //
370         //=======================//
371
372         const char * triggerConf = GetTriggerConfiguration();
373
374         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
375                 Log("STANDALONE partition for current run, using Trigger Configuration dummy value");
376                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyConfig");
377                 if (!cdbEntry) {
378                         Log(Form("No dummy CTP configuration entry found, going into error..."));
379                         error |= 16;
380                 }
381                 else{
382                         AliTriggerConfiguration *runcfg = (AliTriggerConfiguration*)cdbEntry->GetObject();
383                         if (!runcfg){
384                                 Log(Form("dummy CTP config not found in OCDB entry, going into error..."));
385                                 error |= 16;
386                         }
387                         else {
388                                 TString titleCTPcfg = Form("CTP cfg for run %i from Dummy entry in OCDB",fRun);
389                                 runcfg->SetTitle(titleCTPcfg);
390                                 AliCDBMetaData metaData;
391                                 metaData.SetResponsible("Roman Lietava");
392                                 metaData.SetComment("CTP run configuration from dummy entry in OCDB");
393                                 if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
394                                         Log("Unable to store the dummy CTP run configuration object to OCDB!");
395                                         error |= 16;
396                                 }
397                         }
398                 }
399         }
400
401         else if (!partition.IsNull() && detector.IsNull()){ // global partition
402                 Log("GLOBAL partition for current run, using Trigger Configuration from DAQ Logbook");
403                 if (triggerConf!= NULL) {
404                         Log("Found trigger configuration in DAQ logbook");
405                         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConf);      
406                         if (!runcfg) {
407                                 Log("Bad CTP run configuration file from DAQ logbook! The corresponding CDB entry will not be filled!");
408                                 error |= 16;
409                         }
410                         else {
411                                 TString titleCTPcfg = Form("CTP cfg for run %i from DAQ",fRun);
412                                 runcfg->SetTitle(titleCTPcfg);
413                                 AliCDBMetaData metaData;
414                                 metaData.SetBeamPeriod(0);
415                                 metaData.SetResponsible("Roman Lietava");
416                                 metaData.SetComment("CTP run configuration from DAQ logbook");
417                                 if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
418                                         Log("Unable to store the CTP run configuration object to OCDB!");
419                                         error |= 16;
420                                 }
421                         }
422                 }
423
424                 else {
425                         Log("Trigger configuration NULL in DAQ logbook");
426                         error |= 16;
427                 }
428         }
429
430         else {
431                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger configuration...",partition.Data(),detector.Data()));
432                 error |= 32;
433         }
434
435         //===========================//
436         // Trigger Timing Parameters //
437         //===========================//
438
439         
440         const char * triggerCTPtiming = GetCTPTimeParams();
441
442         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
443                 Log("STANDALONE partition for current run, using CTP timing params dummy value");
444                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyCTPtime");
445                 if (!cdbEntry) {
446                         Log(Form("No dummy CTP timing parameters entry found, going into error..."));
447                         error |= 64;
448                 }
449                 else{
450                         AliCTPTimeParams *runCTPtiming = (AliCTPTimeParams*)cdbEntry->GetObject();
451                         if (!runCTPtiming){
452                                 Log(Form("dummy CTP timing parameters not found in OCDB entry, going into error..."));
453                                 error |= 64;
454                         }
455                         else {
456                                 TString titleCTPtiming = Form("CTP timing params for run %i from Dummy entry in OCDB",fRun);
457                                 runCTPtiming->SetTitle(titleCTPtiming);
458                                 AliCDBMetaData metadata;
459                                 metadata.SetResponsible("Roman Lietava");
460                                 metadata.SetComment("CTP run timing parameters from dummy entry in OCDB");
461                                 if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
462                                         Log("Unable to store the dummy CTP timing params object to OCDB!");
463                                         error |= 64;
464                                 }
465                         }
466                 }
467         }
468
469         else if (!partition.IsNull() && detector.IsNull()){ // global partition
470                 Log("GLOBAL partition for current run, using Trigger Timing Parameters from DAQ Logbook");
471                 if (triggerCTPtiming!= NULL) {
472                         Log("Found trigger timing params in DAQ logbook");
473                         AliDebug(2,Form("%s",triggerCTPtiming));
474                         AliCTPTimeParams *runCTPtiming = AliCTPTimeParams::LoadCTPTimeParamsFromString(triggerCTPtiming);         
475                         if (!runCTPtiming) {
476                                 Log("Bad CTP trigger timing params file from DAQ logbook! The corresponding CDB entry will not be filled!");
477                                 error |= 64;
478                         }
479                         else {
480                                 TString titleCTPtiming = Form("CTP timing params for run %i from DAQ",fRun);
481                                 runCTPtiming->SetTitle(titleCTPtiming);
482                                 AliCDBMetaData metadata;
483                                 metadata.SetBeamPeriod(0);
484                                 metadata.SetResponsible("Roman Lietava");
485                                 metadata.SetComment("CTP timing params from DAQ logbook");
486                                 if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
487                                         Log("Unable to store the CTP timing params object to OCDB!");
488                                         error |= 64;
489                                 }
490                         }
491                 }
492
493                 else {
494                         Log("Trigger timing params NULL in DAQ logbook");
495                         error |= 64;
496                 }
497         }
498
499         else {
500                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger timing parameters...",partition.Data(),detector.Data()));
501                 error |= 32;
502         }
503
504
505         //=================//
506         // LHC Data        //
507         //=================//
508
509         UInt_t iLHCData = ProcessLHCData(grpobj);
510
511         if( iLHCData == 0 ) {
512                 Log(Form("LHC Data from DCS FXS, successful!"));
513         } else  if (iLHCData == 1) {
514                 Log(Form("LHC Data, problems with DCS FXS!"));
515                 error |= 256;
516         } else if (iLHCData ==3){
517                 Log(Form("Problems in storing LHC Data - but not going into Error"));
518         } else if (iLHCData ==4){
519                 Log(Form("Problems with LHC Data to be put in /GRP/GRP/LHCData - but not going into Error"));
520         } else{
521                 Log(Form("LHC Data problems"));
522                 error |= 512;
523         }
524         
525         //==================//
526         // SPD Mean Vertex  //
527         //==================//
528         if (runType == "PHYSICS"){
529                 UInt_t iSPDMeanVertex = ProcessSPDMeanVertex();
530                 if( iSPDMeanVertex == 1 ) {
531                         Log(Form("SPD Mean Vertex, successful!"));
532                 } else {
533                         Log(Form("SPD Mean Vertex failed!!!"));
534                         error |= 128; 
535                 }
536         }
537         else {
538                 Log("SPD Mean Vertex not processed since runType != PHYSICS");
539         }
540
541         // storing AliGRPObject in OCDB
542
543         AliCDBMetaData md;
544         md.SetResponsible("Chiara Zampolli");
545         md.SetComment("Output parameters from the GRP preprocessor.");
546         
547         Bool_t result = kTRUE;
548         result = Store("GRP", "Data", grpobj, &md); 
549         delete grpobj;
550
551         if (result && !error ) {
552                 Log("GRP Preprocessor Success");
553                 return 0;
554         } else {
555                 Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s%s%s%s%s",
556                           kppError[(error&1)?1:0],
557                           kppError[(error&2)?2:0],
558                           kppError[(error&4)?3:0],
559                           kppError[(error&8)?4:0],
560                           kppError[(error&16)?5:0],
561                           kppError[(error&32)?6:0],
562                           kppError[(error&64)?7:0],
563                           kppError[(error&128)?8:0],
564                           kppError[(error&256)?9:0],
565                           kppError[(error&512)?10:0]
566                           ));
567                 return error;
568         }
569
570
571 }
572
573 //_______________________________________________________________
574
575 UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
576 {
577         //
578         //Getting the LHC Data from DCS FXS
579         //
580
581         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
582         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
583         if (timeStartString.IsNull() || timeEndString.IsNull()){
584                 if (timeStartString.IsNull()){ 
585                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
586                 }
587                 else if (timeEndString.IsNull()){
588                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
589                 }
590                 return 2;
591         }  
592
593         Double_t timeStart = timeStartString.Atof();
594         Double_t timeEnd = timeEndString.Atof();
595
596         TString fileName = GetFile(kDCS, "LHCData","");
597         if (fileName.Length()>0){
598                 AliInfo("Got The LHC Data file");
599                 AliLHCReader lhcReader;
600                 TMap* lhcMap = (TMap*)lhcReader.ReadLHCDP(fileName.Data());
601                 if (lhcMap) {
602                         Log(Form("LHCData map entries = %d",lhcMap->GetEntries()));
603                         
604                         // Processing data to be put in AliGRPObject
605                         // Energy
606                         TObjArray* energyArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[0]);
607                         if (energyArray){                       
608                                 Float_t energy = ProcessEnergy(energyArray,timeStart,timeEnd);
609                                 if (energy != -1) {
610                                         grpobj->SetBeamEnergy(energy);
611                                         grpobj->SetBeamEnergyIsSqrtSHalfGeV(kTRUE);
612                                 }
613                         }
614                         else {
615                                 AliError("Energy not found in LHC Data file!!!");
616                         }       
617                         Double_t timeBeamMode = 0;
618                         Double_t timeMachineMode = 0;
619                         Bool_t flagBeamMode = kFALSE;  //flag set true if at least one BeamMode measurement is found within DAQ_time_start and DAQ_time_end
620                         Bool_t flagMachineMode = kFALSE;  //flag set true if at least one MachineMode measurement is found within DAQ_time_start and DAQ_time_end
621                         // BeamMode
622                         TObjArray* beamModeArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[2]);
623                         Int_t nBeamMode = -1;
624                         if (beamModeArray){     
625                                 nBeamMode = beamModeArray->GetEntries();        
626                                 if (nBeamMode==0){
627                                         AliInfo("Found zero entries for  the Beam Mode, setting it to UNKNOWN");
628                                         grpobj->SetLHCState("UNKNOWN");
629                                 }
630                                 else{
631                                         if (nBeamMode==1){
632                                                 AliDCSArray* beamMode = (AliDCSArray*)beamModeArray->At(0);
633                                                 if (beamMode->GetTimeStamp()>=timeStart && beamMode->GetTimeStamp()<=timeEnd){
634                                                         TObjString* beamModeString = beamMode->GetStringArray(0);
635                                                         AliInfo(Form("LHC State (corresponding to BeamMode) = %s",(beamModeString->String()).Data()));
636                                                         grpobj->SetLHCState(beamModeString->String());
637                                                 }
638                                                 else{
639                                                         AliInfo("No Beam Mode found within DAQ_time_start and DAQ_time_end, setting it to UNKNOWN");
640                                                         grpobj->SetLHCState("UNKNOWN");
641                                                 }
642                                         }
643                                         else {
644                                                 for (Int_t iBeamMode = 0; iBeamMode<nBeamMode; iBeamMode++){
645                                                         AliDCSArray* beamMode = (AliDCSArray*)beamModeArray->At(iBeamMode);
646                                                         if (beamMode->GetTimeStamp()>=timeStart && beamMode->GetTimeStamp()<=timeEnd){
647                                                                 AliDCSArray* beamMode1 = (AliDCSArray*)beamModeArray->At(iBeamMode+1);
648                                                                 if (beamMode1->GetTimeStamp()>=timeStart && beamMode1->GetTimeStamp()<=timeEnd){
649                                                                         timeBeamMode = beamMode1->GetTimeStamp();
650                                                                         AliWarning(Form("The beam mode changed at timestamp %f! Setting it to the first value found and keeping track of the time of the change to set MaxTimeLHCValidity afterward",timeBeamMode));
651                                                                 }
652                                                                 TObjString* beamModeString = beamMode->GetStringArray(0);
653                                                                 AliInfo(Form("LHC State (corresponding to BeamMode) = %s",(beamModeString->String()).Data()));
654                                                                 grpobj->SetLHCState(beamModeString->String());
655                                                                 flagBeamMode = kTRUE;
656                                                                 break;
657                                                         }
658                                                 }
659                                                 if (!flagBeamMode){
660                                                         AliError("Found values for BeamMode, but none within DAQ_time_start and DAQ_time_end, setting BeamMode to UNKNOWN");
661                                                         grpobj->SetLHCState("UNKONWN");
662                                                 }
663                                         }
664                                 }
665                         }
666                         else{
667                                 AliError("Beam mode array not found in LHC Data file!!!");
668                         }
669
670                         // MachineMode
671                         TObjArray* machineModeArray = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[1]);
672                         Int_t nMachineMode = -1;
673                         if (machineModeArray){
674                                 nMachineMode = machineModeArray->GetEntries();
675                                 if (nMachineMode==0){
676                                         AliInfo("No Machine Mode found, setting it to UNKNOWN");
677                                         grpobj->SetMachineMode("UNKNOWN");
678                                 }
679                                 else{
680                                         if (nMachineMode ==1) {
681                                                 AliDCSArray* machineMode = (AliDCSArray*)machineModeArray->At(0);
682                                                 if (machineMode->GetTimeStamp()>=timeStart && machineMode->GetTimeStamp()<=timeEnd){
683                                                         TObjString* machineModeString = machineMode->GetStringArray(0);
684                                                         AliInfo(Form("Machine Mode = %s",(machineModeString->String()).Data()));
685                                                         grpobj->SetMachineMode(machineModeString->String());
686                                                 }
687                                                 else{
688                                                         AliInfo("No Machine Mode found within DAQ_time_start and DAQ_time_end, setting it to UNKNOWN");
689                                                         grpobj->SetMachineMode("UNKNOWN");
690                                                 }
691                                         }
692                                         else {
693                                                 for (Int_t iMachineMode = 0; iMachineMode<nMachineMode; iMachineMode++){
694                                                         AliDCSArray* machineMode = (AliDCSArray*)machineModeArray->At(iMachineMode);
695                                                         if (machineMode->GetTimeStamp()>=timeStart && machineMode->GetTimeStamp()<=timeEnd){
696                                                                 AliDCSArray* machineMode1 = (AliDCSArray*)machineModeArray->At(iMachineMode+1);
697                                                                 if (machineMode1->GetTimeStamp()>=timeStart && machineMode1->GetTimeStamp()<=timeEnd){
698                                                                         timeMachineMode = machineMode1->GetTimeStamp();
699                                                                         AliWarning(Form("The machine mode changed at timestamp %f! Setting it to the first value found and keeping track of the time of the change to set MaxTimeLHCValidity afterwards",timeMachineMode));
700                                                                 }
701                                                                 TObjString* machineModeString = machineMode->GetStringArray(0);
702                                                                 AliInfo(Form("Machine mode = %s",(machineModeString->String()).Data()));
703                                                                 grpobj->SetMachineMode(machineModeString->String());
704                                                                 flagMachineMode = kTRUE;
705                                                                 break;
706                                                         }
707                                                 }
708                                                 if (!flagMachineMode){
709                                                         AliError("Found values for MachineMode, but none within DAQ_time_start and DAQ_time_end, setting MachineMode to UNKNOWN");
710                                                         grpobj->SetMachineMode("UNKONWN");
711                                                 }
712                                         }
713                                 }
714                         }
715                         else {
716                                 AliError("Machine mode array not found in LHC Data file!!!");
717                         }       
718
719                         // Setting minTimeLHCValidity
720                         if (timeBeamMode!=0 || timeMachineMode!=0){
721                                 Double_t minTimeLHCValidity;
722                                 if (timeBeamMode == 0){
723                                         minTimeLHCValidity = timeMachineMode;
724                                 }
725                                 else if (timeMachineMode == 0){
726                                         minTimeLHCValidity = timeBeamMode;
727                                 }
728                                 else {
729                                         minTimeLHCValidity= TMath::Min(timeBeamMode,timeMachineMode);
730                                 }
731                                 AliWarning(Form("Setting MaxTimeLHCValidity to %f",minTimeLHCValidity));
732                                 grpobj->SetMaxTimeLHCValidity(minTimeLHCValidity);
733                         }
734                         
735                         // BeamType1 and BeamType2 
736                         TObjArray* beam1Array = (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[3]);
737                         if (beam1Array){                        
738                                 AliInfo(Form("%d entries for Beam1",beam1Array->GetEntries()));
739                         }
740                         else{
741                                 AliError("Beam1 array not found in LHC data file!!!");
742                         }                       
743                         TObjArray* beam2Array = 
744                                 (TObjArray*)lhcMap->GetValue(fgkLHCDataPoints[4]);
745                         if (beam2Array){
746                                 AliInfo(Form("%d entries for Beam2",beam2Array->GetEntries()));
747                         }
748                         else{
749                                 AliError("Beam2 array not found in LHC data file!!!");
750                         }       
751                         // Processing data to go to AliLHCData object
752                         AliLHCData* dt = new AliLHCData(lhcMap,timeStart,timeEnd);
753                                                         
754                         // storing AliLHCData in OCDB
755                         if (dt){                        
756                                 AliCDBMetaData md;
757                                 md.SetResponsible("Ruben Shahoyan");
758                                 md.SetComment("LHC data from the GRP preprocessor.");
759                                 
760                                 Bool_t result = kTRUE;
761                                 result = Store("GRP", "LHCData", dt, &md); 
762                                 delete dt;
763                                 if (result) return 0;
764                                 else return 3;
765                         }
766                         else return 4;
767                         delete lhcMap; 
768                 }
769                 else {
770                         AliError("Cannot read correctly LHCData file");
771                         return 2;
772                 }
773         }
774   
775         else {
776                 AliError("No LHCData file found in DCS FXS");
777                 return 1;
778         }
779
780 }
781
782 //_______________________________________________________________
783
784 UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
785 {
786         //
787         //Getting the SPD Mean Vertex
788         //
789
790         TList* list = GetForeignFileSources("SPD", kDAQ, "VertexDiamond");
791         Bool_t storeResult = kTRUE;
792         if (list !=0x0 && list->GetEntries()!=0)
793                 {
794                         AliInfo("The following sources produced files with the id VertexDiamond from SPD");
795                         list->Print();
796                         for (Int_t jj=0;jj<list->GetEntries();jj++){
797                                 TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
798                                 AliInfo(Form("found source %s", str->String().Data()));
799                                 TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
800                                 if (fileNameRun.Length()>0){
801                                         AliInfo(Form("Got the file %s", fileNameRun.Data()));
802                                         TFile daqFile(fileNameRun.Data(),"READ");
803                                         if (daqFile.IsOpen()) {
804                                                 AliESDVertex* meanVtx = dynamic_cast<AliESDVertex*>(daqFile.Get("MeanVertexPos"));
805                                                 if (meanVtx){
806                                                         meanVtx->Print();       
807                                                         // storing in the OCDB 
808                                                         AliCDBMetaData md;
809                                                         md.SetResponsible("Cvetan Cheshkov");
810                                                         md.SetComment("SPD Mean Vertex");                                       
811                                                         storeResult = Store("Calib", "MeanVertexSPD", meanVtx, &md, 0, kTRUE); 
812                                                 }
813                                                 else{
814                                                         AliWarning("No SPD Mean Vertex object found in file");
815                                                 } 
816                                         }
817                                         else {
818                                                 AliError("Can't open file");
819                                                 storeResult = kFALSE;
820                                         }
821                                 }
822                                 else{
823                                         AliWarning("No file found for current source for SPD Mean Vertex");
824                                 }
825                         }
826                 }
827         else {
828                 AliWarning("No list found for SPD Mean Vertex");
829         }
830
831         if (list) delete list;
832
833         return storeResult;
834 }
835
836
837 //_______________________________________________________________
838
839 Int_t AliGRPPreprocessor::ProcessDaqLB(AliGRPObject* grpObj)
840 {
841         //Getting the DAQ lb information
842         
843         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
844         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
845         Float_t beamEnergy = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof());
846         TString beamType = (TString)GetRunParameter("beamType");
847         Char_t numberOfDetectors = (Char_t)(((TString)GetRunParameter("numberOfDetectors")).Atoi());
848         UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
849         TString lhcPeriod = (TString)GetRunParameter("LHCperiod");
850         TString runType = (TString)GetRunType();
851
852         UInt_t nparameter = 0;
853         if (timeStart != 0){
854                 grpObj->SetTimeStart(timeStart);
855                 Log(Form("Start time for run %d: %d",fRun, (Int_t)timeStart));
856                 nparameter++;
857         } 
858         else {
859                 Log(Form("Start time not put in logbook, setting to invalid in GRP entry!"));
860         }
861
862         if (timeEnd != 0){
863                 grpObj->SetTimeEnd(timeEnd);
864                 Log(Form("End time for run %d: %i",fRun, (Int_t)timeEnd));
865                 nparameter++;
866         } 
867         else {
868                 Log(Form("End time not put in logbook, setting to invalid in GRP entry!"));
869         }
870
871         if (beamEnergy != 0){
872                 grpObj->SetBeamEnergy(beamEnergy);
873                 Log(Form("Beam Energy for run %d: %f",fRun, beamEnergy));
874                 //if ((runType == "PHYSICS" && beamType!="Cosmics")){
875                 if ((runType == "PHYSICS" && !beamType.IsNull())){   // if beamType is NOT Null, then we're not in a Cosmics run
876                         nparameter++; // increasing nparameters only in case we're in PHYSICS runs with beamType != NULL
877                 }
878         } 
879         else {
880                 //if ((runType == "PHYSICS" && beamType!="Cosmics")){
881                 if ((runType == "PHYSICS" && !beamType.IsNull())){ // if beamType is NOT Null, then we're not in a Cosmics run
882                         Log(Form("Beam Energy not put in logbook, setting to invalid in GRP entry, and producing an error (beamType = %s, runType = %s)",beamType.Data(), runType.Data()));
883                 }
884                 else{
885                         Log(Form("Beam Energy not put in logbook, setting to invalid in GRP entry, but not producing any error (beamType = NULL, runType = %s)", runType.Data()));
886                 }
887         }
888
889                 
890         if (beamType.Length() != 0){
891                 grpObj->SetBeamType(beamType);
892                 Log(Form("Beam Type for run %d: %s",fRun, beamType.Data()));
893                 nparameter++; 
894         } 
895         else {
896                 Log(Form("Beam Type not put in logbook, setting to invalid in GRP entry! Not producing any error, considering this as a Cosmics run"));
897                 nparameter++;
898         }
899                 
900         if (numberOfDetectors != 0){
901                 grpObj->SetNumberOfDetectors(numberOfDetectors);
902                 Log(Form("Number Of Detectors for run %d: %d",fRun, (Int_t)numberOfDetectors));
903                 nparameter++;
904         } 
905         else {
906                 Log(Form("Number Of Detectors not put in logbook, setting to invalid in GRP entry!"));
907         }
908
909         if (detectorMask != 0){
910                 grpObj->SetDetectorMask(detectorMask);
911                 Log(Form("Detector Mask for run %d: %d",fRun, detectorMask));
912                 nparameter++;
913         } 
914         else {
915                 Log(Form("Detector Mask not put in logbook, setting to invalid in GRP entry!"));
916         }
917
918         if (lhcPeriod.Length() != 0) {
919                 grpObj->SetLHCPeriod(lhcPeriod);
920                 Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod.Data()));
921                 nparameter++;
922         } 
923         else {
924                 Log(Form("LHCperiod not put in logbook, setting to invalid in GRP entry!"));
925         }
926         if (runType.Length() != 0) {
927                 grpObj->SetRunType(runType);
928                 Log(Form("Run Type (DAQ) for run %d: %s",fRun, runType.Data()));
929                 nparameter++;
930         } 
931         else {
932                 Log(Form("Run Type not put in logbook, setting to invalid in GRP entry!"));
933         }
934
935         return nparameter;
936 }
937
938 //_______________________________________________________________
939
940 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
941 {
942         //======DAQ FXS======//
943         
944         AliRawEventHeaderV3_9::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_9 - temporary fix 
945         TList* list = GetFileSources(kDAQ);  
946         if (!list) {
947                 Log("No raw data tag list: connection problems with DAQ FXS logbook!");
948                 return 1;
949         }
950         
951         if (list->GetEntries() == 0) {
952                 Log("no raw data tags in this run: nothing to merge!");
953                 delete  list; list=0;
954                 return 0;
955         }
956         
957         TChain *fRawTagChain = new TChain("T");
958         Int_t nFiles=0;
959         TIterator* iter = list->MakeIterator();
960         TObject* obj = 0;
961         while ((obj = iter->Next())) {
962                 TObjString* objStr = dynamic_cast<TObjString*> (obj);
963                 if (objStr) {
964                         Log(Form("Found source %s", objStr->String().Data()));
965                         TList* list2 = GetFileIDs(kDAQ, objStr->String());
966                         if (!list2) {
967                                 Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
968                                 delete fRawTagChain; fRawTagChain=0;
969                                 return 1;
970                         }
971                         Log(Form("Number of ids: %d",list2->GetEntries()));
972                         for(Int_t i = 0; i < list2->GetEntries(); i++) {
973                                 TObjString *idStr = (TObjString *)list2->At(i);
974                                 TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
975                                 if (fileName.Length() > 0) {
976                                         Log(Form("Adding file in the chain: %s",fileName.Data()));
977                                         fRawTagChain->Add(fileName.Data());
978                                         nFiles++;
979                                 } else {
980                                         Log(Form("Could not retrieve file with id %s from source %s: "
981                                                  "connection problems with DAQ FXS!",
982                                                  idStr->String().Data(), objStr->String().Data()));
983                                         delete list; list=0;
984                                         delete list2; list2=0;
985                                         delete fRawTagChain; fRawTagChain=0;
986                                         return 2;
987                                 }
988                         }
989                         delete list2;
990                 }
991         }
992         
993         TString fRawDataFileName = "GRP_Merged.tag.root";
994         Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
995         
996         if( fRawTagChain->Merge(fRawDataFileName) < 1 ) {
997                 Log("Error merging raw data files!!!");
998                 return 3;
999         }
1000         
1001         TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
1002         Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
1003         
1004         if (!result) {
1005                 Log("Problem storing raw data tags in local file!!!");
1006         } else {
1007                 Log("Raw data tags merged successfully!!");
1008         }
1009         
1010         delete iter;
1011         delete list;
1012         delete fRawTagChain; fRawTagChain=0;
1013         
1014         if (result == kFALSE) {
1015                 return 4;
1016         }
1017         
1018         return 0;
1019         
1020 }
1021
1022 //_______________________________________________________________
1023 UInt_t AliGRPPreprocessor::ProcessDcsFxs(TString partition, TString detector)
1024 {
1025
1026         // processing the info
1027         // stored in the DCS FXS
1028         // coming from the trigger
1029
1030         // Get the CTP counters information
1031
1032         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
1033                 Log("STANDALONE partition for current run, using Trigger Scalers dummy value");
1034                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyScalers");
1035                 if (!cdbEntry) {
1036                         Log(Form("No dummy CTP scalers entry found, going into error..."));
1037                         return 1;
1038                 }
1039                 else{
1040                         AliTriggerRunScalers *scalers = (AliTriggerRunScalers*)cdbEntry->GetObject();
1041                         if (!scalers){
1042                                 Log(Form("CTP dummy scalers not found in OCDB entry, going into error..."));
1043                                 return 1;
1044                         }
1045                         else {
1046                                 AliCDBMetaData metaData;
1047                                 metaData.SetResponsible("Roman Lietava");
1048                                 metaData.SetComment("CTP scalers from dummy entry in OCDB");
1049                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1050                                         Log("Unable to store the dummy CTP scalers object to OCDB!");
1051                                         return 1;
1052                                 }
1053                         }
1054                 }
1055         }
1056
1057         else if (!partition.IsNull() && detector.IsNull()){ // global partition
1058                 Log("GLOBAL partition for current run, using CTP scalers from DCS FXS");
1059                 TString countersfile = GetFile(kDCS, "CTP_xcounters","");
1060                 if (countersfile.IsNull()) {
1061                         Log("No CTP counters files has been found: empty source!");
1062                         return 1;
1063                 }
1064                 else {
1065                         Log(Form("File with Id CTP_xcounters found in DCS FXS! Copied to %s",countersfile.Data()));
1066                         AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
1067                         if (!scalers) {
1068                                 Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
1069                                 return 1;
1070                         }
1071                         else {
1072                                 AliCDBMetaData metaData;
1073                                 metaData.SetBeamPeriod(0);
1074                                 metaData.SetResponsible("Roman Lietava");
1075                                 metaData.SetComment("CTP scalers");
1076                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1077                                         Log("Unable to store the CTP scalers object to OCDB!");
1078                                         delete scalers;                                 
1079                                         return 1;
1080                                 }
1081                         }
1082                         delete scalers;
1083                 }
1084         }
1085         
1086
1087         else{   
1088                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error...",partition.Data(),detector.Data()));
1089                 return 2;
1090         }
1091
1092         return 0;
1093
1094 }
1095 //_______________________________________________________________
1096
1097 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
1098 {
1099
1100         //
1101         // processing DCS DPs
1102         //
1103
1104         Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
1105         Int_t nL3Entries = 0;
1106         Int_t nDipoleEntries = 0;
1107         Int_t nEnvEntries = 0;
1108         Int_t nHallProbesEntries = 0;
1109         nL3Entries = ProcessL3DPs(valueMap, grpObj);
1110         nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
1111         nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
1112         nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
1113         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
1114         Log(Form("L3Entries = %d, nDipoleEntries =%d, nEnvEntries = %d, nHallProbesEntries = %d", nL3Entries, nDipoleEntries, nEnvEntries, nHallProbesEntries));
1115         entries = nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
1116         return entries;
1117
1118 }
1119
1120 //_______________________________________________________________
1121
1122 Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpObj)
1123 {
1124
1125         // processing DPs
1126         // related to 
1127         // L3 info
1128
1129         Int_t nL3Entries = 0;
1130
1131         TObjArray *array = 0x0;
1132         Int_t indexDP = -1;
1133         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1134
1135         AliInfo(Form("==========L3Current==========="));
1136         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1137         indexDP = kL3Current;
1138         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1139         if(!array) {
1140                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1141         } 
1142         else {
1143                 if (array->GetEntries() == 0){
1144                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1145                 }
1146                 else {
1147                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1148                         if (floatDCS != NULL){
1149                                 grpObj->SetL3Current(floatDCS);
1150                         }
1151                         else{
1152                                 outOfRange = kTRUE;
1153                         }       
1154                         if (floatDCS){
1155                                 delete[] floatDCS;
1156                                 floatDCS = 0x0;
1157                         }
1158                 }
1159                 if (!outOfRange) {
1160                         nL3Entries++;
1161                         ffailedDPs->RemoveAt(indexDP);
1162                 }
1163         }
1164
1165         if (array) array = 0x0;
1166
1167         AliInfo(Form("==========L3Polarity==========="));
1168         indexDP = kL3Polarity;
1169         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1170         if(!array) {
1171                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1172         } 
1173         else {
1174                 if (array->GetEntries() == 0){
1175                         AliError(Form("No entries found in array! setting %s Polarity to invalid...",fgkDCSDataPoints[indexDP]));
1176                 }
1177                 else {
1178                         Bool_t change = kFALSE;
1179                         Char_t charDCS = ProcessBool(array,change);
1180                         if (change == kFALSE){
1181                                 grpObj->SetL3Polarity(charDCS);
1182                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
1183                                 ffailedDPs->RemoveAt(indexDP);
1184                                 nL3Entries++;
1185                         }
1186                         else if (isZero){
1187                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1188                                 ffailedDPs->RemoveAt(indexDP);
1189                                 nL3Entries++;
1190                         }
1191                         else {
1192                                 AliError(Form("%s value changed within the run, while the magnet was ON (according to the current), setting it to invalid and considering the DP as wrong",fgkDCSDataPoints[indexDP]));
1193                         }
1194                 }
1195         }
1196
1197         return nL3Entries;
1198
1199 }
1200 //_______________________________________________________________
1201
1202 Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* grpObj)
1203 {
1204         // processing DPs
1205         // related to 
1206         // the Dipole info
1207
1208         Int_t nDipoleEntries = 0;
1209         TObjArray *array = 0x0;
1210         Int_t indexDP = -1;
1211         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1212
1213         AliInfo(Form("==========DipoleCurrent==========="));
1214         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1215         indexDP = kDipoleCurrent;
1216         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1217         if(!array) {
1218                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1219         } 
1220         else {
1221                 if (array->GetEntries() == 0){
1222                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1223                 }
1224                 else {
1225                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1226                         if (floatDCS != NULL){
1227                                 grpObj->SetDipoleCurrent(floatDCS);
1228                         } 
1229                         else{
1230                                 outOfRange=kTRUE;
1231                         }
1232                         if (floatDCS){
1233                                 delete[] floatDCS;
1234                                 floatDCS = 0x0;
1235                         }
1236                 }
1237                 if (!outOfRange) {
1238                         nDipoleEntries++;
1239                         ffailedDPs->RemoveAt(indexDP);
1240                 }
1241         }
1242
1243         if (array) array = 0x0;
1244
1245         AliInfo(Form("==========DipolePolarity==========="));
1246         indexDP = kDipolePolarity;
1247         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1248         if(!array) {
1249                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1250         } 
1251         else {
1252                 if (array->GetEntries() == 0){
1253                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1254                 }
1255                 else {
1256                         Bool_t change = kFALSE;
1257                         Char_t charDCS = ProcessBool(array,change);
1258                         if (!change){
1259                                 grpObj->SetDipolePolarity(charDCS);
1260                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
1261                                 ffailedDPs->RemoveAt(indexDP);
1262                                 nDipoleEntries++;
1263                         }
1264                         else if (isZero){
1265                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1266                                 ffailedDPs->RemoveAt(indexDP);
1267                                 nDipoleEntries++;
1268                         }
1269                         else{
1270                                 AliError(Form("%s value changed within the run while the magnet was ON (according to the current), setting it to invalid and considering the DP as wrong", fgkDCSDataPoints[indexDP]));
1271                         }
1272                 }
1273         }
1274
1275         return nDipoleEntries;
1276
1277 }
1278 //_______________________________________________________________
1279
1280 Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
1281 {
1282         // processing DPs
1283         // related to 
1284         // evironment conditions (temperature, pressure) info
1285
1286         Int_t nEnvEntries = 0;
1287         TObjArray *array = 0x0;
1288         Int_t indexDP = -1;
1289
1290         AliInfo(Form("==========CavernTemperature==========="));
1291         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1292         indexDP = kCavernTemperature;
1293         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1294         if(!array) {
1295                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1296         } 
1297         else {
1298                 if (array->GetEntries() == 0){
1299                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1300                 }
1301                 else {
1302                         Float_t *floatDCS = ProcessFloatAll(array);
1303                         if (floatDCS != NULL){
1304                                 grpObj->SetCavernTemperature(floatDCS);
1305                         }
1306                         else{
1307                                 outOfRange = kTRUE;
1308                         }
1309                         if (floatDCS){
1310                                 delete[] floatDCS;
1311                                 floatDCS = 0x0;
1312                         }
1313                 }
1314                 if (!outOfRange) {
1315                         ffailedDPs->RemoveAt(indexDP);
1316                         nEnvEntries++;
1317                 }
1318         }
1319
1320         if (array) array = 0x0;
1321
1322         AliInfo(Form("========== AtmosPressures (Cavern + Surface + Cavern2) ==========="));
1323         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
1324         //dcsSensorArray->Print();
1325         if( fPressure->NumFits()<kNumSensors ) {
1326                 Log(Form("Check the pressure sensor values! Not all the %d pressure sensors have been fit",kNumSensors));
1327         } 
1328         Log(Form("Number of fits performed = %d",fPressure->NumFits()));
1329
1330         AliInfo(Form("==========CavernAtmosPressure==========="));
1331         indexDP = kCavernAtmosPressure;
1332         AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1333         TGraph* graph = sensorCavernP2->GetGraph();
1334         AliDebug(3,Form("index = %d",indexDP));
1335         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1336         AliDebug(2,Form("graph = %p",graph));
1337         AliDebug(3,Form("sensorCavernP2 = %p", sensorCavernP2));
1338         if(sensorCavernP2->GetFit() || graph) {
1339                 if (sensorCavernP2->GetFit()){
1340                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1341                 }
1342                 else {
1343                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1344                 }
1345                 grpObj->SetCavernAtmosPressure(sensorCavernP2);
1346                 ffailedDPs->RemoveAt(indexDP);
1347                 nEnvEntries++;
1348         } 
1349         //if (sensorP2) delete sensorP2;
1350         else {
1351                 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] ));
1352         }
1353         
1354         AliInfo(Form("==========SurfaceAtmosPressure==========="));
1355         indexDP = kSurfaceAtmosPressure;
1356         AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1357         graph = sensorP2->GetGraph();
1358         AliDebug(3,Form("index = %d",indexDP));
1359         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1360         AliDebug(2,Form("graph = %p",graph));   
1361         AliDebug(3,Form("sensorP2 = %p", sensorP2));
1362         if(sensorP2->GetFit() || graph) {
1363                 if (sensorP2->GetFit()){
1364                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1365                 }
1366                 else {
1367                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1368                 }
1369                 grpObj->SetSurfaceAtmosPressure(sensorP2);
1370                 ffailedDPs->RemoveAt(indexDP);
1371                 nEnvEntries++;
1372         } 
1373         //if (sensorP2) delete sensorP2;
1374         else {
1375                 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] ));
1376         }
1377
1378         AliInfo(Form("==========CavernAtmosPressure2==========="));
1379         indexDP = kCavernAtmosPressure2;
1380         AliDCSSensor* sensorCavernP22 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1381         graph = sensorCavernP22->GetGraph();
1382         AliDebug(3,Form("index = %d",indexDP));
1383         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1384         AliDebug(2,Form("graph = %p",graph));   
1385         AliDebug(3,Form("sensorCavernP2_2 = %p", sensorCavernP22));
1386         if(sensorCavernP22->GetFit() || graph) {
1387                 if (sensorCavernP22->GetFit()){
1388                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1389                 }
1390                 else {
1391                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1392                 }
1393                 grpObj->SetCavernAtmosPressure2(sensorCavernP22);
1394                 ffailedDPs->RemoveAt(indexDP);
1395                 nEnvEntries++;
1396         } 
1397         //if (sensorP2) delete sensorP2;
1398         else {
1399                 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] ));
1400         }
1401         
1402         
1403         return nEnvEntries;
1404 }
1405 //_______________________________________________________________
1406
1407 Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpObj)
1408 {
1409         // processing DPs
1410         // related to 
1411         // Hall Probes info
1412
1413         Int_t nHPEntries = 0;
1414         TObjArray *array = 0x0;
1415         Int_t indexDP = -1;
1416         Bool_t outOfRange; // flag to monitor if any value collected by DCS is out of range
1417
1418         if (fgknDCSDPHallProbes != AliGRPObject::GetNumberOfHP()){
1419                 AliError(Form("Number of Hall probes expected in GRP Preprocessor (i.e. %d) different from number of Hall Probes foreseen in GRP object (i.e. %d). Looping on entries from GRP object anyway.", fgknDCSDPHallProbes, AliGRPObject::GetNumberOfHP()));
1420         }
1421         for (indexDP = 0; indexDP < AliGRPObject::GetNumberOfHP(); indexDP++){
1422                 outOfRange = kFALSE; // resetting outOfRange flag at each HP
1423                 AliInfo(Form("==========%s===========",AliGRPObject::GetHPDP(indexDP)));
1424                 array = (TObjArray *)valueMap->GetValue(AliGRPObject::GetHPDP(indexDP));
1425                 if(!array) {
1426                         Log(Form("%s not found in the map!!!",AliGRPObject::GetHPDP(indexDP)));
1427                 } 
1428                 else {
1429                         if (array->GetEntries() == 0){
1430                                 AliError(Form("No entries found in array! setting %s to invalid...",AliGRPObject::GetHPDP(indexDP)));
1431                         }
1432                         else {
1433                                 Float_t *floatDCS = ProcessFloatAll(array);
1434                                 if (floatDCS != NULL){
1435                                         AliDebug(2,Form("value[0] = %f, value[1] = %f, value[2] = %f, value[3] = %f, value[4] = %f",floatDCS[0],floatDCS[1],floatDCS[2],floatDCS[3],floatDCS[4])); 
1436                                         grpObj->SetHallProbes((AliGRPObject::DP_HallProbes)indexDP,floatDCS);
1437                                         for (Int_t kk = 0 ; kk< 5; kk++){
1438                                                 AliDebug(2,Form("HallProbe[%d][%d]=%f",indexDP,kk,grpObj->GetHallProbes((AliGRPObject::DP_HallProbes)indexDP,(AliGRPObject::Stats)kk)));
1439                                         }
1440                                 }
1441                                 else{
1442                                         outOfRange = kTRUE;
1443                                 }
1444                                 if (floatDCS){
1445                                         delete[] floatDCS;
1446                                         floatDCS = 0x0;
1447                                 }
1448                         }
1449                         if (!outOfRange) {
1450                                 ffailedDPs->RemoveAt(indexDP + fgkDCSDPHallTopShift);  // 7 = shift in the complete list of DPs to get to the Hall Probes
1451                                 nHPEntries++;
1452                         }
1453                 }
1454         }
1455                 
1456         Log(Form("Hall Probes = %d ", nHPEntries));
1457         return nHPEntries;
1458 }
1459
1460 //_________________________________________________________________________
1461
1462 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
1463
1464
1465         // 
1466         // returning Spline Fit 
1467         // 
1468
1469         Int_t entriesarray = array->GetEntries();
1470         Float_t* value = new Float_t[entriesarray];
1471         Float_t* time = new Float_t[entriesarray];
1472         AliDCSValue* v = 0x0;
1473         for (Int_t iarray = 0; iarray < entriesarray; iarray++){
1474                 v = (AliDCSValue*)array->At(iarray);
1475                 value[iarray] = v->GetFloat();
1476                 time[iarray] = v->GetTimeStamp();
1477                 AliDebug(2,Form("iarray = %d, value = %f, time = %f",iarray,value[iarray],time[iarray]));
1478         }
1479         TGraph* gr = new TGraph(entriesarray,value,time);
1480         if (!gr ) {
1481                 AliWarning(Form("%s: no input graph to compute SplineFit",stringID.Data()));
1482                 return NULL;
1483         }
1484         AliSplineFit *fit = new AliSplineFit();
1485         fit->SetMinPoints(10);
1486         fit->InitKnots(gr,10,10,0.0);
1487         fit->SplineFit(2);
1488         fit->Cleanup();
1489         if (!fit) {
1490                 AliWarning(Form("%s: no fit performed",stringID.Data()));
1491                 return NULL;
1492         } 
1493         return fit;
1494 }
1495
1496 //_________________________________________________________________________
1497
1498 TString AliGRPPreprocessor::ProcessChar(const TObjArray *array)
1499 {
1500
1501         // 
1502         // processing char
1503         //
1504
1505         TString aDCSString="";
1506         
1507         AliDCSValue *v = 0x0;
1508         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1509                 v = (AliDCSValue *)array->At(iCount);
1510                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1511                         AliError(Form("DCS values for the parameter outside the queried interval"));
1512                         continue;
1513                 }
1514                 if (iCount > 0) {
1515                         if (aDCSString != v->GetChar())
1516                         AliError(Form("DCS values for the parameter changed from %s to %c within the queried interval", aDCSString.Data(), (Char_t)v->GetChar()));
1517                 }
1518                 aDCSString = (TString)v->GetChar();  // keeping always last value in the array
1519         }
1520         return aDCSString;
1521 }
1522
1523 //__________________________________________________________________________________________________________________
1524
1525 Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
1526 {
1527         // 
1528         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1529         //
1530         // parameters[0] = mean
1531         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1532         // parameters[2] = median
1533         // parameters[3] = standard deviation wrt mean
1534         // parameters[4] = standard deviation wrt median
1535         //
1536
1537         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1538         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1539         if (timeStartString.IsNull() || timeStartString.IsNull()){
1540                 if (timeStartString.IsNull()){ 
1541                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1542                 }
1543                 else if (timeStartString.IsNull()){
1544                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1545                 }
1546                 fdaqStartEndTimeOk = kFALSE;
1547                 return 0;
1548         }  
1549
1550         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1551         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1552         Float_t* parameters = new Float_t[5];
1553         Int_t iCounts = 0;
1554         Int_t iCountsRun = 0;
1555         Int_t nCounts = array->GetEntries();
1556         Float_t valueBeforeSOR = 0;
1557         Float_t valueAfterEOR = 0;
1558         Int_t timestampBeforeSOR = -1;
1559         Int_t timestampAfterEOR = -1;
1560         Int_t ientrySOR = -1;
1561         Int_t ientryEOR = -1;
1562         Float_t* arrayValues = 0x0; 
1563         Double_t* arrayWeights = 0x0; 
1564         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
1565         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
1566
1567         for(Int_t i = 0; i < nCounts; i++) {
1568                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1569                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1570                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1571                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1572                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1573                         return NULL;
1574                 }
1575                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1576                         AliDebug(2,Form("%d-th entry = %f at timestamp %i",i,v->GetFloat(),v->GetTimeStamp()));
1577                         iCounts += 1;
1578                         // look for the last value before SOR and the first value before EOR
1579                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1580                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1581                                 AliDebug(2,Form("timestamp of last value before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1582                                 valueBeforeSOR = v->GetFloat();
1583                         }
1584                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1585                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1586                                 valueAfterEOR = v->GetFloat();
1587                                 AliDebug(2,Form("timestamp of first value after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1588                         }
1589                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1590                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1591                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1592                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1593                                 AliDebug(2,Form("entry between SOR and EOR"));
1594                                 iCountsRun += 1;
1595                         }
1596                 }
1597                 else {
1598                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1599                 }
1600         }
1601
1602         if (timestampBeforeSOR == -1){
1603                 AliWarning("No value found before SOR");
1604         }
1605         if (timestampAfterEOR == -1){
1606                 AliWarning("No value found after EOR");
1607         }
1608
1609         AliDebug(2,Form("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries",iCounts,nCounts));
1610         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1611         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1612         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1613         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1614         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1615
1616         Int_t nentriesUsed = 0;
1617         if (iCountsRun > 1){
1618                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1619                 AliDebug(2,"Calculating (weighted) Mean and Median");
1620                 arrayValues = new Float_t[iCountsRun]; 
1621                 arrayWeights = new Double_t[iCountsRun]; 
1622                 nentriesUsed = iCountsRun;
1623                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1624                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1625                         Int_t timestamp2 = 0;
1626                         if (i < ientryEOR){
1627                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1628                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1629                         }
1630                         else {
1631                                 timestamp2 = timeEnd+1;
1632                         }
1633                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1634                         arrayValues[i-ientrySOR] = v->GetFloat();
1635                 }
1636                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1637                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
1638         }
1639         else if (iCountsRun == 1){
1640                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1641                 nentriesUsed = 2;
1642                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1643                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR. Truncated mean won't be calculated.");
1644                         arrayValues = new Float_t[2];
1645                         arrayWeights = new Double_t[2];
1646                         arrayValues[0] = valueBeforeSOR;
1647                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1648                         arrayValues[1] = v->GetFloat();
1649                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1650                         AliDebug(2, Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1651                         AliDebug(2, Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1652                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
1653                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
1654                         truncMeanFlag = kFALSE;
1655                 }
1656                 else{
1657                         AliError("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");
1658                         parameters[0] = AliGRPObject::GetInvalidFloat();
1659                         parameters[1] = AliGRPObject::GetInvalidFloat();
1660                         parameters[2] = AliGRPObject::GetInvalidFloat();
1661                         parameters[3] = AliGRPObject::GetInvalidFloat();
1662                         parameters[4] = AliGRPObject::GetInvalidFloat();
1663                         return parameters;
1664                 }
1665         }
1666         else { // iCountsRun == 0, using only the point immediately before SOR
1667                 if (timestampBeforeSOR == -1){
1668                         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");
1669                         parameters[0] = AliGRPObject::GetInvalidFloat();
1670                         parameters[1] = AliGRPObject::GetInvalidFloat();
1671                         parameters[2] = AliGRPObject::GetInvalidFloat();
1672                         parameters[3] = AliGRPObject::GetInvalidFloat();
1673                         parameters[4] = AliGRPObject::GetInvalidFloat();
1674                         return parameters;
1675                 }
1676                 else {
1677                         AliWarning("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.");
1678                         AliDebug(2,Form("value = %f",valueBeforeSOR)); 
1679                         parameters[0] = valueBeforeSOR;
1680                         parameters[2] = valueBeforeSOR;
1681                         truncMeanFlag = kFALSE;
1682                         sdFlag = kFALSE;
1683                 }
1684         }
1685
1686         Float_t temp = 0;
1687         Float_t temp1 = 0;
1688         Float_t sumweights = 0; 
1689         Int_t entriesTruncMean = 0;
1690         Float_t* arrayValuesTruncMean = new Float_t[nentriesUsed]; 
1691         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
1692
1693         // calculating SD wrt Mean and Median
1694         AliDebug(2,"Calculating SD wrt Mean and SD wrt Median");
1695         if (sdFlag){
1696                 for (Int_t i =0; i< nentriesUsed; i++){
1697                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1698                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
1699                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
1700                         sumweights += arrayWeights[i];
1701                 }
1702                 // setting SD wrt Mean 
1703                 if (sumweights != 0 ){
1704                         parameters[3] = TMath::Sqrt(temp1/sumweights);
1705                 }
1706                 else {
1707                         AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
1708                         parameters[3] = AliGRPObject::GetInvalidFloat();
1709                 }
1710                 // setting SD wrt Median
1711                 if (nentriesUsed != 0){
1712                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
1713                 }
1714                 else{
1715                         AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
1716                         parameters[4] = AliGRPObject::GetInvalidFloat();
1717                 }
1718         }
1719         else {
1720                 parameters[3] = AliGRPObject::GetInvalidFloat();
1721                 parameters[4] = AliGRPObject::GetInvalidFloat();
1722         }               
1723
1724         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
1725         if (truncMeanFlag){
1726                 AliDebug(2,"Calculating Truncated Mean");
1727                 for (Int_t i =0; i< nentriesUsed; i++){
1728                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1729                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
1730                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
1731                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
1732                                 AliDebug(2,Form("For Truncated Mean: Entry %d: value = %f, weight = %f",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]));
1733                                 entriesTruncMean++;                     
1734                         }
1735                         else{
1736                                 AliDebug(2,"Discarding entry");
1737                         }
1738                 }
1739                 // setting truncated mean 
1740                 if (entriesTruncMean >1){
1741                         AliDebug(2,Form("%d entries used for truncated mean",entriesTruncMean));
1742                         parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
1743                 }
1744                 else{   
1745                         AliDebug(2,Form("Too few entries (%d) to calculate truncated mean",entriesTruncMean));
1746                         parameters[1] = AliGRPObject::GetInvalidFloat();
1747                 }
1748         }
1749         else{
1750                         parameters[1] = AliGRPObject::GetInvalidFloat();
1751         }
1752
1753         AliInfo(Form("(weighted) mean = %f ",parameters[0]));
1754         AliInfo(Form("(weighted) truncated mean = %f ",parameters[1]));
1755         AliInfo(Form("median = %f ",parameters[2]));
1756         AliInfo(Form("(weighted) standard deviation with (weighted) mean = %f ",parameters[3]));
1757         AliInfo(Form("standard deviation with median = %f ",parameters[4]));
1758         
1759         return parameters;
1760 }
1761
1762 //__________________________________________________________________________________________________________________
1763
1764 Float_t* AliGRPPreprocessor::ProcessFloatAllMagnet(const TObjArray* array, Int_t indexDP, Bool_t &isZero)
1765 {
1766         // 
1767         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1768         // used for L3 and Dipole magnets, using isZero flag to decide whther the magnet was OFF/ON
1769         // the flag is set according to the L3/Dipole current value
1770         // current threshold for L3 = 350 A (value provided by DCS)
1771         // current threshold for Dipole = 450 A (value provided by DCS)
1772         //
1773         // parameters[0] = mean
1774         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1775         // parameters[2] = median
1776         // parameters[3] = standard deviation wrt mean
1777         // parameters[4] = standard deviation wrt median
1778         //
1779
1780         AliInfo(Form("indexDP = %d",indexDP)); 
1781
1782         Int_t nCounts = array->GetEntries();
1783         for(Int_t i = 0; i < nCounts; i++) {
1784                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1785                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1786                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1787                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1788                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1789                         return NULL;
1790                 }
1791                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1792                         AliDebug(2,Form("%d-th entry = %f",i,v->GetFloat()));
1793                         if (indexDP == kL3Current && v->GetFloat() > 350 && isZero == kTRUE) isZero=kFALSE; 
1794                         if (indexDP == kDipoleCurrent && v->GetFloat() > 450 && isZero == kTRUE) isZero=kFALSE; 
1795                 }
1796                 else {
1797                         AliError(Form("DCS values for the parameter outside the queried interval"));
1798                 }
1799         }
1800
1801         return ProcessFloatAll(array);
1802 }
1803
1804
1805 //_______________________________________________________________
1806
1807 Char_t AliGRPPreprocessor::ProcessBool(const TObjArray* array, Bool_t &change)
1808 {
1809         // 
1810         // processing Boolean values
1811         //
1812
1813         Bool_t aDCSBool = kTRUE;
1814
1815         AliDCSValue *v = 0x0;
1816
1817         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1818                 v = (AliDCSValue *)array->At(iCount);
1819                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1820                         AliError(Form("DCS values for the parameter outside the queried interval"));
1821                         continue;
1822                 }
1823                 if (iCount > 0) {
1824                         if (aDCSBool != v->GetBool()) {
1825                                 AliError(Form("DCS values for the parameter changed from %d to %d within the queried interval", (UInt_t)aDCSBool, (UInt_t)v->GetBool()));
1826                                 change = kTRUE;
1827                         }
1828                 }
1829                 aDCSBool = v->GetBool(); // always keeping last value
1830                 AliDebug(2,Form("Bool = %d",(Int_t)aDCSBool));
1831         }
1832         
1833         Char_t caDCSBool = (Char_t) aDCSBool;
1834         return caDCSBool;
1835         
1836 }
1837
1838 //_______________________________________________________________
1839
1840 Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
1841 {
1842         // 
1843         // processing Int values, returning mean
1844         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
1845         // are outside the queried time interval or their value is out of range
1846         //
1847
1848         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1849         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1850         if (timeStartString.IsNull() || timeStartString.IsNull()){
1851                 if (timeStartString.IsNull()){ 
1852                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1853                 }
1854                 else if (timeStartString.IsNull()){
1855                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1856                 }
1857                 return 0;
1858         }  
1859
1860         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1861         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1862         Float_t aDCSArrayMean = 0.0;
1863         Int_t iCounts = 0;
1864         Float_t valueBeforeSOR = 0;
1865         Float_t valueAfterEOR = 0;
1866         Int_t timestampBeforeSOR = -1;
1867         Int_t timestampAfterEOR = -1;
1868         Int_t ientrySOR = -1;
1869         Int_t ientryEOR = -1;
1870         Float_t* arrayValues = 0x0; 
1871         Double_t* arrayWeights = 0x0; 
1872         Int_t iCountsRun = 0;
1873         Int_t nCounts = array->GetEntries();
1874
1875         for(Int_t i = 0; i < nCounts; i++) {
1876                 AliDCSValue* v = (AliDCSValue *)array->At(i);
1877                 if ((v->GetInt() < fminInt) || (v->GetInt() > fmaxInt)) {
1878                         AliError(Form("Error! Int value found in DCS map at %d-th entry is OUT OF RANGE: value = %d",i, v->GetInt()));
1879                         return AliGRPObject::GetInvalidFloat();
1880                 }
1881                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1882                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetInt(),v->GetTimeStamp()));
1883                         iCounts += 1;
1884                         // look for the last value before SOR and the first value before EOR
1885                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1886                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1887                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1888                                 valueBeforeSOR = (Float_t) v->GetInt();
1889                         }
1890                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1891                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1892                                 valueAfterEOR = (Float_t) v->GetInt();
1893                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1894                         }
1895                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1896                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1897                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1898                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1899                                 AliDebug(2,Form("entry between SOR and EOR"));
1900                                 iCountsRun += 1;
1901                         }
1902                 }
1903                 else {
1904                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1905                 }
1906         }
1907
1908         if (timestampBeforeSOR == -1){
1909                 AliWarning("No value found before SOR!");
1910         }
1911         if (timestampAfterEOR == -1){
1912                 AliWarning("No value found after EOR!");
1913         }
1914
1915         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
1916         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1917         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1918         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1919         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1920         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1921
1922         Int_t nentriesUsed = 0;
1923         if (iCountsRun > 1){
1924                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1925                 AliDebug(2,"Calculating (weighted) Mean");
1926                 arrayValues = new Float_t[iCountsRun]; 
1927                 arrayWeights = new Double_t[iCountsRun]; 
1928                 nentriesUsed = iCountsRun;
1929                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1930                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1931                         Int_t timestamp2 = 0;
1932                         if (i < ientryEOR){
1933                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1934                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1935                         }
1936                         else {
1937                                 timestamp2 = timeEnd+1;
1938                         }
1939                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1940                         arrayValues[i-ientrySOR] = (Float_t)v->GetInt();
1941                 }
1942                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1943         }
1944         else if (iCountsRun == 1){
1945                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1946                 nentriesUsed = 2;
1947                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1948                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
1949                         arrayValues = new Float_t[2];
1950                         arrayWeights = new Double_t[2];
1951                         arrayValues[0] = valueBeforeSOR;
1952                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1953                         arrayValues[1] = (Float_t)v->GetInt();
1954                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1955                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1956                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1957                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
1958                 }
1959                 else{
1960                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
1961                         return AliGRPObject::GetInvalidFloat();
1962                 }
1963         }
1964         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
1965                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
1966                         if (timestampBeforeSOR == -1){
1967                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
1968                         }
1969                         if (timestampAfterEOR == -1){
1970                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
1971                         }
1972                         return AliGRPObject::GetInvalidFloat();
1973                 }
1974                 else {
1975                         AliWarning("Using last entry before SOR and first entry after EOR.");
1976                         nentriesUsed = 2;
1977                         arrayValues = new Float_t[2];
1978                         arrayWeights = new Double_t[2];
1979                         arrayValues[0] = valueBeforeSOR;
1980                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
1981                         arrayValues[1] = valueAfterEOR;
1982                         arrayWeights[1] = 1.;
1983                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1984                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1985                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
1986                 }
1987         }
1988
1989         AliInfo(Form("mean = %f ", aDCSArrayMean));
1990         return aDCSArrayMean;
1991
1992 }
1993 //_______________________________________________________________
1994
1995 Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
1996 {
1997         // 
1998         // processing Int values, returning mean 
1999         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
2000         // are outside the queried time interval or their value is out of range
2001         //
2002
2003         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2004         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2005         if (timeStartString.IsNull() || timeStartString.IsNull()){
2006                 if (timeStartString.IsNull()){ 
2007                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2008                 }
2009                 else if (timeStartString.IsNull()){
2010                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2011                 }
2012                 return 0;
2013         }  
2014
2015         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2016         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2017         Float_t aDCSArrayMean = 0.0;
2018         Int_t iCounts = 0;
2019         Float_t valueBeforeSOR = 0;
2020         Float_t valueAfterEOR = 0;
2021         Int_t timestampBeforeSOR = -1;
2022         Int_t timestampAfterEOR = -1;
2023         Int_t ientrySOR = -1;
2024         Int_t ientryEOR = -1;
2025         Float_t* arrayValues = 0x0; 
2026         Double_t* arrayWeights = 0x0; 
2027         Int_t iCountsRun = 0;
2028         Int_t nCounts = array->GetEntries();
2029
2030         for(Int_t i = 0; i < nCounts; i++) {
2031                 AliDCSValue* v = (AliDCSValue *)array->At(i);
2032                 if ((v->GetUInt() < fminUInt) || (v->GetUInt() > fmaxUInt)) {
2033                         AliError(Form("Error! UInt value found in DCS map at %d-th entry is OUT OF RANGE: value = %u",i,v->GetUInt()));
2034                         return AliGRPObject::GetInvalidFloat();
2035                 }
2036                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2037                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetUInt(),v->GetTimeStamp()));
2038                         iCounts += 1;
2039                         // look for the last value before SOR and the first value before EOR
2040                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2041                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2042                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2043                                 valueBeforeSOR = (Float_t)v->GetUInt();
2044                         }
2045                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2046                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2047                                 valueAfterEOR = (Float_t)v->GetUInt();
2048                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2049                         }
2050                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2051                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2052                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2053                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2054                                 AliDebug(2,Form("entry between SOR and EOR"));
2055                                 iCountsRun += 1;
2056                         }
2057                 }
2058                 else {
2059                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2060                 }
2061         }
2062
2063         if (timestampBeforeSOR == -1){
2064                 AliWarning("No value found before SOR!");
2065         }
2066         if (timestampAfterEOR == -1){
2067                 AliWarning("No value found after EOR!");
2068         }
2069
2070         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
2071         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2072         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2073         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2074         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2075         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2076
2077         Int_t nentriesUsed = 0;
2078         if (iCountsRun > 1){
2079                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2080                 AliDebug(2,"Calculating (weighted) Mean");
2081                 arrayValues = new Float_t[iCountsRun]; 
2082                 arrayWeights = new Double_t[iCountsRun]; 
2083                 nentriesUsed = iCountsRun;
2084                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2085                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2086                         Int_t timestamp2 = 0;
2087                         if (i < ientryEOR){
2088                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2089                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2090                         }
2091                         else {
2092                                 timestamp2 = timeEnd+1;
2093                         }
2094                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2095                         arrayValues[i-ientrySOR] = (Float_t)v->GetUInt();
2096                 }
2097                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2098         }
2099         else if (iCountsRun == 1){
2100                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2101                 nentriesUsed = 2;
2102                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2103                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
2104                         arrayValues = new Float_t[2];
2105                         arrayWeights = new Double_t[2];
2106                         arrayValues[0] = valueBeforeSOR;
2107                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2108                         arrayValues[1] = (Float_t)v->GetUInt();
2109                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2110                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2111                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2112                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
2113                 }
2114                 else{
2115                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2116                         return AliGRPObject::GetInvalidFloat();
2117                 }
2118         }
2119         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2120                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2121                         if (timestampBeforeSOR == -1){
2122                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2123                         }
2124                         if (timestampAfterEOR == -1){
2125                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2126                         }
2127                         return AliGRPObject::GetInvalidFloat();
2128                 }
2129                 else {
2130                         AliWarning("Using last entry before SOR and first entry after EOR.");
2131                         nentriesUsed = 2;
2132                         arrayValues = new Float_t[2];
2133                         arrayWeights = new Double_t[2];
2134                         arrayValues[0] = valueBeforeSOR;
2135                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2136                         arrayValues[1] = valueAfterEOR;
2137                         arrayWeights[1] = 1.;
2138                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2139                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2140                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2141                 }
2142         }
2143
2144         AliInfo(Form("mean = %f ",aDCSArrayMean));
2145         return aDCSArrayMean;
2146
2147 }
2148
2149
2150 //_______________________________________________________________
2151
2152 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
2153 {
2154         // extract DCS pressure maps. Perform fits to save space
2155         
2156         TMap *map = fPressure->ExtractDCS(dcsAliasMap);
2157         if (map) {
2158                 AliDebug(2,Form("Map has %d entries",map->GetEntries()));
2159                 fPressure->MakeSplineFit(map);
2160                 Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
2161                 if (fitFraction > kFitFraction ) {
2162                         AliInfo(Form("Pressure values extracted, %d fits performed for %d sensors.", fPressure->NumFits(),fPressure->NumSensors()));
2163                 } else { 
2164                         AliInfo("Too few pressure maps fitted!!!");
2165                 }
2166         } else {
2167                 AliInfo("no atmospheric pressure map extracted!!!");
2168         }
2169         delete map;
2170         
2171         return fPressure;
2172 }
2173
2174
2175   
2176 //_______________________________________________________________
2177 Int_t AliGRPPreprocessor::ReceivePromptRecoParameters(UInt_t run, const char* dbHost, Int_t dbPort, const char* dbName, const char* user, const char* password, const char *cdbRoot, TString &gdc)
2178 {
2179         //
2180         // Retrieves logbook and trigger information from the online logbook
2181         // This information is needed for prompt reconstruction
2182         //
2183         // Parameters are:
2184         // Run number
2185         // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
2186         // cdbRoot
2187         //
2188         // returns:
2189         //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
2190         //         0 on success and no run was found
2191         //         negative on error
2192         //
2193         // This function is NOT called during the preprocessor run in the Shuttle!
2194         //
2195         
2196         // defaults
2197         if (dbPort == 0)
2198                 dbPort = 3306;
2199         
2200         // CDB connection
2201         AliCDBManager* cdb = AliCDBManager::Instance();
2202         cdb->SetDefaultStorage(cdbRoot);
2203         
2204         // SQL connection
2205         TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
2206         
2207         if (!server)
2208                 {
2209                         Printf("ERROR: Could not connect to DAQ LB");
2210                         return -1;
2211                 }
2212         
2213         // main logbook
2214         TString sqlQuery;
2215         sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent FROM logbook WHERE run = %d", run);
2216         TSQLResult* result = server->Query(sqlQuery);
2217         if (!result)
2218                 {
2219                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2220                         return -2;
2221                 }
2222         
2223         if (result->GetRowCount() == 0)
2224                 {
2225                         Printf("ERROR: Run %d not found", run);
2226                         delete result;
2227                         return -3;
2228                 }
2229         
2230         TSQLRow* row = result->Next();
2231         if (!row)
2232                 {
2233                         Printf("ERROR: Could not receive data from run %d", run);
2234                         delete result;
2235                         return -4;
2236                 }
2237         
2238         TString timeStartString(row->GetField(0));
2239         TString runType(row->GetField(1));
2240         TString detectorMaskString(row->GetField(2));
2241         TString l3CurrentString(row->GetField(3));
2242         TString dipoleCurrentString(row->GetField(4));
2243         time_t timeStart = (time_t)(timeStartString.Atoi());
2244         UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
2245         Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
2246         Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
2247         Char_t l3Polarity = (l3CurrentString.Atof() < 0) ? 1 : 0;
2248         Char_t dipolePolarity = (dipoleCurrentString.Atof() < 0) ? 1 : 0;
2249         
2250         AliGRPObject * grpObj = new AliGRPObject();
2251         grpObj->SetTimeStart(timeStart); 
2252         grpObj->SetRunType((TString)(row->GetField(1)));
2253         grpObj->SetDetectorMask(detectorMask);
2254         grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
2255         grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
2256         grpObj->SetL3Polarity(l3Polarity);
2257         grpObj->SetDipolePolarity(dipolePolarity);
2258         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
2259
2260         delete row;
2261         row = 0;
2262         
2263         delete result;
2264         result = 0;
2265         
2266         Printf("Storing GRP/GRP/Data object with the following content");
2267         grpObj->Dump();
2268         
2269         AliCDBMetaData metadata;
2270         metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus & Chiara Zampolli");
2271         metadata.SetComment("GRP Output parameters received during online running");
2272         
2273         AliCDBId id("GRP/GRP/Data", run, run);
2274         Bool_t success = cdb->Put(grpObj, id, &metadata);
2275         
2276         delete grpObj;
2277         
2278         if (!success)
2279                 {
2280                         Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
2281                         return -5;
2282                 }
2283         
2284         // Receive trigger information
2285         sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
2286         result = server->Query(sqlQuery);
2287         if (!result)
2288                 {
2289                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2290                         return -11;
2291                 }
2292         
2293         if (result->GetRowCount() == 0)
2294                 {
2295                         Printf("ERROR: Run %d not found in logbook_trigger_config", run);
2296                         delete result;
2297                         return -12;
2298                 }
2299         
2300         row = result->Next();
2301         if (!row)
2302                 {
2303                         Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
2304                         delete result;
2305                         return -13;
2306                 }
2307         
2308         TString triggerConfig(row->GetField(0));
2309         
2310         delete row;
2311         row = 0;
2312         
2313         delete result;
2314         result = 0;
2315         
2316         Printf("Found trigger configuration: %s", triggerConfig.Data());
2317         
2318         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
2319         if (!runcfg)
2320                 {
2321                         Printf("ERROR: Could not create CTP configuration object");
2322                         return -14;
2323                 }
2324         
2325         metadata.SetComment("CTP run configuration received during online running");
2326         
2327         AliCDBId id2("GRP/CTP/Config", run, run);
2328         success = cdb->Put(runcfg, id2, &metadata);
2329         
2330         delete runcfg;
2331         runcfg = 0;
2332         
2333         if (!success)
2334                 {
2335                         Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
2336                         return -15;
2337                 }
2338         
2339
2340         // Receive list of GDCs for this run
2341         sqlQuery.Form("SELECT GDC FROM logbook_stats_GDC WHERE run = %d", run);
2342         result = server->Query(sqlQuery);
2343         if (!result)
2344                 {
2345                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2346                         return -24;
2347                 }
2348         
2349         if (result->GetRowCount() == 0)
2350                 {
2351                         Printf("ERROR: Run %d not found in logbook_stats_GDC", run);
2352                         delete result;
2353                         return -25;
2354                 }
2355
2356         gdc = "";
2357         for (Int_t iGDC = 0; iGDC < result->GetRowCount(); iGDC++) {
2358           row = result->Next();
2359           if (!row)
2360             {
2361               Printf("ERROR: Could not receive logbook_stats_GDC data from run %d", run);
2362               delete result;
2363               return -26;
2364             }
2365           gdc += row->GetField(0);
2366           gdc += " ";
2367         }
2368
2369         delete row;
2370         row = 0;
2371         
2372         delete result;
2373         result = 0;
2374         
2375         Printf("Found GDC: %s", gdc.Data());
2376
2377         // get last run with same run type that was already processed by the SHUTTLE
2378         
2379         sqlQuery.Form("SELECT max(logbook.run) FROM logbook LEFT JOIN logbook_shuttle ON logbook_shuttle.run = logbook.run WHERE run_type = '%s' AND shuttle_done = 1", runType.Data());
2380         result = server->Query(sqlQuery);
2381         if (!result)
2382                 {
2383                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2384                         return -21;
2385                 }
2386         
2387         if (result->GetRowCount() == 0)
2388                 {
2389                         Printf("ERROR: No result with query <%s>", sqlQuery.Data());
2390                         delete result;
2391                         return -22;
2392                 }
2393         
2394         row = result->Next();
2395         if (!row)
2396                 {
2397                         Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
2398                         delete result;
2399                         return -23;
2400                 }
2401         
2402         TString lastRunStr(row->GetField(0));
2403         Int_t lastRun = lastRunStr.Atoi();
2404         
2405         Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
2406         
2407         delete row;
2408         row = 0;
2409         
2410         delete result;
2411         result = 0;
2412         
2413         server->Close();
2414         delete server;
2415         server = 0;
2416         
2417         return lastRun;
2418 }
2419 //-----------------------------------------------------------------
2420 Double_t AliGRPPreprocessor::CalculateMean(TObjArray* array){
2421
2422         //
2423         // Calculating mean over TObjArray from LHC Data
2424         //
2425
2426         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2427         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2428         if (timeStartString.IsNull() || timeStartString.IsNull()){
2429                 if (timeStartString.IsNull()){ 
2430                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2431                 }
2432                 else if (timeStartString.IsNull()){
2433                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2434                 }
2435                 return 0;
2436         }  
2437
2438         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2439         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2440         timeStart = 1260646960;
2441         timeEnd = 1260652740;
2442         Double_t* parameters = new Double_t[5];
2443         parameters[0] = -1.;
2444         parameters[1] = -1.;
2445         parameters[2] = -1.;
2446         parameters[3] = -1.;
2447         parameters[4] = -1.;
2448         Int_t iCounts = 0;
2449         Int_t iCountsRun = 0;
2450         Int_t nCounts = array->GetEntries();
2451         printf("ncounts = %d\n",nCounts);
2452         Double_t valueBeforeSOR = 0;
2453         Double_t valueAfterEOR = 0;
2454         Double_t timestampBeforeSOR = -1.;
2455         Double_t timestampAfterEOR = -1.;
2456         Int_t ientrySOR = -1;
2457         Int_t ientryEOR = -1;
2458         Double_t* arrayValues = 0x0; 
2459         Double_t* arrayWeights = 0x0; 
2460         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
2461         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
2462
2463         for(Int_t i = 0; i < nCounts; i++) {
2464                 AliDCSArray *dcs = (AliDCSArray*)array->At(i);
2465                 if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
2466                         AliDebug(2,Form("%d-th entry = %f at timestamp %f\n",i,(Double_t)(dcs->GetInt(0)),dcs->GetTimeStamp()));
2467                         iCounts += 1;
2468                         // look for the last value before SOR and the first value before EOR
2469                         if ((dcs->GetTimeStamp() >= timeStart) && (dcs->GetTimeStamp() < timeStart)) {
2470                                 timestampBeforeSOR = dcs->GetTimeStamp();
2471                                 AliDebug(2,Form("timestamp of last value before SOR = %f, with DAQ_time_start = %d\n",timestampBeforeSOR,timeStart));
2472                                 valueBeforeSOR = (Double_t)(dcs->GetInt(0));
2473                         }
2474                         else if ((dcs->GetTimeStamp() <= timeEnd) && (dcs->GetTimeStamp() > timeEnd) && timestampAfterEOR == -1){
2475                                 timestampAfterEOR = dcs->GetTimeStamp();
2476                                 valueAfterEOR = (Double_t)(dcs->GetInt(0));
2477                                 AliDebug(2,Form("timestamp of first value after EOR = %f, with DAQ_time_end = %d\n",timestampAfterEOR,timeEnd));
2478                         }
2479                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2480                         if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
2481                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2482                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2483                                 AliDebug(2,Form("entry between SOR and EOR\n"));
2484                                 iCountsRun += 1;
2485                         }
2486                 }
2487                 else {
2488                         printf("DCS values for the parameter outside the queried interval: timestamp = %f\n",dcs->GetTimeStamp());
2489                 }
2490         }
2491
2492         if (timestampBeforeSOR == -1.){
2493                 printf("No value found before SOR\n");
2494         }
2495         if (timestampAfterEOR == -1.){
2496                 printf("No value found after EOR\n");
2497         }
2498
2499         printf("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries\n",iCounts,nCounts);
2500         printf("Last value before DAQ_time_start (SOR) = %f at timestamp = %f\n",valueBeforeSOR,timestampBeforeSOR);
2501         printf("First value after DAQ_time_end (EOR)   = %f at timestamp = %f\n",valueAfterEOR,timestampAfterEOR);
2502         printf("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n",iCountsRun);
2503         printf("Index of first entry after DAQ_time_start (SOR) = %d\n ",ientrySOR);
2504         printf("Index of first entry before DAQ_time_end (EOR) = %d\n ",ientryEOR);
2505
2506         Int_t nentriesUsed = 0;
2507         if (iCountsRun > 1){
2508                 printf("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n");
2509                 printf("Calculating (weighted) Mean and Median\n" );
2510                 arrayValues = new Double_t[iCountsRun]; 
2511                 arrayWeights = new Double_t[iCountsRun]; 
2512                 nentriesUsed = iCountsRun;
2513                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2514                         AliDCSArray *dcs = (AliDCSArray *)array->At(i);
2515                         Double_t timestamp2 = 0;
2516                         if (i < ientryEOR){
2517                                 AliDCSArray *dcs1 = (AliDCSArray *)array->At(i+1);
2518                                 timestamp2 = dcs1->GetTimeStamp();
2519                         }
2520                         else {
2521                                 timestamp2 = (Double_t)timeEnd+1;
2522                         }
2523                         arrayWeights[i-ientrySOR] = (Double_t)((Double_t)timestamp2 - dcs->GetTimeStamp());
2524                         arrayValues[i-ientrySOR] = (Double_t)(dcs->GetInt(0));
2525                         printf("Entry %d: value = %f, weight = %f\n",i-ientrySOR,arrayValues[i-ientrySOR],arrayWeights[i-ientrySOR]);
2526                 }
2527                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2528                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
2529         }
2530         else if (iCountsRun == 1){
2531                 AliDCSArray* dcs = (AliDCSArray *)array->At(ientrySOR);
2532                 nentriesUsed = 2;
2533                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)dcs->GetTimeStamp()){
2534                         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");
2535                         arrayValues = new Double_t[2];
2536                         arrayWeights = new Double_t[2];
2537                         arrayValues[0] = valueBeforeSOR;
2538                         arrayWeights[0] = (Double_t)(dcs->GetTimeStamp()-(Double_t)timestampBeforeSOR);
2539                         arrayValues[1] = (Double_t)(dcs->GetInt(0));
2540                         arrayWeights[1] = (Double_t)((Double_t)timeEnd+1-dcs->GetTimeStamp());
2541                         printf("value0 = %f, with weight = %f\n",arrayValues[0],arrayWeights[0]); 
2542                         printf("value1 = %f, with weight = %f\n",arrayValues[1],arrayWeights[1]); 
2543                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
2544                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
2545                         truncMeanFlag = kFALSE;
2546                 }
2547                 else{
2548                         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");
2549                         parameters[0] = -1;
2550                         parameters[1] = -1;
2551                         parameters[2] = -1;
2552                         parameters[3] = -1;
2553                         parameters[4] = -1;
2554                         return parameters[0];
2555                 }
2556         }
2557         else { // iCountsRun == 0, using only the point immediately before SOR
2558                 if (timestampBeforeSOR == -1.){
2559                         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");
2560                         parameters[0] = -1;
2561                         parameters[1] = -1;
2562                         parameters[2] = -1;
2563                         parameters[3] = -1;
2564                         parameters[4] = -1;
2565                         return parameters[0];
2566                 }
2567                 else {
2568                         printf("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.\n");
2569                         printf("value = %f\n",valueBeforeSOR); 
2570                         parameters[0] = valueBeforeSOR;
2571                         parameters[2] = valueBeforeSOR;
2572                         truncMeanFlag = kFALSE;
2573                         sdFlag = kFALSE;
2574                 }
2575         }
2576
2577         Double_t temp = 0;
2578         Double_t temp1 = 0;
2579         Double_t sumweights = 0; 
2580         Int_t entriesTruncMean = 0;
2581         Double_t* arrayValuesTruncMean = new Double_t[nentriesUsed]; 
2582         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
2583
2584         // calculating SD wrt Mean and Median
2585         printf("Calculating SD wrt Mean and SD wrt Median\n");
2586         if (sdFlag){
2587                 for (Int_t i =0; i< nentriesUsed; i++){
2588                         //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
2589                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
2590                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
2591                         sumweights += arrayWeights[i];
2592                 }
2593                 // setting SD wrt Mean 
2594                 if (sumweights != 0 ){
2595                         parameters[3] = TMath::Sqrt(temp1/sumweights);
2596                 }
2597                 else {
2598                         printf("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid\n");
2599                         parameters[3] = -1;
2600                 }
2601                 // setting SD wrt Median
2602                 if (nentriesUsed != 0){
2603                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
2604                 }
2605                 else{
2606                         printf("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid\n");
2607                         parameters[4] = -1;
2608                 }
2609         }
2610         else {
2611                 parameters[3] = -1;
2612                 parameters[4] = -1;
2613         }               
2614
2615         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
2616         if (truncMeanFlag){
2617                 printf("Calculating Truncated Mean\n");
2618                 for (Int_t i =0; i< nentriesUsed; i++){
2619                         //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
2620                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
2621                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
2622                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
2623                                 printf("For Truncated Mean: Entry %d: value = %f, weight = %f\n",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]);
2624                                 entriesTruncMean++;                     
2625                         }
2626                         else{
2627                                 printf("Discarding entry\n");
2628                         }
2629                 }
2630                 // setting truncated mean 
2631                 if (entriesTruncMean >1){
2632                         printf("%d entries used for truncated mean\n",entriesTruncMean);
2633                         parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
2634                 }
2635                 else{   
2636                         printf("Too few entries (%d) to calculate truncated mean\n",entriesTruncMean);
2637                         parameters[1] = -1;
2638                 }
2639         }
2640         else{
2641                         parameters[1] = -1;
2642         }
2643         
2644         printf("(weighted) mean = %f \n",parameters[0]);
2645         printf("(weighted) truncated mean = %f \n",parameters[1]);
2646         printf("median = %f \n",parameters[2]);
2647         printf("(weighted) standard deviation with (weighted) mean = %f \n",parameters[3]);
2648         printf("standard deviation with median = %f \n",parameters[4]);
2649         
2650         return (parameters[0]);
2651 }
2652 //------------------------------------------------------------------------------------------------------
2653 Float_t AliGRPPreprocessor::ProcessEnergy(TObjArray* array, Double_t timeStart, Double_t timeEnd){
2654
2655         //
2656         // Method to processo LHC Energy information
2657         // Only the first value is returned, provided that it is withing DAQ_time_start and DAQ_time_end
2658         //
2659
2660         Int_t nCounts = array->GetEntries();
2661         Float_t energy = -1;
2662         Bool_t inRange = kFALSE;
2663         AliDebug(2,Form("Energy measurements = %d\n",nCounts));
2664         if (nCounts ==0){
2665                 AliWarning("No Energy values found! Beam Energy remaining invalid!");
2666         }
2667         else{
2668                 for(Int_t i = 0; i < nCounts; i++) {
2669                         AliDCSArray *dcs = (AliDCSArray*)array->At(i);
2670                         if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
2671                                 energy = (Float_t)(TMath::Nint(((Double_t)(dcs->GetInt(0)))*120/1000)); // sqrt(s)/2 energy in GeV
2672                                 AliInfo(Form("Energy value found = %d, converting --> sqrt(s)/2 = %f (GeV)", dcs->GetInt(0),energy));
2673                                 inRange = kTRUE;
2674                                 break;
2675                         }
2676                 }
2677                 if (inRange == kFALSE){
2678                                 AliInfo("No Energy value found between DAQ_time_start and DAQ_time_end - energy will remain invalid!");
2679                 }
2680         
2681         }
2682
2683         return energy;
2684 }