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