Logo coherent WaveBurst  
Library Reference Guide
Logo
GWOSC_Tools.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 // this macro is a collection of tools used manage the CWOSC data used for the cwb_gwosc command (see $CWB_SCRIPTS/Makefile.gwosc)
19 // these functions work for the GWOSC GWTC-1 events
20 
21 #include <lal/LALSimInspiral.h>
22 
23 #define MAX_IFOS 3
24 
25 void ConvertSamples(TString ifname, TString ofname, double gps);
26 void Posterior2XML(TString gwname, int seed=-1);
27 void ConvertPSD(TString ifname);
28 
29 void GWOSC_Tools(TString tool, TString spar1="", TString spar2="", double dpar1=0) {
30 
31  if(tool=="ConvertSamples") ConvertSamples(spar1, spar2, dpar1);
32  if(tool=="Posterior2XML") Posterior2XML(spar1,int(dpar1));
33  if(tool=="ConvertPSD") ConvertPSD(spar1);
34 
35 }
36 
37 void ConvertSamples(TString ifname, TString ofname, double gps) {
38 //
39 // this macro is used to convert public samples (IMRPhenomPv2) format to the format used by cWB
40 //
41 // input: ifname: name of input posterior sample file, obtained from the public hdf5 file and converted with h5dump command
42 // eg: h5dump -o GW150914_GWTC-1.txt -y -w 400 GW150914_GWTC-1.hdf5
43 // gps: gps time used to generate the xml file (the gps time is not included in the public hdf5 file)
44 // output: ofname: name of the converted output file
45 //
46 
47  std::map<TString, double> psample;
48  std::vector<TString> pname;
49 
50  TString name="";
51 
52  name="dummy"; psample[name] = 0; pname.push_back(name); // defined to avoid a bug in mdc::Posterior2XML
53 
54  name="time"; psample[name] = gps; pname.push_back(name); // coa time
55 
56  name="costheta_jn"; psample[name] = 0; pname.push_back(name); // simTable->inclination
57  name="distance"; psample[name] = 0; pname.push_back(name); // luminosity distance
58  name="ra"; psample[name] = 0; pname.push_back(name);
59  name="dec"; psample[name] = 0; pname.push_back(name);
60  name="m1"; psample[name] = 0; pname.push_back(name); // mass1 in the detector frame
61  name="m2"; psample[name] = 0; pname.push_back(name); // mass2 in the detector frame
62  name="a1"; psample[name] = 0; pname.push_back(name); // spin1
63  name="a2"; psample[name] = 0; pname.push_back(name); // spin2
64  name="costilt1"; psample[name] = 0; pname.push_back(name); // used to compute spin components s1x,s1y,s1z
65  name="costilt2"; psample[name] = 0; pname.push_back(name); // used to compute spin components s2x,s2y,s2z
66 
67  if(ifname.Contains("GW170817")) {
68  name="lambda1"; psample[name] = 0; pname.push_back(name); // lambda1
69  name="lambda2"; psample[name] = 0; pname.push_back(name); // lambda2
70  }
71 
72  name="q"; psample[name] = 0; pname.push_back(name); // mass ratio
73  name="mc"; psample[name] = 0; pname.push_back(name); // chirp mass
74 
75  name="lal_pnorder"; psample[name] = 8; pname.push_back(name); // pseudoFourPN
76  name="lal_amporder"; psample[name] = 0; pname.push_back(name);
77  name="lal_approximant"; psample[name] = IMRPhenomPv2; pname.push_back(name); // IMRPhenomPv2
78  name="flow"; psample[name] = 20; pname.push_back(name); // simTable->f_lower
79  name="f_ref"; psample[name] = 20; pname.push_back(name); // simTable->f_final
80 
81  name="phase"; psample[name] = 0; pname.push_back(name); // simTable->coa_phase
82  name="psi"; psample[name] = 0; pname.push_back(name); // polarization
83  name="phi12"; psample[name] = 0; pname.push_back(name); // used to compute spin components s1x,s1y,s1z
84  name="phi_jl"; psample[name] = 0; pname.push_back(name); // used to compute spin components s2x,s2y,s2z
85 
86 
87  ifstream in;
88  in.open(ifname.Data(),ios::in);
89  if(!in.good()) {cout << "Error Opening File : " << ifname.Data() << endl;exit(1);}
90 
91  ofstream out;
92  out.open(ofname.Data(),ios::out);
93  if(!out.good()) {cout << "Error Opening File : " << ofname.Data() << endl;exit(1);}
94  out.precision(14);
95 
96  // write header
97  for(int i = 0; i < pname.size(); i++) out << pname[i] << "\t";
98  out << endl;
99 
100  int entries=0;
101  char line[1024];
102  bool data=false;
103  while (1) {
104  in >> line; if(!in.good()) break;
105  if(TString(line).Contains("{")) {
106  in >> line; if(!in.good()) break;
107  data=true;
108  }
109  if(data) {
110  psample["costheta_jn"] = TString(line).Atof(); in >> line; if(!in.good()) break;
111  psample["distance"] = TString(line).Atof(); in >> line; if(!in.good()) break;
112  psample["ra"] = TString(line).Atof(); in >> line; if(!in.good()) break;
113  psample["dec"] = TString(line).Atof(); in >> line; if(!in.good()) break;
114  psample["m1"] = TString(line).Atof(); in >> line; if(!in.good()) break;
115  psample["m2"] = TString(line).Atof(); in >> line; if(!in.good()) break;
116  if(ifname.Contains("GW170817")) {
117  psample["lambda1"] = TString(line).Atof(); in >> line; if(!in.good()) break;
118  psample["lambda2"] = TString(line).Atof(); in >> line; if(!in.good()) break;
119  }
120  psample["a1"] = TString(line).Atof(); in >> line; if(!in.good()) break;
121  psample["a2"] = TString(line).Atof(); in >> line; if(!in.good()) break;
122  psample["costilt1"] = TString(line).Atof(); in >> line; if(!in.good()) break;
123  psample["costilt2"] = TString(line).Atof(); in >> line; if(!in.good()) break;
124 
125  double m1 = psample["m1"];
126  double m2 = psample["m2"];
127  psample["mc"] = pow(m1*m2,3./5.)/pow(m1+m2,1./5.);
128  psample["q"] = m2<m1 ? m2/m1 : m1/m2;
129 
130  // write data
131  for(int i = 0; i < pname.size(); i++) out << psample[pname[i]] << "\t";
132  out << endl;
133 
134  entries++;
135 /*
136  cout << "----------------------------------------------------------" << endl;
137  cout << entries << endl;
138  cout << "----------------------------------------------------------" << endl;
139 
140  for (int i = 0; i < pname.size(); i++) {
141  cout << pname[i] << "\t" << psample[pname[i]] << endl;
142  }
143 */
144  }
145  if(TString(line).Contains("}")) data=false;
146 
147 // if(entries>3) break;
148  }
149 
150  cout << endl << "ConvertSamples.C - converted entries: " << entries << endl;
151 
152  cout << endl << "ConvertSamples.C - created file: " << ofname << endl << endl;
153 
154  out.close();
155  in.close();
156 
157  exit(0);
158 }
159 
160 void Posterior2XML(TString gwname, int seed) {
161 //
162 // extract one sample from the posterior samples file and convert into the xml format
163 // the public files are at: https://dcc.ligo.org/LIGO-P1800370/public
164 // how to download, eg: wget https://dcc.ligo.org/public/0157/P1800370/005/GW150914_GWTC-1.hdf5 -q -O GW150914_GWTC-1.hdf5
165 //
166 // input: gwname: GW name
167 // seed: seed used to randomly select a sample from the gwname+"_GWTC-1.dat" posterior sample file
168 // if <0 then the first sample is selected
169 //
170 
171  gRandom->SetSeed(seed);
172 
173  TString options = "";
174 
175  options += "--ninjections 1 ";
176  options += TString::Format("--seed %d ",seed);
177 
178  options += "--source "+gwname+" ";
179  options += "--waveform IMRPhenomPv2pseudoFourPN ";
180  options += "--f_ref 20 ";
181  options += "--f_lower 20 ";
182 
183  TString posteriorFile = gwname+"_GWTC-1.dat";
184 
185  TString xmlFile = gwname+"_GWTC-1.xml";
186  options += "--clb_file "+gwname+"_GWTC-1.clb ";
187 
188  CWB::mdc::Posterior2XML(posteriorFile, xmlFile, options);
189 
190  exit(0);
191 }
192 
193 void ConvertPSD(TString ifname) {
194 //
195 // convert the GWOSC PSD curves to the format used by the the CWB_Plugin_SimNoise.C plugin
196 // the public files are at: https://dcc.ligo.org/LIGO-P1900011/public
197 // how to download, eg: wget https://dcc.ligo.org/public/0158/P1900011/001/GWTC1_GW150914_PSDs.dat -q -O GWTC1_GW150914_PSDs.dat
198 //
199 // input: ifname: input public PSD file
200 //
201 
202  ifstream in;
203  in.open(ifname.Data(), ios::in);
204  if (!in.good()) {cout << "Error Opening File : " << ifname << endl;exit(1);}
205 
206  int size=0;
207  char str[1024];
208  int fpos=0;
209  while(true) {
210  in.getline(str,1024);
211  if (!in.good()) break;
212  if(str[0] != '#') size++;
213  }
214  cout << "size " << size << endl;
215  in.clear(ios::goodbit);
216  in.seekg(0, ios::beg);
217 
218  in.getline(str,1024); // get header
219  TString header = str;
220 
221  int iH1 = header.Index("LIGO_Hanford_PSD");
222  int iL1 = header.Index("LIGO_Livingston_PSD");
223  int iV1 = header.Index("Virgo_PSD");
224 
225  double iIFO[MAX_IFOS];
226  TString sIFO[MAX_IFOS];
227 
228  int nIFO=0;
229  if(iH1>=0) {iIFO[nIFO]=iH1;sIFO[nIFO]="H1";nIFO++;}
230  if(iL1>=0) {iIFO[nIFO]=iL1;sIFO[nIFO]="L1";nIFO++;}
231  if(iV1>=0) {iIFO[nIFO]=iV1;sIFO[nIFO]="V1";nIFO++;}
232 
233  if(nIFO==0 || nIFO>MAX_IFOS) {cout << "Error: no IFOs in PSDs file or not allowed Format" << endl; exit(1);}
234 
235  double* freq = new double[size];
236  double* psd[MAX_IFOS]; for(int n=0;n<MAX_IFOS;n++) psd[n] = new double[size];
237 
238  int lines=0;
239  while(1) {
240  if(nIFO==1) in >> freq[lines] >> psd[0][lines];
241  if(nIFO==2) in >> freq[lines] >> psd[0][lines] >> psd[1][lines];
242  if(nIFO==3) in >> freq[lines] >> psd[0][lines] >> psd[1][lines] >> psd[2][lines];
243  if (!in.good()) break;
244  size=lines;
245  lines++;
246  }
247  in.close();
248 
249  int idx[3];
250  TMath::Sort(nIFO,iIFO,idx,false);
251 
252  for(int n=0;n<nIFO;n++) {
253  int m=idx[n];
254  TString ofname = ifname;
255  ofname.ReplaceAll("_PSDs.dat","_ASD_"+sIFO[m]+".dat");
256  cout << ofname << endl;
257  ofstream out;
258  out.open(ofname.Data(),ios::out);
259  if(!out.good()) {cout << "Error Opening Output File : " << ofname << endl;exit(1);}
260  out << 0 << "\t" << 10*sqrt(psd[m][0]) << endl; // blind the detector at low frequencies
261  for(int i=0;i<size;i++) out << freq[i] << "\t" << sqrt(psd[m][i]) << endl;
262  out << 8192 << "\t" << 10*sqrt(psd[m][size-1]) << endl; // blind the detector at high frequencies
263  out.close();
264  }
265 
266  exit(0);
267 }
268 
double m1
par [0] name
int n
Definition: cwb_net.C:28
TString("c")
TString posteriorFile
Definition: Posterior2XML.C:33
ofstream out
Definition: cwb_merge.C:214
wavearray< double > psd(33)
TString xmlFile
Definition: Posterior2XML.C:34
Long_t size
int m
Definition: cwb_net.C:28
i drho i
#define MAX_IFOS
Definition: GWOSC_Tools.C:23
#define nIFO
char str[1024]
wavearray< double > freq
Definition: Regression_H1.C:79
void ConvertSamples(TString ifname, TString ofname, double gps)
Definition: GWOSC_Tools.C:37
void GWOSC_Tools(TString tool, TString spar1="", TString spar2="", double dpar1=0)
Definition: GWOSC_Tools.C:29
char options[256]
double gps
ifstream in
double m2
void Posterior2XML(TString gwname, int seed=-1)
Definition: GWOSC_Tools.C:160
char line[1024]
void ConvertPSD(TString ifname)
Definition: GWOSC_Tools.C:193
exit(0)