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