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