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