]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliGRPPreprocessor.cxx
0bf8292c28212df7f658095fd0ebb8aa7a0a8bf3
[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
51 class AliDCSValue;
52 class AliShuttleInterface;
53
54 // needed for ReceivePromptRecoParameters
55
56 #include <TSQLServer.h>
57 #include <TSQLResult.h>
58 #include <TSQLRow.h>
59 #include <AliCDBManager.h>
60 #include <AliCDBMetaData.h>
61 #include <AliCDBId.h>
62 #include <AliTriggerConfiguration.h>
63 #include <AliCTPTimeParams.h>
64
65 const Double_t kFitFraction = -1.;                 // Fraction of DCS sensor fits required
66
67 ClassImp(AliGRPPreprocessor)
68
69 //_______________________________________________________________
70
71   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 8; // num parameters in the logbook for PHYSICS runs, when beamType from DAQ logbook == NULL
72   const Int_t AliGRPPreprocessor::fgknDAQLbParReduced = 7; // num parameters in the logbook for the other cases
73   const Int_t AliGRPPreprocessor::fgknDCSDP = 51;   // number of dcs dps
74   const Int_t AliGRPPreprocessor::fgknDCSDPHallProbes = 40;   // number of dcs dps
75   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
76                    "LHCState",              // missing in DCS
77                    "L3Polarity",
78                    "DipolePolarity",
79                    "LHCLuminosity",         // missing in DCS
80                    "BeamIntensity",         // missing in DCS
81                    "L3Current",
82                    "DipoleCurrent",
83                    "L3_BSF17_H1",
84                    "L3_BSF17_H2",
85                    "L3_BSF17_H3",
86                    "L3_BSF17_Temperature",
87                    "L3_BSF4_H1",
88                    "L3_BSF4_H2",
89                    "L3_BSF4_H3",
90                    "L3_BSF4_Temperature",
91                    "L3_BKF17_H1",
92                    "L3_BKF17_H2",
93                    "L3_BKF17_H3",
94                    "L3_BKF17_Temperature",
95                    "L3_BKF4_H1",
96                    "L3_BKF4_H2",
97                    "L3_BKF4_H3",
98                    "L3_BKF4_Temperature",
99                    "L3_BSF13_H1",
100                    "L3_BSF13_H2",
101                    "L3_BSF13_H3",
102                    "L3_BSF13_Temperature",
103                    "L3_BSF8_H1",
104                    "L3_BSF8_H2",
105                    "L3_BSF8_H3",
106                    "L3_BSF8_Temperature",
107                    "L3_BKF13_H1",
108                    "L3_BKF13_H2",
109                    "L3_BKF13_H3",
110                    "L3_BKF13_Temperature",
111                    "L3_BKF8_H1",
112                    "L3_BKF8_H2",
113                    "L3_BKF8_H3",
114                    "L3_BKF8_Temperature",
115                    "Dipole_Inside_H1",
116                    "Dipole_Inside_H2",
117                    "Dipole_Inside_H3",
118                    "Dipole_Inside_Temperature",
119                    "Dipole_Outside_H1",
120                    "Dipole_Outside_H2",
121                    "Dipole_Outside_H3",
122                    "Dipole_Outside_Temperature",
123                    "CavernTemperature",
124                    "CavernAtmosPressure",
125                    "SurfaceAtmosPressure",
126                    "CavernAtmosPressure2"
127                  };
128
129   const char* AliGRPPreprocessor::fgkDCSDataPointsHallProbes[AliGRPPreprocessor::fgknDCSDPHallProbes] = {
130                    "L3_BSF17_H1",
131                    "L3_BSF17_H2",
132                    "L3_BSF17_H3",
133                    "L3_BSF17_Temperature",
134                    "L3_BSF4_H1",
135                    "L3_BSF4_H2",
136                    "L3_BSF4_H3",
137                    "L3_BSF4_Temperature",
138                    "L3_BKF17_H1",
139                    "L3_BKF17_H2",
140                    "L3_BKF17_H3",
141                    "L3_BKF17_Temperature",
142                    "L3_BKF4_H1",
143                    "L3_BKF4_H2",
144                    "L3_BKF4_H3",
145                    "L3_BKF4_Temperature",
146                    "L3_BSF13_H1",
147                    "L3_BSF13_H2",
148                    "L3_BSF13_H3",
149                    "L3_BSF13_Temperature",
150                    "L3_BSF8_H1",
151                    "L3_BSF8_H2",
152                    "L3_BSF8_H3",
153                    "L3_BSF8_Temperature",
154                    "L3_BKF13_H1",
155                    "L3_BKF13_H2",
156                    "L3_BKF13_H3",
157                    "L3_BKF13_Temperature",
158                    "L3_BKF8_H1",
159                    "L3_BKF8_H2",
160                    "L3_BKF8_H3",
161                    "L3_BKF8_Temperature",
162                    "Dipole_Inside_H1",
163                    "Dipole_Inside_H2",
164                    "Dipole_Inside_H3",
165                    "Dipole_Inside_Temperature",
166                    "Dipole_Outside_H1",
167                    "Dipole_Outside_H2",
168                    "Dipole_Outside_H3",
169                    "Dipole_Outside_Temperature"
170                  };
171                  
172   const Short_t kSensors = 48; // start index position of sensor in DCS DPs
173   const Short_t kNumSensors = 3; // Number of sensors in DCS DPs (CavernAtmosPressure, SurfaceAtmosPressure, CavernAtmosPressure2)
174
175
176   const char* AliGRPPreprocessor::fgkLHCState[20] = {
177                    "P", "PREPARE",
178                    "J", "PREINJECTION",
179                    "I", "INJECTION",
180                    "F", "FILLING",
181                    "A", "ADJUST",
182                    "U", "UNSTABLE BEAMS",
183                    "S", "STABLE BEAMS",
184                    "D", "BEAM DUMP",
185                    "R", "RECOVER",
186                    "C", "PRECYCLE"
187                  };
188
189   const char* kppError[] = {
190                    "",
191                    "(DAQ logbook ERROR)",
192                    "(DAQ FXS ERROR)",
193                    "(Trigger Scalers not found in DCS FXS - ERROR)",
194                    "(DCS data points ERROR)",
195                    "(Trigger Configuration ERROR)",
196                    "(DAQ logbook ERROR determining partition of the run)",
197                    "(CTP timing ERROR)",
198                    "(SPD Mean Vertex ERROR)"
199   };
200
201 //_______________________________________________________________
202
203 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
204         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))
205 {
206         // constructor - shuttle must be instantiated!
207
208         AddRunType("COSMIC");
209         AddRunType("LASER");
210         AddRunType("PHYSICS");
211         AddRunType("CALIBRATION_BC");
212         AddRunType("CALIBRATION_CENTRAL");
213         AddRunType("CALIBRATION_EMD");
214         AddRunType("CALIBRATION_MB");
215         AddRunType("CALIBRATION_SEMICENTRAL");
216         AddRunType("CALIBRATION");
217         AddRunType("PEDESTAL");
218         AddRunType("STANDALONE");
219         AddRunType("GAIN");
220         AddRunType("NOISE");
221         AddRunType("PULSER");
222         AddRunType("STANDALONE_PULSER");
223         AddRunType("STANDALONE_BC");
224
225         fmaxFloat = FLT_MAX;
226         fminFloat = -FLT_MAX;
227         fmaxDouble = DBL_MAX;
228         fminDouble = -DBL_MAX;
229         fmaxInt = kMaxInt;
230         fminInt = kMinInt;
231         fmaxUInt = kMaxUInt;
232         fminUInt = 0;
233
234         AliInfo(Form("Max allowed float = %6.5e",fmaxFloat));
235         AliInfo(Form("Min allowed float = %6.5e",fminFloat));
236         AliInfo(Form("Max allowed double = %6.5e",fmaxDouble));
237         AliInfo(Form("Min allowed double = %6.5e",fminDouble));
238         AliInfo(Form("Max allowed integer = %d",fmaxInt));
239         AliInfo(Form("Min allowed integer = %d",fminInt));
240         AliInfo(Form("Max allowed unsigned integer = %u",(Int_t)fmaxUInt));
241         AliInfo(Form("Min allowed unsigned integer = %u",(Int_t)fminUInt));
242
243         ffailedDPs->SetOwner(kTRUE);
244 }
245
246 //_______________________________________________________________
247
248 AliGRPPreprocessor::~AliGRPPreprocessor()
249 {
250         //destructor
251         
252         delete fPressure;
253         delete ffailedDPs;
254
255 }
256
257 //_______________________________________________________________
258
259 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
260 {
261         // Initialize preprocessor
262
263         AliPreprocessor::Initialize(run, startTime, endTime);
264         
265         AliInfo("Initialization of the GRP preprocessor.");
266         AliInfo(Form("Start Time DCS = %d",GetStartTimeDCSQuery()));
267         AliInfo(Form("End Time DCS = %d",GetEndTimeDCSQuery()));
268         TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
269         for(Int_t j = 0; j < kNumSensors; j++) {
270                 AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
271                 sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
272         }
273         AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
274         
275         fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
276
277         for (Int_t iDP=0; iDP < fgknDCSDP; iDP++){
278                 TObjString* dp = new TObjString(fgkDCSDataPoints[iDP]);
279                 ffailedDPs->AddAt(dp,iDP);
280         }
281
282 }
283
284 //_______________________________________________________________
285
286 UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
287 {
288         // process data retrieved by the Shuttle
289         
290         // retrieving "partition" and "detector" fields from DAQ logbook to 
291         // determine the partition in which the run was taken
292         // the partition is used to decide how to react in case of errors for CTP
293
294         TString partition = (TString)GetRunParameter("partition");  
295         TString detector = (TString)GetRunParameter("detector");   
296
297         AliGRPObject *grpobj = new AliGRPObject();  // object to store data
298         grpobj->SetBeamEnergyIsSqrtSHalfGeV(); // new format
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-4));
346         if (fdaqStartEndTimeOk){
347                 if( entries < fgknDCSDP-4 ) { // FIXME (!= ) LHState, LHCLuminosity, BeamIntensity, L3_BSF4_H3 are not working yet...  
348                         Log(Form("Problem with the DCS data points!!! Only %d/%d entries found",entries,fgknDCSDP-4));
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 != "LHCState" && name != "LHCLuminosity" && name != "BeamIntensity" && name != "L3_BSF4_H3"){
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         // storing AliGRPObject in OCDB
504
505         AliCDBMetaData md;
506         md.SetResponsible("Chiara Zampolli");
507         md.SetComment("Output parameters from the GRP preprocessor.");
508         
509         Bool_t result = kTRUE;
510         result = Store("GRP", "Data", grpobj, &md); 
511         delete grpobj;
512         
513         //==================//
514         // SPD Mean Vertex  //
515         //==================//
516         UInt_t iSPDMeanVertex = ProcessSPDMeanVertex();
517         if( iSPDMeanVertex == 1 ) {
518                 Log(Form("SPD Mean Vertex, successful!"));
519         } else {
520                 Log(Form("SPD Mean Vertex failed!!!"));
521                 error |= 128; 
522         }
523         
524         if (result && !error ) {
525                 Log("GRP Preprocessor Success");
526                 return 0;
527         } else {
528                 Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s%s%s",
529                           kppError[(error&1)?1:0],
530                           kppError[(error&2)?2:0],
531                           kppError[(error&4)?3:0],
532                           kppError[(error&8)?4:0],
533                           kppError[(error&16)?5:0],
534                           kppError[(error&32)?6:0],
535                           kppError[(error&64)?7:0],
536                           kppError[(error&128)?8:0]
537                           ));
538                 return error;
539         }
540 }
541
542 //_______________________________________________________________
543
544 UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
545 {
546         //Getting the SPD Mean Vertex
547         TList* list = GetForeignFileSources("SPD", kDAQ, "VertexDiamond");
548         Bool_t storeResult = kTRUE;
549         if (list !=0x0 && list->GetEntries()!=0)
550                 {
551                         AliInfo("The following sources produced files with the id VertexDiamond from SPD");
552                         list->Print();
553                         for (Int_t jj=0;jj<list->GetEntries();jj++){
554                                 TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
555                                 AliInfo(Form("found source %s", str->String().Data()));
556                                 TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
557                                 if (fileNameRun.Length()>0){
558                                         AliInfo(Form("Got the file %s", fileNameRun.Data()));
559                                         TFile daqFile(fileNameRun.Data(),"READ");
560                                         if (daqFile.IsOpen()) {
561                                                 AliESDVertex* meanVtx = dynamic_cast<AliESDVertex*>(daqFile.Get("MeanVertexPos"));
562                                                 if (meanVtx){
563                                                         meanVtx->Print();       
564                                                         // storing in the OCDB 
565                                                         AliCDBMetaData md;
566                                                         md.SetResponsible("Cvetan Cheshkov");
567                                                         md.SetComment("SPD Mean Vertex");                                       
568                                                         storeResult = Store("GRP", "MeanVertexSPD", meanVtx, &md); 
569                                                 }
570                                                 else{
571                                                         AliWarning("No SPD Mean Vertex object found in file");
572                                                 } 
573                                         }
574                                         else {
575                                                 AliError("Can't open file");
576                                                 storeResult = kFALSE;
577                                         }
578                                 }
579                                 else{
580                                         AliWarning("No file found for current source for SPD Mean Vertex");
581                                 }
582                         }
583                 }
584         else {
585                 AliWarning("No list found for SPD Mean Vertex");
586         }
587
588         if (list) delete list;
589
590         return storeResult;
591 }
592
593
594 //_______________________________________________________________
595
596 Int_t AliGRPPreprocessor::ProcessDaqLB(AliGRPObject* grpObj)
597 {
598         //Getting the DAQ lb information
599         
600         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
601         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
602         Float_t beamEnergy = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof());
603         TString beamType = (TString)GetRunParameter("beamType");
604         Char_t numberOfDetectors = (Char_t)(((TString)GetRunParameter("numberOfDetectors")).Atoi());
605         UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
606         TString lhcPeriod = (TString)GetRunParameter("LHCperiod");
607         TString runType = (TString)GetRunType();
608
609         UInt_t nparameter = 0;
610         if (timeStart != 0){
611                 grpObj->SetTimeStart(timeStart);
612                 Log(Form("Start time for run %d: %d",fRun, (Int_t)timeStart));
613                 nparameter++;
614         } 
615         else {
616                 Log(Form("Start time not put in logbook, setting to invalid in GRP entry!"));
617         }
618
619         if (timeEnd != 0){
620                 grpObj->SetTimeEnd(timeEnd);
621                 Log(Form("End time for run %d: %i",fRun, (Int_t)timeEnd));
622                 nparameter++;
623         } 
624         else {
625                 Log(Form("End time not put in logbook, setting to invalid in GRP entry!"));
626         }
627
628         if (beamEnergy != 0){
629                 grpObj->SetBeamEnergy(beamEnergy);
630                 Log(Form("Beam Energy for run %d: %f",fRun, beamEnergy));
631                 //if ((runType == "PHYSICS" && beamType!="Cosmics")){
632                 if ((runType == "PHYSICS" && !beamType.IsNull())){   // if beamType is NOT Null, then we're not in a Cosmics run
633                         nparameter++; // increasing nparameters only in case we're in PHYSICS runs with beamType != NULL
634                 }
635         } 
636         else {
637                 //if ((runType == "PHYSICS" && beamType!="Cosmics")){
638                 if ((runType == "PHYSICS" && !beamType.IsNull())){ // if beamType is NOT Null, then we're not in a Cosmics run
639                         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()));
640                 }
641                 else{
642                         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()));
643                 }
644         }
645
646                 
647         if (beamType.Length() != 0){
648                 grpObj->SetBeamType(beamType);
649                 Log(Form("Beam Type for run %d: %s",fRun, beamType.Data()));
650                 nparameter++; 
651         } 
652         else {
653                 Log(Form("Beam Type not put in logbook, setting to invalid in GRP entry! Not producing any error, considering this as a Cosmics run"));
654                 nparameter++;
655         }
656                 
657         if (numberOfDetectors != 0){
658                 grpObj->SetNumberOfDetectors(numberOfDetectors);
659                 Log(Form("Number Of Detectors for run %d: %d",fRun, (Int_t)numberOfDetectors));
660                 nparameter++;
661         } 
662         else {
663                 Log(Form("Number Of Detectors not put in logbook, setting to invalid in GRP entry!"));
664         }
665
666         if (detectorMask != 0){
667                 grpObj->SetDetectorMask(detectorMask);
668                 Log(Form("Detector Mask for run %d: %d",fRun, detectorMask));
669                 nparameter++;
670         } 
671         else {
672                 Log(Form("Detector Mask not put in logbook, setting to invalid in GRP entry!"));
673         }
674
675         if (lhcPeriod.Length() != 0) {
676                 grpObj->SetLHCPeriod(lhcPeriod);
677                 Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod.Data()));
678                 nparameter++;
679         } 
680         else {
681                 Log(Form("LHCperiod not put in logbook, setting to invalid in GRP entry!"));
682         }
683         if (runType.Length() != 0) {
684                 grpObj->SetRunType(runType);
685                 Log(Form("Run Type (DAQ) for run %d: %s",fRun, runType.Data()));
686                 nparameter++;
687         } 
688         else {
689                 Log(Form("Run Type not put in logbook, setting to invalid in GRP entry!"));
690         }
691
692         return nparameter;
693 }
694
695 //_______________________________________________________________
696
697 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
698 {
699         //======DAQ FXS======//
700         
701         AliRawEventHeaderV3_9::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_9 - temporary fix 
702         TList* list = GetFileSources(kDAQ);  
703         if (!list) {
704                 Log("No raw data tag list: connection problems with DAQ FXS logbook!");
705                 return 1;
706         }
707         
708         if (list->GetEntries() == 0) {
709                 Log("no raw data tags in this run: nothing to merge!");
710                 delete  list; list=0;
711                 return 0;
712         }
713         
714         TChain *fRawTagChain = new TChain("T");
715         Int_t nFiles=0;
716         TIterator* iter = list->MakeIterator();
717         TObject* obj = 0;
718         while ((obj = iter->Next())) {
719                 TObjString* objStr = dynamic_cast<TObjString*> (obj);
720                 if (objStr) {
721                         Log(Form("Found source %s", objStr->String().Data()));
722                         TList* list2 = GetFileIDs(kDAQ, objStr->String());
723                         if (!list2) {
724                                 Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
725                                 delete fRawTagChain; fRawTagChain=0;
726                                 return 1;
727                         }
728                         Log(Form("Number of ids: %d",list2->GetEntries()));
729                         for(Int_t i = 0; i < list2->GetEntries(); i++) {
730                                 TObjString *idStr = (TObjString *)list2->At(i);
731                                 TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
732                                 if (fileName.Length() > 0) {
733                                         Log(Form("Adding file in the chain: %s",fileName.Data()));
734                                         fRawTagChain->Add(fileName.Data());
735                                         nFiles++;
736                                 } else {
737                                         Log(Form("Could not retrieve file with id %s from source %s: "
738                                                  "connection problems with DAQ FXS!",
739                                                  idStr->String().Data(), objStr->String().Data()));
740                                         delete list; list=0;
741                                         delete list2; list2=0;
742                                         delete fRawTagChain; fRawTagChain=0;
743                                         return 2;
744                                 }
745                         }
746                         delete list2;
747                 }
748         }
749         
750         TString fRawDataFileName = "GRP_Merged.tag.root";
751         Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
752         
753         if( fRawTagChain->Merge(fRawDataFileName) < 1 ) {
754                 Log("Error merging raw data files!!!");
755                 return 3;
756         }
757         
758         TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
759         Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
760         
761         if (!result) {
762                 Log("Problem storing raw data tags in local file!!!");
763         } else {
764                 Log("Raw data tags merged successfully!!");
765         }
766         
767         delete iter;
768         delete list;
769         delete fRawTagChain; fRawTagChain=0;
770         
771         if (result == kFALSE) {
772                 return 4;
773         }
774         
775         return 0;
776         
777 }
778
779 //_______________________________________________________________
780 UInt_t AliGRPPreprocessor::ProcessDcsFxs(TString partition, TString detector)
781 {
782
783         // processing the info
784         // stored in the DCS FXS
785         // coming from the trigger
786
787         // Get the CTP counters information
788
789         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
790                 Log("STANDALONE partition for current run, using Trigger Scalers dummy value");
791                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyScalers");
792                 if (!cdbEntry) {
793                         Log(Form("No dummy CTP scalers entry found, going into error..."));
794                         return 1;
795                 }
796                 else{
797                         AliTriggerRunScalers *scalers = (AliTriggerRunScalers*)cdbEntry->GetObject();
798                         if (!scalers){
799                                 Log(Form("CTP dummy scalers not found in OCDB entry, going into error..."));
800                                 return 1;
801                         }
802                         else {
803                                 AliCDBMetaData metaData;
804                                 metaData.SetResponsible("Roman Lietava");
805                                 metaData.SetComment("CTP scalers from dummy entry in OCDB");
806                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
807                                         Log("Unable to store the dummy CTP scalers object to OCDB!");
808                                         return 1;
809                                 }
810                         }
811                 }
812         }
813
814         else if (!partition.IsNull() && detector.IsNull()){ // global partition
815                 Log("GLOBAL partition for current run, using CTP scalers from DCS FXS");
816                 TString countersfile = GetFile(kDCS, "CTP_xcounters","");
817                 if (countersfile.IsNull()) {
818                         Log("No CTP counters files has been found: empty source!");
819                         return 1;
820                 }
821                 else {
822                         Log(Form("File with Id CTP_xcounters found in DCS FXS! Copied to %s",countersfile.Data()));
823                         AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
824                         if (!scalers) {
825                                 Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
826                                 return 1;
827                         }
828                         else {
829                                 AliCDBMetaData metaData;
830                                 metaData.SetBeamPeriod(0);
831                                 metaData.SetResponsible("Roman Lietava");
832                                 metaData.SetComment("CTP scalers");
833                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
834                                         Log("Unable to store the CTP scalers object to OCDB!");
835                                         return 1;
836                                 }
837                         }
838                 }
839         }
840         
841
842         else{   
843                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error...",partition.Data(),detector.Data()));
844                 return 2;
845         }
846
847         return 0;
848
849 }
850 //_______________________________________________________________
851
852 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
853 {
854
855         //
856         // processing DCS DPs
857         //
858
859         Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
860         Int_t nLHCEntries = 0;
861         Int_t nL3Entries = 0;
862         Int_t nDipoleEntries = 0;
863         Int_t nEnvEntries = 0;
864         Int_t nHallProbesEntries = 0;
865         nLHCEntries = ProcessLHCDPs(valueMap, grpObj);
866         nL3Entries = ProcessL3DPs(valueMap, grpObj);
867         nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
868         nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
869         nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
870         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
871         Log(Form("nLHCEntries = %d, L3Entries = %d, nDipoleEntries =%d, nEnvEntries = %d, nHallProbesEntries = %d", nLHCEntries, nL3Entries, nDipoleEntries, nEnvEntries, nHallProbesEntries));
872         entries = nLHCEntries + nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
873         return entries;
874
875 }
876
877 //_______________________________________________________________
878
879 Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpObj)
880 {
881
882         // processing DPs
883         // related to 
884         // L3 info
885
886         Int_t nL3Entries = 0;
887
888         TObjArray *array = 0x0;
889         Int_t indexDP = -1;
890         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
891
892         AliInfo(Form("==========L3Current==========="));
893         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
894         indexDP = kL3Current;
895         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
896         if(!array) {
897                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
898         } 
899         else {
900                 if (array->GetEntries() == 0){
901                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
902                 }
903                 else {
904                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
905                         if (floatDCS != NULL){
906                                 grpObj->SetL3Current(floatDCS);
907                         }
908                         else{
909                                 outOfRange = kTRUE;
910                         }       
911                         if (floatDCS){
912                                 delete[] floatDCS;
913                                 floatDCS = 0x0;
914                         }
915                 }
916                 if (!outOfRange) {
917                         nL3Entries++;
918                         ffailedDPs->RemoveAt(indexDP);
919                 }
920         }
921
922         if (array) array = 0x0;
923
924         AliInfo(Form("==========L3Polarity==========="));
925         indexDP = kL3Polarity;
926         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
927         if(!array) {
928                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
929         } 
930         else {
931                 if (array->GetEntries() == 0){
932                         AliError(Form("No entries found in array! setting %s Polarity to invalid...",fgkDCSDataPoints[indexDP]));
933                 }
934                 else {
935                         Bool_t change = kFALSE;
936                         Char_t charDCS = ProcessBool(array,change);
937                         if (change == kFALSE){
938                                 grpObj->SetL3Polarity(charDCS);
939                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
940                                 ffailedDPs->RemoveAt(indexDP);
941                                 nL3Entries++;
942                         }
943                         else if (isZero){
944                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
945                                 ffailedDPs->RemoveAt(indexDP);
946                                 nL3Entries++;
947                         }
948                         else {
949                                 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]));
950                         }
951                 }
952         }
953
954         return nL3Entries;
955
956 }
957 //_______________________________________________________________
958
959 Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* grpObj)
960 {
961         // processing DPs
962         // related to 
963         // the Dipole info
964
965         Int_t nDipoleEntries = 0;
966         TObjArray *array = 0x0;
967         Int_t indexDP = -1;
968         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
969
970         AliInfo(Form("==========DipoleCurrent==========="));
971         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
972         indexDP = kDipoleCurrent;
973         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
974         if(!array) {
975                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
976         } 
977         else {
978                 if (array->GetEntries() == 0){
979                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
980                 }
981                 else {
982                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
983                         if (floatDCS != NULL){
984                                 grpObj->SetDipoleCurrent(floatDCS);
985                         } 
986                         else{
987                                 outOfRange=kTRUE;
988                         }
989                         if (floatDCS){
990                                 delete[] floatDCS;
991                                 floatDCS = 0x0;
992                         }
993                 }
994                 if (!outOfRange) {
995                         nDipoleEntries++;
996                         ffailedDPs->RemoveAt(indexDP);
997                 }
998         }
999
1000         if (array) array = 0x0;
1001
1002         AliInfo(Form("==========DipolePolarity==========="));
1003         indexDP = kDipolePolarity;
1004         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1005         if(!array) {
1006                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1007         } 
1008         else {
1009                 if (array->GetEntries() == 0){
1010                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1011                 }
1012                 else {
1013                         Bool_t change = kFALSE;
1014                         Char_t charDCS = ProcessBool(array,change);
1015                         if (!change){
1016                                 grpObj->SetDipolePolarity(charDCS);
1017                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
1018                                 ffailedDPs->RemoveAt(indexDP);
1019                                 nDipoleEntries++;
1020                         }
1021                         else if (isZero){
1022                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1023                                 ffailedDPs->RemoveAt(indexDP);
1024                                 nDipoleEntries++;
1025                         }
1026                         else{
1027                                 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]));
1028                         }
1029                 }
1030         }
1031
1032         return nDipoleEntries;
1033
1034 }
1035 //_______________________________________________________________
1036
1037 Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
1038 {
1039         // processing DPs
1040         // related to 
1041         // evironment conditions (temperature, pressure) info
1042
1043         Int_t nEnvEntries = 0;
1044         TObjArray *array = 0x0;
1045         Int_t indexDP = -1;
1046
1047         AliInfo(Form("==========CavernTemperature==========="));
1048         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1049         indexDP = kCavernTemperature;
1050         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1051         if(!array) {
1052                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1053         } 
1054         else {
1055                 if (array->GetEntries() == 0){
1056                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1057                 }
1058                 else {
1059                         Float_t *floatDCS = ProcessFloatAll(array);
1060                         if (floatDCS != NULL){
1061                                 grpObj->SetCavernTemperature(floatDCS);
1062                         }
1063                         else{
1064                                 outOfRange = kTRUE;
1065                         }
1066                         if (floatDCS){
1067                                 delete[] floatDCS;
1068                                 floatDCS = 0x0;
1069                         }
1070                 }
1071                 if (!outOfRange) {
1072                         ffailedDPs->RemoveAt(indexDP);
1073                         nEnvEntries++;
1074                 }
1075         }
1076
1077         if (array) array = 0x0;
1078
1079         AliInfo(Form("========== AtmosPressures (Cavern + Surface + Cavern2) ==========="));
1080         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
1081         //dcsSensorArray->Print();
1082         if( fPressure->NumFits()<kNumSensors ) {
1083                 Log(Form("Check the pressure sensor values! Not all the %d pressure sensors have been fit",kNumSensors));
1084         } 
1085         Log(Form("Number of fits performed = %d",fPressure->NumFits()));
1086
1087         AliInfo(Form("==========CavernAtmosPressure==========="));
1088         indexDP = kCavernAtmosPressure;
1089         AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1090         TGraph* graph = sensorCavernP2->GetGraph();
1091         AliDebug(3,Form("index = %d",indexDP));
1092         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1093         AliDebug(2,Form("graph = %p",graph));
1094         AliDebug(3,Form("sensorCavernP2 = %p", sensorCavernP2));
1095         if(sensorCavernP2->GetFit() || graph) {
1096                 if (sensorCavernP2->GetFit()){
1097                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1098                 }
1099                 else {
1100                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1101                 }
1102                 grpObj->SetCavernAtmosPressure(sensorCavernP2);
1103                 ffailedDPs->RemoveAt(indexDP);
1104                 nEnvEntries++;
1105         } 
1106         //if (sensorP2) delete sensorP2;
1107         else {
1108                 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] ));
1109         }
1110         
1111         AliInfo(Form("==========SurfaceAtmosPressure==========="));
1112         indexDP = kSurfaceAtmosPressure;
1113         AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1114         graph = sensorP2->GetGraph();
1115         AliDebug(3,Form("index = %d",indexDP));
1116         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1117         AliDebug(2,Form("graph = %p",graph));   
1118         AliDebug(3,Form("sensorP2 = %p", sensorP2));
1119         if(sensorP2->GetFit() || graph) {
1120                 if (sensorP2->GetFit()){
1121                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1122                 }
1123                 else {
1124                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1125                 }
1126                 grpObj->SetSurfaceAtmosPressure(sensorP2);
1127                 ffailedDPs->RemoveAt(indexDP);
1128                 nEnvEntries++;
1129         } 
1130         //if (sensorP2) delete sensorP2;
1131         else {
1132                 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] ));
1133         }
1134
1135         AliInfo(Form("==========CavernAtmosPressure2==========="));
1136         indexDP = kCavernAtmosPressure2;
1137         AliDCSSensor* sensorCavernP22 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1138         graph = sensorCavernP22->GetGraph();
1139         AliDebug(3,Form("index = %d",indexDP));
1140         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1141         AliDebug(2,Form("graph = %p",graph));   
1142         AliDebug(3,Form("sensorCavernP2_2 = %p", sensorCavernP22));
1143         if(sensorCavernP22->GetFit() || graph) {
1144                 if (sensorCavernP22->GetFit()){
1145                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1146                 }
1147                 else {
1148                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1149                 }
1150                 grpObj->SetCavernAtmosPressure2(sensorCavernP22);
1151                 ffailedDPs->RemoveAt(indexDP);
1152                 nEnvEntries++;
1153         } 
1154         //if (sensorP2) delete sensorP2;
1155         else {
1156                 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] ));
1157         }
1158         
1159         
1160         return nEnvEntries;
1161 }
1162 //_______________________________________________________________
1163
1164 Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpObj)
1165 {
1166         // processing DPs
1167         // related to 
1168         // Hall Probes info
1169
1170         Int_t nHPEntries = 0;
1171         TObjArray *array = 0x0;
1172         Int_t indexDP = -1;
1173         Bool_t outOfRange; // flag to monitor if any value collected by DCS is out of range
1174
1175         if (fgknDCSDPHallProbes != AliGRPObject::GetNumberOfHP()){
1176                 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()));
1177         }
1178         for (indexDP = 0; indexDP < AliGRPObject::GetNumberOfHP(); indexDP++){
1179                 outOfRange = kFALSE; // resetting outOfRange flag at each HP
1180                 AliInfo(Form("==========%s===========",AliGRPObject::GetHPDP(indexDP)));
1181                 array = (TObjArray *)valueMap->GetValue(AliGRPObject::GetHPDP(indexDP));
1182                 if(!array) {
1183                         Log(Form("%s not found in the map!!!",AliGRPObject::GetHPDP(indexDP)));
1184                 } 
1185                 else {
1186                         if (array->GetEntries() == 0){
1187                                 AliError(Form("No entries found in array! setting %s to invalid...",AliGRPObject::GetHPDP(indexDP)));
1188                         }
1189                         else {
1190                                 Float_t *floatDCS = ProcessFloatAll(array);
1191                                 if (floatDCS != NULL){
1192                                         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])); 
1193                                         grpObj->SetHallProbes((AliGRPObject::DP_HallProbes)indexDP,floatDCS);
1194                                         for (Int_t kk = 0 ; kk< 5; kk++){
1195                                                 AliDebug(2,Form("HallProbe[%d][%d]=%f",indexDP,kk,grpObj->GetHallProbes((AliGRPObject::DP_HallProbes)indexDP,(AliGRPObject::Stats)kk)));
1196                                         }
1197                                 }
1198                                 else{
1199                                         outOfRange = kTRUE;
1200                                 }
1201                                 if (floatDCS){
1202                                         delete[] floatDCS;
1203                                         floatDCS = 0x0;
1204                                 }
1205                         }
1206                         if (!outOfRange) {
1207                                 ffailedDPs->RemoveAt(indexDP + 7);  // 7 = shift in the complete list of DPs to get to the Hall Probes
1208                                 nHPEntries++;
1209                         }
1210                 }
1211         }
1212                 
1213         Log(Form("Hall Probes = %d ", nHPEntries));
1214         return nHPEntries;
1215 }
1216
1217 //_______________________________________________________________
1218
1219 Int_t AliGRPPreprocessor::ProcessLHCDPs(const TMap* valueMap, AliGRPObject* grpObj)
1220 {
1221
1222         //
1223         // processing of LHC related DCS DPs, i.e.:
1224         // LHCState
1225         // LHCLuminosity
1226         // BeamIntensity
1227         //
1228
1229         Int_t nLHCEntries = 0;
1230         TObjArray *array = 0x0;
1231         Int_t indexDP = -1;
1232
1233         AliInfo(Form("==========LHCState==========="));
1234         indexDP = kLHCState;
1235         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1236         if(!array) {
1237                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1238         } 
1239         else {
1240                 if (array->GetEntries() == 0){
1241                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1242                 }
1243                 else {
1244                         TString stringDCS = ProcessChar(array);
1245                         if (stringDCS.Length()!=0) {
1246                                 Bool_t found = kFALSE;
1247                                 for( Int_t i=0; i<20; i+=2 ) {
1248                                         if( stringDCS.CompareTo(fgkLHCState[i]) == 0 ) {
1249                                                 stringDCS = fgkLHCState[i+1];
1250                                                 found = kTRUE;
1251                                                 break;
1252                                         }
1253                                 }
1254                                 if (found){
1255                                         Log(Form("<%s> for run %d: %s",fgkDCSDataPoints[indexDP],fRun, stringDCS.Data()));
1256                                         grpObj->SetLHCState(stringDCS);
1257                                 }
1258                                 else{
1259                                         Log(Form("%s values found not valid!",fgkDCSDataPoints[indexDP]));
1260                                         grpObj->SetLHCState(AliGRPObject::GetInvalidString());
1261                                 } 
1262                         }
1263                         else {
1264                                 Log(Form("%s not valid (null length), string set as invalid!",fgkDCSDataPoints[indexDP]));
1265                                 grpObj->SetLHCState(AliGRPObject::GetInvalidString());
1266                         }         
1267                 }
1268                 ffailedDPs->RemoveAt(indexDP);
1269                 nLHCEntries++;
1270         }
1271         
1272         if (array) array = 0x0;
1273
1274         AliInfo(Form("==========LHCLuminosity==========="));
1275         Bool_t outOfRange = kFALSE; // flag to monitor if any value collected by DCS is out of range
1276         indexDP = kLHCLuminosity;
1277         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1278         if(!array) {
1279                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1280         } 
1281         else {
1282                 if (array->GetEntries() == 0){
1283                         AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
1284                 }
1285                 else {
1286                         Float_t *floatDCS = ProcessFloatAll(array);
1287                         if (floatDCS != NULL){
1288                                 grpObj->SetLHCLuminosity(floatDCS);
1289                                 AliSplineFit* splfit = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
1290                                 grpObj->SetLHCLuminositySplineFit(splfit);
1291                         //              delete splfit;
1292                         }
1293                         else {
1294                                 outOfRange = kTRUE;
1295                         }
1296                         if (floatDCS){
1297                                 delete[] floatDCS;
1298                                 floatDCS = 0x0;
1299                         }
1300                 }
1301                 if (!outOfRange) {
1302                         ffailedDPs->RemoveAt(indexDP);
1303                         nLHCEntries++;
1304                 }
1305         }
1306
1307         if (array) array = 0x0;
1308
1309         AliInfo(Form("==========BeamIntensity==========="));
1310         if (outOfRange) outOfRange = kFALSE;  // resetting outOfRange if needed
1311         indexDP = kBeamIntensity;
1312         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1313         if(!array) {
1314                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1315         } 
1316         else {
1317                 if (array->GetEntries() == 0){
1318                         AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
1319                 }
1320                 else {
1321                         Float_t *floatDCS = ProcessFloatAll(array);
1322                         if (floatDCS != NULL){
1323                                 grpObj->SetBeamIntensity(floatDCS);
1324                                 AliSplineFit* splfit1 = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
1325                                 grpObj->SetBeamIntensitySplineFit(splfit1);
1326                                 //delete splfit;
1327                         }
1328                         else{
1329                                 outOfRange = kTRUE;
1330                         }
1331                         if (floatDCS){
1332                                 delete[] floatDCS;
1333                                 floatDCS = 0x0;
1334                         }
1335                 }
1336                 if (!outOfRange) {
1337                         nLHCEntries++;
1338                         ffailedDPs->RemoveAt(indexDP);
1339                 }
1340         }
1341
1342         return nLHCEntries;
1343 }
1344 //_________________________________________________________________________
1345
1346 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
1347
1348
1349         // 
1350         // returning Spline Fit 
1351         // 
1352
1353         Int_t entriesarray = array->GetEntries();
1354         Float_t* value = new Float_t[entriesarray];
1355         Float_t* time = new Float_t[entriesarray];
1356         AliDCSValue* v = 0x0;
1357         for (Int_t iarray = 0; iarray < entriesarray; iarray++){
1358                 v = (AliDCSValue*)array->At(iarray);
1359                 value[iarray] = v->GetFloat();
1360                 time[iarray] = v->GetTimeStamp();
1361                 AliDebug(2,Form("iarray = %d, value = %f, time = %f",iarray,value[iarray],time[iarray]));
1362         }
1363         TGraph* gr = new TGraph(entriesarray,value,time);
1364         if (!gr ) {
1365                 AliWarning(Form("%s: no input graph to compute SplineFit",stringID.Data()));
1366                 return NULL;
1367         }
1368         AliSplineFit *fit = new AliSplineFit();
1369         fit->SetMinPoints(10);
1370         fit->InitKnots(gr,10,10,0.0);
1371         fit->SplineFit(2);
1372         fit->Cleanup();
1373         if (!fit) {
1374                 AliWarning(Form("%s: no fit performed",stringID.Data()));
1375                 return NULL;
1376         } 
1377         return fit;
1378 }
1379
1380 //_________________________________________________________________________
1381
1382 TString AliGRPPreprocessor::ProcessChar(const TObjArray *array)
1383 {
1384
1385         // 
1386         // processing char
1387         //
1388
1389         TString aDCSString="";
1390         
1391         AliDCSValue *v = 0x0;
1392         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1393                 v = (AliDCSValue *)array->At(iCount);
1394                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1395                         AliError(Form("DCS values for the parameter outside the queried interval"));
1396                         continue;
1397                 }
1398                 if (iCount > 0) {
1399                         if (aDCSString != v->GetChar())
1400                         AliError(Form("DCS values for the parameter changed from %s to %c within the queried interval", aDCSString.Data(), (Char_t)v->GetChar()));
1401                 }
1402                 aDCSString = (TString)v->GetChar();  // keeping always last value in the array
1403         }
1404         return aDCSString;
1405 }
1406
1407 //__________________________________________________________________________________________________________________
1408
1409 Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
1410 {
1411         // 
1412         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1413         //
1414         // parameters[0] = mean
1415         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1416         // parameters[2] = median
1417         // parameters[3] = standard deviation wrt mean
1418         // parameters[4] = standard deviation wrt median
1419         //
1420
1421         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1422         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1423         if (timeStartString.IsNull() || timeStartString.IsNull()){
1424                 if (timeStartString.IsNull()){ 
1425                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1426                 }
1427                 else if (timeStartString.IsNull()){
1428                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1429                 }
1430                 fdaqStartEndTimeOk = kFALSE;
1431                 return 0;
1432         }  
1433
1434         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1435         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1436         Float_t* parameters = new Float_t[5];
1437         Int_t iCounts = 0;
1438         Int_t iCountsRun = 0;
1439         Int_t nCounts = array->GetEntries();
1440         Float_t valueBeforeSOR = 0;
1441         Float_t valueAfterEOR = 0;
1442         Int_t timestampBeforeSOR = -1;
1443         Int_t timestampAfterEOR = -1;
1444         Int_t ientrySOR = -1;
1445         Int_t ientryEOR = -1;
1446         Float_t* arrayValues = 0x0; 
1447         Double_t* arrayWeights = 0x0; 
1448         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
1449         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
1450
1451         for(Int_t i = 0; i < nCounts; i++) {
1452                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1453                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1454                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1455                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1456                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1457                         return NULL;
1458                 }
1459                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1460                         AliDebug(2,Form("%d-th entry = %f at timestamp %i",i,v->GetFloat(),v->GetTimeStamp()));
1461                         iCounts += 1;
1462                         // look for the last value before SOR and the first value before EOR
1463                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1464                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1465                                 AliDebug(2,Form("timestamp of last value before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1466                                 valueBeforeSOR = v->GetFloat();
1467                         }
1468                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1469                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1470                                 valueAfterEOR = v->GetFloat();
1471                                 AliDebug(2,Form("timestamp of first value after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1472                         }
1473                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1474                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1475                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1476                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1477                                 AliDebug(2,Form("entry between SOR and EOR"));
1478                                 iCountsRun += 1;
1479                         }
1480                 }
1481                 else {
1482                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1483                 }
1484         }
1485
1486         if (timestampBeforeSOR == -1){
1487                 AliWarning("No value found before SOR");
1488         }
1489         if (timestampAfterEOR == -1){
1490                 AliWarning("No value found after EOR");
1491         }
1492
1493         AliDebug(2,Form("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries",iCounts,nCounts));
1494         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1495         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1496         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1497         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1498         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1499
1500         Int_t nentriesUsed = 0;
1501         if (iCountsRun > 1){
1502                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1503                 AliDebug(2,"Calculating (weighted) Mean and Median");
1504                 arrayValues = new Float_t[iCountsRun]; 
1505                 arrayWeights = new Double_t[iCountsRun]; 
1506                 nentriesUsed = iCountsRun;
1507                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1508                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1509                         Int_t timestamp2 = 0;
1510                         if (i < ientryEOR){
1511                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1512                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1513                         }
1514                         else {
1515                                 timestamp2 = timeEnd+1;
1516                         }
1517                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1518                         arrayValues[i-ientrySOR] = v->GetFloat();
1519                 }
1520                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1521                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
1522         }
1523         else if (iCountsRun == 1){
1524                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1525                 nentriesUsed = 2;
1526                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1527                         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.");
1528                         arrayValues = new Float_t[2];
1529                         arrayWeights = new Double_t[2];
1530                         arrayValues[0] = valueBeforeSOR;
1531                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1532                         arrayValues[1] = v->GetFloat();
1533                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1534                         AliDebug(2, Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1535                         AliDebug(2, Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1536                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
1537                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
1538                         truncMeanFlag = kFALSE;
1539                 }
1540                 else{
1541                         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");
1542                         parameters[0] = AliGRPObject::GetInvalidFloat();
1543                         parameters[1] = AliGRPObject::GetInvalidFloat();
1544                         parameters[2] = AliGRPObject::GetInvalidFloat();
1545                         parameters[3] = AliGRPObject::GetInvalidFloat();
1546                         parameters[4] = AliGRPObject::GetInvalidFloat();
1547                         return parameters;
1548                 }
1549         }
1550         else { // iCountsRun == 0, using only the point immediately before SOR
1551                 if (timestampBeforeSOR == -1){
1552                         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");
1553                         parameters[0] = AliGRPObject::GetInvalidFloat();
1554                         parameters[1] = AliGRPObject::GetInvalidFloat();
1555                         parameters[2] = AliGRPObject::GetInvalidFloat();
1556                         parameters[3] = AliGRPObject::GetInvalidFloat();
1557                         parameters[4] = AliGRPObject::GetInvalidFloat();
1558                         return parameters;
1559                 }
1560                 else {
1561                         AliWarning("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.");
1562                         AliDebug(2,Form("value = %f",valueBeforeSOR)); 
1563                         parameters[0] = valueBeforeSOR;
1564                         parameters[2] = valueBeforeSOR;
1565                         truncMeanFlag = kFALSE;
1566                         sdFlag = kFALSE;
1567                 }
1568         }
1569
1570         Float_t temp = 0;
1571         Float_t temp1 = 0;
1572         Float_t sumweights = 0; 
1573         Int_t entriesTruncMean = 0;
1574         Float_t* arrayValuesTruncMean = new Float_t[nentriesUsed]; 
1575         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
1576
1577         // calculating SD wrt Mean and Median
1578         AliDebug(2,"Calculating SD wrt Mean and SD wrt Median");
1579         if (sdFlag){
1580                 for (Int_t i =0; i< nentriesUsed; i++){
1581                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1582                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
1583                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
1584                         sumweights += arrayWeights[i];
1585                 }
1586                 // setting SD wrt Mean 
1587                 if (sumweights != 0 ){
1588                         parameters[3] = TMath::Sqrt(temp1/sumweights);
1589                 }
1590                 else {
1591                         AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
1592                         parameters[3] = AliGRPObject::GetInvalidFloat();
1593                 }
1594                 // setting SD wrt Median
1595                 if (nentriesUsed != 0){
1596                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
1597                 }
1598                 else{
1599                         AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
1600                         parameters[4] = AliGRPObject::GetInvalidFloat();
1601                 }
1602         }
1603         else {
1604                 parameters[3] = AliGRPObject::GetInvalidFloat();
1605                 parameters[4] = AliGRPObject::GetInvalidFloat();
1606         }               
1607
1608         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
1609         if (truncMeanFlag){
1610                 AliDebug(2,"Calculating Truncated Mean");
1611                 for (Int_t i =0; i< nentriesUsed; i++){
1612                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1613                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
1614                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
1615                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
1616                                 AliDebug(2,Form("For Truncated Mean: Entry %d: value = %f, weight = %f",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]));
1617                                 entriesTruncMean++;                     
1618                         }
1619                         else{
1620                                 AliDebug(2,"Discarding entry");
1621                         }
1622                 }
1623                 // setting truncated mean 
1624                 if (entriesTruncMean >1){
1625                         AliDebug(2,Form("%d entries used for truncated mean",entriesTruncMean));
1626                         parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
1627                 }
1628                 else{   
1629                         AliDebug(2,Form("Too few entries (%d) to calculate truncated mean",entriesTruncMean));
1630                         parameters[1] = AliGRPObject::GetInvalidFloat();
1631                 }
1632         }
1633         else{
1634                         parameters[1] = AliGRPObject::GetInvalidFloat();
1635         }
1636
1637         AliInfo(Form("(weighted) mean = %f ",parameters[0]));
1638         AliInfo(Form("(weighted) truncated mean = %f ",parameters[1]));
1639         AliInfo(Form("median = %f ",parameters[2]));
1640         AliInfo(Form("(weighted) standard deviation with (weighted) mean = %f ",parameters[3]));
1641         AliInfo(Form("standard deviation with median = %f ",parameters[4]));
1642         
1643         return parameters;
1644 }
1645
1646 //__________________________________________________________________________________________________________________
1647
1648 Float_t* AliGRPPreprocessor::ProcessFloatAllMagnet(const TObjArray* array, Int_t indexDP, Bool_t &isZero)
1649 {
1650         // 
1651         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1652         // used for L3 and Dipole magnets, using isZero flag to decide whther the magnet was OFF/ON
1653         // the flag is set according to the L3/Dipole current value
1654         // current threshold for L3 = 350 A (value provided by DCS)
1655         // current threshold for Dipole = 450 A (value provided by DCS)
1656         //
1657         // parameters[0] = mean
1658         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1659         // parameters[2] = median
1660         // parameters[3] = standard deviation wrt mean
1661         // parameters[4] = standard deviation wrt median
1662         //
1663
1664         AliInfo(Form("indexDP = %d",indexDP)); 
1665
1666         Int_t nCounts = array->GetEntries();
1667         for(Int_t i = 0; i < nCounts; i++) {
1668                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1669                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1670                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1671                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1672                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1673                         return NULL;
1674                 }
1675                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1676                         AliDebug(2,Form("%d-th entry = %f",i,v->GetFloat()));
1677                         if (indexDP == kL3Current && v->GetFloat() > 350 && isZero == kTRUE) isZero=kFALSE; 
1678                         if (indexDP == kDipoleCurrent && v->GetFloat() > 450 && isZero == kTRUE) isZero=kFALSE; 
1679                 }
1680                 else {
1681                         AliError(Form("DCS values for the parameter outside the queried interval"));
1682                 }
1683         }
1684
1685         return ProcessFloatAll(array);
1686 }
1687
1688
1689 //_______________________________________________________________
1690
1691 Char_t AliGRPPreprocessor::ProcessBool(const TObjArray* array, Bool_t &change)
1692 {
1693         // 
1694         // processing Boolean values
1695         //
1696
1697         Bool_t aDCSBool = kTRUE;
1698
1699         AliDCSValue *v = 0x0;
1700
1701         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1702                 v = (AliDCSValue *)array->At(iCount);
1703                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1704                         AliError(Form("DCS values for the parameter outside the queried interval"));
1705                         continue;
1706                 }
1707                 if (iCount > 0) {
1708                         if (aDCSBool != v->GetBool()) {
1709                                 AliError(Form("DCS values for the parameter changed from %d to %d within the queried interval", (UInt_t)aDCSBool, (UInt_t)v->GetBool()));
1710                                 change = kTRUE;
1711                         }
1712                 }
1713                 aDCSBool = v->GetBool(); // always keeping last value
1714                 AliDebug(2,Form("Bool = %d",(Int_t)aDCSBool));
1715         }
1716         
1717         Char_t caDCSBool = (Char_t) aDCSBool;
1718         return caDCSBool;
1719         
1720 }
1721
1722 //_______________________________________________________________
1723
1724 Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
1725 {
1726         // 
1727         // processing Int values, returning mean
1728         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
1729         // are outside the queried time interval or their value is out of range
1730         //
1731
1732         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1733         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1734         if (timeStartString.IsNull() || timeStartString.IsNull()){
1735                 if (timeStartString.IsNull()){ 
1736                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1737                 }
1738                 else if (timeStartString.IsNull()){
1739                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1740                 }
1741                 return 0;
1742         }  
1743
1744         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1745         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1746         Float_t aDCSArrayMean = 0.0;
1747         Int_t iCounts = 0;
1748         Float_t valueBeforeSOR = 0;
1749         Float_t valueAfterEOR = 0;
1750         Int_t timestampBeforeSOR = -1;
1751         Int_t timestampAfterEOR = -1;
1752         Int_t ientrySOR = -1;
1753         Int_t ientryEOR = -1;
1754         Float_t* arrayValues = 0x0; 
1755         Double_t* arrayWeights = 0x0; 
1756         Int_t iCountsRun = 0;
1757         Int_t nCounts = array->GetEntries();
1758
1759         for(Int_t i = 0; i < nCounts; i++) {
1760                 AliDCSValue* v = (AliDCSValue *)array->At(i);
1761                 if ((v->GetInt() < fminInt) || (v->GetInt() > fmaxInt)) {
1762                         AliError(Form("Error! Int value found in DCS map at %d-th entry is OUT OF RANGE: value = %d",i, v->GetInt()));
1763                         return AliGRPObject::GetInvalidFloat();
1764                 }
1765                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1766                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetInt(),v->GetTimeStamp()));
1767                         iCounts += 1;
1768                         // look for the last value before SOR and the first value before EOR
1769                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1770                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1771                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1772                                 valueBeforeSOR = (Float_t) v->GetInt();
1773                         }
1774                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1775                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1776                                 valueAfterEOR = (Float_t) v->GetInt();
1777                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1778                         }
1779                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1780                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1781                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1782                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1783                                 AliDebug(2,Form("entry between SOR and EOR"));
1784                                 iCountsRun += 1;
1785                         }
1786                 }
1787                 else {
1788                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1789                 }
1790         }
1791
1792         if (timestampBeforeSOR == -1){
1793                 AliWarning("No value found before SOR!");
1794         }
1795         if (timestampAfterEOR == -1){
1796                 AliWarning("No value found after EOR!");
1797         }
1798
1799         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
1800         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1801         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1802         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1803         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1804         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1805
1806         Int_t nentriesUsed = 0;
1807         if (iCountsRun > 1){
1808                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1809                 AliDebug(2,"Calculating (weighted) Mean");
1810                 arrayValues = new Float_t[iCountsRun]; 
1811                 arrayWeights = new Double_t[iCountsRun]; 
1812                 nentriesUsed = iCountsRun;
1813                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1814                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1815                         Int_t timestamp2 = 0;
1816                         if (i < ientryEOR){
1817                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1818                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1819                         }
1820                         else {
1821                                 timestamp2 = timeEnd+1;
1822                         }
1823                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1824                         arrayValues[i-ientrySOR] = (Float_t)v->GetInt();
1825                 }
1826                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1827         }
1828         else if (iCountsRun == 1){
1829                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1830                 nentriesUsed = 2;
1831                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1832                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
1833                         arrayValues = new Float_t[2];
1834                         arrayWeights = new Double_t[2];
1835                         arrayValues[0] = valueBeforeSOR;
1836                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1837                         arrayValues[1] = (Float_t)v->GetInt();
1838                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1839                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1840                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1841                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
1842                 }
1843                 else{
1844                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
1845                         return AliGRPObject::GetInvalidFloat();
1846                 }
1847         }
1848         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
1849                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
1850                         if (timestampBeforeSOR == -1){
1851                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
1852                         }
1853                         if (timestampAfterEOR == -1){
1854                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
1855                         }
1856                         return AliGRPObject::GetInvalidFloat();
1857                 }
1858                 else {
1859                         AliWarning("Using last entry before SOR and first entry after EOR.");
1860                         nentriesUsed = 2;
1861                         arrayValues = new Float_t[2];
1862                         arrayWeights = new Double_t[2];
1863                         arrayValues[0] = valueBeforeSOR;
1864                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
1865                         arrayValues[1] = valueAfterEOR;
1866                         arrayWeights[1] = 1.;
1867                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1868                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1869                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
1870                 }
1871         }
1872
1873         AliInfo(Form("mean = %f ", aDCSArrayMean));
1874         return aDCSArrayMean;
1875
1876 }
1877 //_______________________________________________________________
1878
1879 Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
1880 {
1881         // 
1882         // processing Int values, returning mean 
1883         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
1884         // are outside the queried time interval or their value is out of range
1885         //
1886
1887         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1888         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1889         if (timeStartString.IsNull() || timeStartString.IsNull()){
1890                 if (timeStartString.IsNull()){ 
1891                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1892                 }
1893                 else if (timeStartString.IsNull()){
1894                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1895                 }
1896                 return 0;
1897         }  
1898
1899         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1900         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1901         Float_t aDCSArrayMean = 0.0;
1902         Int_t iCounts = 0;
1903         Float_t valueBeforeSOR = 0;
1904         Float_t valueAfterEOR = 0;
1905         Int_t timestampBeforeSOR = -1;
1906         Int_t timestampAfterEOR = -1;
1907         Int_t ientrySOR = -1;
1908         Int_t ientryEOR = -1;
1909         Float_t* arrayValues = 0x0; 
1910         Double_t* arrayWeights = 0x0; 
1911         Int_t iCountsRun = 0;
1912         Int_t nCounts = array->GetEntries();
1913
1914         for(Int_t i = 0; i < nCounts; i++) {
1915                 AliDCSValue* v = (AliDCSValue *)array->At(i);
1916                 if ((v->GetUInt() < fminUInt) || (v->GetUInt() > fmaxUInt)) {
1917                         AliError(Form("Error! UInt value found in DCS map at %d-th entry is OUT OF RANGE: value = %u",i,v->GetUInt()));
1918                         return AliGRPObject::GetInvalidFloat();
1919                 }
1920                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1921                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetUInt(),v->GetTimeStamp()));
1922                         iCounts += 1;
1923                         // look for the last value before SOR and the first value before EOR
1924                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1925                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1926                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1927                                 valueBeforeSOR = (Float_t)v->GetUInt();
1928                         }
1929                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1930                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1931                                 valueAfterEOR = (Float_t)v->GetUInt();
1932                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1933                         }
1934                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1935                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1936                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1937                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1938                                 AliDebug(2,Form("entry between SOR and EOR"));
1939                                 iCountsRun += 1;
1940                         }
1941                 }
1942                 else {
1943                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1944                 }
1945         }
1946
1947         if (timestampBeforeSOR == -1){
1948                 AliWarning("No value found before SOR!");
1949         }
1950         if (timestampAfterEOR == -1){
1951                 AliWarning("No value found after EOR!");
1952         }
1953
1954         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
1955         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1956         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1957         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1958         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1959         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1960
1961         Int_t nentriesUsed = 0;
1962         if (iCountsRun > 1){
1963                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1964                 AliDebug(2,"Calculating (weighted) Mean");
1965                 arrayValues = new Float_t[iCountsRun]; 
1966                 arrayWeights = new Double_t[iCountsRun]; 
1967                 nentriesUsed = iCountsRun;
1968                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1969                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1970                         Int_t timestamp2 = 0;
1971                         if (i < ientryEOR){
1972                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1973                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1974                         }
1975                         else {
1976                                 timestamp2 = timeEnd+1;
1977                         }
1978                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1979                         arrayValues[i-ientrySOR] = (Float_t)v->GetUInt();
1980                 }
1981                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1982         }
1983         else if (iCountsRun == 1){
1984                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1985                 nentriesUsed = 2;
1986                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1987                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
1988                         arrayValues = new Float_t[2];
1989                         arrayWeights = new Double_t[2];
1990                         arrayValues[0] = valueBeforeSOR;
1991                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1992                         arrayValues[1] = (Float_t)v->GetUInt();
1993                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1994                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1995                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1996                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
1997                 }
1998                 else{
1999                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2000                         return AliGRPObject::GetInvalidFloat();
2001                 }
2002         }
2003         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2004                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2005                         if (timestampBeforeSOR == -1){
2006                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2007                         }
2008                         if (timestampAfterEOR == -1){
2009                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2010                         }
2011                         return AliGRPObject::GetInvalidFloat();
2012                 }
2013                 else {
2014                         AliWarning("Using last entry before SOR and first entry after EOR.");
2015                         nentriesUsed = 2;
2016                         arrayValues = new Float_t[2];
2017                         arrayWeights = new Double_t[2];
2018                         arrayValues[0] = valueBeforeSOR;
2019                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2020                         arrayValues[1] = valueAfterEOR;
2021                         arrayWeights[1] = 1.;
2022                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2023                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2024                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2025                 }
2026         }
2027
2028         AliInfo(Form("mean = %f ",aDCSArrayMean));
2029         return aDCSArrayMean;
2030
2031 }
2032
2033
2034 //_______________________________________________________________
2035
2036 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
2037 {
2038         // extract DCS pressure maps. Perform fits to save space
2039         
2040         TMap *map = fPressure->ExtractDCS(dcsAliasMap);
2041         if (map) {
2042                 AliDebug(2,Form("Map has %d entries",map->GetEntries()));
2043                 fPressure->MakeSplineFit(map);
2044                 Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
2045                 if (fitFraction > kFitFraction ) {
2046                         AliInfo(Form("Pressure values extracted, %d fits performed for %d sensors.", fPressure->NumFits(),fPressure->NumSensors()));
2047                 } else { 
2048                         AliInfo("Too few pressure maps fitted!!!");
2049                 }
2050         } else {
2051                 AliInfo("no atmospheric pressure map extracted!!!");
2052         }
2053         delete map;
2054         
2055         return fPressure;
2056 }
2057
2058
2059   
2060 //_______________________________________________________________
2061 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)
2062 {
2063         //
2064         // Retrieves logbook and trigger information from the online logbook
2065         // This information is needed for prompt reconstruction
2066         //
2067         // Parameters are:
2068         // Run number
2069         // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
2070         // cdbRoot
2071         //
2072         // returns:
2073         //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
2074         //         0 on success and no run was found
2075         //         negative on error
2076         //
2077         // This function is NOT called during the preprocessor run in the Shuttle!
2078         //
2079         
2080         // defaults
2081         if (dbPort == 0)
2082                 dbPort = 3306;
2083         
2084         // CDB connection
2085         AliCDBManager* cdb = AliCDBManager::Instance();
2086         cdb->SetDefaultStorage(cdbRoot);
2087         
2088         // SQL connection
2089         TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
2090         
2091         if (!server)
2092                 {
2093                         Printf("ERROR: Could not connect to DAQ LB");
2094                         return -1;
2095                 }
2096         
2097         // main logbook
2098         TString sqlQuery;
2099         sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent FROM logbook WHERE run = %d", run);
2100         TSQLResult* result = server->Query(sqlQuery);
2101         if (!result)
2102                 {
2103                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2104                         return -2;
2105                 }
2106         
2107         if (result->GetRowCount() == 0)
2108                 {
2109                         Printf("ERROR: Run %d not found", run);
2110                         delete result;
2111                         return -3;
2112                 }
2113         
2114         TSQLRow* row = result->Next();
2115         if (!row)
2116                 {
2117                         Printf("ERROR: Could not receive data from run %d", run);
2118                         delete result;
2119                         return -4;
2120                 }
2121         
2122         TString timeStartString(row->GetField(0));
2123         TString runType(row->GetField(1));
2124         TString detectorMaskString(row->GetField(2));
2125         TString l3CurrentString(row->GetField(3));
2126         TString dipoleCurrentString(row->GetField(4));
2127         time_t timeStart = (time_t)(timeStartString.Atoi());
2128         UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
2129         Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
2130         Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
2131         Char_t l3Polarity = (l3CurrentString.Atof() < 0) ? 1 : 0;
2132         Char_t dipolePolarity = (dipoleCurrentString.Atof() < 0) ? 1 : 0;
2133         
2134         AliGRPObject * grpObj = new AliGRPObject();
2135         grpObj->SetTimeStart(timeStart); 
2136         grpObj->SetRunType((TString)(row->GetField(1)));
2137         grpObj->SetDetectorMask(detectorMask);
2138         grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
2139         grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
2140         grpObj->SetL3Polarity(l3Polarity);
2141         grpObj->SetDipolePolarity(dipolePolarity);
2142         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
2143
2144         delete row;
2145         row = 0;
2146         
2147         delete result;
2148         result = 0;
2149         
2150         Printf("Storing GRP/GRP/Data object with the following content");
2151         grpObj->Dump();
2152         
2153         AliCDBMetaData metadata;
2154         metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus & Chiara Zampolli");
2155         metadata.SetComment("GRP Output parameters received during online running");
2156         
2157         AliCDBId id("GRP/GRP/Data", run, run);
2158         Bool_t success = cdb->Put(grpObj, id, &metadata);
2159         
2160         delete grpObj;
2161         
2162         if (!success)
2163                 {
2164                         Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
2165                         return -5;
2166                 }
2167         
2168         // Receive trigger information
2169         sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
2170         result = server->Query(sqlQuery);
2171         if (!result)
2172                 {
2173                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2174                         return -11;
2175                 }
2176         
2177         if (result->GetRowCount() == 0)
2178                 {
2179                         Printf("ERROR: Run %d not found in logbook_trigger_config", run);
2180                         delete result;
2181                         return -12;
2182                 }
2183         
2184         row = result->Next();
2185         if (!row)
2186                 {
2187                         Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
2188                         delete result;
2189                         return -13;
2190                 }
2191         
2192         TString triggerConfig(row->GetField(0));
2193         
2194         delete row;
2195         row = 0;
2196         
2197         delete result;
2198         result = 0;
2199         
2200         Printf("Found trigger configuration: %s", triggerConfig.Data());
2201         
2202         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
2203         if (!runcfg)
2204                 {
2205                         Printf("ERROR: Could not create CTP configuration object");
2206                         return -14;
2207                 }
2208         
2209         metadata.SetComment("CTP run configuration received during online running");
2210         
2211         AliCDBId id2("GRP/CTP/Config", run, run);
2212         success = cdb->Put(runcfg, id2, &metadata);
2213         
2214         delete runcfg;
2215         runcfg = 0;
2216         
2217         if (!success)
2218                 {
2219                         Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
2220                         return -15;
2221                 }
2222         
2223
2224         // Receive list of GDCs for this run
2225         sqlQuery.Form("SELECT GDC FROM logbook_stats_GDC WHERE run = %d", run);
2226         result = server->Query(sqlQuery);
2227         if (!result)
2228                 {
2229                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2230                         return -24;
2231                 }
2232         
2233         if (result->GetRowCount() == 0)
2234                 {
2235                         Printf("ERROR: Run %d not found in logbook_stats_GDC", run);
2236                         delete result;
2237                         return -25;
2238                 }
2239
2240         gdc = "";
2241         for (Int_t iGDC = 0; iGDC < result->GetRowCount(); iGDC++) {
2242           row = result->Next();
2243           if (!row)
2244             {
2245               Printf("ERROR: Could not receive logbook_stats_GDC data from run %d", run);
2246               delete result;
2247               return -26;
2248             }
2249           gdc += row->GetField(0);
2250           gdc += " ";
2251         }
2252
2253         delete row;
2254         row = 0;
2255         
2256         delete result;
2257         result = 0;
2258         
2259         Printf("Found GDC: %s", gdc.Data());
2260
2261         // get last run with same run type that was already processed by the SHUTTLE
2262         
2263         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());
2264         result = server->Query(sqlQuery);
2265         if (!result)
2266                 {
2267                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2268                         return -21;
2269                 }
2270         
2271         if (result->GetRowCount() == 0)
2272                 {
2273                         Printf("ERROR: No result with query <%s>", sqlQuery.Data());
2274                         delete result;
2275                         return -22;
2276                 }
2277         
2278         row = result->Next();
2279         if (!row)
2280                 {
2281                         Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
2282                         delete result;
2283                         return -23;
2284                 }
2285         
2286         TString lastRunStr(row->GetField(0));
2287         Int_t lastRun = lastRunStr.Atoi();
2288         
2289         Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
2290         
2291         delete row;
2292         row = 0;
2293         
2294         delete result;
2295         result = 0;
2296         
2297         server->Close();
2298         delete server;
2299         server = 0;
2300         
2301         return lastRun;
2302 }