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