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