abb50c8bdab453434793ef2315fda18ea9a9f711
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibPadStatus.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 //                                                                        //
20 // Example: fill pedestal with Gaussian noise                             //
21 //                                                                        //
22 //  AliTRDCalibPadStatus ped;                                             //
23 //  ped.TestEvent(numberofevent);                                         //
24 //                                                                        //
25 //  // Method without histo                                               //
26 //  ped.Analyse();                                                        //
27 //                                                                        //
28 //  // Create the histo of the AliTRDCalROC                               //
29 //  TH2F * histo2dm = ped.GetCalRocMean(0,kFALSE)->MakeHisto2D();         //
30 //  histo2dm->Scale(10.0);                                                //
31 //  TH1F * histo1dm = ped.GetCalRocMean(0,kFALSE)->MakeHisto1D();         //
32 //  histo1dm->Scale(10.0);                                                //
33 //  TH2F * histo2ds = ped.GetCalRocSquares(0,kFALSE)->MakeHisto2D();      //
34 //  histo2ds->Scale(10.0);                                                //
35 //  TH1F * histo1ds = ped.GetCalRocSquares(0,kFALSE)->MakeHisto1D();      //
36 //  histo1ds->Scale(10.0)                                                 //
37 //                                                                        //
38 //  // Draw output                                                        //
39 //  TCanvas* c1 = new TCanvas;                                            //
40 //  c1->Divide(2,2);                                                      //
41 //  c1->cd(1);                                                            //
42 //  histo2dm->Draw("colz");                                               //
43 //  c1->cd(2);                                                            //
44 //  histo1dm->Draw();                                                     //
45 //  c1->cd(3);                                                            //
46 //  histo2ds->Draw("colz");                                               //
47 //  c1->cd(4);                                                            //
48 //  histo1ds->Draw();                                                     //
49 //                                                                        //
50 //  // Method with histo                                                  //
51 //  ped.AnalyseHisto();                                                   //
52 //                                                                        //
53 //  // Take the histo                                                     //
54 //  TH1F *histo = ped.GetHisto(31);                                       //
55 //  histo->SetEntries(1);                                                 //
56 //  histo->Draw();                                                        //
57 //                                                                        //
58 ////////////////////////////////////////////////////////////////////////////
59
60 //Root includes
61 #include <TObjArray.h>
62 #include <TH2F.h>
63 #include <TString.h>
64 #include <TMath.h>
65 #include <TRandom.h>
66 #include <TDirectory.h>
67 #include <TFile.h>
68
69 //AliRoot includes
70 #include <AliMathBase.h>
71 #include "AliRawReader.h"
72 #include "AliRawReaderRoot.h"
73 #include "AliRawReaderDate.h"
74
75 //header file
76 #include "AliLog.h"
77 #include "AliTRDCalibPadStatus.h"
78 #include "AliTRDRawStreamV2.h"
79 #include "AliTRDgeometry.h"
80 #include "AliTRDCommonParam.h"
81 #include "./Cal/AliTRDCalROC.h"
82 #include "./Cal/AliTRDCalPadStatus.h"
83 #include "./Cal/AliTRDCalDet.h"
84 #include "./Cal/AliTRDCalPad.h"
85 #include "./Cal/AliTRDCalSingleChamberStatus.h"
86
87 #ifdef ALI_DATE
88 #include "event.h"
89 #endif
90
91 ClassImp(AliTRDCalibPadStatus) /*FOLD00*/
92
93 //_____________________________________________________________________
94 AliTRDCalibPadStatus::AliTRDCalibPadStatus() : /*FOLD00*/
95   TObject(),
96   fGeo(0),
97   fAdcMin(0),
98   fAdcMax(20),
99   fDetector(-1),
100   fNumberOfTimeBins(0),
101   fCalRocArrayMean(540),
102   fCalRocArrayRMS(540),
103   fCalRocArrayMeand(540),
104   fCalRocArrayRMSd(540),
105   fHistoArray(540)
106 {
107     //
108     // default constructor
109     //
110
111   fGeo = new AliTRDgeometry();
112
113 }
114
115 //_____________________________________________________________________
116 AliTRDCalibPadStatus::AliTRDCalibPadStatus(const AliTRDCalibPadStatus &ped) : /*FOLD00*/
117   TObject(ped),
118   fGeo(0),
119   fAdcMin(ped.GetAdcMin()),
120   fAdcMax(ped.GetAdcMax()),
121   fDetector(ped.fDetector),
122   fNumberOfTimeBins(ped.fNumberOfTimeBins),
123   fCalRocArrayMean(540),
124   fCalRocArrayRMS(540),
125   fCalRocArrayMeand(540),
126   fCalRocArrayRMSd(540),
127   fHistoArray(540)
128 {
129     //
130     // copy constructor
131     //
132     for (Int_t idet = 0; idet < 540; idet++){
133         const AliTRDCalROC *calRocMean  = (AliTRDCalROC*)ped.fCalRocArrayMean.UncheckedAt(idet);
134         const AliTRDCalROC *calRocRMS   = (AliTRDCalROC*)ped.fCalRocArrayRMS.UncheckedAt(idet);
135         const AliTRDCalROC *calRocMeand = (AliTRDCalROC*)ped.fCalRocArrayMeand.UncheckedAt(idet);
136         const AliTRDCalROC *calRocRMSd  = (AliTRDCalROC*)ped.fCalRocArrayRMSd.UncheckedAt(idet);
137         const TH2F         *hped        = (TH2F*)ped.fHistoArray.UncheckedAt(idet);
138     
139         if ( calRocMean != 0x0 ) fCalRocArrayMean.AddAt(new AliTRDCalROC(*calRocMean), idet);
140         if ( calRocRMS != 0x0 )  fCalRocArrayRMS.AddAt(new AliTRDCalROC(*calRocRMS), idet);
141
142         if ( calRocMeand != 0x0 ) fCalRocArrayMeand.AddAt(new AliTRDCalROC(*calRocMeand), idet);
143         if ( calRocRMSd != 0x0 )  fCalRocArrayRMSd.AddAt(new AliTRDCalROC(*calRocRMSd), idet);
144
145         if ( hped != 0x0 ){
146           TH2F *hNew = new TH2F(*hped);
147           hNew->SetDirectory(0);
148           fHistoArray.AddAt(hNew,idet);
149         }
150         
151     }
152     if (fGeo) {
153       delete fGeo;
154     }
155     fGeo = new AliTRDgeometry();
156 }
157
158 //_____________________________________________________________________
159 AliTRDCalibPadStatus& AliTRDCalibPadStatus::operator = (const  AliTRDCalibPadStatus &source)
160 {
161   //
162   // assignment operator
163   //
164   if (&source == this) return *this;
165   new (this) AliTRDCalibPadStatus(source);
166
167   return *this;
168 }
169
170 //_____________________________________________________________________
171 AliTRDCalibPadStatus::~AliTRDCalibPadStatus() /*FOLD00*/
172 {
173   //
174   // destructor
175   //
176   if (fGeo) {
177     delete fGeo;
178   }
179 }
180
181 //_____________________________________________________________________
182 Int_t AliTRDCalibPadStatus::UpdateHisto(const Int_t icdet, /*FOLD00*/
183                                         const Int_t icRow,
184                                         const Int_t icCol,
185                                         const Int_t csignal,
186                                         const Int_t crowMax,
187                                         const Int_t ccold,
188                                         const Int_t icMcm)
189 {
190   //
191   // Signal filling methode 
192   //
193   Int_t nbchannel = icRow+icCol*crowMax;
194
195   // now the case of double read channel
196   if(ccold > 0){
197     nbchannel = (((ccold-1)*8+ icMcm)*crowMax+icRow)+144*crowMax;
198     //printf("nbchannel %d, ccold %d, icMcm %d, crowMax %d, icRow %d\n",nbchannel,ccold,icMcm,crowMax,icRow);
199   }
200   
201   // fast filling methode.
202   // Attention: the entry counter of the histogram is not increased
203   //            this means that e.g. the colz draw option gives an empty plot
204   Int_t bin = 0;
205   if ( !(((Int_t)csignal>fAdcMax ) || ((Int_t)csignal<fAdcMin)) )
206     bin = (nbchannel+1)*(fAdcMax-fAdcMin+2)+((Int_t)csignal-fAdcMin+1);
207   
208   GetHisto(icdet,kTRUE)->GetArray()[bin]++;
209   
210   return 0;
211 }
212 //_____________________________________________________________________
213 Int_t AliTRDCalibPadStatus::ProcessEvent(AliTRDRawStreamV2 *rawStream, Bool_t nocheck)
214 {
215   //
216   // Event Processing loop - AliTRDRawStreamCosmic
217   // 0 time bin problem or zero suppression
218   // 1 no input
219   // 2 input
220   //  
221
222   Int_t withInput = 1;
223
224   //rawStream->SetSharedPadReadout(kTRUE);
225
226   if(!nocheck) {
227     while (rawStream->Next()) {
228       Int_t rawversion = rawStream->GetRawVersion();                     //  current raw version
229       //if(!rawStream->IsDataZeroSuppressed()) {
230       if(rawversion > 2) {
231         AliInfo(Form("this is not no-zero-suppressed data, the version is %d",rawversion));
232         return 0;
233       }
234       Int_t idetector  = rawStream->GetDet();                            //  current detector
235       Int_t iRow       = rawStream->GetRow();                            //  current row
236       Int_t iRowMax    = rawStream->GetMaxRow();                         //  current rowmax
237       Int_t iCol       = rawStream->GetCol();                            //  current col
238       
239
240       Int_t iADC       = 21-rawStream->GetADC();                            //  current ADC
241       Int_t col        = 0;
242       if(iADC == 1) col = 1;
243       else {
244         col = TMath::Max(0,(Int_t)(iADC-19));
245         if(col > 0) col++;
246       }
247       Int_t mcm        = (Int_t)(iCol/18);                               //  current group of 18 col pads
248       if(col > 1) mcm -= 1;      
249       if(col ==1) mcm += 1;
250
251       //Bool_t shared = rawStream->IsCurrentPadShared();                  
252       //printf("ADC %d, iCol %d, col %d, mcm %d, shared %d\n",iADC,iCol,col,mcm,(Int_t)shared);
253
254       Int_t iTimeBin   = rawStream->GetTimeBin();                        //  current time bin
255       Int_t *signal    = rawStream->GetSignals();                        //  current ADC signal
256       Int_t nbtimebin  = rawStream->GetNumberOfTimeBins();               //  number of time bins read from data
257
258       if((fDetector != -1) && (nbtimebin != fNumberOfTimeBins)) {
259         AliInfo(Form("the number of time bins is %d, is different from the previous one %d",nbtimebin,fNumberOfTimeBins));
260         return 0;
261       }
262       fNumberOfTimeBins = nbtimebin;
263       
264       Int_t fin        = TMath::Min(nbtimebin,(iTimeBin+3));
265       Int_t n          = 0;
266       
267       for(Int_t k = iTimeBin; k < fin; k++){
268         if(signal[n]>0) UpdateHisto(idetector,iRow,iCol,signal[n],iRowMax,col,mcm);
269         n++;
270       }
271       
272       withInput = 2;
273     }
274   }
275   else {
276     while (rawStream->Next()) {
277       Int_t idetector  = rawStream->GetDet();                            //  current detector
278       Int_t iRow       = rawStream->GetRow();                            //  current row
279       Int_t iRowMax    = rawStream->GetMaxRow();                         //  current rowmax
280       Int_t iCol       = rawStream->GetCol();                            //  current col
281       
282
283       Int_t iADC       = 21-rawStream->GetADC();                            //  current ADC
284       Int_t col        = 0;
285       if(iADC == 1) col = 1;
286       else {
287         col = TMath::Max(0,(Int_t)(iADC-19));
288         if(col > 0) col++;
289       }
290       Int_t mcm        = (Int_t)(iCol/18);                               //  current group of 18 col pads
291       if(col > 1) mcm -= 1;      
292       if(col ==1) mcm += 1;
293
294       Int_t iTimeBin   = rawStream->GetTimeBin();                        //  current time bin
295       Int_t *signal    = rawStream->GetSignals();                        //  current ADC signal
296       Int_t nbtimebin = rawStream->GetNumberOfTimeBins();               //  number of time bins read from data
297       
298       Int_t fin        = TMath::Min(nbtimebin,(iTimeBin+3));
299       Int_t n          = 0;
300       
301       for(Int_t k = iTimeBin; k < fin; k++){
302         if(signal[n]>0) UpdateHisto(idetector,iRow,iCol,signal[n],iRowMax,col,mcm);
303         n++;
304       }
305       
306       withInput = 2;
307     }
308   }
309   
310   return withInput;
311 }
312
313 //_____________________________________________________________________
314 Int_t AliTRDCalibPadStatus::ProcessEvent(AliRawReader *rawReader, Bool_t nocheck)
315 {
316   //
317   //  Event processing loop - AliRawReader
318   //
319
320
321   AliTRDRawStreamV2 rawStream(rawReader);
322
323   rawReader->Select("TRD");
324
325   return ProcessEvent(&rawStream, nocheck);
326 }
327
328 //_________________________________________________________________________
329 Int_t AliTRDCalibPadStatus::ProcessEvent(
330 #ifdef ALI_DATE
331                                           eventHeaderStruct *event,
332                                           Bool_t nocheck
333 #else
334                                           eventHeaderStruct* /*event*/,
335                                           Bool_t /*nocheck*/
336             
337 #endif 
338                                           ) const
339 {
340   //
341   //  process date event
342   //
343 #ifdef ALI_DATE
344     AliRawReader *rawReader = new AliRawReaderDate((void*)event);
345     Bool_t result=ProcessEvent(rawReader, nocheck);
346     delete rawReader;
347     return result;
348 #else
349     Fatal("AliTRDCalibPadStatus", "this class was compiled without DATE");
350     return 0;
351 #endif
352
353 }
354
355 //_____________________________________________________________________
356 Bool_t AliTRDCalibPadStatus::TestEventHisto(Int_t nevent, Int_t sm) /*FOLD00*/
357 {
358   //
359   //  Test event loop
360   // fill one oroc and one iroc with random gaus
361   //
362
363   gRandom->SetSeed(0);
364
365     for (Int_t ism=sm; ism<sm+1; ism++){
366         for (Int_t ich=0; ich < 5; ich++){
367             for (Int_t ipl=0; ipl < 6; ipl++){
368               for(Int_t irow = 0; irow < fGeo->GetRowMax(ipl,ich,ism); irow++){
369                 for(Int_t icol = 0; icol < fGeo->GetColMax(ipl); icol++){
370                   for (Int_t iTimeBin=0; iTimeBin<(30*nevent); iTimeBin++){
371                     Int_t signal=(Int_t)(gRandom->Gaus(10.0,1.2));
372                     if ( signal>0 )UpdateHisto((ipl+ich*6+ism*6*5),irow,icol,signal,fGeo->GetRowMax(ipl,ich,ism),0,0);
373                   }
374                 }
375               }
376             }
377         }
378     }
379     return kTRUE;
380 }
381
382 //_____________________________________________________________________
383 TH2F* AliTRDCalibPadStatus::GetHisto(Int_t det, TObjArray *arr, /*FOLD00*/
384                                   Int_t nbinsY, Float_t ymin, Float_t ymax,
385                                   Char_t *type, Bool_t force)
386 {
387     //
388     // return pointer to histogram
389     // if force is true create a new histogram if it doesn't exist allready
390     //
391     if ( !force || arr->UncheckedAt(det) )
392         return (TH2F*)arr->UncheckedAt(det);
393
394     // if we are forced and histogram doesn't yes exist create it
395     Char_t name[255], title[255];
396
397     sprintf(name,"hCalib%s%.2d",type,det);
398     sprintf(title,"%s calibration histogram detector %.2d;ADC channel;Channel (pad)",type,det);
399
400    
401     Int_t nbchannels = fGeo->GetRowMax(GetPlane(det),GetChamber(det),GetSector(det))*fGeo->GetColMax(GetPlane(det));
402     
403     // we will add 3*8*rowMax channels at the end for the double counted
404     nbchannels += 3*8*(fGeo->GetRowMax(GetPlane(det),GetChamber(det),GetSector(det)));
405
406
407     // new histogram with calib information. One value for each pad!
408     TH2F* hist = new TH2F(name,title,
409                           nbinsY, ymin, ymax,
410                           nbchannels,0,nbchannels
411                           );
412     hist->SetDirectory(0);
413     arr->AddAt(hist,det);
414     return hist;
415 }
416
417 //_____________________________________________________________________
418 TH2F* AliTRDCalibPadStatus::GetHisto(Int_t det, Bool_t force) /*FOLD00*/
419 {
420     //
421     // return pointer to histogram
422     // if force is true create a new histogram if it doesn't exist allready
423     //
424     TObjArray *arr = &fHistoArray;
425     return GetHisto(det, arr, fAdcMax-fAdcMin, fAdcMin, fAdcMax, "Pedestal", force);
426 }
427
428 //_____________________________________________________________________
429 AliTRDCalROC* AliTRDCalibPadStatus::GetCalRoc(Int_t det, TObjArray* arr, Bool_t force) /*FOLD00*/
430 {
431     //
432     // return pointer to ROC Calibration
433     // if force is true create a new AliTRDCalROC if it doesn't exist allready
434     //
435     if ( !force || arr->UncheckedAt(det) )
436         return (AliTRDCalROC*)arr->UncheckedAt(det);
437
438     // if we are forced and histogram doesn't yes exist create it
439
440     // new AliTRDCalROC. One value for each pad!
441     AliTRDCalROC *croc = new AliTRDCalROC(GetPlane(det),GetChamber(det));
442     arr->AddAt(croc,det);
443     return croc;
444 }
445 //_____________________________________________________________________
446 AliTRDCalROC* AliTRDCalibPadStatus::GetCalRocMean(Int_t det, Bool_t force) /*FOLD00*/
447 {
448     //
449     // return pointer to Carge ROC Calibration
450     // if force is true create a new histogram if it doesn't exist allready
451     //
452     TObjArray *arr = &fCalRocArrayMean;
453     return GetCalRoc(det, arr, force);
454 }
455
456 //_____________________________________________________________________
457 AliTRDCalROC* AliTRDCalibPadStatus::GetCalRocRMS(Int_t det, Bool_t force) /*FOLD00*/
458 {
459     //
460     // return pointer to Carge ROC Calibration
461     // if force is true create a new histogram if it doesn't exist allready
462     //
463     TObjArray *arr = &fCalRocArrayRMS;
464     return GetCalRoc(det, arr, force);
465 }
466 //_____________________________________________________________________
467 AliTRDCalROC* AliTRDCalibPadStatus::GetCalRocMeand(Int_t det, Bool_t force) /*FOLD00*/
468 {
469     //
470     // return pointer to Carge ROC Calibration
471     // if force is true create a new histogram if it doesn't exist allready
472     //
473     TObjArray *arr = &fCalRocArrayMeand;
474     return GetCalRoc(det, arr, force);
475 }
476
477 //_____________________________________________________________________
478 AliTRDCalROC* AliTRDCalibPadStatus::GetCalRocRMSd(Int_t det, Bool_t force) /*FOLD00*/
479 {
480     //
481     // return pointer to Carge ROC Calibration
482     // if force is true create a new histogram if it doesn't exist allready
483     //
484     TObjArray *arr = &fCalRocArrayRMSd;
485     return GetCalRoc(det, arr, force);
486 }
487
488 //_____________________________________________________________________
489 void AliTRDCalibPadStatus::AnalyseHisto() /*FOLD00*/
490 {
491     //
492     //  Calculate calibration constants
493     //
494
495     Int_t nbinsAdc = fAdcMax-fAdcMin;
496
497     TVectorD param(3);
498     TMatrixD dummy(3,3);
499
500     Float_t *arrayHP=0;
501
502
503     for (Int_t idet=0; idet<540; idet++){
504         TH2F *hP = GetHisto(idet);
505         if ( !hP ) {
506           continue;
507         }
508
509         AliTRDCalROC *rocMean     = GetCalRocMean(idet,kTRUE);
510         AliTRDCalROC *rocRMS      = GetCalRocRMS(idet,kTRUE);
511
512         arrayHP = hP->GetArray();
513         Int_t nChannels = rocMean->GetNchannels();
514
515         for (Int_t iChannel=0; iChannel<nChannels; iChannel++){
516             Int_t offset = (nbinsAdc+2)*(iChannel+1)+1;
517             Double_t ret = AliMathBase::FitGaus(arrayHP+offset,nbinsAdc,fAdcMin,fAdcMax,&param,&dummy);
518             // if the fitting failed set noise and pedestal to 0
519             if ((ret==-4) || (ret==-1) || (ret==-2)) {
520                 param[1]=0.0;
521                 param[2]=0.0;
522             }
523             if((param[1]/10.0) > 65534.0) param[1] = 0.0;
524             if((param[2]/10.0) > 65534.0) param[2] = 0.0;
525             rocMean->SetValue(iChannel,param[1]/10.0);
526             rocRMS->SetValue(iChannel,param[2]/10.0);
527         }
528
529         // here we analyse doubled read channels
530         
531         AliTRDCalROC *rocMeand     = GetCalRocMeand(idet,kTRUE);
532         AliTRDCalROC *rocRMSd      = GetCalRocRMSd(idet,kTRUE);
533
534         Int_t nrows = rocMeand->GetNrows();
535         Int_t shift = 144*nrows;
536         Int_t total = shift+3*8*nrows; 
537
538         for (Int_t iChannel=shift; iChannel<total; iChannel++){
539             Int_t offset = (nbinsAdc+2)*(iChannel+1)+1;
540             Double_t ret = AliMathBase::FitGaus(arrayHP+offset,nbinsAdc,fAdcMin,fAdcMax,&param,&dummy);
541             // if the fitting failed set noise and pedestal to 0
542             if ((ret==-4) || (ret==-1) || (ret==-2)) {
543                 param[1]=0.0;
544                 param[2]=0.0;
545             }
546             if((param[1]/10.0) > 65534.0) param[1] = 0.0;
547             if((param[2]/10.0) > 65534.0) param[2] = 0.0;
548             
549             // here we have to recalculate backward
550             Int_t nb   = iChannel-shift;
551             Int_t row  = nb%nrows;
552             Int_t j    = (Int_t)(nb/nrows);
553             Int_t imcm = j%8;
554             Int_t icol = (Int_t)(j/8);
555             
556             Int_t finalcol = 18*imcm;
557             if(icol > 0) icol += 17;
558             else icol = -1;
559             finalcol += icol;
560
561             Int_t channel = row+finalcol*nrows;
562
563             //printf("iChannel %d, nrows %d, finalcol %d, row %d, channel %d\n",iChannel,nrows,finalcol,row,channel);
564             if((finalcol < 0) || (finalcol >= 144)) continue;
565
566             rocMeand->SetValue(channel,param[1]/10.0);
567             rocRMSd->SetValue(channel,param[2]/10.0);
568         }
569
570     }
571       
572 }
573
574 //_______________________________________________________________________________________
575 AliTRDCalPadStatus* AliTRDCalibPadStatus::CreateCalPadStatus()
576 {
577   //
578   // Create Pad Status out of Mean and RMS values
579   //
580
581   AliTRDCalPadStatus* obj = new AliTRDCalPadStatus("padstatus", "padstatus");
582   
583   for (Int_t idet=0; idet<540; ++idet)
584     {
585       AliTRDCalSingleChamberStatus *calROC = obj->GetCalROC(idet);
586
587       //Take the stuff
588       AliTRDCalROC *calRocMean    = ((AliTRDCalROC *)GetCalRocMean(idet));
589       AliTRDCalROC *calRocRMS     = ((AliTRDCalROC *)GetCalRocRMS(idet));
590
591       //Take the stuff second chance
592       AliTRDCalROC *calRocMeand    = ((AliTRDCalROC *)GetCalRocMeand(idet));
593       AliTRDCalROC *calRocRMSd     = ((AliTRDCalROC *)GetCalRocRMSd(idet));
594
595       if ( !calRocMean ) {
596         for(Int_t k = 0; k < calROC->GetNchannels(); k++){
597           calROC->SetStatus(k,AliTRDCalPadStatus::kMasked);
598         }
599         continue;
600       }
601       
602       //Range
603       Int_t channels = calROC->GetNchannels();
604       
605       Double_t rmsmean       = calRocMean->GetRMS()*10.0;
606       Double_t meanmean      = calRocMean->GetMean()*10.0;
607       Double_t meansquares   = calRocRMS->GetMean()*10.0;
608
609       for(Int_t ich = 0; ich < channels; ich++){
610         
611         Float_t mean     = calRocMean->GetValue(ich)*10.0;
612         Float_t rms      = calRocRMS->GetValue(ich)*10.0;
613         
614         if((rms <= 0.0001) || (TMath::Abs(mean-meanmean)>(5*rmsmean)) || (TMath::Abs(rms)>(5.0*TMath::Abs(meansquares)))) {
615           // look at second chance
616           Float_t meand     = calRocMeand->GetValue(ich)*10.0;
617           Float_t rmsd      = calRocRMSd->GetValue(ich)*10.0;
618
619           if((rmsd <= 0.0001) || (TMath::Abs(meand-meanmean)>(5*rmsmean)) || (TMath::Abs(rmsd)>(5.0*TMath::Abs(meansquares)))) {
620             calROC->SetStatus(ich, AliTRDCalPadStatus::kMasked);
621           }
622           else calROC->SetStatus(ich, AliTRDCalPadStatus::kReadSecond);
623         }
624         
625       }
626     }
627   
628   return obj;
629   
630 }
631 //_______________________________________________________________________________________
632 AliTRDCalPad* AliTRDCalibPadStatus::CreateCalPad()
633 {
634   //
635   // Create Pad Noise out of RMS values
636   //
637
638   AliTRDCalPad* obj = new AliTRDCalPad("PadNoise", "PadNoise");
639   
640   
641   for (Int_t det=0; det<AliTRDgeometry::kNdet; ++det)  {
642     
643     AliTRDCalROC *calROC22 = obj->GetCalROC(det);
644
645     AliTRDCalROC *calRocRMS     = ((AliTRDCalROC *)GetCalRocRMS(det,kTRUE));
646    
647     for(Int_t k = 0; k < calROC22->GetNchannels(); k++){
648       calROC22->SetValue(k,calRocRMS->GetValue(k));
649     }
650
651   }
652   
653   return obj;
654   
655 }
656
657 //_______________________________________________________________________________________
658 AliTRDCalDet* AliTRDCalibPadStatus::CreateCalDet() const
659 {
660   //
661   // Create Det Noise correction factor
662   //
663
664   AliTRDCalDet* obj = new AliTRDCalDet("DetNoise", "DetNoise (correction factor)");
665
666   for(Int_t l = 0; l < 540; l++){
667     obj->SetValue(l,10.0);
668   }
669   
670   return obj;
671   
672 }
673
674 //_____________________________________________________________________
675 void AliTRDCalibPadStatus::DumpToFile(const Char_t *filename, const Char_t *dir, Bool_t append) /*FOLD00*/
676 {
677     //
678     //  Write class to file
679     //
680
681     TString sDir(dir);
682     TString option;
683
684     if ( append )
685         option = "update";
686     else
687         option = "recreate";
688
689     TDirectory *backup = gDirectory;
690     TFile f(filename,option.Data());
691     f.cd();
692     if ( !sDir.IsNull() ){
693         f.mkdir(sDir.Data());
694         f.cd(sDir);
695     }
696     this->Write();
697     f.Close();
698
699     if ( backup ) backup->cd();
700 }
701
702 //_____________________________________________________________________
703 void AliTRDCalibPadStatus::SetCalRocMean(AliTRDCalROC *mean, Int_t det) /*FOLD00*/
704 {
705     //
706     //  Put the AliTRDCalROC in the array fCalRocArrayMean
707     //
708
709
710   AliTRDCalROC *rocMean = GetCalRocMean(det,kTRUE);
711   
712   Int_t nChannels = rocMean->GetNchannels();
713   
714   for (Int_t iChannel=0; iChannel<nChannels; iChannel++){
715     
716     rocMean->SetValue(iChannel,mean->GetValue(iChannel));
717     
718   }
719   
720 }
721
722 //_____________________________________________________________________
723 void AliTRDCalibPadStatus::SetCalRocRMS(AliTRDCalROC *rms, Int_t det) /*FOLD00*/
724 {
725     //
726     //  Put the AliTRDCalROC in the array fCalRocArrayRMS
727     //
728
729
730   AliTRDCalROC *rocRms = GetCalRocRMS(det,kTRUE);
731   
732   Int_t nChannels = rocRms->GetNchannels();
733   
734   for (Int_t iChannel=0; iChannel<nChannels; iChannel++){
735     
736     rocRms->SetValue(iChannel,rms->GetValue(iChannel));
737     
738   }
739   
740 }
741 //_____________________________________________________________________
742 void AliTRDCalibPadStatus::SetCalRocMeand(AliTRDCalROC *mean, Int_t det) /*FOLD00*/
743 {
744     //
745     //  Put the AliTRDCalROC in the array fCalRocArrayMean
746     //
747
748
749   AliTRDCalROC *rocMean = GetCalRocMeand(det,kTRUE);
750   
751   Int_t nChannels = rocMean->GetNchannels();
752   
753   for (Int_t iChannel=0; iChannel<nChannels; iChannel++){
754     
755     rocMean->SetValue(iChannel,mean->GetValue(iChannel));
756     
757   }
758   
759 }
760
761 //_____________________________________________________________________
762 void AliTRDCalibPadStatus::SetCalRocRMSd(AliTRDCalROC *rms, Int_t det) /*FOLD00*/
763 {
764     //
765     //  Put the AliTRDCalROC in the array fCalRocArrayRMS
766     //
767
768
769   AliTRDCalROC *rocRms = GetCalRocRMSd(det,kTRUE);
770   
771   Int_t nChannels = rocRms->GetNchannels();
772   
773   for (Int_t iChannel=0; iChannel<nChannels; iChannel++){
774     
775     rocRms->SetValue(iChannel,rms->GetValue(iChannel));
776     
777   }
778   
779 }
780 //_____________________________________________________________________________
781 Int_t AliTRDCalibPadStatus::GetPlane(Int_t d) const
782 {
783   //
784   // Reconstruct the plane number from the detector number
785   //
786
787   return ((Int_t) (d % 6));
788
789 }
790
791 //_____________________________________________________________________________
792 Int_t AliTRDCalibPadStatus::GetChamber(Int_t d) const
793 {
794   //
795   // Reconstruct the chamber number from the detector number
796   //
797
798   return ((Int_t) (d % 30) / 6);
799
800 }
801
802 //_____________________________________________________________________________
803 Int_t AliTRDCalibPadStatus::GetSector(Int_t d) const
804 {
805   //
806   // Reconstruct the sector number from the detector number
807   //
808
809   return ((Int_t) (d / 30));
810
811 }
812
813