42 #pragma GCC system_header
48 #include "wavearray.hh"
50 #include "TObjArray.h"
51 #include "TObjString.h"
85 CWB_Plugin(TFile* jfile, CWB::config* cfg,
network* net, WSeries<double>* x, TString ifo,
int type) {
95 if(type==CWB_PLUGIN_CONFIG) {
97 cout <<
"-----> CWB_Plugin_Gating.C" << endl;
101 if(type==CWB_PLUGIN_ICOHERENCE) {
104 cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
105 double Tb = gCWB2G->GetSegBegin()-cfg->segEdge;
106 double Te = gCWB2G->GetSegEnd()+cfg->segEdge;
111 int nIFO = (gCWB2G->IsSingleDetector()) ? 1 : net->ifoListSize();
112 int size = net->getifo(0)->getHoT()->size();
113 double rate = net->getifo(0)->getHoT()->rate();
118 wavearray<double>* hot[NIFO_MAX];
119 for(
int n=0;
n<
nIFO;
n++) hot[
n] = net->getifo(
n)->getHoT();
127 int X = int(cfg->segEdge/dt+0.5);
128 int M = int(
TEDG/dt)+1;
129 int N = int(
TWIN/dt);
131 vector<waveSegment> glist;
132 wavearray<double> SE(
size);
133 wavearray<int> sVeto(
size);
140 for(
int j=0;j<N;j++) SE[N-1]+=pow(hot[
n]->
data[N-1-j],2);
141 for(
int i=N;i<
size;i++) {
143 SE[i] -= pow(hot[
n]->
data[i-N],2);
144 SE[i] += pow(hot[
n]->
data[i],2);
148 for(
int i=0;i<
size;i++) {
150 int is = i-
M>X ? i-
M : X;
152 for(
int k=is;k<es;k++) sVeto[k]=1;
156 sVeto[0]=0;sVeto[
size-1]=0;
157 for(
int i=0;i<
size-1;i++) sVeto[i]=sVeto[i+1]-sVeto[i];
162 for(
int i=0;i<
size;i++) {
163 if(sVeto[i]== 1) gseg.start = Tb+int(i*dt);
165 gseg.stop = Tb+int(i*dt+0.5);
167 glist.push_back(gseg);
174 for(
int j=gsize;j<glist.size();j++) {
175 cout << j <<
" -----> CWB_Plugin_Gating.C - " << net->getifo(
n)->Name <<
" gating time : start="
176 << glist[j].start <<
" stop=" << glist[j].stop <<
" len=" << glist[j].stop-glist[j].start << endl;
183 double gating_time = 0;
185 glist = CWB::Toolbox::unionSegments(glist);
186 gating_time = CWB::Toolbox::getTimeSegList(glist);
187 glist = CWB::Toolbox::invertSegments(glist);
188 net->segList = CWB::Toolbox::mergeSegLists(glist,net->segList);
191 cout<<
"-----> CWB_Plugin_Gating.C - gating time : " << gating_time << endl << endl;
195 sprintf(info,
"-GT:%g",gating_time);
196 gCWB2G->PrintAnalysisInfo(CWB_STAGE_COHERENCE,
"cwb2G::Coherence",info,
false);
239 #pragma GCC system_header
243 #include "network.hh"
244 #include "wavearray.hh"
246 #include "TObjArray.h"
247 #include "TObjString.h"
249 #include "TComplex.h"
252 #include "watplot.hh"
253 #include "gwavearray.hh"
267 double GetQfactor(wavearray<double>*
wf,
double frequency,
bool fixAmax);
269 void GetGaussFitPars(wavearray<double>*
wf,
double& mean,
double& sigma,
bool doenv=
true);
270 void GetGaussFitPars2(wavearray<double>*
wf,
double& mean,
double& sigma,
bool fixAmax);
272 void GetQveto(wavearray<double>*
wf,
float &Qveto,
float &Qfactor);
273 void GetLveto(netcluster* pwc,
int cid,
int nifo,
float* Lveto);
274 void PlotWaveform(TString ifo, wavearray<double>* wfREC,
275 CWB::config* cfg,
bool fft=
false,
bool strain=
false);
278 std::vector<netpixel>
DoPCA(
network* NET, CWB::config* cfg,
int lag,
int id);
279 void ResetPCA(
network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX,
int ID);
283 CWB_Plugin(TFile* jfile, CWB::config* cfg,
network* NET, WSeries<double>* x, TString ifo,
int type) {
296 if(type==CWB_PLUGIN_CONFIG) {
298 cout <<
"-----> CWB_Plugin_QLveto.C" << endl;
303 if(type==CWB_PLUGIN_ILIKELIHOOD) {
308 TList *files = (TList*)gROOT->GetListOfFiles();
311 int nIFO = NET->ifoListSize();
317 while ((file=(TSystemFile*)next())) {
318 fname = file->GetName();
320 if(
fname.Contains(
"wave_")) {
321 froot=(TFile*)file;froot->cd();
323 outDump.ReplaceAll(
".root.tmp",
".txt");
328 cout <<
"CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
332 cout <<
"CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
336 TTree* net_tree = (TTree *) froot->Get(
"waveburst");
338 EVT =
new netevent(
nIFO);
339 net_tree = EVT->setTree();
340 net_tree->Branch(
"Qveto",Qveto,TString::Format(
"Qveto[%i]/F",4));
341 net_tree->Branch(
"Lveto",Lveto,TString::Format(
"Lveto[%i]/F",3));
344 bool qveto_exists=
false;
345 bool lveto_exists=
false;
346 TIter next(net_tree->GetListOfBranches());
347 while ((branch=(TBranch*)next())) {
348 if(TString(
"Qveto").CompareTo(branch->GetName())==0) qveto_exists=
true;
349 if(TString(
"Lveto").CompareTo(branch->GetName())==0) lveto_exists=
true;
352 if(!qveto_exists) net_tree->Branch(
"Qveto",Qveto,TString::Format(
"Qveto[%i]/F",4));
353 if(!lveto_exists) net_tree->Branch(
"Lveto",Lveto,TString::Format(
"Lveto[%i]/F",3));
357 if(type==CWB_PLUGIN_OLIKELIHOOD) {
359 if(TString(cfg->analysis)!=
"2G") {
360 cout <<
"CWB_Plugin_QLveto.C -> "
361 <<
"CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
366 int gIFACTOR=-1; IMPORT(
int,gIFACTOR)
371 float* gSLAGSHIFT=NULL; IMPORT(
float*,gSLAGSHIFT)
373 int nIFO = NET->ifoListSize();
376 wavearray<double>
id;
378 double factor = cfg->factors[gIFACTOR];
383 TList *files = (TList*)gROOT->GetListOfFiles();
390 while ((file=(TSystemFile*)next())) {
391 fname = file->GetName();
393 if(
fname.Contains(
"wave_")) {
394 froot=(TFile*)file;froot->cd();
396 outDump.ReplaceAll(
".root.tmp",
".txt");
401 cout <<
"CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
405 cout <<
"CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
409 TTree* net_tree = (TTree *) froot->Get(
"waveburst");
411 EVT =
new netevent(net_tree,
nIFO);
412 net_tree->SetBranchAddress(
"Qveto",Qveto);
413 net_tree->SetBranchAddress(
"Lveto",Lveto);
415 EVT =
new netevent(
nIFO);
416 net_tree = EVT->setTree();
417 net_tree->Branch(
"Qveto",Qveto,TString::Format(
"Qveto[%i]/F",4));
418 net_tree->Branch(
"Lveto",Lveto,TString::Format(
"Lveto[%i]/F",3));
420 EVT->setSLags(gSLAGSHIFT);
422 for(
int k=0; k<K; k++) {
424 id = NET->getwc(k)->get(
const_cast<char*
>(
"ID"), 0,
'L', rate);
426 for(
int j=0; j<(int)
id.
size(); j++) {
428 int ID = size_t(
id.
data[j]+0.5);
430 if(NET->getwc(k)->sCuts[ID-1]!=-1)
continue;
433 if(cfg->simulation==4) ofactor=-factor;
434 else if(cfg->simulation==3) ofactor=-gIFACTOR;
437 EVT->output2G(NULL,NET,ID,k,ofactor);
439 wavearray<double>** pwfREC =
new wavearray<double>*[
nIFO];
440 detector* pd = NET->getifo(0);
441 int idSize = pd->RWFID.size();
444 for (
int mm=0; mm<idSize; mm++)
if (pd->RWFID[mm]==ID) wfIndex=mm;
445 if(wfIndex==-1)
continue;
447 netcluster* pwc = NET->getwc(k);
448 cout << endl <<
"----------------------------------------------------------------" << endl;
451 Qveto[0]=Qveto[1]=1.e20;
452 double Qfactor1[NIFO_MAX];
453 double Qfactor2[NIFO_MAX];
458 pwfREC[
n] = pd->RWFP[wfIndex];
459 wavearray<double>* wfREC = pwfREC[
n];
461 #ifdef PLOT_WHITENED_WAVEFORMS
469 NET->getMRAwave(ID,k,
'S',0,
true);
470 GetQveto(&(pd->waveForm), qveto, qfactor);
471 if(qveto<Qveto[0]) Qveto[0]=qveto;
472 if(qfactor<Qveto[1]) Qveto[1]=qfactor;
474 Qfactor1[
n] = qfactor;
476 Qfactor2[
n] =
GetQfactor(&(pd->waveForm), fpeak,
true);
478 cout <<
"Qfactor1/2 : " << pd->Name <<
" frequency[0] = " << EVT->frequency[0] <<
" fpeak = " << fpeak
479 <<
" -> " << Qfactor1[
n] <<
" / " << Qfactor2[
n] << endl;
483 NET->getMRAwave(ID,k,
'W',0,
true);
484 GetQveto(&(pd->waveBand), qveto, qfactor);
485 if(qveto<Qveto[0]) Qveto[0]=qveto;
486 if(qfactor<Qveto[1]) Qveto[1]=qfactor;
488 cout <<
"Qveto : " << pd->Name <<
" Qveto[0] = " << Qveto[0]
489 <<
" Qveto[1] = " << Qveto[1] << endl;
497 for(
int n=0;
n<
nIFO;
n++) Qveto[2]+=EVT->sSNR[
n]*Qfactor1[
n];
498 Qveto[2]/=EVT->likelihood;
501 for(
int n=0;
n<
nIFO;
n++) Qveto[3]+=EVT->sSNR[
n]*Qfactor2[
n];
502 Qveto[3]/=EVT->likelihood;
503 cout <<
"Qveto :" <<
" Qveto[2] = " << Qveto[2]
504 <<
" Qveto[3] = " << Qveto[3] << endl;
506 std::vector<netpixel> vPIX;
507 if(cfg->pattern>0) vPIX =
DoPCA(NET, cfg, k, ID);
509 if(cfg->pattern>0)
ResetPCA(NET, cfg, pwc, &vPIX, ID);
512 cout <<
"Lveto : " <<
"fmean : " << Lveto[0] <<
" frms : " << Lveto[1]
513 <<
" Energy Ratio : " << Lveto[2] << endl << endl;
514 cout <<
"----------------------------------------------------------------" << endl;
516 std::vector<int> sCuts = NET->getwc(k)->sCuts;
518 for(
int i=0; i<(int)sCuts.size(); i++)
if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
521 NET->getwc(k)->sCuts = sCuts;
523 if(cfg->dump) EVT->dopen(outDump.Data(),
const_cast<char*
>(
"a"),
false);
524 EVT->output2G(net_tree,NET,ID,k,ofactor);
527 fprintf(EVT->fP,
"Qveto: ");
528 for(
int i=0; i<2*
nIFO; i++) fprintf(EVT->fP,
"%f ",Qveto[i]);
529 fprintf(EVT->fP,
"\n");
531 fprintf(EVT->fP,
"Lveto: ");
532 for(
int i=0; i<3; i++) fprintf(EVT->fP,
"%f ",Lveto[i]);
533 fprintf(EVT->fP,
"\n");
535 if(cfg->dump) EVT->dclose();
536 if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1;
547 GetQveto(wavearray<double>*
wf,
float &Qveto,
float &Qfactor) {
549 wavearray<double> x = *
wf;;
554 x.resize(4*x.size());
556 for(
int j=xsize;j<x.size();j++) x[j]=0;
560 wavearray<double> a(x.size());
562 double dt = 1./x.rate();
565 for (
int i=1;i<x.size();i++) {
566 if(fabs(x[i])>xmax) xmax=fabs(x[i]);
578 for (
int i=1;i<
size;i++) {
579 if(a[i]>amax) {amax=a[i];imax=i;}
595 for (
int i=0;i<
size;i++) {
596 if(abs(imax-i)<=
NTHR) {
600 if(a[i]>amax/
ATHR) eout+=a[i]*a[i];
604 Qveto = ein>0 ? eout/ein : 0.;
608 float R = (a[imax-1]+a[imax+1])/a[imax]/2.;
609 Qfactor = sqrt(-pow(TMath::Pi(),2)/log(
R)/2.);
616 GetLveto(netcluster* pwc,
int cid,
int nifo,
float* Lveto) {
628 Lveto[0] = Lveto[1] = Lveto[2] = 0;
630 std::vector<int>* vint = &(pwc->cList[cid-1]);
631 int V = vint->size();
642 for(
int n=0;
n<V;
n++) {
643 netpixel* pix = pwc->getPixel(cid,
n);
644 if(pix->layers%2==0) {
645 cout <<
"CWB_Plugin_QLveto.C - Error : is enabled only for WDM (2G)" << endl;
648 if(!pix->core)
continue;
651 for(
int m=0;
m<nifo;
m++) {
652 likePix += pow(pix->getdata(
'S',
m),2);
653 likePix += pow(pix->getdata(
'P',
m),2);
656 double freq = pix->frequency*pix->rate/2.;
657 double df = pix->rate/2.;
660 if(likePix>likeMax) {likeMax=likePix;freqMax=freq;dfreqMax=df;}
672 for(
int n=0;
n<V;
n++) {
673 netpixel* pix = pwc->getPixel(cid,
n);
674 if(!pix->core)
continue;
677 for(
int m=0;
m<nifo;
m++) {
678 likePix += pow(pix->getdata(
'S',
m),2);
679 likePix += pow(pix->getdata(
'P',
m),2);
684 double freq = pix->frequency*pix->rate/2.;
685 if(fabs(freq-freqMax)<=dfreqMax) {
687 fmean += likePix*freq;
688 frms += likePix*freq*freq;
692 fmean = fmean/likeLin;
693 frms = frms/likeLin-fmean*fmean;
694 frms = frms>0 ? sqrt(frms) : 0.;
696 if(frms<dfreqMax/2.) frms=dfreqMax/2.;
704 Lveto[2] = likeTot>0. ? likeLin/likeTot : 0.;
710 #if defined PLOT_LIKELIHOOD
711 watplot WTS(
const_cast<char*
>(
"wts"));
712 WTS.plot(pwc, cid, nifo,
'L', 0,
const_cast<char*
>(
"COLZ"));
713 WTS.canvas->Print(
"l_tfmap_scalogram.png");
721 CWB::config* cfg,
bool fft,
bool strain) {
723 watplot PTS(
const_cast<char*
>(
"ptswrc"),200,20,800,500);
726 double tmin = wfREC->start();
727 wfREC->start(wfREC->start()-tmin);
729 PTS.plot(wfREC,
const_cast<char*
>(
"ALP"), 1, 0, 0,
true, cfg->fLow, cfg->fHigh);
731 PTS.plot(wfREC,
const_cast<char*
>(
"ALP"), 1, 0, 0);
733 PTS.graph[0]->SetLineWidth(1);
734 wfREC->start(wfREC->start()+tmin);
745 PTS.canvas->Print(
fname);
746 cout <<
"write : " <<
fname << endl;
755 n = ifo->IWFP.size();
756 for (
int i=0;i<
n;i++) {
757 wavearray<double>*
wf = (wavearray<double>*)ifo->IWFP[i];
763 n = ifo->RWFP.size();
764 for (
int i=0;i<
n;i++) {
765 wavearray<double>*
wf = (wavearray<double>*)ifo->RWFP[i];
772 std::vector<netpixel>
DoPCA(
network* NET, CWB::config* cfg,
int lag,
int id) {
776 size_t nIFO = NET->ifoList.size();
778 float En = 2*NET->acor*NET->acor*
nIFO;
780 int size = NET->a_00.size();
784 netcluster* pwc = NET->getwc(lag);
785 std::vector<netpixel> vPIX;
787 std::vector<int> pI = NET->wdmMRA.getXTalk(pwc,
id,
false);
789 if(V>cfg->BATCH)
return vPIX;
791 wavearray<float> xi(
size); xi=0;
792 wavearray<float> XI(
size); XI=0;
794 __m128* _xi = (__m128*) xi.data;
795 __m128* _XI = (__m128*) XI.data;
797 __m128* _aa = (__m128*) NET->a_00.data;
798 __m128* _AA = (__m128*) NET->a_90.data;
801 for(
int j=0; j<V; j++) {
803 _sse_zero_ps(_xi+jf);
804 _sse_zero_ps(_XI+jf);
805 ee = _sse_abs_ps(_aa+jf,_AA+jf);
806 if(ee>En) nPC++;
else ee=0.;
807 NET->rNRG.data[j] = ee;
808 NET->pNRG.data[j] = NET->rNRG.data[j];
811 nPC = NET->_sse_mra_ps(xi.data,XI.data,En,nPC);
813 for(
int j=0; j<V; j++) {
814 pix = pwc->getPixel(
id,pI[j]);
815 vPIX.push_back(*pix);
817 ee = NET->pNRG.data[j];
820 for(
int i=0; i<
nIFO; i++) {
821 pix->setdata(
double(xi.data[j*NIFO+i]),
'S',i);
822 pix->setdata(
double(XI.data[j*NIFO+i]),
'P',i);
829 void ResetPCA(
network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX,
int ID) {
831 std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, ID,
false);
833 if(V>cfg->BATCH)
return;
834 for(
int j=0; j<V; j++) {
835 netpixel* pix = pwc->getPixel(ID,pI[j]);
839 while(!vPIX->empty()) vPIX->pop_back();
840 vPIX->clear(); std::vector<netpixel>().swap(*vPIX);
844 GetQfactor(wavearray<double>*
wf,
double frequency,
bool fixAmax) {
858 wavearray<double> x = *
wf;;
864 x.resize(4*x.size());
866 for(
int j=xsize;j<x.size();j++) x[j]=0;
870 double gmean, gsigma;
874 double xmin = gmean-3.0*gsigma;
875 double xmax = gmean+3.0*gsigma;
878 x = CWB::mdc::GetEnvelope(&x);
881 double dt = 1./x.rate();
885 for (
int i=1;i<x.size();i++) {
886 if(x[i]>amax) {amax=x[i];imax=i;}
890 for (
int i=1;i<x.size();i++) {
892 if(
t>xmin &&
t<xmax) area+=x[i]*dt;
895 double sigma = area/amax/sqrt(2*TMath::Pi());
897 double qfactor = sigma*(TMath::TwoPi()*frequency);
913 wavearray<double> x = *
wf;
914 wavearray<double> ex = CWB::mdc::GetEnvelope(&x);
916 for(
int i=0;i<N;i++) x[i]*=ex[i];
921 double df=(x.rate()/(double)N)/2.;
924 for(
int i=0;i<N;i+=2) {
926 double amp=x[i]*x[i]+x[i+1]*x[i+1];
927 if(amp>amax) {fmax=freq;amax=amp;}
934 GetGaussFitPars(wavearray<double>*
wf,
double& mean,
double& sigma,
bool doenv) {
945 wavearray<double> x = doenv ? CWB::mdc::GetEnvelope(
wf) : *
wf;
949 double* time =
new double[N];
950 double* amp =
new double[N];
951 double dt = 1./x.rate();
952 for(
int i=0;i<N;i++) {
956 TGraph
gr(N,time,amp);
960 TF1* gaus = (TF1*)
gr.GetFunction(
"gaus");
962 mean = gaus->GetParameter(
"Mean");
963 sigma = gaus->GetParameter(
"Sigma");
989 wavearray<double> x = CWB::mdc::GetEnvelope(
wf);
993 wavearray<double> x2 = x;
994 for(
int i=0;i<x.size();i++) x2[i]=x[i]*x[i];
996 double gmean, gsigma;
1000 double xmin = gmean-3.0*gsigma;
1001 double xmax = gmean+3.0*gsigma;
1006 double* time =
new double[N];
1007 double* amp =
new double[N];
1008 double dt = 1./x.rate();
1010 for(
int i=0;i<N;i++) {
1013 if(fabs(x[i])>amax) amax=fabs(x[i]);
1015 TGraph
gr(N,time,amp);
1019 TF1 *xgaus =
new TF1(
"xgaus",
"gaus",xmin,xmax);
1020 if(fixAmax) xgaus->SetParLimits(0,amax*0.999,amax*1.001);
1021 gr.Fit(
"xgaus",
"RQ");
1023 mean = xgaus->GetParameter(
"Mean");
1024 sigma = xgaus->GetParameter(
"Sigma");
1053 #pragma GCC system_header
1058 CWB_Plugin(TFile* jfile, CWB::config* cfg,
network* net, WSeries<double>* x, TString ifo,
int type) {
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
sprintf(tag,"wave_%s", data_label)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
double GetQfactor(wavearray< double > *wf, double frequency, bool fixAmax)
void ResetPCA(network *NET, CWB::config *cfg, netcluster *pwc, std::vector< netpixel > *vPIX, int ID)
double GetPeakFrequency(wavearray< double > *wf)
std::vector< netpixel > DoPCA(network *NET, CWB::config *cfg, int lag, int id)
void GetQveto(wavearray< double > *wf, float &Qveto, float &Qfactor)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
void GetLveto(netcluster *pwc, int cid, int nifo, float *Lveto)
void ClearWaveforms(detector *ifo)
void GetGaussFitPars(wavearray< double > *wf, double &mean, double &sigma, bool doenv=true)
void GetGaussFitPars2(wavearray< double > *wf, double &mean, double &sigma, bool fixAmax)
void PlotWaveform(TString ifo, wavearray< double > *wfREC, CWB::config *cfg, bool fft=false, bool strain=false)