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