CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
SubSample.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 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 
34 #include "GlobalFunc.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 
40 
41 // ============================================================================
42 
43 
45 {
46  coutCBL << "I'm putting data and random objects in regions given by R.A.-Dec tiles." << endl;
47 
48  if (nz <= 0)
49  ErrorCBL("nz must be >0.", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
50 
51  // Check if the necessary quantities are properly set
52  for (size_t i=0; i<data.nObjects(); i++) {
53 
54  if (data.isSetVar(i, catalogue::Var::_Region_) == false)
55  ErrorCBL("The tile number for the object "+cbl::conv(i,cbl::par::fINT)+" in the data catalogue is not set.", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
56  if (data.var(i, catalogue::Var::_Region_) < 0)
57  ErrorCBL("The tile number for the object "+cbl::conv(i,cbl::par::fINT)+" in the data catalogue is <0. The tile numbers must be all the integers between 0 and N, where N is the highest tile number.", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
58 
59  }
60 
61  for (size_t i=0; i<random.nObjects(); i++) {
62 
63  if (random.isSetVar(i, catalogue::Var::_Region_) == false)
64  ErrorCBL("The tile number for the object "+cbl::conv(i,cbl::par::fINT)+" in the random catalogue is not set.", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
65  if (random.var(i, catalogue::Var::_Region_) < 0)
66  ErrorCBL("The tile number for the object "+cbl::conv(i,cbl::par::fINT)+" in the random catalogue is <0. The tile numbers must be all the integers between 0 and N, where N is the highest tile number.", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
67 
68  }
69 
70  // Re-set the region numbers, so that they become
71  // all the integers between 0 and N, where N is the
72  // highest number among the regions
73 
74  std::vector<long int> observedRegions (data.nObjects(), -1);
75  std::vector<long int> randomRegions (random.nObjects(), -1);
76 
77  std::vector<bool> ok_data_tile (data.nObjects(), false);
78  std::vector<bool> ok_random_tile (random.nObjects(), false);
79 
80  long int value = 0;
81 
82  for (size_t i=0; i<data.nObjects(); i++) {
83 
84  bool update_value = false;
85 
86  for (size_t j=0; j<data.nObjects(); j++)
87  if (data.region(j) == data.region(i) && ok_data_tile[j] == false) {
88  observedRegions[j] = value;
89  ok_data_tile[j] = true;
90 
91  update_value = true;
92  }
93 
94  for (size_t j=0; j<random.nObjects(); j++)
95  if (random.region(j) == data.region(i) && ok_random_tile[j] == false) {
96  randomRegions[j] = value;
97  ok_random_tile[j] = true;
98  }
99 
100  if (update_value)
101  value ++;
102 
103  }
104 
105  for (size_t i=0; i<random.nObjects(); i++)
106 
107  if (ok_random_tile[i] == false) {
108 
109  const long int new_value = cbl::Max(randomRegions)+1;
110 
111  for (size_t j=0; j<random.nObjects(); j++)
112  if (random.region(j) == random.region(i)) {
113  randomRegions[j] = new_value;
114  ok_random_tile[j] = true;
115  }
116  }
117 
118  // If the number of random regions is higher
119  // than the number of data regions, remove the
120  // random objects in excess
121 
122  if (cbl::Max(randomRegions) > cbl::Max(observedRegions)) {
123 
124  const int original_nObj = random.nObjects();
125 
126  for (int i=0; i<original_nObj; i++) {
127 
128  const int idx = original_nObj-1-i;
129 
130  if ((int)(randomRegions[idx]) > (int)(cbl::Max(observedRegions))) {
131  random.remove_object(idx);
132  randomRegions.erase(randomRegions.begin()+idx);
133  }
134 
135  }
136  }
137 
138  // Divide the samples in redshift sub-regions, by separating
139  // the objects in the same tile but in different redshift regions.
140 
141  if (nz > 1) {
142 
143  const double zMin = data.Min(catalogue::Var::_Redshift_);
144  const double Cell_z = (data.Max(catalogue::Var::_Redshift_)-zMin)/nz;
145 
146  int next_max_tile_number = cbl::Max(observedRegions)+1;
147  std::vector<std::vector<long int>> tile_z_idx (cbl::Max(observedRegions)+1, std::vector<long int>(nz, -1));
148 
149  std::vector<long int> dummy_observedRegions = observedRegions;
150 
151  for (size_t i=0; i<observedRegions.size(); i++) {
152 
153  int z_idx = min(int((data.redshift(i)-zMin)/Cell_z), nz-1);
154 
155  if (z_idx != 0) {
156 
157  if (tile_z_idx[dummy_observedRegions[i]][z_idx] != -1)
158  observedRegions[i] = tile_z_idx[dummy_observedRegions[i]][z_idx];
159 
160  else {
161  observedRegions[i] = next_max_tile_number; // Given the same tile number, if the z cell is different also the region number is different.
162  tile_z_idx[dummy_observedRegions[i]][z_idx] = next_max_tile_number;
163  next_max_tile_number ++;
164  }
165 
166  }
167 
168  }
169 
170  // Reassign the regions to the random objects
171 
172  std::vector<long int> dummy_randomRegions = randomRegions;
173 
174  for (size_t i=0; i<randomRegions.size(); i++) {
175  int z_idx = min(int((random.redshift(i)-zMin)/Cell_z), nz-1);
176  randomRegions[i] = tile_z_idx[dummy_randomRegions[i]][z_idx];
177  }
178 
179  // It might happen that, given a tile, no objects lie in
180  // the first redshift bin. In this way, indices are lost.
181  // In the loop below we recover such indices, i.e. we
182  // "fill the gaps" by changing the existing indices.
183 
184  std::vector<long int> sorted_regions = cbl::different_elements(observedRegions);
185  std::sort(sorted_regions.begin(), sorted_regions.end());
186 
187  std::vector<long int> missed_index;
188  int idx = -1;
189 
190  for (size_t i=0; i<sorted_regions.size(); i++) {
191  idx ++;
192  if ((size_t)(sorted_regions[idx]) != i) {
193  missed_index.emplace_back(i);
194  idx --;
195  }
196  }
197 
198  for (size_t i=0; i<missed_index.size(); i++) {
199 
200  const long int max_idx = sorted_regions[sorted_regions.size()-1];
201 
202  for (size_t j=0; j<observedRegions.size(); j++)
203  if (observedRegions[j] == max_idx)
204  observedRegions[j] = missed_index[i];
205 
206  for (size_t j=0; j<randomRegions.size(); j++)
207  if (randomRegions[j] == max_idx)
208  randomRegions[j] = missed_index[i];
209 
210  sorted_regions.erase(sorted_regions.end()-1);
211 
212  }
213 
214  }
215 
216  for (size_t i=0; i<random.nObjects(); i++)
217  if (randomRegions[i] == -1)
218  ErrorCBL("Some random objects fall in redshift bins that are not populated by any data object!", "set_ObjectRegion_Tiles_Redshift", "GlobalFunc/SubSample.cpp");
219 
220  // Set the regions
221  const int nRegions_data = (int)((cbl::different_elements(observedRegions)).size());
222  const int nRegions_random = (int)((cbl::different_elements(randomRegions)).size());
223 
224  data.set_region(observedRegions, nRegions_data);
225  random.set_region(randomRegions, nRegions_random);
226 
227  cbl::check_regions(data, random);
228 
229  coutCBL << "Done!" << endl;
230 }
231 
232 
233 // ============================================================================
234 
235 
236 void cbl::set_ObjectRegion_SubBoxes (catalogue::Catalogue &data, const int nx, const int ny, const int nz)
237 {
238  const double xMin = data.Min(catalogue::Var::_X_);
239  const double yMin = data.Min(catalogue::Var::_Y_);
240  const double zMin = data.Min(catalogue::Var::_Z_);
241 
242  const double Cell_X = (data.Max(catalogue::Var::_X_)-xMin)/nx;
243  const double Cell_Y = (data.Max(catalogue::Var::_Y_)-yMin)/ny;
244  const double Cell_Z = (data.Max(catalogue::Var::_Z_)-zMin)/nz;
245 
246  vector<long> dataReg(data.nObjects());
247 
248 #pragma omp parallel num_threads(omp_get_max_threads())
249  {
250 
251 #pragma omp for schedule(static, 2)
252  for (size_t i=0; i<data.nObjects(); i++) {
253  const int i1 = min(int((data.xx(i)-xMin)/Cell_X), nx-1);
254  const int j1 = min(int((data.yy(i)-yMin)/Cell_Y), ny-1);
255  const int z1 = min(int((data.zz(i)-zMin)/Cell_Z), nz-1);
256  const int index = z1+nz*(j1+ny*i1);
257  dataReg[i] = index;
258  }
259  }
260 
261  data.set_region(dataReg, nx*ny*nz);
262 }
263 
264 
265 // ============================================================================
266 
267 
268 void cbl::set_ObjectRegion_mangle (catalogue::Catalogue &data, const int nSamples, const std::string polygonfile)
269 {
270  cbl::Path path;
271  string mangle_dir = path.DirCosmo()+"/External/mangle/";
272 
273  string mangle_working_dir = mangle_dir+"output/";
274  string mkdir = "mkdir -p "+mangle_working_dir;
275  if (system(mkdir.c_str())) {}
276 
277  string out_cat = mangle_working_dir+"data.dat";
278  string out_ran = mangle_working_dir+"ran.dat";
279 
280  ofstream fout(out_cat.c_str()); checkIO(fout, out_cat); fout.precision(10);
281 
282  for (size_t i=0; i<data.nObjects(); i++)
283  fout << data.ra(i) << " " << data.dec(i) << endl;
284  fout.clear(); fout.close();
285 
286  string cmd = mangle_dir+"bin/polyid -ur "+polygonfile+" "+out_cat+" "+out_cat+".id";
287  if (system(cmd.c_str())) {}
288 
289  vector<int> poly_data, poly_list;
290 
291  string line;
292  string in_cat = out_cat+".id";
293 
294  ifstream fin(in_cat.c_str()); checkIO(fin, in_cat);
295  getline(fin, line);
296  while (getline(fin, line)) {
297  stringstream ss(line); double NUM; int pp=-100;
298  ss >> NUM;
299  ss >> NUM;
300  ss >> pp;
301  if (pp==-100) ErrorCBL("", "set_ObjectRegion_mangle", "GlobalFunc/SubSample.cpp");
302  poly_data.push_back(pp);
303  }
304  fin.clear(); fin.close();
305 
306  vector<int>::iterator it = poly_list.begin();
307  sort(poly_list.begin(), poly_list.end());
308  it = unique(poly_list.begin(), poly_list.end());
309  poly_list.resize(distance(poly_list.begin(), it));
310 
311  int nPoly = poly_list.size();
312 
313  vector<int> boundaries(nSamples+1, 0);
314  boundaries[0] = Min(poly_list); boundaries[nSamples] = Max(poly_list)+100;
315 
316  for (int i=1; i<nSamples; i++)
317  boundaries[i] = poly_list[i*int(nPoly/(nSamples))];
318 
319  vector<long> dataReg(data.nObjects());
320 
321  for (size_t i=1; i<boundaries.size(); i++) {
322  for (size_t j=0; j<poly_data.size(); j++)
323  if (poly_data[j]>=boundaries[i-1] && poly_data[j] <boundaries[i])
324  dataReg[j] = i-1;
325  }
326 
327  data.set_region(dataReg, nSamples);
328 
329  string RM = "rm -rf "+mangle_working_dir;
330  if (system(RM.c_str())) {}
331 
332 }
333 
334 
335 // ============================================================================
336 
337 
338 void cbl::set_ObjectRegion_SubBoxes (catalogue::Catalogue &data, catalogue::Catalogue &random, const int nx, const int ny, const int nz)
339 {
340  coutCBL << "I'm putting data and random objects in box-sized regions."<<endl;
341 
342  const double xMin = data.Min(catalogue::Var::_X_);
343  const double yMin = data.Min(catalogue::Var::_Y_);
344  const double zMin = data.Min(catalogue::Var::_Z_);
345 
346  const double Cell_X = (data.Max(catalogue::Var::_X_)-xMin)/nx;
347  const double Cell_Y = (data.Max(catalogue::Var::_Y_)-yMin)/ny;
348  const double Cell_Z = (data.Max(catalogue::Var::_Z_)-zMin)/nz;
349 
350  vector<long> dataReg(data.nObjects());
351  vector<long> randReg(random.nObjects());
352 
353 #pragma omp parallel num_threads(omp_get_max_threads())
354  {
355 
356 #pragma omp for schedule(static, 2)
357  for (size_t i=0; i<data.nObjects(); i++) {
358  const int i1 = min(int((data.xx(i)-xMin)/Cell_X), nx-1);
359  const int j1 = min(int((data.yy(i)-yMin)/Cell_Y), ny-1);
360  const int z1 = min(int((data.zz(i)-zMin)/Cell_Z), nz-1);
361  const int index = z1+nz*(j1+ny*i1);
362  dataReg[i] = index;
363  }
364 
365 #pragma omp for schedule(static, 2)
366  for (size_t i=0; i<random.nObjects(); i++) {
367  const int i1 = min(int((random.xx(i)-xMin)/Cell_X), nx-1);
368  const int j1 = min(int((random.yy(i)-yMin)/Cell_Y), ny-1);
369  const int z1 = min(int((random.zz(i)-zMin)/Cell_Z), nz-1);
370  const int index = z1+nz*(j1+ny*i1);
371  randReg[i] = index;
372  }
373  }
374 
375  data.set_region(dataReg, nx*ny*nz);
376  random.set_region(randReg, nx*ny*nz);
377 
378  cbl::check_regions(data, random);
379 
380  coutCBL << "Done!" << endl;
381 }
382 
383 
384 // ============================================================================
385 
386 
387 void cbl::set_ObjectRegion_mangle (catalogue::Catalogue &data, catalogue::Catalogue &random, const int nSamples, const std::string polygonfile)
388 {
389  cbl::Path path;
390  string mangle_dir = path.DirCosmo()+"/External/mangle/";
391 
392  string mangle_working_dir = mangle_dir+"output/";
393  string mkdir = "mkdir -p "+mangle_working_dir;
394  if (system(mkdir.c_str())) {}
395 
396  string out_cat = mangle_working_dir+"data.dat";
397  string out_ran = mangle_working_dir+"ran.dat";
398 
399  ofstream fout(out_cat.c_str()); checkIO(fout, out_cat); fout.precision(10);
400 
401  for (size_t i=0; i<data.nObjects(); i++)
402  fout << data.ra(i) << " " << data.dec(i) << endl;
403  fout.clear(); fout.close();
404 
405  fout.open(out_ran.c_str()); checkIO(fout, out_ran);
406  for (size_t i=0; i<random.nObjects(); i++)
407  fout << random.ra(i) << " " << random.dec(i) << endl;
408  fout.clear(); fout.close();
409 
410  string cmd = mangle_dir+"bin/polyid -ur "+polygonfile+" "+out_cat+" "+out_cat+".id";
411  if (system(cmd.c_str())) {}
412  cmd = mangle_dir+"bin/polyid -ur "+polygonfile+" "+out_ran+" "+out_ran+".id";
413  if (system(cmd.c_str())) {}
414 
415  vector<int> poly_data, poly_random, poly_list;
416 
417  string line;
418  string in_cat = out_cat+".id";
419  string in_ran = out_ran+".id";
420 
421  ifstream fin(in_cat.c_str()); checkIO(fin, in_cat);
422  getline(fin, line);
423  while (getline(fin, line)) {
424  stringstream ss(line); double NUM; int pp=-100;
425  ss >> NUM;
426  ss >> NUM;
427  ss >> pp;
428  if (pp==-100) ErrorCBL("", "set_ObjectRegion_mangle", "GlobalFunc/SubSample.cpp");
429  poly_data.push_back(pp);
430  }
431  fin.clear(); fin.close();
432 
433  fin.open(in_ran.c_str()); checkIO(fin, in_ran);
434  getline(fin, line);
435  while (getline(fin, line)) {
436  stringstream ss(line); double NUM; int pp = -100;
437  ss >> NUM;
438  ss >> NUM;
439  ss >> pp;
440  if (pp==-100) ErrorCBL("", "set_ObjectRegion_mangle", "GlobalFunc/SubSample.cpp");
441  poly_random.push_back(pp);
442  poly_list.push_back(pp);
443  }
444  fin.clear(); fin.close();
445 
446  vector<int>::iterator it = poly_list.begin();
447  sort(poly_list.begin(), poly_list.end());
448  it = unique(poly_list.begin(), poly_list.end());
449  poly_list.resize(distance(poly_list.begin(), it));
450 
451  int nPoly = poly_list.size();
452 
453  vector<int> boundaries(nSamples+1, 0);
454  boundaries[0] = Min(poly_list); boundaries[nSamples] = Max(poly_list)+100;
455 
456  for (int i=1; i<nSamples; i++)
457  boundaries[i] = poly_list[i*int(nPoly/(nSamples))];
458 
459  vector<long> dataReg(data.nObjects());
460  vector<long> randReg(random.nObjects());
461 
462  for (size_t i=1; i<boundaries.size(); i++) {
463  for (size_t j=0; j<poly_data.size(); j++)
464  if (poly_data[j]>=boundaries[i-1] && poly_data[j] <boundaries[i])
465  dataReg[j] = i-1;
466 
467  for (size_t j=0; j<poly_random.size(); j++)
468  if (poly_random[j]>=boundaries[i-1] && poly_random[j]<boundaries[i])
469  randReg[j] = i-1;
470  }
471 
472  string RM = "rm -rf "+mangle_working_dir;
473  if (system(RM.c_str())) {}
474 
475  data.set_region(dataReg, nSamples);
476  random.set_region(randReg, nSamples);
477 
478  cbl::check_regions(data, random);
479 }
480 
481 // ============================================================================
482 
483 
484 vector<double> cbl::colatitude (std::vector<double> latitude)
485 {
486  vector<double> colatitude(latitude.size());
487 
488  for (size_t i=0; i<latitude.size(); i++)
489  colatitude[i] = cbl::par::pi/2-latitude[i];
490 
491  return colatitude;
492 }
493 
494 
495 // ============================================================================
496 
497 
498 void cbl::set_ObjectRegion_RaDec (catalogue::Catalogue &data, const int nCells_Ra, const int nCells_Dec, const bool use_colatitude)
499 {
500  vector<double> data_x = data.var(catalogue::Var::_RA_);
501  vector<double> data_y = (use_colatitude) ? cbl::colatitude(data.var(catalogue::Var::_Dec_)) : data.var(catalogue::Var::_Dec_);
502  vector<double> cos_data_y(data.nObjects(), 0);
503  for (size_t i=0; i<data.nObjects(); i++)
504  cos_data_y[i] = cos(data_y[i]);
505 
506  double min_ra = Min(data_x);
507  double max_ra = Max(data_x);
508  double deltaRa = (max_ra-min_ra)/nCells_Ra;
509 
510  double min_cdec = Min(cos_data_y);
511  double max_cdec = Max(cos_data_y);
512  double deltaCDec = (max_cdec-min_cdec)/nCells_Dec;
513 
514  int nCells = nCells_Ra*nCells_Dec;
515  double Area = deltaRa*deltaCDec*nCells;
516 
517  coutCBL << "Survey area is: " << Area << endl;
518  coutCBL << "Number of cells will be: " << nCells << endl;
519 
520  vector<long> dataReg(data.nObjects());
521 
522 #pragma omp parallel num_threads(omp_get_max_threads())
523  {
524 
525 #pragma omp for schedule(static, 2)
526  for (size_t i=0; i<data.nObjects(); i++) {
527  int j1 = min(int((cos_data_y[i]-min_cdec)/deltaCDec), nCells_Dec-1);
528  int i1 = min(int((data_x[i]-min_ra)/deltaRa), nCells_Ra-1);
529  dataReg[i] = i1*nCells_Dec+j1;
530  }
531  }
532 
533  data.set_region(dataReg, nCells);
534 }
535 
536 
537 
538 // ============================================================================
539 
540 
541 void cbl::set_ObjectRegion_RaDec (catalogue::Catalogue &data, catalogue::Catalogue &random, const int nCells_Ra, const int nCells_Dec, const bool use_colatitude)
542 {
543  vector<double> data_x = data.var(catalogue::Var::_RA_);
544  vector<double> data_y = (use_colatitude) ? cbl::colatitude(data.var(catalogue::Var::_Dec_)) : data.var(catalogue::Var::_Dec_);
545  vector<double> cos_data_y(data.nObjects(), 0);
546  for (size_t i=0; i<data.nObjects(); i++)
547  cos_data_y[i] = cos(data_y[i]);
548 
549 
550  vector<double> random_x = random.var(catalogue::Var::_RA_);
551  vector<double> random_y = (use_colatitude) ? cbl::colatitude(random.var(catalogue::Var::_Dec_)) : random.var(catalogue::Var::_Dec_);
552  vector<double> cos_random_y(random.nObjects(), 0);
553  for (size_t i=0; i<random.nObjects(); i++)
554  cos_random_y[i] = cos(random_y[i]);
555 
556  double min_ra = Min(random_x);
557  double max_ra = Max(random_x);
558  double deltaRa = (max_ra-min_ra)/nCells_Ra;
559 
560  double min_cdec = Min(cos_random_y);
561  double max_cdec = Max(cos_random_y);
562  double deltaCDec = (max_cdec-min_cdec)/nCells_Dec;
563 
564  int nCells = nCells_Ra*nCells_Dec;
565  double Area = deltaRa*deltaCDec*nCells;
566 
567  coutCBL << "Survey area is: " << Area << endl;
568  coutCBL << "Number of cells will be: " << nCells << endl;
569 
570  vector<long> dataReg(data.nObjects());
571  vector<long> randReg(random.nObjects());
572 
573 #pragma omp parallel num_threads(omp_get_max_threads())
574  {
575 
576 #pragma omp for schedule(static, 2)
577  for (size_t i=0; i<data.nObjects(); i++) {
578  int j1 = min(int((cos_data_y[i]-min_cdec)/deltaCDec), nCells_Dec-1);
579  int i1 = min(int((data_x[i]-min_ra)/deltaRa), nCells_Ra-1);
580  dataReg[i] = i1*nCells_Dec+j1;
581  }
582 
583 #pragma omp for schedule(static, 2)
584  for (size_t i=0; i<random.nObjects(); i++) {
585  int j1 = min(int((cos_random_y[i]-min_cdec)/deltaCDec), nCells_Dec-1);
586  int i1 = min(int((random_x[i]-min_ra)/deltaRa), nCells_Ra-1);
587  randReg[i] = i1*nCells_Dec+j1;
588  }
589  }
590 
591  data.set_region(dataReg, nCells);
592  random.set_region(randReg, nCells);
593 
594  cbl::check_regions(data, random);
595 }
596 
597 // ============================================================================
598 
599 
600 
602 {
603  coutCBL << "Checking if the regions have been assigned correctly..." << endl;
604 
605  // check if data and random catalogues have the same number of regions
606  // nRegions is a "proxy" for the region geometry
607  if (data.nRegions()!=random.nRegions())
608  ErrorCBL("data and random have different number of regions: data_regions.size() = "+conv(data.nRegions(), par::fINT)+", random_regions.size = "+conv(random.nRegions(), par::fINT)+": please set the number of regions through set_region_number() or check your inputs!", "check_regions", "GlobalFunc/SubSample.cpp");
609 
610  size_t nRegions = data.nRegions();
611 
612  // count how many objects fall in the regions
613  vector<long> dataObj_region(nRegions, 0);
614  vector<long> data_region = data.region();
615 
616  for (size_t i=0; i<data.nObjects(); ++i) {
617  checkDim(dataObj_region, data_region[i], "dataObj_region", false);
618  dataObj_region[data_region[i]] ++;
619  }
620 
621  vector<long> randObj_region(nRegions, 0);
622  vector<long> rand_region = random.region();
623  for (size_t i=0; i<random.nObjects(); ++i) {
624  checkDim(randObj_region, rand_region[i], "randObj_region", false);
625  randObj_region[rand_region[i]] ++;
626  }
627 
628  // empty random regions are not allowed!
629  // rearrange regions
630  size_t region_eff = 0;
631  std::map<long, long> region_list_eff;
632  for (size_t i=0; i<nRegions; i++) {
633  if (randObj_region[i]!=0) {
634  region_list_eff.insert(pair<long, long>(i, region_eff));
635  region_eff ++;
636  }
637  else {
638  // throw error if random region is empty and data region is not
639  if (dataObj_region[i]!=0)
640  ErrorCBL("the "+conv(i, par::fINT)+" region is empty in the random sample, but contains "+conv(dataObj_region[i], par::fINT)+" data points; this is not allowed, Please check your inputs!", "check_regions", "GlobalFunc/SubSample.cpp");
641  }
642  }
643 
644  if (region_eff!=nRegions) {
645  coutCBL << "Found "+conv(region_eff, par::fINT)+" non-empty regions" << endl;
646  coutCBL << "Rearranging regions in data and random sample..." << endl;
647  for (size_t i=0; i<data.nObjects(); ++i) {
648  checkDim(data_region, i, "data_region", false);
649  if (int(region_list_eff.size())<data_region[i]) ErrorCBL("the dimension of region_list_eff is: " + conv(region_list_eff.size(), par::fINT) + " ( < " + conv(data_region[i], par::fINT) + " )", "check_regions", "SubSample.cpp");
650  data_region[i] = region_list_eff[data_region[i]];
651  }
652 
653  for (size_t i=0; i<random.nObjects(); ++i) {
654  checkDim(rand_region, i, "rand_region", false);
655  if (int(region_list_eff.size())<rand_region[i]) ErrorCBL("the dimension of region_list_eff is: " + conv(region_list_eff.size(), par::fINT) + " ( < " + conv(rand_region[i], par::fINT) + " )", "check_regions", "SubSample.cpp");
656  rand_region[i] = region_list_eff[rand_region[i]];
657  }
658 
659  data.set_region(data_region, region_eff+1);
660  random.set_region(rand_region, region_eff+1);
661  coutCBL << "Done!" << endl;
662  }
663 
664  coutCBL << "End check regions!" << endl;
665 
666 }
667 
668 
669 // ============================================================================
670 
671 
673 {
674  vector<double> lambda, eta, random_lambda, random_eta;
675  vector<int> stripe, random_stripe, str_u, random_str_u;
676 
677  eq2sdss(data.var(catalogue::Var::_RA_), data.var(catalogue::Var::_Dec_), lambda, eta);
678  sdss_stripe (eta, lambda, stripe, str_u);
679 
680  eq2sdss(random.var(catalogue::Var::_RA_), random.var(catalogue::Var::_Dec_), random_lambda, random_eta);
681  sdss_stripe (random_eta, random_lambda, random_stripe, random_str_u);
682 
683  if (!isDimEqual(str_u, random_str_u))
684  ErrorCBL("data and random catalogues have different stripes!", "set_ObjectRegion_SDSS_stripes", "GlobalFunc/SubSample.cpp");
685 
686  for (size_t i=0; i<data.nObjects(); i++)
687  data.set_var(i, catalogue::Var::_Region_, stripe[i]);
688 
689  for (size_t i=0; i<random.nObjects(); i++)
690  random.set_var(i, catalogue::Var::_Region_, random_stripe[i]);
691 
692  data.set_region_number(str_u.size());
693  random.set_region_number(random_str_u.size());
694 }
Generic functions that use one or more classes of the CosmoBolognaLib.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Path.
Definition: Path.h:145
std::string DirCosmo()
get the directory where the CosmoBolognaLbi are stored
Definition: Path.h:178
The class Catalogue.
Definition: Catalogue.h:654
size_t nRegions()
get the private member m_nRegions
Definition: Catalogue.cpp:396
double yy(const int i) const
get the private member Catalogue::m_object[i]->m_yy
Definition: Catalogue.h:1814
double Min(const Var var_name) const
get the minimum value of a variable of the catalogue objects
Definition: Catalogue.h:2287
size_t nObjects() const
get the number of objects of the catalogue
Definition: Catalogue.h:2264
void set_region_number(const size_t nRegions)
set the private variable m_nRegion
Definition: Catalogue.cpp:944
double dec(const int i) const
get the private member Catalogue::m_object[i]->m_dec
Definition: Catalogue.h:1863
bool isSetVar(const int index, const Var var_name) const
check if the given variable of the i-th object is set
Definition: Catalogue.cpp:740
double xx(const int i) const
get the private member Catalogue::m_object[i]->m_xx
Definition: Catalogue.h:1807
double redshift(const int i) const
get the private member Catalogue::m_object[i]->m_redshift
Definition: Catalogue.h:1891
double ra(const int i) const
get the private member Catalogue::m_object[i]->m_ra
Definition: Catalogue.h:1856
double var(const int index, const Var var_name) const
get the value of the i-th object variable
Definition: Catalogue.cpp:449
double Max(const Var var_name) const
get the maximum value of a variable of the catalogue objects
Definition: Catalogue.h:2295
double zz(const int i) const
get the private member Catalogue::m_object[i]->m_zz
Definition: Catalogue.h:1821
long region(const int i) const
get the private member Catalogue::m_object[i]->m_region
Definition: Catalogue.h:1954
void set_region(const std::vector< long > region, const int nRegions=-1)
set a private variable
Definition: Catalogue.cpp:933
void set_var(const int index, const Var var_name, const double value, const cosmology::Cosmology cosmology={}, const bool update_coordinates=true)
set a private variable
Definition: Catalogue.cpp:978
void remove_object(const int index)
remove an existing object
Definition: Catalogue.h:2699
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const double pi
: the ratio of a circle's circumference to its diameter
Definition: Constants.h:199
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
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
void set_ObjectRegion_RaDec(catalogue::Catalogue &data, const int nCells_Ra, const int nCells_Dec, const bool use_colatitude=true)
set the object region in angular SubBoxes
Definition: SubSample.cpp:498
std::vector< T > different_elements(const std::vector< T > vect_input)
get the unique elements of a std::vector
Definition: Kernel.h:1349
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
void check_regions(catalogue::Catalogue &data, catalogue::Catalogue &random)
check if the subdivision process produced the correct results
Definition: SubSample.cpp:601
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
void set_ObjectRegion_Tiles_Redshift(catalogue::Catalogue &data, catalogue::Catalogue &random, const int nz)
set data and random objects' regions given R.A.-Dec tiles and a number of redshift sub-samples....
Definition: SubSample.cpp:44
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
std::vector< double > colatitude(std::vector< double > latitude)
convert to colatitude
Definition: SubSample.cpp:484
void set_ObjectRegion_SubBoxes(catalogue::Catalogue &data, const int nx, const int ny, const int nz)
set the object region in sub-boxes
Definition: SubSample.cpp:236
void set_ObjectRegion_mangle(catalogue::Catalogue &data, const int nSamples, const std::string polygonfile)
set the object region in sub-regions using mangle
Definition: SubSample.cpp:268
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
bool isDimEqual(const std::vector< T > vect1, const std::vector< T > vect2)
check if the dimensions of two std::vectors are equal
Definition: Kernel.h:1493
void sdss_stripe(const std::vector< double > eta, const std::vector< double > lambda, std::vector< int > &stripe, std::vector< int > &str_u)
compute the SDSS stripe given SDSS coordinates
Definition: Func.cpp:1420
void set_ObjectRegion_SDSS_stripes(catalogue::Catalogue &data, catalogue::Catalogue &random)
set the object region in SDSS stripes
Definition: SubSample.cpp:672
void eq2sdss(const std::vector< double > ra, const std::vector< double > dec, std::vector< double > &lambda, std::vector< double > &eta)
convert from equatorial coordinates R.A., Dec to sdss coordinates
Definition: Func.cpp:1373