Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_combine_cbc.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 //
19 // This macro implements the combine statistic for the IMBHB and BBH searches
20 // See CWB::Toolbox::CombineCBC for a description of the algorithm
21 //
22 
23 // WARNING: the combine procedure is based:
24 // MDC -> on comparison of the injected times time[nIFO]
25 // the time[nIFO] could be different for BBH and IMBHB
26 // because are the weighted of whitened waveforms.
27 // The estimation could be different for BBH and IMBHB
28 // WAVE -> on comparison of time[0]
29 // The estimation could be different for BBH and IMBHB
30 // The time uncertainty is defined in CWB::mdc::CombineCBC by the parameter
31 // #define TIME_UNCERTAINTY 0.1 // sec
32 
33 {
35 
36  cwb_merge_label = TString(gSystem->Getenv("CWB_MERGE_LABEL"));
37  TString cwb_combine_options = TString(gSystem->Getenv("CWB_COMBINE_OPTIONS"));
38 
40 
41  // check if cwb_combine_options contains '--' than cwb_combine_options is used to extract all combine parameters
49  int cwb_combine_lag = 0;
51  if(cwb_combine_options.Contains("--")) {
52  TString option="";
53  // get the frequency threshold used to combine IMBHB & BBH searches
54  cwb_combine_sfthr = TB.getParameter(cwb_combine_options,"--fthr");
55  // get the ifar threshold used to select zero lag events
56  cwb_combine_sifarthr = TB.getParameter(cwb_combine_options,"--ifarthr");
57  // get the search type (IMBHB or BBH) to be combined
58  cwb_combine_search = TB.getParameter(cwb_combine_options,"--search");
59  // get the working dir of the search to be combined
60  cwb_combine_wdir = TB.getParameter(cwb_combine_options,"--wdir");
61  // get the merge label of the search to be combined
62  cwb_combine_mlabel = TB.getParameter(cwb_combine_options,"--mlabel");
63  // get the user label to be used to tag the combined output wave/mdc files
64  cwb_combine_ulabel = TB.getParameter(cwb_combine_options,"--ulabel");
65  // get the lag used to select background data to be combined
66  TString lag_str = TB.getParameter(cwb_combine_options,"--lag");
67  if(lag_str.IsDigit()) cwb_combine_lag=lag_str.Atoi();
68  // get the slag used to select background data to be combined
69  TString slag_str = TB.getParameter(cwb_combine_options,"--slag");
70  if(slag_str.IsDigit()) cwb_combine_slag=slag_str.Atoi();
71  // get check option (background -> printout zero lag livetimes)
72  cwb_combine_check = TB.getParameter(cwb_combine_options,"--check");
73  cwb_combine_check.ToUpper();
74  if(cwb_combine_check=="") cwb_combine_check = "FALSE";
75  }
76 
77  cout << endl;
78  // check if the check option is correct
79  if(cwb_combine_check!="FALSE" && cwb_combine_check!="TRUE") {
80  cout << endl << "cwb_combine_cbc : Error - check available values are : true/false!!!" << endl << endl;
81  gSystem->Exit(1);
82  }
83  // check if cwb_combine_lag and cwb_combine_slag are>0
84  if(cwb_combine_lag<0 || cwb_combine_slag<0) {
85  cout << "cwb_combine_cbc.C : Error - lag,slag must be >= 0 : --lag "
86  << cwb_combine_lag << " --slag " << cwb_combine_slag << endl << endl;
87  gSystem->Exit(1);
88  }
89  // check if cwb_combine_fthr is an integer number
91  if(cwb_combine_sfthr.IsDigit()) {
92  cwb_combine_fthr = cwb_combine_sfthr.Atoi();
93  } else {
94  cout << "cwb_combine_cbc.C : Error - fthr is not an interger number: --fthr " << cwb_combine_sfthr << endl << endl;
95  gSystem->Exit(1);
96  }
97  // check if cwb_combine_ifarthr is an integer number
98  float cwb_combine_ifarthr = 0.;
99  if(cwb_combine_sifarthr.IsFloat()) {
100  cwb_combine_ifarthr = cwb_combine_sifarthr.Atof();
101  if(cwb_combine_ifarthr<0) {
102  cout << "cwb_combine_cbc.C : Error - ifarthr must be>=0: --ifarthr " << cwb_combine_sifarthr << endl << endl;
103  gSystem->Exit(1);
104  }
105  } else if(cwb_combine_sifarthr!="") {
106  cout << "cwb_combine_cbc.C : Error - ifarthr is not a float number: --ifarthr " << cwb_combine_sifarthr << endl << endl;
107  gSystem->Exit(1);
108  }
109  // simulation -> check if cwb_merge_label contains the unique events tag
110  if(simulation && !cwb_merge_label.Contains(".C_U")) {
111  cout << "cwb_combine_cbc.C : Error - _merge label not contains the unique events tag '.C_U': merge label = " << cwb_merge_label << endl << endl;
112  gSystem->Exit(1);
113  }
114  // check if cwb_combine_search is defined
115  if(cwb_combine_search!="IMBHB" && cwb_combine_search!="BBH") {
116  cout << "cwb_combine_cbc.C : Error - search not defined, valid values are: IMBHB,BBH" << endl << endl;
117  gSystem->Exit(1);
118  }
119  // check if cwb_combine_wdir is defined
120  if(cwb_combine_wdir=="") {
121  cout << "cwb_combine_cbc.C : Error - wdir not defined" << endl << endl;
122  gSystem->Exit(1);
123  }
124  // check if cwb_combine_mlabel is defined
125  if(cwb_combine_mlabel=="") {
126  cout << "cwb_combine_cbc.C : Error - mlabel not defined" << endl << endl;
127  gSystem->Exit(1);
128  }
129  // simulation -> check if cwb_combine_mlabel contains the unique events tag
130  if(simulation && !cwb_combine_mlabel.Contains(".C_U")) {
131  cout << "cwb_combine_cbc.C : Error - mlabel not contains the unique events tag '.C_U': --mlabel " << cwb_combine_mlabel << endl << endl;
132  gSystem->Exit(1);
133  }
134  // check if all characters in cwb_combine_ulabel are alphanumeric
135  if(cwb_combine_ulabel!="" && !cwb_combine_ulabel.IsAlnum()) {
136  cout << "cwb_combine_cbc.C : Error - ulabel is not alphanumeric: --ulabel " << cwb_combine_ulabel << endl << endl;
137  gSystem->Exit(1);
138  }
139 
140  vector<TString> ifwave(2);
141  // create input wave root combine file name
142  TString ifile1 = TString::Format("%s/wave_%s.%s.root",merge_dir,data_label,cwb_merge_label.Data());
143  TString ifile2 = TString::Format("%s/%s/wave_%s.%s.root",cwb_combine_wdir.Data(),merge_dir,
144  gSystem->BaseName(cwb_combine_wdir),cwb_combine_mlabel.Data());
145  ifwave[0] = (cwb_combine_search=="BBH") ? ifile1 : ifile2;
146  ifwave[1] = (cwb_combine_search=="BBH") ? ifile2 : ifile1;
147  // check if input wave files exist
148  for(int n=0;n<2;n++) CWB::Toolbox::checkFile(ifwave[n]);
149 
150  vector<TString> ifmdc;
151  vector<TString> iflive;
152  if(simulation) { // simulation
153  ifmdc.resize(2);
154  ifmdc[0] = ifwave[0]; ifmdc[0].ReplaceAll("wave_","mdc_");
155  ifmdc[1] = ifwave[1]; ifmdc[1].ReplaceAll("wave_","mdc_");
156  // check if input mdc files exist
157  for(int n=0;n<2;n++) CWB::Toolbox::checkFile(ifmdc[n]);
158  }
159  if(!simulation && cwb_combine_check=="TRUE") { // background
160  // check livetimes zero lag IMBHB vs BBH
161  cout << "Check livetimes zero lag IMBHB vs BBH ..." << endl << endl;
162  iflive.resize(2);
163  iflive[0] = ifwave[0]; iflive[0].ReplaceAll("wave_","live_");
164  iflive[1] = ifwave[1]; iflive[1].ReplaceAll("wave_","live_");
165  // check if input live files exist
166  for(int n=0;n<2;n++) CWB::Toolbox::checkFile(iflive[n]);
167  // check if lag,slag livetime is the same for IMBHB and BBH
168  TChain live1("liveTime");
169  live1.Add(iflive[0]);
170  TChain live2("liveTime");
171  live2.Add(iflive[1]);
172  double livetime1,livetime2;
173  if(cwb_combine_lag==0 && cwb_combine_slag==0) { // zero lag
174  livetime1=CWB::Toolbox::getZeroLiveTime(nIFO,live1);
175  livetime2=CWB::Toolbox::getZeroLiveTime(nIFO,live2);
176  } else { // fake zero lag
177  wavearray<double> Trun(500000); Trun = 0.;
182  livetime1=CWB::Toolbox::getLiveTime(nIFO,live1,Trun,Wlag,Wslag,Tlag,Tdlag,cwb_combine_lag,cwb_combine_slag);
183  livetime2=CWB::Toolbox::getLiveTime(nIFO,live2,Trun,Wlag,Wslag,Tlag,Tdlag,cwb_combine_lag,cwb_combine_slag);
184  }
185  cout << "-----------------------------------------------------------------------------------" << endl;
186  cout << "cwb_combine_cbc.C : zero lag livetime of IMBHB "
187  << livetime1 << " (sec) " << livetime1/(24*3600) << " (days) " << endl;
188  cout << "cwb_combine_cbc.C : zero lag livetime of BBH "
189  << livetime2 << " (sec) " << livetime2/(24*3600) << " (days) " << endl;
190  cout << "-----------------------------------------------------------------------------------" << endl;
191  cout << endl;
192  exit(0);
193  }
194 
195  // input files
196  cout << endl;
197  cout << "Input IMBHB files : " << endl;
198  cout << ifwave[0] << endl;
199  if(simulation) cout << ifmdc[0] << endl;
200  cout << endl;
201  cout << "Input BBH files : " << endl;
202  cout << ifwave[1] << endl;
203  if(simulation) cout << ifmdc[1] << endl;
204  cout << endl;
205 
206  // create output wave root combine file name
207  TString ofwave = ifile1;
208  TString cwb_combine_tag = TString::Format("J_%s_f%dHz",cwb_combine_search.Data(),cwb_combine_fthr);
209  if(!simulation) cwb_combine_tag = cwb_combine_tag+TString::Format("_lag%d_slag%d",cwb_combine_lag,cwb_combine_slag);
210  if(cwb_combine_ulabel!="") cwb_combine_tag = cwb_combine_tag+"_"+cwb_combine_ulabel;
211  ofwave.ReplaceAll(".root",TString::Format(".%s.root",cwb_combine_tag.Data()));
212  // create output mdc root combine file name
213  TString ofmdc = ofwave;
214  ofmdc.ReplaceAll("wave_","mdc_");
215 
216  // combine searches
217  TString msearch = (cwb_combine_search=="BBH") ? "IMBHB" : "BBH"; // extract master search (used in history)
218  TString infos = "("+cwb_combine_tag+") - ( "+cwb_combine_options+" )"; // infos to be stored into history
219  int err = TB.CombineCBC(ifwave, ifmdc, ofwave, ofmdc, nIFO, cwb_combine_fthr,
220  msearch, infos, cwb_combine_lag, cwb_combine_slag, cwb_combine_ifarthr);
221  if(err) {
222  cout << "cwb_combine_cbc.C : Warning - no events selected, create a symbolic link to wave file" << endl << endl;
223  Long_t id,size,flags,mt;
224  int estat = gSystem->GetPathInfo(ifile1,&id,&size,&flags,&mt);
225  if(estat==0) {
226  char cmd[1024]; sprintf(cmd,"ln -sf ../%s %s",ifile1.Data(),ofwave.Data());
227  cout << cmd << endl;
228  gSystem->Exec(cmd);
229  }
230  }
231 
232  // create output merge list combine file name
233  TString ofmerge = ofwave;
234  ofmerge.ReplaceAll("wave_","merge_");
235  ofmerge.ReplaceAll(".root",".lst");
236  char cmd[1024]; sprintf(cmd,"touch %s",ofmerge.Data());
237  gSystem->Exec(cmd);
238 
240  if(simulation==0) { // background
241  // WARNING: this is correct if the zeo lag livetime IMBHB = BBH
242  // create symbolic link to live root file
243  TString iflive = ifile1;
244  iflive.ReplaceAll("wave_","live_");
245  iflive.Remove(0,iflive.Last('/')+1); // strip path
246  oflive = ofwave;
247  oflive.ReplaceAll("wave_","live_");
248  oflive.Remove(0,oflive.Last('/')+1); // strip path
249  //cout << oflive << endl;
250  Long_t id,size,flags,mt;
251  int estat = gSystem->GetPathInfo(mdir+"/"+iflive,&id,&size,&flags,&mt);
252  if(estat==0) {
253  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),iflive.Data(),oflive.Data());
254  //cout << cmd << endl;
255  gSystem->Exec(cmd);
256  }
257  }
258 
259  // output files
260  cout << endl;
261  cout << "Output Combined files : " << endl;
262  cout << ofwave << endl;
263  if(simulation) cout << ofmdc << endl;
264  else cout << mdir+"/"+oflive << endl;
265  cout << ofmerge << endl;
266  cout << endl;
267 
268  exit(0);
269 }
TString cwb_combine_check
TString cwb_combine_mlabel
wavearray< double > Trun(500000)
int n
Definition: cwb_net.C:28
int cwb_combine_lag
TString("c")
TString ofmerge
CWB::Toolbox TB
Long_t flags
int cwb_combine_slag
float cwb_combine_ifarthr
Long_t size
static double getLiveTime(vector< waveSegment > &jobList, vector< waveSegment > &dqList)
Definition: Toolbox.cc:2094
TString mdir
TString cwb_combine_options
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
TString cwb_combine_sfthr
int cwb_combine_fthr
static double getZeroLiveTime(int nIFO, TChain &liv, int dummy=0)
Definition: Toolbox.cc:4889
#define nIFO
char data_label[512]
Definition: test_config1.C:160
sprintf(cmd,"touch %s", ofmerge.Data())
TString cwb_combine_ulabel
static int CombineCBC(vector< TString > ifwave, vector< TString > ifmdc, TString ofwave, TString ofmdc, int nIFO, float fthr, TString msearch, TString infos="", int lag=0, int slag=0, float ifarthr=0.)
Definition: Toolbox.cc:3514
TString cwb_combine_search
ifwave[0]
const int NIFO_MAX
Definition: wat.hh:22
wavearray< double > Tlag
char merge_dir[512]
Definition: test_config1.C:147
wavearray< double > Tdlag
cwb_merge_label
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
TString cwb_combine_sifarthr
char cmd[1024]
int estat
Long_t mt
wavearray< double > Wlag[NIFO_MAX+1]
TString cwb_combine_wdir
Long_t id
TString ifile2
TString ifile1
simulation
Definition: cwb_eced.C:26
TString oflive
exit(0)
wavearray< double > Wslag[NIFO_MAX+1]