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