CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
TwoPointCorrelation_deprojected.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2015 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
39 #include "Data1D_extra.h"
40 
41 using namespace std;
42 
43 using namespace cbl;
44 using namespace catalogue;
45 using namespace chainmesh;
46 using namespace pairs;
47 using namespace measure;
48 using namespace twopt;
49 
50 
51 // ============================================================================================
52 
53 
54 std::shared_ptr<data::Data> cbl::measure::twopt::TwoPointCorrelation_deprojected::Deprojected (const std::vector<double> rp, const std::vector<double> ww, const std::vector<double> error_ww)
55 {
56  vector<double> rad(rp.size(),0), xi(rp.size(),0), error(rp.size(),0);
57 
58  for (size_t i=0; i<rp.size(); i++) {
59 
60  const double ri = rp[i];
61  rad[i] = ri;
62 
63  for (size_t j=i; j<rp.size()-1; j++) {
64  const double rj = rp[j];
65  const double rj1 = rp[j+1];
66  const double fact = 1./(rj1-rj)*log((rj1+sqrt(max(0., rj1*rj1-ri*ri)))/(rj+sqrt(max(0., rj*rj-ri*ri))));
67 
68  xi[i] -= (ww[j+1]>-1. && ww[j]>-1.) ? (ww[j+1]-ww[j])*fact : 0.;
69  error[i] += (ww[j+1]>-1. && ww[j]>-1.) ? pow(error_ww[j+1]*fact, 2)+pow(error_ww[j]*fact, 2) : 0.;
70  }
71 
72  }
73 
74  for_each( xi.begin(), xi.end(), [] (double &vv) { vv /= par::pi;} );
75  for_each( error.begin(), error.end(), [] (double &vv) { vv = sqrt(vv/par::pi);} );
76 
77  return (!m_compute_extra_info) ? move(unique_ptr<data::Data1D>(new data::Data1D(rad, xi, error))) : data_with_extra_info(rad, xi, error);
78 }
79 
80 
81 // ============================================================================================
82 
83 
84 void cbl::measure::twopt::TwoPointCorrelation_deprojected::measure (const ErrorType errorType, const std::string dir_output_pairs, const std::vector<std::string> dir_input_pairs, const std::string dir_output_resample, const int nMocks, const bool count_dd, const bool count_rr, const bool count_dr, const bool tcount, const Estimator estimator, const double fact, const int seed)
85 {
86  switch (errorType) {
87  case (ErrorType::_Poisson_) :
88  measurePoisson(dir_output_pairs, dir_input_pairs, count_dd, count_rr, count_dr, tcount, estimator, fact);
89  break;
90  case (ErrorType::_Jackknife_) :
91  measureJackknife(dir_output_pairs, dir_input_pairs, dir_output_resample, count_dd, count_rr, count_dr, tcount, estimator, fact);
92  break;
93  case (ErrorType::_Bootstrap_) :
94  measureBootstrap(nMocks, dir_output_pairs, dir_input_pairs, dir_output_resample, count_dd, count_rr, count_dr, tcount, estimator, fact, seed);
95  break;
96  default:
97  ErrorCBL("unknown type of error!", "measure", "TwoPointCorrelation_deprojected.cpp");
98  }
99 }
100 
101 
102 // ============================================================================================
103 
104 
105 void cbl::measure::twopt::TwoPointCorrelation_deprojected::measurePoisson (const std::string dir_output_pairs, const std::vector<std::string> dir_input_pairs, const bool count_dd, const bool count_rr, const bool count_dr, const bool tcount, const Estimator estimator, const double fact)
106 {
107  // ----------- measure the 2D two-point correlation function, xi(rp,pi) -----------
108 
109  TwoPointCorrelation_projected::measurePoisson(dir_output_pairs, dir_input_pairs, count_dd, count_rr, count_dr, tcount, estimator, fact);
110 
111 
112  // ----------- integrate the 2D two-point correlation function along the parallel direction -----------
113 
114  vector<double> xx = m_dataset->xx();
115 
116  m_dataset = Deprojected(xx, TwoPointCorrelation_projected::dataset()->data(), TwoPointCorrelation_projected::dataset()->error());
117 }
118 
119 
120 // ============================================================================================
121 
122 
123 void cbl::measure::twopt::TwoPointCorrelation_deprojected::measureJackknife (const std::string dir_output_pairs, const std::vector<std::string> dir_input_pairs, const std::string dir_output_resample, const bool count_dd, const bool count_rr, const bool count_dr, const bool tcount, const Estimator estimator, const double fact)
124 {
125  if (dir_output_resample!=par::defaultString && dir_output_resample!="") {
126  string mkdir = "mkdir -p "+dir_output_resample;
127  if (system(mkdir.c_str())) {}
128  }
129 
130  vector<shared_ptr<data::Data>> data;
131  vector<shared_ptr<pairs::Pair>> dd_regions, rr_regions, dr_regions;
132  count_allPairs_region(dd_regions, rr_regions, dr_regions, TwoPType::_2D_Cartesian_, dir_output_pairs, dir_input_pairs, count_dd, count_rr, count_dr, tcount, estimator, fact);
133 
134  auto data_cart = (estimator==Estimator::_natural_) ? correlation_NaturalEstimator(m_dd, m_rr) : correlation_LandySzalayEstimator(m_dd, m_rr, m_dr);
135 
136  vector<double> xx_cart = data_cart->xx(), yy_cart = data_cart->yy();
137  vector<vector<double>> dd_cart, error_cart;
138  data_cart->get_data(dd_cart); data_cart->get_error(error_cart);
139 
140  auto data_proj = TwoPointCorrelation_projected::Projected(xx_cart, yy_cart, dd_cart, error_cart);
141 
142  if (estimator==Estimator::_natural_)
143  data = XiJackknife(dd_regions, rr_regions);
144  else if (estimator==Estimator::_LandySzalay_)
145  data = XiJackknife(dd_regions, rr_regions, dr_regions);
146  else
147  ErrorCBL("the chosen estimator is not implemented!", "measureJackknife", "TwoPointCorrelation_deprojected.cpp");
148 
149  vector<vector<double>> ww, covariance;
150  for (size_t i=0; i<data.size(); i++) {
151  ww.push_back(data[i]->data());
152 
153  if (dir_output_resample!=par::defaultString && dir_output_resample!="") {
154  string file = "xi_deprojected_Jackknife_"+conv(i, par::fINT)+".dat";
155  string header = "[1] separation at the bin centre # [2] deprojected two-point correlation function # [3] error";
156  if (m_compute_extra_info) header += " # [4] mean separation # [5] standard deviation of the separation distribution # [6] mean redshift # [7] standard deviation of the redshift distribution";
157  data[i]->write(dir_output_resample, file, header, 10, 0);
158  }
159  }
160 
161  covariance_matrix(ww, covariance, true);
162 
163  vector<double> xx = data_proj->xx();
164 
165  m_dataset = Deprojected(xx, data_proj->data(), data_proj->error());
166  m_dataset->set_covariance(covariance);
167 
168 }
169 
170 
171 // ============================================================================================
172 
173 
174 void cbl::measure::twopt::TwoPointCorrelation_deprojected::measureBootstrap (const int nMocks, const std::string dir_output_pairs, const std::vector<std::string> dir_input_pairs, const std::string dir_output_resample, const bool count_dd, const bool count_rr, const bool count_dr, const bool tcount, const Estimator estimator, const double fact, const int seed)
175 {
176  if (nMocks<=0)
177  ErrorCBL("the number of mocks must be >0!", "measureBootstrap", "TwoPointCorrelation_deprojected.cpp");
178 
179  if (dir_output_resample!=par::defaultString && dir_output_resample!="") {
180  string mkdir = "mkdir -p "+dir_output_resample;
181  if (system(mkdir.c_str())) {}
182  }
183 
184  vector<shared_ptr<data::Data>> data;
185  vector<shared_ptr<pairs::Pair>> dd_regions, rr_regions, dr_regions;
186  count_allPairs_region(dd_regions, rr_regions, dr_regions, TwoPType::_2D_Cartesian_, dir_output_pairs, dir_input_pairs, count_dd, count_rr, count_dr, tcount, estimator, fact);
187 
188  auto data_cart = (estimator==Estimator::_natural_) ? correlation_NaturalEstimator(m_dd, m_rr) : correlation_LandySzalayEstimator(m_dd, m_rr, m_dr);
189 
190  vector<double> xx_cart = data_cart->xx(), yy_cart = data_cart->yy();
191  vector<vector<double>> dd_cart, error_cart;
192  data_cart->get_data(dd_cart); data_cart->get_error(error_cart);
193 
194  auto data_proj = TwoPointCorrelation_projected::Projected(xx_cart, yy_cart, dd_cart, error_cart);
195 
196  if (estimator==Estimator::_natural_)
197  data = XiBootstrap(nMocks, dd_regions, rr_regions, seed);
198  else if (estimator==Estimator::_LandySzalay_)
199  data = XiBootstrap(nMocks, dd_regions, rr_regions, dr_regions, seed);
200  else
201  ErrorCBL("the chosen estimator is not implemented!", "measureBootstrap", "TwoPointCorrelation_deprojected.cpp");
202 
203  vector<vector<double>> ww, covariance;
204  for (size_t i=0; i<data.size(); i++) {
205  ww.push_back(data[i]->data());
206 
207  if (dir_output_resample!=par::defaultString && dir_output_resample!="") {
208  string file = "xi_deprojected_Bootstrap_"+conv(i, par::fINT)+".dat";
209  string header = "[1] separation at the bin centre # [2] deprojected two-point correlation function # [3] error";
210  if (m_compute_extra_info) header += " # [4] mean separation # [5] standard deviation of the separation distribution # [6] mean redshift # [7] standard deviation of the redshift distribution";
211  data[i]->write(dir_output_resample, file, header, 10, 0);
212  }
213  }
214 
215  covariance_matrix(ww, covariance, false);
216 
217  vector<double> xx = data_proj->xx();
218 
219  m_dataset = Deprojected(xx, data_proj->data(), data_proj->error());
220  m_dataset->set_covariance(covariance);
221 }
222 
223 
224 // ============================================================================================
225 
226 
227 std::vector<std::shared_ptr<data::Data>> cbl::measure::twopt::TwoPointCorrelation_deprojected::XiJackknife (const std::vector<std::shared_ptr<pairs::Pair>> dd, const std::vector<std::shared_ptr<pairs::Pair>> rr)
228 {
229  vector<shared_ptr<data::Data>> data;
230 
231  auto data_proj = TwoPointCorrelation_projected::XiJackknife(dd, rr);
232 
233  for (size_t i=0; i<data_proj.size(); i++) {
234  vector<double> xx = data_proj[i]->xx();
235  data.push_back(move(Deprojected(xx, data_proj[i]->data(), data_proj[i]->error())));
236  }
237 
238  return data;
239 }
240 
241 
242 // ============================================================================================
243 
244 
245 std::vector<std::shared_ptr<data::Data>> cbl::measure::twopt::TwoPointCorrelation_deprojected::XiJackknife (const std::vector<std::shared_ptr<pairs::Pair>> dd, const std::vector<std::shared_ptr<pairs::Pair>> rr, const std::vector<std::shared_ptr<pairs::Pair>> dr)
246 {
247  vector<shared_ptr<data::Data>> data;
248 
249  auto data_proj = TwoPointCorrelation_projected::XiJackknife(dd, rr, dr);
250  for (size_t i=0; i<data_proj.size(); i++) {
251  vector<double> xx = data_proj[i]->xx();
252  data.push_back(move(Deprojected(xx, data_proj[i]->data(), data_proj[i]->error())));
253  }
254 
255  return data;
256 }
257 
258 
259 // ============================================================================================
260 
261 
262 std::vector<std::shared_ptr<data::Data>> cbl::measure::twopt::TwoPointCorrelation_deprojected::XiBootstrap (const int nMocks, const std::vector<std::shared_ptr<pairs::Pair>> dd, const std::vector<std::shared_ptr<pairs::Pair>> rr, const int seed)
263 {
264  vector<shared_ptr<data::Data>> data;
265 
266  auto data_proj = TwoPointCorrelation_projected::XiBootstrap(nMocks, dd, rr, seed);
267  for (size_t i=0; i<data_proj.size(); i++) {
268  vector<double> xx = data_proj[i]->xx();
269  data.push_back(move(Deprojected(xx, data_proj[i]->data(), data_proj[i]->error())));
270  }
271 
272 
273  return data;
274 }
275 
276 
277 // ============================================================================================
278 
279 
280 std::vector<std::shared_ptr<data::Data>> cbl::measure::twopt::TwoPointCorrelation_deprojected::XiBootstrap (const int nMocks, const std::vector<std::shared_ptr<pairs::Pair>> dd, const std::vector<std::shared_ptr<pairs::Pair>> rr, const std::vector<std::shared_ptr<pairs::Pair>> dr, const int seed)
281 {
282  vector<shared_ptr<data::Data>> data;
283 
284  auto data_proj = TwoPointCorrelation_projected::XiBootstrap(nMocks, dd, rr, dr, seed);
285  for (size_t i=0; i<data_proj.size(); i++) {
286  vector<double> xx = data_proj[i]->xx();
287  data.push_back(move(Deprojected(xx, data_proj[i]->data(), data_proj[i]->error())));
288  }
289 
290  return data;
291 }
292 
293 
294 // ============================================================================================
295 
296 
297 void cbl::measure::twopt::TwoPointCorrelation_deprojected::read (const std::string dir, const std::string file)
298 {
299  m_dataset->read(dir+file);
300 }
301 
302 
303 // ============================================================================================
304 
305 
306 void cbl::measure::twopt::TwoPointCorrelation_deprojected::write (const std::string dir, const std::string file, const int rank) const
307 {
308  vector<double> xx = m_dataset->xx();
309 
310  checkDim(xx, m_dd->nbins_D1(), "rad");
311 
312  string header = "[1] separation at the bin centre # [2] deprojected two-point correlation function # [3] error";
313  if (m_compute_extra_info) header += " # [4] mean separation # [5] standard deviation of the separation distribution # [6] mean redshift # [7] standard deviation of the redshift distribution";
314 
315  m_dataset->write(dir, file, header, 5, rank);
316 }
317 
318 
The class Data1D_extra.
The class TwoPointCorrelation_deprojected.
The class Data1D.
Definition: Data1D.h:51
void measureJackknife(const std::string dir_output_pairs=par::defaultString, const std::vector< std::string > dir_input_pairs={}, const std::string dir_output_resample=par::defaultString, const bool count_dd=true, const bool count_rr=true, const bool count_dr=true, const bool tcount=true, const Estimator estimator=Estimator::_LandySzalay_, const double fact=0.1) override
measure the deprojected two-point correlation function estimating the covariance with Jackknife resam...
void read(const std::string dir, const std::string file) override
read the deprojected two-point correlation function
void write(const std::string dir=par::defaultString, const std::string file=par::defaultString, const int rank=0) const override
write the deprojected two-point correlation function
void measureBootstrap(const int nMocks, const std::string dir_output_pairs, const std::vector< std::string > dir_input_pairs={}, const std::string dir_output_resample=par::defaultString, const bool count_dd=true, const bool count_rr=true, const bool count_dr=true, const bool tcount=true, const Estimator estimator=Estimator::_LandySzalay_, const double fact=0.1, const int seed=3213) override
measure the deprojected two-point correlation function estimating the covariance with Bootstrap resam...
void measurePoisson(const std::string dir_output_pairs=par::defaultString, const std::vector< std::string > dir_input_pairs={}, const bool count_dd=true, const bool count_rr=true, const bool count_dr=true, const bool tcount=true, const Estimator estimator=Estimator::_LandySzalay_, const double fact=0.1) override
measure the deprojected two-point correlation function with Poisson errors
std::vector< std::shared_ptr< data::Data > > XiBootstrap(const int nMocks, const std::vector< std::shared_ptr< pairs::Pair >> dd, const std::vector< std::shared_ptr< pairs::Pair >> rr, const int seed=3213) override
measure the bootstrap resampling of the two-point correlation function, ξ(r)
void measure(const ErrorType errorType=ErrorType::_Poisson_, const std::string dir_output_pairs=par::defaultString, const std::vector< std::string > dir_input_pairs={}, const std::string dir_output_resample=par::defaultString, const int nMocks=0., const bool count_dd=true, const bool count_rr=true, const bool count_dr=true, const bool tcount=true, const Estimator estimator=Estimator::_LandySzalay_, const double fact=0.1, const int seed=3213) override
measure the deprojected two-point correlation function
std::shared_ptr< data::Data > Deprojected(const std::vector< double > rp, const std::vector< double > xi, const std::vector< double > error_xi) override
measure deprojected correlation function
std::vector< std::shared_ptr< data::Data > > XiJackknife(const std::vector< std::shared_ptr< pairs::Pair >> dd, const std::vector< std::shared_ptr< pairs::Pair >> rr) override
measure the jackknife resampling of the two-point correlation function, ξ(r)
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const std::string defaultString
default std::string value
Definition: Constants.h:336
Estimator
the two-point correlation estimator
ErrorType
the two-point correlation function error type
Definition: Measure.h:59
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
void covariance_matrix(const std::vector< std::vector< double >> mat, std::vector< std::vector< double >> &cov, const bool JK=false)
compute the covariance matrix from an input dataset
Definition: Func.cpp:761