Logo coherent WaveBurst  
Library Reference Guide
Logo
ReadNetCluster.C
Go to the documentation of this file.
1 // this macro shows how to read the netcluster from output ROOT file produced with CWB_Plugin_WF.C
2 // netcluster must be enabled in the config/user_parameters.C with the following option:
3 //
4 // options += "wf_output_enable=cluster "; // enable save event crate/cluster into the output root file
5 //
6 // Author : G.Vedovato
7 
8 // ------------------------------------------------------------
9 // structure
10 // ------------------------------------------------------------
11 
12 struct XPARMS { // structure for output for estimated parameters
13 
14  std::vector<TString> ifo; // network ifo names
15 
16  float isnr; // injected network SNR
17  float osnr; // reconstructed network SNR
18 
19  double seggps; // segment start time
20 
21  netcluster nc; // netcluster
22 };
23 
24 // ------------------------------------------------------------
25 // functions
26 // ------------------------------------------------------------
27 
28 int ReadDataFromROOT(TString ifile, XPARMS* xparms);
29 void PlotNetCluster(watplot* WTS, netcluster* pwc, double seggps, TString odir);
30 void DumpNetCluster(netcluster* pwc, int cid, int nifo, TString odir);
31 
32 
33 // ------------------------------------------------------------
34 // global variables
35 // ------------------------------------------------------------
36 
37 int gENTRY;
38 
40 int nIFO;
41 
42 TCanvas* canvas;
43 
44 void ReadNetCluster(TString ifroot, TString odir=".", int entry=0) {
45 //
46 // ifroot: input root file name (event parameters + nectcluster)
47 // odir: output directory where to dump the plots likelihood + null
48 // entry: event entry in the input root file to be investigated
49 //
50 
51  // init
52  canvas=NULL;
53  WTS=NULL;
54 
55  gROOT->SetBatch(true); // macro is executed in batch mode
56 
57  gENTRY = entry;
58 
60 
61  // read xparms from input root file
62  XPARMS xparms;
63  int err = ReadDataFromROOT(ifroot, &xparms);
64  if(err>0) exit(1);
65 
66  PlotNetCluster(WTS, &xparms.nc, xparms.seggps, odir);
67 
68  DumpNetCluster(&xparms.nc, 1, nIFO, odir);
69 
70  exit(0);
71 }
72 
73 int ReadDataFromROOT(TString ifile, XPARMS* xparms) {
74 //
75 // ----------------------------------------------------
76 // Read netcluster
77 // ----------------------------------------------------
78 
79  TFile* froot = new TFile(ifile,"READ");
80  if(froot==NULL) {cout << "ReadDataFromROOT - Error opening file " << ifile << endl;return 1;}
81  TTree* itree = (TTree*)froot->Get("waveburst");
82  if(itree==NULL) {cout << "ReadDataFromROOT - Error : no waveburst present in the file" << endl;return 1;}
83 
84  // get detector list
85  TList* list = itree->GetUserInfo();
86  nIFO=list->GetSize();
87  if (nIFO==0) {cout << "ReadDataFromROOT - Error : no ifo present in the tree" << endl;return 1;}
88  for (int n=0;n<list->GetSize();n++) {
89  detector* pDetector;
90  pDetector = (detector*)list->At(n);
91  xparms->ifo.push_back(pDetector->Name);
92  detectorParams dParams = pDetector->getDetectorParams();
93  //pDetector->print();
94  }
95 
96  itree->SetBranchAddress("ndim",&nIFO);
97  itree->GetEntry(gENTRY);
98 
99  double seggps=0; // segment start time
100  float crate; // netcluster::rate
101  float iSNR[NIFO_MAX];
102  float oSNR[NIFO_MAX];
103  std::vector<netpixel>* cluster;
104 
105  cluster = new std::vector<netpixel>;
106 
107  itree->SetBranchAddress("iSNR",iSNR);
108  itree->SetBranchAddress("oSNR",oSNR);
109 
110  itree->SetBranchAddress("seggps",&seggps);
111  itree->SetBranchAddress("crate",&crate);
112  itree->SetBranchAddress("cluster",&cluster);
113 
114  itree->GetEntry(gENTRY);
115 
116  xparms->isnr = 0;
117  for(int n=0;n<nIFO;n++) xparms->isnr += iSNR[n];
118  xparms->isnr = sqrt(xparms->isnr);
119  xparms->osnr = 0;
120  for(int n=0;n<nIFO;n++) xparms->osnr += oSNR[n];
121  xparms->osnr = sqrt(xparms->osnr);
122 
123  xparms->seggps=seggps;
124 
125  cout << "segment start time " << int(seggps) << endl;
126  cout << "cluster rate " << crate << endl;
127  cout << "cluster size " << cluster->size() << endl;
128 
129  if(cluster->size()==0) {cout << "ReadDataFromROOT - Error : no clusters in the root file !!!" << endl;return 1;}
130 
131  xparms->nc.rate=crate;
132  std::vector<int> pList;
133  for(int i=0;i<cluster->size();i++) {
134  xparms->nc.pList.push_back((*cluster)[i]);
135  pList.push_back(i);
136  }
137  xparms->nc.cList.push_back(pList);
138  clusterdata cd; // dummy cluster data
139  xparms->nc.cData.push_back(cd);
140 
141  if(cluster) delete cluster;
142  if(itree) delete itree;
143  if(froot) delete froot;
144 
145  return 0;
146 }
147 
148 void PlotNetCluster(watplot* WTS, netcluster* pwc, double seggps, TString odir) {
149 //
150 // ----------------------------------------------------
151 // Plot netcluster
152 // ----------------------------------------------------
153 
154  if(WTS==NULL) WTS = new watplot(const_cast<char*>("wtswrc"));
155 
156  WTS->canvas->cd();
157 
158  char fname[1024];
159 
160  TString xtitle = TString::Format("Time (sec) : GPS OFFSET = %.3f",seggps);
161 
162  // dump likelihood plot
163  sprintf(fname, "%s/l_tfmap_scalogram.%s", odir.Data(), "png");
164  WTS->plot(pwc, 1, nIFO, 'L', 0, const_cast<char*>("COLZ"),256,true);
165  WTS->hist2D->GetXaxis()->SetTitle(xtitle);
166  WTS->canvas->Print(fname);
167  WTS->clear();
168 
169  // dump null plot
170  sprintf(fname, "%s/n_tfmap_scalogram.%s", odir.Data(), "png");
171  WTS->plot(pwc, 1, nIFO, 'N', 0, const_cast<char*>("COLZ"),256,true);
172  WTS->hist2D->GetXaxis()->SetTitle(xtitle);
173  WTS->canvas->Print(fname);
174  WTS->clear();
175 
176  return;
177 }
178 
179 void DumpNetCluster(netcluster* pwc, int cid, int nifo, TString odir) {
180 //
181 // pwc : pointer to netcluster object
182 // cid : cluster id
183 // nifo : number of detectors
184 //
185 
186  char ofname[1024];
187  sprintf(ofname, "%s/tfmap_dump.%s", odir.Data(), "txt");
188 
189  FILE *fp;
190  if((fp = fopen(ofname, "w")) == NULL ) {
191  cout << "DumpNetCluster error: cannot open file " << ofname <<". \n";
192  return;
193  };
194  cout << endl << "ofile name = " << ofname << endl << endl;
195 
196  double RATE = pwc->rate; // original rate
197 
198  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
199 
200  int V = vint->size(); // cluster size
201  if(!V) return;
202 
203  netpixel* pix = pwc->getPixel(cid,0);
204 
205  for(int n=0; n<V; n++) {
206  netpixel* pix = pwc->getPixel(cid,n);
207  if(!pix->core) continue;
208 
209  double like = pix->likelihood>0. ? pix->likelihood : 0.;
210  double null = pix->null>0. ? pix->null : 0.;
211 
212  double time = int(pix->time/pix->layers)/double(pix->rate); // central pixel time
213  double dt = 1./pix->rate;
214  time -= dt/2.; // begin pixel time
215 
216  double freq = pix->frequency*pix->rate/2.;
217  double df = 1./(2.*dt);
218 
219  char sout[1024];
220  sprintf(sout,"pixel %*d\ttime = %*.3f (sec) \tfreq = %*.3f (Hz) \tnull = %*.3f \tlikelihood = %*.3f \tRes = %*.3f x %*.3f (sec,Hz)\n",7,n,7,time,7,freq,7,null,7,like,7,dt,7,df);
221  cout << sout;
222  fprintf(fp,"%s\n",sout);
223  }
224 
225  fclose(fp);
226 
227 
228  return;
229 }
230 
detectorParams getDetectorParams()
Definition: detector.cc:218
char xtitle[1024]
void ReadNetCluster(TString ifroot, TString odir=".", int entry=0)
int nIFO
std::vector< pixel > cluster
Definition: wavepath.hh:61
size_t frequency
Definition: netpixel.hh:111
float likelihood
Definition: netpixel.hh:114
int n
Definition: cwb_net.C:28
TString("c")
char odir[1024]
void PlotNetCluster(watplot *WTS, netcluster *pwc, double seggps, TString odir)
netpixel pix(nifo)
netcluster * pwc
Definition: cwb_job_obj.C:38
size_t layers
Definition: netpixel.hh:112
std::vector< double > oSNR[NIFO_MAX]
std::vector< vector_int > cList
Definition: netcluster.hh:397
canvas cd(1)
i drho i
double rate
Definition: netcluster.hh:378
TList * list
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
void plot(wavearray< double > &, char *=NULL, int=1, double=0., double=0., bool=false, float=0., float=0., bool=false, float=0., bool=false)
Definition: watplot.cc:150
bool core
Definition: netpixel.hh:120
char ifo[NIFO_MAX][8]
TH2F * hist2D
Definition: watplot.hh:193
fprintf(stdout,"start=%f duration=%f rate=%f\, x.start(), x.size()/x.rate(), x.rate())
void clear()
Definition: watplot.hh:58
TCanvas * canvas
Definition: watplot.hh:192
wavearray< double > freq
Definition: Regression_H1.C:79
int ReadDataFromROOT(TString ifile, XPARMS *xparms)
i() int(T_cor *100))
const int NIFO_MAX
Definition: wat.hh:22
char fname[1024]
watplot * WTS
std::vector< double > iSNR[NIFO_MAX]
size_t time
Definition: netpixel.hh:110
double * entry
Definition: cwb_setcuts.C:224
TFile * ifile
TFile * froot
netpixel * getPixel(size_t n, size_t i)
Definition: netcluster.hh:413
double dt
#define RATE
int nifo
TCanvas * canvas
char Name[16]
Definition: detector.hh:327
int gENTRY
void DumpNetCluster(netcluster *pwc, int cid, int nifo, TString odir)
double df
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
TTree * itree
fclose(ftrig)
float rate
Definition: netpixel.hh:113
float null
Definition: netpixel.hh:115
exit(0)