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