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