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