CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
RandomCatalogueVIPERS.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 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 "Catalogue.h"
36 
37 using namespace std;
38 
39 using namespace cbl;
40 
41 
42 // ============================================================================
43 
44 
46 
47 cbl::catalogue::Catalogue::Catalogue (const RandomType type, const string WField, const bool isSpectroscopic, const Catalogue catalogue, const Catalogue catalogue_for_nz, const double N_R, const cosmology::Cosmology &cosm, const int step_redshift, const vector<double> lim, const double redshift_min, const double redshift_max, const bool do_convol, const double sigma, const bool use_venice, const bool do_zdistr_with_venice, const string file_random, const string mask, const string pointing_file, const string dir_venice, const int seed)
48 {
49  if (type!=RandomType::_createRandom_VIPERS_) ErrorCBL("the random catalogue has to be of type _VIPERS_ !", "Catalogue", "RandomCatalogueVIPERS.cpp");
50 
51  coutCBL << par::col_green << "I'm creating the random catalogue..." << par::col_default << endl;
52 
53 
54  // ----- compute the redshift distribution of the input (spectroscopic) catalogue -----
55 
56  vector<double> xx, yy, err;
57 
58  cbl::Path path;
59  string file_nz = path.DirLoc()+"/file_nz";
60 
61  if (isSpectroscopic) {
62  vector<double> redshift = catalogue_for_nz.var(Var::_Redshift_);
63  vector<double> weight = catalogue_for_nz.var(Var::_Weight_);
64  const double weightedN = catalogue_for_nz.weightedN();
65  distribution(xx, yy, err, redshift, weight, step_redshift, true, file_nz, weightedN, cbl::Min(redshift), cbl::Max(redshift), "Linear", do_convol, sigma);
66  }
67 
68 
69  // ----- read the pointings -----
70 
71  vector<string> field, pointing;
72 
73  char WW = (WField=="W1") ? '1' : '4';
74 
75  if (isSpectroscopic) {
76 
77  string file_pointings = path.DirCosmo()+"/External/VIPERS/masks/pointings/"+pointing_file;
78  ifstream fin_pointings(file_pointings); checkIO(fin_pointings, file_pointings);
79 
80  string line, Field, Quadrant;
81 
82  while (getline(fin_pointings, line)) {
83  stringstream ss(line);
84  ss >> Field; ss >> Quadrant;
85 
86  if (Field.at(1)==WW || WField=="W1W4") {
87  field.emplace_back(Field);
88  pointing.emplace_back(Field+Quadrant);
89  }
90 
91  }
92 
93  fin_pointings.clear(); fin_pointings.close();
94  }
95 
96 
97  // ----- run venice -----
98 
99  vector<string> file_input;
100 
101  if (use_venice) {
102 
103  vector<string> where = {"inside"};
104  if (!isSpectroscopic) where.push_back("outside");
105 
106  const int nRandom = catalogue.nObjects()*N_R*10.;
107  const string Dir_venice = path.DirCosmo()+"/External/VIPERS/"+dir_venice+"/";
108 
109  // compile venice
110  const string MAKE = /*"make clean -C "+Dir_venice+" && "+*/ "make CC=gcc -C "+Dir_venice+" ; ";
111  coutCBL << endl << "--> " << MAKE << endl << endl;
112  if (system(MAKE.c_str())) {}
113 
114  if (!isSpectroscopic) {
115  for (size_t i=0; i<where.size(); ++i) {
116  file_input.emplace_back(path.DirLoc()+"/temp"+conv(i, par::fINT));
117  string DO = Dir_venice+"venice -m "+mask+" -r -f "+where[i]+" -npart "+conv(nRandom, par::fINT)+" -o "+path.DirLoc()+"/temp"+conv(i, par::fINT);
118  if (do_zdistr_with_venice && isSpectroscopic) DO += " -nz "+file_nz;
119  coutCBL << endl << "--> " << MAKE << endl << endl;
120  if (system(DO.c_str())) {}
121  }
122  }
123 
124  else {
125 
126  for (size_t i=0; i<where.size(); ++i) {
127 
128  file_input.emplace_back(path.DirLoc()+"/temp"+conv(i, par::fINT));
129 
130 
131  // set how many random galaxies put in each pointing
132 
133  random::UniformRandomNumbers_Int random(0, pointing.size(), seed);
134 
135  vector<int> rnd_pnt(pointing.size(), 0);
136 
137  for (int i=0; i<nRandom; ++i)
138  rnd_pnt[random()] ++;
139 
140 
141  // use each single pointing (separately)
142 
143  string filelist, file, Mask;
144 
145  for (size_t mm=0; mm<pointing.size(); ++mm) {
146 
147  file = path.DirLoc()+"/temp"+conv(i, par::fINT)+"_"+conv(mm, par::fINT);
148 
149  Mask = mask+"mask_"+pointing[mm]+".reg";
150 
151  string DO = path.DirCosmo()+"/External/VIPERS/"+dir_venice+"/venice -m "+Mask+" -r -f "+where[i]+" -npart "+conv(rnd_pnt[mm], par::fINT)+" -o "+file;
152  if (do_zdistr_with_venice && isSpectroscopic) DO += " -nz "+file_nz;
153 
154  //coutCBL << endl << "--> " << DO << endl << endl;
155  if (system(DO.c_str())) {}
156 
157  filelist += file+" ";
158 
159 
160  // add the pointing names
161 
162  ifstream fin(file); checkIO(fin, file);
163  string line; vector<string> Line;
164  while (getline(fin, line))
165  if (line.find("#") == string::npos)
166  Line.emplace_back(line);
167  fin.clear(); fin.close();
168 
169  ofstream fout(file); checkIO(fout, file);
170  for (auto &&LL : Line)
171  fout << LL << " " << pointing[mm] << endl;
172  fout.clear(); fout.close();
173 
174  }
175 
176  string Merge = "cat "+filelist+" > "+path.DirLoc()+"/temp"+conv(i, par::fINT)+"; rm -f "+path.DirLoc()+"/temp*_*";
177  if (system(Merge.c_str())) {}
178 
179  }
180  }
181  }
182 
183  else
184  file_input.emplace_back(file_random);
185 
186 
187  // ----- read the random catalogues created by venice -----
188 
189  for (size_t ff=0; ff<file_input.size(); ++ff) {
190 
191  coutCBL << "reading the field " << file_input[ff] << "..." << endl;
192 
193  ifstream fin(file_input[ff].c_str()); checkIO(fin, file_input[ff]);
194  string line;
195  getline(fin, line);
196 
197  double RA, DEC, REDSHIFT;
198  string FIELD;
199 
200  if (do_zdistr_with_venice && isSpectroscopic) { // use venice to extract the redshifts of the random objects
201  while (getline(fin, line))
202  if (line.find("#") == string::npos) { // skip a comment
203  stringstream ss(line);
204  ss >> RA; ss >> DEC; ss >> REDSHIFT; ss >> FIELD;
205  if (redshift_min<REDSHIFT && REDSHIFT<redshift_max && lim[0]<RA && RA<lim[1] && lim[2]<DEC && DEC<lim[3]) {
206  observedCoordinates coord = {RA, DEC, REDSHIFT};
207  m_object.push_back(move(Object::Create(ObjectType::_Random_, coord, CoordinateUnits::_degrees_, cosm, 1., 0, -1, FIELD)));
208  }
209  }
210  }
211 
212  else { // extract the redshifts of the random objects directly
213 
214  vector<double> random_ra, random_dec, random_redshift;
215  vector<string> field;
216 
217  // read R.A., Dec, and the observed field (used for jackknife and bootstrap)
218  while (getline(fin, line))
219  if (line.find("#") == string::npos) { // skip a comment
220  stringstream ss(line);
221  ss >> RA; ss >> DEC; ss >> FIELD;
222  if (lim[0]<RA && RA<lim[1] && lim[2]<DEC && DEC<lim[3]) {
223  random_ra.push_back(RA);
224  random_dec.push_back(DEC);
225  field.emplace_back(FIELD);
226  }
227  }
228 
229  if (isSpectroscopic) {
230  // extract the random redshifts from the distribution
231  vector<double> redshift = catalogue_for_nz.var(Var::_Redshift_);
232  double zmin = max(cbl::Min(redshift), redshift_min);
233  double zmax = min(cbl::Max(redshift), redshift_max);
234  random_redshift = vector_from_distribution(random_ra.size(), xx, yy, zmin, zmax, seed);
235  }
236  else
237  for (size_t i=0; i<random_ra.size(); ++i)
238  random_redshift.emplace_back(1.);
239 
240  // construct the objects
241  for (size_t i=0; i<random_ra.size(); ++i) {
242  observedCoordinates coord = {random_ra[i], random_dec[i], random_redshift[i]};
243  m_object.push_back(move(Object::Create(ObjectType::_Random_, coord, CoordinateUnits::_degrees_, cosm, 1., 0, -1, field[i])));
244  }
245 
246  }
247 
248  fin.clear(); fin.close();
249  }
250 
251 
252  if (isSpectroscopic)
253  computeComovingCoordinates(cosm);
254 
255 
256  // ----- resize the random catalogue to have exactly N_R*catalogue.nObjects() objects -----
257 
258  size_t nFieldsB = nFields();
259 
260  std::shuffle(begin(m_object), end(m_object), default_random_engine{});
261  m_object.resize(N_R*catalogue.nObjects());
262 
263 
264  // ----- check the number of fields -----
265 
266  if (nFields() != nFieldsB)
267  ErrorCBL("nFields() = "+conv(nFields(), par::fINT)+" != nFieldsB = "+conv(nFieldsB, par::fINT)+" --> the random sample should be probably enlarged", "Catalogue", "RandomCatalogueVIPERS.cpp");
268 
269  if (catalogue.nFields()>nFields())
270  ErrorCBL("catalogue.nFields() = "+conv(catalogue.nFields(), par::fINT)+" > nFields = "+conv(nFields(), par::fINT)+" --> the random sample should be probably enlarged", "Catalogue", "RandomCatalogueVIPERS.cpp");
271 
272  coutCBL <<"total number of fields in the catalogue = "<<catalogue.nFields() << endl;
273  coutCBL <<"total number of fields in the random = "<<nFields()<<endl;
274 
275 
276  // ----- remove temporary files -----
277 
278  string RM = "rm -rf "+path.DirLoc()+"/temp* "+file_nz;
279  if (system(RM.c_str())) {}
280 
281 }
282 
The class Catalogue
#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
std::string DirLoc()
get the local directory
Definition: Path.h:185
Catalogue()=default
default constructor
The class Cosmology.
Definition: Cosmology.h:277
The class UniformRandomNumbers_Int.
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 char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
RandomType
the type of random catalogue
Definition: Catalogue.h:345
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void distribution(std::vector< double > &xx, std::vector< double > &fx, std::vector< double > &err, const std::vector< double > FF, const std::vector< double > WW, const int nbin, const bool linear=true, const std::string file_out=par::defaultString, const double fact=1., const double V1=par::defaultDouble, const double V2=par::defaultDouble, const std::string bin_type="Linear", const bool conv=false, const double sigma=0.)
derive and store the number distribution of a given std::vector
Definition: Func.cpp:1654
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 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
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
std::vector< double > vector_from_distribution(const int nRan, const std::vector< double > xx, const std::vector< double > fx, const double xmin, const double xmax, const int seed)
return a std::vector of numbers sampled from a given distribution
Definition: Func.cpp:1455