Logo coherent WaveBurst  
Library Reference Guide
Logo
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CWB_Plugin_Sim4.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 #define XIFO 4
20 
21 #pragma GCC system_header
22 
23 #include "cwb.hh"
24 #include "config.hh"
25 #include "network.hh"
26 #include "wavearray.hh"
27 #include "TString.h"
28 #include "TObjArray.h"
29 #include "TObjString.h"
30 #include "TRandom.h"
31 #include "TComplex.h"
32 #include "TMath.h"
33 #include "mdc.hh"
34 #include <vector>
35 
36 size_t SetFactors(wavearray<double> &wi, std::vector<double>* pT, std::vector<double>* pF, double dT);
37 
38 void
40 
41  // Plugin to generate simulated MDC injected 'on the fly' using simulation=4
42 
43  cout << endl;
44  cout << "-----> CWB_Plugin_Sim4.C" << endl;
45  cout << "ifo " << ifo.Data() << endl;
46  cout << "type " << type << endl;
47  cout << endl;
48 
50 
51  // import istage,jstage
52  int gISTAGE=-1; IMPORT(int,gISTAGE)
53  int gJSTAGE=-1; IMPORT(int,gJSTAGE)
54  cout << "-----> CWB_Plugin_Sim4.C -> " << " gISTAGE : " << gISTAGE << " gJSTAGE : " << gJSTAGE << endl;
55 
56  if(type==CWB_PLUGIN_CONFIG) {
57  cfg->dataPlugin=false; // disable read data from frames
58  cfg->mdcPlugin=true; // disable read mdc from frames
59  }
60 
61  if(type==CWB_PLUGIN_MDC) {
62 
63  // import ifactor
64  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
65  cout << "-----> CWB_Plugin_Sim4.C -> " << " gIFACTOR : " << gIFACTOR << endl;
66 
67  // export to CINT net,cfg
68  char cmd[128];
69  sprintf(cmd,"network* net = (network*)%p;",net);
70  gROOT->ProcessLine(cmd);
71  sprintf(cmd,"CWB::config* cfg = (CWB::config*)%p;",cfg);
72  gROOT->ProcessLine(cmd);
73 
74  CWB::mdc MDC(net);
75 
76  // ---------------------------------
77  // read plugin config
78  // ---------------------------------
79 
80  cfg->configPlugin.Exec();
81 
82  // ---------------------------------
83  // set list of mdc waveforms
84  // ---------------------------------
85 
86  // import MDC form plugin config
87  IMPORT(CWB::mdc,MDC)
88  MDC.Print();
89 
90  // import mdcHRSS form plugin config
91  std::vector<double> mdcHRSS;
92  IMPORT(std::vector<double>,mdcHRSS)
93  int M = mdcHRSS.size();
94  for(int i=0;i<M;i++) cout << i << " mdcHRSS = " << mdcHRSS[i] << endl;
95 
96  // ---------------------------------
97  // get mdc data
98  // ---------------------------------
99 
100  MDC.Get(*x,ifo);
101 
102  // compute normalization factors
103  int K = MDC.mdcList.size();
104  int L = MDC.mdcType.size();
105  std::vector<double> mdcFactor(K);
106  double inj_hrss = MDC.GetInjHrss();
107  for(int i=0;i<K;i++) {
108  for(int j=0;j<L;j++) {
109  if(TString(MDC.mdcList[i]).Contains(MDC.mdcType[j])) mdcFactor[i]=mdcHRSS[j]/inj_hrss;
110  }
111  }
112  for(int i=0;i<K;i++) cout << i << " mdcFactor = " << mdcFactor[i] << endl;
113 
114  // normalize x data
115  SetFactors(*x, &MDC.mdcTime, &mdcFactor, cfg->iwindow/2.);
116 
117  // rescale amplitudes stored in the mdcList
118  for(int k=0; k<(int)K; k++) {
119  int ilog[5] = {1,3,12,13,14};
120  for(int l=0;l<5;l++) {
121  double mfactor = l<2 ? mdcFactor[k] : mdcFactor[k]*mdcFactor[k];
122  TString slog = TB.GetMDCLog(MDC.mdcList[k], ilog[l]);
123  MDC.mdcList[k]=TB.SetMDCLog(MDC.mdcList[k], ilog[l], mfactor*slog.Atof());
124  }
125  }
126 
127  // ---------------------------------
128  // set mdc list in the network class
129  // ---------------------------------
130 
131  if(ifo.CompareTo(net->ifoName[0])==0) {
132 
133  net->mdcList.clear();
134  net->mdcType.clear();
135  net->mdcTime.clear();
136  net->mdcList=MDC.mdcList;
137  net->mdcType=MDC.mdcType;
138  net->mdcTime=MDC.mdcTime;
139  }
140 
141  // for multi stage analysis store mdc infos to job root file
142  if((gJSTAGE!=CWB_STAGE_FULL) && (ifo.CompareTo(net->ifoName[0])==0)) {
143 
144  int ifactor;
145  std::vector<std::string>* mdcList = new vector<std::string>; // list of injections
146  std::vector<std::string>* mdcType = new vector<std::string>; // list of injection types
147  std::vector<double>* mdcTime = new vector<double>; // gps time of selected injections
148 
149  int inj_size = (int)MDC.mdcList.size();
150 
151  jfile->cd();
152 
153  TTree* mdc_tree = (TTree*)jfile->Get("injections");
154  if(mdc_tree==NULL) {
155  mdc_tree = new TTree("injections", "injections");
156  mdc_tree->Branch("ifactor", &ifactor, "ifactor/I");
157  mdc_tree->Branch("mdcList", &mdcList, inj_size, 0);
158  mdc_tree->Branch("mdcType", &mdcType, inj_size, 0);
159  mdc_tree->Branch("mdcTime", &mdcTime, inj_size, 0);
160  } else {
161  mdc_tree->SetBranchAddress("ifactor", &ifactor);
162  mdc_tree->SetBranchAddress("mdcList", &mdcList);
163  mdc_tree->SetBranchAddress("mdcType", &mdcType);
164  mdc_tree->SetBranchAddress("mdcTime", &mdcTime);
165  }
166 
167  ifactor=gIFACTOR;
168 
169  *mdcList=MDC.mdcList;
170  *mdcType=MDC.mdcType;
171  *mdcTime=MDC.mdcTime;
172 
173  mdc_tree->Fill();
174 
175  jfile->Write();
176  }
177 
178  cout.precision(14);
179  for(int k=0;k<(int)MDC.mdcList.size();k++) cout << k << " mdcList " << MDC.mdcList[k] << endl;
180  for(int k=0;k<(int)MDC.mdcTime.size();k++) cout << k << " mdcTime " << MDC.mdcTime[k] << endl;
181  for(int k=0;k<(int)MDC.mdcType.size();k++) cout << k << " mdcType " << MDC.mdcType[k] << endl;
182 
183  }
184 
185  // for multi stage analysis read mdc infos to job root file
186  if((gJSTAGE!=CWB_STAGE_FULL) && (type==CWB_PLUGIN_ILIKELIHOOD)) {
187 
188  // import ifactor
189  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
190  cout << "-----> CWB_Plugin_Sim4.C -> " << " gIFACTOR : " << gIFACTOR << endl;
191 
192  int ifactor;
193  std::vector<std::string>* mdcList = new vector<std::string>; // list of injections
194  std::vector<std::string>* mdcType = new vector<std::string>; // list of injection types
195  std::vector<double>* mdcTime = new vector<double>; // gps time of selected injections
196 
197  jfile->cd();
198  TTree* mdc_tree = (TTree*)jfile->Get("injections");
199  if(mdc_tree==NULL) {
200  cout << "-----> CWB_Plugin_Sim4.C : Error-> " << " injection tree not present in job file" << endl;
201  gSystem->Exit(1);
202  } else {
203  mdc_tree->SetBranchAddress("ifactor", &ifactor);
204  mdc_tree->SetBranchAddress("mdcList", &mdcList);
205  mdc_tree->SetBranchAddress("mdcType", &mdcType);
206  mdc_tree->SetBranchAddress("mdcTime", &mdcTime);
207  }
208 
209  int tree_size = (int)mdc_tree->GetEntries();
210 
211  bool ifactor_check=false;
212  for(int i=0;i<tree_size;i++) {
213  mdc_tree->GetEntry(i);
214  if(ifactor==gIFACTOR) {
215  ifactor_check=true;
216  net->mdcList.clear();
217  net->mdcType.clear();
218  net->mdcTime.clear();
219  net->mdcList=*mdcList;
220  net->mdcType=*mdcType;
221  net->mdcTime=*mdcTime;
222  }
223  }
224  if(!ifactor_check) {
225  cout << "-----> CWB_Plugin_Sim4.C : Error-> " << " mdc infos not present in the injection tree " << endl;
226  gSystem->Exit(1);
227  }
228 
229  cout.precision(14);
230  for(int k=0;k<(int)net->mdcList.size();k++) cout << k << " mdcList " << net->mdcList[k] << endl;
231  for(int k=0;k<(int)net->mdcTime.size();k++) cout << k << " mdcTime " << net->mdcTime[k] << endl;
232  for(int k=0;k<(int)net->mdcType.size();k++) cout << k << " mdcType " << net->mdcType[k] << endl;
233  }
234 
235  return;
236 }
237 
238 // modify input signals (wi) at times pT according the factor pF
239 size_t
240 SetFactors(wavearray<double> &wi, std::vector<double>* pT, std::vector<double>* pF, double dT) {
241 
242  int j,nstop,nstrt;
243  size_t k;
244  double F,T;
245  size_t K = pT->size();
246  size_t N = wi.size();
247  size_t M = 0;
248  double rate = wi.rate(); // simulation rate
249 
250  dT = fabs(dT);
251 
252  wavearray<double> w; w=wi;
253 
254  // isolate injections
255  for(k=0; k<K; k++) {
256 
257  F = (*pF)[k];
258  T = (*pT)[k] - w.start();
259 
260  nstrt = int((T - dT)*rate);
261  nstop = int((T + dT)*rate);
262  if(nstrt<=0) nstrt = 0;
263  if(nstop>=int(N)) nstop = N;
264  if(nstop<=0) continue; // outside of the segment
265  if(nstrt>=int(N)) continue; // outside of the segment
266 
267  for(j=nstrt; j<nstop; j++) {
268  w.data[j]*=F;
269  }
270 
271  M++;
272  }
273  wi = w;
274  return M;
275 }
276 
std::vector< char * > ifoName
Definition: network.hh:609
CWB::config * cfg
double iwindow
Definition: config.hh:200
double GetInjHrss()
Definition: mdc.hh:310
double M
Definition: DrawEBHH.C:13
std::vector< double > mdcHRSS(K)
TMacro configPlugin
Definition: config.hh:362
TString Get(wavearray< double > &x, TString ifo)
Definition: mdc.cc:1529
bool mdcPlugin
Definition: config.hh:365
std::vector< std::string > mdcList
Definition: mdc.hh:389
virtual void rate(double r)
Definition: wavearray.hh:141
bool dataPlugin
Definition: config.hh:364
TString("c")
std::vector< std::string > mdcType
Definition: mdc.hh:390
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
CWB::Toolbox TB
std::vector< std::string > mdcType
Definition: network.hh:613
CWB::mdc * MDC
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
std::vector< double > mdcTime
Definition: network.hh:614
#define N
void Print(int level=0)
Definition: mdc.cc:2736
char ifo[NIFO_MAX][8]
network ** net
NOISE_MDC_SIMULATION.
wavearray< double > w
Definition: Test1.C:27
size_t SetFactors(wavearray< double > &wi, std::vector< double > *pT, std::vector< double > *pF, double dT)
virtual size_t size() const
Definition: wavearray.hh:145
jfile
Definition: cwb_job_obj.C:43
static TString GetMDCLog(TString log, int pos)
Definition: Toolbox.cc:2278
Definition: mdc.hh:248
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
i() int(T_cor *100))
int ifactor
std::vector< std::string > mdcList
Definition: network.hh:612
int l
int k
double F
double T
Definition: testWDM_4.C:11
double fabs(const Complex &x)
Definition: numpy.cc:55
static TString SetMDCLog(TString log, int pos, TString val)
Definition: Toolbox.cc:2238
int gIFACTOR
char cmd[1024]
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
DataType_t * data
Definition: wavearray.hh:319
double dT
Definition: testWDM_5.C:12
std::vector< double > mdcTime
Definition: mdc.hh:392