14 from FWCore.PythonUtilities.LumiList
import LumiList
16 from pprint
import pprint
17 from datetime
import datetime
18 import Utilities.General.cmssw_das_client
as das_client
22 edmPickEvent.py dataset run1:lumi1:event1 run2:lumi2:event2 26 edmPickEvent.py dataset listOfEvents.txt 28 listOfEvents is a text file: 29 # this line is ignored as a comment 30 # since '#' is a valid comment character 31 run1 lumi_section1 event1 32 run2 lumi_section2 event2 40 run, lumi_section, and event are integers that you can get from 43 dataset: it just a name of the physics dataset, if you don't know exact name 44 you can provide a mask, e.g.: *QCD*RAW 46 For updated information see Wiki: 47 https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents 58 splitRE = re.compile (
r'[\s:,]+')
60 pieces = Event.splitRE.split (line.strip())
62 self[
'run'] =
int( pieces[0] )
63 self[
'lumi'] =
int( pieces[1] )
64 self[
'event'] =
int( pieces[2] )
65 self[
'dataset'] = Event.dataset
67 raise RuntimeError(
"Can not parse '%s' as Event object" \
69 if not self[
'dataset']:
70 print "No dataset is defined for '%s'. Aborting." % line.strip()
71 raise RuntimeError(
'Missing dataset')
77 return "run = %(run)i, lumi = %(lumi)i, event = %(event)i, dataset = %(dataset)s" % self
87 query =
"file dataset=%(dataset)s run=%(run)i lumi=%(lumi)i | grep file.name" % event
88 jsondict = das_client.get_data(query)
89 status = jsondict[
'status']
91 print "DAS query status: %s"%(status)
94 mongo_query = jsondict[
'mongo_query']
95 filters = mongo_query[
'filters']
96 data = jsondict[
'data']
100 file = [r
for r
in das_client.get_value(row, filters[
'grep'])][0]
101 if len(file) > 0
and not file
in files:
108 base = os.environ.get (
'CMSSW_BASE')
110 raise RuntimeError(
"CMSSW Environment not set")
111 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
113 if os.path.exists (retval):
115 base = os.environ.get (
'CMSSW_RELEASE_BASE')
116 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
118 if os.path.exists (retval):
120 raise RuntimeError(
"Could not find copyPickMerge_cfg.py")
123 return '%s@%s' % (commands.getoutput (
'whoami'),
124 '.'.
join(commands.getoutput(
'hostname').
split(
'.')[-2:]))
127 date = datetime.now().strftime(
'%Y%m%d_%H%M%S')
130 crab[
'runEvent'] =
'%s_runEvents.txt' % base
132 crab[
'output'] =
'%s.root' % base
133 crab[
'crabcfg'] =
'%s_crab.py' % base
134 crab[
'json'] =
'%s.json' % base
135 crab[
'dataset'] = Event.dataset
136 crab[
'email'] = options.email
137 crab[
'WorkArea'] = date
138 if options.crabCondor:
139 crab[
'scheduler'] =
'condor' 142 crab[
'scheduler'] =
'remoteGlidein' 144 crab[
'useServer'] =
'' 149 ## Edited By Raman Khurana 151 ## CRAB documentation : https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideCrab 153 ## CRAB 3 parameters : https://twiki.cern.ch/twiki/bin/view/CMSPublic/CRAB3ConfigurationFile#CRAB_configuration_parameters 155 ## Once you are happy with this file, please run 158 ## In CRAB3 the configuration file is in Python language. It consists of creating a Configuration object imported from the WMCore library: 160 from WMCore.Configuration import Configuration 161 config = Configuration() 163 ## Once the Configuration object is created, it is possible to add new sections into it with corresponding parameters 164 config.section_("General") 165 config.General.requestName = 'pickEvents' 166 config.General.workArea = 'crab_pickevents_%(WorkArea)s' 169 config.section_("JobType") 170 config.JobType.pluginName = 'Analysis' 171 config.JobType.psetName = '%(copyPickMerge)s' 172 config.JobType.pyCfgParams = ['eventsToProcess_load=%(runEvent)s', 'outputFile=%(output)s'] 174 config.section_("Data") 175 config.Data.inputDataset = '%(dataset)s' 177 config.Data.inputDBS = 'global' 178 config.Data.splitting = 'LumiBased' 179 config.Data.unitsPerJob = 5 180 config.Data.lumiMask = '%(json)s' 181 #config.Data.publication = True 182 #config.Data.publishDbsUrl = 'phys03' 183 #config.Data.publishDataName = 'CRAB3_CSA_DYJets' 184 #config.JobType.allowNonProductionCMSSW=True 186 config.section_("Site") 187 ## Change site name accordingly 188 config.Site.storageSite = "T2_US_Wisconsin" 198 if __name__ ==
"__main__":
200 parser = optparse.OptionParser (
"Usage: %prog [options] dataset events_or_events.txt", description=
'''This program 201 facilitates picking specific events from a data set. For full details, please visit 202 https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents ''')
203 parser.add_option (
'--output', dest=
'base', type=
'string',
204 default=
'pickevents',
205 help=
'Base name to use for output files (root, JSON, run and event list, etc.; default "%default")')
206 parser.add_option (
'--runInteractive', dest=
'runInteractive', action=
'store_true',
207 help =
'Call "cmsRun" command if possible. Can take a long time.')
208 parser.add_option (
'--printInteractive', dest=
'printInteractive', action=
'store_true',
209 help =
'Print "cmsRun" command instead of running it.')
210 parser.add_option (
'--maxEventsInteractive', dest=
'maxEventsInteractive', type=
'int',
212 help =
'Maximum number of events allowed to be processed interactively.')
213 parser.add_option (
'--crab', dest=
'crab', action=
'store_true',
214 help =
'Force CRAB setup instead of interactive mode')
215 parser.add_option (
'--crabCondor', dest=
'crabCondor', action=
'store_true',
216 help =
'Tell CRAB to use Condor scheduler (FNAL or OSG sites).')
217 parser.add_option (
'--email', dest=
'email', type=
'string',
219 help=
"Specify email for CRAB (default '%s')" % email )
220 (options, args) = parser.parse_args()
227 if not options.email:
228 options.email = email
230 Event.dataset = args.pop(0)
231 commentRE = re.compile (
r'#.+$')
232 colonRE = re.compile (
r':')
234 if len (args) > 1
or colonRE.search (args[0]):
238 event = Event (piece)
240 raise RuntimeError(
"'%s' is not a proper event" % piece)
241 eventList.append (event)
244 source = open(args[0],
'r') 246 line = commentRE.sub (
'', line)
250 print "Skipping '%s'." % line.strip()
252 eventList.append(event)
256 print "No events defined. Aborting." 259 if len (eventList) > options.maxEventsInteractive:
267 if options.runInteractive:
268 raise RuntimeError(
"This job cannot be run interactively, but rather by crab. Please call without the '--runInteractive' flag or increase the '--maxEventsInteractive' value.")
269 runsAndLumis = [ (event.run, event.lumi)
for event
in eventList]
270 json = LumiList (lumis = runsAndLumis)
272 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
273 crabDict = setupCrabDict (options)
274 json.writeJSON (crabDict[
'json'])
275 target = open (crabDict[
'runEvent'],
'w')
276 target.write (
"%s\n" % eventsToProcess)
278 target = open (crabDict[
'crabcfg'],
'w')
279 target.write (crabTemplate % crabDict)
281 print "Please visit CRAB twiki for instructions on how to setup environment for CRAB:\nhttps://twiki.cern.ch/twiki/bin/viewauth/CMS/SWGuideCrab\n" 282 if options.crabCondor:
283 print "You are running on condor. Please make sure you have read instructions on\nhttps://twiki.cern.ch/twiki/bin/view/CMS/CRABonLPCCAF\n" 284 if not os.path.exists (
'%s/.profile' % os.environ.get(
'HOME')):
285 print "** WARNING: ** You are missing ~/.profile file. Please see CRABonLPCCAF instructions above.\n" 286 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
295 for event
in eventList:
296 eventFiles = getFileNames (event)
297 if eventFiles == [
'[]']:
298 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)
299 eventPurgeList.append( event )
301 files.extend( eventFiles )
303 for event
in eventPurgeList:
304 eventList.remove( event )
308 for filename
in files:
309 if filename
in fileSet:
311 fileSet.add (filename)
312 uniqueFiles.append (filename)
313 source =
','.join (uniqueFiles) +
'\n' 314 eventsToProcess =
','.
join(\
315 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
316 command =
'edmCopyPickMerge outputFile=%s.root \\\n eventsToProcess=%s \\\n inputFiles=%s' \
317 % (options.base, eventsToProcess, source)
318 print "\n%s" % command
319 if options.runInteractive
and not options.printInteractive:
def getFileNames(event)
Subroutines ##.
def __getattr__(self, key)
static std::string join(char **cmd)
def __init__(self, line, kwargs)
def setupCrabDict(options)