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