DESY Hbb Analysis Framework
submitCrab3Data.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os.path
4 import urllib2
5 import sys
6 from WMCore.Configuration import Configuration
7 from CRABClient.UserUtilities import getUsernameFromSiteDB
8 
9 # colors
10 W = '\033[0m' # white (normal)
11 R = '\033[31m' # red
12 G = '\033[32m' # green
13 O = '\033[33m' # orange
14 B = '\033[34m' # blue
15 P = '\033[35m' # purple
16 
17 
18 ARGSN = len(sys.argv)
19 if ARGSN < 4:
20  print (R+"You need to provide the CMSSW python config, the samples file and the campaign, e.g Run2017, in this order."+W)
21  print (R+"Optionally you can provide the UNITS_PER_JOB (def. 500)"+W)
22  sys.exit()
23 
24 # ---
25 # Some parameter steering
26 RUN_RANGE = ''
27 UNITS_PER_JOB = 500
28 TYPE = 'DATA'
29 
30 #CRABCMDOPTS = '--dryrun'
31 CRABCMDOPTS = ''
32 
33 IS_NANO = False
34 
35 ARGS = sys.argv
36 PSET = ARGS[1]
37 if PSET.find('nano') >= 0:
38  IS_NANO = True
39  print "Producing NanoAOD ntuples..."
40 else:
41  print "Producing Ntuplizer ntuples..."
42 
43 
44 psetname, pset_ext = os.path.splitext(PSET)
45 SAMPLE = ARGS[2]
46 
47 samplename, sample_ext = os.path.splitext(SAMPLE)
48 CAMPAIGN = ARGS[3] + '/' + psetname
49 if ARGSN == 5:
50  UNITS_PER_JOB = int(ARGS[4])
51 
52 
53 if not ( os.path.isfile(PSET) and pset_ext == '.py' ):
54  print (R+"The given python config does not exist or it is not a python file"+W)
55  sys.exit()
56 
57 if not ( os.path.isfile(SAMPLE) and sample_ext == '.txt' ):
58  print (R+"The given sample list file does not exist or it is not a txt file"+W)
59  sys.exit()
60 
61 # Some parameter steering
62 PROCESS = samplename.split('/')[-1]
63 MYPATH = '/store/user/rwalsh/'
64 #MYPATH = '/store/user/%s/' % (getUsernameFromSiteDB())
65 BASEOUTDIR = MYPATH+'Analysis/Ntuples/' + TYPE + '/' + CAMPAIGN
66 
67 dataset_list = 'samples/data/' + PROCESS + '.txt'
68 f_datasets = open(dataset_list,'r')
69 datasets = f_datasets.readlines()
70 
71 # _________________________________________________________________________
72 
73 if __name__ == '__main__':
74 
75  from CRABAPI.RawCommand import crabCommand
76  from CRABClient.ClientExceptions import ClientException
77  from httplib import HTTPException
78 
79  from Analysis.Ntuplizer.crabConfig import crabConfig
80  config = crabConfig()
81 
82 # ====== GENERAL
83  config.General.workArea += '_' + PROCESS
84 
85 
86 # ====== DATA
87 # config.Data.splitting = 'Automatic'
88 # config.Data.unitsPerJob = UNITS_PER_JOB
89 # config.Data.totalUnits = -1
90  config.Data.outLFNDirBase = BASEOUTDIR + '/'
91 # config.Data.lumiMask = 'https://cms-service-dqm.web.cern.ch/cms-service-dqm/CAF/certification/Collisions18/13TeV/DCSOnly/json_DCSONLY.txt'
92 # config.Data.inputDBS = 'https://cmsweb.cern.ch/dbs/prod/phys03/DBSReader/'
93 # config.Data.allowNonValidInputDataset = True # If dataset not valid yet, will run over valid files only
94  if RUN_RANGE != '':
95  config.Data.runRange = RUN_RANGE
96 
97 
98 # ====== JOBTYPE
99  config.JobType.psetName = PSET
100  config.JobType.numCores = 4
101  config.JobType.maxMemoryMB = 10000
102 # config.JobType.inputFiles = ['Fall15_25nsV2_DATA_PtResolution_AK4PFPuppi.txt','Fall15_25nsV2_DATA_PtResolution_AK4PFchs.txt','Fall15_25nsV2_DATA_SF_AK4PFPuppi.txt','Fall15_25nsV2_DATA_SF_AK4PFchs.txt']
103 
104 
105  if IS_NANO:
106  config.JobType.outputFiles = ['nano.root']
107 
108  for dataset in datasets:
109  dataset=dataset.replace(" ", "")
110  if dataset[0] == '#':
111  continue
112  dataset = dataset.split('\n')[0]
113  dataset_name = dataset.split('/')[1]
114  dataset_cond = dataset.split('/')[2]
115  dataset_tier = dataset.split('/')[3]
116 #
117  config.Data.inputDataset = dataset
118  config.Data.outputDatasetTag = dataset_cond
119 #
120  config.General.requestName = dataset_name
121  config.General.requestName += '_'+dataset_cond
122  if RUN_RANGE != '':
123  config.General.requestName += '_'+RUN_RANGE
124 
125  outtext = "Submitting dataset " + dataset + "..."
126  print (O+str(outtext)+W)
127 #
128 # crabCommand('submit', config = config, *CRABCMDOPTS.split())
129  crabCommand('submit', config = config)
130  print (O+"--------------------------------"+W)
131  print
132 
133 # _________________________________________________________________________