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