DESY Hbb Analysis Framework
hlt18/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 ARGS = sys.argv
34 PSET = ARGS[1]
35 psetname, pset_ext = os.path.splitext(PSET)
36 SAMPLE = ARGS[2]
37 samplename, sample_ext = os.path.splitext(SAMPLE)
38 CAMPAIGN = ARGS[3] + '/' + psetname
39 if ARGSN == 5:
40  UNITS_PER_JOB = int(ARGS[4])
41 
42 if not ( os.path.isfile(PSET) and pset_ext == '.py' ):
43  print (R+"The given python config does not exist or it is not a python file"+W)
44  sys.exit()
45 
46 if not ( os.path.isfile(SAMPLE) and sample_ext == '.txt' ):
47  print (R+"The given sample list file does not exist or it is not a txt file"+W)
48  sys.exit()
49 
50 # Some parameter steering
51 PROCESS = samplename.split('/')[-1]
52 MYPATH = '/store/user/%s/' % (getUsernameFromSiteDB())
53 BASEOUTDIR = MYPATH+'Analysis/Ntuples/' + TYPE + '/' + CAMPAIGN
54 
55 dataset_list = 'samples/data/' + PROCESS + '.txt'
56 f_datasets = open(dataset_list,'r')
57 datasets = f_datasets.readlines()
58 
59 # _________________________________________________________________________
60 
61 if __name__ == '__main__':
62 
63  from CRABAPI.RawCommand import crabCommand
64  from CRABClient.ClientExceptions import ClientException
65  from httplib import HTTPException
66 
67  from Analysis.Ntuplizer.crabConfig import crabConfig
68  config = crabConfig()
69 
70 # ====== GENERAL
71  config.General.workArea += '_' + PROCESS
72  config.General.transferLogs = True
73 
74 
75 # ====== DATA
76  config.Data.splitting = 'LumiBased'
77  config.Data.unitsPerJob = UNITS_PER_JOB
78  config.Data.totalUnits = -1
79  config.Data.outLFNDirBase = BASEOUTDIR + '/'
80  config.Data.useParent = True
81 # config.Data.lumiMask = 'https://cms-service-dqm.web.cern.ch/cms-service-dqm/CAF/certification/Collisions17/13TeV/PromptReco/Cert_294927-306462_13TeV_PromptReco_Collisions17_JSON.txt'
82  config.Data.lumiMask = 'json_rates.txt'
83 # config.Data.inputDBS = 'https://cmsweb.cern.ch/dbs/prod/phys03/DBSReader/'
84 # config.Data.allowNonValidInputDataset = True # If dataset not valid yet, will run over valid files only
85  if RUN_RANGE != '':
86  config.Data.runRange = RUN_RANGE
87 
88 # ===== SITE
89 # config.Site.whitelist = ['T2_DE_DESY']
90 
91 # ====== JOBTYPE
92  config.JobType.numCores = 4
93  config.JobType.psetName = PSET
94 # 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']
95 
96 
97  for dataset in datasets:
98  dataset=dataset.replace(" ", "")
99  if dataset[0] == '#':
100  continue
101  dataset = dataset.split('\n')[0]
102  dataset_name = dataset.split('/')[1]
103  dataset_cond = dataset.split('/')[2]
104  dataset_tier = dataset.split('/')[3]
105 #
106  config.Data.inputDataset = dataset
107  config.Data.outputDatasetTag = dataset_cond
108 #
109  config.General.requestName = dataset_name
110  config.General.requestName += '_'+dataset_cond
111  if RUN_RANGE != '':
112  config.General.requestName += '_'+RUN_RANGE
113 
114  outtext = "Submitting dataset " + dataset + "..."
115  print (O+str(outtext)+W)
116 #
117 # crabCommand('submit', config = config, *CRABCMDOPTS.split())
118  crabCommand('submit', config = config)
119  print (O+"--------------------------------"+W)
120  print
121 
122 # _________________________________________________________________________