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