]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliGRPPreprocessor.cxx
Possibility to alias some params to others
[u/mrichter/AliRoot.git] / STEER / 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 <THashList.h>
30 #include <TObjString.h>
31 #include <TObjArray.h>
32 #include <TGraph.h>
33 #include <TString.h>
34 #include <TFile.h>
35 #include <TPRegexp.h>
36
37 #include <float.h>
38
39 #include "AliGRPPreprocessor.h"
40 #include "AliGRPObject.h"
41 #include "AliDCSSensor.h"
42 #include "AliSplineFit.h"
43 #include "AliDCSSensorArray.h"
44 #include "AliRawEventHeaderVersions.h"
45
46 #include "AliTriggerConfiguration.h"
47 #include "AliTriggerRunScalers.h"
48 #include "AliTriggerInput.h"
49
50 #include "AliCDBMetaData.h"
51 #include "AliESDVertex.h"
52 #include "AliLHCReader.h"
53 #include "AliLHCData.h"
54 #include "AliDCSArray.h"
55 #include "AliDAQ.h"
56 #include "AliLTUConfig.h"
57 #include "AliQAThresholds.h"
58
59 class AliLog;
60 class AliDCSValue;
61 class AliShuttleInterface;
62
63 // needed for ReceivePromptRecoParameters
64
65 #include <TSQLServer.h>
66 #include <TSQLResult.h>
67 #include <TSQLRow.h>
68 #include <AliCDBManager.h>
69 #include <AliCDBMetaData.h>
70 #include <AliCDBId.h>
71 #include <AliTriggerConfiguration.h>
72 #include "AliCTPTimeParams.h"
73 #include "AliLHCClockPhase.h"
74
75 ClassImp(AliGRPPreprocessor)
76
77
78 const Double_t kFitFraction = -1.;                 // Fraction of DCS sensor fits required
79
80 //_______________________________________________________________
81
82   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 6; // num parameters in the logbook used to fill the GRP object
83   const Int_t AliGRPPreprocessor::fgknDCSDP = 48;   // number of dcs dps
84   const Int_t AliGRPPreprocessor::fgknDCSDPHallProbes = 40;   // number of dcs dps
85   const Int_t AliGRPPreprocessor::fgknLHCDP = 9;   // number of dcs dps from LHC data
86   const Int_t AliGRPPreprocessor::fgkDCSDPHallTopShift = 4;   // shift from the top to get tp the Hall Probes names in the list of DCS DPs
87   const Int_t AliGRPPreprocessor::fgkDCSDPNonWorking = 5; // number of non working DCS DPs
88   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
89                    "L3Polarity",
90                    "DipolePolarity",
91                    "L3Current",
92                    "DipoleCurrent",
93                    "L3_BSF17_H1",
94                    "L3_BSF17_H2",
95                    "L3_BSF17_H3",
96                    "L3_BSF17_Temperature",
97                    "L3_BSF4_H1",
98                    "L3_BSF4_H2",
99                    "L3_BSF4_H3",
100                    "L3_BSF4_Temperature",
101                    "L3_BKF17_H1",
102                    "L3_BKF17_H2",
103                    "L3_BKF17_H3",
104                    "L3_BKF17_Temperature",
105                    "L3_BKF4_H1",
106                    "L3_BKF4_H2",
107                    "L3_BKF4_H3",
108                    "L3_BKF4_Temperature",
109                    "L3_BSF13_H1",
110                    "L3_BSF13_H2",
111                    "L3_BSF13_H3",
112                    "L3_BSF13_Temperature",
113                    "L3_BSF8_H1",
114                    "L3_BSF8_H2",
115                    "L3_BSF8_H3",
116                    "L3_BSF8_Temperature",
117                    "L3_BKF13_H1",
118                    "L3_BKF13_H2",
119                    "L3_BKF13_H3",
120                    "L3_BKF13_Temperature",
121                    "L3_BKF8_H1",
122                    "L3_BKF8_H2",
123                    "L3_BKF8_H3",
124                    "L3_BKF8_Temperature",
125                    "Dipole_Inside_H1",
126                    "Dipole_Inside_H2",
127                    "Dipole_Inside_H3",
128                    "Dipole_Inside_Temperature",
129                    "Dipole_Outside_H1",
130                    "Dipole_Outside_H2",
131                    "Dipole_Outside_H3",
132                    "Dipole_Outside_Temperature",
133                    "CavernTemperature",
134                    "CavernAtmosPressure",
135                    "SurfaceAtmosPressure",
136                    "CavernAtmosPressure2"
137                  };
138
139   const char* AliGRPPreprocessor::fgkDCSDataPointsHallProbes[AliGRPPreprocessor::fgknDCSDPHallProbes] = {
140                    "L3_BSF17_H1",
141                    "L3_BSF17_H2",
142                    "L3_BSF17_H3",
143                    "L3_BSF17_Temperature",
144                    "L3_BSF4_H1",
145                    "L3_BSF4_H2",
146                    "L3_BSF4_H3",
147                    "L3_BSF4_Temperature",
148                    "L3_BKF17_H1",
149                    "L3_BKF17_H2",
150                    "L3_BKF17_H3",
151                    "L3_BKF17_Temperature",
152                    "L3_BKF4_H1",
153                    "L3_BKF4_H2",
154                    "L3_BKF4_H3",
155                    "L3_BKF4_Temperature",
156                    "L3_BSF13_H1",
157                    "L3_BSF13_H2",
158                    "L3_BSF13_H3",
159                    "L3_BSF13_Temperature",
160                    "L3_BSF8_H1",
161                    "L3_BSF8_H2",
162                    "L3_BSF8_H3",
163                    "L3_BSF8_Temperature",
164                    "L3_BKF13_H1",
165                    "L3_BKF13_H2",
166                    "L3_BKF13_H3",
167                    "L3_BKF13_Temperature",
168                    "L3_BKF8_H1",
169                    "L3_BKF8_H2",
170                    "L3_BKF8_H3",
171                    "L3_BKF8_Temperature",
172                    "Dipole_Inside_H1",
173                    "Dipole_Inside_H2",
174                    "Dipole_Inside_H3",
175                    "Dipole_Inside_Temperature",
176                    "Dipole_Outside_H1",
177                    "Dipole_Outside_H2",
178                    "Dipole_Outside_H3",
179                    "Dipole_Outside_Temperature"
180                  };
181                  
182   const Short_t kSensors = 45; // start index position of sensor in DCS DPs
183   const Short_t kNumSensors = 3; // Number of sensors in DCS DPs (CavernAtmosPressure, SurfaceAtmosPressure, CavernAtmosPressure2)
184
185
186   const char* AliGRPPreprocessor::fgkLHCDataPoints[AliGRPPreprocessor::fgknLHCDP] = {
187           "LHC_Beam_Energy",
188           "LHC_MachineMode",
189           "LHC_BeamMode",
190           "LHC_Beams_Particle_Type",
191           "BPTX_Phase_Shift_B1",
192           "BPTX_Phase_Shift_B2",
193           "LHC_Particle_Type_B1",
194           "LHC_Particle_Type_B2",
195           "LHC_Data_Quality_Flag"
196   };
197
198   const char* kppError[] = {
199                    "",
200                    "(DAQ logbook ERROR)",
201                    "(DAQ FXS ERROR)",
202                    "(Trigger Scalers not found in FXS - ERROR)",
203                    "(DCS data points ERROR)",
204                    "(Trigger Configuration ERROR)",
205                    "(DAQ logbook ERROR determining partition of the run)",
206                    "(CTP timing ERROR)",
207                    "(SPD Mean Vertex ERROR)",
208                    "(FXS Error for LHC Data)",
209                    "(LHC Data Error)",
210                    "(LHC Clock Phase Error (from LHC Data))",
211                    "(LTU Configuration Error)",
212                    "(DQM Failure)",
213                    "(Trigger Aliases wrong or not found in DCS FXS - ERROR)"
214   };
215
216 //_______________________________________________________________
217
218 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
219         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))
220 {
221         // constructor - shuttle must be instantiated!
222
223         AddRunType("COSMIC");
224         AddRunType("LASER");
225         AddRunType("PHYSICS");
226         AddRunType("CALIBRATION_BC");
227         AddRunType("CALIBRATION_CENTRAL");
228         AddRunType("CALIBRATION_EMD");
229         AddRunType("CALIBRATION_MB");
230         AddRunType("CALIBRATION_SEMICENTRAL");
231         AddRunType("CALIBRATION");
232         AddRunType("PEDESTAL");
233         AddRunType("STANDALONE");
234         AddRunType("GAIN");
235         AddRunType("NOISE");
236         AddRunType("PULSER");
237         AddRunType("STANDALONE_PULSER");
238         AddRunType("STANDALONE_BC");
239
240         fmaxFloat = FLT_MAX;
241         fminFloat = -FLT_MAX;
242         fmaxDouble = DBL_MAX;
243         fminDouble = -DBL_MAX;
244         fmaxInt = kMaxInt;
245         fminInt = kMinInt;
246         fmaxUInt = kMaxUInt;
247         fminUInt = 0;
248
249         AliInfo(Form("Max allowed float = %6.5e",fmaxFloat));
250         AliInfo(Form("Min allowed float = %6.5e",fminFloat));
251         AliInfo(Form("Max allowed double = %6.5e",fmaxDouble));
252         AliInfo(Form("Min allowed double = %6.5e",fminDouble));
253         AliInfo(Form("Max allowed integer = %d",fmaxInt));
254         AliInfo(Form("Min allowed integer = %d",fminInt));
255         AliInfo(Form("Max allowed unsigned integer = %u",(Int_t)fmaxUInt));
256         AliInfo(Form("Min allowed unsigned integer = %u",(Int_t)fminUInt));
257
258         ffailedDPs->SetOwner(kTRUE);
259 }
260
261 //_______________________________________________________________
262
263 AliGRPPreprocessor::~AliGRPPreprocessor()
264 {
265         //destructor
266         
267         delete fPressure;
268         delete ffailedDPs;
269
270 }
271
272 //_______________________________________________________________
273
274 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
275 {
276         // Initialize preprocessor
277
278         AliPreprocessor::Initialize(run, startTime, endTime);
279         
280         AliInfo("Initialization of the GRP preprocessor.");
281         AliInfo(Form("Start Time DCS = %d",GetStartTimeDCSQuery()));
282         AliInfo(Form("End Time DCS = %d",GetEndTimeDCSQuery()));
283         TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
284         for(Int_t j = 0; j < kNumSensors; j++) {
285                 AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
286                 sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
287         }
288         AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
289         
290         fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
291
292         ffailedDPs->Clear(); // cleaning ffailedDPs for current run
293         for (Int_t iDP=0; iDP < fgknDCSDP; iDP++){
294                 TObjString* dp = new TObjString(fgkDCSDataPoints[iDP]);
295                 ffailedDPs->AddAt(dp,iDP);
296         }
297
298 }
299
300 //_______________________________________________________________
301
302 UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
303 {
304         // process data retrieved by the Shuttle
305         
306         // retrieving "partition" and "detector" fields from DAQ logbook to 
307         // determine the partition in which the run was taken
308         // the partition is used to decide how to react in case of errors for CTP
309
310         TString partition = (TString)GetRunParameter("partition");  
311         TString detector = (TString)GetRunParameter("detector");   
312
313         AliGRPObject *grpobj = new AliGRPObject();  // object to store data
314         grpobj->SetBeamEnergyIsSqrtSHalfGeV(); // new format
315
316         //=================//
317         // DAQ logbook     //
318         //=================//
319
320         Log("*************** Processing DAQ logbook");
321
322         UInt_t error = 0;
323         
324         Int_t iDaqLB = ProcessDaqLB(grpobj);
325         TString runType = (TString)GetRunType();
326         TString beamType = (TString)GetRunParameter("beamType");
327         if(iDaqLB == fgknDAQLbPar) {
328                 Log(Form("DAQ Logbook, successful! Retrieved %d/%d entries",iDaqLB,fgknDAQLbPar));
329         } else {
330                 Log(Form("DAQ Logbook, could not get all expected entries!!! Retrieved only %d/%d entries",iDaqLB,fgknDAQLbPar));
331                 error |= 1;
332         }
333
334         //=================//
335         // DAQ FXS         //
336         //=================//
337
338         Log("*************** Processing DAQ FXS");
339
340         UInt_t iDaqFxs = ProcessDaqFxs();
341         if( iDaqFxs == 0 ) {
342                 Log(Form("DAQ FXS, successful!"));
343         } else {
344                 Log(Form("DAQ FXS, could not store run raw tag file!!!"));
345                 error |= 2;
346         }
347         
348         //=================//
349         // DCS FXS         //
350         //=================//
351
352         Log("*************** Processing DCS FXS");
353
354         UInt_t iDcsFxs = ProcessDcsFxs(partition, detector);
355         if( iDcsFxs == 0 ) {
356                 Log(Form("DCS FXS, successful!"));
357         } else  if (iDcsFxs ==1) {
358                 Log(Form("Could not store CTP scalers!!!"));
359                 error |= 4;
360         } else  if (iDcsFxs == 2) {
361                 Log(Form("Could not store CTP aliases!!!"));
362                 error |= 8192;
363         } else{
364                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without CTP scalers...",partition.Data(),detector.Data()));
365                 error |= 32;
366         }
367         
368         //=================//
369         // DCS data points //
370         //=================//
371
372         Log("*************** Processing DCS DPs");
373
374         Log(Form("Starting DCS Query at %d and finishing at %d",GetStartTimeDCSQuery(),GetEndTimeDCSQuery()));
375         Int_t entries = ProcessDcsDPs( valueMap, grpobj );
376         Log(Form("entries found = %d (should be %d)",entries, fgknDCSDP-fgkDCSDPNonWorking));
377         if (fdaqStartEndTimeOk){
378                 if( entries < fgknDCSDP - fgkDCSDPNonWorking ) { // L3_BSF4_H3, L3_BSF17_H1, L3_BSF17_H2, L3_BSF17_H3, L3_BSF17_Temperature are not working yet...  
379                         Log(Form("Possible problem with the DCS data points!!! Only %d/%d entries found - Please read further for more details",entries,fgknDCSDP-fgkDCSDPNonWorking));
380                         Log(Form("The DPs giving problems were:"));
381                         for (Int_t iDP = 0; iDP < fgknDCSDP; iDP++){
382                                 TObjString *dpString = (TObjString*)ffailedDPs->At(iDP);
383                                 if (dpString){
384                                         TString name = dpString->String();
385                                         if (name != "L3_BSF4_H3" && name != "L3_BSF17_H1" && name != "L3_BSF17_H2" && name != "L3_BSF17_H3" && name != "L3_BSF17_Temperature" ){
386                                                 Log(Form("******** %s ******** not present, but foreseen --> causing an ERROR",name.Data()));
387                                         }
388                                         else {
389                                                 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()));
390                                         }
391                                 }
392                         }
393                         error |= 8;
394                 }
395                 else  Log(Form("DCS data points, successful!"));
396         }
397         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")); 
398         
399         //=======================//
400         // Trigger Configuration //
401         //=======================//
402
403         Log("*************** Processing Trigger Configuration");
404
405         const char * triggerConf = GetTriggerConfiguration();
406
407         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
408                 Log("STANDALONE partition for current run, using Trigger Configuration dummy value");
409                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyConfig");
410                 if (!cdbEntry) {
411                         Log(Form("No dummy CTP configuration entry found, going into error..."));
412                         error |= 16;
413                 }
414                 else{
415                         AliTriggerConfiguration *runcfg = (AliTriggerConfiguration*)cdbEntry->GetObject();
416                         if (!runcfg){
417                                 Log(Form("dummy CTP config not found in OCDB entry, going into error..."));
418                                 error |= 16;
419                         }
420                         else {
421                                 TString titleCTPcfg = Form("CTP cfg for run %i from Dummy entry in OCDB",fRun);
422                                 runcfg->SetTitle(titleCTPcfg);
423                                 AliCDBMetaData metaData;
424                                 metaData.SetResponsible("Roman Lietava");
425                                 metaData.SetComment("CTP run configuration from dummy entry in OCDB");
426                                 if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
427                                         Log("Unable to store the dummy CTP run configuration object to OCDB!");
428                                         error |= 16;
429                                 }
430                         }
431                 }
432         }
433
434         else if (!partition.IsNull() && detector.IsNull()){ // global partition
435                 Log("GLOBAL partition for current run, using Trigger Configuration from DAQ Logbook");
436                 if (triggerConf!= NULL) {
437                         Log("Found trigger configuration in DAQ logbook");
438                         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConf);      
439                         if (!runcfg) {
440                                 Log("Bad CTP run configuration file from DAQ logbook! The corresponding CDB entry will not be filled!");
441                                 error |= 16;
442                         }
443                         else {
444                                 TString titleCTPcfg = Form("CTP cfg for run %i from DAQ",fRun);
445                                 runcfg->SetTitle(titleCTPcfg);
446                                 AliCDBMetaData metaData;
447                                 metaData.SetBeamPeriod(0);
448                                 metaData.SetResponsible("Roman Lietava");
449                                 metaData.SetComment("CTP run configuration from DAQ logbook");
450                                 if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
451                                         Log("Unable to store the CTP run configuration object to OCDB!");
452                                         error |= 16;
453                                 }
454                         }
455                 }
456
457                 else {
458                         Log("Trigger configuration NULL in DAQ logbook");
459                         error |= 16;
460                 }
461         }
462
463         else {
464                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger configuration...",partition.Data(),detector.Data()));
465                 error |= 32;
466         }
467
468         //===========================//
469         // Trigger Timing Parameters //
470         //===========================//
471
472         Log("*************** Processing Trigger Time Params");
473         
474         const char * triggerCTPtiming = GetCTPTimeParams();
475
476         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
477                 Log("STANDALONE partition for current run, using CTP timing params dummy value");
478                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyCTPtime");
479                 if (!cdbEntry) {
480                         Log(Form("No dummy CTP timing parameters entry found, going into error..."));
481                         error |= 64;
482                 }
483                 else{
484                         AliCTPTimeParams *runCTPtiming = (AliCTPTimeParams*)cdbEntry->GetObject();
485                         if (!runCTPtiming){
486                                 Log(Form("dummy CTP timing parameters not found in OCDB entry, going into error..."));
487                                 error |= 64;
488                         }
489                         else {
490                                 TString titleCTPtiming = Form("CTP timing params for run %i from Dummy entry in OCDB",fRun);
491                                 runCTPtiming->SetTitle(titleCTPtiming);
492                                 AliCDBMetaData metadata;
493                                 metadata.SetResponsible("Roman Lietava");
494                                 metadata.SetComment("CTP run timing parameters from dummy entry in OCDB");
495                                 if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
496                                         Log("Unable to store the dummy CTP timing params object to OCDB!");
497                                         error |= 64;
498                                 }
499                         }
500                 }
501         }
502
503         else if (!partition.IsNull() && detector.IsNull()){ // global partition
504                 Log("GLOBAL partition for current run, using Trigger Timing Parameters from DAQ Logbook");
505                 if (triggerCTPtiming!= NULL) {
506                         Log("Found trigger timing params in DAQ logbook");
507                         AliDebug(2,Form("%s",triggerCTPtiming));
508                         AliCTPTimeParams *runCTPtiming = AliCTPTimeParams::LoadCTPTimeParamsFromString(triggerCTPtiming);         
509                         if (!runCTPtiming) {
510                                 Log("Bad CTP trigger timing params file from DAQ logbook! The corresponding CDB entry will not be filled!");
511                                 error |= 64;
512                         }
513                         else {
514                                 TString titleCTPtiming = Form("CTP timing params for run %i from DAQ",fRun);
515                                 runCTPtiming->SetTitle(titleCTPtiming);
516                                 AliCDBMetaData metadata;
517                                 metadata.SetBeamPeriod(0);
518                                 metadata.SetResponsible("Roman Lietava");
519                                 metadata.SetComment("CTP timing params from DAQ logbook");
520                                 if (!Store("CTP","CTPtiming", runCTPtiming, &metadata, 0, 0)) {
521                                         Log("Unable to store the CTP timing params object to OCDB!");
522                                         error |= 64;
523                                 }
524                         }
525                 }
526
527                 else {
528                         Log("Trigger timing params NULL in DAQ logbook");
529                         error |= 64;
530                 }
531         }
532
533         else {
534                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger timing parameters...",partition.Data(),detector.Data()));
535                 error |= 32;
536         }
537
538         //===========================//
539         // LTU Configuration         //
540         //===========================//
541
542         Log("*************** Processing LTU Configuration");
543         
544         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
545                 Log("STANDALONE partition for current run, using LTU configuration dummy value");
546                 AliCDBEntry *cdbEntry = GetFromOCDB("CTP","DummyLTUConfig");
547                 if (!cdbEntry) {
548                         Log(Form("No dummy LTU Config entry found, going into error..."));
549                         error |= 2048;
550                 }
551                 else{
552                         TObjArray *ltuConfig = (TObjArray*)cdbEntry->GetObject();
553                         if (!ltuConfig){
554                                 Log(Form("dummy LTU Config not found in OCDB entry, going into error..."));
555                                 error |= 2048;
556                         }
557                         else {
558                                 AliCDBMetaData metadata;
559                                 metadata.SetResponsible("Roman Lietava");
560                                 metadata.SetComment("LTU Config from dummy entry in OCDB");
561                                 if (!Store("CTP","LTUConfig", ltuConfig, &metadata, 0, 0)) {
562                                         Log("Unable to store the dummy LTU Config object to OCDB!");
563                                         error |= 2048;
564                                 }
565                         }
566                 }
567         }
568
569         else if (!partition.IsNull() && detector.IsNull()){ // global partition
570         
571                 Log("GLOBAL partition for current run, getting LTU Config from DAQ Logbook (logbook_detectors table)");
572                 UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
573                 Printf ("detectormask = %d",detectorMask);
574                 TObjArray * ltuarray = new TObjArray();
575                 ltuarray->SetOwner(1);
576                 Bool_t isLTUok = kTRUE;
577                 for(Int_t i = 0; i<AliDAQ::kNDetectors-2; i++){
578                         if ((detectorMask >> i) & 0x1) {
579                                 TString det = AliDAQ::OfflineModuleName(i);
580                                 TString detCTPName = AliTriggerInput::fgkCTPDetectorName[i];
581                                 if (detCTPName == "CTP") {
582                                         detCTPName="TRG"; // converting according to what is found in DAQ logbook_detectors                                     
583                                         Printf("Processing CTP (CTP Detector name %s) --> SKIPPING, CTP does not have any LTU!!!!!!",detCTPName.Data());
584                                         continue;
585                                 }                               
586                                 Printf("Processing detector %s (CTP Detector name %s)",det.Data(),detCTPName.Data());
587                                 TString* ltu = GetLTUConfig(detCTPName.Data());
588                                 if (!ltu){
589                                         Log(Form("No LTU Configuration from DAQ logbook for detector %s (BUT it was expected)! The corresponding CDB entry will not be filled!",detCTPName.Data()));
590                                         error |= 2048;
591                                         isLTUok = kFALSE;
592                                         break;
593                                 }
594                                 else{
595                                         Float_t ltuFineDelay1 = ltu[0].Atof();
596                                         Float_t ltuFineDelay2 = ltu[1].Atof();
597                                         Float_t ltuBCDelayAdd = ltu[2].Atof();
598                                         const char* name = AliDAQ::DetectorName(i);
599                                         AliLTUConfig* ltuConfig = new AliLTUConfig((UChar_t)AliDAQ::DetectorID(name),ltuFineDelay1,ltuFineDelay2,ltuBCDelayAdd);
600                                         ltuarray->AddAtAndExpand(ltuConfig,i);
601                                 }                               
602                         }
603                 }
604                 if (isLTUok){
605                         AliCDBMetaData metadata;
606                         metadata.SetBeamPeriod(0);
607                         metadata.SetResponsible("Roman Lietava");
608                         metadata.SetComment("LTU Configuration for current run");
609                         if (!Store("CTP","LTUConfig", ltuarray, &metadata, 0, 0)) {
610                                 Log("Unable to store the LTU Config object to OCDB!");
611                                 error |= 2048;
612                         }               
613                 }
614                 delete ltuarray;
615         }
616
617         else {
618                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error without trigger timing parameters...",partition.Data(),detector.Data()));
619                 error |= 32;
620         }
621
622
623         //=================//
624         // LHC Data        //
625         //=================//
626
627         if (runType == "PHYSICS"){  // processing the LHC file only in PHYSICS runs
628                 Log("*************** Processing LHC Data");
629
630                 UInt_t iLHCData = ProcessLHCData(grpobj);
631                 
632                 if( iLHCData == 0 ) {
633                         Log(Form("LHC Data from FXS, successful!"));
634                 } else  if (iLHCData == 1) {
635                         Log(Form("LHC Data, problems with FXS!"));
636                         error |= 256;
637                 } else  if (iLHCData == 2) {
638                         Log(Form("LHC Data, problems with DAQ_time_start/DAQ_time_end!"));
639                         error |= 512;
640                 } else if (iLHCData ==3){
641                         Log(Form("Problems in storing LHC Phase - going into Error"));
642                         error |= 1024;
643                 } else if (iLHCData ==4){
644                         Log(Form("Problems with LHC Phase - going into Error"));
645                         error |= 1024;
646                 } else{
647                         Log(Form("LHC Data problems"));
648                         error |= 512;
649                 }
650         
651         }
652
653         //==================//
654         // SPD Mean Vertex  //
655         //==================//
656
657         Log("*************** Processing SPD Mean Vertex");
658
659         if (runType == "PHYSICS"){
660                 UInt_t iSPDMeanVertex = ProcessSPDMeanVertex();
661                 if( iSPDMeanVertex == 1 ) {
662                         Log(Form("SPD Mean Vertex, successful!"));
663                 } else {
664                         Log(Form("SPD Mean Vertex failed!!!"));
665                         error |= 128; 
666                 }
667         }
668         else {
669                 Log("SPD Mean Vertex not processed since runType != PHYSICS");
670         }
671
672         //=================//
673         // DQM FXS         //
674         //=================//
675
676         Log("*************** Processing DQM FXS");
677
678         UInt_t iDqmFxs = ProcessDqmFxs();
679         if( iDqmFxs == 1 ) {
680                 Log(Form("DQM FXS, successful!"));
681         } else {
682                 Log(Form("DQM FXS failed!!!"));
683                 error |= 4096;
684         }
685
686         // storing AliGRPObject in OCDB
687
688         AliCDBMetaData md;
689         md.SetResponsible("Chiara Zampolli");
690         md.SetComment("Output parameters from the GRP preprocessor.");
691         
692         Bool_t result = kTRUE;
693         result = Store("GRP", "Data", grpobj, &md); 
694         delete grpobj;
695
696         if (result && !error ) {
697                 Log("GRP Preprocessor Success");
698                 return 0;
699         } else {
700                 Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
701                           kppError[(error&1)?1:0],
702                           kppError[(error&2)?2:0],
703                           kppError[(error&4)?3:0],
704                           kppError[(error&8)?4:0],
705                           kppError[(error&16)?5:0],
706                           kppError[(error&32)?6:0],
707                           kppError[(error&64)?7:0],
708                           kppError[(error&128)?8:0],
709                           kppError[(error&256)?9:0],
710                           kppError[(error&512)?10:0],
711                           kppError[(error&1024)?11:0],
712                           kppError[(error&2048)?12:0],
713                           kppError[(error&4096)?13:0],
714                           kppError[(error&8192)?14:0]
715                           ));
716                 return error;
717         }
718
719
720 }
721
722 //_______________________________________________________________
723
724 UInt_t AliGRPPreprocessor::ProcessLHCData(AliGRPObject *grpobj)
725 {
726         //
727         //Getting the LHC Data from DCS FXS
728         //
729
730         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
731         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
732         if (timeStartString.IsNull() || timeEndString.IsNull()){
733                 if (timeStartString.IsNull()){ 
734                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
735                 }
736                 else if (timeEndString.IsNull()){
737                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
738                 }
739                 return 2;
740         }  
741
742         Double_t timeStart = timeStartString.Atof();
743         Double_t timeEnd = timeEndString.Atof();
744
745         TString fileName = GetFile(kDCS, "LHCData","");
746         if (fileName.Length()>0){
747                 AliInfo("Got The LHC Data file");
748                 AliLHCReader lhcReader;
749
750                 // Processing data to be put in AliGRPObject
751
752                 // Energy
753                 Log("*************Energy ");
754                 TObjArray* energyArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[0]);
755                 if (energyArray){                       
756                         Float_t energy = ProcessEnergy(energyArray,timeStart);
757                         if (energy != -1.) {
758                                 grpobj->SetBeamEnergy(energy);
759                                 grpobj->SetBeamEnergyIsSqrtSHalfGeV(kTRUE);
760                         }
761                         delete energyArray;
762                 }
763                 else {
764                         AliError("Energy not found in LHC Data file!!!");
765                 }       
766
767                 Double_t timeBeamModeEnd = timeEnd;        // max validity for Beam Mode 
768                 Double_t timeMachineModeEnd = timeEnd;     // max validity for Machine Mode
769                 Double_t timeBeamEnd = timeEnd;            // max validity for Beam Type
770                 Double_t timeBeamTypeEnd[2] = {timeEnd, timeEnd}; // max validity for Beam Type1,2
771                 Double_t timeBeamModeStart = -1;    // min validity for Beam Mode
772                 Double_t timeMachineModeStart = -1; // min validity for Machine Mode
773                 Double_t timeBeamStart = -1;        // min validity for Beam Type
774                 Double_t timeBeamTypeStart[2] = {-1,-1};        // min validity for Beam Type1,2
775                 Int_t indexBeamMode = -1;                  // index of measurement used to set Beam Mode
776                 Int_t indexMachineMode = -1;               // index of measurement used to set Machine Mode
777                 Int_t indexBeam = -1;                      // index of measurement used to set Beam Type
778                 Int_t indexBeamType[2] = {-1, -1};                      // index of measurement used to set Beam Type1,2
779                 Bool_t foundBeamModeStart = kFALSE;        // flag to be set in case an entry for the Beam Mode is found before (or at) SOR
780                 Bool_t foundMachineModeStart = kFALSE;     // flag to be set in case an entry for the Machine Mode is found before (or at) SOR
781                 Bool_t foundBeamStart = kFALSE;            // flag to be set in case an entry for the Beam Type is found before (or at) SOR
782                 Bool_t foundBeamTypeStart[2] = {kFALSE, kFALSE};            // flag to be set in case an entry for the Beam Type1,2 is found before (or at) SOR
783                 Bool_t flagBeamMode = kFALSE;  //flag set true if a changed occurred in BeamMode
784                 Bool_t flagMachineMode = kFALSE;  //flag set true if a changed occurred in MachineMode
785                 Bool_t flagBeam = kFALSE;  //flag set true if a changed occurred in BeamType
786                 Bool_t flagBeamType[2] = {kFALSE, kFALSE};  //flag set true if a changed occurred in BeamType1,2
787                 
788                 Double_t arrayTimes[5]={2.E9, 2.E9, 2.E9, 2.E9, 2.E9}; // array to keep track of the times of the possible changes of the LHC DPs; each entry set to Wed May 18 2033, 03:33:20 GMT (ALICE should not be running anymore...)
789                                                                        // arrayTimes elements order correspond to the one used in the array of the strings fgkLHCDataPoints, i.e.:
790                                                                        // arrayTimes[0] --> MachineMode
791                                                                        // arrayTimes[1] --> BeamMode
792                                                                        // arrayTimes[2] --> BeamType (when written together)
793                                                                        // arrayTimes[3] --> BeamType1 (when written separate)
794                                                                        // arrayTimes[4] --> BeamType2 (when written separate)
795
796                 // BeamMode
797                 Log("*************BeamMode (LHCState) ");
798                 TObjArray* beamModeArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[2]);
799                 Int_t nBeamMode = -1;
800                 if (beamModeArray){     
801                         nBeamMode = beamModeArray->GetEntries();        
802                         if (nBeamMode==0){
803                                 AliInfo("Found zero entries for the Beam Mode, leaving it empty");
804                         }
805                         else{
806                                 for (Int_t iBeamMode = 0; iBeamMode<nBeamMode; iBeamMode++){
807                                         AliDCSArray* beamMode = (AliDCSArray*)beamModeArray->At(iBeamMode);
808                                         if (beamMode){
809                                                 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
810                                                         timeBeamModeStart = beamMode->GetTimeStamp();
811                                                         indexBeamMode = iBeamMode;
812                                                         foundBeamModeStart = kTRUE;
813                                                 }
814                                                 else {
815                                                         break;
816
817                                                 }
818                                         }
819                                 }
820                                 if (!foundBeamModeStart){
821                                         AliInfo("No value for the Beam Mode found before start of run, the Beam Mode will remain empty");
822                                 }
823                                 else {
824                                         AliDCSArray* beamMode = (AliDCSArray*)beamModeArray->At(indexBeamMode);
825                                         TObjString* beamModeString = beamMode->GetStringArray(0);
826                                         AliInfo(Form("LHC State (corresponding to BeamMode) = %s (set at %f)",(beamModeString->String()).Data(),beamMode->GetTimeStamp()));
827                                         grpobj->SetLHCState(beamModeString->String());
828                                         if (indexBeamMode < nBeamMode-1){
829                                                 AliDCSArray* beamMode1 = (AliDCSArray*)beamModeArray->At(indexBeamMode+1);
830                                                 if (beamMode1){
831                                                         if (beamMode1->GetTimeStamp()<=timeStart){
832                                                                 AliError("you did not choose the correct value! there is still something before (or at) SOR, but later than this!");
833                                                         }
834                                                         else if (beamMode1->GetTimeStamp()>timeStart && beamMode1->GetTimeStamp()<=timeEnd){
835                                                                 timeBeamModeEnd = beamMode1->GetTimeStamp();
836                                                                 TObjString* beamModeString1 = beamMode1->GetStringArray(0);
837                                                                 TString bmString0 = beamModeString->String();
838                                                                 TString bmString1 = beamModeString1->String();
839                                                                 if (bmString0.CompareTo(bmString1.Data(),TString::kIgnoreCase) == -1){
840                                                                         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(), timeBeamModeEnd, bmString0.Data()));
841                                                                         flagBeamMode = kTRUE;
842                                                                         arrayTimes[1]=timeBeamModeEnd;
843
844                                                                 }
845                                                         }
846                                                 }
847                                                 else {
848                                                         AliInfo("Invalid pointer for the first entry for Beam Mode after the first valid one, not considering anything after what has already been found");
849                                                 }
850                                         }
851                                 }
852                         }
853                         delete beamModeArray;
854                 }
855                 else{
856                         AliError("Beam mode array not found in LHC Data file!!!");
857                 }
858                 
859                 // MachineMode
860                 Log("*************MachineMode ");
861                 TObjArray* machineModeArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[1]);
862                 Int_t nMachineMode = -1;
863                 if (machineModeArray){
864                         nMachineMode = machineModeArray->GetEntries();
865                         if (nMachineMode==0){
866                                 AliInfo("No Machine Mode found, leaving it empty");
867                         }
868                         else{
869                                 for (Int_t iMachineMode = 0; iMachineMode<nMachineMode; iMachineMode++){
870                                         AliDCSArray* machineMode = (AliDCSArray*)machineModeArray->At(iMachineMode);
871                                         if (machineMode){
872                                                 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
873                                                         timeMachineModeStart = machineMode->GetTimeStamp();
874                                                         indexMachineMode = iMachineMode;
875                                                         foundMachineModeStart = kTRUE;
876                                                 }
877                                                 else{
878                                                         break;
879                                                 }
880                                         }
881                                 }
882                                 if (!foundMachineModeStart){
883                                         AliInfo("No value for the Machine Mode found before start of run, the Machine Mode will remain empty");
884                                 }
885                                 else {
886                                         AliDCSArray* machineMode = (AliDCSArray*)machineModeArray->At(indexMachineMode);
887                                         TObjString* machineModeString = machineMode->GetStringArray(0);
888                                         AliInfo(Form("MachineMode = %s (set at %f)",(machineModeString->String()).Data(),machineMode->GetTimeStamp()));
889                                         grpobj->SetMachineMode(machineModeString->String());
890                                         if (indexMachineMode < nMachineMode-1){
891                                                 AliDCSArray* machineMode1 = (AliDCSArray*)machineModeArray->At(indexMachineMode+1);
892                                                 if (machineMode1){
893                                                         if (machineMode1->GetTimeStamp()>timeStart && machineMode1->GetTimeStamp()<=timeEnd){
894                                                                 timeMachineModeEnd = machineMode1->GetTimeStamp();
895                                                                 TObjString* machineModeString1 = machineMode1->GetStringArray(0);
896                                                                 TString mmString0 = machineModeString->String();
897                                                                 TString mmString1 = machineModeString1->String();
898                                                                 if (mmString0.CompareTo(mmString1.Data(),TString::kIgnoreCase) == -1){
899                                                                         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(),timeMachineModeEnd,mmString0.Data()));
900                                                                         flagMachineMode = kTRUE;
901                                                                         arrayTimes[0]=timeMachineModeEnd;
902                                                                 }
903                                                         }
904                                                 }
905                                                 else {
906                                                         AliInfo("Invalid pointer for the first entry for Machine Mode after the first valid one, not considering anything after what has already been found");
907                                                 }
908                                         }
909                                 }
910                         }
911                         delete machineModeArray;
912                 }
913                 else{
914                         AliError("Machine mode array not found in LHC Data file!!!");
915                 }
916                 
917                 // BeamType1 and BeamType2 - both put in the same string
918                 Log("*************BeamType ");
919                 TObjArray* beamArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[3]);
920                 if (beamArray){                 
921                         Int_t nBeam = beamArray->GetEntries();
922                         if (nBeam==0){
923                                 AliInfo("No Beam Type found, leaving it empty");
924                         }
925                         else{
926                                 for (Int_t iBeam = 0; iBeam<nBeam; iBeam++){
927                                         AliDCSArray* beam = (AliDCSArray*)beamArray->At(iBeam);
928                                         if (beam){
929                                                 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
930                                                         timeBeamStart = beam->GetTimeStamp();
931                                                         indexBeam = iBeam;
932                                                         foundBeamStart = kTRUE;
933                                                 }
934                                                 else{
935                                                         break;
936                                                 }
937                                         }
938                                 }
939                                 if (!foundBeamStart){
940                                         AliInfo("No value for the Beam Type found before start of run, the (common) Beam Type will remain empty");
941                                 }
942                                 else {
943                                         AliDCSArray* beam = (AliDCSArray*)beamArray->At(indexBeam);
944                                         TObjString* beamString = beam->GetStringArray(0);
945                                         TString beamType = beamString->String();
946                                         AliInfo(Form("Beam Type = %s",beamType.Data()));        
947                                         if (beamType.CompareTo("PROTON",TString::kIgnoreCase) == 0){
948                                                 AliInfo("Setting beam type to p-p");
949                                                 grpobj->SetBeamType("p-p");
950                                         }
951                                         else { // if there is no PROTON beam, we suppose it is Pb, and we put A-A
952                                                 AliInfo("Setting beam type to A-A");
953                                                 grpobj->SetBeamType("A-A");
954                                         }
955                                         /*
956                                           else if (beamType.CompareTo("LEAD82",TString::kIgnoreCase) == 0){
957                                                 AliInfo("Setting beam type to Pb-Pb");
958                                                 grpobj->SetBeamType("Pb-Pb");
959                                         }
960                                         else{
961                                                 AliError("Beam Type not known, leaving it empty");
962                                         }
963                                         */
964                                         if (indexBeam < nBeam-1){
965                                                 AliDCSArray* beam1 = (AliDCSArray*)beamArray->At(indexBeam+1);
966                                                 if (beam1){
967                                                         if (beam1->GetTimeStamp()>timeStart && beam1->GetTimeStamp()<=timeEnd){
968                                                                 timeBeamEnd = beam1->GetTimeStamp();
969                                                                 TObjString* beamString1 = beam1->GetStringArray(0);
970                                                                 TString beamType1 = beamString1->String();
971                                                                 if (beamType.CompareTo(beamType1.Data(),TString::kIgnoreCase) == -1){
972                                                                         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(),timeBeamEnd,beamType.Data()));
973                                                                         flagBeam = kTRUE;
974                                                                         arrayTimes[2] = timeBeamEnd;
975                                                                 }
976                                                         }
977                                                 }
978                                                 else {
979                                                         AliInfo("Invalid pointer for the first entry for Beam Type after the first valid one, not considering anything after what has already been found");
980                                                 }
981                                         }
982                                 }
983                         }
984                         delete beamArray;
985                 }
986                 else{
987                         AliError("Beam Type array not found in LHC Data file!!!");
988                 }               
989
990                 // BeamType1 and BeamType2 - in separete string
991                 Log("*************BeamType, 1 and 2 ");
992                 Int_t indexBeamTypeString = 6;  // index of the string with the alias of BeanType1 in the array fgkLHCDataPoints
993                 TString combinedBeamType = "-";  // combined beam type, built from beam type 1 and beam type 2
994                 TString combinedBeamTypeFromLHC = "-";  // combined beam type, built from beam type 1 and beam type 2 AS SENT FROM LHC
995                 for (Int_t ibeamType = 0; ibeamType<2; ibeamType++){
996                         beamArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[indexBeamTypeString+ibeamType]);
997                         if (beamArray){                 
998                                 Int_t nBeam = beamArray->GetEntries();
999                                 if (nBeam==0){
1000                                         AliInfo(Form("No Beam Type %s found, leaving it empty",fgkLHCDataPoints[indexBeamTypeString+ibeamType]));
1001                                 }
1002                                 else{
1003                                         for (Int_t iBeam = 0; iBeam<nBeam; iBeam++){
1004                                                 AliDCSArray* beam = (AliDCSArray*)beamArray->At(iBeam);
1005                                                 if (beam){
1006                                                         if (beam->GetTimeStamp()<=timeStart && beam->GetTimeStamp()>=timeBeamTypeStart[ibeamType]){// taking always the very last entry: of two measurements have the same timestamp, the last one is taken
1007                                                                 timeBeamTypeStart[ibeamType] = beam->GetTimeStamp();
1008                                                                 indexBeamType[ibeamType] = iBeam;
1009                                                                 foundBeamTypeStart[ibeamType] = kTRUE;
1010                                                         }
1011                                                         else{
1012                                                                 break;
1013                                                         }
1014                                                 }
1015                                         }
1016                                         if (!foundBeamTypeStart[ibeamType]){
1017                                                 AliInfo(Form("No value for the Beam Type %s found before start of run, the Beam Type %d will remain empty", fgkLHCDataPoints[indexBeamTypeString+ibeamType], ibeamType));
1018                                         }
1019                                         else {
1020                                                 AliDCSArray* beam = (AliDCSArray*)beamArray->At(indexBeam);
1021                                                 TObjString* beamString = beam->GetStringArray(0);
1022                                                 TString beamType = beamString->String();
1023                                                 AliInfo(Form("Beam Type (for %s) = %s", fgkLHCDataPoints[indexBeamTypeString+ibeamType], beamType.Data()));
1024                                                 TString singleBeam = ParseBeamTypeString(beamType,ibeamType);
1025                                                 AliInfo(Form("Single Beam Type for beam %d set to %s", ibeamType, singleBeam.Data()));
1026                                                 grpobj->SetSingleBeamType(ibeamType, singleBeam);
1027                                                 if (beamType.CompareTo("PROTON",TString::kIgnoreCase) == 0){
1028                                                         AliInfo(Form("Setting beam %d for combined beam type to p", ibeamType));
1029                                                         if (ibeamType == 0) combinedBeamType.Prepend("p"); 
1030                                                         else combinedBeamType.Append("p");
1031                                                 }
1032                                                 else { // if there is no PROTON beam, we suppose it is Pb, and we put A-A
1033                                                         AliInfo(Form("Setting beam %d for combined beam type to A",ibeamType));
1034                                                         if (ibeamType == 0) combinedBeamType.Prepend("A");
1035                                                         else combinedBeamType.Append("A");
1036                                                 }
1037                                                 if (ibeamType == 0) combinedBeamTypeFromLHC.Prepend(beamType); 
1038                                                 else combinedBeamTypeFromLHC.Append(beamType);
1039                                                 /*
1040                                                   else if (beamType.CompareTo("LEAD82",TString::kIgnoreCase) == 0){
1041                                                   AliInfo("Setting beam type to Pb-Pb");
1042                                                   grpobj->SetSingleBeamType(ibeamType, "Pb-Pb");
1043                                                   }
1044                                                   else{
1045                                                   AliError("Beam Type not known, leaving it empty");
1046                                                   }
1047                                                 */
1048                                                 if (indexBeamType[ibeamType] < nBeam-1){
1049                                                         AliDCSArray* beam1 = (AliDCSArray*)beamArray->At(indexBeam+1);
1050                                                         if (beam1){
1051                                                                 if (beam1->GetTimeStamp()>timeStart && beam1->GetTimeStamp()<=timeEnd){
1052                                                                         timeBeamTypeEnd[ibeamType] = beam1->GetTimeStamp();
1053                                                                         TObjString* beamString1 = beam1->GetStringArray(0);
1054                                                                         TString beamType1 = beamString1->String();
1055                                                                         if (beamType.CompareTo(beamType1.Data(),TString::kIgnoreCase) == -1){
1056                                                                                 AliWarning(Form("The Beam Type for %s 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",fgkLHCDataPoints[indexBeamTypeString+ibeamType],beamType.Data(),(beamString1->String()).Data(),timeBeamEnd,beamType.Data()));
1057                                                                                 flagBeamType[ibeamType] = kTRUE;
1058                                                                                 arrayTimes[3+ibeamType] = timeBeamTypeEnd[ibeamType];
1059                                                                         }
1060                                                                 }
1061                                                         }
1062                                                         else {
1063                                                                 AliInfo(Form("Invalid pointer for the first entry for Beam Type %s after the first valid one, not considering anything after what has already been found",fgkLHCDataPoints[indexBeamTypeString+ibeamType]));
1064                                                         }
1065                                                 }
1066                                         }
1067                                 }
1068                                 delete beamArray;
1069                         }
1070                         else{
1071                                 AliError(Form("Beam Type %s array not found in LHC Data file!!!",fgkLHCDataPoints[indexBeamTypeString+ibeamType]));
1072                         }               
1073                 }
1074                 AliInfo(Form("Setting combined beam type to %s",combinedBeamType.Data()));
1075                 grpobj->SetBeamType(combinedBeamType);
1076                 AliInfo(Form("Setting combined beam type form LHC to %s",combinedBeamTypeFromLHC.Data()));
1077                 grpobj->SetBeamTypeFromLHC(combinedBeamTypeFromLHC);
1078                 
1079                 // Setting minTimeLHCValidity
1080                 if (flagBeamMode == kTRUE || flagMachineMode == kTRUE || flagBeam == kTRUE || flagBeamType[0] == kTRUE || flagBeamType[1] == kTRUE){ 
1081                         Double_t minTimeLHCValidity= TMath::MinElement(5,arrayTimes);
1082                         AliWarning(Form("Setting MaxTimeLHCValidity to %f",minTimeLHCValidity));
1083                         grpobj->SetMaxTimeLHCValidity(minTimeLHCValidity);
1084                 }
1085                 /* 
1086                    // Old way to determine the Maximum Time during which the LHC info is valid
1087                 if (timeBeamModeEnd!=0 || timeMachineModeEnd!=0 || timeBeamEnd !=0){
1088                         Double_t minTimeLHCValidity;
1089                         if (flagBeamMode == kFALSE && flagMachineMode == kFALSE && flagBeam == kTRUE){ // flagBeam only true --> it is the only one that changed
1090                                 minTimeLHCValidity = timeBeamEnd;
1091                         }
1092                         else if (flagBeamMode == kFALSE && flagMachineMode == kTRUE && flagBeam == kFALSE){ // flagMachineMode only true
1093                                 minTimeLHCValidity = timeMachineModeEnd;
1094                         }
1095                         else if (flagBeamMode == kTRUE && flagMachineMode == kFALSE && flagBeam == kFALSE){ // flagBeamMode only true
1096                                 minTimeLHCValidity = timeBeamModeEnd;
1097                         }
1098                         else if (flagBeamMode == kFALSE && flagMachineMode == kTRUE && flagBeam == kTRUE){ // flagBeam and flagMachineMode only true
1099                                 minTimeLHCValidity= TMath::Min(timeBeamEnd,timeMachineModeEnd);
1100                         }
1101                         else if (flagBeamMode == kTRUE && flagMachineMode == kFALSE && flagBeam == kTRUE){ // flagBeam and flagBeamMode only true
1102                                 minTimeLHCValidity= TMath::Min(timeBeamEnd,timeBeamModeEnd);
1103                         }
1104                         else if (flagBeamMode == kTRUE && flagMachineMode == kTRUE && flagBeam == kFALSE){ // flagMachineMode and flagBeamMode only true
1105                                 minTimeLHCValidity= TMath::Min(timeMachineModeEnd,timeBeamModeEnd);
1106                         }
1107                         else {
1108                                 Double_t arrayTimes[3] = {timeBeamModeEnd,timeMachineModeEnd,timeBeamEnd};// flagMachineMode and flagBeamMode and flagBeam 
1109                                 minTimeLHCValidity= TMath::MinElement(3,arrayTimes);
1110                         }
1111                         AliWarning(Form("Setting MaxTimeLHCValidity to %f",minTimeLHCValidity));
1112                         grpobj->SetMaxTimeLHCValidity(minTimeLHCValidity);
1113                 }
1114                 */
1115                 
1116                 // Data Quality Flag --> storing start and end values of periods within the run during which the value was found to be FALSE
1117                 Log("*************Data Quality Flag ");
1118                 TObjArray* dataQualityArray = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[8]);
1119                 Int_t nDataQuality = -1;
1120                 Double_t timeDataQualityStart = -1; // min validity for Data Quality Flag
1121                 Int_t indexDataQuality = -1;               // index of first measurement used to set Data Quality Flag
1122                 Bool_t foundDataQualityStart = kFALSE;     // flag to be set in case an entry for the Data Quality Flag is found before (or at) SOR
1123
1124                 if (dataQualityArray){
1125                         nDataQuality = dataQualityArray->GetEntries();
1126                         if (nDataQuality==0){
1127                                 AliInfo("No Data Quality Flag found, leaving it empty");
1128                         }
1129                         else{
1130                                 for (Int_t iDataQuality = 0; iDataQuality<nDataQuality; iDataQuality++){
1131                                         AliDCSArray* dataQuality = (AliDCSArray*)dataQualityArray->At(iDataQuality);
1132                                         if (dataQuality){
1133                                                 if (dataQuality->GetTimeStamp()<=timeStart && dataQuality->GetTimeStamp()>=timeDataQualityStart){// taking always the very last entry: if two measurements have the same timestamp, the last one is taken
1134                                                         timeDataQualityStart = dataQuality->GetTimeStamp();
1135                                                         indexDataQuality = iDataQuality;
1136                                                         foundDataQualityStart = kTRUE;
1137                                                 }
1138                                                 else{
1139                                                         // we suppose here that if the first measurement is not before SOR, then none will be (they MUST be in chronological order!!!) 
1140                                                         break;
1141                                                 }
1142                                         }
1143                                 }
1144                                 if (!foundDataQualityStart){
1145                                         // The Data Quality Flag should be found and TRUE at the start of the run. For the time being, if it is not found, don't do anything, but it means there is a problem..
1146                                         AliInfo("No value for the Data Quality Flag found before start of run, the Data Quality Flag will remain empty");
1147                                 }
1148                                 else {
1149                                         // counting how many FALSE values there are
1150                                         Bool_t foundEndOfFalse = kFALSE;
1151                                         Int_t nFalse = 0;
1152                                         for (Int_t iDataQuality = indexDataQuality; iDataQuality < nDataQuality; iDataQuality ++){
1153                                                 AliDCSArray* dataQuality = (AliDCSArray*)dataQualityArray->At(iDataQuality);
1154                                                 AliDebug(4,Form("dataQuality->GetTimeStamp() = %f, timeDataQualityStart = %f, timeEnd = %f", dataQuality->GetTimeStamp(), timeDataQualityStart, timeEnd ));
1155                                                 if (dataQuality->GetTimeStamp()>=timeDataQualityStart && dataQuality->GetTimeStamp()<=timeEnd){ // considering only values between the first valid and the end of the run
1156                                                         Bool_t dataQualityFlag = dataQuality->GetBool(0);
1157                                                         AliDebug(3,Form("DataQuality = %d (set at %f)",(Int_t)dataQualityFlag,dataQuality->GetTimeStamp()));
1158                                                         if (dataQualityFlag != kTRUE){
1159                                                                 if (iDataQuality == indexDataQuality) {  // the first Data Quality value should be TRUE, but ignoring the problem now...
1160                                                                         AliError("The first value for the Data Quality MUST be TRUE! Ignoring for now...");
1161                                                                 }
1162                                                                 nFalse++;
1163                                                         }
1164                                                 }
1165                                         }
1166
1167                                         AliInfo(Form("Found %d FALSE values for the Data Quality Flag",nFalse));
1168                                         Double_t falses[nFalse*2];  // dimensioning this to the maximum possible, as if each false value was followed by a true one --> the false periods correspond to the number of falses
1169
1170                                         Int_t iDataQuality = indexDataQuality;
1171                                         if (nFalse > 0){
1172                                                 Int_t iFalse = 0;
1173                                                 // filling the info about the periods when the flag was set to FALSE
1174                                                 // starting, like for the other DPS, from the measurement closest to SOR (the index of which is iDataQuality)
1175                                                 while (iDataQuality < nDataQuality){
1176                                                         AliDebug(3,Form("iDataQuality = %d",iDataQuality));
1177                                                         AliDCSArray* dataQuality = (AliDCSArray*)dataQualityArray->At(iDataQuality);
1178                                                         if (dataQuality->GetTimeStamp()>=timeDataQualityStart && dataQuality->GetTimeStamp()<=timeEnd){ // considering only values between the first valid and the end of the run
1179                                                                 Bool_t dataQualityFlag = dataQuality->GetBool(0);
1180                                                                 AliDebug(3,Form("DataQuality = %d (set at %f)",(Int_t)dataQualityFlag,dataQuality->GetTimeStamp()));
1181                                                                 if (dataQualityFlag == kTRUE){
1182                                                                         // found TRUE value, continuing
1183                                                                         iDataQuality++;
1184                                                                         continue;
1185                                                                 }
1186                                                                 else{
1187                                                                         /*
1188                                                                         // the check was already done before
1189                                                                         if (iDataQuality == indexDataQuality) {  // the first Data Quality value should be TRUE, but ignoring the problem now...
1190                                                                         AliError("The first value for the Data Quality MUST be TRUE! Ignoring for now...");
1191                                                                         }
1192                                                                         */
1193                                                                         falses[iFalse*2] = dataQuality->GetTimeStamp();
1194                                                                         foundEndOfFalse = kFALSE;
1195                                                                         Int_t iDataQualityNext = iDataQuality+1;
1196                                                                         while (iDataQualityNext < nDataQuality){
1197                                                                                 AliDCSArray* dataQualityNext = (AliDCSArray*)dataQualityArray->At(iDataQualityNext);
1198                                                                                 if (dataQualityNext->GetTimeStamp()>timeDataQualityStart && dataQualityNext->GetTimeStamp()<=timeEnd && dataQualityNext->GetTimeStamp() > dataQuality->GetTimeStamp()){ // considering only values between the first valid and the end of the run, and subsequent to the current value
1199                                                                                         Bool_t dataQualityFlagNext = dataQualityNext->GetBool(0);
1200                                                                                         AliDebug(3,Form("DataQualityNext = %d (set at %f)",(Int_t)dataQualityFlagNext,dataQualityNext->GetTimeStamp()));
1201                                                                                         if (dataQualityFlagNext == kTRUE){
1202                                                                                                 // found TRUE value, first FALSE period completed
1203                                                                                                 foundEndOfFalse = kTRUE;
1204                                                                                                 falses[iFalse*2+1] = dataQualityNext->GetTimeStamp();
1205                                                                                                 iFalse++;
1206                                                                                                 break;
1207                                                                                         }
1208                                                                                         iDataQualityNext++;
1209                                                                                 }
1210                                                                         }
1211                                                                         if (!foundEndOfFalse) {
1212                                                                                 AliInfo("Please, note that the last FALSE value lasted until the end of the run");
1213                                                                                 falses[iFalse*2+1] = timeEnd;
1214                                                                                 iFalse++;
1215                                                                                 break;
1216                                                                         }
1217                                                                         iDataQuality = iDataQualityNext+1;
1218                                                                 }
1219                                                         }
1220                                                 }
1221                                                 grpobj->SetNFalseDataQualityFlag(iFalse);
1222                                                 grpobj->SetFalseDataQualityFlagPeriods(falses);
1223                                         }
1224                                 }
1225                         }
1226                         delete dataQualityArray;
1227                 }
1228                 else{
1229                         AliError("Data Quality Flag array not found in LHC Data file!!!");
1230                 }
1231
1232                 // Processing data to go to AliLHCData object
1233                 AliLHCData* dt = new AliLHCData(fileName.Data(),timeStart,timeEnd);
1234                 // storing AliLHCData in OCDB
1235                 if (dt){
1236                   AliInfo(Form("Filled %d records to AliLHCData object",dt->GetData().GetEntriesFast()));
1237                   AliCDBMetaData md;
1238                   md.SetResponsible("Ruben Shahoyan");
1239                   md.SetComment("LHC data from the GRP preprocessor.");
1240                   Bool_t result = kTRUE;
1241                   result = Store("GRP", "LHCData", dt, &md); 
1242                   delete dt;
1243                   if (!result){
1244                         Log(Form("Problems in storing LHC Data - but not going into Error"));
1245                   }
1246                 }
1247
1248                 // processing LHC Phase
1249
1250                 TObjArray *beam1phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[4]);
1251                 TObjArray *beam2phase = lhcReader.ReadSingleLHCDP(fileName.Data(),fgkLHCDataPoints[5]);
1252                 if (beam1phase == 0x0 || beam2phase == 0x0){
1253                         Log(Form("Problems in retrieving LHC Clock data from LHC file"));
1254                         return 4;
1255                 }                       
1256                 AliLHCClockPhase *phaseObj = ProcessLHCClockPhase(beam1phase,beam2phase,timeEnd);
1257                 delete beam1phase;
1258                 delete beam2phase;
1259                 if (phaseObj){
1260                         AliInfo(Form("LHC Phase found"));
1261                         AliCDBMetaData mdPhase;
1262                         mdPhase.SetResponsible("Cvetan Cheshkov");
1263                         mdPhase.SetComment("LHC Clock Phase");
1264                         Bool_t result = kTRUE;
1265                         result = Store("Calib", "LHCClockPhase", phaseObj, &mdPhase); 
1266                         delete phaseObj;
1267                         if (!result) return 3;
1268                 }
1269                 else return 4;
1270         }
1271         
1272         else {
1273                 AliError("No LHCData file found in FXS");
1274                 return 1;
1275         }
1276
1277         return 0;
1278 }
1279
1280 //_______________________________________________________________
1281
1282 UInt_t AliGRPPreprocessor::ProcessSPDMeanVertex()
1283 {
1284         //
1285         //Getting the SPD Mean Vertex
1286         //
1287
1288         TList* list = GetForeignFileSources("SPD", kDAQ, "VertexDiamond");
1289         Bool_t storeResult = kTRUE;
1290         if (list !=0x0 && list->GetEntries()!=0)
1291                 {
1292                         AliInfo("The following sources produced files with the id VertexDiamond from SPD");
1293                         list->Print();
1294                         for (Int_t jj=0;jj<list->GetEntries();jj++){
1295                                 TObjString * str = dynamic_cast<TObjString*> (list->At(jj)); 
1296                                 if (!str){
1297                                         AliError(Form("Expecting a TObjString in the list for the %d-th source, but something else was found.",jj));
1298                                         continue;
1299                                 }
1300                                 AliInfo(Form("found source %s", str->String().Data()));
1301                                 TString fileNameRun = GetForeignFile("SPD", kDAQ, "VertexDiamond", str->GetName());
1302                                 if (fileNameRun.Length()>0){
1303                                         AliInfo(Form("Got the file %s", fileNameRun.Data()));
1304                                         TFile daqFile(fileNameRun.Data(),"READ");
1305                                         if (daqFile.IsOpen()) {
1306                                                 AliESDVertex* meanVtx = dynamic_cast<AliESDVertex*>(daqFile.Get("MeanVertexPos"));
1307                                                 if (meanVtx){
1308                                                         meanVtx->Print();       
1309                                                         // storing in the OCDB 
1310                                                         AliCDBMetaData md;
1311                                                         md.SetResponsible("Cvetan Cheshkov");
1312                                                         md.SetComment("SPD Mean Vertex");                                       
1313                                                         storeResult = Store("Calib", "MeanVertexSPD", meanVtx, &md, 0, kTRUE); 
1314                                                 }
1315                                                 else{
1316                                                         AliWarning("No SPD Mean Vertex object found in file");
1317                                                 } 
1318                                         }
1319                                         else {
1320                                                 AliError("Can't open file");
1321                                                 storeResult = kFALSE;
1322                                         }
1323                                 }
1324                                 else{
1325                                         AliWarning("No file found for current source for SPD Mean Vertex");
1326                                 }
1327                         }
1328                 }
1329         else {
1330                 AliWarning("No list found for SPD Mean Vertex");
1331         }
1332
1333         if (list) delete list;
1334
1335         return storeResult;
1336 }
1337 //_______________________________________________________________
1338
1339 UInt_t AliGRPPreprocessor::ProcessDqmFxs()
1340 {
1341         //
1342         // Processing DQM fxs information
1343         //
1344
1345         // TriggerClassesAndHistosToClone
1346         TList* list = GetFileSources(kDQM, "TriggerClassesAndHistosToClone");
1347         Bool_t storeResult = kTRUE;
1348         Bool_t storeResultQAThr = kTRUE;
1349         if (list !=0x0 && list->GetEntries()!=0){
1350                 AliInfo("The following sources produced files with the id TriggerClassesAndHistosToClone for GRP");
1351                 list->Print();
1352                 for (Int_t jj=0;jj<list->GetEntries();jj++){
1353                         TObjString * str = dynamic_cast<TObjString*> (list->At(jj)); 
1354                         if (!str){
1355                                 AliError(Form("Expecting a TObjString in the list for the %d-th source, but something else was found.",jj));
1356                                 continue;
1357                         }
1358                         AliInfo(Form("found source %s", str->String().Data()));
1359                         TString fileNameRun = GetFile(kDQM, "TriggerClassesAndHistosToClone", str->GetName());
1360                         if (fileNameRun.Length()>0){
1361                                 AliInfo(Form("Got the file %s", fileNameRun.Data()));
1362                                 TFile dqmFile(fileNameRun.Data(),"READ");
1363                                 if (dqmFile.IsOpen()) {
1364                                         dqmFile.ls();
1365                                 }
1366                                 else {
1367                                         AliError("Can't open file");
1368                                         storeResult = kFALSE;
1369                                 }
1370                         }
1371                         else{
1372                                 AliWarning("No file found for current source for DQM TriggerClassesAndHistosToClone");
1373                         }
1374                 }
1375         }
1376         else {
1377                 AliWarning("No list found for DQM TriggerClassesAndHistosToClone");
1378         }
1379         
1380         if (list) delete list;
1381
1382         // QAThresholds
1383         TObjArray* qaThrArray = new TObjArray();
1384         for (Int_t idet = 0; idet < AliDAQ::kNDetectors; idet++){
1385                 TString detName = AliDAQ::OnlineName(idet);
1386                 if (detName == "TRI" || detName == "HLT" || detName == "TST") continue;   // skipping TRI, HLT, TST since they do not produce QAThresholds
1387                 AliDebug(2, Form("Processing QAThreshold for detector %s",detName.Data())); 
1388                 TList* listQAThr = GetForeignFileSources(detName.Data(), kDQM, "QAThresholds");
1389                 if (listQAThr !=0x0){
1390                         if (listQAThr->GetEntries() > 1){
1391                                 AliError(Form("More than one sources found for QAThresholds from detector %s, skipping",detName.Data()));
1392                                 continue;
1393                         }
1394                         else if (listQAThr->GetEntries()==1){
1395                                 AliInfo(Form("The following source produced files with the id QAThresholds for GRP, coming from detector %s:",detName.Data()));
1396                                 listQAThr->Print();
1397                                 TObjString * str = dynamic_cast<TObjString*> (listQAThr->At(0)); 
1398                                 if (!str){
1399                                         AliError(Form("Expecting a TObjString in the list for detector %s, but something else was found.",detName.Data()));
1400                                         delete listQAThr;
1401                                         continue;
1402                                 }
1403                                 AliInfo(Form("found source %s", str->String().Data()));
1404                                 TString fileNameRun = GetForeignFile(detName.Data(), kDQM, "QAThresholds", str->GetName());
1405                                 if (fileNameRun.Length()>0){
1406                                         AliInfo(Form("Got the file %s", fileNameRun.Data()));
1407                                         TFile dqmFile(fileNameRun.Data(),"READ");
1408                                         if (dqmFile.IsOpen()) {
1409                                                 AliQAThresholds* qaThr = dynamic_cast<AliQAThresholds*>(dqmFile.Get(detName.Data()));
1410                                                 if (qaThr){
1411                                                         Int_t qaThrId = qaThr->GetDetectorId();
1412                                                         if (qaThrId != idet){
1413                                                                 AliError(Form("Expecting QA threshold for detector %s, but found that for detector %s, skipping",detName.Data(), AliDAQ::OnlineName(qaThrId)));
1414                                                                 delete listQAThr;
1415                                                                 continue;
1416                                                         }
1417                                                         else{
1418                                                                 qaThrArray->AddAtAndExpand(qaThr, qaThrId);
1419                                                                 delete listQAThr;
1420                                                         }
1421                                                 }
1422                                                 else {
1423                                                         AliError(Form("No QAThresholds object found in the file for detector %s, skipping",detName.Data()));
1424                                                         delete listQAThr;
1425                                                         continue;
1426                                                 }
1427                                         }                             
1428                                         else {
1429                                                 AliError(Form("Can't open QAThreshold file for detector %s, skipping",detName.Data()));
1430                                                 delete listQAThr;
1431                                                 continue;                                       
1432                                         }
1433                                 }
1434                                 else{
1435                                         AliWarning(Form("No file found for DQM QAThreshold for detector %s, skipping",detName.Data()));
1436                                         delete listQAThr;
1437                                         continue;
1438                                 }
1439                         }
1440                         else {
1441                                 AliError(Form("No sources found for QAThresholds from detector %s, skipping",detName.Data()));
1442                                 delete listQAThr;
1443                                 continue;
1444                         }
1445                 }
1446                 else {
1447                         AliWarning(Form("No list found for DQM QAThreshold for detector %s, skipping",detName.Data()));
1448                         continue;
1449                 }
1450         }
1451         if (qaThrArray->GetEntries() > 0){
1452                 AliCDBMetaData md;
1453                 md.SetResponsible("Barthélémy von Haller");
1454                 md.SetComment("QA Threshold TObjArray");                                        
1455                 storeResultQAThr = Store("Calib", "QAThresholds", qaThrArray, &md, 0, kTRUE); 
1456         }
1457         else{
1458                 Printf("No valid QAThresholds entries found, storing nothing in the OCDB");
1459         }
1460
1461         //      return storeResult;
1462         return kTRUE;  // temporary!!
1463 }
1464
1465
1466 //_______________________________________________________________
1467
1468 Int_t AliGRPPreprocessor::ProcessDaqLB(AliGRPObject* grpObj)
1469 {
1470         //Getting the DAQ lb information
1471         
1472         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
1473         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
1474         Float_t beamEnergy = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof());
1475         TString beamType = (TString)GetRunParameter("beamType");
1476         Char_t numberOfDetectors = (Char_t)(((TString)GetRunParameter("numberOfDetectors")).Atoi());
1477         UInt_t  detectorMask = (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
1478         TString lhcPeriod = (TString)GetRunParameter("LHCperiod");
1479         TString runType = (TString)GetRunType();
1480
1481         if (timeEnd >= 2.E9) AliFatal("ALICE run finshed later than Wed May 18 2033, 03:33:20 GMT, maximum time allowed for LHC data --> fix the GRP preprocessor!!!");
1482
1483         UInt_t nparameter = 0;
1484         if (timeStart != 0){
1485                 grpObj->SetTimeStart(timeStart);
1486                 Log(Form("Start time for run %d: %d",fRun, (Int_t)timeStart));
1487                 nparameter++;
1488         } 
1489         else {
1490                 Log(Form("Start time not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1491         }
1492
1493         if (timeEnd != 0){
1494                 grpObj->SetTimeEnd(timeEnd);
1495                 Log(Form("End time for run %d: %i",fRun, (Int_t)timeEnd));
1496                 nparameter++;
1497         } 
1498         else {
1499                 Log(Form("End time not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1500         }
1501
1502         if (beamEnergy != 0){
1503                 Log(Form("Beam Energy for run %d: %f (NOT USING IT TO FILL THE GRP OBJECT, taking it from the LHC file)",fRun, beamEnergy));
1504         } 
1505         else {
1506                 Log(Form("Beam Energy not put in logbook, but not using it anyway for the GRP object (taking it from the LHC file)"));
1507         }
1508
1509                 
1510         if (beamType.Length() != 0){
1511                 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()));
1512         } 
1513         else {
1514                 Log(Form("Beam Type not put in logbook, but not using it anyway for the GRP entry (taking it from the LHC file)"));
1515         }
1516                 
1517         if (numberOfDetectors != 0){
1518                 grpObj->SetNumberOfDetectors(numberOfDetectors);
1519                 Log(Form("Number Of Detectors for run %d: %d",fRun, (Int_t)numberOfDetectors));
1520                 nparameter++;
1521         } 
1522         else {
1523                 Log(Form("Number Of Detectors not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1524         }
1525
1526         if (detectorMask != 0){
1527                 grpObj->SetDetectorMask(detectorMask);
1528                 Log(Form("Detector Mask for run %d: %d",fRun, detectorMask));
1529                 nparameter++;
1530         } 
1531         else {
1532                 Log(Form("Detector Mask not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1533         }
1534
1535         if (lhcPeriod.Length() != 0) {
1536                 grpObj->SetLHCPeriod(lhcPeriod);
1537                 Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod.Data()));
1538                 nparameter++;
1539         } 
1540         else {
1541                 Log(Form("LHCperiod not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1542         }
1543         if (runType.Length() != 0) {
1544                 grpObj->SetRunType(runType);
1545                 Log(Form("Run Type (DAQ) for run %d: %s",fRun, runType.Data()));
1546                 nparameter++;
1547         } 
1548         else {
1549                 Log(Form("Run Type not put in logbook, setting to invalid in GRP entry, and causing an error!"));
1550         }
1551
1552         return nparameter;
1553 }
1554 //_______________________________________________________________
1555
1556 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
1557 {
1558         //======DAQ FXS======//
1559         
1560         AliRawEventHeaderV3_9::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_9 - temporary fix 
1561         AliRawEventHeaderV3_11::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_11 - temporary fix 
1562         AliRawEventHeaderV3_12::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_12 - temporary fix 
1563         AliRawEventHeaderV3_13::Class()->IgnoreTObjectStreamer(); // to avoid trying reading TObject store in AliRawEventHeaderV3_13 - temporary fix 
1564         Log("Processing DAQ FXS");
1565         TList* list = GetFileSources(kDAQ);     
1566         if (!list) {
1567                 Log("No raw data tag list: connection problems with DAQ FXS logbook!");
1568                 return 1;
1569         }
1570         
1571         if (list->GetEntries() == 0) {
1572                 Log("no raw data tags in this run: nothing to merge!");
1573                 delete  list; list=0;
1574                 return 0;
1575         }
1576         
1577         TChain *fRawTagChain = new TChain("T");
1578         Int_t nFiles=0;
1579         TIterator* iter = list->MakeIterator();
1580         TObject* obj = 0;
1581         while ((obj = iter->Next())) {
1582                 TObjString* objStr = dynamic_cast<TObjString*> (obj);
1583                 if (objStr) {
1584                         Log(Form("Found source %s", objStr->String().Data()));
1585                         TList* list2 = GetFileIDs(kDAQ, objStr->String());
1586                         if (!list2) {
1587                                 Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
1588                                 delete fRawTagChain; fRawTagChain=0;
1589                                 return 1;
1590                         }
1591                         Log(Form("Number of ids: %d",list2->GetEntries()));
1592                         for(Int_t i = 0; i < list2->GetEntries(); i++) {
1593                                 TObjString *idStr = (TObjString *)list2->At(i);
1594                                 if (idStr->String().CompareTo("QAThreshold") == 0 || idStr->String().CompareTo("TriggerClassesAndHistosToClone") == 0) {
1595                                         Log(Form("Skipping file with Id %s",idStr->String().Data()));
1596                                         continue; 
1597                                 }
1598                                 TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
1599                                 if (fileName.Length() > 0) {
1600                                         Log(Form("Adding file in the chain: %s",fileName.Data()));
1601                                         fRawTagChain->Add(fileName.Data());
1602                                         nFiles++;
1603                                 } else {
1604                                         Log(Form("Could not retrieve file with id %s from source %s: "
1605                                                  "connection problems with DAQ FXS!",
1606                                                  idStr->String().Data(), objStr->String().Data()));
1607                                         delete list; list=0;
1608                                         delete list2; list2=0;
1609                                         delete fRawTagChain; fRawTagChain=0;
1610                                         return 2;
1611                                 }
1612                         }
1613                         delete list2;
1614                 }
1615         }
1616
1617         if (nFiles == 0){
1618                 Log("no raw data tags in this run: it could be that one or more files were found in the DAQ FXS, but they were ignored, since not interesting for the raw data tag: nothing to merge!");
1619                 delete iter;
1620                 iter = 0;
1621                 delete list;
1622                 list = 0;
1623                 delete fRawTagChain; 
1624                 fRawTagChain=0;
1625                 return 0;
1626         }
1627         
1628         TString fRawDataFileName = "GRP_Merged.tag.root";
1629         Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
1630         
1631         if (fRawTagChain->Merge(fRawDataFileName) < 1 ) {
1632                 Log(Form("Error merging %d raw data files!!!",nFiles));
1633                 delete iter;
1634                 iter = 0;
1635                 delete list;
1636                 list = 0;
1637                 delete fRawTagChain; 
1638                 fRawTagChain=0;
1639                 return 3;
1640         }
1641                 
1642         TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
1643         Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
1644         
1645         if (!result) {
1646                 Log("Problem storing raw data tags in local file!!!");
1647         } else {
1648                 Log("Raw data tags merged successfully!!");
1649         }
1650         
1651         delete iter;
1652         iter = 0;
1653         delete list;
1654         list = 0;
1655         delete fRawTagChain; fRawTagChain=0;
1656         
1657         if (result == kFALSE) {
1658                 return 4;
1659         }
1660         
1661         return 0;
1662         
1663 }
1664
1665 //_______________________________________________________________
1666 UInt_t AliGRPPreprocessor::ProcessDcsFxs(TString partition, TString detector)
1667 {
1668
1669         // processing the info
1670         // stored in the DCS FXS
1671         // coming from the trigger
1672
1673         // Get the CTP counters information
1674         //              +
1675         // Get the CTP aliases information
1676
1677         if (partition.IsNull() && !detector.IsNull()){ // standalone partition
1678                 Log("STANDALONE partition for current run, using Trigger Scalers and Trigger Aliases dummy values");
1679
1680                 AliCDBEntry *cdbEntryScalers = GetFromOCDB("CTP","DummyScalers");
1681                 if (!cdbEntryScalers) {
1682                         Log(Form("No dummy CTP scalers entry found, going into error..."));
1683                         return 1;
1684                 }
1685                 else{
1686                         AliTriggerRunScalers *scalers = (AliTriggerRunScalers*)cdbEntryScalers->GetObject();
1687                         if (!scalers){
1688                                 Log(Form("CTP dummy scalers not found in OCDB entry, going into error..."));
1689                                 return 1;
1690                         }
1691                         else {
1692                                 AliCDBMetaData metaData;
1693                                 metaData.SetResponsible("Roman Lietava");
1694                                 metaData.SetComment("CTP scalers from dummy entry in OCDB");
1695                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1696                                         Log("Unable to store the dummy CTP scalers object to OCDB!");
1697                                         delete scalers;
1698                                         return 1;
1699                                 }
1700                         }
1701                 }
1702
1703                 AliCDBEntry *cdbEntryAliases = GetFromOCDB("CTP","DummyAliases");
1704                 if (!cdbEntryAliases) {
1705                         Log(Form("No dummy CTP aliases entry found, going into error..."));
1706                         return 2;
1707                 }
1708                 else{
1709                         THashList *aliases = dynamic_cast<THashList*>(cdbEntryAliases->GetObject());
1710                         if (!aliases){
1711                                 Log(Form("CTP dummy aliases not found in OCDB entry, going into error..."));
1712                                 return 2;
1713                         }
1714                         else {
1715                                 AliCDBMetaData metaData;
1716                                 metaData.SetResponsible("Evgeny Kryshen");
1717                                 metaData.SetComment("CTP mapping of trigger classes to trigger aliases");
1718                                 if (!Store("CTP","Aliases", aliases, &metaData, 0, 0)) {
1719                                         Log("Unable to store the dummy CTP aliases object to OCDB!");
1720                                         delete aliases;                                 
1721                                         return 2;
1722                                 }
1723                         }
1724                 }
1725         }
1726
1727         else if (!partition.IsNull() && detector.IsNull()){ // global partition
1728                 Log("GLOBAL partition for current run, using CTP scalers from DCS FXS");
1729                 TString countersfile = GetFile(kDCS, "CTP_xcounters","");
1730                 if (countersfile.IsNull()) {
1731                         Log("No CTP counters files has been found: empty source!");
1732                         return 1;
1733                 }
1734                 else {
1735                         Log(Form("File with Id CTP_xcounters found in DCS FXS! Copied to %s",countersfile.Data()));
1736                         AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
1737                         if (!scalers) {
1738                                 Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
1739                                 return 1;
1740                         }
1741                         else {
1742                                 AliCDBMetaData metaData;
1743                                 metaData.SetBeamPeriod(0);
1744                                 metaData.SetResponsible("Roman Lietava");
1745                                 metaData.SetComment("CTP scalers");
1746                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
1747                                         Log("Unable to store the CTP scalers object to OCDB!");
1748                                         delete scalers;                                 
1749                                         return 1;
1750                                 }
1751                         }
1752                         delete scalers;
1753                 }
1754
1755
1756
1757                 TString aliasesFile = GetFile(kDCS, "CTP_aliases","");
1758                 if (aliasesFile.IsNull()) {
1759                         Log("No CTP aliases files has been found: empty source!");
1760                         return 2;
1761                 }
1762                 else {
1763                         Log(Form("File with Id CTP_aliases found in DCS FXS! Copied to %s",aliasesFile.Data()));
1764                         // We build the THashList of TNamed("triggerclass","comma_separated_list_of_corresponding_aliases")
1765                         THashList* trClasses2Aliases = ProcessAliases(aliasesFile);
1766                         if (!trClasses2Aliases) {
1767                                 Log("Bad CTP aliases file! The corresponding CDB entry will not be filled!");
1768                                 return 2;
1769                         }
1770                         else {
1771                                 AliCDBMetaData metaData;
1772                                 metaData.SetBeamPeriod(0);
1773                                 metaData.SetResponsible("Evgeny Kryshen");
1774                                 metaData.SetComment("CTP mapping of trigger classes to trigger aliases");
1775                                 if (!Store("CTP","Aliases", trClasses2Aliases, &metaData, 0, 0)) {
1776                                         Log("Unable to store the CTP aliases object to OCDB!");
1777                                         delete trClasses2Aliases;                                       
1778                                         return 2;
1779                                 }
1780                         }
1781                         delete trClasses2Aliases;
1782                 }
1783         }
1784         
1785
1786         else{   
1787                 Log(Form("Incorrect field in DAQ logbook for partition = %s and detector = %s, going into error...",partition.Data(),detector.Data()));
1788                 return 3;
1789         }
1790
1791         return 0;
1792
1793 }
1794
1795 //_______________________________________________________________
1796 THashList* AliGRPPreprocessor::ProcessAliases(const char* aliasesFile)
1797 {
1798
1799         //
1800         // build the THashList of triggerclasses-to-triggeraliases from text file  
1801         // each line of the file is supposed to be a string composed by
1802         // triggerclass+spaces+commaseparatedlistofcorrespondingaliases\n
1803         // it will add a TNamed("triggerclass","commaseparatedlistofcorrespondingaliases")
1804         // to the hashlist
1805         //
1806
1807         if (gSystem->AccessPathName(aliasesFile)) {
1808                 Printf("file (%s) not found", aliasesFile);
1809                 return 0;
1810         }
1811
1812         ifstream *file = new ifstream(aliasesFile);
1813         if (!*file) {
1814                 Printf("Error opening file (%s) !",aliasesFile);
1815                 file->close();
1816                 delete file;
1817                 return 0;
1818         }
1819
1820         THashList *hList = new THashList(10);
1821         hList->SetName("List of trigger classes to trigger aliases strings");
1822
1823         TString strLine;
1824         while (strLine.ReadLine(*file)) {
1825
1826             // safety for null lines, tabs instead of whitespaces, trailing carriage return, leading or trailing spaces/tabs
1827                 if (strLine.IsNull()) continue;
1828                 strLine.ReplaceAll('\t',' ');
1829                 strLine.Remove(TString::kLeading,' ');
1830                 strLine.Remove(TString::kTrailing,'\r');
1831                 strLine.Remove(TString::kTrailing,' ');
1832
1833                 TObjArray* arr = strLine.Tokenize(' ');
1834                 if(arr->GetEntries() != 2){
1835                         Printf("The line:\n%s\nunexpectedly contains %d tokens, instead of two.",strLine.Data(),arr->GetEntries());
1836                         return 0;
1837                 }
1838                 TObjString *osTC = (TObjString*) arr->At(0);
1839                 TObjString *osTAlist = (TObjString*) arr->At(1);
1840                 TNamed *ctoa = new TNamed(osTC->GetName(),osTAlist->GetName());
1841                 hList->Add(ctoa);
1842         }
1843
1844         file->close();
1845         delete file;
1846
1847         return hList;
1848 }
1849
1850 //_______________________________________________________________
1851 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
1852 {
1853
1854         //
1855         // processing DCS DPs
1856         //
1857
1858         Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
1859         Int_t nL3Entries = 0;
1860         Int_t nDipoleEntries = 0;
1861         Int_t nEnvEntries = 0;
1862         Int_t nHallProbesEntries = 0;
1863         nL3Entries = ProcessL3DPs(valueMap, grpObj);
1864         nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
1865         nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
1866         nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
1867         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
1868         Log(Form("L3Entries = %d, nDipoleEntries =%d, nEnvEntries = %d, nHallProbesEntries = %d", nL3Entries, nDipoleEntries, nEnvEntries, nHallProbesEntries));
1869         entries = nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
1870         return entries;
1871
1872 }
1873
1874 //_______________________________________________________________
1875
1876 Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpObj)
1877 {
1878
1879         // processing DPs
1880         // related to 
1881         // L3 info
1882
1883         Int_t nL3Entries = 0;
1884
1885         TObjArray *array = 0x0;
1886         Int_t indexDP = -1;
1887         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1888
1889         AliInfo(Form("==========L3Current==========="));
1890         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1891         indexDP = kL3Current;
1892         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1893         if(!array) {
1894                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1895         } 
1896         else {
1897                 if (array->GetEntries() == 0){
1898                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1899                 }
1900                 else {
1901                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1902                         if (floatDCS != NULL){
1903                                 grpObj->SetL3Current(floatDCS);
1904                         }
1905                         else{
1906                                 outOfRange = kTRUE;
1907                         }       
1908                         if (floatDCS){
1909                                 delete[] floatDCS;
1910                                 floatDCS = 0x0;
1911                         }
1912                 }
1913                 if (!outOfRange) {
1914                         nL3Entries++;
1915                         ffailedDPs->RemoveAt(indexDP);
1916                 }
1917         }
1918
1919         if (array) array = 0x0;
1920
1921         AliInfo(Form("==========L3Polarity==========="));
1922         indexDP = kL3Polarity;
1923         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1924         if(!array) {
1925                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1926         } 
1927         else {
1928                 if (array->GetEntries() == 0){
1929                         AliError(Form("No entries found in array! setting %s Polarity to invalid...",fgkDCSDataPoints[indexDP]));
1930                 }
1931                 else {
1932                         Bool_t change = kFALSE;
1933                         Char_t charDCS = ProcessBool(array,change);
1934                         if (change == kFALSE){
1935                                 grpObj->SetL3Polarity(charDCS);
1936                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
1937                                 ffailedDPs->RemoveAt(indexDP);
1938                                 nL3Entries++;
1939                         }
1940                         else if (isZero){
1941                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
1942                                 ffailedDPs->RemoveAt(indexDP);
1943                                 nL3Entries++;
1944                         }
1945                         else {
1946                                 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]));
1947                         }
1948                 }
1949         }
1950
1951         return nL3Entries;
1952
1953 }
1954 //_______________________________________________________________
1955
1956 Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* grpObj)
1957 {
1958         // processing DPs
1959         // related to 
1960         // the Dipole info
1961
1962         Int_t nDipoleEntries = 0;
1963         TObjArray *array = 0x0;
1964         Int_t indexDP = -1;
1965         Bool_t isZero = kTRUE; // flag to monitor L3Current. If set to true, the magnet is OFF, and the polarity can change
1966
1967         AliInfo(Form("==========DipoleCurrent==========="));
1968         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
1969         indexDP = kDipoleCurrent;
1970         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
1971         if(!array) {
1972                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
1973         } 
1974         else {
1975                 if (array->GetEntries() == 0){
1976                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
1977                 }
1978                 else {
1979                         Float_t *floatDCS = ProcessFloatAllMagnet(array, indexDP, isZero);
1980                         if (floatDCS != NULL){
1981                                 grpObj->SetDipoleCurrent(floatDCS);
1982                         } 
1983                         else{
1984                                 outOfRange=kTRUE;
1985                         }
1986                         if (floatDCS){
1987                                 delete[] floatDCS;
1988                                 floatDCS = 0x0;
1989                         }
1990                 }
1991                 if (!outOfRange) {
1992                         nDipoleEntries++;
1993                         ffailedDPs->RemoveAt(indexDP);
1994                 }
1995         }
1996
1997         if (array) array = 0x0;
1998
1999         AliInfo(Form("==========DipolePolarity==========="));
2000         indexDP = kDipolePolarity;
2001         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
2002         if(!array) {
2003                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
2004         } 
2005         else {
2006                 if (array->GetEntries() == 0){
2007                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
2008                 }
2009                 else {
2010                         Bool_t change = kFALSE;
2011                         Char_t charDCS = ProcessBool(array,change);
2012                         if (!change){
2013                                 grpObj->SetDipolePolarity(charDCS);
2014                                 AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
2015                                 ffailedDPs->RemoveAt(indexDP);
2016                                 nDipoleEntries++;
2017                         }
2018                         else if (isZero){
2019                                 AliInfo(Form("%s set to invalid, but magnet was OFF (according to the current), DP not considered wrong",fgkDCSDataPoints[indexDP]));
2020                                 ffailedDPs->RemoveAt(indexDP);
2021                                 nDipoleEntries++;
2022                         }
2023                         else{
2024                                 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]));
2025                         }
2026                 }
2027         }
2028
2029         return nDipoleEntries;
2030
2031 }
2032 //_______________________________________________________________
2033
2034 Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
2035 {
2036         // processing DPs
2037         // related to 
2038         // evironment conditions (temperature, pressure) info
2039
2040         Int_t nEnvEntries = 0;
2041         TObjArray *array = 0x0;
2042         Int_t indexDP = -1;
2043
2044         AliInfo(Form("==========CavernTemperature==========="));
2045         Bool_t outOfRange = kFALSE;  // flag to monitor if any value collected by DCS is out of range
2046         indexDP = kCavernTemperature;
2047         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
2048         if(!array) {
2049                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
2050         } 
2051         else {
2052                 if (array->GetEntries() == 0){
2053                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
2054                 }
2055                 else {
2056                         Float_t *floatDCS = ProcessFloatAll(array);
2057                         if (floatDCS != NULL){
2058                                 grpObj->SetCavernTemperature(floatDCS);
2059                         }
2060                         else{
2061                                 outOfRange = kTRUE;
2062                         }
2063                         if (floatDCS){
2064                                 delete[] floatDCS;
2065                                 floatDCS = 0x0;
2066                         }
2067                 }
2068                 if (!outOfRange) {
2069                         ffailedDPs->RemoveAt(indexDP);
2070                         nEnvEntries++;
2071                 }
2072         }
2073
2074         if (array) array = 0x0;
2075
2076         AliInfo(Form("========== AtmosPressures (Cavern + Surface + Cavern2) ==========="));
2077         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
2078         //dcsSensorArray->Print();
2079         if( fPressure->NumFits()<kNumSensors ) {
2080                 Log(Form("Check the pressure sensor values! Not all the %d pressure sensors have been fit",kNumSensors));
2081         } 
2082         Log(Form("Number of fits performed = %d",fPressure->NumFits()));
2083
2084         AliInfo(Form("==========CavernAtmosPressure==========="));
2085         indexDP = kCavernAtmosPressure;
2086         AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
2087         TGraph* graph = sensorCavernP2->GetGraph();
2088         AliDebug(3,Form("index = %d",indexDP));
2089         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
2090         AliDebug(2,Form("graph = %p",graph));
2091         AliDebug(3,Form("sensorCavernP2 = %p", sensorCavernP2));
2092         if(sensorCavernP2->GetFit() || graph) {
2093                 if (sensorCavernP2->GetFit()){
2094                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
2095                 }
2096                 else {
2097                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
2098                 }
2099                 grpObj->SetCavernAtmosPressure(sensorCavernP2);
2100                 ffailedDPs->RemoveAt(indexDP);
2101                 nEnvEntries++;
2102         } 
2103         //if (sensorP2) delete sensorP2;
2104         else {
2105                 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] ));
2106         }
2107         
2108         AliInfo(Form("==========SurfaceAtmosPressure==========="));
2109         indexDP = kSurfaceAtmosPressure;
2110         AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
2111         graph = sensorP2->GetGraph();
2112         AliDebug(3,Form("index = %d",indexDP));
2113         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
2114         AliDebug(2,Form("graph = %p",graph));   
2115         AliDebug(3,Form("sensorP2 = %p", sensorP2));
2116         if(sensorP2->GetFit() || graph) {
2117                 if (sensorP2->GetFit()){
2118                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
2119                 }
2120                 else {
2121                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
2122                 }
2123                 grpObj->SetSurfaceAtmosPressure(sensorP2);
2124                 ffailedDPs->RemoveAt(indexDP);
2125                 nEnvEntries++;
2126         } 
2127         //if (sensorP2) delete sensorP2;
2128         else {
2129                 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] ));
2130         }
2131
2132         AliInfo(Form("==========CavernAtmosPressure2==========="));
2133         indexDP = kCavernAtmosPressure2;
2134         AliDCSSensor* sensorCavernP22 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
2135         graph = sensorCavernP22->GetGraph();
2136         AliDebug(3,Form("index = %d",indexDP));
2137         AliDebug(3,Form("name = %s",fgkDCSDataPoints[indexDP]));
2138         AliDebug(2,Form("graph = %p",graph));   
2139         AliDebug(3,Form("sensorCavernP2_2 = %p", sensorCavernP22));
2140         if(sensorCavernP22->GetFit() || graph) {
2141                 if (sensorCavernP22->GetFit()){
2142                         Log(Form("Fit for sensor %s found",fgkDCSDataPoints[indexDP]));
2143                 }
2144                 else {
2145                         Log(Form("Fit for sensor %s not found, but the graph is there - NOT going into error",fgkDCSDataPoints[indexDP]));
2146                 }
2147                 grpObj->SetCavernAtmosPressure2(sensorCavernP22);
2148                 ffailedDPs->RemoveAt(indexDP);
2149                 nEnvEntries++;
2150         } 
2151         //if (sensorP2) delete sensorP2;
2152         else {
2153                 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] ));
2154         }
2155         
2156         
2157         return nEnvEntries;
2158 }
2159 //_______________________________________________________________
2160
2161 Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpObj)
2162 {
2163         // processing DPs
2164         // related to 
2165         // Hall Probes info
2166
2167         Int_t nHPEntries = 0;
2168         TObjArray *array = 0x0;
2169         Int_t indexDP = -1;
2170         Bool_t outOfRange; // flag to monitor if any value collected by DCS is out of range
2171
2172         if (fgknDCSDPHallProbes != AliGRPObject::GetNumberOfHP()){
2173                 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()));
2174         }
2175         for (indexDP = 0; indexDP < AliGRPObject::GetNumberOfHP(); indexDP++){
2176                 outOfRange = kFALSE; // resetting outOfRange flag at each HP
2177                 AliInfo(Form("==========%s===========",AliGRPObject::GetHPDP(indexDP)));
2178                 array = (TObjArray *)valueMap->GetValue(AliGRPObject::GetHPDP(indexDP));
2179                 if(!array) {
2180                         Log(Form("%s not found in the map!!!",AliGRPObject::GetHPDP(indexDP)));
2181                 } 
2182                 else {
2183                         if (array->GetEntries() == 0){
2184                                 AliError(Form("No entries found in array! setting %s to invalid...",AliGRPObject::GetHPDP(indexDP)));
2185                         }
2186                         else {
2187                                 Float_t *floatDCS = ProcessFloatAll(array);
2188                                 if (floatDCS != NULL){
2189                                         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])); 
2190                                         grpObj->SetHallProbes((AliGRPObject::DP_HallProbes)indexDP,floatDCS);
2191                                         for (Int_t kk = 0 ; kk< 5; kk++){
2192                                                 AliDebug(2,Form("HallProbe[%d][%d]=%f",indexDP,kk,grpObj->GetHallProbes((AliGRPObject::DP_HallProbes)indexDP,(AliGRPObject::Stats)kk)));
2193                                         }
2194                                 }
2195                                 else{
2196                                         outOfRange = kTRUE;
2197                                 }
2198                                 if (floatDCS){
2199                                         delete[] floatDCS;
2200                                         floatDCS = 0x0;
2201                                 }
2202                         }
2203                         if (!outOfRange) {
2204                                 ffailedDPs->RemoveAt(indexDP + fgkDCSDPHallTopShift);  // 7 = shift in the complete list of DPs to get to the Hall Probes
2205                                 nHPEntries++;
2206                         }
2207                 }
2208         }
2209                 
2210         Log(Form("Hall Probes = %d ", nHPEntries));
2211         return nHPEntries;
2212 }
2213
2214 //_________________________________________________________________________
2215
2216 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
2217
2218
2219         // 
2220         // returning Spline Fit 
2221         // 
2222
2223         Int_t entriesarray = array->GetEntries();
2224         Float_t* value = new Float_t[entriesarray];
2225         Float_t* time = new Float_t[entriesarray];
2226         AliDCSValue* v = 0x0;
2227         for (Int_t iarray = 0; iarray < entriesarray; iarray++){
2228                 v = (AliDCSValue*)array->At(iarray);
2229                 value[iarray] = v->GetFloat();
2230                 time[iarray] = v->GetTimeStamp();
2231                 AliDebug(2,Form("iarray = %d, value = %f, time = %f",iarray,value[iarray],time[iarray]));
2232         }
2233         TGraph* gr = new TGraph(entriesarray,value,time);
2234         if (!gr ) {
2235                 AliWarning(Form("%s: no input graph to compute SplineFit",stringID.Data()));
2236                 return NULL;
2237         }
2238         AliSplineFit *fit = new AliSplineFit();
2239         fit->SetMinPoints(10);
2240         fit->InitKnots(gr,10,10,0.0);
2241         fit->SplineFit(2);
2242         fit->Cleanup();
2243         return fit;
2244 }
2245
2246 //_________________________________________________________________________
2247
2248 TString AliGRPPreprocessor::ProcessChar(const TObjArray *array)
2249 {
2250
2251         // 
2252         // processing char
2253         //
2254
2255         TString aDCSString="";
2256         
2257         AliDCSValue *v = 0x0;
2258         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
2259                 v = (AliDCSValue *)array->At(iCount);
2260                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
2261                         AliError(Form("DCS values for the parameter outside the queried interval"));
2262                         continue;
2263                 }
2264                 if (iCount > 0) {
2265                         if (aDCSString != v->GetChar())
2266                         AliError(Form("DCS values for the parameter changed from %s to %c within the queried interval", aDCSString.Data(), (Char_t)v->GetChar()));
2267                 }
2268                 aDCSString = (TString)v->GetChar();  // keeping always last value in the array
2269         }
2270         return aDCSString;
2271 }
2272
2273 //__________________________________________________________________________________________________________________
2274
2275 Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
2276 {
2277         // 
2278         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
2279         //
2280         // parameters[0] = mean
2281         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
2282         // parameters[2] = median
2283         // parameters[3] = standard deviation wrt mean
2284         // parameters[4] = standard deviation wrt median
2285         //
2286
2287         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2288         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2289         if (timeStartString.IsNull() || timeStartString.IsNull()){
2290                 if (timeStartString.IsNull()){ 
2291                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2292                 }
2293                 else if (timeStartString.IsNull()){
2294                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2295                 }
2296                 fdaqStartEndTimeOk = kFALSE;
2297                 return 0;
2298         }  
2299
2300         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2301         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2302         Float_t* parameters = new Float_t[5];
2303         Int_t iCounts = 0;
2304         Int_t iCountsRun = 0;
2305         Int_t nCounts = array->GetEntries();
2306         Float_t valueBeforeSOR = 0;
2307         Float_t valueAfterEOR = 0;
2308         Int_t timestampBeforeSOR = -1;
2309         Int_t timestampAfterEOR = -1;
2310         Int_t ientrySOR = -1;
2311         Int_t ientryEOR = -1;
2312         Float_t* arrayValues = 0x0; 
2313         Double_t* arrayWeights = 0x0; 
2314         Bool_t truncMeanFlag = kTRUE;  // flag to indicate whether Truncated Mean should be calculated or not
2315         Bool_t sdFlag = kTRUE;  // flag to indicate whether SD (wrt Mean/Median) should be calculated or not
2316
2317         for(Int_t i = 0; i < nCounts; i++) {
2318                 AliDCSValue *v = (AliDCSValue *)array->At(i);
2319                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
2320                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
2321                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
2322                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
2323                         delete [] parameters;
2324                         return NULL;
2325                 }
2326                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2327                         AliDebug(2,Form("%d-th entry = %f at timestamp %i",i,v->GetFloat(),v->GetTimeStamp()));
2328                         iCounts += 1;
2329                         // look for the last value before SOR and the first value before EOR
2330                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2331                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2332                                 AliDebug(2,Form("timestamp of last value before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2333                                 valueBeforeSOR = v->GetFloat();
2334                         }
2335                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2336                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2337                                 valueAfterEOR = v->GetFloat();
2338                                 AliDebug(2,Form("timestamp of first value after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2339                         }
2340                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2341                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2342                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2343                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2344                                 AliDebug(2,Form("entry between SOR and EOR"));
2345                                 iCountsRun += 1;
2346                         }
2347                 }
2348                 else {
2349                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2350                 }
2351         }
2352
2353         if (timestampBeforeSOR == -1){
2354                 AliWarning("No value found before SOR");
2355         }
2356         if (timestampAfterEOR == -1){
2357                 AliWarning("No value found after EOR");
2358         }
2359
2360         AliDebug(2,Form("Number of valid entries (within DCS query interval) = %i, from a total amount of %i entries",iCounts,nCounts));
2361         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2362         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2363         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2364         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2365         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2366
2367         Int_t nentriesUsed = 0;
2368         if (iCountsRun > 1){
2369                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2370                 AliDebug(2,"Calculating (weighted) Mean and Median");
2371                 arrayValues = new Float_t[iCountsRun]; 
2372                 arrayWeights = new Double_t[iCountsRun]; 
2373                 nentriesUsed = iCountsRun;
2374                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2375                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2376                         Int_t timestamp2 = 0;
2377                         if (i < ientryEOR){
2378                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2379                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2380                         }
2381                         else {
2382                                 timestamp2 = timeEnd+1;
2383                         }
2384                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2385                         arrayValues[i-ientrySOR] = v->GetFloat();
2386                 }
2387                 parameters[0] = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2388                 parameters[2] = TMath::Median(iCountsRun,arrayValues,arrayWeights);
2389         }
2390         else if (iCountsRun == 1){
2391                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2392                 nentriesUsed = 2;
2393                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2394                         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.");
2395                         arrayValues = new Float_t[2];
2396                         arrayWeights = new Double_t[2];
2397                         arrayValues[0] = valueBeforeSOR;
2398                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2399                         arrayValues[1] = v->GetFloat();
2400                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2401                         AliDebug(2, Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2402                         AliDebug(2, Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2403                         parameters[0] = TMath::Mean(2,arrayValues,arrayWeights);
2404                         parameters[2] = TMath::Median(2,arrayValues,arrayWeights);
2405                         truncMeanFlag = kFALSE;
2406                 }
2407                 else{
2408                         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");
2409                         parameters[0] = AliGRPObject::GetInvalidFloat();
2410                         parameters[1] = AliGRPObject::GetInvalidFloat();
2411                         parameters[2] = AliGRPObject::GetInvalidFloat();
2412                         parameters[3] = AliGRPObject::GetInvalidFloat();
2413                         parameters[4] = AliGRPObject::GetInvalidFloat();
2414                         return parameters;
2415                 }
2416         }
2417         else { // iCountsRun == 0, using only the point immediately before SOR
2418                 if (timestampBeforeSOR == -1){
2419                         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");
2420                         parameters[0] = AliGRPObject::GetInvalidFloat();
2421                         parameters[1] = AliGRPObject::GetInvalidFloat();
2422                         parameters[2] = AliGRPObject::GetInvalidFloat();
2423                         parameters[3] = AliGRPObject::GetInvalidFloat();
2424                         parameters[4] = AliGRPObject::GetInvalidFloat();
2425                         return parameters;
2426                 }
2427                 else {
2428                         AliWarning("Using only last entry before SOR. Truncated mean and Standard deviations (wrt mean/median) won't be calculated.");
2429                         AliDebug(2,Form("value = %f",valueBeforeSOR)); 
2430                         parameters[0] = valueBeforeSOR;
2431                         parameters[2] = valueBeforeSOR;
2432                         truncMeanFlag = kFALSE;
2433                         sdFlag = kFALSE;
2434                 }
2435         }
2436
2437         Float_t temp = 0;
2438         Float_t temp1 = 0;
2439         Float_t sumweights = 0; 
2440         Int_t entriesTruncMean = 0;
2441         Float_t* arrayValuesTruncMean = new Float_t[nentriesUsed]; 
2442         Double_t* arrayWeightsTruncMean = new Double_t[nentriesUsed]; 
2443
2444         // calculating SD wrt Mean and Median
2445         AliDebug(2,"Calculating SD wrt Mean and SD wrt Median");
2446         if (sdFlag){
2447                 for (Int_t i =0; i< nentriesUsed; i++){
2448                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
2449                         temp += (arrayValues[i]-parameters[2])*(arrayValues[i]-parameters[2]);
2450                         temp1 += arrayWeights[i]*(arrayValues[i]-parameters[0])*(arrayValues[i]-parameters[0]);
2451                         sumweights += arrayWeights[i];
2452                 }
2453                 // setting SD wrt Mean 
2454                 if (sumweights != 0 ){
2455                         parameters[3] = TMath::Sqrt(temp1/sumweights);
2456                 }
2457                 else {
2458                         AliError("Sum of weights to calculate Standard Deviation (wrt mean) <= 0, setting the SD to invalid");
2459                         parameters[3] = AliGRPObject::GetInvalidFloat();
2460                 }
2461                 // setting SD wrt Median
2462                 if (nentriesUsed != 0){
2463                         parameters[4] = TMath::Sqrt(temp/nentriesUsed);
2464                 }
2465                 else{
2466                         AliError("Number of entries used to calculate Standard Deviation (wrt median) <= 0, setting the SD to invalid");
2467                         parameters[4] = AliGRPObject::GetInvalidFloat();
2468                 }
2469         }
2470         else {
2471                 parameters[3] = AliGRPObject::GetInvalidFloat();
2472                 parameters[4] = AliGRPObject::GetInvalidFloat();
2473         }               
2474
2475         // calculating truncated mean (this comes afterwards since you need the SD wrt Mean)
2476         if (truncMeanFlag){
2477                 AliDebug(2,"Calculating Truncated Mean");
2478                 for (Int_t i =0; i< nentriesUsed; i++){
2479                         AliDebug(2,Form("Entry %d: value = %f, weight = %f",i,arrayValues[i],arrayWeights[i]));
2480                         if ((arrayValues[i]<=parameters[0]+3*parameters[3]) && (arrayValues[i]>=parameters[0]-3*parameters[3])){
2481                                 arrayValuesTruncMean[entriesTruncMean]=arrayValues[i];
2482                                 arrayWeightsTruncMean[entriesTruncMean]=arrayWeights[i];
2483                                 AliDebug(2,Form("For Truncated Mean: Entry %d: value = %f, weight = %f",entriesTruncMean,arrayValuesTruncMean[entriesTruncMean],arrayWeightsTruncMean[entriesTruncMean]));
2484                                 entriesTruncMean++;                     
2485                         }
2486                         else{
2487                                 AliDebug(2,"Discarding entry");
2488                         }
2489                 }
2490                 // setting truncated mean 
2491                 if (entriesTruncMean >1){
2492                         AliDebug(2,Form("%d entries used for truncated mean",entriesTruncMean));
2493                         parameters[1] = TMath::Mean(entriesTruncMean,arrayValuesTruncMean,arrayWeightsTruncMean);
2494                 }
2495                 else{   
2496                         AliDebug(2,Form("Too few entries (%d) to calculate truncated mean",entriesTruncMean));
2497                         parameters[1] = AliGRPObject::GetInvalidFloat();
2498                 }
2499         }
2500         else{
2501                         parameters[1] = AliGRPObject::GetInvalidFloat();
2502         }
2503
2504         if (arrayValues){
2505                 delete [] arrayValues;
2506         } 
2507         if (arrayWeights){
2508                 delete [] arrayWeights;
2509         } 
2510         delete [] arrayValuesTruncMean;
2511         delete [] arrayWeightsTruncMean;
2512
2513         AliInfo(Form("(weighted) mean = %f ",parameters[0]));
2514         AliInfo(Form("(weighted) truncated mean = %f ",parameters[1]));
2515         AliInfo(Form("median = %f ",parameters[2]));
2516         AliInfo(Form("(weighted) standard deviation with (weighted) mean = %f ",parameters[3]));
2517         AliInfo(Form("standard deviation with median = %f ",parameters[4]));
2518         
2519         return parameters;
2520 }
2521
2522 //__________________________________________________________________________________________________________________
2523
2524 Float_t* AliGRPPreprocessor::ProcessFloatAllMagnet(const TObjArray* array, Int_t indexDP, Bool_t &isZero)
2525 {
2526         // 
2527         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
2528         // used for L3 and Dipole magnets, using isZero flag to decide whther the magnet was OFF/ON
2529         // the flag is set according to the L3/Dipole current value
2530         // current threshold for L3 = 350 A (value provided by DCS)
2531         // current threshold for Dipole = 450 A (value provided by DCS)
2532         //
2533         // parameters[0] = mean
2534         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
2535         // parameters[2] = median
2536         // parameters[3] = standard deviation wrt mean
2537         // parameters[4] = standard deviation wrt median
2538         //
2539
2540         AliInfo(Form("indexDP = %d",indexDP)); 
2541
2542         Int_t nCounts = array->GetEntries();
2543         for(Int_t i = 0; i < nCounts; i++) {
2544                 AliDCSValue *v = (AliDCSValue *)array->At(i);
2545                 if ((v->GetFloat() <= fminFloat) || (v->GetFloat() >= fmaxFloat)) {
2546                         AliError(Form("Error! Float value found in DCS map at %d-th entry is OUT OF RANGE: value = %6.5e",i,v->GetFloat()));
2547                         if (v->GetFloat() < fminFloat) AliInfo(Form("The value is smaller than %6.5e",fminFloat));
2548                         if (v->GetFloat() > fmaxFloat) AliInfo(Form("The value is greater than %6.5e",fmaxFloat));
2549                         return NULL;
2550                 }
2551                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2552                         AliDebug(2,Form("%d-th entry = %f",i,v->GetFloat()));
2553                         if (indexDP == kL3Current && v->GetFloat() > 350 && isZero == kTRUE) isZero=kFALSE; 
2554                         if (indexDP == kDipoleCurrent && v->GetFloat() > 450 && isZero == kTRUE) isZero=kFALSE; 
2555                 }
2556                 else {
2557                         AliError(Form("DCS values for the parameter outside the queried interval"));
2558                 }
2559         }
2560
2561         return ProcessFloatAll(array);
2562 }
2563
2564
2565 //_______________________________________________________________
2566
2567 Char_t AliGRPPreprocessor::ProcessBool(const TObjArray* array, Bool_t &change)
2568 {
2569         // 
2570         // processing Boolean values
2571         //
2572
2573         Bool_t aDCSBool = kTRUE;
2574
2575         AliDCSValue *v = 0x0;
2576
2577         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
2578                 v = (AliDCSValue *)array->At(iCount);
2579                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
2580                         AliError(Form("DCS values for the parameter outside the queried interval"));
2581                         continue;
2582                 }
2583                 if (iCount > 0) {
2584                         if (aDCSBool != v->GetBool()) {
2585                                 AliError(Form("DCS values for the parameter changed from %d to %d within the queried interval", (UInt_t)aDCSBool, (UInt_t)v->GetBool()));
2586                                 change = kTRUE;
2587                         }
2588                 }
2589                 aDCSBool = v->GetBool(); // always keeping last value
2590                 AliDebug(2,Form("Bool = %d",(Int_t)aDCSBool));
2591         }
2592         
2593         Char_t caDCSBool = (Char_t) aDCSBool;
2594         return caDCSBool;
2595         
2596 }
2597
2598 //_______________________________________________________________
2599
2600 Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
2601 {
2602         // 
2603         // processing Int values, returning mean
2604         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
2605         // are outside the queried time interval or their value is out of range
2606         //
2607
2608         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2609         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2610         if (timeStartString.IsNull() || timeStartString.IsNull()){
2611                 if (timeStartString.IsNull()){ 
2612                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2613                 }
2614                 else if (timeStartString.IsNull()){
2615                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2616                 }
2617                 return 0;
2618         }  
2619
2620         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2621         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2622         Float_t aDCSArrayMean = 0.0;
2623         Int_t iCounts = 0;
2624         Float_t valueBeforeSOR = 0;
2625         Float_t valueAfterEOR = 0;
2626         Int_t timestampBeforeSOR = -1;
2627         Int_t timestampAfterEOR = -1;
2628         Int_t ientrySOR = -1;
2629         Int_t ientryEOR = -1;
2630         Float_t* arrayValues = 0x0; 
2631         Double_t* arrayWeights = 0x0; 
2632         Int_t iCountsRun = 0;
2633         Int_t nCounts = array->GetEntries();
2634
2635         for(Int_t i = 0; i < nCounts; i++) {
2636                 AliDCSValue* v = (AliDCSValue *)array->At(i);
2637                 if ((v->GetInt() < fminInt) || (v->GetInt() > fmaxInt)) {
2638                         AliError(Form("Error! Int value found in DCS map at %d-th entry is OUT OF RANGE: value = %d",i, v->GetInt()));
2639                         return AliGRPObject::GetInvalidFloat();
2640                 }
2641                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2642                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetInt(),v->GetTimeStamp()));
2643                         iCounts += 1;
2644                         // look for the last value before SOR and the first value before EOR
2645                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2646                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2647                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2648                                 valueBeforeSOR = (Float_t) v->GetInt();
2649                         }
2650                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2651                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2652                                 valueAfterEOR = (Float_t) v->GetInt();
2653                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2654                         }
2655                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2656                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2657                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2658                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2659                                 AliDebug(2,Form("entry between SOR and EOR"));
2660                                 iCountsRun += 1;
2661                         }
2662                 }
2663                 else {
2664                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2665                 }
2666         }
2667
2668         if (timestampBeforeSOR == -1){
2669                 AliWarning("No value found before SOR!");
2670         }
2671         if (timestampAfterEOR == -1){
2672                 AliWarning("No value found after EOR!");
2673         }
2674
2675         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
2676         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2677         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2678         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2679         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2680         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2681
2682         Int_t nentriesUsed = 0;
2683         if (iCountsRun > 1){
2684                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2685                 AliDebug(2,"Calculating (weighted) Mean");
2686                 arrayValues = new Float_t[iCountsRun]; 
2687                 arrayWeights = new Double_t[iCountsRun]; 
2688                 nentriesUsed = iCountsRun;
2689                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2690                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2691                         Int_t timestamp2 = 0;
2692                         if (i < ientryEOR){
2693                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2694                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2695                         }
2696                         else {
2697                                 timestamp2 = timeEnd+1;
2698                         }
2699                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2700                         arrayValues[i-ientrySOR] = (Float_t)v->GetInt();
2701                 }
2702                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2703                 delete [] arrayValues;
2704                 delete [] arrayWeights;
2705         }
2706         else if (iCountsRun == 1){
2707                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2708                 nentriesUsed = 2;
2709                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2710                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
2711                         arrayValues = new Float_t[2];
2712                         arrayWeights = new Double_t[2];
2713                         arrayValues[0] = valueBeforeSOR;
2714                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2715                         arrayValues[1] = (Float_t)v->GetInt();
2716                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2717                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2718                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2719                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
2720                         delete [] arrayValues;
2721                         delete [] arrayWeights;
2722                 }
2723                 else{
2724                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2725                         return AliGRPObject::GetInvalidFloat();
2726                 }
2727         }
2728         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2729                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2730                         if (timestampBeforeSOR == -1){
2731                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2732                         }
2733                         if (timestampAfterEOR == -1){
2734                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2735                         }
2736                         return AliGRPObject::GetInvalidFloat();
2737                 }
2738                 else {
2739                         AliWarning("Using last entry before SOR and first entry after EOR.");
2740                         nentriesUsed = 2;
2741                         arrayValues = new Float_t[2];
2742                         arrayWeights = new Double_t[2];
2743                         arrayValues[0] = valueBeforeSOR;
2744                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2745                         arrayValues[1] = valueAfterEOR;
2746                         arrayWeights[1] = 1.;
2747                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2748                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2749                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2750                         delete [] arrayValues;
2751                         delete [] arrayWeights;
2752                 }
2753         }
2754
2755         AliInfo(Form("mean = %f ", aDCSArrayMean));
2756         return aDCSArrayMean;
2757
2758 }
2759 //_______________________________________________________________
2760
2761 Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
2762 {
2763         // 
2764         // processing Int values, returning mean 
2765         // AliGRPObject::GetInvalidFloat() is returned if any of the DCS values
2766         // are outside the queried time interval or their value is out of range
2767         //
2768
2769         TString timeStartString = (TString)GetRunParameter("DAQ_time_start");
2770         TString timeEndString = (TString)GetRunParameter("DAQ_time_end");
2771         if (timeStartString.IsNull() || timeStartString.IsNull()){
2772                 if (timeStartString.IsNull()){ 
2773                         AliError("DAQ_time_start not set in logbook! Setting statistical values for current DP to invalid");
2774                 }
2775                 else if (timeStartString.IsNull()){
2776                         AliError("DAQ_time_end not set in logbook! Setting statistical values for current DP to invalid");
2777                 }
2778                 return 0;
2779         }  
2780
2781         Int_t timeStart = (Int_t)(timeStartString.Atoi());
2782         Int_t timeEnd = (Int_t)(timeEndString.Atoi());
2783         Float_t aDCSArrayMean = 0.0;
2784         Int_t iCounts = 0;
2785         Float_t valueBeforeSOR = 0;
2786         Float_t valueAfterEOR = 0;
2787         Int_t timestampBeforeSOR = -1;
2788         Int_t timestampAfterEOR = -1;
2789         Int_t ientrySOR = -1;
2790         Int_t ientryEOR = -1;
2791         Float_t* arrayValues = 0x0; 
2792         Double_t* arrayWeights = 0x0; 
2793         Int_t iCountsRun = 0;
2794         Int_t nCounts = array->GetEntries();
2795
2796         for(Int_t i = 0; i < nCounts; i++) {
2797                 AliDCSValue* v = (AliDCSValue *)array->At(i);
2798                 if ((v->GetUInt() < fminUInt) || (v->GetUInt() > fmaxUInt)) {
2799                         AliError(Form("Error! UInt value found in DCS map at %d-th entry is OUT OF RANGE: value = %u",i,v->GetUInt()));
2800                         return AliGRPObject::GetInvalidFloat();
2801                 }
2802                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
2803                         AliDebug(2,Form("%d-th entry = %d at timestamp %i",i,v->GetUInt(),v->GetTimeStamp()));
2804                         iCounts += 1;
2805                         // look for the last value before SOR and the first value before EOR
2806                         if (((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) && (Int_t)(v->GetTimeStamp()) < timeStart) {
2807                                 timestampBeforeSOR = (Int_t)(v->GetTimeStamp());
2808                                 AliDebug(2,Form("timestamp of last entry before SOR = %d, with DAQ_time_start = %d",timestampBeforeSOR,timeStart));
2809                                 valueBeforeSOR = (Float_t)v->GetUInt();
2810                         }
2811                         else if ((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery() && (Int_t)(v->GetTimeStamp()) > timeEnd && timestampAfterEOR == -1){
2812                                 timestampAfterEOR = (Int_t)(v->GetTimeStamp());
2813                                 valueAfterEOR = (Float_t)v->GetUInt();
2814                                 AliDebug(2,Form("timestamp of first entry after EOR = %d, with DAQ_time_end = %d",timestampAfterEOR,timeEnd));
2815                         }
2816                         // check if there are DPs between DAQ_time_start and DAQ_time_end
2817                         if(((Int_t)(v->GetTimeStamp()) >= timeStart) &&((Int_t)(v->GetTimeStamp()) <= timeEnd)) {
2818                                 if (ientrySOR == -1) ientrySOR = i;  // first entry after SOR
2819                                 if (ientryEOR < i) ientryEOR = i;  // last entry before EOR
2820                                 AliDebug(2,Form("entry between SOR and EOR"));
2821                                 iCountsRun += 1;
2822                         }
2823                 }
2824                 else {
2825                         AliError(Form("DCS values for the parameter outside the queried interval: timestamp = %d",v->GetTimeStamp()));
2826                 }
2827         }
2828
2829         if (timestampBeforeSOR == -1){
2830                 AliWarning("No value found before SOR!");
2831         }
2832         if (timestampAfterEOR == -1){
2833                 AliWarning("No value found after EOR!");
2834         }
2835
2836         AliDebug(2,Form("Number of valid entries (within query interval) = %i, starting from %i entries",iCounts,nCounts));
2837         AliDebug(2,Form("Last value before DAQ_time_start (SOR) = %f at timestamp = %d",valueBeforeSOR,timestampBeforeSOR));
2838         AliDebug(2,Form("First value after DAQ_time_end (EOR)   = %f at timestamp = %d",valueAfterEOR,timestampAfterEOR));
2839         AliInfo(Form("Found %d entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)",iCountsRun));
2840         AliDebug(2,Form("Index of first entry after DAQ_time_start (SOR) = %d ",ientrySOR));
2841         AliDebug(2,Form("Index of first entry before DAQ_time_end (EOR) = %d ",ientryEOR));
2842
2843         Int_t nentriesUsed = 0;
2844         if (iCountsRun > 1){
2845                 AliInfo("Using entries between DAQ_time_start (SOR) and DAQ_time_end (EOR)");
2846                 AliDebug(2,"Calculating (weighted) Mean");
2847                 arrayValues = new Float_t[iCountsRun]; 
2848                 arrayWeights = new Double_t[iCountsRun]; 
2849                 nentriesUsed = iCountsRun;
2850                 for (Int_t i = ientrySOR; i <= ientryEOR; i++){
2851                         AliDCSValue *v = (AliDCSValue *)array->At(i);
2852                         Int_t timestamp2 = 0;
2853                         if (i < ientryEOR){
2854                                 AliDCSValue *v1 = (AliDCSValue *)array->At(i+1);
2855                                 timestamp2 = (Int_t)v1->GetTimeStamp();
2856                         }
2857                         else {
2858                                 timestamp2 = timeEnd+1;
2859                         }
2860                         arrayWeights[i-ientrySOR] = (Double_t)(timestamp2 - (Int_t)v->GetTimeStamp());
2861                         arrayValues[i-ientrySOR] = (Float_t)v->GetUInt();
2862                 }
2863                 aDCSArrayMean = TMath::Mean(iCountsRun,arrayValues,arrayWeights);
2864                 delete [] arrayValues;
2865                 delete [] arrayWeights;
2866         }
2867         else if (iCountsRun == 1){
2868                 AliDCSValue* v = (AliDCSValue *)array->At(ientrySOR);
2869                 nentriesUsed = 2;
2870                 if (timestampBeforeSOR != -1 && timestampBeforeSOR != (Int_t)v->GetTimeStamp()){
2871                         AliWarning("Using single entry between DAQ_time_start (SOR) and DAQ_time_end (EOR) and last entry before SOR.");
2872                         arrayValues = new Float_t[2];
2873                         arrayWeights = new Double_t[2];
2874                         arrayValues[0] = valueBeforeSOR;
2875                         arrayWeights[0] = (Double_t)((Int_t)v->GetTimeStamp()-timestampBeforeSOR);
2876                         arrayValues[1] = (Float_t)v->GetUInt();
2877                         arrayWeights[1] = (Double_t)(timeEnd+1-(Int_t)v->GetTimeStamp());
2878                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2879                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2880                         aDCSArrayMean = TMath::Mean(2,arrayValues,arrayWeights);
2881                         delete [] arrayValues;
2882                         delete [] arrayWeights;
2883                 }
2884                 else{
2885                         AliError("Cannot calculate mean - only one value collected during the run, but no value before with which to calculate the statistical quantities");
2886                         return AliGRPObject::GetInvalidFloat();
2887                 }
2888         }
2889         else { // iCountsRun == 0, using the point immediately before SOR and the one immediately after EOR
2890                 if (timestampBeforeSOR == -1 || timestampAfterEOR == -1){
2891                         if (timestampBeforeSOR == -1){
2892                                 AliError("Cannot calculate mean - no points during the run collected, and point before SOR missing");
2893                         }
2894                         if (timestampAfterEOR == -1){
2895                                 AliError("Cannot calculate maen - no points during the run collected, and point after EOR missing");
2896                         }
2897                         return AliGRPObject::GetInvalidFloat();
2898                 }
2899                 else {
2900                         AliWarning("Using last entry before SOR and first entry after EOR.");
2901                         nentriesUsed = 2;
2902                         arrayValues = new Float_t[2];
2903                         arrayWeights = new Double_t[2];
2904                         arrayValues[0] = valueBeforeSOR;
2905                         arrayWeights[0] = (Double_t)(timestampAfterEOR - timestampBeforeSOR);
2906                         arrayValues[1] = valueAfterEOR;
2907                         arrayWeights[1] = 1.;
2908                         AliDebug(2,Form("value0 = %f, with weight = %f",arrayValues[0],arrayWeights[0])); 
2909                         AliDebug(2,Form("value1 = %f, with weight = %f",arrayValues[1],arrayWeights[1])); 
2910                         aDCSArrayMean = TMath::Mean(1,arrayValues,arrayWeights);
2911                         delete [] arrayValues;
2912                         delete [] arrayWeights;
2913                 }
2914         }
2915
2916         AliInfo(Form("mean = %f ",aDCSArrayMean));
2917         return aDCSArrayMean;
2918
2919 }
2920
2921
2922 //_______________________________________________________________
2923
2924 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
2925 {
2926         // extract DCS pressure maps. Perform fits to save space
2927         
2928         TMap *map = fPressure->ExtractDCS(dcsAliasMap);
2929         if (map) {
2930                 AliDebug(2,Form("Map has %d entries",map->GetEntries()));
2931                 fPressure->MakeSplineFit(map);
2932                 Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
2933                 if (fitFraction > kFitFraction ) {
2934                         AliInfo(Form("Pressure values extracted, %d fits performed for %d sensors.", fPressure->NumFits(),fPressure->NumSensors()));
2935                 } else { 
2936                         AliInfo("Too few pressure maps fitted!!!");
2937                 }
2938         } else {
2939                 AliInfo("no atmospheric pressure map extracted!!!");
2940         }
2941         delete map;
2942         
2943         return fPressure;
2944 }
2945
2946
2947   
2948 //_______________________________________________________________
2949 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)
2950 {
2951         //
2952         // Retrieves logbook and trigger information from the online logbook
2953         // This information is needed for prompt reconstruction
2954         //
2955         // Parameters are:
2956         // Run number
2957         // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
2958         // cdbRoot
2959         //
2960         // returns:
2961         //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
2962         //         0 on success and no run was found
2963         //         negative on error
2964         //
2965         // This function is NOT called during the preprocessor run in the Shuttle!
2966         //
2967         
2968         // defaults
2969         if (dbPort == 0)
2970                 dbPort = 3306;
2971         
2972         // CDB connection
2973         AliCDBManager* cdb = AliCDBManager::Instance();
2974         cdb->SetDefaultStorage(cdbRoot);
2975         
2976         // SQL connection
2977         TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
2978         
2979         if (!server)
2980                 {
2981                         Printf("ERROR: Could not connect to DAQ LB");
2982                         return -1;
2983                 }
2984         
2985         // main logbook
2986         TString sqlQuery;
2987         sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent,beamType FROM logbook WHERE run = %d", run);
2988         TSQLResult* result = server->Query(sqlQuery);
2989         if (!result)
2990                 {
2991                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
2992                         return -2;
2993                 }
2994         
2995         if (result->GetRowCount() == 0)
2996                 {
2997                         Printf("ERROR: Run %d not found", run);
2998                         delete result;
2999                         return -3;
3000                 }
3001         
3002         TSQLRow* row = result->Next();
3003         if (!row)
3004                 {
3005                         Printf("ERROR: Could not receive data from run %d", run);
3006                         delete result;
3007                         return -4;
3008                 }
3009         
3010         TString timeStartString(row->GetField(0));
3011         TString runType(row->GetField(1));
3012         TString detectorMaskString(row->GetField(2));
3013         TString l3CurrentString(row->GetField(3));
3014         TString dipoleCurrentString(row->GetField(4));
3015         TString beamTypeString(row->GetField(5));
3016         time_t timeStart = (time_t)(timeStartString.Atoi());
3017         UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
3018         Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
3019         Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
3020         Char_t l3Polarity = (l3CurrentString.Atof() < 0) ? 1 : 0;
3021         Char_t dipolePolarity = (dipoleCurrentString.Atof() < 0) ? 1 : 0;
3022         if (beamTypeString.CompareTo("Pb-Pb",TString::kIgnoreCase) == 0){
3023                 beamTypeString="A-A";
3024         }
3025         
3026         AliGRPObject * grpObj = new AliGRPObject();
3027         grpObj->SetTimeStart(timeStart); 
3028         grpObj->SetRunType((TString)(row->GetField(1)));
3029         grpObj->SetDetectorMask(detectorMask);
3030         grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
3031         grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
3032         grpObj->SetL3Polarity(l3Polarity);
3033         grpObj->SetDipolePolarity(dipolePolarity);
3034         grpObj->SetPolarityConventionLHC();  // after the dipole cables swap we comply with LHC convention
3035         grpObj->SetBeamType(beamTypeString);
3036
3037         delete row;
3038         row = 0;
3039         
3040         delete result;
3041         result = 0;
3042         
3043         Printf("Storing GRP/GRP/Data object with the following content");
3044         grpObj->Dump();
3045         
3046         AliCDBMetaData metadata;
3047         metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus & Chiara Zampolli");
3048         metadata.SetComment("GRP Output parameters received during online running");
3049         
3050         AliCDBId id("GRP/GRP/Data", run, run);
3051         Bool_t success = cdb->Put(grpObj, id, &metadata);
3052         
3053         delete grpObj;
3054         
3055         if (!success)
3056                 {
3057                         Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
3058                         return -5;
3059                 }
3060         
3061         // Receive trigger information
3062         sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
3063         result = server->Query(sqlQuery);
3064         if (!result)
3065                 {
3066                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
3067                         return -11;
3068                 }
3069         
3070         if (result->GetRowCount() == 0)
3071                 {
3072                         Printf("ERROR: Run %d not found in logbook_trigger_config", run);
3073                         delete result;
3074                         return -12;
3075                 }
3076         
3077         row = result->Next();
3078         if (!row)
3079                 {
3080                         Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
3081                         delete result;
3082                         return -13;
3083                 }
3084         
3085         TString triggerConfig(row->GetField(0));
3086         
3087         delete row;
3088         row = 0;
3089         
3090         delete result;
3091         result = 0;
3092         
3093         Printf("Found trigger configuration: %s", triggerConfig.Data());
3094         
3095         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
3096         if (!runcfg)
3097                 {
3098                         Printf("ERROR: Could not create CTP configuration object");
3099                         return -14;
3100                 }
3101         
3102         metadata.SetComment("CTP run configuration received during online running");
3103         
3104         AliCDBId id2("GRP/CTP/Config", run, run);
3105         success = cdb->Put(runcfg, id2, &metadata);
3106         
3107         delete runcfg;
3108         runcfg = 0;
3109         
3110         if (!success)
3111                 {
3112                         Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
3113                         return -15;
3114                 }
3115         
3116
3117         // Receive list of GDCs for this run
3118         sqlQuery.Form("SELECT GDC FROM logbook_stats_GDC WHERE run = %d", run);
3119         result = server->Query(sqlQuery);
3120         if (!result)
3121                 {
3122                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
3123                         return -24;
3124                 }
3125         
3126         if (result->GetRowCount() == 0)
3127                 {
3128                         Printf("ERROR: Run %d not found in logbook_stats_GDC", run);
3129                         delete result;
3130                         return -25;
3131                 }
3132
3133         gdc = "";
3134         for (Int_t iGDC = 0; iGDC < result->GetRowCount(); iGDC++) {
3135           row = result->Next();
3136           if (!row)
3137             {
3138               Printf("ERROR: Could not receive logbook_stats_GDC data from run %d", run);
3139               delete result;
3140               return -26;
3141             }
3142           gdc += row->GetField(0);
3143           gdc += " ";
3144         }
3145
3146         delete row;
3147         row = 0;
3148         
3149         delete result;
3150         result = 0;
3151         
3152         Printf("Found GDC: %s", gdc.Data());
3153
3154         // get last run with same run type that was already processed by the SHUTTLE
3155         
3156         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());
3157         result = server->Query(sqlQuery);
3158         if (!result)
3159                 {
3160                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
3161                         return -21;
3162                 }
3163         
3164         if (result->GetRowCount() == 0)
3165                 {
3166                         Printf("ERROR: No result with query <%s>", sqlQuery.Data());
3167                         delete result;
3168                         return -22;
3169                 }
3170         
3171         row = result->Next();
3172         if (!row)
3173                 {
3174                         Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
3175                         delete result;
3176                         return -23;
3177                 }
3178         
3179         TString lastRunStr(row->GetField(0));
3180         Int_t lastRun = lastRunStr.Atoi();
3181         
3182         Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
3183         
3184         delete row;
3185         row = 0;
3186         
3187         delete result;
3188         result = 0;
3189         
3190         server->Close();
3191         delete server;
3192         server = 0;
3193         
3194         return lastRun;
3195 }
3196 //------------------------------------------------------------------------------------------------------
3197 Float_t AliGRPPreprocessor::ProcessEnergy(TObjArray* const array, Double_t timeStart){
3198
3199         //
3200         // Method to processo LHC Energy information
3201         // Only the first value is returned, provided that it is withing DAQ_time_start and DAQ_time_end
3202         //
3203
3204         Int_t nCounts = array->GetEntries();
3205         Float_t energy = -1;
3206         Double_t timeEnergy = -1;
3207         Int_t indexEnergy = -1;
3208         Bool_t foundEnergy = kFALSE;
3209
3210         AliDebug(2,Form("Energy measurements = %d\n",nCounts));
3211         if (nCounts ==0){
3212                 AliWarning("No Energy values found! Beam Energy remaining invalid!");
3213         }
3214         else{
3215                 for (Int_t i = 0; i < nCounts; i++){
3216                         AliDCSArray *dcs = (AliDCSArray*)array->At(i);
3217                         if (dcs){
3218                                 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
3219                                         timeEnergy = dcs->GetTimeStamp();
3220                                         indexEnergy = i;
3221                                         foundEnergy = kTRUE;
3222                                 }
3223                                 else{
3224                                         break;
3225                                 }
3226                         }
3227                 }
3228                 if (!foundEnergy){
3229                         AliInfo("No value for the Energy found before start of run, the Energy will remain invalid");
3230                 }
3231                 else {
3232                         AliDCSArray* dcs = (AliDCSArray*)array->At(indexEnergy);
3233                         energy = (Float_t)(TMath::Nint(((Double_t)(dcs->GetInt(0)))*120/1000)); // sqrt(s)/2 energy in GeV
3234                         AliInfo(Form("Energy value found = %d (at %f), converting --> sqrt(s)/2 = %f (GeV)", dcs->GetInt(0),dcs->GetTimeStamp(),energy));
3235                 }
3236         }
3237
3238         return energy;
3239 }
3240 //------------------------------------------------------------------------------------------------------
3241 AliLHCClockPhase* AliGRPPreprocessor::ProcessLHCClockPhase(TObjArray *beam1phase,TObjArray *beam2phase, Double_t timeEnd)
3242 {
3243   //
3244   // Method to process LHC-Clock Phase data
3245   // Only the values between DAQ_time_start and DAQ_time_end are kept
3246   //
3247   AliLHCClockPhase *phaseObj = new AliLHCClockPhase;
3248
3249   Bool_t foundBeam1Phase = kFALSE, foundBeam2Phase = kFALSE;
3250   const Float_t threshold = 0.050; // we store the measurement only in case they differ with more 50ps from the previous one 
3251
3252   TString timeCreatedStr = GetRunParameter("time_created");
3253   Double_t timeCreated = timeCreatedStr.Atof();
3254
3255   Int_t nCounts = beam1phase->GetEntries();
3256   AliDebug(2,Form("Beam1 phase measurements = %d\n",nCounts));
3257   if (nCounts ==0){
3258     AliWarning("No beam1 LHC clock phase values found!");
3259     delete phaseObj;
3260     return NULL;
3261   }
3262   else{
3263     Double_t prevPhase = 0;
3264     for (Int_t i = 0; i < nCounts; i++){
3265       AliDCSArray *dcs = (AliDCSArray*)beam1phase->At(i);
3266       if (dcs){
3267               //if (dcs->GetTimeStamp()>=timeStart && dcs->GetTimeStamp()<=timeEnd) {
3268               if (dcs->GetTimeStamp()>=timeCreated && dcs->GetTimeStamp()<=timeEnd) {
3269           if ((i == 0) || (i == (nCounts-1)) ||
3270               !foundBeam1Phase ||
3271               (TMath::Abs(dcs->GetDouble(0)-prevPhase) > threshold)) {
3272             prevPhase = dcs->GetDouble(0);
3273             foundBeam1Phase = kTRUE;
3274             AliInfo(Form("B1 Clk Phase = %f at TS = %f",
3275                          (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
3276             phaseObj->AddPhaseB1DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
3277           }
3278         }
3279       }
3280     }
3281     if (!foundBeam1Phase){
3282       AliError("No beam1 LHC clock phase values found within the run!");
3283       delete phaseObj;
3284       return NULL;
3285     }
3286   }
3287
3288   nCounts = beam2phase->GetEntries();
3289   AliDebug(2,Form("Beam2 phase measurements = %d\n",nCounts));
3290   if (nCounts ==0){
3291     AliWarning("No beam2 LHC clock phase values found!");
3292     delete phaseObj;
3293     return NULL;
3294   }
3295   else{
3296     Double_t prevPhase = 0;
3297     for (Int_t i = 0; i < nCounts; i++){
3298       AliDCSArray *dcs = (AliDCSArray*)beam2phase->At(i);
3299       if (dcs){
3300         if (dcs->GetTimeStamp()>=timeCreated && dcs->GetTimeStamp()<=timeEnd) {
3301           if ((i == 0) || (i == (nCounts-1)) ||
3302               !foundBeam2Phase ||
3303               (TMath::Abs(dcs->GetDouble(0)-prevPhase) > threshold)) {
3304             prevPhase = dcs->GetDouble(0);
3305             foundBeam2Phase = kTRUE;
3306             AliInfo(Form("B2 Clk Phase = %f at TS = %f",
3307                          (Float_t)dcs->GetDouble(0),dcs->GetTimeStamp()));  
3308             phaseObj->AddPhaseB2DP((UInt_t)dcs->GetTimeStamp(),(Float_t)dcs->GetDouble(0));
3309           }
3310         }
3311       }
3312     }
3313     if (!foundBeam2Phase){
3314       AliError("No beam2 LHC clock phase values found within the run!");
3315       delete phaseObj;
3316       return NULL;
3317     }
3318   }
3319
3320   return phaseObj;
3321 }
3322 //------------------------------------------------------------------------------------------------------
3323 TString AliGRPPreprocessor::ParseBeamTypeString(TString beamType, Int_t iBeamType)
3324 {
3325         // Method to return the convention for the separate beam type
3326         // in the form A*1000+Z
3327         // e.g.: Pb82 --> 208000 + 82 = 208082
3328         //       p --> 1000 + 1 = 1001
3329
3330         Int_t a = 0;
3331         Int_t z = 0;
3332         TString separateString("");
3333         Log(Form("Setting Beam Type for beam %d to A*1000+Z",iBeamType));
3334         if (beamType.CompareTo("PROTON",TString::kIgnoreCase) == 0){
3335                 Log(Form("Beam type %d is PROTON --> The single beam type will be set to 1001 (A = 1, Z = 1)",iBeamType));
3336                 separateString = "1001";
3337                 return separateString;
3338         }
3339         else { 
3340                 TPRegexp regexpA("\\D+");
3341                 TPRegexp regexpZ("\\d+");
3342                 TObjArray* arrayA = regexpA.MatchS(beamType);
3343                 TObjArray* arrayZ = regexpZ.MatchS(beamType);
3344                 if (arrayA->GetEntries() != 1 || arrayZ->GetEntries() != 1){
3345                         Log(Form("The beamType string for beam %d does not contain the necessary information! Returning the info as published by LHC (i.e. %s)",iBeamType, beamType.Data()));
3346                         return beamType;
3347                 }
3348                 else{
3349                         TString strA = ((TObjString*)(arrayA->At(0)))->String();
3350                         TString strZ = ((TObjString*)(arrayZ->At(0)))->String();
3351                         if (strA.CompareTo("LEAD",TString::kIgnoreCase) == 0 || strA.CompareTo("PB",TString::kIgnoreCase) == 0){
3352                                 Log(Form("Beam %d is %s --> A = 208",iBeamType, strA.Data()));
3353                                 a = 208;
3354                         }
3355                         else{
3356                                 Log(Form("This beam was not foreseen so far, leaving A=0"));
3357                         }
3358                         z = strZ.Atoi();
3359                         Log(Form("Beam %d has Z = %d",iBeamType, z));
3360                         separateString = Form("%d",a*1000+z);
3361                         return separateString;
3362                 }                                            
3363         }
3364                
3365         return separateString;
3366
3367 }
3368