Coverity fix
[u/mrichter/AliRoot.git] / TRD / TRDbase / AliTRDCalROC.cxx
CommitLineData
7754cd1f 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// Calibration base class for a single ROC //
21// Contains one UShort_t value per pad //
22// However, values are set and get as float, there are stored internally as //
23// (UShort_t) value * 10000 //
24// //
25///////////////////////////////////////////////////////////////////////////////
26
4c87f26e 27#include <TStyle.h>
c893147d 28#include <TMath.h>
29#include <TH2F.h>
30#include <TH1F.h>
4c87f26e 31
4c87f26e 32#include "AliMathBase.h"
c893147d 33
34#include "AliTRDCalROC.h"
7754cd1f 35
36ClassImp(AliTRDCalROC)
37
38//_____________________________________________________________________________
2745a409 39AliTRDCalROC::AliTRDCalROC()
40 :TObject()
41 ,fPla(0)
42 ,fCha(0)
43 ,fNrows(0)
44 ,fNcols(0)
45 ,fNchannels(0)
46 ,fData(0)
7754cd1f 47{
48 //
49 // Default constructor
50 //
51
7754cd1f 52}
53
54//_____________________________________________________________________________
2745a409 55AliTRDCalROC::AliTRDCalROC(Int_t p, Int_t c)
56 :TObject()
57 ,fPla(p)
58 ,fCha(c)
59 ,fNrows(0)
60 ,fNcols(144)
61 ,fNchannels(0)
62 ,fData(0)
7754cd1f 63{
64 //
65 // Constructor that initializes a given pad plane type
66 //
67
7754cd1f 68 //
69 // The pad plane parameter
70 //
71 switch (p) {
72 case 0:
73 if (c == 2) {
74 // L0C0 type
75 fNrows = 12;
76 }
77 else {
78 // L0C1 type
79 fNrows = 16;
80 }
81 break;
82 case 1:
83 if (c == 2) {
84 // L1C0 type
85 fNrows = 12;
86 }
87 else {
88 // L1C1 type
89 fNrows = 16;
90 }
91 break;
92 case 2:
93 if (c == 2) {
94 // L2C0 type
95 fNrows = 12;
96 }
97 else {
98 // L2C1 type
99 fNrows = 16;
100 }
101 break;
102 case 3:
103 if (c == 2) {
104 // L3C0 type
105 fNrows = 12;
106 }
107 else {
108 // L3C1 type
109 fNrows = 16;
110 }
111 break;
112 case 4:
113 if (c == 2) {
114 // L4C0 type
115 fNrows = 12;
116 }
117 else {
118 // L4C1 type
119 fNrows = 16;
120 }
121 break;
122 case 5:
123 if (c == 2) {
124 // L5C0 type
125 fNrows = 12;
126 }
127 else {
128 // L5C1 type
129 fNrows = 16;
130 }
131 break;
132 };
133
134 fNchannels = fNrows * fNcols;
2745a409 135 if (fNchannels != 0) {
7754cd1f 136 fData = new UShort_t[fNchannels];
2745a409 137 }
7754cd1f 138
2745a409 139 for (Int_t i=0; i<fNchannels; ++i) {
7754cd1f 140 fData[i] = 0;
2745a409 141 }
142
7754cd1f 143}
144
145//_____________________________________________________________________________
2745a409 146AliTRDCalROC::AliTRDCalROC(const AliTRDCalROC &c)
147 :TObject(c)
148 ,fPla(c.fPla)
149 ,fCha(c.fCha)
150 ,fNrows(c.fNrows)
151 ,fNcols(c.fNcols)
152 ,fNchannels(c.fNchannels)
153 ,fData(0)
7754cd1f 154{
155 //
156 // AliTRDCalROC copy constructor
157 //
158
2745a409 159 Int_t iBin = 0;
160
4c87f26e 161 fData = new UShort_t[fNchannels];
2745a409 162 for (iBin = 0; iBin < fNchannels; iBin++) {
4c87f26e 163 fData[iBin] = ((AliTRDCalROC &) c).fData[iBin];
2745a409 164 }
7754cd1f 165
166}
167
168//_____________________________________________________________________________
169AliTRDCalROC::~AliTRDCalROC()
170{
171 //
172 // AliTRDCalROC destructor
173 //
174
175 if (fData) {
176 delete [] fData;
177 fData = 0;
178 }
2745a409 179
7754cd1f 180}
181
182//_____________________________________________________________________________
183AliTRDCalROC &AliTRDCalROC::operator=(const AliTRDCalROC &c)
184{
185 //
186 // Assignment operator
187 //
188
a2865918 189 if (this == &c) {
190 return *this;
191 }
192
193 fPla = c.fPla;
194 fCha = c.fCha;
195 fNrows = c.fNrows;
196 fNcols = c.fNcols;
197 fNchannels = c.fNchannels;
198
199 if (fData) {
200 delete [] fData;
201 }
202 fData = new UShort_t[fNchannels];
203 for (Int_t iBin = 0; iBin < fNchannels; iBin++) {
204 fData[iBin] = ((AliTRDCalROC &) c).fData[iBin];
205 }
206
7754cd1f 207 return *this;
208
209}
210
211//_____________________________________________________________________________
212void AliTRDCalROC::Copy(TObject &c) const
213{
214 //
215 // Copy function
216 //
217
218 ((AliTRDCalROC &) c).fPla = fPla;
219 ((AliTRDCalROC &) c).fCha = fCha;
220
221 ((AliTRDCalROC &) c).fNrows = fNrows;
222 ((AliTRDCalROC &) c).fNcols = fNcols;
223
224 Int_t iBin = 0;
225
226 ((AliTRDCalROC &) c).fNchannels = fNchannels;
227
228 if (((AliTRDCalROC &) c).fData) delete [] ((AliTRDCalROC &) c).fData;
229 ((AliTRDCalROC &) c).fData = new UShort_t[fNchannels];
230 for (iBin = 0; iBin < fNchannels; iBin++) {
231 ((AliTRDCalROC &) c).fData[iBin] = fData[iBin];
232 }
4c87f26e 233
7754cd1f 234 TObject::Copy(c);
235
236}
237
4c87f26e 238//___________________________________________________________________________________
c893147d 239Double_t AliTRDCalROC::GetMean(AliTRDCalROC* const outlierROC) const
4c87f26e 240{
241 //
242 // Calculate the mean
243 //
244
245 Double_t *ddata = new Double_t[fNchannels];
c893147d 246 Int_t nPoints = 0;
4c87f26e 247 for (Int_t i=0;i<fNchannels;i++) {
248 if ((!outlierROC) || (!(outlierROC->GetValue(i)))) {
b3fcfd96 249 //if(fData[i] > 0.000000000000001){
c893147d 250 ddata[nPoints]= (Double_t) fData[i]/10000;
251 nPoints++;
b3fcfd96 252 //}
4c87f26e 253 }
254 }
c893147d 255 Double_t mean = TMath::Mean(nPoints,ddata);
4c87f26e 256 delete [] ddata;
257 return mean;
258}
259
260//_______________________________________________________________________________________
c893147d 261Double_t AliTRDCalROC::GetMedian(AliTRDCalROC* const outlierROC) const
4c87f26e 262{
263 //
264 // Calculate the median
265 //
266
267 Double_t *ddata = new Double_t[fNchannels];
c893147d 268 Int_t nPoints = 0;
4c87f26e 269 for (Int_t i=0;i<fNchannels;i++) {
270 if ((!outlierROC) || (!(outlierROC->GetValue(i)))) {
37b0cf5e 271 if(fData[i] > 0.000000000000001){
c893147d 272 ddata[nPoints]= (Double_t) fData[i]/10000;
273 nPoints++;
37b0cf5e 274 }
275 }
4c87f26e 276 }
c893147d 277 Double_t mean = TMath::Median(nPoints,ddata);
4c87f26e 278 delete [] ddata;
279 return mean;
280}
281
282//____________________________________________________________________________________________
c893147d 283Double_t AliTRDCalROC::GetRMS(AliTRDCalROC* const outlierROC) const
4c87f26e 284{
285 //
286 // Calculate the RMS
287 //
288
289 Double_t *ddata = new Double_t[fNchannels];
c893147d 290 Int_t nPoints = 0;
37b0cf5e 291 for (Int_t i=0;i<fNchannels;i++) {
292 if ((!outlierROC) || (!(outlierROC->GetValue(i)))) {
b3fcfd96 293 //if(fData[i] > 0.000000000000001){
c893147d 294 ddata[nPoints]= (Double_t)fData[i]/10000;
295 nPoints++;
b3fcfd96 296 //}
37b0cf5e 297 }
298 }
c893147d 299 Double_t mean = TMath::RMS(nPoints,ddata);
37b0cf5e 300 delete [] ddata;
301 return mean;
4c87f26e 302}
303
304//______________________________________________________________________________________________
c893147d 305Double_t AliTRDCalROC::GetLTM(Double_t *sigma, Double_t fraction, AliTRDCalROC* const outlierROC)
7754cd1f 306{
307 //
4c87f26e 308 // Calculate LTM mean and sigma
7754cd1f 309 //
310
4c87f26e 311 Double_t *ddata = new Double_t[fNchannels];
312 Double_t mean=0, lsigma=0;
c893147d 313 UInt_t nPoints = 0;
4c87f26e 314 for (Int_t i=0;i<fNchannels;i++) {
315 if (!outlierROC || !(outlierROC->GetValue(i))) {
37b0cf5e 316 if(fData[i] > 0.000000000000001){
c893147d 317 ddata[nPoints]= (Double_t) fData[i]/10000;
318 nPoints++;
37b0cf5e 319 }
4c87f26e 320 }
7754cd1f 321 }
c893147d 322 Int_t hh = TMath::Min(TMath::Nint(fraction *nPoints), Int_t(nPoints));
323 AliMathBase::EvaluateUni(nPoints,ddata, mean, lsigma, hh);
4c87f26e 324 if (sigma) *sigma=lsigma;
325 delete [] ddata;
326 return mean;
327}
2745a409 328
4c87f26e 329//___________________________________________________________________________________
330Bool_t AliTRDCalROC::Add(Float_t c1)
331{
332 //
333 // add constant
334 //
c893147d 335
4c87f26e 336 Bool_t result = kTRUE;
337 for (Int_t idata = 0; idata< fNchannels; idata++) {
338 if(((GetValue(idata)+c1) <= 6.5535) && ((GetValue(idata)+c1) >= 0.0)) SetValue(idata,GetValue(idata)+c1);
339 else {
340 SetValue(idata,0.0);
341 result = kFALSE;
342 }
343 }
344 return result;
345}
346
347//_______________________________________________________________________________________
348Bool_t AliTRDCalROC::Multiply(Float_t c1)
349{
350 //
351 // multiply constant
352 //
c893147d 353
4c87f26e 354 Bool_t result = kTRUE;
355 if(c1 < 0) return kFALSE;
356 for (Int_t idata = 0; idata< fNchannels; idata++) {
357 if((GetValue(idata)*c1) <= 6.5535) SetValue(idata,GetValue(idata)*c1);
358 else {
359 SetValue(idata,0.0);
360 result = kFALSE;
361 }
362 }
363 return result;
364}
365
366//____________________________________________________________________________________________
367Bool_t AliTRDCalROC::Add(const AliTRDCalROC * roc, Double_t c1)
368{
369 //
370 // add values
371 //
c893147d 372
4c87f26e 373 Bool_t result = kTRUE;
374 for (Int_t idata = 0; idata< fNchannels; idata++){
375 if(((GetValue(idata)+roc->GetValue(idata)*c1) <= 6.5535) &&
376 ((GetValue(idata)+roc->GetValue(idata)*c1) >= 0.0))
377 SetValue(idata,GetValue(idata)+roc->GetValue(idata)*c1);
378 else {
379 SetValue(idata,0.0);
380 result = kFALSE;
381 }
382 }
383 return result;
384}
385
386//____________________________________________________________________________________________
387Bool_t AliTRDCalROC::Multiply(const AliTRDCalROC* roc)
388{
389 //
390 // multiply values - per by pad
391 //
c893147d 392
4c87f26e 393 Bool_t result = kTRUE;
394 for (Int_t idata = 0; idata< fNchannels; idata++){
395 if((GetValue(idata)*roc->GetValue(idata)) <= 6.5535)
396 SetValue(idata,GetValue(idata)*roc->GetValue(idata));
397 else {
398 SetValue(idata,0.0);
399 result = kFALSE;
400 }
401 }
402 return result;
403}
404
405//______________________________________________________________________________________________
406Bool_t AliTRDCalROC::Divide(const AliTRDCalROC* roc)
407{
408 //
409 // divide values
410 //
c893147d 411
4c87f26e 412 Bool_t result = kTRUE;
413 Float_t kEpsilon=0.00000000000000001;
414 for (Int_t idata = 0; idata< fNchannels; idata++){
415 if (TMath::Abs(roc->GetValue(idata))>kEpsilon){
416 if((GetValue(idata)/roc->GetValue(idata)) <= 6.5535)
417 SetValue(idata,GetValue(idata)/roc->GetValue(idata));
418 else {
419 SetValue(idata,0.0);
420 result = kFALSE;
421 }
422 }
423 else result = kFALSE;
424 }
425 return result;
426}
37b0cf5e 427//______________________________________________________________________________________________
428Bool_t AliTRDCalROC::Unfold()
429{
430 //
431 // Compute the mean value per pad col
432 // Divide with this value each pad col
433 // This is for the noise study
434 // Return kFALSE if one or more of the pad col was not normalised
435 //
c893147d 436
37b0cf5e 437 Bool_t result = kTRUE;
438 Float_t kEpsilon=0.00000000000000001;
439
440 // calcul the mean value per col
441 for(Int_t icol = 0; icol < fNcols; icol++){
442
443 Float_t mean = 0.0;
444 Float_t nb = 0.0;
445
446 for(Int_t irow = 0; irow < fNrows; irow++){
447 if((GetValue(icol,irow) > 0.06) && (GetValue(icol,irow) < 0.15)){
448 mean += GetValue(icol,irow);
449 nb += 1.0;
450 }
451 }
452
453 if(nb > kEpsilon) {
454
455 mean = mean/nb;
456
457 if(mean > kEpsilon){
458 for(Int_t irow = 0; irow < fNrows; irow++){
459 Float_t value = GetValue(icol,irow);
460 SetValue(icol,irow,(Float_t)(value/mean));
461 }
462 }
463 else result = kFALSE;
464
465 }
466
467 else result = kFALSE;
468
469
470 }
471
472
473 return result;
474}
4c87f26e 475
476//__________________________________________________________________________________
7dcf6933 477TH2F * AliTRDCalROC::MakeHisto2D(Float_t min, Float_t max,Int_t type, Float_t mu)
4c87f26e 478{
479 //
480 // make 2D histo
481 // type -1 = user defined range
482 // 0 = nsigma cut nsigma=min
483 // 1 = delta cut around median delta=min
c893147d 484
4c87f26e 485 Float_t kEpsilonr = 0.005;
486 gStyle->SetPalette(1);
487
488 if (type>=0){
489 if (type==0){
490 // nsigma range
491 Float_t mean = GetMean();
492 Float_t sigma = GetRMS();
493 Float_t nsigma = TMath::Abs(min);
494 sigma *= nsigma;
495 if(sigma < kEpsilonr) sigma = kEpsilonr;
496 min = mean-sigma;
497 max = mean+sigma;
498 }
499 if (type==1){
500 // fixed range
501 Float_t mean = GetMedian();
502 Float_t delta = min;
503 min = mean-delta;
504 max = mean+delta;
505 }
506 if (type==2){
507 Double_t sigma;
508 Float_t mean = GetLTM(&sigma,max);
509 sigma*=min;
510 if(sigma < kEpsilonr) sigma = kEpsilonr;
511 min = mean-sigma;
512 max = mean+sigma;
513
514 }
515 }
516
517 char name[1000];
02f3bfcc 518 snprintf(name,1000,"%s 2D Plane %d Chamber %d",GetTitle(),fPla, fCha);
4c87f26e 519 TH2F * his = new TH2F(name,name,fNrows,0, fNrows, fNcols, 0, fNcols);
520 for (Int_t irow=0; irow<fNrows; irow++){
521 for (Int_t icol=0; icol<fNcols; icol++){
7dcf6933 522 his->Fill(irow+0.5,icol+0.5,GetValue(icol,irow)*mu);
4c87f26e 523 }
524 }
525 his->SetStats(0);
526 his->SetMaximum(max);
527 his->SetMinimum(min);
528 return his;
529}
530
531//_______________________________________________________________________________________
7dcf6933 532TH1F * AliTRDCalROC::MakeHisto1D(Float_t min, Float_t max,Int_t type, Float_t mu)
4c87f26e 533{
534 //
535 // make 1D histo
536 // type -1 = user defined range
537 // 0 = nsigma cut nsigma=min
538 // 1 = delta cut around median delta=min
4c87f26e 539
c893147d 540 Float_t kEpsilonr = 0.005;
4c87f26e 541
542 if (type>=0){
543 if (type==0){
544 // nsigma range
545 Float_t mean = GetMean();
546 Float_t sigma = GetRMS();
547 Float_t nsigma = TMath::Abs(min);
548 sigma *= nsigma;
549 if(sigma < kEpsilonr) sigma = kEpsilonr;
550 min = mean-sigma;
551 max = mean+sigma;
552 }
553 if (type==1){
554 // fixed range
555 Float_t mean = GetMedian();
556 Float_t delta = min;
557 min = mean-delta;
558 max = mean+delta;
559 }
560 if (type==2){
561 //
562 // LTM mean +- nsigma
563 //
564 Double_t sigma;
565 Float_t mean = GetLTM(&sigma,max);
566 sigma*=min;
567 if(sigma < kEpsilonr) sigma = kEpsilonr;
568 min = mean-sigma;
569 max = mean+sigma;
570 }
571 }
572 char name[1000];
02f3bfcc 573 snprintf(name,1000,"%s 1D Plane %d Chamber %d",GetTitle(),fPla, fCha);
4c87f26e 574 TH1F * his = new TH1F(name,name,100, min,max);
575 for (Int_t irow=0; irow<fNrows; irow++){
576 for (Int_t icol=0; icol<fNcols; icol++){
7dcf6933 577 his->Fill(GetValue(icol,irow)*mu);
4c87f26e 578 }
579 }
580 return his;
7754cd1f 581}