1 ///////////////////////////////////////////////////////////////////////////////
3 // Base class for the AliTPCCalibViewer and AliTRDCalibViewer //
4 // used for the calibration monitor //
6 // Authors: Marian Ivanov (Marian.Ivanov@cern.ch) //
7 // Jens Wiechula (Jens.Wiechula@cern.ch) //
8 // Ionut Arsene (iarsene@cern.ch) //
10 ///////////////////////////////////////////////////////////////////////////////
19 //#include <TCanvas.h>
25 #include <THashTable.h>
26 #include <TObjString.h>
27 #include <TLinearFitter.h>
31 #include <TDirectory.h>
32 #include <TFriendElement.h>
34 #include "TTreeStream.h"
35 #include "AliBaseCalibViewer.h"
37 ClassImp(AliBaseCalibViewer)
39 AliBaseCalibViewer::AliBaseCalibViewer()
43 fListOfObjectsToBeDeleted(0),
44 fTreeMustBeDeleted(0),
49 // Default constructor
53 //_____________________________________________________________________________
54 AliBaseCalibViewer::AliBaseCalibViewer(const AliBaseCalibViewer &c)
58 fListOfObjectsToBeDeleted(0),
59 fTreeMustBeDeleted(0),
64 // dummy AliBaseCalibViewer copy constructor
68 fTreeMustBeDeleted = c.fTreeMustBeDeleted;
69 fListOfObjectsToBeDeleted = c.fListOfObjectsToBeDeleted;
70 fAbbreviation = c.fAbbreviation;
71 fAppendString = c.fAppendString;
74 //_____________________________________________________________________________
75 AliBaseCalibViewer::AliBaseCalibViewer(TTree* tree)
79 fListOfObjectsToBeDeleted(0),
80 fTreeMustBeDeleted(0),
85 // Constructor that initializes the calibration viewer
88 fTreeMustBeDeleted = kFALSE;
89 fListOfObjectsToBeDeleted = new TObjArray();
91 fAppendString = ".fElements";
94 //_____________________________________________________________________________
95 AliBaseCalibViewer::AliBaseCalibViewer(const Char_t* fileName, const Char_t* treeName)
99 fListOfObjectsToBeDeleted(0),
100 fTreeMustBeDeleted(0),
106 // Constructor to initialize the calibration viewer
107 // the file 'fileName' contains the tree 'treeName'
109 fFile = new TFile(fileName, "read");
110 fTree = (TTree*) fFile->Get(treeName);
111 fTreeMustBeDeleted = kTRUE;
112 fListOfObjectsToBeDeleted = new TObjArray();
114 fAppendString = ".fElements";
117 //____________________________________________________________________________
118 AliBaseCalibViewer & AliBaseCalibViewer::operator =(const AliBaseCalibViewer & param)
121 // assignment operator - dummy
122 // not yet working!!!
124 if(¶m == this) return *this;
125 TObject::operator=(param);
129 fTreeMustBeDeleted = param.fTreeMustBeDeleted;
130 fListOfObjectsToBeDeleted = param.fListOfObjectsToBeDeleted;
131 fAbbreviation = param.fAbbreviation;
132 fAppendString = param.fAppendString;
136 //_____________________________________________________________________________
137 AliBaseCalibViewer::~AliBaseCalibViewer()
140 // AliBaseCalibViewer destructor
141 // all objects will be deleted, the file will be closed, the pictures will disappear
143 if (fTree && fTreeMustBeDeleted) {
144 fTree->SetCacheSize(0);
152 for (Int_t i = fListOfObjectsToBeDeleted->GetEntriesFast()-1; i >= 0; i--) {
153 delete fListOfObjectsToBeDeleted->At(i);
155 delete fListOfObjectsToBeDeleted;
158 //_____________________________________________________________________________
159 void AliBaseCalibViewer::Delete(Option_t* /*option*/) {
161 // Should be called from AliBaseCalibViewerGUI class only.
162 // If you use Delete() do not call the destructor.
163 // All objects (except those contained in fListOfObjectsToBeDeleted) will be deleted, the file will be closed.
166 if (fTree && fTreeMustBeDeleted) {
167 fTree->SetCacheSize(0);
171 delete fListOfObjectsToBeDeleted;
174 //_____________________________________________________________________________
175 void AliBaseCalibViewer::FormatHistoLabels(TH1 *histo) const {
177 // formats title and axis labels of histo
178 // removes '.fElements'
181 TString replaceString(fAppendString.Data());
182 TString *str = new TString(histo->GetTitle());
183 str->ReplaceAll(replaceString, "");
184 histo->SetTitle(str->Data());
186 if (histo->GetXaxis()) {
187 str = new TString(histo->GetXaxis()->GetTitle());
188 str->ReplaceAll(replaceString, "");
189 histo->GetXaxis()->SetTitle(str->Data());
192 if (histo->GetYaxis()) {
193 str = new TString(histo->GetYaxis()->GetTitle());
194 str->ReplaceAll(replaceString, "");
195 histo->GetYaxis()->SetTitle(str->Data());
198 if (histo->GetZaxis()) {
199 str = new TString(histo->GetZaxis()->GetTitle());
200 str->ReplaceAll(replaceString, "");
201 histo->GetZaxis()->SetTitle(str->Data());
206 //_____________________________________________________________________________
207 TFriendElement* AliBaseCalibViewer::AddReferenceTree(const Char_t* filename, const Char_t* treename, const Char_t* refname){
209 // add a reference tree to the current tree
210 // by default the treename is 'tree' and the reference treename is 'R'
212 TFile *file = new TFile(filename);
213 fListOfObjectsToBeDeleted->Add(file);
214 TTree * tree = (TTree*)file->Get(treename);
215 return AddFriend(tree, refname);
218 //_____________________________________________________________________________
219 TString* AliBaseCalibViewer::Fit(const Char_t* drawCommand, const Char_t* formula, const Char_t* cuts,
220 Double_t & chi2, TVectorD &fitParam, TMatrixD &covMatrix){
222 // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
223 // returns chi2, fitParam and covMatrix
224 // returns TString with fitted formula
227 TString formulaStr(formula);
228 TString drawStr(drawCommand);
229 TString cutStr(cuts);
232 drawStr.ReplaceAll(fAbbreviation, fAppendString);
233 cutStr.ReplaceAll(fAbbreviation, fAppendString);
234 formulaStr.ReplaceAll(fAbbreviation, fAppendString);
236 formulaStr.ReplaceAll("++", fAbbreviation);
237 TObjArray* formulaTokens = formulaStr.Tokenize(fAbbreviation.Data());
238 Int_t dim = formulaTokens->GetEntriesFast();
240 fitParam.ResizeTo(dim);
241 covMatrix.ResizeTo(dim,dim);
243 TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
244 fitter->StoreData(kTRUE);
245 fitter->ClearPoints();
247 Int_t entries = Draw(drawStr.Data(), cutStr.Data(), "goff");
250 delete formulaTokens;
251 return new TString("An ERROR has occured during fitting!");
253 Double_t **values = new Double_t*[dim+1] ;
255 for (Int_t i = 0; i < dim + 1; i++){
257 if (i < dim) centries = fTree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff");
258 else centries = fTree->Draw(drawStr.Data(), cutStr.Data(), "goff");
260 if (entries != centries) {
263 delete formulaTokens;
264 return new TString("An ERROR has occured during fitting!");
266 values[i] = new Double_t[entries];
267 memcpy(values[i], fTree->GetV1(), entries*sizeof(Double_t));
270 // add points to the fitter
271 for (Int_t i = 0; i < entries; i++){
273 for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
274 fitter->AddPoint(x, values[dim][i], 1);
278 fitter->GetParameters(fitParam);
279 fitter->GetCovarianceMatrix(covMatrix);
280 chi2 = fitter->GetChisquare();
282 TString *preturnFormula = new TString(Form("( %e+",fitParam[0])), &returnFormula = *preturnFormula;
284 for (Int_t iparam = 0; iparam < dim; iparam++) {
285 returnFormula.Append(Form("%s*(%e)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
286 if (iparam < dim-1) returnFormula.Append("+");
288 returnFormula.Append(" )");
289 delete formulaTokens;
291 for (Int_t i = 0; i < dim + 1; i++) delete [] values[i];
293 return preturnFormula;
296 //_____________________________________________________________________________
297 Double_t AliBaseCalibViewer::GetLTM(Int_t n, Double_t *array, Double_t *sigma, Double_t fraction){
299 // returns the LTM and sigma
301 Double_t *ddata = new Double_t[n];
302 Double_t mean = 0, lsigma = 0;
304 for (UInt_t i = 0; i < (UInt_t)n; i++) {
305 ddata[nPoints]= array[nPoints];
308 Int_t hh = TMath::Min(TMath::Nint(fraction * nPoints), Int_t(n));
309 AliMathBase::EvaluateUni(nPoints, ddata, mean, lsigma, hh);
310 if (sigma) *sigma = lsigma;
315 //_____________________________________________________________________________
316 Int_t AliBaseCalibViewer::GetBin(Float_t value, Int_t nbins, Double_t binLow, Double_t binUp){
317 // Returns the 'bin' for 'value'
318 // The interval between 'binLow' and 'binUp' is divided into 'nbins' equidistant bins
319 // avoid index out of bounds error: 'if (bin < binLow) bin = binLow' and vice versa
321 GetBin(value) = #frac{nbins - 1}{binUp - binLow} #upoint (value - binLow) +1
325 Int_t bin = TMath::Nint( (Float_t)(value - binLow) / (Float_t)(binUp - binLow) * (nbins-1) ) + 1;
326 // avoid index out of bounds:
327 if (value < binLow) bin = 0;
328 if (value > binUp) bin = nbins + 1;
333 //_____________________________________________________________________________
334 TH1F* AliBaseCalibViewer::SigmaCut(TH1F *histogram, Float_t mean, Float_t sigma, Float_t sigmaMax,
335 Float_t sigmaStep, Bool_t pm) {
337 // Creates a cumulative histogram Begin_Latex S(t, #mu, #sigma) End_Latex, where you can see, how much of the data are inside sigma-intervals around the mean value
338 // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'histogram'
339 // 'mean' and 'sigma' are Begin_Latex #mu End_Latex and Begin_Latex #sigma End_Latex of the distribution in 'histogram', to be specified by the user
340 // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
341 // sigmaStep: the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
342 // pm: Decide weather Begin_Latex t > 0 End_Latex (first case) or Begin_Latex t End_Latex arbitrary (secound case)
343 // The actual work is done on the array.
345 f(x, #mu, #sigma) #Rightarrow S(t, #mu, #sigma) = (#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx + #int_{#mu}^{#mu - t #sigma} f(x, #mu, #sigma) dx) / (#int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx), for t > 0
347 f(x, #mu, #sigma) #Rightarrow S(t, #mu, #sigma) = #int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
353 Float_t sigmaMax = 4;
354 gROOT->SetStyle("Plain");
355 TH1F *distribution = new TH1F("Distribution1", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
357 for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
358 Float_t *ar = distribution->GetArray();
360 TCanvas* macro_example_canvas = new TCanvas("cAliBaseCalibViewer", "", 350, 350);
361 macro_example_canvas->Divide(0,3);
362 TVirtualPad *pad1 = macro_example_canvas->cd(1);
365 distribution->Draw();
366 TVirtualPad *pad2 = macro_example_canvas->cd(2);
370 TH1F *shist = AliTPCCalibViewer::SigmaCut(distribution, mean, sigma, sigmaMax);
371 shist->SetNameTitle("Cumulative","Cumulative S(t, #mu, #sigma)");
373 TVirtualPad *pad3 = macro_example_canvas->cd(3);
376 TH1F *shistPM = AliTPCCalibViewer::SigmaCut(distribution, mean, sigma, sigmaMax, -1, kTRUE);
378 return macro_example_canvas;
383 Float_t *array = histogram->GetArray();
384 Int_t nbins = histogram->GetXaxis()->GetNbins();
385 Float_t binLow = histogram->GetXaxis()->GetXmin();
386 Float_t binUp = histogram->GetXaxis()->GetXmax();
387 return AliBaseCalibViewer::SigmaCut(nbins, array, mean, sigma, nbins, binLow, binUp, sigmaMax, sigmaStep, pm);
390 //_____________________________________________________________________________
391 TH1F* AliBaseCalibViewer::SigmaCut(Int_t n, Float_t *array, Float_t mean, Float_t sigma, Int_t nbins, Float_t binLow, Float_t binUp, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm){
393 // Creates a histogram Begin_Latex S(t, #mu, #sigma) End_Latex, where you can see, how much of the data are inside sigma-intervals around the mean value
394 // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'array', 'n' specifies the length of the array
395 // 'mean' and 'sigma' are Begin_Latex #mu End_Latex and Begin_Latex #sigma End_Latex of the distribution in 'array', to be specified by the user
396 // 'nbins': number of bins, 'binLow': first bin, 'binUp': last bin
397 // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
398 // sigmaStep: the binsize of the generated histogram
399 // Here the actual work is done.
401 if (TMath::Abs(sigma) < 1.e-10) return 0;
402 Float_t binWidth = (binUp-binLow)/(nbins - 1);
403 if (sigmaStep <= 0) sigmaStep = binWidth;
404 Int_t kbins = (Int_t)(sigmaMax * sigma / sigmaStep) + 1; // + 1 due to overflow bin in histograms
405 if (pm) kbins = 2 * (Int_t)(sigmaMax * sigma / sigmaStep) + 1;
406 Float_t kbinLow = !pm ? 0 : -sigmaMax;
407 Float_t kbinUp = sigmaMax;
408 TH1F *hist = new TH1F("sigmaCutHisto","Cumulative; Multiples of #sigma; Fraction of included data", kbins, kbinLow, kbinUp);
409 hist->SetDirectory(0);
412 // calculate normalization
413 Double_t normalization = 0;
414 for (Int_t i = 0; i <= n; i++) {
415 normalization += array[i];
418 // given units: units from given histogram
419 // sigma units: in units of sigma
420 // iDelta: integrate in interval (mean +- iDelta), given units
421 // x: ofset from mean for integration, given units
425 for (Float_t iDelta = 0; iDelta <= sigmaMax * sigma; iDelta += sigmaStep) {
427 Double_t valueP = array[GetBin(mean, nbins, binLow, binUp)];
428 Double_t valueM = array[GetBin(mean-binWidth, nbins, binLow, binUp)];
429 // add bin of mean value only once to the histogram
430 for (Float_t x = binWidth; x <= iDelta; x += binWidth) {
431 valueP += (mean + x <= binUp) ? array[GetBin(mean + x, nbins, binLow, binUp)] : 0;
432 valueM += (mean-binWidth - x >= binLow) ? array[GetBin(mean-binWidth - x, nbins, binLow, binUp)] : 0;
435 if (valueP / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail +++ \n", valueP, normalization);
436 if (valueP / normalization > 100) return hist;
437 if (valueM / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail +++ \n", valueM, normalization);
438 if (valueM / normalization > 100) return hist;
439 valueP = (valueP / normalization);
440 valueM = (valueM / normalization);
442 Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
443 hist->SetBinContent(bin, valueP);
444 bin = GetBin(-iDelta/sigma, kbins, kbinLow, kbinUp);
445 hist->SetBinContent(bin, valueM);
448 Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
449 hist->SetBinContent(bin, valueP + valueM);
452 if (!pm) hist->SetMaximum(1.2);
456 //_____________________________________________________________________________
457 TH1F* AliBaseCalibViewer::SigmaCut(Int_t /*n*/, Double_t */*array*/, Double_t /*mean*/, Double_t /*sigma*/,
458 Int_t /*nbins*/, Double_t */*xbins*/, Double_t /*sigmaMax*/){
460 // SigmaCut for variable binsize
461 // NOT YET IMPLEMENTED !!!
463 printf("SigmaCut with variable binsize, Not yet implemented\n");
468 //_____________________________________________________________________________
469 Int_t AliBaseCalibViewer::DrawHisto1D(const Char_t* drawCommand, const Char_t* sector, const Char_t* cuts,
470 const Char_t *sigmas, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM) const
473 // Easy drawing of data, in principle the same as EasyDraw1D
474 // Difference: A line for the mean / median / LTM is drawn
475 // in 'sigmas' you can specify in which distance to the mean/median/LTM you want to see a line in sigma-units, separated by ';'
476 // example: sigmas = "2; 4; 6;" at Begin_Latex 2 #sigma End_Latex, Begin_Latex 4 #sigma End_Latex and Begin_Latex 6 #sigma End_Latex a line is drawn.
477 // "plotMean", "plotMedian" and "plotLTM": what kind of lines do you want to see?
479 Int_t oldOptStat = gStyle->GetOptStat();
480 gStyle->SetOptStat(0000000);
481 Double_t ltmFraction = 0.8;
483 TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");
484 TVectorF nsigma(sigmasTokens->GetEntriesFast());
485 for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
486 TString str(((TObjString*)sigmasTokens->At(i))->GetString());
487 Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
492 TString drawStr(drawCommand);
493 Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
494 if (dangerousToDraw) {
495 Warning("DrawHisto1D", "The draw string must not contain ':' or '>>'.");
498 drawStr += " >> tempHist";
499 Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts);
500 TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
501 // FIXME is this histogram deleted automatically?
502 Double_t *values = fTree->GetV1(); // value is the array containing 'entries' numbers
504 Double_t mean = TMath::Mean(entries, values);
505 Double_t median = TMath::Median(entries, values);
506 Double_t sigma = TMath::RMS(entries, values);
507 Double_t maxY = htemp->GetMaximum();
509 TLegend * legend = new TLegend(.7,.7, .99, .99, "Statistical information");
510 //fListOfObjectsToBeDeleted->Add(legend);
514 TLine* line = new TLine(mean, 0, mean, maxY);
515 //fListOfObjectsToBeDeleted->Add(line);
516 line->SetLineColor(kRed);
517 line->SetLineWidth(2);
518 line->SetLineStyle(1);
520 legend->AddEntry(line, Form("Mean: %f", mean), "l");
522 for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
523 TLine* linePlusSigma = new TLine(mean + nsigma[i] * sigma, 0, mean + nsigma[i] * sigma, maxY);
524 //fListOfObjectsToBeDeleted->Add(linePlusSigma);
525 linePlusSigma->SetLineColor(kRed);
526 linePlusSigma->SetLineStyle(2 + i);
527 linePlusSigma->Draw();
528 TLine* lineMinusSigma = new TLine(mean - nsigma[i] * sigma, 0, mean - nsigma[i] * sigma, maxY);
529 //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
530 lineMinusSigma->SetLineColor(kRed);
531 lineMinusSigma->SetLineStyle(2 + i);
532 lineMinusSigma->Draw();
533 legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma)), "l");
538 TLine* line = new TLine(median, 0, median, maxY);
539 //fListOfObjectsToBeDeleted->Add(line);
540 line->SetLineColor(kBlue);
541 line->SetLineWidth(2);
542 line->SetLineStyle(1);
544 legend->AddEntry(line, Form("Median: %f", median), "l");
546 for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
547 TLine* linePlusSigma = new TLine(median + nsigma[i] * sigma, 0, median + nsigma[i]*sigma, maxY);
548 //fListOfObjectsToBeDeleted->Add(linePlusSigma);
549 linePlusSigma->SetLineColor(kBlue);
550 linePlusSigma->SetLineStyle(2 + i);
551 linePlusSigma->Draw();
552 TLine* lineMinusSigma = new TLine(median - nsigma[i] * sigma, 0, median - nsigma[i]*sigma, maxY);
553 //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
554 lineMinusSigma->SetLineColor(kBlue);
555 lineMinusSigma->SetLineStyle(2 + i);
556 lineMinusSigma->Draw();
557 legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma)), "l");
563 Double_t ltm = GetLTM(entries, values, <mRms, ltmFraction);
564 TLine* line = new TLine(ltm, 0, ltm, maxY);
565 //fListOfObjectsToBeDeleted->Add(line);
566 line->SetLineColor(kGreen+2);
567 line->SetLineWidth(2);
568 line->SetLineStyle(1);
570 legend->AddEntry(line, Form("LTM: %f", ltm), "l");
572 for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
573 TLine* linePlusSigma = new TLine(ltm + nsigma[i] * ltmRms, 0, ltm + nsigma[i] * ltmRms, maxY);
574 //fListOfObjectsToBeDeleted->Add(linePlusSigma);
575 linePlusSigma->SetLineColor(kGreen+2);
576 linePlusSigma->SetLineStyle(2+i);
577 linePlusSigma->Draw();
579 TLine* lineMinusSigma = new TLine(ltm - nsigma[i] * ltmRms, 0, ltm - nsigma[i] * ltmRms, maxY);
580 //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
581 lineMinusSigma->SetLineColor(kGreen+2);
582 lineMinusSigma->SetLineStyle(2+i);
583 lineMinusSigma->Draw();
584 legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f", (Int_t)(nsigma[i]), (Float_t)(nsigma[i] * ltmRms)), "l");
587 if (!plotMean && !plotMedian && !plotLTM) return -1;
589 gStyle->SetOptStat(oldOptStat);
593 //_____________________________________________________________________________
594 Int_t AliBaseCalibViewer::SigmaCut(const Char_t* drawCommand, const Char_t* sector, const Char_t* cuts,
595 Float_t sigmaMax, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, Bool_t pm,
596 const Char_t *sigmas, Float_t sigmaStep) const {
598 // Creates a histogram, where you can see, how much of the data are inside sigma-intervals
599 // around the mean/median/LTM
600 // with drawCommand, sector and cuts you specify your input data, see EasyDraw
601 // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma)
602 // sigmaStep: the binsize of the generated histogram
603 // plotMean/plotMedian/plotLTM: specifies where to put the center
606 Double_t ltmFraction = 0.8;
608 TString drawStr(drawCommand);
609 Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
610 if (dangerousToDraw) {
611 Warning("SigmaCut", "The draw string must not contain ':' or '>>'.");
614 drawStr += " >> tempHist";
616 Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
617 TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
618 // FIXME is this histogram deleted automatically?
619 Double_t *values = fTree->GetV1(); // value is the array containing 'entries' numbers
621 Double_t mean = TMath::Mean(entries, values);
622 Double_t median = TMath::Median(entries, values);
623 Double_t sigma = TMath::RMS(entries, values);
625 TLegend * legend = new TLegend(.7,.7, .99, .99, "Cumulative");
626 //fListOfObjectsToBeDeleted->Add(legend);
627 TH1F *cutHistoMean = 0;
628 TH1F *cutHistoMedian = 0;
629 TH1F *cutHistoLTM = 0;
631 TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");
632 TVectorF nsigma(sigmasTokens->GetEntriesFast());
633 for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
634 TString str(((TObjString*)sigmasTokens->At(i))->GetString());
635 Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
641 cutHistoMean = SigmaCut(htemp, mean, sigma, sigmaMax, sigmaStep, pm);
643 //fListOfObjectsToBeDeleted->Add(cutHistoMean);
644 cutHistoMean->SetLineColor(kRed);
645 legend->AddEntry(cutHistoMean, "Mean", "l");
646 cutHistoMean->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
647 cutHistoMean->Draw();
648 DrawLines(cutHistoMean, nsigma, legend, kRed, pm);
649 } // if (cutHistoMean)
653 cutHistoMedian = SigmaCut(htemp, median, sigma, sigmaMax, sigmaStep, pm);
654 if (cutHistoMedian) {
655 //fListOfObjectsToBeDeleted->Add(cutHistoMedian);
656 cutHistoMedian->SetLineColor(kBlue);
657 legend->AddEntry(cutHistoMedian, "Median", "l");
658 cutHistoMedian->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
659 if (plotMean && cutHistoMean) cutHistoMedian->Draw("same");
660 else cutHistoMedian->Draw();
661 DrawLines(cutHistoMedian, nsigma, legend, kBlue, pm);
662 } // if (cutHistoMedian)
666 Double_t ltm = GetLTM(entries, values, <mRms, ltmFraction);
667 cutHistoLTM = SigmaCut(htemp, ltm, ltmRms, sigmaMax, sigmaStep, pm);
669 //fListOfObjectsToBeDeleted->Add(cutHistoLTM);
670 cutHistoLTM->SetLineColor(kGreen+2);
671 legend->AddEntry(cutHistoLTM, "LTM", "l");
672 cutHistoLTM->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
673 if ((plotMean && cutHistoMean) || (plotMedian && cutHistoMedian)) cutHistoLTM->Draw("same");
674 else cutHistoLTM->Draw();
675 DrawLines(cutHistoLTM, nsigma, legend, kGreen+2, pm);
678 if (!plotMean && !plotMedian && !plotLTM) return -1;
683 //_____________________________________________________________________________
684 Int_t AliBaseCalibViewer::Integrate(const Char_t* drawCommand, const Char_t* sector, const Char_t* cuts,
685 Float_t /*sigmaMax*/, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM,
686 const Char_t *sigmas, Float_t /*sigmaStep*/) const {
688 // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"
689 // "mean" and "sigma" are Begin_Latex #mu End_Latex and Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
690 // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate
691 // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
692 // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
693 // The actual work is done on the array.
695 f(x, #mu, #sigma) #Rightarrow S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
699 Double_t ltmFraction = 0.8;
701 TString drawStr(drawCommand);
702 Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
703 if (dangerousToDraw) {
704 Warning("Integrate", "The draw string must not contain ':' or '>>'.");
707 drawStr += " >> tempHist";
709 Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
710 TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
711 TGraph *integralGraphMean = 0;
712 TGraph *integralGraphMedian = 0;
713 TGraph *integralGraphLTM = 0;
714 Double_t *values = fTree->GetV1(); // value is the array containing 'entries' numbers
715 Int_t *index = new Int_t[entries];
716 Float_t *xarray = new Float_t[entries];
717 Float_t *yarray = new Float_t[entries];
718 TMath::Sort(entries, values, index, kFALSE);
720 Double_t mean = TMath::Mean(entries, values);
721 Double_t median = TMath::Median(entries, values);
722 Double_t sigma = TMath::RMS(entries, values);
724 // parse sigmas string
725 TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");
726 TVectorF nsigma(sigmasTokens->GetEntriesFast());
727 for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
728 TString str(((TObjString*)sigmasTokens->At(i))->GetString());
729 Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
733 TLegend * legend = new TLegend(.7,.7, .99, .99, "Integrated histogram");
734 //fListOfObjectsToBeDeleted->Add(legend);
737 for (Int_t i = 0; i < entries; i++) {
738 xarray[i] = (values[index[i]] - mean) / sigma;
739 yarray[i] = float(i) / float(entries);
741 integralGraphMean = new TGraph(entries, xarray, yarray);
742 if (integralGraphMean) {
743 //fListOfObjectsToBeDeleted->Add(integralGraphMean);
744 integralGraphMean->SetLineColor(kRed);
745 legend->AddEntry(integralGraphMean, "Mean", "l");
746 integralGraphMean->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
747 integralGraphMean->Draw("alu");
748 DrawLines(integralGraphMean, nsigma, legend, kRed, kTRUE);
752 for (Int_t i = 0; i < entries; i++) {
753 xarray[i] = (values[index[i]] - median) / sigma;
754 yarray[i] = float(i) / float(entries);
756 integralGraphMedian = new TGraph(entries, xarray, yarray);
757 if (integralGraphMedian) {
758 //fListOfObjectsToBeDeleted->Add(integralGraphMedian);
759 integralGraphMedian->SetLineColor(kBlue);
760 legend->AddEntry(integralGraphMedian, "Median", "l");
761 integralGraphMedian->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
762 if (plotMean && integralGraphMean) integralGraphMedian->Draw("samelu");
763 else integralGraphMedian->Draw("alu");
764 DrawLines(integralGraphMedian, nsigma, legend, kBlue, kTRUE);
769 Double_t ltm = GetLTM(entries, values, <mRms, ltmFraction);
770 for (Int_t i = 0; i < entries; i++) {
771 xarray[i] = (values[index[i]] - ltm) / ltmRms;
772 yarray[i] = float(i) / float(entries);
774 integralGraphLTM = new TGraph(entries, xarray, yarray);
775 if (integralGraphLTM) {
776 //fListOfObjectsToBeDeleted->Add(integralGraphLTM);
777 integralGraphLTM->SetLineColor(kGreen+2);
778 legend->AddEntry(integralGraphLTM, "LTM", "l");
779 integralGraphLTM->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
780 if ((plotMean && integralGraphMean) || (plotMedian && integralGraphMedian)) integralGraphLTM->Draw("samelu");
781 else integralGraphLTM->Draw("alu");
782 DrawLines(integralGraphLTM, nsigma, legend, kGreen+2, kTRUE);
788 if (!plotMean && !plotMedian && !plotLTM) return -1;
793 //_____________________________________________________________________________
794 TH1F* AliBaseCalibViewer::Integrate(TH1F *histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
796 // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"
797 // "mean" and "sigma" are Begin_Latex #mu End_Latex and Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
798 // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate
799 // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
800 // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
801 // The actual work is done on the array.
803 f(x, #mu, #sigma) #Rightarrow S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
809 Float_t sigmaMax = 4;
810 gROOT->SetStyle("Plain");
811 TH1F *distribution = new TH1F("Distribution2", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
813 for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
814 Float_t *ar = distribution->GetArray();
816 TCanvas* macro_example_canvas = new TCanvas("macro_example_canvas_Integrate", "", 350, 350);
817 macro_example_canvas->Divide(0,2);
818 TVirtualPad *pad1 = macro_example_canvas->cd(1);
821 distribution->Draw();
822 TVirtualPad *pad2 = macro_example_canvas->cd(2);
825 TH1F *shist = AliTPCCalibViewer::Integrate(distribution, mean, sigma, sigmaMax);
826 shist->SetNameTitle("Cumulative","Cumulative S(t, #mu, #sigma)");
829 return macro_example_canvas_Integrate;
835 Float_t *array = histogram->GetArray();
836 Int_t nbins = histogram->GetXaxis()->GetNbins();
837 Float_t binLow = histogram->GetXaxis()->GetXmin();
838 Float_t binUp = histogram->GetXaxis()->GetXmax();
839 return Integrate(nbins, array, nbins, binLow, binUp, mean, sigma, sigmaMax, sigmaStep);
842 //_____________________________________________________________________________
843 TH1F* AliBaseCalibViewer::Integrate(Int_t n, Float_t *array, Int_t nbins, Float_t binLow, Float_t binUp,
844 Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
845 // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"
846 // "mean" and "sigma" are Begin_Latex #mu End_Latex and Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
847 // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate
848 // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
849 // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
850 // Here the actual work is done.
852 Bool_t givenUnits = kTRUE;
853 if (TMath::Abs(sigma) < 1.e-10 && TMath::Abs(sigmaMax) < 1.e-10) givenUnits = kFALSE;
856 sigmaMax = (binUp - binLow) / 2.;
859 Float_t binWidth = (binUp-binLow)/(nbins - 1);
860 if (sigmaStep <= 0) sigmaStep = binWidth;
861 Int_t kbins = (Int_t)(sigmaMax * sigma / sigmaStep) + 1; // + 1 due to overflow bin in histograms
862 Float_t kbinLow = givenUnits ? binLow : -sigmaMax;
863 Float_t kbinUp = givenUnits ? binUp : sigmaMax;
865 if (givenUnits) hist = new TH1F("integratedHisto","Integrated Histogram; Given x; Fraction of included data", kbins, kbinLow, kbinUp);
866 if (!givenUnits) hist = new TH1F("integratedHisto","Integrated Histogram; Multiples of #sigma; Fraction of included data", kbins, kbinLow, kbinUp);
867 hist->SetDirectory(0);
870 // calculate normalization
871 // printf("calculating normalization, integrating from bin 1 to %i \n", n);
872 Double_t normalization = 0;
873 for (Int_t i = 1; i <= n; i++) {
874 normalization += array[i];
876 // printf("normalization: %f \n", normalization);
878 // given units: units from given histogram
879 // sigma units: in units of sigma
880 // iDelta: integrate in interval (mean +- iDelta), given units
881 // x: ofset from mean for integration, given units
885 for (Float_t iDelta = mean - sigmaMax * sigma; iDelta <= mean + sigmaMax * sigma; iDelta += sigmaStep) {
888 for (Float_t x = mean - sigmaMax * sigma; x <= iDelta; x += binWidth) {
889 value += (x <= binUp && x >= binLow) ? array[GetBin(x, nbins, binLow, binUp)] : 0;
891 if (value / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail +++ \n", value, normalization);
892 if (value / normalization > 100) return hist;
893 Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
894 // printf("first integration bin: %i, last integration bin: %i \n", GetBin(mean - sigmaMax * sigma, nbins, binLow, binUp), GetBin(iDelta, nbins, binLow, binUp));
895 // printf("value: %f, normalization: %f, normalized value: %f, iDelta: %f, Bin: %i \n", value, normalization, value/normalization, iDelta, bin);
896 value = (value / normalization);
897 hist->SetBinContent(bin, value);
902 //_____________________________________________________________________________
903 void AliBaseCalibViewer::DrawLines(TH1F *histogram, TVectorF nsigma, TLegend *legend, Int_t color, Bool_t pm) const {
905 // Private function for SigmaCut(...) and Integrate(...)
906 // Draws lines into the given histogram, specified by "nsigma", the lines are addeed to the legend
909 // start to draw the lines, loop over requested sigmas
910 for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
912 Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
913 TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
914 //fListOfObjectsToBeDeleted->Add(lineUp);
915 lineUp->SetLineColor(color);
916 lineUp->SetLineStyle(2 + i);
918 TLine* lineLeft = new TLine(nsigma[i], histogram->GetBinContent(bin), 0, histogram->GetBinContent(bin));
919 //fListOfObjectsToBeDeleted->Add(lineLeft);
920 lineLeft->SetLineColor(color);
921 lineLeft->SetLineStyle(2 + i);
923 legend->AddEntry(lineLeft, Form("Fraction(%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
926 Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
927 TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
928 //fListOfObjectsToBeDeleted->Add(lineUp1);
929 lineUp1->SetLineColor(color);
930 lineUp1->SetLineStyle(2 + i);
932 TLine* lineLeft1 = new TLine(nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
933 //fListOfObjectsToBeDeleted->Add(lineLeft1);
934 lineLeft1->SetLineColor(color);
935 lineLeft1->SetLineStyle(2 + i);
937 legend->AddEntry(lineLeft1, Form("Fraction(+%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
938 bin = histogram->GetXaxis()->FindBin(-nsigma[i]);
939 TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], histogram->GetBinContent(bin));
940 //fListOfObjectsToBeDeleted->Add(lineUp2);
941 lineUp2->SetLineColor(color);
942 lineUp2->SetLineStyle(2 + i);
944 TLine* lineLeft2 = new TLine(-nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
945 //fListOfObjectsToBeDeleted->Add(lineLeft2);
946 lineLeft2->SetLineColor(color);
947 lineLeft2->SetLineStyle(2 + i);
949 legend->AddEntry(lineLeft2, Form("Fraction(-%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
951 } // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)
954 //_____________________________________________________________________________
955 void AliBaseCalibViewer::DrawLines(TGraph *graph, TVectorF nsigma, TLegend *legend, Int_t color, Bool_t pm) const {
957 // Private function for SigmaCut(...) and Integrate(...)
958 // Draws lines into the given histogram, specified by "nsigma", the lines are addeed to the legend
961 // start to draw the lines, loop over requested sigmas
962 for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
964 TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
965 //fListOfObjectsToBeDeleted->Add(lineUp);
966 lineUp->SetLineColor(color);
967 lineUp->SetLineStyle(2 + i);
969 TLine* lineLeft = new TLine(nsigma[i], graph->Eval(nsigma[i]), 0, graph->Eval(nsigma[i]));
970 //fListOfObjectsToBeDeleted->Add(lineLeft);
971 lineLeft->SetLineColor(color);
972 lineLeft->SetLineStyle(2 + i);
974 legend->AddEntry(lineLeft, Form("Fraction(%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i])), "l");
977 TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
978 //fListOfObjectsToBeDeleted->Add(lineUp1);
979 lineUp1->SetLineColor(color);
980 lineUp1->SetLineStyle(2 + i);
982 TLine* lineLeft1 = new TLine(nsigma[i], graph->Eval(nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(nsigma[i]));
983 //fListOfObjectsToBeDeleted->Add(lineLeft1);
984 lineLeft1->SetLineColor(color);
985 lineLeft1->SetLineStyle(2 + i);
987 legend->AddEntry(lineLeft1, Form("Fraction(+%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i])), "l");
988 TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], graph->Eval(-nsigma[i]));
989 //fListOfObjectsToBeDeleted->Add(lineUp2);
990 lineUp2->SetLineColor(color);
991 lineUp2->SetLineStyle(2 + i);
993 TLine* lineLeft2 = new TLine(-nsigma[i], graph->Eval(-nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(-nsigma[i]));
994 //fListOfObjectsToBeDeleted->Add(lineLeft2);
995 lineLeft2->SetLineColor(color);
996 lineLeft2->SetLineStyle(2 + i);
998 legend->AddEntry(lineLeft2, Form("Fraction(-%f #sigma) = %f",nsigma[i], graph->Eval(-nsigma[i])), "l");
1000 } // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)