//初始化群体 void PSO::Initialize() { if(!Particle) { return; } static int kk=(unsigned)time(NULL); srand((unsigned)time(NULL)+kk++); GBestIndex=0; //初始化所有粒子的个体 for(int i=0;i<PNum;i++) { for(int j=0;j<Particle[i].Dim;j++) { Particle[i].X[j]=rand()/(double)RAND_MAX*(Xup[j]-Xdown[j])+Xdown[j];//随机初始化坐标 Particle[i].XBest[j]=Particle[i].X[j]; Particle[i].V[j]=rand()/(double)RAND_MAX*Vmax[j]-Vmax[j]/2;//随机初始化速度 } Particle[i].Fit=GetFit(Particle[i]);//计算每个微粒适合度 Particle[i].FitBest=Particle[i].Fit;//计算最优适合度值 if(Particle[i].Fit>Particle[GBestIndex].Fit) { //如果这个鸟的适合度大于群体的最大适合度的话,记录下查找群体的最优微粒 GBestIndex=i; } } }
//计算群体各个微粒的适合度 void PSO::CalFit() { if(!Particle) { return; } for(int i=0;i<PNum;i++) { Particle[i].Fit=GetFit(Particle[i]); } }
void fit(const char *run="428211_429133_5s", int key=1, int bmin=10, bool draw=true, bool pa=false) { int minentries=1000; gSystem->Exec( Form("mkdir -p %s/SEN%03d",run,key/128) ); gSystem->Exec( Form("mkdir -p %s/SEN%03d",run,key/128) ); int state = findstate(key); printf("state %d\n",state); // data TString inname = Form("%s/adc/HI_KEY%05d.root",run,key); TString outname = Form("HI_KEY%05d",key); TFile *file = new TFile( inname.Data() ); cout << inname.Data() << endl; TH1D *out = (TH1D*) file->Get("out"); double xfit_min=out->GetBinLowEdge(bmin); double xfit_max=122.5; int bmax = out->GetXaxis()->FindBin(xfit_max); int entries = out->Integral(bmin,bmax); if(entries<minentries) { cout << "not enough entries: "; cout << entries << endl; return; } // fit TCanvas *main = new TCanvas("main","main"); int pkt=0; if(pa) pkt = (key%(8*4*12*64))/(4*12*64); TF1 *fitH = GetFit( Form("%s/SEN%03d/%s.dat",run,key/128,outname.Data()) ,pkt,xfit_min); out->Fit(fitH,"MELIR","",xfit_min,xfit_max); TF1 *MIPH1 = GetMIP(fitH,1,kCyan-3); TF1 *MIPH2 = GetMIP(fitH,2,kGreen-3); TF1 *MIPH3 = GetMIP(fitH,3,kOrange-3); TF1 *MIPH4 = GetMIP(fitH,4,kMagenta-3); TF1 *BGR = GetBGR(fitH,xfit_min); double amp = fitH->GetParameter(0); double eamp= fitH->GetParError(0); double lda = fitH->GetParameter(1); double elda= fitH->GetParError(1); double sg1 = fitH->GetParameter(2); double esg1= fitH->GetParError(2); double fr2 = fitH->GetParameter(3); double efr2= fitH->GetParError(3); double fr3 = fitH->GetParameter(4); double efr3= fitH->GetParError(4); double fr4 = fitH->GetParameter(5); double efr4= fitH->GetParError(5); double fr1 = 1 - fr2 - fr3 - fr4; double ncs = fitH->GetChisquare()/fitH->GetNDF(); double ba = fitH->GetParameter(6); double eba= fitH->GetParError(6); double bsl = fitH->GetParameter(7); double ebsl= fitH->GetParError(7); // saving fit ofstream outfit; outfit.open( Form("%s/SEN%03d/%s.dat",run,key/128,outname.Data()) ); outfit << amp << " " << eamp << endl; outfit << lda << " " << elda << endl; outfit << sg1 << " " << esg1 << endl; outfit << fr2 << " " << efr2 << endl; outfit << fr3 << " " << efr3 << endl; outfit << fr4 << " " << efr4 << endl; outfit << ba << " " << eba << endl; outfit << bsl << " " << ebsl << endl; outfit << ncs << endl; outfit.close(); cout << "Parameters saved to "; cout << outname.Data() << ".dat" << endl; // draw if(!draw) return; gStyle->SetOptFit(0); gStyle->SetOptStat(0); out->Draw("HE"); double ymax = out->GetBinContent( out->FindBin(xfit_min) )*1.5; out->GetYaxis()->SetRangeUser(0.5,ymax); out->GetXaxis()->SetRangeUser(-5,125); out->Sumw2(); out->SetLineColor(kBlack); out->SetMarkerStyle(20); out->SetTitle(""); out->GetXaxis()->SetTitle("ADC-PED (a.u.)"); BGR->Draw("SAME"); MIPH1->Draw("SAME"); MIPH2->Draw("SAME"); MIPH3->Draw("SAME"); MIPH4->Draw("SAME"); fitH->SetRange(xfit_min,xfit_max); fitH->Draw("SAME"); TLatex *text = new TLatex(); text->DrawLatex(0, (1.03*(ymax)), inname.Data() ); text->DrawLatex(30, (0.83*(ymax)), Form("Entries %d",entries) ); text->DrawLatex(30, (0.73*(ymax)), Form("State %d",state) ); text->DrawLatex(30, (0.53*(ymax)), Form("#lambda %.1f #pm %.1f",lda,elda) ); text->DrawLatex(30, (0.43*(ymax)), Form("#sigma %.1f #pm %.1f",sg1,esg1) ); text->SetTextColor(kRed-3); text->DrawLatex(30, (0.63*(ymax)), Form("#Chi^{2} / NDF %.2f",ncs) ); text->SetTextColor(kBlue-3); text->DrawLatex(75, (0.73*(ymax)), Form("#Alpha %.0f #pm %.0f",fitH->GetParameter(0),fitH->GetParError(0)) ); text->SetTextColor(kCyan-3); text->DrawLatex(75, (0.63*(ymax)), Form("f_{1} %.2f",fr1) ); text->SetTextColor(kGreen-3); text->DrawLatex(75, (0.53*(ymax)), Form("f_{2} %.2f #pm %.2f",fr2,efr2) ); text->SetTextColor(kOrange-3); text->DrawLatex(75, (0.43*(ymax)), Form("f_{3} %.2f #pm %.2f",fr3,efr3) ); text->SetTextColor(kMagenta-3); text->DrawLatex(75, (0.33*(ymax)), Form("f_{4} %.2f #pm %.2f",fr4,efr4) ); text->SetTextColor(kGray); text->DrawLatex(75, (0.83*(ymax)), Form("b %.2f",bsl) ); text->SetTextColor(kBlack); text->SetTextSize(0.035); text->SetTextColor( kRed-3 ); text->DrawLatex(30, (0.93*(ymax)), "e^{bx} + #Alpha ( f_{1} L_{1}(x) + f_{2} L_{2}(x) + f_{3} L_{3}(x) + f_{4} L_{4}(x))"); main->SaveAs( Form("%s/SEN%03d/%s.eps",run,key/128,outname.Data()), "eps" ); return; }
void fitLOW(const char *run="19020_19035", int arm=0, int lyr=2, int sen=21, int mpd=0, bool draw=true, //int old_xfit_min=12, int old_xfit_max=82, int old_xfit_min=8, int old_xfit_max=62, double xfit_min=2.5, double xfit_max=18.5) { gStyle->SetOptFit(0); gStyle->SetOptStat(0); TString inname = Form("%s/adc/LO_ARM%d_LYR%d_S%d_M%d.root",run,arm,lyr,sen,mpd); TString prename = Form("HI_ARM%d_LYR%d_S%d_M%d_%d_%d",arm,lyr,sen,mpd,old_xfit_min,old_xfit_max); TString outname = Form("LO_ARM%d_LYR%d_S%d_M%d_%.0f_%.0f",arm,lyr,sen,mpd,xfit_min,xfit_max); TFile *file = new TFile( inname.Data() ); cout << inname.Data() << endl; cout << prename.Data() << endl; cout << outname.Data() << endl; TF1 *fitH = GetFit(run,prename.Data(),outname.Data()); if(!fitH) return; if(lda<1) return; // data TH1D *out = (TH1D*) file->Get("out"); double ymax = out->GetMaximum(); int entries = out->Integral(xfit_min,xfit_max); out->Sumw2(); out->SetLineColor(kBlack); out->SetMarkerStyle(20); out->SetTitle(""); // fit TCanvas *main = new TCanvas("main","main"); main->SetLogy(1); out->Draw("HE"); out->GetYaxis()->SetRangeUser(0.5,ymax); out->GetXaxis()->SetRangeUser(-4.5,50.5); out->Fit(fitH,"QENIML","",xfit_min,xfit_max); TF1 *MIPH1 = GetMIP(fitH,1,kCyan-3); TF1 *MIPH2 = GetMIP(fitH,2,kGreen-3); TF1 *MIPH3 = GetMIP(fitH,3,kOrange-3); TF1 *MIPH4 = GetMIP(fitH,4,kMagenta-3); double sLDA = fitH->GetParameter(0); double eLDA= fitH->GetParError(0); double sSGM = fitH->GetParameter(1); double eSGM = fitH->GetParError(1); double ncs = fitH->GetChisquare()/fitH->GetNDF(); double ldalow = lda*sLDA; double sgmlow = sgm*sSGM; double eldalow = TMath::Abs(ldalow)*TMath::Sqrt( elda/lda*elda/lda + eLDA/sLDA*eLDA/sLDA ); double esgmlow = TMath::Abs(sgmlow)*TMath::Sqrt( esgm/sgm*esgm/sgm + eSGM/sSGM*eSGM/sSGM ); ofstream outfit; outfit.open( Form("%s/fit/%s.dat",run,outname.Data()) ); outfit << sLDA << " " << eLDA << endl; outfit << sSGM << " " << eSGM << endl; outfit << ldalow << endl; outfit << sgmlow << endl; outfit << ncs << endl; outfit.close(); // draw if(!draw) return; MIPH1->Draw("SAME"); MIPH2->Draw("SAME"); MIPH3->Draw("SAME"); MIPH4->Draw("SAME"); fitH->SetRange(xfit_min,xfit_max); fitH->Draw("SAME"); TLatex *text = new TLatex(); text->DrawLatex(1, TMath::Exp(1.03*TMath::Log(ymax)), inname ); text->DrawLatex(20, TMath::Exp(0.93*TMath::Log(ymax)), Form("Entries %d",entries) ); text->SetTextColor(kRed-3); text->DrawLatex(44, TMath::Exp(0.83*TMath::Log(ymax)), Form("#chi^{2} / NDF %.2f",ncs) ); text->DrawLatex(20, TMath::Exp(0.83*TMath::Log(ymax)), Form("S_{#lambda} %.3f #pm %.3f",sLDA,eLDA) ); text->DrawLatex(20, TMath::Exp(0.73*TMath::Log(ymax)), Form("S_{#sigma} %.3f #pm %.3f",sSGM,eSGM) ); text->SetTextColor(kBlue-3); text->DrawLatex(20, TMath::Exp(0.60*TMath::Log(ymax)), Form("#lambda^{H} %.1f #pm %.1f #rightarrow #lambda^{L} %.1f #pm %.1f",lda,elda,ldalow,eldalow) ); text->DrawLatex(20, TMath::Exp(0.50*TMath::Log(ymax)), Form("#sigma^{H} %.1f #pm %.1f #rightarrow #sigma^{L} %.1f #pm %.1f",sgm,esgm,sgmlow,esgmlow) ); text->SetTextColor(kBlack); text->SetTextSize(0.04); main->SaveAs( Form("%s/fiteps/%s.png",run,outname.Data()), "png" ); return; }