Logo coherent WaveBurst  
Library Reference Guide
Logo
frame.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 
19 #include "frame.hh"
20 #include "Meyer.hh"
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 /* BEGIN_HTML
24 The frame class is a partial c++ wrapper to the c frame library<br>
25 framelib : (http://lappweb.in2p3.fr/virgo/FrameL/)<br>
26 it is designed to implements only the functions used by cWB<br>
27 <br>
28 the following are some examples which illustrate how to use the class
29 
30 <h3><a name="example1">Example 1</a></h3>
31 This example shows how to read a frame from frame file list<br>
32 using the frfile structure
33 <br><br>
34 <pre>
35  root[] #define FRLIST_NAME "input/H1_LDAS_C02_L2.frl" // frame files list name
36  root[] #define CHANNEL_NAME "H1:LDAS-STRAIN" // channel to be read
37  root[] double start = 942449664; // start time
38  root[] double stop = start+600; // stop time
39  root[] wavearray<double> x; // array
40  root[] CWB::frame fr(FRLIST_NAME); // open frame file list
41  root[] int nfiles=fr.getNfiles(); // get number of files in frame list
42  root[] cout << "nfiles : " << nfiles << endl;
43  root[] frfile FRF = fr.getFrList(start, stop, 0); // fill the frfile structure
44  root[] fr.readFrames(FRF,CHANNEL_NAME,x); // read data in x array
45 </pre>
46 <h3><a name="example2">Example 2</a></h3>
47 This example shows how to read a frame from frame file list using the read operator ">>"
48 <br><br>
49 <pre>
50  root[] #define FRLIST_NAME "input/H1_LDAS_C02_L2.frl" // frame files list name
51  root[] #define CHANNEL_NAME "H1:LDAS-STRAIN" // channel to be read
52  root[] wavearray<double> x; // array
53  root[] x.start(942449664); // start time
54  root[] x.stop(x.start()+600); // stop time
55  root[] frame ifr(FRLIST_NAME,CHANNEL_NAME,"READ"); // open frame file list
56  root[] ifr >> x; // read data in x array
57  root[] cout << x.start() << " " << x.rate() << " " << x.size() << endl; // print data info
58 </pre>
59 <h3><a name="example3">Example 3</a></h3>
60 This example shows how to write data to frame file using the write operator "<<"
61 <br><br>
62 <pre>
63  root[] #define CHANNEL_NAME "H1:LDAS-STRAIN" // channel to be read
64  root[] #define FRNAME "TEST" // output frame name
65  root[] wavearray<double> x; // array
66  root[] x.start(942449664); // set start time
67  root[] x.stop(x.start()+600); // set stop time
68  root[] x.rate(16384); // set rate (Hz)
69  root[] for(int i=0;i&lt;x.size();i++) x[i]=i; // fill array
70  root[] frame ofr("TEST.gwf","CHNAME","WRITE"); // open output frame file
71  root[] ofr.setFrName(FRNAME); // set frame name
72  root[] ofr << x; // read data in x array
73  root[] ofr.close(); // close output frame file
74 </pre>
75 <h3><a name="example4">Example 4</a></h3>
76 This example shows how to resample read data
77 <br><br>
78 <pre>
79  root[] #define FRLIST_NAME "input/H1_LDAS_C02_L2.frl" // frame files list name
80  root[] #define CHANNEL_NAME "H1:LDAS-STRAIN" // channel to be read
81  root[] wavearray<double> x; // array
82  root[] x.start(942449664); // start time
83  root[] x.stop(x.start()+600); // stop time
84  root[] frame ifr(FRLIST_NAME,CHANNEL_NAME,"READ"); // open frame file list
85  root[] ifr.setSRIndex(10); // read data are resampled @ 1024 Hz
86  root[] ifr >> x; // read data in x array
87  root[] cout << x.start() << " " << x.rate() << " " << x.size() << endl; // print data info
88 </pre>
89 
90 
91 END_HTML */
92 ////////////////////////////////////////////////////////////////////////////////
93 
94 ClassImp(CWB::frame)
95 
96 //______________________________________________________________________________
97 CWB::frame::frame() : frtree_List(NULL), chName(""), frName("Frame"),
98  rfName(""), frFile(NULL), fOption(""), srIndex(-1),
99  verbose(false), frRetryTime(60), xstart(0.), xstop(0.) {
100 // default constructor
101 // initialize only the frame class parameters
102 //
103 }
104 
105 //______________________________________________________________________________
106 CWB::frame::frame(TString ioFile, TString chName, Option_t* option, bool onDisk, TString label, unsigned int mode) :
107  frtree_List(NULL), chName(""), frName("Frame"), rfName(""),
108  frFile(NULL), fOption(""), srIndex(-1), verbose(false), frRetryTime(60), xstart(0.), xstop(0.) {
109 //
110 // initialize frame class parameters & open frame file list
111 //
112 // see the 'CWB::frame::open' method description
113 //
114  open(ioFile, chName, option, onDisk, label, mode);
115 }
116 
117 //______________________________________________________________________________
119 //
120 // destructor
121 //
122 
123  // remove temporary root file
124  if(rfName!="") gSystem->Exec(TString("rm "+rfName).Data());
125  close();
126 }
127 
128 //______________________________________________________________________________
130 //
131 // operator : read frame into wavearray x
132 //
133 
134  fr.readFrames(x);
135  return x;
136 }
137 
138 //______________________________________________________________________________
139 CWB::frame& operator << (CWB::frame& fr, wavearray<double>& x) {
140 //
141 // operator : write wavearray x to frame
142 //
143 
144  if(fr.getOption()=="READ") {
145  cout << "CWB::frame::operator(<<) : Not allowed in READ mode" << endl;
146  exit(1);
147  }
148 
149  if(fr.getChName()=="") {
150  cout << "CWB::frame::operator(<<) : channel not defined";
151  exit(1);
152  }
153 
154  if(fr.getFrName()=="") {
155  cout << "CWB::frame::operator(<<) : frame name not defined" << endl;
156  exit(1);
157  }
158 
160  return fr;
161 }
162 
163 //______________________________________________________________________________
165 //
166 // operator : write wavearray x to frame
167 //
168 
169  fr << x;
170  return fr;
171 }
172 
173 //______________________________________________________________________________
174 void
176 //
177 // write in frame the data contained in the wavearray x
178 // frame is saved in the frFile (object must be opened in write mode)
179 //
180 // x : in - data array which contains the data
181 // chName : in - name of channel to be extracted
182 // frName : in - frame name
183 //
184 
185  if (frFile==NULL) {
186  cout << "CWB::frame::writeFrame : output frame file close" << endl;
187  exit(1);
188  }
189  if(x.rate()<=0) {
190  cout << "CWB::frame::writeFrame : input rate must be > 0" << endl;
191  exit(1);
192  }
193  if(x.size()==0) {
194  cout << "CWB::frame::writeFrame : input size must be > 0" << endl;
195  exit(1);
196  }
197 
198  /*----------------------- Create a new frame ---------*/
199  FrameH* frFrame = FrameNew(const_cast<char*>(frName.Data()));
200  frFrame->frame = 0;
201  frFrame->run = -1;
202  frFrame->dt = x.size()/x.rate();
203  frFrame->GTimeS = x.start();
204  frFrame->GTimeN = 0;
205 
206  cout << "Size (sec) " << x.size()/x.rate() << endl;
207  FrProcData* proc = FrProcDataNew(frFrame,const_cast<char*>(chName.Data()),x.rate(),x.size(),-64);
208  if(proc == NULL) {
209  cout << "CWB::frame::writeFrame : Cannot create FrProcData" << endl;
210  exit(1);
211  }
212  proc->timeOffset = 0;
213  proc->tRange = frFrame->dt;
214  proc->type = 1; // Time Serie
215 
216  for (int i=0;i<(int)proc->data->nData;i++) proc->data->dataD[i] = x[i];
217 
218  int err=FrameWrite(frFrame,frFile);
219  if (err) {
220  cout << "CWB::frame::writeFrame : Error writing frame" << endl;
221  exit(1);
222  }
223  FrameFree(frFrame);
224 
225  return;
226 }
227 
228 //______________________________________________________________________________
229 void
230 CWB::frame::open(TString ioFile, TString chName, Option_t* option, bool onDisk, TString label, unsigned int mode) {
231 //
232 // open frame file in read or write mode
233 //
234 // ioFile : in - if option="READ" -> ioFile = input frame file list name
235 // out - if option="WRITE" -> ioFile = output frame file name
236 // chName : in - name of the data channel (def="")
237 // option : in - READ/WRITE mode (def="")
238 // onDisk : in - true -> tmp tree on root file
239 // false (def) -> tmp tree is stored in ram
240 // true must be used only if the input file frame list is huge
241 // label : in - this is a string used to select files listed in the input frame file list
242 // def = ".gwf"
243 // mode : in - if mode=0 (default) the frame file list is a list of frame file paths
244 //
245 // has the following format :
246 // DIR_NAME/XXXXX-GPS-LEN.gwf where :
247 // DIR_NAME : is the directory which contains the frame file.
248 // The strings 'file://localhost' or 'framefile=' at the
249 // beginning of the path will be automatically removed
250 // GPS : is the start frame time in sec
251 // LEN : is the frame length time in sec
252 //
253 // if mode=1 the frame file list is a list of line with the following format :
254 //
255 // FILE_PATH GPS LEN
256 //
257 
258  if(rfName!="") gSystem->Exec(TString("rm "+rfName).Data());
259  close();
260 
261  this->chName=chName;
262  this->frName="Frame";
263 
264  // if onDisk store tree on root file otherwise it is stored in ram
265  if(onDisk) {
266  // create temporary file
267  gRandom->SetSeed(0);
268  int rnID = int(gRandom->Rndm(13)*1.e9);
269  UserGroup_t* uinfo = gSystem->GetUserInfo();
270  TString uname = uinfo->fUser;
271  // create temporary dir
272  gSystem->Exec(TString("mkdir -p /dev/shm/")+uname);
273  char fName[1024]="";
274  sprintf(fName,"/dev/shm/%s/cwbframe_%d.root",uname.Data(),rnID);
275  rfName=fName;
276  }
277 
278  fOption = option;
279  fOption.ToUpper();
280  if((fOption!="READ")&&(fOption!="WRITE")) fOption="READ";
281 
282  if(fOption=="READ") nfiles = frl2FrTree(ioFile, rfName, label, mode);
283  if(fOption=="WRITE") {
284  if(!fNameCheck(ioFile)) exit(1);
285  frFile = FrFileONew(const_cast<char*>(ioFile.Data()),1); // gzip compression
286  if(frFile==NULL) {
287  cout << "CWB::frame::frame : Error opening file " << ioFile.Data() << endl;
288  exit(1);
289  }
290  }
291  return;
292 }
293 
294 //______________________________________________________________________________
295 void
297 //
298 // close frame file and reset parameters
299 //
300 
301  if(frtree_List!=NULL) frtree_List->Reset();
302  frtree_List=NULL;
303  if (frFile!=NULL) FrFileOEnd(frFile);
304  frFile=NULL;
305  fOption="";
306  chName="";
307  frName="";
308 }
309 
310 //______________________________________________________________________________
311 int
313 //
314 // convert input frame file list to a tree
315 // note : this method can be used only in READ mode
316 //
317 // iFile : in - input frame file list name
318 // rfName : in - name of auxiliary file name used to store the temporary tree
319 // if rfName=="" the tree is saved & sorted to the private frtree_List
320 // label : in - this is a string used to select files listed in the input frame file list
321 // def = ".gwf"
322 // mode : in - 0/1 (def=0)
323 //
324 
325  if(fOption!="READ") {
326  cout << "CWB::frame::frl2FrTree : allowed only in READ mode" << endl;
327  exit(1);
328  }
329 
330  TFile* ofile = NULL;
331  if(rfName.Sizeof()>1) ofile=new TFile(rfName.Data(),"RECREATE");
332 
333  TTree* frtree = new TTree("frl","frl");
334  double gps;
335  double length;
336  double start;
337  double stop;
338  char atlas_path[1024];
339  frtree->Branch("gps",&gps,"gps/D");
340  frtree->Branch("length",&length,"length/D");
341  frtree->Branch("start",&start,"start/D");
342  frtree->Branch("stop",&stop,"stop/D");
343  frtree->Branch("path",atlas_path,"path/C");
344 
345  if(rfName.Sizeof()>1) frtree->SetDirectory(ofile);
346  else frtree->SetDirectory(0);
347 
348  char ifile_name[1024];
349  sprintf(ifile_name,"%s",iFile.Data());
350  cout << "CWB::frame::frl2FrTree - " << ifile_name << endl;
351  FILE *in;
352  char s[512];
353  char f[512];
354  TString idir_name;
355  cout.precision(14);
356  int cnt=0;
357  // read frame file list
358  if( (in=fopen(ifile_name,"r"))!=NULL ) {
359  while(fgets(s,512,in) != NULL) {
360  sprintf(f,"%s",s);
361  //cout << f << endl;
362  TString line(f);
363  // remove header created with gw_data_find
364  line.ReplaceAll("framefile=","");
365  line.ReplaceAll("file://localhost","");
366  line.ReplaceAll("gsiftp://ldr.aei.uni-hannover.de:15000","");
367  //cout << line.Data() << endl;
368  if (line.Contains(label)) {
369  // select the first token of the string
370  TObjArray* token0 = TString(line).Tokenize(TString(" "));
371  TObjString* sline = (TObjString*)token0->At(0);
372  TString path = sline->GetString().Data();
373 
374  //cout << f << endl;
375  sprintf(atlas_path,"%s",path.Data());
376 
377  if(mode==0) {
378  TObjArray* token1 = TString(path).Tokenize(TString("/"));
379  TObjString* path_tok1 = (TObjString*)token1->At(token1->GetEntries()-1);
380  //cout << path_tok1->GetString().Data() << endl;
381 
382  TObjArray* token2 = TString(path_tok1->GetString()).Tokenize(TString("-"));
383  TObjString* gps_tok = (TObjString*)token2->At(token2->GetEntries()-2);
384  TString sgps = gps_tok->GetString().Data();
385  //cout << sgps.Data() << endl;
386  gps = sgps.Atof();
387 
388  TObjString* length_tok = (TObjString*)token2->At(token2->GetEntries()-1);
389  TString slength = length_tok->GetString().Data();
390  slength.ReplaceAll(label,"");
391  //cout << slength.Data() << endl;
392  length = slength.Atof();
393 
394  delete token1;
395  delete token2;
396  } else {
397  TObjArray* token1 = TString(line).Tokenize(TString(" "));
398 
399  if(token1->GetEntries()<3) {
400  cout << "CWB::frame::frl2FrTree : bad format " << ifile_name << endl;
401  exit(1);
402  }
403 
404  TObjString* gps_tok = (TObjString*)token1->At(1);
405  TString sgps = gps_tok->GetString().Data();
406  //cout << sgps.Data() << endl;
407  gps = sgps.Atof();
408 
409  TObjString* length_tok = (TObjString*)token1->At(2);
410  TString slength = length_tok->GetString().Data();
411  //cout << slength.Data() << endl;
412  length = slength.Atof();
413 
414  delete token1;
415  }
416  delete token0;
417 
418  start = gps;
419 
420  stop = start+length;
421 
422  // skip file if it is out of time range
423  if(xstart && stop<xstart) continue;
424  if(xstop && start>xstop) continue;
425 
426  frtree->Fill();
427 
428  //cout << "Frame info : " << gps << " " << atlas_path << " " << length << " " << start << " " << stop << endl;
429  //if (++cnt%100==0) cout << cnt << endl;
430  ++cnt;
431  }
432  }
433  } else {
434  cout << "CWB::frame::frl2FrTree : Error opening file " << ifile_name << endl;
435  exit(1);
436  }
437 
438  if(ofile!=NULL) {
439  frtree->Write();
440  ofile->Close();
441  } else { // copy tree to internal frtree
442  if(frtree_List!=NULL) delete frtree_List;
443  frtree_List = (TTree*)frtree->CloneTree();
444  sortFrTree();
445  delete frtree;
446  }
447  return cnt;
448 }
449 
450 //______________________________________________________________________________
451 int
453 //
454 // sort the tree contained in iFile according the GPS time and save it to the auxiliary file rfName
455 // note : this method can be used only in READ mode
456 //
457 // iFile : in - name of input root file containing tree to be sorted
458 // rfName : in - name of auxiliary file name used to store the temporary tree
459 // if rfName=="" the tree is saved & sorted to the private frtree_List
460 //
461 
462  if(fOption!="READ") {
463  cout << "CWB::frame::sortFrTree : allowed only in READ mode" << endl;
464  exit(1);
465  }
466  TFile f(iFile.Data());
467  TTree *tree = (TTree*)f.Get(LST_TREE_NAME);
468  Int_t nentries = (Int_t)tree->GetEntries();
469  tree->Draw("gps","","goff");
470  Int_t *index = new Int_t[nentries];
471  TMath::Sort(nentries,tree->GetV1(),index,false);
472 
473  TFile f2(rfName.Data(),"recreate");
474  TTree *tsorted = (TTree*)tree->CloneTree(0);
475  for (Int_t i=0;i<nentries;i++) {
476  //if (i%1000==0) cout << i << endl;
477  tree->GetEntry(index[i]);
478  tsorted->Fill();
479  }
480  tsorted->Write();
481  f2.Close();
482  delete [] index;
483 
484  return nentries;
485 }
486 
487 //______________________________________________________________________________
488 int
490 //
491 // sort the the auxiliary tree according the GPS time
492 // note : this method can be used only in READ mode
493 //
494 
495  if(fOption!="READ") {
496  cout << "CWB::frame::sortFrTree : allowed only in READ mode" << endl;
497  exit(1);
498  }
499  Int_t nentries = (Int_t)frtree_List->GetEntries();
500  frtree_List->Draw("gps","","goff");
501  double* gps = frtree_List->GetV1();
502  Int_t *index = new Int_t[nentries];
503  TMath::Sort(nentries,gps,index,false);
504 
505  double pgps = -1;
506  TTree *sorted_frtree_List = (TTree*)frtree_List->CloneTree(0);
507  for (Int_t i=0;i<nentries;i++) {
508  //if (i%1000==0) cout << i << endl;
509  int j = index[i]; // j = sorted index
510  if(gps[j]==pgps) continue; // skip duplicated entries
511  pgps=gps[j];
512  frtree_List->GetEntry(j);
513  sorted_frtree_List->Fill();
514  }
515  delete [] index;
516 
517  // copy sorted tree to the original not sorted tree
518  delete frtree_List;
519  frtree_List = (TTree*)sorted_frtree_List->CloneTree();
520  delete sorted_frtree_List;
521 
522  return nentries;
523 }
524 
525 //______________________________________________________________________________
526 frfile
527 CWB::frame::getFrList(int istart, int istop, int segEdge) {
528 //
529 // return the list of frfile structures of the frame files
530 // cointained in the range [istart,istop]
531 //
532 // istart : in - start time sec
533 // istop : in - stop time sec
534 // segEdge : in - wavelet boundary offset [sec]
535 //
536 
537  frfile frf;
538  if(rfName!="") {
539  frf = getFrList(rfName, istart, istop, segEdge);
540  } else {
541  frf = getFrList(istart, istop, segEdge, NULL);
542  }
543  return frf;
544 }
545 
546 //______________________________________________________________________________
547 frfile
548 CWB::frame::getFrList(TString rfName, int istart, int istop, int segEdge) {
549 //
550 // return the frame list of frames contained in the range [start-segEdge,stop-segEdge]
551 // note : this method can be used only in READ mode
552 //
553 // rfName : in - name of auxiliary file name used to store the temporary tree
554 // use the tree contained in rfName root file
555 // istart : in - start time sec
556 // istop : in - stop time sec
557 // segEdge : in - wavelet boundary offset [sec]
558 //
559 
560  if(fOption!="READ") {
561  cout << "CWB::frame::getFrList : allowed only in READ mode" << endl;
562  exit(1);
563  }
564 
565  frfile frf;
566 
567  TFile *ifile = TFile::Open(rfName.Data());
568  if (ifile==NULL) {cout << "No " << rfName.Data() << " file !!!" << endl;exit(1);}
569  TTree* itree = (TTree *) gROOT->FindObject(LST_TREE_NAME);
570  if (itree==NULL) {cout << "No ffl tree name match !!!" << endl;exit(1);}
571  frf=getFrList(istart, istop, segEdge, itree);
572  delete itree;
573  delete ifile;
574 
575  return frf;
576 }
577 
578 //______________________________________________________________________________
579 vector<frfile>
580 CWB::frame::getFrList(int istart, int istop) {
581 //
582 // return the list of frfile structures of the frame files
583 // cointained in the range [istart,istop]
584 // note : this method can be used only in READ mode
585 //
586 // istart : in - start time sec
587 // istop : in - stop time sec
588 //
589 
590  if(fOption!="READ") {
591  cout << "CWB::frame::getFrList : allowed only in READ mode" << endl;
592  exit(1);
593  }
594 
595  vector<frfile> frlist;
596  frfile frf;
597 
598  TTree* itree=frtree_List;
599  if(itree==NULL) {cout << "CWB::frame::getFrList - Error : No tree defined !!!" << endl;exit(1);}
600 
601  int size = itree->GetEntries();
602  //cout << size << endl;
603 
604  double gps;
605  double length;
606  char path[1024];
607  itree->SetBranchAddress("gps",&gps);
608  itree->SetBranchAddress("length",&length);
609  itree->SetBranchAddress("path",path);
610 
611  double start = istart!=0 ? istart : 0;
612  double stop = istop!=0 ? istop : 2000000000;
613  char cut[1024];
614  sprintf(cut,"(gps>=%f && gps<=%f)", start,stop);
615  //cout << cut << endl;
616  itree->SetEstimate(size);
617  itree->Draw("Entry$",cut,"goff");
618  int isel = itree->GetSelectedRows();
619  double* entry = itree->GetV1();
620 
621  for (int i=0;i<isel;i++) {
622  itree->GetEntry(i);
623  frf.start=gps;
624  frf.stop=gps+length;
625  frf.length=length;
626  frf.file.push_back(path);
627  frlist.push_back(frf);
628  frf.file.clear();
629  }
630 
631  return frlist;
632 }
633 
634 //______________________________________________________________________________
635 frfile
636 CWB::frame::getFrList(int istart, int istop, int segEdge, TTree* itree) {
637 //
638 // return the frame list of frames contained in the range [start-segEdge,stop-segEdge]
639 // note : this method can be used only in READ mode
640 //
641 // istart : in - start time sec
642 // istop : in - stop time sec
643 // segEdge : in - wavelet boundary offset [sec]
644 // itree : in - if itree=NULL use the private frtree_List
645 //
646 
647  if(fOption!="READ") {
648  cout << "CWB::frame::getFrList : allowed only in READ mode" << endl;
649  exit(1);
650  }
651 
652  frfile frf;
653 
654  if(itree==NULL) itree=frtree_List;
655  if(itree==NULL) {cout << "CWB::frame::getFrList - Error : No tree defined !!!" << endl;exit(1);}
656 
657  int size = itree->GetEntries();
658  //cout << size << endl;
659 
660  double gps;
661  double length;
662  char path[1024];
663  itree->SetBranchAddress("gps",&gps);
664  itree->SetBranchAddress("length",&length);
665  itree->SetBranchAddress("path",path);
666 
667  double start=istart-segEdge;
668  double stop= istop+segEdge;
669  char cut[1024];
670  sprintf(cut,"((%f>=gps && %f<=gps+length) || (%f>=gps && %f<=gps+length) || (%f<=gps && %f>=gps+length))",
671  start,start,stop,stop,start,stop);
672  //cout << cut << endl;
673  itree->SetEstimate(size);
674  itree->Draw("Entry$",cut,"goff");
675  int isel = itree->GetSelectedRows();
676  double* entry = itree->GetV1();
677 
678 
679  // CHECK
680  if (isel==0) {
681  cout << endl;
682  cout << "CWB::frame::getFrList - Error : No files matched in the range "
683  << start << ":" << stop << " !!!" << endl << endl;
684  sprintf(cut,"gps>=%f && gps<=%f",start-500,stop+500);
685  cout << "Dump frame segments available in the input list in the range : " << cut << endl << endl;
686  itree->SetScanField(0);
687  itree->Scan("Entry$:gps:length",cut,"goff");
688  exit(1);
689  }
690  if (isel==1) {
691  itree->GetEntry(entry[0]);
692  if (start < gps) {
693  cout << endl;
694  cout << "CWB::frame::getFrList - Error : " << " No files matched !!!" << endl;
695  cout << "start buffer (" << start << ") < begin gps frame (" << gps << ")" << endl;
696  cout << "check input frame list" << endl << endl;
697  exit(1);
698  }
699  if (stop > gps+length) {
700  cout << endl;
701  cout << "CWB::frame::getFrList - Error : " << " No files matched !!!" << endl;
702  cout << "stop buffer (" << stop << ") > end gps frame (" << gps+length << ")" << endl;
703  cout << "check input frame list" << endl << endl;
704  exit(1);
705  }
706  }
707  if (isel>1) {
708  itree->GetEntry(entry[0]);
709  if (start < gps) {
710  cout << endl;
711  cout << "CWB::frame::getFrList - Error : " << " No files matched !!!" << endl;
712  cout << "start buffer (" << start << ") < begin gps frame (" << gps << ")" << endl;
713  cout << "check input frame list" << endl << endl;
714  exit(1);
715  }
716  itree->GetEntry(entry[isel-1]);
717  if (stop > gps+length) {
718  cout << endl;
719  cout << "CWB::frame::getFrList - Error : " << " No files matched !!!" << endl;
720  cout << "stop buffer (" << stop << ") > end gps frame (" << gps+length << ")" << endl;
721  cout << "check input frame list" << endl << endl;
722  exit(1);
723  }
724  // check if frames are contiguous
725  itree->GetEntry(entry[0]);
726  double stop0=gps+length;
727  for(int i=1;i<isel;i++) {
728  itree->GetEntry(entry[i]);
729  if(gps!=stop0) {
730  cout << "CWB::frame::getFrList - Error : " << " Requested frames are not contiguous !!!" << endl;
731  cout << "start : " << gps << " is != from the end of the previous segment " << stop0 << endl;
732  exit(1);
733  }
734  stop0=gps+length;
735  }
736  }
737 
738  itree->GetEntry(entry[isel-1]);
739 /*
740  cout << isel << endl;
741  cout << int(length) << endl;
742  cout << int(start) << endl;
743  cout << int(stop) << endl;
744 */
745  frf.start=start;
746  frf.stop=stop;
747  frf.length=length;
748  frf.file.clear();
749  for (int i=0;i<isel;i++) {
750  itree->GetEntry(entry[i]);
751  frf.file.push_back(path);
752  }
753 
754  return frf;
755 }
756 
757 //______________________________________________________________________________
760 //
761 // return the begin and end range of the frame list
762 // note : this method can be used only in READ mode
763 //
764 // itree : in - if itree=NULL use the private frtree_List
765 //
766 
767  if(fOption!="READ") {
768  cout << "CWB::frame::getFrRange : allowed only in READ mode" << endl;
769  exit(1);
770  }
771 
772  waveSegment SEG={0,0.,0.};
773 
774  if(itree==NULL) itree=frtree_List;
775  if(itree==NULL) {cout << "CWB::frame::getFrRange - No tree defined !!!" << endl;exit(1);}
776 
777  int size = itree->GetEntries();
778 
779  double gps;
780  double length;
781  itree->SetBranchAddress("gps",&gps);
782  itree->SetBranchAddress("length",&length);
783 
784  double min=4000000000.;
785  double max=0.;
786  for (int i=0;i<size;i++) {
787  itree->GetEntry(i);
788  if(min>gps) min=gps;
789  if(max<gps+length) max=gps+length;
790  }
791 
792  SEG.start=min;
793  SEG.stop=max;
794 
795  return SEG;
796 }
797 
798 //______________________________________________________________________________
799 void
801 //
802 // read in w the channel data contained in the frames
803 // the range of data to be read is defined in the wavearray object
804 // user must initialize the object with :
805 // - w.start(GPS)
806 // - w.stop(GPS+LEN)
807 //
808 // note : this method can be used only in READ mode
809 //
810 // w : out - data array which contains the data read
811 //
812 
813  if(getOption()!="READ") {
814  cout << "CWB::frame::readFrame : allowed only in READ mode" << endl;
815  exit(1);
816  }
817  if(getChName()=="") {
818  cout << "CWB::frame::readFrame : channel not defined" << endl;
819  exit(1);
820  }
821  frfile frf = getFrList(w.start(), w.stop(), 0);
822  readFrames(frf,const_cast<char*>(getChName().Data()),w);
823  return;
824 }
825 
826 //______________________________________________________________________________
827 void
828 CWB::frame::readFrames(char* filename, char *channel, wavearray<double> &w) {
829 //
830 // read in w the channel data contained in the frames defined in the filename
831 // this method is obsolete, was used by the wat-5.3.9 pipeline infrastructure
832 // the input file must contains the following informations :
833 //
834 // - nfiles // number of files to be read
835 // - frame length // dummy, not used anymore
836 // - start time // (sec)
837 // - stop time // (sec)
838 // - rate // dummy, not used anymore
839 // - frame file path // this field is repeated nfiles times
840 //
841 // note : this method can be used only in READ mode
842 //
843 // filename : in - name of frame file
844 // channel : in - name of channel to be extracted
845 // w : out - data array which contains the data read
846 //
847 
848  if(fOption!="READ") {
849  cout << "CWB::frame::readFrames : allowed only in READ mode" << endl;
850  exit(1);
851  }
852 
853  int buffersize=1024;
854  char *buffer=new char[buffersize];
855 
856  double rf_start, rf_stop;
857  double frlen;
858  int nframes;
859  double rf_rate;
860 
861  vector<frfile> frlist;
862  frfile frf;
863 
864  FILE *framelist=fopen(filename,"r");
865 
866  fgets(buffer,buffersize,framelist);
867  nframes=(int)atoi(buffer);
868 
869  if(nframes<=0) {
870  fprintf(stderr,"nframes=%d\n",nframes);
871  exit(1);
872  }
873 
874  fgets(buffer,buffersize,framelist);
875  frlen=(double)atof(buffer);//dummy, not used anymore
876  fgets(buffer,buffersize,framelist);
877  rf_start=(double)atof(buffer);
878  fgets(buffer,buffersize,framelist);
879  rf_stop=(double)atof(buffer);
880  fgets(buffer,buffersize,framelist);
881  rf_rate=(double)atof(buffer);//dummy, not used anymore
882 
883  if(verbose) {
884  fprintf(stdout,"-------------\n");
885  fprintf(stdout,"filename=%s channel=%s nframes=%d frlen=%f start=%f stop=%f rate=%f \n",
886  filename, channel, nframes, frlen, rf_start, rf_stop, rf_rate);
887  }
888 
889  frf.start=rf_start;
890  frf.stop=rf_stop;
891  frf.length=frlen;
892  for(int i=0;i<nframes;i++) {
893  fgets(buffer,buffersize,framelist);
894  if(verbose) fprintf(stdout,"framefile=%s\n",buffer);
895  frf.file.push_back(TString(buffer));
896  }
897  delete [] buffer;
898  fclose(framelist);
899 
900  readFrames(frf, channel, w);
901 
902  return;
903 }
904 
905 //______________________________________________________________________________
906 void
908 //
909 // read in w the channel data contained in the frames defined in the frf structure
910 // the range of data to be read is defined in the frf structure
911 //
912 // note : this method can be used only in READ mode
913 //
914 // frf : in - frame file structure
915 // channel : in - name of channel to be extracted
916 // w : out - data array which contains the data read
917 //
918 
919  if(fOption!="READ") {
920  cout << "CWB::frame::readFrames : allowed only in READ mode" << endl;
921  exit(1);
922  }
923 
924  FrVect *v;
925  FrFile *ifile;
926 
927  double rf_start, rf_stop, fstart, fend;
928  double frlen;
929  int nframes;
930  double rf_rate;
931  unsigned int wcounter=0;
932  double rf_time=0;
933  double rf_step=-1.0;
934 
935  nframes=frf.file.size();
936  rf_start=frf.start;
937  rf_stop=frf.stop;
938  frlen=frf.length;
939 
940  // BIG FRAMES : when frlen>8000 (Virgo VSR1-V2) FrFileIGetV crash (GV)
941  bool bigFrame = false;
942  if (frlen>1024) {
943  if(verbose) cout << "CWB::frame::readFrames - bigFrame !!! : " << frlen << endl;
944  bigFrame = true;
945  }
946 
947  double fend0=0;
948  for(int i=0;i<nframes;i++) {
949 
950  if(verbose) fprintf(stdout,"CWB::frame::readFrames - framefile=%s channel=%s\n",frf.file[i].Data(),channel);
951 
952  // Read Loop
953  int ntry=0;
954  ifile=NULL;
955  while(ifile==NULL&&ntry<3) {
956  ifile=FrFileINew(const_cast<char*>(frf.file[i].Data()));
957  if(ifile!=NULL || frRetryTime==0) break;
958  ntry++;
959  cout << "CWB::frame::readFrames - Failed to read file - "
960  << "retry after " << ntry*frRetryTime << " sec ..."
961  << "\t[" << ntry << "/3]" << endl;
962  if(ifile==NULL) gSystem->Sleep(1000*ntry*frRetryTime); // wait
963  }
964 
965  if(ifile==NULL) {
966  fprintf(stderr,"CWB::frame::readFrames - Cannot read file %s\n",frf.file[i].Data());
967  fflush(stderr);
968  fflush(stdout);
969  exit(1);
970  // break;
971  }
972 
973  if(ifile->error) {
974  fprintf(stderr,"CWB::frame::readFrames - Cannot read file %s, error=%d\n",frf.file[i].Data(),ifile->error);
975  fflush(stderr);
976  fflush(stdout);
977  exit(1);
978  // break;
979  }
980  fstart=(double)FrFileITStart(ifile);
981  fend=(double)FrFileITEnd(ifile);
982 
983  // check if frames are contiguous
984  if(fend0!=0 && fstart!=fend0) {
985  cout << "CWB::frame::readFrames - Error : " << " Requested frames are not contiguous !!!" << endl;
986  cout << "start : " << fstart << " is != from the end of the previous segment " << fend0 << endl;
987  exit(1);
988  }
989  fend0=fend;
990 
991  // BIG FRAMES CHECK (GV)
992  if (bigFrame) {
993  if (fstart<rf_start) fstart=rf_start;
994  if (fend>rf_stop) fend=rf_stop;
995  }
996  frlen=fend-fstart;
997  if(verbose) cout << "frlen : " << frlen << endl;
998  if (frlen<=0) continue;
999 
1000  rf_time=fstart;
1001 
1002  v=FrFileIGetV(ifile,channel, fstart, frlen);
1003 
1004  // BIG FRAMES CHECK (GV)
1005  if (bigFrame&&(v!=NULL)) {
1006  double time_offset = fstart-v->GTime;
1007  if (time_offset!=0) {
1008  fprintf(stdout,"CWB::frame::readFrames - BigFrame Check Mismatch : v->GTime=%.8f != fstart=%.8f \n",v->GTime,fstart);
1009  FrVectFree(v);
1010  v=FrFileIGetV(ifile,channel, fstart+time_offset, frlen);
1011  }
1012  }
1013 
1014  if(v==NULL) {
1015  fprintf(stderr,"CWB::frame::readFrames - FrFileIGetV failed: channel %s not present\n",channel);
1016  exit(1);
1017  }
1018  if((v->type!=FR_VECT_4R && v->type!=FR_VECT_8R)&&
1019  (v->type!=FR_VECT_2S && v->type!=FR_VECT_4S && v->type!=FR_VECT_8S)&&
1020  (v->type!=FR_VECT_2U && v->type!=FR_VECT_4U && v->type!=FR_VECT_8U)) {
1021  fprintf(stderr,"CWB::frame::readFrames - Wrong vector type %d\n",v->type);fflush(stderr);
1022  exit(1);
1023  }
1024  rf_step=v->dx[0];
1025  rf_rate=1./rf_step;
1026  w.resize(int((rf_stop-rf_start)*rf_rate));
1027  w.rate(rf_rate);
1028  w.start(rf_start);
1029  w.stop(rf_stop);
1030  long samples=long(frlen*rf_rate);
1031 
1032  if(verbose) {
1033  fprintf(stdout,"CWB::frame::readFrames - samples=%ld v->nDim=%d v->nData=%ld v->type=%d 8R=%d v->GTime=%.8f v->localtime=%d rate=%.1f step=%.8f\n",
1034  samples,(int)v->nDim,v->nData,v->type,FR_VECT_8R,v->GTime, v->localTime, rf_rate, rf_step);
1035  for(long j=0;j<v->nDim;j++) {
1036  fprintf(stdout,"v->nx[%ld]=%ld v->dx[%ld]=%.8f v->startX[%ld]=%.8f\n",
1037  j,v->nx[j],j,v->dx[j],j,v->startX[j]);
1038  }
1039  }
1040 
1041  // BIG FRAMES CHECK (GV)
1042  if (bigFrame&&(v->GTime!=fstart)) {fprintf(stdout,"CWB::frame::readFrames - Error : v->GTime=%.8f != fstart=%.8f \n",v->GTime,fstart);exit(1);}
1043 
1044  for(long j=0;j<samples;j++) {
1045  rf_time=fstart+j*rf_step;
1046  if(rf_time>=rf_start && rf_time<rf_stop) {
1047  if(wcounter<w.size()) {
1048  if(v->type==FR_VECT_2S) w.data[wcounter]=double(v->dataS[j]);
1049  if(v->type==FR_VECT_4S) w.data[wcounter]=double(v->dataI[j]);
1050  if(v->type==FR_VECT_8S) w.data[wcounter]=double(v->dataL[j]);
1051  if(v->type==FR_VECT_4R) w.data[wcounter]=double(v->dataF[j]);
1052  if(v->type==FR_VECT_8R) w.data[wcounter]=v->dataD[j];
1053  if(v->type==FR_VECT_2U) w.data[wcounter]=double(v->dataUS[j]);
1054  if(v->type==FR_VECT_4U) w.data[wcounter]=double(v->dataUI[j]);
1055  if(v->type==FR_VECT_8U) w.data[wcounter]=double(v->dataUL[j]);
1056  wcounter++;
1057  } else {
1058  fprintf(stderr,"w overflow\n");
1059  exit(1);
1060  }
1061  }
1062  }
1063 
1064  FrVectFree(v);
1065  FrFileIEnd(ifile);
1066  }
1067  if(verbose) fprintf(stdout,"CWB::frame::readFrames - wcounter=%d w.size()=%d\n",(int)wcounter,(int)w.size());
1068 
1069  if(srIndex>=0 && w.rate()!=(1<<srIndex)) {
1070  w.Resample(1<<srIndex); // resampling
1071  if(verbose) {
1072  fprintf(stdout,"--------------------------------\n");
1073  fprintf(stdout,"After resampling: rate=%f, size=%d, start=%f\n", w.rate(),(int)w.size(),w.start());
1074  fprintf(stdout,"--------------------------------\n");
1075  }
1076  }
1077 
1078  return;
1079 }
1080 
1081 //______________________________________________________________________________
1082 int
1084 //
1085 // dump to file ofName the list of frames defined in the frf structure
1086 // note : this method can be used only in READ mode
1087 // - list file used for the old cWB analysis
1088 // - output file format
1089 //
1090 // size : number of frame files
1091 // length : stop-start
1092 // start : start time
1093 // stop : stop time
1094 // sRate : rate
1095 // file1 :
1096 // ..... :
1097 // fileN :
1098 //
1099 // frf : in - frfile structure
1100 // ofName : in - name of the output file
1101 // sRate : in - sample rate of frame data
1102 //
1103 
1104  if(fOption!="READ") {
1105  cout << "CWB::frame::dumpFrList : allowed only in READ mode" << endl;
1106  exit(1);
1107  }
1108 
1109  char ofile_name[1024];
1110  sprintf(ofile_name,"%s",ofName.Data());
1111  cout << ofile_name << endl;
1112  ofstream out;
1113  out.open(ofile_name,ios::out);
1114  if (!out.good()) {cout << "Error Opening File : " << ofName.Data() << endl;exit(1);}
1115  out.precision(14);
1116 
1117  out << frf.file.size() << endl;
1118  out << frf.length << endl;
1119  out << frf.start << endl;
1120  out << frf.stop << endl;
1121  out << int(sRate) << endl;
1122  for (int i=0;i<(int)frf.file.size();i++) {
1123  out << frf.file[i];
1124  }
1125 
1126  out.close();
1127 
1128  return 0;
1129 }
1130 
1131 //______________________________________________________________________________
1132 bool
1134 //
1135 // check if file has the correct format
1136 // xxxx-GPS-LENGTH.yyy
1137 //
1138 
1139  // remove file extension
1140  TObjArray* token0 = fName.Tokenize(TString("."));
1141  TObjString* body_tok = (TObjString*)token0->At(0);
1142  TString body = body_tok->GetString().Data();
1143 
1144  TObjArray* token = body.Tokenize(TString("-"));
1145 
1146  if(token->GetEntries()<2) {
1147  cout << "CWB::frame::fNameCheck : File Name Format Error - " << fName.Data() << endl;
1148  cout << "GPS & LENGTH must be integers : xxxx-GPS-LENGTH.yyy" << endl;
1149  return false;
1150  }
1151  TObjString* gps_tok = (TObjString*)token->At(token->GetEntries()-2);
1152  TString sgps = gps_tok->GetString().Data();
1153 
1154  TObjString* length_tok = (TObjString*)token->At(token->GetEntries()-1);
1155  TString slength = length_tok->GetString().Data();
1156 
1157  if((slength.IsDigit()) && (sgps.IsDigit())) return true;
1158 
1159  cout << "CWB::frame::fNameCheck : File Name Format Error - " << fName.Data() << endl;
1160  cout << "GPS & LENGTH must be integers : xxxx-GPS-LENGTH.yyy" << endl;
1161  return false;
1162 }
TString getOption()
Definition: frame.hh:132
char ofile[1024]
TTree * tree
Definition: TimeSortTree.C:20
double sRate
Definition: TestFrame5.C:14
TString rfName
Definition: frame.hh:179
double start
Definition: network.hh:55
TString ofName
~frame()
Definition: frame.cc:118
int stop
Definition: Toolbox.hh:94
Definition: ced.hh:42
double min(double x, double y)
Definition: eBBH.cc:31
virtual void rate(double r)
Definition: wavearray.hh:141
wavearray< double > & operator>>(CWB::frame &fr, wavearray< double > &x)
Definition: frame.cc:129
TString getFrName()
Definition: frame.hh:129
Int_t nentries
Definition: TimeSortTree.C:24
WSeries< float > v[nIFO]
Definition: cwb_net.C:80
TString("c")
int start
Definition: Toolbox.hh:93
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
void close()
Definition: frame.cc:296
CWB::frame fr(FRLIST_NAME)
frame()
Definition: frame.cc:97
void writeFrame(wavearray< double > x, TString frName, TString chName)
Definition: frame.cc:175
Long_t size
int dumpFrList(frfile frf, TString ofName, double sRate=16384.)
Definition: frame.cc:1083
#define LST_TREE_NAME
Definition: CBCTool.hh:80
virtual void start(double s)
Definition: wavearray.hh:137
double segEdge
Definition: test_config1.C:49
int j
Definition: cwb_net.C:28
int srIndex
Definition: frame.hh:183
i drho i
TString chName
auxiliary tree used to store frame file infos
Definition: frame.hh:177
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
waveSegment getFrRange()
Definition: frame.hh:107
fprintf(stdout,"start=%f duration=%f rate=%f\, x.start(), x.size()/x.rate(), x.rate())
TString frName
Definition: frame.hh:178
size_t mode
wavearray< double > w
Definition: Test1.C:27
virtual size_t size() const
Definition: wavearray.hh:145
int nfiles
Definition: frame.hh:180
TString chName[NIFO_MAX]
double xstart
Definition: frame.hh:187
int frl2FrTree(TString iFile, TString rfName="", TString label=".gwf", unsigned int mode=0)
Definition: frame.cc:312
waveSegment SEG
int length
Definition: Toolbox.hh:95
TF1 * f2
i() int(T_cor *100))
TString label
Definition: MergeTrees.C:21
void Resample(const wavearray< DataType_t > &, double, int=6)
Definition: wavearray.cc:640
bool fNameCheck(TString fName)
Definition: frame.cc:1133
char cut[512]
frfile frf
UserGroup_t * uinfo
Definition: cwb_frdisplay.C:91
TObjArray * token
char ifile_name[1024]
bool verbose
Definition: frame.hh:184
double * entry
Definition: cwb_setcuts.C:224
TFile * ifile
int * xstart
TString getChName()
Definition: frame.hh:123
s s
Definition: cwb_net.C:155
TString frName[NIFO_MAX]
double gps
ifstream in
int frRetryTime
Definition: frame.hh:185
wavearray< int > index
virtual void stop(double s)
Definition: wavearray.hh:139
TString fOption
frame file pointer
Definition: frame.hh:182
TTree * frtree_List
Definition: frame.hh:176
void readFrames(char *filename, char *channel, wavearray< double > &w)
Definition: frame.cc:828
int cnt
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
frfile getFrList(int istart, int istop, int segEdge)
Definition: frame.cc:527
TTree * itree
FrFile * frFile
Definition: frame.hh:181
DataType_t * data
Definition: wavearray.hh:319
vector< TString > file
Definition: Toolbox.hh:96
char line[1024]
fclose(ftrig)
cout<< fr.getNfiles()<< endl;std::vector< frfile > frlist
int sortFrTree()
Definition: frame.cc:489
void open(TString ioFile, TString chName="", Option_t *option="", bool onDisk=false, TString label=".gwf", unsigned int mode=0)
Definition: frame.cc:230
char fName[256]
virtual void resize(unsigned int)
Definition: wavearray.cc:463
double length
Definition: TestBandPass.C:18
double stop
Definition: network.hh:56
double xstop
Definition: frame.hh:188
exit(0)
TTree * tsorted
Definition: TimeSortTree.C:34