Fixing typo.
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALPreprocessor.cxx
CommitLineData
9e788b10 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$ */
132753b5 17
9e788b10 18///////////////////////////////////////////////////////////////////////////////
19// EMCAL Preprocessor class. It runs by Shuttle at the end of the run,
132753b5 20// calculates stuff to be posted in OCDB
9e788b10 21//
22// Author: Boris Polichtchouk, 4 October 2006
23// Adapted for EMCAL by Gustavo Conesa Balbastre, October 2006
132753b5 24// Updated by David Silvermyr May 2008, based on TPC code
9e788b10 25///////////////////////////////////////////////////////////////////////////////
26
80c68391 27//Root
9e788b10 28#include "TFile.h"
132753b5 29#include "TTree.h"
30#include "TEnv.h"
31#include "TParameter.h"
32
33#include <TTimeStamp.h>
8e8e4fc8 34
80c68391 35//AliRoot
132753b5 36#include "AliShuttleInterface.h"
80c68391 37#include "AliEMCALPreprocessor.h"
38#include "AliLog.h"
39#include "AliCDBMetaData.h"
132753b5 40#include "AliCaloCalibPedestal.h"
41#include "AliCaloCalibSignal.h"
42#include "AliEMCALSensorTempArray.h"
9e788b10 43
132753b5 44const Int_t kValCutTemp = 100; // discard temperatures > 100 degrees
45const Int_t kDiffCutTemp = 5; // discard temperature differences > 5 degrees
46const TString kPedestalRunType = "PEDESTAL"; // pedestal run identifier
47const TString kPhysicsRunType = "PHYSICS"; // physics run identifier
8bc94fba 48const TString kStandAloneRunType = "STANDALONE_BC"; // standalone run identifier
32fbe941 49const TString kAmandaTemp = "PT_%02d.Temperature"; // Amanda string for temperature entries
132753b5 50//const Double_t kFitFraction = 0.7; // Fraction of DCS sensor fits required
51const Double_t kFitFraction = -1.0; // Don't require minimum number of fits during commissioning
9e788b10 52
132753b5 53const TString kMetaResponsible = "David Silvermyr";
54//legacy comments and return codes from TPC
55const TString kMetaComment = "Preprocessor AliEMCAL data base entries.";
56const int kReturnCodeNoInfo = 9;
57const int kReturnCodeNoObject = 2;
58const int kReturnCodeNoEntries = 1;
59
60ClassImp(AliEMCALPreprocessor)
61
9e788b10 62//_______________________________________________________________________________________
63AliEMCALPreprocessor::AliEMCALPreprocessor() :
132753b5 64 AliPreprocessor("EMC",0),
65 fConfEnv(0),
66 fTemp(0),
67 fConfigOK(kTRUE)
9e788b10 68{
69 //default constructor
70}
71
72//_______________________________________________________________________________________
80c68391 73AliEMCALPreprocessor::AliEMCALPreprocessor(AliShuttleInterface* shuttle):
132753b5 74 AliPreprocessor("EMC",shuttle),
75 fConfEnv(0),
76 fTemp(0),
77 fConfigOK(kTRUE)
9e788b10 78{
132753b5 79 // Constructor AddRunType(kPedestalRunType);
80
81 // define run types to be processed
82 AddRunType(kPedestalRunType);
83 AddRunType(kPhysicsRunType);
84 AddRunType(kStandAloneRunType);
85}
6c0734ce 86
132753b5 87//______________________________________________________________________________________________
88AliEMCALPreprocessor::AliEMCALPreprocessor(const AliEMCALPreprocessor& ) :
89 AliPreprocessor("EMCAL",0),
90 fConfEnv(0), fTemp(0), fConfigOK(kTRUE)
91{
92 Fatal("AliEMCALPreprocessor", "copy constructor not implemented");
9e788b10 93}
94
132753b5 95// assignment operator; use copy ctor to make life easy.
96//______________________________________________________________________________________________
97AliEMCALPreprocessor& AliEMCALPreprocessor::operator = (const AliEMCALPreprocessor &source )
9e788b10 98{
132753b5 99 // assignment operator; use copy ctor
100 if (&source == this) return *this;
b3204f43 101
132753b5 102 new (this) AliEMCALPreprocessor(source);
103 return *this;
104}
09744db4 105
132753b5 106//____________________________________________________________________________
107AliEMCALPreprocessor::~AliEMCALPreprocessor()
108{
109 // destructor
110 if (fTemp) delete fTemp;
111}
112
113//______________________________________________________________________________________________
114void AliEMCALPreprocessor::Initialize(Int_t run, UInt_t startTime,
115 UInt_t endTime)
116{
117 // Creates AliTestDataDCS object -- start maps half an hour beforre actual run start
118 UInt_t startTimeLocal = startTime-1800;
119 AliPreprocessor::Initialize(run, startTimeLocal, endTime);
80c68391 120
132753b5 121 AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
122 TTimeStamp((time_t)startTime,0).AsString(),
123 TTimeStamp((time_t)endTime,0).AsString()));
80c68391 124
132753b5 125 // Preprocessor configuration
126 AliCDBEntry* entry = GetFromOCDB("Config", "Preprocessor");
127 if (entry) fConfEnv = (TEnv*) entry->GetObject();
128 if ( fConfEnv==0 ) {
8f79a6d7 129 Log("AliEMCALPreprocessor: Preprocessor Config OCDB entry missing.\n");
132753b5 130 fConfigOK = kFALSE;
131 return;
09744db4 132 }
133
132753b5 134 // Temperature sensors
135 TTree *confTree = 0;
09744db4 136
132753b5 137 TString tempConf = fConfEnv->GetValue("Temperature","ON");
138 tempConf.ToUpper();
139 if (tempConf != "OFF" ) {
140 entry = GetFromOCDB("Config", "Temperature");
141 if (entry) confTree = (TTree*) entry->GetObject();
142 if ( confTree==0 ) {
8f79a6d7 143 Log("AliEMCALPreprocessor: Temperature Config OCDB entry missing.\n");
132753b5 144 fConfigOK = kFALSE;
145 return;
09744db4 146 }
132753b5 147 fTemp = new AliEMCALSensorTempArray(startTimeLocal, fEndTime, confTree, kAmandaTemp);
148 fTemp->SetValCut(kValCutTemp);
149 fTemp->SetDiffCut(kDiffCutTemp);
150 }
151
152 return;
153}
09744db4 154
132753b5 155//______________________________________________________________________________________________
156UInt_t AliEMCALPreprocessor::Process(TMap* dcsAliasMap)
157{
158 // Fills data into EMCAL calibrations objects
159 // Amanda servers provide information directly through dcsAliasMap
160
161 if (!fConfigOK) return kReturnCodeNoInfo;
162 UInt_t result = 0;
163 TObjArray *resultArray = new TObjArray();
164 TString errorHandling = fConfEnv->GetValue("ErrorHandling","ON");
165 errorHandling.ToUpper();
166 TObject * status;
167
168 UInt_t dcsResult=0;
169 if (errorHandling == "OFF" ) {
170 if (!dcsAliasMap) dcsResult = kReturnCodeNoEntries;
171 if (dcsAliasMap->GetEntries() == 0 ) dcsResult = kReturnCodeNoEntries;
172 status = new TParameter<int>("dcsResult",dcsResult);
173 resultArray->Add(status);
174 }
175 else {
176 if (!dcsAliasMap) return kReturnCodeNoInfo;
177 if (dcsAliasMap->GetEntries() == 0 ) return kReturnCodeNoInfo;
178 }
179
180 TString runType = GetRunType();
181
182 // Temperature sensors are processed by AliEMCALCalTemp
183 TString tempConf = fConfEnv->GetValue("Temperature","ON");
184 tempConf.ToUpper();
185 if (tempConf != "OFF" ) {
186 UInt_t tempResult = MapTemperature(dcsAliasMap);
187 result=tempResult;
188 status = new TParameter<int>("tempResult",tempResult);
189 resultArray->Add(status);
190 }
191
192 // Other calibration information will be retrieved through FXS files
193 // examples:
194 // TList* fileSourcesDAQ = GetFile(AliShuttleInterface::kDAQ, "pedestals");
195 // const char* fileNamePed = GetFile(AliShuttleInterface::kDAQ, "pedestals", "LDC1");
196 //
197 // TList* fileSourcesHLT = GetFile(AliShuttleInterface::kHLT, "calib");
198 // const char* fileNameHLT = GetFile(AliShuttleInterface::kHLT, "calib", "LDC1");
199
200 // PEDESTAL ENTRIES:
201
8bc94fba 202 if (runType == kPedestalRunType || runType == kStandAloneRunType) {
132753b5 203 Int_t numSources = 1;
204 Int_t pedestalSource[2] = {AliShuttleInterface::kDAQ, AliShuttleInterface::kHLT} ;
205 TString source = fConfEnv->GetValue("Pedestal","DAQ");
206 source.ToUpper();
207 if (source != "OFF" ) {
208 if ( source == "HLT") pedestalSource[0] = AliShuttleInterface::kHLT;
209 if (!GetHLTStatus()) pedestalSource[0] = AliShuttleInterface::kDAQ;
210 if (source == "HLTDAQ" ) {
211 numSources=2;
212 pedestalSource[0] = AliShuttleInterface::kHLT;
213 pedestalSource[1] = AliShuttleInterface::kDAQ;
214 }
215 if (source == "DAQHLT" ) numSources=2;
216 UInt_t pedestalResult=0;
217 for (Int_t i=0; i<numSources; i++ ) {
218 pedestalResult = ExtractPedestals(pedestalSource[i]);
219 if ( pedestalResult == 0 ) break;
220 }
221 result += pedestalResult;
222 status = new TParameter<int>("pedestalResult",pedestalResult);
223 resultArray->Add(status);
09744db4 224 }
132753b5 225 }
226
227 // SIGNAL/LED ENTRIES:
228
8bc94fba 229 if( runType == kStandAloneRunType ) {
132753b5 230 Int_t numSources = 1;
231 Int_t signalSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
232 TString source = fConfEnv->GetValue("Signal","DAQ");
233 source.ToUpper();
234 if ( source != "OFF") {
235 if ( source == "HLT") signalSource[0] = AliShuttleInterface::kHLT;
236 if (!GetHLTStatus()) signalSource[0] = AliShuttleInterface::kDAQ;
237 if (source == "HLTDAQ" ) {
238 numSources=2;
239 signalSource[0] = AliShuttleInterface::kHLT;
240 signalSource[1] = AliShuttleInterface::kDAQ;
241 }
242 if (source == "DAQHLT" ) numSources=2;
243 UInt_t signalResult=0;
244 for (Int_t i=0; i<numSources; i++ ) {
245 signalResult = ExtractSignal(signalSource[i]);
246 if ( signalResult == 0 ) break;
09744db4 247 }
132753b5 248 result += signalResult;
249 status = new TParameter<int>("signalResult",signalResult);
250 resultArray->Add(status);
09744db4 251 }
132753b5 252 }
253
254
255 // overall status at the end
256 if (errorHandling == "OFF" ) {
257 AliCDBMetaData metaData;
258 metaData.SetBeamPeriod(0);
259 metaData.SetResponsible(kMetaResponsible);
260 metaData.SetComment("Preprocessor AliEMCAL status.");
261 Store("Calib", "PreprocStatus", resultArray, &metaData, 0, kFALSE);
262 resultArray->Delete();
263 return 0;
264 }
265 else {
266 return result;
267 }
268
269}
270//______________________________________________________________________________________________
271UInt_t AliEMCALPreprocessor::MapTemperature(TMap* dcsAliasMap)
272{
273 // extract DCS temperature maps. Perform fits to save space
274 UInt_t result=0;
275
276 TMap *map = fTemp->ExtractDCS(dcsAliasMap);
277 if (map) {
278 fTemp->MakeSplineFit(map);
279 Double_t fitFraction = 1.0*fTemp->NumFits()/fTemp->NumSensors();
280 if (fitFraction > kFitFraction ) {
281 AliInfo(Form("Temperature values extracted, fits performed.\n"));
282 }
283 else {
284 Log ("Too few temperature maps fitted. \n");
285 result = kReturnCodeNoInfo;
286 }
287 }
288 else {
289 Log("No temperature map extracted. \n");
290 result = kReturnCodeNoInfo;
291 }
292 delete map;
293 // Now store the final CDB file
294
295 if ( result == 0 ) { // some info was found
296 AliCDBMetaData metaData;
297 metaData.SetBeamPeriod(0);
298 metaData.SetResponsible(kMetaResponsible);
299 metaData.SetComment(kMetaComment);
09744db4 300
132753b5 301 Bool_t storeOK = Store("Calib", "Temperature", fTemp, &metaData, 0, kFALSE);
302 if ( !storeOK ) result=1;
303 }
304
305 return result;
306}
307
308//______________________________________________________________________________________________
309UInt_t AliEMCALPreprocessor::ExtractPedestals(Int_t sourceFXS)
310{
311 UInt_t result=0;
312 //
313 // Read pedestal file from file exchange server
314 // Keep original entry from OCDB in case no new pedestals are available
315 //
316 AliCaloCalibPedestal *calibPed=0;
317 AliCDBEntry* entry = GetFromOCDB("Calib", "Pedestals");
318 if (entry) calibPed = (AliCaloCalibPedestal*)entry->GetObject();
319 if ( calibPed==NULL ) {
8f79a6d7 320 Log("AliEMCALPreprocessor: No previous EMCAL pedestal entry available.\n");
132753b5 321 calibPed = new AliCaloCalibPedestal(AliCaloCalibPedestal::kEmCal);
322 }
323
324 TList* list = GetFileSources(sourceFXS,"pedestals");
325 if (list && list->GetEntries()>0) {
326
327 calibPed->Reset(); // let's make a fresh start before possibly adding stuff below
328
329 // loop through all files from LDCs
330
331 int changes = 0;
332 UInt_t index = 0;
333 while (list->At(index)!=NULL) {
334 TObjString* fileNameEntry = (TObjString*) list->At(index);
335 if (fileNameEntry!=NULL) {
336 TString fileName = GetFile(sourceFXS, "pedestals",
337 fileNameEntry->GetString().Data());
338 TFile *f = TFile::Open(fileName);
339 if (!f) {
340 Log ("Error opening pedestal file.");
341 result = kReturnCodeNoObject;
342 break;
343 }
344 AliCaloCalibPedestal *calPed;
345 f->GetObject("emcCalibPedestal",calPed);
346 if ( !calPed ) {
347 Log ("No pedestal calibration object in file.");
348 result = kReturnCodeNoObject;
349 break;
350 }
351 if ( calPed->GetNEvents()>0 && calPed->GetNChanFills()>0 ) {
352 // add info for the modules available in the present file
353 Bool_t status = calibPed->AddInfo(calPed);
354 if (status) { changes++; }
b3204f43 355 }
132753b5 356
357 delete calPed;
358 f->Close();
09744db4 359 }
132753b5 360 index++;
361 } // while(list)
362
363 //
364 // Store updated pedestal entry to OCDB
365 //
366 if (changes>0) {
367 AliCDBMetaData metaData;
368 metaData.SetBeamPeriod(0);
369 metaData.SetResponsible(kMetaResponsible);
370 metaData.SetComment(kMetaComment);
371
372 Bool_t storeOK = StoreReferenceData("Calib", "Pedestals", calibPed, &metaData);
373 if ( !storeOK ) result++;
09744db4 374 }
132753b5 375 }
376 else {
377 Log ("Error: no entries in input file list!");
378 result = kReturnCodeNoEntries;
379 }
b3204f43 380
132753b5 381 return result;
382}
383
384//______________________________________________________________________________________________
385UInt_t AliEMCALPreprocessor::ExtractSignal(Int_t sourceFXS)
386{
387 UInt_t result=0;
388 //
389 // Read signal file from file exchange server
390 // Keep original entry from OCDB in case no new signal are available
391 //
392 AliCaloCalibSignal *calibSig=0;
81c4c29b 393 AliCDBEntry* entry = GetFromOCDB("Calib", "LED");
132753b5 394 if (entry) calibSig = (AliCaloCalibSignal*)entry->GetObject();
395 if ( calibSig==NULL ) {
8f79a6d7 396 Log("AliEMCALPreprocessor: No previous EMCAL signal entry available.\n");
132753b5 397 calibSig = new AliCaloCalibSignal(AliCaloCalibSignal::kEmCal);
398 }
b3204f43 399
132753b5 400 TList* list = GetFileSources(sourceFXS,"signal");
401 if (list && list->GetEntries()>0) {
32fbe941 402
403 /* DS: 17 June 2008 - commented out this reset to avoid crash in shuttle.
404 Not sure why it occurs, in standalone tests a Reset() seemed to work OK..
132753b5 405
406 calibSig->Reset(); // let's make a fresh start before possibly adding stuff below
32fbe941 407 */
132753b5 408
409 // loop through all files from LDCs
410
411 int changes = 0;
412 UInt_t index = 0;
413 while (list->At(index)!=NULL) {
414 TObjString* fileNameEntry = (TObjString*) list->At(index);
415 if (fileNameEntry!=NULL) {
416 TString fileName = GetFile(sourceFXS, "signal",
417 fileNameEntry->GetString().Data());
418 TFile *f = TFile::Open(fileName);
419 if (!f) {
420 Log ("Error opening signal file.");
421 result = kReturnCodeNoObject;
422 break;
423 }
424 AliCaloCalibSignal *calSig;
425 f->GetObject("emcCalibSignal",calSig);
426 if ( !calSig ) {
427 Log ("No signal calibration object in file.");
428 result = kReturnCodeNoObject;
429 break;
430 }
431 if ( calSig->GetNEvents()>0 ) {
432 // add info for the modules available in the present file
433 Bool_t status = calibSig->AddInfo(calSig);
434 if (status) { changes++; }
435 }
436
437 delete calSig;
438 f->Close();
439 }
440 index++;
441 } // while(list)
442
443 //
444 // Store updated signal entry to OCDB
445 //
446 if (changes>0) {
447 AliCDBMetaData metaData;
448 metaData.SetBeamPeriod(0);
449 metaData.SetResponsible(kMetaResponsible);
450 metaData.SetComment(kMetaComment);
451
81c4c29b 452 Bool_t storeOK = Store("Calib", "LED", calibSig, &metaData, 0, kFALSE);
132753b5 453 if ( !storeOK ) result++;
454 }
455 }
456 else {
457 Log ("Error: no entries in input file list!");
458 result = kReturnCodeNoEntries;
459 }
460
461 return result;
9e788b10 462}
132753b5 463
464