Logo coherent WaveBurst  
Library Reference Guide
Logo
AddRedshift2WAVE.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 // ---------------------------------------------------------------------------------
20 // INFOS
21 // ---------------------------------------------------------------------------------
22 
23 // This macro is used to add to the wave root file the redshift value reported in the mdc file
24 //
25 // how to use
26 // Ex: root -l -b 'AddRedshift2WAVE.C("merge/wave_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.root")'
27 //
28 // outputs:
29 // new output wave file with leaf redshift
30 // merge/wave_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.U_redshift.root
31 //
32 // symbolic link to the merge/mdc_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.root
33 // merge/mdc_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.U_redshift.root
34 //
35 // symbolic link to the merge/merge_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.lst
36 // merge/merge_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.U_redshift.lst
37 //
38 
39 
40 #define MAX_TREE_SIZE 100000000000LL
41 
42 int binarySearch(double array[], int start, int end, double key);
43 int binarySearch(double array[], int size, double key);
44 
45 void AddRedshift2WAVE(TString ifname) {
46 //
47 // ifname = is the name of the input wave root simulation file
48 // Ex: "merge/wave_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.root"
49 //
50 
51  // ---------------------------------------------------------------
52  // Open input WAVE ROOT file
53  // ---------------------------------------------------------------
54 
55  TString iwfile = ifname;
56  cout << endl << " " << iwfile << endl;
57 
58  TFile *iwroot = TFile::Open(iwfile);
59  if(iwroot==NULL) {cout << "Error opening file: " << iwfile << endl;exit(1);}
60 
61  TTree* iwtree = (TTree *) gROOT->FindObject("waveburst");
62  if(iwtree==NULL) {cout << "waveburst tree not found !!!" << endl;exit(1);}
63  int wentries = (int)iwtree->GetEntries();
64  cout << endl << " wntries = " << wentries << endl;
65 
66  // get number of detectors in the network
67  TList* ifoList = iwtree->GetUserInfo();
68  int nIFO = ifoList->GetSize();
69  cout << endl << " nIFO = " << nIFO << endl << endl;
70 
71  // ---------------------------------------------------------------
72  // Open input MDC ROOT file
73  // ---------------------------------------------------------------
74 
75  TString imfile = ifname;
76  imfile.ReplaceAll("wave_","mdc_");
77  cout << " " << imfile << endl;
78 
79  TFile *imroot = TFile::Open(imfile);
80  if(imroot==NULL) {cout << "Error opening file: " << imfile << endl;exit(1);}
81 
82  TTree* imtree = (TTree *) gROOT->FindObject("mdc");
83  if(imtree==NULL) {cout << "mdc tree not found !!!" << endl;exit(1);}
84 
85  // sort mdc injection wrt time of the first detector
86  imtree->Draw("time[0]:redshift","","goff");
87  int imentries = (int)imtree->GetSelectedRows();
88  int *mindex = new int[imentries];
89  double* imtime = imtree->GetV1();
90  double* imredshift = imtree->GetV2();
91  TMath::Sort(imentries,imtime,mindex,false);
92 
93  // create sorted mdc time array
94  double* stime = new double[imentries];
95  for (Int_t i=0;i<imentries;i++) stime[i] = imtime[mindex[i]];
96 
97  // ---------------------------------------------------------------
98  // create updated output WAVE ROOT file with new redshift leaf
99  // ---------------------------------------------------------------
100 
101  TString owfile = ifname;
102  owfile.ReplaceAll(".root",".U_redshift.root");
103  cout << endl << " new output tree file = " << owfile << endl << endl;
104 
105  //open new file to store the updated tree
106  TFile owroot(owfile,"recreate");
107  //Create an empty clone of the original input iwtree
108  TTree *owtree = (TTree*)iwtree->CloneTree(0);
109  owtree->SetMaxTreeSize(MAX_TREE_SIZE);
110 
111  // add redshift leaf to owtree
112  TBranch* branch;
113  bool redshift_exists=false;
114  TIter next(iwtree->GetListOfBranches());
115  while ((branch=(TBranch*)next())) {
116  if(TString("redshift").CompareTo(branch->GetName())==0) redshift_exists=true;
117  }
118  next.Reset();
119  float wredshift;
120  if (redshift_exists) iwtree->SetBranchAddress("redshift",&wredshift);
121  else owtree->Branch("redshift",&wredshift,"redshift/F");
122 
123  double* iwtime = new double[2*nIFO];
124  iwtree->SetBranchAddress("time",iwtime);
125 
126  cout << endl;
127  for (Int_t i=0;i<wentries;i++) {
128  if (i%1000==0 && i>0) cout << " write entry : " << i << endl;
129  iwtree->GetEntry(i);
130  int sindex = binarySearch(stime, 0, imentries, iwtime[nIFO]);
131  wredshift = imredshift[mindex[sindex]];
132  owtree->Fill();
133  }
134  owtree->Write();
135  delete [] mindex;
136 
137  // ---------------------------------------------------------------
138  // create symbolic link to input mdc root file & merge lst
139  // ---------------------------------------------------------------
140 
141  TString cmd;
142 
143  TString omfile = ifname;
144  omfile.ReplaceAll("wave_","mdc_");
145  omfile.ReplaceAll(".root",".U_redshift.root");
146  TString imfile_base = gSystem->BaseName(imfile.Data()); // base name
147  cmd = TString::Format("ln -sf %s %s",imfile_base.Data(),omfile.Data());
148  cout << endl << " " << cmd << endl << endl;
149  gSystem->Exec(cmd.Data());
150 
151  TString ilfile = ifname;
152  ilfile.ReplaceAll("wave_","merge_");
153  ilfile.ReplaceAll(".root",".lst");
154  TString olfile = ifname;
155  olfile.ReplaceAll("wave_","merge_");
156  olfile.ReplaceAll(".root",".U_redshift.lst");
157  TString ilfile_base = gSystem->BaseName(ilfile.Data()); // base name
158  cmd = TString::Format("ln -sf %s %s",ilfile_base.Data(),olfile.Data());
159  cout << endl << " " << cmd << endl << endl;
160  gSystem->Exec(cmd.Data());
161 
162  exit(0);
163 }
164 
165 //______________________________________________________________________________
166 int binarySearch(double array[], int start, int end, double key) {
167  // Determine the search point.
168  // int searchPos = end + ((start - end) >> 1);
169  // int searchPos = (start + end) / 2;
170  int searchPos = (start + end) >> 1;
171  // If we crossed over our bounds or met in the middle, then it is not here.
172  if (start > end)
173  return -1;
174  // Search the bottom half of the array if the query is smaller.
175  if (array[searchPos] > key)
176  return binarySearch (array, start, searchPos - 1, key);
177  // Search the top half of the array if the query is larger.
178  if (array[searchPos] < key)
179  return binarySearch (array, searchPos + 1, end, key);
180  // If we found it then we are done.
181  if (array[searchPos] == key)
182  return searchPos;
183 
184  return -1;
185 }
186 
187 //______________________________________________________________________________
188 int binarySearch(double array[], int size, double key) {
189  return binarySearch(array, 0, size - 1, key);
190 }
191 
TString("c")
Long_t size
i drho i
#define nIFO
int binarySearch(double array[], int start, int end, double key)
i() int(T_cor *100))
TIter next(twave->GetListOfBranches())
void AddRedshift2WAVE(TString ifname)
#define MAX_TREE_SIZE
TBranch * branch
char cmd[1024]
exit(0)