Adding processing of LHC Clock Phase to the GRP preprocessor (Cvetan + me).
[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 Machine Mode 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 (beamType.CompareTo("LEAD82",TString::kIgnoreCase) == 0){
818                                                 AliInfo("Setting beam type to Pb-Pb");
819                                                 grpobj->SetBeamType("Pb-Pb");
820                                         }
821                                         else{
822                                                 AliError("Beam Type not known, leaving it empty");
823                                         }
824                                         if (indexBeam < nBeam-1){
825                                                 AliDCSArray* beam1 = (AliDCSArray*)beamArray->At(indexBeam+1);
826                                                 if (beam1){
827                                                         if (beam1->GetTimeStamp()>timeStart && beam1->GetTimeStamp()<=timeEnd){
828                                                                 timeBeamEnd = beam1->GetTimeStamp();
829                                                                 TObjString* beamString1 = beam1->GetStringArray(0);
830                                                                 TString beamType1 = beamString1->String();
831                                                                 if (beamType.CompareTo(beamType1.Data(),TString::kIgnoreCase) == -1){
832                                                                         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()));
833                                                                         flagBeam = kTRUE;
834                                                                 }
835                                                         }
836                                                 }
837                                                 else {
838                                                         AliInfo("Invalid pointer for the first entry for Beam Type after the first valid one, not considering anything after what has already been found");
839                                                 }
840                                         }
841                                 }
842                         }
843                         delete beamArray;
844                 }
845                 else{
846                         AliError("Beam Type array not found in LHC Data file!!!");
847                 }               
848                 
849                 // Setting minTimeLHCValidity
850                 if (timeBeamModeEnd!=0 || timeMachineModeEnd!=0 || timeBeamEnd !=0){
851                         Double_t minTimeLHCValidity;
852                         if (flagBeamMode == kFALSE && flagMachineMode == kFALSE && flagBeam == kTRUE){ // flagBeam only true
853                                 minTimeLHCValidity = timeBeamEnd;
854                         }
855                         else if (flagBeamMode == kFALSE && flagMachineMode == kTRUE && flagBeam == kFALSE){ // flagMachineMode only true
856                                 minTimeLHCValidity = timeMachineModeEnd;
857                         }
858                         else if (flagBeamMode == kTRUE && flagMachineMode == kFALSE && flagBeam == kFALSE){ // flagBeamMode only true
859                                 minTimeLHCValidity = timeBeamModeEnd;
860                         }
861                         else if (flagBeamMode == kFALSE && flagMachineMode == kTRUE && flagBeam == kTRUE){ // flagBeam and flagMachineMode only true
862                                 minTimeLHCValidity= TMath::Min(timeBeamEnd,timeMachineModeEnd);
863                         }
864                         else if (flagBeamMode == kTRUE && flagMachineMode == kFALSE && flagBeam == kTRUE){ // flagBeam and flagBeamMode only true
865                                 minTimeLHCValidity= TMath::Min(timeBeamEnd,timeBeamModeEnd);
866                         }
867                         else if (flagBeamMode == kTRUE && flagMachineMode == kTRUE && flagBeam == kFALSE){ // flagMachineMode and flagBeamMode only true
868                                 minTimeLHCValidity= TMath::Min(timeMachineModeEnd,timeBeamModeEnd);
869                         }
870                         else {
871                                 Double_t arrayTimes[3] = {timeBeamModeEnd,timeMachineModeEnd,timeBeamEnd};// flagMachineMode and flagBeamMode and flagBeam 
872                                 minTimeLHCValidity= TMath::MinElement(3,arrayTimes);
873                         }
874                         AliWarning(Form("Setting MaxTimeLHCValidity to %f",minTimeLHCValidity));
875                         grpobj->SetMaxTimeLHCValidity(minTimeLHCValidity);
876                 }
877                 
878                 // Processing data to go to AliLHCData object
879                 AliLHCData* dt = new AliLHCData(fileName.Data(),timeStart,timeEnd);
880                 // storing AliLHCData in OCDB
881                 if (dt){
882                   AliInfo(Form("Filled %d records to AliLHCData object",dt->GetData().GetEntriesFast()));
883                   AliCDBMetaData md;
884                   md.SetResponsible("Ruben Shahoyan");
885                   md.SetComment("LHC data from the GRP preprocessor.");
886                   Bool_t result = kTRUE;
887                   result = Store("GRP", "LHCData", dt, &md); 
888                   delete dt;
889                   if (!result){
890                         Log(Form("Problems in storing LHC Data - but not going into Error"));
891                   }
892                 }
893
894                 // processing LHC Phase
895
896                 TObjArray *beam1phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[4]);
897                 TObjArray *beam2phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[5]);
898                 if (beam1phase == 0x0 || beam2phase == 0x0){
899                         Log(Form("Problems in retrieving LHC Clock data from LHC file"));
900                         return 4;
901                 }                       
902                 AliLHCClockPhase *phaseObj = ProcessLHCClockPhase(beam1phase,beam2phase,timeStart,timeEnd);
903                 if (phaseObj){
904                         AliInfo(Form("LHC Phase found"));
905                         AliCDBMetaData mdPhase;
906                         mdPhase.SetResponsible("Cvetan Cheshkov");
907                         mdPhase.SetComment("LHC Clock Phase");
908                         Bool_t result = kTRUE;
909                         result = Store("Calib", "LHCClockPhase", phaseObj, &mdPhase); 
910                         delete phaseObj;
911                         if (!result) return 3;
912                 }
913                 else return 4;
914         }
915         
916         else {
917                 AliError("No LHCData file found in DCS FXS");
918                 return 1;
919         }
920
921         return 0;
922 }
923
924 //_______________________________________________________________
925
926 UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
927 {
928         //
929         //Getting the SPD Mean Vertex
930         //
931
932         TList* list = GetForeignFileSources("SPD", kDAQ, "VertexDiamond");
933         Bool_t storeResult = kTRUE;
934         if (list !=0x0 && list->GetEntries()!=0)
935                 {
936                         AliInfo("The following sources produced files with the id VertexDiamond from SPD");
937                         list->Print();
938                         for (Int_t jj=0;jj<list->GetEntries();jj++){
939                                 TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
940                                 AliInfo(Form("found source %s", str->String().Data()));
941                                 TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
942                                 if (fileNameRun.Length()>0){
943                                         AliInfo(Form("Got the file %s", fileNameRun.Data()));
944                                         TFile daqFile(fileNameRun.Data(),"READ");
945                                         if (daqFile.IsOpen()) {
946                                                 AliESDVertex* meanVtx = dynamic_cast<AliESDVertex*>(daqFile.Get("MeanVertexPos"));
947                                                 if (meanVtx){
948                                                         meanVtx->Print();       
949                                                         // storing in the OCDB 
950                                                         AliCDBMetaData md;
951                                                         md.SetResponsible("Cvetan Cheshkov");
952                                                         md.SetComment("SPD Mean Vertex");                                       
953                                                         storeResult = Store("Calib", "MeanVertexSPD", meanVtx, &md, 0, kTRUE); 
954                                                 }
955                                                 else{
956                                                         AliWarning("No SPD Mean Vertex object found in file");
957                                                 } 
958                                         }
959                                         else {
960                                                 AliError("Can't open file");
961                                                 storeResult = kFALSE;
962                                         }
963                                 }
964                                 else{
965                                         AliWarning("No file found for current source for SPD Mean Vertex");
966                                 }
967                         }
968                 }
969         else {
970                 AliWarning("No list found for SPD Mean Vertex");
971         }
972
973         if (list) delete list;
974
975         return storeResult;
976 }
977
978
979 //_______________________________________________________________
980
981 Int_t AliGRPPreprocessor::ProcessDaqLB(AliGRPObject* grpObj)
982 {
983         //Getting the DAQ lb information
984         
985         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
986         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
987         Float_t beamEnergy = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof());
988         TString beamType = (TString)GetRunParameter("beamType");
989         Char_t numberOfDetectors = (Char_t)(((TString)GetRunParameter("numberOfDetectors")).Atoi());
990         UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
991         TString lhcPeriod = (TString)GetRunParameter("LHCperiod");
992         TString runType = (TString)GetRunType();
993
994         UInt_t nparameter = 0;
995         if (timeStart != 0){
996                 grpObj->SetTimeStart(timeStart);
997                 Log(Form("Start time for run %d: %d",fRun, (Int_t)timeStart));
998                 nparameter++;
999         } 
1000         else {
1001                 Log(Form("Start time not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1002         }
1003
1004         if (timeEnd != 0){
1005                 grpObj->SetTimeEnd(timeEnd);
1006                 Log(Form("End time for run %d: %i",fRun, (Int_t)timeEnd));
1007                 nparameter++;
1008         } 
1009         else {
1010                 Log(Form("End time not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1011         }
1012
1013         if (beamEnergy != 0){
1014                 Log(Form("Beam Energy for run %d: %f (NOT USING IT TO FILL THE GRP OBJECT, taking it from the LHC file)",fRun, beamEnergy));
1015         } 
1016         else {
1017                 Log(Form("Beam Energy not put in logbook, but not using it anyway for the GRP object (taking it from the LHC file)"));
1018         }
1019
1020                 
1021         if (beamType.Length() != 0){
1022                 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()));
1023         } 
1024         else {
1025                 Log(Form("Beam Type not put in logbook, but not using it anyway for the GRP entry (taking it from the LHC file)"));
1026         }
1027                 
1028         if (numberOfDetectors != 0){
1029                 grpObj->SetNumberOfDetectors(numberOfDetectors);
1030                 Log(Form("Number Of Detectors for run %d: %d",fRun, (Int_t)numberOfDetectors));
1031                 nparameter++;
1032         } 
1033         else {
1034                 Log(Form("Number Of Detectors not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1035         }
1036
1037         if (detectorMask != 0){
1038                 grpObj->SetDetectorMask(detectorMask);
1039                 Log(Form("Detector Mask for run %d: %d",fRun, detectorMask));
1040                 nparameter++;
1041         } 
1042         else {
1043                 Log(Form("Detector Mask not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1044         }
1045
1046         if (lhcPeriod.Length() != 0) {
1047                 grpObj->SetLHCPeriod(lhcPeriod);
1048                 Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod.Data()));
1049                 nparameter++;
1050         } 
1051         else {
1052                 Log(Form("LHCperiod not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1053         }
1054         if (runType.Length() != 0) {
1055                 grpObj->SetRunType(runType);
1056                 Log(Form("Run Type (DAQ) for run %d: %s",fRun, runType.Data()));
1057                 nparameter++;
1058         } 
1059         else {
1060                 Log(Form("Run Type not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1061         }
1062
1063         return nparameter;
1064 }
1065
1066 //_______________________________________________________________
1067
1068 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
1069 {
1070         //======DAQ FXS======//
1071         
1072         AliRawEventHeaderV3_9::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_9 - temporary fix 
1073         TList* list = GetFileSources(kDAQ);  
1074         if (!list) {
1075                 Log("No raw data tag list: connection problems with DAQ FXS logbook!");
1076                 return 1;
1077         }
1078         
1079         if (list->GetEntries() == 0) {
1080                 Log("no raw data tags in this run: nothing to merge!");
1081                 delete  list; list=0;
1082                 return 0;
1083         }
1084         
1085         TChain *fRawTagChain = new TChain("T");
1086         Int_t nFiles=0;
1087         TIterator* iter = list->MakeIterator();
1088         TObject* obj = 0;
1089         while ((obj = iter->Next())) {
1090                 TObjString* objStr = dynamic_cast<TObjString*> (obj);
1091                 if (objStr) {
1092                         Log(Form("Found source %s", objStr->String().Data()));
1093                         TList* list2 = GetFileIDs(kDAQ, objStr->String());
1094                         if (!list2) {
1095                                 Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
1096                                 delete fRawTagChain; fRawTagChain=0;
1097                                 return 1;
1098                         }
1099                         Log(Form("Number of ids: %d",list2->GetEntries()));
1100                         for(Int_t i = 0; i < list2->GetEntries(); i++) {
1101                                 TObjString *idStr = (TObjString *)list2->At(i);
1102                                 TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
1103                                 if (fileName.Length() > 0) {
1104                                         Log(Form("Adding file in the chain: %s",fileName.Data()));
1105                                         fRawTagChain->Add(fileName.Data());
1106                                         nFiles++;
1107                                 } else {
1108                                         Log(Form("Could not retrieve file with id %s from source %s: "
1109                                                  "connection problems with DAQ FXS!",
1110                                                  idStr->String().Data(), objStr->String().Data()));
1111                                         delete list; list=0;
1112                                         delete list2; list2=0;
1113                                         delete fRawTagChain; fRawTagChain=0;
1114                                         return 2;
1115                                 }
1116                         }
1117                         delete list2;
1118                 }
1119         }
1120         
1121         TString fRawDataFileName = "GRP_Merged.tag.root";
1122         Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
1123         
1124         if( fRawTagChain->Merge(fRawDataFileName) < 1 ) {
1125                 Log("Error merging raw data files!!!");
1126                 return 3;
1127         }
1128         
1129         TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
1130         Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
1131         
1132         if (!result) {
1133                 Log("Problem storing raw data tags in local file!!!");
1134         } else {
1135                 Log("Raw data tags merged successfully!!");
1136         }
1137         
1138         delete iter;
1139         delete list;
1140         delete fRawTagChain; fRawTagChain=0;
1141         
1142         if (result == kFALSE) {
1143                 return 4;
1144         }
1145         
1146         return 0;
1147         
1148 }
1149
1150 //_______________________________________________________________
1151 UInt_t AliGRPPreprocessor::ProcessDcsFxs(TString partition, TString detector)
1152 {
1153
1154         // processing the info
1155         // stored in the DCS FXS
1156         // coming from the trigger
1157
1158         // Get the CTP counters information
1159
1160         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
1161                 Log("STANDALONE partition for current run, using Trigger Scalers dummy value");
1162                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyScalers");
1163                 if (!cdbEntry) {
1164                         Log(Form("No dummy CTP scalers entry found, going into error..."));
1165                         return 1;
1166                 }
1167                 else{
1168                         AliTriggerRunScalers *scalers = (AliTriggerRunScalers*)cdbEntry->GetObject();
1169                         if (!scalers){
1170                                 Log(Form("CTP dummy scalers not found in OCDB entry, going into error..."));
1171                                 return 1;
1172                         }
1173                         else {
1174                                 AliCDBMetaData metaData;
1175                                 metaData.SetResponsible("Roman Lietava");
1176                                 metaData.SetComment("CTP scalers from dummy entry in OCDB");
1177                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1178                                         Log("Unable to store the dummy CTP scalers object to OCDB!");
1179                                         return 1;
1180                                 }
1181                         }
1182                 }
1183         }
1184
1185         else if (!partition.IsNull() && detector.IsNull()){ // global partition
1186                 Log("GLOBAL partition for current run, using CTP scalers from DCS FXS");
1187                 TString countersfile = GetFile(kDCS, "CTP_xcounters","");
1188                 if (countersfile.IsNull()) {
1189                         Log("No CTP counters files has been found: empty source!");
1190                         return 1;
1191                 }
1192                 else {
1193                         Log(Form("File with Id CTP_xcounters found in DCS FXS! Copied to %s",countersfile.Data()));
1194                         AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
1195                         if (!scalers) {
1196                                 Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
1197                                 return 1;
1198                         }
1199                         else {
1200                                 AliCDBMetaData metaData;
1201                                 metaData.SetBeamPeriod(0);
1202                                 metaData.SetResponsible("Roman Lietava");
1203                                 metaData.SetComment("CTP scalers");
1204                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1205                                         Log("Unable to store the CTP scalers object to OCDB!");
1206                                         delete scalers;                                 
1207                                         return 1;
1208                                 }
1209                         }
1210                         delete scalers;
1211                 }
1212         }
1213         
1214
1215         else{   
1216                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error...",partition.Data(),detector.Data()));
1217                 return 2;
1218         }
1219
1220         return 0;
1221
1222 }
1223 //_______________________________________________________________
1224
1225 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
1226 {
1227
1228         //
1229         // processing DCS DPs
1230         //
1231
1232         Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
1233         Int_t nL3Entries = 0;
1234         Int_t nDipoleEntries = 0;
1235         Int_t nEnvEntries = 0;
1236         Int_t nHallProbesEntries = 0;
1237         nL3Entries = ProcessL3DPs(valueMap, grpObj);
1238         nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
1239         nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
1240         nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
1241         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
1242         Log(Form("L3Entries = %d, nDipoleEntries =%d, nEnvEntries = %d, nHallProbesEntries = %d", nL3Entries, nDipoleEntries, nEnvEntries, nHallProbesEntries));
1243         entries = nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
1244         return entries;
1245
1246 }
1247
1248 //_______________________________________________________________
1249
1250 Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpObj)
1251 {
1252
1253         // processing DPs
1254         // related to 
1255         // L3 info
1256
1257         Int_t nL3Entries = 0;
1258
1259         TObjArray *array = 0x0;
1260         Int_t indexDP = -1;
1261         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1262
1263         AliInfo(Form("==========L3Current==========="));
1264         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1265         indexDP = kL3Current;
1266         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1267         if(!array) {
1268                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1269         } 
1270         else {
1271                 if (array->GetEntries() == 0){
1272                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1273                 }
1274                 else {
1275                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1276                         if (floatDCS != NULL){
1277                                 grpObj->SetL3Current(floatDCS);
1278                         }
1279                         else{
1280                                 outOfRange = kTRUE;
1281                         }       
1282                         if (floatDCS){
1283                                 delete[] floatDCS;
1284                                 floatDCS = 0x0;
1285                         }
1286                 }
1287                 if (!outOfRange) {
1288                         nL3Entries++;
1289                         ffailedDPs->RemoveAt(indexDP);
1290                 }
1291         }
1292
1293         if (array) array = 0x0;
1294
1295         AliInfo(Form("==========L3Polarity==========="));
1296         indexDP = kL3Polarity;
1297         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1298         if(!array) {
1299                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1300         } 
1301         else {
1302                 if (array->GetEntries() == 0){
1303                         AliError(Form("No entries found in array! setting %s Polarity to invalid...",fgkDCSDataPoints[indexDP]));
1304                 }
1305                 else {
1306                         Bool_t change = kFALSE;
1307                         Char_t charDCS = ProcessBool(array,change);
1308                         if (change == kFALSE){
1309                                 grpObj->SetL3Polarity(charDCS);
1310                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
1311                                 ffailedDPs->RemoveAt(indexDP);
1312                                 nL3Entries++;
1313                         }
1314                         else if (isZero){
1315                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1316                                 ffailedDPs->RemoveAt(indexDP);
1317                                 nL3Entries++;
1318                         }
1319                         else {
1320                                 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]));
1321                         }
1322                 }
1323         }
1324
1325         return nL3Entries;
1326
1327 }
1328 //_______________________________________________________________
1329
1330 Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* grpObj)
1331 {
1332         // processing DPs
1333         // related to 
1334         // the Dipole info
1335
1336         Int_t nDipoleEntries = 0;
1337         TObjArray *array = 0x0;
1338         Int_t indexDP = -1;
1339         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1340
1341         AliInfo(Form("==========DipoleCurrent==========="));
1342         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1343         indexDP = kDipoleCurrent;
1344         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1345         if(!array) {
1346                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1347         } 
1348         else {
1349                 if (array->GetEntries() == 0){
1350                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1351                 }
1352                 else {
1353                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1354                         if (floatDCS != NULL){
1355                                 grpObj->SetDipoleCurrent(floatDCS);
1356                         } 
1357                         else{
1358                                 outOfRange=kTRUE;
1359                         }
1360                         if (floatDCS){
1361                                 delete[] floatDCS;
1362                                 floatDCS = 0x0;
1363                         }
1364                 }
1365                 if (!outOfRange) {
1366                         nDipoleEntries++;
1367                         ffailedDPs->RemoveAt(indexDP);
1368                 }
1369         }
1370
1371         if (array) array = 0x0;
1372
1373         AliInfo(Form("==========DipolePolarity==========="));
1374         indexDP = kDipolePolarity;
1375         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1376         if(!array) {
1377                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1378         } 
1379         else {
1380                 if (array->GetEntries() == 0){
1381                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1382                 }
1383                 else {
1384                         Bool_t change = kFALSE;
1385                         Char_t charDCS = ProcessBool(array,change);
1386                         if (!change){
1387                                 grpObj->SetDipolePolarity(charDCS);
1388                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
1389                                 ffailedDPs->RemoveAt(indexDP);
1390                                 nDipoleEntries++;
1391                         }
1392                         else if (isZero){
1393                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1394                                 ffailedDPs->RemoveAt(indexDP);
1395                                 nDipoleEntries++;
1396                         }
1397                         else{
1398                                 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]));
1399                         }
1400                 }
1401         }
1402
1403         return nDipoleEntries;
1404
1405 }
1406 //_______________________________________________________________
1407
1408 Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
1409 {
1410         // processing DPs
1411         // related to 
1412         // evironment conditions (temperature, pressure) info
1413
1414         Int_t nEnvEntries = 0;
1415         TObjArray *array = 0x0;
1416         Int_t indexDP = -1;
1417
1418         AliInfo(Form("==========CavernTemperature==========="));
1419         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1420         indexDP = kCavernTemperature;
1421         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1422         if(!array) {
1423                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1424         } 
1425         else {
1426                 if (array->GetEntries() == 0){
1427                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1428                 }
1429                 else {
1430                         Float_t *floatDCS = ProcessFloatAll(array);
1431                         if (floatDCS != NULL){
1432                                 grpObj->SetCavernTemperature(floatDCS);
1433                         }
1434                         else{
1435                                 outOfRange = kTRUE;
1436                         }
1437                         if (floatDCS){
1438                                 delete[] floatDCS;
1439                                 floatDCS = 0x0;
1440                         }
1441                 }
1442                 if (!outOfRange) {
1443                         ffailedDPs->RemoveAt(indexDP);
1444                         nEnvEntries++;
1445                 }
1446         }
1447
1448         if (array) array = 0x0;
1449
1450         AliInfo(Form("========== AtmosPressures (Cavern + Surface + Cavern2) ==========="));
1451         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
1452         //dcsSensorArray->Print();
1453         if( fPressure->NumFits()<kNumSensors ) {
1454                 Log(Form("Check the pressure sensor values! Not all the %d pressure sensors have been fit",kNumSensors));
1455         } 
1456         Log(Form("Number of fits performed = %d",fPressure->NumFits()));
1457
1458         AliInfo(Form("==========CavernAtmosPressure==========="));
1459         indexDP = kCavernAtmosPressure;
1460         AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1461         TGraph* graph = sensorCavernP2->GetGraph();
1462         AliDebug(3,Form("index = %d",indexDP));
1463         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1464         AliDebug(2,Form("graph = %p",graph));
1465         AliDebug(3,Form("sensorCavernP2 = %p", sensorCavernP2));
1466         if(sensorCavernP2->GetFit() || graph) {
1467                 if (sensorCavernP2->GetFit()){
1468                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1469                 }
1470                 else {
1471                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1472                 }
1473                 grpObj->SetCavernAtmosPressure(sensorCavernP2);
1474                 ffailedDPs->RemoveAt(indexDP);
1475                 nEnvEntries++;
1476         } 
1477         //if (sensorP2) delete sensorP2;
1478         else {
1479                 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] ));
1480         }
1481         
1482         AliInfo(Form("==========SurfaceAtmosPressure==========="));
1483         indexDP = kSurfaceAtmosPressure;
1484         AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1485         graph = sensorP2->GetGraph();
1486         AliDebug(3,Form("index = %d",indexDP));
1487         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1488         AliDebug(2,Form("graph = %p",graph));   
1489         AliDebug(3,Form("sensorP2 = %p", sensorP2));
1490         if(sensorP2->GetFit() || graph) {
1491                 if (sensorP2->GetFit()){
1492                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1493                 }
1494                 else {
1495                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1496                 }
1497                 grpObj->SetSurfaceAtmosPressure(sensorP2);
1498                 ffailedDPs->RemoveAt(indexDP);
1499                 nEnvEntries++;
1500         } 
1501         //if (sensorP2) delete sensorP2;
1502         else {
1503                 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] ));
1504         }
1505
1506         AliInfo(Form("==========CavernAtmosPressure2==========="));
1507         indexDP = kCavernAtmosPressure2;
1508         AliDCSSensor* sensorCavernP22 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
1509         graph = sensorCavernP22->GetGraph();
1510         AliDebug(3,Form("index = %d",indexDP));
1511         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
1512         AliDebug(2,Form("graph = %p",graph));   
1513         AliDebug(3,Form("sensorCavernP2_2 = %p", sensorCavernP22));
1514         if(sensorCavernP22->GetFit() || graph) {
1515                 if (sensorCavernP22->GetFit()){
1516                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
1517                 }
1518                 else {
1519                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
1520                 }
1521                 grpObj->SetCavernAtmosPressure2(sensorCavernP22);
1522                 ffailedDPs->RemoveAt(indexDP);
1523                 nEnvEntries++;
1524         } 
1525         //if (sensorP2) delete sensorP2;
1526         else {
1527                 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] ));
1528         }
1529         
1530         
1531         return nEnvEntries;
1532 }
1533 //_______________________________________________________________
1534
1535 Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpObj)
1536 {
1537         // processing DPs
1538         // related to 
1539         // Hall Probes info
1540
1541         Int_t nHPEntries = 0;
1542         TObjArray *array = 0x0;
1543         Int_t indexDP = -1;
1544         Bool_t outOfRange; // flag to monitor if any value collected by DCS is out of range
1545
1546         if (fgknDCSDPHallProbes != AliGRPObject::GetNumberOfHP()){
1547                 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()));
1548         }
1549         for (indexDP = 0; indexDP < AliGRPObject::GetNumberOfHP(); indexDP++){
1550                 outOfRange = kFALSE; // resetting outOfRange flag at each HP
1551                 AliInfo(Form("==========%s===========",AliGRPObject::GetHPDP(indexDP)));
1552                 array = (TObjArray *)valueMap->GetValue(AliGRPObject::GetHPDP(indexDP));
1553                 if(!array) {
1554                         Log(Form("%s not found in the map!!!",AliGRPObject::GetHPDP(indexDP)));
1555                 } 
1556                 else {
1557                         if (array->GetEntries() == 0){
1558                                 AliError(Form("No entries found in array! setting %s to invalid...",AliGRPObject::GetHPDP(indexDP)));
1559                         }
1560                         else {
1561                                 Float_t *floatDCS = ProcessFloatAll(array);
1562                                 if (floatDCS != NULL){
1563                                         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])); 
1564                                         grpObj->SetHallProbes((AliGRPObject::DP_HallProbes)indexDP,floatDCS);
1565                                         for (Int_t kk = 0 ; kk< 5; kk++){
1566                                                 AliDebug(2,Form("HallProbe[%d][%d]=%f",indexDP,kk,grpObj->GetHallProbes((AliGRPObject::DP_HallProbes)indexDP,(AliGRPObject::Stats)kk)));
1567                                         }
1568                                 }
1569                                 else{
1570                                         outOfRange = kTRUE;
1571                                 }
1572                                 if (floatDCS){
1573                                         delete[] floatDCS;
1574                                         floatDCS = 0x0;
1575                                 }
1576                         }
1577                         if (!outOfRange) {
1578                                 ffailedDPs->RemoveAt(indexDP + fgkDCSDPHallTopShift);  // 7 = shift in the complete list of DPs to get to the Hall Probes
1579                                 nHPEntries++;
1580                         }
1581                 }
1582         }
1583                 
1584         Log(Form("Hall Probes = %d ", nHPEntries));
1585         return nHPEntries;
1586 }
1587
1588 //_________________________________________________________________________
1589
1590 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
1591
1592
1593         // 
1594         // returning Spline Fit 
1595         // 
1596
1597         Int_t entriesarray = array->GetEntries();
1598         Float_t* value = new Float_t[entriesarray];
1599         Float_t* time = new Float_t[entriesarray];
1600         AliDCSValue* v = 0x0;
1601         for (Int_t iarray = 0; iarray < entriesarray; iarray++){
1602                 v = (AliDCSValue*)array->At(iarray);
1603                 value[iarray] = v->GetFloat();
1604                 time[iarray] = v->GetTimeStamp();
1605                 AliDebug(2,Form("iarray = %d, value = %f, time = %f",iarray,value[iarray],time[iarray]));
1606         }
1607         TGraph* gr = new TGraph(entriesarray,value,time);
1608         if (!gr ) {
1609                 AliWarning(Form("%s: no input graph to compute SplineFit",stringID.Data()));
1610                 return NULL;
1611         }
1612         AliSplineFit *fit = new AliSplineFit();
1613         fit->SetMinPoints(10);
1614         fit->InitKnots(gr,10,10,0.0);
1615         fit->SplineFit(2);
1616         fit->Cleanup();
1617         if (!fit) {
1618                 AliWarning(Form("%s: no fit performed",stringID.Data()));
1619                 return NULL;
1620         } 
1621         return fit;
1622 }
1623
1624 //_________________________________________________________________________
1625
1626 TString AliGRPPreprocessor::ProcessChar(const TObjArray *array)
1627 {
1628
1629         // 
1630         // processing char
1631         //
1632
1633         TString aDCSString="";
1634         
1635         AliDCSValue *v = 0x0;
1636         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1637                 v = (AliDCSValue *)array->At(iCount);
1638                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1639                         AliError(Form("DCS values for the parameter outside the queried interval"));
1640                         continue;
1641                 }
1642                 if (iCount > 0) {
1643                         if (aDCSString != v->GetChar())
1644                         AliError(Form("DCS values for the parameter changed from %s to %c within the queried interval", aDCSString.Data(), (Char_t)v->GetChar()));
1645                 }
1646                 aDCSString = (TString)v->GetChar();  // keeping always last value in the array
1647         }
1648         return aDCSString;
1649 }
1650
1651 //__________________________________________________________________________________________________________________
1652
1653 Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
1654 {
1655         // 
1656         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1657         //
1658         // parameters[0] = mean
1659         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1660         // parameters[2] = median
1661         // parameters[3] = standard deviation wrt mean
1662         // parameters[4] = standard deviation wrt median
1663         //
1664
1665         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1666         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1667         if (timeStartString.IsNull() || timeStartString.IsNull()){
1668                 if (timeStartString.IsNull()){ 
1669                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1670                 }
1671                 else if (timeStartString.IsNull()){
1672                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1673                 }
1674                 fdaqStartEndTimeOk = kFALSE;
1675                 return 0;
1676         }  
1677
1678         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1679         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1680         Float_t* parameters = new Float_t[5];
1681         Int_t iCounts = 0;
1682         Int_t iCountsRun = 0;
1683         Int_t nCounts = array->GetEntries();
1684         Float_t valueBeforeSOR = 0;
1685         Float_t valueAfterEOR = 0;
1686         Int_t timestampBeforeSOR = -1;
1687         Int_t timestampAfterEOR = -1;
1688         Int_t ientrySOR = -1;
1689         Int_t ientryEOR = -1;
1690         Float_t* arrayValues = 0x0; 
1691         Double_t* arrayWeights = 0x0; 
1692         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
1693         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
1694
1695         for(Int_t i = 0; i < nCounts; i++) {
1696                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1697                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1698                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1699                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1700                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1701                         return NULL;
1702                 }
1703                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1704                         AliDebug(2,Form("%d-th entry = %f at timestamp %i",i,v->GetFloat(),v->GetTimeStamp()));
1705                         iCounts += 1;
1706                         // look for the last value before SOR and the first value before EOR
1707                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
1708                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
1709                                 AliDebug(2,Form("timestamp of last value before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
1710                                 valueBeforeSOR = v->GetFloat();
1711                         }
1712                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
1713                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
1714                                 valueAfterEOR = v->GetFloat();
1715                                 AliDebug(2,Form("timestamp of first value after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
1716                         }
1717                         // check if there are DPs between DAQ_time_start and DAQ_time_end
1718                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
1719                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
1720                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
1721                                 AliDebug(2,Form("entry between SOR and EOR"));
1722                                 iCountsRun += 1;
1723                         }
1724                 }
1725                 else {
1726                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
1727                 }
1728         }
1729
1730         if (timestampBeforeSOR == -1){
1731                 AliWarning("No value found before SOR");
1732         }
1733         if (timestampAfterEOR == -1){
1734                 AliWarning("No value found after EOR");
1735         }
1736
1737         AliDebug(2,Form("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries",iCounts,nCounts));
1738         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
1739         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
1740         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
1741         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
1742         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
1743
1744         Int_t nentriesUsed = 0;
1745         if (iCountsRun > 1){
1746                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
1747                 AliDebug(2,"Calculating (weighted) Mean and Median");
1748                 arrayValues = new Float_t[iCountsRun]; 
1749                 arrayWeights = new Double_t[iCountsRun]; 
1750                 nentriesUsed = iCountsRun;
1751                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
1752                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1753                         Int_t timestamp2 = 0;
1754                         if (i < ientryEOR){
1755                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
1756                                 timestamp2 = (Int_t)v1->GetTimeStamp();
1757                         }
1758                         else {
1759                                 timestamp2 = timeEnd+1;
1760                         }
1761                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
1762                         arrayValues[i-ientrySOR] = v->GetFloat();
1763                 }
1764                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
1765                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
1766         }
1767         else if (iCountsRun == 1){
1768                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
1769                 nentriesUsed = 2;
1770                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
1771                         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.");
1772                         arrayValues = new Float_t[2];
1773                         arrayWeights = new Double_t[2];
1774                         arrayValues[0] = valueBeforeSOR;
1775                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
1776                         arrayValues[1] = v->GetFloat();
1777                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
1778                         AliDebug(2, Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
1779                         AliDebug(2, Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
1780                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
1781                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
1782                         truncMeanFlag = kFALSE;
1783                 }
1784                 else{
1785                         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");
1786                         parameters[0] = AliGRPObject::GetInvalidFloat();
1787                         parameters[1] = AliGRPObject::GetInvalidFloat();
1788                         parameters[2] = AliGRPObject::GetInvalidFloat();
1789                         parameters[3] = AliGRPObject::GetInvalidFloat();
1790                         parameters[4] = AliGRPObject::GetInvalidFloat();
1791                         return parameters;
1792                 }
1793         }
1794         else { // iCountsRun == 0, using only the point immediately before SOR
1795                 if (timestampBeforeSOR == -1){
1796                         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");
1797                         parameters[0] = AliGRPObject::GetInvalidFloat();
1798                         parameters[1] = AliGRPObject::GetInvalidFloat();
1799                         parameters[2] = AliGRPObject::GetInvalidFloat();
1800                         parameters[3] = AliGRPObject::GetInvalidFloat();
1801                         parameters[4] = AliGRPObject::GetInvalidFloat();
1802                         return parameters;
1803                 }
1804                 else {
1805                         AliWarning("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.");
1806                         AliDebug(2,Form("value = %f",valueBeforeSOR)); 
1807                         parameters[0] = valueBeforeSOR;
1808                         parameters[2] = valueBeforeSOR;
1809                         truncMeanFlag = kFALSE;
1810                         sdFlag = kFALSE;
1811                 }
1812         }
1813
1814         Float_t temp = 0;
1815         Float_t temp1 = 0;
1816         Float_t sumweights = 0; 
1817         Int_t entriesTruncMean = 0;
1818         Float_t* arrayValuesTruncMean = new Float_t[nentriesUsed]; 
1819         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
1820
1821         // calculating SD wrt Mean and Median
1822         AliDebug(2,"Calculating SD wrt Mean and SD wrt Median");
1823         if (sdFlag){
1824                 for (Int_t i =0; i< nentriesUsed; i++){
1825                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1826                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
1827                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
1828                         sumweights += arrayWeights[i];
1829                 }
1830                 // setting SD wrt Mean 
1831                 if (sumweights != 0 ){
1832                         parameters[3] = TMath::Sqrt(temp1/sumweights);
1833                 }
1834                 else {
1835                         AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
1836                         parameters[3] = AliGRPObject::GetInvalidFloat();
1837                 }
1838                 // setting SD wrt Median
1839                 if (nentriesUsed != 0){
1840                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
1841                 }
1842                 else{
1843                         AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
1844                         parameters[4] = AliGRPObject::GetInvalidFloat();
1845                 }
1846         }
1847         else {
1848                 parameters[3] = AliGRPObject::GetInvalidFloat();
1849                 parameters[4] = AliGRPObject::GetInvalidFloat();
1850         }               
1851
1852         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
1853         if (truncMeanFlag){
1854                 AliDebug(2,"Calculating Truncated Mean");
1855                 for (Int_t i =0; i< nentriesUsed; i++){
1856                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
1857                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
1858                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
1859                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
1860                                 AliDebug(2,Form("For Truncated Mean: Entry %d: value = %f, weight = %f",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]));
1861                                 entriesTruncMean++;                     
1862                         }
1863                         else{
1864                                 AliDebug(2,"Discarding entry");
1865                         }
1866                 }
1867                 // setting truncated mean 
1868                 if (entriesTruncMean >1){
1869                         AliDebug(2,Form("%d entries used for truncated mean",entriesTruncMean));
1870                         parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
1871                 }
1872                 else{   
1873                         AliDebug(2,Form("Too few entries (%d) to calculate truncated mean",entriesTruncMean));
1874                         parameters[1] = AliGRPObject::GetInvalidFloat();
1875                 }
1876         }
1877         else{
1878                         parameters[1] = AliGRPObject::GetInvalidFloat();
1879         }
1880
1881         AliInfo(Form("(weighted) mean = %f ",parameters[0]));
1882         AliInfo(Form("(weighted) truncated mean = %f ",parameters[1]));
1883         AliInfo(Form("median = %f ",parameters[2]));
1884         AliInfo(Form("(weighted) standard deviation with (weighted) mean = %f ",parameters[3]));
1885         AliInfo(Form("standard deviation with median = %f ",parameters[4]));
1886         
1887         return parameters;
1888 }
1889
1890 //__________________________________________________________________________________________________________________
1891
1892 Float_t* AliGRPPreprocessor::ProcessFloatAllMagnet(const TObjArray* array, Int_t indexDP, Bool_t &isZero)
1893 {
1894         // 
1895         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1896         // used for L3 and Dipole magnets, using isZero flag to decide whther the magnet was OFF/ON
1897         // the flag is set according to the L3/Dipole current value
1898         // current threshold for L3 = 350 A (value provided by DCS)
1899         // current threshold for Dipole = 450 A (value provided by DCS)
1900         //
1901         // parameters[0] = mean
1902         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1903         // parameters[2] = median
1904         // parameters[3] = standard deviation wrt mean
1905         // parameters[4] = standard deviation wrt median
1906         //
1907
1908         AliInfo(Form("indexDP = %d",indexDP)); 
1909
1910         Int_t nCounts = array->GetEntries();
1911         for(Int_t i = 0; i < nCounts; i++) {
1912                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1913                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
1914                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
1915                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
1916                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
1917                         return NULL;
1918                 }
1919                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1920                         AliDebug(2,Form("%d-th entry = %f",i,v->GetFloat()));
1921                         if (indexDP == kL3Current && v->GetFloat() > 350 && isZero == kTRUE) isZero=kFALSE; 
1922                         if (indexDP == kDipoleCurrent && v->GetFloat() > 450 && isZero == kTRUE) isZero=kFALSE; 
1923                 }
1924                 else {
1925                         AliError(Form("DCS values for the parameter outside the queried interval"));
1926                 }
1927         }
1928
1929         return ProcessFloatAll(array);
1930 }
1931
1932
1933 //_______________________________________________________________
1934
1935 Char_t AliGRPPreprocessor::ProcessBool(const TObjArray* array, Bool_t &change)
1936 {
1937         // 
1938         // processing Boolean values
1939         //
1940
1941         Bool_t aDCSBool = kTRUE;
1942
1943         AliDCSValue *v = 0x0;
1944
1945         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1946                 v = (AliDCSValue *)array->At(iCount);
1947                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1948                         AliError(Form("DCS values for the parameter outside the queried interval"));
1949                         continue;
1950                 }
1951                 if (iCount > 0) {
1952                         if (aDCSBool != v->GetBool()) {
1953                                 AliError(Form("DCS values for the parameter changed from %d to %d within the queried interval", (UInt_t)aDCSBool, (UInt_t)v->GetBool()));
1954                                 change = kTRUE;
1955                         }
1956                 }
1957                 aDCSBool = v->GetBool(); // always keeping last value
1958                 AliDebug(2,Form("Bool = %d",(Int_t)aDCSBool));
1959         }
1960         
1961         Char_t caDCSBool = (Char_t) aDCSBool;
1962         return caDCSBool;
1963         
1964 }
1965
1966 //_______________________________________________________________
1967
1968 Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
1969 {
1970         // 
1971         // processing Int values, returning mean
1972         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
1973         // are outside the queried time interval or their value is out of range
1974         //
1975
1976         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
1977         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
1978         if (timeStartString.IsNull() || timeStartString.IsNull()){
1979                 if (timeStartString.IsNull()){ 
1980                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
1981                 }
1982                 else if (timeStartString.IsNull()){
1983                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
1984                 }
1985                 return 0;
1986         }  
1987
1988         Int_t timeStart = (Int_t)(timeStartString.Atoi());
1989         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
1990         Float_t aDCSArrayMean = 0.0;
1991         Int_t iCounts = 0;
1992         Float_t valueBeforeSOR = 0;
1993         Float_t valueAfterEOR = 0;
1994         Int_t timestampBeforeSOR = -1;
1995         Int_t timestampAfterEOR = -1;
1996         Int_t ientrySOR = -1;
1997         Int_t ientryEOR = -1;
1998         Float_t* arrayValues = 0x0; 
1999         Double_t* arrayWeights = 0x0; 
2000         Int_t iCountsRun = 0;
2001         Int_t nCounts = array->GetEntries();
2002
2003         for(Int_t i = 0; i < nCounts; i++) {
2004                 AliDCSValue* v = (AliDCSValue *)array->At(i);
2005                 if ((v->GetInt() < fminInt) || (v->GetInt() > fmaxInt)) {
2006                         AliError(Form("Error! Int value found in DCS map at %d-th entry is OUT OF RANGE: value = %d",i, v->GetInt()));
2007                         return AliGRPObject::GetInvalidFloat();
2008                 }
2009                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2010                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetInt(),v->GetTimeStamp()));
2011                         iCounts += 1;
2012                         // look for the last value before SOR and the first value before EOR
2013                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2014                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2015                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2016                                 valueBeforeSOR = (Float_t) v->GetInt();
2017                         }
2018                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2019                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2020                                 valueAfterEOR = (Float_t) v->GetInt();
2021                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2022                         }
2023                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2024                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2025                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2026                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2027                                 AliDebug(2,Form("entry between SOR and EOR"));
2028                                 iCountsRun += 1;
2029                         }
2030                 }
2031                 else {
2032                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2033                 }
2034         }
2035
2036         if (timestampBeforeSOR == -1){
2037                 AliWarning("No value found before SOR!");
2038         }
2039         if (timestampAfterEOR == -1){
2040                 AliWarning("No value found after EOR!");
2041         }
2042
2043         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
2044         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2045         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2046         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2047         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2048         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2049
2050         Int_t nentriesUsed = 0;
2051         if (iCountsRun > 1){
2052                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2053                 AliDebug(2,"Calculating (weighted) Mean");
2054                 arrayValues = new Float_t[iCountsRun]; 
2055                 arrayWeights = new Double_t[iCountsRun]; 
2056                 nentriesUsed = iCountsRun;
2057                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2058                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2059                         Int_t timestamp2 = 0;
2060                         if (i < ientryEOR){
2061                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2062                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2063                         }
2064                         else {
2065                                 timestamp2 = timeEnd+1;
2066                         }
2067                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2068                         arrayValues[i-ientrySOR] = (Float_t)v->GetInt();
2069                 }
2070                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2071         }
2072         else if (iCountsRun == 1){
2073                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2074                 nentriesUsed = 2;
2075                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2076                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
2077                         arrayValues = new Float_t[2];
2078                         arrayWeights = new Double_t[2];
2079                         arrayValues[0] = valueBeforeSOR;
2080                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2081                         arrayValues[1] = (Float_t)v->GetInt();
2082                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2083                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2084                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2085                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
2086                 }
2087                 else{
2088                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2089                         return AliGRPObject::GetInvalidFloat();
2090                 }
2091         }
2092         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2093                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2094                         if (timestampBeforeSOR == -1){
2095                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2096                         }
2097                         if (timestampAfterEOR == -1){
2098                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2099                         }
2100                         return AliGRPObject::GetInvalidFloat();
2101                 }
2102                 else {
2103                         AliWarning("Using last entry before SOR and first entry after EOR.");
2104                         nentriesUsed = 2;
2105                         arrayValues = new Float_t[2];
2106                         arrayWeights = new Double_t[2];
2107                         arrayValues[0] = valueBeforeSOR;
2108                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2109                         arrayValues[1] = valueAfterEOR;
2110                         arrayWeights[1] = 1.;
2111                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2112                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2113                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2114                 }
2115         }
2116
2117         AliInfo(Form("mean = %f ", aDCSArrayMean));
2118         return aDCSArrayMean;
2119
2120 }
2121 //_______________________________________________________________
2122
2123 Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
2124 {
2125         // 
2126         // processing Int values, returning mean 
2127         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
2128         // are outside the queried time interval or their value is out of range
2129         //
2130
2131         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2132         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2133         if (timeStartString.IsNull() || timeStartString.IsNull()){
2134                 if (timeStartString.IsNull()){ 
2135                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2136                 }
2137                 else if (timeStartString.IsNull()){
2138                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2139                 }
2140                 return 0;
2141         }  
2142
2143         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2144         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2145         Float_t aDCSArrayMean = 0.0;
2146         Int_t iCounts = 0;
2147         Float_t valueBeforeSOR = 0;
2148         Float_t valueAfterEOR = 0;
2149         Int_t timestampBeforeSOR = -1;
2150         Int_t timestampAfterEOR = -1;
2151         Int_t ientrySOR = -1;
2152         Int_t ientryEOR = -1;
2153         Float_t* arrayValues = 0x0; 
2154         Double_t* arrayWeights = 0x0; 
2155         Int_t iCountsRun = 0;
2156         Int_t nCounts = array->GetEntries();
2157
2158         for(Int_t i = 0; i < nCounts; i++) {
2159                 AliDCSValue* v = (AliDCSValue *)array->At(i);
2160                 if ((v->GetUInt() < fminUInt) || (v->GetUInt() > fmaxUInt)) {
2161                         AliError(Form("Error! UInt value found in DCS map at %d-th entry is OUT OF RANGE: value = %u",i,v->GetUInt()));
2162                         return AliGRPObject::GetInvalidFloat();
2163                 }
2164                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2165                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetUInt(),v->GetTimeStamp()));
2166                         iCounts += 1;
2167                         // look for the last value before SOR and the first value before EOR
2168                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2169                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2170                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2171                                 valueBeforeSOR = (Float_t)v->GetUInt();
2172                         }
2173                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2174                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2175                                 valueAfterEOR = (Float_t)v->GetUInt();
2176                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2177                         }
2178                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2179                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2180                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2181                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2182                                 AliDebug(2,Form("entry between SOR and EOR"));
2183                                 iCountsRun += 1;
2184                         }
2185                 }
2186                 else {
2187                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2188                 }
2189         }
2190
2191         if (timestampBeforeSOR == -1){
2192                 AliWarning("No value found before SOR!");
2193         }
2194         if (timestampAfterEOR == -1){
2195                 AliWarning("No value found after EOR!");
2196         }
2197
2198         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
2199         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2200         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2201         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2202         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2203         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2204
2205         Int_t nentriesUsed = 0;
2206         if (iCountsRun > 1){
2207                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2208                 AliDebug(2,"Calculating (weighted) Mean");
2209                 arrayValues = new Float_t[iCountsRun]; 
2210                 arrayWeights = new Double_t[iCountsRun]; 
2211                 nentriesUsed = iCountsRun;
2212                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2213                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2214                         Int_t timestamp2 = 0;
2215                         if (i < ientryEOR){
2216                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2217                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2218                         }
2219                         else {
2220                                 timestamp2 = timeEnd+1;
2221                         }
2222                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2223                         arrayValues[i-ientrySOR] = (Float_t)v->GetUInt();
2224                 }
2225                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2226         }
2227         else if (iCountsRun == 1){
2228                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2229                 nentriesUsed = 2;
2230                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2231                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
2232                         arrayValues = new Float_t[2];
2233                         arrayWeights = new Double_t[2];
2234                         arrayValues[0] = valueBeforeSOR;
2235                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2236                         arrayValues[1] = (Float_t)v->GetUInt();
2237                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2238                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2239                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2240                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
2241                 }
2242                 else{
2243                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2244                         return AliGRPObject::GetInvalidFloat();
2245                 }
2246         }
2247         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2248                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2249                         if (timestampBeforeSOR == -1){
2250                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2251                         }
2252                         if (timestampAfterEOR == -1){
2253                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2254                         }
2255                         return AliGRPObject::GetInvalidFloat();
2256                 }
2257                 else {
2258                         AliWarning("Using last entry before SOR and first entry after EOR.");
2259                         nentriesUsed = 2;
2260                         arrayValues = new Float_t[2];
2261                         arrayWeights = new Double_t[2];
2262                         arrayValues[0] = valueBeforeSOR;
2263                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2264                         arrayValues[1] = valueAfterEOR;
2265                         arrayWeights[1] = 1.;
2266                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2267                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2268                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2269                 }
2270         }
2271
2272         AliInfo(Form("mean = %f ",aDCSArrayMean));
2273         return aDCSArrayMean;
2274
2275 }
2276
2277
2278 //_______________________________________________________________
2279
2280 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
2281 {
2282         // extract DCS pressure maps. Perform fits to save space
2283         
2284         TMap *map = fPressure->ExtractDCS(dcsAliasMap);
2285         if (map) {
2286                 AliDebug(2,Form("Map has %d entries",map->GetEntries()));
2287                 fPressure->MakeSplineFit(map);
2288                 Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
2289                 if (fitFraction > kFitFraction ) {
2290                         AliInfo(Form("Pressure values extracted, %d fits performed for %d sensors.", fPressure->NumFits(),fPressure->NumSensors()));
2291                 } else { 
2292                         AliInfo("Too few pressure maps fitted!!!");
2293                 }
2294         } else {
2295                 AliInfo("no atmospheric pressure map extracted!!!");
2296         }
2297         delete map;
2298         
2299         return fPressure;
2300 }
2301
2302
2303   
2304 //_______________________________________________________________
2305 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)
2306 {
2307         //
2308         // Retrieves logbook and trigger information from the online logbook
2309         // This information is needed for prompt reconstruction
2310         //
2311         // Parameters are:
2312         // Run number
2313         // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
2314         // cdbRoot
2315         //
2316         // returns:
2317         //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
2318         //         0 on success and no run was found
2319         //         negative on error
2320         //
2321         // This function is NOT called during the preprocessor run in the Shuttle!
2322         //
2323         
2324         // defaults
2325         if (dbPort == 0)
2326                 dbPort = 3306;
2327         
2328         // CDB connection
2329         AliCDBManager* cdb = AliCDBManager::Instance();
2330         cdb->SetDefaultStorage(cdbRoot);
2331         
2332         // SQL connection
2333         TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
2334         
2335         if (!server)
2336                 {
2337                         Printf("ERROR: Could not connect to DAQ LB");
2338                         return -1;
2339                 }
2340         
2341         // main logbook
2342         TString sqlQuery;
2343         sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent FROM logbook WHERE run = %d", run);
2344         TSQLResult* result = server->Query(sqlQuery);
2345         if (!result)
2346                 {
2347                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2348                         return -2;
2349                 }
2350         
2351         if (result->GetRowCount() == 0)
2352                 {
2353                         Printf("ERROR: Run %d not found", run);
2354                         delete result;
2355                         return -3;
2356                 }
2357         
2358         TSQLRow* row = result->Next();
2359         if (!row)
2360                 {
2361                         Printf("ERROR: Could not receive data from run %d", run);
2362                         delete result;
2363                         return -4;
2364                 }
2365         
2366         TString timeStartString(row->GetField(0));
2367         TString runType(row->GetField(1));
2368         TString detectorMaskString(row->GetField(2));
2369         TString l3CurrentString(row->GetField(3));
2370         TString dipoleCurrentString(row->GetField(4));
2371         time_t timeStart = (time_t)(timeStartString.Atoi());
2372         UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
2373         Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
2374         Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
2375         Char_t l3Polarity = (l3CurrentString.Atof() < 0) ? 1 : 0;
2376         Char_t dipolePolarity = (dipoleCurrentString.Atof() < 0) ? 1 : 0;
2377         
2378         AliGRPObject * grpObj = new AliGRPObject();
2379         grpObj->SetTimeStart(timeStart); 
2380         grpObj->SetRunType((TString)(row->GetField(1)));
2381         grpObj->SetDetectorMask(detectorMask);
2382         grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
2383         grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
2384         grpObj->SetL3Polarity(l3Polarity);
2385         grpObj->SetDipolePolarity(dipolePolarity);
2386         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
2387
2388         delete row;
2389         row = 0;
2390         
2391         delete result;
2392         result = 0;
2393         
2394         Printf("Storing GRP/GRP/Data object with the following content");
2395         grpObj->Dump();
2396         
2397         AliCDBMetaData metadata;
2398         metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus & Chiara Zampolli");
2399         metadata.SetComment("GRP Output parameters received during online running");
2400         
2401         AliCDBId id("GRP/GRP/Data", run, run);
2402         Bool_t success = cdb->Put(grpObj, id, &metadata);
2403         
2404         delete grpObj;
2405         
2406         if (!success)
2407                 {
2408                         Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
2409                         return -5;
2410                 }
2411         
2412         // Receive trigger information
2413         sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
2414         result = server->Query(sqlQuery);
2415         if (!result)
2416                 {
2417                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2418                         return -11;
2419                 }
2420         
2421         if (result->GetRowCount() == 0)
2422                 {
2423                         Printf("ERROR: Run %d not found in logbook_trigger_config", run);
2424                         delete result;
2425                         return -12;
2426                 }
2427         
2428         row = result->Next();
2429         if (!row)
2430                 {
2431                         Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
2432                         delete result;
2433                         return -13;
2434                 }
2435         
2436         TString triggerConfig(row->GetField(0));
2437         
2438         delete row;
2439         row = 0;
2440         
2441         delete result;
2442         result = 0;
2443         
2444         Printf("Found trigger configuration: %s", triggerConfig.Data());
2445         
2446         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
2447         if (!runcfg)
2448                 {
2449                         Printf("ERROR: Could not create CTP configuration object");
2450                         return -14;
2451                 }
2452         
2453         metadata.SetComment("CTP run configuration received during online running");
2454         
2455         AliCDBId id2("GRP/CTP/Config", run, run);
2456         success = cdb->Put(runcfg, id2, &metadata);
2457         
2458         delete runcfg;
2459         runcfg = 0;
2460         
2461         if (!success)
2462                 {
2463                         Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
2464                         return -15;
2465                 }
2466         
2467
2468         // Receive list of GDCs for this run
2469         sqlQuery.Form("SELECT GDC FROM logbook_stats_GDC WHERE run = %d", run);
2470         result = server->Query(sqlQuery);
2471         if (!result)
2472                 {
2473                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2474                         return -24;
2475                 }
2476         
2477         if (result->GetRowCount() == 0)
2478                 {
2479                         Printf("ERROR: Run %d not found in logbook_stats_GDC", run);
2480                         delete result;
2481                         return -25;
2482                 }
2483
2484         gdc = "";
2485         for (Int_t iGDC = 0; iGDC < result->GetRowCount(); iGDC++) {
2486           row = result->Next();
2487           if (!row)
2488             {
2489               Printf("ERROR: Could not receive logbook_stats_GDC data from run %d", run);
2490               delete result;
2491               return -26;
2492             }
2493           gdc += row->GetField(0);
2494           gdc += " ";
2495         }
2496
2497         delete row;
2498         row = 0;
2499         
2500         delete result;
2501         result = 0;
2502         
2503         Printf("Found GDC: %s", gdc.Data());
2504
2505         // get last run with same run type that was already processed by the SHUTTLE
2506         
2507         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());
2508         result = server->Query(sqlQuery);
2509         if (!result)
2510                 {
2511                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2512                         return -21;
2513                 }
2514         
2515         if (result->GetRowCount() == 0)
2516                 {
2517                         Printf("ERROR: No result with query <%s>", sqlQuery.Data());
2518                         delete result;
2519                         return -22;
2520                 }
2521         
2522         row = result->Next();
2523         if (!row)
2524                 {
2525                         Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
2526                         delete result;
2527                         return -23;
2528                 }
2529         
2530         TString lastRunStr(row->GetField(0));
2531         Int_t lastRun = lastRunStr.Atoi();
2532         
2533         Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
2534         
2535         delete row;
2536         row = 0;
2537         
2538         delete result;
2539         result = 0;
2540         
2541         server->Close();
2542         delete server;
2543         server = 0;
2544         
2545         return lastRun;
2546 }
2547 //-----------------------------------------------------------------
2548 Double_t AliGRPPreprocessor::CalculateMean(TObjArray* const array){
2549
2550         //
2551         // Calculating mean over TObjArray from LHC Data
2552         //
2553
2554         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2555         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2556         if (timeStartString.IsNull() || timeStartString.IsNull()){
2557                 if (timeStartString.IsNull()){ 
2558                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2559                 }
2560                 else if (timeStartString.IsNull()){
2561                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2562                 }
2563                 return 0;
2564         }  
2565
2566         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2567         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2568         timeStart = 1260646960;
2569         timeEnd = 1260652740;
2570         Double_t* parameters = new Double_t[5];
2571         parameters[0] = -1.;
2572         parameters[1] = -1.;
2573         parameters[2] = -1.;
2574         parameters[3] = -1.;
2575         parameters[4] = -1.;
2576         Int_t iCounts = 0;
2577         Int_t iCountsRun = 0;
2578         Int_t nCounts = array->GetEntries();
2579         printf("ncounts = %d\n",nCounts);
2580         Double_t valueBeforeSOR = 0;
2581         Double_t valueAfterEOR = 0;
2582         Double_t timestampBeforeSOR = -1.;
2583         Double_t timestampAfterEOR = -1.;
2584         Int_t ientrySOR = -1;
2585         Int_t ientryEOR = -1;
2586         Double_t* arrayValues = 0x0; 
2587         Double_t* arrayWeights = 0x0; 
2588         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
2589         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
2590
2591         for(Int_t i = 0; i < nCounts; i++) {
2592                 AliDCSArray *dcs = (AliDCSArray*)array->At(i);
2593                 if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
2594                         AliDebug(2,Form("%d-th entry = %f at timestamp %f\n",i,(Double_t)(dcs->GetInt(0)),dcs->GetTimeStamp()));
2595                         iCounts += 1;
2596                         // look for the last value before SOR and the first value before EOR
2597                         if ((dcs->GetTimeStamp() >= timeStart) && (dcs->GetTimeStamp() < timeStart)) {
2598                                 timestampBeforeSOR = dcs->GetTimeStamp();
2599                                 AliDebug(2,Form("timestamp of last value before SOR = %f, with DAQ_time_start = %d\n",timestampBeforeSOR,timeStart));
2600                                 valueBeforeSOR = (Double_t)(dcs->GetInt(0));
2601                         }
2602                         else if ((dcs->GetTimeStamp() <= timeEnd) && (dcs->GetTimeStamp() > timeEnd) && timestampAfterEOR == -1){
2603                                 timestampAfterEOR = dcs->GetTimeStamp();
2604                                 valueAfterEOR = (Double_t)(dcs->GetInt(0));
2605                                 AliDebug(2,Form("timestamp of first value after EOR = %f, with DAQ_time_end = %d\n",timestampAfterEOR,timeEnd));
2606                         }
2607                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2608                         if((dcs->GetTimeStamp() >= timeStart) &&(dcs->GetTimeStamp() <= timeEnd)) {
2609                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2610                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2611                                 AliDebug(2,Form("entry between SOR and EOR\n"));
2612                                 iCountsRun += 1;
2613                         }
2614                 }
2615                 else {
2616                         printf("DCS values for the parameter outside the queried interval: timestamp = %f\n",dcs->GetTimeStamp());
2617                 }
2618         }
2619
2620         if (timestampBeforeSOR == -1.){
2621                 printf("No value found before SOR\n");
2622         }
2623         if (timestampAfterEOR == -1.){
2624                 printf("No value found after EOR\n");
2625         }
2626
2627         printf("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries\n",iCounts,nCounts);
2628         printf("Last value before DAQ_time_start (SOR) = %f at timestamp = %f\n",valueBeforeSOR,timestampBeforeSOR);
2629         printf("First value after DAQ_time_end (EOR)   = %f at timestamp = %f\n",valueAfterEOR,timestampAfterEOR);
2630         printf("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n",iCountsRun);
2631         printf("Index of first entry after DAQ_time_start (SOR) = %d\n ",ientrySOR);
2632         printf("Index of first entry before DAQ_time_end (EOR) = %d\n ",ientryEOR);
2633
2634         Int_t nentriesUsed = 0;
2635         if (iCountsRun > 1){
2636                 printf("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)\n");
2637                 printf("Calculating (weighted) Mean and Median\n" );
2638                 arrayValues = new Double_t[iCountsRun]; 
2639                 arrayWeights = new Double_t[iCountsRun]; 
2640                 nentriesUsed = iCountsRun;
2641                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2642                         AliDCSArray *dcs = (AliDCSArray *)array->At(i);
2643                         Double_t timestamp2 = 0;
2644                         if (i < ientryEOR){
2645                                 AliDCSArray *dcs1 = (AliDCSArray *)array->At(i+1);
2646                                 timestamp2 = dcs1->GetTimeStamp();
2647                         }
2648                         else {
2649                                 timestamp2 = (Double_t)timeEnd+1;
2650                         }
2651                         arrayWeights[i-ientrySOR] = (Double_t)((Double_t)timestamp2 - dcs->GetTimeStamp());
2652                         arrayValues[i-ientrySOR] = (Double_t)(dcs->GetInt(0));
2653                         printf("Entry %d: value = %f, weight = %f\n",i-ientrySOR,arrayValues[i-ientrySOR],arrayWeights[i-ientrySOR]);
2654                 }
2655                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2656                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
2657         }
2658         else if (iCountsRun == 1){
2659                 AliDCSArray* dcs = (AliDCSArray *)array->At(ientrySOR);
2660                 nentriesUsed = 2;
2661                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)dcs->GetTimeStamp()){
2662                         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");
2663                         arrayValues = new Double_t[2];
2664                         arrayWeights = new Double_t[2];
2665                         arrayValues[0] = valueBeforeSOR;
2666                         arrayWeights[0] = (Double_t)(dcs->GetTimeStamp()-(Double_t)timestampBeforeSOR);
2667                         arrayValues[1] = (Double_t)(dcs->GetInt(0));
2668                         arrayWeights[1] = (Double_t)((Double_t)timeEnd+1-dcs->GetTimeStamp());
2669                         printf("value0 = %f, with weight = %f\n",arrayValues[0],arrayWeights[0]); 
2670                         printf("value1 = %f, with weight = %f\n",arrayValues[1],arrayWeights[1]); 
2671                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
2672                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
2673                         truncMeanFlag = kFALSE;
2674                 }
2675                 else{
2676                         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");
2677                         parameters[0] = -1;
2678                         parameters[1] = -1;
2679                         parameters[2] = -1;
2680                         parameters[3] = -1;
2681                         parameters[4] = -1;
2682                         return parameters[0];
2683                 }
2684         }
2685         else { // iCountsRun == 0, using only the point immediately before SOR
2686                 if (timestampBeforeSOR == -1.){
2687                         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");
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                 else {
2696                         printf("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.\n");
2697                         printf("value = %f\n",valueBeforeSOR); 
2698                         parameters[0] = valueBeforeSOR;
2699                         parameters[2] = valueBeforeSOR;
2700                         truncMeanFlag = kFALSE;
2701                         sdFlag = kFALSE;
2702                 }
2703         }
2704
2705         Double_t temp = 0;
2706         Double_t temp1 = 0;
2707         Double_t sumweights = 0; 
2708         Int_t entriesTruncMean = 0;
2709         Double_t* arrayValuesTruncMean = new Double_t[nentriesUsed]; 
2710         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
2711
2712         // calculating SD wrt Mean and Median
2713         printf("Calculating SD wrt Mean and SD wrt Median\n");
2714         if (sdFlag){
2715                 for (Int_t i =0; i< nentriesUsed; i++){
2716                         //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
2717                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
2718                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
2719                         sumweights += arrayWeights[i];
2720                 }
2721                 // setting SD wrt Mean 
2722                 if (sumweights != 0 ){
2723                         parameters[3] = TMath::Sqrt(temp1/sumweights);
2724                 }
2725                 else {
2726                         printf("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid\n");
2727                         parameters[3] = -1;
2728                 }
2729                 // setting SD wrt Median
2730                 if (nentriesUsed != 0){
2731                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
2732                 }
2733                 else{
2734                         printf("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid\n");
2735                         parameters[4] = -1;
2736                 }
2737         }
2738         else {
2739                 parameters[3] = -1;
2740                 parameters[4] = -1;
2741         }               
2742
2743         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
2744         if (truncMeanFlag){
2745                 printf("Calculating Truncated Mean\n");
2746                 for (Int_t i =0; i< nentriesUsed; i++){
2747                         //printf("Entry %d: value = %f, weight = %f\n",i,arrayValues[i],arrayWeights[i]);
2748                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
2749                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
2750                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
2751                                 printf("For Truncated Mean: Entry %d: value =&nbs