forked from UCATLAS/xAODAnaHelpers
/
AnalysisBase.h
324 lines (274 loc) · 11.7 KB
/
AnalysisBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "xAODRootAccess/Init.h"
#include "SampleHandler/SampleHandler.h"
#include "SampleHandler/DiskListLocal.h"
#include "SampleHandler/DiskListEOS.h"
#include "SampleHandler/ToolsDiscovery.h"
#include <fstream>
#include <unistd.h>
#include <string>
#include <vector>
#include <sys/stat.h>
#include "EventLoop/DirectDriver.h"
#include "EventLoop/CondorDriver.h"
#include "EventLoopGrid/PrunDriver.h"
#include "TEnv.h"
#include "TSystem.h"
int setupJob(int argc, char* argv[], EL::Job& job,
std::string& configName, std::string& submitDir, std::string& systName,
float& systVal, bool& f_grid, bool& f_production, std::string& outputName, std::vector< std::string >& outputContainerNames){
/////////// Retrieve job arguments //////////////////////////
std::vector< std::string> options;
for(int ii=1; ii < argc; ++ii){
options.push_back( std::string(argv[ii]) );
}
if (argc > 1 && options.at(0).compare("-h") == 0) {
std::cout << std::endl
<< " job submission" << std::endl
<< std::endl
<< " Optional arguments:" << std::endl
<< " -h Prints this menu" << std::endl
<< " -inFile Path to a folder, root file, or text file" << std::endl
<< " -inputTag A wildcarded file name to run on" << std::endl
<< " -outputTag Version string to be appended to job name" << std::endl
<< " -submitDir Name of output directory" << std::endl
<< " -configName Path to config file" << std::endl
<< " -syst Name AND value for systematic" << std::endl
<< " -production Group name for the production role" << std::endl
<< std::endl;
exit(1);
}
std::string samplePath = ".";
std::string outputTag = "";
std::string inputTag = "";
std::string productionName = "";
int iArg = 0;
while(iArg < argc-1) {
if (options.at(iArg).compare("-h") == 0) {
// Ignore if not first argument
++iArg;
} else if (options.at(iArg).compare("-inFile") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || tmpChar == '-' ) {
std::cout << " -inFile should be followed by a file or folder" << std::endl;
return 1;
} else {
samplePath = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-inputTag") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || tmpChar == '-' ) {
std::cout << " -inputTag is a wildcarded file name to run on" << std::endl;
return 1;
} else {
inputTag = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-outputTag") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || tmpChar == '-' ) {
std::cout << " -outputTag should be followed by a job version string" << std::endl;
return 1;
} else {
outputTag = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-submitDir") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || tmpChar == '-' ) {
std::cout << " -submitDir should be followed by a folder name" << std::endl;
return 1;
} else {
submitDir = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-configName") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || tmpChar == '-' ) {
std::cout << " -configName should be followed by a config file" << std::endl;
return 1;
} else {
configName = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-production") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc-1 || tmpChar == '-' ) {
std::cout << " -production should be followed by the production role (i.e. phys-exotics)" << std::endl;
return 1;
} else {
productionName = options.at(iArg+1);
iArg += 2;
}
} else if (options.at(iArg).compare("-syst") == 0) {
char tmpChar = options.at(iArg+1)[0];
if (iArg+1 == argc || iArg+2 == argc || tmpChar == '-' ) {
std::cout << " -syst should be followed by a systematic string and an integer" << std::endl;
return 1;
} else {
std::stringstream ss;
systName = options.at(iArg+1);
ss << options.at(iArg+2);
ss >> systVal;
ss.str("");
if(systVal == 0 && options.at(iArg+2)[0] != '0'){
std::cout << " -inFile should be followed by a systematic string and an INTEGER" << std::endl;
return 1;
}
iArg += 3;
}
}else{
std::cout << "Couldn't understand argument " << options.at(iArg) << std::endl;
return 1;
}
}//while arguments
if (productionName.size() > 0)
f_production = true;
//if grid job
// Set up the job for xAOD access:
xAOD::Init().ignore();
// Construct the samples to run on:
SH::SampleHandler sh;
std::string containerName;
std::string userName = getlogin();
if (f_production)
userName = productionName;
//Check if input is a directory or a file
struct stat buf;
stat(samplePath.c_str(), &buf);
if( samplePath.substr(0, 4).find("eos") != std::string::npos){
SH::DiskListEOS list(samplePath.c_str());
std::cout << "Running on EOS directory " << samplePath << " with tag " << inputTag << std::endl;
if (inputTag.size() > 0){
SH::scanDir (sh, list, inputTag); //Run on all files within dir containing inputTag
}else{
SH::scanDir (sh, list); //Run on all files within dir
}
}else if( S_ISDIR(buf.st_mode) ){ //if it is a local directory
SH::DiskListLocal list (samplePath);
SH::scanDir (sh, list); // Run on all files within dir
std::cout << "Running Locally on directory " << samplePath << std::endl;
} else { //if it is a file
if( samplePath.substr( samplePath.size()-4 ).find(".txt") != std::string::npos){ //It is a text file of samples
if( samplePath.find("grid") != std::string::npos ) //It is samples for the grid
f_grid = true;
std::ifstream inFile( samplePath );
while(std::getline(inFile, containerName) ){
if (containerName.size() > 1 && containerName.find("#") != 0 ){
std::cout << "Adding container " << containerName << std::endl;
if(f_grid){
SH::scanDQ2( sh, containerName);
//Add output container name to file of containers
//follows grid format: "user."+userName+".%in:name[1]%.%in:name[2]%.%in:name[3]%"+outputTag
int namePosition = 0;
namePosition = containerName.find_first_of(".", namePosition)+1;
namePosition = containerName.find_first_of(".", namePosition)+1;
namePosition = containerName.find_first_of(".", namePosition)+1;
outputContainerNames.push_back( ("user."+userName+"."+containerName.substr(0, namePosition)+outputTag+"/") );
}else{
//Get full path of file
char fullPath[300];
realpath( containerName.c_str(), fullPath );
std::string thisPath = fullPath;
//split into fileName and directory two levels above file
std::string fileName = thisPath.substr(containerName.find_last_of("/")+1);
thisPath = thisPath.substr(0, thisPath.find_last_of("/"));
thisPath = thisPath.substr(0, thisPath.find_last_of("/"));
std::cout << "path and filename are " << thisPath << " and " << fileName << std::endl;
SH::DiskListLocal list (thisPath);
//SH::SampleHandler sh_tmp;
//SH::scanDir (sh_tmp, list);
//sh.add( sh_tmp.findByName, ("*"+fileName).c_str() );
SH::scanDir (sh, list, fileName); // specifying one particular file for testing
}
}
}
}else{ //It is a single root file to run on
//Get full path of file
char fullPath[300];
realpath( samplePath.c_str(), fullPath );
std::string thisPath = fullPath;
//split into fileName and directory two levels above file
std::string fileName = thisPath.substr(thisPath.find_last_of("/")+1);
thisPath = thisPath.substr(0, thisPath.find_last_of("/"));
thisPath = thisPath.substr(0, thisPath.find_last_of("/"));
std::cout << "path and file " << thisPath << " and " << fileName << std::endl;
SH::DiskListLocal list (thisPath);
SH::scanDir (sh, list, fileName); // specifying one particular file for testing
}
}//it's a file
///////// Set output container name //////////////
if( outputTag.size() > 0)
outputTag = "."+outputTag+"/";
else
outputTag = "/";
if(f_grid){
if (f_production){
outputName = "group."+userName+".%in:name[1]%.%in:name[2]%"+outputTag;
//outputName = "group."+userName+".%in:name[1]%.%in:name[2]%.%in:name[3]%"+outputTag;
}else
outputName = "user."+userName+".%in:name[3]%.%in:name[4]%.%in:name[5]%"+outputTag;
}else
outputName = "%in:name%"+outputTag;
std::string replaceStr = "Charybdis2Pythia8EvtGen_A14CTEQ6L1_BH2_n6";
size_t index = outputName.find(replaceStr);
if (index != std::string::npos){
/* Make the replacement. */
std::cout << "Changing output Name from " << outputName;
outputName.replace(index, replaceStr.length(), "BH_n6");
std::cout << " to " << outputName;
std::cout << std::endl;
}else{
std::cout << "No Match " << outputName << std::endl;
}
// Set the name of the input TTree. It's always "CollectionTree" for xAOD files.
sh.setMetaString( "nc_tree", "CollectionTree" );
sh.setMetaString( "nc_excludeSite", "ANALY_RAL_SL6");
sh.setMetaString( "nc_grid_filter", "*");
sh.print();
job.sampleHandler( sh );
// To automatically delete submitDir
job.options()->setDouble(EL::Job::optRemoveSubmitDir, 1);
//
// Set the number of events
//
TEnv* config = new TEnv(gSystem->ExpandPathName( configName.c_str() ));
int nEvents = config->GetValue("MaxEvent", -1);
if(nEvents > 0)
job.options()->setDouble(EL::Job::optMaxEvents, nEvents);
return 0;
}
void submitJob(EL::Job& job, std::string outputName, std::string submitDir, bool f_grid, bool f_production, std::vector<std::string> outputContainerNames, bool f_condor = false){
if(!f_grid && !f_condor){
// Run the job using the local/direct driver:
EL::DirectDriver driver;
driver.options()->setString("nc_outputSampleName", outputName);
driver.submit( job, submitDir );
}else if(f_condor){
//std::cout << "Submitting in condor"<<std::endl;
//EL::CondorDriver driver;
//driver.options()->setBool(EL::Job::optBatchSharedFileSystem, false);
//driver.submit(job, submitDir);
}else{
EL::PrunDriver driver;
driver.options()->setString("nc_outputSampleName", outputName);
driver.options()->setString(EL::Job::optGridMergeOutput, "true");
if (f_production)
driver.options()->setString(EL::Job::optSubmitFlags, "--official");
//driver.options()->setDouble(EL::Job::optGridNFilesPerJob, 2);
//driver.options()->setString(EL::Job::optRootVer, "5.34.25");
//driver.options()->setString(EL::Job::optCmtConfig, "x86_64-slc6-gcc48-opt");
////driver.options()->setDouble("nc_nGBPerJob", 1);
//driver.options()->setString(EL::Job::optGridMergeOutput, "false");
//driver.options()->setDouble(EL::Job::optGridMemory,10240); // 10 GB
//driver.submit(job, submitDir); // with monitoring
driver.submitOnly(job, submitDir); //without monitoring
}
///// For grid, save list of ouput containers to the submission directory /////
std::ofstream fileList((submitDir+"/outputContainers.txt"), std::ios_base::out);
for( unsigned int iCont=0; iCont < outputContainerNames.size(); ++iCont){
fileList << outputContainerNames.at(iCont)+"\n";
}
fileList.close();
return;
}