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