Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_clonedir.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 // cwb_clonedir.C macro, used to process cwb_clonedir options
20 
21 {
22  #include <vector>
23 
25 
26  // get cwb_clonedir options
27  TString cwb_clonedir_options=TString(gSystem->Getenv("CWB_CLONEDIR_OPTIONS"));
28 
29  // check if cwb_clonedir options have a correct format
30  if(gSystem->Getenv("CWB_CLONEDIR_CHECK")!=NULL) {
31  bool check;
32  CWB::Toolbox::getParameter(cwb_clonedir_options);
33  // get output option
35  CWB::Toolbox::getParameter(cwb_clonedir_options,"--output");
36  cwb_clonedir_options_output.ToUpper();
37  if(cwb_clonedir_options_output!="") { // check if the output option is correct
38  check=false;
39  if(cwb_clonedir_options_output=="LINKS") check=true;
40  if(cwb_clonedir_options_output=="MERGE") check=true;
41  if(!check) {
42  cout << endl << "cwb_clonedir Error : --output available value are : links/merge!!!" << endl << endl;
43  gSystem->Exit(1);
44  }
45  }
46 
47  // get config option
49  CWB::Toolbox::getParameter(cwb_clonedir_options,"--config");
50  cwb_clonedir_options_config.ToUpper();
51  if(cwb_clonedir_options_config!="") { // check if the config option is correct
52  check=false;
53  if(cwb_clonedir_options_config=="CHECK") check=true;
54  if(!check) {
55  cout << endl << "cwb_clonedir Error : --check available values are : true/false!!!" << endl << endl;
56  gSystem->Exit(1);
57  }
58  }
59 
60  // get simulation option
62  CWB::Toolbox::getParameter(cwb_clonedir_options,"--simulation");
63  cwb_clonedir_options_simulation.ToUpper();
64  if(cwb_clonedir_options_simulation!="") { // check if the simulation option is correct
65  check=false;
66  if(cwb_clonedir_options_simulation=="TRUE") check=true;
67  if(cwb_clonedir_options_simulation=="FALSE") check=true;
68  if(!check) {
69  cout << endl << "cwb_clonedir Error : --simulation available values are : true/false!!!" << endl << endl;
70  gSystem->Exit(1);
71  }
72  }
73 
74  // get chunk option
76  CWB::Toolbox::getParameter(cwb_clonedir_options,"--chunk");
77  if(cwb_clonedir_options_chunk!="") {
78  // get label option
80  CWB::Toolbox::getParameter(cwb_clonedir_options,"--label");
81  if(cwb_clonedir_options_label!="") {
82  cout << endl << "cwb_clonedir Error : --chunk option can not be used when --label option is used!!!" << endl << endl;
83  gSystem->Exit(1);
84  }
85  if(!cwb_clonedir_options_chunk.IsDigit()) {
86  cout << endl << "cwb_clonedir Error : ckunk option is not a positive integer number!!!" << endl << endl;
87  gSystem->Exit(1);
88  }
89  }
90 
91  gSystem->Exit(0);
92  }
93 
94  if(cwb_clonedir_options=="") {
95  cout << endl;
96  cout << "Destination directory already exist ..." << endl;
97  cout << endl;
98  gSystem->Exit(0);
99  }
100 
101  TB.checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"));
102  TB.checkFile(gSystem->Getenv("CWB_PARAMETERS_FILE"));
103  TB.checkFile(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
104 
105  // get directory where cwb_clonedir has been executed
107  if(gSystem->Getenv("CWB_CLONEDIR_PWD")!=NULL) {
108  cwb_clonedir_pwd=TString(gSystem->Getenv("CWB_CLONEDIR_PWD"));
109  } else {
110  cout << "cwb_clonedir.C - Error : CWB_CLONEDIR_PWD not declared" << endl;
111  exit(1);
112  }
113 
114  // get cwb_clonedir_src
116  if(gSystem->Getenv("CWB_CLONEDIR_SRC")!=NULL) {
117  cwb_clonedir_src=TString(gSystem->Getenv("CWB_CLONEDIR_SRC"));
118  if(!cwb_clonedir_src.BeginsWith("/")) {
119  cwb_clonedir_src=cwb_clonedir_pwd+"/"+cwb_clonedir_src;
120  }
121  } else {
122  cout << "cwb_clonedir.C - Error : CWB_CLONEDIR_SRC not declared" << endl;
123  exit(1);
124  }
125  TB.checkFile(cwb_clonedir_src);
126 
127  // get cwb_clonedir_dest
129  if(gSystem->Getenv("CWB_CLONEDIR_DEST")!=NULL) {
130  cwb_clonedir_dest=TString(gSystem->Getenv("CWB_CLONEDIR_DEST"));
131  } else {
132  cout << "cwb_clonedir.C - Error : CWB_CLONEDIR_DEST not declared" << endl;
133  exit(1);
134  }
135 
136  // get output option
138  CWB::Toolbox::getParameter(cwb_clonedir_options,"--output");
139  cwb_clonedir_options_output.ToUpper();
140 
141  // get config option
143  CWB::Toolbox::getParameter(cwb_clonedir_options,"--config");
144  cwb_clonedir_options_config.ToUpper();
145 
146  // get simulation option
148  CWB::Toolbox::getParameter(cwb_clonedir_options,"--simulation");
149  cwb_clonedir_options_simulation.ToUpper();
150 
151  // get chunk option
153  CWB::Toolbox::getParameter(cwb_clonedir_options,"--chunk");
154 
155  // get label option
157  CWB::Toolbox::getParameter(cwb_clonedir_options,"--label");
158 
159  // get stage option
161  CWB::Toolbox::getParameter(cwb_clonedir_options,"--jstage");
162  cwb_clonedir_options_jstage.ToUpper();
163  // convert stage name to value
164  TString cwb_stage_label="supercluster_";
165  if(cwb_clonedir_options_jstage=="FULL") cwb_stage_label="wave_";
166  if(cwb_clonedir_options_jstage=="INIT") cwb_stage_label="init_";
167  if(cwb_clonedir_options_jstage=="STRAIN") cwb_stage_label="strain_";
168  if(cwb_clonedir_options_jstage=="CSTRAIN") cwb_stage_label="cstrain_";
169  if(cwb_clonedir_options_jstage=="COHERENCE") cwb_stage_label="coherence_";
170  if(cwb_clonedir_options_jstage=="SUPERCLUSTER") cwb_stage_label="supercluster_";
171  if(cwb_clonedir_options_jstage=="LIKELIHOOD") cwb_stage_label="wave_";
172 
173  char src_output_dir[1024];
174  sprintf(src_output_dir,"%s/%s",cwb_clonedir_src.Data(),output_dir);
175 
176  // get rid of final "/" to makes gSystem->BaseName working properly
177  if(cwb_clonedir_src.EndsWith("/")) cwb_clonedir_src.Resize(cwb_clonedir_src.Sizeof()-2);
178  // extract the source data label
179  const char* src_data_label = gSystem->BaseName(cwb_clonedir_src.Data());
180 
181  // check if source config files are compatible with the dest config files
182  if(cwb_clonedir_options_config=="CHECK") {
183  char cmd[1024];
184  char fparms[1024];
185 
186  sprintf(fparms,"%s/%s",cwb_clonedir_src.Data(),gSystem->ExpandPathName("$CWB_UPARAMETERS_FILE"));
187 
188  // check if files exist
189  TB.checkFile(fparms);
190 
191  cout << "fparms : " << fparms << endl;
192 
193  // diff config files
194  sprintf(cmd,"diff %s %s | less",fparms,gSystem->ExpandPathName("$CWB_UPARAMETERS_FILE"));
195  gSystem->Exec(cmd);
196 
197  char answer[256];
198  strcpy(answer,"");
199  do {
200  cout << "Are src & dest config files compatibles ? (y/n) " << endl;
201  cin >> answer;
202  cout << endl << endl;
203  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
204  if (strcmp(answer,"n")==0) gSystem->Exit(1);
205  }
206 
207  // Create symbolic links under the dest output dir of the source output dir files
208  // The link data_label is the dest data_label
209  if(cwb_clonedir_options_output=="LINKS") {
210  vector<TString> fileList = TB.getFileListFromDir(src_output_dir, ".root",
211  cwb_stage_label, src_data_label);
212  cout << "cwb_clonedir : create symbolic links in output dir ..." << endl;
213  int newlinks=0;
214  int estat;
215  Long_t id,size,flags,mt;
216  char cmd[1024];
217  char link_to[1024];
218  for(int i=0;i<fileList.size();i++) {
219  if(i%100==0) cout << i << "/" << fileList.size() << endl;
220  //cout << i << " " << fileList[i].Data() << endl;
221  const char* file_name_base = gSystem->BaseName(fileList[i].Data());
222  TString sfile_name_base = file_name_base;
223  sfile_name_base.ReplaceAll(src_data_label,data_label);
224 
225  TString filePath = TString(output_dir)+"/"+sfile_name_base;
226  estat = gSystem->GetPathInfo(filePath,&id,&size,&flags,&mt);
227  if (estat!=0) {
228  newlinks++;
229  sprintf(link_to,"%s/%s",output_dir,sfile_name_base.Data());
230  gSystem->Symlink(fileList[i].Data(),link_to);
231  }
232  }
233  cout << "Number of new links " << newlinks << "/" << fileList.size() << endl;
234  }
235 
236  char src_merge_dir[1024];
237  sprintf(src_merge_dir,"%s/%s",cwb_clonedir_src.Data(),merge_dir);
238 
239  // merge wave&live under the dest output dir of the source merged files
240  if(cwb_clonedir_options_output=="MERGE") {
241  int estat;
242  Long_t id,size,flags,mt;
243  vector<TString> fileList = TB.getFileListFromDir(src_merge_dir, ".root", "", src_data_label);
244  cout << "cwb_clonedir : merge src wave&live in output dir ..." << endl;
245  TRegexp reg1(".*M[0-9]+.root");
246  // compute max merge version
247  int max_version=0;
248  for(int i=0;i<fileList.size();i++) {
249  // select merged file with *.M#.root format
250  if(fileList[i].Contains(reg1)) {
251  //cout << i << " " << fileList[i] << endl;
252  TObjArray* token = TString(fileList[i]).Tokenize(TString("."));
253  TString smergeID = ((TObjString*)token->At(token->GetEntries()-2))->GetString();
254  smergeID.ReplaceAll("M","");
255  if(smergeID.IsDigit()) {
256  int mergeID = smergeID.Atoi();
257  if(max_version<mergeID) max_version=mergeID;
258  }
259  delete token;
260  }
261  }
262  // cout << "max_version : " << max_version << endl;
263  TString chunk_label = cwb_clonedir_options_chunk!="" ? TString(".K_chunk")+cwb_clonedir_options_chunk : "";
264  char fwave[1024];
265  char flive[1024];
266  char fmerge[1024];
267  char flist[1024];
268  char fmdc[1024];
269  if(cwb_clonedir_options_label=="") {
270  sprintf(fwave,"%s/wave_%s.M%d%s.root",src_merge_dir,src_data_label,max_version,chunk_label.Data());
271  sprintf(flive,"%s/live_%s.M%d%s.root",src_merge_dir,src_data_label,max_version,chunk_label.Data());
272  sprintf(fmerge,"%s/wave_%s.M%d%s.root",output_dir,src_data_label,max_version,chunk_label.Data());
273  sprintf(flist,"%s/wave_%s.M%d%s.lst",output_dir,src_data_label,max_version,chunk_label.Data());
274  sprintf(fmdc,"%s/mdc_%s.M%d%s.root",src_merge_dir,src_data_label,max_version,chunk_label.Data());
275  } else {
276  sprintf(fwave,"%s/wave_%s.%s.root",src_merge_dir,src_data_label,cwb_clonedir_options_label.Data());
277  sprintf(flive,"%s/live_%s.%s.root",src_merge_dir,src_data_label,cwb_clonedir_options_label.Data());
278  sprintf(fmerge,"%s/wave_%s.%s.root",output_dir,src_data_label,cwb_clonedir_options_label.Data());
279  sprintf(flist,"%s/wave_%s.%s.lst",output_dir,src_data_label,cwb_clonedir_options_label.Data());
280  sprintf(fmdc,"%s/mdc_%s.%s.root",src_merge_dir,src_data_label,cwb_clonedir_options_label.Data());
281  }
282  cout << "fwave : " << fwave << endl;
283  cout << "flive : " << flive << endl;
284  cout << "fmerge : " << fmerge << endl;
285  cout << "flist : " << flist << endl;
286  cout << "fmdc : " << fmdc << endl;
287  // check if files exist
288  estat = gSystem->GetPathInfo(fwave,&id,&size,&flags,&mt);
289  if (estat!=0) {cout << "cwb_clonedir.C : Error - wave not exist : " << fwave << endl;exit(1);}
290  if(cwb_clonedir_options_simulation=="TRUE") {
291  estat = gSystem->GetPathInfo(fmdc,&id,&size,&flags,&mt);
292  if (estat!=0) {cout << "cwb_clonedir.C : Error - mdc not exist : " << fmdc << endl;exit(1);}
293  } else {
294  estat = gSystem->GetPathInfo(flive,&id,&size,&flags,&mt);
295  if (estat!=0) {cout << "cwb_clonedir.C : Error - live not exist : " << flive << endl;exit(1);}
296  }
297  // check if file with a different version .M# already exist in the output_dir
298  vector<TString> checkList = TB.getFileListFromDir(output_dir, "", "", src_data_label);
299  if(checkList.size()!=0) {
300  cout << endl;
301  cout << "cwb_clonedir.C : Error - files with source label : " << src_data_label << endl;
302  cout << "already exist in the output directory : " << cwb_clonedir_dest <<
303  "/" << output_dir << endl;
304  cout << "Before to apply cwb_clonedir.C the following files must be removed !!!" << endl << endl;
305  for(int i=0;i<checkList.size();i++) {
306  cout << i << " " << checkList[i].Data() << endl;
307  }
308  gSystem->Exit(1);
309  }
310  if(chunk_label!="") {
311  // check if file with same chunk label .K_chunk# already exist in the output_dir
312  checkList = TB.getFileListFromDir(output_dir, "", "", chunk_label);
313  if(checkList.size()!=0) {
314  cout << endl;
315  cout << "cwb_clonedir.C : Error - files with chunk label : " << chunk_label << endl;
316  cout << "already exist in the output directory : " << cwb_clonedir_dest <<
317  "/" << output_dir << endl;
318  cout << "Before to apply cwb_clonedir.C the following files must be removed !!!" << endl << endl;
319  for(int i=0;i<checkList.size();i++) {
320  cout << i << " " << checkList[i].Data() << endl;
321  }
322  gSystem->Exit(1);
323  }
324  }
325  // merge wave&live
326  TFileMerger M;
327  M.AddFile(fwave,true);
328  if(cwb_clonedir_options_simulation=="TRUE") {
329  M.AddFile(fmdc,true);
330  } else {
331  M.AddFile(flive,true);
332  }
333  M.OutputFile(fmerge);
334  if(!M.Merge()) {
335  char cmd[1024];
336  sprintf(cmd,"rm %s",fmerge);
337  //cout << cmd << endl;
338  gSystem->Exec(cmd);
339  cout << "cwb_clonedir.C : Error - Merge failed !!!" << endl;
340  gSystem->Exit(1);
341  }
342  // write merge file list
343  vector<TString> mergeList(2);
344  mergeList[0]=fwave;
345  if(cwb_clonedir_options_simulation=="TRUE") {
346  mergeList[1]=fmdc;
347  } else {
348  mergeList[1]=flive;
349  }
350  TB.dumpFileList(mergeList, flist);
351  }
352 
353  gSystem->Exit(0);
354 }
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5108
double M
Definition: DrawEBHH.C:13
TString cwb_stage_label
Definition: cwb_clonedir.C:164
TString cwb_clonedir_options_output
Definition: cwb_clonedir.C:137
TString cwb_clonedir_options_chunk
Definition: cwb_clonedir.C:152
TString("c")
CWB::Toolbox TB
char src_merge_dir[1024]
Definition: cwb_clonedir.C:236
Long_t flags
exit(1)
Long_t size
TFile * flive
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
char data_label[512]
Definition: test_config1.C:160
TString cwb_clonedir_options
Definition: cwb_clonedir.C:27
static void dumpFileList(vector< TString > fileList, TString ofName)
Definition: Toolbox.cc:2769
TString cwb_clonedir_dest
Definition: cwb_clonedir.C:128
char merge_dir[512]
Definition: test_config1.C:147
TString cwb_clonedir_pwd
Definition: cwb_clonedir.C:106
TObjArray * token
TString cwb_clonedir_src
Definition: cwb_clonedir.C:115
TString cwb_clonedir_options_jstage
Definition: cwb_clonedir.C:160
char answer[256]
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
cout<< "Starting reading output directory ..."<< endl;vector< TString > fileList
TString cwb_clonedir_options_config
Definition: cwb_clonedir.C:142
char cmd[1024]
int estat
strcpy(RunLabel, RUN_LABEL)
Long_t mt
Long_t id
TString cwb_clonedir_options_simulation
Definition: cwb_clonedir.C:147
sprintf(src_merge_dir,"%s/%s", cwb_clonedir_src.Data(), merge_dir)
TString cwb_clonedir_options_label
Definition: cwb_clonedir.C:156
int check
char output_dir[512]
Definition: test_config1.C:146