1 ///////////////////////////////////////////////
2 // Author: Henrik Tydesjo //
3 // Preprocessor Class for the SPD //
5 ///////////////////////////////////////////////
7 #include "AliITSPreprocessorSPD.h"
8 #include "AliITSCalibrationSPD.h"
9 #include "AliITSOnlineCalibrationSPDhandler.h"
10 #include "AliCDBEntry.h"
11 #include "AliCDBMetaData.h"
12 #include "AliShuttleInterface.h"
14 #include <TTimeStamp.h>
15 #include <TObjString.h>
21 ClassImp(AliITSPreprocessorSPD)
23 //______________________________________________________________________________________________
24 AliITSPreprocessorSPD::AliITSPreprocessorSPD(AliShuttleInterface* shuttle) :
25 AliPreprocessor("SPD", shuttle), fIdList()
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");
37 fIdList.SetOwner(kTRUE);
40 //______________________________________________________________________________________________
41 AliITSPreprocessorSPD::~AliITSPreprocessorSPD()
46 //______________________________________________________________________________________________
47 void AliITSPreprocessorSPD::Initialize(Int_t run, UInt_t startTime,
51 AliPreprocessor::Initialize(run, startTime, endTime);
53 AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
54 TTimeStamp(startTime).AsString(),
55 TTimeStamp(endTime).AsString()));
58 //______________________________________________________________________________________________
59 UInt_t AliITSPreprocessorSPD::Process(TMap* /*dcsAliasMap*/)
61 // Do the actual preprocessing
64 // *** GET RUN TYPE ***
66 TString runType = GetRunType();
72 // ******************************************************************************************** //
73 // *** GET THE FILE IDs FOR DEBUGGING *** //
74 if (runType == "DAQ_MIN_TH_SCAN" ||
75 runType == "DAQ_MEAN_TH_SCAN" ||
76 runType == "DAQ_GEN_DAC_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());
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()));
95 idFile.open(fileName.Data(), ifstream::in);
97 Log(Form("Could not open file (%s) for reading.",fileName.Data()));
104 if (idFile.eof()) break;
105 // Add id to the list;
106 fIdList.AddLast(new TObjString(id));
114 Log("Failed to retrieve any id list file.");
120 // ******************************************************************************************** //
121 // *** REFERENCE DATA *** //
124 if (runType == "DAQ_MIN_TH_SCAN" ||
125 runType == "DAQ_MEAN_TH_SCAN" ||
126 runType == "DAQ_GEN_DAC_SCAN" ||
127 runType == "DAQ_UNIFORMITY_SCAN" ||
128 runType == "DAQ_NOISY_PIX_SCAN" ||
129 runType == "DAQ_PIX_DELAY_SCAN") {
130 // Store scan container files for all equipments used - as reference data
131 // ids from FXS follow ("SPD_ref_scan_%d",eq), while Alien follow ("SPD_ref_scan_eq_%d",eq)
132 // the first part of the id is passed as argument here:
133 if (!StoreRefForIdStartingWith("SPD_ref_scan")) return 1;
137 else if (runType == "DAQ_FO_UNIF_SCAN") {
138 // Store fo container files for all equipments used - as reference data
139 // ids from FXS follow ("SPD_ref_fo_%d",eq), while Alien follow ("SPD_ref_fo_eq_%d",eq)
140 // the first part of the id is passed as argument here:
141 if (!StoreRefForIdStartingWith("SPD_ref_fo")) return 1;
144 // Physics runs (online monitoring):
145 else if (runType == "PHYSICS") {
146 // Store the phys "per run" container files - as reference data
147 if (!StoreRefFromTarForId("SPD_ref_phys")) return 1;
148 // Store the phys "dead" container files - as reference data
149 if (!StoreRefFromTarForId("SPD_ref_phys_dead")) return 1;
154 // ******************************************************************************************** //
155 // *** NOISY AND DEAD DATA *** //
158 if (runType == "DAQ_NOISY_PIX_SCAN") {
159 // Retrieve and unpack tared calibration files from FXS
160 TString id = "SPD_scan_noisy";
161 TList* list = GetFileSources(kDAQ,id.Data());
164 while (list->At(index)!=NULL) {
165 TObjString* fileNameEntry = (TObjString*) list->At(index);
166 TString fileName = GetFile(kDAQ, id.Data(), fileNameEntry->GetString().Data());
167 if (fileName.IsNull()) {
168 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
171 if (!RemoveIdFromList("SPD_scan_noisy")) {
172 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id.Data()));
174 TString command = Form("tar -xf %s",fileName.Data());
175 gSystem->Exec(command.Data());
179 // Create new database entries
180 TObjArray* spdEntryNoisy = new TObjArray(240);
181 spdEntryNoisy->SetOwner(kTRUE);
182 for(UInt_t module=0; module<240; module++){
183 AliITSCalibrationSPD* calObj = new AliITSCalibrationSPD();
184 spdEntryNoisy->Add(calObj);
186 // Add noisy from the copied FXS files
187 AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
188 TString fileLoc = ".";
189 handler->SetFileLocation(fileLoc.Data());
190 handler->ReadNoisyFromFiles();
191 for (Int_t module=0; module<240; module++) {
192 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetNrBadSingle( handler->GetNrNoisySingle(module) );
193 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetBadList( handler->GetNoisyArray(module) );
196 // Store the new calibration objects in OCDB
197 Log("Noisy lists (scan) will be stored...");
198 AliCDBMetaData metaData;
199 metaData.SetBeamPeriod(0);
200 metaData.SetResponsible("Henrik Tydesjo");
201 metaData.SetComment("Created by SPD PreProcessor");
202 // validity for this run until infinity
203 if (!Store("Calib", "SPDNoisy", spdEntryNoisy, &metaData, 0, kTRUE)) {
204 Log("Failed to store calibration data.");
207 Log("Database updated.");
208 delete spdEntryNoisy;
211 // Physics runs (online monitoring):
212 else if (runType == "PHYSICS") {
215 // Read noisy from previous calibration
216 AliCDBEntry* cdbEntry = GetFromOCDB("Calib", "SPDNoisy");
217 TObjArray* spdEntryNoisy;
219 spdEntryNoisy = (TObjArray*)cdbEntry->GetObject();
220 if(!spdEntryNoisy) return 1;
223 Log("Old calibration not found in database. This is required for further processing.");
226 AliITSOnlineCalibrationSPDhandler* handOld = new AliITSOnlineCalibrationSPDhandler();
227 handOld->ReadNoisyFromCalibObj(spdEntryNoisy);
228 // Retrieve and unpack tared calibration files from FXS
229 TString idN = "SPD_phys_noisy";
230 TList* listN = GetFileSources(kDAQ,idN.Data());
233 while (listN->At(index)!=NULL) {
234 TObjString* fileNameEntry = (TObjString*) listN->At(index);
235 TString fileName = GetFile(kDAQ, idN.Data(), fileNameEntry->GetString().Data());
236 if (fileName.IsNull()) {
237 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
240 if (!RemoveIdFromList(idN.Data())) {
241 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",idN.Data()));
243 TString command = Form("tar -xf %s",fileName.Data());
244 gSystem->Exec(command.Data());
248 AliITSOnlineCalibrationSPDhandler* handNew = new AliITSOnlineCalibrationSPDhandler();
249 handNew->SetFileLocation(".");
250 handNew->ReadNoisyFromFiles();
251 // add the new list to the old one
252 UInt_t nrNewNoisy = handOld->AddNoisyFrom(handNew);
253 // If new noisy pixels were found: Update calibration objects
255 for (Int_t module=0; module<240; module++) {
256 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetNrBadSingle( handOld->GetNrNoisySingle(module) );
257 ((AliITSCalibrationSPD*) spdEntryNoisy->At(module)) -> SetBadList( handOld->GetNoisyArray(module) );
259 // Store the new calibration objects in OCDB
260 Log("Noisy lists (phys) will be stored...");
261 AliCDBMetaData metaData;
262 metaData.SetBeamPeriod(0);
263 metaData.SetResponsible("Henrik Tydesjo");
264 metaData.SetComment("Created by SPD PreProcessor");
265 // validity for this run only
266 if (!Store("Calib", "SPDNoisy", spdEntryNoisy, &metaData, 0, kFALSE)) {
267 Log("Failed to store calibration data.");
270 Log("Database updated.");
275 // Retrieve and unpack tared calibration files from FXS
276 TString idD = "SPD_phys_dead";
277 TList* listD = GetFileSources(kDAQ,idD.Data());
278 UInt_t nrPhysDeadFiles = 0;
281 while (listD->At(index)!=NULL) {
282 TObjString* fileNameEntry = (TObjString*) listD->At(index);
283 TString fileName = GetFile(kDAQ, idD.Data(), fileNameEntry->GetString().Data());
284 if (fileName.IsNull()) {
285 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
289 if (!RemoveIdFromList("SPD_phys_dead")) {
290 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",idD.Data()));
292 TString command = Form("tar -xf %s",fileName.Data());
293 gSystem->Exec(command.Data());
297 if (nrPhysDeadFiles==0) {
298 Log(Form("Could not find files with id %s. Should be present for each run.",idD.Data()));
301 // Create new database entries
302 TObjArray* spdEntryDead = new TObjArray(240);
303 spdEntryDead->SetOwner(kTRUE);
304 for(UInt_t module=0; module<240; module++){
305 AliITSCalibrationSPD* calObj = new AliITSCalibrationSPD();
306 spdEntryDead->Add(calObj);
308 // Add dead from the copied FXS files
309 handOld->SetFileLocation(".");
310 handOld->ReadSilentFromFiles();
311 for (UInt_t module=0; module<240; module++) {
312 AliITSCalibrationSPD* calibSPD = (AliITSCalibrationSPD*) spdEntryDead->At(module);
313 calibSPD->SetNrBadSingle( handOld->GetNrDeadSingle(module) );
314 calibSPD->SetBadList( handOld->GetDeadArray(module) );
315 for (UInt_t chipIndex=0; chipIndex<5; chipIndex++) {
316 UInt_t eq,hs,chip,col,row;
317 AliITSRawStreamSPD::OfflineToOnline(module, chipIndex*32, 0, eq, hs, chip, col, row);
318 if (handOld->IsSilentChip(eq,hs,chip)) {
319 calibSPD->SetChipBad(chipIndex);
322 calibSPD->UnSetChipBad(chipIndex);
327 // Store the new calibration objects in OCDB
328 Log("Dead lists (phys) will be stored...");
329 AliCDBMetaData metaData;
330 metaData.SetBeamPeriod(0);
331 metaData.SetResponsible("Henrik Tydesjo");
332 metaData.SetComment("Created by SPD PreProcessor");
333 // validity for this run only
334 if (!Store("Calib", "SPDDead", spdEntryDead, &metaData, 0, kFALSE)) {
335 Log("Failed to store calibration data.");
338 Log("Database updated.");
345 // ******************************************************************************************** //
346 // check that there are no ids left in the list:
347 if (fIdList.First()!=NULL) {
348 TString logMessage = "";
349 TListIter *iter = new TListIter(&fIdList);
350 while (TObjString *st = (TObjString*)iter->Next()) {
351 logMessage.Append(st->GetString());
352 logMessage.Append(" ");
355 Log(Form("Files with the following ids were never retrieved: %s.",logMessage.Data()));
361 return 0; // 0 means success
364 //_________________________________________________________________________________________
365 Bool_t AliITSPreprocessorSPD::RemoveIdFromList(const Char_t *id) {
366 // removes id from the list of ids
367 Bool_t found = kFALSE;
368 TListIter *iter = new TListIter(&fIdList);
369 while (TObjString *st = (TObjString*)iter->Next()) {
370 if (st->GetString().CompareTo(id)==0) {
379 //_________________________________________________________________________________________
380 Bool_t AliITSPreprocessorSPD::StoreRefForIdStartingWith(const Char_t *idStart) {
381 // Store the standalone container files as reference data (0 or 1 file for each equipment)
382 // idStart is the first part of the id string (which also should contain the actual eq)
383 for (UInt_t eq=0; eq<20; eq++) {
384 TString id = Form("%s_%d",idStart,eq);
385 TList* list = GetFileSources(kDAQ,id.Data()); // (the id should be unique, so always 1 file)
387 TObjString* fileNameEntry = (TObjString*) list->First();
388 if (fileNameEntry!=NULL) {
389 TString fileName = GetFile(kDAQ, id, fileNameEntry->GetString().Data());
390 if (fileName.IsNull()) {
391 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
394 if (!RemoveIdFromList(id.Data())) {
395 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id.Data()));
397 TString refCAT = Form("%s_eq_%d",idStart,eq);
398 if (!StoreReferenceFile(fileName.Data(),refCAT.Data())) {
399 Log(Form("Failed to store reference file %s.",fileName.Data()));
407 //_________________________________________________________________________________________
408 Bool_t AliITSPreprocessorSPD::StoreRefFromTarForId(const Char_t *id) {
409 // store reference files from tar file for the id given (this is just to not duplicate code)
410 TList* list = GetFileSources(kDAQ,id);
413 while (list->At(index)!=NULL) {
414 TObjString* fileNameEntry = (TObjString*) list->At(index);
415 TString fileName = GetFile(kDAQ, id, fileNameEntry->GetString().Data());
416 if (fileName.IsNull()) {
417 Log(Form("GetFile failed to retrieve file %s.",fileNameEntry->GetString().Data()));
420 if (!RemoveIdFromList(id)) {
421 Log(Form("Warning: Retrieved file with id %s, that was not in the id list!",id));
423 // get the file names from the tar file
424 // TString pwd = gSystem->pwd();
425 // TString tempFileName = Form("%s/tempTar.txt",pwd.Data());
426 TString tempFileName = "tempTar.txt";
427 TString command = Form("tar -tf %s > %s",fileName.Data(),tempFileName.Data());
428 gSystem->Exec(command.Data());
431 tempFile.open(tempFileName.Data(), ifstream::in);
432 if (tempFile.fail()) {
433 Log(Form("Could not open file (%s) for reading.",tempFileName.Data()));
440 if (tempFile.eof()) break;
441 fList.AddLast(new TObjString(fileN));
444 // close and remove temp file
446 command = Form("rm -f %s",tempFileName.Data());
447 gSystem->Exec(command.Data());
449 command = Form("tar -xf %s",fileName.Data());
450 gSystem->Exec(command.Data());
453 while (fList.At(index2)!=NULL) {
454 TString eqFileName = ((TObjString*)fList.At(index2))->GetString();
456 TString eqStr = eqFileName.Data();
457 UInt_t len = eqStr.Length();
458 eqStr.Replace(0,len-7,"",0);
459 eqStr.ReplaceAll("_",1,"",0);
460 eqStr.ReplaceAll(".root",5,"",0);
461 Int_t eqId = eqStr.Atoi();
462 if (eqId>=0 && eqId<20) {
463 TString refCAT = Form("%s_eq_%d",id,eqId);
464 if (!StoreReferenceFile(eqFileName.Data(),refCAT.Data())) {
465 Log(Form("Failed to store reference file %s.",eqFileName.Data()));
470 Log(Form("Eq ID %d out of bounds for file %s",eqId,eqFileName.Data()));