]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALPreprocessor.cxx
Preprocessor related update (mainly from Jiri) to pick up TRU config info from DCS...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALPreprocessor.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 // EMCAL Preprocessor class. It runs by Shuttle at the end of the run,
20 // calculates stuff to be posted in OCDB
21 //
22 // Author: Boris Polichtchouk, 4 October 2006
23 // Adapted for EMCAL by Gustavo Conesa Balbastre, October 2006
24 // Updated by David Silvermyr May 2008, based on TPC code
25 ///////////////////////////////////////////////////////////////////////////////
26
27 //Root
28 #include "TFile.h"
29 #include "TTree.h"
30 #include "TEnv.h"
31 #include "TParameter.h"
32
33 #include <TTimeStamp.h>
34
35 //AliRoot
36 #include "AliShuttleInterface.h"
37 #include "AliEMCALPreprocessor.h"
38 #include "AliLog.h"
39 #include "AliDCSValue.h"
40 #include "AliCDBMetaData.h"
41 #include "AliEMCALTriggerTRUDCSConfig.h"
42 #include "AliEMCALTriggerDCSConfig.h"
43 #include "AliCaloCalibPedestal.h"
44 #include "AliCaloCalibSignal.h"
45 #include "AliEMCALSensorTempArray.h"
46
47 const Int_t kValCutTemp = 100;               // discard temperatures > 100 degrees
48 const Int_t kDiffCutTemp = 5;                // discard temperature differences > 5 degrees
49 const TString kPedestalRunType = "PEDESTAL";  // pedestal run identifier
50 const TString kPhysicsRunType = "PHYSICS";   // physics run identifier
51 const TString kStandAloneRunType = "STANDALONE_BC"; // standalone run identifier
52 const TString kAmandaTemp = "PT_%02d.Temperature"; // Amanda string for temperature entries
53 //const Double_t kFitFraction = 0.7;                 // Fraction of DCS sensor fits required 
54 const Double_t kFitFraction = -1.0;          // Don't require minimum number of fits during commissioning 
55
56 const TString kMetaResponsible = "David Silvermyr";
57 //legacy comments and return codes from TPC
58 const TString kMetaComment = "Preprocessor AliEMCAL data base entries.";
59 const int kReturnCodeNoInfo = 9;
60 const int kReturnCodeNoObject = 2;
61 const int kReturnCodeNoEntries = 1;
62
63 const int kNTRU = 30; // From 2011; 10 SuperModules (SM) * 3 TRU per SM
64
65 ClassImp(AliEMCALPreprocessor)
66   
67 //_______________________________________________________________________________________
68 AliEMCALPreprocessor::AliEMCALPreprocessor() :
69   AliPreprocessor("EMC",0),
70   fConfEnv(0), 
71   fTemp(0), 
72   fConfigOK(kTRUE)
73 {
74   //default constructor
75 }
76
77 //_______________________________________________________________________________________
78 AliEMCALPreprocessor::AliEMCALPreprocessor(AliShuttleInterface* shuttle):
79   AliPreprocessor("EMC",shuttle),
80   fConfEnv(0), 
81   fTemp(0), 
82   fConfigOK(kTRUE)
83 {
84   // Constructor AddRunType(kPedestalRunType);
85   
86   // define run types to be processed
87   AddRunType(kPedestalRunType);
88   AddRunType(kPhysicsRunType);
89 }
90
91 //______________________________________________________________________________________________
92 AliEMCALPreprocessor::AliEMCALPreprocessor(const AliEMCALPreprocessor&  ) :
93   AliPreprocessor("EMCAL",0),
94   fConfEnv(0), fTemp(0), fConfigOK(kTRUE)
95 {
96   Fatal("AliEMCALPreprocessor", "copy constructor not implemented");
97 }
98
99 // assignment operator; use copy ctor to make life easy.
100 //______________________________________________________________________________________________
101 AliEMCALPreprocessor& AliEMCALPreprocessor::operator = (const AliEMCALPreprocessor &source ) 
102 {
103   // assignment operator; use copy ctor
104   if (&source == this) return *this;
105   
106   new (this) AliEMCALPreprocessor(source);
107   return *this;
108 }
109
110 //____________________________________________________________________________
111 AliEMCALPreprocessor::~AliEMCALPreprocessor()
112 {
113   // destructor
114   if (fTemp) delete fTemp;
115 }
116
117 //______________________________________________________________________________________________
118 void AliEMCALPreprocessor::Initialize(Int_t run, UInt_t startTime,
119                                       UInt_t endTime)
120 {
121   // Creates AliTestDataDCS object -- start maps half an hour beforre actual run start
122   UInt_t startTimeLocal = startTime-1800;
123   AliPreprocessor::Initialize(run, startTimeLocal, endTime);
124   
125   AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
126                TTimeStamp((time_t)startTime,0).AsString(),
127                TTimeStamp((time_t)endTime,0).AsString()));
128   
129   // Preprocessor configuration
130   AliCDBEntry* entry = GetFromOCDB("Config", "Preprocessor");
131   if (entry) fConfEnv = (TEnv*) entry->GetObject();
132   if ( fConfEnv==0 ) {
133     Log("AliEMCALPreprocessor: Preprocessor Config OCDB entry missing.\n");
134     fConfigOK = kFALSE;
135     return;
136   }
137   
138   // Temperature sensors
139   TTree *confTree = 0;
140   
141   TString tempConf = fConfEnv->GetValue("Temperature","ON");
142   tempConf.ToUpper();
143   if (tempConf != "OFF" ) {
144     entry = GetFromOCDB("Config", "Temperature");
145     if (entry) confTree = (TTree*) entry->GetObject();
146     if ( confTree==0 ) {
147       Log("AliEMCALPreprocessor: Temperature Config OCDB entry missing.\n");
148       fConfigOK = kFALSE;
149       return;
150     }
151     fTemp = new AliEMCALSensorTempArray(startTimeLocal, fEndTime, confTree, kAmandaTemp);
152     fTemp->SetValCut(kValCutTemp);
153     fTemp->SetDiffCut(kDiffCutTemp);
154   }
155   
156   return;
157 }
158
159 //______________________________________________________________________________________________
160 UInt_t AliEMCALPreprocessor::Process(TMap* dcsAliasMap)
161 {
162   // Fills data into EMCAL calibrations objects
163   // Amanda servers provide information directly through dcsAliasMap
164   
165   if (!fConfigOK) return kReturnCodeNoInfo;
166   UInt_t result = 0;
167   TObjArray *resultArray = new TObjArray();
168   TString errorHandling = fConfEnv->GetValue("ErrorHandling","ON");
169   errorHandling.ToUpper();
170   TObject * status;
171   
172   UInt_t dcsResult=0;
173   if (errorHandling == "OFF" ) {
174     if (!dcsAliasMap) dcsResult = kReturnCodeNoEntries;
175     else if (dcsAliasMap->GetEntries() == 0 ) dcsResult = kReturnCodeNoEntries;  
176     status = new TParameter<int>("dcsResult",dcsResult);
177     resultArray->Add(status);
178   } 
179   else {
180     if (!dcsAliasMap) return kReturnCodeNoInfo;
181     else if (dcsAliasMap->GetEntries() == 0 ) return kReturnCodeNoInfo;
182   }
183   
184     
185   TString runType = GetRunType();
186   
187   // Temperature sensors are processed by AliEMCALCalTemp
188   TString tempConf = fConfEnv->GetValue("Temperature","ON");
189   tempConf.ToUpper();
190   if (tempConf != "OFF" && dcsAliasMap ) {
191     UInt_t tempResult = MapTemperature(dcsAliasMap);
192     result=tempResult;
193     status = new TParameter<int>("tempResult",tempResult);
194     resultArray->Add(status);
195   }
196   // Trigger configuration processing
197   TString triggerConf = fConfEnv->GetValue("Trigger","ON");
198   triggerConf.ToUpper();
199   if (triggerConf != "OFF" && dcsAliasMap ) {
200     UInt_t triggerResult = MapTriggerConfig(dcsAliasMap);
201     result=triggerResult;
202     status = new TParameter<int>("triggerResult",triggerResult);
203     resultArray->Add(status);
204   }
205   
206   // Other calibration information will be retrieved through FXS files
207   //  examples:
208   //    TList* fileSourcesDAQ = GetFile(AliShuttleInterface::kDAQ, "pedestals");
209   //    const char* fileNamePed = GetFile(AliShuttleInterface::kDAQ, "pedestals", "LDC1");
210   //
211   //    TList* fileSourcesHLT = GetFile(AliShuttleInterface::kHLT, "calib");
212   //    const char* fileNameHLT = GetFile(AliShuttleInterface::kHLT, "calib", "LDC1");
213   
214   // PEDESTAL ENTRIES:
215   
216   if ( runType == kPedestalRunType ) {
217     Int_t numSources = 1;
218     Int_t pedestalSource[2] = {AliShuttleInterface::kDAQ, AliShuttleInterface::kHLT} ;
219     TString source = fConfEnv->GetValue("Pedestal","DAQ");
220     source.ToUpper();
221     if (source != "OFF" ) { 
222       if ( source == "HLT") pedestalSource[0] = AliShuttleInterface::kHLT;
223       if (!GetHLTStatus()) pedestalSource[0] = AliShuttleInterface::kDAQ;
224       if (source == "HLTDAQ" ) {
225         numSources=2;
226         pedestalSource[0] = AliShuttleInterface::kHLT;
227         pedestalSource[1] = AliShuttleInterface::kDAQ;
228       }
229       if (source == "DAQHLT" ) numSources=2;
230       UInt_t pedestalResult=0;
231       for (Int_t i=0; i<numSources; i++ ) {     
232         pedestalResult = ExtractPedestals(pedestalSource[i]);
233         if ( pedestalResult == 0 ) break;
234       }
235       result += pedestalResult;
236       status = new TParameter<int>("pedestalResult",pedestalResult);
237       resultArray->Add(status);
238     }
239   }
240   
241   // SIGNAL/LED ENTRIES:
242   if( runType == kPhysicsRunType ) {
243     Int_t numSources = 1;
244     Int_t signalSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
245     TString source = fConfEnv->GetValue("Signal","DAQ");
246     source.ToUpper();
247     if ( source != "OFF") { 
248       if ( source == "HLT") signalSource[0] = AliShuttleInterface::kHLT;
249       if (!GetHLTStatus()) signalSource[0] = AliShuttleInterface::kDAQ;
250       if (source == "HLTDAQ" ) {
251         numSources=2;
252         signalSource[0] = AliShuttleInterface::kHLT;
253         signalSource[1] = AliShuttleInterface::kDAQ;
254       }
255       if (source == "DAQHLT" ) numSources=2;
256       UInt_t signalResult=0;
257       for (Int_t i=0; i<numSources; i++ ) {     
258         signalResult = ExtractSignal(signalSource[i]);
259         if ( signalResult == 0 ) break;
260       }
261       result += signalResult;
262       status = new TParameter<int>("signalResult",signalResult);
263       resultArray->Add(status);
264     }
265   }
266   
267   
268   // overall status at the end
269   if (errorHandling == "OFF" ) {
270     AliCDBMetaData metaData;
271     metaData.SetBeamPeriod(0);
272     metaData.SetResponsible(kMetaResponsible);
273     metaData.SetComment("Preprocessor AliEMCAL status.");
274     Bool_t storeOK = Store("Calib", "PreprocStatus", resultArray, &metaData, 0, kFALSE);
275     resultArray->Delete();
276     result = 0;
277     if ( !storeOK )  result=1;
278     return result;
279   } 
280   else { 
281     return result;
282   }
283   
284 }
285 //______________________________________________________________________________________________
286 UInt_t AliEMCALPreprocessor::MapTemperature(TMap* dcsAliasMap)
287 { // extract DCS temperature maps. Perform fits to save space
288   UInt_t result=0;
289
290   TMap *map = fTemp->ExtractDCS(dcsAliasMap);
291   if (map) {
292     fTemp->MakeSplineFit(map);
293     Double_t fitFraction = 1.0*fTemp->NumFits()/fTemp->NumSensors(); 
294     if (fitFraction > kFitFraction ) {
295       AliInfo(Form("Temperature values extracted, fits performed.\n"));
296     } 
297     else { 
298       Log ("Too few temperature maps fitted. \n");
299       result = kReturnCodeNoInfo;
300     }
301   } 
302   else {
303     Log("No temperature map extracted. \n");
304     result = kReturnCodeNoInfo;
305   }
306   delete map;
307   // Now store the final CDB file
308   
309   if ( result == 0 ) { // some info was found
310     AliCDBMetaData metaData;
311     metaData.SetBeamPeriod(0);
312     metaData.SetResponsible(kMetaResponsible);
313     metaData.SetComment(kMetaComment);
314     
315     Bool_t storeOK = Store("Calib", "Temperature", fTemp, &metaData, 0, kFALSE);
316     if ( !storeOK )  result=1;
317   }
318   
319   return result;
320 }
321
322 //______________________________________________________________________________________________
323 UInt_t AliEMCALPreprocessor::MapTriggerConfig(TMap* dcsAliasMap)
324 { // extract DCS trigger info
325   Int_t i, iTRU;
326   char buf[100];
327
328   AliDCSValue *dcsVal;
329   TObjArray *arrL0ALGSEL, *arrPEAKFINDER, *arrGLOBALTHRESH, *arrCOSMTHRESH;
330   TObjArray *arrMASK[6];
331
332   // overall object to hold STU and DCS config info
333   // DS comment: for now only holds TRU info, i.e. only partially filled
334   // (STU info only in raw data header; unfortunately not also picked up via DCS DPs)
335   AliEMCALTriggerDCSConfig *trigConfig = new AliEMCALTriggerDCSConfig();
336
337   // loop through all TRUs
338   for( iTRU = 0; iTRU < kNTRU; iTRU++){
339
340     // get the shuttled values
341     sprintf( buf, "EMC_TRU%02d_L0ALGSEL", iTRU );
342     arrL0ALGSEL = (TObjArray*) dcsAliasMap->GetValue( buf );
343     sprintf( buf, "EMC_TRU%02d_PEAKFINDER", iTRU );
344     arrPEAKFINDER = (TObjArray*) dcsAliasMap->GetValue( buf );
345     sprintf( buf, "EMC_TRU%02d_GLOBALTHRESH", iTRU );
346     arrGLOBALTHRESH = (TObjArray*) dcsAliasMap->GetValue( buf );
347     sprintf( buf, "EMC_TRU%02d_COSMTHRESH", iTRU );
348     arrCOSMTHRESH = (TObjArray*) dcsAliasMap->GetValue( buf );
349     
350     for( i = 0; i < 6; i++ ){
351       sprintf( buf, "EMC_TRU%02d_MASK%d", iTRU, i );
352       arrMASK[i] = (TObjArray*) dcsAliasMap->GetValue( buf );
353     }
354     
355     // fill the objects
356     AliEMCALTriggerTRUDCSConfig* truConfig = trigConfig->GetTRUDCSConfig(iTRU);
357     
358     // get last entries. fill the TRU object
359     if( ! arrL0ALGSEL ){
360       AliWarning( Form("EMC DCS TRU%02d L0ALGSEL alias not found!\n", iTRU ));
361     }
362     else{
363       dcsVal = (AliDCSValue *) arrL0ALGSEL->At( arrL0ALGSEL->GetEntries() - 1 );
364       truConfig->SetL0SEL( dcsVal->GetUInt() );
365     }
366     if( ! arrPEAKFINDER ){
367       AliWarning( Form("EMC DCS TRU%02d PEAKFINDER alias not found!\n", iTRU ));
368     }
369     else{
370       dcsVal = (AliDCSValue *) arrPEAKFINDER->At( arrPEAKFINDER->GetEntries() - 1 );
371       truConfig->SetSELPF( dcsVal->GetUInt() );
372     }
373     if( ! arrGLOBALTHRESH ){
374       AliWarning( Form("EMC DCS TRU%02d GLOBALTHRESH alias not found!\n", iTRU ));
375     }
376     else{
377       dcsVal = (AliDCSValue *) arrGLOBALTHRESH->At( arrGLOBALTHRESH->GetEntries() - 1 );
378       truConfig->SetGTHRL0( dcsVal->GetUInt() );
379     }
380     if( ! arrCOSMTHRESH ){
381       AliWarning( Form("EMC DCS TRU%02d COSMTHRESH alias not found!\n", iTRU ));
382     }
383     else{
384       dcsVal = (AliDCSValue *) arrCOSMTHRESH->At( arrCOSMTHRESH->GetEntries() - 1 );
385       truConfig->SetL0COSM( dcsVal->GetUInt() );
386     }
387     
388     for( i = 0; i < 6; i++ ){
389       if( ! arrMASK[i] ){
390         AliWarning( Form("EMC DCS TRU%02d MASK%d alias not found!\n", iTRU, i ));
391       }
392       else{
393         dcsVal = (AliDCSValue *) arrMASK[i]->At( arrMASK[i]->GetEntries() - 1 );
394         truConfig->SetMaskReg( dcsVal->GetUInt(), i );
395       }
396     }
397     
398   } // TRUs
399   
400   // save the objects
401   AliCDBMetaData metaData;
402   metaData.SetBeamPeriod(0);
403   metaData.SetResponsible(kMetaResponsible);
404   metaData.SetComment(kMetaComment); 
405       
406   Bool_t retCode = Store("Calib", "LED", trigConfig, &metaData, 0, kFALSE);
407   return retCode;
408 }
409
410 //______________________________________________________________________________________________
411 UInt_t AliEMCALPreprocessor::ExtractPedestals(Int_t sourceFXS)
412 {
413   //  Read pedestal file from file exchange server
414   //  Only store if new pedestal info is available
415   //
416   UInt_t result=0;
417
418   AliCaloCalibPedestal *calibPed = new AliCaloCalibPedestal(AliCaloCalibPedestal::kEmCal);
419   calibPed->Init();
420
421   TList* list = GetFileSources(sourceFXS,"pedestals");
422   if (list && list->GetEntries()>0) {
423     
424     //  loop through all files from LDCs
425
426     int changes = 0;
427     UInt_t index = 0;
428     while (list->At(index)!=NULL) {
429       TObjString* fileNameEntry = (TObjString*) list->At(index);
430       if (fileNameEntry!=NULL) {
431         TString fileName = GetFile(sourceFXS, "pedestals",
432                                    fileNameEntry->GetString().Data());
433         TFile *f = TFile::Open(fileName);
434         if (!f) {
435           Log ("Error opening pedestal file.");
436           result = kReturnCodeNoObject;
437           break;
438         }
439         AliCaloCalibPedestal *calPed;
440         f->GetObject("emcCalibPedestal",calPed);
441         if ( !calPed ) {
442           Log ("No pedestal calibration object in file.");
443           result = kReturnCodeNoObject;
444           break;
445         }
446         if ( calPed->GetNEvents()>0 && calPed->GetNChanFills()>0 ) {
447           // add info for the modules available in the present file
448           Bool_t status = calibPed->AddInfo(calPed);
449           if (status) { changes++; }
450         }
451         
452         delete calPed; 
453         f->Close();
454       }
455       index++;
456     }  // while(list)
457     
458     //
459     //  Store updated pedestal entry to OCDB
460     //
461     if (changes>0) {
462       AliCDBMetaData metaData;
463       metaData.SetBeamPeriod(0);
464       metaData.SetResponsible(kMetaResponsible);
465       metaData.SetComment(kMetaComment); 
466       
467       Bool_t storeOK = StoreReferenceData("Calib", "Pedestals", calibPed, &metaData);
468       if ( !storeOK ) result++;
469     }
470   } 
471   else {
472     Log ("Error: no entries in input file list!");
473     result = kReturnCodeNoEntries;
474   }
475   
476   return result;
477 }
478
479 //______________________________________________________________________________________________
480 UInt_t AliEMCALPreprocessor::ExtractSignal(Int_t sourceFXS)
481 { //  Read signal file from file exchange server
482   //  Only store if new signal info is available
483   //
484   UInt_t result=0;
485   AliCaloCalibSignal *calibSig = new AliCaloCalibSignal(AliCaloCalibSignal::kEmCal); 
486   
487   TList* list = GetFileSources(sourceFXS,"signal");
488   if (list && list->GetEntries()>0) {
489
490     //  loop through all files from LDCs
491     
492     int changes = 0;
493     UInt_t index = 0;
494     while (list->At(index)!=NULL) {
495       TObjString* fileNameEntry = (TObjString*) list->At(index);
496       if (fileNameEntry!=NULL) {
497         TString fileName = GetFile(sourceFXS, "signal",
498                                    fileNameEntry->GetString().Data());
499         TFile *f = TFile::Open(fileName);
500         if (!f) {
501           Log ("Error opening signal file.");
502           result = kReturnCodeNoObject;
503           break;
504         }
505         AliCaloCalibSignal *calSig;
506         f->GetObject("emcCalibSignal",calSig);
507         if ( !calSig ) {
508           Log ("No signal calibration object in file.");
509           result = kReturnCodeNoObject;
510           break;
511         }
512         if ( calSig->GetNEvents()>0 ) {
513           // add info for the modules available in the present file
514           Bool_t status = calibSig->AddInfo(calSig);
515           if (status) { changes++; }
516         }
517         
518         delete calSig; 
519         f->Close();
520       }
521       index++;
522     }  // while(list)
523     
524     //
525     //  Store updated signal entry to OCDB
526     //
527     if (changes>0) {
528       AliCDBMetaData metaData;
529       metaData.SetBeamPeriod(0);
530       metaData.SetResponsible(kMetaResponsible);
531       metaData.SetComment(kMetaComment); 
532       
533       Bool_t storeOK = Store("Calib", "LED", calibSig, &metaData, 0, kFALSE);
534       if ( !storeOK ) result++;
535     }
536   } 
537   else {
538     Log ("Error: no entries in input file list!");
539     result = kReturnCodeNoEntries;
540   }
541
542   return result;
543 }
544
545