Logo coherent WaveBurst  
Library Reference Guide
Logo
CreateGraphRadiusIFAR.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Francesco Salemi
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 // Create Graphs of Radius vs IFAR
20 // Note : this macro is used to generate the CBC report
21 // Author : Francesco Salemi
22 
23 #include "TROOT.h"
24 #include "TSystem.h"
25 #include "TFile.h"
26 #include "TTree.h"
27 #include "TTreeIndex.h"
28 #include <fstream>
29 #include <iostream>
30 #include "TGraphErrors.h"
31 #include "TMultiGraph.h"
32 #include "TCanvas.h"
33 #include "TH1F.h"
34 #include "TMath.h"
35 #include "TH1F.h"
36 #include "TH2F.h"
37 #include <TComplex.h>
38 #include <TStyle.h>
39 #include <TRandom.h>
40 #include "TVector3.h"
41 #include "TRotation.h"
42 #include "Math/Vector3Dfwd.h"
43 #include "Math/Rotation3D.h"
44 
45 #define MINMAXIFAR 4278.
46 
47 //TGraphErrors* CreateGraphRadiusIFAR (char* sim_file_name, char* mdc_file_name, float shell_volume, double liveTot=1e6, float T_ifar=0.0, float T_win=0.2, int TRIALS = 1, TString SEL="", Color_t color=kBlue, TString opt="default"){
48 TGraphErrors* CreateGraphRadiusIFAR(char* sim_file_name, char* mdc_file_name, TString SEL, float shell_volume, Color_t color=kBlue, TString opt="default",double liveTot=1e6, float T_ifar=0.0, float T_win=0.2, int TRIALS = 1, int nIFO = 2){
49 
50  float myifar, netcc[3];
51  float rho[2];
52  double mytime[6];
53  float factor, distance, mchirp;
54  float mass[2];
55  float spin[6];
56  // float chi[3];
57 
58  float chirp[6];
59  float range[2];
60 
61  CWB::CBCTool cbcTool;
62 
63  TFile* filein = new TFile(sim_file_name);
64  TTree* sim_org = nullptr;
65  filein->GetObject("waveburst",sim_org);
66  gROOT->cd();
67  if(!sim_org->GetListOfBranches()->FindObject("chip")) {
68  cout << "Adding Chi_p branch to wave tree"<<endl;
69  cbcTool.AddChip(sim_file_name, "waveburst");
70  }
71  TTree* sim = sim_org->CopyTree(SEL);
72  sim->SetBranchAddress("mass", mass);
73  sim->SetBranchAddress("factor", &factor);
74  sim->SetBranchAddress("range", range);
75  sim->SetBranchAddress("chirp", chirp);
76  sim->SetBranchAddress("rho", rho);
77  sim->SetBranchAddress("netcc", netcc);
78  sim->SetBranchAddress("ifar", &myifar);
79  sim->SetBranchAddress("time", mytime);
80  sim->SetBranchAddress("spin", spin);
81 
82 
83  TFile* filein2 = new TFile(mdc_file_name);
84  TTree* mdc_org = nullptr;
85  filein2->GetObject("mdc",mdc_org);
86  gROOT->cd();
87  SEL.ReplaceAll("chirp[0]","mchirp");
88  if(!mdc_org->GetListOfBranches()->FindObject("chip")) {
89  cout << "Adding Chi_p branch to mdc tree" << endl;
90  cbcTool.AddChip(mdc_file_name, "mdc");
91  }
92  TTree* mdc = mdc_org->CopyTree(SEL);
93  mdc->SetBranchAddress("time",mytime);
94  mdc->SetBranchAddress("mass", mass);
95  mdc->SetBranchAddress("factor", &factor);
96  mdc->SetBranchAddress("distance", &distance);
97  mdc->SetBranchAddress("mchirp", &mchirp);
98  mdc->SetBranchAddress("spin", spin);
99 
100  double dV = 0.0;
101  std::vector<double> vdv;
102  std::vector<double> vifar;
103  std::vector<double> vrho;
104  std::vector<double> vV;
105  std::vector<double> veV;
106  std::vector<double> vR;
107  std::vector<double> veR;
108  std::vector<double> vsifar;
109  std::vector<double> vseifar;
110  std::vector<double> vVr;
111  std::vector<double> veVr;
112  std::vector<double> vRr;
113  std::vector<double> veRr;
114  std::vector<double> vsrho;
115  TGraphErrors* co_gr = nullptr;
116 
117  int nevts = (int)mdc->GetEntries();
118  if (nevts == 0) {
119  cout <<"No events left after cut!"<<endl;
120  vR.push_back(0.0);
121  vsifar.push_back(0.0);
122  co_gr = new TGraphErrors(vR.size(), &vsifar[0], &vR[0], 0, 0);
123  return co_gr;
124  }
125  float shell_volume_per_injection = shell_volume/nevts;
126  int ifactor;
127  float maxIFAR = 0.0;
128  float CYS = 86400.*365.25;
129  if (sim->GetListOfBranches()->FindObject("ifar")) {
130  maxIFAR =
131  TMath::CeilNint(sim->GetMaximum("ifar") / CYS );
132  // cout << "Maximum empirically estimated IFAR : " << maxIFAR << " [years]" << endl;
133  } else {
134  cout << "Missing ifar branch: either use cbc_plots or add it "
135  "to wave tree."
136  << endl;
137  exit(1);
138  }
139  if(opt.Contains("rho")) {
140  sim->BuildIndex("0","rho[1]*10000"); //BEWARE rho[1] could be search depandent!
141  } else {
142  sim->BuildIndex("ifar","rho[1]*1000"); //BEWARE rho[1] could be search depandent!
143  }
144  TTreeIndex *I1=(TTreeIndex*)sim->GetTreeIndex(); // get the tree index
145  Long64_t* index1=I1->GetIndex(); //create an array of entries in sorted order
146 
147  //cout << nevts << " injected signals " << sim->GetEntries("ifar>0") << " recovered signals" <<endl;
148  int countv = 0;
149  int countvifar = 0;
150  bool Error = false;
151  double mpcTscale = 1.0;
152  for (int g = 0; g < (int)sim->GetEntries(); g++) {
153  sim->GetEntry(index1[g]);
154  ifactor = (int)factor - 1;
155  //cout << "g=" << g << " trueindex=" <<index1[g] <<" IFAR=" << myifar/CYS << " RHO1=" << rho[1] <<endl;
156  if (myifar <= T_ifar * CYS) {
157  countvifar++;
158  //cout << g << " " <<index1[g] <<" " << myifar/CYS << " ";
159  continue;
160 
161  }
162 
163  if ((mytime[0] - mytime[nIFO]) < -T_win ||
164  (mytime[0] - mytime[nIFO]) > 2 * T_win) {
165  countv++;
166  continue;
167  } // NOT checking for detector 1 and 2: very small bias...
168 
169  if (opt.Contains("DDistrVolume")){
170  dV = shell_volume_per_injection;
171  }
172  else if (opt.Contains("DDistrUniform")){
173  dV = pow(range[1], 2) * shell_volume_per_injection;
174  }
175  else if (opt.Contains("DDistrChirpMass")){
176  dV = pow(range[1], 2) * shell_volume_per_injection * pow(chirp[0] / 1.22,5./6.);
177  }
178  else if (opt.Contains("Redshift")){
179  dV = VT/(double)nevts;
180  mpcTscale = Tscale*1e-9; //Conversion from Gpc^3 to Mpc^3
181  }
182  else {
183  if(!Error){
184  cout << "No defined distance distribution? "
185  "WARNING: Assuming uniform in volume"
186  << endl;
187  Error=1;
188  }
189  dV = shell_volume_per_injection;
190  }
191  //vdv.push_back(dV+internal_volume);
192  vdv.push_back(dV);
193  vifar.push_back(myifar);
194  vrho.push_back(rho[1]);
195 
196  }
197 
198  //cout << endl;
199  //cout << countvifar << " events vetoed by T_ifar : " << T_ifar << endl;
200  //cout << countv << " events vetoed by T_win" << endl;
201  //cout << vdv.size() << " events selected" << endl;
202 
203  //cout << "DEB2" << endl;
204  vV.push_back(vdv[vdv.size()-1]);
205  veV.push_back(pow(vdv[vdv.size()-1], 2));
206  if(vifar[vdv.size()-1]< MINMAXIFAR*CYS ){vsifar.push_back(vifar[vdv.size()-1]);}
207  else {vsifar.push_back(MINMAXIFAR*CYS);}
208  vVr.push_back(vdv[vdv.size()-1]);
209  veVr.push_back(pow(vdv[vdv.size()-1], 2));
210  vsrho.push_back(vrho[vdv.size()-1]);
211  //cout << "vifar[vdv.size()-1] = " << vifar[vdv.size()-1] << endl;
212  //cout << "vsifar[0] = " << vsifar[0] << endl;
213  // break;
214  int mcount_ifar = 0;
215  int mcount_rho = 0;
216  for (int i = vdv.size()-1; i >= 0; i--) {
217  if (vifar[i] == 0) {
218  continue;
219  }
220  if (vifar[i] > vsifar[0]) {
221  vV[0] += vdv[i];
222  veV[0] += pow(vdv[i], 2);
223 
224  } else if (vifar[i] == vsifar[mcount_ifar]) {
225  vV[mcount_ifar] += vdv[i];
226  veV[mcount_ifar] += pow(vdv[i], 2);
227  } else {
228  vsifar.push_back(vifar[i]);
229  vseifar.push_back(TMath::Sqrt(TMath::Nint(liveTot * vifar[i])));
230  vV.push_back(vV[mcount_ifar] + vdv[i]);
231  veV.push_back(veV[mcount_ifar] + pow(vdv[i], 2));
232  mcount_ifar++;
233  }
234  // cout << i << " " << vsifar[mcount_ifar] << " " << vV[mcount_ifar] << endl;
235  if (vrho[i] == vsrho[mcount_rho]) {
236  vVr[mcount_rho] += vdv[i];
237  veVr[mcount_rho] += pow(vdv[i], 2);
238  } else {
239  vsrho.push_back(vrho[i]);
240  vVr.push_back(vVr[mcount_rho] + vdv[i]);
241  veVr.push_back(veVr[mcount_rho] + pow(vdv[i], 2));
242  mcount_rho++;
243  }
244  }
245  //cout << "Length of ifar/volume vector: " << vV.size() << endl;
246  //cout << "Length of rho/volume vector: " << vVr.size() << endl;
247 // float
248  for (int i = 0; i < (int) vV.size(); i++) {
249  veV[i] = TMath::Sqrt(veV[i]);
250  vsifar[i] /= (TRIALS * CYS);
251  vR.push_back(
252  pow(3. / (4. * TMath::Pi()* mpcTscale) * vV[i], 1. / 3.));
253  veR.push_back(pow(3. / (4 * TMath::Pi() * mpcTscale), 1. / 3.) *
254  1. / 3 * pow(vV[i], -2. / 3.) * veV[i]);
255  // cout << i << " " << vsifar[i] << " " << vR[i] << " " <<vV[i] <<endl;
256  }
257 
258  for (int i = 0; i < (int) vVr.size(); i++) {
259  veVr[i] = TMath::Sqrt(veVr[i]);
260  vRr.push_back(
261  pow(3. / (4. * TMath::Pi()* mpcTscale) * vVr[i], 1. / 3.));
262  veRr.push_back(pow(3. / (4 * TMath::Pi() * mpcTscale), 1. / 3.) *
263  1. / 3 * pow(vVr[i], -2. / 3.) * veVr[i]);
264  //cout << vsrho[i] << " ";
265 
266  }
267  //cout << endl;
268 
269  //std::vector<TGraphErrors> v_gr(2);
270  //TGraphErrors *co_gr = new TGraphErrors[2];
271  //TGraphErrors co_gr;
272  //TGraphErrors* co_gr = new TGraphErrors(vR.size(), &vsifar[0], &vR[0], 0, &veR[0]);
273  if(opt.Contains("rho")) {co_gr = new TGraphErrors(vRr.size(), &vsrho[0], &vRr[0], 0, &veRr[0]);}
274  else {co_gr = new TGraphErrors(vR.size(), &vsifar[0], &vR[0], 0, &veR[0]);}
275  //co_gr[0] = TGraphErrors(vR.size(), &vsifar[0], &vR[0], 0, &veR[0]);
276  //co_gr->GetYaxis()->SetTitle("Sensitive Distance [Mpc]");
277  //co_gr->GetXaxis()->SetTitle("Inverse False Alarm Rate [yr]");
278  // co_gr->GetXaxis()->SetLimits(MINIFAR,MAXIFAR);
279  // co_gr->GetYaxis()->SetRangeUser(MINRADIUS,MAXRADIUS);
280  co_gr->GetXaxis()->SetTitleOffset(1.3);
281  co_gr->GetYaxis()->SetTitleOffset(1.25);
282  co_gr->GetXaxis()->SetTickLength(0.01);
283  co_gr->GetYaxis()->SetTickLength(0.01);
284  co_gr->GetXaxis()->CenterTitle(kTRUE);
285  co_gr->GetYaxis()->CenterTitle(kTRUE);
286  co_gr->GetXaxis()->SetTitleFont(42);
287  co_gr->GetXaxis()->SetLabelFont(42);
288  co_gr->GetYaxis()->SetTitleFont(42);
289  co_gr->GetYaxis()->SetLabelFont(42);
290  co_gr->SetMarkerStyle(20);
291  co_gr->SetMarkerSize(1.0);
292  co_gr->SetMarkerColor(1);
293  co_gr->SetLineColor(color);
294  co_gr->SetLineWidth(3);
295  co_gr->SetTitle("");
296  co_gr->SetFillColor(color);
297  co_gr->SetFillStyle(3001);
298  //co_gr->Draw("aple3");
299 
300  filein->Close();
301  filein2->Close();
302  delete filein, filein2;
303  delete mdc, mdc_org, sim, sim_org;
304  vifar.clear(); vdv.clear(); vrho.clear(); vsifar.clear(); vseifar.clear(); vsrho.clear();
305  vR.clear(); veR.clear(); veRr.clear(); vV.clear(); veV.clear(); vRr.clear(); vVr.clear();
306  veVr.clear();
307  return co_gr;
308 
309  //exit(0);
310 
311 }
312 
double rho
float Tscale
double T_ifar
TChain sim("waveburst")
static double g(double e)
Definition: GNGen.cc:116
std::vector< double > vseifar
float factor
TString opt
TString("c")
float CYS
TString mdc[4]
std::vector< double > vR
i drho i
TGraphErrors * CreateGraphRadiusIFAR(char *sim_file_name, char *mdc_file_name, TString SEL, float shell_volume, Color_t color=kBlue, TString opt="default", double liveTot=1e6, float T_ifar=0.0, float T_win=0.2, int TRIALS=1, int nIFO=2)
#define MINMAXIFAR
std::vector< double > vdv
#define nIFO
i() int(T_cor *100))
double Pi
int ifactor
std::vector< double > vsifar
float mchirp
std::vector< double > veR
static void AddChip(TString filein, TString treename)
Definition: CBCTool.cc:1129
double liveTot
std::vector< double > vifar
float spin[6]
double e
double VT
char sim_file_name[1024]
std::vector< double > veV
double T_win
cout<< endl;if(write_ascii) { fev.close();for(int l=0;l< nfactor;l++) { fev_single[l].close();} } cout<< "Recovered entries: "<< cnt<< endl;cout<< "Recovered entries: "<< cnt2<< endl;cout<< "Recovered entries cut by frequency: "<< cntfreq<< endl;cout<< "Recovered entries vetoed: "<< countv<< endl;cout<< "dV : "<< dV<< " dV1 : "<< dV1<< endl;internal_volume=4./3. *TMath::Pi() *pow(minDistance[0]/1000., 3.);if(INCLUDE_INTERNAL_VOLUME) { for(int i=0;i< vdv.size();i++) { if(vdv[i] > 0.0) { vdv[i]+=internal_volume;} } for(int i=0;i< RHO_NBINS;i++) { if(Vrho[i] > 0.0) { Vrho[i]+=internal_volume;} } for(int i=0;i< NBINS_MTOT+1;i++) { for(int j=0;j< NBINS_SPIN+1;j++) { if(spin_mtot_volume[i][j] > 0.0) { spin_mtot_volume[i][j]+=internal_volume;} } } for(int i=0;i< NBINS_mass;i++) { for(int j=0;j< NBINS_mass2;j++) { if(volume[i][j] > 0.0) { volume[i][j]+=internal_volume;} } } } Int_t *mindex=new Int_t[vdv.size()];TMath::Sort((int) vdv.size(), &vifar[0], mindex, true);std::vector< double > vV
double mytime[6]
float distance
float mass[2]
float * shell_volume
char mdc_file_name[1024]
int TRIALS
exit(0)