CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ThreePointCorrelation.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli, Michele Moresco *
3  * and Alfonso Veropalumbo *
4  * *
5  * federico.marulli3@unibo.it *
6  * *
7  * This program is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License as *
9  * published by the Free Software Foundation; either version 2 of *
10  * the License, or (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public *
18  * License along with this program; if not, write to the Free *
19  * Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ********************************************************************/
22 
40 #include "ThreePointCorrelation.h"
47 
48 using namespace std;
49 
50 using namespace cbl;
51 using namespace catalogue;
52 using namespace chainmesh;
53 using namespace triplets;
54 using namespace measure::threept;
55 
56 
57 // ============================================================================
58 
59 
60 std::shared_ptr<ThreePointCorrelation> cbl::measure::threept::ThreePointCorrelation::Create (const ThreePType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const triplets::TripletType tripletType, const double side_s, const double side_u, const double perc_increase, const int nbins)
61 {
62  if (type==ThreePType::_angular_connected_) return move(unique_ptr<ThreePointCorrelation_angular_connected>(new ThreePointCorrelation_angular_connected(data, random, side_s, side_u, perc_increase, nbins)));
63 
64  if (type==ThreePType::_angular_reduced_) return move(unique_ptr<ThreePointCorrelation_angular_reduced>(new ThreePointCorrelation_angular_reduced(data, random, side_s, side_u, perc_increase, nbins)));
65 
66  if (type==ThreePType::_comoving_connected_) return move(unique_ptr<ThreePointCorrelation_comoving_connected>(new ThreePointCorrelation_comoving_connected(data, random, tripletType, side_s, side_u, perc_increase, nbins)));
67 
68  if (type==ThreePType::_comoving_reduced_) return move(unique_ptr<ThreePointCorrelation_comoving_reduced>(new ThreePointCorrelation_comoving_reduced(data, random, tripletType, side_s, side_u, perc_increase, nbins)));
69 
70  else ErrorCBL("no such type of object!", "Create", "ThreePointCorrelation.cpp");
71 
72  return NULL;
73 }
74 
75 
76 // ============================================================================
77 
78 
79 std::shared_ptr<ThreePointCorrelation> cbl::measure::threept::ThreePointCorrelation::Create (const ThreePType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const triplets::TripletType tripletType, const double r12, const double r12_binSize, const double r13, const double r13_binSize, const int nbins)
80 {
81  if (type==ThreePType::_angular_connected_) return move(unique_ptr<ThreePointCorrelation_angular_connected>(new ThreePointCorrelation_angular_connected(data, random, r12, r12_binSize, r13, r13_binSize, nbins)));
82 
83  if (type==ThreePType::_angular_reduced_) return move(unique_ptr<ThreePointCorrelation_angular_reduced>(new ThreePointCorrelation_angular_reduced(data, random, r12, r12_binSize, r13, r13_binSize, nbins)));
84 
85  if (type==ThreePType::_comoving_connected_) return move(unique_ptr<ThreePointCorrelation_comoving_connected>(new ThreePointCorrelation_comoving_connected(data, random, tripletType, r12, r12_binSize, r13, r13_binSize, nbins)));
86 
87  if (type==ThreePType::_comoving_reduced_) return move(unique_ptr<ThreePointCorrelation_comoving_reduced>(new ThreePointCorrelation_comoving_reduced(data, random, tripletType, r12, r12_binSize, r13, r13_binSize, nbins)));
88 
89  else ErrorCBL("no such type of object!", "Create", "ThreePointCorrelation.cpp");
90 
91  return NULL;
92 }
93 
94 
95 // ============================================================================
96 
97 
98 std::shared_ptr<ThreePointCorrelation> cbl::measure::threept::ThreePointCorrelation::Create (const catalogue::Catalogue data, const catalogue::Catalogue random, const double r12Min, const double r12Max, const double r13Min, const double r13Max, const int nOrders, const double split, const int seed)
99 {
100  return move(unique_ptr<ThreePointCorrelation_comoving_multipoles_single>(new ThreePointCorrelation_comoving_multipoles_single(data, random, r12Min, r12Max, r13Min, r13Max, nOrders, split, seed)));
101 }
102 
103 
104 // ============================================================================
105 
106 
107 std::shared_ptr<ThreePointCorrelation> cbl::measure::threept::ThreePointCorrelation::Create (const catalogue::Catalogue data, const catalogue::Catalogue random, const double rMin, const double rMax, const double binSize, const int nOrders, const double split, const int seed)
108 {
109  return move(unique_ptr<ThreePointCorrelation_comoving_multipoles_all>(new ThreePointCorrelation_comoving_multipoles_all(data, random, rMin, rMax, binSize, nOrders, split, seed)));
110 }
111 
112 
113 // ============================================================================
114 
115 
116 void cbl::measure::threept::ThreePointCorrelation::count_triplets (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChainMesh_rMAX1, const ChainMesh_Catalogue &ChainMesh_rMAX2, std::shared_ptr<Triplet> tt, const bool tcount)
117 {
118  time_t start; time (&start);
119 
120  int nObj = cat1->nObjects();
121 
122  float fact_count = 100./nObj;
123 
124  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
125 
126  shared_ptr<Catalogue> cat2 = ChainMesh_rMAX1.catalogue();
127  shared_ptr<Catalogue> cat3 = ChainMesh_rMAX2.catalogue();
128 
129  double r12_min = tt->r12()-0.5*tt->r12_binSize();
130  double r12_max = tt->r12()+0.5*tt->r12_binSize();
131  double r13_min = tt->r13()-0.5*tt->r13_binSize();
132  double r13_max = tt->r13()+0.5*tt->r13_binSize();
133 
134  int tid = 0;
135 #pragma omp parallel private(tid)
136  {
137  tid = omp_get_thread_num();
138 
139  // if (tid == 0) coutCBL << "Number of threads = " << omp_get_num_threads() << endl;
140 
141  // internal object used by each thread to handle triplets
142  shared_ptr<Triplet> tt_thread = move(Triplet::Create(tt->tripletType(), tt->r12(), tt->r12_binSize(), tt->r13(), tt->r13_binSize(), tt->nbins()));
143 
144 #pragma omp for schedule(static, 2)
145  for (int i=0; i<nObj; i++) { // loop on the objects of the catalogue
146 
147  // get the indexes of the objects at r12
148  vector<long> close_objects12 = ChainMesh_rMAX1.close_objects(cat1->coordinate(i), -1);
149 
150  // get the indexes of objects at r13
151  vector<long> close_objects13 = ChainMesh_rMAX2.close_objects(cat1->coordinate(i), -1);
152 
153  vector<double> x2, y2, z2, r12, w2, x3, y3, z3, r13, w3;
154 
155  for (auto &&j : close_objects12) { // loop on the objects at r12
156 
157  double rr = cat1->distance(i, cat2->catalogue_object(j));
158  if (r12_min<rr && rr<r12_max){
159  x2.push_back(cat2->xx(j));
160  y2.push_back(cat2->yy(j));
161  z2.push_back(cat2->zz(j));
162  r12.push_back(rr);
163  w2.push_back(cat2->weight(j));
164  }
165  }
166 
167  for (auto &&k : close_objects13) { // loop on the objects at r13
168  double rr = cat1->distance(i, cat3->catalogue_object(k));
169  if (r13_min<rr && rr<r13_max){
170  x3.push_back(cat3->xx(k));
171  y3.push_back(cat3->yy(k));
172  z3.push_back(cat3->zz(k));
173  r13.push_back(rr);
174  w3.push_back(cat3->weight(k));
175  }
176  }
177 
178  for (size_t j=0; j<r12.size(); j++)
179  for (size_t k=0; k<r13.size(); k++){
180  double r23 = sqrt( pow(x2[j]-x3[k],2)+pow(y2[j]-y3[k],2)+pow(z2[j]-z3[k],2));
181  double ww = cat1->weight(i)*w2[j]*w3[k];
182  tt_thread->put(r12[j], r13[k], r23, ww);
183  }
184 
185  time_t end_temp; time (&end_temp); double diff_temp = difftime(end_temp, start);
186  if (tcount && tid==0) { coutCBL <<"\r..."<<float(i)*fact_count<<"% completed ("<<diff_temp/60<<" minutes)\r"; cout.flush(); }
187  if (i==int(nObj*0.25)) coutCBL <<".....25% completed "<<endl;
188  if (i==int(nObj*0.5)) coutCBL <<".....50% completed "<<endl;
189  if (i==int(nObj*0.75)) coutCBL <<".....75% completed "<<endl;
190  }
191 
192 #pragma omp critical
193  {
194  // sum all the object triplets computed by each thread
195  tt->Sum(tt_thread);
196  }
197 
198  }
199 
200 
201  time_t end; time (&end);
202  double diff = difftime(end,start);
203  if (diff<3600) coutCBL <<" time spent to count the triplets: "<<diff/60<<" minutes"<<endl<<endl;
204  else coutCBL <<" time spent to count the triplets: "<<diff/3600<<" hours"<<endl<<endl;
205 
206  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(6);
207 }
208 
209 
210 // ============================================================================
211 
212 
213 void cbl::measure::threept::ThreePointCorrelation::count_allTriplets (const std::string dir_output_triplets, const std::vector<std::string> dir_input_triplets, const bool count_ddd, const bool count_rrr, const bool count_ddr, const bool count_drr, const bool tcount, const double fact)
214 {
215  // ----- double chain-mesh -----
216 
217  double rMAX1 = m_ddd->r12()+m_ddd->r12_binSize();
218  double rMAX2 = m_ddd->r13()+m_ddd->r13_binSize();
219 
220  double cell_size1 = max(5., rMAX1*fact);
221  double cell_size2 = max(5., rMAX2*fact);
222 
223  ChainMesh_Catalogue ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2;
224 
225  if (count_ddd || count_ddr || count_drr) {
226  auto data_rMAX1 = make_shared<catalogue::Catalogue>(*m_data);
227  auto data_rMAX2 = make_shared<catalogue::Catalogue>(*m_data);
228  ChainMesh_data_rMAX1.set_par(cell_size1, data_rMAX1, rMAX1);
229  ChainMesh_data_rMAX2.set_par(cell_size2, data_rMAX2, rMAX2);
230  }
231  if (count_rrr || count_ddr || count_drr) {
232  auto random_rMAX1 = make_shared<catalogue::Catalogue>(*m_random);
233  auto random_rMAX2 = make_shared<catalogue::Catalogue>(*m_random);
234  ChainMesh_random_rMAX1.set_par(cell_size1, random_rMAX1, rMAX1);
235  ChainMesh_random_rMAX2.set_par(cell_size2, random_rMAX2, rMAX2);
236  }
237 
238  // ----------- count the number of triplets -----------
239 
240  string file;
241 
242  coutCBL << par::col_green << "data-data-data" << par::col_default << endl;
243 
244  file = "ddd.dat";
245 
246  if (count_ddd) {
247  count_triplets(m_data, ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, m_ddd, tcount);
248  if (dir_output_triplets!=par::defaultString) write_triplets(m_ddd, dir_output_triplets, file);
249  }
250  else read_triplets (m_ddd, dir_input_triplets, file);
251 
252 
253  coutCBL << par::col_green << "random-random-random" << par::col_default << endl;
254 
255  file = "rrr.dat";
256 
257  if (count_rrr==1) {
258  count_triplets(m_random, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2, m_rrr, tcount);
259  if (dir_output_triplets!=par::defaultString) write_triplets(m_rrr, dir_output_triplets, file);
260  }
261  else if (count_rrr==0) read_triplets (m_rrr, dir_input_triplets, file);
262 
263 
264  coutCBL << par::col_green << "data-data-random" << par::col_default << endl;
265 
266  file = "ddr.dat";
267 
268  if (count_ddr) {
269 
270  shared_ptr<Triplet> ddr1 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
271  shared_ptr<Triplet> ddr2 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
272  shared_ptr<Triplet> ddr3 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
273 
274  count_triplets(m_data, ChainMesh_data_rMAX1, ChainMesh_random_rMAX2, ddr1, tcount);
275  count_triplets(m_data, ChainMesh_random_rMAX1, ChainMesh_data_rMAX2, ddr2, tcount);
276  count_triplets(m_random, ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, ddr3, tcount);
277 
278  m_ddr->Sum(ddr1); m_ddr->Sum(ddr2); m_ddr->Sum(ddr3);
279 
280  if (dir_output_triplets!=par::defaultString) write_triplets (m_ddr, dir_output_triplets, file);
281  }
282 
283  else read_triplets(m_ddr, dir_input_triplets, file);
284 
285 
286  coutCBL << par::col_green << "data-random-random" << par::col_default << endl;
287 
288  file = "drr.dat";
289 
290  if (count_drr) {
291 
292  shared_ptr<Triplet> drr1 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
293  shared_ptr<Triplet> drr2 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
294  shared_ptr<Triplet> drr3 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
295 
296  count_triplets(m_random, ChainMesh_random_rMAX1, ChainMesh_data_rMAX2, drr1, tcount);
297  count_triplets(m_random, ChainMesh_data_rMAX1, ChainMesh_random_rMAX2, drr2, tcount);
298  count_triplets(m_data, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2, drr3, tcount);
299 
300  m_drr->Sum(drr1); m_drr->Sum(drr2); m_drr->Sum(drr3);
301 
302  if (dir_output_triplets!=par::defaultString) write_triplets(m_drr, dir_output_triplets, file);
303  }
304 
305  else
306  read_triplets (m_drr, dir_input_triplets, file);
307 }
308 
309 
310 // ============================================================================
311 
312 
313 void cbl::measure::threept::ThreePointCorrelation::count_triplets_region (const std::shared_ptr<Catalogue> cat1, const ChainMesh_Catalogue &ChainMesh_rMAX1, const ChainMesh_Catalogue &ChainMesh_rMAX2, std::shared_ptr<Triplet> tt, std::vector<std::shared_ptr<Triplet>> tt_region, const std::vector<std::vector<double>> weight, const bool tcount)
314 {
315  time_t start; time (&start);
316 
317  int nObj = cat1->nObjects();
318 
319  float fact_count = 100./nObj;
320 
321  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
322 
323  shared_ptr<Catalogue> cat2 = ChainMesh_rMAX1.catalogue();
324  shared_ptr<Catalogue> cat3 = ChainMesh_rMAX2.catalogue();
325 
326  double r12_min = tt->r12()-0.5*tt->r12_binSize();
327  double r12_max = tt->r12()+0.5*tt->r12_binSize();
328  double r13_min = tt->r13()-0.5*tt->r13_binSize();
329  double r13_max = tt->r13()+0.5*tt->r13_binSize();
330 
331  int tid = 0;
332 #pragma omp parallel private(tid)
333  {
334  tid = omp_get_thread_num();
335 
336  // if (tid == 0) coutCBL << "Number of threads = " << omp_get_num_threads() << endl;
337 
338  // internal object used by each thread to handle triplets
339  shared_ptr<Triplet> tt_thread = move(Triplet::Create(tt->tripletType(), tt->r12(), tt->r12_binSize(), tt->r13(), tt->r13_binSize(), tt->nbins()));
340  vector<shared_ptr<Triplet> > tt_region_thread(tt_region.size());
341 
342  for (size_t i=0; i<tt_region.size(); ++i)
343  tt_region_thread[i] = move(Triplet::Create(tt->tripletType(), tt->r12(), tt->r12_binSize(), tt->r13(), tt->r13_binSize() , tt->nbins()));
344 
345 #pragma omp for schedule(static, 2)
346  for (int i=0; i<nObj; i++) { // loop on the objects of the catalogue
347 
348  int reg1 = cat1->region(i);
349 
350  // get the indexes of the objects at r12
351  vector<long> close_objects12 = ChainMesh_rMAX1.close_objects(cat1->coordinate(i), -1);
352 
353  // get the indexes of objects at r13
354  vector<long> close_objects13 = ChainMesh_rMAX2.close_objects(cat1->coordinate(i), -1);
355 
356  vector<double> x2, y2, z2, r12, w2, x3, y3, z3, r13, w3;
357  vector<long> reg2, reg3;
358 
359  for (auto &&j : close_objects12) { // loop on the objects at r12
360  double rr = cat1->distance(i, cat2->catalogue_object(j));
361 
362  if (r12_min<rr && rr<r12_max) {
363  x2.push_back(cat2->xx(j));
364  y2.push_back(cat2->yy(j));
365  z2.push_back(cat2->zz(j));
366  r12.push_back(rr);
367  w2.push_back(cat2->weight(j));
368  reg2.push_back(cat2->region(j));
369  }
370  }
371 
372  for (auto &&k : close_objects13) { // loop on the objects at r12
373  double rr = cat1->distance(i, cat3->catalogue_object(k));
374 
375  if (r13_min<rr && rr<r13_max) {
376  x3.push_back(cat3->xx(k));
377  y3.push_back(cat3->yy(k));
378  z3.push_back(cat3->zz(k));
379  r13.push_back(rr);
380  w3.push_back(cat3->weight(k));
381  reg3.push_back(cat3->region(k));
382  }
383  }
384 
385  for (size_t j=0; j<r12.size(); j++)
386  for (size_t k=0; k<r13.size(); k++){
387  int klin;
388  double r23 = sqrt( pow(x2[j]-x3[k],2)+pow(y2[j]-y3[k],2)+pow(z2[j]-z3[k],2));
389  double ww = cat1->weight(i)*w2[j]*w3[k];
390  tt_thread->get_triplet(r12[j], r13[k], r23, klin);
391  tt_thread->set_triplet(klin, ww);
392  for (size_t r=0; r<weight.size(); r++) {
393  double region_weight = weight[r][reg1]*weight[r][reg2[j]]*weight[r][reg3[k]];
394  tt_region_thread[r]->set_triplet(klin, ww*region_weight);
395  }
396  }
397  time_t end_temp; time (&end_temp); double diff_temp = difftime(end_temp, start);
398  if (tcount && tid==0) { coutCBL <<"\r..."<<float(i)*fact_count<<"% completed ("<<diff_temp/60<<" minutes)\r"; cout.flush(); }
399  if (i==int(nObj*0.25)) coutCBL <<".....25% completed "<<endl;
400  if (i==int(nObj*0.5)) coutCBL <<".....50% completed "<<endl;
401  if (i==int(nObj*0.75)) coutCBL <<".....75% completed "<<endl;
402  }
403 
404 #pragma omp critical
405  {
406  // sum all the object triplets computed by each thread
407  tt->Sum(tt_thread);
408 
409  for (size_t k=0; k< weight.size(); k++)
410  tt_region[k]->Sum(tt_region_thread[k]);
411 
412  }
413 
414  }
415 
416 
417  time_t end; time (&end);
418  double diff = difftime(end,start);
419  if (diff<3600) coutCBL <<" time spent to count the triplets: "<<diff/60<<" minutes"<<endl<<endl;
420  else coutCBL <<" time spent to count the triplets: "<<diff/3600<<" hours"<<endl<<endl;
421 
422  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(6);
423 }
424 
425 
426 // ============================================================================
427 
428 
429 void cbl::measure::threept::ThreePointCorrelation::count_allTriplets_region (const std::vector<std::vector<double>> weight, const std::string dir_output_triplets, const std::vector<std::string> dir_input_triplets, const bool count_ddd, const bool count_rrr, const bool count_ddr, const bool count_drr, const bool tcount, const double fact)
430 {
431  // ----- double chain-mesh -----
432 
433  double rMAX1 = m_ddd->r12()+m_ddd->r12_binSize();
434  double rMAX2 = m_ddd->r13()+m_ddd->r13_binSize();
435 
436  double cell_size1 = max(5., rMAX1*fact);
437  double cell_size2 = max(5., rMAX2*fact);
438 
439  ChainMesh_Catalogue ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2;
440 
441  if (count_ddd || count_ddr || count_drr) {
442  auto data_rMAX1 = make_shared<catalogue::Catalogue>(*m_data);
443  auto data_rMAX2 = make_shared<catalogue::Catalogue>(*m_data);
444  ChainMesh_data_rMAX1.set_par(cell_size1, data_rMAX1, rMAX1);
445  ChainMesh_data_rMAX2.set_par(cell_size2, data_rMAX2, rMAX2);
446  }
447  if (count_rrr || count_ddr || count_drr) {
448  auto random_rMAX1 = make_shared<catalogue::Catalogue>(*m_random);
449  auto random_rMAX2 = make_shared<catalogue::Catalogue>(*m_random);
450  ChainMesh_random_rMAX1.set_par(cell_size1, random_rMAX1, rMAX1);
451  ChainMesh_random_rMAX2.set_par(cell_size2, random_rMAX2, rMAX2);
452  }
453 
454  // ----------- initialize the triplet vectors used for resampling -----------
455 
456  m_ddd_regions.erase(m_ddd_regions.begin(), m_ddd_regions.end());
457  m_rrr_regions.erase(m_rrr_regions.begin(), m_rrr_regions.end());
458  m_ddr_regions.erase(m_ddr_regions.begin(), m_ddr_regions.end());
459  m_drr_regions.erase(m_drr_regions.begin(), m_drr_regions.end());
460 
461  int nResamplings = weight.size();
462 
463  for (int i=0; i<nResamplings; ++i) {
464  m_ddd_regions.push_back(move(Triplet::Create(m_ddd->tripletType(), m_ddd->r12(), m_ddd->r12_binSize(), m_ddd->r13(), m_ddd->r13_binSize(), m_ddd->nbins())));
465  m_rrr_regions.push_back(move(Triplet::Create(m_rrr->tripletType(), m_rrr->r12(), m_rrr->r12_binSize(), m_rrr->r13(), m_rrr->r13_binSize(), m_rrr->nbins())));
466  m_ddr_regions.push_back(move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins())));
467  m_drr_regions.push_back(move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins())));
468  }
469 
470  // ----------- count the number of triplets -----------
471 
472  string file;
473 
474  coutCBL << par::col_green << "data-data-data" << par::col_default << endl;
475 
476  file = "ddd.dat";
477 
478  if (count_ddd) {
479  count_triplets_region(m_data, ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, m_ddd, m_ddd_regions, weight, tcount);
480  if (dir_output_triplets!=par::defaultString) write_triplets(m_ddd, dir_output_triplets, file);
481  }
482  else read_triplets (m_ddd, dir_input_triplets, file);
483 
484 
485  coutCBL << par::col_green << "random-random-random" << par::col_default << endl;
486 
487  file = "rrr.dat";
488 
489  if (count_rrr==1) {
490  count_triplets_region(m_random, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2, m_rrr, m_rrr_regions, weight, tcount);
491  if (dir_output_triplets!=par::defaultString) write_triplets(m_rrr, dir_output_triplets, file);
492  }
493  else if (count_rrr==0) read_triplets (m_rrr, dir_input_triplets, file);
494 
495 
496  coutCBL << par::col_green << "data-data-random" << par::col_default << endl;
497 
498  file = "ddr.dat";
499 
500  if (count_ddr) {
501 
502  shared_ptr<Triplet> ddr1 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
503  shared_ptr<Triplet> ddr2 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
504  shared_ptr<Triplet> ddr3 = move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins()));
505 
506  vector<shared_ptr<Triplet>> ddr1_regions;
507  vector<shared_ptr<Triplet>> ddr2_regions;
508  vector<shared_ptr<Triplet>> ddr3_regions;
509 
510  for (int i=0; i<nResamplings; i++) {
511  ddr1_regions.push_back(move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins())));
512  ddr2_regions.push_back(move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins())));
513  ddr3_regions.push_back(move(Triplet::Create(m_ddr->tripletType(), m_ddr->r12(), m_ddr->r12_binSize(), m_ddr->r13(), m_ddr->r13_binSize(), m_ddr->nbins())));
514  }
515 
516  count_triplets_region(m_data, ChainMesh_data_rMAX1, ChainMesh_random_rMAX2, ddr1, ddr1_regions, weight, tcount);
517  count_triplets_region(m_data, ChainMesh_random_rMAX1, ChainMesh_data_rMAX2, ddr2, ddr2_regions, weight,tcount);
518  count_triplets_region(m_random, ChainMesh_data_rMAX1, ChainMesh_data_rMAX2, ddr3, ddr3_regions, weight, tcount);
519 
520  m_ddr->Sum(ddr1); m_ddr->Sum(ddr2); m_ddr->Sum(ddr3);
521 
522  for (int i=0; i<nResamplings; i++) {
523  m_ddr_regions[i]->Sum(ddr1_regions[i]);
524  m_ddr_regions[i]->Sum(ddr2_regions[i]);
525  m_ddr_regions[i]->Sum(ddr3_regions[i]);
526  }
527 
528  if (dir_output_triplets!=par::defaultString) write_triplets (m_ddr, dir_output_triplets, file);
529  }
530 
531  else read_triplets(m_ddr, dir_input_triplets, file);
532 
533 
534  coutCBL << par::col_green << "data-random-random" << par::col_default << endl;
535 
536  file = "drr.dat";
537 
538  if (count_drr) {
539 
540  shared_ptr<Triplet> drr1 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
541  shared_ptr<Triplet> drr2 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
542  shared_ptr<Triplet> drr3 = move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins()));
543 
544  vector<shared_ptr<Triplet>> drr1_regions;
545  vector<shared_ptr<Triplet>> drr2_regions;
546  vector<shared_ptr<Triplet>> drr3_regions;
547 
548  for (int i=0; i<nResamplings; i++) {
549  drr1_regions.push_back(move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins())));
550  drr2_regions.push_back(move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins())));
551  drr3_regions.push_back(move(Triplet::Create(m_drr->tripletType(), m_drr->r12(), m_drr->r12_binSize(), m_drr->r13(), m_drr->r13_binSize(), m_drr->nbins())));
552  }
553 
554  count_triplets_region(m_random, ChainMesh_random_rMAX1, ChainMesh_data_rMAX2, drr1, drr1_regions, weight, tcount);
555  count_triplets_region(m_random, ChainMesh_data_rMAX1, ChainMesh_random_rMAX2, drr2, drr1_regions, weight, tcount);
556  count_triplets_region(m_data, ChainMesh_random_rMAX1, ChainMesh_random_rMAX2, drr3, drr1_regions, weight, tcount);
557 
558  m_drr->Sum(drr1); m_drr->Sum(drr2); m_drr->Sum(drr3);
559 
560  for (int i=0; i<nResamplings; i++) {
561  m_drr_regions[i]->Sum(drr1_regions[i]);
562  m_drr_regions[i]->Sum(drr2_regions[i]);
563  m_drr_regions[i]->Sum(drr3_regions[i]);
564  }
565 
566  if (dir_output_triplets!=par::defaultString) write_triplets(m_drr, dir_output_triplets, file);
567  }
568 
569  else
570  read_triplets (m_drr, dir_input_triplets, file);
571 }
572 
573 
574 // ============================================================================
575 
576 
577 void cbl::measure::threept::ThreePointCorrelation::write_triplets (std::shared_ptr<triplets::Triplet> TT, const std::string dir, const std::string file) const
578 {
579  string MK = "mkdir -p "+dir;
580  if (system (MK.c_str())) {}
581 
582  string file_out = dir+file;
583  ofstream fout(file_out.c_str()); checkIO(fout, file_out);
584 
585  for (int i=0; i<TT->nbins(); i++)
586  fout << TT->TT1D(i) << endl;
587 
588  fout.clear(); fout.close(); coutCBL << "I wrote the file " << file << endl << endl;
589 }
590 
591 
592 
593 // ============================================================================
594 
595 
596 void cbl::measure::threept::ThreePointCorrelation::read_triplets (std::shared_ptr<triplets::Triplet> TT, const std::vector<std::string> dir, const std::string file)
597 {
598  if (dir.size()==0)
599  ErrorCBL("dir.size()=0!", "read_triplets", "TwoPointCorrelation.cpp");
600 
601  for (size_t dd=0; dd<dir.size(); dd++) {
602 
603  string file_in = dir[dd]+file;
604  coutCBL << "I'm reading the triplet file: " << file_in << endl;
605 
606  ifstream fin(file_in.c_str()); checkIO(fin, file_in);
607 
608  double pp;
609  for (int i=0; i<TT->nbins(); i++) {
610  fin >>pp;
611  TT->add_TT1D(i, pp);
612  }
613 
614  fin.clear(); fin.close(); coutCBL << "I read the file " << file_in << endl;
615  }
616 }
617 
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class ThreePointCorrelation.
The class ThreePointCorrelation_angular_connected.
The class ThreePointCorrelation_angular_reduced.
The class ThreePointCorrelation_comoving_connected.
The class ThreePointCorrelation_comoving_multipoles_all.
The class ThreePointCorrelation_comoving_multipoles_single.
The class ThreePointCorrelation_comoving_reduced.
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_allTriplets_region(const std::vector< std::vector< double >> weight, const std::string dir_output_triplets=par::defaultString, const std::vector< std::string > dir_input_triplets={}, const bool count_ddd=true, const bool count_rrr=true, const bool count_ddr=true, const bool count_drr=true, const bool tcount=false, const double fact=0.1)
count the data-data-data, random-random-random, data-data-random and data-random-random triplets,...
void count_triplets_region(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChainMesh_rMAX1, const chainmesh::ChainMesh_Catalogue &ChainMesh_rMAX2, std::shared_ptr< triplets::Triplet > tt, std::vector< std::shared_ptr< triplets::Triplet >> tt_regions, const std::vector< std::vector< double >> weight, const bool tcount=false)
method to count the number of triplets
static std::shared_ptr< ThreePointCorrelation > Create(const ThreePType type, const catalogue::Catalogue data, const catalogue::Catalogue random, const triplets::TripletType tripletType, const double side_s, const double side_u, const double perc_increase, const int nbins)
static factory used to construct three-point correlation functions of any type
void count_triplets(const std::shared_ptr< catalogue::Catalogue > cat1, const chainmesh::ChainMesh_Catalogue &ChainMesh_rMAX1, const chainmesh::ChainMesh_Catalogue &ChainMesh_rMAX2, std::shared_ptr< triplets::Triplet > tt, const bool tcount=false)
method to count the number of triplets
void write_triplets(const std::shared_ptr< triplets::Triplet > TT, const std::string dir, const std::string file) const
write the number of triplets
void read_triplets(std::shared_ptr< triplets::Triplet > TT, const std::vector< std::string > dir, const std::string file)
read the number of triplets
void count_allTriplets(const std::string dir_output_triplets=par::defaultString, const std::vector< std::string > dir_input_triplets={}, const bool count_ddd=true, const bool count_rrr=true, const bool count_ddr=true, const bool count_drr=true, const bool tcount=false, const double fact=0.1)
count the data-data-data, random-random-random, data-data-random and data-random-random triplets,...
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
ThreePType
the three-point correlation function type
TripletType
the triplet type
Definition: Triplet.h:62
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
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