]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSOnlineCalibrationSPDhandler.cxx
Memory leak corrected (Ch. Klein-Boesing)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineCalibrationSPDhandler.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16
17 /* $Id$ */
18
19 //////////////////////////////////////////////////////////////////////
20 // Author: Henrik Tydesjo                                           //
21 // For easier handling of dead and noisy pixels they are kept in    //
22 // container maps (AliITSIntMap).                                   //
23 // The TArrayI objects that are put in the AliITSCalibrationSPD     //
24 // objects can be obtained from the methods GetDeadArray and        //
25 // GetNoisyArray.                                                   //
26 //////////////////////////////////////////////////////////////////////   
27
28 #include "AliITSOnlineCalibrationSPDhandler.h"
29 #include "AliITSOnlineCalibrationSPD.h"
30 #include "AliITSIntMap.h"
31 #include <TObjArray.h>
32 #include <TArrayI.h>
33 #include <TFile.h>
34 #include <TError.h>
35 #include <fstream>
36
37 #ifndef SPD_DA_OFF // you may want to exclude cdb classes, if using this class outside aliroot
38 #include "AliITSCalibrationSPD.h"
39 #include "AliCDBManager.h"
40 #include "AliCDBEntry.h"
41 #endif
42
43 AliITSOnlineCalibrationSPDhandler::AliITSOnlineCalibrationSPDhandler():
44   fFileLocation("."),
45   fModuleMapInited(kFALSE)
46 {
47   // constructor
48   for (UInt_t module=0; module<240; module++) {
49     fNrDead[module]=0;
50     fNrNoisy[module]=0;
51     fDeadPixelMap[module] = new AliITSIntMap();
52     fNoisyPixelMap[module] = new AliITSIntMap();
53   }
54 }
55
56 AliITSOnlineCalibrationSPDhandler::AliITSOnlineCalibrationSPDhandler(const AliITSOnlineCalibrationSPDhandler& handle): 
57   fFileLocation("."),
58   fModuleMapInited(kFALSE)
59 {
60   // copy constructor
61   for (UInt_t module=0; module<240; module++) {
62     fNrDead[module] = handle.fNrDead[module];
63     fNrNoisy[module] = handle.fNrNoisy[module];
64     fDeadPixelMap[module] = handle.fDeadPixelMap[module]->Clone();
65     fNoisyPixelMap[module] = handle.fNoisyPixelMap[module]->Clone();
66   }
67   fFileLocation = handle.fFileLocation;
68 }
69
70 AliITSOnlineCalibrationSPDhandler::~AliITSOnlineCalibrationSPDhandler() {
71   ClearMaps();
72 }
73
74 AliITSOnlineCalibrationSPDhandler& AliITSOnlineCalibrationSPDhandler::operator=(const AliITSOnlineCalibrationSPDhandler& handle) {
75   // assignment operator
76   if (this!=&handle) {
77     this->ClearMaps();
78     for (UInt_t module=0; module<240; module++) {
79       fNrDead[module] = handle.fNrDead[module];
80       fNrNoisy[module] = handle.fNrNoisy[module];
81       fDeadPixelMap[module] = handle.fDeadPixelMap[module]->Clone();
82       fNoisyPixelMap[module] = handle.fNoisyPixelMap[module]->Clone();
83     }
84     fFileLocation = handle.fFileLocation;
85   }
86   return *this;
87 }
88
89 void AliITSOnlineCalibrationSPDhandler::ClearMaps() {
90   // clear the lists of dead and noisy
91   ResetDead();
92   ResetNoisy();
93 }
94
95 Bool_t AliITSOnlineCalibrationSPDhandler::ReadFromFiles() {
96   // read dead and noisy files from file location. returns true if at least one file found
97   Bool_t returnval=kFALSE;
98   for (UInt_t module=0; module<240; module++) {
99     if (ReadFromFile(module)) {
100       returnval=kTRUE;
101     }
102   }
103   return returnval;
104 }
105 Bool_t AliITSOnlineCalibrationSPDhandler::ReadFromFile(UInt_t module) {
106   // read dead and noisy files for module from file location. 
107   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
108   return ReadFromFileName(fileName.Data());
109 }
110 Bool_t AliITSOnlineCalibrationSPDhandler::ReadFromFileName(const char *fileName) {
111   // read dead and noisy from file fileName
112   AliITSOnlineCalibrationSPD* calib;
113   FILE* fp0 = fopen(fileName, "r");
114   if (fp0 == NULL) {return kFALSE;}
115   else {
116     fclose(fp0);
117     TFile file(fileName, "READ");
118     if (file.IsOpen()) {
119       file.GetObject("AliITSOnlineCalibrationSPD", calib);
120       file.Close();
121       if (calib!=NULL) {
122         UInt_t module = calib->GetModuleNr();
123         Int_t nrDead=calib->GetNrDead();
124         for (Int_t index=0; index<nrDead; index++) {
125           UInt_t colM=calib->GetDeadColAt(index);
126           UInt_t row=calib->GetDeadRowAt(index);
127           SetDeadPixelM(module,colM,row);
128         }
129         Int_t nrNoisy=calib->GetNrNoisy();
130         for (Int_t index=0; index<nrNoisy; index++) {
131           UInt_t colM=calib->GetNoisyColAt(index);
132           UInt_t row=calib->GetNoisyRowAt(index);
133           SetNoisyPixelM(module,colM,row);
134         }
135       }
136     }
137   }
138   return kTRUE;
139 }
140 Bool_t AliITSOnlineCalibrationSPDhandler::ReadDeadFromFiles() {
141   // read dead files from file location. returns true if at least one file found
142   Bool_t returnval=kFALSE;
143   for (UInt_t module=0; module<240; module++) {
144     if (ReadDeadFromFile(module)) {
145       returnval=kTRUE;
146     }
147   }
148   return returnval;
149 }
150 Bool_t AliITSOnlineCalibrationSPDhandler::ReadDeadFromFile(UInt_t module) {
151   // read dead for module from file location. 
152   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
153   return ReadDeadFromFileName(fileName.Data());
154 }
155 Bool_t AliITSOnlineCalibrationSPDhandler::ReadDeadFromFileName(const char *fileName) {
156   // read dead from file fileName
157   AliITSOnlineCalibrationSPD* calib;
158   FILE* fp0 = fopen(fileName, "r");
159   if (fp0 == NULL) {return kFALSE;}
160   else {
161     fclose(fp0);
162     TFile file(fileName, "READ");
163     if (file.IsOpen()) {
164       file.GetObject("AliITSOnlineCalibrationSPD", calib);
165       file.Close();
166       if (calib!=NULL) {
167         UInt_t module = calib->GetModuleNr();
168         Int_t nrDead=calib->GetNrDead();
169         for (Int_t index=0; index<nrDead; index++) {
170           UInt_t colM=calib->GetDeadColAt(index);
171           UInt_t row=calib->GetDeadRowAt(index);
172           SetDeadPixelM(module,colM,row);
173         }
174       }
175     }
176   }
177   return kTRUE;
178 }
179 Bool_t AliITSOnlineCalibrationSPDhandler::ReadNoisyFromFiles() {
180   // read noisy files from file location. returns true if at least one file found
181   Bool_t returnval=kFALSE;
182   for (UInt_t module=0; module<240; module++) {
183     if (ReadNoisyFromFile(module)) {
184       returnval=kTRUE;
185     }
186   }
187   return returnval;
188 }
189 Bool_t AliITSOnlineCalibrationSPDhandler::ReadNoisyFromFile(UInt_t module) {
190   // read noisy for module from file location. 
191   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
192   return ReadNoisyFromFileName(fileName.Data());
193 }
194 Bool_t AliITSOnlineCalibrationSPDhandler::ReadNoisyFromFileName(const char *fileName) {
195   // read noisy from file fileName
196   AliITSOnlineCalibrationSPD* calib;
197   FILE* fp0 = fopen(fileName, "r");
198   if (fp0 == NULL) {return kFALSE;}
199   else {
200     fclose(fp0);
201     TFile file(fileName, "READ");
202     if (file.IsOpen()) {
203       file.GetObject("AliITSOnlineCalibrationSPD", calib);
204       file.Close();
205       if (calib!=NULL) {
206         UInt_t module = calib->GetModuleNr();
207         Int_t nrNoisy=calib->GetNrNoisy();
208         for (Int_t index=0; index<nrNoisy; index++) {
209           UInt_t colM=calib->GetNoisyColAt(index);
210           UInt_t row=calib->GetNoisyRowAt(index);
211           SetNoisyPixelM(module,colM,row);
212         }
213       }
214     }
215   }
216   return kTRUE;
217 }
218
219
220 UInt_t AliITSOnlineCalibrationSPDhandler::ReadDeadFromText(const char *fileName) {
221   // read dead from a text file (lines with eqId,hs,chip,col,row). returns nr of pixels added (not already there)
222   UInt_t newNrDead=0;
223   ifstream textFile;
224   textFile.open(fileName, ifstream::in);
225   if (textFile.fail()) {
226     Error("AliITSOnlineCalibrationSPDhandler::ReadDeadFromText","No noisy text file (%s) present.",fileName);
227   }
228   else {
229     while(1) {
230       UInt_t eqId,hs,chip,col,row;
231       textFile >> eqId; if (textFile.eof()) break;
232       textFile >> hs; if (textFile.eof()) break;
233       textFile >> chip; if (textFile.eof()) break;
234       textFile >> col; if (textFile.eof()) break;
235       textFile >> row; 
236       if (SetDeadPixel(eqId,hs,chip,col,row)) {
237         newNrDead++;
238       }
239       if (textFile.eof()) break;
240     }
241     textFile.close();
242   }
243   return newNrDead;
244 }
245
246 UInt_t AliITSOnlineCalibrationSPDhandler::ReadNoisyFromText(const char *fileName) {
247   // read noisy from a text file (lines with eqId,hs,chip,col,row). returns nr of pixels added (not already here)
248   UInt_t newNrNoisy=0;
249   ifstream textFile;
250   textFile.open(fileName, ifstream::in);
251   if (textFile.fail()) {
252     Error("AliITSOnlineCalibrationSPDhandler::ReadNoisyFromText","No noisy text file (%s) present.",fileName);
253   }
254   else {
255     while(1) {
256       UInt_t eqId,hs,chip,col,row;
257       textFile >> eqId; if (textFile.eof()) break;
258       textFile >> hs; if (textFile.eof()) break;
259       textFile >> chip; if (textFile.eof()) break;
260       textFile >> col; if (textFile.eof()) break;
261       textFile >> row; 
262       if (SetNoisyPixel(eqId,hs,chip,col,row)) {
263         newNrNoisy++;
264       }
265       if (textFile.eof()) break;
266     }
267     textFile.close();
268   }
269   return newNrNoisy;
270 }
271
272
273 #ifndef SPD_DA_OFF
274 Bool_t AliITSOnlineCalibrationSPDhandler::ReadModuleFromDB(UInt_t module, Int_t runNr) {
275   // reads dead and noisy pixels from DB for given module and runNr
276   AliCDBManager* man = AliCDBManager::Instance();
277   if(!man->IsDefaultStorageSet()) {
278     man->SetDefaultStorage("local://$ALICE_ROOT");
279   }
280   AliCDBEntry *cdbEntry = man->Get("ITS/Calib/CalibSPD", runNr);
281   TObjArray* spdEntry;
282   if(cdbEntry) {
283     spdEntry = (TObjArray*)cdbEntry->GetObject();
284     if(!spdEntry) return kFALSE;
285   }
286   else {
287     Warning("AliITSOnlineCalibrationSPDhandler::ReadModuleFromDB","Calibration for run %d not found in database.",runNr);
288     return kFALSE;
289   }
290   AliITSCalibrationSPD* calibSPD;
291   calibSPD = (AliITSCalibrationSPD*) spdEntry->At(module);
292   UInt_t nrDead = calibSPD->GetNrDead();
293   for (UInt_t index=0; index<nrDead; index++) {
294     UInt_t colM = calibSPD->GetDeadColAt(index);
295     UInt_t row  = calibSPD->GetDeadRowAt(index);
296     SetDeadPixelM(module,colM,row);
297   }
298   UInt_t nrNoisy = calibSPD->GetNrNoisy();
299   for (UInt_t index=0; index<nrNoisy; index++) {
300     UInt_t colM = calibSPD->GetNoisyColAt(index);
301     UInt_t row  = calibSPD->GetNoisyRowAt(index);
302     SetNoisyPixelM(module,colM,row);
303   }
304   spdEntry->SetOwner(kTRUE);
305   spdEntry->Clear();
306   return kTRUE;
307 }
308
309 Bool_t AliITSOnlineCalibrationSPDhandler::ReadFromDB(Int_t runNr) {
310   // reads dead and noisy pixels from DB for given runNr
311   // note that you may want to clear the lists (if they are not empty) before reading
312   AliCDBManager* man = AliCDBManager::Instance();
313   if(!man->IsDefaultStorageSet()) {
314     man->SetDefaultStorage("local://$ALICE_ROOT");
315   }
316   AliCDBEntry *cdbEntry = man->Get("ITS/Calib/CalibSPD", runNr);
317   TObjArray* spdEntry;
318   if(cdbEntry) {
319     spdEntry = (TObjArray*)cdbEntry->GetObject();
320     if(!spdEntry) return kFALSE;
321   }
322   else {
323     Warning("AliITSOnlineCalibrationSPDhandler::ReadFromDB","Calibration for run %d not found in database.",runNr);
324     return kFALSE;
325   }
326   AliITSCalibrationSPD* calibSPD;
327   for (UInt_t module=0; module<240; module++) {
328     //    printf("Reading module %d\n",module);
329     calibSPD = (AliITSCalibrationSPD*) spdEntry->At(module);
330     UInt_t nrDead = calibSPD->GetNrDead();
331     for (UInt_t index=0; index<nrDead; index++) {
332       UInt_t colM = calibSPD->GetDeadColAt(index);
333       UInt_t row  = calibSPD->GetDeadRowAt(index);
334       SetDeadPixelM(module,colM,row);
335     }
336     UInt_t nrNoisy = calibSPD->GetNrNoisy();
337     for (UInt_t index=0; index<nrNoisy; index++) {
338       UInt_t colM = calibSPD->GetNoisyColAt(index);
339       UInt_t row  = calibSPD->GetNoisyRowAt(index);
340       SetNoisyPixelM(module,colM,row);
341     }
342   }
343   spdEntry->SetOwner(kTRUE);
344   spdEntry->Clear();
345   return kTRUE;
346 }
347
348 Bool_t AliITSOnlineCalibrationSPDhandler::WriteToDB(Int_t runNrStart, Int_t runNrEnd) {
349   // writes dead and noisy pixels to DB for given runNr
350   // overwrites any previous entries
351   AliCDBManager* man = AliCDBManager::Instance();
352   if(!man->IsDefaultStorageSet()) {
353     man->SetDefaultStorage("local://$ALICE_ROOT");
354   }
355   AliCDBMetaData* metaData = new AliCDBMetaData();
356   metaData->SetResponsible("Henrik Tydesjo");
357   metaData->SetComment("Created by AliITSOnlineCalibrationSPDhandler.");
358   AliCDBId idCalSPD("ITS/Calib/CalibSPD",runNrStart,runNrEnd);
359   TObjArray* spdEntry = new TObjArray(240);
360   spdEntry->SetOwner(kTRUE);
361   for(UInt_t module=0; module<240; module++){
362     AliITSCalibrationSPD* calObj = new AliITSCalibrationSPD();
363     spdEntry->Add(calObj);
364   }
365   for(UInt_t module=0; module<240; module++){
366     ((AliITSCalibrationSPD*) spdEntry->At(module)) -> SetNrDead( GetNrDead(module) );
367     ((AliITSCalibrationSPD*) spdEntry->At(module)) -> SetDeadList( GetDeadArray(module) );
368     ((AliITSCalibrationSPD*) spdEntry->At(module)) -> SetNrNoisy( GetNrNoisy(module) );
369     ((AliITSCalibrationSPD*) spdEntry->At(module)) -> SetNoisyList( GetNoisyArray(module) );
370   }
371   AliCDBEntry* cdbEntry = new AliCDBEntry((TObject*)spdEntry,idCalSPD,metaData);
372   man->Put(cdbEntry);
373   delete spdEntry;
374   delete cdbEntry;
375   delete metaData;
376   return kTRUE;
377 }
378 #endif
379
380 void AliITSOnlineCalibrationSPDhandler::WriteToFiles() {
381   // write the lists of dead and noisy to files (only if there are >0 dead or noisy pixels)
382   for (UInt_t module=0; module<240; module++) {
383     if (fNrDead[module]+fNrNoisy[module] > 0) {
384       WriteToFile(module);
385     }
386   }
387 }
388 void AliITSOnlineCalibrationSPDhandler::WriteToFile(UInt_t module) {
389   // write the lists of dead and noisy for module to file
390   AliITSOnlineCalibrationSPD* calib = new AliITSOnlineCalibrationSPD();
391   calib->SetModuleNr(module);
392   calib->SetDeadList(GetDeadArray(module));
393   calib->SetNoisyList(GetNoisyArray(module));
394   calib->SetNrDead(GetNrDead(module));
395   calib->SetNrNoisy(GetNrNoisy(module));
396   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
397   TFile file(fileName.Data(), "RECREATE");
398   file.WriteTObject(calib, "AliITSOnlineCalibrationSPD");
399   file.Close();
400   delete calib;
401 }
402 void AliITSOnlineCalibrationSPDhandler::WriteDeadToFiles() {
403   // write the lists of dead to files (only if there are >0 dead pixels)
404   for (UInt_t module=0; module<240; module++) {
405     if (fNrDead[module] > 0) {
406       WriteDeadToFile(module);
407     }
408   }
409 }
410 void AliITSOnlineCalibrationSPDhandler::WriteDeadToFile(UInt_t module) {
411   // write the lists of dead for module to file
412   AliITSOnlineCalibrationSPD* calib = new AliITSOnlineCalibrationSPD();
413   calib->SetModuleNr(module);
414   calib->SetDeadList(GetDeadArray(module));
415   calib->SetNrDead(GetNrDead(module));
416   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
417   TFile file(fileName.Data(), "RECREATE");
418   file.WriteTObject(calib, "AliITSOnlineCalibrationSPD");
419   file.Close();
420   delete calib;
421 }
422 void AliITSOnlineCalibrationSPDhandler::WriteNoisyToFiles() {
423   // write the lists of noisy to files (only if there are >0 dead pixels)
424   for (UInt_t module=0; module<240; module++) {
425     if (fNrNoisy[module] > 0) {
426       printf("writing noisy to file for module %d\n",module);
427       WriteNoisyToFile(module);
428     }
429   }
430 }
431 void AliITSOnlineCalibrationSPDhandler::WriteNoisyToFile(UInt_t module) {
432   // write the lists of noisy for module to file
433   AliITSOnlineCalibrationSPD* calib = new AliITSOnlineCalibrationSPD();
434   calib->SetModuleNr(module);
435   calib->SetNoisyList(GetNoisyArray(module));
436   calib->SetNrNoisy(GetNrNoisy(module));
437   TString fileName = Form("%s/SPD_DeadNoisy_%d.root",fFileLocation.Data(),module);
438   TFile file(fileName.Data(), "RECREATE");
439   file.WriteTObject(calib, "AliITSOnlineCalibrationSPD");
440   file.Close();
441   delete calib;
442 }
443
444 TArrayI AliITSOnlineCalibrationSPDhandler::GetDeadArray(UInt_t module) {
445   // get a TArrayI of the dead pixels (format for the AliITSCalibrationSPD object)
446   TArrayI returnArray;
447   returnArray.Set(GetNrDead(module)*2);
448   fDeadPixelMap[module]->PrepareSerialize(); // for tree ordered values
449   for (UInt_t index=0; index<GetNrDead(module); index++) {
450     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
451     Int_t colM = GetColMFromKey(key);
452     Int_t row = GetRowFromKey(key);
453     returnArray.AddAt(colM,index*2);
454     returnArray.AddAt(row,index*2+1);
455   }
456   return returnArray;
457 }
458 TArrayI AliITSOnlineCalibrationSPDhandler::GetNoisyArray(UInt_t module) {
459   // get a TArrayI of the noisy pixels (format for the AliITSCalibrationSPD object)
460   TArrayI returnArray;
461   returnArray.Set(GetNrNoisy(module)*2);
462   fNoisyPixelMap[module]->PrepareSerialize(); // for tree ordered values
463   for (UInt_t index=0; index<GetNrNoisy(module); index++) {
464     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
465     Int_t colM = GetColMFromKey(key);
466     Int_t row = GetRowFromKey(key);
467     returnArray.AddAt(colM,index*2);
468     returnArray.AddAt(row,index*2+1);
469   }
470   return returnArray;
471 }
472
473 void AliITSOnlineCalibrationSPDhandler::ResetDead() {
474   // reset the dead pixel map
475   for (UInt_t module=0; module<240; module++) {
476     fNrDead[module]=0;
477     fDeadPixelMap[module]->Clear();
478   }
479 }
480
481 void AliITSOnlineCalibrationSPDhandler::ResetDeadForChip(UInt_t eqId, UInt_t hs, UInt_t chip) {
482   // clear the noisy pixels for this chip
483   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
484   for (UInt_t col=0; col<32; col++) {
485     for (UInt_t row=0; row<256; row++) {
486       Int_t key = GetKey(eqId,hs,chip,col,row);
487       if (fDeadPixelMap[module]->Remove(key)) {
488         fNrDead[module]--;
489       }
490     }
491   }
492 }
493
494 Bool_t AliITSOnlineCalibrationSPDhandler::SetDeadPixel(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
495   // set a dead pixel, returns false if pixel is already dead
496   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
497   Int_t key = GetKey(eqId,hs,chip,col,row);
498 //!!!  // if noisy we dont want to add it...
499 //!!!  if (fNoisyPixelMap[module]->Find(key) != NULL) return kFALSE;
500   if (fDeadPixelMap[module]->Insert(key,module)) {
501     fNrDead[module]++;
502     return kTRUE;
503   }
504   return kFALSE;
505 }
506
507 Bool_t AliITSOnlineCalibrationSPDhandler::SetDeadPixelM(UInt_t module, UInt_t colM, UInt_t row) {
508   // set a dead pixel, returns false if pixel is already dead
509   UInt_t eqId = GetEqIdFromOffline(module);
510   UInt_t hs = GetHSFromOffline(module);
511   UInt_t chip = GetChipFromOffline(module,colM);
512   UInt_t col = GetColFromOffline(colM);
513   Int_t key = GetKey(eqId,hs,chip,col,row);
514 //!!!  // if noisy we dont want to add it...
515 //!!!  if (fNoisyPixelMap[module]->Find(key) != NULL) return kFALSE;
516   if (fDeadPixelMap[module]->Insert(key,module)) {
517     fNrDead[module]++;
518     return kTRUE;
519   }
520   return kFALSE;
521 }
522
523 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelDeadKey(Int_t key) const {
524   // is this pixel noisy?
525   UInt_t eqId = GetEqIdFromKey(key);
526   UInt_t hs = GetHSFromKey(key);
527   UInt_t chip = GetChipFromKey(key);
528   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
529   return IsPixelDeadMKey(module,key);
530 }
531
532 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelDeadMKey(UInt_t module, Int_t key) const {
533   // is this pixel dead?
534   if ( fDeadPixelMap[module]->Find(key) != NULL ) {
535     return kTRUE;
536   }
537   else {
538     return kFALSE;
539   }
540 }
541
542 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelDead(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) const {
543   // is the pixel dead?
544   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
545   Int_t key = GetKey(eqId,hs,chip,col,row);
546   return IsPixelDeadMKey(module,key);
547 }
548
549 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelDeadM(UInt_t module, UInt_t colM, UInt_t row) {
550   // is the pixel dead?
551   UInt_t eqId = GetEqIdFromOffline(module);
552   UInt_t hs = GetHSFromOffline(module);
553   UInt_t chip = GetChipFromOffline(module,colM);
554   UInt_t col = GetColFromOffline(colM);
555   Int_t key = GetKey(eqId,hs,chip,col,row);
556   return IsPixelDeadMKey(module,key);
557 }
558
559 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrDead() const {
560   // returns the total nr of dead pixels
561   UInt_t nrDead = 0;
562   for (UInt_t module=0; module<240; module++) {
563     nrDead+=fNrDead[module];
564   }
565   return nrDead;
566 }
567
568 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrDead(UInt_t module) const {
569   // returns the number of dead pixels for a certain module
570   if (module<240) {
571     return fNrDead[module];
572   }
573   else {
574     return 0;
575   }
576 }
577
578 UInt_t AliITSOnlineCalibrationSPDhandler::GetDeadEqIdAt(UInt_t module, UInt_t index) {
579   // get eqId for the dead pixel at position index in list of dead
580   if (index<GetNrDead(module)) {
581     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
582     return GetEqIdFromKey(key);
583   }
584   else {
585     Error("AliITSOnlineCalibrationSPDhandler::GetDeadEqIdAt", "Index %d out of bounds.", index);
586     return 0;
587   }
588 }
589 UInt_t AliITSOnlineCalibrationSPDhandler::GetDeadHSAt(UInt_t module, UInt_t index) {
590   // get hs for the dead pixel at position index in list of dead
591   if (index<GetNrDead(module)) {
592     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
593     return GetHSFromKey(key);
594   }
595   else {
596     Error("AliITSOnlineCalibrationSPDhandler::GetDeadHSAt", "Index %d out of bounds.", index);
597     return 0;
598   }
599 }
600 UInt_t AliITSOnlineCalibrationSPDhandler::GetDeadChipAt(UInt_t module, UInt_t index) {
601   // get chip for the dead pixel at position index in list of dead
602   if (index<GetNrDead(module)) {
603     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
604     return GetChipFromKey(key);
605   }
606   else {
607     Error("AliITSOnlineCalibrationSPDhandler::GetDeadChipAt", "Index %d out of bounds.", index);
608     return 0;
609   }
610 }
611 UInt_t AliITSOnlineCalibrationSPDhandler::GetDeadColAt(UInt_t module, UInt_t index) {
612   // get column for the dead pixel at position index in list of dead
613   if (index<GetNrDead(module)) {
614     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
615     return GetColFromKey(key);
616   }
617   else {
618     Error("AliITSOnlineCalibrationSPDhandler::GetDeadColAt", "Index %d out of bounds.", index);
619     return 0;
620   }
621 }
622 UInt_t AliITSOnlineCalibrationSPDhandler::GetDeadRowAt(UInt_t module, UInt_t index) {
623   // get row for the dead pixel at position index in list of dead
624   if (index<GetNrDead(module)) {
625     Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
626     return GetRowFromKey(key);
627   }
628   else {
629     Error("AliITSOnlineCalibrationSPDhandler::GetDeadRowAt", "Index %d out of bounds.", index);
630     return 0;
631   }
632 }
633
634 void AliITSOnlineCalibrationSPDhandler::ResetNoisy() {
635   // clear the list of noisy pixels
636   for (UInt_t module=0; module<240; module++) {
637     fNrNoisy[module]=0;
638     fNoisyPixelMap[module]->Clear();
639   }
640 }
641 void AliITSOnlineCalibrationSPDhandler::ResetNoisyForChip(UInt_t eqId, UInt_t hs, UInt_t chip) {
642   // clear the noisy pixels for this chip
643   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
644   for (UInt_t col=0; col<32; col++) {
645     for (UInt_t row=0; row<256; row++) {
646       Int_t key = GetKey(eqId,hs,chip,col,row);
647       if (fNoisyPixelMap[module]->Remove(key)) {
648         fNrNoisy[module]--;
649       }
650     }
651   }
652 }
653
654
655 Bool_t AliITSOnlineCalibrationSPDhandler::SetNoisyPixel(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
656   // set a noisy pixel, returns false if already there
657   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
658   Int_t key = GetKey(eqId,hs,chip,col,row);
659 //!!!  // if dead before - remove from the dead list 
660 //!!!  UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
661 //!!!  if (fDeadPixelMap.Remove(key)) {
662 //!!!    fNrDead[module]--;
663 //!!!  }
664   if (fNoisyPixelMap[module]->Insert(key,col)) {
665     fNrNoisy[module]++;
666     return kTRUE;
667   }
668   return kFALSE;
669 }
670
671 Bool_t AliITSOnlineCalibrationSPDhandler::SetNoisyPixelM(UInt_t module, UInt_t colM, UInt_t row) {
672   // set a noisy pixel, returns false if already there
673   UInt_t eqId = GetEqIdFromOffline(module);
674   UInt_t hs = GetHSFromOffline(module);
675   UInt_t chip = GetChipFromOffline(module,colM);
676   UInt_t col = GetColFromOffline(colM);
677   Int_t key = GetKey(eqId,hs,chip,col,row);
678 //!!!  // if dead before - remove from the dead list 
679 //!!!  if (fDeadPixelMap[module]->Remove(key)) {
680 //!!!    fNrDead[module]--;
681 //!!!  }
682   if (fNoisyPixelMap[module]->Insert(key,col)) {
683     fNrNoisy[module]++;
684     return kTRUE;
685   }
686   return kFALSE;
687 }
688
689 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelNoisyKey(Int_t key) const {
690   // is this pixel noisy?
691   UInt_t eqId = GetEqIdFromKey(key);
692   UInt_t hs = GetHSFromKey(key);
693   UInt_t chip = GetChipFromKey(key);
694   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
695   return IsPixelNoisyMKey(module,key);
696 }
697
698 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelNoisyMKey(UInt_t module, Int_t key) const {
699   // is this pixel noisy?
700   if ( fNoisyPixelMap[module]->Find(key) != NULL ) {
701     return kTRUE;
702   }
703   else {
704     return kFALSE;
705   }
706 }
707
708 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelNoisy(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) const {
709   // is this pixel noisy?
710   UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,hs,chip);
711   Int_t key = GetKey(eqId,hs,chip,col,row);
712   return IsPixelNoisyMKey(module,key);
713 }
714
715 Bool_t AliITSOnlineCalibrationSPDhandler::IsPixelNoisyM(UInt_t module, UInt_t colM, UInt_t row) {
716   // is this pixel noisy?
717   UInt_t eqId = GetEqIdFromOffline(module);
718   UInt_t hs = GetHSFromOffline(module);
719   UInt_t chip = GetChipFromOffline(module,colM);
720   UInt_t col = GetColFromOffline(colM);
721   Int_t key = GetKey(eqId,hs,chip,col,row);
722   return IsPixelNoisyMKey(module,key);
723 }
724
725 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrNoisy() const {
726   // returns the total nr of noisy pixels
727   UInt_t nrNoisy = 0;
728   for (UInt_t module=0; module<240; module++) {
729     nrNoisy+=fNrNoisy[module];
730   }
731   return nrNoisy;
732 }
733
734 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrNoisy(UInt_t module) const {
735 // returns the number of noisy pixels for a certain module
736   if (module<240) {
737     return fNrNoisy[module];
738   }
739   else {
740     return 0;
741   }
742 }
743
744 UInt_t AliITSOnlineCalibrationSPDhandler::GetNoisyEqIdAt(UInt_t module, UInt_t index) {
745   // get chip for the noisy pixel at position index in list of noisy
746   if (index<GetNrNoisy(module)) {
747     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
748     return GetEqIdFromKey(key);
749   }
750   else {
751     Error("AliITSOnlineCalibrationSPDhandler::GetNoisyEqIdAt", "Index %d out of bounds.", index);
752     return 0;
753   }
754 }
755 UInt_t AliITSOnlineCalibrationSPDhandler::GetNoisyHSAt(UInt_t module, UInt_t index) {
756   // get chip for the noisy pixel at position index in list of noisy
757   if (index<GetNrNoisy(module)) {
758     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
759     return GetHSFromKey(key);
760   }
761   else {
762     Error("AliITSOnlineCalibrationSPDhandler::GetNoisyHSAt", "Index %d out of bounds.", index);
763     return 0;
764   }
765 }
766 UInt_t AliITSOnlineCalibrationSPDhandler::GetNoisyChipAt(UInt_t module, UInt_t index) {
767   // get chip for the noisy pixel at position index in list of noisy
768   if (index<GetNrNoisy(module)) {
769     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
770     return GetChipFromKey(key);
771   }
772   else {
773     Error("AliITSOnlineCalibrationSPDhandler::GetNoisyChipAt", "Index %d out of bounds.", index);
774     return 0;
775   }
776 }
777 UInt_t AliITSOnlineCalibrationSPDhandler::GetNoisyColAt(UInt_t module, UInt_t index) {
778   // get column for the noisy pixel at position index in list of noisy
779   if (index<GetNrNoisy(module)) {
780     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
781     return GetColFromKey(key);
782   }
783   else {
784     Warning("AliITSOnlineCalibrationSPDhandler::GetNoisyColAt", "Index %d out of bounds.", index);
785     return 0;
786   }
787 }
788 UInt_t AliITSOnlineCalibrationSPDhandler::GetNoisyRowAt(UInt_t module, UInt_t index) {
789   // get row for the noisy pixel at position index in list of noisy
790   if (index<GetNrNoisy(module)) {
791     Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
792     return GetRowFromKey(key);
793   }
794   else {
795     Error("AliITSOnlineCalibrationSPDhandler::GetNoisyRowAt", "Index %d out of bounds.", index);
796     return 0;
797   }
798 }
799
800 void AliITSOnlineCalibrationSPDhandler::PrintDead() const {
801   // print the dead pixels to screen
802   printf("-----------------------------------\n");
803   printf("Dead Pixels: (eqId,hs,chip,col,row)\n");
804   printf("-----------------------------------\n");
805   for (UInt_t module=0; module<240; module++) {
806     for (UInt_t index=0; index<GetNrDead(module); index++) {
807       Int_t key = fDeadPixelMap[module]->GetKeyIndex(index);
808       UInt_t eqId = GetEqIdFromKey(key);
809       UInt_t hs = GetHSFromKey(key);
810       UInt_t chip = GetChipFromKey(key);
811       UInt_t col = GetColFromKey(key);
812       UInt_t row = GetRowFromKey(key);
813       printf("%d,%d,%d,%d,%d\n",eqId,hs,chip,col,row);
814     }
815   }
816 }
817
818 void AliITSOnlineCalibrationSPDhandler::PrintNoisy() const {
819   // print the dead pixels to screen
820   printf("-----------------------------------\n");
821   printf("Noisy Pixels: (eqId,hs,chip,col,row)\n");
822   printf("-----------------------------------\n");
823   for (UInt_t module=0; module<240; module++) {
824     for (UInt_t index=0; index<GetNrNoisy(module); index++) {
825       Int_t key = fNoisyPixelMap[module]->GetKeyIndex(index);
826       UInt_t eqId = GetEqIdFromKey(key);
827       UInt_t hs = GetHSFromKey(key);
828       UInt_t chip = GetChipFromKey(key);
829       UInt_t col = GetColFromKey(key);
830       UInt_t row = GetRowFromKey(key);
831       printf("%d,%d,%d,%d,%d\n",eqId,hs,chip,col,row);
832     }
833   }
834 }
835
836 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrDeadDiff(AliITSOnlineCalibrationSPDhandler* other) const {
837   // returns nr of dead in this' lists and not in other's lists
838   UInt_t returnval=0;
839   for (UInt_t module=0; module<240; module++) {
840     for (UInt_t ind1=0; ind1<fNrDead[module]; ind1++) {
841       Int_t key = fDeadPixelMap[module]->GetKeyIndex(ind1);
842       UInt_t eqId = GetEqIdFromKey(key);
843       UInt_t hs = GetHSFromKey(key);
844       UInt_t chip = GetChipFromKey(key);
845       UInt_t col = GetColFromKey(key);
846       UInt_t row = GetRowFromKey(key);
847       if (!(other->IsPixelDead(eqId,hs,chip,col,row))) {
848         returnval++;
849       }
850     }
851   }
852   return returnval;
853 }
854
855 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrNoisyDiff(AliITSOnlineCalibrationSPDhandler* other) const {
856   // returns nr of noisy in this' lists and not in other's lists
857   UInt_t returnval=0;
858   for (UInt_t module=0; module<240; module++) {
859     for (UInt_t ind1=0; ind1<fNrNoisy[module]; ind1++) {
860       Int_t key = fNoisyPixelMap[module]->GetKeyIndex(ind1);
861       UInt_t eqId = GetEqIdFromKey(key);
862       UInt_t hs = GetHSFromKey(key);
863       UInt_t chip = GetChipFromKey(key);
864       UInt_t col = GetColFromKey(key);
865       UInt_t row = GetRowFromKey(key);
866       if (!(other->IsPixelNoisy(eqId,hs,chip,col,row))) {
867         returnval++;
868       }
869     }
870   }
871   return returnval;
872 }
873
874 UInt_t AliITSOnlineCalibrationSPDhandler::GetNrDiff(AliITSOnlineCalibrationSPDhandler* other) const {
875   // returns nr of dead/noisy in this' lists and not in other's lists
876   return GetNrDeadDiff(other) + GetNrNoisyDiff(other);
877 }
878
879 AliITSOnlineCalibrationSPDhandler* AliITSOnlineCalibrationSPDhandler::GetDiff(AliITSOnlineCalibrationSPDhandler* other) const {
880   // returns handler with dead/noisy in this' lists, except for those in other's lists
881   AliITSOnlineCalibrationSPDhandler* newHandler = new AliITSOnlineCalibrationSPDhandler();
882   for (UInt_t module=0; module<240; module++) {
883     for (UInt_t ind1=0; ind1<fNrDead[module]; ind1++) {
884       Int_t key = fDeadPixelMap[module]->GetKeyIndex(ind1);
885       UInt_t eqId = GetEqIdFromKey(key);
886       UInt_t hs = GetHSFromKey(key);
887       UInt_t chip = GetChipFromKey(key);
888       UInt_t col = GetColFromKey(key);
889       UInt_t row = GetRowFromKey(key);
890       if (!(other->IsPixelDead(eqId,hs,chip,col,row))) {
891         newHandler->SetDeadPixel(eqId,hs,chip,col,row);
892       }
893     }
894     for (UInt_t ind2=0; ind2<fNrNoisy[module]; ind2++) {
895       Int_t key = fNoisyPixelMap[module]->GetKeyIndex(ind2);
896       UInt_t eqId = GetEqIdFromKey(key);
897       UInt_t hs = GetHSFromKey(key);
898       UInt_t chip = GetChipFromKey(key);
899       UInt_t col = GetColFromKey(key);
900       UInt_t row = GetRowFromKey(key);
901       if (!(other->IsPixelNoisy(eqId,hs,chip,col,row))) {
902         newHandler->SetNoisyPixel(eqId,hs,chip,col,row);
903       }
904     }
905   }
906   return newHandler;
907 }
908
909 AliITSOnlineCalibrationSPDhandler* AliITSOnlineCalibrationSPDhandler::GetDeadDiff(AliITSOnlineCalibrationSPDhandler* other) const {
910   // returns handler with dead in this' lists, except for those in other's lists
911   AliITSOnlineCalibrationSPDhandler* newHandler = new AliITSOnlineCalibrationSPDhandler();
912   for (UInt_t module=0; module<240; module++) {
913     for (UInt_t ind1=0; ind1<fNrDead[module]; ind1++) {
914       Int_t key = fDeadPixelMap[module]->GetKeyIndex(ind1);
915       UInt_t eqId = GetEqIdFromKey(key);
916       UInt_t hs = GetHSFromKey(key);
917       UInt_t chip = GetChipFromKey(key);
918       UInt_t col = GetColFromKey(key);
919       UInt_t row = GetRowFromKey(key);
920       if (!(other->IsPixelDead(eqId,hs,chip,col,row))) {
921         newHandler->SetDeadPixel(eqId,hs,chip,col,row);
922       }
923     }
924   }
925   return newHandler;
926 }
927
928 AliITSOnlineCalibrationSPDhandler* AliITSOnlineCalibrationSPDhandler::GetNoisyDiff(AliITSOnlineCalibrationSPDhandler* other) const {
929   // returns handler with noisy in this' lists, except for those in other's lists
930   AliITSOnlineCalibrationSPDhandler* newHandler = new AliITSOnlineCalibrationSPDhandler();
931   for (UInt_t module=0; module<240; module++) {
932     for (UInt_t ind2=0; ind2<fNrNoisy[module]; ind2++) {
933       Int_t key = fNoisyPixelMap[module]->GetKeyIndex(ind2);
934       UInt_t eqId = GetEqIdFromKey(key);
935       UInt_t hs = GetHSFromKey(key);
936       UInt_t chip = GetChipFromKey(key);
937       UInt_t col = GetColFromKey(key);
938       UInt_t row = GetRowFromKey(key);
939       if (!(other->IsPixelNoisy(eqId,hs,chip,col,row))) {
940         newHandler->SetNoisyPixel(eqId,hs,chip,col,row);
941       }
942     }
943   }
944   return newHandler;
945 }
946
947 void AliITSOnlineCalibrationSPDhandler::InitModuleMaps() {
948   // initializes the module mapping arrays needed for the methods below (GetEqIdFromOffline etc.)
949   for (UInt_t iDDL=0; iDDL<20; iDDL++) {
950     for (UInt_t iModule=0; iModule<12; iModule++) {
951       UInt_t module = AliITSRawStreamSPD::GetModuleNumber(iDDL,iModule);
952       fiDDL[module] = iDDL;
953       fiModule[module] = iModule;
954     }
955   }
956 }
957
958 void AliITSOnlineCalibrationSPDhandler::GenerateDCSConfigFile(const Char_t* fileName) {
959   // generates an ascii file in the format as the one produced by online da (but with dummy runNr=0)
960   ofstream dcsfile;
961   dcsfile.open(fileName);
962   dcsfile << "[SPD SCAN]\n";
963   dcsfile << "RunNumber=" << "0" << "\n"; // dummy value
964   dcsfile << "Type=" << "4" << "\n";
965   dcsfile << "Router=" << "0" << "\n"; // dummy value
966   dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values
967   dcsfile << "[NOISY]\n";
968   for (UInt_t module=0; module<240; module++) {
969     UInt_t headkey=20*10*6;
970     for (UInt_t ind=0; ind<fNrNoisy[module]; ind++) {
971       UInt_t newkey = GetNoisyEqIdAt(module,ind)*10*6 +
972         GetNoisyHSAt(module,ind)*10 +
973         GetNoisyChipAt(module,ind);
974       if (newkey!=headkey) { // print eqId,hs,chip header
975         headkey = newkey;
976         dcsfile << "-" << newkey/(6*10) << "," << (newkey%(6*10))/10 << "," << (newkey%(6*10))%10 << "\n";
977       }
978       dcsfile << GetNoisyColAt(module,ind) << "," << GetNoisyRowAt(module,ind) << "\n";
979     }
980   }
981   dcsfile.close();
982 }
983
984 UInt_t AliITSOnlineCalibrationSPDhandler::GetEqIdFromOffline(UInt_t module) {
985   // module to eqId mapping
986   if (!fModuleMapInited) InitModuleMaps();
987   return fiDDL[module];
988 }
989 UInt_t AliITSOnlineCalibrationSPDhandler::GetHSFromOffline(UInt_t module) {
990   // module to hs mapping
991   if (!fModuleMapInited) InitModuleMaps();
992   return fiModule[module]/2;
993 }
994 UInt_t AliITSOnlineCalibrationSPDhandler::GetChipFromOffline(UInt_t module, UInt_t colM) {
995   // module,colM to chip mapping
996   if (!fModuleMapInited) InitModuleMaps();
997   return colM/32 + 5*(fiModule[module]%2);
998 }
999 UInt_t AliITSOnlineCalibrationSPDhandler::GetColFromOffline(UInt_t colM) const {
1000   // colM to col mapping
1001   return colM%32;
1002 }