]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliGRPPreprocessor.cxx
bug fixed
[u/mrichter/AliRoot.git] / STEER / AliGRPPreprocessor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //                          Class AliGRPPreprocessor
20 //                  Global Run Parameters (GRP) preprocessor
21 //    Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
22 //    Modified: Ernesto.Lopez.Torres@cern.ch  CEADEN-CERN
23 //-------------------------------------------------------------------------
24
25 #include <TChain.h>
26 #include <TList.h>
27 #include <TMap.h>
28 #include <TObjString.h>
29 #include <TGraph.h>
30
31 #include "AliGRPPreprocessor.h"
32 #include "AliGRPObject.h"
33 #include "AliDCSSensor.h"
34 #include "AliSplineFit.h"
35 #include "AliDCSSensorArray.h"
36
37 #include "AliTriggerConfiguration.h"
38 #include "AliTriggerRunScalers.h"
39
40 #include "AliCDBMetaData.h"
41 #include "AliLog.h"
42
43 class AliDCSValue;
44 class AliShuttleInterface;
45
46 // needed for ReceivePromptRecoParameters
47
48 #include <TSQLServer.h>
49 #include <TSQLResult.h>
50 #include <TSQLRow.h>
51 #include <AliCDBManager.h>
52 #include <AliCDBMetaData.h>
53 #include <AliCDBId.h>
54 #include <AliTriggerConfiguration.h>
55
56 const Double_t kFitFraction = 0.7;                 // Fraction of DCS sensor fits required
57
58 ClassImp(AliGRPPreprocessor)
59
60 //_______________________________________________________________
61
62   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 8; // num parameters in the logbook
63   const Int_t AliGRPPreprocessor::fgknDCSDP = 50;   // number of dcs dps
64   const Int_t AliGRPPreprocessor::fgknDCSDPHallProbes = 40;   // number of dcs dps
65   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
66                    "LHCState",              // missing in DCS
67                    "L3Polarity",
68                    "DipolePolarity",
69                    "LHCLuminosity",         // missing in DCS
70                    "BeamIntensity",         // missing in DCS
71                    "L3Current",
72                    "DipoleCurrent",
73                    "L3_BSF17_H1",
74                    "L3_BSF17_H2",
75                    "L3_BSF17_H3",
76                    "L3_BSF17_Temperature",
77                    "L3_BSF4_H1",
78                    "L3_BSF4_H2",
79                    "L3_BSF4_H3",
80                    "L3_BSF4_Temperature",
81                    "L3_BKF17_H1",
82                    "L3_BKF17_H2",
83                    "L3_BKF17_H3",
84                    "L3_BKF17_Temperature",
85                    "L3_BKF4_H1",
86                    "L3_BKF4_H2",
87                    "L3_BKF4_H3",
88                    "L3_BKF4_Temperature",
89                    "L3_BSF13_H1",
90                    "L3_BSF13_H2",
91                    "L3_BSF13_H3",
92                    "L3_BSF13_Temperature",
93                    "L3_BSF8_H1",
94                    "L3_BSF8_H2",
95                    "L3_BSF8_H3",
96                    "L3_BSF8_Temperature",
97                    "L3_BKF13_H1",
98                    "L3_BKF13_H2",
99                    "L3_BKF13_H3",
100                    "L3_BKF13_Temperature",
101                    "L3_BKF8_H1",
102                    "L3_BKF8_H2",
103                    "L3_BKF8_H3",
104                    "L3_BKF8_Temperature",
105                    "Dipole_Inside_H1",
106                    "Dipole_Inside_H2",
107                    "Dipole_Inside_H3",
108                    "Dipole_Inside_Temperature",
109                    "Dipole_Outside_H1",
110                    "Dipole_Outside_H2",
111                    "Dipole_Outside_H3",
112                    "Dipole_Outside_Temperature",
113                    "CavernTemperature",
114                    "CavernAtmosPressure",
115                    "SurfaceAtmosPressure"
116                  };
117
118   const char* AliGRPPreprocessor::fgkDCSDataPointsHallProbes[AliGRPPreprocessor::fgknDCSDPHallProbes] = {
119                    "L3_BSF17_H1",
120                    "L3_BSF17_H2",
121                    "L3_BSF17_H3",
122                    "L3_BSF17_Temperature",
123                    "L3_BSF4_H1",
124                    "L3_BSF4_H2",
125                    "L3_BSF4_H3",
126                    "L3_BSF4_Temperature",
127                    "L3_BKF17_H1",
128                    "L3_BKF17_H2",
129                    "L3_BKF17_H3",
130                    "L3_BKF17_Temperature",
131                    "L3_BKF4_H1",
132                    "L3_BKF4_H2",
133                    "L3_BKF4_H3",
134                    "L3_BKF4_Temperature",
135                    "L3_BSF13_H1",
136                    "L3_BSF13_H2",
137                    "L3_BSF13_H3",
138                    "L3_BSF13_Temperature",
139                    "L3_BSF8_H1",
140                    "L3_BSF8_H2",
141                    "L3_BSF8_H3",
142                    "L3_BSF8_Temperature",
143                    "L3_BKF13_H1",
144                    "L3_BKF13_H2",
145                    "L3_BKF13_H3",
146                    "L3_BKF13_Temperature",
147                    "L3_BKF8_H1",
148                    "L3_BKF8_H2",
149                    "L3_BKF8_H3",
150                    "L3_BKF8_Temperature",
151                    "Dipole_Inside_H1",
152                    "Dipole_Inside_H2",
153                    "Dipole_Inside_H3",
154                    "Dipole_Inside_Temperature",
155                    "Dipole_Outside_H1",
156                    "Dipole_Outside_H2",
157                    "Dipole_Outside_H3",
158                    "Dipole_Outside_Temperature",
159                  };
160                  
161   const Short_t kSensors = 48; // start index position of sensor in DCS DPs
162   const Short_t kNumSensors = 2; // Number of sensors in DCS DPs
163
164   const char* AliGRPPreprocessor::fgkLHCState[20] = {
165                    "P", "PREPARE",
166                    "J", "PREINJECTION",
167                    "I", "INJECTION",
168                    "F", "FILLING",
169                    "A", "ADJUST",
170                    "U", "UNSTABLE BEAMS",
171                    "S", "STABLE BEAMS",
172                    "D", "BEAM DUMP",
173                    "R", "RECOVER",
174                    "C", "PRECYCLE"
175                  };
176
177   const char* kppError[] = {
178                    "",
179                    "(DAQ logbook ERROR)",
180                    "(DAQ FXS ERROR)",
181                    "(DCS FXS ERROR)",
182                    "(DCS data points ERROR)",
183                    "(Trigger Configuration ERROR)"
184   };
185
186 //_______________________________________________________________
187
188 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
189         AliPreprocessor("GRP",shuttle),  fPressure(0)
190 {
191         // constructor - shuttle must be instantiated!
192
193         AddRunType("COSMIC");
194         AddRunType("LASER");
195         AddRunType("PHYSICS");
196 }
197
198 //_______________________________________________________________
199
200 AliGRPPreprocessor::~AliGRPPreprocessor()
201 {
202         //destructor
203         
204         delete fPressure;
205 }
206
207 //_______________________________________________________________
208
209 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
210 {
211   // Initialize preprocessor
212
213   AliPreprocessor::Initialize(run, startTime, endTime);
214
215   AliInfo("Initialization of the GRP preprocessor.");
216   TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
217   for(Int_t j = 0; j < kNumSensors; j++) {
218     AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
219     sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
220   }
221   AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
222
223   //  fPressure = new AliDCSSensorArray(fStartTime, fEndTime, array);
224   fPressure = new AliDCSSensorArray(GetStartTimeDCSQuery(), GetEndTimeDCSQuery(), array);
225 }
226
227 //_______________________________________________________________
228
229 UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
230 {
231         // process data retrieved by the Shuttle
232         
233         AliGRPObject *grpobj = new AliGRPObject();  // object to store data
234
235         //=================//
236         // DAQ logbook     //
237         //=================//
238         UInt_t error = 0;
239         
240         grpobj = ProcessDaqLB();
241
242         //=================//
243         // DAQ FXS         //
244         //=================//
245         UInt_t iDaqFxs = ProcessDaqFxs();
246         if( iDaqFxs == 0 ) {
247                 Log(Form("DAQ FXS, successful!"));
248         } else {
249                 Log(Form("DAQ FXS, could not store run raw tag file!!!"));
250                 error |= 2;
251         }
252         
253         //=================//
254         // DCS FXS         //
255         //=================//
256         UInt_t iDcsFxs = ProcessDcsFxs();
257         if( iDcsFxs == 0 ) {
258                 Log(Form("DCS FXS, successful!"));
259         } else {
260                 Log(Form("DCS FXS, Could not store CTP run configuration and scalers!!!"));
261                 error |= 4;
262         }
263         
264         //=================//
265         // DCS data points //
266         //=================//
267         Log(Form("Starting DCS Query at %d and finishing at %d",GetStartTimeDCSQuery(),GetEndTimeDCSQuery()));
268         Int_t entries = ProcessDcsDPs( valueMap, grpobj );
269         Log(Form("entries found = %d",entries));
270         if( entries < fgknDCSDP-7 ) { // FIXME (!= ) LHState and pressure map are not working yet... and removing the 4 values for the missing HP 
271                 Log(Form("Problem with the DCS data points!!!"));
272                 error |= 8;
273         } else  Log(Form("DCS data points, successful!"));
274         
275         //=======================//
276         // Trigger Configuration //
277         //=======================//
278         // either from DAQ logbook.....
279         const char * triggerConf = GetTriggerConfiguration();
280         if (triggerConf!= NULL) {
281                 Log("Found trigger configuration in DAQ logbook");
282                 AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConf);      
283                 if (!runcfg) {
284                         Log("Bad CTP run configuration file from DAQ logbook! The corresponding CDB entry will not be filled!");
285                         error |= 16;
286                 }
287                 else {
288                         TString titleCTPcfg = Form("CTP cfg for run %i from DAQ",fRun);
289                         runcfg->SetTitle(titleCTPcfg);
290                         AliCDBMetaData metaData;
291                         metaData.SetBeamPeriod(0);
292                         metaData.SetResponsible("Roman Lietava");
293                         metaData.SetComment("CTP run configuration from DAQ logbook");
294                         if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
295                                 Log("Unable to store the CTP run configuration object to OCDB!");
296                                 error |= 16;
297                         }
298                 }
299         }
300         /*  the DCS should not been used any more to access this stuff!!!
301         // ...or from DCS FXS
302         else{
303                 Log("No trigger configuration found in the DAQ logbook!! Trying reading from DCS FXS...");
304                 TString runcfgfile = GetFile(kDCS, "CTP_runconfig", "");
305                 if (runcfgfile.IsNull()) {
306                         Log("No CTP runconfig files has been found in DCS FXS!");
307                         error |= 16;
308                 }
309                 else {
310                         Log(Form("File with Id CTP_runconfig found! Copied to %s",runcfgfile.Data()));
311                         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfiguration(runcfgfile);
312                         if (!runcfg) {
313                                 Log("Bad CTP run configuration file from DCS FXS! The corresponding CDB entry will not be filled!");
314                                 error |= 16;;
315                         }
316                         else {
317                                 TString titleCTPcfg = Form("CTP cfg for run %i from DCS",fRun);
318                                 runcfg->SetTitle(titleCTPcfg);
319                                 AliCDBMetaData metaData;
320                                 metaData.SetBeamPeriod(0);
321                                 metaData.SetResponsible("Roman Lietava");
322                                 metaData.SetComment("CTP run configuration from DCS FXS");
323                                 if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
324                                         Log("Unable to store the CTP run configuration object to OCDB!");
325                                         error |= 16;
326                                 }
327                         }
328                 }
329         }
330         */
331         AliCDBMetaData md;
332         md.SetResponsible("Ernesto Lopez Torres");
333         md.SetComment("Output parameters from the GRP preprocessor.");
334         
335         Bool_t result = kTRUE;
336         Log("Before storing");
337         result = Store("GRP", "Data", grpobj, &md); 
338         delete grpobj;
339         
340         if (result && !error ) {
341                 Log("GRP Preprocessor Success");
342                 return 0;
343         } else {
344                 Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s",
345                           kppError[(error&1)?1:0],
346                           kppError[(error&2)?2:0],
347                           kppError[(error&4)?3:0],
348                           kppError[(error&8)?4:0],
349                           kppError[(error&16)?5:0]
350                           ));
351                 return error;
352         }
353 }
354
355 //_______________________________________________________________
356
357 AliGRPObject* AliGRPPreprocessor::ProcessDaqLB()
358 {
359         //Getting the DAQ lb information
360         
361         time_t timeStart         = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
362         time_t timeEnd         = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
363         Float_t beamEnergy         = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof());
364         TString beamType = (TString)GetRunParameter("beamType");
365         Char_t numberOfDetectors = (Char_t)(((TString)GetRunParameter("numberOfDetectors")).Atoi());
366         UInt_t  detectorMask= (UInt_t)(((TString)GetRunParameter("detectorMask")).Atoi());
367         TString lhcPeriod = (TString)GetRunParameter("LHCperiod");
368         TString runType = (TString)GetRunType();
369
370         AliGRPObject* grpObj = new AliGRPObject();
371
372         if (timeStart != 0){
373                 grpObj->SetTimeStart(timeStart);
374                 Log(Form("Start time for run %d: %d",fRun, (Int_t)timeStart));
375         } 
376         else {
377                 Log(Form("Start time not put in logbook, setting to invalid in GRP entry!"));
378         }
379
380         if (timeEnd != 0){
381                 grpObj->SetTimeEnd(timeEnd);
382                 Log(Form("End time for run %d: %i",fRun, (Int_t)timeEnd));
383         } 
384         else {
385                 Log(Form("End time not put in logbook, setting to invalid in GRP entry!"));
386         }
387
388         if (beamEnergy != 0){
389                 grpObj->SetBeamEnergy(beamEnergy);
390                 Log(Form("Beam Energy for run %d: %f",fRun, beamEnergy));
391         } 
392         else {
393                 Log(Form("Beam Energy not put in logbook, setting to invalid in GRP entry!"));
394         }
395
396                 Log(Form("Beam Type for run %d: %s",fRun, beamType.Data()));
397                 
398         if (beamType.Length() != 0){
399                 grpObj->SetBeamType(beamType);
400                 Log(Form("Beam Type for run %d: %s",fRun, beamType.Data()));
401         } 
402         else {
403                 Log(Form("Beam Type not put in logbook, setting to invalid in GRP entry!"));
404         }
405                 
406         if (numberOfDetectors != 0){
407                 grpObj->SetNumberOfDetectors(numberOfDetectors);
408                 Log(Form("Number Of Detectors for run %d: %d",fRun, (Int_t)numberOfDetectors));
409         } 
410         else {
411                 Log(Form("Number Of Detectors not put in logbook, setting to invalid in GRP entry!"));
412         }
413
414         if (detectorMask != 0){
415                 grpObj->SetDetectorMask(detectorMask);
416                 Log(Form("Detector Mask for run %d: %d",fRun, detectorMask));
417         } 
418         else {
419                 Log(Form("Detector Mask not put in logbook, setting to invalid in GRP entry!"));
420         }
421
422         if (lhcPeriod.Length() != 0) {
423                 grpObj->SetLHCPeriod(lhcPeriod);
424                 Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod.Data()));
425         } else {
426                 Log(Form("LHCperiod not put in logbook, setting to invalid in GRP entry!"));
427         }
428         if (runType.Length() != 0) {
429                 grpObj->SetRunType(runType);
430                 Log(Form("Run Type (DAQ) for run %d: %s",fRun, runType.Data()));
431         } else {
432                 Log(Form("Run Type not put in logbook, setting to invalid in GRP entry!"));
433         }
434
435         return grpObj;
436 }
437
438 //_______________________________________________________________
439
440 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
441 {
442         //======DAQ FXS======//
443         
444         TList* list = GetFileSources(kDAQ);  
445         if (!list) {
446                 Log("No raw data tag list: connection problems with DAQ FXS logbook!");
447                 return 1;
448         }
449         
450         if (list->GetEntries() == 0) {
451                 Log("no raw data tags in this run: nothing to merge!");
452                 delete  list; list=0;
453                 return 0;
454         }
455         
456         TChain *fRawTagChain = new TChain("T");
457         Int_t nFiles=0;
458         TIterator* iter = list->MakeIterator();
459         TObject* obj = 0;
460         while ((obj = iter->Next())) {
461                 TObjString* objStr = dynamic_cast<TObjString*> (obj);
462                 if (objStr) {
463                         Log(Form("Found source %s", objStr->String().Data()));
464                         TList* list2 = GetFileIDs(kDAQ, objStr->String());
465                         if (!list2) {
466                                 Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
467                                 delete fRawTagChain; fRawTagChain=0;
468                                 return 1;
469                         }
470                         Log(Form("Number of ids: %d",list2->GetEntries()));
471                         for(Int_t i = 0; i < list2->GetEntries(); i++) {
472                                 TObjString *idStr = (TObjString *)list2->At(i);
473                                 TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
474                                 if (fileName.Length() > 0) {
475                                         Log(Form("Adding file in the chain: %s",fileName.Data()));
476                                         fRawTagChain->Add(fileName.Data());
477                                         nFiles++;
478                                 } else {
479                                         Log(Form("Could not retrieve file with id %s from source %s: "
480                                                  "connection problems with DAQ FXS!",
481                                                  idStr->String().Data(), objStr->String().Data()));
482                                         delete list; list=0;
483                                         delete list2; list2=0;
484                                         delete fRawTagChain; fRawTagChain=0;
485                                         return 2;
486                                 }
487                         }
488                         delete list2;
489                 }
490         }
491         
492         TString fRawDataFileName = "GRP_Merged.tag.root";
493         Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
494         
495         if( fRawTagChain->Merge(fRawDataFileName) < 1 ) {
496                 Log("Error merging raw data files!!!");
497                 return 3;
498         }
499         
500         TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
501         Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
502         
503         if (!result) {
504                 Log("Problem storing raw data tags in local file!!!");
505         } else {
506                 Log("Raw data tags merged successfully!!");
507         }
508         
509         delete iter;
510         delete list;
511         delete fRawTagChain; fRawTagChain=0;
512         
513         if (result == kFALSE) {
514                 return 4;
515         }
516         
517         return 0;
518         
519 }
520
521 //_______________________________________________________________
522 UInt_t AliGRPPreprocessor::ProcessDcsFxs()
523 {
524
525         // processing the info
526         // stored in the DCS FXS
527         // coming from the trigger
528
529         {
530                 // Get the CTP counters information
531                 TList* list = GetFileSources(kDCS,"CTP_xcounters");  
532                 if (!list) {
533                         Log("No CTP counters file: connection problems with DAQ FXS logbook!");
534                         return 1;
535                 }
536                 
537                 if (list->GetEntries() == 0) {
538                         Log("No CTP counters file to be processed!");
539                         return 1;
540                 }
541                 else {
542                         TIter iter(list);
543                         TObjString *source;
544                         while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
545                                 TString countersfile = GetFile(kDCS, "CTP_xcounters", source->GetName());
546                                 if (countersfile.IsNull()) {
547                                         Log("No CTP counters files has been found: empty source!");
548                                 }
549                                 else {
550                                         Log(Form("File with Id CTP_xcounters found in source %s! Copied to %s",source->GetName(),countersfile.Data()));
551                                         AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
552                                         if (!scalers) {
553                                                 Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
554                                                 return 1;
555                                         }
556                                         else {
557                                                 AliCDBMetaData metaData;
558                                                 metaData.SetBeamPeriod(0);
559                                                 metaData.SetResponsible("Roman Lietava");
560                                                 metaData.SetComment("CTP scalers");
561                                                 if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
562                                                         Log("Unable to store the CTP scalers object to OCDB!");
563                                                 }
564                                         }
565                                 }
566                         }
567                 }
568                 delete list;
569         }
570         
571         return 0;
572 }
573 //_______________________________________________________________
574
575 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, AliGRPObject* grpObj)
576 {
577
578         //
579         // processing DCS DPs
580         //
581
582         Int_t entries = 0;  // counting the entries that are in the DCS DB, not taking care whether they have values or not
583         Int_t nLHCEntries = 0;
584         Int_t nL3Entries = 0;
585         Int_t nDipoleEntries = 0;
586         Int_t nEnvEntries = 0;
587         Int_t nHallProbesEntries = 0;
588         nLHCEntries = ProcessLHCDPs(valueMap, grpObj);
589         nL3Entries = ProcessL3DPs(valueMap, grpObj);
590         nDipoleEntries = ProcessDipoleDPs(valueMap, grpObj);
591         nEnvEntries = ProcessEnvDPs(valueMap, grpObj);
592         nHallProbesEntries = ProcessHPDPs(valueMap, grpObj);
593
594         entries = nLHCEntries + nL3Entries + nDipoleEntries + nEnvEntries + nHallProbesEntries;
595         return entries;
596
597 }
598
599 //_______________________________________________________________
600
601 Int_t AliGRPPreprocessor::ProcessL3DPs(const TMap* valueMap, AliGRPObject* grpObj)
602 {
603
604         // processing DPs
605         // related to 
606         // L3 info
607
608         Int_t nL3Entries = 0;
609         TObjArray *array = 0x0;
610         Int_t indexDP = -1;
611
612         AliInfo(Form("==========L3Polarity==========="));
613         indexDP = kL3Polarity;
614         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
615         if(!array) {
616                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
617         } 
618         else {
619                 if (array->GetEntries() == 0){
620                         AliError(Form("No entries found in array! setting %s Polarity to invalid...",fgkDCSDataPoints[indexDP]));
621                 }
622                 else {
623                         Char_t charDCS = ProcessBool(array);
624                         grpObj->SetL3Polarity(charDCS);
625                         AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetL3Polarity())));
626                 }
627                 nL3Entries++;
628         }
629
630         if (array) array = 0x0;
631
632         AliInfo(Form("==========L3Current==========="));
633         indexDP = kL3Current;
634         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
635         if(!array) {
636                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
637         } 
638         else {
639                 if (array->GetEntries() == 0){
640                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
641                 }
642                 else {
643                         Float_t *floatDCS = ProcessFloatAll(array);
644                         grpObj->SetL3Current(floatDCS);
645                         delete floatDCS;
646                 }
647                 nL3Entries++;
648         }
649
650         return nL3Entries;
651
652 }
653 //_______________________________________________________________
654
655 Int_t AliGRPPreprocessor::ProcessDipoleDPs(const TMap* valueMap, AliGRPObject* grpObj)
656 {
657         // processing DPs
658         // related to 
659         // the Dipole info
660
661         Int_t nDipoleEntries = 0;
662         TObjArray *array = 0x0;
663         Int_t indexDP = -1;
664
665         AliInfo(Form("==========DipolePolarity==========="));
666         indexDP = kDipolePolarity;
667         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
668         if(!array) {
669                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
670         } 
671         else {
672                 if (array->GetEntries() == 0){
673                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
674                 }
675                 else {
676                         Char_t charDCS = ProcessBool(array);
677                         grpObj->SetDipolePolarity(charDCS);
678                         AliInfo(Form("%s set to %d",fgkDCSDataPoints[indexDP],(Int_t)(grpObj->GetDipolePolarity())));
679                 }
680                 nDipoleEntries++;
681         }
682
683         if (array) array = 0x0;
684
685         AliInfo(Form("==========DipoleCurrent==========="));
686         indexDP = kDipoleCurrent;
687         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
688         if(!array) {
689                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
690         } 
691         else {
692                 if (array->GetEntries() == 0){
693                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
694                 }
695                 else {
696                         Float_t *floatDCS = ProcessFloatAll(array);
697                         grpObj->SetDipoleCurrent(floatDCS);
698                         delete floatDCS;
699                 }
700                 nDipoleEntries++;
701         }
702
703         return nDipoleEntries;
704
705 }
706 //_______________________________________________________________
707
708 Int_t AliGRPPreprocessor::ProcessEnvDPs(TMap* valueMap, AliGRPObject* grpObj)
709 {
710         // processing DPs
711         // related to 
712         // evironment conditions (temperature, pressure) info
713
714         Int_t nEnvEntries = 0;
715         TObjArray *array = 0x0;
716         Int_t indexDP = -1;
717
718         AliInfo(Form("==========CavernTemperature==========="));
719         indexDP = kCavernTemperature;
720         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
721         if(!array) {
722                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
723         } 
724         else {
725                 if (array->GetEntries() == 0){
726                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
727                 }
728                 else {
729                         Float_t *floatDCS = ProcessFloatAll(array);
730                         grpObj->SetCavernTemperature(floatDCS);
731                         delete floatDCS;
732                 }
733                 nEnvEntries++;
734         }
735
736         if (array) array = 0x0;
737
738         AliInfo(Form("==========AtmosPressures (Cavern + Surface)==========="));
739         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
740         dcsSensorArray->Print();
741         if( fPressure->NumFits()==0 ) {
742                 Log("Problem with the pressure sensor values!!!");
743         } 
744         else {
745                 AliInfo(Form("==========CavernAtmosPressure==========="));
746                 indexDP = kCavernAtmosPressure;
747                 AliDCSSensor* sensorCavernP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
748                 AliDebug(2,Form("sensorCavernP2 = %p", sensorCavernP2));
749                 if( sensorCavernP2->GetFit() ) {
750                         Log(Form("<%s> for run %d: Sensor Fit found",fgkDCSDataPoints[indexDP], fRun));
751                         grpObj->SetCavernAtmosPressure(sensorCavernP2);
752                         nEnvEntries++;
753                 } 
754                 //if (sensorP2) delete sensorP2;
755                 else {
756                         Log(Form("ERROR Sensor Fit for %s not found: ", fgkDCSDataPoints[indexDP] ));
757                 }
758                 AliInfo(Form("==========SurfaceAtmosPressure==========="));
759                 indexDP = kSurfaceAtmosPressure;
760                 AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
761                 AliDebug(2,Form("sensorP2 = %p", sensorP2));
762                 if( sensorP2->GetFit() ) {
763                         Log(Form("<%s> for run %d: Sensor Fit found",fgkDCSDataPoints[indexDP], fRun));
764                         grpObj->SetSurfaceAtmosPressure(sensorP2);
765                         nEnvEntries++;
766                 } 
767                 //if (sensorP2) delete sensorP2;
768                 else {
769                         Log(Form("ERROR Sensor Fit for %s not found: ", fgkDCSDataPoints[indexDP] ));
770                 }
771                 
772         }
773
774         /*      indexDP = kCavernAtmosPressure;
775         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
776         if(!array) {
777                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
778         } 
779         else {
780                 Float_t *floatDCS = ProcessFloatAll(array);
781                 grpObj->SetCavernAtmosPressure(floatDCS);
782                 delete floatDCS;
783                 nEnvEntries++;
784         }
785         
786
787         if (array) array = 0x0;
788
789         AliInfo(Form("==========SurfaceAtmosPressure==========="));
790         indexDP = kSurfaceAtmosPressure;
791         AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
792         dcsSensorArray->Print();
793         AliInfo(Form("fPressure = %p",fPressure));
794         AliInfo(Form("dcsSensorArray = %p",dcsSensorArray));
795         if( fPressure->NumFits()==0 ) {
796                 Log("Problem with the pressure sensor values!!!");
797         } 
798         else {
799                 AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[indexDP]);
800                 AliDebug(2,Form("sensorP2 = %p", sensorP2));
801                 if( sensorP2->GetFit() ) {
802                         Log(Form("<%s> for run %d: Sensor Fit found",fgkDCSDataPoints[indexDP], fRun));
803                         grpObj->SetSurfaceAtmosPressure(sensorP2);
804                         nEnvEntries++;
805                 } 
806                 //if (sensorP2) delete sensorP2;
807                 else {
808                         Log(Form("ERROR Sensor Fit for %s not found: ", fgkDCSDataPoints[indexDP] ));
809                 }
810                 
811         }
812         */
813
814         return nEnvEntries;
815 }
816 //_______________________________________________________________
817
818 Int_t AliGRPPreprocessor::ProcessHPDPs(const TMap* valueMap, AliGRPObject* grpObj)
819 {
820         // processing DPs
821         // related to 
822         // Hall Probes info
823
824         Int_t nHPEntries = 0;
825         TObjArray *array = 0x0;
826         Int_t indexDP = -1;
827
828         if (fgknDCSDPHallProbes != AliGRPObject::GetNumberOfHP()){
829                 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()));
830         }
831         for (indexDP = 0; indexDP < AliGRPObject::GetNumberOfHP(); indexDP++){
832                 AliInfo(Form("==========%s===========",AliGRPObject::GetHPDP(indexDP)));
833                 array = (TObjArray *)valueMap->GetValue(AliGRPObject::GetHPDP(indexDP));
834                 if(!array) {
835                         Log(Form("%s not found in the map!!!",AliGRPObject::GetHPDP(indexDP)));
836                 } 
837                 else {
838                         if (array->GetEntries() == 0){
839                                 AliError(Form("No entries found in array! setting %s to invalid...",AliGRPObject::GetHPDP(indexDP)));
840                         }
841                         else {
842                                 Float_t *floatDCS = ProcessFloatAll(array);
843                                 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])); 
844                                 grpObj->SetHallProbes((AliGRPObject::DP_HallProbes)indexDP,floatDCS);
845                                 for (Int_t kk = 0 ; kk< 5; kk++){
846                                         AliDebug(2,Form("HallProbe[%d][%d]=%f",indexDP,kk,grpObj->GetHallProbes((AliGRPObject::DP_HallProbes)indexDP,(AliGRPObject::Stats)kk)));
847                                 }
848                                 delete floatDCS;
849                         }
850                         nHPEntries++;
851                 }
852         }
853                 
854         Log(Form("Hall Probes = %d ", nHPEntries));
855         return nHPEntries;
856 }
857
858 //_______________________________________________________________
859
860 Int_t AliGRPPreprocessor::ProcessLHCDPs(const TMap* valueMap, AliGRPObject* grpObj)
861 {
862
863         //
864         // processing of LHC related DCS DPs, i.e.:
865         // LHCState
866         // LHCLuminosity
867         // BeamIntensity
868         //
869
870         Int_t nLHCEntries = 0;
871         TObjArray *array = 0x0;
872         Int_t indexDP = -1;
873
874         AliInfo(Form("==========LHCState==========="));
875         indexDP = kLHCState;
876         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
877         if(!array) {
878                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
879         } 
880         else {
881                 if (array->GetEntries() == 0){
882                         AliError(Form("No entries found in array! setting %s to invalid...",fgkDCSDataPoints[indexDP]));
883                 }
884                 else {
885                         TString stringDCS = ProcessChar(array);
886                         if (stringDCS.Length()!=0) {
887                                 Bool_t found = kFALSE;
888                                 for( Int_t i=0; i<20; i+=2 ) {
889                                         if( stringDCS.CompareTo(fgkLHCState[i]) == 0 ) {
890                                                 stringDCS = fgkLHCState[i+1];
891                                                 found = kTRUE;
892                                                 break;
893                                         }
894                                 }
895                                 if (found){
896                                         Log(Form("<%s> for run %d: %s",fgkDCSDataPoints[indexDP],fRun, stringDCS.Data()));
897                                         grpObj->SetLHCState(stringDCS);
898                                 }
899                                 else{
900                                         Log(Form("%s values found not valid!",fgkDCSDataPoints[indexDP]));
901                                         grpObj->SetLHCState(AliGRPObject::GetInvalidString());
902                                 } 
903                         }
904                         else {
905                                 Log(Form("%s not valid (null length), string set as invalid!",fgkDCSDataPoints[indexDP]));
906                                 grpObj->SetLHCState(AliGRPObject::GetInvalidString());
907                         }         
908                 }
909                 nLHCEntries++;
910         }
911         
912         if (array) array = 0x0;
913
914         AliInfo(Form("==========LHCLuminosity==========="));
915         indexDP = kLHCLuminosity;
916         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
917         if(!array) {
918                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
919         } 
920         else {
921                 if (array->GetEntries() == 0){
922                         AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
923                 }
924                 else {
925                         Float_t *floatDCS = ProcessFloatAll(array);
926                         grpObj->SetLHCLuminosity(floatDCS);
927                         delete floatDCS;
928                         AliSplineFit* splfit = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
929                         grpObj->SetLHCLuminositySplineFit(splfit);
930                         //              delete splfit;
931                 }
932                 nLHCEntries++;
933         }
934
935         if (array) array = 0x0;
936
937         AliInfo(Form("==========BeamIntensity==========="));
938         indexDP = kBeamIntensity;
939         array = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[indexDP]);
940         if(!array) {
941                 Log(Form("%s not found in the map!!!",fgkDCSDataPoints[indexDP]));
942         } 
943         else {
944                 if (array->GetEntries() == 0){
945                         AliError(Form("No entries found in array! setting %s and its Spline Fit to invalid...",fgkDCSDataPoints[indexDP]));
946                 }
947                 else {
948                         Float_t *floatDCS = ProcessFloatAll(array);
949                         grpObj->SetBeamIntensity(floatDCS);
950                         delete floatDCS;
951                         AliSplineFit* splfit1 = GetSplineFit(array,fgkDCSDataPoints[indexDP]);
952                         grpObj->SetBeamIntensitySplineFit(splfit1);
953                         //delete splfit;
954                 }
955                 nLHCEntries++;
956         }
957
958         return nLHCEntries;
959 }
960 //_________________________________________________________________________
961
962 AliSplineFit* AliGRPPreprocessor::GetSplineFit(const TObjArray *array, const TString& stringID){
963
964
965         // 
966         // returning Spline Fit 
967         // 
968
969         Int_t entriesarray = array->GetEntries();
970         Float_t* value = new Float_t[entriesarray];
971         Float_t* time = new Float_t[entriesarray];
972         AliDCSValue* v = 0x0;
973         for (Int_t iarray = 0; iarray < entriesarray; iarray++){
974                 v = (AliDCSValue*)array->At(iarray);
975                 value[iarray] = v->GetFloat();
976                 time[iarray] = v->GetTimeStamp();
977                 AliInfo(Form("iarray = %d, value = %f, time = %f",iarray,value[iarray],time[iarray]));
978         }
979         TGraph* gr = new TGraph(entriesarray,value,time);
980         if (!gr ) {
981                 AliWarning(Form("%s: no input graph to compute SplineFit",stringID.Data()));
982                 return NULL;
983         }
984         AliSplineFit *fit = new AliSplineFit();
985         fit->SetMinPoints(10);
986         fit->InitKnots(gr,10,10,0.0);
987         fit->SplineFit(2);
988         fit->Cleanup();
989         if (!fit) {
990                 AliWarning(Form("%s: no fit performed",stringID.Data()));
991                 return NULL;
992         } 
993         return fit;
994 }
995
996 //_________________________________________________________________________
997
998 TString AliGRPPreprocessor::ProcessChar(const TObjArray *array)
999 {
1000
1001         // 
1002         // processing char
1003         //
1004
1005         TString aDCSString="";
1006         
1007         AliDCSValue *v = 0x0;
1008         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1009                 v = (AliDCSValue *)array->At(iCount);
1010                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1011                         AliError(Form("DCS values for the parameter outside the queried interval"));
1012                         continue;
1013                 }
1014                 if (iCount > 0) {
1015                         if (aDCSString != v->GetChar())
1016                         AliError(Form("DCS values for the parameter changed from %s to %c within the queried interval", aDCSString.Data(), (Char_t)v->GetChar()));
1017                 }
1018                 aDCSString = (TString)v->GetChar();  // keeping always last value in the array
1019         }
1020         return aDCSString;
1021 }
1022
1023 //__________________________________________________________________________________________________________________
1024
1025 Float_t* AliGRPPreprocessor::ProcessFloatAll(const TObjArray* array)
1026 {
1027         // 
1028         // processing Float values using Mean, Median, Standard Deviation wrt Mean, Standar Deviation wrt Median 
1029         //
1030         // parameters[0] = mean
1031         // parameters[1] = truncated mean (calculated excluding points outside +/- 3RMS from mean
1032         // parameters[2] = median
1033         // parameters[3] = standard deviation wrt mean
1034         // parameters[4] = standard deviation wrt median
1035         //
1036
1037         Float_t* parameters = new Float_t[5];
1038         Double_t aDCSArrayMean = 0;     // Mean
1039         Double_t aDCSArrayTruncMean = 0;// Truncated Mean
1040         Double_t aDCSArrayMedian = 0;   // Median
1041         Double_t aDCSArraySDMean = 0;   // Standard Deviation wrt Mean
1042         Double_t aDCSArraySDMedian = 0; // Standard Deviation wrt Median
1043         Float_t aDCSArraySum = 0.0;
1044         Int_t iCounts = 0;
1045         Int_t iCounts1 = 0;
1046         Float_t temp = 0;
1047         Float_t temp1 = 0;
1048         Int_t nCounts = array->GetEntries();
1049         Float_t *tempArray = new Float_t[nCounts];
1050         for(Int_t i = 0; i < nCounts; i++) {
1051                 AliDCSValue *v = (AliDCSValue *)array->At(i);
1052                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1053                         aDCSArraySum += v->GetFloat();
1054                         tempArray[i] = v->GetFloat();
1055                         AliDebug(2,Form("%d-th entry = %f",i,tempArray[i]));
1056                         iCounts += 1;
1057                 }
1058         }
1059
1060         AliDebug(2,Form("Using %i entries, starting from %i entries",iCounts,nCounts));
1061         if(iCounts != 0) {
1062                 aDCSArrayMean = TMath::Mean(iCounts,tempArray);
1063                 aDCSArrayMedian = TMath::Median(iCounts,tempArray);
1064                 aDCSArraySDMean = TMath::RMS(iCounts,tempArray);
1065                 AliDebug(2,Form("SD = %f",aDCSArraySDMean));
1066                 // computing standard deviation wrt median
1067                 AliDebug(2,Form("maximum = %f, minimum = %f", aDCSArrayMean+3*aDCSArraySDMean, aDCSArrayMean-3*aDCSArraySDMean));
1068                 for (Int_t i = 0; i < iCounts; i++){
1069                         AliDCSValue *v = (AliDCSValue *)array->At(i);
1070                         AliDebug(2,Form("maximum = %f, minimum = %f", aDCSArrayMean+3*aDCSArraySDMean, aDCSArrayMean-3*aDCSArraySDMean));
1071                         AliDebug(2,Form("%i-th entry = %f",i, v->GetFloat())); 
1072                         if ((v->GetFloat()<=aDCSArrayMean+3*aDCSArraySDMean) && (v->GetFloat()>=aDCSArrayMean-3*aDCSArraySDMean)){
1073                                 temp1+=v->GetFloat();
1074                                 iCounts1++;
1075                                 AliDebug(2,Form("temp1 = %f, iCounts1 = %i",temp1,iCounts1));
1076                         }
1077                         temp += (v->GetFloat()-aDCSArrayMedian)*(v->GetFloat()-aDCSArrayMedian);
1078                 }
1079                 AliDebug(3,Form("temp before the ratio = %f, with %d counts", temp, iCounts));
1080                 temp/=iCounts;
1081                 AliDebug(3,Form("temp after the ratio = %f", temp));
1082                 if (temp>0) {
1083                         aDCSArraySDMedian = TMath::Sqrt(temp);
1084                 }
1085                 else if (temp==0) {
1086                         AliInfo(Form("Radical = 0 in computing standard deviation wrt median! Setting it to zero...."));
1087                         aDCSArraySDMedian = 0;
1088                 }
1089                 else{
1090                         AliError(Form("Radical < 0 in computing standard deviation! Setting it to invalid...."));
1091                         aDCSArraySDMedian = AliGRPObject::GetInvalidFloat();
1092                 }
1093         }
1094         else {
1095                 aDCSArrayMean = AliGRPObject::GetInvalidFloat();
1096                 aDCSArrayMedian = AliGRPObject::GetInvalidFloat();
1097                 aDCSArraySDMean = AliGRPObject::GetInvalidFloat();
1098         }
1099         AliDebug(2,Form("iCounts1 = %d and temp1 = %f",iCounts1, temp1));
1100         if (iCounts1 > 0) {
1101                 aDCSArrayTruncMean = temp1/iCounts1;
1102         }
1103         else{
1104                 aDCSArrayTruncMean = AliGRPObject::GetInvalidFloat();
1105         }
1106         
1107         
1108         
1109         AliDebug(2,Form("mean within %d counts = %f ",iCounts,aDCSArrayMean));
1110         AliDebug(2,Form("truncated mean within %d counts = %f (%i values used)",iCounts,aDCSArrayTruncMean,iCounts1));
1111         AliDebug(2,Form("median within %d counts = %f ",iCounts,aDCSArrayMedian));
1112         AliDebug(2,Form("standard deviation with mean within %d counts = %f ",iCounts,aDCSArraySDMean));
1113         AliDebug(2,Form("standard deviation with median within %d counts = %f ",iCounts,aDCSArraySDMedian));
1114         
1115         parameters[0] = aDCSArrayMean;
1116         parameters[1] = aDCSArrayTruncMean;
1117         parameters[2] = aDCSArrayMedian;
1118         parameters[3] = aDCSArraySDMean;
1119         parameters[4] = aDCSArraySDMedian;
1120
1121         //      AliDebug(2,Form("mean = %f, truncated mean = %f, median = %f, SD wrt mean = %f, SD wrt median = %f ",parameters[0],parameters[1],parameters[2],parameters[3],parameters[4]));
1122         AliInfo(Form("mean = %f, truncated mean = %f, median = %f, SD wrt mean = %f, SD wrt median = %f ",parameters[0],parameters[1],parameters[2],parameters[3],parameters[4]));
1123
1124         return parameters;
1125 }
1126
1127
1128 //_______________________________________________________________
1129
1130 Char_t AliGRPPreprocessor::ProcessBool(const TObjArray* array)
1131 {
1132         // 
1133         // processing Boolean values
1134         //
1135
1136         Bool_t aDCSBool = kTRUE;
1137
1138         AliDCSValue *v = 0x0;
1139
1140         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1141                 v = (AliDCSValue *)array->At(iCount);
1142                 if (((Int_t)(v->GetTimeStamp()) < (Int_t)GetStartTimeDCSQuery()) || ((Int_t)(v->GetTimeStamp()) > (Int_t)GetEndTimeDCSQuery())) {
1143                         AliError(Form("DCS values for the parameter outside the queried interval"));
1144                         continue;
1145                 }
1146                 if (iCount > 0) {
1147                         if (aDCSBool != v->GetBool())
1148                         AliError(Form("DCS values for the parameter changed from %d to %d within the queried interval", (UInt_t)aDCSBool, (UInt_t)v->GetBool()));
1149                 }
1150                 aDCSBool = v->GetBool(); // always keeping last value
1151                 AliDebug(2,Form("Bool = %d",(Int_t)aDCSBool));
1152         }
1153         
1154         Char_t caDCSBool = (Char_t) aDCSBool;
1155         return caDCSBool;
1156         
1157 }
1158
1159 //_______________________________________________________________
1160
1161 Float_t AliGRPPreprocessor::ProcessInt(const TObjArray* array)
1162 {
1163         // 
1164         // processing Int values, returning mean
1165         //
1166
1167         Float_t aDCSArraySum = 0.0;
1168         Float_t aDCSArrayMean = 0.0;
1169         Int_t iCounts = 0;
1170         AliDCSValue* v = 0x0;
1171
1172         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1173                 v = (AliDCSValue *)array->At(iCount);
1174                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1175                         aDCSArraySum += v->GetInt();
1176                         iCounts += 1;
1177                 }
1178         }
1179
1180         if(iCounts != 0) aDCSArrayMean = aDCSArraySum/iCounts;
1181         else aDCSArrayMean = AliGRPObject::GetInvalidFloat();
1182         
1183         return aDCSArrayMean;
1184
1185 }
1186 //_______________________________________________________________
1187
1188 Float_t AliGRPPreprocessor::ProcessUInt(const TObjArray* array)
1189 {
1190         // 
1191         // processing Int values, returning mean
1192         //
1193
1194         Float_t aDCSArraySum = 0.0;
1195         Float_t aDCSArrayMean = 0.0;
1196         Int_t iCounts = 0;
1197         AliDCSValue* v = 0x0;
1198
1199         for(Int_t iCount = 0; iCount < array->GetEntries(); iCount++) {
1200                 v = (AliDCSValue *)array->At(iCount);
1201                 if(((Int_t)(v->GetTimeStamp()) >= (Int_t)GetStartTimeDCSQuery()) &&((Int_t)(v->GetTimeStamp()) <= (Int_t)GetEndTimeDCSQuery())) {
1202                         aDCSArraySum += v->GetUInt();
1203                         iCounts += 1;
1204                 }
1205         }
1206
1207         if(iCounts != 0) aDCSArrayMean = aDCSArraySum/iCounts;
1208         else aDCSArrayMean = AliGRPObject::GetInvalidFloat();
1209         
1210         return aDCSArrayMean;
1211
1212 }
1213
1214
1215 //_______________________________________________________________
1216
1217 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
1218 {
1219         // extract DCS pressure maps. Perform fits to save space
1220         
1221         TMap *map = fPressure->ExtractDCS(dcsAliasMap);
1222         if (map) {
1223                 fPressure->MakeSplineFit(map);
1224                 Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
1225                 if (fitFraction > kFitFraction ) {
1226                         AliInfo(Form("Pressure values extracted, %d fits performed.", fPressure->NumFits()));
1227                 } else { 
1228                         AliInfo("Too few pressure maps fitted!!!");
1229                 }
1230         } else {
1231                 AliInfo("no atmospheric pressure map extracted!!!");
1232         }
1233         delete map;
1234         
1235         return fPressure;
1236 }
1237
1238
1239   
1240 //_______________________________________________________________
1241 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)
1242 {
1243         //
1244         // Retrieves logbook and trigger information from the online logbook
1245         // This information is needed for prompt reconstruction
1246         //
1247         // Parameters are:
1248         // Run number
1249         // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
1250         // cdbRoot
1251         //
1252         // returns:
1253         //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
1254         //         0 on success and no run was found
1255         //         negative on error
1256         //
1257         // This function is NOT called during the preprocessor run in the Shuttle!
1258         //
1259         
1260         // defaults
1261         if (dbPort == 0)
1262                 dbPort = 3306;
1263         
1264         // CDB connection
1265         AliCDBManager* cdb = AliCDBManager::Instance();
1266         cdb->SetDefaultStorage(cdbRoot);
1267         
1268         // SQL connection
1269         TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
1270         
1271         if (!server)
1272                 {
1273                         Printf("ERROR: Could not connect to DAQ LB");
1274                         return -1;
1275                 }
1276         
1277         // main logbook
1278         TString sqlQuery;
1279         sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask, L3_magnetCurrent, Dipole_magnetCurrent FROM logbook WHERE run = %d", run);
1280         TSQLResult* result = server->Query(sqlQuery);
1281         if (!result)
1282                 {
1283                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
1284                         return -2;
1285                 }
1286         
1287         if (result->GetRowCount() == 0)
1288                 {
1289                         Printf("ERROR: Run %d not found", run);
1290                         delete result;
1291                         return -3;
1292                 }
1293         
1294         TSQLRow* row = result->Next();
1295         if (!row)
1296                 {
1297                         Printf("ERROR: Could not receive data from run %d", run);
1298                         delete result;
1299                         return -4;
1300                 }
1301         
1302         TString timeStartString(row->GetField(0));
1303         TString runType(row->GetField(1));
1304         TString detectorMaskString(row->GetField(2));
1305         TString l3CurrentString(row->GetField(3));
1306         TString dipoleCurrentString(row->GetField(4));
1307         time_t timeStart = (time_t)(timeStartString.Atoi());
1308         UInt_t detectorMask = (UInt_t)(detectorMaskString.Atoi());
1309         Float_t l3Current = (Float_t)(TMath::Abs(l3CurrentString.Atof()));
1310         Float_t dipoleCurrent = (Float_t)(TMath::Abs(dipoleCurrentString.Atof()));
1311         
1312         AliGRPObject * grpObj = new AliGRPObject();
1313         grpObj->SetTimeStart(timeStart); 
1314         grpObj->SetRunType((TString)(row->GetField(1)));
1315         grpObj->SetDetectorMask(detectorMask);
1316         grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
1317         grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);
1318
1319         delete row;
1320         row = 0;
1321         
1322         delete result;
1323         result = 0;
1324         
1325         Printf("Storing GRP/GRP/Data object with the following content");
1326         grpObj->Dump();
1327         
1328         AliCDBMetaData metadata;
1329         metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus & Chiara Zampolli");
1330         metadata.SetComment("GRP Output parameters received during online running");
1331         
1332         AliCDBId id("GRP/GRP/Data", run, run);
1333         Bool_t success = cdb->Put(grpObj, id, &metadata);
1334         
1335         delete grpObj;
1336         
1337         if (!success)
1338                 {
1339                         Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
1340                         return -5;
1341                 }
1342         
1343         // Receive trigger information
1344         sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
1345         result = server->Query(sqlQuery);
1346         if (!result)
1347                 {
1348                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
1349                         return -11;
1350                 }
1351         
1352         if (result->GetRowCount() == 0)
1353                 {
1354                         Printf("ERROR: Run %d not found in logbook_trigger_config", run);
1355                         delete result;
1356                         return -12;
1357                 }
1358         
1359         row = result->Next();
1360         if (!row)
1361                 {
1362                         Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
1363                         delete result;
1364                         return -13;
1365                 }
1366         
1367         TString triggerConfig(row->GetField(0));
1368         
1369         delete row;
1370         row = 0;
1371         
1372         delete result;
1373         result = 0;
1374         
1375         Printf("Found trigger configuration: %s", triggerConfig.Data());
1376         
1377         AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
1378         if (!runcfg)
1379                 {
1380                         Printf("ERROR: Could not create CTP configuration object");
1381                         return -14;
1382                 }
1383         
1384         metadata.SetComment("CTP run configuration received during online running");
1385         
1386         AliCDBId id2("GRP/CTP/Config", run, run);
1387         success = cdb->Put(runcfg, id2, &metadata);
1388         
1389         delete runcfg;
1390         runcfg = 0;
1391         
1392         if (!success)
1393                 {
1394                         Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
1395                         return -15;
1396                 }
1397         
1398         // get last run with same run type that was already processed by the SHUTTLE
1399         
1400         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());
1401         result = server->Query(sqlQuery);
1402         if (!result)
1403                 {
1404                         Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
1405                         return -21;
1406                 }
1407         
1408         if (result->GetRowCount() == 0)
1409                 {
1410                         Printf("ERROR: No result with query <%s>", sqlQuery.Data());
1411                         delete result;
1412                         return -22;
1413                 }
1414         
1415         row = result->Next();
1416         if (!row)
1417                 {
1418                         Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
1419                         delete result;
1420                         return -23;
1421                 }
1422         
1423         TString lastRunStr(row->GetField(0));
1424         Int_t lastRun = lastRunStr.Atoi();
1425         
1426         Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
1427         
1428         delete row;
1429         row = 0;
1430         
1431         delete result;
1432         result = 0;
1433         
1434         server->Close();
1435         delete server;
1436         server = 0;
1437         
1438         return lastRun;
1439 }