// Create a quadrilateral window function of ones (rest is zero) given an Nside, and write to outfile: // Shape is hard-coded below: void CreateWindow(std::string outfile, int nside) { Healpix_Map<MAP_PRECISION> Map; std::vector<pointing> vertex; std::vector<int> pixlist; rangeset<int> pixset; int npixels, i, count; // Here you define the vertices of the square in radians (theta, phi): // top-left top-right bottom-left bottom-right //pointing v1(1.483529, 1.483529), v2(1.483529, 1.658063), v3(1.658063, 1.483529), v4(1.658063, 1.658063); // 10deg x 10deg. pointing v1(1.540566, 1.540566), v2(1.540566, 1.601026), v3(1.601026, 1.540566), v4(1.601026, 1.601026); // Square of 12deg^2: // Create window function in Healpix map: vertex.push_back(v1); vertex.push_back(v2); vertex.push_back(v4); vertex.push_back(v3); Map.SetNside(nside, RING); Map.query_polygon(vertex, pixset); pixset.toVector(pixlist); npixels = 12*Map.Nside()*Map.Nside(); for (i=0; i<npixels; i++) Map[i]=0.0; count = 0; for (i=0; i<pixlist.size(); i++) { Map[pixlist[i]]=1.0; count++; } // Print total area: printf("Total area (deg^2): %g\n",((double)count)/((double)npixels)*41252.96125); // Output file: write_Healpix_map_to_fits("!"+outfile, Map, planckType<MAP_PRECISION>()); }
int main (int argc, char *argv[]) { if ((argc < 3) || (argc > 4)) usage (argv[0]); std::string mapfile = argv[1]; std::string quad_list_prefix = argv[2]; Healpix_Map<double> map; read_Healpix_map_from_fits (mapfile, map); bool have_mask = false; Healpix_Map<double> mask; if (argc == 4) { read_Healpix_map_from_fits (argv[3], mask); have_mask = true; } // Figure out how many bins there are by trying to open files. std::vector<std::string> quad_list_files = Npoint_Functions::get_range_file_list(quad_list_prefix, 0, 180); std::vector<double> bin_list(quad_list_files.size()); std::vector<double> Corr(quad_list_files.size()); #pragma omp parallel shared(Corr, bin_list, quad_list_files) { Npoint_Functions::Quadrilateral_List_File<int> qlf; #pragma omp for schedule(dynamic,2) for (size_t k=0; k < quad_list_files.size(); ++k) { if (! qlf.initialize (quad_list_files[k])) { std::cerr << "Error initializing quadrilateral list from " << quad_list_files[k] << std::endl; std::exit(1); } if (static_cast<size_t>(map.Nside()) != qlf.Nside()) { std::cerr << "Map has Nside = " << map.Nside() << " but quad list has Nside = " << qlf.Nside() << "\nGiving up!\n"; std::exit(1); } if (map.Scheme() != qlf.Scheme()) map.swap_scheme(); if (have_mask) { if (static_cast<size_t>(mask.Nside()) != qlf.Nside()) { std::cerr << "Mask and quadrilateral lists do not have" << " the same Nside: " << mask.Nside() << " != " << qlf.Nside() << std::endl; std::exit(1); } if (mask.Scheme() != qlf.Scheme()) mask.swap_scheme(); } #pragma omp critical { std::cerr #ifdef OMP << omp_get_thread_num() << " " #endif << k << std::endl; } bin_list[k] = qlf.bin_value(); if (have_mask) { Corr[k] = calculate_masked_fourpoint_function (map, mask, qlf); } else { Corr[k] = calculate_fourpoint_function (map, qlf); } } } for (size_t k=0; k < bin_list.size(); ++k) { // Same format as spice std::cout << bin_list[k]*M_PI/180 << " " << cos(bin_list[k]*M_PI/180) << " " << Corr[k] << std::endl; } return 0; }
int main (int argc, char *argv[]) { if ((argc < 5) || (argc > 6)) usage (argv[0]); std::string quad_list_prefix = argv[1]; std::string alm_dir = argv[2]; size_t Nstart, Nend; if (! Npoint_Functions::from_string (argv[3], Nstart)) { std::cerr << "Could not parse Nstart\n"; usage (argv[0]); } if (! Npoint_Functions::from_string (argv[4], Nend)) { std::cerr << "Could not parse Nend\n"; usage (argv[0]); } bool have_mask = false; Healpix_Map<double> mask; if (argc == 6) { read_Healpix_map_from_fits (argv[5], mask); have_mask = true; } // Figure out how many bins there are by trying to open files. std::vector<std::string> quad_list_files = Npoint_Functions::get_range_file_list(quad_list_prefix, 0, 400); if (quad_list_files.size() == 0) { std::cerr << "No quad list files found!\n"; usage (argv[0]); } int Lmax; std::vector<Healpix_Map<double> > maps (Nend-Nstart); // Make maps { Npoint_Functions::Quadrilateral_List_File<int> qlf; qlf.initialize (quad_list_files[0]); if (have_mask) { if (static_cast<size_t>(mask.Nside()) != qlf.Nside()) { std::cerr << "Mask and quadrilateral lists do not have" << " the same Nside: " << mask.Nside() << " != " << qlf.Nside() << std::endl; std::exit(1); } if (mask.Scheme() != qlf.Scheme()) mask.swap_scheme(); } Lmax = std::min(200UL, 4*qlf.Nside()+1); //#pragma omp parallel shared(qlf, maps) { Alm<xcomplex<double> > alm (Lmax, Lmax); //#pragma omp for schedule(static) for (size_t k=0; k < maps.size(); ++k) { read_Alm_from_fits (dirtree::filename(alm_dir, "alm_T_", ".fits", k+Nstart), alm, Lmax, Lmax); maps[k].SetNside (qlf.Nside(), RING); alm2map (alm, maps[k]); if (maps[k].Scheme() != qlf.Scheme()) maps[k].swap_scheme(); } } } std::vector<double> bin_list(quad_list_files.size()); /* We will generate this by bin for each map so make the bin number the * first index. */ std::vector<std::vector<double> > Corr(quad_list_files.size()); #pragma omp parallel shared(Corr, bin_list, quad_list_files, maps, mask) { Npoint_Functions::Quadrilateral_List_File<int> qlf; #pragma omp for schedule(dynamic,2) for (size_t k=0; k < quad_list_files.size(); ++k) { if (! qlf.initialize (quad_list_files[k])) { std::cerr << "Error initializing quadrilateral list from " << quad_list_files[k] << std::endl; std::exit(1); } bin_list[k] = qlf.bin_value(); if (have_mask) { Npoint_Functions::calculate_masked_fourpoint_function_list (maps, mask, qlf, Corr[k]); } else { Npoint_Functions::calculate_fourpoint_function_list (maps, qlf, Corr[k]); } } } std::cout << "# LCDM four point function from " << quad_list_prefix << std::endl; std::cout << "# First line is bin values, rest are the four point function.\n"; for (size_t k=0; k < bin_list.size(); ++k) { std::cout << bin_list[k] << " "; } std::cout << std::endl; for (size_t j=0; j < maps.size(); ++j) { for (size_t k=0; k < bin_list.size(); ++k) { std::cout << Corr[k][j] << " "; } std::cout << std::endl; } return 0; }
void TestLikeHigh::runSubTest(unsigned int i, double& res, double& expected, std::string& subTestName) { using namespace Math; check(i >= 0 && i < 1, "invalid index " << i); const long nSide = 2048; const int lMaxSim = 2 * int(nSide); const int lMin = 31; const int lMax = 2000; const double fwhm = double(5) / 60.0; const int n = 5000; const double h = 0.6704; const double omBH2 = 0.022032; const double omCH2 = 0.12038; const double tau = 0.0925; const double ns = 0.9619; const double as = 2.2154e-9; const double pivot = 0.05; LambdaCDMParams paramsLCDM(omBH2, omCH2, h, tau, ns, as, pivot); CMB cmb; std::vector<double> clTT; output_screen1("Calculating Cl..." << std::endl); cmb.preInitialize(lMaxSim + 1000, false, true, false); cmb.initialize(paramsLCDM, true, false, true, false); cmb.getLensedCl(&clTT); output_screen1("OK" << std::endl); double nl = 0.005; std::vector<double> beam(lMaxSim + 1); Utils::readPixelWindowFunction(beam, nSide, lMaxSim, fwhm); Healpix_Map<double> uniformMask; uniformMask.SetNside(nSide, RING); for(unsigned long i = 0; i < uniformMask.Npix(); ++i) uniformMask[i] = 1; Healpix_Map<double>& mask = uniformMask; const double omegaPixel = 4 * Math::pi / mask.Npix(); const double w = omegaPixel / nl; // mask out some stuff std::stringstream maskFileName; maskFileName << "slow_test_files/test_highl_mask_" << i << ".fits"; std::stringstream maskFileName1; maskFileName1 << "slow_test_files/test_highl_mask_ap_" << i << ".fits"; Healpix_Map<double> apodizedMask; if(!fileExists(maskFileName.str().c_str())) { output_screen1("Masking out some regions..." << std::endl); int nRegions = 25; time_t seed1 = 1000000; Math::UniformRealGenerator thetaGen(seed1, pi / 50, pi - pi / 50), phiGen(seed1 + 1, 0, 2 * pi), angleGen(seed1 + 2, pi / 60, pi / 40); std::vector<double> maskTheta(nRegions), maskPhi(nRegions), maskAngle(nRegions); for(int i = 0; i < nRegions; ++i) { maskTheta[i] = thetaGen.generate(); maskPhi[i] = phiGen.generate(); maskAngle[i] = angleGen.generate(); } Utils::maskRegions(mask, maskTheta, maskPhi, maskAngle); for(unsigned long i = 0; i < mask.Npix(); ++i) { double theta, phi; pix2ang_ring(nSide, i, &theta, &phi); if(theta < Math::pi / 2 + pi / 20 && theta > pi / 2 - pi / 20) mask[i] = 0; } output_screen1("OK" << std::endl); fitshandle outMaskHandle; outMaskHandle.create(maskFileName.str().c_str()); write_Healpix_map_to_fits(outMaskHandle, mask, PLANCK_FLOAT64); outMaskHandle.close(); } else read_Healpix_map_from_fits(maskFileName.str(), mask); if(!fileExists(maskFileName1.str().c_str())) { output_screen1("Apodizing the mask..." << std::endl); MaskApodizer ap(mask); const double apAngle = 30.0 / 60.0 / 180.0 * pi; ap.apodize(MaskApodizer::COSINE_APODIZATION, apAngle, apodizedMask); output_screen1("OK" << std::endl); fitshandle outMaskHandle1; outMaskHandle1.create(maskFileName1.str().c_str()); write_Healpix_map_to_fits(outMaskHandle1, apodizedMask, PLANCK_FLOAT64); outMaskHandle1.close(); } else read_Healpix_map_from_fits(maskFileName1.str(), apodizedMask); check(apodizedMask.Nside() == nSide, ""); Healpix_Map<double> noiseWeight, noiseInvMat; noiseWeight.SetNside(nSide, RING); noiseInvMat.SetNside(nSide, RING); double nwAvg = 0; for(unsigned long i = 0; i < noiseWeight.Npix(); ++i) { noiseInvMat[i] = w; nwAvg += 1.0 / noiseInvMat[i]; noiseWeight[i] = noiseInvMat[i] * apodizedMask[i]; } nwAvg /= noiseWeight.Npix(); check(nwAvg > 0, ""); nl = omegaPixel * nwAvg; std::vector<double> nlTT(lMaxSim + 1, nl); std::ofstream outCl("slow_test_files/test_like_high_cl.txt"); for(int l = 0; l <= lMax; ++l) { const double factor = l * (l + 1) / (2 * Math::pi); outCl << l << '\t' << clTT[l] * factor << '\t' << nlTT[l] * factor << std::endl; } outCl.close(); std::stringstream couplingKernelFileName; couplingKernelFileName << "slow_test_files/" << "test_highl_mask_" << i << "_cc.txt"; std::stringstream noiseCouplingKernelFileName; noiseCouplingKernelFileName << "slow_test_files/" << "test_highl_noise_" << i << "_cc.txt"; output_screen1("Calculating mask coupling kernel..." << std::endl); Master::calculateCouplingKernel(apodizedMask, lMax + 500, couplingKernelFileName.str().c_str()); output_screen1("OK" << std::endl); output_screen1("Calculating noise coupling kernel..." << std::endl); Master::calculateCouplingKernel(noiseWeight, lMax + 500, noiseCouplingKernelFileName.str().c_str()); output_screen1("OK" << std::endl); Master master(apodizedMask, couplingKernelFileName.str().c_str(), beam, NULL, lMax + 500); output_screen1("Starting simulations..." << std::endl); time_t seed = std::time(0); StandardException exc; std::ofstream outLike("slow_test_files/test_highl_likes.txt"); if(!outLike) { std::string exceptionStr = "Cannot write into file slow_test_files/test_highl_likes.txt"; exc.set(exceptionStr); throw exc; } Math::Histogram<double> chi2Hist; Healpix_Map<double> noiseMap; noiseMap.SetNside(nSide, RING); ProgressMeter meter(n); for(int i = 0; i < n; ++i) { Alm<xcomplex<double> > alm, noiseAlm; Simulate::simulateAlm(clTT, alm, lMaxSim, seed); ++seed; Math::GaussianGenerator noiseGen(seed, 0.0, 1.0); for(unsigned long j = 0; j < noiseMap.Npix(); ++j) { check(noiseInvMat[j] > 0, ""); const double r = noiseGen.generate(); noiseMap[j] = r * std::sqrt(1.0 / noiseInvMat[j]); } ++seed; noiseAlm.Set(lMaxSim, lMaxSim); arr<double> weight(2 * noiseMap.Nside(), 1); map2alm(noiseMap, noiseAlm, weight); for(int l = 0; l <= lMaxSim; ++l) { for(int m = 0; m <= l; ++m) { alm(l, m) += noiseAlm(l, m); alm(l, m) *= beam[l]; } } Healpix_Map<double> map; map.SetNside(nSide, RING); alm2map(alm, map); master.calculate(map); std::vector<double> clSim(lMax + 1); for(int l = 0; l <= lMax; ++l) { std::map<double, double> ps = master.powerSpectrum(); check(ps.find(double(l)) != ps.end(), ""); const double lFactor = (l == 0 ? 1 : 2.0 * Math::pi / double(l * (l + 1))); clSim[l] = ps[double(l)] * lFactor - nlTT[l]; } LikelihoodHigh likelihood(clSim, nlTT, couplingKernelFileName.str().c_str(), lMin, lMax, noiseCouplingKernelFileName.str().c_str()); double like; like = likelihood.calculate(clTT); outLike << like << std::endl; chi2Hist.addData(like); meter.advance(); } outLike.close(); output_screen1("OK" << std::endl); chi2Hist.createHistogram(chi2Hist.min(), chi2Hist.max()); typedef Math::Histogram<double>::HistogramType HistogramType; const HistogramType& chi2Histogram = chi2Hist.getHistogram(); output_screen1("A total of " << chi2Hist.getDataSize() << " elements in the histogram." << std::endl); output_screen1("Calculating the chi squared distribution..." << std::endl); std::ofstream outDist("slow_test_files/test_highl_chi2.txt"); if(!outDist) { std::string exceptionStr = "Cannot write into file slow_test_files/test_highl_chi2.txt."; exc.set(exceptionStr); throw exc; } const double min = chi2Hist.min(), max = chi2Hist.max(); const int dof = lMax - lMin; Math::ChiSquared chi2(dof); int num = 10000; const double step = (max - min) / num; for(int i = 0; i < num; ++i) { const double x = min + step * i; const double y = chi2.evaluate(x); outDist << x << ' ' << y << std::endl; } outDist.close(); output_screen1("OK" << std::endl); std::ofstream outChi2("slow_test_files/test_highl_chi2_histogram.txt"); if(!outChi2) { std::string exceptionStr = "Cannot write into file slow_test_files/test_highl_chi2_histogram.txt."; exc.set(exceptionStr); throw exc; } double normalization = chi2Hist.getDataSize() * (chi2Hist.max() - chi2Hist.min()) / chi2Histogram.size(); const double deltaX = (chi2Hist.max() - chi2Hist.min()) / chi2Histogram.size(); double myChi2 = 0; int myDof = 0; for(HistogramType::const_iterator it = chi2Histogram.begin(); it != chi2Histogram.end(); ++it) { ++myDof; const double x = (*it).first + deltaX / 2; const double y = (double)(*it).second / normalization; outChi2 << x << ' ' << y << std::endl; const double expectedY = chi2.evaluate(x); const double diff = y - expectedY; const double e = std::sqrt(expectedY / normalization); myChi2 += diff * diff / (e * e); } output_screen("Chi^2 = " << myChi2 << " per " << myDof - 1 << " degrees of freedom." << std::endl); outChi2.close(); res = 1; expected = 1; const double chi2PerDof = myChi2 / (myDof - 1); const double chi2Sigma = std::sqrt(2.0 * (myDof - 1)); if(std::abs(myChi2 - myDof +1) > 5 * chi2Sigma) { output_screen("FAIL: Not a good fit!" << std::endl); res = 0; } subTestName = "highl"; }