void AccDistr_EH3_AD2() { Double_t WidthOfBin = 86164.09/24.00;// 1 sidereal day = 86164.09 seconds Double_t StartTime = 1324678393.80705; Double_t EndTime = 1385769600.00000; Int_t NumOfBin = (EndTime - StartTime)/WidthOfBin; Int_t ADNo = 1; Int_t AD_index = 5; const Int_t NumOfRun = 409; const Int_t NumOfInfoLine = 14038; Double_t Tc = 200.0*1e-6; Double_t MultiEffT[8]={1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; ///////////Acc Per Run information////////////////////////////////// Int_t RunID[NumOfRun]; Double_t AccPerRun[NumOfRun]; Double_t E_AccPerRun[NumOfRun]; std::string s; ifstream is("Acc_EH3_AD2.txt"); for(int idx=0;idx<NumOfRun;idx++) { getline(is,s); { string subs; stringstream ss; subs = s.substr(0,15); ss<<subs; ss>>RunID[idx]; } { string subs; stringstream ss; subs = s.substr(127,16); ss<<subs; ss>>AccPerRun[idx]; ////coutidx<<" "<<RunID[idx]<<" "<<AccPerRun[idx]<<endl; } { string subs; stringstream ss; subs = s.substr(143,16); ss<<subs; ss>>E_AccPerRun[idx]; ////coutidx<<" "<<RunID[idx]<<" "<<AccPerRun[idx]<<endl; } } is.close(); ///////////////////////////////////////////////////////////////////////// //////////////////single's rate information//////////////////////////// Double_t AccInEachLine[NumOfInfoLine]={0.0}; Double_t E_AccInEachLine[NumOfInfoLine]={0.0}; Double_t RunIDInEachLine[NumOfInfoLine]; Int_t BinIDInEachLine[NumOfInfoLine]; Double_t WeightInEachLine[NumOfInfoLine]={0.0}; Double_t AccTotalInEachRun[NumOfInfoLine] = {0.0}; Double_t E_AccTotalInEachRun[NumOfInfoLine] = {0.0}; std::string s2; ifstream is2("../InfoInEachBin_EH3.txt"); for(int idx=0;idx<NumOfInfoLine;idx++) { Double_t FullTime,Veto[4],LiveTime,Rs=0.0,Rmu=0.0; Double_t NSgUp[4],NSgLow[4],NNet[4]; getline(is2,s2); { string subs; stringstream ss; subs = s2.substr(0,110);//92->95,FullTime ss<<subs; ss>>RunIDInEachLine[idx]; ////coutRunIDInEachLine[idx]<<endl; if(RunIDInEachLine[idx]==0) { RunIDInEachLine[idx] = RunIDInEachLine[idx-1]; } } { string subs; stringstream ss; subs = s2.substr(27,5);//92->95,FullTime ss<<subs; ss>>BinIDInEachLine[idx]; } { string subs; stringstream ss; subs = s2.substr(92,16);//92->95,FullTime ss<<subs; ss>>FullTime; } { string subs; stringstream ss; subs = s2.substr(124,16);//92->95,FullTime ss<<subs; ss>>Veto[0]; } { string subs; stringstream ss; subs = s2.substr(141,16);//92->95,FullTime ss<<subs; ss>>Veto[1]; } { string subs; stringstream ss; subs = s2.substr(157,16);//92->95,FullTime ss<<subs; ss>>Veto[2]; } { string subs; stringstream ss; subs = s2.substr(173,16);//92->95,FullTime ss<<subs; ss>>Veto[3]; } for(int i=0;i<4;i++) { string subs; stringstream ss; subs = s2.substr(189+16*i,16); ss<<subs; ss>>NSgUp[i]; } for(int i=0;i<4;i++) { string subs; stringstream ss; subs = s2.substr(253+16*i,16); ss<<subs; ss>>NSgLow[i]; } for(int i=0;i<4;i++) { string subs; stringstream ss; subs = s2.substr(317+16*i,16); ss<<subs; ss>>NNet[i]; } LiveTime = FullTime- Veto[ADNo]; if(LiveTime >0.0001) { Rs = 0.5*(NSgUp[ADNo]+NSgLow[ADNo])/LiveTime; Rmu = NNet[ADNo]/FullTime; } if(LiveTime >0.0001){ WeightInEachLine[idx] = FullTime*RSS_DYB(Rs,Rmu,Tc); } for(int idxRun=0;idxRun<NumOfRun;idxRun++) { if(RunIDInEachLine[idx]==RunID[idxRun]) { AccTotalInEachRun[idx] = AccPerRun[idxRun]; E_AccTotalInEachRun[idx] = E_AccPerRun[idxRun]; break; } } ////coutidx<<" "<<RunIDInEachLine[idx]<<" "<<NSgUp[ADNo]<<" "<<NSgLow[ADNo]<<" "<<FullTime<<" "<<Veto[ADNo]<<" "<<LiveTime<<" "<<WeightInEachLine[idx]<<" "<<AccTotalInEachRun[idx]<<endl; } is2.close(); Double_t TotalWeight = 0.0; Int_t idxLine=0; Int_t BeginLine=0; while(idxLine<NumOfInfoLine) { TotalWeight += WeightInEachLine[idxLine]; if(RunIDInEachLine[idxLine+1]!=RunIDInEachLine[idxLine]) { for(int i=BeginLine;i<=idxLine;i++) { AccInEachLine[i] = WeightInEachLine[i]*AccTotalInEachRun[i]/TotalWeight; E_AccInEachLine[i] = sqrt(WeightInEachLine[i]/TotalWeight)*E_AccTotalInEachRun[i]; ////coutE_AccTotalInEachRun[i]<<" "<<WeightInEachLine[i]<<" "<<TotalWeight<<endl; } TotalWeight = 0.0; BeginLine = idxLine+1; } idxLine ++; } ///////////////////NumOfAccInEachBin/////////////////////////////////////////////////// Double_t *NumOfAccInEachBin = new Double_t[NumOfBin]; memset(NumOfAccInEachBin,0.0,sizeof(NumOfAccInEachBin)); Double_t *E2_NumOfAccInEachBin = new Double_t[NumOfBin]; memset(E2_NumOfAccInEachBin,0.0,sizeof(NumOfAccInEachBin)); for(int idx=0;idx<NumOfInfoLine;idx++) { NumOfAccInEachBin[BinIDInEachLine[idx]] += AccInEachLine[idx]; E2_NumOfAccInEachBin[BinIDInEachLine[idx]] += pow(E_AccInEachLine[idx],2.0); } //save the to .root file Double_t NumOfAccInBin; TFile *NumOfAccF = new TFile("NumOfAccInEachBin_EH3_AD2.root","recreate"); TTree *NumOfAccTree = new TTree("NumOfAccInEachBin","NumOfAccInEachBin"); NumOfAccTree->Branch("NumOfAccInEachBin",&NumOfAccInBin,"NumOfAccInBin/D"); for(int i=0;i<NumOfBin;i++) { ////cout<<" ["<<i<<"]: "<<NumOfAccInEachBin[i]; NumOfAccInBin = NumOfAccInEachBin[i]/MultiEffT[AD_index]; NumOfAccTree->Fill(); } NumOfAccTree->Write(); NumOfAccF->Close(); Double_t E2_NumOfAccInBin; TFile *E2_NumOfAccF = new TFile("E2_NumOfAccInEachBin_EH3_AD2.root","recreate"); TTree *E2_NumOfAccTree = new TTree("E2_NumOfAccInEachBin","E2_NumOfAccInEachBin"); E2_NumOfAccTree->Branch("E2_NumOfAccInEachBin",&E2_NumOfAccInBin,"E2_NumOfAccInBin/D"); for(int i=0;i<NumOfBin;i++) { E2_NumOfAccInBin = E2_NumOfAccInEachBin[i]/pow(MultiEffT[AD_index],2.0); E2_NumOfAccTree->Fill(); //coutNumOfAccInEachBin[i]/MultiEffT[AD_index]<<" "<<sqrt(E2_NumOfAccInBin)<<endl; } E2_NumOfAccTree->Write(); E2_NumOfAccF->Close(); //live time in one day Int_t NumOf24Acc[24]={0}; Double_t NumOfAccInOneDay; TFile *NumOfAccF2 = new TFile("NumOfAccInOneDay_EH3_AD2.root","recreate"); TTree *NumOfAccTree2 = new TTree("NumOfAccInOneDay","NumOfAccInOneDay"); NumOfAccTree2->Branch("NumOfAccInOneDay",&NumOfAccInOneDay,"NumOfAccInOneDay/D"); for(int i =0;i<NumOfBin;i++) { Int_t N24 = i%24; Point_y[N24] += NumOfAccInEachBin[i]/MultiEffT[AD_index]; NumOf24Acc[N24] ++; } Double_t TotalAcc = 0; for(int i=0;i<24;i++) { TotalAcc += Point_y[i]; NumOfAccInOneDay = Point_y[i]; NumOfAccTree2->Fill(); } NumOfAccTree2->Write(); NumOfAccF2->Close(); cout<<"Total NumOfAcc in EH3_AD2: "<<TotalAcc<<endl; ////////////////////////////E2 Int_t E2_NumOf24Acc[24]={0}; Double_t E2_OneDay[24] = {0.0}; Double_t E2_NumOfAccInOneDay; TFile *E2_NumOfAccF2 = new TFile("E2_NumOfAccInOneDay_EH3_AD2.root","recreate"); TTree *E2_NumOfAccTree2 = new TTree("E2_NumOfAccInOneDay","E2_NumOfAccInOneDay"); E2_NumOfAccTree2->Branch("E2_NumOfAccInOneDay",&E2_NumOfAccInOneDay,"E2_NumOfAccInOneDay/D"); for(int i =0;i<NumOfBin;i++) { Int_t N24 = i%24; E2_NumOf24Acc[N24] ++; E2_OneDay[N24] += E2_NumOfAccInEachBin[i]/pow(MultiEffT[AD_index],2.0); } for(int i=0;i<24;i++) { E2_NumOfAccInOneDay = E2_OneDay[i]; E2_NumOfAccTree2->Fill(); //coutPoint_y[i]<<" "<<sqrt(E2_OneDay[i])<<" "<<E2_NumOf24Acc[i]<<endl; } E2_NumOfAccTree2->Write(); E2_NumOfAccF2->Close(); TCanvas *C_Acc2 = new TCanvas("C_Acc2","C_Acc2",0,0,1000,800); TF1 *t_Acc = new TF1("NumOfAcc", GetY, 0, 24, 0); t_Acc->SetTitle("NumOfAcc - EH3_AD2"); t_Acc->GetHistogram()->GetXaxis()->SetTitle("sidereal time(1 bin = 86164.00/24.00 seconds)"); t_Acc->GetHistogram()->GetYaxis()->SetTitle("NumOfAcc in one bin/seconds"); t_Acc->Draw(); C_Acc2->SaveAs("NumOfAccInOneDay_EH3_AD2.gif"); ///////////////////////////////////////////////////////////////////////////////////////////// }
//do a line..deal with any mud markup language tags (ignore them) String filter_line(char* line) { char chr; int len = strlen(line); String retval(100); int new_posn; const char* str; const char* ending_txt = NULL; if (strncasecmp("syntax", line, 6) == 0) { retval += "<font color=\"#ff0000\">"; ending_txt = "</font>"; } else if (strncasecmp("example", line, 7) == 0) { retval += "<font color=\"#00ff00\">"; ending_txt = "</font>"; } else if (strncasecmp("see also", line, 8) == 0) { retval += "<font color=\"#0000ff\"> See Also: "; // Assume the rest of the line is comma seperated names. char tmp[200]; int strt = 8; if (line[8] == ':') strt = 9; my_sstream ss(line + strt); while (ss) { ss >> tmp; if (strlen(tmp) == 0) { break; } if (tmp[strlen(tmp) - 1] == ',') { tmp[strlen(tmp) - 1] = 0; } //Now, lets find the links. String fname(200); fname = tmp; fname.Strip(); fname.Append("_1"); ifstream is(fname); if (is) { retval += "<A href = \"#"; retval += fname; retval += "\">"; retval += tmp; retval += "</a> "; } else { fname.Prepend("IMM_"); ifstream is2(fname); if (is2) { retval += "<A href = \"#"; retval += fname; retval += "\">"; retval += tmp; retval += "</a> "; }//if else { if (strcasecmp(tmp, "asave") == 0) { retval += " <A href = \"#IMM_write_zone_1\">asave</a>"; } else { retval += " "; retval += tmp; retval += " "; } }//else }//else }//while we can get the next token retval += "</font>"; return retval; }
// // main // int main(int argc, char **argv) { dbCursor<LibM> cursor1; if (argc != 4) { printf("Usage: %s target lib_root_path full:0|1\n", argv[0]); return 0; } lib_root_path = argv[2]; bFull = argv[3][0] == '1'; TCHAR *filename = argv[1]; TCHAR filename2[MAX_PATH]; int i; DeleteFile("LibM.fdb"); if (!db.open("LibM")) { return 0; } printf("Built on " __DATE__ " " __TIME__ "\nInit db..."); build_instruction_db(); printf("total:%d OK\n", lib_count); //getch(); InitializeCriticalSection(&cs); while (1) { printf("Loading...\n"); if (!LoadFileR(filename, &stMapFile)) { cerr << "ERROR FILE!" << endl; //continue; break; } if (!IsPEFile(stMapFile.ImageBase)) { cerr << "NOT PE FILE!" << endl; UnLoadFile(&stMapFile); //continue; break; } pOH = GetOptionalHeader(stMapFile.ImageBase); pImageNtH = GetNtHeaders(stMapFile.ImageBase); int c0 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, count = 0; // read target function info wsprintf(filename2, _T("%s.txt"), filename); ifstream is2(filename2, ios::binary); if (!is2.is_open()) { break; } int tt; db.detach(); f_info.clear(); while (!is2.eof()) { int startEA, endEA; is2.read((char *)&startEA, sizeof(int)); is2.read((char *)&endEA, sizeof(int)); if (endEA - startEA < MIN_INS_LENGTH) { continue; } function_info fi; memset(&fi, 0, sizeof(fi)); fi.startEA = startEA; fi.len = endEA - startEA; fi.lib_name[0] = 0; f_info.push_back(fi); } total_function_len = f_info.size(); //sort(f_info.begin(), f_info.end(), myfunction); printf("OK\n"); tt = GetTickCount(); int thread_num = bMultiThread ? THREAD_NUM : 1; if (show_dump) { thread_num = 1; } HANDLE *thread = new HANDLE[thread_num]; for (i = 0; i < thread_num; i++) { thread[i] = (HANDLE)_beginthreadex(NULL, 0, bFull ? (&MatchThreadForFull) : (&MatchThread), (void *)i, 0, NULL); } WaitForMultipleObjects(thread_num, thread, TRUE, INFINITE); tt = GetTickCount() - tt; delete[] thread; db.attach(); UnLoadFile(&stMapFile); if (cursor1.select() > 0) { do { delete (ControlFlowGraph *)cursor1->cfg; } while (cursor1.next()); } DWORD s = 0; DWORD _r0 = 0, _r1 = 0, _r34 = 0, _r2 = 0, _r5 = 0, _fn = 0; for (i = 0; i < THREAD_NUM; i++) { s += instruction_count[i]; _r0 += r0[i]; _r1 += r1[i]; _r2 += r2[i]; _r34 += r34[i]; _r5 += r5[i]; _fn += fn[i]; } printf("#fn:%d #instr:%d t:%d found:%d\n", _fn, s, tt, found_c); printf("r1:\t%d\n", _r1); printf("r2:\t%d\n", _r2); printf("r34:\t%d\n", _r34); printf("r5:\t%d\n", _r5); printf("r0:\t%d\n", _r0); printf("fulled:\t%d\n", fulled); printf("check failed:\t%d\n", check_failed); printf("END.\n"); break; } DeleteCriticalSection(&cs); db.detach(); #ifdef _DEBUG cout << "Press ENTER..." << endl; char c; c = cin.get(); #endif return 0; }
int main() { std::cout.setf(std::ios_base::boolalpha); std::polynomial<double> P({0.0, 1.0, 2.0, 3.0}); std::cout << "P = " << P << std::endl; std::cout << "+P = " << +P << std::endl; std::cout << "-P = " << -P << std::endl; std::cout << "P = " << P << std::endl; std::cout << "degree(P) = " << P.degree() << std::endl; std::polynomial<double> Q({2.0, 1.0}); std::cout << "Q = " << Q << std::endl; std::cout << "degree(Q) = " << Q.degree() << std::endl; std::cout << "P + Q = " << P + Q << std::endl; std::cout << "P - Q = " << P - Q << std::endl; std::cout << "P * Q = " << P * Q << std::endl; std::cout << "P / Q = " << P / Q << std::endl; std::cout << "P % Q = " << P % Q << std::endl; double b = 5.0; std::cout << "b = " << b << std::endl; std::cout << "P + b = " << P + b << std::endl; std::cout << "P - b = " << P - b << std::endl; std::cout << "P * b = " << P * b << std::endl; std::cout << "P / b = " << P / b << std::endl; std::cout << "P % b = " << P % b << std::endl; double a = 2.0; std::cout << "a = " << a << std::endl; std::cout << "a + Q = " << a + Q << std::endl; std::cout << "a - Q = " << a - Q << std::endl; std::cout << "a * Q = " << a * Q << std::endl; std::cout << "a / Q = " << a / Q << std::endl; std::cout << "a % Q = " << a % Q << std::endl; std::polynomial<double> B;// = b; B = b; std::cout << "B = " << B << std::endl; std::cout << "P % B = " << P % B << std::endl; Q = {0.0, -2.0, 4.0, -6.0, 8.0, -12.0}; std::cout << "Q = " << Q << std::endl; std::polynomial<double> P2; P2 = P; std::cout << "P2 = " << P2 << std::endl; std::cout << "P2 == P = " << (P2 == P) << std::endl; for (int i = 0; i <= 100; ++i) { double x = i * 0.1; std::cout << "P(" << x << ") = " << P(x) << std::endl; } std::polynomial<std::complex<double>> CP({std::complex<double>(0.0, -1.0), std::complex<double>(1.0, -2.0), std::complex<double>(2.0, -3.0), std::complex<double>(3.0, -4.0)}); std::cout << "CP = " << CP << std::endl; std::cout << "CP * CP = " << CP * CP << std::endl; std::polynomial<int> IP({0, 1, 2, 3}); std::cout << "IP = " << IP << std::endl; std::cout << "IP * IP = " << IP * IP << std::endl; std::array<double, 10> arr; P.eval(1.0, arr); std::cout << "P(" << 1.0 << ") ="; for (int i = 0; i < arr.size(); ++i) std::cout << " " << arr[i]; std::cout << std::endl; P.eval(1.0, arr.begin(), arr.end()); std::cout << "P(" << 1.0 << ") ="; for (auto iarr = arr.cbegin(); iarr != arr.cend(); ++iarr) std::cout << " " << *iarr; std::cout << std::endl; std::istringstream is("(-2.0, -1.0, 0.0)"); std::polynomial<double> R; is >> R; std::cout << "R = " << R << std::endl; std::istringstream is2("(5.0)"); std::polynomial<double> S; is2 >> S; std::cout << "S = " << S << std::endl; std::istringstream is3("42.0"); std::polynomial<double> T; is3 >> T; std::cout << "T = " << T << std::endl; std::polynomial<double> u({1.0, 3.0, 3.0, 1.0}); std::polynomial<double> v({1.0, 1.0}); std::polynomial<double> q, r; std::divmod(u, v, q, r); std::cout << "u = " << u << std::endl; std::cout << "v = " << v << std::endl; std::cout << "q = " << q << std::endl; std::cout << "r = " << r << std::endl; std::polynomial<double> u1({1.0, -3.0, 3.0, -1.0}); std::polynomial<double> v1({1.0, -2.0, 1.0}); std::polynomial<double> q1, r1; std::divmod(u1, v1, q1, r1); std::cout << "u1 = " << u1 << std::endl; std::cout << "v1 = " << v1 << std::endl; std::cout << "q1 = " << q1 << std::endl; std::cout << "r1 = " << r1 << std::endl; std::polynomial<double> u2({1.0, 1.0}); std::polynomial<double> v2({1.0, 3.0, 3.0, 1.0}); std::polynomial<double> q2, r2; std::divmod(u2, v2, q2, r2); std::cout << "u2 = " << u2 << std::endl; std::cout << "v2 = " << v2 << std::endl; std::cout << "q2 = " << q2 << std::endl; std::cout << "r2 = " << r2 << std::endl; std::polynomial<double> u3({1.0, 0.0, 0.0, 1.0}); std::polynomial<double> v3({1.0, 3.0, 3.0, 1.0}); std::polynomial<double> q3, r3; std::divmod(u3, v3, q3, r3); std::cout << "u3 = " << u3 << std::endl; std::cout << "v3 = " << v3 << std::endl; std::cout << "q3 = " << q3 << std::endl; std::cout << "r3 = " << r3 << std::endl; std::cout << "P = " << P << std::endl; std::cout << "P' = " << P.derivative() << std::endl; std::cout << "I = " << P.integral(42.0) << std::endl; //std::array<double, 5> aaa{{1.1, 2.2, 3.3, 4.4, 5.5}}; //std::cout << "aaa = " << std::polynomial_eval(aaa, 3.13149) << std::endl; }
int main() { //--- // Test ossiIpt::operator>> //--- std::string is1(" ( 0, 1 )"); std::string is2(" (2,3)"); std::string is3(" ( 4, 5 )"); std::string is4_5_6_7(" ( 6, 7 )(8, 9) ( 10, 11 ) ( 12, 13) 9876"); ossimIpt ip1; ossimIpt ip2; ossimIpt ip3; ossimIpt ip4; ossimIpt ip5; ossimIpt ip6; ossimIpt ip7; ip1.toPoint(is1); ip2.toPoint(is2); ip3.toPoint(is3); int i; std::istringstream istr(is4_5_6_7); istr >> ip4 >> ip5 >> ip6 >> ip7 >> i; //--- // Test ossiDpt::operator>> //--- std::string ds1(" ( 0.0, 1.1 )"); std::string ds2(" (2.2,3.3)"); std::string ds3(" ( 4.4, 5.5 )"); std::string ds4_5_6_7(" ( 6.6, 7.7 )(8.8, 9.9) ( 10.0, 11.0 ) ( 12.0, 13.0) 9876.12345678"); std::string ds8("12 20"); ossimDpt dp1; ossimDpt dp2; ossimDpt dp3; ossimDpt dp4; ossimDpt dp5; ossimDpt dp6; ossimDpt dp7; ossimDpt dp8; dp1.toPoint(ds1); dp2.toPoint(ds2); dp3.toPoint(ds3); double d; std::istringstream istr2(ds4_5_6_7); istr2 >> dp4 >> dp5 >> dp6 >> dp7 >> d; dp8.toPoint(ds8); // Test an invalid string "12 20" //--- // Test ossiDpt3d //--- std::string ds3d1 = " ( 0.0, 1.1, 2.2 )"; std::string ds3d2 = "(1.0,2.0,3.0)"; ossimDpt3d dp3d1; ossimDpt3d dp3d2; dp3d1.toPoint(ds3d1); dp3d2.toPoint(ds3d2); //--- // Test ossiGpt::operator>> //--- std::string gs1("(0.0,0.0,0.0,WGE)"); std::string gs2("(1.1,2.2,3.3,NAR-C)"); std::string gs3(" (4.4,5.5,6.6,NAS-C )"); std::string gs4_5_6_7(" (4.4,5.5,6.6,NAS-C )( 10.0, 10.0 ,5.0, TOY-C ) (17, -89, 50.0, xxx) (28.2, -44.5, 10000.0, NAS-B) 12345.6789"); ossimGpt gp1; ossimGpt gp2; ossimGpt gp3; ossimGpt gp4; ossimGpt gp5; ossimGpt gp6; ossimGpt gp7; double d2; gp1.toPoint(gs1); gp2.toPoint(gs2); gp3.toPoint(gs3); std::istringstream istr4(gs4_5_6_7); istr4 >> gp4 >> gp5 >> gp6 >> gp7 >> d2; //--- // Test ossimEcefPoint toString and toPoint methods. //--- std::string es1("(1.0,2.0,3.0)"); ossimEcefPoint ep1; ep1.toPoint(es1); std::string es2 = ep1.toString(10).string(); //--- // Test ossimEcefPoint toString and toPoint methods. //--- ossimEcefVector ev1; ev1.toPoint(es1); std::string es3 = ev1.toString(10).string(); std::cout << "\nis1: " << is1 << "\nip1: " << ip1 << "\nis2: " << is2 << "\nip2: " << ip2 << "\nis3: " << is3 << "\nip3: " << ip3 << "\nis4_5_6_7: " << is4_5_6_7 << "\nip4: " << ip4 << "\nip5: " << ip5 << "\nip6: " << ip6 << "\nip7: " << ip7 << "\ni: " << i << "\n\n\nds1: " << ds1 << "\ndp1: " << dp1 << "\nds2: " << ds2 << "\ndp2: " << dp2 << "\nds3: " << ds3 << "\ndp3: " << dp3 << "\nds4_5_6_7: " << ds4_5_6_7 << "\ndp4: " << dp4 << "\ndp5: " << dp5 << "\ndp6: " << dp6 << "\ndp7: " << dp7 << "\nds8: " << ds8 << "\ndp8: " << dp8 << "\nd: " << d << "\n\nds3d1: " << ds3d1 << "\nds3d2: " << ds3d2 << "\ndp3d1: " << dp3d1 << "\ndp3d2: " << dp3d2 << "\n\n\ngs1: " << gs1 << "\ngp1: " << gp1 << "\ngs2: " << gs2 << "\ngp2: " << gp2 << "\ngs3: " << gs3 << "\ngp3: " << gp3 << "\ngs4_5_6_7: " << gs4_5_6_7 << "\ngp4: " << gp4 << "\ngp5: " << gp5 << "\ngp6: " << gp6 << "\ngp7: " << gp7 << "\nd2: " << d2 << "\n\n\nes1: " << es1 << "\nep1: " << ep1 << "\nes2: " << es2 << "\nev1: " << ev1 << "\nes3: " << es3 << std::endl; return 0; }
double lfun (double x[], int np) { GaussLegendreRule(&xI, &wI, com.npoints); for (Gtree=0; Gtree<com.nGtree;Gtree++){ for(igrid=0; igrid<K*K; igrid++){ if(Gtree==G1a||Gtree==G1b || Gtree==G1c){ if(com.model==M2SIM3s){ xtoy(Q,Q1,DIM*DIM); complexroots (Q1, Pt1, t[0]*t[1]); if(0 && igrid==0){ debug_print(Pt1, DIM); } xtoy(Q,Q1,DIM*DIM); complexroots (Q1, Pt0, t[0]-t[0]*t[1]); if(0 && igrid==0){ debug_print(Pt0, DIM); } if (Gtree==G1c){ } else if (Gtree==G1b){ } else if (Gtree==G1a){ } } b[1]=t[0]*t[1]; b[0]=t[0]-t[0]*t[1]; } else if(Gtree==G2a || Gtree==G2b || Gtree==G2c || Gtree== G3a || Gtree ==G3b || Gtree ==G3c){ if(com.model==M2SIM3s){ xtoy(Q,Q1,DIM*DIM); complexroots (Q1, Pt0, tau1-t[1]); /*ÔÚpt.cÀֱ½ÓÓÿÉÄÜÓÐÎÊÌâ*/ xtoy(Q,Q1,DIM*DIM); complexroots (Q1, Pt1, t[1]); //printf("\n In lfun after end2\n"); for(s=1;s<=8;s++){ //problems about s-1 if (Gtree==G2c || Gtree==G3c) f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2(11,Pt0,DIM) + Pt1[s,2,DIM]*is2(20,Pt0,DIM)) * 2/theta1 + (Pt1[get_ij(s,7,DIM)] * is2(17,Pt0,DIM) + Pt1[s,8,DIM]*is2(14,Pt0,DIM)) * 2/theta2; else if (Gtree==G2b || Gtree==G3b) f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2(10,Pt0,DIM) + Pt1[s,3,DIM]*is2(19,Pt0,DIM))*2/theta1 +(Pt1[get_ij(s,6,DIM)] * is2(16,Pt0,DIM) + Pt1[s,8,DIM]*is2(13,Pt0,DIM))*2/theta2; else if (Gtree==G2a || Gtree==G3a) f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2( 9,Pt0,DIM) + Pt1[s,5,DIM]*is2(18,Pt0,DIM)) *2/theta1 +(Pt1[get_ij(s,4,DIM)] * is2(15,Pt0,DIM) + Pt1[s,8,DIM]*is2(12,Pt0,DIM)) *2/theta2; } } if(Gtree==G2a || Gtree==G2b || Gtree==G2c){ g=exp(-t[0]); for(s=1;s<=8;s++) f[s-1]*=g; b[1]=t[1]; b[0]= 2.0*t[0]/theta5+tau1-t[1]; } else if(Gtree==G3a || Gtree==G3b || Gtree==G3c){ g= exp(-t[0])*exp(-2*(tau0-tau1)/theta5); for(s=1;s<=8;s++) f[s-1]*=g; b[1]=t[1]; b[0]= 2.0*t[0]/theta4+tau0-t[1]; } } else{ //printf("\n In lfun after end3\n"); if (Gtree==G4a || Gtree==G4b || Gtree==G4c){ g= exp(-3*t[0]*t[1]-t[0]*(1-t[1])); b[1]=theta5/2*t[0]*t[1]+tau1; b[0]=theta5/2*t[0]+tau1-b[1]; }//above checked else if (Gtree==G5a || Gtree==G5b || Gtree==G5c){ g=exp(-2*t[1]-t[0])* exp(-2.0*(tau0-tau1)/theta5); b[1]=theta5/2*t[1]+tau1; b[0]=theta4/2*t[0]+tau0-b[1]; } else if (Gtree==G6a || Gtree==G6b || Gtree==G6c){ g= exp(-3*t[1]-t[0])* exp(-6.0*(tau0-tau1)/theta5); b[1]=theta4/2*t[1]+tau0; b[0]=theta4/2*(t[0]+t[1])+tau0-b[1]; } /*end else if*/ if(com.model==M2SIM3s){ xtoy(Q,Q1,DIM*DIM); complexroots (Q1, Ptau1, tau1); for(s=1;s<=8;s++) f[s-1]= is3(s,Ptau1,DIM)*g; } } for(s=0;s<8;s++) { com.wwprior[Gtree][igrid][s] = f[s] * com.wwprior[Gtree][igrid][0]; //printf("wwprior[%d][%d][%d]=%f ",Gtree, igrid,s,com.wwprior[Gtree][igrid][s]); } //printf("\n"); //hhh if(!com.fix_locusrate) p0124Fromb0b1(com.bp0124[Gtree]+igrid*5, b); else { com.bp0124[Gtree][igrid*2+0] = b[0]; com.bp0124[Gtree][igrid*2+1] = b[1]; } /* printf("%-3s %5d y%9.5f%9.5f t%9.5f%9.5f b%9.5f%9.5f\n", GtreeStr[Gtree], igrid+1, y[0],y[1], t[0],t[1], b[0],b[1]); */ //printf("In lfun after igrid loop !!!!\n"); zzz } for(locus=0; locus<com.ndata; locus++) { Li = lnpD_locus(locus,com.state[locus]); if(com.model==M1DiscreteBeta) com.pDclass[itau1*com.ndata+locus] = Li; else lnL += Li; n = com.Nij + locus*5; if(debug) printf("%d\t%d\t%d\t%d\t%d\t%.6f\n", n[0],n[1],n[2],n[3],n[4], Li); } return(-lnL); }
int main() { /* Basic examples. */ { // This is the problem case that we are trying to solve: memory leaks { { Base *base = new Base(1); assert(base->i == 1); assert(Base::count == 1); // You will forget to do this one day. //delete base; } // Then base is lost forever: it went out of scope. // And the memory has leaked. assert(Base::count == 1); // Hack up the count for future tests. Base::count = 0; } // This is how unique_ptr solves it. { { std::unique_ptr<Base> base(new Base(1)); assert(base->i == 1); assert(Base::count == 1); } // Destructor called automatically! No memory leaks! assert(Base::count == 0); } } /* More commonly, the pointers are inside another container. * When the parent goes out of scope, it just calls the * child constructors in the same way. */ { { std::vector<std::unique_ptr<Base>> bases; bases.push_back(std::unique_ptr<Base>(new Base(0))); bases.push_back(std::unique_ptr<Base>(new Base(1))); assert(bases[0]->i == 0); assert(bases[1]->i == 1); assert(Base::count == 2); } assert(Base::count == 0); } // Convert to raw pointer. { std::unique_ptr<int> p(new int); int *raw; // Not possible, the cast operator is not defined. //raw = p; // But can be done explicitly with .get(). raw = p.get(); } // Copy constructor is deleted. // This imposes uniqueness of ownership. { std::unique_ptr<int> p(new int); // ERROR. //std::unique_ptr<int> p2(p); // Consequence: for loops over vectors must use references &. // http://stackoverflow.com/questions/20292682/iterating-through-vectorunique-ptrmytype-using-c11-for-loops { std::vector<std::unique_ptr<int>> is; for (auto& i : is) {} // ERROR. //for (auto i : is) {} } // Must move glvalues. // http://stackoverflow.com/questions/3283778/why-can-i-not-push-back-a-unique-ptr-into-a-vector { std::vector<std::unique_ptr<int>> is; std::unique_ptr<int> i(new int(1)); is.push_back(std::move(i)); assert(*is.back() == 1); // Without intermediate variable, we don't need to move, because it is an rvalue, // and unique_ptr does have an move constructor. is.push_back(std::unique_ptr<int>(new int(2))); assert(*is.back() == 2); // Analogously, must move containers instead of copy. { std::vector<std::unique_ptr<int>> is; is.push_back(std::unique_ptr<int>(new int(1))); //std::vector<std::unique_ptr<int>> is2(is); std::vector<std::unique_ptr<int>> is2(std::move(is)); assert(*(is2.front()) == 1); } } } // # reset { /* With no arguments, explicitly destroy the pointer. Equivalent to `delete`. * * http://stackoverflow.com/questions/25609457/unique-ptr-explicit-delete */ { std::unique_ptr<Base> p(new Base(1)); assert(Base::count == 1); p.reset(); assert(Base::count == 0); } // Reset with an argument // does what you would expect: release and replace, // just like assignment. { std::unique_ptr<Base> p(new Base(1)); assert(p->i == 1); assert(Base::count == 1); /* Nope. TODO why? * * - https://stackoverflow.com/questions/48104034/why-cant-i-assign-to-unique-ptr-of-type-uint-8 * - https://stackoverflow.com/questions/34882140/why-cant-a-pointer-be-automatically-converted-into-a-unique-ptr-when-returning */ //p = new Base(2); // Yup. p = std::unique_ptr<Base>(new Base(2)); assert(p->i == 2); assert(Base::count == 1); // Yup, same. Less verbose, I like it. p.reset(new Base(3)); assert(p->i == 3); assert(Base::count == 1); } assert(Base::count == 0); } /* # unique_ptr function arguments * * - transferring ownership TODO examples * - use raw pointers on the interface, and convert it to unique_ptr inside callee * - if you already have an unique_ptr, release() it * - this allows you to not tie down to a specific smart pointer on the function interface * - use unique_ptr on interface and move on caller. * - Advantage: unique_ptr on interface documents ownership transfer, * and prevents callee from passing non new pointer to it by mistake. * - TODO for not transferring ownership: * - `const & std::unique_ptr<T>` * - `get()`. Simple and efficient. But how to use it for containers like `vector<std::unique_ptr>`? * - `T&` on function, `*t` on caller. Looks good! * * - http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-functionhttp://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function * - http://stackoverflow.com/questions/11277249/how-to-pass-stdunique-ptr-around */ { } // Return unique_ptr from function. { { Base::count = 0; auto base = return_unique_ptr(); assert(Base::count == 1); assert(base->i == 1); } assert(Base::count == 0); } #if __cplusplus >= 201402L // # make_unique // Does new and puts it inside unique_ptr. Very convenient. { { Base::count = 0; auto base = std::make_unique<Base>(1); assert(Base::count == 1); assert(base->i == 1); } assert(Base::count == 0); } #endif /* Array inside unique_ptr * https://stackoverflow.com/questions/16711697/is-there-any-use-for-unique-ptr-with-array */ { size_t n = 2; Base::count = 0; { std::unique_ptr<Base[]> array(new Base[n]); array[0].i = 1; array[0].i = 1; assert(Base::count == 2); } assert(Base::count == 0); } }