Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_xnet.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_xnet : interface to the cwb class for 1G/2G pipelines
20 //
21 // fName : for 1G/(2G jstage=1) analysis it is the user_parameters.C file name
22 // for 2G jstage=2 analysis :
23 // if fName is the (1G jstage=1) output cluster file name
24 // then runID is retrieved from file name
25 // jstage : for 1G jstage=CWB_STAGE_FULL
26 // for 2G jstage=CWB_STAGE_FULL / CWB_STAGE_INIT / CWB_STAGE_STRAIN / CWB_STAGE_CSTRAIN
27 // CWB_STAGE_COHERENCE / CWB_STAGE_SUPERCLUSTER / CWB_STAGE_LIKELIHOOD
28 // uName : used only for (2G jstage=2) -> is an auxiliary user_parameters.C file
29 // batch : true/false -> batch/interactive mode
30 // ced : true/false -> if true the easy ced script cwb_eced.C is executed (used only for easy ced)
31 //
32 
33 #include <unistd.h>
34 
35 void SystemExec(char* command, int maxtry=3);
36 void GetJobFromGPS();
37 
38 void
39 cwb_xnet(TString fName, CWB_STAGE jstage=CWB_STAGE_FULL, TString uName="", bool batch=false, bool eced=false) {
40 
41  GetJobFromGPS(); // if srunID="@" then jobID is automatically computed using the environmental CWB_GPS_EVENT
42 
43  int runID=0;
44  char cmd[1024];
45 
46  char tmpOut[1024]; // temporary out log file name
47  char outLog[1024]; // final out log file name
48  char tmpErr[1024]; // temporary err log file name
49  char errLog[1024]; // final err log file name
50  if(batch) {
51  // job stdout is redirect to local file when job is executed in batch mode (condor)
52  // this is done to avoid network traffic between the execution node and the working user directory
53  // when the job is finished the log file is moved to the log working directory
54 
55  // import nodedir from CINT (used in batch mode in multistages analysis)
56  char nodedir_cint[1024];
57  TGlobal* GLOBAL = (TGlobal*)gROOT->GetGlobal("nodedir",true);
58  if(GLOBAL!=NULL)
59  memcpy((void*)&nodedir_cint,(void*)GLOBAL->GetAddress(),1024*sizeof(char));
60  // makes the nodedir_cint readable from the group
61  sprintf(cmd,"chmod g+r %s",nodedir_cint);
62  cout << cmd << endl;
63  SystemExec(cmd);
64 
65  unsigned int Pid = gSystem->GetPid(); // used to tag in a unique way the temporary files
66 
67  TString srunID = TString(gSystem->Getenv("CWB_JOBID")); // get jobID
68  TString slabel = gSystem->BaseName(work_dir); // get label
69  TString sstage = cwb::GetStageString(jstage); // get stage name
70 
71  // create tmpOut & outLog
72  sprintf(tmpOut,"%s/%s_%s_%s_%d.out", nodedir_cint,srunID.Data(),slabel.Data(),sstage.Data(),Pid);
73  sprintf(outLog,"%s/%s/%s_%s_%s.out", work_dir,log_dir,srunID.Data(),slabel.Data(),sstage.Data());
74  cout << tmpOut << endl; cout << outLog << endl;
75 
76  // create tmpErr & errLog
77  sprintf(tmpErr,"%s/%s_%s_%s_%d.err", nodedir_cint,srunID.Data(),slabel.Data(),sstage.Data(),Pid);
78  sprintf(errLog,"%s/%s/%s_%s_%s.err", work_dir,log_dir,srunID.Data(),slabel.Data(),sstage.Data());
79  cout << tmpErr << endl; cout << errLog << endl;
80 
81  // NOTE: The creation of symbolic links has been removed because in general the working directory in the remote node is not visible from head node
82  // for condor batch systems the output and error log files can be see at run time using the remote access to the the node using condor_ssh_to_job
83  // only at the end of jobs the output and error log files are copied to the user log directory in the head node
84 
85 /*
86  if(gSystem->Getenv("_USE_PEGASUS")==NULL) {
87  // create in log dir a symbolic link to the temporary log files
88  // the remove is necessary because the command 'ln' fails if the link points to a unavailable node
89  // the command touch it is necessary because condor fails if xxxLog not exist
90  sprintf(cmd,"rm -f %s;ln -sf %s %s;touch %s",outLog,tmpOut,outLog,outLog);
91  cout << cmd << endl;
92  SystemExec(cmd);
93  sprintf(cmd,"rm -f %s;ln -sf %s %s;touch %s",errLog,tmpErr,errLog,errLog);
94  cout << cmd << endl;
95  SystemExec(cmd);
96  }
97 */
98 
99  freopen(tmpOut,"w",stdout); // redirect stdout to file
100  freopen(tmpErr,"w",stderr); // redirect stderr to file
101 
102  // Print CWB logo
104  }
105 
106  if(TString(analysis)=="1G") {
107  cwb1G CWB(fName);
108  if(!batch) {
109  CWB::config* cfg = CWB.GetConfig();
110  if(eced) cfg->Import(gSystem->ExpandPathName("$CWB_MACROS/cwb_eced.C")); // easy ced
111  cfg->Import(gSystem->ExpandPathName("$CWB_MACROS/cwb_inet.C"));
112  //runID=1;
113  }
114  //cfg->Print();
115  CWB.run(runID);
116  } else
117  if(TString(analysis)=="2G") {
119 
120  // import nodedir from CINT (used in batch mode in multistages analysis)
121  char nodedir_cint[1024];
122  TGlobal* GLOBAL = (TGlobal*)gROOT->GetGlobal("nodedir",true);
123  if(GLOBAL!=NULL)
124  memcpy((void*)&nodedir_cint,(void*)GLOBAL->GetAddress(),1024*sizeof(char));
125 
126  // create cwb2G object
127  cwb2G CWB(fName,uName,jstage);
128 
129  // set configuration
130  CWB::config* cfg = CWB.GetConfig();
131  if(!batch) {
132  if(eced) gROOT->Macro(gSystem->ExpandPathName("$CWB_MACROS/cwb_eced.C")); // easy ced
133  cfg->Import(gSystem->ExpandPathName("$CWB_MACROS/cwb_inet.C"));
134  //runID=1;
135  } else {
136  // nodedir must be updated because in multistage the nodedir change
137  sprintf(cfg->nodedir,"%s",nodedir_cint);
138  }
139 
140  // set save options for job file
141  CWB.SetupStage(jstage);
142  //cfg->Print();
143 
144  // run analysis
145  CWB.run(runID);
146  } else {
147  cout << "cwb_xnet - Error : analysis must be 1G or 2G" << endl;
148  gSystem->Exit(1);
149  }
150 
151  if(batch) {
152  if(ftell(stdout)!=-1) fclose(stdout); // close stdout
153  if(ftell(stderr)!=-1) fclose(stderr); // close stderr
154 
155  // move condor temporary log files to the working log directory
156  // if size>1000000 it is not copied (to avoid network traffic)
157  int estat;
158  Long_t id,size,flags,mt;
159 
160  estat = gSystem->GetPathInfo(tmpOut,&id,&size,&flags,&mt);
161  if (estat==0 && size<1000000) {
162  sprintf(cmd,"mv %s %s",tmpOut,outLog);
163  cout << cmd << endl;
164  SystemExec(cmd);
165  }
166 
167  estat = gSystem->GetPathInfo(tmpErr,&id,&size,&flags,&mt);
168  if (estat==0 && size<1000000) {
169  sprintf(cmd,"mv %s %s",tmpErr,errLog);
170  cout << cmd << endl;
171  SystemExec(cmd);
172  }
173  }
174 
175  gSystem->Exit(0);
176 }
177 
178 void
179 SystemExec(char* command, int maxtry) {
180 //
181 // Execute System Command
182 //
183 // command : system command
184 // maxtry : number of retry before to exit with error (default=3)
185 //
186 
187  int ntry=0;
188  int ecommand=1;
189  while(ecommand&&(ntry<maxtry)) {
190  ecommand=gSystem->Exec(command);
191  if(ecommand) gSystem->Sleep(int(gRandom->Uniform(1000,3000))); // wait [1,3] sec
192  ntry++;
193  }
194  if(ecommand) {cout << "cwb_xnet.C - Error -> " << command << endl;gSystem->Exit(1);}
195  return;
196 }
197 
198 void
200 
201  TString srunID = TString(gSystem->Getenv("CWB_JOBID")); // get jobID
202  TString slabel = gSystem->BaseName(work_dir); // get label
203 
204  if(srunID!="@") return;
205 
206  int gps_event=0;
207  TString cwb_gps_event=TString(gSystem->Getenv("CWB_GPS_EVENT"));
208  if(cwb_gps_event.CompareTo("")!=0) {
209  if(!cwb_gps_event.IsFloat()) {cout<< "Error : CWB_GPS_EVENT is not a number" << endl;exit(1);}
210  if(cwb_gps_event.Atoi()>0) gps_event=cwb_gps_event.Atoi();
211  }
212 
213  TString cwb_scripts = TString(gSystem->Getenv("CWB_SCRIPTS"));
214  TString exec_cmd = TString::Format("%s/cwb_dump.csh sjob",cwb_scripts.Data());
215  int ret=gSystem->Exec(exec_cmd);
216  if(ret) {cout << "Error while executing cwb_dum sjob !!!" << endl;exit(1);}
217 
218  TString sjob_file = TString("report/dump/")+slabel+".sjob";
219 
220  CWB::Toolbox::checkFile(sjob_file);
221 
222  // Open chunk list
223  ifstream in;
224  in.open(sjob_file.Data(),ios::in);
225  if (!in.good()) {cout << "Error Opening File : " << sjob_file << endl;exit(1);}
226 
227  int job=0;
228  double start,stop;
229  while(true) {
230  in >> start >> stop;
231  if(!in.good()) break;
232  job++;
233  if(gps_event>=start && gps_event<=stop) {
234  TString sjob = TString::Format("%d",job);
235  gSystem->Setenv("CWB_JOBID",sjob.Data());
236  }
237  }
238 
239  return;
240 }
241 
CWB::config * cfg
TString cwb_scripts
TString GetLALVersion(TString options="")
Definition: Toolfun.hh:1004
void GetJobFromGPS()
Definition: cwb_xnet.C:199
TString("c")
CWB::Toolbox TB
Long_t flags
Long_t size
char command[1024]
Definition: cwb_compile.C:44
void Import(TString umacro="")
Definition: config.cc:352
char nodedir[1024]
Definition: config.hh:352
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
Definition: cwb2G.hh:33
static TString GetStageString(CWB_STAGE jstage)
Definition: cwb.cc:2206
void SetupStage(CWB_STAGE jstage)
Definition: cwb.cc:2242
virtual void run(int runID=0)
Definition: cwb.cc:281
char log_dir[512]
Definition: test_config1.C:151
ifstream in
CWB::config * GetConfig()
Definition: cwb.hh:150
int gps_event
Definition: cwb_inet.C:188
Definition: cwb1G.hh:31
char cmd[1024]
int estat
PrintLogoCWB(GetLALVersion())
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
Long_t id
char work_dir[512]
Definition: test_config1.C:143
TString cwb_gps_event
Definition: cwb_inet.C:189
fclose(ftrig)
char fName[256]
void cwb_xnet(TString fName, CWB_STAGE jstage=CWB_STAGE_FULL, TString uName="", bool batch=false, bool eced=false)
Definition: cwb_xnet.C:39
CWB_STAGE
Definition: cwb.hh:122
void SystemExec(char *command, int maxtry=3)
Definition: cwb_xnet.C:179
exit(0)