Logo coherent WaveBurst  
Library Reference Guide
Logo
Toolbox.cc
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 #include "Toolbox.hh"
19 #include "watversion.hh"
20 #include "TTreeFormula.h"
21 #include "GToolbox.hh"
22 #include "TThread.h"
23 #include <Riostream.h>
24 
25 #define CCAST(PAR) const_cast<char*>(PAR) \
26 
27 // Without this macro the THtml doc for CWB::Toolbox can not be generated
28 ClassImp(CWB::Toolbox)
29 
30 int compareSegments(waveSegment a, waveSegment b) {return a.start < b.start;}
31 
32 // definitions used by CWB::Toolbox::mergeCWBTrees with threads
33 
34 #define MAX_THREADS 16
35 
36 #define MAX_TREE_SIZE 100000000000LL
37 
38 struct MergeParms {
39  int threadID;
40  vector<TString> fileList;
41  bool simulation;
42  TString odir;
43  TString label;
44  bool brms;
45  bool bvar;
46  bool bpsm;
47 };
48 
49 void *MergeHandle(void *ptr) {
50 
51  MergeParms mp = *((MergeParms*) ptr);
52 
53  // dump thread file list
54  char ofName[1024];
55  sprintf(ofName,"%s/mergeList.T%d.txt",mp.odir.Data(),mp.threadID);
56 
57  CWB::Toolbox::dumpFileList(mp.fileList, ofName);
58 
59  // exec macro merge
60  char cmd[1024];
61  sprintf(cmd,"root -l -b '%s/cwb_merge_thread.C(\"%s\",%d,\"%s\",\"%s\",%d,%d,%d)'",
62  gSystem->ExpandPathName("$CWB_MACROS"), ofName, mp.simulation,
63  mp.odir.Data(), mp.label.Data(), mp.brms, mp.bvar, mp.bpsm);
64  gSystem->Exec(cmd);
65 
66  // remove thread file list
67  gSystem->Exec(TString::Format("rm %s",ofName));
68 
69  return 0;
70 }
71 
72 //______________________________________________________________________________
73 vector<waveSegment>
75 //
76 // read segment list from file
77 //
78 // Input: ifile - input file name
79 //
80 // Output: return the waveSegment list
81 //
82 
83  // Open file segment list
84  ifstream in;
85  in.open(ifile.Data(),ios::in);
86  if(!in.good()) {
87  cout << "CWB::Toolbox::readSegments - Error Opening File : " << ifile << endl;
88  gSystem->Exit(1);
89  }
90 
91  char str[1024];
92  int fpos=0;
93  int index=0;
94  double start;
95  double stop;
97  vector<waveSegment> iseg;
98  while (1) {
99  fpos=in.tellg();
100  in.getline(str,1024);
101  if(str[0] == '#') continue;
102  in.seekg(fpos, ios::beg);
103 
104  in >> start >> stop;
105  if (!in.good()) break;
106  fpos=in.tellg();
107  in.seekg(fpos+1, ios::beg);
108 
109  seg.index=index++; seg.start=start; seg.stop=stop; iseg.push_back(seg);
110  }
111 
112  in.close();
113 
114  return iseg;
115 }
116 
117 //______________________________________________________________________________
118 vector<waveSegment>
119 CWB::Toolbox::unionSegments(vector<waveSegment>& ilist) {
120 //
121 // Join & sort a waveSegment list
122 //
123 // Input: ilist - waveSegment list
124 //
125 // Output: return the joined waveSegment list
126 //
127 // ilist
128 // xxxxxxxx xxxxx
129 // xxxxxxxx xxx xxx
130 //
131 // output list
132 // xxxxxxxxxxxxx xxxxxxx xxx
133 //
134 
135  vector<waveSegment> vsegs;
136  int n = ilist.size();
137  if(n==0) return vsegs;
138 
139  waveSegment* isegs = new waveSegment[n];
140  waveSegment* osegs = new waveSegment[n+1];
141 
142  for(int i=0;i<n+1;i++) {osegs[i].start=0;osegs[i].stop=0;}
143  for(int i=0;i<n;i++) {
144  if(ilist[i].start>ilist[i].stop) {
145  cout << "CWB::Toolbox::unionSegments - Error : start must be <= stop - start = "
146  << ilist[i].start << " stop = " << ilist[i].stop << endl;
147  exit(1);
148  }
149  if(ilist[i].start<0) {
150  cout << "CWB::Toolbox::unionSegments - Error : start must be positive - start = "
151  << ilist[i].start << endl;
152  exit(1);
153  }
154  isegs[i] = ilist[i];
155  }
156 
157  std::sort(isegs, isegs + n, compareSegments);
158  double right_most = -1;
159  int cnt = 0;
160  for (int i = 0 ; i < n ; i++) {
161  if (isegs[i].start > right_most) {
162  right_most = isegs[i].stop;
163  ++cnt;
164  osegs[cnt].start = isegs[i].start;
165  osegs[cnt].stop = isegs[i].stop;
166  }
167  if (isegs[i].stop > right_most) {
168  right_most = isegs[i].stop;
169  osegs[cnt].stop = isegs[i].stop;
170  }
171  }
172 
173  int vcnt=0;
175  for(int i=0;i<cnt+1;i++) if(osegs[i].stop>0) {seg=osegs[i];seg.index=vcnt++;vsegs.push_back(seg);}
176 
177  delete [] isegs;
178  delete [] osegs;
179 
180  return vsegs;
181 }
182 
183 //______________________________________________________________________________
184 vector<waveSegment>
185 CWB::Toolbox::sortSegments(vector<waveSegment>& ilist) {
186 //
187 // sort a waveSegment list
188 //
189 // Input: ilist - waveSegment list
190 //
191 // Output: return the sorted waveSegment list
192 //
193 
194  int size = ilist.size();
195 
196  double* start = new double[size];
197  double* stop = new double[size];
198  for(int i=0;i<size;i++) {
199  start[i] = ilist[i].start;
200  stop[i] = ilist[i].stop;
201  }
202 
203 // sort list
204 
205  Int_t *id = new Int_t[size];
206  TMath::Sort(size,start,id,false);
207  for(int i=1;i<size;i++) {
208  bool flag=true;
209  if(start[id[i]]<=0) flag=false;
210  if(start[id[i]]>stop[id[i]]) flag=false;
211  if(start[id[i]]<stop[id[i-1]]) flag=false;
212  if(!flag) {
213  cout.precision(14);
214  cout << "CWB::Toolbox::invertSegments - Error in segment list (duplicated veto) : " << endl;
215  cout << id[i-1]+1 << " " << start[id[i-1]] << " " << stop[id[i-1]] << " flag " << flag << endl;
216  cout << id[i]+1 << " " << start[id[i]] << " " << stop[id[i]] << " flag " << flag << endl;
217  gSystem->Exit(1);
218  }
219  }
220 
221 // write list to output vector
222 
223  vector<waveSegment> olist;
224  double ctime=0.;
226  SEG.index = 0;
227  olist.clear();
228 
229  // sort list
230  for(int i=0;i<size;i++) {
231  SEG.index++;
232  SEG.start = start[id[i]];
233  SEG.stop = stop[id[i]];
234  ctime+=stop[id[i]]-start[id[i]];
235  olist.push_back(SEG);
236  }
237 
238  delete [] id;
239  delete [] start;
240  delete [] stop;
241 
242  return olist;
243 }
244 
245 //______________________________________________________________________________
246 vector<waveSegment>
247 CWB::Toolbox::invertSegments(vector<waveSegment>& ilist) {
248 //
249 // invert a waveSegment list
250 //
251 // Input: ilist - waveSegment list
252 //
253 // Output: return the inverted waveSegment list
254 //
255 // ilist
256 // xxxxxxxx xxxxx
257 //
258 // output list
259 // xxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx
260 //
261 
262  int size = ilist.size();
263 
264  double* start = new double[size];
265  double* stop = new double[size];
266  for(int i=0;i<size;i++) {
267  start[i] = ilist[i].start;
268  stop[i] = ilist[i].stop;
269  }
270 
271 // sort list
272 
273  Int_t *id = new Int_t[size];
274  TMath::Sort(size,start,id,false);
275  for(int i=1;i<size;i++) {
276  bool flag=true;
277  if(start[id[i]]<=0) flag=false;
278  if(start[id[i]]>stop[id[i]]) flag=false;
279  if(start[id[i]]<stop[id[i-1]]) flag=false;
280  if(!flag) {
281  cout.precision(14);
282  cout << "CWB::Toolbox::invertSegments - Error in segment list (duplicated veto) : " << endl;
283  cout << id[i-1]+1 << " " << start[id[i-1]] << " " << stop[id[i-1]] << " flag " << flag << endl;
284  cout << id[i]+1 << " " << start[id[i]] << " " << stop[id[i]] << " flag " << flag << endl;
285  gSystem->Exit(1);
286  }
287  }
288 
289 // write list to output vector
290 
291  vector<waveSegment> olist;
292  double ctime=0.;
294  SEG.index = 0;
295  olist.clear();
296 
297  // invert bad with good periods
298  SEG.index++;
299  SEG.start = 0;
300  SEG.stop = start[id[0]];
301  olist.push_back(SEG);
302  for(int i=0;i<size-1;i++) {
303  SEG.index++;
304  SEG.start = stop[id[i]];
305  SEG.stop = start[id[i+1]];
306  ctime+=SEG.stop-SEG.start;
307  olist.push_back(SEG);
308  }
309  SEG.index++;
310  SEG.start = stop[id[size-1]];
311  SEG.stop = 4000000000;
312  olist.push_back(SEG);
313 
314  delete [] id;
315  delete [] start;
316  delete [] stop;
317 
318  return olist;
319 }
320 
321 //______________________________________________________________________________
322 void
323 CWB::Toolbox::blackmanharris (double* window, int n) {
324 //
325 // blackmanharris window
326 //
327 // Input/Output - window - array of double intialized (must be initialized)
328 // return the window values
329 // Input - n - window length
330 //
331 
332  for (int i = 0; i < n; i++)
333  {
334  double f = 0.0;
335  f = ((double) i) / ((double) (n - 1));
336  window[i] = 0.35875 -
337  0.48829 * cos(2.0 * TMath::Pi() * f) +
338  0.14128 * cos(4.0 * TMath::Pi() * f) -
339  0.01168 * cos(6.0 * TMath::Pi() * f);
340  }
341 
342  double norm = 0;
343  for (int i=0;i<n;i++) norm += pow(window[i],2);
344  norm /= n;
345  for (int i=0;i<n;i++) window[i] /= sqrt(norm);
346 }
347 
348 //______________________________________________________________________________
349 vector<waveSegment>
350 CWB::Toolbox::mergeSegLists(vector<waveSegment>& ilist1, vector<waveSegment>& ilist2) {
351 //
352 // Merge 2 segment lists
353 // NOTE : the input lists must be sorted !!!
354 //
355 // Input: ilist1 - first waveSegment list
356 // ilist2 - second waveSegment list
357 //
358 // Output: return the merged waveSegment list
359 //
360 // ilist1
361 // xxxxxxxx xxxxxxxxxxxx
362 //
363 // ilist2
364 // xxxx xxxxxx xxx
365 //
366 // olist
367 // xxx xxxxx
368 //
369 
370  int i=0;
371  int j=0;
372  int n1=ilist1.size();
373  int n2=ilist2.size();
374  double ctime=0;
375  double start=0;
376  double stop=0;
377 
378  vector<waveSegment> olist;
380  SEG.index = 0;
381 
382  while (i<n1 && j<n2) {
383  if (ilist2[j].stop<=ilist1[i].start) j++;
384  else if (ilist1[i].stop <= ilist2[j].start) i++;
385  else {
386  if (ilist2[j].start < ilist1[i].start) start=ilist1[i].start;
387  else start = ilist2[j].start;
388  if (ilist2[j].stop > ilist1[i].stop) stop=ilist1[i].stop;
389  else stop=ilist2[j].stop;
390  if (ilist2[j].stop >= ilist1[i].stop) i++;
391  else j++;
392  ctime+=stop-start;
393 
394  SEG.index++;
395  SEG.start = start;
396  SEG.stop = stop;
397  olist.push_back(SEG);
398  }
399  }
400 
401  //ilist2=olist;
402  //olist.clear();
403 
404  return olist;
405 }
406 
407 //______________________________________________________________________________
408 vector<waveSegment>
410 //
411 // Read DQ file
412 // Format A : #entry start stop stop-start
413 // Format B : start stop
414 // list is sorted, check if start<stop, check if entries are overlapped
415 //
416 // Input: DQF - DQ structure
417 //
418 // Output: return the list of time ranges
419 //
420 
421  vector<waveSegment> olist;
422 
423 // Open list
424 
425  ifstream in;
426  in.open(DQF.file,ios::in);
427  if (!in.good()) {cout << "CWB::Toolbox::readSegList - Error Opening File : " << DQF.file << endl;gSystem->Exit(1);}
428 
429  int size=0;
430  char str[1024];
431  int fpos=0;
432  while(true) {
433  in.getline(str,1024);
434  if (!in.good()) break;
435  if(str[0] != '#') size++;
436  }
437  //cout << "size " << size << endl;
438  in.clear(ios::goodbit);
439  in.seekg(0, ios::beg);
440  if (size==0) {cout << "CWB::Toolbox::readSegList - Error : File " << DQF.file << " is empty" << endl;gSystem->Exit(1);}
441 
442  int N=0;
443  float dummy;
444  double* start = new double[size];
445  double* stop = new double[size];
446  while (1) {
447  fpos=in.tellg();
448  in.getline(str,1024);
449  if(str[0] == '#') continue;
450  in.seekg(fpos, ios::beg);
451 
452  if(DQF.c4) in >> dummy >> start[N] >> stop[N] >> dummy;
453  else in >> start[N] >> stop[N];
454  if (!in.good()) break;
455  fpos=in.tellg();
456  in.seekg(fpos+1, ios::beg);
457  start[N]+=DQF.shift;
458  stop[N]+=DQF.shift;
459  if(stop[N]<=start[N]) {
460  cout.precision(14);
461  cout << "CWB::Toolbox::readSegList - Error Ranges : " << start[N] << " " << stop[N] << endl;
462  gSystem->Exit(1);
463  }
464  N++;
465  if(N>size) {
466  cout << "CWB::Toolbox::readSegList - Error Max Range : " << N << " " << size << endl;
467  gSystem->Exit(1);
468  }
469  }
470  in.close();
471 
472 // sort list
473 
474  Int_t *id = new Int_t[N];
475  TMath::Sort(N,start,id,false);
476  for(int i=1;i<N;i++) {
477  bool flag=true;
478  if(start[id[i]]<=0) flag=false;
479  if(start[id[i]]>stop[id[i]]) flag=false;
480  if(start[id[i]]<stop[id[i-1]]) flag=false;
481  if(!flag) {
482  cout.precision(14);
483  cout << "CWB::Toolbox::readSegList - Error in segment list file (duplicated veto) : " << DQF.file << endl;
484  cout << id[i-1]+1 << " " << start[id[i-1]] << " " << stop[id[i-1]] << " flag " << flag << endl;
485  cout << id[i]+1 << " " << start[id[i]] << " " << stop[id[i]] << " flag " << flag << endl;
486  gSystem->Exit(1);
487  }
488  }
489 
490 // write list to output vector
491 
492  double ctime=0.;
494  SEG.index = 0;
495  olist.clear();
496  if(DQF.invert) { // invert bad with good periods
497  SEG.index++;
498  SEG.start = 0;
499  SEG.stop = start[id[0]];
500  olist.push_back(SEG);
501  for(int i=0;i<N-1;i++) {
502  SEG.index++;
503  SEG.start = stop[id[i]];
504  SEG.stop = start[id[i+1]];
505  ctime+=SEG.stop-SEG.start;
506  olist.push_back(SEG);
507  }
508  SEG.index++;
509  SEG.start = stop[id[N-1]];
510  SEG.stop = 4000000000;
511  olist.push_back(SEG);
512  } else {
513  for(int i=0;i<N;i++) {
514  SEG.index++;
515  SEG.start = start[id[i]];
516  SEG.stop = stop[id[i]];
517  ctime+=stop[id[i]]-start[id[i]];
518  olist.push_back(SEG);
519  }
520  }
521 
522  delete [] id;
523  delete [] start;
524  delete [] stop;
525 
526  return olist;
527 }
528 
529 //______________________________________________________________________________
530 vector<waveSegment>
532 //
533 // Read & Merge DQ files with DQF.cat<=dqcat
534 //
535 //
536 // Input: nDQF - number of DQ files
537 // dqfile - DQ structure array
538 // dqcat - max DQ cat
539 //
540 // Output: return the list of time ranges which pass the merged max DQ cat conditions
541 //
542 
543  vector<waveSegment> olist;
544 
545  int ndqf=0;
546  dqfile dqf[nDQF];
547  for(int n=0;n<nDQF;n++) { // Read DQ files with DQF.cat<=dqcat
548  if(DQF[n].cat<=dqcat) dqf[ndqf++]=DQF[n];
549  }
550  if(ndqf==0) {
551  cout << "CWB::Toolbox::readSegList - no CWB_CAT=" << dqcat << " files in the list" << endl;
552  gSystem->Exit(1);
553  };
554 
555  vector<waveSegment>* list = new vector<waveSegment>[ndqf];
556 
557  for(int n=0;n<ndqf;n++) list[n]=readSegList(dqf[n]);
558 
559  olist=list[0];
560  for(int n=1;n<ndqf;n++) olist = mergeSegLists(list[n],olist); // Merge DQ files
561 
562  for(int n=0;n<ndqf;n++) list[n].clear();
563 
564  delete [] list;
565 
566  return olist; // return the list of time ranges
567 }
568 
569 //______________________________________________________________________________
570 int
571 CWB::Toolbox::dumpSegList(vector<waveSegment> list, TString fName, bool c4) {
572 //
573 // Dump to file a waveSegment list
574 //
575 // Input: list - waveSegment list
576 // fName - output file name
577 // c4 - output format
578 // false : start stop
579 // true : index start stop (stop-start)
580 //
581 
582  FILE* fP;
583  if((fP = fopen(fName.Data(), "w")) == NULL) {
584  cout << "cannot open output file " << fName.Data() <<". \n";
585  gSystem->Exit(1);
586  };
587  cout << "Write output file : " << fName.Data() << endl;
588 
589  if(c4)
590  fprintf(fP,"# seg start stop duration\n");
591  for(int i=0;i<(int)list.size();i++) {
592  if(c4) {
593  fprintf(fP,"%-d\t%-d\t%-d\t%-d\n",
594  int(list[i].index)-1,
595  int(list[i].start),
596  int(list[i].stop),
597  int(list[i].stop-list[i].start));
598  } else {
599  //fprintf(fP,"%-d\t%-d\n",
600  fprintf(fP,"%d %d\n",
601  int(list[i].start),
602  int(list[i].stop));
603  }
604  }
605  if(fP!=NULL) fclose(fP);
606  return 0;
607 }
608 
609 //______________________________________________________________________________
610 double
611 CWB::Toolbox::getTimeSegList(vector<waveSegment> list) {
612 //
613 // Input: list - segment list
614 //
615 // Return the length in sec of the sum of the segments
616 //
617 
618  double segtime=0;
619  for(int i=0;i<(int)list.size();i++) segtime+=list[i].stop-list[i].start;
620  return segtime;
621 }
622 
623 //______________________________________________________________________________
624 void
625 CWB::Toolbox::dumpJobList(vector<waveSegment> ilist, TString fName, double segLen, double segMLS, double segEdge) {
626 //
627 // dump to file the job segment list
628 //
629 // Input: ilist - waveSegment list
630 // fName - output file name
631 // segLen - Segment length [sec]
632 // segMLS - Minimum Segment Length after DQ_CAT1 [sec]
633 // segEdge - wavelet boundary offset [sec]
634 //
635 
636  vector<waveSegment> olist;
637  olist=getJobList(ilist, segLen, segMLS, segEdge);
638  dumpSegList(olist, fName, false);
639  olist.clear();
640 
641  return;
642 }
643 
644 //______________________________________________________________________________
645 vector<waveSegment>
646 CWB::Toolbox::getJobList(vector<waveSegment> ilist, double segLen, double segMLS, double segEdge) {
647 //
648 // build the job segment list
649 //
650 // the job segments are builded starting from the input ilist
651 // each segment must have a minimum length of segMLS+2segEdge and a maximum length of segLen+2*segEdge
652 // in order to maximize the input live time each segment with lenght<2*(segLen+segEdge) is
653 // divided in 2 segments with length<segLen+2*segEdge
654 // segEdge : xxx
655 // segMLS : -------
656 // segLen : ---------------
657 // input seg : ----------------------------
658 // output segA : xxx---------xxx
659 // output segB : xxx----------xxx
660 //
661 // Input: ilist - number of detectors
662 // segLen - Segment length [sec]
663 // segMLS - Minimum Segment Length after DQ_CAT1 [sec]
664 // segEdge - wavelet boundary offset [sec]
665 //
666 // Output: Return the job segment list
667 //
668 
669  if(segMLS>segLen) {
670  cout << endl << "CWB::Toolbox::getJobList - Error : segMLS must be <= segLen" << endl;
671  cout << "segMLS = " << segMLS << " - segLen = " << segLen << endl << endl;
672  exit(1);
673  }
674 
675  int start,stop,len;
676  int remainder,half;
677  int n;
678  int size = ilist.size();
679  int lostlivetime=0;
680  vector<waveSegment> olist;
681 
683  SEG.index=0;
684  olist.clear();
685 
686  for(int i=0;i<size;i++) {
687  start = fmod(ilist[i].start,1)!=0 ? int(ilist[i].start+1) : ilist[i].start; // 1.x -> 2.0
688  stop = fmod(ilist[i].stop ,1)!=0 ? int(ilist[i].stop) : ilist[i].stop; // 1.x -> 1.0
689  start += segEdge;
690  stop -= segEdge;
691  len=stop-start;
692  if(len<=0) continue;
693 
694  n=len/segLen;
695  if(n==0) {
696  if(len<segMLS) {
697  //printf("too small a segment = %d %d %d %d\n",i,start-8,stop+8,len);
698  lostlivetime+=len;
699  continue;
700  }
701  SEG.index++;
702  SEG.start=start;
703  SEG.stop=stop;
704  olist.push_back(SEG);
705  continue;
706  }
707  if(n==1) {
708  if(len>segLen) {
709  remainder=len;
710  half=int(remainder/2);
711  if(half>=segMLS) {
712  SEG.index++;
713  SEG.start=start;
714  SEG.stop=SEG.start+half;
715  olist.push_back(SEG);
716  SEG.index++;
717  SEG.start=SEG.stop;
718  SEG.stop=stop;
719  olist.push_back(SEG);
720  } else {
721  SEG.index++;
722  SEG.start=start;
723  SEG.stop=SEG.start+segLen;
724  olist.push_back(SEG);
725  }
726  } else {
727  SEG.index++;
728  SEG.start=start;
729  SEG.stop=stop;
730  olist.push_back(SEG);
731  }
732  continue;
733  }
734 
735  for(int j=0;j<n-1;j++) {
736  SEG.index++;
737  SEG.start=segLen*j+start;
738  SEG.stop=SEG.start+segLen;
739  olist.push_back(SEG);
740  }
741  remainder=stop-SEG.stop;
742  half=int(remainder/2);
743  if(half>=segMLS) {
744  SEG.index++;
745  SEG.start=SEG.stop;
746  SEG.stop=SEG.start+half;
747  olist.push_back(SEG);
748  SEG.index++;
749  SEG.start=SEG.stop;
750  SEG.stop=stop;
751  olist.push_back(SEG);
752  } else {
753  SEG.index++;
754  SEG.start=SEG.stop;
755  SEG.stop=SEG.start+segLen;
756  olist.push_back(SEG);
757  }
758  }
759 
760  printf("Toolbox::getJobList : lost livetime after building of the standard job list = %d sec\n",lostlivetime);
761  return olist;
762 }
763 
764 //______________________________________________________________________________
765 vector<waveSegment>
766 CWB::Toolbox::getJobList(vector<waveSegment> cat1List, vector<waveSegment> cat2List,
767  double segLen, double segMLS, double segTHR, double segEdge) {
768 //
769 // build the job segment list
770 //
771 // 1) build job list starting from cat1List
772 // 2) select jobs which have lenght < segTHR after cat2List
773 //
774 // Input: ilist - number of detectors
775 // segLen - Segment length [sec]
776 // segMLS - Minimum Segment Length after DQ_CAT1 [sec]
777 // segTHR - Minimum Segment Length after DQ_CAT2 [sec]
778 // segEdge - wavelet boundary offset [sec]
779 //
780 // Output: Return the job segment list
781 //
782 
783  // build job list starting from cat1
784  vector<waveSegment> jobList1=getJobList(cat1List, segLen, segMLS, segEdge);
785 
786  // select jobs which have after cat2 lenght < segTHR
787  vector<waveSegment> jobList2;
788  for(int i=0;i<jobList1.size();i++) {
789  // check if job segment after cat2 is < segTHR
790  vector<waveSegment> detSegs_dq2;
791  detSegs_dq2.push_back(jobList1[i]);
792  detSegs_dq2 = mergeSegLists(detSegs_dq2,cat2List);
793  double detSegs_ctime = getTimeSegList(detSegs_dq2);
794  if(detSegs_ctime<segTHR) {
795  cout << "CWB::Toolbox::getJobList : job segment=" << i+1
796  << " live time after cat2 : " << detSegs_ctime << endl;
797  cout << " segTHR=" << segTHR
798  << " sec -> job removed !!!" << endl;
799  } else {
800  jobList2.push_back(jobList1[i]);
801  }
802  }
803 
804  return jobList2;
805 }
806 
807 //______________________________________________________________________________
808 vector<slag>
810  size_t slagMin, size_t slagMax, size_t* slagSite, char* slagFile) {
811 //
812 // Get SuperLags list
813 //
814 //
815 // Input: nIFO - number of ifos
816 // slagSize - number of super lags (=1 if simulation>0) - if slagSize=0 -> Standard Segments
817 // slagSegs - number of segments
818 // slagOff - first slag id (slagOff=0 - include zero slag )
819 // slagMin - select the minimum available slag distance (see example) : slagMin must be <= slagMax
820 // slagMax - select the maximum available slag distance (see example)
821 // slagSite - site index starting with 0 (same definition as lagSite)
822 // slagFile - user slag file list (default = NULL)
823 // format : slagID slagID_ifo1 slagID_ifo2 ...
824 // lines starting with # are skipped
825 // Example with 3 ifos :
826 //
827 // # SLAG ifo[0] ifo[1] ifo[2]
828 // 0 0 0 0
829 // 1 0 1 -1
830 // 2 0 -1 1
831 //
832 // Return the slag list
833 //
834 // Built-in slags
835 // The built-in algorithm generates the slag list according the increasing value of the slag distance
836 // The entries with the same distance are randomly reshuffled
837 // The slag distance is defined as the sum over the ifos of the absolute values of the shifts
838 //
839 // Example with 3 ifos :
840 //
841 // SLAG ifo[0] ifo[1] ifo[2]
842 // 0 0 0 0
843 // 1 0 1 -1
844 // 2 0 -1 1
845 // 3 0 2 1
846 // 4 0 2 -1
847 // 5 0 -1 2
848 // 6 0 -2 -1
849 // 7 0 1 2
850 // 8 0 1 -2
851 // 9 0 -1 -2
852 //
853 // the distance is :
854 // 0 for SLAG 0
855 // 2 for SLAG 1,2
856 // 3 for SLAG 3,4,5,6,7,8,9
857 //
858 // slagMin,slagMax select the minimum/maximum available distance
859 // ex: if slagMin=3,slagMax=3 the available slags are 3,4,5,6,7,8,9
860 // slagOff select the slag offset of the available slags
861 // ex: if slagMin=3,slagMax=3,slagOff=2 the available slags are 5,6,7,8,9
862 // slagSize define the number of selected slags
863 // ex: if slagMin=3,slagMax=3,slagOff=2,slagSize=2 then the selected slags are 5,6
864 //
865 
866  if(slagSize<1) slagSize=1;
867 
868  if((int)slagMax>slagSegs) {
869  cout << "CWB::Toolbox::makeSlagList : Error - slagMax must be < slagSegs" << endl;
870  gSystem->Exit(1);
871  }
872 
873  if(slagSegs<=0.) {
874  cout << "CWB::Toolbox::makeSlagList : Error - slagSegs must be positive" << endl;
875  gSystem->Exit(1);
876  }
877 
878  if((slagMax>0)&&(slagMin>slagMax)) {
879  cout << "CWB::Toolbox::getSlagList : Error - slagMin must be < slagMax" << endl;
880  gSystem->Exit(-1);
881  }
882 
883  if(slagSite!=NULL) for(int n=0; n<(int)nIFO; n++) {
884  if(slagSite[n] >= nIFO) {
885  cout << "CWB::Toolbox::getSlagList : Error slagSite - value out of range " << endl;
886  gSystem->Exit(-1);
887  }
888  }
889 
890  vector<slag> slagList;
891  vector<int> id;
892 
893  if(slagFile) { // read list of slags from file
894 
895  cout << endl << "CWB::Toolbox::getSlagList : read slags from file -> " << slagFile << endl;
896 
897  ifstream in;
898  in.open(slagFile, ios::in);
899  if(!in.good()) {
900  cout << "CWB::Toolbox::getSlagList : Error Opening File : " << slagFile << endl;
901  gSystem->Exit(1);
902  }
903 
904  char str[1024];
905  int fpos=0;
906  int id;
907  int size = slagOff+slagSize;
908  while(true) {
909  fpos=in.tellg();
910  in.getline(str,1024);
911  if(str[0] == '#') continue;
912  in.seekg(fpos, ios::beg);
913  slag SLAG;
914  in >> id;
915  if (!in.good()) break;
916  // check duplicated slag id
917  for(int j=0; j<(int)slagList.size(); j++) {
918  if(id==slagList[j].jobId) {
919  cout << "CWB::Toolbox::getSlagList : duplicated slag id "
920  << " or wrong format in slag file -> " << slagFile << endl;
921  gSystem->Exit(1);
922  }
923  }
924  SLAG.jobId=id;
925  for(int n=0; n<(int)nIFO; n++) {
926  in >> id; SLAG.slagId.push_back(id);
927  }
928  slagList.push_back(SLAG);
929  if (slagList.size()==size) break;
930  if (!in.good()) break;
931  fpos=in.tellg();
932  in.seekg(fpos+1, ios::beg);
933  }
934 
935  in.close();
936 
937  if(size>slagList.size()) {
938  cout << "CWB::Toolbox::getSlagList : Error - slagOff+slagSize " << size << " is > entries in slagFile " << slagList.size() << endl;
939  gSystem->Exit(1);
940  }
941 
942  } else { // built-in slag generation
943 
944  cout << endl << "CWB::Toolbox::getSlagList : built-in slags" << endl;
945 
946  // find number of non co-located detectors
947  vector<int> ifo;
948  for(int n=0;n<(int)nIFO;n++) {
949  if(slagSite!=NULL) {
950  bool unique=true;
951  for(int m=0;m<(int)ifo.size();m++) if((int)slagSite[n]==ifo[m]) unique=false;
952  if(unique) ifo.push_back(slagSite[n]);
953  } else {
954  ifo.push_back(n);
955  }
956  }
957  //for(int m=0;m<(int)ifo.size();m++) cout << m << " " << ifo[m] << endl;
958 
959  // add slag 0
960  int jobId=0;
961  if(slagMin==0) {
962  slag SLAG;
963  SLAG.jobId=jobId++;
964  for(int n=0; n<(int)nIFO; n++) SLAG.slagId.push_back(0);
965  slagList.push_back(SLAG);
966  }
967  for(int m=(int)slagMin;m<=(int)slagMax;m++) {
968  //int size = slagSize ? slagOff+slagSize : 0;
969  int size = 0; // get all slags contained in the range
970  int nifo = ifo.size(); // number of non co-located detectors
971  vector<slag> slags;
972  getSlagList(slags,size,m,nifo,id); // get slag list located at range m
973  // add slags to slagList in random mode
974  gRandom->SetSeed(m+1); // random seed is initialized with range
975  while(slags.size()>0) {
976  int k = int(gRandom->Uniform(0,int(slags.size())));
977  slags[k].jobId=jobId++;
978  slagList.push_back(slags[k]);
979  slags.erase(slags.begin()+k); // remove slag from slags list
980  }
981  }
982  // set slags according to slagSite setup
983  if(slagSite!=NULL) {
984  for(int j=0; j<(int)slagList.size(); j++) {
985  slag SLAG = slagList[j];
986  slagList[j].slagId.resize(nIFO);
987  for(int n=0; n<(int)nIFO; n++) {
988  for(int m=0;m<(int)ifo.size();m++)
989  if((int)slagSite[n]==ifo[m]) slagList[j].slagId[n] = SLAG.slagId[m];
990  }
991  }
992  }
993  }
994 
995  if((slagSize>slagList.size()-slagOff)) {
996  cout << "CWB::Toolbox::getSlagList : Error - the number of available slags = "
997  << slagList.size() << " are less than the requested slag (slagSize) = " << slagSize << endl;
998  gSystem->Exit(-1);
999  }
1000 
1001  // print slag list
1002  cout << endl;
1003  printf("%14s ","SLAG");
1004  for(int n=0; n<nIFO; n++) printf("%8sifo[%d]","",n);
1005  printf("\n");
1006  for(int i=(int)slagOff; i<int(slagOff+slagSize); i++) {
1007  printf("%14d", slagList[i].jobId);
1008  for (int n=0; n<nIFO; n++) printf("%14d",slagList[i].slagId[n]);
1009  printf("\n");
1010  }
1011  cout << endl;
1012 
1013  // fill slag list
1014  int jobID=0;
1015  slag SLAG;
1016  vector<slag> slist;
1017  for(int j=(int)slagOff; j<int(slagOff+slagSize); j++){
1018  int nseg=0;
1019  for(int k=1; k<=slagSegs; k++){
1020  bool check=true;
1021  for (int n=0; n<(int)nIFO; n++) if((k+slagList[j].slagId[n])>slagSegs) check=false;
1022  for (int n=0; n<(int)nIFO; n++) if((k+slagList[j].slagId[n])<0) check=false;
1023  if(check){
1024  SLAG.jobId=++jobID;
1025  SLAG.slagId.clear();
1026  SLAG.segId.clear();
1027  SLAG.slagId.push_back(slagList[j].jobId);
1028  SLAG.slagId.push_back(++nseg); // this number identify the segment number in a slag
1029  for(int n=0; n<(int)nIFO; n++) {
1030  SLAG.slagId.push_back(slagList[j].slagId[n]);
1031  SLAG.segId.push_back(slagList[j].slagId[n]+k);
1032  }
1033  slist.push_back(SLAG);
1034  }
1035  }
1036  }
1037  slagList.clear();
1038 
1039  return slist;
1040 }
1041 
1042 //______________________________________________________________________________
1043 void
1044 CWB::Toolbox::getSlagList(vector<slag>& slagList, int slagSize, int slagRank, int nifo, vector<int>& id) {
1045 //
1046 // Get SuperLags list associate to the slagRank value
1047 //
1048 // Input: slagSize - number of super lags
1049 // slagRank - slag distance
1050 // nifo - number of ifos
1051 // id - temporary auxiliary vector id
1052 //
1053 // Return the slag list for the slagRank value
1054 //
1055 
1056  if(slagSize && ((int)slagList.size()==slagSize)) return;
1057  for(int j=-slagRank;j<=slagRank;j++) {
1058  if(j==0) continue;
1059  bool unique=true;
1060  for(int n=0; n<(int)id.size(); n++) if(j==id[n]) unique=false;
1061  if(!unique) continue;
1062  if(nifo==2) {
1063  id.push_back(j);
1064  int m=0;
1065  for(int n=0; n<(int)id.size(); n++) m+=abs(id[n]);
1066  if(m==slagRank) {
1067  slag SLAG;
1068  SLAG.jobId=slagList.size();
1069  SLAG.slagId.push_back(0); // set first detector with slag shift = 0
1070  for(int n=0; n<(int)id.size(); n++) SLAG.slagId.push_back(id[n]);
1071  slagList.push_back(SLAG);
1072  if(slagSize && ((int)slagList.size()==slagSize)) return;
1073  }
1074  id.resize(id.size()-1);
1075  continue;
1076  } else {
1077  id.push_back(j);
1078  getSlagList(slagList, slagSize, slagRank, nifo-1, id);
1079  id.resize(id.size()-1);
1080  }
1081  }
1082  return;
1083 }
1084 
1085 //______________________________________________________________________________
1086 void
1087 CWB::Toolbox::dumpSlagList(vector<slag> slagList, TString slagFile, bool slagOnly) {
1088 //
1089 // dump slag list to file
1090 //
1091 // Input: slagList - slag list
1092 // slagFile - output file name
1093 // slagOnly - true: write only slagID
1094 // Example with 3 ifos :
1095 //
1096 // # SLAG ifo[0] ifo[1] ifo[2]
1097 // 0 0 0 0
1098 // 1 0 1 -1
1099 // 2 0 -1 1
1100 //
1101 // - false: write slagID + segID
1102 //
1103 
1104  if(((int)slagList.size()>0)&&(slagFile!="")) {
1105 
1106  FILE *fP=NULL;
1107  if((fP = fopen(slagFile.Data(), "w")) == NULL) {
1108  cout << "CWB::Toolbox::makeSlagList : Error - cannot open file " << slagFile.Data() << endl;
1109  gSystem->Exit(1);
1110  }
1111 
1112  int nIFO = slagList[0].segId.size();
1113 
1114  // write header
1115 
1116  fprintf(fP,"#");for (int n=0;n<=nIFO+1;n++) fprintf(fP,"--------------");fprintf(fP,"\n");
1117  fprintf(fP,"# Super Lags List - %d jobs\n",(int)slagList.size());
1118  fprintf(fP,"#");for (int n=0;n<=nIFO+1;n++) fprintf(fP,"--------------");fprintf(fP,"\n");
1119  fprintf(fP,"# nIFO %13d\n",int(nIFO));
1120  fprintf(fP,"#");for (int n=0;n<=nIFO+1;n++) fprintf(fP,"--------------");fprintf(fP,"\n");
1121  if(!slagOnly) fprintf(fP,"#%13s","jobId"); else fprintf(fP,"#");
1122  fprintf(fP,"%14s","slagId");
1123  for(int n=0; n<nIFO; n++) fprintf(fP,"%11s[%1d]","segID",int(n));
1124  fprintf(fP,"\n");
1125  fprintf(fP,"#");for (int n=0;n<=nIFO+1;n++) fprintf(fP,"--------------");fprintf(fP,"\n");
1126 
1127  // write jobs
1128  for(int j=0; j<(int)slagList.size(); j++){
1129  if(slagOnly) {
1130  if(slagList[j].slagId[1]==1) {
1131  fprintf(fP,"%14d", slagList[j].slagId[0]);
1132  for (int n=0; n<nIFO; n++) fprintf(fP,"%14d",slagList[j].slagId[n+2]);
1133  fprintf(fP,"\n");
1134  }
1135  } else {
1136  fprintf(fP,"%14d", slagList[j].jobId); // jobID
1137  fprintf(fP,"%14d", slagList[j].slagId[0]); // slagID
1138  for (int n=0; n<nIFO; n++) fprintf(fP,"%14d",slagList[j].segId[n]); // segID
1139  fprintf(fP,"\n");
1140  }
1141  }
1142 
1143  if(fP!=NULL) fclose(fP);
1144  }
1145 
1146  return;
1147 }
1148 
1149 //______________________________________________________________________________
1150 int
1152  vector<TString> jobFiles, TString stage, int jobmin, int jobmax) {
1153 //
1154 // produce the condor dag file for stages
1155 //
1156 // Input: jobList - list of job ID
1157 // condor_dir - dag,sub condor directory
1158 // label - label used for dag file = 'condor_dir'/'label'.dag
1159 // jobFiles - name of job files created by the previous stage
1160 // stage - analysis stage (Ex : SUPERCLUSTER, LIKELIHOOD)
1161 // jobmin - beg jobList index array
1162 // jobmax - end jobList index array
1163 //
1164 
1165  vector<waveSegment> _jobList(jobList.size());
1166  for(int i=0;i<(int)jobList.size();i++) _jobList[i].index=jobList[i];
1167  return createDagFile(_jobList, condor_dir, label, jobFiles, stage, jobmin, jobmax);
1168 }
1169 
1170 //______________________________________________________________________________
1171 int
1173  int jobmin, int jobmax) {
1174 //
1175 // produce the condor dag file for CWB_STAGE_FULL stage
1176 //
1177 // Input: jobList - list of job : only waveSegment::index is used
1178 // condor_dir - dag,sub condor directory
1179 // label - label used for dag file = 'condor_dir'/'label'.dag
1180 // jobmin - beg jobList index array
1181 // jobmax - end jobList index array
1182 //
1183 
1184  vector<TString> jobFiles;
1185  return createDagFile(jobList, condor_dir, label, jobFiles, "CWB_STAGE_FULL", jobmin, jobmax);
1186 }
1187 
1188 //______________________________________________________________________________
1189 int
1191  vector<TString> jobFiles, TString stage, int jobmin, int jobmax) {
1192 //
1193 // produce the condor dag file for stages
1194 //
1195 // Input: jobList - list of job : only waveSegment::index is used
1196 // condor_dir - dag,sub condor directory
1197 // label - label used for dag file = 'condor_dir'/'label'.dag
1198 // jobFiles - name of job files created by the previous stage
1199 // stage - analysis stage (Ex : SUPERCLUSTER, LIKELIHOOD)
1200 // jobmin - beg jobList index array
1201 // jobmax - end jobList index array
1202 //
1203 
1204  if(jobFiles.size()==0) { // fill jobFile with CWB_UPARAMETERS_FILE env
1206  if(gSystem->Getenv("CWB_UPARAMETERS_FILE")!=NULL) {
1207  cwb_uparameters_file=TString(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
1208  if(cwb_uparameters_file!="") checkFile(cwb_uparameters_file);
1209  jobFiles.resize(jobList.size());
1210  for(int n=0;n<(int)jobFiles.size();n++) jobFiles[n]=cwb_uparameters_file;
1211  } else {
1212  cout << "CWB::Toolbox::createDagFile : Error - CWB_UPARAMETERS_FILE env is not defined" << endl;
1213  gSystem->Exit(1);
1214  }
1215  }
1216  if(jobFiles.size()!=jobList.size()) {
1217  cout << "CWB::Toolbox::createDagFile : Error - jobFiles size " << jobFiles.size() <<
1218  "is != jobList size " << jobList.size() << endl;
1219  gSystem->Exit(1);
1220  }
1221 
1222  int start = jobmin<1 ? 1 : jobmin;
1223  int end = jobmax<1||jobmax>(int)jobList.size() ? jobList.size() : jobmax;
1224 
1225  char ofile[1024];
1226  sprintf(ofile,"%s/%s.dag",condor_dir.Data(),label.Data());
1227  ofstream out;
1228  out.open(ofile,ios::out);
1229 
1230  // read jobList
1231  int jID=0;
1232  for(int n=end;n>=start;n--) {
1233  if(jobFiles[n-1]=="") continue;
1234  jID = jobList[n-1].index;
1235  char ostring[1024];
1236  sprintf(ostring,"JOB A%i %s/%s.sub",jID,condor_dir.Data(),label.Data());
1237  out << ostring << endl;
1238  sprintf(ostring,"VARS A%i PID=\"%i\" CWB_UFILE=\"%s\" CWB_STAGE=\"%s\"",
1239  jID,jID,jobFiles[n-1].Data(),stage.Data());
1240  out << ostring << endl;
1241  if(gSystem->Getenv("_USE_OSG")!=NULL) {
1242  sprintf(ostring,"SCRIPT POST A%i %s/cwb_net_osg_post.sh %i",jID,gSystem->Getenv("CWB_SCRIPTS"),jID);
1243  out << ostring << endl;
1244  } else {
1245  sprintf(ostring,"RETRY A%i 3000",jID);
1246  out << ostring << endl;
1247  }
1248  }
1249  out.close();
1250  return 0;
1251 }
1252 
1253 //______________________________________________________________________________
1254 int
1256  int jobmin, int jobmax) {
1257 //
1258 // produce the condor dag file for CWB_STAGE_FULL stage
1259 //
1260 // Input: slagList - list of jobId : only slag::index is used
1261 // condor_dir - dag,sub condor directory
1262 // label - label used for dag file = 'condor_dir'/'label'.dag
1263 // jobmin - beg jobList index array
1264 // jobmax - end jobList index array
1265 //
1266 
1267  vector<TString> jobFiles;
1268  return createDagFile(slagList, condor_dir, label, jobFiles, "CWB_STAGE_FULL", jobmin, jobmax);
1269 }
1270 
1271 //______________________________________________________________________________
1272 int
1274  vector<TString> jobFiles, TString stage, int jobmin, int jobmax) {
1275 //
1276 // produce the condor dag file for stages
1277 //
1278 // Input: slagList - list of jobId : only slag::index is used
1279 // condor_dir - dag,sub condor directory
1280 // label - label used for dag file = 'condor_dir'/'label'.dag
1281 // jobFiles - name of job files created by the previous stage
1282 // stage - analysis stage (Ex : SUPERCLUSTER, LIKELIHOOD)
1283 // jobmin - beg jobList index array
1284 // jobmax - end jobList index array
1285 //
1286 
1287  if(jobFiles.size()==0) { // fill jobFile with CWB_UPARAMETERS_FILE env
1289  if(gSystem->Getenv("CWB_UPARAMETERS_FILE")!=NULL) {
1290  cwb_uparameters_file=TString(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
1291  if(cwb_uparameters_file!="") checkFile(cwb_uparameters_file);
1292  jobFiles.resize(slagList.size());
1293  for(int n=0;n<(int)jobFiles.size();n++) jobFiles[n]=cwb_uparameters_file;
1294  } else {
1295  cout << "CWB::Toolbox::createDagFile : Error - CWB_UPARAMETERS_FILE env is not defined" << endl;
1296  gSystem->Exit(1);
1297  }
1298  }
1299  if(jobFiles.size()!=slagList.size()) {
1300  cout << "CWB::Toolbox::createDagFile : Error - jobFiles size " << jobFiles.size() <<
1301  "is != slagList size " << slagList.size() << endl;
1302  gSystem->Exit(1);
1303  }
1304 
1305  int start = jobmin<1 ? 1 : jobmin;
1306  int end = jobmax<1||jobmax>(int)slagList.size() ? slagList.size() : jobmax;
1307 
1308  char ofile[1024];
1309  sprintf(ofile,"%s/%s.dag",condor_dir.Data(),label.Data());
1310  ofstream out;
1311  out.open(ofile,ios::out);
1312 
1313  // read slagList // SLAG
1314  int jID=0;
1315  for(int n=end;n>=start;n--) {
1316  if(jobFiles[n-1]=="") continue;
1317  jID = slagList[n-1].jobId;
1318  char ostring[1024];
1319  sprintf(ostring,"JOB A%i %s/%s.sub",jID,condor_dir.Data(),label.Data());
1320  out << ostring << endl;
1321  sprintf(ostring,"VARS A%i PID=\"%i\" CWB_UFILE=\"%s\" CWB_STAGE=\"%s\"",
1322  jID,jID,jobFiles[n-1].Data(),stage.Data());
1323  out << ostring << endl;
1324  sprintf(ostring,"RETRY A%i 3000",jID);
1325  out << ostring << endl;
1326  if(gSystem->Getenv("_USE_OSG")!=NULL) {
1327  sprintf(ostring,"SCRIPT POST A%i %s/cwb_net_osg_post.sh",jID,gSystem->Getenv("CWB_SCRIPTS"));
1328  out << ostring << endl;
1329  }
1330  }
1331  out.close();
1332  return 0;
1333 }
1334 
1335 //______________________________________________________________________________
1336 int
1338  TString log_dir, TString ext, TString condor_tag) {
1339 //
1340 // produce the condor sub file
1341 //
1342 // Input: label - label used for dag file = 'condor_dir'/'label'.dag
1343 // condor_dir - dag,sub condor directory
1344 // out_dir - out condor files directory
1345 // err_dir - err condor files directory
1346 // ext - condor directory
1347 // condor_tag - Define a Unique Tag for Condor Jobs
1348 //
1349 
1350  char ofile[1024];
1351  if(ext=="")
1352  sprintf(ofile,"%s/%s.sub",condor_dir.Data(),label.Data());
1353  else
1354  sprintf(ofile,"%s/%s.sub.%s",condor_dir.Data(),label.Data(),ext.Data());
1355 
1356  FILE *fP=NULL;
1357  if((fP = fopen(ofile, "w")) == NULL) {
1358  cout << "CWB::Toolbox::createSubFile : Error - cannot open file " << ofile << endl;
1359  gSystem->Exit(1);
1360  }
1361 
1362  fprintf(fP,"universe = vanilla\n");
1363  fprintf(fP,"getenv = true\n");
1364  fprintf(fP,"priority = $(PRI)\n");
1365  fprintf(fP,"on_exit_hold = ( ExitCode != 0 )\n");
1366  fprintf(fP,"request_memory = 3000\n");
1367  fprintf(fP,"executable = cwb.sh\n");
1368  fprintf(fP,"job_machine_attrs = Machine\n");
1369  fprintf(fP,"job_machine_attrs_history_length = 5\n");
1370  fprintf(fP,"requirements = target.machine =!= MachineAttrMachine1 && target.machine =!= MachineAttrMachine2 && target.machine =!= MachineAttrMachine3 && target.machine =!= MachineAttrMachine4 && target.machine =!= MachineAttrMachine5\n");
1371  fprintf(fP,"environment = CWB_JOBID=$(PID);CWB_UFILE=$(CWB_UFILE);CWB_STAGE=$(CWB_STAGE)\n");
1372  if(condor_tag!="") fprintf(fP,"accounting_group = %s\n",condor_tag.Data());
1373  fprintf(fP,"output = %s/$(PID)_%s_$(CWB_STAGE).out\n",out_dir.Data(),label.Data());
1374  fprintf(fP,"error = %s/$(PID)_%s_$(CWB_STAGE).err\n",err_dir.Data(),label.Data());
1375  if(gSystem->Getenv("_USE_OSG")!=NULL) {
1376  TString home_dir= TString(gSystem->Getenv("HOME"));
1377  //cout<<home_dir.Data()<<endl;
1378  fprintf(fP,"log = %s/condor/%s.log\n",home_dir.Data(),label.Data());
1379  fprintf(fP,"should_transfer_files = YES\n");
1380  fprintf(fP,"when_to_transfer_output = ON_EXIT\n");
1381  fprintf(fP,"transfer_input_files = %s/.rootrc, %s/%s.tgz\n",home_dir.Data(), condor_dir.Data(),label.Data());
1382  fprintf(fP,"transfer_output_files = %s/output, %s/log\n", label.Data(), label.Data());
1383  fprintf(fP,"request_memory = 2000\n");
1384  } else {
1385  fprintf(fP,"log = %s/%s.log\n",log_dir.Data(),label.Data());
1386  }
1387  fprintf(fP,"notification = never\n");
1388  fprintf(fP,"rank=memory\n");
1389  fprintf(fP,"queue\n");
1390 
1391  fclose(fP);
1392 
1393  return 0;
1394 }
1395 
1396 //______________________________________________________________________________
1397 vector<int>
1399 //
1400 // get the job list from the dag condor file
1401 //
1402 // Input: condor_dir - dag,sub condor directory
1403 // label - label used for dag file = 'condor_dir'/'label'.dag
1404 //
1405 
1406  char ifile[1024];
1407  sprintf(ifile,"%s/%s.dag",condor_dir.Data(),label.Data());
1408  ifstream in;
1409  in.open(ifile);
1410  if(!in.good()) {cout << "Error Opening File : " << ifile << endl;gSystem->Exit(1);}
1411 
1412  vector<int> jobs;
1413 
1414  char istring[1024];
1415  while(1) {
1416  in.getline(istring,1024);
1417  if (!in.good()) break;
1418  TObjArray* token = TString(istring).Tokenize(TString(' '));
1419  TObjString* stoken =(TObjString*)token->At(0);
1420  TString jobLabel = stoken->GetString();
1421  if(jobLabel.CompareTo("JOB")!=0) continue;
1422  stoken =(TObjString*)token->At(1);
1423  int jobID=TString(stoken->GetString()).ReplaceAll("A","").Atoi();
1424  jobs.push_back(jobID);
1425  if(token) delete token;
1426  }
1427 
1428  in.close();
1429 
1430  vector<int> jobList;
1431  Int_t *id = new Int_t[jobs.size()];
1432  Int_t *jd = new Int_t[jobs.size()];
1433  for(int i=0;i<(int)jobs.size();i++) jd[i]=jobs[i];
1434  TMath::Sort((int)jobs.size(),jd,id,false);
1435  for(int i=0;i<(int)jobs.size();i++) jobList.push_back(jd[id[i]]);
1436  jobs.clear();
1437  delete [] id;
1438  delete [] jd;
1439 
1440  return jobList;
1441 }
1442 
1443 //______________________________________________________________________________
1444 vector<float>
1445 CWB::Toolbox::getJobBenchmark(TString ifName, int stageID, TString bench) {
1446 //
1447 // extract benchmark info from bench status history line
1448 // return in vector vbench : vbench[0]=job number - vbench[1]=bench value
1449 //
1450  return getJobBenchmark(ifName, stageID, -1, -1, bench);
1451 }
1452 
1453 //______________________________________________________________________________
1454 vector<float>
1455 CWB::Toolbox::getJobBenchmark(TString ifName, int stageID, int resID, int factorID, TString bench) {
1456 //
1457 // extract benchmark info from bench status history line
1458 // return in vector vbench : vbench[0]=job number - vbench[1]=bench value
1459 //
1460 // These are the format of the bench status history line
1461 // GPS:X1-JOB:X2-STG:X3-FCT:X4-JET:X5-SET:X6-MEM:X7-JFS:X8
1462 // GPS:X1-JOB:X2-STG:X3-FCT:X4-RES:X5-THR:X6
1463 // GPS:X1-JOB:X2-STG:X3-FCT:X4-RES:X5-CSIZE:X6-PSIZE:X7
1464 //
1465 // ifName - root file name containig the history object
1466 // stageID - stage ID (defined in cwb.hh)
1467 // resID - resolution level ID
1468 // factorID - factor ID (defined in user_parameters.C)
1469 //
1470 // bench is :
1471 // GPS : gps time (sec)
1472 // JOB : job number
1473 // STG : stage number (defined in cwb.hh)
1474 // FCT : factors index array
1475 // JET : Job Elapsed Time (sec)
1476 // SET : Stage Elapsed Time (sec)
1477 // MEM : Memory usage (MB)
1478 // JFS : Job File Size - temporary file (bytes)
1479 // THR : Threshold @ Coherence Stage
1480 // CSIZE : cluster size per lag @ coherence stage
1481 // PSIZE : pixels size per lag @ coherence stage
1482 //
1483 
1484  vector<float> vbench(2);
1485  vbench[0]=-1; // job number
1486  vbench[1]=-1; // bench value
1487 
1488  TFile *ifile = TFile::Open(ifName);
1489  if(ifile==NULL) {
1490  cout << "Failed to open " << ifName.Data() << endl;
1491  return vbench;
1492  }
1493 
1494  CWB::History* ihistory = (CWB::History*)ifile->Get("history");
1495  if(ihistory==NULL) {
1496  cout << "Error : history is not present!!!" << endl;
1497  return vbench;
1498  }
1499 
1500  // build stage tag in the bench line status
1501  char stageName[64]; sprintf(stageName,"STG:%d-",stageID);
1502 
1503  // build resolution tag in the bench line status
1504  char resName[64]; sprintf(resName,"RES:%d-",resID);
1505 
1506  // build factor tag in the bench line status
1507  char factorName[64]; sprintf(factorName,"FCT:%d-",factorID);
1508 
1509  int log_size = ihistory->GetLogSize(CCAST("FULL"));
1510  for(int i=0;i<log_size;i++) {
1511  TString log = ihistory->GetLog(CCAST("FULL"),i);
1512  //cout << "log " << log.Data() << endl;
1513  if(!log.Contains(stageName)) continue;
1514  if(resID>=0 && !log.Contains(resName)) continue;
1515  if(factorID>=0 && !log.Contains(factorName)) continue;
1516  if(log.Contains(bench+TString(":"))) {
1517  TObjArray* token = log.Tokenize('\n');
1518  for(int j=0;j<token->GetEntries();j++) {
1519  TString line = ((TObjString*)token->At(j))->GetString();
1520  // extract bench line which contains the "bench:" string
1521  if(line.Contains(bench+TString(":"))) {
1522  //cout << j << " " << (((TObjString*)token->At(j))->GetString()).Data() << endl;
1523  // extract tokens separated by "-"
1524  TObjArray* ltoken = line.Tokenize('-');
1525  for(int k=0;k<ltoken->GetEntries();k++) {
1526  TString stat = ((TObjString*)ltoken->At(k))->GetString();
1527  //cout << k << " " << stat.Data() << endl;
1528  TObjArray* stoken = stat.Tokenize(':');
1529  if(stoken->GetEntries()!=2) continue;
1530  TString stat_name = ((TObjString*)stoken->At(0))->GetString();
1531  TString stat_value = ((TObjString*)stoken->At(1))->GetString();
1532  //cout << stat_name.Data() << " " << stat_value.Data() << endl;
1533  // extract value associated to bench "bench:value"
1534  if(stat_name==bench) {
1535  float value = stat_value.Atof();
1536  if(value>vbench[1]) vbench[1]=value;
1537  //cout << stat_name.Data() << " " << stat_value.Data() << endl;
1538  }
1539  if(stat_name=="JOB") {
1540  vbench[0] = stat_value.Atoi(); // Get JOB ID from bench status line
1541  }
1542  delete stoken;
1543  }
1544  delete ltoken;
1545  }
1546  }
1547  delete token;
1548  }
1549  }
1550 
1551  ifile->Close();
1552 
1553  // if jobID is not present in the bench line then it is extracted from file name
1554  if(vbench[0]==-1) vbench[0] = getJobId(ifName);
1555 
1556  return vbench;
1557 }
1558 
1559 //______________________________________________________________________________
1560 TString
1561 CWB::Toolbox::DAG2LSF(char* dagFile, char* data_label, char* nodedir, char* data_dir,
1562  char* condor_dir, char* log_dir, char* output_dir, char* work_dir) {
1563 //
1564 // Input: dagFile - Dag File
1565 // data_label - data label
1566 // nodedir - temporary dir directory
1567 // data_dir - data directory
1568 // condor_dir - condor directory
1569 // log_dir - log dir
1570 // output_dir - output dir
1571 // work_dir - working dir
1572 //
1573 // Output: input dagFile is used to produce the script LSF file
1574 // if number of jobs>0 return lsf file name otherwise empty string
1575 //
1576 
1577  // get user name
1578  UserGroup_t* uinfo = gSystem->GetUserInfo();
1579  TString uname = uinfo->fUser;
1580 
1581  // get home wat path
1582  TString cwb_home_wat="";
1583  if(gSystem->Getenv("HOME_WAT")!=NULL) {
1584  cwb_home_wat=TString(gSystem->Getenv("HOME_WAT"));
1585  }
1586  if(cwb_home_wat=="") {
1587  cout << "CWB::Toolbox::DAG2LSF : Error : HOME_WAT not defined !!!" << endl;
1588  gSystem->Exit(1);
1589  }
1590 
1591  // get LSF queue name
1592  TString lsf_queue="";
1593  if(gSystem->Getenv("LSF_QUEUE")!=NULL) {
1594  lsf_queue=TString(gSystem->Getenv("LSF_QUEUE"));
1595  }
1596  if(lsf_queue=="") {
1597  cout << "CWB::Toolbox::DAG2LSF : Error : LSF_QUEUE not defined !!!" << endl;
1598  gSystem->Exit(1);
1599  }
1600 
1601  // --------------------------------------------------
1602  // create LSF job script file
1603  // --------------------------------------------------
1604 
1605  // open dag file
1606  ifstream in;
1607  in.open(dagFile);
1608  if(!in.good()) {
1609  cout << "CWB::Toolbox::DAG2LSF - Error Opening File : " << dagFile << endl;
1610  gSystem->Exit(1);
1611  }
1612 
1613  // create LSF file
1614  TString lsfFile = dagFile;
1615  lsfFile.ReplaceAll(".dag",".lsf");
1616  ofstream out;
1617  out.open(lsfFile.Data(),ios::out);
1618  if(!out.good()) {
1619  cout << "CWB::Toolbox::DAG2LSF - Error Opening File : " << lsfFile << endl;
1620  gSystem->Exit(1);
1621  }
1622 
1623  out << "#!/bin/bash" << endl << endl;
1624 
1625  int lsf_job_cnt=0;
1626  char istring[1024];
1627  while(1) {
1628  int PID=0;
1629  TString CWB_UFILE="";
1630  TString CWB_STAGE="";
1631  in.getline(istring,1024);
1632  if (!in.good()) break;
1633  if(!TString(istring).BeginsWith("VARS")) continue;
1634  TObjArray* token = TString(istring).Tokenize(TString(' '));
1635  for(int j=2;j<token->GetEntries();j++) {
1636  TString item = ((TObjString*)token->At(j))->GetString();
1637  if(item.BeginsWith("PID=")) {
1638  item.ReplaceAll("PID=","");
1639  item.ReplaceAll("\"","");
1640  PID = item.Atoi();
1641  continue;
1642  }
1643  if(item.BeginsWith("CWB_UFILE=")) {
1644  item.ReplaceAll("CWB_UFILE=","");
1645  item.ReplaceAll("\"","");
1646  CWB_UFILE=item;;
1647  continue;
1648  }
1649  if(item.BeginsWith("CWB_STAGE=")) {
1650  item.ReplaceAll("CWB_STAGE=","");
1651  item.ReplaceAll("\"","");
1652  CWB_STAGE=item;;
1653  continue;
1654  }
1655  }
1656  //cout << "PID : " << PID << " CWB_UFILE : " << CWB_UFILE << " CWB_STAGE : " << CWB_STAGE << endl;
1657  char lsf_cmd[2048];
1658  char lsf_label[1024]; // label used in the execution node
1659  if(CWB_STAGE=="CWB_STAGE_FULL") {
1660  sprintf(lsf_label,"%s",data_label);
1661  } else {
1662  sprintf(lsf_label,"%s_%s",data_label,CWB_STAGE.Data());
1663  }
1664  sprintf(lsf_cmd,"bsub -q %s -J A%d -g /%s/%s \
1665  \\\n -f \"%s > %s/%d_%s.ufile\" \
1666  \\\n -f \"%s/%s.tgz > %s/%d_%s.tgz\" \
1667  \\\n -o %d_%s.out -f \"%s/%s/%d_%s.out < %d_%s.out\" \
1668  \\\n -e %d_%s.err -f \"%s/%s/%d_%s.err < %d_%s.err\" \
1669  \\\n -f \"%s/%s/%d_%s.tgz < %s/%d_%s.tgz\" \
1670  \\\n -Ep \"rm %d_%s.out\" \
1671  \\\n -Ep \"rm %d_%s.err\" \
1672  \\\n -Ep \"rm %s/%d_%s.tgz\" \
1673  \\\n \"/bin/bash %s/tools/cwb/scripts/cwb_net_lsf.sh %d %s %s %s %s %s\"",
1674  lsf_queue.Data(), PID, uname.Data(), data_label,
1675  CWB_UFILE.Data(), nodedir, PID, lsf_label,
1676  condor_dir, lsf_label, nodedir, PID, lsf_label,
1677  PID, lsf_label, work_dir, log_dir, PID, lsf_label, PID, lsf_label,
1678  PID, lsf_label, work_dir, log_dir, PID, lsf_label, PID, lsf_label,
1679  work_dir, output_dir, PID, lsf_label, nodedir, PID, lsf_label,
1680  PID, lsf_label,
1681  PID, lsf_label,
1682  nodedir, PID, lsf_label,
1683  cwb_home_wat.Data(), PID, CWB_STAGE.Data(), nodedir, data_label, data_dir, CWB_UFILE.Data());
1684  out << lsf_cmd << endl << endl;
1685  //cout << lsf_cmd << endl;
1686  if(token) delete token;
1687  //if(PID%1000==0) cout << PID << endl;
1688  lsf_job_cnt++;
1689  }
1690  out.close();
1691  in.close();
1692 
1693  return lsf_job_cnt>0 ? lsfFile : "";
1694 }
1695 
1696 //______________________________________________________________________________
1697 vector<int>
1699 //
1700 // return vector job numbers extracted from the merged file list
1701 // the job number ID is extracted from the file name : XXX_jobID.root
1702 //
1703 // Input: merge_dir - merge directory
1704 // label - label of the merge name list
1705 // version - version of the merge name list
1706 // - merge name list = 'merge_dir'/merge_'label'.M'version.lst
1707 //
1708 
1709  char ifile[1024];
1710  sprintf(ifile,"%s/merge_%s.M%d.lst",merge_dir.Data(),label.Data(),version);
1711  vector<TString> jfname;
1712  return getMergeJobList(ifile,jfname);
1713 }
1714 
1715 //______________________________________________________________________________
1716 vector<int>
1718 //
1719 // return vector job numbers extracted from the ifname file list
1720 // the job number ID is extracted from the file name : XXX_jobID.root
1721 //
1722 // Input: ifname - input file name which contains the list of files
1723 //
1724 
1725  vector<TString> jfname;
1726  return getMergeJobList(ifname,jfname);
1727 }
1728 
1729 //______________________________________________________________________________
1730 vector<int>
1731 CWB::Toolbox::getMergeJobList(TString ifname, vector<TString>& jobFileList) {
1732 //
1733 // return vector job numbers extracted from the ifname file list
1734 // the job number ID is extracted from the file name : XXX_jobID.root
1735 //
1736 // Input: ifname - input file name which contains the list of files
1737 //
1738 // Output: jobFileList - the job file names list
1739 //
1740 
1741  ifstream in;
1742  in.open(ifname.Data());
1743  if(!in.good()) {
1744  cout << "CWB::Toolbox::getMergeJobList : Error Opening File : " << ifname << endl;
1745  gSystem->Exit(1);
1746  }
1747 
1748  vector<int> job;
1749  vector<TString> jfname;
1750 
1751  char istring[1024];
1752  while(1) {
1753  in.getline(istring,1024);
1754  if (!in.good()) break;
1755  TObjArray* token = TString(istring).Tokenize(TString('_'));
1756  TObjString* stoken =(TObjString*)token->At(token->GetEntries()-1);
1757  int jobID=TString(stoken->GetString()).ReplaceAll("job","").ReplaceAll(".root","").Atoi();
1758  if(jobID==0) continue; // file root is a merge file not a job file
1759  job.push_back(jobID);
1760  jfname.push_back(istring);
1761  if(token) delete token;
1762  }
1763 
1764  in.close();
1765 
1766  jobFileList.clear();
1767  vector<int> jobList;
1768  Int_t *id = new Int_t[job.size()];
1769  Int_t *jd = new Int_t[job.size()];
1770  for(int i=0;i<(int)job.size();i++) jd[i]=job[i];
1771  TMath::Sort((int)job.size(),jd,id,false);
1772  for(int i=0;i<(int)job.size();i++) {
1773  jobList.push_back(jd[id[i]]);
1774  jobFileList.push_back(jfname[id[i]]);
1775  }
1776  job.clear();
1777  jfname.clear();
1778  delete [] id;
1779  delete [] jd;
1780 
1781  return jobList;
1782 }
1783 
1784 /*--------------------------------------------------------------------------
1785  return the list of segments with length=segLen contained in the interval
1786  ilist time_max-time_min
1787  the edges of the segments are a multiple of segLen
1788 --------------------------------------------------------------------------*/
1789 //______________________________________________________________________________
1790 vector<waveSegment>
1791 CWB::Toolbox::getSlagJobList(vector<waveSegment> ilist, int seglen) {
1792 //
1793 // Extract Time MIN and MAX from the ilist
1794 // recompute MIN MAX to be a integer multiple of seglen
1795 // Compute the list of segments with length seglen within the MIN-MAX range
1796 //
1797 //
1798 // Input: ilist - list of segments (start,stop)
1799 // seglen - segment length
1800 //
1801 // Output: return the list of segment time ranges
1802 //
1803 
1804  if(ilist.size()==0) {cout << "CWB::Toolbox::getSlagJobList - Error ilist size=0" << endl;gSystem->Exit(1);}
1805  if(seglen<=0) {cout << "CWB::Toolbox::getSlagJobList - Error seglen<=0" << endl;gSystem->Exit(1);}
1806 
1807  waveSegment SEG;
1808  vector<waveSegment> jlist;
1809 
1810  int start=ilist[0].start;
1811  int stop=ilist[ilist.size()-1].stop;
1812  start=seglen*TMath::Nint(double(start/seglen));
1813  stop=seglen*TMath::Nint(double(stop/seglen));
1814 
1815  int njob=(stop-start)/seglen;
1816  for(int n=0;n<njob;n++) {
1817  SEG.start=start+n*seglen;
1818  SEG.stop=SEG.start+seglen;
1819  jlist.push_back(SEG);
1820  }
1821 
1822  return jlist;
1823 }
1824 
1825 //______________________________________________________________________________
1826 waveSegment
1827 CWB::Toolbox::getMaxSeg(vector<waveSegment> list) {
1828 //
1829 //
1830 // Input: list - segment list
1831 //
1832 // Return the segment with the maximum length
1833 //
1834 
1835  waveSegment SEG={0,0,0};
1836  for(int i=0;i<(int)list.size();i++) {
1837  if((list[i].stop-list[i].start)>(SEG.stop-SEG.start)) SEG=list[i];
1838  }
1839  return SEG;
1840 }
1841 
1842 //______________________________________________________________________________
1843 slag
1844 CWB::Toolbox::getSlag(vector<slag> slagList, int jobid) {
1845 //
1846 //
1847 // Input: slagList - slag list
1848 // jobid - job id
1849 //
1850 // Return SLAG structure with slagList.jobId == jobid
1851 //
1852 
1853  slag SLAG;SLAG.jobId=-1;
1854  for(int j=0;j<(int)slagList.size();j++) if(slagList[j].jobId==jobid) {SLAG=slagList[j];break;}
1855  return SLAG;
1856 }
1857 
1858 //______________________________________________________________________________
1859 vector<slag>
1860 CWB::Toolbox::getSlagList(vector<slag> islagList, vector<TString> ifos,
1861  double segLen, double segMin, double segEdge,
1862  int nDQF, dqfile* iDQF, CWB_CAT dqcat) {
1863 //
1864 //
1865 // Input: islagList - vector list of slag structures
1866 // ifos - vector list of ifo names
1867 // segLen - Segment length [sec]
1868 // segMin - Minimum Segment Length after dqcat [sec]
1869 // segEdge - wavelet boundary offset [sec]
1870 // nDQF - size of iDQF array
1871 // iDQF - DQ structure array
1872 // dqcat - dq cat
1873 //
1874 // if dqcat=CWB_CAT1 -> return the list of slags with dq len > segMin+2*segEdge
1875 // if dqcat=CWB_CAT2 -> return the list of slags with dq len > segMin
1876 //
1877 
1878  // dqcat must be CWB_CAT1 or CWB_CAT2
1879  if((dqcat!=CWB_CAT1)&&(dqcat!=CWB_CAT2)) {
1880  cout << "CWB::Toolbox::getSlagList : dqcat must be CWB_CAT1 or CWB_CAT2 !!!" << endl;
1881  gSystem->Exit(1);
1882  }
1883 
1884  int nIFO=0;
1885  slag SLAG;
1886  int lsize=islagList.size();
1887  int nRejected=0;
1888  int livetime1=0;
1889  int livetime2=0;
1890  int segLength=0;
1891  waveSegment SEG,MSEG;
1892  vector<waveSegment> dqList;
1893  vector<waveSegment> dq1List;
1894  vector<waveSegment> jobList;
1895  vector<waveSegment> segList;
1896  vector<slag> oslagList;
1897  int jobID=0;int segID[20];int slagID=0;
1898 
1899  if(lsize>0) nIFO=islagList[0].segId.size();
1900 
1901  dqfile* DQF = new dqfile[nDQF];
1902  for(int i=0;i<nDQF;i++) DQF[i]=iDQF[i];
1903 
1904  // get zero lag merged dq cat 1 list
1905  dq1List=readSegList(nDQF, DQF, CWB_CAT1);
1906  // get number/list of the available super lag jobs
1907  jobList=getSlagJobList(dq1List, segLen);
1908  cout << "input list size " << lsize << endl;
1909  cout << "jobList size " << jobList.size() << endl;
1910  dqList.clear();
1911 
1912  // read dq lists from files and store in ilist
1913  vector<waveSegment>* ilist = new vector<waveSegment>[nDQF];
1914  for(int i=0;i<nDQF;i++) ilist[i]=readSegList(DQF[i]);
1915 
1916  // get range segment for this job intersecting with cat1 list (dqList)
1917  for(int j=0;j<lsize;j++) {
1918  if(j%1000==0) printf("%6d - Rejected slags = %d/%lu\n",j,nRejected,islagList.size());
1919  SLAG = islagList[j]; // slag structure
1920  slagID = SLAG.slagId[0];
1921  jobID = SLAG.jobId;
1922  for(int n=0; n<nIFO; n++) segID[n]=SLAG.segId[n];
1923  cout.precision(2);
1924 
1925  // shift dq files
1926  for(int i=0;i<nDQF;i++) DQF[i].shift=0.;
1927  setSlagShifts(SLAG, ifos, segLen, nDQF, DQF);
1928 
1929  // create shifted merged dq cat file list
1930  dqList.clear();
1931  bool first=true;
1932  for(int i=0;i<nDQF;i++) {
1933  if(DQF[i].cat<=dqcat) {
1934  for(int k=0;k<(int)ilist[i].size();k++) { // apply time shifts
1935  ilist[i][k].start+=DQF[i].shift;
1936  ilist[i][k].stop+=DQF[i].shift;
1937  }
1938  if(first) {dqList=ilist[i];first=false;}
1939  else dqList = mergeSegLists(ilist[i],dqList);
1940  for(int k=0;k<(int)ilist[i].size();k++) { // restore time shifts
1941  ilist[i][k].start-=DQF[i].shift;
1942  ilist[i][k].stop-=DQF[i].shift;
1943  }
1944  }
1945  }
1946 
1947  // create job file list
1948  if(dqList.size()==0) continue;
1949 
1950  segList.clear();
1951  SEG.start = jobList[segID[0]-1].start-segEdge;
1952  SEG.stop = jobList[segID[0]-1].stop+segEdge;
1953 
1954  if(dqcat==CWB_CAT2) {
1955 
1956  // create shifted merged dq cat1 file list
1957  dq1List.clear();
1958  bool first=true;
1959  for(int i=0;i<nDQF;i++) {
1960  if(DQF[i].cat<=CWB_CAT1) {
1961  for(int k=0;k<(int)ilist[i].size();k++) { // apply time shifts
1962  ilist[i][k].start+=DQF[i].shift;
1963  ilist[i][k].stop+=DQF[i].shift;
1964  }
1965  if(first) {dq1List=ilist[i];first=false;}
1966  else dq1List = mergeSegLists(ilist[i],dq1List);
1967  for(int k=0;k<(int)ilist[i].size();k++) { // restore time shifts
1968  ilist[i][k].start-=DQF[i].shift;
1969  ilist[i][k].stop-=DQF[i].shift;
1970  }
1971  }
1972  }
1973 
1974  segList.push_back(SEG);
1975  segList = mergeSegLists(dq1List,segList); // merge detector segments with dq cat 1
1976  SEG = getMaxSeg(segList); // extract max length segment (only this segment is used for analysis)
1977  SEG.start+=segEdge;
1978  SEG.stop-=segEdge;
1979  segList.clear();
1980  }
1981 
1982  segList.push_back(SEG);
1983  segList = mergeSegLists(dqList,segList); // merge detector segments with dq cat
1984  double lenTHR;
1985  if(dqcat==CWB_CAT1) {
1986  MSEG = getMaxSeg(segList); // max length segment
1987  segLength=MSEG.stop-MSEG.start;
1988  lenTHR=segMin+2*segEdge;
1989  }
1990  if(dqcat==CWB_CAT2) {
1991  segLength=getTimeSegList(segList);
1992  lenTHR=segMin;
1993  }
1994  livetime1+=segLength;
1995  if(segLength<lenTHR) {
1996  nRejected++;
1997  } else {
1998  livetime2+=segLength;
1999  oslagList.push_back(SLAG);
2000  }
2001  }
2002  printf("%6lu - Rejected slags = %d/%lu\n\n",islagList.size(),nRejected,islagList.size());
2003  printf("Slag livetime before dq cat%d is approximately %d sec \t= %.2f days\n",dqcat,livetime1,double(livetime1)/86400.);
2004  printf("Slag livetime after dq cat%d is approximately %d sec \t= %.2f days\n",dqcat,livetime2,double(livetime2)/86400.);
2005 
2006  dqList.clear();
2007  dq1List.clear();
2008  for(int i=0;i<nDQF;i++) ilist[i].clear();
2009  delete [] ilist;
2010  delete [] DQF;
2011 
2012  return oslagList;
2013 }
2014 
2015 //______________________________________________________________________________
2016 void
2017 CWB::Toolbox::setSlagShifts(slag SLAG, vector<TString> ifos, double segLen, int nDQF, dqfile* DQF) {
2018 //
2019 // Apply slag shifts into the DQF structures (DQF[].shift)
2020 //
2021 // Input: SLAG - slag structure
2022 // segLen - segment length
2023 // nDQF - size of DQF array
2024 // DQF - array of DQ structures
2025 //
2026 
2027 
2028  // shift dq files
2029  for(int i=0;i<nDQF;i++) {
2030  int ifoID=-1;
2031  for(int j=0;j<(int)ifos.size();j++) if(ifos[j]==DQF[i].ifo) ifoID=j;
2032  if(ifoID==-1) {
2033  cout << "CWB::Toolbox::setSlagShifts - " << DQF[i].ifo << " is not in the contained into ifos vector" << endl;
2034  gSystem->Exit(1);
2035  }
2036  if(ifoID>=(int)SLAG.segId.size()) {
2037  cout << "CWB::Toolbox::setSlagShifts - " << DQF[i].ifo << " index " << ifoID << "is not correct" << endl;
2038  gSystem->Exit(1);
2039  }
2040  DQF[i].shift+=-segLen*(SLAG.segId[ifoID]-SLAG.segId[0]); // apply slag shift to dq
2041  }
2042 
2043  return;
2044 }
2045 
2046 //______________________________________________________________________________
2047 vector<waveSegment>
2048 CWB::Toolbox::getSegList(slag SLAG, vector<waveSegment> jobList,
2049  double segLen, double segMLS, double segEdge,
2050  vector<waveSegment> dqList) {
2051 //
2052 // create merged dq cat 1 file list
2053 //
2054 // Input: SLAG - slag structure
2055 // jobList - list of available super lag segments
2056 // segLen - Segment length [sec]
2057 // segMLS - Minimum Segment Length after DQ_CAT1 [sec]
2058 // segEdge - wavelet boundary offset [sec]
2059 // dqList - dq cat1 list
2060 //
2061 // Return the detector's slag segment ranges associated to SLAG infos & dq cat1 list
2062 //
2063 
2064  waveSegment SEG,MSEG;
2065  vector<waveSegment> segList;
2066 
2067  int nIFO=SLAG.segId.size();
2068 
2069  // get range segment for this job intersecting with cat1 list (dqList)
2070  SEG.start = jobList[SLAG.segId[0]-1].start-segEdge;
2071  SEG.stop = jobList[SLAG.segId[0]-1].stop+segEdge;
2072  segList.push_back(SEG);
2073  segList = mergeSegLists(dqList,segList); // merge detector segments with dq cat 1
2074  MSEG = getMaxSeg(segList); // extract max length segment (only this segment is used for analysis)
2075  int segLength=MSEG.stop-MSEG.start;
2076  if(segLength<segMLS+2*segEdge) {
2077  cout << "CWB::Toolbox::getSegList - Segment length too small : " << segLength << endl;
2078  gSystem->Exit(1);
2079  }
2080 
2081  segList.clear();
2082  for(int n=0; n<nIFO; n++) {
2083  SEG.start = MSEG.start+segLen*(SLAG.segId[n]-SLAG.segId[0])+segEdge;
2084  SEG.stop = MSEG.stop+segLen*(SLAG.segId[n]-SLAG.segId[0])-segEdge;
2085  segList.push_back(SEG);
2086  }
2087  jobList.clear();
2088 
2089  return segList;
2090 }
2091 
2092 //_______________________________________________________________________________________
2093 double
2094 CWB::Toolbox::getLiveTime(vector<waveSegment>& jobList, vector<waveSegment>& dqList) {
2095 
2096  vector<double> dummy;
2097  return getLiveTime(jobList, dqList, dummy);
2098 }
2099 
2100 //_______________________________________________________________________________________
2101 double
2102 CWB::Toolbox::getLiveTime(vector<waveSegment>& jobList, vector<waveSegment>& dqList, vector<double> shiftList) {
2103 //
2104 // return live time in sec
2105 //
2106 // NOTE : this procedure compute the livetime of the jobList after dq=cat2 when lags are applied
2107 // the returned livetime should be with a good approximation consistent
2108 // with the one computed by the cWB algorithm
2109 //
2110 // Input: jobList - job list
2111 // dqList - data quality list
2112 // shiftList - list of time shifts (sec), the size is the detector number
2113 // shift[0]=shift_ifo_1, shift[1]=shift_ifo_2, ... shift[n-1]=shift_ifo_n
2114 // if shift list size is 0 then no shifts are applied
2115 //
2116 // Procedure : for each time shift and for each job the data quality list inside the job interval
2117 // are circulary rotated by a time=shift, the new shifted list is saved into a new segment list
2118 // finaly the new segment list are merged and the total livetime is returned
2119 //
2120 
2121  if(shiftList.size()==0) shiftList.push_back(0.);
2122  int nshift = shiftList.size(); // extract the number of shifts
2123 
2124  waveSegment seg;
2125  vector<waveSegment>* segList = new vector<waveSegment>[nshift];
2126  vector<waveSegment> mergeList=mergeSegLists(jobList,dqList);
2127 
2128  for(int i=0;i<nshift;i++) { // loop over shifts
2129  double shift = shiftList[i];
2130  if(shift==0) {segList[i]=mergeList;continue;} // skip if shift=0
2131  int jsize = jobList.size();
2132  for(int j=0;j<jsize;j++) { // loop over job list
2133  double jstart = jobList[j].start;
2134  double jstop = jobList[j].stop;
2135  double jlen = jstop-jstart;
2136  int ksize = mergeList.size();
2137  for(int k=0;k<ksize;k++) { // loop over dq list
2138  double kstart = mergeList[k].start;
2139  double kstop = mergeList[k].stop;
2140  if((kstop<=jstart)||(kstart>=jstop)) continue; // skip dq outside the job range
2141  // add shift
2142  kstart+=shift;
2143  kstop +=shift;
2144  // circular shift
2145  if(kstop<=jstop) {
2146  seg.start=kstart;seg.stop=kstop;segList[i].push_back(seg);
2147  }
2148  if(kstart>=jstop) {
2149  seg.start=kstart-jlen;seg.stop=kstop-jlen;segList[i].push_back(seg);
2150  }
2151  if((kstart<jstop)&&(kstop>jstop)) {
2152  seg.start=kstart;seg.stop=jstop;segList[i].push_back(seg);
2153  seg.start=jstart;seg.stop=kstop-jlen;segList[i].push_back(seg);
2154  }
2155  }
2156  }
2157  }
2158 
2159  // sort lists (needed by mergeSegLists)
2160  for(int i=0;i<nshift;i++) segList[i]=unionSegments(segList[i]);
2161 
2162  // merge segLists
2163  for(int i=1;i<nshift;i++) segList[0]=mergeSegLists(segList[0],segList[i]);
2164 
2165  double livetime=getTimeSegList(segList[0]);
2166  for(int i=0;i<nshift;i++) segList[i].clear();
2167  delete [] segList;
2168 
2169  return livetime;
2170 }
2171 
2172 //______________________________________________________________________________
2173 int
2174 CWB::Toolbox::shiftBurstMDCLog(std::vector<std::string>& mdcList, vector<TString> ifos, double mdc_shift) {
2175 //
2176 // apply a time shift to the log entries contained in the mdcList
2177 //
2178 // Input: mdcList - list of the log string (one entry for each injected MDC)
2179 // ifos - array of input detector's names
2180 // mdc_shift - time shift
2181 //
2182 
2183  cout.precision(14);
2184  for(int i=0;i<(int)mdcList.size();i++) {
2185  char mdc_string[1024]="";
2186  //cout << "IN-" << mdcList[i] << endl;
2187  TObjArray* token = TString(mdcList[i]).Tokenize(' ');
2188  sprintf(mdc_string,"%s",(((TObjString*)token->At(0))->GetString()).Data());
2189  for(int j=1;j<9;j++)
2190  sprintf(mdc_string,"%s %s",mdc_string,(((TObjString*)token->At(j))->GetString()).Data());
2191  double frTime = (((TObjString*)token->At(9))->GetString()).Atof();
2192  sprintf(mdc_string,"%s %10.6f",mdc_string,frTime+mdc_shift);
2193  //cout << "TIME " << frTime << endl;
2194  double mdcTime = (((TObjString*)token->At(10))->GetString()).Atof();
2195  sprintf(mdc_string,"%s %10.6f",mdc_string,mdcTime+mdc_shift);
2196  //cout << "TIME " << mdcTime << endl;
2197  for(int j=11;j<15;j++)
2198  sprintf(mdc_string,"%s %s",mdc_string,(((TObjString*)token->At(j))->GetString()).Data());
2199  for(int j=15;j<token->GetEntries();j++) {
2200  TString stoken = ((TObjString*)token->At(j))->GetString();
2201  sprintf(mdc_string,"%s %s",mdc_string,stoken.Data());
2202  for(int n=0;n<(int)ifos.size();n++) {
2203  if(ifos[n]==stoken) {
2204  double ifoTime = (((TObjString*)token->At(j+1))->GetString()).Atof();
2205  sprintf(mdc_string,"%s %10.6f",mdc_string,ifoTime+mdc_shift);
2206  //cout << "TIME " << ifos[n].Data() << " " << ifoTime << endl;
2207  j++;
2208  }
2209  }
2210  }
2211  mdcList[i]=mdc_string;
2212  //cout << "OUT-" << mdcList[i] << endl;
2213  if(token) delete token;
2214  }
2215 
2216  return 0;
2217 }
2218 
2219 //______________________________________________________________________________
2220 TString
2222 //
2223 // set entry at position pos contained in the log string
2224 //
2225 // Input: log - log string
2226 // pos - entry position in the log string
2227 // val - string value used to modify the log string
2228 //
2229 // return the modified log string
2230 //
2231 
2232  char sval[32];sprintf(sval,"%e",val);
2233  return SetMDCLog(log, pos, sval);
2234 }
2235 
2236 //______________________________________________________________________________
2237 TString
2239 //
2240 // set entry at position pos contained in the log string
2241 //
2242 // Input: log - log string
2243 // pos - entry position in the log string
2244 // val - string value used to modify the log string
2245 //
2246 // return the modified log string
2247 //
2248 
2249  char log_string[1024]="";
2250 
2251  TObjArray* token = TString(log).Tokenize(' ');
2252  for(int n=0;n<token->GetEntries();n++) {
2253  if(n==pos) sprintf(log_string,"%s %s",log_string, val.Data());
2254  else sprintf(log_string,"%s %s",log_string, (((TObjString*)token->At(n))->GetString()).Data());
2255  }
2256  if(token) delete token;
2257 
2258  return log_string;
2259 }
2260 
2261 //______________________________________________________________________________
2262 int
2264 //
2265 // return the number of entries contained in the log string
2266 //
2267 // Input: log - log string
2268 //
2269 
2270  TObjArray* token = TString(log).Tokenize(' ');
2271  int size = token->GetEntries();
2272  delete token;
2273  return size;
2274 }
2275 
2276 //______________________________________________________________________________
2277 TString
2279 //
2280 // return the entry at position pos contained in the log string
2281 //
2282 // Input: log - log string
2283 // pos - entry position in the log string
2284 //
2285 
2286  TObjArray* token = TString(log).Tokenize(' ');
2287  for(int n=0;n<token->GetEntries();n++) {
2288  if(n==pos) return (((TObjString*)token->At(n))->GetString()).Data();
2289  }
2290  delete token;
2291  return "";
2292 }
2293 
2294 //______________________________________________________________________________
2295 double
2296 CWB::Toolbox::getMDCShift(mdcshift mshift, double time) {
2297 //
2298 // compute the MDC time shift to be applied to MDC data
2299 //
2300 // mshift - mdc shift structure
2301 // time - time
2302 //
2303 // the GPS range P=[mshift.startMDC,mshift.stopMDC] defines the MDC period to be used
2304 //
2305 // startMDC stopMDC
2306 // ^ ^
2307 // ................|xxxxxx P xxxxxx|..............
2308 //
2309 // the period P is replicated starting from mshift.offset
2310 //
2311 // mshift.offset time
2312 // ^ ^
2313 // ...|xxxxxx P xxxxxx|xxxxxx P xxxxxx|xxxxxx P xxxxxx|...
2314 // ^
2315 // mshift.offset+mlength*int(time/mlength)
2316 //
2317 // |.. return shift ..|
2318 //
2319 
2320  if(mshift.startMDC<=0.) return 0.;
2321  double mlength = mshift.stopMDC-mshift.startMDC;
2322  if(mlength<=0) return 0.;
2323  return mshift.offset+mlength*int(time/mlength)-mshift.startMDC;
2324 }
2325 
2326 //______________________________________________________________________________
2327 vector<TString>
2329  TString label, bool brms, bool bvar, bool bpsm) {
2330 //
2331 // merge list of tree contained in dir_name using threads
2332 //
2333 // nthreads - number of threads
2334 // simulation - true/false -> simulation/background
2335 // odir - output directory where to store the merged file
2336 // label - label used for the output file name
2337 // brms - true -> merge rms tree
2338 // bvar - true -> merge variability tree
2339 // bpsm - true -> merge probability skymap tree
2340 //
2341 
2342  vector<TString> fileList = getFileListFromDir(dir_name,".root","","wave_");
2343  mergeCWBTrees(nthreads, fileList, simulation, odir, label, brms, bvar, bpsm);
2344  return fileList;
2345 }
2346 
2347 //______________________________________________________________________________
2348 void
2349 CWB::Toolbox::mergeCWBTrees(int nthreads, vector<TString> fileList, bool simulation,
2350  TString odir, TString label, bool brms, bool bvar, bool bpsm) {
2351 //
2352 // merge list of tree contained in the root files listed in fileList using threads
2353 //
2354 // nthreads - number of threads
2355 // ... - see descriptions of parameters in the n the overload methods
2356 //
2357 
2358  if(nthreads>MAX_THREADS) {
2359  cout << "CWB::Toolbox::mergeCWBTrees : Error - nthreads must be <= : " << MAX_THREADS << endl;
2360  exit(1);
2361  }
2362 
2363  TThread *thread[MAX_THREADS];
2364  MergeParms mergeParms[MAX_THREADS];
2365  vector<TString> fList[MAX_THREADS];
2366  int lSize[MAX_THREADS];
2367  TString tLabel[MAX_THREADS];
2368  vector<TString> waveList;
2369  vector<TString> mdcList;
2370  vector<TString> liveList;
2371  vector<TString> rmsList;
2372  vector<TString> varList;
2373 
2374  // compute file lists size
2375  int listSize=1;
2376  if(fileList.size()<=nthreads) {
2377  nthreads=1;
2378  lSize[0]=fileList.size();
2379  } else {
2380  if(fileList.size()%nthreads!=0) {
2381  for(int i=0;i<nthreads;i++) lSize[i]=int(fileList.size()/nthreads);
2382  lSize[0]+=fileList.size()%nthreads;
2383  } else {
2384  for(int i=0;i<nthreads;i++) lSize[i]=int(fileList.size()/nthreads);
2385  }
2386  }
2387 
2388  // fill file lists merged files name
2389  int k=0;
2390  for(int i=0;i<nthreads;i++) {
2391  for(int j=0;j<lSize[i];j++) fList[i].push_back(fileList[k++]);
2392  tLabel[i]=TString::Format("%s.T%d",label.Data(),i);
2393 
2394  waveList.push_back(TString::Format("%s/wave_%s.root",odir.Data(),tLabel[i].Data()));
2395  if(simulation) {
2396  mdcList.push_back(TString::Format("%s/mdc_%s.root",odir.Data(),tLabel[i].Data()));
2397  } else {
2398  liveList.push_back(TString::Format("%s/live_%s.root",odir.Data(),tLabel[i].Data()));
2399  if(brms) rmsList.push_back(TString::Format("%s/rms_%s.root",odir.Data(),tLabel[i].Data()));
2400  if(bvar) varList.push_back(TString::Format("%s/var_%s.root",odir.Data(),tLabel[i].Data()));
2401  }
2402  }
2403 
2404  // fill merge structures
2405  for(int i=0;i<nthreads;i++) {
2406  mergeParms[i].threadID = i;
2407  mergeParms[i].fileList = fList[i];
2408  mergeParms[i].simulation = simulation;
2409  mergeParms[i].odir = odir;
2410  mergeParms[i].label = tLabel[i];
2411  mergeParms[i].brms = brms;
2412  mergeParms[i].bvar = bvar;
2413  mergeParms[i].bpsm = bpsm;
2414  }
2415 
2416  // create & start threads
2417  for(int i=0;i<nthreads;i++) {
2418  char name[128]; sprintf(name,"Merge.T%d",i);
2419  thread[i] = new TThread(name, MergeHandle, (void*) &mergeParms[i]);
2420  thread[i]->Run();
2421  printf("Starting Thread : %s\n",name);
2422  }
2423 
2424  // join threads
2425  for(int i=0;i<nthreads;i++) thread[i]->Join();
2426 
2427  // merge final output merged files
2428  TString fName;
2429 
2430  fName=TString::Format("wave_%s",label.Data());
2431  if(waveList.size()) {
2432  mergeTrees(waveList, "waveburst", odir, fName, true);
2433  for(int i=0;i<waveList.size();i++) gSystem->Exec("rm "+waveList[i]);
2434  }
2435  fName=TString::Format("mdc_%s",label.Data());
2436  if(mdcList.size()) {
2437  mergeTrees(mdcList, "mdc", odir, fName, true);
2438  for(int i=0;i<waveList.size();i++) gSystem->Exec("rm "+mdcList[i]);
2439  }
2440  fName=TString::Format("live_%s",label.Data());
2441  if(liveList.size()) {
2442  mergeTrees(liveList, "liveTime", odir, fName, true);
2443  for(int i=0;i<liveList.size();i++) gSystem->Exec("rm "+liveList[i]);
2444  }
2445  fName=TString::Format("rms_%s",label.Data());
2446  if(rmsList.size()) {
2447  mergeTrees(rmsList, "noise", odir, fName, true);
2448  for(int i=0;i<rmsList.size();i++) gSystem->Exec("rm "+rmsList[i]);
2449  }
2450  fName=TString::Format("var_%s",label.Data());
2451  if(varList.size()) {
2452  mergeTrees(varList, "variability", odir, fName, true);
2453  for(int i=0;i<varList.size();i++) gSystem->Exec("rm "+varList[i]);
2454  }
2455 
2456  // delete threads
2457  for(int i=0;i<nthreads;i++) delete thread[i];
2458 }
2459 
2460 //______________________________________________________________________________
2461 vector<TString>
2463  TString label, bool brms, bool bvar, bool bpsm) {
2464 //
2465 // merge trees contained in dir_name
2466 //
2467 // simulation - true/false -> simulation/background
2468 // odir - output directory where to store the merged file
2469 // label - label used for the output file name
2470 // brms - true -> merge rms tree
2471 // bvar - true -> merge variability tree
2472 // bpsm - true -> merge probability skymap tree
2473 //
2474 
2475  vector<TString> fileList = getFileListFromDir(dir_name,".root","","wave_");
2476  mergeCWBTrees(fileList, simulation, odir, label, brms, bvar, bpsm);
2477  return fileList;
2478 }
2479 
2480 //______________________________________________________________________________
2481 void
2483  TString label, bool brms, bool bvar, bool bpsm) {
2484 //
2485 // merge list of tree contained in the root files listed in fileList
2486 //
2487 // Input: fileList - list of tree root files
2488 // simulation - true/false -> simulation/background
2489 // odir - output directory where to store the merged file
2490 // label - label used for the output file name
2491 // if simulation=true the following files are created
2492 // - 'odir'/wave_'label'.root // detected events
2493 // - 'odir'/mdc_'label'.root // injected events
2494 // - 'odir'/merge_'label'.lst // list of merged files
2495 // if simulation=false the following files are created
2496 // - 'odir'/wave_'label'.root // detected events
2497 // - 'odir'/live_'label'.root // live times
2498 // - 'odir'/merge_'label'.lst // list of merged files
2499 // - 'odir'/rms_'label'.root // if brms=true
2500 // - 'odir'/var_'label'.root // if bvar=true
2501 // brms - true -> merge rms tree
2502 // bvar - true -> merge variability tree
2503 // bpsm - true -> merge probability skymap tree
2504 //
2505 
2506  char s[1024];
2507  char f[1024];
2508  char cmd[4096];
2509 
2510  char c;
2511  if (simulation==1) c = 's';
2512  else c = 'b';
2513  if (simulation)
2514  cout << "CWB::Toolbox::mergeCWBTrees - Start merging "
2515  << fileList.size() << " files (simulation) ..." << endl;
2516  else
2517  cout << "CWB::Toolbox::mergeCWBTrees - Start merging "
2518  << fileList.size() << " files (production) ..." << endl;
2519 
2520  TChain wav("waveburst");
2521  if(!bpsm) wav.SetBranchStatus("Psm",false); // include/exclude from merge the probability skymap
2522  wav.SetMaxTreeSize(MAX_TREE_SIZE);
2523  TChain mdc("mdc");
2524  TChain rms("noise");
2525  TChain var("variability");
2526  TChain liv("liveTime");
2527  int ZombieCnt = 0;
2528 
2529  CWB::History* history = NULL;
2530 
2531  int nfiles=0;
2532  bool bhistory=true;
2533  cout << "CWB::Toolbox::mergeCWBTrees - Add file to chain in progress ..." << endl;
2534  for(int n=0;n<(int)fileList.size();n++) {
2535  sprintf(f,"%s",fileList[n].Data());
2536  //cout << f << endl;
2537 
2538 /* this stuff has been commented out to speed up the merging phase
2539  TFile f1(f,"READ");
2540 
2541  if(f1.IsZombie()) {
2542  ZombieCnt++;
2543  sprintf(cmd,"mv %s %s.zombie",f,f);
2544  cout<<cmd<<endl;
2545  gSystem->Exec(cmd);
2546 
2547  // Check if file txt exist
2548  char txtfile[1024];
2549  sprintf(txtfile,"%s",f);
2550  Long_t id,size,flags,mt;
2551  int estat = gSystem->GetPathInfo(txtfile,&id,&size,&flags,&mt);
2552  if (estat==0) {
2553  TString cmd2 = cmd;
2554  cmd2.ReplaceAll(".root",".txt");
2555  gSystem->Exec(cmd2.Data());
2556  cout<<"Zombie file!!! Renamed root and txt files as :"<<f<<".zombie"<<endl;
2557  }
2558  continue;
2559  }
2560 */
2561 
2562  if(bhistory) {
2563  TFile f1(f,"READ");
2564  history=(CWB::History*)f1.Get("history");
2565  if(history==NULL) history=new CWB::History();
2566  bhistory=false;
2567  }
2568 
2569  wav.Add(f);
2570  if (++nfiles%1000==0) cout << "CWB::Toolbox::mergeCWBTrees - " << nfiles
2571  << "/" << fileList.size() << " files" << endl;
2572  if(c=='s') mdc.Add(f);
2573  else { liv.Add(f); if(brms) rms.Add(f); if(bvar) var.Add(f);}
2574  }
2575 
2576  sprintf(s,"%s/wave_%s.root",odir.Data(),label.Data());
2577  cout << "CWB::Toolbox::mergeCWBTrees - Merging " << s << " in progress ..." << endl;
2578  wav.Merge(s);
2579 
2580  // update history
2581  if(history!=NULL) {
2582  history->SetHistoryModify(true);
2583  const CWB::HistoryStage* stage = history->GetStage(CCAST("MERGE"));
2584  if(stage==NULL) {
2585  history->AddStage(CCAST("MERGE"));
2586  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
2587  char work_dir[512]="";
2588  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
2589  history->AddHistory(CCAST("MERGE"), CCAST("WORKDIR"), work_dir);
2590  }
2591  }
2592  // write history to merge file
2593  if(history!=NULL) {
2594  TFile fwav(s,"UPDATE");
2595  history->Write("history");
2596  fwav.Close();
2597  }
2598 
2599  if(c=='s') {
2600  sprintf(s,"%s/mdc_%s.root",odir.Data(),label.Data());
2601  cout << "CWB::Toolbox::mergeCWBTrees - Merging " << s << " in progress ..." << endl;
2602  mdc.Merge(s);
2603  // write history to merge file
2604  if(history!=NULL) {
2605  TFile fmdc(s,"UPDATE");
2606  history->Write();
2607  fmdc.Close();
2608  }
2609  }
2610  else {
2611  sprintf(s,"%s/live_%s.root",odir.Data(),label.Data());
2612  cout << "CWB::Toolbox::mergeCWBTrees - Merging " << s << " in progress ..." << endl;
2613  liv.Merge(s);
2614  // write history to merge file
2615  if(history!=NULL) {
2616  TFile fliv(s,"UPDATE");
2617  history->Write();
2618  fliv.Close();
2619  }
2620  if(brms) {
2621  sprintf(s,"%s/rms_%s.root",odir.Data(),label.Data());
2622  cout << "CWB::Toolbox::mergeCWBTrees - Merging " << s << " in progress ..." << endl;
2623  rms.Merge(s);
2624  if(history!=NULL) {
2625  TFile frms(s,"UPDATE");
2626  history->Write();
2627  frms.Close();
2628  }
2629  }
2630  if(bvar) {
2631  sprintf(s,"%s/var_%s.root",odir.Data(),label.Data());
2632  cout << "CWB::Toolbox::mergeCWBTrees - Merging " << s << " in progress ..." << endl;
2633  var.Merge(s);
2634  if(history!=NULL) {
2635  TFile fvar(s,"UPDATE");
2636  history->Write();
2637  fvar.Close();
2638  }
2639  }
2640  }
2641  if(history!=NULL) delete history;
2642 
2643  cout << "CWB::Toolbox::mergeCWBTrees - Merged files : " << nfiles<<endl;
2644  //cout << "CWB::Toolbox::mergeCWBTrees - Zombie files : " << ZombieCnt<<endl;
2645  return;
2646 }
2647 
2648 //______________________________________________________________________________
2649 void
2651  TString odir, TString ofName, bool bhistory) {
2652 //
2653 // merge list of tree contained in the root files listed in fileList
2654 //
2655 // Input: fileList - list of tree root files
2656 // treeName - name of tree
2657 // odir - output directory where to store the merged file
2658 // ofName - name used for the output file name
2659 // if ofName not ends with ".root" then ofile_name = 'odir'/'ofName'.root
2660 // else ofile_name = 'odir'/'ofName'
2661 // bhistory - if true then the history is copied into the merged file
2662 //
2663 
2664  char s[1024];
2665  char f[1024];
2666  char cmd[4096];
2667 
2668  cout << "CWB::Toolbox::mergeTrees - Start merging "
2669  << fileList.size() << " files (simulation) ..." << endl;
2670 
2671  TChain tree(treeName);
2672  tree.SetMaxTreeSize(MAX_TREE_SIZE);
2673  int ZombieCnt = 0;
2674 
2675  CWB::History* history = NULL;
2676 
2677  int nfiles=0;
2678  cout << "CWB::Toolbox::mergeTrees - Add file to chain in progress ..." << endl;
2679  for(int n=0;n<(int)fileList.size();n++) {
2680  sprintf(f,"%s",fileList[n].Data());
2681  //cout << f << endl;
2682  TFile f1(f,"READ");
2683 
2684  if(f1.IsZombie()) {
2685  ZombieCnt++;
2686  sprintf(cmd,"mv %s %s.zombie",f,f);
2687  cout<<cmd<<endl;
2688  gSystem->Exec(cmd);
2689 
2690  // Check if file txt exist
2691  char txtfile[1024];
2692  sprintf(txtfile,"%s",f);
2693  Long_t id,size,flags,mt;
2694  int estat = gSystem->GetPathInfo(txtfile,&id,&size,&flags,&mt);
2695  if (estat==0) {
2696  TString cmd2 = cmd;
2697  cmd2.ReplaceAll(".root",".txt");
2698  gSystem->Exec(cmd2.Data());
2699  cout<<"Zombie file!!! Renamed root and txt files as :"<<f<<".zombie"<<endl;
2700  }
2701  continue;
2702  }
2703 
2704  if(bhistory) {
2705  TFile f1(f,"READ");
2706  history=(CWB::History*)f1.Get("history");
2707  if(history==NULL) history=new CWB::History();
2708  bhistory=false;
2709  }
2710 
2711  tree.Add(f);
2712  if (++nfiles%1000==0) cout << "CWB::Toolbox::mergeTrees - " << nfiles
2713  << "/" << fileList.size() << " files" << endl;
2714  }
2715 
2716  if(ofName.EndsWith(".root")) {
2717  sprintf(s,"%s/%s",odir.Data(),ofName.Data());
2718  } else {
2719  sprintf(s,"%s/%s.root",odir.Data(),ofName.Data());
2720  }
2721  cout << "CWB::Toolbox::mergeTrees - Merging " << s << " in progress ..." << endl;
2722  tree.Merge(s);
2723 
2724  // write history to merge file
2725  if(history!=NULL) {
2726  TFile froot(s,"UPDATE");
2727  history->Write("history");
2728  froot.Close();
2729  }
2730  if(history!=NULL) delete history;
2731 
2732  cout << "CWB::Toolbox::mergeTrees - Merged files : " << nfiles<<endl;
2733  cout << "CWB::Toolbox::mergeTrees - Zombie files : " << ZombieCnt<<endl;
2734  return;
2735 }
2736 
2737 //______________________________________________________________________________
2738 vector<TString>
2740 //
2741 // read file from ifName file
2742 //
2743 // Input: ifName - input file name
2744 //
2745 // return a vector of the file names contained in ifName
2746 //
2747 
2748  ifstream in;
2749  in.open(ifName.Data(),ios::in);
2750  if(!in.good()) {
2751  cout << "CWB::Toolbox::readFileList - Error Opening File : " << ifName.Data() << endl;
2752  gSystem->Exit(1);
2753  }
2754 
2755  vector<TString> fileList;
2756 
2757  char istring[8192];
2758  while (1) {
2759  in.getline(istring,8192);
2760  if (!in.good()) break;
2761  fileList.push_back(istring);
2762  }
2763 
2764  return fileList;
2765 }
2766 
2767 //______________________________________________________________________________
2768 void
2770 //
2771 // write file list to ofName file
2772 //
2773 // Input: fileList - input file list
2774 // ofName - output file name
2775 //
2776 
2777  char ofile_name[1024];
2778  if(ofName[0]!='/') {
2779  sprintf(ofile_name,"%s/%s",gSystem->WorkingDirectory(),ofName.Data());
2780  } else {
2781  sprintf(ofile_name,"%s",ofName.Data());
2782  }
2783  cout << ofile_name << endl;
2784  ofstream out;
2785  out.open(ofile_name,ios::out);
2786  if (!out.good()) {cout << "CWB::Toolbox::dumpFileList - Error Opening File : " << ofName.Data() << endl;gSystem->Exit(1);}
2787 
2788  for (int i=0;i<(int)fileList.size();i++) {
2789  out << fileList[i] << endl;
2790  }
2791 
2792  out.close();
2793 
2794  return;
2795 }
2796 
2797 //______________________________________________________________________________
2798 vector<waveSegment>
2800  double segLen, double segMLS, double segEdge,
2801  vector<waveSegment> dqList) {
2802 //
2803 // create merged dq cat 1 file list
2804 //
2805 // Input: jobId - job number
2806 // nIfO - detector number
2807 // segLen - Segment length [sec]
2808 // segMLS - Minimum Segment Length after DQ_CAT1 [sec]
2809 // segEdge - wavelet boundary offset [sec]
2810 // dqList - dq cat1 list
2811 //
2812 // Return the detector's slag segment ranges associated to SLAG infos & dq cat1 list
2813 //
2814 
2815 
2816  if (jobId<1) {cout << "CWB::Toolbox::getSegList - Error : jobId must be > 0" << endl;gSystem->Exit(1);}
2817 
2818  vector<waveSegment> jobList = getJobList(dqList, segLen, segMLS, segEdge); // get standard job list
2819 
2820  if (jobId>(int)jobList.size()) {
2821  cout << "CWB::Toolbox::getSegList - Error : jobId is > max jobs " << jobList.size() << endl;
2822  gSystem->Exit(1);
2823  }
2824 
2825  vector<waveSegment> detSegs(nIFO);
2826  for(int i=0;i<nIFO;i++) detSegs[i] = jobList[jobId-1];
2827 
2828  jobList.clear();
2829 
2830  return detSegs;
2831 }
2832 
2833 //______________________________________________________________________________
2834 char*
2836 //
2837 // return the file content in a char buffer
2838 //
2839 // Input: ifName - file name
2840 //
2841 
2842  char* fileBuffer=NULL;
2843 
2844  Long_t id,size,flags,mt;
2845  int estat = gSystem->GetPathInfo(ifName.Data(),&id,&size,&flags,&mt);
2846  if (estat==0) {
2847 
2848  ifstream in;
2849  in.open(ifName.Data(),ios::in);
2850  if(!in.good()) {
2851  cout << "CWB::Toolbox::readFile - Error Opening File : " << ifName.Data() << endl;
2852  gSystem->Exit(1);
2853  }
2854 
2855  fileBuffer = new char[2*size+1];
2856  bzero(fileBuffer,(2*size+1)*sizeof(char));
2857 
2858  char istring[8192];
2859  int fileLength = 0;
2860  while (1) {
2861  in.getline(istring,8192);
2862  if (!in.good()) break;
2863  //cout << istring << endl;
2864  int len = strlen(istring);
2865  istring[len]=0x0a;
2866  strncpy(fileBuffer+fileLength,istring,len+1);
2867  fileLength += len+1;
2868  }
2869  }
2870 
2871  return fileBuffer;
2872 }
2873 
2874 //______________________________________________________________________________
2875 int
2876 CWB::Toolbox::setCuts(TString ifName, TString idir, TString odir, TString trname, TString cuts, TString olabel) {
2877 //
2878 // apply selection cuts to trname tree in ifname root file
2879 // and create an output root file with the selected tree entries
2880 // return selected entries
2881 //
2882 // ifname : input root file name
2883 // idir : input dir
2884 // trname : tree name
2885 // cuts : selection cuts (Ex: netcc[2]>0.5)
2886 // odir : output dir
2887 // olabel : output label used in the output file name (Ex: cc2_gt_0d5)
2888 // ofname.root = ifname.Colabel.root
2889 //
2890 
2891  if(cuts=="") {
2892  cout << "CWB::Toolbox::setCuts - cuts is empty : nothing to do" << endl;
2893  gSystem->Exit(1);
2894  }
2895 
2896  TString ifname = idir+"/"+ifName;
2897 
2898  // open input root file
2899  cout<<"Opening File : " << ifname.Data() << endl;
2900  TFile ifile(ifname);
2901  if (ifile.IsZombie()) {
2902  cout << "CWB::Toolbox::setCuts - Error opening file " << ifname.Data() << endl;
2903  gSystem->Exit(1);
2904  }
2905  TTree *itree = (TTree*)ifile.Get(trname.Data());
2906  if (itree==NULL) {
2907  cout << "CWB::Toolbox::setCuts - tree " << trname
2908  << " is not present in file " << ifname.Data() << endl;
2909  gSystem->Exit(1);
2910  }
2911  Int_t isize = (Int_t)itree->GetEntries();
2912  cout << "tree size : " << isize << endl;
2913  if (isize==0) {
2914  cout << "CWB::Toolbox::setCuts - tree " << trname
2915  << " is empty in file " << ifname.Data() << endl;
2916  gSystem->Exit(1);
2917  }
2918 
2919  // check cuts
2920  TTreeFormula formula("trCuts", cuts.Data(), itree);
2921  int err = formula.Compile(cuts.Data());
2922  if(err) {
2923  cout << "CWB::Toolbox::setCuts - wrong input cuts " << cuts << endl;
2924  return -1;
2925  }
2926 
2927  // get selected entries
2928  itree->SetEstimate(isize);
2929  itree->Draw("Entry$",cuts.Data(),"goff",isize);
2930  int nentries = itree->GetSelectedRows();
2931  cout << "CWB::Toolbox::setCuts - selected entries : "
2932  << nentries << "/" << itree->GetEntries() << endl;
2933 
2934  // create output root file name
2935  TString ofname = odir+"/"+ifName;
2936  ofname.ReplaceAll(".root",TString(".C_")+olabel+".root");
2937  cout << "CWB::Toolbox::setCuts - Create cuts file : " << ofname << endl;
2938  bool overwrite = checkFile(ofname,true);
2939  if(!overwrite) gSystem->Exit(0);
2940 
2941  // create output root file with selection cuts
2942  TFile ofile(ofname,"RECREATE");
2943  TTree* otree = itree->CopyTree(cuts.Data());
2944  otree->SetDirectory(&ofile);
2945  otree->Write();
2946  ofile.Close();
2947 
2948  // create setCuts history
2949  CWB::History* history = (CWB::History*)ifile.Get("history");
2950  if(history==NULL) history=new CWB::History();
2951  if(history!=NULL) {
2952  TList* stageList = history->GetStageNames(); // get stage list
2953  TString pcuts="";
2954  for(int i=0;i<stageList->GetSize();i++) { // get previous cuts
2955  TObjString* stageObjString = (TObjString*)stageList->At(i);
2956  TString stageName = stageObjString->GetString();
2957  char* stage = const_cast<char*>(stageName.Data());
2958  if(stageName=="CUTS") pcuts=history->GetHistory(stage,const_cast<char*>("PARAMETERS"));
2959  }
2960  // update history
2961  history->SetHistoryModify(true);
2962  if(!history->StageAlreadyPresent(CCAST("CUTS"))) history->AddStage(CCAST("CUTS"));
2963  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
2964  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
2965  char work_dir[1024]="";
2966  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
2967  history->AddHistory(CCAST("CUTS"), CCAST("WORKDIR"), work_dir);
2968  TString _cuts = (pcuts!="") ? pcuts+" "+cuts : cuts;
2969  history->AddHistory(CCAST("CUTS"), CCAST("PARAMETERS"), CCAST(_cuts.Data()));
2970  char logmsg[2048]; sprintf(logmsg,"Apply Selection Cuts : %s",cuts.Data());
2971  history->AddLog(CCAST("CUTS"), CCAST(logmsg));
2972  }
2973 
2974  // close input root file
2975  ifile.Close();
2976 
2977  // write setCuts history to merge+cuts file
2978  if(history!=NULL) {
2979  TFile ofile(ofname,"UPDATE");
2980  history->Write("history");
2981  ofile.Close();
2982  delete history;
2983  }
2984 
2985  return nentries;
2986 }
2987 
2988 //______________________________________________________________________________
2989 int
2991  TString sels, TString farFile, int irho, TString olabel, bool inclusive) {
2992 //
2993 // add a new ifar (inverse false alarm rate) leaf to the selected entries in the merged wave root file
2994 // for each selected entry a new leaf is created "ifar" which value is obtained from the farFile
2995 // farFile is a text file which contains a corrispondence between rho and far (from background)
2996 // return selected entries
2997 //
2998 // ifname : input root file name
2999 // idir : input dir
3000 // odir : output dir
3001 // trname : tree name
3002 // sels : selection sels (Ex: netcc[2]>0.5)
3003 // farFile : far file (text file with a list entries : rho far)
3004 // irho ; rho index 0/1 : rho[0]/rho[1] to be used for rho to far association
3005 // olabel : output label used in the output file name (Ex: cc2_gt_0d5)
3006 // ofname.root = ifname.Folabel.root
3007 // inclusive : true(def)/false=inclusive/exclusive : used to compute the ifar
3008 //
3009 
3010  if(irho!=0 && irho!=1) {
3011  cout << "CWB::Toolbox::setIFAR - bad irho value : irho must be 0/1" << endl;
3012  gSystem->Exit(1);
3013  }
3014 
3015  if(sels=="") {
3016  cout << "CWB::Toolbox::setIFAR - sels is empty : nothing to do" << endl;
3017  gSystem->Exit(1);
3018  }
3019 
3020  // check if farFile exist
3021  bool exist = checkFile(farFile,false);
3022  if(!exist) gSystem->Exit(0);
3023  // read farFile
3024  vector<double> xrho,xfar;
3025  int xsize = GetStepFunction(farFile, xrho, xfar);
3026  // remove elements with xfar=0
3027  vector<double> trho,tfar;
3028  for(int i=0;i<xsize;i++) {
3029  if(xfar[i]!=0) {
3030  trho.push_back(xrho[i]);
3031  tfar.push_back(xfar[i]);
3032  }
3033  }
3034  xrho=trho; xfar=tfar;
3035  // insert a new element at the beginning with xrho=-DBL_MAX and xfar[xfar[0]]
3036  std::vector<double>::iterator it;
3037  it = xrho.begin(); xrho.insert(it,-DBL_MAX);
3038  it = xfar.begin(); xfar.insert(it,xfar[0]);
3039  // insert a new element at the end with xrho=DBL_MAX/2 and xfar[xfar[xfar.size()-1]]
3040  xrho.push_back(DBL_MAX/2);
3041  xfar.push_back(xfar[xfar.size()-1]);
3042  xsize=xfar.size();
3043  // create step graph used to get far from rho
3044  TGraph gr;
3045  int cnt=0;
3046  for(int i=1;i<xsize;i++) {
3047  double dx = (xrho[i]-xrho[i-1])/100000.;
3048  gr.SetPoint(cnt++,xrho[i]-dx,xfar[i-1]);
3049  gr.SetPoint(cnt++,xrho[i]+dx,xfar[i]);
3050  }
3051 
3052  TString ifname = idir+"/"+ifName;
3053 
3054  // open input root file
3055  cout<<"Opening File : " << ifname.Data() << endl;
3056  TFile ifile(ifname);
3057  if (ifile.IsZombie()) {
3058  cout << "CWB::Toolbox::setIFAR - Error opening file " << ifname.Data() << endl;
3059  gSystem->Exit(1);
3060  }
3061  TTree *itree = (TTree*)ifile.Get(trname.Data());
3062  if (itree==NULL) {
3063  cout << "CWB::Toolbox::setIFAR - tree " << trname
3064  << " is not present in file " << ifname.Data() << endl;
3065  gSystem->Exit(1);
3066  }
3067  Int_t isize = (Int_t)itree->GetEntries();
3068  itree->SetEstimate(isize);
3069  cout << "tree size : " << isize << endl;
3070  if (isize==0) {
3071  cout << "CWB::Toolbox::setIFAR - tree " << trname
3072  << " is empty in file " << ifname.Data() << endl;
3073  gSystem->Exit(1);
3074  }
3075 
3076  // check sels
3077  TTreeFormula fsel("sels", sels.Data(), itree);
3078  int err = fsel.Compile(sels.Data());
3079  if(err) {
3080  cout << "CWB::Toolbox::setIFAR - wrong input sels " << sels << endl;
3081  return -1;
3082  }
3083 
3084  // create output root file name
3085  TString ofname = odir+"/"+ifName;
3086  ofname.ReplaceAll(".root",TString(".S_")+olabel+".root");
3087  cout << "CWB::Toolbox::setIFAR - Create sels file : " << ofname << endl;
3088  bool overwrite = checkFile(ofname,true);
3089  if(!overwrite) gSystem->Exit(0);
3090 
3091  // create output root file with selection sels
3092  TFile ofile(ofname,"RECREATE");
3093  TTree *otree = (TTree*)itree->CloneTree(0);
3094  otree->SetMaxTreeSize(MAX_TREE_SIZE);
3095 
3096  // add ifar,bin leaf to otree
3097  TBranch* branch;
3098  bool ifar_exists=false;
3099  bool bin_exists=false;
3100  TIter next(itree->GetListOfBranches());
3101  while ((branch=(TBranch*)next())) {
3102  if(TString("ifar").CompareTo(branch->GetName())==0) ifar_exists=true;
3103  if(TString("bin").CompareTo(branch->GetName())==0) bin_exists=true;
3104  }
3105  next.Reset();
3106  float ifar=0;
3107  if (ifar_exists) itree->SetBranchAddress("ifar",&ifar);
3108  else otree->Branch("ifar",&ifar,"ifar/F");
3109  string* bin = new string();
3110  if (bin_exists) itree->SetBranchAddress("bin",&bin);
3111  else otree->Branch("bin",bin);
3112 
3113  // get selected entries
3114  itree->Draw("Entry$",sels.Data(),"goff",isize);
3115  double* entry = itree->GetV1();
3116  int nsel = itree->GetSelectedRows();
3117  cout << "CWB::Toolbox::setIFAR - selected entries : "
3118  << nsel << "/" << itree->GetEntries() << endl;
3119  // write ifar for the selected entries
3120  float rho[2];
3121  itree->SetBranchAddress("rho",rho);
3122  for(int i=0;i<nsel;i++) {
3123  ifar=0;
3124  itree->GetEntry(int(entry[i]));
3125  double far = gr.Eval(rho[irho]);
3126  double xifar = far>0 ? 1./far : -1;
3127  if(inclusive) { // inclusive
3128  if(xifar>ifar) {
3129  ifar=xifar;
3130  *bin=olabel.Data();
3131  }
3132  } else { // exclusive
3133  ifar = xifar;
3134  *bin = olabel.Data();
3135  }
3136  otree->Fill();
3137  }
3138  // get not selected entries
3139  itree->Draw("Entry$",TString::Format("!(%s)",sels.Data()).Data(),"goff",isize);
3140  int _nsel = itree->GetSelectedRows();
3141  double* _entry = itree->GetV1();
3142  cout << "CWB::Toolbox::setIFAR - not selected entries : "
3143  << _nsel << "/" << itree->GetEntries() << endl;
3144  // write ifar for the not selected entries
3145  for(int i=0;i<_nsel;i++) {
3146  itree->GetEntry(int(_entry[i]));
3147  if(!ifar_exists) ifar=0; // initialize ifar
3148  if(!bin_exists) *bin=""; // initialize bin
3149  otree->Fill();
3150  }
3151  delete bin;
3152  otree->Write();
3153  ofile.Close();
3154 
3155  // create setIFAR history
3156  CWB::History* history = (CWB::History*)ifile.Get("history");
3157  if(history==NULL) history=new CWB::History();
3158  if(history!=NULL) {
3159  TList* stageList = history->GetStageNames(); // get stage list
3160  TString psels="";
3161  char* pfile=NULL;
3162  for(int i=0;i<stageList->GetSize();i++) { // get previous sels
3163  TObjString* stageObjString = (TObjString*)stageList->At(i);
3164  TString stageName = stageObjString->GetString();
3165  char* stage = const_cast<char*>(stageName.Data());
3166  if(stageName=="IFAR") psels=history->GetHistory(stage,CCAST("PARAMETERS"));
3167  if(stageName=="IFAR") pfile=history->GetHistory(stage,CCAST("FARFILE"));
3168  }
3169  // update history
3170  history->SetHistoryModify(true);
3171  if(!history->StageAlreadyPresent(CCAST("IFAR"))) history->AddStage(CCAST("IFAR"));
3172  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
3173  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
3174  if(!history->TypeAllowed(CCAST("FARFILE"))) history->AddType(CCAST("FARFILE"));
3175  char work_dir[1024]="";
3176  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
3177  history->AddHistory(CCAST("IFAR"), CCAST("WORKDIR"), work_dir);
3178  TString _sels = (psels!="") ? psels+"\n"+sels : sels;
3179  history->AddHistory(CCAST("IFAR"), CCAST("PARAMETERS"), CCAST(_sels.Data()));
3180  char logmsg[2048]; sprintf(logmsg,"Created ifar on the bin set (%s) specified by the selection : %s",olabel.Data(),sels.Data());
3181  history->AddLog(CCAST("IFAR"), CCAST(logmsg));
3182  char* buffer = readFile(farFile); // read farFile file
3183  if(buffer!=NULL) {
3184  char* farBuffer=NULL;
3185  if(pfile!=NULL) {
3186  farBuffer = new char[strlen(pfile)+strlen(buffer)+farFile.Sizeof()+10];
3187  sprintf(farBuffer,"%s\n%s\n%s\n",pfile,farFile.Data(),buffer);
3188  } else {
3189  farBuffer = new char[strlen(buffer)+farFile.Sizeof()+10];
3190  sprintf(farBuffer,"%s\n%s\n",farFile.Data(),buffer);
3191  }
3192  history->AddHistory(CCAST("IFAR"), CCAST("FARFILE"), farBuffer);
3193  delete [] buffer;
3194  delete [] farBuffer;
3195  }
3196  }
3197 
3198  // close input root file
3199  ifile.Close();
3200 
3201  // write setIFAR history to merge+sels file
3202  if(history!=NULL) {
3203  TFile ofile(ofname,"UPDATE");
3204  history->Write("history");
3205  ofile.Close();
3206  delete history;
3207  }
3208 
3209  return nsel;
3210 }
3211 
3212 //______________________________________________________________________________
3213 int
3214 CWB::Toolbox::setChunk(TString ifName, TString idir, TString odir, TString trname, int chunk) {
3215 //
3216 // add a new chunk (data chunk id) leaf to the selected entries in the merged wave root file
3217 // return error status
3218 //
3219 // ifname : input root file name
3220 // idir : input dir
3221 // odir : output dir
3222 // trname : tree name
3223 // chunk : chunk id
3224 //
3225 
3226  if(chunk<=0) {
3227  cout << "CWB::Toolbox::setChunk - bad chunk chunk value : chunk must be >0" << endl;
3228  gSystem->Exit(1);
3229  }
3230 
3231  TString ifname = idir+"/"+ifName;
3232 
3233  // open input root file
3234  cout<<"Opening File : " << ifname.Data() << endl;
3235  TFile ifile(ifname);
3236  if (ifile.IsZombie()) {
3237  cout << "CWB::Toolbox::setChunk - Error opening file " << ifname.Data() << endl;
3238  gSystem->Exit(1);
3239  }
3240  TTree *itree = (TTree*)ifile.Get(trname.Data());
3241  if (itree==NULL) {
3242  cout << "CWB::Toolbox::setChunk - tree " << trname
3243  << " is not present in file " << ifname.Data() << endl;
3244  gSystem->Exit(1);
3245  }
3246  Int_t isize = (Int_t)itree->GetEntries();
3247  itree->SetEstimate(isize);
3248  cout << "tree size : " << isize << endl;
3249  if (isize==0) {
3250  cout << "CWB::Toolbox::setChunk - tree " << trname
3251  << " is empty in file " << ifname.Data() << endl;
3252  gSystem->Exit(1);
3253  }
3254 
3255  // create output root file name
3256  TString ofname = odir+"/"+ifName;
3257  char schunk[32];sprintf(schunk,"chunk%d",chunk);
3258  ofname.ReplaceAll(".root",TString(".K_")+schunk+".root");
3259  cout << "CWB::Toolbox::setChunk - Create sels file : " << ofname << endl;
3260  bool overwrite = checkFile(ofname,true);
3261  if(!overwrite) gSystem->Exit(0);
3262 
3263  // create output root file with selection sels
3264  TFile ofile(ofname,"RECREATE");
3265  TTree *otree = (TTree*)itree->CloneTree(0);
3266  otree->SetMaxTreeSize(MAX_TREE_SIZE);
3267 
3268  // add chunk leaf to otree
3269  TBranch* branch;
3270  bool chunk_exists=false;
3271  TIter next(itree->GetListOfBranches());
3272  while ((branch=(TBranch*)next())) {
3273  if(TString("chunk").CompareTo(branch->GetName())==0) chunk_exists=true;
3274  }
3275  next.Reset();
3276  if (chunk_exists) itree->SetBranchAddress("chunk",&chunk);
3277  else otree->Branch("chunk",&chunk,"chunk/I");
3278 
3279  // get selected entries
3280  itree->Draw("Entry$","","goff",isize);
3281  double* entry = itree->GetV1();
3282  int nsel = itree->GetSelectedRows();
3283  cout << "CWB::Toolbox::setChunk - selected entries : "
3284  << nsel << "/" << itree->GetEntries() << endl;
3285  // write chunk for the selected entries
3286  for(int i=0;i<nsel;i++) {
3287  itree->GetEntry(int(entry[i]));
3288  otree->Fill();
3289  }
3290  otree->Write();
3291  ofile.Close();
3292 
3293  // create setChunk history
3294  CWB::History* history = (CWB::History*)ifile.Get("history");
3295  if(history==NULL) history=new CWB::History();
3296  if(history!=NULL) {
3297  TList* stageList = history->GetStageNames(); // get stage list
3298  TString psels="";
3299  char* pfile=NULL;
3300  for(int i=0;i<stageList->GetSize();i++) { // get previous sels
3301  TObjString* stageObjString = (TObjString*)stageList->At(i);
3302  TString stageName = stageObjString->GetString();
3303  char* stage = const_cast<char*>(stageName.Data());
3304  if(stageName=="CHUNK") psels=history->GetHistory(stage,CCAST("PARAMETERS"));
3305  if(stageName=="CHUNK") pfile=history->GetHistory(stage,CCAST("FARFILE"));
3306  }
3307  // update history
3308  history->SetHistoryModify(true);
3309  if(!history->StageAlreadyPresent(CCAST("CHUNK"))) history->AddStage(CCAST("CHUNK"));
3310  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
3311  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
3312  char work_dir[1024]="";
3313  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
3314  history->AddHistory(CCAST("CHUNK"), CCAST("WORKDIR"), work_dir);
3315  char logmsg[2048]; sprintf(logmsg,"Created chunk leaf : %d",chunk);
3316  history->AddLog(CCAST("CHUNK"), CCAST(logmsg));
3317  }
3318 
3319  // close input root file
3320  ifile.Close();
3321 
3322  // write setChunk history to merge+sels file
3323  if(history!=NULL) {
3324  TFile ofile(ofname,"UPDATE");
3325  history->Write("history");
3326  ofile.Close();
3327  delete history;
3328  }
3329 
3330  return 0;
3331 }
3332 
3333 //_______________________________________________________________________________________
3334 void
3336 //
3337 // select from the root ifwave file a unique reconstructed event for each injected
3338 // signal. The signal is selected from the reconstructed events which have the same
3339 // injected gps time, the selected one has the maximum rho
3340 //
3341 // ifwave : name of simulation input wave file
3342 // ofwave : name of simulation output wave file (unique reconstructed events)
3343 // nifo : number of detectors
3344 // nfactor : number of simulation factors
3345 // factors : array of simulation factors
3346 // pp_irho : index used for rho = rho[pp_irho]
3347 //
3348 
3349  cout << "CWB::Toolbox::setUniqueEvents - Create unique events file : " << ofwave << endl;
3350  bool overwrite = checkFile(ofwave,true);
3351  if(!overwrite) gSystem->Exit(0);
3352 
3353  TFile* iwroot = TFile::Open(ifwave);
3354  if(iwroot==NULL||!iwroot->IsOpen()) {
3355  cout << "CWB::Toolbox::setUniqueEvents - Error : input file " << ifwave << " not found" << endl;
3356  exit(1);
3357  }
3358 
3359  TTree* iwtree = (TTree*) iwroot->Get("waveburst");
3360  if(iwtree==NULL) {
3361  cout << "CWB::Toolbox::setUniqueEvents - Error : tree waveburst not found in file " << ifwave << endl;
3362  exit(1);
3363  }
3364  cout << endl;
3365  cout << "CWB::Toolbox::setUniqueEvents : number of input events : " << iwtree->GetEntries() << endl;
3366  cout << endl;
3367 
3368  TFile* owroot = new TFile(ofwave,"RECREATE");
3369  if(owroot->IsZombie()) {
3370  cout << "CWB::Toolbox::setUniqueEvents - Error : output file " << ofwave << " not opened" << endl;
3371  exit(1);
3372  }
3373 
3374  TTree *owtree = (TTree*)iwtree->CloneTree(0);
3375  owtree->SetMaxTreeSize(MAX_TREE_SIZE);
3376 
3377  // -------------------------------------------------------------------------
3378  // add new leave to owtree (fsize : number of fragments)
3379  // -------------------------------------------------------------------------
3380  TString leaf_name="fsize";
3381  TBranch* branch;
3382  bool replaceLeaf=false;
3383  TIter next(iwtree->GetListOfBranches());
3384  while ((branch=(TBranch*)next())) {
3385  if (leaf_name.CompareTo(branch->GetName())==0) {
3386  cout << "fragment leaf [" << leaf_name << "] already applied" << endl;
3387  char answer[1024];
3388  strcpy(answer,"");
3389  do {
3390  cout << "CWB::Toolbox::setUniqueEvents : Do you want to overwrite the previous leaf ? (y/n) ";
3391  cin >> answer;
3392  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
3393  if (strcmp(answer,"y")==0) {
3394  replaceLeaf=true;
3395  } else {
3396  gSystem->Exit(-1);
3397  }
3398  }
3399  }
3400  next.Reset();
3401  int fsize=1;
3402  if (replaceLeaf) owtree->SetBranchAddress("fsize",&fsize);
3403  else owtree->Branch("fsize",&fsize,"fsize/I");
3404 
3405  char sel[1024];
3406  sprintf(sel,"Entry$:rho[%i]:time[%i]:factor",pp_irho,nIFO);
3407  int iwsize=iwtree->GetEntries();
3408  iwtree->Draw(sel,"","goff",iwsize);
3409  double* entry = iwtree->GetV1();
3410  double* rho = iwtree->GetV2();
3411  double* time = iwtree->GetV3();
3412  double* factor = iwtree->GetV4();
3413 
3414  // extract factors
3415  std::map <float, float> mfactors;
3416  for(int i=0;i<iwsize;i++) { // loop over the tree
3417  iwtree->GetEntry(i);
3418  mfactors[factor[i]]=factor[i];
3419  }
3420  vector<float> factors;
3421  std::map<float, float>::iterator iter;
3422  for (iter=mfactors.begin(); iter!=mfactors.end(); iter++) {
3423  factors.push_back(iter->second);
3424  }
3425  int nfactor=factors.size();
3426 
3427  char cuts[1024];
3428  for(int i=0;i<nfactor;i++) { // loop over the factors
3429  sprintf(cuts,"abs(factor-%g)/%g<0.0001",factors[i],factors[i]);
3430  iwtree->Draw(sel,cuts,"goff",iwsize);
3431  int size=iwtree->GetSelectedRows();
3432  int unique_evt=0; // number of unique events for factor i
3433  if(size==1) {
3434  iwtree->GetEntry(entry[0]);
3435  owtree->Fill();
3436  unique_evt++;
3437  } else if(size>1) {
3438  int* index = new int[size];
3439  TMath::Sort(size,time,index,kFALSE); // sort events in time
3440  int j=index[0];
3441  float rhomax=rho[j];
3442  int imax=j;
3443  double time1=time[j];
3444  double time2=0.;
3445  for(int k=1; k<size; k++) {
3446  j=index[k];
3447  time2=time[j];
3448  if(abs(time1-time2)>0.001 || k==size-1) {
3449  iwtree->GetEntry(entry[imax]);
3450  owtree->Fill(); // store the event with max rho[pp_irho]
3451  imax=j;
3452  rhomax=rho[j];
3453  unique_evt++;
3454  fsize=1;
3455  } else {
3456  if(rho[j]>rhomax) {rhomax=rho[j];imax=j;}
3457  fsize++;
3458  }
3459  time1=time2;
3460  }
3461  delete index;
3462  }
3463  cout << " factor id = " << i << "\t reconstructed events : "
3464  << iwtree->GetSelectedRows() << "\t unique events : "
3465  << unique_evt << "\tfactor = " << factors[i] << endl;
3466  }
3467  cout << endl;
3468  owtree->Write();
3469  owroot->Close();
3470  owroot->Close();
3471 
3472  // create setCuts history
3473  CWB::History* history = (CWB::History*)iwroot->Get("history");
3474  if(history==NULL) history=new CWB::History();
3475  if(history!=NULL) {
3476  TList* stageList = history->GetStageNames(); // get stage list
3477  TString pcuts="";
3478  for(int i=0;i<stageList->GetSize();i++) { // get previous cuts
3479  TObjString* stageObjString = (TObjString*)stageList->At(i);
3480  TString stageName = stageObjString->GetString();
3481  char* stage = const_cast<char*>(stageName.Data());
3482  if(stageName=="CUTS") pcuts=history->GetHistory(stage,const_cast<char*>("PARAMETERS"));
3483  }
3484  // update history
3485  history->SetHistoryModify(true);
3486  if(!history->StageAlreadyPresent(CCAST("CUTS"))) history->AddStage(CCAST("CUTS"));
3487  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
3488  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
3489  char work_dir[1024]="";
3490  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
3491  history->AddHistory(CCAST("CUTS"), CCAST("WORKDIR"), work_dir);
3492  TString cuts = (pcuts!="") ? pcuts+" (U)" : "(U)";
3493  history->AddHistory(CCAST("CUTS"), CCAST("PARAMETERS"), CCAST(cuts.Data()));
3494  char logmsg[2048]; sprintf(logmsg,"Apply Selection Cuts : %s","(U)");
3495  history->AddLog(CCAST("CUTS"), CCAST(logmsg));
3496  }
3497 
3498  // close input root file
3499  iwroot->Close();
3500 
3501  // write setCuts history to merge+cuts file
3502  if(history!=NULL) {
3503  TFile ofile(ofwave,"UPDATE");
3504  history->Write("history");
3505  ofile.Close();
3506  delete history;
3507  }
3508 
3509  return;
3510 }
3511 
3512 //_______________________________________________________________________________________
3513 int
3514 CWB::Toolbox::CombineCBC(vector<TString> ifwave, vector<TString> ifmdc, TString ofwave, TString ofmdc,
3515  int nIFO, float fthr, TString msearch, TString infos, int lag, int slag, float ifarthr) {
3516 //
3517 // Combine IMBHB & BBH searches
3518 //
3519 // ifwave : name of simulation/background input wave files
3520 // ifwave[0] = IMBHB
3521 // ifwave[1] = BBH
3522 // ifmdc : simulation -> name of simulation input mdc files
3523 // ifmdc[0] = IMBHB
3524 // ifmdc[1] = BBH
3525 // background -> ifmdc.size()=0
3526 // ofwave : name of simulation output wave file (reconstructed events)
3527 // ofmdc : name of simulation output mdc file (injected events)
3528 // nIFO : number of detectors
3529 // fthr : frequency threshold used to combine searches
3530 // IMBHB band -> freq<=fthr
3531 // BBH band -> freq >fthr
3532 // msearch : is the master search. Used only by history -> save the history of the master search. Available values are IMBHB,BBH
3533 // infos : user bookkeeping informations to be saved to the history output root files
3534 //
3535 // lag,slag : only for background (ifmdc.size()=0). Used to select the data where to apply the combined search
3536 // lag=slag=0 -> zero lag
3537 // ifarthr : Used to select events with ifar>ifarthr, ifarthr is in years
3538 // ifarthr must be >=0. The condition with ifar>0 select all events that pass the post-production cuts
3539 //
3540 // NOTES
3541 //
3542 // This function implements the combine statistic for the IMBHB and BBH searches
3543 // It can be used for simulation and background zero/non_zero lag
3544 // Simulation:
3545 // The same injections (from XML files) should be made for both searches.
3546 // The algorithm select only the common injections (compare the MDC log strings used for simulation).
3547 // The wave files must contain unique reconstructed events (no more than 1 event for each injection).
3548 // The algorithm exit if entries are not unique.
3549 // Before to use this funcion apply the CWB::Toolbox::setUniqueEvents
3550 // The common injected events are written to the output MDC file ofmdc
3551 // The combined reconstructed events are written to the output WAVE file ofwave
3552 // Background:
3553 // The combined reconstructed events are written to the output WAVE file ofwave
3554 //
3555 // Combined IFAR -> the IFAR of any event reconstructed by the BBH and IMBHB searches is defined in the table below
3556 //
3557 // -----------------------------------------------------------------------------------------------------------------
3558 // Case 1 -> IMBHB band (IMBHB BBH), BBH band ( ) -> IFAR = IFAR(IMBHB) TRIALS=1
3559 // Case 2 -> IMBHB band ( ), BBH band (IMBHB BBH) -> IFAR = IFAR(BBH ) TRIALS=1
3560 // Case 3 -> IMBHB band (IMBHB ), BBH band ( BBH) -> IFAR = max IFAR(IMBHB,BBH)/2 TRIALS=2
3561 // Case 4 -> IMBHB band ( BBH), BBH band (IMBHB ) -> IFAR = max IFAR(IMBHB,BBH)/2 TRIALS=2
3562 // Case 5 -> IMBHB band (IMBHB ), BBH band ( ) -> IFAR = max IFAR(IMBHB,BBH) TRIALS=1
3563 // Case 6 -> IMBHB band ( BBH), BBH band ( ) -> IFAR = max IFAR(IMBHB,BBH)/2 TRIALS=2
3564 // Case 7 -> IMBHB band ( ), BBH band (IMBHB ) -> IFAR = max IFAR(IMBHB,BBH)/2 TRIALS=2
3565 // Case 8 -> IMBHB band ( ), BBH band ( BBH) -> IFAR = max IFAR(IMBHB,BBH) TRIALS=1
3566 // -----------------------------------------------------------------------------------------------------------------
3567 //
3568 // Note: 'Case' is stored into the output wave file in the 'combine' parameter
3569 // The selected search IMBHB/BBH is stored into the output wave file in the 'search' parameter
3570 //
3571 
3572  #define YEAR (24.*3600.*365.)
3573  #define TRIALS 2 // number of trials used in the combine algorithm
3574  #define TIME_UNCERTAINTY 0.1 // (sec) used to select entries belonging to the same event
3575  // the injected time could be not the same for IMBHB and BBH searches
3576  // because it is the median of the whitened injected event
3577 
3578  // check if output files exist
3579  cout << "CWB::Toolbox::CombineCBC - Create combine wave output file : " << ofwave << endl;
3580  bool overwrite = checkFile(ofwave,true);
3581  if(!overwrite) gSystem->Exit(1);
3582 
3583  // check if ifarthr>=0
3584  if(ifarthr<0) {
3585  cout << "CWB::Toolbox::CombineCBC - Error : ifarthr must be >=0" << endl;
3586  exit(1);
3587  }
3588 
3589  // check if msearch is defined
3590  if(msearch!="IMBHB" && msearch!="BBH") {
3591  cout << "CWB::Toolbox::CombineCBC - Error : msearch not defined, valid values are: IMBHB,BBH" << endl;
3592  exit(1);
3593  }
3594  int msearch_id = (msearch=="IMBHB") ? 0 : 1; // get master search id
3595 
3596  bool simulation = ifmdc.size()>0 ? true : false; // case simulation/zero_lag
3597 
3598  // open input wave files
3599  TFile* iwroot[2];
3600  TTree* iwtree[2];
3601  if(ifwave.size()!=2) {cout << "CWB::Toolbox::CombineCBC - Error in input wave files declaration" << endl; exit(1);}
3602  for(int n=0;n<2;n++) {
3603  iwroot[n] = TFile::Open(ifwave[n]);
3604  if(iwroot==NULL||!iwroot[n]->IsOpen()) {
3605  cout << "CWB::Toolbox::CombineCBC - Error : input wave file " << ifwave[n] << " not found" << endl;
3606  exit(1);
3607  }
3608 
3609  iwtree[n] = (TTree*) iwroot[n]->Get("waveburst");
3610  if(iwtree[n]==NULL) {
3611  cout << "CWB::Toolbox::CombineCBC - Error : tree waveburst not found in file " << ifwave[n] << endl;
3612  exit(1);
3613  }
3614  cout << endl;
3615  cout << "CWB::Toolbox::CombineCBC : number of input wave events : " << iwtree[n]->GetEntries()
3616  << " ( " << ((n==0)?"IMBHB":"BBH") << " )" << endl;
3617  cout << endl;
3618  }
3619 
3620  // open input mdc files
3621  TFile* imroot[2];
3622  TTree* imtree[2];
3623  if(simulation && ifmdc.size()!=2) {cout << "CWB::Toolbox::CombineCBC - Error in input mdc files declaration" << endl; exit(1);}
3624  if(simulation) {
3625  for(int n=0;n<2;n++) {
3626  imroot[n] = TFile::Open(ifmdc[n]);
3627  if(imroot==NULL||!imroot[n]->IsOpen()) {
3628  cout << "CWB::Toolbox::CombineCBC - Error : input mdc file " << ifmdc[n] << " not found" << endl;
3629  exit(1);
3630  }
3631 
3632  imtree[n] = (TTree*) imroot[n]->Get("mdc");
3633  if(imtree[n]==NULL) {
3634  cout << "CWB::Toolbox::CombineCBC - Error : tree mdc not found in file " << ifmdc[n] << endl;
3635  exit(1);
3636  }
3637  cout << endl;
3638  cout << "CWB::Toolbox::CombineCBC : number of input mdc events : " << imtree[n]->GetEntries()
3639  << " ( " << ((n==0)?"IMBHB":"BBH") << " )" << endl;
3640  cout << endl;
3641  }
3642  }
3643 
3644  // create temporary output wave files
3645  TString tfwave[2];
3646  TFile* twroot[2];
3647  TTree* twtree[2];
3648  if(simulation) {
3649  for(int n=0;n<2;n++) {
3650  tfwave[n] = ofwave;
3651  tfwave[n].ReplaceAll(".root",TString::Format("_tmp%d.root",n));
3652  twroot[n] = new TFile(tfwave[n],"RECREATE");
3653  if(twroot[n]->IsZombie()) {
3654  cout << "CWB::Toolbox::CombineCBC - Error : temporary output wave file " << tfwave[n] << " not opened" << endl;
3655  exit(1);
3656  }
3657  twtree[n] = (TTree*)iwtree[n]->CloneTree(0);
3658  twtree[n]->SetMaxTreeSize(MAX_TREE_SIZE);
3659  }
3660  }
3661 
3662  // create temporary output mdc files
3663  TString tfmdc[2];
3664  TFile* tmroot[2];
3665  TTree* tmtree[2];
3666  if(simulation) {
3667  for(int n=0;n<2;n++) {
3668  tfmdc[n] = ofmdc;
3669  tfmdc[n].ReplaceAll(".root",TString::Format("_tmp%d.root",n));
3670  tmroot[n] = new TFile(tfmdc[n],"RECREATE");
3671  if(tmroot[n]->IsZombie()) {
3672  cout << "CWB::Toolbox::CombineCBC - Error : temporary output mdc file " << tfmdc[n] << " not opened" << endl;
3673  exit(1);
3674  }
3675  tmtree[n] = (TTree*)imtree[n]->CloneTree(0);
3676  tmtree[n]->SetMaxTreeSize(MAX_TREE_SIZE);
3677  }
3678  }
3679 
3680  // -------------------------------------------------------------------------
3681  // add leaves 'search' and 'combine' to owtree
3682  // search -> IMBHB, BBH
3683  // combine -> [1,8] is defined in the description of CombineCBC function
3684  // -------------------------------------------------------------------------
3685  // if 'search' or 'combine' leaves exist the combine is aborted -> the input root files are already combined
3686  for(int n=0;n<2;n++) {
3687  bool check_ifar=false;
3688  TBranch* branch;
3689  TIter next(iwtree[n]->GetListOfBranches());
3690  while ((branch=(TBranch*)next())) {
3691  if(TString(branch->GetName())=="search") {
3692  cout << "CWB::Toolbox::CombineCBC - Error : 'search' leaf already exist"; gSystem->Exit(1);
3693  }
3694  if(TString(branch->GetName())=="combine") {
3695  cout << "CWB::Toolbox::CombineCBC - Error : 'combine' leaf already exist"; gSystem->Exit(1);
3696  }
3697  if(TString(branch->GetName())=="ifar") check_ifar=true;
3698  }
3699  next.Reset();
3700  if(!check_ifar) {
3701  cout << "CWB::Toolbox::CombineCBC - Error : ifar is not defined in wave root file " << ifwave[n] << endl;
3702  cout << endl;
3703  gSystem->Exit(1);
3704  }
3705  }
3706  string* SEARCH = new string(); // new tree leaf used to store the selected combine search
3707  int COMBINE=1; // new tree leaf used to store the selected combine case [1,8]
3708  float IFAR; // variable used to update the IFAR
3709  if(simulation) {
3710  for(int n=0;n<2;n++) {
3711  twtree[n]->Branch("search",SEARCH);
3712  twtree[n]->Branch("combine",&COMBINE,"combine/I");
3713  twtree[n]->SetBranchAddress("ifar",&IFAR);
3714  }
3715  }
3716 
3717  // define vectors used for zero lag case, contains the combine event parameters for zero lag background
3718  vector<double> vGPS;
3719  vector<string> vSEARCH;
3720  vector<float> vIFAR;
3721  vector<int> vCOMBINE;
3722 
3723  // extract factors
3724  vector<float> factors;
3725  std::map <float, float> mfactors;
3726  if(simulation) {
3727  for(int n=0;n<2;n++) {
3728  int size=imtree[n]->GetEntries();
3729  imtree[n]->Draw("factor","","goff",size);
3730  double* factor = imtree[n]->GetV1();
3731  for(int i=0;i<size;i++) { // loop over the tree
3732  mfactors[factor[i]]=factor[i];
3733  }
3734  }
3735  }
3736  std::map<float, float>::iterator iter;
3737  for (iter=mfactors.begin(); iter!=mfactors.end(); iter++) {
3738  factors.push_back(iter->second);
3739  }
3740  int nfactor= simulation ? factors.size() : 1;
3741 
3742  // loop over the factors
3743  int nREC=0; // total number of recostructed events in combined search
3744  int nINJ=0; // total number of injected events in combined search;
3745  for(int i=0;i<nfactor;i++) {
3746  char wcuts[1024]="";
3747  char mcuts[1024]="";
3748  if(simulation) { // simulation -> select factor where to apply the combined search
3749  sprintf(wcuts,"(abs(factor-%g)/%g<0.0001)",factors[i],factors[i]);
3750  sprintf(mcuts,"(abs(factor-%g)/%g<0.0001)",factors[i],factors[i]);
3751  } else { // background -> select lag/slag where to apply the combined search
3752  sprintf(wcuts,"lag[%d]==0&&slag[%d]==0",nIFO,nIFO);
3753  }
3754  if(ifarthr>=0) {char cuts[1024];sprintf(cuts,"%s&&(ifar>%e)",wcuts,ifarthr*YEAR);strcpy(wcuts,cuts);}
3755  cout << endl << " Factor " << i+1 << "\twave cuts -> " << wcuts << endl;
3756  if(simulation) cout << " Factor " << i+1 << "\tmdc cuts -> " << mcuts << endl;
3757  cout << endl;
3758  // extract Entry,time,ifar,frequency from wave tree
3759  int iwsize[2]; // total number of entries in the wave tree
3760  double* iwentry[2]; // wave tree entry index
3761  double* iwtime[2]; // injection GPS time of the first ifo
3762  double* iwifar[2]; // ifar
3763  double* iwfreq[2]; // reconstructed median frequency
3764  int cwsize[2]={0,0}; // number of wave entries with factor=factors[i]
3765  char wsel[1024];
3766  if(simulation) {
3767  sprintf(wsel,"Entry$:time[%i]:ifar:frequency[0]",nIFO);
3768  } else { // zero lag
3769  sprintf(wsel,"Entry$:time[0]:ifar:frequency[0]");
3770  }
3771  for(int n=0;n<2;n++) {
3772  iwsize[n]=iwtree[n]->GetEntries();
3773  iwtree[n]->Draw(wsel,wcuts,"goff",iwsize[n]);
3774  iwentry[n] = iwtree[n]->GetV1();
3775  iwtime[n] = iwtree[n]->GetV2();
3776  iwifar[n] = iwtree[n]->GetV3();
3777  iwfreq[n] = iwtree[n]->GetV4();
3778  cwsize[n] = iwtree[n]->GetSelectedRows();
3779  }
3780 
3781  // extract Entry,time,factor from mdc tree
3782  int imsize[2]; // total number of entries in the mdc tree
3783  double* imentry[2]; // mdc tree entry index
3784  double* imtime[2]; // injection GPS time of the first ifo
3785  int cmsize[2]={0,0}; // number of mdc entries with factor=factors[i]
3786  char msel[1024];
3787  sprintf(msel,"Entry$:time[0]");
3788  if(simulation) cout << " Factor " << i+1 << "\tmdc selections-> " << msel << endl << endl;
3789  if(simulation) {
3790  for(int n=0;n<2;n++) {
3791  imsize[n]=imtree[n]->GetEntries();
3792  imtree[n]->Draw(msel,mcuts,"goff",imsize[n]);
3793  imentry[n] = imtree[n]->GetV1();
3794  imtime[n] = imtree[n]->GetV2();
3795  cmsize[n] = imtree[n]->GetSelectedRows();
3796  }
3797  }
3798 
3799  // merge arrays extracted from wave,mdc tree
3800  int size = cwsize[0]+cwsize[1]+cmsize[0]+cmsize[1];
3801  if(size==0) {cout << "CWB::Toolbox::CombineCBC - Error : no entries in wave/mdc tree" << endl; return 1;}
3802 
3803  int* index = new int[size]; // used by sort function
3804  int* entry = new int[size]; // entry id for root tree
3805  int* ttype = new int[size]; // tree type. 0/1 -> wave/mdc
3806  int* stype = new int[size]; // search type. 0/1 -> IMBHB/BBH
3807  double* time = new double[size]; // GPS time of the injection for the first ifo
3808  double* ifar = new double[size]; // inverse FAR in sec
3809  double* freq = new double[size]; // reconstructed median event frequency
3810  string* log = new string[size]; // mdc log string, contains the input parameters for simulation
3811  string* ilog = new string(); // used by SetBranchAddress to exctract log from ttree entry
3812 
3813  int k=0;
3814  for(int n=0;n<2;n++) { // loop over search type 0/1 -> IMBHB/BBH
3815  // fill wave
3816  for(int i=0;i<cwsize[n];i++) {
3817  entry[k] = iwentry[n][i];
3818  ttype[k] = 0; // wave
3819  stype[k] = n;
3820  time[k] = iwtime[n][i];
3821  ifar[k] = iwifar[n][i];
3822  freq[k] = iwfreq[n][i];
3823  log[k] = "";
3824  k++;
3825  }
3826  if(!simulation) continue; // skip mdc if wave files are background
3827  // fill mdc
3828  imtree[n]->SetBranchAddress("log",&ilog);
3829  for(int i=0;i<cmsize[n];i++) {
3830  entry[k] = imentry[n][i];
3831  ttype[k] = 1; // mdc
3832  stype[k] = n;
3833  time[k] = imtime[n][i];
3834  ifar[k] = 0;
3835  freq[k] = 0;
3836  imtree[n]->GetEntry(entry[k]);
3837  log[k] = *ilog;
3838  k++;
3839  }
3840  }
3841 
3842  if(!simulation) {
3843  cout << endl << "List of uncombined events found in " << msearch << " search" << endl << endl;
3844  for(int k=0;k<size;k++) {
3845  cout << k+1 << "\tsearch -> " << (stype[k]==0 ? "IMBHB" : "BBH")
3846  << "\tGPS -> " << std::setprecision(14) << time[k]
3847  << "\tFREQ -> " << std::setprecision(4) << freq[k]
3848  << "\tIFAR -> " << std::setprecision(6) << ifar[k]/YEAR << " (years)" << endl;
3849  }
3850  cout << endl;
3851  }
3852 
3853  // sort events wrt time
3854  TMath::Sort(size,time,index,kFALSE);
3855 
3856  // array to store the number of entries in wave/mdc
3857  int esize[2]={0,0}; // wave/mdc -> 0/1
3858 
3859  // arrays used to store the event with the same GPS time. position 0/1 -> IMBHB/BBH
3860  float eifar[2]={0,0}; // inverse far
3861  float efreq[2]={0,0}; // reconstructed median frequency
3862  string elog[2]={"",""}; // MDC log string used for simulation
3863  int ewidx[2]={-1,-1}; // ttree entry wave index
3864  int emidx[2]={-1,-1}; // ttree entry mdc index
3865 
3866  // events found in wave/mdc/cases
3867  int nWAVE=0; // reconstructed events
3868  int nMDC=0; // injected events
3869  int nX[8]={0,0,0,0,0,0,0,0}; // events found in combine cases
3870 
3871  // fill first entry
3872  int j=index[0];
3873  double ptime=time[j]; // previous GPS event time
3874  double ctime=0.; // current GPS event time
3875 
3876  if(ttype[j]==0) {
3877  eifar[stype[j]]=ifar[j];
3878  efreq[stype[j]]=freq[j];
3879  ewidx[stype[j]]=j;
3880  }
3881  if(ttype[j]==1) {
3882  elog[stype[j]]=log[j];
3883  emidx[stype[j]]=j;
3884  }
3885  esize[ttype[j]]++;
3886 
3887  int iSEARCH=0; // store the search type IMBHB/BBH -> 0/1
3888  int kstart = size==1 ? 0 : 1; // manage the case with size=1;
3889  int kstop = size+1; // manage the last event
3890  for(int k=kstart; k<kstop; k++) { // loop over the IMBHB,BBH wave/mdc events
3891  // event with the same GPS time belong to the same event
3892  // event is combined if the same injection has been performed in both searches: IMBHB and BBH -> esize[1]=2 and elog[0]=elog[1]
3893  // esize[0]=0 -> event not reconstructed in both searches
3894  // esize[0]=1 -> event reconstructed in only in one search -> iSEARCH=stype[j]
3895  // esize[0]=2 -> event reconstructed on both searches
3896  if(k<size) {
3897  j=index[k];
3898  ctime=time[j];
3899  } else { // last event
3900  ctime=ptime+1; // just to make ctime>ptime
3901  }
3902  if(fabs(ctime-ptime)>TIME_UNCERTAINTY) { // New event, we process the previous
3903 
3904  if(esize[0]>2) {
3905  cout << "CWB::Toolbox::CombineCBC - Error : ";
3906  if(simulation) cout << "number of reconstructed events per injection is > 2" << endl;
3907  else cout << "number of reconstructed events within " << TIME_UNCERTAINTY << " (sec) is > 2" << endl;
3908  cout << endl;
3909  exit(1);
3910  }
3911  if(esize[1]>ifmdc.size()) {
3912  cout << "CWB::Toolbox::CombineCBC - Error : number of injections per event is > " << ifmdc.size() << endl;
3913  exit(1);
3914  }
3915 
3916  // case simulation: if injection is present in BBH and IMBHB then write mdc entry in output wave/mdc files
3917  if(esize[1]==ifmdc.size()) {
3918 
3919  // MDC log IMBHB & BBH must be the same !!!
3920  if(elog[0].compare(elog[1])!=0) {
3921  cout << endl << "CWB::Toolbox::CombineCBC - Error : mdc log string are different" << endl << endl;
3922  cout << "IMBHB MDC log string: " << endl << elog[0].c_str() << endl << endl;
3923  cout << "BBH MDC log string: " << endl << elog[1].c_str() << endl << endl;
3924  exit(1);
3925  }
3926 
3927  if(simulation) nMDC++;
3928 
3929  COMBINE=0;
3930  if(esize[0]==1) { // event has been reconstructed only in one search
3931  iSEARCH=stype[ewidx[ewidx[0]!=-1?0:1]]; // store search type into output var 0/1 -> IMBHB/BBH
3932  if(iSEARCH==0 && efreq[0]<=fthr) {COMBINE=5;IFAR=eifar[0];} // found by IMBHB search in IMBHB band
3933  if(iSEARCH==1 && efreq[1]<=fthr) {COMBINE=6;IFAR=eifar[1]/TRIALS;} // found by BBH search in IMBHB band
3934  if(iSEARCH==0 && efreq[0] >fthr) {COMBINE=7;IFAR=eifar[0]/TRIALS;} // found by IMBHB search in BBH band
3935  if(iSEARCH==1 && efreq[1] >fthr) {COMBINE=8;IFAR=eifar[1];} // found by BBH search in BBH band
3936  nWAVE++;
3937  }
3938  if(esize[0]==2) { // event has been reconstructed in both searches
3939  if(efreq[0]<=fthr && efreq[1]>fthr) COMBINE=3;
3940  if(efreq[1]<=fthr && efreq[0]>fthr) COMBINE=4;
3941  iSEARCH = eifar[0]>eifar[1] ? 0 : 1; // search type is the one with greater ifar
3942  IFAR = eifar[0]>eifar[1] ? eifar[0]/TRIALS : eifar[1]/TRIALS; // both freqs are in the wrong search band
3943  if(efreq[0]<=fthr && efreq[1]<=fthr) {COMBINE=1;IFAR=eifar[0];iSEARCH=0;} // both freqs are <= fthr (IMBHB band)
3944  if(efreq[0] >fthr && efreq[1] >fthr) {COMBINE=2;IFAR=eifar[1];iSEARCH=1;} // both freqs are > fthr (BBH band)
3945  nWAVE++;
3946  }
3947  nX[COMBINE-1]++;
3948 
3949  // save entry into mdc output file
3950  if(esize[1]==2) {
3951  imtree[iSEARCH]->GetEntry(entry[emidx[iSEARCH]]);
3952  tmtree[iSEARCH]->Fill();
3953  nINJ++;
3954  }
3955  // if event has been reconstructed in IMBHB or/and BBH searches then save entry into wave output file
3956  if(esize[0]>0) {
3957  *SEARCH = iSEARCH==0 ? "IMBHB" : "BBH";
3958  if(simulation) {
3959  iwtree[iSEARCH]->GetEntry(entry[ewidx[iSEARCH]]);
3960  twtree[iSEARCH]->Fill();
3961  } else { // zero lag
3962  vGPS.push_back(ptime);
3963  vSEARCH.push_back(*SEARCH);
3964  vIFAR.push_back(IFAR);
3965  vCOMBINE.push_back(COMBINE);
3966  }
3967  nREC++;
3968  }
3969  }
3970  // reset counters
3971  esize[0]= 0;esize[1]= 0;
3972  ewidx[0]=-1;ewidx[1]=-1;
3973  emidx[0]=-1;emidx[1]=-1;
3974  }
3975 
3976  // update combine event parameters
3977  ptime=ctime;
3978  if(ttype[j]==0) { // wave
3979  eifar[stype[j]]=ifar[j];
3980  efreq[stype[j]]=freq[j];
3981  ewidx[stype[j]]=j;
3982  }
3983  if(ttype[j]==1) { // mdc
3984  elog[stype[j]]=log[j];
3985  emidx[stype[j]]=j;
3986  }
3987  esize[ttype[j]]++;
3988  }
3989 
3990  // print selected events statistic
3991  cout << endl;
3992  cout << "----------------------------------------------------------------------------------------------" << endl;
3993  cout << " factor id = " << i+1 << endl;
3994  cout << " " << " -> injected events IMBHB/BBH : " << cmsize[0] << "/" << cmsize[1] << endl;
3995  cout << " " << " -> reconstructed events IMBHB/BBH : " << cwsize[0] << "/" << cwsize[1] << endl;
3996  cout << " " << " -> combined reconstructed/injected events : " << nWAVE << "/" << nMDC << endl;
3997  cout << "----------------------------------------------------------------------------------------------" << endl;
3998  cout << " Case 1 -> TRIALS=1 IMBHB band (IMBHB BBH), BBH band ( ) = " << nX[0] << endl;
3999  cout << " Case 2 -> TRIALS=1 IMBHB band ( ), BBH band (IMBHB BBH) = " << nX[1] << endl;
4000  cout << " Case 3 -> TRIALS=2 IMBHB band (IMBHB ), BBH band ( BBH) = " << nX[2] << endl;
4001  cout << " Case 4 -> TRIALS=2 IMBHB band ( BBH), BBH band (IMBHB ) = " << nX[3] << endl;
4002  cout << " Case 5 -> TRIALS=1 IMBHB band (IMBHB ), BBH band ( ) = " << nX[4] << endl;
4003  cout << " Case 6 -> TRIALS=2 IMBHB band ( BBH), BBH band ( ) = " << nX[5] << endl;
4004  cout << " Case 7 -> TRIALS=2 IMBHB band ( ), BBH band (IMBHB ) = " << nX[6] << endl;
4005  cout << " Case 8 -> TRIALS=1 IMBHB band ( ), BBH band ( BBH) = " << nX[7] << endl;
4006  cout << "----------------------------------------------------------------------------------------------" << endl;
4007  cout << endl;
4008 
4009  // release memory
4010  delete[] index;
4011  delete[] entry;
4012  delete[] ttype;
4013  delete[] stype;
4014  delete[] time;
4015  delete[] ifar;
4016  delete[] freq;
4017  delete[] log;
4018  delete ilog;
4019  }
4020  delete SEARCH;
4021  cout << endl;
4022  cout << " Total number of REC/INJ = " << nREC << "/" << nINJ << endl;
4023  cout << endl;
4024 
4025  if(simulation) {
4026 
4027  // write temporary output mdc/wave file
4028  for(int n=0;n<2;n++) {
4029  twroot[n]->cd();
4030  twtree[n]->Write();
4031  twroot[n]->Close();
4032  }
4033  for(int n=0;n<2;n++) {
4034  tmroot[n]->cd();
4035  tmtree[n]->Write();
4036  tmroot[n]->Close();
4037  }
4038 
4039  // merge temporary wave/mdc root files into final root files
4040  TList *owlist = new TList;
4041  for(int n=0;n<2;n++) {
4042  twroot[n] = new TFile(tfwave[n],"READ"); // open output temporary wave root file
4043  twtree[n] = (TTree *)twroot[n]->Get("waveburst");
4044  owlist->Add(twtree[n]);
4045  }
4046  TFile* owroot = new TFile(ofwave,"RECREATE"); // open output wave root file
4047  if(owroot->IsZombie()) {
4048  cout << "CWB::Toolbox::CombineCBC - Error : output wave file " << ofwave << " not opened" << endl;
4049  exit(1);
4050  }
4051  gROOT->cd();
4052  owroot->cd();
4053  TTree* owtree = TTree::MergeTrees(owlist);
4054  owtree->SetName("waveburst");
4055  owtree->Write();
4056  owroot->Close();
4057  for(int n=0;n<2;n++) twroot[n]->Close();
4058 
4059  // create output mdc file
4060  TList *omlist = new TList;
4061  for(int n=0;n<2;n++) {
4062  tmroot[n] = new TFile(tfmdc[n],"READ"); // open output temporary mdc root file
4063  tmtree[n] = (TTree *)tmroot[n]->Get("mdc");
4064  omlist->Add(tmtree[n]);
4065  }
4066  TFile* omroot = new TFile(ofmdc,"RECREATE"); // open output mdc root file
4067  if(omroot->IsZombie()) {
4068  cout << "CWB::Toolbox::CombineCBC - Error : output mdc file " << ofmdc << " not opened" << endl;
4069  exit(1);
4070  }
4071  gROOT->cd();
4072  TTree* omtree = TTree::MergeTrees(omlist);
4073  omroot->cd();
4074  omtree->SetName("mdc");
4075  omtree->Write();
4076  omroot->Close();
4077  for(int n=0;n<2;n++) twroot[n]->Close();
4078 
4079  // remove temporary mdc/wave root files
4080  for(int n=0;n<2;n++) {
4081  char cmd[1024]; sprintf(cmd,"rm %s",tfwave[n].Data());
4082  //cout << cmd << endl;
4083  gSystem->Exec(cmd);
4084  }
4085  for(int n=0;n<2;n++) {
4086  char cmd[1024]; sprintf(cmd,"rm %s",tfmdc[n].Data());
4087  //cout << cmd << endl;
4088  gSystem->Exec(cmd);
4089  }
4090  } else { // write combined output wave file, only zero lag events are updated
4091 
4092  iwroot[msearch_id]->cd();
4093  int isize = iwtree[msearch_id]->GetEntries();
4094 
4095  TFile* owroot = new TFile(ofwave,"RECREATE"); // open output wave root file
4096  if(owroot->IsZombie()) {
4097  cout << "CWB::Toolbox::CombineCBC - Error : output wave file " << ofwave << " not opened" << endl;
4098  exit(1);
4099  }
4100  TTree* owtree = (TTree*)iwtree[msearch_id]->CloneTree(0);
4101  owtree->SetMaxTreeSize(MAX_TREE_SIZE);
4102 
4103  float oIFAR; // variable used to update the IFAR
4104  int oCOMBINE=0; // new tree leaf used to store the selected combine case
4105  string* oSEARCH = new string(); // new tree leaf used to store the selected combine search
4106  float iIFAR;
4107  double* iTIME = new double[2*nIFO];
4108  float* iFREQ = new float[nIFO];
4109  float* iLAG = new float[nIFO+1];
4110  float* iSLAG = new float[nIFO+1];
4111 
4112  owtree->SetBranchAddress("ifar",&oIFAR);
4113  owtree->Branch("combine",&oCOMBINE,"combine/I");
4114  owtree->Branch("search",oSEARCH);
4115  iwtree[msearch_id]->SetBranchAddress("ifar",&iIFAR);
4116  iwtree[msearch_id]->SetBranchAddress("time",iTIME);
4117  iwtree[msearch_id]->SetBranchAddress("frequency",iFREQ);
4118  iwtree[msearch_id]->SetBranchAddress("lag",iLAG);
4119  iwtree[msearch_id]->SetBranchAddress("slag",iSLAG);
4120 
4121  cout << "List of combined events found in " << msearch << " search" << endl;
4122  cout << endl;
4123  int k=1;
4124  for(int i=0;i<isize;i++) {
4125  iwtree[msearch_id]->GetEntry(i);
4126  if(iLAG[nIFO]==0 && iSLAG[nIFO]==0) {
4127  for(int j=0;j<vGPS.size();j++) {
4128  if(fabs(iTIME[0]-vGPS[j])<TIME_UNCERTAINTY) {
4129  *oSEARCH = vSEARCH[j];
4130  oCOMBINE = vCOMBINE[j];
4131  oIFAR = vIFAR[j];
4132  cout << k++ << "\tcombine -> " << oCOMBINE << "\tsearch -> " << oSEARCH->c_str()
4133  << "\tGPS -> " << std::setprecision(14) << vGPS[j]
4134  << "\tFREQ -> " << std::setprecision(4) << iFREQ[0]
4135  << "\toIFAR -> " << std::setprecision(6) << oIFAR/YEAR << " (years)" << endl;
4136  }
4137  }
4138  } else {
4139  *oSEARCH = msearch;
4140  oCOMBINE = 0;
4141  oIFAR = iIFAR;
4142  }
4143  owtree->Fill();
4144  }
4145  cout << endl;
4146 
4147  owroot->cd();
4148  owtree->Write();
4149  owroot->Close();
4150 
4151  delete oSEARCH;
4152  delete[] iTIME;
4153  delete[] iLAG;
4154  delete[] iSLAG;
4155  }
4156 
4157  // create CombineCBC history
4158  // note: only the history of master search is saved
4159  CWB::History* history = (CWB::History*)iwroot[msearch_id]->Get("history");
4160  if(history==NULL) history=new CWB::History();
4161  if(history!=NULL) {
4162  TList* stageList = history->GetStageNames(); // get stage list
4163  TString pcuts="";
4164  for(int i=0;i<stageList->GetSize();i++) { // get previous cuts
4165  TObjString* stageObjString = (TObjString*)stageList->At(i);
4166  TString stageName = stageObjString->GetString();
4167  char* stage = const_cast<char*>(stageName.Data());
4168  if(stageName=="CUTS") pcuts=history->GetHistory(stage,const_cast<char*>("PARAMETERS"));
4169  }
4170  // update history
4171  history->SetHistoryModify(true);
4172  if(!history->StageAlreadyPresent(CCAST("COMBINE"))) history->AddStage(CCAST("COMBINE"));
4173  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
4174  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
4175  char work_dir[1024]="";
4176  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
4177  history->AddHistory(CCAST("COMBINE"), CCAST("WORKDIR"), work_dir);
4178  TString cuts = (pcuts!="") ? pcuts+" ("+infos+")" : "("+infos+")";
4179  history->AddHistory(CCAST("CUTS"), CCAST("PARAMETERS"), CCAST(cuts.Data()));
4180  char iparms[1024];
4181  sprintf(iparms,"IMBHB_ifwave=%s, BBH_ifwave=%s, fthr=%0.2f",ifwave[0].Data(),ifwave[1].Data(),fthr);
4182  history->AddHistory(CCAST("COMBINE"), CCAST("PARAMETERS"), CCAST(iparms));
4183  char logmsg[2048]; sprintf(logmsg,"Combine IMBHB and BBH searches : %s","(J)");
4184  history->AddLog(CCAST("COMBINE"), CCAST(logmsg));
4185  }
4186 
4187  // close input root file
4188  for(int n=0;n<2;n++) iwroot[n]->Close();
4189 
4190  // write CombineCBC history into wave/mdc files
4191  if(history!=NULL) {
4192  TFile owfile(ofwave,"UPDATE");
4193  history->Write("history");
4194  owfile.Close();
4195  if(simulation) {
4196  TFile omfile(ofmdc,"UPDATE");
4197  history->Write("history");
4198  omfile.Close();
4199  }
4200  delete history;
4201  }
4202 
4203  return 0;
4204 }
4205 
4206 //______________________________________________________________________________
4207 int
4208 CWB::Toolbox::setVeto(TString ifName, TString idir, TString odir, vector<TString> ifos, int nVDQF, dqfile* VDQF,
4209  int nDQF, dqfile* DQF, double segLen, double segMLS, double segEdge) {
4210 //
4211 // apply Data Quality and veto to waveburst in ifname root file
4212 // and create an output root file adding a flag for each Data Quality
4213 // return selected entries
4214 //
4215 // ifname : input root file name
4216 // idir : input dir
4217 // odir : output dir
4218 // ifos : detector lists
4219 // nVDQF : number of Data Quality files
4220 // VDQF : Data Quality files structures
4221 // nDQF :
4222 // DQF :
4223 // segLen : segment job lenght
4224 // segMLS : minimum segment lenght after DQ2
4225 // segEdge : scratch lenght
4226 //
4227 
4228 
4229  int nIFO = ifos.size();
4230 
4231  CWB::History* history = NULL;
4232  bool bhistory=true;
4233 
4234  // extract from VDQF the set of declared detector's names not included in the ifos list
4235  vector<TString> wifos;
4236  for(int j=0;j<nVDQF;j++) {
4237  if(VDQF[j].cat==CWB_CAT0) continue;
4238  if(VDQF[j].cat==CWB_CAT1) continue;
4239  bool bifo=true;
4240  for(int i=0;i<(int)wifos.size();i++) if(wifos[i].CompareTo(VDQF[j].ifo)==0) bifo=false;
4241  for(int i=0;i<nIFO;i++) if(ifos[i].CompareTo(VDQF[j].ifo)==0) bifo=false;
4242  if(bifo) wifos.push_back(VDQF[j].ifo);
4243  }
4244  for(int i=0;i<(int)wifos.size();i++) cout << wifos[i].Data() << " ";
4245  cout << endl;
4246 //gSystem->Exit(0);
4247 
4248  // build XDQF structure used for internal's computations
4249  int nXDQF=0;
4250  dqfile* XDQF = new dqfile[nVDQF*nIFO];
4251  // netifo contains network with dimension > nIFO (for exclusion vetoes)
4252  int nNET=TMath::Factorial(5);
4253  vector<TString>* xifos = new vector<TString>[nNET];
4254  for(int j=0;j<nVDQF;j++) {
4255  if(VDQF[j].cat==CWB_CAT0) continue;
4256  if(VDQF[j].cat==CWB_CAT1) continue;
4257  bool bifo=false;
4258  for(int i=0;i<nIFO;i++) if(ifos[i].CompareTo(VDQF[j].ifo)==0) bifo=true;
4259  // change CAT if ifo not presents into ifos array
4260  if(!bifo) {
4261  // insert CWB_EXC (WARNING : miss cases with det>3 !!!)
4262  for(int i=0;i<nIFO;i++) {
4263  for(int k=0;k<(int)wifos.size();k++) {
4264  if(wifos[k].CompareTo(VDQF[j].ifo)==0) {
4265  XDQF[nXDQF]=VDQF[j];
4266  strcpy(XDQF[nXDQF].ifo,ifos[i].Data());
4267  XDQF[nXDQF].cat=CWB_EXC;
4268  xifos[nXDQF]=ifos;
4269  xifos[nXDQF].push_back(wifos[k]);
4270  nXDQF++;
4271  }
4272  }
4273  }
4274  } else {
4275  xifos[nXDQF]=ifos;
4276  XDQF[nXDQF]=VDQF[j];
4277  nXDQF++;
4278  }
4279  }
4280  if(nXDQF==0) {
4281  cout << "CWB::Toolbox::setVeto - No veto found : setVeto terminated" << endl;
4282  gSystem->Exit(1);
4283  }
4284  for(int j=0;j<nXDQF;j++) {
4285  cout << "XDQF[" << j << "] " << CWB_CAT_NAME[XDQF[j].cat] << "_" << XDQF[j].ifo << " NETWORK : ";
4286  for(int i=0;i<(int)xifos[j].size();i++) cout << xifos[j][i].Data() << " ";
4287  cout << endl;
4288  }
4289 
4290  // get standard job list
4291  vector<waveSegment> cat1List = readSegList(nDQF, DQF, CWB_CAT1);
4292  vector<waveSegment> jobList = getJobList(cat1List, segLen, segMLS, segEdge);
4293  //vector<waveSegment> cat2List = readSegList(nDQF, DQF, CWB_CAT2);
4294 
4295  // build veto file label
4296  char ifo_label[10][1024];
4297  char veto_label[10][1024];
4298  int nset=0;
4299  for(int n=0;n<nXDQF;n++) {
4300  TString veto_name = CWB_CAT_NAME[XDQF[n].cat];
4301  bool bnew=true;
4302  for(int m=0;m<nset;m++) if(veto_name.CompareTo(veto_label[m])==0) bnew=false;
4303  if(bnew) strcpy(veto_label[nset++],veto_name.Data());
4304  }
4305  for(int m=0;m<nset;m++) strcpy(ifo_label[m],"");
4306  for(int n=0;n<nXDQF;n++) {
4307  TString veto_name = CWB_CAT_NAME[XDQF[n].cat];
4308  for(int m=0;m<nset;m++) {
4309  if(veto_name.CompareTo(veto_label[m])==0) {
4310  if(!TString(ifo_label[m]).Contains(XDQF[n].ifo)) {
4311  sprintf(ifo_label[m],"%s%s",ifo_label[m],XDQF[n].ifo);
4312  }
4313  }
4314  }
4315  }
4316  char veto_file_label[1024]="";
4317  for(int m=0;m<nset;m++) {
4318  if(strlen(veto_file_label)==0) {
4319  sprintf(veto_file_label,".V_%s%s",veto_label[m],ifo_label[m]);
4320  } else {
4321  sprintf(veto_file_label,"%s_%s%s",veto_file_label,veto_label[m],ifo_label[m]);
4322  }
4323  }
4324  strcpy(veto_file_label,TString(veto_file_label).ReplaceAll("veto_","").Data());
4325  strcpy(veto_file_label,TString(veto_file_label).ReplaceAll("1","").Data());
4326  sprintf(veto_file_label,"%s.root",veto_file_label);
4327  cout << "veto file label : " << veto_file_label << endl;
4328 
4329  TString efname = odir+"/"+ifName;
4330  efname.ReplaceAll(".root",veto_file_label);
4331  bool overwrite = checkFile(efname,true);
4332  if(!overwrite) gSystem->Exit(0);
4333 
4334  TString ifname = idir+"/"+ifName;
4335  TString ofname = odir+"/"+ifName;
4336  ofname.ReplaceAll(".root",".root.tmp.1");
4337 
4338 //cout.precision(14);
4339 //for(int k=0;k<jobList.size();k++)
4340 // cout << k << " " << jobList[k].start << " " << jobList[k].stop << endl;
4341 //gSystem->Exit(0);
4342 
4343  vector<waveSegment> vlist;
4344  for(int n=0;n<nXDQF;n++) {
4345  // find ifo index
4346  int iIFO=-1;
4347  for(int i=0;i<nIFO;i++) if(ifos[i].CompareTo(XDQF[n].ifo)==0) iIFO=i;
4348  if (iIFO==-1) continue; // sky ifo not presents into ifos array
4349 
4350 //cout << n << " ifile " << ifname.Data() << endl;
4351 //cout << n << " ofile " << ofname.Data() << endl;
4352 //cout << endl;
4353 
4354  // get veto list
4355  if(XDQF[n].cat==CWB_CAT2) {
4356  // for cat2 is necessary to merge all CAT2 in XDQF
4357  // RDQF contains only ifo which are presents in ifos array
4358  int nRDQF=0;
4359  dqfile* RDQF = new dqfile[nVDQF];
4360  for(int j=0;j<nVDQF;j++) {
4361  for(int k=0;k<nIFO;k++) if(ifos[k].CompareTo(VDQF[j].ifo)==0) RDQF[nRDQF++]=VDQF[j];
4362  }
4363 // for(int j=0;j<nRDQF;j++) cout << "RDQF[" << j << "] " << CWB_CAT_NAME[RDQF[j].cat] << "_" << RDQF[j].ifo.Data() << endl;
4364 //gSystem->Exit(0);
4365  vlist=readSegList(nRDQF, RDQF, CWB_CAT2);
4366  delete [] RDQF;
4367  } else if(XDQF[n].cat==CWB_EXC) {
4368  // extract list (higher net conf) for exclusion veto
4369  // RDQF contains only ifo which are presents in ifos array
4370  int nRDQF=0;
4371  dqfile* RDQF = new dqfile[nVDQF];
4372  for(int j=0;j<nVDQF;j++) {
4373  for(int k=0;k<(int)xifos[n].size();k++) if(xifos[n][k].CompareTo(VDQF[j].ifo)==0) RDQF[nRDQF++]=VDQF[j];
4374  }
4375 // for(int j=0;j<nRDQF;j++) cout << "EXC -> RDQF[" << j << "] " << CWB_CAT_NAME[RDQF[j].cat] << "_" << RDQF[j].ifo.Data() << endl;
4376  vlist=readSegList(nRDQF, RDQF, CWB_CAT2);
4377 //cout.precision(14);
4378 //for(int i=0;i<vlist.size();i++) {
4379 // cout << i << " " << vlist[i].start << " " << vlist[i].stop << endl;
4380 //}
4381 //cout << CWB_CAT_NAME[XDQF[n].cat].Data() << " list size " << vlist.size() << endl;
4382  } else {
4383  vlist=readSegList(XDQF[n]);
4384  }
4385  double vlist_time = getTimeSegList(vlist);
4386 /*
4387 if(XDQF[n].cat==CWB_CAT3) {
4388 cout.precision(14);
4389 for(int i=0;i<vlist.size();i++) {
4390  cout << i << " " << vlist[i].start << " " << vlist[i].stop << endl;
4391 }
4392 cout << CWB_CAT_NAME[XDQF[n].cat].Data() << " list size " << vlist.size() << endl;
4393 gSystem->Exit(1);
4394 }
4395 */
4396  TString veto_name = CWB_CAT_NAME[XDQF[n].cat]+"_"+XDQF[n].ifo;
4397  cout << veto_name << " list duration " << int(vlist_time) << " list size " << vlist.size() << endl;
4398 
4399  // -------------------------------------------------------------------------
4400  // open root file
4401  // -------------------------------------------------------------------------
4402  cout<<"Opening BKG File : " << ifname.Data() << endl;
4403  TFile ifile(ifname);
4404  if (ifile.IsZombie()) {
4405  cout << "CWB::Toolbox::setVeto - Error opening file " << ifname.Data() << endl;
4406  gSystem->Exit(1);
4407  }
4408  TTree *itree = (TTree*)ifile.Get("waveburst");
4409  if (itree==NULL) {
4410  cout << "CWB::Toolbox::setVeto - tree waveburst is not present in file " << ifname.Data() << endl;
4411  gSystem->Exit(1);
4412  }
4413  Int_t isize = (Int_t)itree->GetEntries();
4414  cout << "tree size : " << isize << endl;
4415  if (isize==0) {
4416  cout << "CWB::Toolbox::setVeto - tree waveburst is empty in file " << ifname.Data() << endl;
4417  gSystem->Exit(1);
4418  }
4419  itree->SetEstimate(isize);
4420  char selection[1024];sprintf(selection,"time[%d]",iIFO);
4421  itree->Draw(selection,"","goff",isize);
4422  double* time = itree->GetV1();
4423  // check if time is NaN
4424  for(int i=0;i<isize;i++) {
4425  if(TMath::IsNaN(time[i])) {
4426  cout.precision(14);
4427  cout << "CWB::Toolbox::setVeto - tree waveburst file " << ifname.Data() << " contains time NaN in ifo=" << ifos[iIFO] << " time=" << time[i] << endl;
4428  gSystem->Exit(1);
4429  }
4430  }
4431  // sort list
4432  Int_t *id = new Int_t[isize];
4433  bool *bveto = new bool[isize];
4434  TMath::Sort((int)isize,time,id,false);
4435  // add dummy veto to vlist to permit a full trigger loop
4436  waveSegment SEG;
4437  SEG.start=time[id[isize-1]];
4438  SEG.stop=time[id[isize-1]];
4439  vlist.push_back(SEG);
4440  int vsize = vlist.size();
4441 /*
4442 for(int h=0;h<isize;h++) {
4443 cout << h << " " << iIFO << " " << time[id[h]] << endl;
4444 }
4445 gSystem->Exit(1);
4446 */
4447 
4448  if(bhistory) {
4449  history=(CWB::History*)ifile.Get("history");
4450  if(history==NULL) history=new CWB::History();
4451  bhistory=false;
4452  }
4453 
4454  // -------------------------------------------------------------------------
4455  // add new leave to itree
4456  // -------------------------------------------------------------------------
4457  TBranch* branch;
4458  bool replaceVeto=false;
4459  TIter next(itree->GetListOfBranches());
4460  while ((branch=(TBranch*)next())) {
4461  if (veto_name.CompareTo(branch->GetName())==0) {
4462  cout << "Veto [" << veto_name << "] already applied" << endl;
4463  char answer[1024];
4464  strcpy(answer,"");
4465  do {
4466  cout << "Do you want to overwrite the previous spurious ? (y/n) ";
4467  cin >> answer;
4468  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
4469  if (strcmp(answer,"y")==0) {
4470  replaceVeto=true;
4471  } else {
4472  gSystem->Exit(-1);
4473  }
4474  }
4475  }
4476  next.Reset();
4477 
4478  // -------------------------------------------------------------------------
4479  // create temporary root file
4480  // -------------------------------------------------------------------------
4481  UChar_t bVeto;
4482  TFile* ftmp = new TFile(ofname,"RECREATE");
4483  if (ftmp->IsZombie()) {
4484  cout << "CWB::Toolbox::setVeto - Error opening file " << ofname.Data() << endl;
4485  gSystem->Exit(1);
4486  }
4487  TTree *trtmp = (TTree*)itree->CloneTree(0);
4488  trtmp->SetMaxTreeSize(MAX_TREE_SIZE);
4489  TString tVeto = veto_name;tVeto+="/b";
4490  if (replaceVeto) {
4491  trtmp->SetBranchAddress(veto_name.Data(),&bVeto);
4492  } else {
4493  trtmp->Branch(veto_name.Data(),&bVeto,tVeto.Data());
4494  }
4495  trtmp->Write();
4496 
4497  // -------------------------------------------------------------------------
4498  // insert flag into event tree
4499  // we add a dummy flag entry to the end of onoff array to avoid to discart good events when
4500  // no flag entries are present
4501  // -------------------------------------------------------------------------
4502  cout << "Start applying flag to time["<<iIFO<<"]..." << endl;
4503 
4504  ftmp->cd();
4505 
4506  int h=0;
4507  int pc = 0;
4508  int ipc = (int)((double)(isize+1)/10.); if(ipc==0) ipc=1;
4509  int count=0;
4510  double ttime=time[id[h]];
4511  for (int h=0;h<isize;h++) bveto[h]=0;
4512  for (int j=0;j<vsize;j++) {
4513  while ((ttime<=vlist[j].stop) && (h<isize)) {
4514  if ((ttime>vlist[j].start)&&(ttime<=vlist[j].stop)) {bveto[id[h]]=1;count++;} else bveto[id[h]]=0;
4515  if(++h<isize) ttime=time[id[h]];
4516  if (h%ipc==0) {cout << pc;if (pc<100) cout << " - ";pc+=10;cout.flush();}
4517  }
4518  }
4519  cout << pc << endl << endl;
4520 
4521  for (int h=0;h<isize;h++) {
4522  itree->GetEntry(h);
4523  bVeto=bveto[h];
4524  trtmp->Fill();
4525  }
4526  trtmp->Write();
4527  delete [] id;
4528  delete [] bveto;
4529 
4530  cout << "Writing new ofile "<< ofname.Data() << endl;
4531  Int_t osize = (Int_t)trtmp->GetEntries();
4532  cout << "osize : " << osize << endl;
4533  cout << "Flagged events: " << count << " Percentage: "<< (double)count/osize << endl;
4534 
4535  if(history!=NULL) {
4536  // update history
4537  history->SetHistoryModify(true);
4538  if(!history->StageAlreadyPresent(CCAST("VETO"))) history->AddStage(CCAST("VETO"));
4539 
4540  // save pp configuration file
4541  if(!history->TypeAllowed(CCAST("PARAMETERS"))) history->AddType(CCAST("PARAMETERS"));
4542  TString ecwb_pparameters_name = TString(gSystem->Getenv("CWB_PPARAMETERS_FILE"));
4543  for(int i=0;i<gApplication->Argc()-1;i++) { // skip last argument (net.C)
4544  if(TString(gApplication->Argv(i)).Contains(".C")) {
4545  char* parametersBuffer = readFile(gApplication->Argv(i));
4546  if(parametersBuffer!=NULL) {
4547  if(TString(gApplication->Argv(i))==ecwb_pparameters_name) {
4548  history->AddHistory(CCAST("VETO"), CCAST("PARAMETERS"), parametersBuffer);
4549  } else {
4550  //history->AddLog(job_stage, parametersBuffer);
4551  }
4552  }
4553  delete [] parametersBuffer;
4554  }
4555  }
4556 
4557  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
4558  char work_dir[1024]="";
4559  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
4560  history->AddHistory(CCAST("VETO"), CCAST("WORKDIR"), work_dir);
4561  //history->AddLog(CCAST("VETO"), CCAST(veto_name.Data()));
4562  char veto_log[1024];
4563  sprintf(veto_log,"%s Flagged events: %d/%d - Percentage : %f ",
4564  veto_name.Data(), count, osize, (double)count/osize );
4565  history->AddLog(CCAST("VETO"), CCAST(veto_log));
4566  }
4567 
4568  ftmp->Close();
4569  ifile.Close();
4570  cout << endl;
4571 
4572  ifname=ofname;
4573  if(n%2) ofname.ReplaceAll(".root.tmp.2",".root.tmp.1");
4574  else ofname.ReplaceAll(".root.tmp.1",".root.tmp.2");
4575  }
4576 
4577  delete [] XDQF;
4578  for(int i=0;i<nNET;i++) xifos[i].clear();
4579  delete [] xifos;
4580 
4581  TString rfname = ofname;
4582  ofname = efname;
4583 
4584  TString lfname = ifName; // out live file name
4585  lfname.ReplaceAll(".root",veto_file_label);
4586  lfname.ReplaceAll("wave_","live_");
4587  TString ilfname = idir+"/"+ifName; // in live file name
4588  ilfname.ReplaceAll("wave_","live_");
4589  TString imfname = ilfname; // in mdc file name
4590  imfname.ReplaceAll("live_","mdc_");
4591  TString mfname = lfname; // out mdc file name
4592  mfname.ReplaceAll("live_","mdc_");
4593  TString ilstfname = idir+"/"+ifName; // in list file name
4594  ilstfname.ReplaceAll("wave_","merge_");
4595  ilstfname.ReplaceAll(".root",".lst");
4596  TString lstfname = lfname; // out list file name
4597  lstfname.ReplaceAll("live_","merge_");
4598  lstfname.ReplaceAll(".root",".lst");
4599 
4600  cout << " rfile : " << rfname.Data() << endl;
4601  cout << " ifile : " << ifname.Data() << endl;
4602  cout << " ofile : " << ofname.Data() << endl;
4603  cout << " lfile : " << lfname.Data() << endl;
4604  cout << " mfile : " << mfname.Data() << endl;
4605  cout << " ilstfile : " << ilstfname.Data() << endl;
4606  cout << " lstfile : " << lstfname.Data() << endl;
4607 
4608  char cmd[1024];
4609  sprintf(cmd,"mv %s %s",ifname.Data(),ofname.Data());
4610  cout << cmd << endl;
4611  gSystem->Exec(cmd);
4612  int estat;
4613  Long_t id,size,flags,mt;
4614  estat = gSystem->GetPathInfo(ilfname,&id,&size,&flags,&mt);
4615  if (estat==0) {
4616  sprintf(cmd,"cd %s;ln -sf ../%s %s",odir.Data(),ilfname.Data(),lfname.Data());
4617  cout << cmd << endl;
4618  gSystem->Exec(cmd);
4619  }
4620  estat = gSystem->GetPathInfo(imfname,&id,&size,&flags,&mt);
4621  if (estat==0) {
4622  sprintf(cmd,"cd %s;ln -sf ../%s %s",odir.Data(),imfname.Data(),mfname.Data());
4623  cout << cmd << endl;
4624  gSystem->Exec(cmd);
4625  }
4626  estat = gSystem->GetPathInfo(ilstfname,&id,&size,&flags,&mt);
4627  if (estat==0) {
4628  sprintf(cmd,"cd %s;ln -sf ../%s %s",odir.Data(),ilstfname.Data(),lstfname.Data());
4629  cout << cmd << endl;
4630  gSystem->Exec(cmd);
4631  }
4632  sprintf(cmd,"rm %s",rfname.Data());
4633  cout << cmd << endl;
4634  gSystem->Exec(cmd);
4635 
4636  // write history to merge+veto file
4637  if(history!=NULL) {
4638  TFile fhist(ofname,"UPDATE");
4639  history->Write("history");
4640  fhist.Close();
4641  delete history;
4642  }
4643 
4644  vlist.clear();
4645 
4646  return 0;
4647 }
4648 
4649 //______________________________________________________________________________
4650 bool
4652 //
4653 // check if file exists
4654 //
4655 // Input: fName - file name to be checked
4656 //
4657 // Return true if it exists
4658 //
4659 
4660  // ----------------------------------------------------------------
4661  // Check if file exists
4662  // ----------------------------------------------------------------
4663  Long_t id,size=0,flags,mt;
4664  int estat = gSystem->GetPathInfo(fName.Data(),&id,&size,&flags,&mt);
4665  return (estat!=0) ? false : true;
4666 }
4667 
4668 //______________________________________________________________________________
4669 bool
4670 CWB::Toolbox::checkFile(TString fName, bool question, TString message) {
4671 //
4672 // check if file exists
4673 //
4674 // Input: fName - file name to be checked
4675 // question - true -> print question
4676 // message - question message
4677 //
4678 // Return true if answer=y
4679 //
4680 
4681  bool overwrite=true;
4682 
4683  // ----------------------------------------------------------------
4684  // Check if file exists
4685  // ----------------------------------------------------------------
4686  Long_t id,size=0,flags,mt;
4687  int estat = gSystem->GetPathInfo(fName.Data(),&id,&size,&flags,&mt);
4688  if ((estat!=0)&&(!question)) {
4689  cout << "CWB::Toolbox::checkFile - Error - File/Dir \"" << fName.Data() << "\" not exist" << endl;
4690  gSystem->Exit(1);
4691  }
4692 
4693  // -------------------------------------------------------------------------
4694  // Check if output file already exists and asks if you want to overwrite it
4695  // -------------------------------------------------------------------------
4696  if ((estat==0)&&(question)) {
4697  char answer[1024];
4698  strcpy(answer,"");
4699  do {
4700  cout << "File/Dir " << fName.Data() << " already exist" << endl;
4701  if(message.Sizeof()>1) cout << message.Data() << endl;
4702  cout << "Do you want to overwrite it ? (y/n) ";
4703  cin >> answer;
4704  cout << endl << endl;
4705  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
4706  if (strcmp(answer,"n")==0) overwrite=false;
4707  }
4708 
4709  return overwrite;
4710 }
4711 
4712 //______________________________________________________________________________
4713 void
4714 CWB::Toolbox::mkDir(TString dir, bool question, bool remove) {
4715 //
4716 // make dir
4717 //
4718 // Input: dir - directory name to be created
4719 // question - true -> ask confirm before creation
4720 // remove - false/true(def) -> not_remove/remove existing files in the dir
4721 //
4722 // Return true if answer=y
4723 //
4724 
4725  char cmd[1024];
4726  Long_t id,size=0,flags,mt;
4727  // Check if dir exist
4728  int estat = gSystem->GetPathInfo(dir.Data(),&id,&size,&flags,&mt);
4729  if((estat==0)&&(question==true)) {
4730  char answer[1024];
4731  strcpy(answer,"");
4732  do {
4733  cout << endl;
4734  cout << "dir \"" << dir.Data() << "\" already exist" << endl;
4735  cout << "Do you want to remove the files & recreate dir ? (y/n) ";
4736  cin >> answer;
4737  cout << endl;
4738  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
4739  if (strcmp(answer,"y")==0) {
4740  if(remove) sprintf(cmd,"rm %s/*",dir.Data());
4741  cout << cmd << endl;
4742  gSystem->Exec(cmd);
4743  sprintf(cmd,"mkdir -p %s",dir.Data());
4744  cout << cmd << endl;
4745  gSystem->Exec(cmd);
4746  }
4747  } else if((estat==0)&&(question==false)) {
4748  if(remove) {
4749  sprintf(cmd,"rm %s/*",dir.Data());
4750  cout << cmd << endl;
4751  gSystem->Exec(cmd);
4752  }
4753  sprintf(cmd,"mkdir -p %s",dir.Data());
4754  cout << cmd << endl;
4755  gSystem->Exec(cmd);
4756  } else {
4757  sprintf(cmd,"mkdir -p %s",dir.Data());
4758  cout << cmd << endl;
4759  gSystem->Exec(cmd);
4760  }
4761 
4762  return;
4763 }
4764 
4765 //______________________________________________________________________________
4766 bool
4767 CWB::Toolbox::rmDir(TString dir, bool question) {
4768 //
4769 // remove dir
4770 //
4771 // Input: dir - directory name to be removed
4772 // question - true -> ask confirm before remove
4773 //
4774 // Return true if answer=y
4775 //
4776 
4777  bool banswer=false;
4778  char cmd[1024];
4779  Long_t id,size=0,flags,mt;
4780  int estat = gSystem->GetPathInfo(dir.Data(),&id,&size,&flags,&mt);
4781  // Check if dir exist
4782  if((estat==0)&&(question==true)) {
4783  char answer[1024];
4784  strcpy(answer,"");
4785  do {
4786  cout << endl;
4787  sprintf(cmd,"rm -rf %s",dir.Data());
4788  cout << cmd << endl;
4789  cout << "Do you want to remove the dir ? (y/n) ";
4790  cin >> answer;
4791  cout << endl;
4792  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
4793  if (strcmp(answer,"y")==0) {
4794  banswer=true;
4795  gSystem->Exec(cmd);
4796  }
4797  } else if((estat==0)&&(question==false)) {
4798  sprintf(cmd,"rm -rf %s",dir.Data());
4799  cout << cmd << endl;
4800  gSystem->Exec(cmd);
4801  }
4802 
4803  return banswer;
4804 }
4805 
4806 //______________________________________________________________________________
4807 bool
4809 //
4810 // print question and wait for answer y/n
4811 //
4812 // question - question to be printed
4813 //
4814 // Return true/false if answer=y/n
4815 //
4816 
4817  char answer[1024];
4818  strcpy(answer,"");
4819  do {
4820  cout << endl;
4821  cout << question << " (y/n) ";
4822  cin >> answer;
4823  cout << endl;
4824  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
4825  return (strcmp(answer,"y")==0) ? true : false;
4826 }
4827 
4828 //______________________________________________________________________________
4829 TString
4830 CWB::Toolbox::addCWBFlags(TMacro macro, TString ofname) {
4831 //
4832 // Add Compiler cWB Flags to macro
4833 //
4834 // macro - input macro
4835 // ofname - if != "" than ofname is used to save macro file name with Compiler cWB Flags added
4836 // otherwise the name is obtained substituting ".C" in macro.GetTitle() with "_CWBFlags.C"
4837 //
4838 // return macro file name with Compiler cWB Flags added
4839 //
4840 
4841  bool extension = ofname=="" ? true : false;
4842  if(ofname=="") ofname = macro.GetTitle();
4843  if(!ofname.EndsWith(".C"))
4844  {cout << "CWB::Toolbox::addCWBFlags - Error : macro : " << ofname << " must have extension .C" << endl;gSystem->Exit(1);}
4845  if(extension) ofname.ReplaceAll(".C","_CWBFlags.C");
4846 
4847  // write macro with cWB compiler flags
4848  ofstream out;
4849  out.open(ofname.Data(),ios::out);
4850  if(!out.good()) {cout << "CWB::Toolbox::addCWBFlags - Error : Opening File : " << ofname << endl;gSystem->Exit(1);}
4851 
4852  // add compiler cWB flags
4853  out << "// -------------------------------------------------------------------------" << endl;
4854  out << "// Compiler cWB Flags" << endl;
4855  out << "// -------------------------------------------------------------------------" << endl;
4856  if(gSystem->Getenv("_USE_HEALPIX")) {
4857  out << "#ifndef _USE_HEALPIX" << endl;
4858  out << "#define _USE_HEALPIX" << endl;
4859  out << "#endif" << endl;
4860  }
4861  if(gSystem->Getenv("_USE_EBBH")) {
4862  out << "#ifndef _USE_EBBH" << endl;
4863  out << "#define _USE_EBBH" << endl;
4864  out << "#endif" << endl;
4865  }
4866  if(gSystem->Getenv("_USE_LAL")) {
4867  out << "#ifndef _USE_LAL" << endl;
4868  out << "#define _USE_LAL" << endl;
4869  out << "#endif" << endl;
4870  }
4871  out << "// -------------------------------------------------------------------------" << endl;
4872  out << endl;
4873 
4874  // write macro code
4875  TList* fLines = macro.GetListOfLines();
4876  TObjString *obj;
4877  TIter next(fLines);
4878  while ((obj = (TObjString*) next())) {
4879  TString line = obj->GetName();
4880  out << line.Data() << endl;
4881  }
4882  out.close();
4883 
4884  return ofname;
4885 }
4886 
4887 //______________________________________________________________________________
4888 double
4889 CWB::Toolbox::getZeroLiveTime(int nIFO, TChain& liv, int dummy) {
4890 //
4891 // Calculate live time of zero lag
4892 //
4893 // Input: nIFO : number of detectors
4894 // liv : tree containing live time informatio
4895 // dummy : not used
4896 //
4897 // Output: value of live time
4898 //
4899 
4900  // check if slag is presents in liv tree
4901  TBranch* branch;
4902  bool slagFound=false;
4903  TIter next(liv.GetListOfBranches());
4904  while ((branch=(TBranch*)next())) {
4905  if (TString("slag").CompareTo(branch->GetName())==0) slagFound=true;
4906  }
4907  next.Reset();
4908  if(!slagFound) {
4909  cout << "CWB::Toolbox::getZeroLiveTime : Error - live tree do not contains slag leaf" << endl;
4910  gSystem->Exit(1);
4911  }
4912 
4913  long int ntot = liv.GetEntries();
4914  liv.Draw("live",TString::Format("lag[%d]==0&&slag[%d]==0",nIFO,nIFO).Data(),"goff",ntot);
4915  long int nsel = liv.GetSelectedRows();
4916  double* live = liv.GetV1();
4917 
4918  double liveZero = 0.;
4919  for(long int i=0; i<nsel; i++) liveZero += live[i];
4920 
4921  return liveZero;
4922 }
4923 
4924 //______________________________________________________________________________
4925 double
4929  int lag_number, int slag_number, int dummy) {
4930 //
4931 // Calculate the live time of the total shift performed.
4932 // If defining a lag_number/slag_number, calculate live time referred to this
4933 //
4934 // Input: nIFO : detector number
4935 // liv : tree containing live time information
4936 //
4937 // Output:Trun : wavearray containing live time for each run number
4938 // - the returned size is the maximum run number
4939 // Wlag : wavearray containing lag progressive number
4940 // Wslag : wavearray containing slag progressive number
4941 // TLag : output list of time lag
4942 // TdLag : output list of time index
4943 // lag_number : specific lag number
4944 // slag_number : specific slag number
4945 // dummy : used to increase calculation speed
4946 //
4947 // Examples : (lag_number==-1) && (slag_number==-1) -> all non zero lags : excluded (lag==0&&slag==0)
4948 // (lag_number>=0) && (slag_number>=0) -> select (lag==lag_number) && (slag==slag_number)
4949 // (lag_number>=0) && (slag_number==-1) -> select lag=lag_number : excluded (lag==0&&slag==0)
4950 // (lag_number==-1) && (slag_number>=0) -> select slag=slag_number : excluded (lag==0&&slag==0)
4951 //
4952 // Note: the variable dummy has been introduced to optimize
4953 // speed of the method (the reason is unknown!!!)
4954 
4955 
4956  float xlag[NIFO_MAX+1];
4957  float xslag[NIFO_MAX+1];
4958  Int_t xrun;
4959  double xlive;
4960  double liveTot = 0.;
4961  double Live=0.;
4962  bool save;
4963 
4964  wavearray<double> Qdlag;
4965  wavearray<double>* Qlag = new wavearray<double>[nIFO+1];
4966  wavearray<double>* Qslag = new wavearray<double>[nIFO+1];
4967  wavearray<double> Qtlag;
4968 
4969  std::map <double, std::map <double, int> > QMap;
4970 
4971  // check if slag is presents in liv tree
4972  TBranch* branch;
4973  bool slagFound=false;
4974  TIter next(liv.GetListOfBranches());
4975  while ((branch=(TBranch*)next())) {
4976  if (TString("slag").CompareTo(branch->GetName())==0) slagFound=true;
4977  }
4978  next.Reset();
4979  if(!slagFound) {
4980  cout << "CWB::Toolbox::getLiveTime : Error - live tree do not contains slag leaf" << endl;
4981  gSystem->Exit(1);
4982  }
4983 
4984  int run_max = liv.GetMaximum("run");
4985 
4986  Trun.resize(run_max);
4987  long int ntot = liv.GetEntries();
4988 
4989  // get max lag
4990  TLeaf* leaf = liv.GetLeaf("lag");
4991  if(!leaf) {
4992  cout << "CWB::Toolbox::getLiveTime : Error - lag leaf is not present" << endl;
4993  gSystem->Exit(1);
4994  }
4995  branch = leaf->GetBranch();
4996  int lag_max=0;
4997  for (long int i=0; i<ntot; ++i) {
4998  long int entryNumber = liv.GetEntryNumber(i);
4999  if (entryNumber < 0) break;
5000  branch->GetEntry(entryNumber);
5001  double val = leaf->GetValue(nIFO);
5002  if (val>lag_max) lag_max = (int)val;
5003  }
5004 
5005  liv.SetBranchAddress("slag",xslag);
5006  liv.SetBranchAddress("lag",xlag);
5007  liv.SetBranchAddress("run",&xrun);
5008  liv.SetBranchAddress("live",&xlive);
5009  liv.SetBranchStatus("*",false);
5010  liv.SetBranchStatus("live",true);
5011  liv.SetBranchStatus("lag",true);
5012  liv.SetBranchStatus("slag",true);
5013  liv.SetBranchStatus("run",true);
5014 
5015  int pc = 0;
5016  int ipc = double(ntot)/10.; if(ipc==0) ipc=1;
5017  for(long int i=0; i<ntot; i++) {
5018  liv.GetEntry(i);
5019 
5020  if(i%ipc==0) {if(ntot>100) {cout << pc<<"%";if (pc<100) cout << " - ";pc+=10;cout.flush();}}
5021 
5022  if((lag_number>=0)&&(slag_number>=0)) {
5023  Live = (xlag[nIFO]==lag_number&&xslag[nIFO]==slag_number) ? xlive : 0.; // lag/slag live time
5024  }
5025  if((lag_number>=0)&&(slag_number==-1)) {
5026  Live = ((xlag[nIFO]==lag_number)&&!((xlag[nIFO]==0)&&(xslag[nIFO]==0))) ? xlive : 0.; // lag live time
5027  }
5028  if((lag_number==-1)&&(slag_number>=0)) {
5029  Live = ((xslag[nIFO]==slag_number)&&!((xlag[nIFO]==0)&&(xslag[nIFO]==0))) ? xlive : 0.; // slag live time
5030  }
5031  if((lag_number==-1)&&(slag_number==-1)) {
5032  Live = !((xlag[nIFO]==0)&&(xslag[nIFO]==0)) ? xlive : 0.; // non-zero live time
5033  }
5034 
5035  liveTot += Live;
5036  Trun.data[xrun-1] += Live;
5037 
5038  save = true;
5039 
5040  int K=-1;
5041  if(QMap.find(xlag[nIFO])!=QMap.end()) {
5042  if(QMap[xlag[nIFO]].find(xslag[nIFO])!=QMap[xlag[nIFO]].end()) {
5043  K=QMap[xlag[nIFO]][xslag[nIFO]];
5044  Qtlag.data[K] += xlive;
5045  save=false;
5046  }
5047  }
5048 
5049  if((lag_number>=0)&&(slag_number>=0)) {
5050  save = (xlag[nIFO]==lag_number&&xslag[nIFO]==slag_number) ? save : false;
5051  }
5052  if((lag_number>=0)&&(slag_number==-1)) {
5053  save = ((xlag[nIFO]==lag_number)&&!((xlag[nIFO]==0)&&(xslag[nIFO]==0))) ? save : false;
5054  }
5055  if((lag_number==-1)&&(slag_number>=0)) {
5056  save = ((xslag[nIFO]==slag_number)&&!((xlag[nIFO]==0)&&(xslag[nIFO]==0))) ? save : false;
5057  }
5058  if((lag_number==-1)&&(slag_number==-1)) {
5059  save = !((xlag[nIFO]==0)&&(xslag[nIFO]==0)) ? save : false;
5060  }
5061  if(save) {
5062  for (int j=0; j<nIFO; j++) Qlag[j].append(xlag[j]);
5063  for (int j=0; j<nIFO; j++) Qslag[j].append(xslag[j]);
5064  Qlag[nIFO].append(xlag[nIFO]);
5065  Qslag[nIFO].append(xslag[nIFO]);
5066  Qtlag.append(xlive);
5067  QMap[xlag[nIFO]][xslag[nIFO]] = Qtlag.size()-1;
5068  double dlag=0;
5069  //for (int j=0; j<nIFO; j++) dlag+=(xslag[j]+xlag[j])*(xslag[j]+xlag[j]);
5070  dlag=xslag[nIFO]*lag_max+xlag[nIFO];
5071  Qdlag.append(sqrt(dlag));
5072  }
5073  }
5074  if(pc==100) cout << pc<<"%";;
5075  cout << endl << endl;
5076 
5077  // sort lag using Qdlag (time distance respect to zero lag)
5078  double* dlag = new double[Qdlag.size()];
5079  for(int i=0;i<(int)Qdlag.size();i++) dlag[i]=Qdlag.data[i];
5080  Int_t *id = new Int_t[Qdlag.size()];
5081  TMath::Sort((int)Qdlag.size(),dlag,id,false);
5082  for(int i=0;i<(int)Qdlag.size();i++) {
5083  Tlag.append(Qtlag[id[i]]);
5084  //Tdlag.append(Qdlag[id[i]]);
5085  Tdlag.append(i);
5086  for (int j=0; j<nIFO+1; j++) {
5087  Wlag[j].append(Qlag[j][id[i]]);
5088  Wslag[j].append(Qslag[j][id[i]]);
5089  }
5090  }
5091  delete [] id;
5092  delete [] dlag;
5093 
5094  for (int j=0; j<nIFO+1; j++) {
5095  Qlag[j].resize(0);
5096  Qslag[j].resize(0);
5097  }
5098  delete [] Qlag;
5099  delete [] Qslag;
5100  Qtlag.resize(0);
5101  Qdlag.resize(0);
5102 
5103  return liveTot;
5104 }
5105 
5106 //______________________________________________________________________________
5107 vector<TString>
5108 CWB::Toolbox::getFileListFromDir(TString dir_name, TString endString, TString beginString, TString containString, bool fast) {
5109 //
5110 // get the file list contained in a directory
5111 //
5112 // Input: dir_name - input dir name
5113 // endString - matched end string in file name
5114 // beginString - matched initial string in file name
5115 // containString - matched contained string in file name
5116 // fast - true -> skip check if exist (faster mode)
5117 //
5118 // Output: Return file name list contained in the directory "dir_name"
5119 //
5120 
5121  TString wdir = gSystem->WorkingDirectory();
5122  TSystemDirectory gdir("", dir_name);
5123  TList *dfiles = NULL;
5124  if(fast) { // skip check if are dirs or files, faster when access to remote files
5125  void *dir = gSystem->OpenDirectory(dir_name);
5126  if(dir) {
5127  const char *file = 0;
5128  dfiles = new TList;
5129  dfiles->SetOwner();
5130  while ((file = gSystem->GetDirEntry(dir))) {
5131  dfiles->Add(new TSystemFile(file, dir_name));
5132  }
5133  gSystem->FreeDirectory(dir);
5134  }
5135  } else { // call standard root method (check if are dirs or files)
5136  dfiles = gdir.GetListOfFiles();
5137  }
5138  if(!dfiles) {
5139  cout << "CWB::Toolbox::getFileListFromDir : Error - dir not found !!!" << endl;
5140  gSystem->Exit(1);
5141  }
5142  TIter dnext(dfiles);
5143  TSystemFile *dfile;
5144  TString fname;
5145  vector<TString> fileList;
5146  char path[1024];
5147 
5148  while ((dfile = (TSystemFile*)dnext())) {
5149  fname = dfile->GetName();
5150  sprintf(path,"%s/%s",dir_name.Data(),fname.Data());
5151  bool fsave=true;
5152  if ((endString!="")&&!fname.EndsWith(endString)) fsave=false;
5153  if ((beginString!="")&&!fname.BeginsWith(beginString)) fsave=false;
5154  if ((containString!="")&&!fname.Contains(containString)) fsave=false;
5155  if(fsave) fileList.push_back(path);
5156  }
5157 
5158  gSystem->ChangeDirectory(wdir); // restore original dir
5159 
5160  delete dfiles;
5161 
5162  //cout << "CWB::Toolbox::getDirFileLists - nfiles : " << fileList.size() << endl;
5163 
5164  return fileList;
5165 }
5166 
5167 //______________________________________________________________________________
5168 std::map<int,TString>
5169 CWB::Toolbox::getJobFileMapFromDir(TString dir_name, TString endString, TString beginString, TString containString, bool fast) {
5170 //
5171 // get the job file map (jobId -> file) contained in a directory
5172 //
5173 // Input: dir_name - input dir name
5174 // endString - matched end string in file name
5175 // beginString - matched initial string in file name
5176 // containString - matched contained string in file name
5177 // fast - true -> skip check if exist (faster mode)
5178 //
5179 // Output: Return job file name map (jobId -> file) contained in the directory "dir_name"
5180 //
5181 
5182  vector<TString> fileList = getFileListFromDir(dir_name, endString, beginString, containString, fast);
5183 
5184  std::map<int, TString> fileMap;
5185  for(int i=0;i<fileList.size();i++) {
5186  fileMap[getJobId(fileList[i])]=fileList[i];
5187  }
5188  return fileMap;
5189 }
5190 
5191 //______________________________________________________________________________
5192 void
5195 //
5196 // It produces Poisson Plot on background stage
5197 //
5198 // Input nIFO : detector number
5199 // Wlag : wavearray containing lag progressive number
5200 // Tlag : wavearray containing live time
5201 // Rlag : wavearray containing rate of each lag
5202 // odir : output dir
5203 //
5204 
5205 // NOTE: Wlag,Tlag,Rlag are sorted using the time distance respect to zero lag -> Tlag[0] is the zero lag livetime
5206 // Uses lags with livetime which differ max 10% respect to lag zero livetime
5207 
5208  int NC=0;
5209  double LT=0.,LAMBDA;
5210  double normalization=0;
5211  double enormalization=0;
5212  double mean=0;
5213  double emean=0;
5214  double chi2=0;
5215  int ndf=0;
5216  int myNDF =-1; //one degree of freedom less to account for the mean estimation
5217  double plevel=0;
5218  double ChiSquare=0.0;
5219  double dChiSquare=0.0;
5220  int minCountsPerBin=5;
5221 
5222  double Tlag0 = Tlag.data[0]; // zero lag time
5223  int Nlags = 0; // number of lags which are used for poisson check
5224 
5225  // compute min/max false alarms per lag
5226  int min_fa_per_lag=1000000000;
5227  int max_fa_per_lag=0;
5228  for(int j=0; j<(int)Wlag[nIFO].size(); j++){
5229  if(Wlag[nIFO].data[j]>0.) {
5230  if(fabs(Tlag.data[j]-Tlag0)/Tlag0>0.1) continue;
5231  int fa_per_lag=Rlag.data[j]*Tlag.data[j];
5232  if(fa_per_lag<min_fa_per_lag) min_fa_per_lag=fa_per_lag;
5233  if(fa_per_lag>max_fa_per_lag) max_fa_per_lag=fa_per_lag;
5234  Nlags++;
5235  }
5236  }
5237  int nbin = max_fa_per_lag-min_fa_per_lag;
5238 
5239  //Histogram for the Poisson Test on the background estimation
5240  TH1I *h1 = new TH1I("h1","h1",nbin,min_fa_per_lag,max_fa_per_lag);
5241 
5242  for(int j=0; j<(int)Wlag[nIFO].size(); j++){
5243  if(Wlag[nIFO].data[j]>0.) { //Fill histogram for the Poisson Test on the background estimation
5244  if(fabs(Tlag.data[j]-Tlag0)/Tlag0>0.1) continue;
5245  h1->Fill(Rlag.data[j]*Tlag.data[j]);
5246  NC+=Rlag.data[j]*Tlag.data[j];
5247  LT+=Tlag.data[j];
5248  }
5249  }
5250  LAMBDA= LT>0 ? NC/LT : 0;
5251 
5252  cout << "Total Number of bkg coinc.: " << NC << " total Live Time: "
5253  << LT << " nb = " << LAMBDA << endl;
5254 
5255  if(NC==0) return;
5256 
5257  TCanvas *canvas = new TCanvas("BackgroundPoissonFit", "Background Poisson Fit",32,55,750,502);
5258  canvas->Range(-0.75,-1.23433,6.75,8.17182);
5259  canvas->SetBorderSize(1);
5260  canvas->SetFillColor(0);
5261  canvas->SetGridx();
5262  canvas->SetGridy();
5263  canvas->SetFrameFillColor(0);
5264 
5265  //Poisson Integer Function
5266  TF1 poisson("PoissonIFunction",PoissonIFunction,min_fa_per_lag,max_fa_per_lag,2);
5267  poisson.FixParameter(0,h1->GetEntries());
5268  poisson.SetParameter(1,h1->GetMean());
5269 
5270  TH1D* hfit = (TH1D*)h1->Clone("hfit");
5271 
5272  //loop over the populated bins (counts > minCountsPerBin) to calculate the p-level
5273  for (int k=1;k<=hfit->GetNbinsX();k++) {
5274  int n = k-1;
5275  if (poisson.Eval(n)>minCountsPerBin) {
5276  myNDF++; //myNDF starts from -1 !!
5277  dChiSquare=pow((hfit->GetBinContent(k)-poisson.Eval(n)),2)/poisson.Eval(n);
5278  ChiSquare+=dChiSquare;
5279  cout << "NCoinc = " << n << " Found = " << hfit->GetBinContent(k) << " Expected = "
5280  << poisson.Eval(n) << " Chi2_bin" << n << "= " << dChiSquare<<endl;
5281  }
5282  }
5283 
5284  if(myNDF<=0) {
5285  cout << "CWB::Toolbox::doPoissonPlot - Warning !!! - Poisson NDF <=0 " << endl;
5286  return;
5287  }
5288 
5289  // chi2
5290  double myPLevel=TMath::Prob(ChiSquare,myNDF);
5291  cout<<"myChiSquare :"<<ChiSquare<<endl;
5292  cout << "NDF : " << myNDF << endl;
5293  cout<<"myPlevel :"<<myPLevel<<endl;
5294  int XMIN=kMaxInt,XMAX=kMinInt;
5295  int g=0;
5296  while((hfit->GetBinContent(g)==0)&&(g<hfit->GetNbinsX())){XMIN=g;g++;}
5297  g=0;
5298  while((hfit->GetBinContent(hfit->GetNbinsX()-g)==0)&&(g<hfit->GetNbinsX())){XMAX=hfit->GetNbinsX()-g;g++;}
5299  XMIN--;
5300  XMAX++;
5301  cout<<XMIN<<" "<<XMAX<<endl;
5302  hfit->Fit("PoissonIFunction","R");
5303  TF1* fpois = hfit->GetFunction("PoissonIFunction");
5304  fpois->SetFillColor(kGreen);
5305  fpois->SetFillStyle(3002);
5306  fpois->SetLineColor(kGreen);
5307  fpois->SetLineStyle(1);
5308  fpois->SetLineWidth(1);
5309  fpois->SetLineColor(kGreen);
5310  hfit->SetTitle("Poisson Fit (Black : Data - Green : Fit)");
5311  //hfit->SetTitle(name);
5312  hfit->SetLineWidth(4);
5313  hfit->GetXaxis()->SetTitle("#False Alarms/lag");
5314  hfit->GetYaxis()->SetTitle("#Counts");
5315  hfit->GetXaxis()->SetTitleSize(0.04);
5316  hfit->GetYaxis()->SetTitleSize(0.05);
5317  hfit->GetXaxis()->SetLabelSize(0.04);
5318  hfit->GetYaxis()->SetLabelSize(0.04);
5319  hfit->GetXaxis()->SetTitleOffset(0.97);
5320  hfit->GetYaxis()->SetTitleOffset(1.0);
5321  hfit->GetXaxis()->SetRange(XMIN,XMAX);
5322  gStyle->SetOptFit(0);
5323  gStyle->SetOptStat(0);
5324 
5325  hfit->Draw();
5326 
5327  //Standard Chi2 & Probability
5328  chi2 = fpois->GetChisquare();
5329  ndf = fpois->GetNDF()-1;
5330  plevel = TMath::Prob(chi2,ndf);
5331  normalization = fpois->GetParameter(0);
5332  enormalization = fpois->GetParError(0);
5333  mean = fpois->GetParameter(1);
5334  emean = fpois->GetParError(1);
5335 
5336  cout << "Mean : "<<mean<<endl;
5337  cout << "Norm : "<<normalization<<endl;
5338  cout << "ChiSquare : " << chi2 << endl;
5339  cout << "NDF : " << ndf << endl;
5340  cout << "Plevel : " << plevel << endl;
5341 
5342  TLegend *legend = new TLegend(0.597,0.660,0.961,0.921,NULL,"brNDC");
5343  legend->SetLineColor(1);
5344  legend->SetTextSize(0.033);
5345  legend->SetBorderSize(1);
5346  legend->SetLineStyle(1);
5347  legend->SetLineWidth(1);
5348  legend->SetFillColor(10);
5349  legend->SetFillStyle(1001);
5350 
5351  legend->SetTextSize(0.03);
5352  char entry[1024];
5353  sprintf(entry,"# lags = %d ",Nlags);
5354  legend->AddEntry("",entry,"");
5355  //sprintf(entry,"Nc = %d @ rho>%3.1f",NC,T_out);
5356  sprintf(entry,"Nc = %d",NC);
5357  legend->AddEntry("*",entry,"");
5358  sprintf(entry,"Total Live time = %d s",int(LT));
5359  legend->AddEntry("*",entry,"");
5360  sprintf(entry,"Mean = %.3f +/-%.3f FA/lag",mean,emean);
5361  legend->AddEntry("*",entry,"");
5362  sprintf(entry,"Chi2 (counts>%d) = %.3f",minCountsPerBin,ChiSquare);
5363  legend->AddEntry("*",entry,"");
5364  sprintf(entry,"NDF = %d",myNDF);
5365  legend->AddEntry("*",entry,"");
5366  sprintf(entry,"p-level = %.3f",myPLevel);
5367  legend->AddEntry("*",entry,"");
5368  legend->Draw();
5369 
5370  canvas->Update();
5371  char fname[1024];
5372  sprintf(fname,"%s/Lag_PoissonFit.png",odir.Data());
5373  canvas->Print(fname);
5374 
5375  delete h1;
5376  delete canvas;
5377 
5378  return;
5379 }
5380 
5381 //______________________________________________________________________________
5382 char*
5384 //
5385 // return a buffer with all environment name variables and their values
5386 //
5387 
5388  const int nCWB = 35;
5389 
5390  TString ecwb_name[nCWB] = {
5391  "SITE_CLUSTER",
5392  "_USE_ICC",
5393  "_USE_CPP11",
5394  "_USE_ROOT6",
5395  "_USE_PEGASUS",
5396  "_USE_HEALPIX",
5397  "_USE_LAL",
5398  "_USE_EBBH",
5399  "HOME_WAT",
5400  "HOME_WAT_FILTERS",
5401  "HOME_WAT_INSTALL",
5402  "HOME_FRLIB",
5403  "HOME_HEALPIX",
5404  "HOME_CFITSIO",
5405  "HOME_CVODE",
5406  "HOME_LAL",
5407  "LAL_INC",
5408  "LALINSPINJ_EXEC",
5409  "CWB_GWAT",
5410  "CWB_HISTORY",
5411  "CWB_MACROS",
5412  "CWB_ROOTLOGON_FILE",
5413  "CWB_PARAMETERS_FILE",
5414  "CWB_UPARAMETERS_FILE",
5415  "CWB_PPARAMETERS_FILE",
5416  "CWB_UPPARAMETERS_FILE",
5417  "CWB_NETS_FILE",
5418  "CWB_NETC_FILE",
5419  "CWB_HTML_INDEX",
5420  "CWB_HTML_HEADER",
5421  "CWB_HTML_BODY_PROD",
5422  "CWB_HTML_BODY_SIM",
5423  "ROOT_VERSION",
5424  "ROOTSYS",
5425  "LD_LIBRARY_PATH"
5426  };
5427 
5428  TString ecwb_value[nCWB];
5429  for(int i=0;i<nCWB;i++) {
5430  if(gSystem->Getenv(ecwb_name[i])==NULL) {
5431  ecwb_value[i]="";
5432  //cout << "Error : environment " << ecwb_name[i].Data() << " is not defined!!!" << endl;gSystem->Exit(1);
5433  } else {
5434  ecwb_value[i]=TString(gSystem->Getenv(ecwb_name[i]));
5435  }
5436 
5437  }
5438 
5439  int ecwb_csize=0;
5440  for(int i=0;i<nCWB;i++) ecwb_csize+=ecwb_name[i].Sizeof();
5441  for(int i=0;i<nCWB;i++) ecwb_csize+=ecwb_value[i].Sizeof();
5442 
5443  char* iBuffer = new char[2*ecwb_csize];
5444  bzero(iBuffer,(2*ecwb_csize)*sizeof(char));
5445 
5446  int len;
5447  int iLength = 0;
5448  for(int i=0;i<nCWB;i++) {
5449  len = ecwb_name[i].Sizeof();
5450  strncpy(iBuffer+iLength,ecwb_name[i].Data(),len);
5451  iLength += len-1;
5452  iBuffer[iLength]='=';
5453  iLength += 1;
5454  len = ecwb_value[i].Sizeof();
5455  strncpy(iBuffer+iLength,ecwb_value[i].Data(),len);
5456  iLength += len-1;
5457  iBuffer[iLength]=0x0a;
5458  iLength += 1;
5459  }
5460 
5461  return iBuffer;
5462 }
5463 
5464 //______________________________________________________________________________
5465 bool
5467 //
5468 // check if leaf is present in the input tree
5469 //
5470 // Input: itree - pointer to the input tree
5471 // leaf - name of the leaf
5472 //
5473 // Return true/false if leaf is present or not present
5474 //
5475 
5476  TBranch* branch;
5477  bool bleaf=false;
5478  TIter next(itree->GetListOfBranches());
5479  while ((branch=(TBranch*)next())) {
5480  if (TString(leaf.Data()).CompareTo(branch->GetName())==0) bleaf=true;
5481  }
5482  next.Reset();
5483  return bleaf;
5484 }
5485 
5486 
5487 //______________________________________________________________________________
5488 void
5489 CWB::Toolbox::makeSpectrum(wavearray<double>& psd, wavearray<double> x, double chuncklen, double scratchlen, bool oneside) {
5490 //
5491 // make PSD
5492 // PSD is computed averaging N=x.size()/chuncklen energy FFT with length=chuncklen
5493 //
5494 // Input: x - input data sample
5495 // chuncklen - FFT length
5496 // scratchlen - input data scratch length
5497 // oneside - true/false = one side/double side PSD
5498 //
5499 // Output : psd - array with the psd values
5500 //
5501 
5502  if(chuncklen<=0) {
5503  cout << "CWB::Toolbox::makeSpectrum : Error - chuncklen<=0" << endl;
5504  exit(1);
5505  }
5506 
5507  if(scratchlen<0) {
5508  cout << "CWB::Toolbox::makeSpectrum : Error - scratchlen<0" << endl;
5509  exit(1);
5510  }
5511 
5512  int scratchsize = scratchlen*x.rate();
5513  int blocksize = chuncklen*x.rate();
5514  scratchsize-=scratchsize%2; // make it even
5515  blocksize-=blocksize%2; // make it even
5516  double df=(double)x.rate()/(double)(blocksize);
5517 
5518  if(x.size()-2*scratchsize<blocksize) {
5519  cout << "CWB::Toolbox::makeSpectrum : Error - data size not enough to produce PSD" << endl;
5520  exit(1);
5521  }
5522 
5523  int loops = (x.size()-2*scratchsize)/blocksize;
5524  cout << "Rate: " << x.rate() << endl;
5525 
5526  double* window = new double[blocksize];
5527  blackmanharris(window, blocksize);
5528 
5529  psd.resize(blocksize/2);
5530  psd=0;
5531 
5532  wavearray<double> y(blocksize);
5533  y.rate(x.rate());
5534 
5535  for (int n=0;n<loops;n++) {
5536 
5537  int shift=n*blocksize;
5538  //cout << "shift: " << shift << endl;
5539  for (int i=0;i<blocksize;i++) y.data[i]=x.data[i+scratchsize+shift];
5540  for (int i=0;i<blocksize;i++) y.data[i]*=window[i];
5541 
5542  y.FFTW(1);
5543  for (int i=0;i<blocksize;i+=2) psd[i/2]+=pow(y.data[i],2)+pow(y.data[i+1],2);
5544  }
5545 
5546  for (int i=0;i<blocksize/2; i++) psd[i]=sqrt(psd[i]/(double)loops);
5547  if(oneside) {
5548  for (int i=0;i<blocksize/2; i++) psd[i]*=sqrt(2/df); // one side psd
5549  } else {
5550  for (int i=0;i<blocksize/2; i++) psd[i]*=sqrt(1/df); // double side psd
5551  }
5552 
5553  psd.start(0.);
5554  psd.stop(df*psd.size());
5555 
5556  delete [] window;
5557  return;
5558 }
5559 
5560 //______________________________________________________________________________
5561 void
5562 CWB::Toolbox::makeSpectrum(TString ofname, wavearray<double> x, double chuncklen, double scratchlen, bool oneside) {
5563 //
5564 // make PSD
5565 // PSD is computed averaging N=x.size()/chuncklen energy FFT with length=chuncklen
5566 // the psd valued are saved to ofname file
5567 //
5568 // Input: ofname - output psd file name (format : freq psd)
5569 // x - input data sample
5570 // chuncklen - FFT length
5571 // scratchlen - input data scratch length
5572 // oneside - true/false = one side/double side PSD
5573 //
5574 
5576  makeSpectrum(psd, x, chuncklen, scratchlen, oneside);
5577  double df = (psd.stop()-psd.start())/psd.size();
5578 
5579  ofstream out;
5580  out.open(ofname.Data(),ios::out);
5581  if (!out.good()) {cout << "CWB::Toolbox::makeSpectrum - Error : Opening File : " << ofname.Data() << endl;gSystem->Exit(1);}
5582  for (int i=0;i<psd.size();i++) {
5583  double freq = i*df+psd.start();
5584  out << freq << " " << psd[i] << endl;
5585  }
5586  out.close();
5587 
5588  return;
5589 }
5590 
5591 //______________________________________________________________________________
5592 void
5594 //
5595 // get colored gaussian noise
5596 //
5597 // Input: fName - input file with PSD values
5598 // format : freq(Hz) singleside-PSD(1/sqrt(Hz))
5599 // seed - base seed for random number generator
5600 // if seed<0 input wavearray is used instead of gaussian random noise
5601 // we pad freq<fabs(seed) with value A = amplitude at freq=fabs(seed)
5602 // 'run' parameter is used as a multiplicative factor for A -> A=A*run
5603 // run - auxiliary seed for random number generator
5604 // SEED = seed+run
5605 // u.size() - input wavearray size
5606 //
5607 // Output: u - wavearray filled with colored gaussian noise @ rate=16384 Hz
5608 //
5609 
5610  #define OTIME_LENGHT 616 // minimum time lenght simulation
5611  #define TIME_SCRATCH 184 // time scratch used by the FFT
5612  #define LOW_CUT_FREQ 2.0 // output noise is 0 for freq<LOW_CUT_FREQ
5613  #define FREQ_RES 0.125 // input PSD is resampled with dF=FREQ_RES
5614  #define SRATE 16384. // output noise is produced @ rate=SRATE
5615 
5616  TRandom3 random;
5617 
5618  // input lenght must be <= OTIME_LENGHT
5619  double ilenght = u.size()/u.rate();
5620  int otime_lenght = (ilenght<OTIME_LENGHT) ? OTIME_LENGHT : int(ilenght);
5621 
5622  // read PSD
5623  ifstream in;
5624  in.open(fName.Data(),ios::in);
5625  if (!in.good()) {
5626  cout << "CWB::Toolbox::getSimNoise - Error Opening File : " << fName.Data() << endl;
5627  gSystem->Exit(1);
5628  }
5629 
5630  int size=0;
5631  char str[1024];
5632  while(true) {
5633  in.getline(str,1024);
5634  if (!in.good()) break;
5635  if(str[0] != '#') size++;
5636  }
5637  //cout << "size " << size << endl;
5638  in.clear(ios::goodbit);
5639  in.seekg(0, ios::beg);
5640 
5641  wavearray<double> ifr(size);
5642  wavearray<double> ish(size);
5643 
5644  int cnt=0;
5645  while (1) {
5646  in >> ifr.data[cnt] >> ish.data[cnt];
5647  if (!in.good()) break;
5648  if(ish.data[cnt]<=0)
5649  {cout << "CWB::Toolbox::getSimNoise - input sensitivity file : " << fName.Data()
5650  << " contains zero at frequency : " << ifr.data[cnt] << " Hz " << endl;gSystem->Exit(1);}
5651  cnt++;
5652  }
5653  in.close();
5654 
5655  // convert frequency sample
5656  size=int((SRATE/2)/FREQ_RES);
5657  wavearray<double> ofr(size);
5658  wavearray<double> osh(size);
5659  for(int i=0;i<(int)ofr.size();i++) ofr[i]=i*FREQ_RES;
5660  convertSampleRate(ifr,ish,ofr,osh);
5661  ifr.resize(0);
5662  ish.resize(0);
5663 
5664  osh*=1./sqrt(osh.size()/(SRATE/2)); // normalization
5665 
5666  int time_factor = (otime_lenght+TIME_SCRATCH)/(2*osh.size()/double(SRATE));
5667 
5668  // change output time lenght
5669  wavearray<double> y; // temporary time series
5670  y.resize(2*osh.size()*time_factor);
5671  y=0.;
5672  for (int i=0;i<(int)osh.size();i++) {
5673  for (int j=0;j<time_factor;j++) {
5674  y.data[i*time_factor+j]=osh.data[i];
5675  }
5676  }
5677  y.rate(SRATE);
5678  y*=1./sqrt(time_factor);
5679  ofr.resize(0);
5680  osh.resize(0);
5681 
5682  wavearray<double> z; // temporary time series
5683  z.rate(y.rate());
5684  z.resize(y.size());
5685  double df=z.rate()/z.size();
5686  for (int i=0;i<(int)z.size()/2;i++) {
5687  double am = y.data[i];
5688  double frequency = df*i;
5689  if (frequency>=LOW_CUT_FREQ) {
5690  z.data[2*i]=am; // (A)
5691  z.data[2*i+1]=am; // (B)
5692  } else {
5693  z.data[2*i]=0;
5694  z.data[2*i+1]=0;
5695  }
5696  }
5697  z*=1./sqrt(2.); // because of (A & B)
5698  y.resize(0);
5699 
5700  int scratch=z.size()/z.rate()-otime_lenght;
5701  cout << "CWB::Toolbox::getSimNoise - scratch : " << scratch << " osize : " << z.size()/z.rate() << endl;
5702  if (scratch<0) {cout << "Error : bad data length -> " << z.size()/z.rate() << endl;gSystem->Exit(1);}
5703 
5704  wavearray<double> x; // temporary time series
5705  x.rate(z.rate());
5706  x.resize(z.size());
5707 
5708  if(seed>=0) {
5709  // generate random gaussian noise -> FFT
5710  random.SetSeed(seed+run);
5711  for (int i=0;i<x.size()-scratch*x.rate();i++) x.data[i]=random.Gaus(0,1);
5712  random.SetSeed(seed+run+1); // to be syncronized with the next frame
5713  for (int i=x.size()-scratch*x.rate();i<(int)x.size();i++) x.data[i]=random.Gaus(0,1);
5714  } else {
5715  // input wavearray is used instead of gaussian random noise
5716  if(u.size()>x.size()) {
5717  cout << "CWB::Toolbox::getSimNoise - Error : whitened data size " << u.size()
5718  << " is greater than temporary datat size " << x.size() << endl;
5719  gSystem->Exit(1);
5720  }
5721  x=0;
5722  int iS = (x.size()-u.size())/2;
5723  for(int i=0;i<u.size();i++) x[i+iS]=u[i];
5724  }
5725  x.FFTW(1);
5726  x*=sqrt(x.size()); // Average x^2 = 1
5727 
5728  // gaussian white noise -> gaussian coloured noise
5729  u=z;
5730  u*=x;
5731  x.resize(0);
5732  z.resize(0);
5733 
5734  if(seed<0) {
5735  // cwb whitened real noise is 0 for freq<16Hz
5736  // we pad freq<fabs(seed) with value = amplitude at freq=fabs(seed)*run
5737  // run parameter is used as a multiplicative factor
5738 
5739  double df=u.rate()/u.size();
5740  int ipad = fabs(seed)/df;
5741  if(ipad>u.size()/2-1) ipad=u.size()/2-1;
5742  double fpad = sqrt(pow(u.data[2*ipad],2)+pow(u.data[2*ipad+1],2))*run;
5743  for(int i=0;i<(int)u.size()/2;i++) {
5744  double frequency = df*i;
5745  if(frequency<=fabs(seed)) {
5746  u.data[2*i] = gRandom->Gaus(0,fpad);
5747  u.data[2*i+1] = gRandom->Gaus(0,fpad);
5748  }
5749  }
5750  }
5751 
5752  // change output frequency SRATE
5753  df=u.rate()/u.size();
5754  int osize = SRATE/df;
5755  u.resize(osize);
5756  u.rate(SRATE);
5757 
5758  u.FFTW(-1);
5759 
5760  scratch=(osize-otime_lenght*u.rate())/2;
5761  for (int i=0;i<otime_lenght*u.rate();i++) u.data[i]=u.data[scratch+i];
5762 
5763  u.resize(otime_lenght*u.rate());
5764 
5765  return;
5766 }
5767 
5768 //______________________________________________________________________________
5771 //
5772 // read PSD from file
5773 //
5774 // input : fName - input file name [freq(Hz) psd]
5775 // fWidth - bandwidth (Hz)
5776 // dFreq - frequency resolution (Hz)
5777 //
5778 // return PSD sampled at df=dFreq up to fWidth Hz
5779 //
5780 
5781  ifstream in;
5782  in.open(fName.Data(),ios::in);
5783  if (!in.good())
5784  {cout << "CWB::Toolbox::ReadDetectorPSD - Error Opening File : "
5785  << fName.Data() << endl;gSystem->Exit(1);}
5786 
5787  cout << "CWB::Toolbox::ReadDetectorPSD - Read File : " << fName.Data() << endl;
5788 
5789  int size=0;
5790  char str[1024];
5791  while(true) {
5792  in.getline(str,1024);
5793  if (!in.good()) break;
5794  if(str[0] != '#') size++;
5795  }
5796  //cout << "size " << size << endl;
5797  in.clear(ios::goodbit);
5798  in.seekg(0, ios::beg);
5799 
5800  wavearray<double> ifr(size);
5801  wavearray<double> ish(size);
5802 
5803  int cnt=0;
5804  while (1) {
5805  in >> ifr.data[cnt] >> ish.data[cnt];
5806  if (!in.good()) break;
5807  if(ish.data[cnt]<=0)
5808  {cout << "CWB::Toolbox::ReadDetectorPSD - input sensitivity file : " << fName.Data()
5809  << " contains zero at frequency : " << ifr.data[cnt] << " Hz " << endl;gSystem->Exit(1);}
5810  cnt++;
5811  }
5812  in.close();
5813 
5814  // convert frequency sample
5815  size=int(fWidth/dFreq);
5816  wavearray<double> ofr(size);
5817  wavearray<double> osh(size);
5818 
5819  for(int i=0;i<(int)ofr.size();i++) ofr[i]=i*dFreq;
5820  convertSampleRate(ifr,ish,ofr,osh);
5821 
5822  osh.rate(size*dFreq);
5823 
5824  return osh;
5825 }
5826 
5827 //______________________________________________________________________________
5828 void
5830 //
5831 // convert sample rate data
5832 // data are resampled using a linear approximation
5833 //
5834 // input : iw - input samples wavearray, sampled @ iw.rate()
5835 // output : ow - output samples wavearray, sampled @ ow.rate()
5836 // ow.rate() is an input parameter
5837 //
5838 
5839  if(iw.size()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - input size <=0" << endl;gSystem->Exit(1);}
5840  if(iw.rate()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - input rate <=0" << endl;gSystem->Exit(1);}
5841  if(ow.size()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - output size <=0" << endl;gSystem->Exit(1);}
5842  if(ow.rate()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - output rate <=0" << endl;gSystem->Exit(1);}
5843 
5844  // initialize input array
5845  wavearray<double> ix(iw.size());
5846  double idx=1./iw.rate();
5847  for (int i=0;i<(int)ix.size();i++) ix[i]=i*idx;
5848 
5849  // initialize output array
5850  wavearray<double> ox(ow.size());
5851  double odx=1./ow.rate();
5852  for (int i=0;i<(int)ox.size();i++) ox[i]=i*odx;
5853 
5854  // smooth amplitudes
5855  TGraph* grin = new TGraph(ix.size(), ix.data, iw.data);
5856  TGraphSmooth* gs = new TGraphSmooth("normal");
5857  TGraph* grout = gs->Approx(grin,"linear", ox.size(), ox.data);
5858 
5859  // save amplitudes
5860  for (int i=0;i<(int)ox.size();i++) {
5861  grout->GetPoint(i,ox[i],ow[i]);
5862 // cout << i << " " << ox[i] << " " << ow[i] << endl;
5863  }
5864 
5865  delete grin;
5866  delete gs;
5867 
5868  return;
5869 }
5870 
5871 //______________________________________________________________________________
5872 void
5874 //
5875 // convert sample rate data
5876 //
5877 // data are resampled using a linear approximation
5878 // ox are used to resample input amplitudes iy sampled at times ix
5879 //
5880 // input : ix - input times wavearray
5881 // iy - input amplitudes wavearray
5882 // ox - output times wavearray
5883 // output : oy - output amplitutes wavearray
5884 //
5885 
5886  if(ix.size()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - input size <=0" << endl;gSystem->Exit(1);}
5887  if(ox.size()<=0) {cout << "CWB::Toolbox::convertSampleRate : Error - output size <=0" << endl;gSystem->Exit(1);}
5888 
5889  // smooth amplitudes
5890  TGraph* grin = new TGraph(ix.size(), ix.data, iy.data);
5891  TGraphSmooth* gs = new TGraphSmooth("normal");
5892  TGraph* grout = gs->Approx(grin,"linear", ox.size(), ox.data);
5893 
5894  // save amplitudes
5895  for (int i=0;i<(int)ox.size();i++) {
5896  grout->GetPoint(i,ox[i],oy[i]);
5897  }
5898 
5899  delete grin;
5900  delete gs;
5901 
5902  return;
5903 }
5904 
5905 //______________________________________________________________________________
5906 int
5908 //
5909 // Create Multiplicity plot
5910 //
5911 // Input: ifName : input file name
5912 // idir : input dir
5913 // odir : output dir
5914 // nIFO : detector number
5915 // Tgap : maximum time gap to associate two events as same event
5916 //
5917 
5918  gRandom->SetSeed(1);
5919 
5920  CWB::History* history = NULL;
5921  bool bhistory=true;
5922 
5923  TString efname = odir+"/"+ifName;
5924  efname.ReplaceAll(".root",".N.root");
5925  bool overwrite = checkFile(efname,true);
5926  if(!overwrite) gSystem->Exit(0);
5927 
5928  TString ifname = idir+"/"+ifName;
5929  TString ofname = odir+"/"+ifName;
5930  ofname.ReplaceAll(".root",".root.tmp.1");
5931 
5932  for(int n=0;n<nIFO;n++) {
5933 
5934  // -------------------------------------------------------------------------
5935  // open root file
5936  // -------------------------------------------------------------------------
5937  cout<<"Opening BKG File : " << ifname.Data() << endl;
5938  TFile ifile(ifname);
5939  if (ifile.IsZombie()) {
5940  cout << "CWB::Toolbox::setMultiplicity - Error opening file " << ifname.Data() << endl;
5941  gSystem->Exit(1);
5942  }
5943  TTree *itree = (TTree*)ifile.Get("waveburst");
5944  if (itree==NULL) {
5945  cout << "CWB::Toolbox::setMultiplicity - tree waveburst is not present in file " << ifname.Data() << endl;
5946  gSystem->Exit(1);
5947  }
5948  Int_t tsize = (Int_t)itree->GetEntries();
5949  cout << "tree size : " << tsize << endl;
5950  if (tsize==0) {
5951  cout << "CWB::Toolbox::setMultiplicity - tree waveburst is empty in file " << ifname.Data() << endl;
5952  gSystem->Exit(1);
5953  }
5954  itree->SetEstimate(tsize);
5955  char selection[1024];sprintf(selection,"time[%d]:Entry$",n);
5956  char cut[1024]="";
5957 // if(n>0) sprintf(cut,"Mm[%d]==1",n-1);
5958  int isize = itree->Draw(selection,cut,"goff",tsize);
5959  double* time = itree->GetV1();
5960  double* entry = itree->GetV2();
5961  // sort list
5962  Int_t *id = new Int_t[isize];
5963  TMath::Sort((int)isize,time,id,false);
5964 
5965  if(bhistory) {
5966  history=(CWB::History*)ifile.Get("CWB::History");
5967  if(history==NULL) history=new CWB::History();
5968  bhistory=false;
5969  }
5970 
5971  // -------------------------------------------------------------------------
5972  // add new leave to itree
5973  // -------------------------------------------------------------------------
5974 
5975  TBranch* branch;
5976  if(n==0) {
5977  TIter next(itree->GetListOfBranches());
5978  while((branch=(TBranch*)next())) {
5979  if(TString("Msize").CompareTo(branch->GetName())==0) {
5980  cout << "Multiplicity already applied" << endl;
5981  char answer[1024];
5982  strcpy(answer,"");
5983  do{
5984  cout << "Do you want to overwrite the previous values ? (y/n) ";
5985  cin >> answer;
5986  } while((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
5987  if(strcmp(answer,"n")==0) {
5988  gSystem->Exit(-1);
5989  }
5990  }
5991  }
5992  next.Reset();
5993  }
5994 
5995  // -------------------------------------------------------------------------
5996  // create temporary root file
5997  // -------------------------------------------------------------------------
5998 
5999  int* Msize = new int[nIFO];
6000  int* Mid = new int[nIFO];
6001  UChar_t* Mm = new UChar_t[nIFO];
6002 
6003  char cMsize[32];sprintf(cMsize,"Msize[%1d]/I",nIFO);
6004  char cMid[32];sprintf(cMid,"Mid[%1d]/I",nIFO);
6005  char cMm[32];sprintf(cMm,"Mm[%1d]/b",nIFO);
6006 
6007  TFile* ftmp = new TFile(ofname,"RECREATE");
6008  if (ftmp->IsZombie()) {
6009  cout << "CWB::Toolbox::setMultiplicity - Error opening file " << ofname.Data() << endl;
6010  gSystem->Exit(1);
6011  }
6012  TTree *trtmp = (TTree*)itree->CloneTree(0);
6013  trtmp->SetMaxTreeSize(MAX_TREE_SIZE);
6014  if (n>0) {
6015  trtmp->SetBranchAddress("Msize",Msize);
6016  trtmp->SetBranchAddress("Mid",Mid);
6017  trtmp->SetBranchAddress("Mm",Mm);
6018  } else {
6019  trtmp->Branch("Msize",Msize,cMsize);
6020  trtmp->Branch("Mid",Mid,cMid);
6021  trtmp->Branch("Mm",Mm,cMm);
6022  }
6023  trtmp->Write();
6024 
6025  // -------------------------------------------------------------------------
6026  // insert flag into event tree
6027  // we add a dummy flag entry to the end of onoff array to avoid to discart good events when
6028  // no flag entries are present
6029  // -------------------------------------------------------------------------
6030  cout << "Start applying flag to time["<<n<<"]..." << endl;
6031 
6032  ftmp->cd();
6033 
6034  int pc = 0;
6035  int ipc = (int)((double)(isize+1)/10.);
6036  int *oMsize = new int[tsize];
6037  int *oMid = new int[tsize];
6038  bool *oMm = new bool[tsize];
6039  for(int j=0;j<tsize;j++) {
6040  oMsize[j]=1;
6041  oMid[j]=1;
6042  oMm[j]=false;
6043  }
6044  vector<int> mlist;
6045  int xMsize=1;
6046  int xMid=1;
6047  int ientry=int(entry[id[0]]);
6048  oMsize[ientry]=xMsize; oMid[ientry]=xMid; oMm[ientry]=false;
6049  mlist.push_back(id[0]);
6050  for (int j=1;j<isize;j++) {
6051  ientry=int(entry[id[j]]);
6052  oMsize[ientry]=1;
6053  oMm[ientry]=false;
6054  if((time[id[j]]-time[id[j-1]])<Tgap) {
6055  xMsize++;
6056  mlist.push_back(id[j]);
6057  } else {
6058  int xMm=mlist[int(gRandom->Uniform(0,mlist.size()))]; // select random multiplicity event
6059  oMm[int(entry[xMm])]=true; // set master multiplicity event
6060  for(int m=0;m<(int)mlist.size();m++) oMsize[int(entry[mlist[m]])]=xMsize;
6061  mlist.clear();
6062  xMsize=1;xMid++;
6063  mlist.push_back(id[j]);
6064  }
6065  oMid[ientry]=xMid;
6066 
6067  if(ipc!=0) if(j%ipc==0) {cout << pc;if (pc<100) cout << " - ";pc+=10;cout.flush();}
6068  }
6069  cout << pc << endl << endl;
6070 
6071  int* iMsize = new int[nIFO];
6072  int* iMid = new int[nIFO];
6073  UChar_t* iMm = new UChar_t[nIFO];
6074  if(n>0) {
6075  itree->SetBranchAddress("Msize",iMsize);
6076  itree->SetBranchAddress("Mid",iMid);
6077  itree->SetBranchAddress("Mm",iMm);
6078  }
6079  int nevt_master=0;
6080  for (int j=0;j<tsize;j++) {
6081  itree->GetEntry(j);
6082  for(int k=0;k<nIFO;k++) {
6083  Msize[k]=iMsize[k];
6084  Mid[k]=iMid[k];
6085  Mm[k]=iMm[k];
6086  }
6087  Msize[n]=oMsize[j];
6088  Mid[n]=oMid[j];
6089  Mm[n]=oMm[j];
6090  if(Mm[n]) nevt_master++;
6091  trtmp->Fill();
6092 /*
6093 if(n==(nIFO-1)) {
6094  if(Mm[n]) trtmp->Fill();
6095 } else {
6096  trtmp->Fill();
6097 }
6098 */
6099  }
6100  trtmp->Write();
6101  delete [] id;
6102  delete [] oMsize;
6103  delete [] oMid;
6104  delete [] oMm;
6105  delete [] Msize;
6106  delete [] Mid;
6107  delete [] Mm;
6108  delete [] iMsize;
6109  delete [] iMid;
6110  delete [] iMm;
6111 
6112  cout << "Writing new ofile "<< ofname.Data() << endl;
6113  Int_t osize = (Int_t)trtmp->GetEntries();
6114  cout << "osize : " << osize << endl;
6115  cout << "Master events: " << nevt_master << " Percentage: "<< 100.*double(nevt_master)/double(tsize) << endl;
6116 
6117  if((history!=NULL)&&(n==nIFO-1)) {
6118  // update history
6119  history->SetHistoryModify(true);
6120  if(!history->StageAlreadyPresent(CCAST("MULTIPLICITY"))) history->AddStage(CCAST("MULTIPLICITY"));
6121 
6122  if(!history->TypeAllowed(CCAST("WORKDIR"))) history->AddType(CCAST("WORKDIR"));
6123  char work_dir[512]="";
6124  sprintf(work_dir,"%s",gSystem->WorkingDirectory());
6125  history->AddHistory(CCAST("MULTIPLICITY"), CCAST("WORKDIR"), work_dir);
6126  char hTgap[1024];sprintf(hTgap,"Tgap = %f",Tgap);
6127  history->AddLog(CCAST("MULTIPLICITY"), CCAST(hTgap));
6128  }
6129 
6130  ftmp->Close();
6131  ifile.Close();
6132  cout << endl;
6133 
6134  ifname=ofname;
6135  if(n%2) ofname.ReplaceAll(".root.tmp.2",".root.tmp.1");
6136  else ofname.ReplaceAll(".root.tmp.1",".root.tmp.2");
6137  }
6138 
6139  TString rfname = ofname;
6140  ofname = efname;
6141 
6142  // define output live merge file
6143  TString lfname = ifName;
6144  lfname.ReplaceAll(".root",".N.root");
6145  lfname.ReplaceAll("wave_","live_");
6146  TString ilfname = idir+"/"+ifName;
6147  ilfname.ReplaceAll("wave_","live_");
6148 
6149  // define output list merge file
6150  TString mfname = ifName;
6151  mfname.ReplaceAll(".root",".N.root");
6152  mfname.ReplaceAll("wave_","merge_");
6153  mfname.ReplaceAll(".root",".lst");
6154  TString imfname = idir+"/"+ifName;
6155  imfname.ReplaceAll("wave_","merge_");
6156  imfname.ReplaceAll(".root",".lst");
6157 
6158  cout << " rfile " << rfname.Data() << endl;
6159  cout << " ifile " << ifname.Data() << endl;
6160  cout << " ofile " << ofname.Data() << endl;
6161  cout << " lfile " << lfname.Data() << endl;
6162  cout << " mfile " << mfname.Data() << endl;
6163 
6164  char cmd[1024];
6165  sprintf(cmd,"mv %s %s",ifname.Data(),ofname.Data());
6166  cout << cmd << endl;
6167  gSystem->Exec(cmd);
6168  // create symbolic link to live root file
6169  sprintf(cmd,"cd %s;ln -s ../%s %s",odir.Data(),ilfname.Data(),lfname.Data());
6170  cout << cmd << endl;
6171  gSystem->Exec(cmd);
6172  // create symbolic link to list merge file
6173  sprintf(cmd,"cd %s;ln -s ../%s %s",odir.Data(),imfname.Data(),mfname.Data());
6174  cout << cmd << endl;
6175  gSystem->Exec(cmd);
6176  sprintf(cmd,"rm %s",rfname.Data());
6177  cout << cmd << endl;
6178  gSystem->Exec(cmd);
6179 
6180  // write history to merge+veto file
6181  if(history!=NULL) {
6182  TFile fhist(ofname,"UPDATE");
6183  history->Write();
6184  fhist.Close();
6185  delete history;
6186  }
6187 
6188  return 0;
6189 }
6190 
6191 //______________________________________________________________________________
6193 CWB::Toolbox::getRate(double rho, double Tgap, int nIFO, TChain& wav, wavearray<int>& Wsel,
6195 //
6196 // Calculate FAR rate at a certain rho treshold
6197 //
6198 // Input rho : rho threshold
6199 // Tgap :
6200 // nIFO : detector number
6201 // wav : input tree
6202 // Wsel :
6203 // Wlag : wavearray containing progressive lag number
6204 // Wslag : wavearray containing progressive slag number
6205 // Tlag : wavearray containing live time
6206 //
6207 
6208 // cout << "rhoThr : " << rho << endl;
6209  gRandom->SetSeed(1);
6210  netevent W(&wav,nIFO);
6211  // slag is not defined in the wat-5.4.0
6212  float* iWslag = new float[nIFO+1];
6213  W.fChain->SetBranchAddress("slag",iWslag);
6214 
6215  int size = Wlag[nIFO].size();
6216 
6217  double liveTot=0;
6218  for(int i=0;i<(int)Tlag.size();i++) liveTot+=Tlag[i];
6219 // cout << "liveTot : " << liveTot << endl;
6220 
6221  // compute the number of indipendent slags,lags
6222  int n;
6223  bool save;
6225  wavearray<int> lag;
6226  for(int i=0; i<size; i++) {
6227  int islag=int(Wslag[nIFO].data[i]);
6228  save = true;
6229  n = slag.size();
6230  for(int j=0; j<n; j++) {
6231  if(slag[j]==islag) {save=false; j=n;}
6232  }
6233  if(save) slag.append(islag);
6234 
6235  int ilag=int(Wlag[nIFO].data[i]);
6236  save = true;
6237  n = lag.size();
6238  for(int j=0; j<n; j++) {
6239  if(lag[j]==ilag) {save=false; j=n;}
6240  }
6241  if(save) lag.append(ilag);
6242  }
6243 
6244 // for(int i=0;i<(int)lag.size();i++) cout << i << " LAG " << lag[i] << endl;
6245 // for(int i=0;i<(int)slag.size();i++) cout << i << " SLAG " << slag[i] << endl;
6246 
6247  // construct the array to associate slag,lag to index
6248  int* slag2id = new int[slag.max()+1];
6249  int* lag2id = new int[lag.max()+1];
6250  for(int i=0;i<(int)slag.size();i++) slag2id[slag[i]]=i;
6251  for(int i=0;i<(int)lag.size();i++) lag2id[lag[i]]=i;
6252 
6253  // construct the matrix to associate lag,slag to live times
6254  int** lag2live = (int**)malloc((slag.size())*sizeof(int*));
6255  for (int i=0;i<(int)slag.size();i++) lag2live[i] = new int[lag.size()];
6256 
6257  for(int i=0; i<size; i++) {
6258  int islag=int(Wslag[nIFO].data[i]);
6259  int ilag=int(Wlag[nIFO].data[i]);
6260  lag2live[slag2id[islag]][lag2id[ilag]]=Tlag[i];
6261  }
6262 
6263  // list of rejected lags
6264  bool** lagRej = (bool**)malloc((slag.size())*sizeof(bool*));
6265  for (int i=0;i<(int)slag.size();i++) lagRej[i] = new bool[lag.size()];
6266  for(int i=0;i<(int)slag.size();i++) for(int j=0;j<(int)lag.size();j++) lagRej[i][j]=false;
6267 
6268  int ntrg = wav.GetEntries();
6269 
6270  int *id = new int[ntrg];
6271  int *wslag = new int[ntrg];
6272  int *wlag = new int[ntrg];
6273  int *entry = new int[ntrg];
6274  double** time = (double**)malloc(nIFO*sizeof(double*));
6275  for (int i=0;i<nIFO;i++) time[i] = new double[ntrg];
6276  bool *trgMaster = new bool[ntrg];
6277  for (int i=0;i<ntrg;i++) trgMaster[i] = false;
6278  bool *wrej = new bool[ntrg];
6279  for (int i=0;i<ntrg;i++) wrej[i] = false;
6280 
6281  int isel=0;
6282  for(int i=0; i<ntrg; i++) {
6283  if(!Wsel[i]) continue;
6284  W.GetEntry(i);
6285  if(W.rho[1]>rho) {
6286  bool skip=false;
6287  for(int j=0;j<nIFO;j++) if(!TMath::IsNaN(W.time[j])) time[j][isel]=W.time[j]; else skip=true;
6288  if(skip) continue;
6289  //wslag[isel]=W.slag[nIFO];
6290  wslag[isel]=iWslag[nIFO];
6291  wlag[isel]=W.lag[nIFO];
6292  entry[isel]=i;
6293  isel++;
6294  }
6295  }
6296 // cout << "selTrg : " << isel << endl;
6297 
6298  int rejTrg=0;
6299  double rejLive=0;
6300  vector<double> livlist;
6301  vector<int> idlist;
6302  for(int i=0;i<nIFO;i++) {
6303  if(isel==0) continue;
6304  TMath::Sort((int)isel,time[i],id,false);
6305  int islag = slag2id[wslag[id[0]]];
6306  int ilag = lag2id[wlag[id[0]]];
6307  double live = lag2live[islag][ilag];
6308  livlist.push_back(live);
6309  idlist.push_back(id[0]);
6310  for (int j=1;j<isel;j++) {
6311  if(wrej[id[j]]) continue; // skip trigger if already rejected
6312  if((time[i][id[j]]-time[i][id[j-1]])>Tgap) {
6313 /*
6314  // find minimum live time in the multiplicity list
6315  double liveMin=kMaxInt; int M=0;
6316  for(int m=0;m<(int)livlist.size();m++) {
6317  if(liveMin>livlist[m]) {liveMin=livlist[m];M=m;}
6318  }
6319 */
6320  int M=int(gRandom->Uniform(0,(int)idlist.size())); // select random multiplicity event
6321  trgMaster[idlist[M]]=true;
6322  for(int m=0;m<(int)idlist.size();m++) {
6323  // select only the trigger in the multiplicity list with minimum live time
6324  // check if live time has been already rejected
6325  int islag = slag2id[wslag[idlist[m]]];
6326  int ilag = lag2id[wlag[idlist[m]]];
6327  if(!trgMaster[idlist[m]]) {
6328  if(!lagRej[islag][ilag]) rejLive+=livlist[m];
6329  lagRej[islag][ilag]=true;
6330  wrej[idlist[m]]=true; // Tag the rejected trigger
6331  Wsel[entry[idlist[m]]]=2; // Tag the rejected trigger with 2
6332  rejTrg++;
6333  }
6334  }
6335  livlist.clear();
6336  idlist.clear();
6337  }
6338  int islag = slag2id[wslag[id[j]]];
6339  int ilag = lag2id[wlag[id[j]]];
6340  live = lag2live[islag][ilag];
6341  livlist.push_back(live);
6342  idlist.push_back(id[j]);
6343  }
6344 /*
6345  // find minimum live time in the multiplicity list
6346  double liveMin=kMaxInt; int M=0;
6347  for(int m=0;m<(int)livlist.size();m++) {
6348  if(liveMin>livlist[m]) {liveMin=livlist[m];M=m;}
6349  }
6350 */
6351  int M=int(gRandom->Uniform(0,(int)idlist.size())); // select random multiplicity event
6352  trgMaster[idlist[M]]=true;
6353  for(int m=0;m<(int)idlist.size();m++) {
6354  // select only the trigger in the multiplicity list with minimum live time
6355  // check if live time has been already rejected
6356  int islag = slag2id[wslag[idlist[m]]];
6357  int ilag = lag2id[wlag[idlist[m]]];
6358  if(!trgMaster[idlist[m]]) {
6359  if(!lagRej[islag][ilag]) rejLive+=livlist[m];
6360  lagRej[islag][ilag]=true;
6361  wrej[idlist[m]]=true; // Tag the rejected trigger
6362  Wsel[entry[idlist[m]]]=2; // Tag the rejected trigger with 2
6363  rejTrg++;
6364  }
6365  }
6366  livlist.clear();
6367  idlist.clear();
6368  }
6369 // cout << "rejTrg : " << rejTrg << endl;
6370 
6371  // reject triggers which belong to the rejected lags
6372  for (int j=0;j<isel;j++) {
6373  if(wrej[j]) continue; // only the triggers not already rejected
6374  int islag = slag2id[wslag[j]];
6375  int ilag = lag2id[wlag[j]];
6376  if(lagRej[islag][ilag]) {
6377  Wsel[entry[j]]=2; // Tag the rejected trigger with 2
6378  rejTrg++;
6379  }
6380  }
6381 // cout << "rejTrg final : " << rejTrg << endl;
6382 // cout << "rejLive : " << rejLive << endl;
6383 
6384 /*
6385 int xrejTrg=0;
6386 for (int j=0;j<isel;j++) {
6387  int islag = slag2id[wslag[j]];
6388  int ilag = lag2id[wlag[j]];
6389  if(lagRej[islag][ilag]) {
6390  xrejTrg++;
6391  }
6392 }
6393 cout << "xrejTrg final: " << xrejTrg << endl;
6394 int nlagRej=0;
6395 for(int i=0;i<(int)slag.size();i++) for(int j=0;j<(int)lag.size();j++) if(lagRej[i][j]) nlagRej++;
6396 cout << "nlagRej final: " << nlagRej << endl;
6397 double xlivRej=0;
6398 for(int i=0;i<(int)slag.size();i++) for(int j=0;j<(int)lag.size();j++) if(lagRej[i][j]) xlivRej+=lag2live[i][j];
6399 cout << "xlivRej final: " << xlivRej << endl;
6400 */
6401 /*
6402  for(int j=0; j<size; j++){
6403  printf("slag=%i\t lag=%i\t ",int(Wslag[nIFO].data[j]),int(Wlag[nIFO].data[j]));
6404  //for (int jj=0; jj<nIFO; jj++) printf("%d:slag=%5.2f\tlag=%5.2f\t",jj,Wslag[jj].data[j],Wlag[jj].data[j]);
6405  printf("live=%9.2f\n",Tlag.data[j]);
6406  }
6407 */
6408 
6409  for (int i=0;i<nIFO;i++) delete [] time[i];
6410  delete [] id;
6411  delete [] wslag;
6412  delete [] wlag;
6413  for (int i=0;i<(int)slag.size();i++) delete [] lag2live[i];
6414  delete [] slag2id;
6415  delete [] lag2id;
6416  delete [] iWslag;
6417  delete [] trgMaster;
6418  delete [] wrej;
6419  delete [] entry;
6420 
6421  double rate = (isel-rejTrg)/(liveTot-rejLive);
6422  double erate = sqrt(isel-rejTrg)/(liveTot-rejLive);
6423  return wavecomplex(rate,erate);
6424 }
6425 
6426 
6427 //_______________________________________________________________________________________
6428 int
6429 CWB::Toolbox::GetStepFunction(TString fName, vector<double>& x, vector<double>& y,
6430  vector<double>& ex, vector<double>& ey) {
6431 
6432  GetStepFunction("", fName, 0, x, y, ex, ey);
6433 
6434  // remove the first and the last elements added by the GetStepFunction method
6435  int size=x.size();
6436 
6437  // erase the first element (adde)
6438  x.erase(x.begin()+size-1);
6439  y.erase(y.begin()+size-1);
6440  ex.erase(ex.begin()+size-1);
6441  ey.erase(ey.begin()+size-1);
6442 
6443  // erase the last element
6444  x.erase(x.begin());
6445  y.erase(y.begin());
6446  ex.erase(ex.begin());
6447  ey.erase(ey.begin());
6448 
6449  return x.size();
6450 }
6451 
6452 //_______________________________________________________________________________________
6453 double
6455  vector<double>& x, vector<double>& y, vector<double>& ex, vector<double>& ey) {
6456 //
6457 // read x,y coodinated from fName
6458 // y[x] is approximated with a step function
6459 //
6460 // V : input value
6461 //
6462 // fName : ascii file with the list of x,y coordinateds
6463 // x1 y1
6464 // x2 y2
6465 // .....
6466 // xN yN
6467 //
6468 // option : "xmin" - return minimum x value
6469 // "xmax" - return maximim x value
6470 // "ymin" - return minimum y value
6471 // "ymax" - return maximim y value
6472 // "y" - return the y value corresponding to x=V
6473 //
6474 // ERRORS : the same options can be used for errors : use 'e' in front of the option
6475 // for example to get error of 'x' use 'ex'
6476 // the input file must have 4 columns format : x y ex ey
6477 //
6478 
6479  bool ferror=false; // if true return errors
6480 
6481  // get option
6482  option.ToUpper();
6483  if(option!="") {
6484  if((option!="XMIN")&&(option!="XMAX")&&
6485  (option!="YMIN")&&(option!="YMAX")&&
6486  (option!="Y")&&(option!="EX")&&(option!="EY")) {
6487  cout<<"GetLinearInterpolation : Error : option --value bad value -> "
6488  <<"must be y/xmin/xmax/ymin/ymax"<<endl;
6489  exit(1);
6490  }
6491  if(option.BeginsWith("E")) {ferror=true;option.Remove(0,1);}
6492  }
6493 
6494  double xmin=DBL_MAX;
6495  double ymin=DBL_MAX;
6496  double xmax=-DBL_MAX;
6497  double ymax=-DBL_MAX;
6498 
6499  double exmin=DBL_MAX;
6500  double eymin=DBL_MAX;
6501  double exmax=-DBL_MAX;
6502  double eymax=-DBL_MAX;
6503 
6504  ifstream in;
6505  in.open(fName.Data());
6506  if (!in.good()) {cout << "GetGraphValue : Error Opening File : " << fName << endl;exit(1);}
6507  double ix,iy;
6508  double iex,iey;
6509  char line[1024];
6510  while(1) {
6511  in.getline(line,1024);
6512  if (in.eof()) break;
6513  std::stringstream linestream(line);
6514  if(ferror) { // get error value
6515  if(linestream >> ix >> iy >> iex >> iey) {
6516  ex.push_back(iex);
6517  ey.push_back(iey);
6518  if(iex<exmin) exmin=iex;
6519  if(iey<eymin) eymin=iey;
6520  if(iex>exmax) exmax=iex;
6521  if(iey>eymax) eymax=iey;
6522  } else {
6523  linestream.str(line);
6524  linestream.clear();
6525  if(!(linestream >> ix >> iy)) {
6526  cout << "GetGraphValue : Wrong line format : must be 'x y' " << endl;
6527  exit(1);
6528  }
6529  ex.push_back(0); ey.push_back(0);
6530  exmin=0;eymin=0;exmax=0;eymax=0;
6531  }
6532  } else {
6533  if(!(linestream >> ix >> iy)) {
6534  cout << "GetGraphValue : Wrong line format : must be 'x y' " << endl;
6535  exit(1);
6536  }
6537  ex.push_back(0); ey.push_back(0);
6538  exmin=0;eymin=0;exmax=0;eymax=0;
6539  }
6540  x.push_back(ix);
6541  y.push_back(iy);
6542  if(ix<xmin) xmin=ix;
6543  if(iy<ymin) ymin=iy;
6544  if(ix>xmax) xmax=ix;
6545  if(iy>ymax) ymax=iy;
6546  }
6547  in.close();
6548 
6549  if(option=="XMIN") return ferror ? exmin : xmin;
6550  if(option=="XMAX") return ferror ? exmax : xmax;
6551  if(option=="YMIN") return ferror ? eymin : ymin;
6552  if(option=="YMAX") return ferror ? eymax : ymax;
6553 
6554  if(x.size()==0) cout << "GetGraphValue : Error - input file empty" << endl;
6555 
6556  // Sort respect to x values
6557  int size = x.size();
6558  wavearray<int> I(size);
6559  wavearray<double> X(size);
6560  for(int i=0;i<size;i++) X[i]=x[i];
6561  TMath::Sort(size,X.data,I.data,false);
6562 
6563  // insert a new element at the beginning with x=-DBL_MAX and y[I[0]]
6564  // necessary for TGraph
6565  std::vector<double>::iterator it;
6566  it = x.begin(); x.insert(it,-DBL_MAX);
6567  it = y.begin(); y.insert(it,y[I[0]]);
6568  it = ex.begin(); ex.insert(it,ex[I[0]]);
6569  it = ey.begin(); ey.insert(it,ey[I[0]]);
6570  size = x.size();
6571 
6572  // Re-Sort respect to x values
6573  I.resize(size);
6574  X.resize(size);
6575  for(int i=0;i<size;i++) X[i]=x[i];
6576  TMath::Sort(size,X.data,I.data,false);
6577 
6578  x.push_back(x[I[size-1]]); y.push_back(-DBL_MAX);
6579  ex.push_back(ex[I[size-1]]); ey.push_back(ey[I[size-1]]);
6580  I.resize(size+1); I[size]=I[size-1]; size++;
6581 
6582  if(!ferror) { // return y
6583  if(V<x[I[0]]) return y[I[0]];
6584  if(V>=x[I[size-1]]) return y[I[size-1]];
6585  for(int i=1;i<size;i++) if((V>=x[I[i-1]]) && (V<x[I[i]])) return y[I[i-1]];
6586  } else {
6587  if(option=="X") { // return ex
6588  if(V<x[I[0]]) return ex[I[0]];
6589  if(V>=x[I[size-1]]) return ex[I[size-1]];
6590  for(int i=1;i<size;i++) if((V>=x[I[i-1]]) && (V<x[I[i]])) return ex[I[i-1]];
6591  } else if(option=="Y") { // return ey
6592  if(V<x[I[0]]) return ey[I[0]];
6593  if(V>=x[I[size-1]]) return ey[I[size-1]];
6594  for(int i=1;i<size;i++) if((V>=x[I[i-1]]) && (V<x[I[i]])) return ey[I[i-1]];
6595  }
6596  }
6597 }
6598 
6599 
6600 //______________________________________________________________________________
6601 void
6603 //
6604 // convert to a power of 2 rate > original rate
6605 //
6606 
6607  // compute the new rate
6608  int R=1;while (R < 2*(int)w.rate()) R*=2;
6609 
6610  if(R==2*w.rate()) return;
6611 
6612  Meyer<double> BB(256);
6613  WSeries<double> ww(BB);
6614  wavearray<double> yy(2*w.size());
6615  yy.rate(2*w.rate());
6616  yy.start(w.start());
6617 
6618  ww.Forward(yy,BB,1);
6619  ww=0.;
6620  ww.putLayer(w,0);
6621  ww.Inverse();
6622  yy.resample(ww,R,32);
6623  ww.Forward(yy,BB,1);
6624  ww.getLayer(w,0);
6625 
6626  fprintf(stdout,"--------------------------------\n");
6627  fprintf(stdout,"After resampling: rate=%f, size=%d, start=%f\n", w.rate(),(int)w.size(),w.start());
6628  fprintf(stdout,"--------------------------------\n");
6629 
6630  return;
6631 }
6632 
6633 //______________________________________________________________________________
6634 void
6636 //
6637 // write date and memory usage at a certain point of the analysis
6638 //
6639 
6640  gSystem->Exec("date");
6641  TString s;
6642  FILE *f = fopen(Form("/proc/%d/statm", gSystem->GetPid()), "r");
6643  s.Gets(f);
6644  Long_t total, rss;
6645  sscanf(s.Data(), "%ld %ld", &total, &rss);
6646  cout << str.Data() << " virtual : " << total * 4 / 1024 << " (mb) rss : " << rss * 4 / 1024 << " (mb)" << endl;
6647  fclose(f);
6648  return;
6649 }
6650 
6651 //______________________________________________________________________________
6652 TString
6654 //
6655 // get name of temporary file on the node
6656 //
6657 // Input: label associate to the analysis
6658 // file extension
6659 //
6660 
6661  // create temporary file
6662  gRandom->SetSeed(0);
6663  int rnID = int(gRandom->Rndm(13)*1.e9);
6664  UserGroup_t* uinfo = gSystem->GetUserInfo();
6665  TString uname = uinfo->fUser;
6666  // create temporary dir
6667  gSystem->Exec(TString("mkdir -p /dev/shm/")+uname);
6668 
6669  char fName[1024]="";
6670 
6671  sprintf(fName,"/dev/shm/%s/%s_%d.%s",uname.Data(),label.Data(),rnID,extension.Data());
6672 
6673  return fName;
6674 }
6675 
6676 //______________________________________________________________________________
6677 vector<TString>
6678 CWB::Toolbox::sortStrings(vector<TString> ilist) {
6679 //
6680 // sort in alphabetic order the TString list
6681 //
6682 // Input: TString list
6683 // return the sorted TString list
6684 //
6685 
6686  std::vector<std::string> vec(ilist.size());
6687  for(int i=0;i<ilist.size();i++) vec[i]=ilist[i].Data();
6688  std::sort( vec.begin(), vec.end() ) ;
6689  vector<TString> slist(ilist.size());
6690  for(int i=0; i<vec.size(); i++) slist[i]=vec[i];
6691 
6692  return slist;
6693 }
6694 
6695 //______________________________________________________________________________
6696 int
6698 //
6699 // return jobId from the file name
6700 // file must have the following format *_job#id.'fext'
6701 //
6702 
6703  if(!file.EndsWith("."+fext)) {
6704  cout << "CWB::Toolbox::getJobId : input file "
6705  << file.Data() << " must terminate with ." << fext << endl;
6706  gSystem->Exit(1);
6707  }
6708 
6709  file.ReplaceAll("."+fext,"");
6710  TObjArray* token = file.Tokenize('_');
6711  TObjString* sjobId = (TObjString*)token->At(token->GetEntries()-1);
6712  TString check = sjobId->GetString(); check.ReplaceAll("job","");
6713  if(!check.IsDigit()) {
6714  cout << "CWB::Toolbox::getJobId : input file " << file.Data()
6715  << " must terminate with _job#id." << fext << endl;
6716  cout << "#id is not a digit" << endl;
6717  gSystem->Exit(1);
6718  }
6719  int jobId = TString(sjobId->GetString()).ReplaceAll("job","").Atoi();
6720  if(token) delete token;
6721 
6722  return jobId;
6723 }
6724 
6725 //______________________________________________________________________________
6726 TString
6728 // get params from option string
6729 // options format : --par1 val1 --par2 val2 ...
6730 
6731  TObjArray* token;
6732 
6733  // check if input param format is correct
6734  token = param.Tokenize(TString(" "));
6735  if((token->GetEntries())>0) {
6736  TObjString* otoken = (TObjString*)token->At(0);
6737  TString stoken = otoken->GetString();
6738  if(((token->GetEntries())>1) || (!stoken.BeginsWith("--"))) {
6739  cout << "CWB::Toolbox::getParameter : Bad param format \"" << param.Data() << "\"" << endl;
6740  cout << "Correct format is : --par1" << endl;
6741  gSystem->Exit(1);
6742  }
6743  }
6744  if(token) delete token;
6745 
6746  token = options.Tokenize(TString(" "));
6747  // check if input options format is correct
6748  if((token->GetEntries())%2==1) {
6749  cout << "CWB::Toolbox::getParameter : Bad options format \"" << options.Data() << "\"" << endl;
6750  cout << "Correct format is : --par1 val1 --par2 val2 ..." << endl;
6751  gSystem->Exit(1);
6752  }
6753  for(int i=0;i<token->GetEntries();i+=2) {
6754  TObjString* otoken = (TObjString*)token->At(i);
6755  TString stoken = otoken->GetString();
6756  if(!stoken.BeginsWith("--")) {
6757  cout << "CWB::Toolbox::getParameter : Bad options format \"" << stoken.Data() << "\"" << endl;
6758  cout << "Correct format is : --par1 val1 --par2 val2 ..." << endl;
6759  gSystem->Exit(1);
6760  }
6761  }
6762  for(int i=0;i<token->GetEntries();i++) {
6763  TObjString* otoken = (TObjString*)token->At(i);
6764  TString stoken = otoken->GetString();
6765  // exctract value
6766  if(stoken==param) {
6767  if(i<token->GetEntries()-1) {
6768  otoken = (TObjString*)token->At(i+1);
6769  return otoken->GetString();
6770  }
6771  }
6772  }
6773  if(token) delete token;
6774 
6775  return "";
6776 }
6777 
6778 //______________________________________________________________________________
6779 TString
6781 //
6782 // get file name from its pointer fp
6783 //
6784 
6785  int MAXSIZE = 0xFFF;
6786  char proclnk[0xFFF];
6787  char filename[0xFFF];
6788  int fno;
6789  ssize_t r;
6790 
6791  if (fp != NULL)
6792  {
6793  fno = fileno(fp);
6794  sprintf(proclnk, "/proc/self/fd/%d", fno);
6795  r = readlink(proclnk, filename, MAXSIZE);
6796  if (r < 0) return "";
6797  filename[r] = '\0';
6798  return filename;
6799  }
6800  return "";
6801 }
6802 
6803 //______________________________________________________________________________
6804 TString
6805 CWB::Toolbox::getFileName(char* symlink) {
6806 //
6807 // get file name from its symbolic link
6808 //
6809 
6810  int MAXSIZE = 0xFFF;
6811  char proclnk[0xFFF];
6812  char filename[0xFFF];
6813  ssize_t r;
6814 
6815  if (symlink != NULL)
6816  {
6817  sprintf(proclnk, "%s", symlink);
6818  r = readlink(symlink, filename, MAXSIZE);
6819  if (r < 0) return "";
6820  filename[r] = '\0';
6821  return filename;
6822  }
6823  return "";
6824 }
6825 
6826 //______________________________________________________________________________
6827 void
6829 //
6830 // extract from ifile (contains a list of files) a unique file list and write it to ofile
6831 //
6832 
6833  vector<std::string> ifileList;
6834  vector<std::string> ipathList;
6835  vector<std::string> ofileList;
6836  vector<std::string> opathList;
6837 
6838  ifstream in;
6839  in.open(ifile.Data());
6840  if(!in.good()) {
6841  cout << "CWB::Toolbox::getUniqueFileList : Error Opening Input File : " << ifile.Data() << endl;
6842  gSystem->Exit(1);
6843  }
6844 
6845  // read file list
6846  char istring[1024];
6847  while(1) {
6848  in.getline(istring,1024);
6849  if (!in.good()) break;
6850  TObjArray* token = TString(istring).Tokenize(TString('/'));
6851  // extract last entry -> file name
6852  TObjString* stoken =(TObjString*)token->At(token->GetEntries()-1);
6853  TString fName = stoken->GetString();
6854  //cout << fName.Data() << endl;
6855  ipathList.push_back(istring);
6856  ifileList.push_back(fName.Data());
6857  }
6858  in.close();
6859 
6860  // extract unique file list
6861  for(int i=0;i<(int)ifileList.size();i++) {
6862  bool check=false;
6863  for(int j=0;j<(int)ofileList.size();j++) {
6864  if(TString(ofileList[j])==TString(ifileList[i])) {check=true;break;}
6865  }
6866  if(!check) {
6867  ofileList.push_back(ifileList[i]);
6868  opathList.push_back(ipathList[i]);
6869  }
6870  //cout << i << " " << ipathList[i] << endl;
6871  //cout << i << " " << ifileList[i] << endl;
6872  }
6873 
6874  // write unique file list
6875  ofstream out;
6876  out.open(ofile.Data(),ios::out);
6877  if(!out.good()) {
6878  cout << "CWB::Toolbox::getUniqueFileList : Error Opening Output File : " << ofile.Data() << endl;
6879  gSystem->Exit(1);
6880  }
6881 
6882  for(int i=0;i<(int)ofileList.size();i++) {
6883  out << opathList[i] << endl;
6884  //cout << i << " " << opathList[i] << endl;
6885  //cout << i << " " << ofileList[i] << endl;
6886  }
6887 
6888  out.close();
6889 
6890  return;
6891 }
6892 
6893 //______________________________________________________________________________
6896 //
6897 // return the Hilbert transform
6898 //
6899 
6901 
6902  y.FFTW(1);
6903  for(int i=1;i<(int)y.size()/2;i++) {
6904  double temp=y[2*i+1];
6905  y[2*i+1]=y[2*i];
6906  y[2*i]=-1*temp;
6907  }
6908  y.FFTW(-1);
6909 
6910  return y;
6911 }
6912 
6913 //______________________________________________________________________________
6916 //
6917 // return the envelope from analytic signal analysis
6918 //
6919 
6921 
6922  // compute hilbert transform
6923  wavearray<double> h = getHilbertTransform(y);
6924 
6925  // compute envelope
6926  for(int i=0;i<(int)y.size();i++) {
6927  y[i]=sqrt(y[i]*y[i]+h[i]*h[i]);
6928  }
6929 
6930  return y;
6931 }
6932 
6933 //______________________________________________________________________________
6936 //
6937 // return the instantaneous frequency from analytic signal analysis
6938 //
6939 
6941 
6942  double twopi = TMath::TwoPi();
6943  double dt = 1./x.rate();
6944 
6945  // compute hilbert transform
6946  wavearray<double> h = getHilbertTransform(y);
6947 
6948  // compute phase (use p as temporary array for phase)
6949  wavearray<float> p(h.size());
6950  for(int i=0;i<(int)y.size();i++) {
6951  p[i]=TMath::ATan2(y[i],h[i]);
6952  }
6953 
6954  unWrapPhase(p);
6955  for(int i=1;i<(int)p.size()/2-1;i++) p[2*i]=(p[2*i-1]+p[2*i+1])/2; // cut alias frequency
6956 
6957  // compute frequency
6958  for(int i=1;i<(int)x.size();i++) {
6959  y[i] = (p[i]-p[i-1])/(twopi*dt);
6960  if(y[i]>x.rate()/2) y[i]=0; // cut unphysical frequency
6961  if(y[i]<0) y[i]=0; // cut unphysical frequency
6962  }
6963  if(y.size()>1) y[0]=y[1]; else y[0]=0;
6964  if(y.size()>1) y[y.size()-1]=y[y.size()-2]; else y[y.size()-1]=0;
6965 
6966  return y;
6967 }
6968 
6969 //______________________________________________________________________________
6972 //
6973 // return TF map wavearray NxN : Matrix[i,j]=N*j+i : i=time_index, j=freq_index
6974 //
6975 
6976  int N = x.size();
6977  int dN = 2*N;
6978 
6979  wavearray<double> y = x;
6980  y.FFTW(1);
6981  y.resize(2*y.size());
6982  for(int i=(int)x.size();i<(int)y.size();i++) y[i]=0;
6983  y.FFTW(-1);
6984 
6985  wavearray<double> z(3*dN);
6986  z=0;for(int i=dN; i<2*dN; ++i) z[i] = y[i-dN];
6987 
6988  wavearray<double> wv(N*N);
6989  wv.start(x.start());
6990  wv.rate(x.rate());
6991 
6992  y.resize(dN);
6993  for(int n=1; n<=N; ++n ) {
6994 
6995  for(int i=0; i<N; ++i) y[i] = z[dN+2*n+i]*z[dN+2*n-i];
6996  for(int i=-N; i<0; ++i) y[dN+i] = z[dN+2*n+i]*z[dN+2*n-i];
6997 
6998  y.FFTW(1);
6999 
7000  for(int m=0; m<N; m++) wv[m*N+(n-1)] = y[2*m];
7001  }
7002 
7003  return wv;
7004 }
7005 
7006 //______________________________________________________________________________
7007 void
7009 //
7010 // Phase unwrapping ensures that all appropriate multiples of 2*pi have been included in phase evolution
7011 // p : phase array
7012 // return in p the unwrapped phase
7013 //
7014 
7015  int N = p.size();
7016 
7017  // ported from matlab (Dec 2002)
7018  wavearray<float> dp(N);
7019  wavearray<float> dps(N);
7020  wavearray<float> dp_corr(N);
7021  wavearray<float> cumsum(N);
7022  float cutoff = M_PI; /* default value in matlab */
7023  int j;
7024 
7025  // incremental phase variation
7026  // MATLAB: dp = diff(p, 1, 1);
7027  for (j = 0; j < N-1; j++) dp[j] = p[j+1] - p[j];
7028 
7029  // equivalent phase variation in [-pi, pi]
7030  // MATLAB: dps = mod(dp+dp,2*pi) - pi;
7031  for (j = 0; j < N-1; j++)
7032  dps[j] = (dp[j]+M_PI) - floor((dp[j]+M_PI) / (2*M_PI))*(2*M_PI) - M_PI;
7033 
7034  // preserve variation sign for +pi vs. -pi
7035  // MATLAB: dps(dps==pi & dp>0,:) = pi;
7036  for (j = 0; j < N-1; j++)
7037  if ((dps[j] == -M_PI) && (dp[j] > 0))
7038  dps[j] = M_PI;
7039 
7040  // incremental phase correction
7041  // MATLAB: dp_corr = dps - dp;
7042  for (j = 0; j < N-1; j++)
7043  dp_corr[j] = dps[j] - dp[j];
7044 
7045  // Ignore correction when incremental variation is smaller than cutoff
7046  // MATLAB: dp_corr(abs(dp)<cutoff,:) = 0;
7047  for (j = 0; j < N-1; j++)
7048  if (fabs(dp[j]) < cutoff)
7049  dp_corr[j] = 0;
7050 
7051  // Find cumulative sum of deltas
7052  // MATLAB: cumsum = cumsum(dp_corr, 1);
7053  cumsum[0] = dp_corr[0];
7054  for (j = 1; j < N-1; j++)
7055  cumsum[j] = cumsum[j-1] + dp_corr[j];
7056 
7057  // Integrate corrections and add to P to produce smoothed phase values
7058  // MATLAB: p(2:m,:) = p(2:m,:) + cumsum(dp_corr,1);
7059  for (j = 1; j < N; j++) p[j] += cumsum[j-1];
7060 
7061 }
7062 
7063 //______________________________________________________________________________
7064 void
7065 CWB::Toolbox::getSineFittingParams(double a, double b, double c, double rate,
7066  double& amplitude, double& omega, double& phase) {
7067 //
7068 // return the amplitude,phase,omega of the 3 numbers Sine fitting
7069 // a,b,c are 3 consecutive discrete values sampled at rate='rate'
7070 // return != 0 only if (b>a && b>c) or (b<a && b<c)
7071 //
7072 
7073  double dt = 1./rate;
7074 
7075  // The time of maximum is the time of b - cp.phase/cp.omega
7076 
7077  if(((a<b && b>=c && b>0)||(a>b && b<=c && b<0)) && (fabs((a+c)/(2*b))<1)){
7078 
7079  omega = acos((a+c)/(2*b))/dt;
7080  phase = atan2(a*a+c*a-2*b*b, 2*a*b*sin(omega*dt));
7081  amplitude = (a!=0)?(a/cos(phase)):(b/sin(omega*dt));
7082 
7083  return;
7084  } else {
7085  amplitude = 0;
7086  return;
7087  }
7088 }
7089 
7090 //______________________________________________________________________________
7091 TString
7093 //
7094 // Write x array to frame file
7095 //
7096 // Input: x - wavearray data
7097 // chName - channel name
7098 // frName - frame name
7099 // frLabel - label used for output file name
7100 // file name path = frDir/frLabel-gps-length.gwf
7101 // frDir - output directory
7102 //
7103 // return file name
7104 //
7105 
7106  // check input wavearray data
7107  if(x.size()==0 || x.rate()==0) {
7108  cout << "CWB::Toolbox::WriteFrameFile - Error : wavearray size=0 or rate=0 " << endl;
7109  exit(1);
7110  }
7111 
7112  double gps = x.start();
7113  double length = (x.size()/x.rate());
7114 
7115  // check gps integer
7116  if(fmod(gps,1)!=0) {
7117  cout << "CWB::Toolbox::WriteFrameFile - Error : start gps time is not integer" << endl;
7118  exit(1);
7119  }
7120  // check length integer
7121  if(fmod(length,1)!=0) {
7122  cout << "CWB::Toolbox::WriteFrameFile - Error : length is not integer" << endl;
7123  exit(1);
7124  }
7125 
7126  if(frDir=="") frDir=".";
7127 
7128  char frFile[1024];
7129  sprintf(frFile,"%s/%s-%lu-%lu.gwf",frDir.Data(),frLabel.Data(),(int)gps,(int)length);
7130  cout << frFile << endl;
7131  FrFile *ofp = FrFileONew(frFile,1); // gzip compression
7132 
7133  /*----------------------- Create a new frame ---------*/
7134 
7135  FrameH* simFrame = FrameNew(const_cast<char*>(frLabel.Data()));
7136  simFrame->frame = 0;
7137  simFrame->run = -1;
7138  simFrame->dt = length;
7139  simFrame->GTimeS = gps;
7140  simFrame->GTimeN = 0;
7141 
7142  cout << "Size (sec) " << x.size()/x.rate() << endl;
7143  FrProcData* proc = FrProcDataNew(simFrame,const_cast<char*>(chName.Data()),x.rate(),x.size(),-64);
7144  if(proc == NULL) {cout << "CWB::Toolbox::WriteFrameFile - Cannot create FrProcData" << endl; exit(-1);}
7145  proc->timeOffset = 0;
7146  proc->tRange = simFrame->dt;
7147  proc->type = 1; // Time Serie
7148 
7149  for (int i=0;i<(int)proc->data->nData;i++) proc->data->dataD[i] = x[i];
7150 
7151  int err=FrameWrite(simFrame,ofp);
7152  if (err) {cout << "CWB::Toolbox::WriteFrameFile - Error writing frame" << endl;exit(1);}
7153  FrameFree(simFrame);
7154 
7155  if (ofp!=NULL) FrFileOEnd(ofp);
7156 
7157  return frFile;
7158 }
7159 
7160 //______________________________________________________________________________
7164 //
7165 // measurement of phase sync between two waveforms
7166 // hi - whitened CBC PE template
7167 // hr - cWB whitened reconstructed signal
7168 // The cWB and CBC waveforms are synchronized and the following data is calculated
7169 // as a function of number of cycles from the nominal merger time
7170 // fi - frequency evolution of the CBC template
7171 // fr - frequency evolution of the CWB waveform
7172 // tt - waveform evolution time [sec]
7173 // s - accumulated cWB SNR
7174 // p - phase difference between the cWB and CBC waveforms
7175 // q - phase difference between the CBC L and H detectors
7176 //
7177 
7178  double R,a;
7179  wavearray<double> x,y,p,q,r;
7180  wavearray<double> wi,wr;
7181  wavearray<double> HI,HR,WI,WR;
7182  double thi,thr,T;
7183  double t,dt,am;
7184  int j=0;
7185  int l=0;
7186 
7187  thi=hi.start();
7188 
7189  T=hi.start();
7190  if(thr>T) T=thr;
7191  if(thi>T) T=thi;
7192 
7193  hr.start(hr.start()-T); hi.start(hi.start()-T);
7194  R = int(hr.rate()+0.01);
7195 
7196  am = 0;
7197  for(int i=0; i<hi.size(); i++) {
7198  t = hi.start()+i/R;
7199  if(t<0) continue;
7200  if(fabs(hi.data[i])>am) {am=fabs(hi.data[i]); T=t;}
7201  }
7202  int Lt = int(R*(int(T)+1)+0.01);
7203 
7204  HR=hr; HI=hi;
7205 
7206  WI=hi; WR=hr;
7207  wi=WI; wr=WR;
7208 
7209  for(int m=HI.size()-1; m>0; m--) {
7210  if(hi.data[m]>0) hi.data[m]=1; else hi.data[m]=-1;
7211  if(hr.data[m]>0) hr.data[m]=1; else hr.data[m]=-1;
7212  if(wi.data[m]>0) wi.data[m]=1; else wi.data[m]=-1;
7213  if(wr.data[m]>0) wr.data[m]=1; else wr.data[m]=-1;
7214  }
7215 
7216  int nl,nr,mm;
7217 
7218  tt=HI; tt=0;
7219  fi=HI; fi=0;
7220  fr=HI; fr=0;
7221  p=HI; p=0;
7222  q=HI; q=0;
7223  s=HI; s=0;
7224  double Er,Ei;
7225  j=0; dt=1; am=0; l=1; nl=0;
7226  for(int m=HI.size()-1; m>0; m--) {
7227  t = wi.start()+m/wi.rate();
7228  if(t<=0 || t>T) continue;
7229  if(j==0 && am==0.) am=wi.data[m];
7230  if(wi.data[m]*am <= 0) {
7231  nr = -wi.data[m]*wr.data[m];
7232  am=wi.data[m];
7233  tt.data[j]=t;
7234  p.data[j] /= l;
7235  q.data[j] /= l; l=0;
7236  fi.data[j]=0.5/dt; dt=0.;
7237  mm=m; while(wr.data[mm++]*wr.data[m]>0) fr.data[j]+=1;
7238  mm=m; while(wr.data[mm--]*wr.data[m]>0) fr.data[j]+=1;
7239  fr.data[j] = 0.5*HI.rate()/fr.data[j];
7240  if(j>0) s.data[j]=s.data[j]+s.data[j-1];
7241  j++;
7242  }
7243  a = wr.data[m]*wi.data[m];
7244  if(a<0) p.data[j]+=a*nr;
7245  q.data[j]+=hi.data[m]; l++;
7246  s.data[j]+=HR.data[m]*HR.data[m];
7247  dt += 1./HI.rate();
7248  }
7249 
7250  fi.resize(j); fi.rate(2);
7251  fr.resize(j); fr.rate(2);
7252  tt.resize(j); tt.rate(2);
7253 
7254  p.resize(j); p.rate(2); p.data[0]=1.; p.data[1]=-2.;
7255  q.resize(j); q.rate(2);
7256  s.resize(j); s.rate(2);
7257 
7258  for(int j=0;j<s.size();j++) s[j]=sqrt(s[j]);
7259 
7260  for(int m=p.size()-2; m>0; m--) {
7261  if(p.data[m]-p.data[m+1] > 1) mm=2; else mm=0;
7262  p.data[m]-=mm;
7263  }
7264 
7265  for(int j=0;j<p.size();j++) {
7266  if(p[j]<-1) p[j]+=2;
7267  if(p[j]>1) p[j]-=2;
7268  }
7269 
7270  return p;
7271 }
7272 
7273 //_______________________________________________________________________________________
7274 Int_t
7276 //
7277 // @brief Returns a color out of the Tableau color blind set
7278 // @details Uses a numerical index to determine which color to return
7279 //
7280 // @param index 0-9 for the different colors
7281 // @return Int_t Reference to the ROOT color number
7282 
7283  switch ( index ) {
7284  case 0: return TColor::GetColor( 0, 107, 164);
7285  case 1: return TColor::GetColor(255, 128, 14);
7286  case 2: return TColor::GetColor(171, 171, 171);
7287  case 3: return TColor::GetColor( 89, 89, 89);
7288  case 4: return TColor::GetColor( 95, 158, 209);
7289  case 5: return TColor::GetColor(200, 82, 0);
7290  case 6: return TColor::GetColor(137, 137, 137);
7291  case 7: return TColor::GetColor(163, 200, 236);
7292  case 8: return TColor::GetColor(255, 188, 121);
7293  case 9: return TColor::GetColor(207, 207, 207);
7294  default: return kBlack;
7295  }
7296 }
7297 
7298 //_______________________________________________________________________________________
7299 Int_t
7301 //
7302 // @brief Returns a color out of the Tableau color blind set
7303 // @details Uses a string representation to determine which color to return
7304 // to increase readability of the code. Names represent similar colors from
7305 // LaTeX' xcolor package with svgnames or x11names (if there is a number at
7306 // end of the name).
7307 //
7308 // @param name Name of the color
7309 // @return Int_t Reference to the ROOT color number
7310 
7311  if ( name == "DeepSkyBlue4" ) return getTableau10BlindColor(0);
7312  else if ( name == "DarkOrange1" ) return getTableau10BlindColor(1);
7313  else if ( name == "DarkGray" ) return getTableau10BlindColor(2);
7314  else if ( name == "DimGray" ) return getTableau10BlindColor(3);
7315  else if ( name == "SkyBlue3" ) return getTableau10BlindColor(4);
7316  else if ( name == "Chocolate3" ) return getTableau10BlindColor(5);
7317  else if ( name == "Gray" ) return getTableau10BlindColor(6);
7318  else if ( name == "SlateGray1" ) return getTableau10BlindColor(7);
7319  else if ( name == "SandyBrown" ) return getTableau10BlindColor(8);
7320  else if ( name == "LightGray" ) return getTableau10BlindColor(9);
7321  else return kBlack;
7322 }
7323 
7324 //_______________________________________________________________________________________
7325 void
7327 //
7328 // @brief Generate a palette for color blinds
7329 // @details Use the tableau 10 blind palette to generate a palette for
7330 // ROOT's 2D histograms.
7331 //
7332 // @param const int Number of steps to generate
7333 // @param Int_t * Pointer to the array storing the palette colors
7334 
7335  // define the colors
7336  const int nColors = 4;
7337  TColor * col[nColors] = {
7338  gROOT->GetColor(getTableau10BlindColor("SlateGray1")),
7339  gROOT->GetColor(getTableau10BlindColor("DeepSkyBlue4")),
7340  gROOT->GetColor(getTableau10BlindColor("SandyBrown")),
7341  gROOT->GetColor(getTableau10BlindColor("Crimson"))
7342  };
7343  Double_t stop[nColors] = {0., .25, .65, 1.0};
7344 
7345  // get color values
7346  Double_t r[nColors], g[nColors], b[nColors];
7347  for ( int c = 0; c < nColors; c++ ) {
7348  r[c] = col[c]->GetRed();
7349  g[c] = col[c]->GetGreen();
7350  b[c] = col[c]->GetBlue();
7351  }
7352 
7353  // generate palette
7354  Int_t FI = TColor::CreateGradientColorTable(nColors, stop, r, g, b, nSteps);
7355  for ( int i = 0; i < nSteps; i++ ) palette[i] = FI+i;
7356 }
7357 
char ofile[1024]
wavearray< double > t(hp.size())
TTree * tree
Definition: TimeSortTree.C:20
#define TRIALS
static void setUniqueEvents(TString ifwave, TString ofwave, int nIFO, int pp_irho)
Definition: Toolbox.cc:3335
size_t append(const wavearray< DataType_t > &)
Definition: wavearray.cc:793
double pc
Definition: DrawEBHH.C:15
double rho
double segMLS
Definition: test_config1.C:47
int slagSize
Definition: test_config1.C:65
static void unWrapPhase(wavearray< float > &p)
Definition: Toolbox.cc:7008
double startMDC
Definition: Toolbox.hh:106
static void PrintProcInfo(TString str="")
Definition: Toolbox.cc:6635
Double_t PoissonIFunction(Double_t *x, Double_t *par)
Definition: Toolfun.hh:33
Float_t * rho
biased null statistics
Definition: netevent.hh:112
double start
Definition: network.hh:55
TString ofName
static waveSegment getMaxSeg(vector< waveSegment > list)
Definition: Toolbox.cc:1827
#define TIME_SCRATCH
static double g(double e)
Definition: GNGen.cc:116
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5108
static wavearray< double > getWignerVilleTransform(wavearray< double > x)
Definition: Toolbox.cc:6971
double M
Definition: DrawEBHH.C:13
size_t * slagSite
Definition: test_config1.C:69
int slagOff
Definition: test_config1.C:68
int nfiles
par [0] value
mdcshift mdc_shift
Definition: test_config1.C:93
double time1
TString live
TB createDagFile(rslagList, full_condor_dir, data_label, jobFiles, cwb_stage_name)
virtual void rate(double r)
Definition: wavearray.hh:141
static int setMultiplicity(TString ifName, TString idir, TString odir, int nIFO, double dTime)
Definition: Toolbox.cc:5907
double Live
float factor
CWB run(runID)
Int_t nentries
Definition: TimeSortTree.C:24
wavearray< double > a(hp.size())
double liveZero
wavearray< double > Trun(500000)
cout<< "slagList size : "<< slagList.size()<< endl;cout<< endl<< "Start segments selection from dq cat1 list ..."<< endl<< endl;rslagList=TB.getSlagList(slagList, ifos, segLen, segMLS, segEdge, nDQF, DQF, CWB_CAT1);cout<< "Number of selected jobs after cat1 : "<< rslagList.size()<< endl;cout<< endl<< "Start segments selection from dq cat2 list ..."<< endl<< endl;rslagList=TB.getSlagList(rslagList, ifos, segLen, segTHR, segEdge, nDQF, DQF, CWB_CAT2);cout<< "Number of selected jobs after cat2 : "<< rslagList.size()<< endl;vector< TString > jobFiles
static TString WriteFrameFile(wavearray< double > x, TString chName, TString frName, TString frLabel, TString frDir="")
Definition: Toolbox.cc:7092
#define XMAX
static int setIFAR(TString ifName, TString idir, TString odir, TString trname, TString sels, TString farFile, int irho, TString olabel, bool inclusive=true)
Definition: Toolbox.cc:2990
par [0] name
int n
Definition: cwb_net.C:28
vector< slag > slist
wavearray< double > z
Definition: Test10.C:32
dqfile VDQF[100]
bool invert
Definition: Toolbox.hh:88
TString("c")
cout<< cwb_condor_flen<< endl;double xtime;char jobListFile[256];sprintf(jobListFile,"%s/%s.sjob", dump_dir, data_label);char dq1ListFile[256];sprintf(dq1ListFile,"%s/%s.cat1", dump_dir, data_label);cout<< endl<<"-------------------------------------------------------------------------------------"<< endl<< endl;CWB_CAT dqcat=CWB_CAT1;vector< waveSegment > dq1List
int palette
Definition: DrawGnetwork2.C:17
int nset
Definition: cwb_mkeff.C:75
char ifo[32]
Definition: Toolbox.hh:84
bool TypeAllowed(char *Name)
Definition: History.cc:127
double stopMDC
Definition: Toolbox.hh:107
static void doPoissonPlot(int nIFO, wavearray< double > *Wlag, wavearray< double > Tlag, wavearray< double > Rlag, TString odir)
Definition: Toolbox.cc:5193
ofstream out
Definition: cwb_merge.C:214
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1) *itime+ifreq;double time=itime *dT;double freq=(ifreq >0) ? ifreq *dF :dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
double frequency
double phase
bool c4
Definition: Toolbox.hh:89
Int_t GetEntry(Int_t)
Definition: netevent.cc:409
CWB_CAT dqcat
char odir[1024]
double amplitude
float xslag[6]
wavearray< double > psd(33)
CWB::frame fr(FRLIST_NAME)
TString mdc[4]
int slagMax
Definition: test_config1.C:67
dqfile * XDQF
vector< int > segId
Definition: Toolbox.hh:102
std::vector< waveSegment > olist
return wmap canvas
CWB_CAT
Definition: Toolbox.hh:72
static std::map< int, TString > getJobFileMapFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5169
Long_t flags
float xlag
CWB::History * history
TTree * fChain
root input file cointainig the analyzed TTree
Definition: netevent.hh:63
static wavearray< double > getHilbertIFrequency(wavearray< double > x)
Definition: Toolbox.cc:6935
cout<< "baudline_FFL : "<< baudline_FFL<< endl;ofstream out;out.open(baudline_FFL, ios::out);if(!out.good()) {cout<< "Error Opening File : "<< baudline_FFL<< endl;exit(1);} ifstream in;in.open(frFiles[ifoID], ios::in);if(!in.good()) {cout<< "Error Opening File : "<< frFiles[ifoID]<< endl;exit(1);} TString pfile_path="";char istring[1024];while(1) { in > istring
Long_t size
NET segList
Definition: cwb_net.C:294
static double getLiveTime(vector< waveSegment > &jobList, vector< waveSegment > &dqList)
Definition: Toolbox.cc:2094
int m
Definition: cwb_net.C:28
static double getTimeSegList(vector< waveSegment > list)
Definition: Toolbox.cc:611
virtual void start(double s)
Definition: wavearray.hh:137
vector< waveSegment > detSegs_dq2
Definition: cwb_net.C:297
double segEdge
Definition: test_config1.C:49
static TString CWB_CAT_NAME[14]
Definition: GToolbox.hh:21
int nVDQF
int j
Definition: cwb_net.C:28
wavearray< int > Wsel(ntrg)
i drho i
static vector< waveSegment > invertSegments(vector< waveSegment > &ilist)
Definition: Toolbox.cc:247
TList * list
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
int isize
static vector< int > getMergeJobList(TString merge_dir, TString label, int version)
Definition: Toolbox.cc:1698
TB mergeTrees(fileList, TREE_NAME, OUTPUT_MERGE_DIR, OFILE_NAME, false)
#define MAX_TREE_SIZE
Definition: Toolbox.cc:36
wc clear()
int jobID
Definition: cwb_net.C:195
#define N
std::vector< waveSegment > mlist
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
int jobId
Definition: Toolbox.hh:100
double fWidth
Definition: DrawPSD.C:16
cout<< "ctime : "<< int(ctime)<< " sec "<< ctime/3600.<< " h "<< ctime/86400.<< " day"<< endl;std::vector< waveSegment > jlist
char ifo[NIFO_MAX][8]
double Tgap
Definition: test_config1.C:23
static char * getEnvCWB()
Definition: Toolbox.cc:5383
#define SRATE
static wavecomplex getRate(double rho, double Tgap, int nIFO, TChain &wav, wavearray< int > &Wsel, wavearray< double > *Wlag, wavearray< double > *Wslag, wavearray< double > Tlag)
Definition: Toolbox.cc:6193
nDQF
Definition: cwb_eced.C:109
int compareSegments(waveSegment a, waveSegment b)
Definition: Toolbox.cc:30
fprintf(stdout,"start=%f duration=%f rate=%f\, x.start(), x.size()/x.rate(), x.rate())
iseg push_back(seg)
static double getZeroLiveTime(int nIFO, TChain &liv, int dummy=0)
Definition: Toolbox.cc:4889
static vector< waveSegment > unionSegments(vector< waveSegment > &ilist)
Definition: Toolbox.cc:119
double segTHR
Definition: test_config1.C:48
void * MergeHandle(void *ptr)
Definition: Toolbox.cc:49
static void mergeTrees(vector< TString > fileList, TString treeName, TString odir, TString ofName, bool bhistory)
Definition: Toolbox.cc:2650
wavearray< double > w
Definition: Test1.C:27
static vector< float > getJobBenchmark(TString ifName, int stageID, TString bench)
Definition: Toolbox.cc:1445
nc append(pix)
#define nIFO
char val[20]
CWB_CAT cat
Definition: Toolbox.hh:86
static int setCuts(TString ifName, TString idir, TString odir, TString trname, TString cuts, TString olabel)
Definition: Toolbox.cc:2876
virtual size_t size() const
Definition: wavearray.hh:145
char data_label[512]
Definition: test_config1.C:160
TString chName[NIFO_MAX]
int nXDQF
double time2
char str[1024]
wavearray< double > freq
Definition: Regression_H1.C:79
char file[1024]
Definition: Toolbox.hh:85
static char * readFile(TString ifName)
Definition: Toolbox.cc:2835
static vector< TString > mergeCWBTrees(TString dir_name, bool simulation, TString odir, TString label, bool brms=false, bool bvar=false, bool bpsm=false)
Definition: Toolbox.cc:2462
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
static void getSimNoise(wavearray< double > &u, TString fName, int seed, int run)
Definition: Toolbox.cc:5593
wavearray< double > Rlag
static TString GetMDCLog(TString log, int pos)
Definition: Toolbox.cc:2278
static int createSubFile(TString label, TString condor_dir, TString out_dir, TString err_dir, TString log_dir, TString ext="", TString condor_tag="")
Definition: Toolbox.cc:1337
waveSegment SEG
TGraph * gr
int jobmax
wavearray< double > h
Definition: Regression_H1.C:25
vector< int > jobList
#define TIME_UNCERTAINTY
static void convertSampleRate(wavearray< double > iw, wavearray< double > ow)
Definition: Toolbox.cc:5829
ifwave[0]
static bool isLeafInTree(TTree *itree, TString leaf)
Definition: Toolbox.cc:5466
static void resampleToPowerOfTwo(wavearray< double > &w)
Definition: Toolbox.cc:6602
vector< slag > slagList
float islag
char * GetLog(char *Stage, int index)
Definition: History.cc:304
i() int(T_cor *100))
int GetLogSize(char *Stage)
Definition: History.cc:298
static vector< TString > readFileList(TString ifName)
Definition: Toolbox.cc:2739
static void dumpFileList(vector< TString > fileList, TString ofName)
Definition: Toolbox.cc:2769
TString label
Definition: MergeTrees.C:21
double Pi
const int NIFO_MAX
Definition: wat.hh:22
bool log
Definition: WaveMDC.C:41
dqfile DQF[12]
Definition: test_config1.C:171
TTree * otree
static int dumpSegList(vector< waveSegment > list, TString fName, bool c4=false)
Definition: Toolbox.cc:571
TString frDir[NIFO_MAX]
static void getTableau10BlindColorPalette(const int nSteps, Int_t *palette)
Definition: Toolbox.cc:7326
wavearray< double > Tlag
char cut[512]
int getLayer(wavearray< DataType_t > &w, double n)
param: n - layer number
Definition: wseries.cc:193
static vector< waveSegment > getSlagJobList(vector< waveSegment > ilist, int seglen=600)
Definition: Toolbox.cc:1791
printf("total live time: non-zero lags = %10.1f \, liveTot)
int l
static int setChunk(TString ifName, TString idir, TString odir, TString trname, int chunk)
Definition: Toolbox.cc:3214
TIter next(twave->GetListOfBranches())
Float_t * lag
time between consecutive events
Definition: netevent.hh:84
char fname[1024]
segLen
Definition: cwb_eced.C:24
double shift
Definition: Toolbox.hh:87
#define nMDC
static int GetMDCLogSize(TString log)
Definition: Toolbox.cc:2263
char merge_dir[512]
Definition: test_config1.C:147
TList * GetStageNames()
Definition: History.cc:427
static bool rmDir(TString dir, bool question=true)
Definition: Toolbox.cc:4767
double liveTot
double xlive
int k
wavearray< double > Tdlag
char data_dir[512]
Definition: test_config1.C:152
char schunk[32]
Definition: cwb_setchunk.C:74
double offset
Definition: Toolbox.hh:108
UserGroup_t * uinfo
Definition: cwb_frdisplay.C:91
TString sel("slag[1]:slag[2]")
bool StageAlreadyPresent(char *Name)
Definition: History.cc:107
vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="")
Definition: cwb_csh2sh.C:168
vector< waveSegment > cat1List
TObjArray * token
char log_dir[512]
Definition: test_config1.C:151
static vector< TString > sortStrings(vector< TString > ilist)
Definition: Toolbox.cc:6678
double * entry
Definition: cwb_setcuts.C:224
static void getUniqueFileList(TString ifile, TString ofile)
Definition: Toolbox.cc:6828
TFile * ifile
static int createDagFile(vector< waveSegment > jobList, TString condor_dir, TString label, int jobmin=0, int jobmax=0)
Definition: Toolbox.cc:1172
static int getJobId(TString file, TString fext="root")
Definition: Toolbox.cc:6697
bool slagFound
static vector< waveSegment > getJobList(vector< waveSegment > ilist, double segLen=600., double segMLS=300., double segEdge=8.)
Definition: Toolbox.cc:646
static bool question(TString question)
Definition: Toolbox.cc:4808
wavearray< double > yy
Definition: TestFrame5.C:12
WSeries< double > ww
Definition: Regression_H1.C:33
waveSegment seg
TFile * froot
TString GetString(TTree *tree, int run, int lag, TString psfix)
Definition: Toolfun.hh:283
bool SetHistoryModify(bool Replace=true)
Definition: History.cc:453
static vector< waveSegment > sortSegments(vector< waveSegment > &ilist)
Definition: Toolbox.cc:185
static double getMDCShift(mdcshift mshift, double time)
Definition: Toolbox.cc:2296
static wavearray< double > getHilbertTransform(wavearray< double > x)
Definition: Toolbox.cc:6895
double dt
virtual void FFTW(int=1)
Definition: wavearray.cc:896
#define OTIME_LENGHT
FILE * fP
static slag getSlag(vector< slag > slagList, int jobid)
Definition: Toolbox.cc:1844
regression r
Definition: Regression_H1.C:44
static int shiftBurstMDCLog(std::vector< std::string > &mdcList, vector< TString > ifos, double mdc_shift)
Definition: Toolbox.cc:2174
vector< waveSegment > detSegs
Definition: cwb_net.C:193
static void dumpJobList(vector< waveSegment > ilist, TString fName, double segLen=600., double segMLS=300., double segEdge=8.)
Definition: Toolbox.cc:625
s s
Definition: cwb_net.C:155
static void setSlagShifts(slag SLAG, vector< TString > ifos, double segLen, int nDQF, dqfile *DQF)
Definition: Toolbox.cc:2017
float irho
int index
Definition: network.hh:54
char options[256]
TString frName[NIFO_MAX]
int nifo
static vector< slag > getSlagList(size_t nIFO, size_t slagSize, int slagSegs, int slagOff, size_t slagMin, size_t slagMax, size_t *slagSite, char *slagFile=NULL)
Definition: Toolbox.cc:809
static void dumpSlagList(vector< slag > slagList, TString slagFile, bool slagOnly=false)
Definition: Toolbox.cc:1087
static vector< waveSegment > readSegList(dqfile DQF)
Definition: Toolbox.cc:409
static wavearray< double > GetDetectorPSD(TString fName, double fWidth=8192., double dFreq=1.)
Definition: Toolbox.cc:5770
static int setVeto(TString ifName, TString idir, TString odir, vector< TString > ifos, int nVDQF, dqfile *VDQF, int nDQF, dqfile *DQF, double segLen, double segMLS, double segEdge)
Definition: Toolbox.cc:4208
vector< waveSegment > iseg
double gps
Double_t * time
beam pattern coefficients for hx
Definition: netevent.hh:94
double T
Definition: testWDM_4.C:11
char answer[256]
static vector< int > getCondorJobList(TString condor_dir, TString label)
Definition: Toolbox.cc:1398
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
condor_log_dir ReplaceAll("X_HOME", uhome.Data())
ifstream in
bool inclusive
Definition: cwb_setifar.C:157
cout<< "Starting reading output directory ..."<< endl;vector< TString > fileList
static int GetStepFunction(TString fName, vector< double > &x, vector< double > &y, vector< double > &ex=DEFAULT_DOUBLE_VECTOR, vector< double > &ey=DEFAULT_DOUBLE_VECTOR)
Definition: Toolbox.cc:6429
wavearray< int > index
Definition: Meyer.hh:36
int jobmin
virtual void stop(double s)
Definition: wavearray.hh:139
vector< int > slagId
Definition: Toolbox.hh:101
#define MAX_THREADS
Definition: Toolbox.cc:34
double fabs(const Complex &x)
Definition: numpy.cc:55
TString dir_name[NDIR]
Definition: cwb_mkdir.C:76
static TString DAG2LSF(char *dagFile, char *data_label, char *nodedir, char *data_dir, char *condor_dir, char *log_dir, char *output_dir, char *work_dir)
Definition: Toolbox.cc:1561
static TString SetMDCLog(TString log, int pos, TString val)
Definition: Toolbox.cc:2238
static void mkDir(TString dir, bool question=false, bool remove=true)
Definition: Toolbox.cc:4714
TBranch * branch
void resample(const wavearray< DataType_t > &, double, int=6)
Definition: wavearray.cc:503
int slagID
Definition: cwb_net.C:195
vector< TString > fList
Definition: LoopChirpMass.C:28
static vector< waveSegment > getSegList(int jobId, int nIFO, double segLen, double segMLS, double segEdge, vector< waveSegment > dqList)
Definition: Toolbox.cc:2799
char cmd[1024]
int estat
void AddLog(char *Stage, char *Log, TDatime *Time=NULL)
Definition: History.cc:187
cout<< "Fixed live file : "<< fix_liv_file_name.Data()<< endl;TFile *flive_fix=new TFile(fix_liv_file_name,"RECREATE");if(flive_fix->IsZombie()) { cout<< "CWB::Toolbox::setVeto - Error opening file "<< fix_liv_file_name.Data()<< endl;exit(1);} TTree *tlive_fix=(TTree *) tlive-> CloneTree(0)
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:246
strcpy(RunLabel, RUN_LABEL)
int cnt
Definition: Toolbox.hh:99
cout<< "total cat1 livetime : "<< int(cat1_time)<< " sec "<< cat1_time/3600.<< " h "<< cat1_time/86400.<< " day"<< endl;cout<< endl;vector< waveSegment > cat2List
Definition: cwb_dump_job.C:40
char ilstfname[1024]
double df
int nfactor
Definition: test_config1.C:83
virtual DataType_t max() const
Definition: wavearray.cc:1334
double omega
Definition: testWDM_4.C:12
static vector< waveSegment > readSegments(TString ifile)
Definition: Toolbox.cc:74
static void makeSpectrum(wavearray< double > &psd, wavearray< double > x, double chuncklen=8, double scratchlen=0, bool oneside=true)
Definition: Toolbox.cc:5489
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
TTree * itree
static TString getFileName(FILE *fp)
Definition: Toolbox.cc:6780
bool overwrite
Definition: cwb_dump_inj.C:100
static void getSineFittingParams(double a, double b, double c, double rate, double &amplitude, double &omega, double &phase)
Definition: Toolbox.cc:7065
static vector< waveSegment > mergeSegLists(vector< waveSegment > &ilist1, vector< waveSegment > &ilist2)
Definition: Toolbox.cc:350
char * AddType(char *TypeName)
Definition: History.cc:505
TString cwb_uparameters_file
flive_fix Close()
wavearray< double > Wlag[NIFO_MAX+1]
slagFile
Definition: cwb_tune_slag.C:25
DataType_t * data
Definition: wavearray.hh:319
static TString getTemporaryFileName(TString label="temporary", TString extension="tmp")
Definition: Toolbox.cc:6653
int jobId
char nodedir[1024]
Definition: test_config1.C:187
Long_t id
double detSegs_ctime
Definition: cwb_net.C:303
char condor_dir[512]
Definition: test_config1.C:148
double dFreq
Definition: DrawPSD.C:17
char work_dir[512]
Definition: test_config1.C:143
int cat
int segID[20]
Definition: cwb_net.C:195
static wavearray< double > getHilbertEnvelope(wavearray< double > x)
Definition: Toolbox.cc:6915
char formula[256]
static bool isFileExisting(TString fName)
Definition: Toolbox.cc:4651
int slagMin
Definition: test_config1.C:66
bool save
double ctime
char * GetHistory(char *StageName, char *Type)
Definition: History.cc:273
char line[1024]
string version
Definition: cWB_conf.py:108
const CWB::HistoryStage * GetStage(char *Name)
Definition: History.cc:694
static wavearray< double > GetPhase(wavearray< double > hi, wavearray< double > hr, wavearray< double > &fi, wavearray< double > &fr, wavearray< double > &s, wavearray< double > &tt)
Definition: Toolbox.cc:7162
simulation
Definition: cwb_eced.C:26
fclose(ftrig)
static Int_t getTableau10BlindColor(Int_t index)
Definition: Toolbox.cc:7275
#define LOW_CUT_FREQ
factors[0]
Definition: cwb_eced.C:27
double shift[NIFO_MAX]
char fName[256]
for(int i=0;i< 101;++i) Cos2[2][i]=0
pointers to detectors
virtual void resize(unsigned int)
Definition: wavearray.cc:463
int check
char output_dir[512]
Definition: test_config1.C:146
double length
Definition: TestBandPass.C:18
void Inverse(int n=-1)
param: n - number of steps (-1 means full reconstruction)
Definition: wseries.cc:291
wavearray< double > y
Definition: Test10.C:31
#define YEAR
double stop
Definition: network.hh:56
void putLayer(wavearray< DataType_t > &, double n)
param: n - layer number
Definition: wseries.cc:219
CWB::History * ihistory
TString frLabel[NIFO_MAX]
float ilag
i drho pp_irho
static void blackmanharris(double *window, int n)
Definition: Toolbox.cc:323
int nsel
Definition: cwb_setcuts.C:237
void AddHistory(char *Stage, char *Type, char *History, TDatime *Time=NULL)
Definition: History.cc:225
TB checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"))
#define SEARCH(TYPE)
Definition: xroot.hh:4
CWB_STAGE
Definition: cwb.hh:122
char * AddStage(char *StageName)
Definition: History.cc:464
TString ifos[60]
#define FREQ_RES
exit(0)
TB dumpSegList(dq1List, dq1ListFile, false)
static TString addCWBFlags(TMacro macro, TString ofname="")
Definition: Toolbox.cc:4830
#define CCAST(PAR)
Definition: Toolbox.cc:25
wavearray< double > Wslag[NIFO_MAX+1]
TString treeName
Definition: MergeTrees.C:15