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