Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_merge.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 // merge output root job files : used by the cwb_merge command
20 
21 {
22 
24 
25  TB.checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"));
26  TB.checkFile(gSystem->Getenv("CWB_PARAMETERS_FILE"));
27  TB.checkFile(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
28 
30  if(gSystem->Getenv("CWB_MERGE_LABEL")==NULL) {
31  cout << "Error : environment CWB_MERGE_LABEL is not defined!!!" << endl;exit(1);
32  } else {
33  cwb_merge_label=TString(gSystem->Getenv("CWB_MERGE_LABEL"));
34  }
35  // check if label has the correct format (M#) & extract merge number
37  if(cwb_merge_label[0]!='M') {
38  cout << "Error : label " << cwb_merge_label.Data() << " has bad format (M#)" << endl;exit(1);
39  } else {
40  TString lcheck=cwb_merge_label;
41  lcheck.Remove(0,1);
42  if(!lcheck.IsDigit()) {
43  cout << "Error : label " << cwb_merge_label.Data() << " has bad format (M#)" << endl;exit(1);
44  }
45  cwb_merge_number=lcheck.Atoi();
46  }
47 
48  // get merge options
49 
50  bool cwb_bpsm = false;
51  bool cwb_brms = false;
52  bool cwb_bvar = false;
53  int cwb_nthreads = 0;
55 
56  TString cwb_merge_opts=TString(gSystem->Getenv("CWB_MERGE_OPTS"));
57  if(cwb_merge_opts!="") {
58  TString option="";
59  // get bpsm
60  option = TB.getParameter(cwb_merge_opts,"--psm");
61  option.ToUpper();
62  cwb_bpsm = (option=="TRUE") ? true : false;
63  // get brms
64  option = TB.getParameter(cwb_merge_opts,"--rms");
65  option.ToUpper();
66  cwb_brms = (option=="TRUE") ? true : false;
67  // get bvar
68  option = TB.getParameter(cwb_merge_opts,"--var");
69  option.ToUpper();
70  cwb_bvar = (option=="TRUE") ? true : false;
71  // get ntreads
72  option = TB.getParameter(cwb_merge_opts,"--nthreads");
73  if(option!="") {
74  if(!option.IsDigit()) {
75  cout << endl << "cwb_merge.C : Error - --nthread argument '"
76  << option << "' is not a number" << endl << endl;
77  exit(1);
78  } else {
79  cwb_nthreads = option.Atoi();
80  }
81  }
82  // get input dir (default is output_dir defined in $CWB_UPARAMETERS_FILE)
83  option = TB.getParameter(cwb_merge_opts,"--idir");
84  if(option!="") strcpy(output_dir,option.Data());
85  // get user tag : is added to the merged files .U_utag
86  option = TB.getParameter(cwb_merge_opts,"--utag");
87  if(option!="") cwb_utag = option;
88  }
89 
90  //vector<TString> fileList = TB.getFileListFromDir(output_dir,".root");
91  //cout << "getDirFileLists - nfiles : " << fileList.size() << endl;
92  char tag[256];sprintf(tag,"wave_%s",data_label);
93  vector<TString> fileList = TB.getFileListFromDir(merge_dir, ".root", tag,"",true);
94 
96  if(cwb_utag!="") mtag = TString::Format(".%s.U_%s.root",cwb_merge_label.Data(),cwb_utag.Data());
97  else mtag = TString::Format(".%s.root",cwb_merge_label.Data());
98  if(cwb_merge_label.CompareTo("M0")!=0) {
99  // check if label already exist
100  for(int i=0;i<fileList.size();i++) {
101  if(fileList[i].Contains(mtag)) {
102  cout << "Ecwb_merge.C : Error - merge label " << cwb_merge_label << " already exist" << endl << endl;
103  gSystem->Exit(1);
104  }
105  }
106  }
107  // compute new version for label
108  int iversion=0;
109  int ipos = (cwb_utag=="") ? 2 : 3;
110  for(int i=0;i<fileList.size();i++) {
111  cout << i << " " << fileList[i].Data() << endl;
112  if(cwb_utag!="") if(!fileList[i].Contains(TString::Format(".U_%s",cwb_utag.Data()))) continue;
113  if(fileList[i].Contains(".root")) {
114  TObjArray* token = TString(fileList[i]).Tokenize(TString("."));
115  TString srescueID = ((TObjString*)token->At(token->GetEntries()-ipos))->GetString();
116  //srescueID.ReplaceAll(".root","");
117  srescueID.ReplaceAll("M","");
118  if(srescueID.IsDigit()) {
119  //cout << i << " " << fileList[i].Data() << endl;
120  int rescueID = srescueID.Atoi();
121  if(iversion<rescueID) iversion=rescueID;
122  }
123  }
124  }
125  iversion++;
126 
127  if(cwb_merge_number==0 || cwb_merge_number>=iversion) { // iversion replaced with the input user merge version
128  if(cwb_merge_number!=0) iversion=cwb_merge_number;
129  } else {
130  cout << endl << "cwb_merge.C : Error - the input merge version (M" << cwb_merge_number
131  << ") must be greater of the most recent merge version (M" << iversion-1 << ")" << endl << endl;
132  gSystem->Exit(1);
133  }
134 
135  // merge mdc log files (if produced by "On the Fly MDC")
136 #ifdef _USE_ROOT6
137  char cmd[128]; sprintf(cmd,"iversion = %d",iversion);
138  EXPORT(int,iversion,cmd)
139 #endif
140  gROOT->Macro(gSystem->ExpandPathName("$CWB_MACROS/cwb_merge_log.C"));
141 
142  char new_data_label[256];
143  if(cwb_utag=="") sprintf(new_data_label,"%s.M%d",data_label,iversion);
144  else sprintf(new_data_label,"%s.M%d.U_%s",data_label,iversion,cwb_utag.Data());
145  cout << "Start merging to file : " << new_data_label << endl << endl;
146  vector<TString> mergeList;
147  if(cwb_nthreads<=1) {
148  mergeList=TB.mergeCWBTrees(output_dir,simulation,merge_dir,new_data_label,cwb_brms,cwb_bvar,cwb_bpsm);
149  } else {
150  // multi threads
151  mergeList=TB.mergeCWBTrees(cwb_nthreads,output_dir,simulation,merge_dir,new_data_label,cwb_brms,cwb_bvar,cwb_bpsm);
152  }
153  char merge_list_file[1024];
154  if(cwb_utag=="") sprintf(merge_list_file,"%s/merge_%s.M%d.lst",merge_dir,data_label,iversion);
155  else sprintf(merge_list_file,"%s/merge_%s.M%d.U_%s.lst",merge_dir,data_label,iversion,cwb_utag.Data());
156  TB.dumpFileList(mergeList, merge_list_file);
157 
158  if(nfactor<=0) nfactor=1; // fix nfactor when nfactor is not defined
159 
160  // get the number of job submit by condor
161  char full_condor_dir[1024];
162  sprintf(full_condor_dir,"%s/%s",work_dir,condor_dir);
163  char condor_dag_file[1024];
164  sprintf(condor_dag_file,"%s/%s%s.dag",full_condor_dir,data_label,"");
165  Long_t id,size=0,flags,mt;
166  int estat = gSystem->GetPathInfo(condor_dag_file,&id,&size,&flags,&mt);
167  vector<int> jobList;
168  if (estat==0) {
169  jobList=TB.getCondorJobList(full_condor_dir, data_label);
170  int ncondor_jobs = jobList.size();
171  cout << endl << "Merged files : " << mergeList.size() << "/" << nfactor*ncondor_jobs << endl << endl;
172  }
173 
174  // for standard merge labels the merge/cwb_user_parameters.C is created
175  // this file is extract from the merge/wave_'label'.root
176  // it is the merge between $CWB_PARAMETERS_FILE & CWB_UPARAMETERS_FILE
177  // it is used in the post production stage
178 
179  if(cwb_utag!="") gSystem->Exit(0);
180 
181  char wave_name[1024]; sprintf(wave_name,"%s/wave_%s.root",merge_dir,new_data_label);
182  TFile *ifile = TFile::Open(wave_name);
183  if(ifile==NULL) {cout << "Failed to open " << wave_name << endl;exit(1);}
184 
185  CWB::History* ihistory = (CWB::History*)ifile->Get("history");
186  if(ihistory==NULL) {
187  cout << "Error : history is not present!!!" << endl;exit(1);
188  }
189 
193  if(ihistory) {
194  TList* stageList = ihistory->GetStageNames(); // get stage list
195  for(int i=0;i<stageList->GetSize();i++) { // get lib versions
196  TObjString* stageObjString = (TObjString*)stageList->At(i);
197  TString stageName = stageObjString->GetString();
198  // check if stage belong to processing stages (skip pp stages)
199  bool isProcessingStage=false;
200  for(int j=0;j<=nStages;j++) {
201  if(cwb::GetStageString((CWB_STAGE)j).Contains(stageName)) isProcessingStage=true;
202  }
203  if(!isProcessingStage) continue;
204  char* stage = const_cast<char*>(stageName.Data());
205  TString slog = ihistory->GetHistory(stage,const_cast<char*>("PARAMETERS"));
206  if(slog!="") {
207  config = slog;
208  git_version = ihistory->GetHistory(stage,const_cast<char*>("GITVERSION"));
209  }
210  }
211  }
212 
213  char configFile[1024]; sprintf(configFile,"%s/cwb_user_parameters.C",merge_dir);
214  ofstream out;
215  out.open(configFile,ios::out);
216  if (!out.good()) {cout << "Error Opening File : " << configFile << endl;exit(1);}
217  out << config.Data();
218  out.close();
219 
220  gSystem->Exit(0);
221 }
char new_data_label[256]
Definition: cwb_merge.C:142
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5108
TString cwb_merge_label
Definition: cwb_merge.C:29
char condor_dag_file[1024]
TString("c")
bool cwb_brms
Definition: cwb_merge.C:51
ofstream out
Definition: cwb_merge.C:214
char full_condor_dir[1024]
CWB::Toolbox TB
TString cwb_utag
Definition: cwb_merge.C:54
Long_t flags
TString mtag
Definition: cwb_merge.C:95
Long_t size
int j
Definition: cwb_net.C:28
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
int nStages
Definition: cwb_merge.C:192
int cwb_nthreads
Definition: cwb_merge.C:53
CWB::History * ihistory
Definition: cwb_merge.C:185
char data_label[512]
Definition: test_config1.C:160
int ncondor_jobs
static vector< TString > mergeCWBTrees(TString dir_name, bool simulation, TString odir, TString label, bool brms=false, bool bvar=false, bool bpsm=false)
Definition: Toolbox.cc:2462
TString cwb_merge_opts
Definition: cwb_merge.C:56
static TString GetStageString(CWB_STAGE jstage)
Definition: cwb.cc:2206
vector< int > jobList
static void dumpFileList(vector< TString > fileList, TString ofName)
Definition: Toolbox.cc:2769
bool cwb_bvar
Definition: cwb_merge.C:52
bool cwb_bpsm
Definition: cwb_merge.C:50
int cwb_merge_number
Definition: cwb_merge.C:36
int ipos
Definition: cwb_merge.C:109
int iversion
Definition: cwb_merge.C:108
char merge_dir[512]
Definition: test_config1.C:147
TList * GetStageNames()
Definition: History.cc:427
static int GetStageSize()
Definition: cwb.hh:162
TObjArray * token
TFile * ifile
char tag[256]
Definition: cwb_merge.C:92
sprintf(tag,"wave_%s", data_label)
#define EXPORT(TYPE, VAR, CMD)
Definition: config.cc:92
static vector< int > getCondorJobList(TString condor_dir, TString label)
Definition: Toolbox.cc:1398
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:6727
char configFile[1024]
Definition: cwb_merge.C:213
char cmd[1024]
int estat
strcpy(RunLabel, RUN_LABEL)
int nfactor
Definition: test_config1.C:83
vector< TString > fileList
Definition: cwb_merge.C:93
Long_t mt
Long_t id
char merge_list_file[1024]
Definition: cwb_merge.C:153
char condor_dir[512]
Definition: test_config1.C:148
char work_dir[512]
Definition: test_config1.C:143
char * GetHistory(char *StageName, char *Type)
Definition: History.cc:273
simulation
Definition: cwb_eced.C:26
TString git_version
Definition: cwb_merge.C:191
char output_dir[512]
Definition: test_config1.C:146
TString config
Definition: cwb_merge.C:190
CWB_STAGE
Definition: cwb.hh:122
exit(0)