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