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