14 from FWCore.PythonUtilities.LumiList
import LumiList
17 from pprint
import pprint
18 from datetime
import datetime
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
89 query =
"file dataset=%(dataset)s run=%(run)i lumi=%(lumi)i | grep file.name" % event
90 jsondict = das_client.get_data(
'https://cmsweb.cern.ch', query, 0, 0,
False)
91 status = jsondict[
'status']
93 print "DAS query status: %s"%(status)
96 mongo_query = jsondict[
'mongo_query']
97 filters = mongo_query[
'filters']
98 data = jsondict[
'data']
102 file = [r
for r
in das_client.get_value(row, filters[
'grep'])][0]
103 if len(file) > 0
and not file
in files:
110 base = os.environ.get (
'CMSSW_BASE')
112 raise RuntimeError,
"CMSSW Environment not set"
113 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
115 if os.path.exists (retval):
117 base = os.environ.get (
'CMSSW_RELEASE_BASE')
118 retval =
"%s/src/PhysicsTools/Utilities/configuration/copyPickMerge_cfg.py" \
120 if os.path.exists (retval):
122 raise RuntimeError,
"Could not find copyPickMerge_cfg.py"
125 return '%s@%s' % (commands.getoutput (
'whoami'),
126 '.'.
join(commands.getoutput(
'hostname').
split(
'.')[-2:]))
129 date = datetime.now().strftime(
'%Y%m%d_%H%M%S')
132 crab[
'runEvent'] =
'%s_runEvents.txt' % base
134 crab[
'output'] =
'%s.root' % base
135 crab[
'crabcfg'] =
'%s_crab.py' % base
136 crab[
'json'] =
'%s.json' % base
137 crab[
'dataset'] = Event.dataset
138 crab[
'email'] = options.email
139 crab[
'WorkArea'] = date
140 if options.crabCondor:
141 crab[
'scheduler'] =
'condor'
144 crab[
'scheduler'] =
'remoteGlidein'
146 crab[
'useServer'] =
''
151 ## Edited By Raman Khurana
153 ## CRAB documentation : https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideCrab
155 ## CRAB 3 parameters : https://twiki.cern.ch/twiki/bin/view/CMSPublic/CRAB3ConfigurationFile#CRAB_configuration_parameters
157 ## Once you are happy with this file, please run
160 ## In CRAB3 the configuration file is in Python language. It consists of creating a Configuration object imported from the WMCore library:
162 from WMCore.Configuration import Configuration
163 config = Configuration()
165 ## Once the Configuration object is created, it is possible to add new sections into it with corresponding parameters
166 config.section_("General")
167 config.General.requestName = 'pickEvents'
168 config.General.workArea = 'crab_pickevents_%(WorkArea)s'
171 config.section_("JobType")
172 config.JobType.pluginName = 'Analysis'
173 config.JobType.psetName = '%(copyPickMerge)s'
174 config.JobType.pyCfgParams = ['eventsToProcess_load=%(runEvent)s', 'outputFile=%(output)s']
176 config.section_("Data")
177 config.Data.inputDataset = '%(dataset)s'
179 config.Data.inputDBS = 'global'
180 config.Data.splitting = 'LumiBased'
181 config.Data.unitsPerJob = 5
182 config.Data.lumiMask = '%(json)s'
183 #config.Data.publication = True
184 #config.Data.publishDbsUrl = 'phys03'
185 #config.Data.publishDataName = 'CRAB3_CSA_DYJets'
186 #config.JobType.allowNonProductionCMSSW=True
188 config.section_("Site")
189 ## Change site name accordingly
190 config.Site.storageSite = "T2_US_Wisconsin"
200 if __name__ ==
"__main__":
202 parser = optparse.OptionParser (
"Usage: %prog [options] dataset events_or_events.txt", description=
'''This program
203 facilitates picking specific events from a data set. For full details, please visit
204 https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents ''')
205 parser.add_option (
'--output', dest=
'base', type=
'string',
206 default=
'pickevents',
207 help=
'Base name to use for output files (root, JSON, run and event list, etc.; default "%default")')
208 parser.add_option (
'--runInteractive', dest=
'runInteractive', action=
'store_true',
209 help =
'Call "cmsRun" command if possible. Can take a long time.')
210 parser.add_option (
'--printInteractive', dest=
'printInteractive', action=
'store_true',
211 help =
'Print "cmsRun" command instead of running it.')
212 parser.add_option (
'--maxEventsInteractive', dest=
'maxEventsInteractive', type=
'int',
214 help =
'Maximum number of events allowed to be processed interactively.')
215 parser.add_option (
'--crab', dest=
'crab', action=
'store_true',
216 help =
'Force CRAB setup instead of interactive mode')
217 parser.add_option (
'--crabCondor', dest=
'crabCondor', action=
'store_true',
218 help =
'Tell CRAB to use Condor scheduler (FNAL or OSG sites).')
219 parser.add_option (
'--email', dest=
'email', type=
'string',
221 help=
"Specify email for CRAB (default '%s')" % email )
222 (options, args) = parser.parse_args()
229 if not options.email:
230 options.email = email
232 Event.dataset = args.pop(0)
233 commentRE = re.compile (
r'#.+$')
234 colonRE = re.compile (
r':')
236 if len (args) > 1
or colonRE.search (args[0]):
240 event = Event (piece)
242 raise RuntimeError,
"'%s' is not a proper event" % piece
243 eventList.append (event)
246 source = open(args[0],
'r')
248 line = commentRE.sub (
'', line)
252 print "Skipping '%s'." % line.strip()
254 eventList.append(event)
258 print "No events defined. Aborting."
261 if len (eventList) > options.maxEventsInteractive:
269 if options.runInteractive:
270 raise RuntimeError,
"This job cannot be run interactively, but rather by crab. Please call without the '--runInteractive' flag or increase the '--maxEventsInteractive' value."
271 runsAndLumis = [ (event.run, event.lumi)
for event
in eventList]
272 json = LumiList (lumis = runsAndLumis)
274 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
275 crabDict = setupCrabDict (options)
276 json.writeJSON (crabDict[
'json'])
277 target = open (crabDict[
'runEvent'],
'w')
278 target.write (
"%s\n" % eventsToProcess)
280 target = open (crabDict[
'crabcfg'],
'w')
281 target.write (crabTemplate % crabDict)
283 print "Please visit CRAB twiki for instructions on how to setup environment for CRAB:\nhttps://twiki.cern.ch/twiki/bin/viewauth/CMS/SWGuideCrab\n"
284 if options.crabCondor:
285 print "You are running on condor. Please make sure you have read instructions on\nhttps://twiki.cern.ch/twiki/bin/view/CMS/CRABonLPCCAF\n"
286 if not os.path.exists (
'%s/.profile' % os.environ.get(
'HOME')):
287 print "** WARNING: ** You are missing ~/.profile file. Please see CRABonLPCCAF instructions above.\n"
288 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
297 for event
in eventList:
298 eventFiles = getFileNames (event)
299 if eventFiles == [
'[]']:
300 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)
301 eventPurgeList.append( event )
303 files.extend( eventFiles )
305 for event
in eventPurgeList:
306 eventList.remove( event )
310 for filename
in files:
311 if filename
in fileSet:
313 fileSet.add (filename)
314 uniqueFiles.append (filename)
315 source =
','.join (uniqueFiles) +
'\n'
316 eventsToProcess =
','.
join(\
317 sorted( [
"%d:%d" % (event.run, event.event)
for event
in eventList ] ) )
318 command =
'edmCopyPickMerge outputFile=%s.root \\\n eventsToProcess=%s \\\n inputFiles=%s' \
319 % (options.base, eventsToProcess, source)
320 print "\n%s" % command
321 if options.runInteractive
and not options.printInteractive:
def getFileNames
Subroutines ##.
static std::string join(char **cmd)
void set(const std::string &name, int value)
set the flag, with a run-time name