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