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