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