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