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