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