CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
TwoPointCorrelation.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2015 by Federico Marulli *
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 
35 #include "TwoPointCorrelation.h"
43 
44 using namespace std;
45 
46 using namespace cbl;
47 using namespace catalogue;
48 using namespace chainmesh;
49 using namespace data;
50 using namespace pairs;
51 using namespace measure::twopt;
52 
53 
54 // ============================================================================
55 
56 
57 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType, const double Min, const double Max, const int nbins, const double shift, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
58 {
59  if (type==TwoPType::_angular_) return move(unique_ptr<TwoPointCorrelation1D_angular>(new TwoPointCorrelation1D_angular(data, random, binType, Min, Max, nbins, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
60 
61  else if (type==TwoPType::_monopole_) return move(unique_ptr<TwoPointCorrelation1D_monopole>(new TwoPointCorrelation1D_monopole(data, random, binType, Min, Max, nbins, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
62 
63  else if (type==TwoPType::_multipoles_direct_) return move(unique_ptr<TwoPointCorrelation_multipoles_direct>(new TwoPointCorrelation_multipoles_direct(data, random, binType, Min, Max, nbins, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
64 
65  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
66 
67  return NULL;
68 }
69 
70 
71 // ============================================================================
72 
73 
74 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType, const double Min, const double Max, const double binSize, const double shift, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
75 {
76  if (type==TwoPType::_angular_) return move(unique_ptr<TwoPointCorrelation1D_angular>(new TwoPointCorrelation1D_angular(data, random, binType, Min, Max, binSize, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
77 
78  else if (type==TwoPType::_monopole_) return move(unique_ptr<TwoPointCorrelation1D_monopole>(new TwoPointCorrelation1D_monopole(data, random, binType, Min, Max, binSize, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
79 
80  else if (type==TwoPType::_multipoles_direct_) return move(unique_ptr<TwoPointCorrelation_multipoles_direct>(new TwoPointCorrelation_multipoles_direct(data, random, binType, Min, Max, binSize, shift, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
81 
82  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
83 
84  return NULL;
85 }
86 
87 
88 // ============================================================================
89 
90 
91 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const int nbins_D1, const double shift_D1, const double Min_D2, const double Max_D2, const int nbins_D2, const double shift_D2, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
92 {
93  if (type==TwoPType::_multipoles_integrated_) return move(unique_ptr<TwoPointCorrelation_multipoles_integrated>(new TwoPointCorrelation_multipoles_integrated(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, Min_D2, Max_D2, nbins_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
94 
95  else if (type==TwoPType::_filtered_) return move(unique_ptr<TwoPointCorrelation1D_filtered>(new TwoPointCorrelation1D_filtered(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, Min_D2, Max_D2, nbins_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
96 
97  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
98 
99  return NULL;
100 }
101 
102 
103 // ============================================================================
104 
105 
106 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const double binSize_D1, const double shift_D1, const double Min_D2, const double Max_D2, const double binSize_D2, const double shift_D2, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
107 {
108  if (type==TwoPType::_multipoles_integrated_) return move(unique_ptr<TwoPointCorrelation_multipoles_integrated>(new TwoPointCorrelation_multipoles_integrated(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, Min_D2, Max_D2, binSize_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
109 
110  else if (type==TwoPType::_filtered_) return move(unique_ptr<TwoPointCorrelation1D_filtered>(new TwoPointCorrelation1D_filtered(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, Min_D2, Max_D2, binSize_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
111 
112  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
113 
114  return NULL;
115 }
116 
117 
118 // ============================================================================
119 
120 
121 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const int nbins_D1, const double shift_D1, const double Min_D2, const double Max_D2, const int nbins_D2, const double shift_D2, const double piMax_integral, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
122 {
123  if (type==TwoPType::_projected_) return move(unique_ptr<TwoPointCorrelation_projected>(new TwoPointCorrelation_projected(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, Min_D2, Max_D2, nbins_D2, shift_D2, piMax_integral, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
124 
125  else if (type==TwoPType::_deprojected_) return move(unique_ptr<TwoPointCorrelation_deprojected>(new TwoPointCorrelation_deprojected(data, random, Min_D1, Max_D1, nbins_D1, shift_D1, Min_D2, Max_D2, nbins_D2, shift_D2, piMax_integral, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
126 
127  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
128 
129  return NULL;
130 }
131 
132 
133 // ============================================================================
134 
135 
136 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const double binSize_D1, const double shift_D1, const double Min_D2, const double Max_D2, const double binSize_D2, const double shift_D2, const double piMax_integral, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
137 {
138  if (type==TwoPType::_projected_) return move(unique_ptr<TwoPointCorrelation_projected>(new TwoPointCorrelation_projected(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, Min_D2, Max_D2, binSize_D2, shift_D2, piMax_integral, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
139 
140  else if (type==TwoPType::_deprojected_) return move(unique_ptr<TwoPointCorrelation_deprojected>(new TwoPointCorrelation_deprojected(data, random, Min_D1, Max_D1, binSize_D1, shift_D1, Min_D2, Max_D2, binSize_D2, shift_D2, piMax_integral, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
141 
142  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
143 
144  return NULL;
145 }
146 
147 
148 // ============================================================================
149 
150 
151 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const int nbins_D1, const double shift_D1, const int nWedges, const int nbins_D2, const double shift_D2, const std::vector<std::vector<double>> mu_integral_limits, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
152 {
153  if (type==TwoPType::_wedges_) return move(unique_ptr<TwoPointCorrelation_wedges>(new TwoPointCorrelation_wedges(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, nWedges, nbins_D2, shift_D2, mu_integral_limits, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
154 
155  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
156 
157  return NULL;
158 }
159 
160 
161 // ============================================================================
162 
163 
164 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const double binSize_D1, const double shift_D1, const int nWedges, const double binSize_D2, const double shift_D2, const std::vector<std::vector<double>> mu_integral_limits, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
165 {
166  if (type==TwoPType::_wedges_) return move(unique_ptr<TwoPointCorrelation_wedges>(new TwoPointCorrelation_wedges(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, nWedges, binSize_D2, shift_D2, mu_integral_limits, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
167 
168  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
169 
170  return NULL;
171 }
172 
173 
174 // ============================================================================
175 
176 
177 shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const int nbins_D1, const double shift_D1, const BinType binType_D2, const double Min_D2, const double Max_D2, const int nbins_D2, const double shift_D2, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
178 {
179  if (type==TwoPType::_2D_Cartesian_) return move(unique_ptr<TwoPointCorrelation2D_cartesian>(new TwoPointCorrelation2D_cartesian(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, binType_D2, Min_D2, Max_D2, nbins_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
180 
181  else if (type==TwoPType::_2D_polar_) return move(unique_ptr<TwoPointCorrelation2D_polar>(new TwoPointCorrelation2D_polar(data, random, binType_D1, Min_D1, Max_D1, nbins_D1, shift_D1, binType_D2, Min_D2, Max_D2, nbins_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
182 
183  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
184 
185  return NULL;
186 }
187 
188 
189 // ============================================================================
190 
192 {
193  // reset the data-data pairs
194  m_dd->reset();
195  // reset the random-random pairs
196  m_rr->reset();
197  // reset the data-random pairs
198  m_dr->reset();
199 }
200 
201 // ============================================================================
202 
203 
204 std::shared_ptr<TwoPointCorrelation> cbl::measure::twopt::TwoPointCorrelation::Create (const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType_D1, const double Min_D1, const double Max_D1, const double binSize_D1, const double shift_D1, const BinType binType_D2, const double Min_D2, const double Max_D2, const double binSize_D2, const double shift_D2, const CoordinateUnits angularUnits, std::function<double(double)> angularWeight, const bool compute_extra_info, const double random_dilution_fraction)
205 {
206  if (type==TwoPType::_2D_Cartesian_) return move(unique_ptr<TwoPointCorrelation2D_cartesian>(new TwoPointCorrelation2D_cartesian(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, binType_D2, Min_D2, Max_D2, binSize_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
207 
208  else if (type==TwoPType::_2D_polar_) return move(unique_ptr<TwoPointCorrelation2D_polar>(new TwoPointCorrelation2D_polar(data, random, binType_D1, Min_D1, Max_D1, binSize_D1, shift_D1, binType_D2, Min_D2, Max_D2, binSize_D2, shift_D2, angularUnits, angularWeight, compute_extra_info, random_dilution_fraction)));
209 
210  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "TwoPointCorrelation.cpp");
211 
212  return NULL;
213 }
214 
215 
216 // ============================================================================
217 
218 
219 void cbl::measure::twopt::TwoPointCorrelation::count_pairs (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChM, std::shared_ptr<Pair> pp, const bool cross, const bool tcount)
220 {
221  // timer
222  time_t start; time(&start);
223  int dp = cout.precision();
224  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
225 
226  // number of objects in the first catalogue
227  int nObj = cat1->nObjects();
228 
229  // factor used by the timer
230  float fact_count = 100./nObj;
231 
232  // pointer to the second catalogue (get from the chain mesh)
233  shared_ptr<Catalogue> cat2 = ChM.catalogue();
234 
235  // thread number
236  int tid = 0;
237 
238  // start the multithreading parallelization
239 #pragma omp parallel num_threads(omp_get_max_threads()) private(tid)
240  {
241 
242  tid = omp_get_thread_num();
243 
244  // if (tid == 0) coutCBL << "Number of threads = " << omp_get_num_threads() << endl;
245 
246 
247  // internal object used by each thread to handle pairs
248 
249  shared_ptr<Pair> pp_thread = (pp->pairDim()==Dim::_1D_) ? move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin(), pp->sMax(), pp->nbins(), pp->shift(), pp->angularUnits(), pp->angularWeight()))
250  : move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin_D1(), pp->sMax_D1(), pp->nbins_D1(), pp->shift_D1(), pp->sMin_D2(), pp->sMax_D2(), pp->nbins_D2(), pp->shift_D2(), pp->angularUnits(), pp->angularWeight()));
251 
252 
253  // parallelized loop
254 #pragma omp for schedule(static, 2)
255 
256  // loop on the objects of the first catalogue
257  for (int i=0; i<nObj; ++i) {
258 
259  // get the indexes of objects in the second catalogue that are close to the object i of the first catalogue
260  // (i.e. objects inside the close cells of the chain-mesh)
261  vector<long> close_objects = ChM.close_objects(cat1->coordinate(i), (cross) ? -1 : (long)i);
262 
263  // loop on the nearby objects
264  for (auto &&j : close_objects)
265  // estimate the distance between the two objects and update the pair count
266  pp_thread->put(cat1->catalogue_object(i), cat2->catalogue_object(j));
267 
268  // estimate the computational time and update the time count
269  time_t end_temp; time(&end_temp); double diff_temp = difftime(end_temp, start);
270  if (tcount && tid==0) { coutCBL << "\r" << float(i)*fact_count << "% completed (" << diff_temp << " seconds)\r"; cout.flush(); }
271  if (i==int(nObj*0.25)) coutCBL << ".............25% completed " << endl;
272  if (i==int(nObj*0.5)) coutCBL << ".............50% completed " << endl;
273  if (i==int(nObj*0.75)) coutCBL << ".............75% completed "<< endl;
274 
275  }
276 
277 #pragma omp critical
278  {
279  // sum all the object pairs computed by each thread
280  pp->Sum(pp_thread);
281  }
282 
283  }
284 
285 
286  // show the time spent by the method
287  time_t end; time (&end);
288  double diff = difftime(end, start);
289  if (tid==0) {
290  if (diff<60) coutCBL << " time spent to count the pairs: " << diff << " seconds" << endl ;
291  else if (diff<3600) coutCBL << " time spent to count the pairs: " << diff/60 << " minutes" << endl ;
292  else coutCBL << " time spent to count the pairs: " << diff/3600 << " hours" << endl ;
293  }
294  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(dp);
295 
296 }
297 
298 
299 // ============================================================================
300 
301 
302 void cbl::measure::twopt::TwoPointCorrelation::count_allPairs (const TwoPType type, 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)
303 {
304  // ----------- compute polar coordinates, if necessary -----------
305 
306  if (!m_data->isSetVar(Var::_RA_) || !m_data->isSetVar(Var::_Dec_) || !m_data->isSetVar(Var::_Dc_))
307  m_data->computePolarCoordinates();
308 
309  if (!m_random->isSetVar(Var::_RA_) || !m_random->isSetVar(Var::_Dec_) || !m_random->isSetVar(Var::_Dc_))
310  m_random->computePolarCoordinates();
311 
312  if (type==TwoPType::_angular_) {
313  m_data->normalizeComovingCoordinates();
314  m_random->normalizeComovingCoordinates();
315  }
316 
317 
318  // ----------- dilute the random catalogue used to compute the RR pairs (to improve the performance) -----------
319 
320  if (estimator==Estimator::_natural_ && m_random_dilution_fraction!=1.) {
321  m_random_dilution_fraction = 1.;
322  WarningMsgCBL("m_random_dilution_fraction = 1, since the random catalogue is not diluted when using the natural estimator!", "count_allPairs", "TwoPointCorrelation.cpp");
323  }
324 
325  auto random_dil = make_shared<catalogue::Catalogue>(catalogue::Catalogue(move(m_random->diluted_catalogue(m_random_dilution_fraction))));
326 
327 
328  // ----------- create the chain-mesh -----------
329 
330  double rMAX;
331  double rMIN;
332 
333  if (type==TwoPType::_monopole_ || type==TwoPType::_multipoles_direct_ || type==TwoPType::_filtered_) {
334  rMAX = m_dd->sMax();
335  rMIN = m_dd->sMin();
336  }
337 
338  else if (type==TwoPType::_angular_) {
339  double xx, yy, zz;
340  cartesian_coord(radians(m_dd->sMax(), m_dd->angularUnits()), radians(m_dd->sMax(), m_dd->angularUnits()), 1., xx, yy, zz);
341  rMAX = sqrt(yy*yy+zz*zz);
342  cartesian_coord(radians(m_dd->sMin(), m_dd->angularUnits()), radians(m_dd->sMin(), m_dd->angularUnits()), 1., xx, yy, zz);
343  rMIN = sqrt(yy*yy+zz*zz);
344  }
345 
346  else if (type==TwoPType::_2D_polar_ || type==TwoPType::_multipoles_integrated_ || type ==TwoPType::_wedges_) {
347  rMAX = m_dd->sMax_D1();
348  rMIN = m_dd->sMin_D1();
349  }
350 
351  else if (type==TwoPType::_2D_Cartesian_ || type==TwoPType::_projected_ || type==TwoPType::_deprojected_) {
352  rMAX = max(m_dd->sMax_D1(), m_dd->sMax_D2())*sqrt(2.);
353  rMIN = max(m_dd->sMin_D1(), m_dd->sMin_D2())*sqrt(2.);
354  }
355 
356  else
357  ErrorCBL("the chosen two-point correlation function type is uknown!", "count_allPairs", "TwoPointCorrelation.cpp");
358 
359 
360  double cell_size = fact*rMAX; // to be optimized!!!
361 
362  ChainMesh_Catalogue ChM_data, ChM_random, ChM_random_dil;
363 
364  if (count_dd)
365  ChM_data.set_par(cell_size, m_data, rMAX, rMIN);
366 
367  if (count_rr)
368  ChM_random_dil.set_par(cell_size, random_dil, rMAX, rMIN);
369 
370  if (count_dr)
371  ChM_random.set_par(cell_size, m_random, rMAX, rMIN);
372 
373  // ----------- reset the pair counts --------------------------------------
374  resets();
375 
376  // ----------- count the number of pairs or read them from file -----------
377 
378  string file;
379 
380  cout << endl; coutCBL << par::col_green << "data-data" << par::col_default << endl;
381  file = "dd.dat";
382 
383  if (count_dd) {
384  count_pairs(m_data, ChM_data, m_dd, false, tcount);
385  if (dir_output_pairs!=par::defaultString) write_pairs(m_dd, dir_output_pairs, file);
386  }
387  else read_pairs(m_dd, dir_input_pairs, file);
388 
389 
390  cout << endl; coutCBL << par::col_green << "random-random" << par::col_default << endl;
391  file = "rr.dat";
392 
393  if (count_rr) {
394  count_pairs(random_dil, ChM_random_dil, m_rr, false, tcount);
395  if (dir_output_pairs!=par::defaultString) write_pairs(m_rr, dir_output_pairs, file);
396  }
397  else read_pairs(m_rr, dir_input_pairs, file);
398 
399 
400  if (estimator==Estimator::_LandySzalay_) {
401 
402  cout << endl; coutCBL << par::col_green << "data-random" << par::col_default << endl;
403  file = "dr.dat";
404 
405  if (count_dr) {
406  count_pairs(m_data, ChM_random, m_dr, true, tcount);
407  if (dir_output_pairs!=par::defaultString) write_pairs(m_dr, dir_output_pairs, file);
408  }
409  else read_pairs(m_dr, dir_input_pairs, file);
410 
411  }
412 
413  if (count_dd)
414  m_data->Order();
415 
416  if (count_rr || count_dr)
417  m_random->Order();
418 
419  if (type==TwoPType::_angular_) {
420  m_data->restoreComovingCoordinates();
421  m_random->restoreComovingCoordinates();
422  }
423 
424 }
425 
426 
427 // ============================================================================
428 
429 
430 double cbl::measure::twopt::TwoPointCorrelation::PoissonError (const Estimator estimator, const double dd, const double rr, const double dr, const int nData, const int nRandom) const
431 {
432  if (estimator!=Estimator::_natural_ && estimator!=Estimator::_LandySzalay_)
433  ErrorCBL("The implementation of Poisson errors for the chosen estimator is not available yet!", "PoissonError", "TwoPointCorrelation.cpp", glob::ExitCode::_workInProgress_);
434 
435  double fR = m_random_dilution_fraction;
436  if (estimator==Estimator::_natural_ && fR!=1) {
437  fR = 1.;
438  WarningMsgCBL("fR = 1, since the random catalogue is not diluted when using the natural estimator!", "PoissonError", "TwoPointCorrelation.cpp");
439  }
440 
441  const double norm1 = double(nRandom)*double(nRandom-1)/(double(nData)*double(nData-1));
442  const double norm2 = double(nRandom-1)/double(nData);
443 
444  const double T1 = pow(norm1*sqrt(dd)/rr, 2);
445  const double T2 = pow(norm2*sqrt(dr)/rr, 2);
446  const double T3 = pow((norm1*dd-norm2*dr)*pow(rr, -1.5), 2);
447 
448  if (min(T2, T3)>T1)
449  WarningMsgCBL("enlarge the random sample, that dominates the Poisson errors!", "PoissonError", "TwoPointCorrelation.cpp");
450 
451  return pow(fR, 2)*sqrt(T1+T2+T3);
452 }
453 
454 
455 // ============================================================================
456 
457 
458 void cbl::measure::twopt::TwoPointCorrelation::count_pairs_region (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChM, std::shared_ptr<Pair> pp, std::vector<std::shared_ptr<Pair>> pp_regions, const bool cross, const bool tcount)
459 {
460  // timer
461  time_t start; time (&start);
462  int dp = cout.precision();
463  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
464 
465  // number of objects in the first catalogue
466  int nObj = cat1->nObjects();
467 
468  // factor used by the timer
469  float fact_count = 100./nObj;
470 
471  // pointer to the second catalogue (get from the chain mesh)
472  shared_ptr<Catalogue> cat2 = ChM.catalogue();
473 
474  // thread number
475  int tid = 0;
476 
477  // set the number of regions (to be checked!)
478  const int nRegions = cat2->nRegions();
479 
480 #pragma omp parallel num_threads(omp_get_max_threads()) private(tid)
481  {
482  tid = omp_get_thread_num();
483 
484  vector<shared_ptr<Pair> > pp_thread(pp_regions.size());
485 
486  for (size_t i=0; i<pp_regions.size(); ++i)
487  pp_thread[i] = (pp->pairDim()==Dim::_1D_) ? move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin(), pp->sMax(), pp->nbins(), pp->shift(), pp->angularUnits(), pp->angularWeight()))
488  : move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin_D1(), pp->sMax_D1(), pp->nbins_D1(), pp->shift_D1(), pp->sMin_D2(), pp->sMax_D2(), pp->nbins_D2(), pp->shift_D2(), pp->angularUnits(), pp->angularWeight()));
489 
490 
491  // parallelized loop
492 #pragma omp for schedule(static, 2)
493  for (int i=0; i<nObj; ++i) {
494 
495  vector<long int> close_objects = ChM.close_objects(cat1->coordinate(i), (cross) ? -1 : i);
496 
497  for (auto &&j : close_objects) {
498  const int reg1 = (cross) ? cat1->region(i) : min(cat1->region(i), cat2->region(j));
499  const int reg2 = (cross) ? cat2->region(j) : max(cat1->region(i), cat2->region(j));
500 
501  // giving an element from lower/upper triangular matrix
502  const size_t index = (cross) ? reg1*nRegions+reg2 : reg1*nRegions+reg2-(reg1-1)*reg1/2-reg1;
503 
504  pp_thread[index]->put(cat1->catalogue_object(i), cat2->catalogue_object(j));
505  }
506 
507  // estimate the computational time and update the time count
508  time_t end_temp; time (&end_temp); double diff_temp = difftime(end_temp, start);
509  if (tcount && tid==0) { coutCBL << "\r" << float(i)*fact_count << "% completed (" << diff_temp << " seconds)\r"; cout.flush(); }
510  if (i==int(nObj*0.25)) coutCBL << ".............25% completed " << endl;
511  if (i==int(nObj*0.5)) coutCBL << ".............50% completed " << endl;
512  if (i==int(nObj*0.75)) coutCBL << ".............75% completed "<< endl;
513  }
514 
515 #pragma omp critical
516  {
517  // sum all the object pairs computed by each thread
518  for (size_t i=0; i<pp_regions.size(); ++i)
519  pp_regions[i]->Sum(pp_thread[i]);
520  }
521 
522  }
523 
524 
525  // show the time spent by the method
526  time_t end; time (&end);
527  double diff = difftime(end, start);
528  if (tid==0) {
529  if (diff<60) coutCBL << " time spent to count the pairs: " << diff << " seconds" << endl ;
530  else if (diff<3600) coutCBL << " time spent to count the pairs: " << diff/60 << " minutes" << endl ;
531  else coutCBL << " time spent to count the pairs: " << diff/3600 << " hours" << endl ;
532  }
533  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(dp);
534 
535 
536  // sum the pairs of the entire sample
537 
538  switch (pp->pairDim()) {
539 
540  case Dim::_1D_:
541  for (size_t i=0; i<pp->PP1D().size(); ++i)
542  for (size_t r=0; r<pp_regions.size(); ++r)
543  pp->add_data1D(i, pp_regions[r]);
544  break;
545 
546  case Dim::_2D_:
547  for (int i=0; i<pp->nbins_D1(); ++i)
548  for (int j=0; j<pp->nbins_D2(); ++j)
549  for (size_t r=0; r<pp_regions.size(); ++r)
550  pp->add_data2D(i, j, pp_regions[r]);
551  break;
552 
553  default:
554  ErrorCBL("no such type of pair dimension!", "count_pairs_region", "TwoPointCorrelation.cpp");
555  break;
556  }
557 
558 }
559 
560 
561 // ============================================================================
562 
563 
564 void cbl::measure::twopt::TwoPointCorrelation::count_pairs_region_test (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChM, std::shared_ptr<Pair> pp, std::vector<std::shared_ptr<Pair>> pp_res, const std::vector<double> weight, const bool cross, const bool tcount)
565 {
566  if(pp->pairDim()==Dim::_1D_)
567  count_pairs_region_test_1D(cat1, ChM, pp, pp_res, weight, cross, tcount);
568  else if(pp->pairDim()==Dim::_2D_)
569  count_pairs_region_test_2D(cat1, ChM, pp, pp_res, weight, cross, tcount);
570  else
571  ErrorCBL("wrong pair type!", "count_pairs_region_test", "TwoPointCorrelation.cpp");
572 }
573 
574 
575 // ============================================================================
576 
577 
578 void cbl::measure::twopt::TwoPointCorrelation::count_pairs_region_test_1D (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChM, std::shared_ptr<Pair> pp, std::vector<std::shared_ptr<Pair>> pp_res, const std::vector<double> weight, const bool cross, const bool tcount)
579 {
580  // timer
581  time_t start; time (&start);
582  int dp = cout.precision();
583  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
584 
585  // number of objects in the first catalogue
586  int nObj = cat1->nObjects();
587 
588  // factor used by the timer
589  float fact_count = 100./nObj;
590 
591  // pointer to the second catalogue (get from the chain mesh)
592  shared_ptr<Catalogue> cat2 = ChM.catalogue();
593 
594  // thread number
595  int tid = 0;
596 
597 
598 #pragma omp parallel num_threads(omp_get_max_threads()) private(tid)
599  {
600  tid = omp_get_thread_num();
601 
602  shared_ptr<Pair> pp_thread;
603  vector<shared_ptr<Pair> > pp_res_thread(pp_res.size());
604 
605  pp_thread = move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin(), pp->sMax(), pp->nbins(), pp->shift(), pp->angularUnits(), pp->angularWeight()));
606 
607  for (size_t i=0; i<pp_res.size(); ++i)
608  pp_res_thread[i] = move(Pair::Create(pp->pairType(), PairInfo::_standard_, pp->sMin(), pp->sMax(), pp->nbins(), pp->shift(), pp->angularUnits(), pp->angularWeight()));
609 
610  // parallelized loop
611 #pragma omp for schedule(static, 2)
612  for (int i=0; i<nObj; ++i) {
613 
614  vector<long int> close_objects = ChM.close_objects(cat1->coordinate(i), (cross) ? -1 : i);
615 
616  for (auto &&j : close_objects) {
617  int kk;
618  double wkk;
619 
620  pp_thread->get(cat1->catalogue_object(i), cat2->catalogue_object(j), kk, wkk);
621  pp_thread->set(kk, wkk);
622 
623  vector<double> ww = weight;
624 
625  ww[cat1->region(i)] = 0;
626  ww[cat2->region(j)] = 0;
627 
628  for(size_t k=0; k< weight.size(); k++)
629  pp_res_thread[k]->set(kk, wkk, ww[k]);
630  }
631 
632  // estimate the computational time and update the time count
633  time_t end_temp; time (&end_temp); double diff_temp = difftime(end_temp, start);
634  if (tcount && tid==0) { coutCBL << "\r" << float(i)*fact_count << "% completed (" << diff_temp << " seconds)\r"; cout.flush(); }
635  if (i==int(nObj*0.25)) coutCBL << ".............25% completed " << endl;
636  if (i==int(nObj*0.5)) coutCBL << ".............50% completed " << endl;
637  if (i==int(nObj*0.75)) coutCBL << ".............75% completed "<< endl;
638  }
639 
640 #pragma omp critical
641  {
642  // sum all the object pairs computed by each thread
643  pp->Sum(pp_thread);
644 
645  for (size_t i=0; i<pp_res.size(); ++i)
646  pp_res[i]->Sum(pp_res_thread[i]);
647  }
648 
649  }
650 
651 
652  // show the time spent by the method
653  time_t end; time (&end);
654  double diff = difftime(end, start);
655  if (tid==0) {
656  if (diff<60) coutCBL << " time spent to count the pairs: " << diff << " seconds" << endl ;
657  else if (diff<3600) coutCBL << " time spent to count the pairs: " << diff/60 << " minutes" << endl ;
658  else coutCBL << " time spent to count the pairs: " << diff/3600 << " hours" << endl ;
659  }
660  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(dp);
661 
662 }
663 
664 
665 // ============================================================================
666 
667 
668 void cbl::measure::twopt::TwoPointCorrelation::count_pairs_region_test_2D (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChM, std::shared_ptr<Pair> pp, std::vector<std::shared_ptr<Pair>> pp_res, const std::vector<double> weight, const bool cross, const bool tcount)
669 {
670  // timer
671  time_t start; time (&start);
672  int dp = cout.precision();
673  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
674 
675  // number of objects in the first catalogue
676  int nObj = cat1->nObjects();
677 
678  // factor used by the timer
679  float fact_count = 100./nObj;
680 
681  // pointer to the second catalogue (get from the chain mesh)
682  shared_ptr<Catalogue> cat2 = ChM.catalogue();
683 
684  // thread number
685  int tid = 0;
686 
687 #pragma omp parallel num_threads(omp_get_max_threads()) private(tid)
688  {
689  tid = omp_get_thread_num();
690 
691  shared_ptr<Pair> pp_thread;
692  vector<shared_ptr<Pair> > pp_res_thread(pp_res.size());
693 
694  pp_thread = move(Pair::Create(pp->pairType(), pp->pairInfo(), pp->sMin_D1(), pp->sMax_D1(), pp->nbins_D1(), pp->shift_D1(), pp->sMin_D2(), pp->sMax_D2(), pp->nbins_D2(), pp->shift_D2(), pp->angularUnits(), pp->angularWeight()));
695 
696  for (size_t i=0; i<pp_res.size(); ++i)
697  pp_res_thread[i] = move(Pair::Create(pp->pairType(), PairInfo::_standard_, pp->sMin_D1(), pp->sMax_D1(), pp->nbins_D1(), pp->shift_D1(), pp->sMin_D2(), pp->sMax_D2(), pp->nbins_D2(), pp->shift_D2(), pp->angularUnits(), pp->angularWeight()));
698 
699  // parallelized loop
700 #pragma omp for schedule(static, 2)
701  for (int i=0; i<nObj; ++i) {
702 
703  vector<long int> close_objects = ChM.close_objects(cat1->coordinate(i), (cross) ? -1 : i);
704 
705  for (auto &&j : close_objects) {
706  int ir, jr;
707  double wkk;
708 
709  pp_thread->get(cat1->catalogue_object(i), cat2->catalogue_object(j), ir, jr, wkk);
710  pp_thread->set(ir, jr, wkk);
711 
712  int reg1 = (cross) ? cat1->region(i) : min(cat1->region(i), cat2->region(j));
713  int reg2 = (cross) ? cat2->region(j) : max(cat1->region(i), cat2->region(j));
714 
715  vector<double> ww = weight;
716 
717  ww[reg1] = 0;
718  ww[reg2] = 0;
719 
720  for(size_t k=0; k< weight.size(); k++)
721  pp_res_thread[k]->set(ir, jr, wkk, ww[k]);
722  }
723 
724  // estimate the computational time and update the time count
725  time_t end_temp; time (&end_temp); double diff_temp = difftime(end_temp, start);
726  if (tcount && tid==0) { coutCBL << "\r" << float(i)*fact_count << "% completed (" << diff_temp << " seconds)\r"; cout.flush(); }
727  if (i==int(nObj*0.25)) coutCBL << ".............25% completed " << endl;
728  if (i==int(nObj*0.5)) coutCBL << ".............50% completed " << endl;
729  if (i==int(nObj*0.75)) coutCBL << ".............75% completed "<< endl;
730  }
731 
732 #pragma omp critical
733  {
734  // sum all the object pairs computed by each thread
735  pp->Sum(pp_thread);
736 
737  for (size_t i=0; i<pp_res.size(); ++i)
738  pp_res[i]->Sum(pp_res_thread[i]);
739  }
740 
741  }
742 
743 
744  // show the time spent by the method
745  time_t end; time (&end);
746  double diff = difftime(end, start);
747  if (tid==0) {
748  if (diff<60) coutCBL << " time spent to count the pairs: " << diff << " seconds" << endl ;
749  else if (diff<3600) coutCBL << " time spent to count the pairs: " << diff/60 << " minutes" << endl ;
750  else coutCBL << " time spent to count the pairs: " << diff/3600 << " hours" << endl ;
751  }
752  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(dp);
753 
754 }
755 
756 
757 // ============================================================================
758 
759 
760 void cbl::measure::twopt::TwoPointCorrelation::count_allPairs_region (std::vector<std::shared_ptr<Pair> > &dd_regions, std::vector<std::shared_ptr<Pair> > &rr_regions, std::vector<std::shared_ptr<Pair> > &dr_regions, const TwoPType type, 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)
761 {
762  // ----------- compute polar coordinates, if necessary -----------
763 
764  if (!m_data->isSetVar(Var::_RA_) || !m_data->isSetVar(Var::_Dec_) || !m_data->isSetVar(Var::_Dc_))
765  m_data->computePolarCoordinates();
766 
767  if (!m_random->isSetVar(Var::_RA_) || !m_random->isSetVar(Var::_Dec_) || !m_random->isSetVar(Var::_Dc_))
768  m_random->computePolarCoordinates();
769 
770  if (type==TwoPType::_angular_) {
771  m_data->normalizeComovingCoordinates();
772  m_random->normalizeComovingCoordinates();
773  }
774 
775 
776  // ----------- dilute the random catalogue used to compute the RR pairs (to improve the performance) -----------
777 
778  if (estimator==Estimator::_natural_ && m_random_dilution_fraction!=1.) {
779  m_random_dilution_fraction = 1.;
780  WarningMsgCBL("m_random_dilution_fraction = 1, since the random catalogue is not diluted when using the natural estimator!", "count_allPairs_region", "TwoPointCorrelation.cpp");
781  }
782 
783  auto random_dil = make_shared<catalogue::Catalogue>(catalogue::Catalogue(move(m_random->diluted_catalogue(m_random_dilution_fraction))));
784 
785 
786  // ----------- create the chain-mesh -----------
787 
788  double rMAX;
789  double rMIN;
790 
791  if (type==TwoPType::_monopole_ || type==TwoPType::_multipoles_direct_ || type==TwoPType::_filtered_) {
792  rMAX = m_dd->sMax();
793  rMIN = m_dd->sMin();
794  }
795 
796  else if (type==TwoPType::_angular_) {
797  double xx, yy, zz;
798  cartesian_coord(radians(m_dd->sMax(), m_dd->angularUnits()), radians(m_dd->sMax(), m_dd->angularUnits()), 1., xx, yy, zz);
799  rMAX = sqrt(yy*yy+zz*zz);
800  cartesian_coord(radians(m_dd->sMin(), m_dd->angularUnits()), radians(m_dd->sMin(), m_dd->angularUnits()), 1., xx, yy, zz);
801  rMIN = sqrt(yy*yy+zz*zz);
802  }
803 
804  else if (type==TwoPType::_2D_polar_ || type==TwoPType::_multipoles_integrated_ || type ==TwoPType::_wedges_) {
805  rMAX = m_dd->sMax_D1();
806  rMIN = m_dd->sMin_D1();
807  }
808 
809  else if (type==TwoPType::_2D_Cartesian_ || type==TwoPType::_projected_ || type==TwoPType::_deprojected_) {
810  rMAX = max(m_dd->sMax_D1(), m_dd->sMax_D2())*sqrt(2.);
811  rMIN = max(m_dd->sMin_D1(), m_dd->sMin_D2())*sqrt(2.);
812  }
813 
814  else
815  ErrorCBL("the chosen two-point correlation function type is uknown!", "count_allPairs_regions", "TwoPointCorrelation.cpp");
816 
817 
818  double cell_size = fact*rMAX; // to be optimized!!!
819 
820  ChainMesh_Catalogue ChM_data, ChM_random, ChM_random_dil;
821 
822  if (count_dd)
823  ChM_data.set_par(cell_size, m_data, rMAX, rMIN);
824 
825  if (count_rr || count_dr)
826  ChM_random.set_par(cell_size, m_random, rMAX, rMIN);
827 
828  if (count_dr)
829  ChM_random_dil.set_par(cell_size, random_dil, rMAX, rMIN);
830 
831 
832  // ----------- initialize the pair vectors used for resampling -----------
833 
834  const int nRegions = m_random->nRegions();
835 
836  if (nRegions<1)
837  ErrorCBL("set regions in data and random catalogue to compute 2pcf Jackknife/Bootstrap error!", "count_allPairs_region", "TwoPointCorrelation.cpp");
838 
839  const int nP_auto = nRegions*(nRegions+1)*0.5;
840  const int nP_cross = nRegions*nRegions;
841 
842  dd_regions.erase(dd_regions.begin(), dd_regions.end());
843  rr_regions.erase(rr_regions.begin(), rr_regions.end());
844  dr_regions.erase(dr_regions.begin(), dr_regions.end());
845 
846  for (int i=0; i<nP_auto; ++i) {
847 
848  dd_regions.push_back((m_dd->pairDim()==Dim::_1D_) ? move(Pair::Create(m_dd->pairType(), m_dd->pairInfo(), m_dd->sMin(), m_dd->sMax(), m_dd->nbins(), m_dd->shift(), m_dd->angularUnits(), m_dd->angularWeight())) : move(Pair::Create(m_dd->pairType(), m_dd->pairInfo(), m_dd->sMin_D1(), m_dd->sMax_D1(), m_dd->nbins_D1(), m_dd->shift_D1(), m_dd->sMin_D2(), m_dd->sMax_D2(), m_dd->nbins_D2(), m_dd->shift_D2(), m_dd->angularUnits(), m_dd->angularWeight())));
849 
850  rr_regions.push_back((m_rr->pairDim()==Dim::_1D_) ? move(Pair::Create(m_rr->pairType(), m_rr->pairInfo(), m_rr->sMin(), m_rr->sMax(), m_rr->nbins(), m_rr->shift(), m_rr->angularUnits(), m_rr->angularWeight())) : move(Pair::Create(m_rr->pairType(), m_rr->pairInfo(), m_rr->sMin_D1(), m_rr->sMax_D1(), m_rr->nbins_D1(), m_rr->shift_D1(), m_rr->sMin_D2(), m_rr->sMax_D2(), m_rr->nbins_D2(), m_rr->shift_D2(), m_rr->angularUnits(), m_rr->angularWeight())));
851  }
852 
853  for (int i=0; i<nP_cross; ++i) {
854 
855  dr_regions.push_back((m_dr->pairDim()==Dim::_1D_) ? move(Pair::Create(m_dr->pairType(), m_dr->pairInfo(), m_dr->sMin(), m_dr->sMax(), m_dr->nbins(), m_dr->shift(), m_dr->angularUnits(), m_dr->angularWeight())) : move(Pair::Create(m_dr->pairType(), m_dr->pairInfo(), m_dr->sMin_D1(), m_dr->sMax_D1(), m_dr->nbins_D1(), m_dr->shift_D1(), m_dr->sMin_D2(), m_dr->sMax_D2(), m_dr->nbins_D2(), m_dr->shift_D2(), m_dr->angularUnits(), m_dr->angularWeight())));
856 
857  }
858 
859  // ----------- reset the pair counts --------------------------------------
860  resets();
861 
862  // ----------- count the number of pairs or read them from file -----------
863 
864  string file, file_regions;
865 
866  cout << endl; coutCBL << par::col_green << "data-data" << par::col_default << endl;
867 
868  file = "dd.dat";
869  file_regions = "dd_regions.dat";
870 
871  if (count_dd) {
872  count_pairs_region(m_data, ChM_data, m_dd, dd_regions, false, tcount);
873  if (dir_output_pairs!=par::defaultString) {
874  write_pairs(m_dd, dir_output_pairs, file);
875  write_pairs(dd_regions, dir_output_pairs, file_regions);
876  }
877  }
878  else {
879  read_pairs(m_dd, dir_input_pairs, file);
880  read_pairs(dd_regions, dir_input_pairs, file_regions);
881  }
882 
883  cout << endl; coutCBL << par::col_green << "random-random" << par::col_default << endl;
884 
885 
886  file = "rr.dat";
887  file_regions = "rr_regions.dat";
888 
889  if (count_rr) {
890  count_pairs_region(random_dil, ChM_random_dil, m_rr, rr_regions, false, tcount);
891  if (dir_output_pairs!=par::defaultString) {
892  write_pairs(m_rr, dir_output_pairs, file);
893  write_pairs(rr_regions, dir_output_pairs, file_regions);
894  }
895  }
896  else {
897  read_pairs(m_rr, dir_input_pairs, file);
898  read_pairs(rr_regions, dir_input_pairs, file_regions);
899  }
900 
901  if (estimator==Estimator::_LandySzalay_) {
902 
903  cout << endl; coutCBL << par::col_green << "data-random" << par::col_default << endl;
904 
905  file = "dr.dat";
906  file_regions = "dr_regions.dat";
907 
908  if (count_dr) {
909  count_pairs_region(m_data, ChM_random, m_dr, dr_regions, true, tcount);
910  if (dir_output_pairs!=par::defaultString) {
911  write_pairs(m_dr, dir_output_pairs, file);
912  write_pairs(dr_regions, dir_output_pairs, file_regions);
913  }
914  }
915  else {
916  read_pairs(m_dr, dir_input_pairs, file);
917  read_pairs(dr_regions, dir_input_pairs, file_regions);
918  }
919 
920  }
921 
922  if (count_dd)
923  m_data->Order();
924 
925  if (count_rr || count_dr)
926  m_random->Order();
927 
928  if (type==TwoPType::_angular_) {
929  m_data->restoreComovingCoordinates();
930  m_random->restoreComovingCoordinates();
931  }
932 
933 }
934 
935 
936 // ============================================================================
937 
938 
939 void cbl::measure::twopt::TwoPointCorrelation::count_allPairs_region_test (const TwoPType type, const std::vector<double> weight, 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)
940 {
941  // ----------- compute polar coordinates, if necessary -----------
942 
943  if (!m_data->isSetVar(Var::_RA_) || !m_data->isSetVar(Var::_Dec_) || !m_data->isSetVar(Var::_Dc_))
944  m_data->computePolarCoordinates();
945 
946  if (!m_random->isSetVar(Var::_RA_) || !m_random->isSetVar(Var::_Dec_) || !m_random->isSetVar(Var::_Dc_))
947  m_random->computePolarCoordinates();
948 
949  if (type==TwoPType::_angular_) {
950  m_data->normalizeComovingCoordinates();
951  m_random->normalizeComovingCoordinates();
952  }
953 
954 
955  // ----------- dilute the random catalogue used to compute the RR pairs (to improve the performance) -----------
956 
957  if (estimator==Estimator::_natural_ && m_random_dilution_fraction!=1.) {
958  m_random_dilution_fraction = 1.;
959  WarningMsgCBL("m_random_dilution_fraction = 1, since the random catalogue is not diluted when using the natural estimator!", "count_allPairs_region_test", "TwoPointCorrelation.cpp");
960  }
961 
962  auto random_dil = make_shared<catalogue::Catalogue>(catalogue::Catalogue(move(m_random->diluted_catalogue(m_random_dilution_fraction))));
963 
964 
965  // ----------- create the chain-mesh -----------
966 
967  double rMAX;
968  double rMIN;
969 
970  if (type==TwoPType::_monopole_ || type==TwoPType::_filtered_) {
971  rMAX = m_dd->sMax();
972  rMIN = m_dd->sMin();
973  }
974 
975  else if (type==TwoPType::_angular_) {
976  double xx, yy, zz;
977  cartesian_coord(radians(m_dd->sMax(), m_dd->angularUnits()), radians(m_dd->sMax(), m_dd->angularUnits()), 1., xx, yy, zz);
978  rMAX = sqrt(yy*yy+zz*zz);
979  cartesian_coord(radians(m_dd->sMin(), m_dd->angularUnits()), radians(m_dd->sMin(), m_dd->angularUnits()), 1., xx, yy, zz);
980  rMIN = sqrt(yy*yy+zz*zz);
981  }
982 
983  else if (type==TwoPType::_2D_polar_ || type==TwoPType::_multipoles_integrated_ || type ==TwoPType::_wedges_) {
984  rMAX = m_dd->sMax_D1();
985  rMIN = m_dd->sMin_D1();
986  }
987 
988  else if (type==TwoPType::_2D_Cartesian_ || type==TwoPType::_projected_ || type==TwoPType::_deprojected_) {
989  rMAX = max(m_dd->sMax_D1(), m_dd->sMax_D2())*sqrt(2.);
990  rMIN = max(m_dd->sMin_D1(), m_dd->sMin_D2())*sqrt(2.);
991  }
992 
993  else
994  ErrorCBL("the chosen two-point correlation function type is uknown!", "count_allPairs_regions", "TwoPointCorrelation.cpp");
995 
996 
997  double cell_size = fact*rMAX; // to be optimized!!!
998 
999  ChainMesh_Catalogue ChM_data, ChM_random, ChM_random_dil;
1000 
1001  if (count_dd)
1002  ChM_data.set_par(cell_size, m_data, rMAX, rMIN);
1003 
1004  if (count_rr || count_dr)
1005  ChM_random.set_par(cell_size, m_random, rMAX, rMIN);
1006 
1007  if (count_dr)
1008  ChM_random_dil.set_par(cell_size, random_dil, rMAX, rMIN);
1009 
1010 
1011  // ----------- initialize the pair vectors used for resampling -----------
1012 
1013  int nResamplings = weight.size();
1014 
1015  for (int i=0; i<nResamplings; ++i) {
1016 
1017  m_dd_res.push_back((m_dd->pairDim()==Dim::_1D_) ? move(Pair::Create(m_dd->pairType(), m_dd->pairInfo(), m_dd->sMin(), m_dd->sMax(), m_dd->nbins(), m_dd->shift(), m_dd->angularUnits(), m_dd->angularWeight())) : move(Pair::Create(m_dd->pairType(), m_dd->pairInfo(), m_dd->sMin_D1(), m_dd->sMax_D1(), m_dd->nbins_D1(), m_dd->shift_D1(), m_dd->sMin_D2(), m_dd->sMax_D2(), m_dd->nbins_D2(), m_dd->shift_D2(), m_dd->angularUnits(), m_dd->angularWeight())));
1018 
1019  m_rr_res.push_back((m_rr->pairDim()==Dim::_1D_) ? move(Pair::Create(m_rr->pairType(), m_rr->pairInfo(), m_rr->sMin(), m_rr->sMax(), m_rr->nbins(), m_rr->shift(), m_rr->angularUnits(), m_rr->angularWeight())) : move(Pair::Create(m_rr->pairType(), m_rr->pairInfo(), m_rr->sMin_D1(), m_rr->sMax_D1(), m_rr->nbins_D1(), m_rr->shift_D1(), m_rr->sMin_D2(), m_rr->sMax_D2(), m_rr->nbins_D2(), m_rr->shift_D2(), m_rr->angularUnits(), m_rr->angularWeight())));
1020 
1021  m_dr_res.push_back((m_dr->pairDim()==Dim::_1D_) ? move(Pair::Create(m_dr->pairType(), m_dr->pairInfo(), m_dr->sMin(), m_dr->sMax(), m_dr->nbins(), m_dr->shift(), m_dr->angularUnits(), m_dr->angularWeight())) : move(Pair::Create(m_dr->pairType(), m_dr->pairInfo(), m_dr->sMin_D1(), m_dr->sMax_D1(), m_dr->nbins_D1(), m_dr->shift_D1(), m_dr->sMin_D2(), m_dr->sMax_D2(), m_dr->nbins_D2(), m_dr->shift_D2(), m_dr->angularUnits(), m_dr->angularWeight())));
1022 
1023  }
1024 
1025 
1026  // ----------- count the number of pairs or read them from file -----------
1027 
1028  string file, file_regions;
1029 
1030  cout << endl; coutCBL << par::col_green << "data-data" << par::col_default << endl;
1031 
1032  file = "dd.dat";
1033  file_regions = "dd_regions.dat";
1034 
1035  if (count_dd) {
1036  count_pairs_region_test(m_data, ChM_data, m_dd, m_dd_res, weight, false, tcount);
1037  if (dir_output_pairs!=par::defaultString)
1038  write_pairs(m_dd, dir_output_pairs, file);
1039  }
1040  else
1041  read_pairs(m_dd, dir_input_pairs, file);
1042 
1043  cout << endl; coutCBL << par::col_green << "random-random" << par::col_default << endl;
1044 
1045  file = "rr.dat";
1046  file_regions = "rr_regions.dat";
1047 
1048  if (count_rr) {
1049  count_pairs_region_test(random_dil, ChM_random_dil, m_rr, m_rr_res, weight, false, tcount);
1050  if (dir_output_pairs!=par::defaultString)
1051  write_pairs(m_rr, dir_output_pairs, file);
1052 
1053  }
1054  else
1055  read_pairs(m_rr, dir_input_pairs, file);
1056 
1057 
1058  if (estimator==Estimator::_LandySzalay_) {
1059 
1060  cout << endl; coutCBL << par::col_green << "data-random" << par::col_default << endl;
1061 
1062  file = "dr.dat";
1063  file_regions = "dr_regions.dat";
1064 
1065  if (count_dr) {
1066  count_pairs_region_test(m_data, ChM_random, m_dr, m_dr_res, weight, true, tcount);
1067  if (dir_output_pairs!=par::defaultString)
1068  write_pairs(m_dr, dir_output_pairs, file);
1069  }
1070  else
1071  read_pairs(m_dr, dir_input_pairs, file);
1072 
1073  }
1074 
1075  if (count_dd)
1076  m_data->Order();
1077 
1078  if (count_rr || count_dr)
1079  m_random->Order();
1080 
1081  if (type==TwoPType::_angular_) {
1082  m_data->restoreComovingCoordinates();
1083  m_random->restoreComovingCoordinates();
1084  }
1085 
1086 }
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class TwoPointCorrelation1D_angular.
The class TwoPointCorrelation1D_filtered.
The class TwoPointCorrelation1D_monopole.
The class TwoPointCorrelation.
The class TwoPointCorrelation_deprojected.
The class TwoPointCorrelation_multipoles_direct.
The class TwoPointCorrelation_multipoles_integrated.
The class TwoPointCorrelation_wedges.
The class Catalogue.
Definition: Catalogue.h:654
The class ChainMesh_Catalogue.
std::shared_ptr< catalogue::Catalogue > catalogue() const
get the internal variable m_catalogue
void set_par(const double cell_size, std::shared_ptr< catalogue::Catalogue > cat, const double rmax, const double rmin=-1.)
function that set parameters for the chain-mesh
std::vector< long > close_objects(std::vector< double > center, long ii=-1) const
get the indeces of the objects close to an object
Definition: ChainMesh.cpp:298
void count_allPairs(const TwoPType type, 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)
count the data-data, random-random and data-random pairs, used to construct the estimator of the two-...
void count_allPairs_region_test(const TwoPType type, const std::vector< double > weight, 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)
count the data-data, random-random and data-random pairs, used to construct the estimator of the two-...
void count_pairs_region(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChM, std::shared_ptr< pairs::Pair > pp, std::vector< std::shared_ptr< pairs::Pair > > pp_regions, const bool cross=true, const bool tcount=false)
count the number of pairs, used for Jackknife/Bootstrap methods
double PoissonError(const Estimator estimator, const double dd, const double rr, const double dr, const int nData, const int nRandom) const
the Poisson errors
void count_pairs_region_test_2D(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChM, std::shared_ptr< pairs::Pair > pp, std::vector< std::shared_ptr< pairs::Pair > > pp_res, const std::vector< double > weight, const bool cross=true, const bool tcount=false)
count the number of pairs, used for Jackknife/Bootstrap methods, 2D pairs
void count_pairs(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChM, std::shared_ptr< pairs::Pair > pp, const bool cross=true, const bool tcount=false)
count the number of pairs
void count_pairs_region_test(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChM, std::shared_ptr< pairs::Pair > pp, std::vector< std::shared_ptr< pairs::Pair > > pp_res, const std::vector< double > weight, const bool cross=true, const bool tcount=false)
count the number of pairs, used for Jackknife/Bootstrap methods
static std::shared_ptr< TwoPointCorrelation > Create(const TwoPType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const BinType binType, const double Min, const double Max, const int nbins, const double shift, const CoordinateUnits angularUnits=CoordinateUnits::_radians_, std::function< double(double)> angularWeight=nullptr, const bool compute_extra_info=false, const double random_dilution_fraction=1.)
static factory used to construct two-point correlation functions of any type
void count_allPairs_region(std::vector< std::shared_ptr< pairs::Pair > > &dd_regions, std::vector< std::shared_ptr< pairs::Pair > > &rr_regions, std::vector< std::shared_ptr< pairs::Pair > > &dr_regions, const TwoPType type, 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)
count the data-data, random-random and data-random pairs, used to construct the estimator of the two-...
void count_pairs_region_test_1D(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChM, std::shared_ptr< pairs::Pair > pp, std::vector< std::shared_ptr< pairs::Pair > > pp_res, const std::vector< double > weight, const bool cross=true, const bool tcount=false)
count the number of pairs, used for Jackknife/Bootstrap methods, 1D pairs
static const std::string col_green
green colour (used when printing something on the screen)
Definition: Constants.h:309
static const std::string col_default
default colour (used when printing something on the screen)
Definition: Constants.h:297
static const std::string defaultString
default std::string value
Definition: Constants.h:336
Estimator
the two-point correlation estimator
TwoPType
the two-point correlation function type
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T Min(const std::vector< T > vect)
minimum element of a std::vector
Definition: Kernel.h:1324
void cartesian_coord(const double ra, const double dec, const double dd, double &XX, double &YY, double &ZZ)
conversion from polar coordinates to Cartesian coordinates
Definition: Func.cpp:221
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
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
double radians(const double angle, const CoordinateUnits inputUnits=CoordinateUnits::_degrees_)
conversion to radians
Definition: Func.cpp:126
CoordinateUnits
the coordinate units
Definition: Kernel.h:562
BinType
the binning type
Definition: Kernel.h:505
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747