AddTaskFemto for train update
[u/mrichter/AliRoot.git] / ITS / AliITSPreprocessorSPD.cxx
CommitLineData
b15de2d2 1///////////////////////////////////////////////
2// Author: Henrik Tydesjo //
3// Preprocessor Class for the SPD //
4// //
5///////////////////////////////////////////////
3f0e013c 6
3f0e013c 7#include "AliITSPreprocessorSPD.h"
b15de2d2 8#include "AliITSCalibrationSPD.h"
b15de2d2 9#include "AliITSOnlineCalibrationSPDhandler.h"
03fc6773 10#include "AliCDBEntry.h"
b15de2d2 11#include "AliCDBMetaData.h"
03fc6773 12#include "AliShuttleInterface.h"
b15de2d2 13#include "AliLog.h"
03fc6773 14#include <TTimeStamp.h>
15#include <TObjString.h>
16#include <TSystem.h>
4f88491f 17#include <fstream>
18
19/* $Id$ */
3f0e013c 20
b15de2d2 21ClassImp(AliITSPreprocessorSPD)
3f0e013c 22
b15de2d2 23//______________________________________________________________________________________________
24AliITSPreprocessorSPD::AliITSPreprocessorSPD(AliShuttleInterface* shuttle) :
4f88491f 25 AliPreprocessor("SPD", shuttle), fIdList()
3f0e013c 26{
b15de2d2 27 // constructor
b2435c94 28 AddRunType("DAQ_MIN_TH_SCAN");
29 AddRunType("DAQ_MEAN_TH_SCAN");
ad07f1ab 30 AddRunType("DAQ_GEN_DAC_SCAN");
b2435c94 31 AddRunType("DAQ_UNIFORMITY_SCAN");
32 AddRunType("DAQ_NOISY_PIX_SCAN");
33 AddRunType("DAQ_PIX_DELAY_SCAN");
34 AddRunType("DAQ_FO_UNIF_SCAN");
35 AddRunType("PHYSICS");
36
4f88491f 37 fIdList.SetOwner(kTRUE);
3f0e013c 38}
39
b15de2d2 40//______________________________________________________________________________________________
41AliITSPreprocessorSPD::~AliITSPreprocessorSPD()
3f0e013c 42{
b15de2d2 43 // destructor
3f0e013c 44}
45
b15de2d2 46//______________________________________________________________________________________________
47void AliITSPreprocessorSPD::Initialize(Int_t run, UInt_t startTime,
48 UInt_t endTime)
3f0e013c 49{
b15de2d2 50 // initialize
51 AliPreprocessor::Initialize(run, startTime, endTime);
3f0e013c 52
b15de2d2 53 AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
54 TTimeStamp(startTime).AsString(),
55 TTimeStamp(endTime).AsString()));
3f0e013c 56}
fcf95fc7 57
b15de2d2 58//______________________________________________________________________________________________
0c0426a2 59UInt_t AliITSPreprocessorSPD::Process(TMap* /*dcsAliasMap*/)
fcf95fc7 60{
b15de2d2 61 // Do the actual preprocessing
fcf95fc7 62
03fc6773 63
53ae21ce 64 // *** GET RUN TYPE ***
ad18504e 65
66 TString runType = GetRunType();
03fc6773 67
4f88491f 68
69 fIdList.Clear();
af866a70 70
4f88491f 71
72 // ******************************************************************************************** //
73 // *** GET THE FILE IDs FOR DEBUGGING *** //
74 if (runType == "DAQ_MIN_TH_SCAN" ||
75 runType == "DAQ_MEAN_TH_SCAN" ||
ad07f1ab 76 runType == "DAQ_GEN_DAC_SCAN" ||
4f88491f 77 runType == "DAQ_UNIFORMITY_SCAN" ||
78 runType == "DAQ_NOISY_PIX_SCAN" ||
79 runType == "DAQ_PIX_DELAY_SCAN" ||
80 runType == "DAQ_FO_UNIF_SCAN" ||
81 runType == "PHYSICS") {
82 TString idListId = "SPD_id_list";
83 TList* list = GetFileSources(kDAQ,idListId.Data());
84 UInt_t nrIdFiles = 0;
85 if (list) {
86 TListIter *iter = new TListIter(list);
87 while (TObjString* fileNameEntry = (TObjString*) iter->Next()) {
88 TString fileName = GetFile(kDAQ, idListId.Data(), fileNameEntry->GetString().Data());
89 if (fileName.IsNull()) {
90 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
91 return 1;
92 }
93 nrIdFiles++;
94 ifstream idFile;
95 idFile.open(fileName.Data(), ifstream::in);
96 if (idFile.fail()) {
97 Log(Form("Could not open file (%s) for reading.",fileName.Data()));
98 return 1;
99 }
100 else {
101 while(1) {
102 Char_t id[50];
103 idFile >> id;
104 if (idFile.eof()) break;
105 // Add id to the list;
106 fIdList.AddLast(new TObjString(id));
107 }
108 }
109 idFile.close();
110 }
111 delete iter;
112 }
113 if (nrIdFiles==0) {
114 Log("Failed to retrieve any id list file.");
115 return 1;
116 }
117 }
118
ad18504e 119
6727e2db 120 // ******************************************************************************************** //
53ae21ce 121 // *** REFERENCE DATA *** //
122
af866a70 123 // SCAN runs:
53ae21ce 124 if (runType == "DAQ_MIN_TH_SCAN" ||
125 runType == "DAQ_MEAN_TH_SCAN" ||
ad07f1ab 126 runType == "DAQ_GEN_DAC_SCAN" ||
53ae21ce 127 runType == "DAQ_UNIFORMITY_SCAN" ||
128 runType == "DAQ_NOISY_PIX_SCAN" ||
af866a70 129 runType == "DAQ_PIX_DELAY_SCAN") {
130 // Store scan container files for all equipments used - as reference data
131 // ids from FXS follow ("SPD_ref_scan_%d",eq), while Alien follow ("SPD_ref_scan_eq_%d",eq)
132 // the first part of the id is passed as argument here:
133 if (!StoreRefForIdStartingWith("SPD_ref_scan")) return 1;
134 }
135
136 // FO runs:
137 else if (runType == "DAQ_FO_UNIF_SCAN") {
138 // Store fo container files for all equipments used - as reference data
139 // ids from FXS follow ("SPD_ref_fo_%d",eq), while Alien follow ("SPD_ref_fo_eq_%d",eq)
140 // the first part of the id is passed as argument here:
141 if (!StoreRefForIdStartingWith("SPD_ref_fo")) return 1;
0c0426a2 142 }
03fc6773 143
53ae21ce 144 // Physics runs (online monitoring):
af866a70 145 else if (runType == "PHYSICS") {
146 // Store the phys "per run" container files - as reference data
4f88491f 147 if (!StoreRefFromTarForId("SPD_ref_phys")) return 1;
af866a70 148 // Store the phys "dead" container files - as reference data
4f88491f 149 if (!StoreRefFromTarForId("SPD_ref_phys_dead")) return 1;
53ae21ce 150 }
03fc6773 151
4f88491f 152
4f88491f 153
6727e2db 154 // ******************************************************************************************** //
53ae21ce 155 // *** NOISY AND DEAD DATA *** //
03fc6773 156
6727e2db 157 // Standalone runs:
158 if (runType == "DAQ_NOISY_PIX_SCAN") {
159 // Retrieve and unpack tared calibration files from FXS
4f88491f 160 TString id = "SPD_scan_noisy";
161 TList* list = GetFileSources(kDAQ,id.Data());
6727e2db 162 if (list) {
163 UInt_t index = 0;
164 while (list->At(index)!=NULL) {
165 TObjString* fileNameEntry = (TObjString*) list->At(index);
4f88491f 166 TString fileName = GetFile(kDAQ, id.Data(), fileNameEntry->GetString().Data());
167 if (fileName.IsNull()) {
168 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
169 return 1;
170 }
171 if (!RemoveIdFromList("SPD_scan_noisy")) {
172 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id.Data()));
173 }
6727e2db 174 TString command = Form("tar -xf %s",fileName.Data());
175 gSystem->Exec(command.Data());
176 index++;
177 }
178 }
179 // Create new database entries
180 TObjArray* spdEntryNoisy = new TObjArray(240);
181 spdEntryNoisy->SetOwner(kTRUE);
182 for(UInt_t module=0; module<240; module++){
183 AliITSCalibrationSPD* calObj = new AliITSCalibrationSPD();
184 spdEntryNoisy->Add(calObj);
185 }
186 // Add noisy from the copied FXS files
187 AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
188 TString fileLoc = ".";
189 handler->SetFileLocation(fileLoc.Data());
190 handler->ReadNoisyFromFiles();
191 for (Int_t module=0; module<240; module++) {
4821f07d 192 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetNrBadSingle( handler->GetNrNoisySingle(module) );
6727e2db 193 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetBadList( handler->GetNoisyArray(module) );
194 }
195 delete handler;
196 // Store the new calibration objects in OCDB
197 Log("Noisy lists (scan) will be stored...");
198 AliCDBMetaData metaData;
199 metaData.SetBeamPeriod(0);
200 metaData.SetResponsible("Henrik Tydesjo");
201 metaData.SetComment("Created by SPD PreProcessor");
202 // validity for this run until infinity
203 if (!Store("Calib", "SPDNoisy", spdEntryNoisy, &metaData, 0, kTRUE)) {
204 Log("Failed to store calibration data.");
205 return 1;
206 }
207 Log("Database updated.");
208 delete spdEntryNoisy;
209 }
210
211 // Physics runs (online monitoring):
212 else if (runType == "PHYSICS") {
213
214 // Noisy pixels:
215 // Read noisy from previous calibration
216 AliCDBEntry* cdbEntry = GetFromOCDB("Calib", "SPDNoisy");
217 TObjArray* spdEntryNoisy;
53ae21ce 218 if(cdbEntry) {
6727e2db 219 spdEntryNoisy = (TObjArray*)cdbEntry->GetObject();
220 if(!spdEntryNoisy) return 1;
03fc6773 221 }
53ae21ce 222 else {
223 Log("Old calibration not found in database. This is required for further processing.");
0c0426a2 224 return 1;
225 }
6727e2db 226 AliITSOnlineCalibrationSPDhandler* handOld = new AliITSOnlineCalibrationSPDhandler();
227 handOld->ReadNoisyFromCalibObj(spdEntryNoisy);
228 // Retrieve and unpack tared calibration files from FXS
4f88491f 229 TString idN = "SPD_phys_noisy";
230 TList* listN = GetFileSources(kDAQ,idN.Data());
6727e2db 231 if (listN) {
232 UInt_t index = 0;
233 while (listN->At(index)!=NULL) {
234 TObjString* fileNameEntry = (TObjString*) listN->At(index);
4f88491f 235 TString fileName = GetFile(kDAQ, idN.Data(), fileNameEntry->GetString().Data());
236 if (fileName.IsNull()) {
237 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
238 return 1;
239 }
240 if (!RemoveIdFromList(idN.Data())) {
241 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",idN.Data()));
242 }
6727e2db 243 TString command = Form("tar -xf %s",fileName.Data());
244 gSystem->Exec(command.Data());
245 index++;
53ae21ce 246 }
6727e2db 247 }
248 AliITSOnlineCalibrationSPDhandler* handNew = new AliITSOnlineCalibrationSPDhandler();
249 handNew->SetFileLocation(".");
250 handNew->ReadNoisyFromFiles();
251 // add the new list to the old one
252 UInt_t nrNewNoisy = handOld->AddNoisyFrom(handNew);
253 // If new noisy pixels were found: Update calibration objects
254 if (nrNewNoisy>0) {
53ae21ce 255 for (Int_t module=0; module<240; module++) {
4821f07d 256 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetNrBadSingle( handOld->GetNrNoisySingle(module) );
6727e2db 257 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetBadList( handOld->GetNoisyArray(module) );
53ae21ce 258 }
6727e2db 259 // Store the new calibration objects in OCDB
260 Log("Noisy lists (phys) will be stored...");
261 AliCDBMetaData metaData;
262 metaData.SetBeamPeriod(0);
263 metaData.SetResponsible("Henrik Tydesjo");
264 metaData.SetComment("Created by SPD PreProcessor");
265 // validity for this run only
266 if (!Store("Calib", "SPDNoisy", spdEntryNoisy, &metaData, 0, kFALSE)) {
267 Log("Failed to store calibration data.");
268 return 1;
53ae21ce 269 }
6727e2db 270 Log("Database updated.");
53ae21ce 271 }
6727e2db 272 delete handNew;
53ae21ce 273
6727e2db 274 // Dead pixels:
275 // Retrieve and unpack tared calibration files from FXS
4f88491f 276 TString idD = "SPD_phys_dead";
277 TList* listD = GetFileSources(kDAQ,idD.Data());
278 UInt_t nrPhysDeadFiles = 0;
6727e2db 279 if (listD) {
280 UInt_t index = 0;
281 while (listD->At(index)!=NULL) {
282 TObjString* fileNameEntry = (TObjString*) listD->At(index);
4f88491f 283 TString fileName = GetFile(kDAQ, idD.Data(), fileNameEntry->GetString().Data());
284 if (fileName.IsNull()) {
285 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
286 return 1;
287 }
288 nrPhysDeadFiles++;
289 if (!RemoveIdFromList("SPD_phys_dead")) {
290 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",idD.Data()));
291 }
6727e2db 292 TString command = Form("tar -xf %s",fileName.Data());
293 gSystem->Exec(command.Data());
294 index++;
295 }
53ae21ce 296 }
4f88491f 297 if (nrPhysDeadFiles==0) {
298 Log(Form("Could not find files with id %s. Should be present for each run.",idD.Data()));
299 return 1;
300 }
6727e2db 301 // Create new database entries
302 TObjArray* spdEntryDead = new TObjArray(240);
303 spdEntryDead->SetOwner(kTRUE);
304 for(UInt_t module=0; module<240; module++){
305 AliITSCalibrationSPD* calObj = new AliITSCalibrationSPD();
306 spdEntryDead->Add(calObj);
307 }
308 // Add dead from the copied FXS files
309 handOld->SetFileLocation(".");
b696414b 310 handOld->ReadSilentFromFiles();
478d804c 311 for (UInt_t module=0; module<240; module++) {
312 AliITSCalibrationSPD* calibSPD = (AliITSCalibrationSPD*) spdEntryDead->At(module);
4821f07d 313 calibSPD->SetNrBadSingle( handOld->GetNrDeadSingle(module) );
478d804c 314 calibSPD->SetBadList( handOld->GetDeadArray(module) );
315 for (UInt_t chipIndex=0; chipIndex<5; chipIndex++) {
316 UInt_t eq,hs,chip,col,row;
317 AliITSRawStreamSPD::OfflineToOnline(module, chipIndex*32, 0, eq, hs, chip, col, row);
318 if (handOld->IsSilentChip(eq,hs,chip)) {
319 calibSPD->SetChipBad(chipIndex);
320 }
321 else {
322 calibSPD->UnSetChipBad(chipIndex);
323 }
324 }
6727e2db 325 }
326 delete handOld;
327 // Store the new calibration objects in OCDB
328 Log("Dead lists (phys) will be stored...");
329 AliCDBMetaData metaData;
330 metaData.SetBeamPeriod(0);
331 metaData.SetResponsible("Henrik Tydesjo");
332 metaData.SetComment("Created by SPD PreProcessor");
333 // validity for this run only
334 if (!Store("Calib", "SPDDead", spdEntryDead, &metaData, 0, kFALSE)) {
335 Log("Failed to store calibration data.");
336 return 1;
337 }
338 Log("Database updated.");
339 delete spdEntryDead;
53ae21ce 340
03fc6773 341 }
342
03fc6773 343
af866a70 344
345 // ******************************************************************************************** //
4f88491f 346 // check that there are no ids left in the list:
347 if (fIdList.First()!=NULL) {
348 TString logMessage = "";
349 TListIter *iter = new TListIter(&fIdList);
350 while (TObjString *st = (TObjString*)iter->Next()) {
351 logMessage.Append(st->GetString());
352 logMessage.Append(" ");
353 }
354 delete iter;
355 Log(Form("Files with the following ids were never retrieved: %s.",logMessage.Data()));
356 return 1;
357 }
4f88491f 358 fIdList.Clear();
359
af866a70 360
53ae21ce 361 return 0; // 0 means success
03fc6773 362
fcf95fc7 363}
4f88491f 364//_________________________________________________________________________________________
365Bool_t AliITSPreprocessorSPD::RemoveIdFromList(const Char_t *id) {
366 // removes id from the list of ids
367 Bool_t found = kFALSE;
368 TListIter *iter = new TListIter(&fIdList);
369 while (TObjString *st = (TObjString*)iter->Next()) {
370 if (st->GetString().CompareTo(id)==0) {
371 fIdList.Remove(st);
372 found = kTRUE;
373 break;
374 }
375 }
376 delete iter;
377 return found;
378}
379//_________________________________________________________________________________________
af866a70 380Bool_t AliITSPreprocessorSPD::StoreRefForIdStartingWith(const Char_t *idStart) {
381 // Store the standalone container files as reference data (0 or 1 file for each equipment)
382 // idStart is the first part of the id string (which also should contain the actual eq)
383 for (UInt_t eq=0; eq<20; eq++) {
384 TString id = Form("%s_%d",idStart,eq);
385 TList* list = GetFileSources(kDAQ,id.Data()); // (the id should be unique, so always 1 file)
386 if (list) {
387 TObjString* fileNameEntry = (TObjString*) list->First();
388 if (fileNameEntry!=NULL) {
389 TString fileName = GetFile(kDAQ, id, fileNameEntry->GetString().Data());
390 if (fileName.IsNull()) {
391 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
392 return kFALSE;
393 }
394 if (!RemoveIdFromList(id.Data())) {
395 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id.Data()));
396 }
397 TString refCAT = Form("%s_eq_%d",idStart,eq);
398 if (!StoreReferenceFile(fileName.Data(),refCAT.Data())) {
399 Log(Form("Failed to store reference file %s.",fileName.Data()));
400 return kFALSE;
401 }
402 }
403 }
404 }
405 return kTRUE;
406}
407//_________________________________________________________________________________________
4f88491f 408Bool_t AliITSPreprocessorSPD::StoreRefFromTarForId(const Char_t *id) {
409 // store reference files from tar file for the id given (this is just to not duplicate code)
410 TList* list = GetFileSources(kDAQ,id);
411 if (list) {
412 UInt_t index = 0;
413 while (list->At(index)!=NULL) {
414 TObjString* fileNameEntry = (TObjString*) list->At(index);
415 TString fileName = GetFile(kDAQ, id, fileNameEntry->GetString().Data());
416 if (fileName.IsNull()) {
417 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
418 return kFALSE;
419 }
420 if (!RemoveIdFromList(id)) {
421 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id));
422 }
423 // get the file names from the tar file
424 // TString pwd = gSystem->pwd();
425 // TString tempFileName = Form("%s/tempTar.txt",pwd.Data());
426 TString tempFileName = "tempTar.txt";
427 TString command = Form("tar -tf %s > %s",fileName.Data(),tempFileName.Data());
428 gSystem->Exec(command.Data());
429 TList fList;
430 ifstream tempFile;
431 tempFile.open(tempFileName.Data(), ifstream::in);
432 if (tempFile.fail()) {
433 Log(Form("Could not open file (%s) for reading.",tempFileName.Data()));
434 return kFALSE;
435 }
436 else {
437 while(1) {
438 Char_t fileN[100];
439 tempFile >> fileN;
440 if (tempFile.eof()) break;
441 fList.AddLast(new TObjString(fileN));
442 }
443 }
444 // close and remove temp file
445 tempFile.close();
446 command = Form("rm -f %s",tempFileName.Data());
447 gSystem->Exec(command.Data());
448 // unpack
449 command = Form("tar -xf %s",fileName.Data());
450 gSystem->Exec(command.Data());
451 // store each file
452 UInt_t index2 = 0;
453 while (fList.At(index2)!=NULL) {
8f02666b 454 TString eqFileName = ((TObjString*)fList.At(index2))->GetString();
4f88491f 455 // get eq id
8f02666b 456 TString eqStr = eqFileName.Data();
4f88491f 457 UInt_t len = eqStr.Length();
458 eqStr.Replace(0,len-7,"",0);
459 eqStr.ReplaceAll("_",1,"",0);
460 eqStr.ReplaceAll(".root",5,"",0);
461 Int_t eqId = eqStr.Atoi();
462 if (eqId>=0 && eqId<20) {
463 TString refCAT = Form("%s_eq_%d",id,eqId);
8f02666b 464 if (!StoreReferenceFile(eqFileName.Data(),refCAT.Data())) {
465 Log(Form("Failed to store reference file %s.",eqFileName.Data()));
4f88491f 466 return kFALSE;
467 }
468 }
469 else {
8f02666b 470 Log(Form("Eq ID %d out of bounds for file %s",eqId,eqFileName.Data()));
4f88491f 471 fList.Clear();
472 return kFALSE;
473 }
474 index2++;
475 }
476 fList.Clear();
477 index++;
478 }
479 }
480 return kTRUE;
481}