]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSOnlineSPDscanAnalyzer.cxx
Changes to obey to the coding conventions (M. Lunardon)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSPDscanAnalyzer.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 /* $Id$ */
17
18 ////////////////////////////////////////////////////////////
19 // Author: Henrik Tydesjo                                 //
20 // This class is used in the detector algorithm framework //
21 // to process the data stored in special container files  //
22 // (see AliITSOnlineSPDscan). For instance, minimum       //
23 // threshold values can be extracted.                     //
24 ////////////////////////////////////////////////////////////
25
26 #include "AliITSOnlineSPDscanAnalyzer.h"
27 #include "AliITSOnlineSPDscan.h"
28 #include "AliITSOnlineSPDscanSingle.h"
29 #include "AliITSOnlineSPDscanMultiple.h"
30 #include "AliITSOnlineSPDscanMeanTh.h"
31 #include "AliITSOnlineCalibrationSPDhandler.h"
32 #include "AliITSRawStreamSPD.h"
33 #include <TStyle.h>
34 #include <TMath.h>
35 #include <TF1.h>
36 #include <TGraph.h>
37 #include <TH2F.h>
38 #include <TError.h>
39 #include <iostream>
40 #include <fstream>
41
42 Double_t itsSpdErrorf(Double_t *x, Double_t *par){
43   if (par[2]<0) par[2]=0;
44   Double_t val = par[2]+(0.12*256*32-par[2])*(0.5+0.5*TMath::Erf((x[0]-par[0])/par[1]/sqrt(2.)));
45   return val;
46 }
47 //Double_t itsSpdErrorfOrig(Double_t *x, Double_t *par){
48 //  return 0.5+0.5*TMath::Erf((x[0]-par[0])/par[1]/sqrt(2.));
49 //}
50
51 //_________________________________________________________________________
52 AliITSOnlineSPDscanAnalyzer::AliITSOnlineSPDscanAnalyzer(const Char_t *fileName, AliITSOnlineCalibrationSPDhandler *handler, Bool_t readFromGridFile) :
53   fType(99),fDacId(99),fFileName(fileName),fScanObj(NULL),fHandler(handler),fTriggers(NULL),fTPeff(0),fTPeffHS(NULL),fDeadPixel(0),fDeadPixelHS(NULL),fNoisyPixel(0),fNoisyPixelHS(NULL),
54   fOverWrite(kFALSE),fNoiseThreshold(0.01),fNoiseMinimumEvents(100),
55   fMinNrStepsBeforeIncrease(5),fMinIncreaseFromBaseLine(2),fStepDownDacSafe(5),fMaxBaseLineLevel(10)
56 {
57   // constructor
58   for (UInt_t chipNr=0; chipNr<11; chipNr++) {
59     for (UInt_t hs=0; hs<6; hs++) {
60       fMeanMultiplicity[hs][chipNr]=NULL;
61       fHitEventEfficiency[hs][chipNr]=NULL;
62     }
63   }
64   for (UInt_t hs=0; hs<6; hs++) {
65     fTPeffChip[hs]=NULL;
66     fDeadPixelChip[hs]=NULL;
67     fNoisyPixelChip[hs]=NULL;
68   }
69
70   for (UInt_t mod=0; mod<240; mod++) {
71     fbModuleScanned[mod]=kFALSE;
72   }
73
74   Init(readFromGridFile);
75 }
76 //_________________________________________________________________________
77 AliITSOnlineSPDscanAnalyzer::AliITSOnlineSPDscanAnalyzer(const AliITSOnlineSPDscanAnalyzer& handle) :
78   fType(99),fDacId(99),fFileName("."),fScanObj(NULL),fHandler(NULL),fTriggers(NULL),fTPeff(0),fTPeffHS(NULL),fDeadPixel(0),fDeadPixelHS(NULL),fNoisyPixel(0),fNoisyPixelHS(NULL),
79   fOverWrite(kFALSE),fNoiseThreshold(0.01),fNoiseMinimumEvents(100),
80   fMinNrStepsBeforeIncrease(5),fMinIncreaseFromBaseLine(2),fStepDownDacSafe(5),fMaxBaseLineLevel(10)
81 {
82   // copy constructor, only copies the filename and params (not the processed data)
83   fFileName=handle.fFileName;
84   fOverWrite=handle.fOverWrite;
85   fNoiseThreshold=handle.fNoiseThreshold;
86   fNoiseMinimumEvents=handle.fNoiseMinimumEvents;
87   fMinNrStepsBeforeIncrease=handle.fMinNrStepsBeforeIncrease;
88   fMinIncreaseFromBaseLine=handle.fMinIncreaseFromBaseLine;
89   fStepDownDacSafe=handle.fStepDownDacSafe;
90   fMaxBaseLineLevel=handle.fMaxBaseLineLevel;
91
92   for (UInt_t chipNr=0; chipNr<11; chipNr++) {
93     for (UInt_t hs=0; hs<6; hs++) {
94       fMeanMultiplicity[hs][chipNr]=NULL;
95       fHitEventEfficiency[hs][chipNr]=NULL;
96     }
97   }
98   for (UInt_t hs=0; hs<6; hs++) {
99     fTPeffChip[hs]=NULL;
100     fDeadPixelChip[hs]=NULL;
101     fNoisyPixelChip[hs]=NULL;
102   }
103
104   for (UInt_t mod=0; mod<240; mod++) {
105     fbModuleScanned[mod]=kFALSE;
106   }
107
108   Init();
109 }
110 //_________________________________________________________________________
111 AliITSOnlineSPDscanAnalyzer::~AliITSOnlineSPDscanAnalyzer() {
112   // destructor
113   for (UInt_t hs=0; hs<6; hs++) {
114     for (UInt_t chipNr=0; chipNr<11; chipNr++) {
115       if (fMeanMultiplicity[hs][chipNr]!=NULL) {
116         delete fMeanMultiplicity[hs][chipNr];
117         fMeanMultiplicity[hs][chipNr]=NULL;
118       }
119       if (fHitEventEfficiency[hs][chipNr]!=NULL) {
120         delete fHitEventEfficiency[hs][chipNr];
121         fHitEventEfficiency[hs][chipNr]=NULL;
122       }
123     }
124   }
125
126   if (fTriggers!=NULL) {
127     delete fTriggers;
128     fTriggers=NULL;
129   }
130
131   DeleteUniformityHistograms();
132
133   if (fScanObj!=NULL) {
134     delete fScanObj;
135     fScanObj=NULL;
136   }
137 }
138 //_________________________________________________________________________
139 AliITSOnlineSPDscanAnalyzer& AliITSOnlineSPDscanAnalyzer::operator=(const AliITSOnlineSPDscanAnalyzer& handle) {
140   // assignment operator, only copies the filename and params (not the processed data)
141   if (this!=&handle) {
142     for (UInt_t hs=0; hs<6; hs++) {
143       for (UInt_t chipNr=0; chipNr<11; chipNr++) {
144         if (fMeanMultiplicity[hs][chipNr]!=NULL) {
145           delete fMeanMultiplicity[hs][chipNr];
146         }
147         if (fHitEventEfficiency[hs][chipNr]!=NULL) {
148           delete fHitEventEfficiency[hs][chipNr];
149         }
150       }
151     }
152     if (fTriggers!=NULL) {
153       delete fTriggers;
154       fTriggers=NULL;
155     }
156
157     DeleteUniformityHistograms();
158
159     if (fScanObj!=NULL) {
160       delete fScanObj;
161       fScanObj=NULL;
162     }
163    
164     fFileName=handle.fFileName;
165     fOverWrite=handle.fOverWrite;
166     fNoiseThreshold=handle.fNoiseThreshold;
167     fNoiseMinimumEvents=handle.fNoiseMinimumEvents;
168     fMinNrStepsBeforeIncrease=handle.fMinNrStepsBeforeIncrease;
169     fMinIncreaseFromBaseLine=handle.fMinIncreaseFromBaseLine;
170     fStepDownDacSafe=handle.fStepDownDacSafe;
171     fMaxBaseLineLevel=handle.fMaxBaseLineLevel;
172
173     for (UInt_t chipNr=0; chipNr<11; chipNr++) {
174       for (UInt_t hs=0; hs<6; hs++) {
175         fMeanMultiplicity[hs][chipNr]=NULL;
176         fHitEventEfficiency[hs][chipNr]=NULL;
177       }
178     }
179     for (UInt_t mod=0; mod<240; mod++) {
180       fbModuleScanned[mod]=kFALSE;
181     }
182
183     fHandler=NULL;
184     
185     fType=99;
186     fDacId=99;
187
188     Init();    
189   }
190   return *this;
191 }
192 //_________________________________________________________________________
193 void AliITSOnlineSPDscanAnalyzer::Init(Bool_t readFromGridFile) {
194   // first checks type of container and then initializes container obj
195   if (!readFromGridFile) {
196     FILE* fp0 = fopen(fFileName.Data(), "r");
197     if (fp0 == NULL) {
198       return;
199     }
200     else {
201       fclose(fp0);
202     }
203   }
204
205   fScanObj = new AliITSOnlineSPDscan(fFileName.Data(),readFromGridFile);
206   fType = fScanObj->GetType();
207   delete fScanObj;
208
209   // init container
210   switch(fType) {
211   case kUNIMA:
212   case kNOISE:
213     fScanObj = new AliITSOnlineSPDscanSingle(fFileName.Data(),readFromGridFile);
214     break;
215   case kMINTH:
216   case kDAC:
217   case kDELAY:
218     fScanObj = new AliITSOnlineSPDscanMultiple(fFileName.Data(),readFromGridFile);
219     fDacId = ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacId();
220     break;
221   case kMEANTH:
222     fScanObj = new AliITSOnlineSPDscanMeanTh(fFileName.Data(),readFromGridFile);
223     fDacId = ((AliITSOnlineSPDscanMeanTh*)fScanObj)->GetDacId();
224     break;
225   default:
226     Error("AliITSOnlineSPDscanAnalyzer::Init","Type %d not defined!",fType);
227     fScanObj=NULL;
228     return;
229     break;
230   }
231
232 }
233 //_________________________________________________________________________
234 void AliITSOnlineSPDscanAnalyzer::SetParam(const Char_t *pname, const Char_t *pval) {
235   // set a parameter
236   TString name = pname;
237   TString val = pval;
238   if (name.CompareTo("fOverWrite")==0) {
239     if (val.CompareTo("YES")==0 || val.CompareTo("1")==0) {
240       fOverWrite = kTRUE;
241     }
242     else fOverWrite = kFALSE;
243   }
244   else if (name.CompareTo("fNoiseThreshold")==0) {
245     fNoiseThreshold = val.Atof();
246   }
247   else if (name.CompareTo("fNoiseMinimumEvents")==0) {
248     fNoiseMinimumEvents = val.Atoi();
249   }
250   else if (name.CompareTo("fMinNrStepsBeforeIncrease")==0) {
251     fMinNrStepsBeforeIncrease = val.Atoi();
252   }
253   else if (name.CompareTo("fMinIncreaseFromBaseLine")==0) {
254     fMinIncreaseFromBaseLine = val.Atof();
255   }
256   else if (name.CompareTo("fStepDownDacSafe")==0) {
257     fStepDownDacSafe = val.Atoi();
258   }
259   else if (name.CompareTo("fMaxBaseLineLevel")==0) {
260     fMaxBaseLineLevel = val.Atof();
261   }
262   else {
263     Error("AliITSOnlineSPDscanAnalyzer::SetParam","Parameter %s in configuration file unknown.",name.Data());
264   }
265 }
266 //_________________________________________________________________________
267 void AliITSOnlineSPDscanAnalyzer::ReadParamsFromLocation(const Char_t *dirName) {
268   // opens file (default name) in dir dirName and reads parameters from it
269   TString paramsFileName = Form("%s/standal_params.txt",dirName);
270   ifstream paramsFile;
271   paramsFile.open(paramsFileName, ifstream::in);
272   if (paramsFile.fail()) {
273     printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName.Data());
274   }
275   else {
276     while(1) {
277       Char_t paramN[50];
278       Char_t paramV[50];
279       paramsFile >> paramN;
280       if (paramsFile.eof()) break;
281       paramsFile >> paramV;
282       SetParam(paramN,paramV);
283       if (paramsFile.eof()) break;
284     }
285     paramsFile.close();
286   }
287 }
288 //_________________________________________________________________________
289 Bool_t AliITSOnlineSPDscanAnalyzer::IsChipPresent(UInt_t hs, UInt_t chipNr) {
290   // is the chip present?
291   if (fScanObj==NULL) {
292     Warning("AliITSOnlineSPDscanAnalyzer::IsChipPresent","No data!");
293     return kFALSE;
294   }
295   return fScanObj->GetChipPresent(hs,chipNr);
296 }
297 //_________________________________________________________________________
298 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessDeadPixels(/*Char_t *oldcalibDir*/) {
299   // process dead pixel data, for uniformity scan, 
300   // NB: This will not be the general way of finding dead pixels.
301   if (fScanObj==NULL) {
302     Warning("AliITSOnlineSPDscanAnalyzer::ProcessDeadPixels","No data!");
303     return kFALSE;
304   }
305   // should be type kUNIMA
306   if (fType!=kUNIMA) {
307     Warning("AliITSOnlineSPDscanAnalyzer::ProcessDeadPixels","Dead pixels only for scan type %d.",kUNIMA);
308     return kFALSE;
309   }
310   // handler should be initialized
311   if (fHandler==NULL) {
312     Error("AliITSOnlineSPDscanAnalyzer::ProcessDeadPixels","Calibration handler is not initialized!");
313     return kFALSE;
314   }
315
316   UInt_t routerNr = fScanObj->GetRouterNr();
317   UInt_t rowStart = fScanObj->GetRowStart();
318   UInt_t rowEnd   = fScanObj->GetRowEnd();
319   for (UInt_t hs=0; hs<6; hs++) {
320     for (UInt_t chipNr=0; chipNr<10; chipNr++) {
321       if (fScanObj->GetChipPresent(hs,chipNr) && fScanObj->GetAverageMultiplicity(0,hs,chipNr)>0) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
322         if (fOverWrite) {fHandler->ResetDeadForChip(routerNr,hs,chipNr);}
323         for (UInt_t col=0; col<32; col++) {
324           for (UInt_t row=rowStart; row<=rowEnd; row++) {
325             if (col!=1 && col!=9 && col!=17 && col!=25) { //exclude test columns!!!
326               if (fScanObj->GetHits(0,hs,chipNr,col,row)==0) {
327                 fHandler->SetDeadPixel(routerNr,hs,chipNr,col,row);
328               }
329             }
330           }
331         }
332       }
333     }
334   }
335   return kTRUE;
336 }
337 //_________________________________________________________________________
338 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessUniformity() {
339   // process uniformity scan data (thanks to Roberta Ferretti for providing this method)
340   if (fScanObj==NULL) {
341     Warning("AliITSOnlineSPDscanAnalyzer::ProcessUniformity","No data!");
342     return kFALSE;
343   }
344   // should be type kUNIMA
345   if (fType!=kUNIMA) {
346     Warning("AliITSOnlineSPDscanAnalyzer::ProcessUniformity","Only for scan type %d.",kUNIMA);
347     return kFALSE;
348   }
349
350   CreateUniformityHistograms(); // create all histograms that will be filled here
351
352   //  UInt_t routerNr = fScanObj->GetRouterNr();
353   UInt_t rowStart = fScanObj->GetRowStart();
354   UInt_t rowEnd   = fScanObj->GetRowEnd();
355   UInt_t NrTriggers = fScanObj->GetTriggers(0)/(rowEnd-rowStart+1);
356
357   Float_t pixel100=0;
358   Float_t zeri=0;
359   Float_t pixelN=0;
360   UInt_t numChipsActive=0;
361
362   for (UInt_t hs=0; hs<6; hs++) {
363     Float_t pixel100hs=0;
364     Float_t zerihs=0;
365     Float_t pixelNhs=0;
366     UInt_t numChipsActiveHS=0;
367
368     for (UInt_t chipNr=0; chipNr<10; chipNr++) {
369       Float_t pixel100chip=0;
370       Float_t zerichip=0;
371       Float_t pixelNchip=0;
372
373       if (fScanObj->GetChipPresent(hs,chipNr)) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
374         numChipsActive++;
375         numChipsActiveHS++;
376
377         for (UInt_t col=0; col<32; col++) {
378           for (UInt_t row=rowStart; row<=rowEnd; row++) {
379             if (col!=1 && col!=9 && col!=17 && col!=25) { //exclude test columns!!!
380             
381               if (fScanObj->GetHits(0,hs,chipNr,col,row)==NrTriggers) {   
382                         pixel100++;
383                         pixel100hs++;
384                         pixel100chip++;
385               }
386               if (fScanObj->GetHits(0,hs,chipNr,col,row)==0) {
387                         zeri++;
388                         zerihs++;
389                         zerichip++;
390               }
391               if (fScanObj->GetHits(0,hs,chipNr,col,row)>NrTriggers) {    
392                         pixelN++;
393                         pixelNhs++;
394                         pixelNchip++;
395               }
396             }
397           }
398         }
399       
400         Float_t TPeffChip=(pixel100chip/(28*(rowEnd-rowStart+1)))*100;
401         fTPeffChip[hs]->Fill(chipNr,TPeffChip);
402         
403         Float_t DeadPixelChip=(zerichip/(28*(rowEnd-rowStart+1)))*100;
404         fDeadPixelChip[hs]->Fill(chipNr,DeadPixelChip);
405         
406         Float_t NoisyPixelChip=(pixelNchip/(28*(rowEnd-rowStart+1)))*100;
407         fNoisyPixelChip[hs]->Fill(chipNr,NoisyPixelChip);
408       }
409     }
410     
411     Float_t TPeffHS=(pixel100hs/(28*numChipsActiveHS*(rowEnd-rowStart+1)))*100;
412     fTPeffHS->Fill(hs,TPeffHS);
413     
414     Float_t DeadPixelHS=(zerihs/(28*numChipsActiveHS*(rowEnd-rowStart+1)))*100;
415     fDeadPixelHS->Fill(hs,DeadPixelHS);
416     
417     Float_t NoisyPixelHS=(pixelNhs/(28*numChipsActiveHS*(rowEnd-rowStart+1)))*100;
418     fNoisyPixelHS->Fill(hs,NoisyPixelHS);
419   }
420   
421   fTPeff=(pixel100/(28*numChipsActive*(rowEnd-rowStart+1)))*100;
422   fDeadPixel=(zeri/(28*numChipsActive*(rowEnd-rowStart+1)))*100;
423   fNoisyPixel=(pixelN/(28*numChipsActive*(rowEnd-rowStart+1)))*100;
424
425   return kTRUE;
426 }
427 //_________________________________________________________________________
428 void AliITSOnlineSPDscanAnalyzer::CreateUniformityHistograms() {
429   // create uniformity histograms to be filled by "ProcessUniformity" method
430   DeleteUniformityHistograms(); // make sure no old histograms are lying around...
431   UInt_t eq = GetRouterNr();
432   TString label;
433
434   label = Form("Ratio of 'Good' Pixels Per HS (eq %d)",eq);
435   fTPeffHS = new TH1F(label.Data(),label.Data(),6,-0.5,5.5);
436   fTPeffHS->SetXTitle("hs");
437   fTPeffHS->SetYTitle("ratio [%]");
438   fTPeffHS->SetFillColor(kBlue);
439   fTPeffHS->SetStats(0);
440
441   label = Form("Ratio of 'Dead' Pixels Per HS (eq %d)",eq);
442   fDeadPixelHS = new TH1F(label.Data(),label.Data(),6,-0.5,5.5);
443   fDeadPixelHS->SetXTitle("hs");
444   fDeadPixelHS->SetYTitle("ratio [%]");
445   fDeadPixelHS->SetFillColor(kBlue);
446   fDeadPixelHS->SetStats(0);
447
448   label = Form("Ratio of 'Noisy' Pixels Per HS (eq %d)",eq);
449   fNoisyPixelHS = new TH1F(label.Data(),label.Data(),6,-0.5,5.5);
450   fNoisyPixelHS->SetXTitle("hs");
451   fNoisyPixelHS->SetYTitle("ratio [%]");
452   fNoisyPixelHS->SetFillColor(kBlue);
453   fNoisyPixelHS->SetStats(0);
454
455   for (UInt_t hs=0; hs<6; hs++) {
456     label = Form("Ratio of 'Good' Pixels Per Chip (eq %d, hs %d)",eq,hs);
457     fTPeffChip[hs] = new TH1F(label.Data(),label.Data(),10,-0.5,9.5);
458     fTPeffChip[hs]->SetXTitle("chip");
459     fTPeffChip[hs]->SetYTitle("ratio [%]");
460     fTPeffChip[hs]->SetFillColor(kBlue);
461     fTPeffChip[hs]->SetStats(0);
462
463     label = Form("Ratio of 'Dead' Pixels Per Chip (eq %d, hs %d)",eq,hs);
464     fDeadPixelChip[hs] = new TH1F(label.Data(),label.Data(),10,-0.5,9.5);
465     fDeadPixelChip[hs]->SetXTitle("chip");
466     fDeadPixelChip[hs]->SetYTitle("ratio [%]");
467     fDeadPixelChip[hs]->SetFillColor(kBlue);
468     fDeadPixelChip[hs]->SetStats(0);
469
470     label = Form("Ratio of 'Noisy' Pixels Per Chip (eq %d, hs %d)",eq,hs);
471     fNoisyPixelChip[hs] = new TH1F(label.Data(),label.Data(),10,-0.5,9.5);
472     fNoisyPixelChip[hs]->SetXTitle("chip");
473     fNoisyPixelChip[hs]->SetYTitle("ratio [%]");
474     fNoisyPixelChip[hs]->SetFillColor(kBlue);
475     fNoisyPixelChip[hs]->SetStats(0);
476   }
477
478 }
479 //_________________________________________________________________________
480 void AliITSOnlineSPDscanAnalyzer::DeleteUniformityHistograms() {
481   // remove uniformity histograms if they are created
482   if (fTPeffHS!=NULL) {
483     delete fTPeffHS;
484     fTPeffHS=NULL;
485   }
486   if (fDeadPixelHS!=NULL) {
487     delete fDeadPixelHS;
488     fDeadPixelHS=NULL;
489   }
490   if (fNoisyPixelHS!=NULL) {
491     delete fNoisyPixelHS;
492     fNoisyPixelHS=NULL;
493   }
494   for (UInt_t hs=0; hs<6; hs++) {
495     if (fTPeffChip[hs]!=NULL) {
496       delete fTPeffChip[hs];
497       fTPeffChip[hs]=NULL;
498     }
499     if (fDeadPixelChip[hs]!=NULL) {
500       delete fDeadPixelChip[hs];
501       fDeadPixelChip[hs]=NULL;
502     }
503     if (fNoisyPixelChip[hs]!=NULL) {
504       delete fNoisyPixelChip[hs];
505       fNoisyPixelChip[hs]=NULL;
506     }
507   }
508 }
509 //_________________________________________________________________________
510 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessNoisyPixels(/*Char_t *oldcalibDir*/) {
511   // process noisy pixel data
512   if (fScanObj==NULL) {
513     Warning("AliITSOnlineSPDscanAnalyzer::ProcessNoisyPixels","No data!");
514     return kFALSE;
515   }
516   // should be type kNOISE
517   if (fType != kNOISE) {
518     Warning("AliITSOnlineSPDscanAnalyzer::ProcessNoisyPixels","Noisy pixels only for scan type %d.",kNOISE);
519     return kFALSE;
520   }
521   // handler should be initialized
522   if (fHandler==NULL) {
523     Error("AliITSOnlineSPDscanAnalyzer::ProcessNoisyPixels","Calibration handler is not initialized!");
524     return kFALSE;
525   }
526   // check if enough statistics
527   if (fScanObj->GetTriggers(0)<fNoiseMinimumEvents) {
528     Warning("AliITSOnlineSPDscanAnalyzer::ProcessNoisyPixels","Process noisy: Too few events.");
529     return kFALSE;
530   }
531
532   UInt_t routerNr = fScanObj->GetRouterNr();
533   for (UInt_t hs=0; hs<6; hs++) {
534     for (UInt_t chipNr=0; chipNr<10; chipNr++) {
535       if (fScanObj->GetChipPresent(hs,chipNr)) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
536         if (fOverWrite) {fHandler->ResetNoisyForChip(routerNr,hs,chipNr);}
537         for (UInt_t col=0; col<32; col++) {
538           for (UInt_t row=0; row<256; row++) {
539             if (fScanObj->GetHitsEfficiency(0,hs,chipNr,col,row)>fNoiseThreshold) {
540               fHandler->SetNoisyPixel(routerNr,hs,chipNr,col,row);
541             }
542           }
543         }
544       }
545     }
546   }
547   return kTRUE;
548 }
549 //_________________________________________________________________________
550 Int_t AliITSOnlineSPDscanAnalyzer::GetDelay(UInt_t hs, UInt_t chipNr) {
551   // get delay
552   if (hs>=6 || chipNr>10) return -1;
553   if (fScanObj==NULL) {
554     Warning("AliITSOnlineSPDscanAnalyzer::GetDelay","No data!");
555     return -1;
556   }
557   // should be type kDELAY or kDAC with id 42 (delay_ctrl)
558   if (fType!=kDELAY && (fType!=kDAC || fDacId!=42)) {
559     Warning("AliITSOnlineSPDscanAnalyzer::GetDelay","Delay only for scan type %d or %d and dac_id 42.",kDELAY,kDAC);
560     return -1;
561   }
562   if (fMeanMultiplicity[hs][chipNr]==NULL) {
563     if (!ProcessMeanMultiplicity()) {
564       return -1;
565     }
566   }
567
568   UInt_t maxStep=0;
569   Float_t maxVal=0;
570   for (UInt_t step=0; step<fScanObj->GetNSteps(); step++) {
571     Double_t thisDac;
572     Double_t thisMult;
573     fMeanMultiplicity[hs][chipNr]->GetPoint(step,thisDac,thisMult);
574     if (thisMult > maxVal) {
575       maxVal = thisMult;
576       maxStep = step;
577     }
578   }
579
580   if (maxVal>0) {
581     return ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(maxStep);
582   }
583   else {
584     return -1;
585   }
586
587 }
588 //_________________________________________________________________________
589 Int_t AliITSOnlineSPDscanAnalyzer::GetNrNoisyUnima(UInt_t hs, UInt_t chipNr) {
590   // in case of a uniformity scan, returns the nr of noisy pixels, (here > 200 hits)
591   if (hs>=6 || chipNr>10) return -1;
592   if (fScanObj==NULL) {
593     Error("AliITSOnlineSPDscanAnalyzer::GetNrNoisyUnima","No data!");
594     return kFALSE;
595   }
596   // should be type kUNIMA
597   if (fType != kUNIMA) {
598     Error("AliITSOnlineSPDscanAnalyzer::GetNrNoisyUnima","Noisy pixels Unima only for scan type %d.",kUNIMA);
599     return kFALSE;
600   }
601   if (fScanObj->GetTriggers(0)!=25600) {
602     Error("AliITSOnlineSPDscanAnalyzer::GetNrNoisyUnima","Process noisy unima: Incorrect number of events (!=25600.");
603     return kFALSE;
604   }
605
606   Int_t nrNoisy=0;
607   if (fScanObj->GetChipPresent(hs,chipNr)) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
608     for (UInt_t col=0; col<32; col++) {
609       for (UInt_t row=0; row<256; row++) {
610         if (fScanObj->GetHits(0,hs,chipNr,col,row)>200) {
611           nrNoisy++;
612         }
613       }
614     }
615   }
616   else {
617     return -1;
618   }
619   return nrNoisy;
620 }
621 //_________________________________________________________________________
622 Int_t AliITSOnlineSPDscanAnalyzer::FindLastMinThDac(UInt_t hs, UInt_t chipNr) {
623   // returns dac value where fMinIncreaseFromBaseLine reached
624   if (hs>=6 || chipNr>10) return -1;
625   if (fMeanMultiplicity[hs][chipNr]==NULL) {
626     if (!ProcessMeanMultiplicity()) {
627       return -1;
628     }
629   }
630   Double_t firstVal, dummy1;
631   fMeanMultiplicity[hs][chipNr]->GetPoint(0,dummy1,firstVal);
632   UInt_t step=0;
633   while (step<fScanObj->GetNSteps()-1) {
634     Double_t graphVal, dummy2;
635     fMeanMultiplicity[hs][chipNr]->GetPoint(step+1,dummy2,graphVal);
636     if (graphVal>firstVal+fMinIncreaseFromBaseLine) break;
637     step++;
638   }
639   if (step==fScanObj->GetNSteps()-1) return -1;
640   return ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step);
641 }
642
643 Int_t AliITSOnlineSPDscanAnalyzer::FindClosestLowerStep(Float_t dacValueInput) {
644   // returns step closest (lower) to a dacvalue 
645   UInt_t step=0;
646   while (step<fScanObj->GetNSteps()-1) {
647     Int_t dacVal = ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step+1);
648     if (dacVal>=dacValueInput) break;
649     step++;
650   }
651   return step;
652 }
653 //_________________________________________________________________________
654 Float_t AliITSOnlineSPDscanAnalyzer::GetCompareLine(UInt_t step, UInt_t hs, UInt_t chipNr, Float_t basePar2) {
655   // returns value to compare mean mult with (when finding min th)
656   if (hs>=6 || chipNr>10) return -1;
657   if (step<fMinNrStepsBeforeIncrease) return -1;
658   Float_t baseLine = basePar2;
659   if (baseLine<0) baseLine=0;
660   Float_t baseAdd;
661   Double_t baseM=0;
662   Double_t baseS=0;
663   Double_t d,m;
664   for (UInt_t st=1;st<2*step/3;st++) { // skip first point...
665     fMeanMultiplicity[hs][chipNr]->GetPoint(st,d,m);
666     baseM+=m-baseLine;
667     baseS+=(m-baseLine)*(m-baseLine);
668   }
669   baseAdd=2*sqrt( baseS/(2*step/3-1) - (baseM/(2*step/3-1))*(baseM/(2*step/3-1)) );
670   baseAdd+=0.03; // magic number
671   if (baseAdd>fMinIncreaseFromBaseLine) baseAdd=fMinIncreaseFromBaseLine;
672   return baseLine + baseAdd;
673 }
674
675 Int_t AliITSOnlineSPDscanAnalyzer::GetMinTh(UInt_t hs, UInt_t chipNr) {
676   // calculates and returns the minimum threshold
677   if (hs>=6 || chipNr>10) return -1;
678   if (fScanObj==NULL) {
679     Error("AliITSOnlineSPDscanAnalyzer::GetMinTh","No data!");
680     return -1;
681   }
682   // should be type  kMINTH  or  kDAC with id 39 (pre_vth)
683   if (fType!=kMINTH && (fType!=kDAC || fDacId!=39)) {
684     Error("AliITSOnlineSPDscanAnalyzer::GetMinTh","MinTh only for scan type %d OR %d with dac_id 39.",kMINTH,kDAC);
685     return -1;
686   }
687   if (fMeanMultiplicity[hs][chipNr]==NULL) {
688     if (!ProcessMeanMultiplicity()) {
689       return -1;
690     }
691   }
692
693   Int_t lastDac = FindLastMinThDac(hs,chipNr);
694   if (lastDac==-1) {
695     Warning("AliITSOnlineSPDscanAnalyzer::GetMinTh","HS%d, Chip%d: Increase of Mean Multiplicity by %1.2f never reached.",hs,chipNr,fMinIncreaseFromBaseLine);
696     return -1;
697   }
698
699   Int_t minDac = ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(0);
700   TString funcName = Form("Fit minth func HS%d CHIP%d",hs,chipNr);
701   TF1 *minThFunc = new TF1(funcName.Data(),itsSpdErrorf,100,500,3);
702   minThFunc->SetParameter(0,lastDac+10);
703   minThFunc->SetParameter(1,2);
704   minThFunc->SetParameter(2,0);
705   minThFunc->SetParName(0,"Mean");
706   minThFunc->SetParName(1,"Sigma");
707   minThFunc->SetParName(2,"BaseLine");
708   minThFunc->SetLineWidth(1);
709   if (fMeanMultiplicity[hs][chipNr]==NULL) {
710     if (!ProcessMeanMultiplicity()) {
711       return -1;
712     }
713   }
714   fMeanMultiplicity[hs][chipNr]->Fit(funcName,"Q0","",minDac,lastDac);
715
716   //  Double_t mean = fMinThFunc[hs][chipNr]->GetParameter(0);
717   //  Double_t sigma = fMinThFunc[hs][chipNr]->GetParameter(1);
718   Double_t baseLine = minThFunc->GetParameter(2);
719   delete minThFunc;
720
721   if (baseLine>fMaxBaseLineLevel) {
722     Warning("AliITSOnlineSPDscanAnalyzer::GetMinTh","HS%d, Chip%d: BaseLine too large (%1.2f>%1.2f).",hs,chipNr,baseLine,fMaxBaseLineLevel);
723     return -1;
724   }
725   UInt_t step=FindClosestLowerStep(lastDac);
726   Float_t compareLine = GetCompareLine(step,hs,chipNr,baseLine);
727   if (compareLine==-1) {
728     Warning("AliITSOnlineSPDscanAnalyzer::GetMinTh","HS%d, Chip%d: Not enough steps (%d<%d) before increase to get a compare line.",hs,chipNr,step,fMinNrStepsBeforeIncrease);
729     return -1;
730   }
731
732   Double_t mult, dummy;
733   mult=1000;
734   while (mult > compareLine && step>0) {
735     fMeanMultiplicity[hs][chipNr]->GetPoint(step,dummy,mult);
736     step--;
737   }
738   Int_t minth = ((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step+1)-fStepDownDacSafe;
739
740   if (step>0) {
741     return minth;
742   }
743   else {
744     Warning("AliITSOnlineSPDscanAnalyzer::GetMinTh","HS%d, Chip%d: Did not find a point below the compare line (%f).",hs,chipNr,compareLine);
745     return -1;
746   }
747 }
748 //_________________________________________________________________________
749 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessMeanMultiplicity() {
750   // process mean multiplicity data
751   if (fScanObj==NULL) {
752     Error("AliITSOnlineSPDscanAnalyzer::ProcessMeanMultiplicity","No data!");
753     return kFALSE;
754   }
755   for (UInt_t step=0; step<fScanObj->GetNSteps(); step++) {
756     for (UInt_t hs=0; hs<6; hs++) {
757       for (UInt_t chipNr=0; chipNr<11; chipNr++) {
758         //      if (fScanObj->GetChipPresent(hs,chipNr)) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
759         if (step==0) {
760           if (fMeanMultiplicity[hs][chipNr]!=NULL) {
761             delete fMeanMultiplicity[hs][chipNr];
762           }
763           fMeanMultiplicity[hs][chipNr] = new TGraph();
764         }
765         Float_t multiplMean=fScanObj->GetAverageMultiplicity(step,hs,chipNr);
766         if (fType==kMINTH || fType==kMEANTH || fType==kDAC || fType==kDELAY) {
767           fMeanMultiplicity[hs][chipNr]->SetPoint(step,((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step),multiplMean);
768         }
769         else {
770           fMeanMultiplicity[hs][chipNr]->SetPoint(step,0,multiplMean);
771         }
772       }
773       //      }
774     }
775   }
776   return kTRUE;
777 }
778 //_________________________________________________________________________
779 TGraph* AliITSOnlineSPDscanAnalyzer::GetMeanMultiplicityG(UInt_t hs, UInt_t chipNr) {
780   // returns mean multiplicity graph
781   if (hs>=6 || chipNr>10) return NULL;
782   if (fMeanMultiplicity[hs][chipNr]==NULL) {
783     if (!ProcessMeanMultiplicity()) {
784       return NULL;
785     }
786   }
787   return fMeanMultiplicity[hs][chipNr];
788 }
789 //_________________________________________________________________________
790 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessHitEventEfficiency() {
791   // process hit event efficiency
792   if (fScanObj==NULL) {
793     Error("AliITSOnlineSPDscanAnalyzer::ProcessHitEventEfficiency","No data!");
794     return kFALSE;
795   }
796   for (UInt_t step=0; step<fScanObj->GetNSteps(); step++) {
797     for (UInt_t hs=0; hs<6; hs++) {
798       for (UInt_t chipNr=0; chipNr<11; chipNr++) {
799         //      if (fScanObj->GetChipPresent(hs,chipNr)) { // check the status of the chippresent parameter in the mood header!!!!!!!!!!!!!!!!!!!!!!!!!!!!
800         if (step==0) {
801           if (fHitEventEfficiency[hs][chipNr]!=NULL) {
802             delete fHitEventEfficiency[hs][chipNr];
803           }
804           fHitEventEfficiency[hs][chipNr] = new TGraph();
805         }
806         Float_t efficiency=fScanObj->GetHitEventsEfficiency(step,hs,chipNr);
807         if (fType==kMINTH || fType==kMEANTH || fType==kDAC || fType==kDELAY) {
808           fHitEventEfficiency[hs][chipNr]->SetPoint(step,((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step),efficiency);
809         }
810         else {
811           fHitEventEfficiency[hs][chipNr]->SetPoint(step,0,efficiency);
812         }
813       }
814       //      }
815     }
816   }
817   return kTRUE;
818 }
819 //_________________________________________________________________________
820 TGraph* AliITSOnlineSPDscanAnalyzer::GetHitEventEfficiencyG(UInt_t hs, UInt_t chipNr) {
821   // returns hit event efficiency graph
822   if (hs>=6 || chipNr>10) return NULL;
823   if (fHitEventEfficiency[hs][chipNr]==NULL) {
824     if (!ProcessHitEventEfficiency()) {
825       return NULL;
826     }
827   }
828   return fHitEventEfficiency[hs][chipNr];
829 }
830 //_________________________________________________________________________
831 Bool_t AliITSOnlineSPDscanAnalyzer::ProcessNrTriggers() {
832   // process nr of triggers data
833   if (fScanObj==NULL) {
834     Error("AliITSOnlineSPDscanAnalyzer::ProcessNrTriggers","No data!");
835     return kFALSE;
836   }
837   for (UInt_t step=0; step<fScanObj->GetNSteps(); step++) {
838     if (step==0) {
839       if (fTriggers!=NULL) {
840         delete fTriggers;
841       }
842       fTriggers = new TGraph();
843     }
844     if (fType==kMINTH || fType==kMEANTH || fType==kDAC || fType==kDELAY) {
845       fTriggers->SetPoint(step,((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step),fScanObj->GetTriggers(step));
846     }
847     else {
848       fTriggers->SetPoint(step,0,fScanObj->GetTriggers(step));
849     }
850   }
851   return kTRUE;
852 }
853 //_________________________________________________________________________
854 TGraph* AliITSOnlineSPDscanAnalyzer::GetNrTriggersG() {
855   // returns nr of triggers graph
856   if (fTriggers==NULL) {
857     if (!ProcessNrTriggers()) {
858       return NULL;
859     }
860   }
861   return fTriggers;
862 }
863 //_________________________________________________________________________
864 Bool_t AliITSOnlineSPDscanAnalyzer::GetHalfStavePresent(UInt_t hs) {
865   // returns half stave present info
866   if (hs<6 && fScanObj!=NULL) {
867     Int_t chipstatus=0;
868     for (Int_t chip=0; chip<10; chip++) {
869       chipstatus+=fScanObj->GetChipPresent(hs,chip);
870     }
871     if (chipstatus>0) return kTRUE;
872   }
873   return kFALSE;
874 }
875 //_________________________________________________________________________
876 UInt_t AliITSOnlineSPDscanAnalyzer::GetRouterNr() {
877   // returns the router nr of scan obj
878   if (fScanObj!=NULL) return fScanObj->GetRouterNr(); 
879   else return 99;
880 }
881 //_________________________________________________________________________
882 TH2F* AliITSOnlineSPDscanAnalyzer::GetHitMapTot(UInt_t step) {
883   // creates and returns a pointer to a hitmap histo (half sector style a la spdmood)
884   if (fScanObj==NULL) {
885     Error("AliITSOnlineSPDscanAnalyzer::GetHitMapTot","No data!");
886     return NULL;
887   }
888   TString histoname;
889   if (fType==kMINTH || fType==kMEANTH || fType==kDAC) {
890     histoname = Form("Router %d , DAC %d",GetRouterNr(),((AliITSOnlineSPDscanMultiple*)fScanObj)->GetDacValue(step));
891   }
892   else {
893     histoname = Form("Router %d ",GetRouterNr());
894   }
895   TH2F* fHitMapTot = new TH2F(histoname.Data(),histoname.Data(),32*10,-0.5,32*10-0.5,256*6,-0.5,256*6-0.5);
896   fHitMapTot->SetNdivisions(-10,"X");
897   fHitMapTot->SetNdivisions(-006,"Y");
898   fHitMapTot->SetTickLength(0,"X");
899   fHitMapTot->SetTickLength(0,"Y");
900   fHitMapTot->GetXaxis()->SetLabelColor(gStyle->GetCanvasColor());
901   fHitMapTot->GetYaxis()->SetLabelColor(gStyle->GetCanvasColor());
902   for (UInt_t hs=0; hs<6; hs++) {
903     for (UInt_t chipNr=0; chipNr<10; chipNr++) {
904       for (UInt_t col=0; col<32; col++) {
905         for (UInt_t row=0; row<256; row++) {
906           fHitMapTot->Fill(chipNr*32+col,(5-hs)*256+row,fScanObj->GetHits(step,hs,chipNr,col,row));
907         }
908       }
909     }
910   }
911   return fHitMapTot;
912 }
913 //_________________________________________________________________________
914 TH2F* AliITSOnlineSPDscanAnalyzer::GetHitMapChip(UInt_t step, UInt_t hs, UInt_t chip) {
915   // creates and returns a pointer to a hitmap histo (chip style a la spdmood)
916   if (fScanObj==NULL) {
917     Error("AliITSOnlineSPDscanAnalyzer::GetHitMapChip","No data!");
918     return NULL;
919   }
920
921   TString histoName;
922   TString histoTitle;
923   histoName = Form("fChipHisto_%d_%d_%d", GetRouterNr(), hs, chip);
924   histoTitle = Form("Eq ID %d, Half Stave %d, Chip %d", GetRouterNr(), hs, chip);
925
926   TH2F *returnHisto = new TH2F(histoName.Data(), histoTitle.Data(), 32, -0.5, 31.5, 256, -0.5, 255.5);
927   returnHisto->SetMinimum(0);
928   for (UInt_t col=0; col<32; col++) {
929     for (UInt_t row=0; row<256; row++) {
930       returnHisto->Fill(col,row,fScanObj->GetHits(step,hs,chip,col,row));
931     }
932   }
933
934   return returnHisto;
935 }
936