]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliGRPPreprocessor.cxx
correct misleading comment re. APD temperature stability
[u/mrichter/AliRoot.git] / STEER / AliGRPPreprocessor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //                          Class AliGRPPreprocessor
20 //                  Global Run Parameters (GRP) preprocessor
21 //    Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
22 //    Modified: Ernesto.Lopez.Torres@cern.ch  CEADEN-CERN
23 //-------------------------------------------------------------------------
24
25 #include <TChain.h>
26 #include <TList.h>
27 #include <TMap.h>
28 #include <TObjString.h>
29 #include <TTimeStamp.h>
30 #include <TSystem.h>
31 #include <TFile.h>
32
33 #include "AliGRPPreprocessor.h"
34 #include "AliGRPDCS.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 #include <TH1.h>
47
48 // needed for ReceivePromptRecoParameters
49 #include <TSQLServer.h>
50 #include <TSQLResult.h>
51 #include <TSQLRow.h>
52 #include <AliCDBManager.h>
53 #include <AliCDBMetaData.h>
54 #include <AliCDBId.h>
55 #include <AliTriggerConfiguration.h>
56
57 const Double_t kFitFraction = 0.7;                 // Fraction of DCS sensor fits required
58
59 ClassImp(AliGRPPreprocessor)
60
61 //_______________________________________________________________
62   const Int_t AliGRPPreprocessor::fgknDAQLbPar = 8; // num parameters in the logbook
63   const Int_t AliGRPPreprocessor::fgknDCSDP = 10;   // number of dcs dps
64   const char* AliGRPPreprocessor::fgkDCSDataPoints[AliGRPPreprocessor::fgknDCSDP] = {
65                    "LHCState",              // missing in DCS
66                    "L3Polarity",
67                    "DipolePolarity",
68                    "LHCLuminosity",         // missing in DCS
69                    "BeamIntensity",         // missing in DCS
70                    "L3Current",
71                    "DipoleCurrent",
72                    "CavernTemperature",
73                    "CavernAtmosPressure",
74                    "SurfaceAtmosPressure"
75                  };
76                  
77   const Short_t kSensors = 9; // start index position of sensor in DCS DPs
78   const Short_t kNumSensors = 1; // Number of sensors in DCS DPs
79
80   const char* AliGRPPreprocessor::fgkLHCState[20] = {
81                    "P", "PREPARE",
82                    "J", "PREINJECTION",
83                    "I", "INJECTION",
84                    "F", "FILLING",
85                    "A", "ADJUST",
86                    "U", "UNSTABLE BEAMS",
87                    "S", "STABLE BEAMS",
88                    "D", "BEAM DUMP",
89                    "R", "RECOVER",
90                    "C", "PRECYCLE"
91                  };
92
93   const char* kppError[] = {
94                    "",
95                    "(DAQ logbook ERROR)",
96                    "(DAQ FXS ERROR)",
97                    "(DCS FXS ERROR)",
98                    "(DCS data points ERROR)",
99                    "(Trigger Configuration ERROR)"
100   };
101
102 //_______________________________________________________________
103 AliGRPPreprocessor::AliGRPPreprocessor(AliShuttleInterface* shuttle):
104         AliPreprocessor("GRP",shuttle),  fPressure(0)
105 {
106   // constructor - shuttle must be instantiated!
107
108   AddRunType("PHYSICS");
109 }
110
111 //_______________________________________________________________
112 AliGRPPreprocessor::~AliGRPPreprocessor()
113 {
114   //destructor
115
116   delete fPressure;
117 }
118
119 //_______________________________________________________________
120 void AliGRPPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
121 {
122   // Initialize preprocessor
123
124   AliPreprocessor::Initialize(run, startTime, endTime);
125
126   AliInfo("Initialization of the GRP preprocessor.");
127   TClonesArray * array = new TClonesArray("AliDCSSensor",kNumSensors); 
128   for(Int_t j = 0; j < kNumSensors; j++) {
129     AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
130     sens->SetStringID(fgkDCSDataPoints[j+kSensors]);
131   }
132   AliInfo(Form("Pressure Entries: %d",array->GetEntries()));
133
134   fPressure = new AliDCSSensorArray(fStartTime, fEndTime, array);
135 }
136
137 //_______________________________________________________________
138 UInt_t AliGRPPreprocessor::Process(TMap* valueMap)
139 {
140   // process data retrieved by the Shuttle
141   
142   //=================//
143   // DAQ logbook     //
144   //=================//
145   UInt_t error = 0;
146   
147   TMap *grpmap = ProcessDaqLB();
148   if( grpmap->GetEntries() == fgknDAQLbPar ) {
149     Log(Form("DAQ logbook, successful!"));
150   } else {
151     Log(Form("DAQ logbook, missing parameters!!!"));
152     error |= 1;
153   }
154   //=================//
155   // DAQ FXS         //
156   //=================//
157   UInt_t iDaqFxs = ProcessDaqFxs();
158   if( iDaqFxs == 0 ) {
159     Log(Form("DAQ FXS, successful!"));
160   } else {
161     Log(Form("DAQ FXS, could not store run raw tag file!!!"));
162     error |= 2;
163   }
164   
165   //=================//
166   // DCS FXS         //
167   //=================//
168   UInt_t iDcsFxs = ProcessDcsFxs();
169   if( iDcsFxs == 0 ) {
170      Log(Form("DCS FXS, successful!"));
171   } else {
172      Log(Form("DCS FXS, Could not store CTP run configuration and scalers!!!"));
173     error |= 4;
174   }
175   
176   //=================//
177   // DCS data points //
178   //=================//
179   Int_t entries = ProcessDcsDPs( valueMap, grpmap );
180   if( entries < fgknDCSDP-3 ) { // FIXME (!= ) LHState and pressure map are not working yet...
181     Log(Form("Problem with the DCS data points!!!"));
182     error |= 8;
183   } else  Log(Form("DCS data points, successful!"));
184
185   //=======================//
186   // Trigger Configuration //
187   //=======================//
188   // either from DAQ logbook.....
189   const char * triggerConf = GetTriggerConfiguration();
190   if (triggerConf!= NULL) {
191     Log("Found trigger configuration in DAQ logbook");
192     AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConf);          
193     if (!runcfg) {
194       Log("Bad CTP run configuration file from DAQ logbook! The corresponding CDB entry will not be filled!");
195       error |= 16;
196     }
197     else {
198       TString titleCTPcfg = Form("CTP cfg for run %i from DAQ",fRun);
199       runcfg->SetTitle(titleCTPcfg);
200       AliCDBMetaData metaData;
201       metaData.SetBeamPeriod(0);
202       metaData.SetResponsible("Roman Lietava");
203       metaData.SetComment("CTP run configuration from DAQ logbook");
204       if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
205         Log("Unable to store the CTP run configuration object to OCDB!");
206         error |= 16;
207       }
208     }
209   }
210   // ...or from DCS FXS
211   else{
212      Log("No trigger configuration found in the DAQ logbook!! Trying reading from DCS FXS...");
213      TString runcfgfile = GetFile(kDCS, "CTP_runconfig", "");
214      if (runcfgfile.IsNull()) {
215        Log("No CTP runconfig files has been found in DCS FXS!");
216        error |= 16;
217      }
218      else {
219        Log(Form("File with Id CTP_runconfig found! Copied to %s",runcfgfile.Data()));
220        AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfiguration(runcfgfile);
221        if (!runcfg) {
222          Log("Bad CTP run configuration file from DCS FXS! The corresponding CDB entry will not be filled!");
223          error |= 16;;
224        }
225        else {
226          TString titleCTPcfg = Form("CTP cfg for run %i from DCS",fRun);
227          runcfg->SetTitle(titleCTPcfg);
228          AliCDBMetaData metaData;
229          metaData.SetBeamPeriod(0);
230          metaData.SetResponsible("Roman Lietava");
231          metaData.SetComment("CTP run configuration from DCS FXS");
232          if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
233            Log("Unable to store the CTP run configuration object to OCDB!");
234            error |= 16;
235          }
236        }
237      }
238   }
239
240   grpmap->SetOwner(1);
241   AliInfo(Form("Final list entries: %d",grpmap->GetEntries()));
242   
243   AliCDBMetaData md;
244   md.SetResponsible("Ernesto Lopez Torres");
245   md.SetComment("Output parameters from the GRP preprocessor.");
246   
247   Bool_t result = Store("GRP", "Data", grpmap, &md);
248   
249   delete grpmap;
250   
251   if (result && !error ) {
252     Log("GRP Preprocessor Success");
253     return 0;
254   } else {
255     Log( Form("GRP Preprocessor FAILS!!! %s%s%s%s%s",
256                                  kppError[(error&1)?1:0],
257                                  kppError[(error&2)?2:0],
258                                  kppError[(error&4)?3:0],
259                                  kppError[(error&8)?4:0],
260                                  kppError[(error&16)?5:0]
261                                   ));
262     return error;
263   }
264 }
265
266 //_______________________________________________________________
267 TMap *AliGRPPreprocessor::ProcessDaqLB()
268 {
269   //Getting the DAQ lb information
270   
271   const char* timeStart         = GetRunParameter("DAQ_time_start");
272   const char* timeEnd           = GetRunParameter("DAQ_time_end");
273   const char* beamEnergy        = GetRunParameter("beamEnergy");
274   const char* beamType          = GetRunParameter("beamType");
275   const char* numberOfDetectors = GetRunParameter("numberOfDetectors");
276   const char* detectorMask      = GetRunParameter("detectorMask");
277   const char* lhcPeriod         = GetRunParameter("LHCperiod");
278   
279   TMap *mapDAQ = new TMap();
280   
281   if (timeStart) {
282     Log(Form("Start time for run %d: %s",fRun, timeStart));
283   } else {
284     Log(Form("Start time not put in logbook!"));
285   }
286   mapDAQ->Add(new TObjString("fAliceStartTime"), new TObjString(timeStart));
287
288   if (timeEnd) {
289     Log(Form("End time for run %d: %s",fRun, timeEnd));
290   } else {
291     Log(Form("End time not put in logbook!"));
292   }
293   mapDAQ->Add(new TObjString("fAliceStopTime"), new TObjString(timeEnd));
294
295   if (beamEnergy) {
296     Log(Form("Beam energy for run %d: %s",fRun, beamEnergy));
297   } else {
298     Log(Form("Beam energy not put in logbook!"));
299   }
300   mapDAQ->Add(new TObjString("fAliceBeamEnergy"), new TObjString(beamEnergy));
301
302   if (beamType) {
303     Log(Form("Beam type for run %d: %s",fRun, beamType));
304   } else {
305     Log(Form("Beam type not put in logbook!"));
306   }
307   mapDAQ->Add(new TObjString("fAliceBeamType"), new TObjString(beamType));
308
309   if (numberOfDetectors) {
310     Log(Form("Number of active detectors for run %d: %s",fRun, numberOfDetectors));
311   } else {
312     Log(Form("Number of active detectors not put in logbook!"));
313   }
314   mapDAQ->Add(new TObjString("fNumberOfDetectors"), new TObjString(numberOfDetectors));
315
316   if (detectorMask) {
317     Log(Form("Detector mask for run %d: %s",fRun, detectorMask));
318   } else {
319     Log(Form("Detector mask not put in logbook!"));
320   }
321   mapDAQ->Add(new TObjString("fDetectorMask"), new TObjString(detectorMask));
322
323   if (lhcPeriod) {
324     Log(Form("LHC period (DAQ) for run %d: %s",fRun, lhcPeriod));
325   } else {
326     Log(Form("LHCperiod not put in logbook!"));
327   }
328   mapDAQ->Add(new TObjString("fLHCPeriod"), new TObjString(lhcPeriod));
329   
330   mapDAQ->Add(new TObjString("fRunType"), new TObjString(GetRunType()));
331   Log( Form("Retrived %d parameters from logbook", mapDAQ->GetEntries() ) );
332
333   return mapDAQ;
334 }
335
336 //_______________________________________________________________
337 UInt_t AliGRPPreprocessor::ProcessDaqFxs()
338 {
339   //======DAQ FXS======//
340
341   TList* list = GetFileSources(kDAQ);  
342   if (!list) {
343     Log("No raw data tag list: connection problems with DAQ FXS logbook!");
344     return 1;
345   }
346
347   if (list->GetEntries() == 0) {
348     Log("no raw data tags in this run: nothing to merge!");
349     delete  list; list=0;
350     return 0;
351   }
352
353   TChain *fRawTagChain = new TChain("T");
354   Int_t nFiles=0;
355   TIterator* iter = list->MakeIterator();
356   TObject* obj = 0;
357   while ((obj = iter->Next())) {
358     TObjString* objStr = dynamic_cast<TObjString*> (obj);
359     if (objStr) {
360       Log(Form("Found source %s", objStr->String().Data()));
361       TList* list2 = GetFileIDs(kDAQ, objStr->String());
362       if (!list2) {
363         Log("No list with ids from DAQ was found: connection problems with DAQ FXS logbook!");
364         delete fRawTagChain; fRawTagChain=0;
365         return 1;
366       }
367       Log(Form("Number of ids: %d",list2->GetEntries()));
368       for(Int_t i = 0; i < list2->GetEntries(); i++) {
369         TObjString *idStr = (TObjString *)list2->At(i);
370         TString fileName = GetFile(kDAQ,idStr->String().Data(),objStr->String().Data());
371         if (fileName.Length() > 0) {
372            Log(Form("Adding file in the chain: %s",fileName.Data()));
373            fRawTagChain->Add(fileName.Data());
374            nFiles++;
375         } else {
376            Log(Form("Could not retrieve file with id %s from source %s: "
377                     "connection problems with DAQ FXS!",
378                      idStr->String().Data(), objStr->String().Data()));
379            delete list; list=0;
380            delete list2; list2=0;
381            delete fRawTagChain; fRawTagChain=0;
382            return 2;
383         }
384       }
385       delete list2;
386     }
387   }
388   
389   TString fRawDataFileName = "GRP_Merged.tag.root";
390   Log(Form("Merging %d raw data tags into file: %s", nFiles, fRawDataFileName.Data()));
391   if( fRawTagChain->Merge(fRawDataFileName) < 1 ) {
392     Log("Error merging raw data files!!!");
393     return 3;
394   }
395   
396   TString outputfile = Form("Run%d.Merged.RAW.tag.root", fRun);
397   Bool_t result = StoreRunMetadataFile(fRawDataFileName.Data(),outputfile.Data());
398   
399   if (!result) {
400     Log("Problem storing raw data tags in local file!!!");
401   } else {
402     Log("Raw data tags merged successfully!!");
403   }
404   
405   delete iter;
406   delete list;
407   delete fRawTagChain; fRawTagChain=0;
408
409   if (result == kFALSE) {
410     return 4;
411   }
412
413   return 0;
414
415 }
416
417 //_______________________________________________________________
418 UInt_t AliGRPPreprocessor::ProcessDcsFxs()
419 {
420   //======DCS FXS======//
421   // Get the CTP run configuration
422   // and scalers from DCS FXS
423
424 /*
425   {
426
427     // Get the CTP run configuration
428     TList* list = GetFileSources(kDCS,"CTP_runconfig");  
429     if (!list) {
430       Log("No CTP runconfig file: connection problems with DCS FXS logbook!");
431       return 1;
432     }
433   
434     if (list->GetEntries() == 0) {
435       Log("No CTP runconfig file to be processed!");
436       return 1;
437     }
438     else {
439       TIter iter(list);
440       TObjString *source;
441       while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
442         TString runcfgfile = GetFile(kDCS, "CTP_runconfig", source->GetName());
443         if (runcfgfile.IsNull()) {
444           Log("No CTP runconfig files has been found: empty source!");
445         }
446         else {
447           Log(Form("File with Id CTP_runconfig found in source %s! Copied to %s",source->GetName(),runcfgfile.Data()));
448           AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfiguration(runcfgfile);
449           if (!runcfg) {
450             Log("Bad CTP run configuration file! The corresponding CDB entry will not be filled!");
451             return 1;
452           }
453           else {
454             AliCDBMetaData metaData;
455             metaData.SetBeamPeriod(0);
456             metaData.SetResponsible("Roman Lietava");
457             metaData.SetComment("CTP run configuration");
458             if (!Store("CTP","Config", runcfg, &metaData, 0, 0)) {
459               Log("Unable to store the CTP run configuration object to OCDB!");
460             }
461           }
462         }
463       }
464     }
465     delete list;
466   }
467 */
468   {
469     // Get the CTP counters information
470     TList* list = GetFileSources(kDCS,"CTP_xcounters");  
471     if (!list) {
472       Log("No CTP counters file: connection problems with DAQ FXS logbook!");
473       return 1;
474     }
475   
476     if (list->GetEntries() == 0) {
477       Log("No CTP counters file to be processed!");
478       return 1;
479     }
480     else {
481       TIter iter(list);
482       TObjString *source;
483       while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
484         TString countersfile = GetFile(kDCS, "CTP_xcounters", source->GetName());
485         if (countersfile.IsNull()) {
486           Log("No CTP counters files has been found: empty source!");
487         }
488         else {
489           Log(Form("File with Id CTP_xcounters found in source %s! Copied to %s",source->GetName(),countersfile.Data()));
490           AliTriggerRunScalers *scalers = AliTriggerRunScalers::ReadScalers(countersfile);
491           if (!scalers) {
492             Log("Bad CTP counters file! The corresponding CDB entry will not be filled!");
493             return 1;
494           }
495           else {
496             AliCDBMetaData metaData;
497             metaData.SetBeamPeriod(0);
498             metaData.SetResponsible("Roman Lietava");
499             metaData.SetComment("CTP scalers");
500             if (!Store("CTP","Scalers", scalers, &metaData, 0, 0)) {
501               Log("Unable to store the CTP scalers object to OCDB!");
502             }
503           }
504         }
505       }
506     }
507     delete list;
508   }
509
510   return 0;
511 }
512
513 //_______________________________________________________________
514 Int_t AliGRPPreprocessor::ProcessDcsDPs(TMap* valueMap, TMap* mapDCS)
515 {
516   //Getting the DCS dps
517   //===========//
518
519   //DCS data points
520   //===========//
521   
522   Int_t entries = 0;
523
524   AliInfo(Form("==========LHCState==========="));
525   TObjArray *aliasLHCState = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[0]);
526   if(!aliasLHCState) {
527     Log(Form("LHCState not found!!!"));
528   } else {
529     AliGRPDCS *dcs1 = new AliGRPDCS(aliasLHCState,fStartTime,fEndTime);
530     TString sLHCState = dcs1->ProcessDCS(2);
531     if (sLHCState) {
532       for( Int_t i=0; i<20; i+=2 ) {
533          if( sLHCState.CompareTo(fgkLHCState[i]) == 0 ) {
534             sLHCState = fgkLHCState[i+1];
535             break;
536          }
537       }
538       Log(Form("<LHCState> for run %d: %s",fRun, sLHCState.Data()));
539     } else {
540       Log("LHCState not put in TMap!");
541     }
542     mapDCS->Add(new TObjString("fLHCState"),new TObjString(sLHCState));
543     ++entries;
544   }
545
546   AliInfo(Form("==========L3Polarity==========="));
547   TObjArray *aliasL3Polarity = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[1]);
548   if(!aliasL3Polarity) {
549     Log(Form("L3Polarity not found!!!"));
550   } else {
551     AliGRPDCS *dcs6 = new AliGRPDCS(aliasL3Polarity,fStartTime,fEndTime);
552     TString sL3Polarity = dcs6->ProcessDCS(1);  
553     if (sL3Polarity) {
554       Log(Form("<L3Polarity> for run %d: %s",fRun, sL3Polarity.Data()));
555     } else {
556       Log("L3Polarity not put in TMap!");
557     }
558     mapDCS->Add(new TObjString("fL3Polarity"),new TObjString(sL3Polarity));
559     ++entries;
560   }
561
562   AliInfo(Form("==========DipolePolarity==========="));
563   TObjArray *aliasDipolePolarity = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[2]);
564   if(!aliasDipolePolarity) {
565     Log(Form("DipolePolarity not found!!!"));
566   } else {
567     AliGRPDCS *dcs8 = new AliGRPDCS(aliasDipolePolarity,fStartTime,fEndTime);
568     TString sDipolePolarity = dcs8->ProcessDCS(1);  
569     if (sDipolePolarity) {
570       Log(Form("<DipolePolarity> for run %d: %s",fRun, sDipolePolarity.Data()));
571     } else {
572       Log("DipolePolarity not put in TMap!");
573     }
574     mapDCS->Add(new TObjString("fDipolePolarity"),new TObjString(sDipolePolarity));
575     ++entries;
576   }
577
578   AliInfo(Form("==========LHCLuminosity==========="));
579   TObjArray *aliasLHCLuminosity = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[3]);
580   if(!aliasLHCLuminosity) {
581     Log(Form("LHCLuminosity not found!!!"));
582   } else {
583     AliGRPDCS *dcs3 = new AliGRPDCS(aliasLHCLuminosity,fStartTime,fEndTime);
584     TString sMeanLHCLuminosity = dcs3->ProcessDCS(5);  
585     if (sMeanLHCLuminosity) {
586       Log(Form("<LHCLuminosity> for run %d: %s",fRun, sMeanLHCLuminosity.Data()));
587     } else {
588       Log("LHCLuminosity not put in TMap!");
589     }
590     mapDCS->Add(new TObjString("fLHCLuminosity"), new TObjString(sMeanLHCLuminosity));
591     ++entries;
592   }
593
594   AliInfo(Form("==========BeamIntensity==========="));
595   TObjArray *aliasBeamIntensity = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[4]);
596   if(!aliasBeamIntensity) {
597     Log(Form("BeamIntensity not found!!!"));
598   } else {
599     AliGRPDCS *dcs4 = new AliGRPDCS(aliasBeamIntensity,fStartTime,fEndTime);
600     TString sMeanBeamIntensity = dcs4->ProcessDCS(5);  
601     if (sMeanBeamIntensity) {
602       Log(Form("<BeamIntensity> for run %d: %s",fRun, sMeanBeamIntensity.Data()));
603     } else {
604       Log("BeamIntensity not put in TMap!");
605     }
606     mapDCS->Add(new TObjString("fBeamIntensity"),new TObjString(sMeanBeamIntensity));
607     ++entries;
608   }
609
610   AliInfo(Form("==========L3Current==========="));
611   TObjArray *aliasL3Current = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[5]);
612   if(!aliasL3Current) {
613     Log(Form("L3Current not found!!!"));
614   } else {
615     AliGRPDCS *dcs5 = new AliGRPDCS(aliasL3Current,fStartTime,fEndTime);
616     TString sMeanL3Current = dcs5->ProcessDCS(5);  
617     if (sMeanL3Current) {
618       Log(Form("<L3Current> for run %d: %s",fRun, sMeanL3Current.Data()));
619     } else {
620       Log("L3Current not put in TMap!");
621     }
622     mapDCS->Add(new TObjString("fL3Current"),new TObjString(sMeanL3Current));
623     ++entries;
624   }
625
626
627   AliInfo(Form("==========DipoleCurrent==========="));
628   TObjArray *aliasDipoleCurrent = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[6]);
629   if(!aliasDipoleCurrent) {
630     Log(Form("DipoleCurrent not found!!!"));
631   }  else {
632     AliGRPDCS *dcs7 = new AliGRPDCS(aliasDipoleCurrent,fStartTime,fEndTime);
633     TString sMeanDipoleCurrent = dcs7->ProcessDCS(5);  
634     if (sMeanDipoleCurrent) {
635       Log(Form("<DipoleCurrent> for run %d: %s",fRun, sMeanDipoleCurrent.Data()));
636     } else {
637       Log("DipoleCurrent not put in TMap!");
638     }
639     mapDCS->Add(new TObjString("fDipoleCurrent"),new TObjString(sMeanDipoleCurrent));
640     ++entries;
641   }
642
643   AliInfo(Form("==========CavernTemperature==========="));
644   TObjArray *aliasCavernTemperature = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[7]);
645   if(!aliasCavernTemperature) {
646     Log(Form("CavernTemperature not found!!!"));
647   }  else {
648     AliGRPDCS *dcs9 = new AliGRPDCS(aliasCavernTemperature,fStartTime,fEndTime);
649     TString sMeanCavernTemperature = dcs9->ProcessDCS(5);  
650     if (sMeanCavernTemperature) {
651       Log(Form("<CavernTemperature> for run %d: %s",fRun, sMeanCavernTemperature.Data()));
652     } else {
653       Log("CavernTemperature not put in TMap!");
654     }
655     mapDCS->Add(new TObjString("fCavernTemperature"),new TObjString(sMeanCavernTemperature));
656     ++entries;
657   }
658
659   AliInfo(Form("==========CavernPressure==========="));
660   TObjArray *aliasCavernPressure = (TObjArray *)valueMap->GetValue(fgkDCSDataPoints[8]);
661   if(!aliasCavernPressure) {
662     Log("CavernPressure not found!!!");
663   } else {
664     AliGRPDCS *dcs10 = new AliGRPDCS(aliasCavernPressure,fStartTime,fEndTime);
665     TString sMeanCavernPressure = dcs10->ProcessDCS(5);  
666     if (sMeanCavernPressure) {
667       Log(Form("<CavernPressure> for run %d: %s",fRun, sMeanCavernPressure.Data()));
668     } else {
669       Log("CavernPressure not put in TMap!");
670     }
671     mapDCS->Add(new TObjString("fCavernPressure"),new TObjString(sMeanCavernPressure));
672     ++entries;
673   }
674
675
676    // NEEDS TO BE REVISED, CONFIRMED
677    AliInfo(Form("==========P2PressureMap==========="));
678    AliDCSSensorArray *dcsSensorArray = GetPressureMap(valueMap);
679    if( fPressure->NumFits()==0 ) {
680      Log("Problem with the pressure sensor values!!!");
681    } 
682    else {
683       AliDCSSensor* sensorP2 = dcsSensorArray->GetSensor(fgkDCSDataPoints[9]);
684       if( sensorP2->GetFit() ) {
685         Log(Form("<P2Pressure> for run %d: Sensor Fit found",fRun));
686         mapDCS->Add( new TObjString("fP2Pressure"), sensorP2 );
687         ++entries;
688       } 
689       else {
690         Log(Form("ERROR Sensor Fit for %s not found: ", fgkDCSDataPoints[9] ));
691       }
692       
693       /*
694       AliDCSSensor* sensorMeyrin = dcsSensorArray->GetSensor(fgkDCSDataPoints[10]);
695       if( sensorMeyrin->GetFit() ) {
696         Log(Form("<MeyrinPressure> for run %d: Sensor Fit found",fRun));
697         mapDCS->Add( new TObjString("fMeyrinPressure"), sensorMeyrin );
698         ++entries;
699       } else {
700         Log(Form("ERROR Sensor Fit for %s not found: ", fgkDCSDataPoints[10] ));
701       }
702       */
703    }
704
705   return entries;
706 }
707
708 //_______________________________________________________________
709 AliDCSSensorArray *AliGRPPreprocessor::GetPressureMap(TMap* dcsAliasMap)
710 {
711   // extract DCS pressure maps. Perform fits to save space
712   
713   TMap *map = fPressure->ExtractDCS(dcsAliasMap);
714   if (map) {
715     fPressure->MakeSplineFit(map);
716     Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
717     if (fitFraction > kFitFraction ) {
718       AliInfo(Form("Pressure values extracted, %d fits performed.", fPressure->NumFits()));
719     } else { 
720       AliInfo("Too few pressure maps fitted!!!");
721     }
722   } else {
723     AliInfo("no atmospheric pressure map extracted!!!");
724   }
725   delete map;
726  
727   return fPressure;
728 }
729
730 //_______________________________________________________________
731 /*UInt_t AliGRPPreprocessor::MapPressure(TMap* dcsAliasMap) {
732   // extract DCS pressure maps. Perform fits to save space
733   
734   UInt_t result=0;
735   TMap *map = fPressure->ExtractDCS(dcsAliasMap);
736   if (map) {
737     fPressure->MakeSplineFit(map);
738     Double_t fitFraction = fPressure->NumFits()/fPressure->NumSensors(); 
739     if (fitFraction > kFitFraction ) {
740       AliInfo(Form("Pressure values extracted, fits performed.\n"));
741     } else { 
742       Log ("Too few pressure maps fitted. \n");
743       result = 9;
744     }
745   } else {
746     Log("AliTPCPreprocsessor: no atmospheric pressure map extracted. \n");
747     result=9;
748   }
749   delete map;
750   // Now store the final CDB file
751   
752   if ( result == 0 ) {
753     AliCDBMetaData metaData;
754     metaData.SetBeamPeriod(0);
755     metaData.SetResponsible("Panos Christakoglou");
756     metaData.SetComment("Preprocessor AliGRP data base pressure entries.");
757     
758     Bool_t storeOK = Store("Calib", "Pressure", fPressure, &metaData, 0, 0);
759     if ( !storeOK ) result=1; 
760   }
761   
762   return result; 
763   }*/
764
765   
766 //_______________________________________________________________
767 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)
768 {
769    //
770    // Retrieves logbook and trigger information from the online logbook
771    // This information is needed for prompt reconstruction
772    //
773    // Parameters are:
774    // Run number
775    // DAQ params: dbHost, dbPort, dbName, user, password, logbookTable, triggerTable
776    // cdbRoot
777    //
778    // returns:
779    //         positive on success: the return code is the run number of last run processed of the same run type already processed by the SHUTTLE
780    //         0 on success and no run was found
781    //         negative on error
782    //
783    // This function is NOT called during the preprocessor run in the Shuttle!
784    //
785
786    // defaults
787    if (dbPort == 0)
788      dbPort = 3306;
789
790    // CDB connection
791    AliCDBManager* cdb = AliCDBManager::Instance();
792    cdb->SetDefaultStorage(cdbRoot);
793
794    // SQL connection
795    TSQLServer* server = TSQLServer::Connect(Form("mysql://%s:%d/%s", dbHost, dbPort, dbName), user, password);
796
797    if (!server)
798    {
799      Printf("ERROR: Could not connect to DAQ LB");
800      return -1;
801    }
802
803    // main logbook
804    TString sqlQuery;
805    sqlQuery.Form("SELECT DAQ_time_start, run_type, detectorMask FROM logbook WHERE run = %d", run);
806    TSQLResult* result = server->Query(sqlQuery);
807    if (!result)
808    {
809      Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
810      return -2;
811    }
812
813    if (result->GetRowCount() == 0)
814    {
815      Printf("ERROR: Run %d not found", run);
816      delete result;
817      return -3;
818    }
819
820    TSQLRow* row = result->Next();
821    if (!row)
822    {
823      Printf("ERROR: Could not receive data from run %d", run);
824      delete result;
825      return -4;
826    }
827
828    TString runType(row->GetField(1));
829
830    TMap grpData;
831    grpData.Add(new TObjString("DAQ_time_start"), new TObjString(row->GetField(0)));
832    grpData.Add(new TObjString("run_type"), new TObjString(runType));
833    grpData.Add(new TObjString("detectorMask"), new TObjString(row->GetField(2)));
834
835    delete row;
836    row = 0;
837
838    delete result;
839    result = 0;
840
841    Printf("Storing GRP/GRP/Data object with the following content");
842    grpData.Print();
843
844    AliCDBMetaData metadata;
845    metadata.SetResponsible("Jan Fiete Grosse-Oetringhaus");
846    metadata.SetComment("GRP Output parameters received during online running");
847
848    AliCDBId id("GRP/GRP/Data", run, run);
849    Bool_t success = cdb->Put(&grpData, id, &metadata);
850
851    grpData.DeleteAll();
852
853    if (!success)
854    {
855      Printf("ERROR: Could not store GRP/GRP/Data into OCDB");
856      return -5;
857    }
858
859    // Receive trigger information
860    sqlQuery.Form("SELECT configFile FROM logbook_trigger_config WHERE run = %d", run);
861    result = server->Query(sqlQuery);
862    if (!result)
863    {
864      Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
865      return -11;
866    }
867
868    if (result->GetRowCount() == 0)
869    {
870      Printf("ERROR: Run %d not found in logbook_trigger_config", run);
871      delete result;
872      return -12;
873    }
874
875    row = result->Next();
876    if (!row)
877    {
878      Printf("ERROR: Could not receive logbook_trigger_config data from run %d", run);
879      delete result;
880      return -13;
881    }
882
883    TString triggerConfig(row->GetField(0));
884
885    delete row;
886    row = 0;
887
888    delete result;
889    result = 0;
890
891    Printf("Found trigger configuration: %s", triggerConfig.Data());
892
893    AliTriggerConfiguration *runcfg = AliTriggerConfiguration::LoadConfigurationFromString(triggerConfig);
894    if (!runcfg)
895    {
896      Printf("ERROR: Could not create CTP configuration object");
897      return -14;
898    }
899
900    metadata.SetComment("CTP run configuration received during online running");
901
902    AliCDBId id2("GRP/CTP/Config", run, run);
903    success = cdb->Put(runcfg, id2, &metadata);
904
905    delete runcfg;
906    runcfg = 0;
907
908    if (!success)
909    {
910      Printf("ERROR: Could not store GRP/CTP/Config into OCDB");
911      return -15;
912    }
913
914    // get last run with same run type that was already processed by the SHUTTLE
915
916    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());
917    result = server->Query(sqlQuery);
918    if (!result)
919    {
920      Printf("ERROR: Can't execute query <%s>!", sqlQuery.Data());
921      return -21;
922    }
923
924    if (result->GetRowCount() == 0)
925    {
926      Printf("ERROR: No result with query <%s>", sqlQuery.Data());
927      delete result;
928      return -22;
929    }
930
931    row = result->Next();
932    if (!row)
933    {
934      Printf("ERROR: Could not receive data for query <%s>", sqlQuery.Data());
935      delete result;
936      return -23;
937    }
938
939    TString lastRunStr(row->GetField(0));
940    Int_t lastRun = lastRunStr.Atoi();
941
942    Printf("Last run with same run type %s is %d", runType.Data(), lastRun);
943
944    delete row;
945    row = 0;
946
947    delete result;
948    result = 0;
949
950    server->Close();
951    delete server;
952    server = 0;
953
954    return lastRun;
955 }