Logo coherent WaveBurst  
Config Reference Guide
Logo
MergeSegments.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 //
20 // Merge Segments
21 // this macro read a list of start stop segments from a file
22 // and merge the overlapping segments
23 // Example :
24 // root -b -l 'MergeSegments.C("ifile.txt","ofile.txt")'
25 //
26 // Author : Gabriele Vedovato
27 
28 
29 void MergeSegments(TString ifile, TString ofile, bool bexit=true) {
30 
31  // --------------------------------------------------------------
32  // Open file segment list
33  // --------------------------------------------------------------
34  ifstream in;
35  in.open(ifile.Data(),ios::in);
36  if (!in.good()) {cout << "Error Opening File : " << ifile << endl;gSystem->Exit(1);}
37  cout << "input file list : " << ifile << endl;
38 
39  char str[1024];
40  int fpos=0;
41  int index=0;
42  double start;
43  double stop;
44  waveSegment seg;
45  vector<waveSegment> iseg;
46  while (1) {
47  fpos=in.tellg();
48  in.getline(str,1024);
49  if(str[0] == '#') continue;
50  in.seekg(fpos, ios::beg);
51  fpos=in.tellg();
52 
53  in >> start >> stop;
54  if (!in.good()) break;
55 
56  seg.index=index++; seg.start=start; seg.stop=stop; iseg.push_back(seg);
57  }
58 
59  in.close();
60 
61 // cout << "List of input segments" << endl;
62 // for(int n=0;n<iseg.size();n++) cout << n << " " << iseg[n].start << " " << iseg[n].stop << endl;
63 
64  // --------------------------------------------------------------
65  // merge segments
66  // --------------------------------------------------------------
67  vector<waveSegment> oseg = CWB::Toolbox::unionSegments(iseg);
68 
69 // cout << "List of output segments" << endl;
70 // for(int n=0;n<oseg.size();n++) cout << n << " " << oseg[n].start << " " << oseg[n].stop << endl;
71 
72  // --------------------------------------------------------------
73  // Write output file segment list
74  // --------------------------------------------------------------
75  ofstream out;
76  out.open(ofile.Data(),ios::out);
77  out.precision(16);
78  for(int n=0;n<oseg.size();n++) {
79  out << oseg[n].start << " " << oseg[n].stop << endl;
80  }
81  out.close();
82 
83  cout << "output file list : " << ofile << endl;
84 
85  if(bexit) exit(0);
86 }
87 
void MergeSegments(TString ifile, TString ofile, bool bexit=true)
Definition: MergeSegments.C:29
shift breaksw case n
Definition: cwb_clchunk.csh:75