2 .L $ALICE_ROOT/STAT/Macros/TStatToolkitTest.C+
11 #include "TStatToolkit.h"
12 #include "TTreeStream.h"
14 #include "TGraphErrors.h"
18 TObjArray arrayFit(3);
19 Int_t kMarkers[10]={25,24,20,21};
20 Int_t kColors[10]={1,2,4,3};
21 const char * names[10] = {"LTM","LThisto","LTMhisto1"};
22 const char * distNames[10] = {"Gauss","Gauss+flat","Gauss(m)+0.2*Gauss(m+5#sigma)","Gauss(m)+0.3*Gauss(m+3#sigma)"};
25 void TestLTM(Int_t nevents=10000){
27 // Goal test and benchamerk numerical stability and precission of the LTM method
28 // Binned data and not binned data:
29 // Here we assume that binwidth<sigma
30 // Distribution types examples used in test:
33 // 1 - gauss+uniform background
34 // 2 - gauss+second gaus 5 sigma away
36 Int_t npointsMax=1000;
37 TTreeSRedirector * pcstream = new TTreeSRedirector("TStatToolkit_LTMtest.root","recreate");
39 TVectorD values(npointsMax);
43 TVectorD meanLTMHisto(6);
44 TVectorD sigmaLTMHisto(6);
45 TVectorD meanLTMHisto1(6);
46 TVectorD sigmaLTMHisto1(6);
47 TVectorD paramLTM(10);
49 for (Int_t iltm=0; iltm<6; iltm++) vecLTM[iltm]=0.99999*(0.5+iltm/10.);
51 for (Int_t ievent = 0; ievent<nevents; ievent++){
52 if (ievent%1000==0) printf("%d\n",ievent);
53 Int_t distType=Int_t(gRandom->Rndm()*4);
54 Int_t npoints= 50+(npointsMax-50)*gRandom->Rndm();
55 Double_t mean = 0.5+(gRandom->Rndm()-0.5)*0.2;
56 Double_t sigma = mean*0.2*(1+2*gRandom->Rndm());
57 TH1F histo("histo","histo",100,-0.5,1.5);
59 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
61 if (distType==0) value=gRandom->Gaus(mean,sigma); // gauss
63 if (gRandom->Rndm()>0.2) { // gauss + background
64 value=gRandom->Gaus(mean,sigma);
66 value=mean+(gRandom->Rndm()-0.5)*2;
70 if (gRandom->Rndm()>0.2) { // gauss + second gaus 5 sigma away
71 value=gRandom->Gaus(mean,sigma);
73 value=gRandom->Gaus(mean+5*sigma,sigma);
77 if (gRandom->Rndm()>0.3) { // gauss + second gaus 4 sigma away
78 value=gRandom->Gaus(mean,sigma);
80 value=gRandom->Gaus(mean+5*sigma,sigma);
87 histo.Fit(&fg,"QN","QN");
88 Double_t meanG = fg.GetParameter(1);
89 Double_t rmsG = fg.GetParameter(2);
90 Double_t meanA = TMath::Mean(npoints,values.GetMatrixArray());
91 Double_t rmsA = TMath::Mean(npoints,values.GetMatrixArray());
92 Double_t meanH = histo.GetMean();
93 Double_t rmsH = histo.GetRMS();
95 for (Int_t iltm=0; iltm<6; iltm++){
97 Double_t meanV,sigmaV=0;
98 TStatToolkit::EvaluateUni(npoints,values.GetMatrixArray(), meanV,sigmaV, vecLTM[iltm]*npoints);
100 sigmaLTM[iltm]=sigmaV;
102 TStatToolkit::LTM(&histo, ¶mLTM, vecLTM[iltm]);
103 meanLTMHisto1[iltm]=paramLTM[1];
104 sigmaLTMHisto1[iltm]=paramLTM[2];
106 TStatToolkit::LTMHisto(&histo, paramLTM, vecLTM[iltm]);
107 meanLTMHisto[iltm]=paramLTM[1];
108 sigmaLTMHisto[iltm]=paramLTM[2];
112 "npoints="<<npoints<<
113 "distType="<<distType<<
116 // "Standart" statistic output
124 "vecLTM.="<<&vecLTM<<
125 "meanLTM.="<<&meanLTM<<
126 "meanLTMHisto.="<<&meanLTMHisto<<
127 "meanLTMHisto1.="<<&meanLTMHisto1<<
129 "sigmaLTM.="<<&sigmaLTM<<
130 "sigmaLTMHisto.="<<&sigmaLTMHisto<<
131 "sigmaLTMHisto1.="<<&sigmaLTMHisto1<<
138 TFile *fltm = TFile::Open("TStatToolkit_LTMtest.root","update");
139 TTree * tree = (TTree*)fltm->Get("ltm");
140 tree->SetMarkerSize(0.5);
141 tree->SetMarkerStyle(25);
143 // 1. Get numerical error estimate of the LTM method for gaussian distribution
145 TH2 * hisLTMTrunc[10]={0};
146 TH1 * hisResol[10]={0};
147 TGraphErrors * grSigma[10]={0};
148 TCanvas *canvasLTM= new TCanvas("canvasLTM","canvasLTM",800,700);
149 canvasLTM->Divide(2,2);
150 for (Int_t itype=0; itype<4; itype++){
151 canvasLTM->cd(itype+1);
152 TCut cutType=TString::Format("distType==0%d",itype).Data();
153 tree->Draw("sqrt(npoints-2)*(meanLTM.fElements-mean)/sigma:vecLTM.fElements>>hisLTM(6,0.45,1.05,100,-10,10)",cutType+"npoints>50&&sigma>0.1","colzgof");
154 hisLTMTrunc[0]= (TH2*)(tree->GetHistogram()->Clone());
155 tree->Draw("sqrt(npoints-2)*(meanLTMHisto.fElements-mean)/sigma:vecLTM.fElements>>hisLTMHisto(6,0.45,1.05,100,-10,10)",cutType+"npoints>50&&sigma>0.1","colzgoff");
156 hisLTMTrunc[1]= (TH2*)tree->GetHistogram()->Clone();
157 tree->Draw("sqrt(npoints-2)*(meanLTMHisto1.fElements-mean)/sigma:vecLTM.fElements>>hisLTMHist1(6,0.45,1.05,100,-10,10)",cutType+"npoints>50&&sigma>0.1","colzgoff");
158 hisLTMTrunc[2]= (TH2*)tree->GetHistogram()->Clone();
159 TLegend * legend = new TLegend(0.5,0.7,0.9,0.9,distNames[itype]);
160 legend->SetBorderSize(0);
161 for (Int_t ihis=0; ihis<3; ihis++){
162 // MakeStat1D(TH2 * his, Int_t deltaBin, Double_t fraction, Int_t returnType, Int_t markerStyle, Int_t markerColor);
163 grSigma[ihis]=TStatToolkit::MakeStat1D( hisLTMTrunc[ihis],0,0.99,5,kMarkers[ihis],kColors[ihis]);
164 grSigma[ihis]->GetXaxis()->SetTitle("LTM fraction");
165 grSigma[ihis]->GetYaxis()->SetTitle("#sigma_{meas}/sigma_{gauss}");
166 if (ihis==0)grSigma[ihis]->Draw("alp");
167 if (ihis>0)grSigma[ihis]->Draw("lp");
168 legend->AddEntry(grSigma[ihis],names[ihis],"p");
172 canvasLTM->SaveAs("robustStatLTM_Performance.pdf");