Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_setcuts.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 // apply selection on leaves to merged root file : used by the cwb_setcuts command
20 
21 {
22 
24 
25  int estat;
26  Long_t id,size,flags,mt;
27  char cmd[1024];
28 
29  cwb_merge_label = TString(gSystem->Getenv("CWB_MERGE_LABEL"));
30  TString cwb_jcuts_tree = TString(gSystem->Getenv("CWB_JCUTS_TREE"));
31  TString cwb_wcuts_tree = TString(gSystem->Getenv("CWB_WCUTS_TREE"));
32  TString cwb_cuts_label = TString(gSystem->Getenv("CWB_CUTS_LABEL"));
33  TString cwb_mcuts_tree = TString(gSystem->Getenv("CWB_MCUTS_TREE"));
34  TString cwb_lcuts_tree = TString(gSystem->Getenv("CWB_LCUTS_TREE"));
35  TString cwb_setcuts_options = TString(gSystem->Getenv("CWB_SETCUTS_OPTIONS"));
36 
38 
39  // check if cwb_setcuts_options contains '--' than cwb_setcuts_options
40  // is used to extract all setcuts parameters
43  if(cwb_setcuts_options.Contains("--")) {
44  TString option="";
45  // get the cwb_tcuts_tree
46  cwb_tcuts_tree = TB.getParameter(cwb_setcuts_options,"--tcuts");
47  // get the cwb_wcuts_tree
48  cwb_wcuts_tree = TB.getParameter(cwb_setcuts_options,"--wcuts");
49  // get the cwb_jcuts_tree
50  cwb_jcuts_tree = TB.getParameter(cwb_setcuts_options,"--jcuts");
51  // get the cwb_mcuts_tree
52  cwb_mcuts_tree = TB.getParameter(cwb_setcuts_options,"--mcuts");
53  // get the cwb_lcuts_tree
54  cwb_lcuts_tree = TB.getParameter(cwb_setcuts_options,"--lcuts");
55  // get the cwb_cuts_label
56  cwb_cuts_label = TB.getParameter(cwb_setcuts_options,"--label");
57  // get the cwb_unique_evt
58  cwb_unique_evt = TB.getParameter(cwb_setcuts_options,"--unique");
59  }
60  // if cwb_tcuts_tree!="" than cwb_tcuts_tree is added to cwb_wcuts_tree
61  if(cwb_tcuts_tree!="") {
62  // check if tcut is defined
63  TGlobal *global=(TGlobal*)gROOT->GetListOfGlobals()->FindObject(cwb_tcuts_tree.Data());
64  if(global==NULL) {
65  cout << "cwb_setcuts.C : Error - tcut " << cwb_tcuts_tree << " is not defined " << endl;
66  cout << " must be included in the user_pparameters.C file" << endl;
67  gSystem->Exit(1);
68  }
69  TCut *tcut = (TCut*)global->GetAddress();
70  if(TString(tcut->GetName())!=cwb_tcuts_tree) {
71  cout << "cwb_setcuts.C : Error - tcut " << cwb_tcuts_tree << " not correspond to global Tcut name" << endl;
72  cout << " must be included in the user_pparameters.C file" << endl;
73  gSystem->Exit(1);
74  }
75  cwb_tcuts_tree = tcut->GetTitle(); // extract TCut
76  cout << "cwb_tcuts_tree : " << cwb_tcuts_tree << endl;
77  if(cwb_wcuts_tree=="") {
78  cwb_wcuts_tree = cwb_tcuts_tree;
79  } else {
80  TString cwb_wcuts = cwb_tcuts_tree;
81  cwb_wcuts = TString("(")+cwb_wcuts_tree+")&&("+cwb_tcuts_tree+")";
82  cwb_wcuts_tree = cwb_wcuts;
83  }
84  cout << "cwb_wcuts_tree : " << cwb_wcuts_tree << endl;
85  }
86  // check if cwb_unique_evt can be applied
87  if((simulation==0)&&(cwb_unique_evt=="true")) {
88  cout << "cwb_setcuts.C : Error - unique option can be applied only to simulated data" << endl;
89  gSystem->Exit(1);
90  }
91  // check if cwb_cuts_label is defined
92  if((cwb_wcuts_tree!="")&&(cwb_cuts_label=="")) {
93  cout << "cwb_setcuts.C : Error - cuts label not defined" << endl;
94  gSystem->Exit(1);
95  }
96  // check if cwb_cuts_label contains '.'
97  if(cwb_cuts_label.Contains(".")) {
98  cout << "cwb_setcuts.C : Error - cwb_cuts_label " << cwb_cuts_label
99  << " can not contains '.'" << endl << endl;
100  gSystem->Exit(1);
101  }
102  // check if cwb_wcuts_tree contains run leaf
103  if(cwb_wcuts_tree.Contains("run")) {
104  cout << "cwb_setcuts.C : Error - cwb_wcuts_tree " << cwb_jcuts_tree
105  << " can not contains leaf : run" << endl << endl;
106  gSystem->Exit(1);
107  }
108  // check if cwb_wcuts_tree contains slag leaf
109  if(cwb_wcuts_tree.Contains("slag")) {
110  cout << "cwb_setcuts.C : Error - cwb_wcuts_tree " << cwb_jcuts_tree
111  << " can not contains leaf : slag" << endl << endl;
112  gSystem->Exit(1);
113  }
114  // check if cwb_wcuts_tree contains lag leaf
115  if(cwb_wcuts_tree.Contains("lag")) {
116  cout << "cwb_setcuts.C : Error - cwb_wcuts_tree " << cwb_jcuts_tree
117  << " can not contains leaf : lag" << endl << endl;
118  gSystem->Exit(1);
119  }
120  if(cwb_wcuts_tree=="") cwb_wcuts_tree="run>0";
121  if(cwb_jcuts_tree=="") cwb_jcuts_tree="run>0";
122 
123  // create wcuts
124  TString cwb_wcuts = cwb_wcuts_tree;
125  cwb_wcuts = TString("(")+cwb_wcuts_tree+")&&("+cwb_jcuts_tree+")";
126  if(cwb_lcuts_tree!="") cwb_wcuts = TString("(")+cwb_wcuts+")&&("+cwb_lcuts_tree+")";
127 
128  // create mcuts
129  TString cwb_mcuts = cwb_mcuts_tree;
130  if(cwb_mcuts!="")
131  cwb_mcuts = TString("(")+cwb_mcuts_tree+")&&("+cwb_jcuts_tree+")";
132  else
133  cwb_mcuts = cwb_jcuts_tree;
134 
135  // create input wave root cuts file name
136  char iwfname[1024];
137  sprintf(iwfname,"wave_%s.%s.root",data_label,cwb_merge_label.Data());
138 
139  // create output wave root cuts file name
140  TString uwfname = mdir+"/"+iwfname;
141  uwfname.ReplaceAll(".root",TString(".C_U.root"));
142 
143  // if cwb_unique_evt=true then create a wave root file with unique
144  // reconstructed events
145  if(cwb_unique_evt=="true") {
146  TB.setUniqueEvents(mdir+"/"+iwfname, uwfname, nIFO, pp_irho);
147  // create symbolic link of mdc file
148  char imfname[1024];
149  sprintf(imfname,"mdc_%s.%s.root",data_label,cwb_merge_label.Data());
150  TString omfname = uwfname;
151  omfname.ReplaceAll("wave_","mdc_");
152  omfname.Remove(0,omfname.Last('/')+1); // strip path
153  cout << omfname << endl;
154  estat = gSystem->GetPathInfo(mdir+"/"+imfname,&id,&size,&flags,&mt);
155  if(estat==0) {
156  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),imfname,omfname.Data());
157  cout << cmd << endl;
158  gSystem->Exec(cmd);
159  }
160  // create symbolic link of merge list file
161  char ilstfname[1024];
162  sprintf(ilstfname,"merge_%s.%s.lst",data_label,cwb_merge_label.Data());
163  TString olstfname = uwfname;
164  olstfname.ReplaceAll("wave_","merge_");
165  olstfname.ReplaceAll(".root",".lst");
166  olstfname.Remove(0,olstfname.Last('/')+1); // strip path
167  cout << olstfname << endl;
168  estat = gSystem->GetPathInfo(mdir+"/"+ilstfname,&id,&size,&flags,&mt);
169  if (estat==0) {
170  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),ilstfname,olstfname.Data());
171  cout << cmd << endl;
172  gSystem->Exec(cmd);
173  }
174  }
175  if((cwb_wcuts_tree="")&&(cwb_cuts_label=="")) exit(0);
176 
177  if(cwb_unique_evt=="true") {
178  sprintf(iwfname,"wave_%s.%s.C_U.root",data_label,cwb_merge_label.Data());
179  cwb_merge_label+=".C_U";
180  }
181 
182  // create output wave root cuts file name
183  TString owfname = mdir+"/"+iwfname;
184  owfname.ReplaceAll(".root",TString(".C_")+cwb_cuts_label+".root");
185 
186  // create a merge*.lst file name & run selection cuts
187  vector<int> jobList;
188  char ilstfname[1024];
189  sprintf(ilstfname,"merge_%s.%s.lst",data_label,cwb_merge_label.Data());
191  olstfname.ReplaceAll("wave_","merge_");
192  olstfname.ReplaceAll(".root",".lst");
193  olstfname.Remove(0,olstfname.Last('/')+1); // strip path
194  cout << olstfname << endl;
195  if(cwb_jcuts_tree=="") { // no jobs cuts -> create a symbolic link
196  estat = gSystem->GetPathInfo(mdir+"/"+ilstfname,&id,&size,&flags,&mt);
197  if (estat==0) {
198  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),ilstfname,olstfname.Data());
199  cout << cmd << endl;
200  gSystem->Exec(cmd);
201  }
202  } else {
203  // build list of selected jobs
204  vector<TString> jobFileList;
205  vector<int> jobList = CWB::Toolbox::getMergeJobList(mdir+"/"+ilstfname,jobFileList);
206  cout << "Number of jobs in the merged lists : " << jobList.size() << endl;
207  // create a dummy tree, fill with merged jobs, apply cuts, get selected jobs
208  TTree jtree("jtree","jtree");
209  int run; jtree.Branch("run",&run,"run/I");
210  TString *jfname = new TString();
211  jtree.Branch("jfname", &jfname);
212  // fill with merged jobs
213  for(int i=0;i<jobList.size();i++) {run=jobList[i]; *jfname=jobFileList[i]; jtree.Fill();}
214  // check cuts
215  TTreeFormula formula("trCuts", cwb_jcuts_tree.Data(), &jtree);
216  int err = formula.Compile(cwb_jcuts_tree.Data());
217  if(err) {
218  cout << "cwb_setcuts.C - wrong input cuts " << cwb_jcuts_tree << endl << endl;
219  gSystem->Exit(1);
220  }
221  // get selected jobs
222  jtree.Draw("Entry$",cwb_jcuts_tree.Data(),"goff");
223  int jsel = jtree.GetSelectedRows();
224  double* entry = jtree.GetV1(); // get selected jobs
225  jobList.clear();
226  jobFileList.clear();
227  for(int i=0;i<jsel;i++) {
228  jtree.GetEntry(entry[i]);
229  //cout << i << " " << run << " " << jfname->Data() << endl;
230  jobList.push_back(run); // list of selected jobs
231  jobFileList.push_back(jfname->Data()); // list of selected file jobs
232  }
233  CWB::Toolbox::dumpFileList(jobFileList,mdir+"/"+olstfname);
234  }
235 
236  // apply wcuts to wave file
237  int nsel = CWB::Toolbox::setCuts(iwfname,mdir,mdir,"waveburst",cwb_wcuts,cwb_cuts_label);
238  if(nsel<=0) {
239  cout << "cwb_setcuts.C : Error - Number of selected waveburst entries = " << nsel << endl << endl;
240  gSystem->Exit(1);
241  }
242  cout << "cwb_setcuts.C : Number of selected waveburst entries = " << nsel << endl;
243 
244  // apply mcuts to mdc file
245  if(simulation) {
246  char imfname[1024];
247  sprintf(imfname,"mdc_%s.%s.root",data_label,cwb_merge_label.Data());
248  int nsel = CWB::Toolbox::setCuts(imfname,mdir,mdir,"mdc",cwb_mcuts,cwb_cuts_label);
249  cout << "cwb_setcuts.C : Number of selected mdc entries = " << nsel << endl << endl;
250  if(nsel<0) { // create a symbolic link to mdc*.root file name
251  TString omfname = owfname;
252  omfname.ReplaceAll("wave_","mdc_");
253  omfname.Remove(0,omfname.Last('/')+1); // strip path
254  cout << omfname << endl;
255  estat = gSystem->GetPathInfo(mdir+"/"+imfname,&id,&size,&flags,&mt);
256  if(estat==0) {
257  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),imfname,omfname.Data());
258  cout << cmd << endl;
259  gSystem->Exec(cmd);
260  }
261  }
262  }
263 
264  // apply cuts to live file
265  if(!simulation) {
266  char ilfname[1024];
267  sprintf(ilfname,"live_%s.%s.root",data_label,cwb_merge_label.Data());
268  TString cwb_live_cuts = cwb_jcuts_tree;
269  if(cwb_lcuts_tree!="") cwb_live_cuts = TString("(")+cwb_live_cuts+")&&("+cwb_lcuts_tree+")";
270  int nsel = -1;
271  if(cwb_live_cuts!="run>0") {
272  nsel = CWB::Toolbox::setCuts(ilfname,mdir,mdir,"liveTime",cwb_live_cuts,cwb_cuts_label);
273  cout << "cwb_setcuts.C : Number of selected live entries = " << nsel << endl;
274  }
275  if(nsel<0) { // create a symbolic link to live*.root file name
276  TString olfname = owfname;
277  olfname.ReplaceAll("wave_","live_");
278  olfname.Remove(0,olfname.Last('/')+1); // strip path
279  cout << olfname << endl;
280  estat = gSystem->GetPathInfo(mdir+"/"+ilfname,&id,&size,&flags,&mt);
281  if(estat==0) {
282  sprintf(cmd,"cd %s;ln -sf %s %s",mdir.Data(),ilfname,olfname.Data());
283  cout << cmd << endl;
284  gSystem->Exec(cmd);
285  }
286  }
287  }
288 
289  exit(0);
290 }
static void setUniqueEvents(TString ifwave, TString ofwave, int nIFO, int pp_irho)
Definition: Toolbox.cc:3335
TString cwb_wcuts_tree
Definition: cwb_setcuts.C:31
CWB run(runID)
int estat
Definition: cwb_setcuts.C:21
TString("c")
Long_t size
Definition: cwb_setcuts.C:26
CWB::Toolbox TB
i drho i
TString owfname
Definition: cwb_setcuts.C:183
int jsel
Definition: cwb_setcuts.C:223
static vector< int > getMergeJobList(TString merge_dir, TString label, int version)
Definition: Toolbox.cc:1698
vector< int > jobList
Definition: cwb_setcuts.C:187
TString cwb_lcuts_tree
Definition: cwb_setcuts.C:34
#define nIFO
static int setCuts(TString ifName, TString idir, TString odir, TString trname, TString cuts, TString olabel)
Definition: Toolbox.cc:2876
char data_label[512]
Definition: test_config1.C:160
TGlobal * global
Definition: config.cc:23
char cmd[1024]
Definition: cwb_setcuts.C:27
static void dumpFileList(vector< TString > fileList, TString ofName)
Definition: Toolbox.cc:2769
TString olstfname
Definition: cwb_setcuts.C:190
Long_t flags
Definition: cwb_setcuts.C:26
Long_t mt
Definition: cwb_setcuts.C:26
char merge_dir[512]
Definition: test_config1.C:147
sprintf(ilstfname,"merge_%s.%s.lst", data_label, cwb_merge_label.Data())
TString cwb_unique_evt
Definition: cwb_setcuts.C:42
double * entry
Definition: cwb_setcuts.C:224
cwb_merge_label
Definition: cwb_setcuts.C:29
TString mdir
Definition: cwb_setcuts.C:37
char ilstfname[1024]
Definition: cwb_setcuts.C:188
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
Long_t id
Definition: cwb_setcuts.C:26
TString cwb_setcuts_options
Definition: cwb_setcuts.C:35
TString cwb_jcuts_tree
Definition: cwb_setcuts.C:30
TString cwb_tcuts_tree
Definition: cwb_setcuts.C:41
TString cwb_mcuts_tree
Definition: cwb_setcuts.C:33
char formula[256]
simulation
Definition: cwb_eced.C:26
TString cwb_cuts_label
Definition: cwb_setcuts.C:32
i drho pp_irho
int nsel
Definition: cwb_setcuts.C:237
exit(0)