9 from __future__
import print_function
15 from FWCore.PythonUtilities.LumiList
import LumiList
17 from pprint
import pprint
18 from datetime
import datetime
20 import Utilities.General.cmssw_das_client
as das_client
24 edmPickEvent.py dataset run1:lumi1:event1 run2:lumi2:event2 28 edmPickEvent.py dataset listOfEvents.txt 30 listOfEvents is a text file: 31 # this line is ignored as a comment 32 # since '#' is a valid comment character 33 run1 lumi_section1 event1 34 run2 lumi_section2 event2 42 run, lumi_section, and event are integers that you can get from 45 dataset: it just a name of the physics dataset, if you don't know exact name 46 you can provide a mask, e.g.: *QCD*RAW 48 For updated information see Wiki: 49 https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents 60 splitRE = re.compile (
r'[\s:,]+')
62 pieces = Event.splitRE.split (line.strip())
64 self[
'run'] =
int( pieces[0] )
65 self[
'lumi'] =
int( pieces[1] )
66 self[
'event'] =
int( pieces[2] )
67 self[
'dataset'] = Event.dataset
69 raise RuntimeError(
"Can not parse '%s' as Event object" \
71 if not self[
'dataset']:
72 print(
"No dataset is defined for '%s'. Aborting." % line.strip())
73 raise RuntimeError(
'Missing dataset')
79 return "run = %(run)i, lumi = %(lumi)i, event = %(event)i, dataset = %(dataset)s" % self
87 """Return files for given DAS query""" 88 if client ==
'das_client':
90 elif client ==
'dasgoclient':
93 for path
in os.getenv(
'PATH').
split(
':'):
94 if os.path.isfile(os.path.join(path,
'dasgoclient')):
99 """Return files for given DAS query via das_client""" 102 query =
"file dataset=%(dataset)s run=%(run)i lumi=%(lumi)i | grep file.name" % event
103 jsondict = das_client.get_data(query)
104 status = jsondict[
'status']
106 print(
"DAS query status: %s"%(status))
109 mongo_query = jsondict[
'mongo_query']
110 filters = mongo_query[
'filters']
111 data = jsondict[
'data']
115 file = [r
for r
in das_client.get_value(row, filters[
'grep'])][0]
116 if len(file) > 0
and not file
in files:
122 """Return files for given DAS query via dasgoclient""" 123 query =
"file dataset=%(dataset)s run=%(run)i lumi=%(lumi)i" % event
124 cmd = [
'dasgoclient',
'-query', query,
'-json']
125 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
127 err = proc.stderr.read()
129 print(
"DAS error: %s" % err)
131 for row
in json.load(proc.stdout):
132 for rec
in row.get(
'file', []):
133 fname = rec.get(
'name',
'')
139 base = os.environ.get (
'CMSSW_BASE')
141 raise RuntimeError(
"CMSSW Environment not set")
142 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
144 if os.path.exists (retval):
146 base = os.environ.get (
'CMSSW_RELEASE_BASE')
147 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
149 if os.path.exists (retval):
151 raise RuntimeError(
"Could not find copyPickMerge_cfg.py")
154 return '%s@%s' % (commands.getoutput (
'whoami'),
155 '.'.
join(commands.getoutput(
'hostname').
split(
'.')[-2:]))
158 date = datetime.now().strftime(
'%Y%m%d_%H%M%S')
161 crab[
'runEvent'] =
'%s_runEvents.txt' % base
163 crab[
'output'] =
'%s.root' % base
164 crab[
'crabcfg'] =
'%s_crab.py' % base
165 crab[
'json'] =
'%s.json' % base
166 crab[
'dataset'] = Event.dataset
167 crab[
'email'] = options.email
168 crab[
'WorkArea'] = date
169 if options.crabCondor:
170 crab[
'scheduler'] =
'condor' 173 crab[
'scheduler'] =
'remoteGlidein' 175 crab[
'useServer'] =
'' 180 ## Edited By Raman Khurana 182 ## CRAB documentation : https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideCrab 184 ## CRAB 3 parameters : https://twiki.cern.ch/twiki/bin/view/CMSPublic/CRAB3ConfigurationFile#CRAB_configuration_parameters 186 ## Once you are happy with this file, please run 189 ## In CRAB3 the configuration file is in Python language. It consists of creating a Configuration object imported from the WMCore library: 191 from WMCore.Configuration import Configuration 192 config = Configuration() 194 ## Once the Configuration object is created, it is possible to add new sections into it with corresponding parameters 195 config.section_("General") 196 config.General.requestName = 'pickEvents' 197 config.General.workArea = 'crab_pickevents_%(WorkArea)s' 200 config.section_("JobType") 201 config.JobType.pluginName = 'Analysis' 202 config.JobType.psetName = '%(copyPickMerge)s' 203 config.JobType.pyCfgParams = ['eventsToProcess_load=%(runEvent)s', 'outputFile=%(output)s'] 205 config.section_("Data") 206 config.Data.inputDataset = '%(dataset)s' 208 config.Data.inputDBS = 'global' 209 config.Data.splitting = 'LumiBased' 210 config.Data.unitsPerJob = 5 211 config.Data.lumiMask = '%(json)s' 212 #config.Data.publication = True 213 #config.Data.publishDbsUrl = 'phys03' 214 #config.Data.publishDataName = 'CRAB3_CSA_DYJets' 215 #config.JobType.allowNonProductionCMSSW=True 217 config.section_("Site") 218 ## Change site name accordingly 219 config.Site.storageSite = "T2_US_Wisconsin" 229 if __name__ ==
"__main__":
231 parser = optparse.OptionParser (
"Usage: %prog [options] dataset events_or_events.txt", description=
'''This program 232 facilitates picking specific events from a data set. For full details, please visit 233 https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents ''')
234 parser.add_option (
'--output', dest=
'base', type=
'string',
235 default=
'pickevents',
236 help=
'Base name to use for output files (root, JSON, run and event list, etc.; default "%default")')
237 parser.add_option (
'--runInteractive', dest=
'runInteractive', action=
'store_true',
238 help =
'Call "cmsRun" command if possible. Can take a long time.')
239 parser.add_option (
'--printInteractive', dest=
'printInteractive', action=
'store_true',
240 help =
'Print "cmsRun" command instead of running it.')
241 parser.add_option (
'--maxEventsInteractive', dest=
'maxEventsInteractive', type=
'int',
243 help =
'Maximum number of events allowed to be processed interactively.')
244 parser.add_option (
'--crab', dest=
'crab', action=
'store_true',
245 help =
'Force CRAB setup instead of interactive mode')
246 parser.add_option (
'--crabCondor', dest=
'crabCondor', action=
'store_true',
247 help =
'Tell CRAB to use Condor scheduler (FNAL or OSG sites).')
248 parser.add_option (
'--email', dest=
'email', type=
'string',
250 help=
"Specify email for CRAB (default '%s')" % email )
252 parser.add_option (
'--das-client', dest=
'das_cli', type=
'string',
254 help=
"Specify das client to use (default '%s')" % das_cli )
255 (options, args) = parser.parse_args()
262 if not options.email:
263 options.email = email
265 Event.dataset = args.pop(0)
266 commentRE = re.compile (
r'#.+$')
267 colonRE = re.compile (
r':')
269 if len (args) > 1
or colonRE.search (args[0]):
273 event = Event (piece)
275 raise RuntimeError(
"'%s' is not a proper event" % piece)
276 eventList.append (event)
279 source = open(args[0],
'r') 281 line = commentRE.sub (
'', line)
285 print(
"Skipping '%s'." % line.strip())
287 eventList.append(event)
291 print(
"No events defined. Aborting.")
294 if len (eventList) > options.maxEventsInteractive:
302 if options.runInteractive:
303 raise RuntimeError(
"This job cannot be run interactively, but rather by crab. Please call without the '--runInteractive' flag or increase the '--maxEventsInteractive' value.")
304 runsAndLumis = [ (event.run, event.lumi)
for event
in eventList]
305 json = LumiList (lumis = runsAndLumis)
307 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
308 crabDict = setupCrabDict (options)
309 json.writeJSON (crabDict[
'json'])
310 target = open (crabDict[
'runEvent'],
'w')
311 target.write (
"%s\n" % eventsToProcess)
313 target = open (crabDict[
'crabcfg'],
'w')
314 target.write (crabTemplate % crabDict)
316 print(
"Please visit CRAB twiki for instructions on how to setup environment for CRAB:\nhttps://twiki.cern.ch/twiki/bin/viewauth/CMS/SWGuideCrab\n")
317 if options.crabCondor:
318 print(
"You are running on condor. Please make sure you have read instructions on\nhttps://twiki.cern.ch/twiki/bin/view/CMS/CRABonLPCCAF\n")
319 if not os.path.exists (
'%s/.profile' % os.environ.get(
'HOME')):
320 print(
"** WARNING: ** You are missing ~/.profile file. Please see CRABonLPCCAF instructions above.\n")
321 print(
"Setup your environment for CRAB and edit %(crabcfg)s to make any desired changed. Then run:\n\ncrab submit -c %(crabcfg)s\n" % crabDict)
330 for event
in eventList:
332 if eventFiles == [
'[]']:
333 print(
"** WARNING: ** According to a DAS query, run = %i; lumi = %i; event = %i not contained in %s. Skipping."%(event.run,event.lumi,event.event,event.dataset))
334 eventPurgeList.append( event )
336 files.extend( eventFiles )
338 for event
in eventPurgeList:
339 eventList.remove( event )
343 for filename
in files:
344 if filename
in fileSet:
346 fileSet.add (filename)
347 uniqueFiles.append (filename)
348 source =
','.join (uniqueFiles) +
'\n' 349 eventsToProcess =
','.
join(\
350 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
351 command =
'edmCopyPickMerge outputFile=%s.root \\\n eventsToProcess=%s \\\n inputFiles=%s' \
352 % (options.base, eventsToProcess, source)
353 print(
"\n%s" % command)
354 if options.runInteractive
and not options.printInteractive:
def getFileNames_dasgoclient(event)
def getFileNames(event, client=None)
Subroutines ##.
S & print(S &os, JobReport::InputFile const &f)
def __getattr__(self, key)
static std::string join(char **cmd)
def getFileNames_das_client(event)
def __init__(self, line, kwargs)
def setupCrabDict(options)