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