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