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