Logo coherent WaveBurst  
Config Reference Guide
Logo
Create_O3_HV_ChunkSlagRndFiles.C
Go to the documentation of this file.
1 #include <algorithm> // std::random_shuffle
2 #include <vector> // std::vector
3 #include <ctime> // std::time
4 #include <cstdlib> // std::rand, std::srand
5 #include "../../MACROS/ReadChunkList.C"
6 
7 #define SEED 150914
8 
9 #define CHUNK_FILE_LIST "Chunk_List.txt"
10 #define CHUNK_MAX_SIZE 100
11 
12 #define EXECUTE
13 
14 // WARNING!!! this macro contains a fix. The fix is used because the BBH/IMBHB background have been performed (by mistake) using the LH DQ
15 // -> all LH slags not available for HV are removed from the final slag lists
16 
17 int BBH_IMBHB_SLAG_SIZE_MAX_LH[20] = { // slagSizeMax for 20 cWB O3 BBH/IMBHB LH chunks, used by fix
18  2451,
19  2223,
20  2357,
21  2109,
22  2343,
23  2647,
24  2833,
25  2255,
26  2221,
27  2165,
28  2201,
29  2443,
30  2115,
31  2327,
32  2243,
33  2591,
34  2299,
35  2201,
36  2155,
37  2665
38 };
39 
40 void Create_SlagRndFile(TString slags_dir, int chunkID, int slagMax, char* cwb_config_env, char* calibVer, TString search);
41 void shuffle(std::vector<int>& vector, int seed=150914);
42 
43 void Create_O3_HV_ChunkSlagRndFiles(TString slags_dir, TString calibVer, TString calibType, TString search="") {
44 
45  CWB::Toolbox TB;
46 
47  // get CWB_CONFIG
48  char cwb_config_env[1024] = "";
49  if(gSystem->Getenv("CWB_CONFIG")!=NULL) {
50  strcpy(cwb_config_env,TString(gSystem->Getenv("CWB_CONFIG")).Data());
51  }
52 
53  // get O3_GPS_START_CXX, O3_GPS_STOP_CXX
54  TString o3_gps_start_cxx = TString::Format("O3_GPS_START_%s",calibVer.Data());
55  double gps_start=0;
56  if(gSystem->Getenv(o3_gps_start_cxx.Data())!=NULL) {
57  gps_start=TString(gSystem->Getenv(o3_gps_start_cxx.Data())).Atof();
58  } else {
59  cout << "Error: O3_GPS_START_CXX env not defined" << endl;exit(1);
60  }
61  TString o3_gps_stop_cxx = TString::Format("O3_GPS_STOP_%s",calibVer.Data());
62  double gps_stop=0;
63  if(gSystem->Getenv(o3_gps_stop_cxx.Data())!=NULL) {
64  gps_stop=TString(gSystem->Getenv(o3_gps_stop_cxx.Data())).Atof();
65  } else {
66  cout << "Error: O3_GPS_STOP_CXX env not defined" << endl;exit(1);
67  }
68 
69  char chunk_file_list[1024];
70  sprintf(chunk_file_list,"%s/O3/CHUNKS/%s/%s",cwb_config_env,search.Data(),CHUNK_FILE_LIST);
71  cout << chunk_file_list << endl;
72 
73  int chunk[CHUNK_MAX_SIZE];
74  double start[CHUNK_MAX_SIZE];
75  double stop[CHUNK_MAX_SIZE];
76 
77  int nChunks = ReadChunkList(chunk_file_list,chunk,start,stop);
78 
79 
80  int nIFO = 2;
81 
82  char ifo[NIFO_MAX][8];
83  strcpy(ifo[0],"H1");
84  strcpy(ifo[1],"V1");
85 
86  vector<TString> ifos(nIFO);
87  for(int n=0;n<nIFO;n++) ifos[n]=ifo[n];
88 
89  int slagMin = 0; // if slagMax=0 -> slagMin must be < slagMax
90  int slagOff = 0;
91  size_t* slagSite = NULL; // site index starting with 0
92  char* slagFile = NULL; // slag file list
93 
94  //jobs
95  int segLen = 1200;
96 
97  // dq file list
98  // {ifo, dqcat_file, dqcat[0/1/2], shift[sec], inverse[false/true], 4columns[true/false]}
99  int nDQF=10;
100  dqfile DQF[10]={
101 
102  {"H1" ,"", CWB_CAT0, 0., false, false},
103  {"V1" ,"", CWB_CAT0, 0., false, false},
104 
105  {"H1" ,"", CWB_CAT1, 0., true, false},
106  {"V1" ,"", CWB_CAT1, 0., true, false},
107 
108  {"H1" ,"", CWB_CAT2, 0., true, false},
109  {"V1" ,"", CWB_CAT2, 0., true, false},
110 
111  {"H1" ,"", CWB_CAT1, 0., true, false},
112  {"V1" ,"", CWB_CAT1, 0., true, false},
113 
114  {"H1" ,"", CWB_CAT0, 0., false, false},
115  {"V1" ,"", CWB_CAT0, 0., false, false}
116 
117  };
118 
119  sprintf(DQF[0].file, "%s/O3/DATA/%s/DQ/%s/H1_cat0.txt",cwb_config_env,calibVer.Data(),calibType.Data());
120  sprintf(DQF[1].file, "%s/O3/DATA/%s/DQ/%s/V1_cat0.txt",cwb_config_env,calibVer.Data(),calibType.Data());
121 
122  sprintf(DQF[2].file, "%s/O3/DATA/%s/DQ/%s/H1_cat1.txt",cwb_config_env,calibVer.Data(),calibType.Data());
123  sprintf(DQF[3].file, "%s/O3/DATA/%s/DQ/%s/V1_cat1.txt",cwb_config_env,calibVer.Data(),calibType.Data());
124 
125  sprintf(DQF[4].file, "%s/O3/DATA/%s/DQ/%s/H1_cat2.txt",cwb_config_env,calibVer.Data(),calibType.Data());
126  sprintf(DQF[5].file, "%s/O3/DATA/%s/DQ/%s/V1_cat2.txt",cwb_config_env,calibVer.Data(),calibType.Data());
127 
128  sprintf(DQF[6].file, "%s/O3/DATA/%s/DQ/%s/H1_cat4.txt",cwb_config_env,calibVer.Data(),calibType.Data());
129  sprintf(DQF[7].file, "%s/O3/DATA/%s/DQ/%s/V1_cat4.txt",cwb_config_env,calibVer.Data(),calibType.Data());
130 
131 #ifdef EXECUTE
132  char ofile[1024];
133  if(search=="") {
134  sprintf(ofile,"%s/%s/slagSizeMax.lst",slags_dir.Data(),calibType.Data());
135  } else {
136  sprintf(ofile,"%s/%s/slagSizeMax.lst",slags_dir.Data(),search.Data());
137  }
138  cout << ofile << endl;
139  ofstream out;
140  out.open(ofile);
141  if (!out.good()) {cout << "Error Opening File : " << ofile << endl;exit(1);}
142  out << "# chunk slagSize" << endl << endl;
143 
144  char ofile2[1024];
145  if(search=="") {
146  sprintf(ofile2,"%s/%s/liveTimes.lst",slags_dir.Data(),calibType.Data());
147  } else {
148  sprintf(ofile2,"%s/%s/liveTimes.lst",slags_dir.Data(),search.Data());
149  }
150  cout << ofile2 << endl;
151  ofstream out2;
152  out2.open(ofile2);
153  if (!out2.good()) {cout << "Error Opening File : " << ofile2 << endl;exit(1);}
154  out2 << "# chunk liveTime_cat0\tliveTime_cat1\tliveTime_cat2" << endl << endl;
155  // write header
156  char sout2[1024];
157  sprintf(sout2,"%s %*s %*s %*s %*s %*s %*s",
158  "# chunk", 22, "liveTime_cat0(sec)", 22, "liveTime_cat0(days)",
159  22, "liveTime_cat1(sec)", 22, "liveTime_cat1(days)",
160  22, "liveTime_cat2(sec)", 22, "liveTime_cat2(days)");
161  out2 << sout2 << endl;
162 #endif
163  cout << "# chunk slagSize" << endl << endl;
164 
165  for(int k=0;k<nChunks;k++) {
166 
167  if(stop[k]<=gps_start || start[k]>=gps_stop || stop[k]>gps_stop) continue;
168 
169  sprintf(DQF[8].file, "%s/O3/CHUNKS/%s/K%02d.period",cwb_config_env,search.Data(),chunk[k]);
170  sprintf(DQF[9].file, "%s/O3/CHUNKS/%s/K%02d.period",cwb_config_env,search.Data(),chunk[k]);
171 
172  vector<waveSegment> cat0List=TB.readSegList(nDQF, DQF, CWB_CAT0);
173  double live_time_cat0 = CWB::Toolbox::getTimeSegList(cat0List);
174  cout << endl << "live_time_cat0 : " << (int)live_time_cat0 << " (sec) - " << live_time_cat0/(24.*3600.) << " (days)" << endl;
175 
176  vector<waveSegment> cat1List=TB.readSegList(nDQF, DQF, CWB_CAT1);
177  double live_time_cat1 = CWB::Toolbox::getTimeSegList(cat1List);
178  cout << endl << "live_time_cat1 : " << (int)live_time_cat1 << " (sec) - " << live_time_cat1/(24.*3600.) << " (days)" << endl;
179 
180  vector<waveSegment> cat2List=TB.readSegList(nDQF, DQF, CWB_CAT2);
181  double live_time_cat2 = CWB::Toolbox::getTimeSegList(cat2List);
182  cout << endl << "live_time_cat2 : " << (int)live_time_cat2 << " (sec) - " << live_time_cat2/(24.*3600.) << " (days)" << endl << endl;
183 
184 #ifdef EXECUTE
185  sprintf(sout2,"K%02d %*d %*.2f %*d %*.2f %*d %*.2f",
186  chunk[k], 22, (int)live_time_cat0, 22, (live_time_cat0/(24.*3600.)),
187  22, (int)live_time_cat1, 22, (live_time_cat1/(24.*3600.)),
188  22, (int)live_time_cat2, 22, (live_time_cat2/(24.*3600.)));
189  out2 << sout2 << endl;
190 #endif
191 
192  int slagMax=TB.getSlagJobList(cat1List, segLen).size();
193 
194  int slagSizeMax = 2*slagMax+1;
195  if(search=="BBH" || search=="IMBHB") { // apply fix
196  slagSizeMax = TMath::Min(2*slagMax+1,BBH_IMBHB_SLAG_SIZE_MAX_LH[chunk[k]-1]);
197  }
198  char string[64]; sprintf(string,"K%02d\t%d",chunk[k],slagSizeMax);
199  cout << "slagSize = \t" << string << endl;
200 
201  if(search=="") {
202  Create_SlagRndFile(slags_dir, chunk[k], slagMax, cwb_config_env, (char*)calibVer.Data(), (char*)calibType.Data());
203  } else {
204  Create_SlagRndFile(slags_dir, chunk[k], slagMax, cwb_config_env, (char*)calibVer.Data(), (char*)search.Data());
205  }
206 
207 #ifdef EXECUTE
208  out << string << endl;
209 #endif
210 
211  }
212 
213 #ifdef EXECUTE
214  out.close();
215  out2.close();
216 #endif
217 
218  exit(0);
219 }
220 
221 void Create_SlagRndFile(TString slags_dir, int chunkID, int slagMax, char* cwb_config_env, char* calibVer, TString search) {
222 
223  int slagMax_HV = slagMax;
224  if(search=="BBH" || search=="IMBHB") { // apply fix
225  slagMax = (BBH_IMBHB_SLAG_SIZE_MAX_LH[chunkID-1]-1)/2;
226  }
227 
228  std::vector<int> RND(2*slagMax);
229  for(int n=0; n<slagMax; n++) {
230  RND[2*n+0]=+(n+1);
231  RND[2*n+1]=-(n+1);
232  }
233 
234  shuffle(RND,SEED); // shuffle data
235 
236  // check
237  for(int n=0; n<slagMax; n++) {
238  int A = +(n+1);
239  int B = -(n+1);
240  int nA=0;
241  int nB=0;
242  for(int n=0; n<2*slagMax; n++) {
243  if(RND[n]==A) nA++;
244  if(RND[n]==B) nB++;
245  }
246  if(nA!=1 && nB!=1) {
247  cout << "ERROR !!!" << endl;
248  exit(1);
249  }
250  }
251 
252  //for(int n=0; n<2*slagMax; n++) cout << n << "\t" << RND[n] << endl;;
253 
254  char ofile[1024];
255  sprintf(ofile,"%s/%s/K%02d.slags",slags_dir.Data(),search.Data(),chunkID);
256  cout << endl << ofile << endl << endl;
257 
258 #ifdef EXECUTE
259  ofstream out;
260  out.open(ofile);
261  if (!out.good()) {cout << "Error Opening File : " << ofile << endl;exit(1);}
262  out << 0 << "\t" << 0 << "\t" << 0 << endl;
263  for(int n=0; n<2*slagMax; n++) {
264  if(search=="BBH" || search=="IMBHB") { // apply fix
265  if(abs(RND[n])<=slagMax_HV) out << n+1 << "\t" << 0 << "\t" << RND[n] << endl;
266  } else {
267  out << n+1 << "\t" << 0 << "\t" << RND[n] << endl;
268  }
269  }
270  out.close();
271 #endif
272 
273  return;
274 }
275 
276 void shuffle(std::vector<int>& vector, int seed) {
277 
278  TRandom3 rnd3(seed);
279 
280  size_t size = vector.size();
281 
282  if(size > 1) {
283  size_t i;
284  for(size_t i=0; i<size; i++) {
285  size_t j = int(rnd3.Uniform(0,size));
286  int t = vector[j];
287  vector[j] = vector[i];
288  vector[i] = t;
289  }
290  }
291 }
slagMin
slagMax
slagOff
nDQF
segLen
slagFile
nIFO
strcpy(analysis,"2G")
TString calibVer
#define CHUNK_FILE_LIST
#define CHUNK_MAX_SIZE
void Create_O3_HV_ChunkSlagRndFiles(TString slags_dir, TString calibVer, TString calibType, TString search="")
void Create_SlagRndFile(TString slags_dir, int chunkID, int slagMax, char *cwb_config_env, char *calibVer, TString search)
void shuffle(std::vector< int > &vector, int seed=150914)
int BBH_IMBHB_SLAG_SIZE_MAX_LH[20]
int ReadChunkList(TString ifile, int *chunk=NULL, double *start=NULL, double *stop=NULL)
Definition: ReadChunkList.C:22
shift breaksw case t
Definition: cwb_clchunk.csh:85
shift breaksw case n
Definition: cwb_clchunk.csh:75
shift breaksw case B
string search
Definition: cWB_conf.py:63
list ifos
Definition: cWB_conf.py:23